Subversion Repositories gelsvn

Rev

Rev 632 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
595 jab 1
/* ----------------------------------------------------------------------- *
2
 * This file is part of GEL, http://www.imm.dtu.dk/GEL
3
 * Copyright (C) the authors and DTU Informatics
4
 * For license and list of authors, see ../../doc/intro.pdf
5
 * ----------------------------------------------------------------------- */
6
 
7
/**
8
 * @file mesh_optimization.h
9
 * @brief Functions for energy minimization based mesh optimization.
10
 */
11
 
448 jab 12
#ifndef __HMESH_MESH_OPTIMIZATION_H
13
#define __HMESH_MESH_OPTIMIZATION_H
149 jab 14
 
636 khor 15
#include <algorithm>
595 jab 16
#include "Manifold.h"
601 jab 17
#include "../CGLA/Vec3d.h"
150 jab 18
namespace HMesh
149 jab 19
{
595 jab 20
    // forward declarations
21
    //class Manifold;
22
    //class HalfEdgeID;
149 jab 23
 
595 jab 24
    /// This class represents the energy of an edge. It is used in optimization schemes where edges are swapped (aka flipped). */
25
    class EnergyFun
26
    {
27
    public:
28
        virtual double delta_energy(const Manifold& m, HalfEdgeID h) const = 0;
29
        virtual double energy(const Manifold& m, HalfEdgeID h) const {return 0;}
30
    };
31
 
32
	class MinAngleEnergy: public EnergyFun
33
	{
34
		double min_angle(const CGLA::Vec3d& v0, const CGLA::Vec3d& v1, const CGLA::Vec3d& v2) const;
35
		double thresh;
36
 
37
	public:
38
 
39
		MinAngleEnergy(double _thresh): thresh(_thresh) {}
40
 
41
		double delta_energy(const HMesh::Manifold& m, HMesh::HalfEdgeID h) const;
42
	};
43
 
44
	class DihedralEnergy: public EnergyFun
45
	{
46
		const double gamma;
47
		const bool use_alpha;
48
 
49
		double cos_ang(const CGLA::Vec3d& n1, const CGLA::Vec3d& n2) const
636 khor 50
		{ 
51
			return (std::max)(-1.0, (std::min)(1.0, CGLA::dot(n1, n2)));
595 jab 52
		}
53
 
54
		double edge_alpha_energy(CGLA::Vec3d v1, CGLA::Vec3d v2, double ca) const
55
		{
56
			return pow(CGLA::length(v1-v2)*(acos(ca)), 1.0f/gamma); 
57
		}
58
 
59
		double edge_c_energy(CGLA::Vec3d v1, CGLA::Vec3d v2, double ca) const
60
		{
61
			return pow(length(v1-v2)*(1-ca), 1.0f/gamma); 
62
		}
63
 
64
		void compute_angles(const HMesh::Manifold & m, HMesh::HalfEdgeID h) const;
65
 
66
 
67
 
68
		mutable double ab_12;
69
		mutable double ab_a1;
70
		mutable double ab_b1;
71
		mutable double ab_2c;
72
		mutable double ab_2d;
73
 
74
		mutable double aa_12;
75
		mutable double aa_b1;
76
		mutable double aa_c1;
77
		mutable double aa_2a;
78
		mutable double aa_2d;
79
 
80
	public:
81
 
82
		DihedralEnergy(double _gamma = 4.0, bool _use_alpha=false): 
83
		gamma(_gamma), use_alpha(_use_alpha) {}
84
 
85
		double energy(const HMesh::Manifold& m, HMesh::HalfEdgeID h) const;
86
 
87
		double delta_energy(const HMesh::Manifold& m, HMesh::HalfEdgeID h) const;
88
 
89
		double min_angle(const HMesh::Manifold& m, HMesh::HalfEdgeID h) const
90
		{
91
			compute_angles(m, h);
636 khor 92
			return (std::min)((std::min)((std::min)((std::min)(aa_12, aa_b1), aa_c1), aa_2a), aa_2d);
595 jab 93
		}		
94
	};
95
 
96
	class CurvatureEnergy: public EnergyFun
97
	{
98
		double abs_mean_curv(const CGLA::Vec3d& v, const std::vector<CGLA::Vec3d>& ring) const;
99
	public:
100
		double delta_energy(const HMesh::Manifold& m, HMesh::HalfEdgeID h) const;
101
	};		
149 jab 102
 
595 jab 103
    class ValencyEnergy: public EnergyFun
104
	{
105
	public:
106
		double delta_energy(const Manifold& m, HalfEdgeID h) const;
107
	};
149 jab 108
 
595 jab 109
 
110
    /// Optimize in a greedy fashion.
111
    void priority_queue_optimization(Manifold& m, const EnergyFun& efun);
112
 
113
    /// Optimize with simulated annealing. Avoids getting trapped in local minima
114
    void simulated_annealing_optimization(Manifold& m, const EnergyFun& efun, int max_iter=10000);
115
 
116
    /// Minimize the angle between adjacent triangles. Almost the same as mean curvature minimization 
117
    void minimize_dihedral_angle(Manifold& m, int max_iter=10000, bool anneal=false, bool alpha=false, double gamma=4.0);
118
 
119
    /// Minimizes mean curvature. This is really the same as dihedral angle optimization except that we weight by edge length 
120
    void minimize_curvature(Manifold& m, bool anneal=false);
121
 
122
    /// Minimizes gaussian curvature. Probably less useful than mean curvature.
123
    void minimize_gauss_curvature(Manifold& m, bool anneal=false);
124
 
125
    /// Maximizes the minimum angle of triangles. Makes the mesh more Delaunay.
126
    void maximize_min_angle(Manifold& m, float thresh, bool anneal=false);
127
 
128
    /// Tries to achieve valence 6 internally and 4 along edges.
129
    void optimize_valency(Manifold& m, bool anneal=false);
130
 
131
    /// Make radom flips. Useful for generating synthetic test cases.
132
    void randomize_mesh(Manifold& m, int max_iter);
133
 
149 jab 134
}
135
 
136
 
137
#endif