particle_cell_2D.hpp 8.67 KB
Newer Older
Pierre Kraemer's avatar
Pierre Kraemer 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
42
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
70
71
// #define DEBUG

template <typename PFP>
void ParticleCell2D<PFP>::display()
{
// 	std::cout << "pos : " << this->m_position << std::endl;
// 	std::cout << "d : " << this->d << std::endl;
// 	std::cout << "state : " << this->state << std::endl;
}

template <typename PFP>
typename PFP::VEC3 ParticleCell2D<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]);
	
	while(Geom::testOrientation2D(p3,p1,p2)==Geom::ALIGNED) {
		dd = m.phi1(dd);
		p3 = m_positions[dd];
	}

	std::cout << "pointInFace " << (p1+p3)*0.5f << std::endl;

	return (p1+p3)*0.5f;
}

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

	return Geom::testOrientation2D(point,vertexPoint,endPoint);
}

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

	Geom::intersection2DSegmentSegment(pA,pB,q1,q2,Inter);

// 	if(VEC3(Inter-pA).norm()>VEC3(pA-pB).norm()) {
// 		std::cout << "probleme : " << pA << "/" << pB << "/" << q1 << "/" << q2 << "/" << Inter << std::endl;
// 		std::cout << "isPointOnHalf " << Algo::Geometry::isPointOnHalfEdge<PFP>(m,d,m_positions,Inter) << std::endl;
// 		std::cout << "isPointOnHalf " << Algo::Geometry::isPointOnHalfEdge<PFP>(m,m.phi1(d),m_positions,Inter) << std::endl;
// 	}

	return Inter;
}

template <typename PFP>
Geom::Orientation2D ParticleCell2D<PFP>::getOrientationFace(VEC3 point, VEC3 sourcePoint, Dart d)
{
	const VEC3& dPoint = m_positions[d];
	return Geom::testOrientation2D(point,sourcePoint,dPoint);
}

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

72
	changeCell = true;
Pierre Kraemer's avatar
Pierre Kraemer committed
73
74
75
76
77
78
	if(Algo::Geometry::isPointOnVertex<PFP>(m,d,m_positions,current)) {
		state = VERTEX_ORBIT;
		return;
	}
	else {
		//orientation step
Thomas's avatar
Thomas committed
79
80
		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);
Pierre Kraemer's avatar
Pierre Kraemer committed
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
		if(getOrientationEdge(current,m.alpha1(d))!=Geom::RIGHT) {
			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::RIGHT && 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 {
    					state = VERTEX_ORBIT;
					return;
				}
			}
		}
		else {
Thomas's avatar
Thomas committed
103
			std::cout << "ploc" << std::endl;
Pierre Kraemer's avatar
Pierre Kraemer committed
104
105
			Dart dd_vert = m.alpha1(d);
			while(getOrientationEdge(current,d)==Geom::RIGHT && dd_vert!=d) {
Thomas's avatar
Thomas committed
106
				std::cout << "tourne" << std::endl;
Pierre Kraemer's avatar
Pierre Kraemer committed
107
108
				d = m.alpha_1(d);

Thomas's avatar
Thomas committed
109
				if(m_positions[d][0]==m_positions[m.phi1(d)][0] && m_positions[d][1]==m_positions[m.phi1(d)][1]) {
Pierre Kraemer's avatar
Pierre Kraemer committed
110
					d=m.alpha_1(d);
Thomas's avatar
Thomas committed
111
				}
Pierre Kraemer's avatar
Pierre Kraemer committed
112
113
114
115
116
			}
		}

		//displacement step
// 		if(!obstacle.isMarked(d)) {
Thomas's avatar
Thomas committed
117
118
			if(getOrientationEdge(current,d)==Geom::ALIGNED && Algo::Geometry::isPointOnHalfEdge<PFP>(m,d,m_positions,current)) {
				std::cout << m.vertexDegree(d) << std::endl;
Pierre Kraemer's avatar
Pierre Kraemer committed
119
				edgeState(current);
Thomas's avatar
Thomas committed
120
			}
Pierre Kraemer's avatar
Pierre Kraemer committed
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
			else {
				d = m.phi1(d);
				faceState(current);
			}
// 		}
	}
}

template <typename PFP>
void ParticleCell2D<PFP>::edgeState(const VEC3& current, Geom::Orientation2D sideOfEdge)
{
	#ifdef DEBUG
	std::cout << "edgeState" <<  d << std::endl;
	#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));

139
	changeCell = true;
Pierre Kraemer's avatar
Pierre Kraemer committed
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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
	if(sideOfEdge==Geom::ALIGNED)
		sideOfEdge = getOrientationEdge(current,d);

	switch(sideOfEdge) {
		case Geom::LEFT :
// 							if(!obstacle.isMarked(d)) {
								d = m.phi1(d);
								faceState(current);
								return;
// 							}
		case Geom::RIGHT:
// 							if(!obstacle.isMarked(m.phi2(d))) {
								d = m.phi1(m.phi2(d));
								faceState(current);
								return;
// 							}
		default :   state = EDGE_ORBIT;
	}

	if(!Algo::Geometry::isPointOnHalfEdge<PFP>(m,d,m_positions,current)) {
		prevPos = m_position;
		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);
		prevPos = m_position;
		m_position = m_positions[d];
		vertexState(current);
		return;
	}
}

template <typename PFP>
void ParticleCell2D<PFP>::faceState(const VEC3& current)
{
	#ifdef DEBUG
	std::cout << "faceState" <<  d << std::endl;
	#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));

	Dart dd=d;
	float wsoe = getOrientationFace(current,m_position,m.phi1(d));

	//orientation step
	if(wsoe!=Geom::RIGHT) {
		d = m.phi1(d);
		wsoe = getOrientationFace(current,m_position,m.phi1(d));
		while(wsoe!=Geom::RIGHT && 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)) {
201
				case Geom::LEFT : 	d=m.phi1(d);
Pierre Kraemer's avatar
Pierre Kraemer committed
202
									break;
203
				case Geom::ALIGNED :m_position = current;
Pierre Kraemer's avatar
Pierre Kraemer committed
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
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
									edgeState(current);
									return;
				case Geom::RIGHT :
									std::cout << "smthg went bad " << m_position << " " << current << std::endl;
									std::cout << "d1 " << m_positions[d] << " d2 " << m_positions[m.phi1(d)] << std::endl;
									m_position = intersectLineEdge(current,m_position,d);
									std::cout << " " << m_position << std::endl;

// 									if(!obstacle.isMarked(m.phi2(d)))
										edgeState(current,Geom::RIGHT);
// 									else 
// 										state = EDGE_ORBIT;
									return;
				}
			} while(d!=dd);
			prevPos = m_position;
			m_position = current;
			state = FACE_ORBIT;

// 			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::RIGHT && 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) {
			d = m.phi_1(d);
			do {
				switch (getOrientationEdge(current,d)) {
				case Geom::LEFT : 			d=m.phi1(d);
									break;
				case Geom::ALIGNED :			
// 									std::cout << "pic" << std::endl;
									m_position = current;
									edgeState(current);
									return;
				case Geom::RIGHT :
									std::cout << "smthg went bad(2) " << m_position << std::endl;
									m_position = intersectLineEdge(current,m_position,d);
// 									std::cout << " " << m_position << std::endl;
// 									if(!obstacle.isMarked(m.phi2(d)))
										edgeState(current,Geom::RIGHT);
// 									else 
// 										state = EDGE_ORBIT;
									return;
				}
			} while(d!=dd);

			m_position = current;
			state = FACE_ORBIT;
			return;
		}
	}

	prevPos = m_position;

	//displacement step
	switch (getOrientationEdge(current,d)) {
	case Geom::LEFT :			m_position = current;
		 				state = FACE_ORBIT;;
		 				break;
// 	case Geom::ALIGNED :			if(wsoe==Geom::ALIGNED) {
// 							m_position = m_positions[d];
// 							vertexState(current);
// 						}
// 						else {
// 							std::cout << "poc" << std::endl;
// 							m_position = current;
// 		 					state = EDGE_ORBIT;
// 						}
// 		 				break;
	default :
						if(wsoe==Geom::ALIGNED) {
							m_position = m_positions[d];
							vertexState(current);
						}
						else {
// 							std::cout << "wsoe : " << wsoe << std::endl;
// 							std::cout << "current " << current << " " << m_position << std::endl;
// 							std::cout << "d " << d << "d1 " << m_positions[d] << " d2 " << m_positions[m.phi2(d)] << std::endl;
							m_position = intersectLineEdge(current,m_position,d);
// 							std::cout << " inter : " << m_position << std::endl;
// 							if(!obstacle.isMarked(m.phi2(d)))
								edgeState(current,Geom::RIGHT);
// 							else
// 								state = EDGE_ORBIT;
						}
	}
}