Subversion Repositories gelsvn

Rev

Rev 182 | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 182 Rev 215
Line 12... Line 12...
12
 
12
 
13
namespace HMesh
13
namespace HMesh
14
{
14
{
15
	namespace
15
	namespace
16
	{
16
	{
17
		float min_angle(const Vec3f& v0,const Vec3f& v1,const Vec3f& v2)
17
		float min_angle(const Vec3d& v0,const Vec3d& v1,const Vec3d& v2)
18
		{
18
		{
19
			Vec3f a = normalize(v1-v0);
19
			Vec3d a = normalize(v1-v0);
20
			Vec3f b = normalize(v2-v1);
20
			Vec3d b = normalize(v2-v1);
21
			Vec3f c = normalize(v0-v2);
21
			Vec3d c = normalize(v0-v2);
22
			
22
			
23
			return min(dot(a,-c), min(dot(b,-a), dot(c,-b)));
23
			return min(dot(a,-c), min(dot(b,-a), dot(c,-b)));
24
		}
24
		}
25
 
25
 
26
		float edge_error(HalfEdgeIter h, 
26
		float edge_error(HalfEdgeIter h, 
27
										 const Vec3f& pa,
27
										 const Vec3d& pa,
28
										 const Vec3f& pb)
28
										 const Vec3d& pb)
29
		{
29
		{
30
			QEM q;
30
			QEM q;
31
			if(h->face != NULL_FACE_ITER)
31
			if(h->face != NULL_FACE_ITER)
32
				q += QEM(Vec3d(0), Vec3d(normal(h->face)));
32
				q += QEM(Vec3d(0), Vec3d(normal(h->face)));
33
			if(h->opp->face != NULL_FACE_ITER)
33
			if(h->opp->face != NULL_FACE_ITER)
34
				q += QEM(Vec3d(0), Vec3d(normal(h->opp->face)));
34
				q += QEM(Vec3d(0), Vec3d(normal(h->opp->face)));
35
			return q.error(pb-pa);
35
			return q.error(pb-pa);
36
		}
36
		}
37
 
37
 
38
 
38
 
39
		float vertex_error(VertexIter va, const Vec3f& pb)
39
		float vertex_error(VertexIter va, const Vec3d& pb)
40
		{
40
		{
41
			QEM q;
41
			QEM q;
42
			Vec3f pa = va->pos;
42
			Vec3d pa(va->pos);
43
			for(VertexCirculator vc(va); !vc.end(); ++vc)
43
			for(VertexCirculator vc(va); !vc.end(); ++vc)
44
				{
44
				{
45
					FaceIter f = vc.get_face();
45
					FaceIter f = vc.get_face();
46
					if(f != NULL_FACE_ITER)
46
					if(f != NULL_FACE_ITER)
47
						{
47
						{
48
							Vec3f n = normal(f);
48
							Vec3d n(normal(f));
49
							q += QEM(Vec3d(0), Vec3d(n));
49
							q += QEM(Vec3d(0), Vec3d(n));
50
						}
50
						}
51
				}
51
				}
52
			return q.error(pb-pa);
52
			return q.error(pb-pa);
53
		}
53
		}
Line 56... Line 56...
56
 
56
 
57
	void remove_caps_from_trimesh(Manifold& mani, float ang_thresh)
57
	void remove_caps_from_trimesh(Manifold& mani, float ang_thresh)
58
	{
58
	{
59
		for(FaceIter fi=mani.faces_begin(); fi != mani.faces_end(); ++fi)
59
		for(FaceIter fi=mani.faces_begin(); fi != mani.faces_end(); ++fi)
60
			{
60
			{
61
				Vec3f p[3];
61
				Vec3d p[3];
62
				HalfEdgeIter he[3];
62
				HalfEdgeIter he[3];
63
				VertexIter vi[3];
63
				VertexIter vi[3];
64
 
64
 
65
				int n=0;
65
				int n=0;
66
				for(FaceCirculator fc(fi); !fc.end(); ++fc,++n)
66
				for(FaceCirculator fc(fi); !fc.end(); ++fc,++n)
67
					{
67
					{
68
						vi[n] = fc.get_vertex();
68
						vi[n] = fc.get_vertex();
69
						p[n]  = fc.get_vertex()->pos;
69
						p[n]  = Vec3d(fc.get_vertex()->pos);
70
						he[n]= fc.get_halfedge();
70
						he[n]= fc.get_halfedge();
71
					}
71
					}
72
				assert(n=3);
72
				assert(n=3);
73
				
73
				
74
				bool is_collapsed = false;
74
				bool is_collapsed = false;
75
				Vec3f edges[3];
75
				Vec3d edges[3];
76
				for(int i=0;i<3;++i)
76
				for(int i=0;i<3;++i)
77
					{
77
					{
78
						edges[i] = p[(i+1)%3]-p[i];
78
						edges[i] = p[(i+1)%3]-p[i];
79
						float l = length(edges[i]);
79
						float l = length(edges[i]);
80
						if(l<1e-20)
80
						if(l<1e-20)
Line 84... Line 84...
84
					}
84
					}
85
				if(is_collapsed) continue;
85
				if(is_collapsed) continue;
86
				
86
				
87
				for(int i=0;i<3;++i)
87
				for(int i=0;i<3;++i)
88
					{
88
					{
89
						float d = max(-1.0f,min(1.0f,dot(-edges[(i+2)%3],edges[i])));
89
							float d = max(-1.0,min(1.0,dot(-edges[(i+2)%3],edges[i])));
90
						float ang = acos(d);
90
						float ang = acos(d);
91
						if(ang > ang_thresh)
91
						if(ang > ang_thresh)
92
							{
92
							{
93
								int iplus1 = (i+1)%3;
93
								int iplus1 = (i+1)%3;
94
								Vec3f edge_dir = edges[iplus1];
94
								Vec3d edge_dir = edges[iplus1];
95
								Vec3f pprj =  
95
								Vec3d pprj =  
96
									edge_dir * dot(edge_dir,p[i]-p[iplus1])+p[iplus1];
96
									edge_dir * dot(edge_dir,p[i]-p[iplus1])+p[iplus1];
97
 
97
 
98
								HalfEdgeIter h = he[iplus1];
98
								HalfEdgeIter h = he[iplus1];
99
								Vec3f v0 = h->vert->pos;
99
								Vec3d v0(h->vert->pos);
100
								Vec3f v1 = h->next->vert->pos;
100
								Vec3d v1(h->next->vert->pos);
101
								Vec3f v2 = h->opp->vert->pos;
101
								Vec3d v2(h->opp->vert->pos);
102
								Vec3f v3 = h->opp->next->vert->pos;
102
								Vec3d v3(h->opp->next->vert->pos);
103
								
103
								
104
								float m1 = min(min_angle(v0,v1,v2), min_angle(v0,v2,v3));
104
								float m1 = min(min_angle(v0,v1,v2), min_angle(v0,v2,v3));
105
								float m2 = min(min_angle(v0,pprj,v3), min_angle(pprj,v2,v3));
105
								float m2 = min(min_angle(v0,pprj,v3), min_angle(pprj,v2,v3));
106
								if(m1 < m2)
106
								if(m1 < m2)
107
									{
107
									{
108
											if(edge_error(he[iplus1], pprj, vi[i]->pos) >
108
											if(edge_error(he[iplus1], pprj, Vec3d(vi[i]->pos)) >
109
											 vertex_error(vi[i], pprj))
109
											 vertex_error(vi[i], pprj))
110
												vi[i]->pos = pprj;
110
												vi[i]->pos = Vec3f(pprj);
111
 
111
 
112
										mani.flip(he[iplus1]);
112
										mani.flip(he[iplus1]);
113
										break;
113
										break;
114
									}
114
									}
115
							}
115
							}
Line 138... Line 138...
138
 
138
 
139
								VertexIter n = vc.get_vertex();
139
								VertexIter n = vc.get_vertex();
140
								float dist = length(he);
140
								float dist = length(he);
141
								if((dist<thresh) && mani.collapse_precond(he))
141
								if((dist<thresh) && mani.collapse_precond(he))
142
									{
142
									{
143
											if(vertex_error(vi,n->pos) < 
143
											if(vertex_error(vi,Vec3d(n->pos)) < 
144
											 vertex_error(n,vi->pos))
144
												 vertex_error(n,Vec3d(vi->pos)))
145
													vi->pos = n->pos;
145
													vi->pos = n->pos;
146
 
146
 
147
										mani.collapse_halfedge(he);
147
										mani.collapse_halfedge(he);
148
										did_work = true;
148
										did_work = true;
149
										break;
149
										break;