particle_cell_2DandHalf.hpp 8.81 KB
Newer Older
Thomas's avatar
Thomas committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
//#define DEBUG

template <typename PFP>
void ParticleCell2DAndHalf<PFP>::display()
{
// 	CGoGNout << "pos : " << this->m_position << CGoGNendl;
// 	CGoGNout << "d : " << this->d << CGoGNendl;
// 	CGoGNout << "state : " << this->state << CGoGNendl;
}

template <typename PFP>
typename PFP::VEC3 ParticleCell2DAndHalf<PFP>::pointInFace(Dart d)
{
	const VEC3& p1(m_positions[d]);
	Dart dd=m.phi1(d);
	const VEC3& p2(m_positions[dd]);
	dd=m.phi1(dd);
	VEC3& p3(m_positions[dd]);

	VEC3 v1(p2-p1);

	while((v1^VEC3(p3-p1)).norm2()==0.0f)
	{
		dd = m.phi1(dd);
		p3 = m_positions[dd];
	}

	CGoGNout << "pointInFace " << (p1+p3)*0.5f << CGoGNendl;

	return (p1+p3)*0.5f;
}

template <typename PFP>
Geom::Orientation3D ParticleCell2DAndHalf<PFP>::getOrientationEdge(const VEC3& point, Dart d)
{
	const VEC3& endPoint = m_positions[m.phi2(d)];
	const VEC3& vertexPoint = m_positions[d];

	const VEC3& n1 = Algo::Geometry::faceNormal<PFP>(m,d,m_positions);

	//orientation relative to the plane orthogonal to the face going through the edge
Thomas's avatar
Thomas committed
42
	return Geom::testOrientation3D(point,vertexPoint, endPoint, vertexPoint+n1);
Thomas's avatar
Thomas committed
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
}

template <typename PFP>
typename PFP::VEC3 ParticleCell2DAndHalf<PFP>::intersectLineEdge(const VEC3& pA, const VEC3& pB, Dart d)
{
	const VEC3& q1 = m_positions[d];
	const VEC3& q2 = m_positions[m.phi2(d)];
	VEC3 Inter;

	VEC3 n1 = Algo::Geometry::faceNormal<PFP>(m,d,m_positions);
	VEC3 n = (q2-q1) ^ n1 ;

	Geom::intersectionLinePlane(pA,pB-pA,q1,n,Inter);

	Geom::Plane3D<float> pl = Algo::Geometry::facePlane<PFP>(m,d,m_positions);
	pl.project(Inter);

	return Inter;
}

template <typename PFP>
Geom::Orientation3D ParticleCell2DAndHalf<PFP>::getOrientationFace(VEC3 point, VEC3 sourcePoint, Dart d)
{
	const VEC3& dPoint = m_positions[d];

	VEC3 n1 = Algo::Geometry::faceNormal<PFP>(m,d,m_positions);

Thomas's avatar
Thomas committed
70
	return Geom::testOrientation3D(point, sourcePoint, dPoint+n1, dPoint);
Thomas's avatar
Thomas committed
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
}

template <typename PFP>
void ParticleCell2DAndHalf<PFP>::vertexState(VEC3 current)
{
	#ifdef DEBUG
	CGoGNout << "vertexState" << d << CGoGNendl;
	#endif
	assert(std::isfinite(current[0]) && std::isfinite(current[1]) && std::isfinite(current[2]));

	crossCell = CROSS_OTHER;

	if(Algo::Geometry::isPointOnVertex<PFP>(m,d,m_positions,current))
	{
		state = VERTEX;
		m_position = current;
		return;
	}
	else
	{
		//orientation step
		if(m_positions[d][0] == m_positions[m.phi1(d)][0] && m_positions[d][1] == m_positions[m.phi1(d)][1])
			d = m.alpha1(d);
		if(getOrientationEdge(current,m.alpha1(d)) != Geom::UNDER)
		{
			Dart dd_vert = d;
			do
			{
				d = m.alpha1(d);
				if(m_positions[d][0] == m_positions[m.phi1(d)][0] && m_positions[d][1] == m_positions[m.phi1(d)][1])
					d = m.alpha1(d);
			} while(getOrientationEdge(current, m.alpha1(d)) != Geom::UNDER && dd_vert != d);

			if(dd_vert == d)
			{
				//orbit with 2 edges : point on one edge
				if(m.alpha1(m.alpha1(d)) == d)
				{
					if(!Algo::Geometry::isPointOnHalfEdge<PFP>(m,d,m_positions,current))
						d = m.alpha1(d);
				}
				else
				{
					m_position = current;
					state = VERTEX;
					return;
				}
			}
		}
		else
		{
			Dart dd_vert = m.alpha1(d);
			while(getOrientationEdge(current, d) == Geom::OVER && dd_vert != d)
			{
				d = m.alpha_1(d);
				if(m_positions[d][0] == m_positions[m.phi1(d)][0] && m_positions[d][1] == m_positions[m.phi1(d)][1])
					d = m.alpha_1(d);
			}
		}

		//displacement step
		if(getOrientationEdge(current, d) == Geom::ON && Algo::Geometry::isPointOnHalfEdge<PFP>(m, d, m_positions, current))
			edgeState(current);
		else
		{
			d = m.phi1(d);
			faceState(current);
		}
	}
}

template <typename PFP>
void ParticleCell2DAndHalf<PFP>::edgeState(VEC3 current, Geom::Orientation3D sideOfEdge)
{
	#ifdef DEBUG
	CGoGNout << "edgeState" <<  d << CGoGNendl;
	#endif

	assert(std::isfinite(current[0]) && std::isfinite(current[1]) && std::isfinite(current[2]));
// 	assert(Algo::Geometry::isPointOnEdge<PFP>(m,d,m_positions,m_position));

	if(crossCell == NO_CROSS)
	{
		crossCell = CROSS_EDGE;
		lastCrossed = d;
	}
	else
		crossCell = CROSS_OTHER;

	if(sideOfEdge == Geom::ON)
		sideOfEdge = getOrientationEdge(current, d);

	switch(sideOfEdge)
	{
		case Geom::UNDER :
			d = m.phi1(d);
			faceState(current);
			return;
		case Geom::OVER:
Thomas's avatar
Thomas committed
170
		{
Thomas's avatar
Thomas committed
171
172
			//transform the displacement into the new entered face
			VEC3 displ = current-m_position;
Thomas's avatar
Thomas committed
173
			VEC3 edge = Algo::Geometry::vectorOutOfDart<PFP>(m,m.phi2(d),m_positions);
174

Thomas's avatar
Thomas committed
175
176
177
178
179
180
181
182
183
184
185
186
			VEC3 n1 = Algo::Geometry::faceNormal<PFP>(m,d,m_positions);
			VEC3 n2 = Algo::Geometry::faceNormal<PFP>(m,m.phi2(d),m_positions);

			float angle = acos(n1*n2);

			Geom::Matrix<4,4,float> mRot;
			mRot.identity();

			Geom::rotate(edge[0],edge[1],edge[2],angle,mRot);

			displ = Geom::transform(displ,mRot);
			current = m_position+displ;
Thomas's avatar
Thomas committed
187
188
189
190

			d = m.phi1(m.phi2(d));
			faceState(current);
			return;
Thomas's avatar
Thomas committed
191
		}
Thomas's avatar
Thomas committed
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
		default :
			state = EDGE;
	}

	if(!Algo::Geometry::isPointOnHalfEdge<PFP>(m, d, m_positions, current))
	{
		m_position = m_positions[d];
		vertexState(current);
		return;
	}
	else if(!Algo::Geometry::isPointOnHalfEdge<PFP>(m, m.phi2(d), m_positions, current))
	{
		d = m.phi2(d);
		m_position = m_positions[d];
		vertexState(current);
		return;
	}

	m_position = current;
}

template <typename PFP>
void ParticleCell2DAndHalf<PFP>::faceState(VEC3 current)
{
	#ifdef DEBUG
	CGoGNout << "faceState" <<  d << CGoGNendl;
	#endif

 	assert(std::isfinite(m_position[0]) && std::isfinite(m_position[1]) && std::isfinite(m_position[2]));
 	assert(std::isfinite(current[0]) && std::isfinite(current[1]) && std::isfinite(current[2]));
// 	assert(Algo::Geometry::isPointInConvexFace2D<PFP>(m,d,m_positions,m_position,true));

Thomas's avatar
Thomas committed
224
225
226
227
228
229
230
231
232
233
	//project current within plane
	VEC3 n1 = Algo::Geometry::faceNormal<PFP>(m,d,m_positions);
	VEC3 n2 = current-m_position;
	n1.normalize();
	VEC3 n3 = n1^n2;
	n3.normalize();
	VEC3 n4 = n3^n1;
	current = m_position+(n2*n4)*n4;

	//track new position within map
Thomas's avatar
Thomas committed
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
	Dart dd = d;
	float wsoe = getOrientationFace(current, m_position, m.phi1(d));

	// orientation step
	if(wsoe != Geom::UNDER)
	{
		d = m.phi1(d);
		wsoe = getOrientationFace(current, m_position, m.phi1(d));
		while(wsoe != Geom::UNDER && dd != d)
		{
			d = m.phi1(d);
			wsoe = getOrientationFace(current, m_position, m.phi1(d));
		}

 		// source and position to reach are the same : verify if no edge is crossed due to numerical approximation
		if(dd == d)
		{
			do
			{
				switch (getOrientationEdge(current, d))
				{
				case Geom::UNDER: 	d = m.phi1(d);
									break;
				case Geom::ON:	m_position = current;
									edgeState(current);
									return;
				case Geom::OVER:
//									CGoGNout << "smthg went bad " << m_position << " " << current << CGoGNendl;
//									CGoGNout << "d1 " << m_positions[d] << " d2 " << m_positions[m.phi1(d)] << CGoGNendl;
									m_position = intersectLineEdge(current, m_position, d);
//									CGoGNout << " " << m_position << CGoGNendl;

									edgeState(current,Geom::OVER);
									return;
				}
			} while(d != dd);
			m_position = current;
			state = FACE;

// 			m_position = Algo::Geometry::faceCentroid<PFP>(m,d,m_positions);
// 			d = m.phi1(d);
// 			m_position = pointInFace(d);
// 			faceState(current);

// 			m_position = m_positions[d];
// 			vertexState(current);
			return;
		}
		// take the orientation with d1 : in case we are going through a vertex
		wsoe = getOrientationFace(current, m_position, d);
	}
	else
	{
		wsoe = getOrientationFace(current,m_position,d);
		while(wsoe == Geom::UNDER && m.phi_1(d) != dd)
		{
			d = m.phi_1(d);
			wsoe = getOrientationFace(current, m_position, d);
		}

		// in case of numerical incoherence
		if(m.phi_1(d) == dd && wsoe == Geom::UNDER)
		{
			d = m.phi_1(d);
			do
			{
				switch (getOrientationEdge(current, d))
				{
				case Geom::UNDER :
					d = m.phi1(d);
					break;
				case Geom::ON :
// 					CGoGNout << "pic" << CGoGNendl;
					m_position = current;
					edgeState(current);
					return;
				case Geom::OVER:
//					CGoGNout << "smthg went bad(2) " << m_position << CGoGNendl;
					m_position = intersectLineEdge(current, m_position, d);
// 					CGoGNout << " " << m_position << CGoGNendl;
					edgeState(current, Geom::OVER);
					return;
				}
			} while(d != dd);

			m_position = current;
			state = FACE;
			return;
		}
	}

	//displacement step
	switch (getOrientationEdge(current, d))
	{
	case Geom::UNDER :
		m_position = current;
		state = FACE;;
		break;
	default :
		if(wsoe == Geom::ON)
		{
Thomas's avatar
Thomas committed
335
336
337
338
339
			std::cout << __FILE__ << " to uncomment and check" << std::endl;
//			d = m.phi1(d); //to check
//			m_position = m_positions[d];
//
//			vertexState(current);
Thomas's avatar
Thomas committed
340
		}
Thomas's avatar
Thomas committed
341
//		else
Thomas's avatar
Thomas committed
342
343
344
345
346
347
348
349
350
351
		{
// 			CGoGNout << "wsoe : " << wsoe << CGoGNendl;
// 			CGoGNout << "current " << current << " " << m_position << CGoGNendl;
// 			CGoGNout << "d " << d << "d1 " << m_positions[d] << " d2 " << m_positions[m.phi2(d)] << CGoGNendl;
			m_position = intersectLineEdge(current, m_position, d);
// 			CGoGNout << " inter : " << m_position << CGoGNendl;
			edgeState(current, Geom::OVER);
		}
	}
}