File: PrimitivePositioning.cpp

package info (click to toggle)
libqglviewer 2.6.3%2Bdfsg2-9
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 9,460 kB
  • sloc: cpp: 26,447; sh: 14; makefile: 11
file content (671 lines) | stat: -rw-r--r-- 19,309 bytes parent folder | download | duplicates (2)
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
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
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
201
202
203
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
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
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
/*
 This file is part of the VRender library.
 Copyright (C) 2005 Cyril Soler (Cyril.Soler@imag.fr)
 Version 1.0.0, released on June 27, 2005.

 http://artis.imag.fr/Members/Cyril.Soler/VRender

 VRender is free software; you can redistribute it and/or modify
 it under the terms of the GNU General Public License as published by
 the Free Software Foundation; either version 2 of the License, or
 (at your option) any later version.

 VRender is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 GNU General Public License for more details.

 You should have received a copy of the GNU General Public License
 along with VRender; if not, write to the Free Software Foundation, Inc.,
 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.
*/

/****************************************************************************

 Copyright (C) 2002-2014 Gilles Debunne. All rights reserved.

 This file is part of the QGLViewer library version 2.6.3.

 http://www.libqglviewer.com - contact@libqglviewer.com

 This file may be used under the terms of the GNU General Public License 
 versions 2.0 or 3.0 as published by the Free Software Foundation and
 appearing in the LICENSE file included in the packaging of this file.
 In addition, as a special exception, Gilles Debunne gives you certain 
 additional rights, described in the file GPL_EXCEPTION in this package.

 libQGLViewer uses dual licensing. Commercial/proprietary software must
 purchase a libQGLViewer Commercial License.

 This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
 WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.

*****************************************************************************/

#include "Primitive.h"
#include "AxisAlignedBox.h"
#include "PrimitivePositioning.h"
#include "math.h"
#include <algorithm>
#include "Vector2.h"

#include <algorithm>

using namespace vrender ;
using namespace std ;

#define DEBUG_TS

double PrimitivePositioning::_EPS = 0.00001 ;

// Computes relative position of the second primitive toward the first.
// As a general rule, the smaller the Z of a primitive, the Upper the primitive.

int PrimitivePositioning::computeRelativePosition(const Primitive *p1,const Primitive *p2)
{
	AxisAlignedBox_xyz bb1(p1->bbox()) ;
	AxisAlignedBox_xyz bb2(p2->bbox()) ;

	// 1 - check if bounding boxes are disjoint. In such a case, a rapid answer is possible.

	if( bb1.maxi().x() < bb2.mini().x() || bb1.mini().x() > bb2.maxi().x()) return Independent ;
	if( bb1.maxi().y() < bb2.mini().y() || bb1.mini().y() > bb2.maxi().y()) return Independent ;

	// 2 - call specific tests for each case.

	if(p1->nbVertices() >= 3)
		if(p2->nbVertices() >= 3)
			return computeRelativePosition( dynamic_cast<const Polygone *>(p1),dynamic_cast<const Polygone *>(p2)) ;
		else if(p2->nbVertices() == 2) // Case of a segment versus a polygon
			return computeRelativePosition( dynamic_cast<const Polygone *>(p1),dynamic_cast<const Segment *>(p2)) ;
		else
			return computeRelativePosition( dynamic_cast<const Polygone *>(p1),dynamic_cast<const Point *>(p2)) ;
	else if(p1->nbVertices() == 2)
		if(p2->nbVertices() >= 3)
			return inverseRP(computeRelativePosition( dynamic_cast<const Polygone *>(p2),dynamic_cast<const Segment *>(p1))) ;
		else if(p2->nbVertices() == 2)
			return computeRelativePosition( dynamic_cast<const Segment *>(p1),dynamic_cast<const Segment *>(p2)) ;
		else
			return Independent ;	// segment vs point => independent
	else
		if(p2->nbVertices() >= 3)
			return inverseRP(computeRelativePosition( dynamic_cast<const Polygone *>(p2),dynamic_cast<const Point *>(p1))) ;
		else if(p2->nbVertices() == 2)
			return Independent ;	// point vs segment => independent
		else
			return Independent ;	// point vs point => independent
}

// Computes the relative position of the point toward a *convex* polygon.

int PrimitivePositioning::computeRelativePosition(const Polygone *Q,const Point *P)
{
	if(pointOutOfPolygon_XY(P->vertex(0),Q,(double)_EPS)) // On met un eps > 0, pour que les
		return Independent ;							 // points du bords soient inclus dans le polygone.

	// now compute the relative position of the point toward the polygon

	if(Q->equation(P->vertex(0)) >= 0)
		return Upper ;
	else
		return Lower ;
}

// Computes the relative position of the segment toward a *convex* polygon.

int PrimitivePositioning::computeRelativePosition(const Polygone *P,const Segment *S)
{
	//  Computes the intersection of the segment and the polygon in 2D, then
	// project the extremities of the intersection onto the segment, and compare
	// the points to the polygon.

	// 1 - 2D-intersection of segment and polygon

	vector<double> intersections ;

	if(!pointOutOfPolygon_XY(S->vertex(0),P,_EPS)) intersections.push_back(0.0);
	if(!pointOutOfPolygon_XY(S->vertex(1),P,_EPS)) intersections.push_back(1.0);

	double t1,t2 ;

		for(size_t i=0;i<P->nbVertices();++i)
		if(intersectSegments_XY(Vector2(S->vertex(0)),Vector2(S->vertex(1)),Vector2(P->vertex(i)),Vector2(P->vertex(i+1)),_EPS,t1,t2))
			intersections.push_back(t1) ;

	// 2 - Checks wether the intersection segment is reduced to a point. In this case,
	// 	both primitives are independent.

	double tmin = FLT_MAX ;
	double tmax = -FLT_MAX ;

	for(unsigned int j=0;j<intersections.size();++j)
	{
		tmin = std::min(tmin,intersections[j]) ;
		tmax = std::max(tmax,intersections[j]) ;
	}

	if(tmax - tmin < 2*_EPS)
		return Independent ;

	// 3 - The intersection segment is not reduced to a point. Compares 3D
	//   projections of the intersections with the plane of the polygon.

	int res = Independent ;

	for(unsigned int k=0;k<intersections.size();++k)
	{
		Vector3 v( (1-intersections[k])*S->vertex(0) + intersections[k]*S->vertex(1) ) ;

		if(P->equation(v) < -_EPS) res |= Lower ;
		if(P->equation(v) >  _EPS) res |= Upper ;
	}

	if(intersections.size() > 1 && res == Independent)	// case of segments tangent to the polygon
		res = Upper ;

	return res ;
}

// Computes the relative position of a polygon toward a convex polygon.

int PrimitivePositioning::computeRelativePosition(const Polygone *P1,const Polygone *P2)
{
	// 1 - use gpc to conservatively check for intersection. This works fine because
	//    gpc produces a null intersection for polygons sharing an edge, which
	//    is exactly what we need.

	gpc_polygon gpc_int ;

	try
	{
		gpc_polygon gpc_p1 = createGPCPolygon_XY(P1) ;
		gpc_polygon gpc_p2 = createGPCPolygon_XY(P2) ;

		gpc_polygon_clip(GPC_INT,&gpc_p1,&gpc_p2,&gpc_int) ;

		gpc_free_polygon(&gpc_p1) ;
		gpc_free_polygon(&gpc_p2) ;
	}
	catch(exception&)
	{
		return Independent ;				// no free, because we don't really now what happenned.
	}

	int res = Independent ;

	if (gpc_int.num_contours != 1) // There is some numerical error in gpc. Let's skip.
	  {
		gpc_free_polygon(&gpc_int) ;
		return res ;
		// throw runtime_error("Intersection with more than 1 contour ! Non convex polygons ?") ;
	  }

	// 2 - polygons are not independent. Compute their relative position.
	//    For this, we project the vertices of the 2D intersection onto the
	//   support plane of each polygon. The epsilon-signs of each point toward
	//   both planes give the relative position of the polygons.

	for(long i=0;i<gpc_int.contour[0].num_vertices && (res < (Upper | Lower));++i)
	{
		if(P1->normal().z() == 0.0) throw runtime_error("could not project point. Unexpected case !") ;
		if(P2->normal().z() == 0.0) throw runtime_error("could not project point. Unexpected case !") ;

		// project point onto support planes

		double f1 = P1->normal().x() * gpc_int.contour[0].vertex[i].x + P1->normal().y() * gpc_int.contour[0].vertex[i].y - P1->c() ;
		double f2 = P2->normal().x() * gpc_int.contour[0].vertex[i].x + P2->normal().y() * gpc_int.contour[0].vertex[i].y - P2->c() ;

		Vector3 v1(gpc_int.contour[0].vertex[i].x,gpc_int.contour[0].vertex[i].y, -f1/P1->normal().z()) ;
		Vector3 v2(gpc_int.contour[0].vertex[i].x,gpc_int.contour[0].vertex[i].y, -f2/P2->normal().z()) ;

		if(P1->equation(v2) < -_EPS) res |= Lower ;
		if(P1->equation(v2) >  _EPS) res |= Upper ;
		if(P2->equation(v1) < -_EPS) res |= Upper ;
		if(P2->equation(v1) >  _EPS) res |= Lower ;
	}
	gpc_free_polygon(&gpc_int) ;
	return res ;
}

// Computes the relative position of a segment toward another segment.

int PrimitivePositioning::computeRelativePosition(const Segment *S1,const Segment *S2)
{
	double t1,t2 ;

	if(!intersectSegments_XY(	Vector2(S1->vertex(0)),Vector2(S1->vertex(1)),
										Vector2(S2->vertex(0)),Vector2(S2->vertex(1)),
										-(double)_EPS,t1,t2 ))
		return Independent ;
	else
	{
		double z1 = (1.0 - t1)*S1->vertex(0).z() + t1*S1->vertex(1).z() ;
		double z2 = (1.0 - t2)*S2->vertex(0).z() + t2*S2->vertex(1).z() ;

		if(z1 <= z2)
			return Lower ;
		else
			return Upper ;
	}
}


// Teste si le point est exterieur au polygone (convexe). Plus I_EPS est grand
// plus il faut etre loin pour que ca soit vrai. EPS=0 correspond au polygone
// lui-meme bords inclus. Pour EPS<0, des points interieurs pres de la frontiere sont
// declares exterieurs.  Plus I_EPS est grand, plus l'ensemble des points
// consideres comme interieur est dilate.

bool PrimitivePositioning::pointOutOfPolygon_XY(const Vector3& P,const Polygone *Q,double I_EPS)
{
	size_t nq = Q->nbVertices() ;
	Vector2 p = Vector2(P) ;

	FLOAT MaxZ = -FLT_MAX ;
	FLOAT MinZ =  FLT_MAX ;

	for(size_t j=0;j<nq;j++)  				//  Regarde si P.(x,y) est a l'interieur
	{                               	// ou a l'exterieur du polygone.
		Vector2 q1 = Vector2(Q->vertex(j)) ;
		Vector2 q2 = Vector2(Q->vertex(j+1)) ;

		double Z = (q1-p)^(q2-p) ;

		MinZ = std::min(Z,MinZ) ;
		MaxZ = std::max(Z,MaxZ) ;
	}

	if((MaxZ <= -I_EPS*I_EPS)||(MinZ >= I_EPS*I_EPS))	// the point is inside the polygon
		return false ;
	else
		return true ;
}

int PrimitivePositioning::inverseRP(int pos)
{
	// Basically switch bits of Lower and Upper

	switch(pos)
	{
		case Independent: return Independent ;
		case Lower: return Upper ;
		case Upper: return Lower ;
		case Upper | Lower: return Upper | Lower ;
		default:
								  throw runtime_error("Unexpected value.") ;
								  return pos ;
	}
}

// Calcule l'intersection des segments [P1,Q1] et [P2,Q2]
// En retour, (1-t1,t1) et (1-t2,t2) sont les coordonnees
// barycentriques de l'intersection dans chaque segment.

bool PrimitivePositioning::intersectSegments_XY(const Vector2& P1,const Vector2& Q1,
																const Vector2& P2,const Vector2& Q2,
																double I_EPS,
																double & t1,double & t2)
{
	double P1x(P1.x()) ;
	double P1y(P1.y()) ;
	double P2x(P2.x()) ;
	double P2y(P2.y()) ;
	double Q1x(Q1.x()) ;
	double Q1y(Q1.y()) ;
	double Q2x(Q2.x()) ;
	double Q2y(Q2.y()) ;

	double a2 = -(Q2y - P2y) ;
	double b2 =  (Q2x - P2x) ;
	double c2 =  P2x*a2+P2y*b2 ;

	double a1 = -(Q1y - P1y) ;
	double b1 =  (Q1x - P1x) ;
	double c1 =  P1x*a1+P1y*b1 ;

	double d2 = a2*(Q1x-P1x)+b2*(Q1y-P1y) ;
	double d1 = a1*(Q2x-P2x)+b1*(Q2y-P2y) ;

	if((fabs(d2) <= fabs(I_EPS))||(fabs(d1) <= fabs(I_EPS)))	// les segments sont paralleles
	{
		if(fabs(a2*P1x + b2*P1y - c2) >= I_EPS)
			return false ;

		double tP1,tQ1 ;

		if(P1x != Q1x)
		{
			tP1 = (P2x-P1x)/(Q1x-P1x) ;
			tQ1 = (Q2x-P1x)/(Q1x-P1x) ;
		}
		else if(P1y != Q1y)
		{
			tP1 = (P2y-P1y)/(Q1y-P1y) ;
			tQ1 = (Q2y-P1y)/(Q1y-P1y) ;
		}
		else
		{
#ifdef DEBUG_TS
			printf("IntersectSegments2D:: Error ! One segment has length 0\n") ;
			printf("This special case is not treated yet.\n") ;
#endif
			return false ;
		}

		double tPQM = std::max(tP1,tQ1) ;
		double tPQm = std::min(tP1,tQ1) ;

		if(( tPQM < -I_EPS) || (tPQm > 1.0+I_EPS))
			return false ;

		if(tPQm > 0.0)
		{
			t1 = tPQm ;
			t2 = 0.0 ;
		}
		else
		{
			t1 = 0.0 ;
			if(P2x != Q2x)
				t2 = (P1x-P2x)/(Q2x-P2x) ;
			else if(P2y != Q2y)
				t2 = (P1y-P2y)/(Q2y-P2y) ;
			else
			{
#ifdef DEBUG_TS
				printf("IntersectSegments2D:: Error ! One segment has length 0\n") ;
				printf("This special case is not treated yet.\n") ;
#endif
				return false ;
			}
		}

		return true ;
	}
	else
	{
		t2 = (c1 - a1*P2x - b1*P2y)/d1 ;
		t1 = (c2 - a2*P1x - b2*P1y)/d2 ;

		if((t2 > 1+I_EPS)||(t2 < -I_EPS)||(t1 > 1+I_EPS)||(t1 < -I_EPS))
			return false ;

		return true ;
	}
}

gpc_polygon PrimitivePositioning::createGPCPolygon_XY(const Polygone *P)
{
	gpc_polygon p ;

	p.num_contours = 0 ;
	p.hole = NULL ;
	p.contour = NULL ;

	gpc_vertex_list *gpc_p_verts = new gpc_vertex_list ;

	gpc_p_verts->num_vertices = P->nbVertices() ;
	gpc_p_verts->vertex = new gpc_vertex[P->nbVertices()] ;

		for(size_t i=0;i<P->nbVertices();++i)
	{
		gpc_p_verts->vertex[i].x = P->vertex(i).x() ;
		gpc_p_verts->vertex[i].y = P->vertex(i).y() ;
	}

	gpc_add_contour(&p,gpc_p_verts,false) ;

	return p ;
}

void PrimitivePositioning::getsigns(const Primitive *P,const NVector3& v,double C,
												vector<int>& signs,vector<double>& zvals,int& Smin,int& Smax,double I_EPS)
{
	if(P == NULL)
		throw runtime_error("Null primitive in getsigns !") ;

	size_t n = P->nbVertices() ;

	Smin =  1 ;
	Smax = -1 ;

	// On classe les sommets en fonction de leur signe

	double zmax = -FLT_MAX ;
	double zmin =  FLT_MAX ;
	zvals.resize(n) ;

	for(size_t i=0;i<n;i++)
	{
		double Z = P->vertex(i) * v - C ;

		if(Z > zmax) zmax = Z ;
		if(Z < zmin) zmin = Z ;

		zvals[i] = Z ;
	}

	signs.resize(n) ;

	for(size_t j=0;j<n;j++)
	{
		if(zvals[j] < -I_EPS)
			signs[j] = -1 ;
		else if(zvals[j] > I_EPS)
			signs[j] = 1 ;
		else
			signs[j] = 0 ;

		if(Smin > signs[j]) Smin = signs[j] ;
		if(Smax < signs[j]) Smax = signs[j] ;
	}
}

void PrimitivePositioning::split(Polygone *P,const NVector3& v,double C,Primitive *& P_plus,Primitive *& P_moins)
{
	vector<int> Signs ;
	vector<double> Zvals ;

	P_plus = NULL ;
	P_moins = NULL ;

	int Smin = 1 ;
	int Smax = -1 ;

	getsigns(P,v,C,Signs,Zvals,Smin,Smax,_EPS) ;

	size_t n = P->nbVertices() ;

	if((Smin == 0)&&(Smax == 0)){ P_moins = P ; P_plus = NULL ; return ; }	// Polygone inclus dans le plan
	if(Smin == 1) 					{ P_plus = P ; P_moins = NULL ; return ; }	// Polygone tout positif
	if(Smax == -1) 					{ P_plus = NULL ; P_moins = P ; return ; }	// Polygone tout negatif

	if((Smin == -1)&&(Smax == 0)) { P_plus = NULL ; P_moins = P ; return ; }	// Polygone tout negatif ou null
	if((Smin == 0)&&(Smax == 1))  { P_plus = P ; P_moins = NULL ; return ; }	// Polygone tout positif ou null

	// Reste le cas Smin = -1 et Smax = 1. Il faut couper

	vector<Feedback3DColor> Ps ;
	vector<Feedback3DColor> Ms ;

	// On teste la coherence des signes.

	int nZero = 0 ;
	int nconsZero = 0 ;

	for(size_t i=0;i<n;i++)
	{
		if(Signs[i] == 0)
		{
			nZero++ ;

			if(Signs[(i+1)%n] == 0)
				nconsZero++ ;
		}
	}

	// Ils y a des imprecisions numeriques dues au fait que le poly estpres du plan.
	if((nZero > 2)||(nconsZero > 0)) { P_moins = P ; P_plus  = NULL ; return ; }

	int dep=0 ; while(Signs[dep] == 0) dep++ ;
	int prev_sign = Signs[dep] ;

	for(size_t j=1;j<=n;j++)
	{
		int sign = Signs[(j+dep)%n] ;

		if(sign == prev_sign)
		{
			if(sign ==  1) Ps.push_back(P->sommet3DColor(j+dep)) ;
			if(sign == -1) Ms.push_back(P->sommet3DColor(j+dep)) ;
		}
		else if(sign == -prev_sign)
		{
			//  Il faut effectuer le calcul en utilisant les memes valeurs que pour le calcul des signes,
			// sinon on risque des incoherences dues aux imprecisions numeriques.

			double Z1 = Zvals[(j+dep-1)%n] ;
			double Z2 = Zvals[(j+dep)%n] ;

			double t = fabs(Z1/(Z2 - Z1)) ;

			if((t < 0.0)||(t > 1.0))
			{
				if(t > 1.0) t = 1.0 ;
				if(t < 0.0) t = 0.0 ;
			}
			Feedback3DColor newVertex((1-t)*P->sommet3DColor(j+dep-1) + t*P->sommet3DColor(j+dep)) ;

			Ps.push_back(newVertex) ;
			Ms.push_back(newVertex) ;

			if(sign == 1)
				Ps.push_back(P->sommet3DColor(j+dep)) ;

			if(sign == -1)
				Ms.push_back(P->sommet3DColor(j+dep)) ;

			prev_sign = sign ;
		} // prev_sign != 0 donc necessairement sign = 0. Le sommet tombe dans le plan
		else
		{
			Feedback3DColor newVertex = P->sommet3DColor(j+dep) ;

			Ps.push_back(newVertex) ;
			Ms.push_back(newVertex) ;

			prev_sign = -prev_sign ;
		}
	}

	if(Ps.size() > 100 || Ms.size() > 100 )
		printf("Primitive::split: Error. nPs = %d, nMs = %d.\n",int(Ps.size()),int(Ms.size())) ;

	// on suppose pour l'instant que les polygones sont convexes

	if(Ps.size() == 1)
		P_plus = new Point(Ps[0]) ;
	else if(Ps.size() == 2)
		P_plus = new Segment(Ps[0],Ps[1]) ;
	else
		P_plus  = new Polygone(Ps) ;

	if(Ms.size() == 1)
		P_moins = new Point(Ms[0]) ;
	else if(Ms.size() == 2)
		P_moins = new Segment(Ms[0],Ms[1]) ;
	else
		P_moins = new Polygone(Ms) ;
}

void PrimitivePositioning::split(Point *P,const NVector3& v,double C,Primitive * & P_plus,Primitive * & P_moins)
{
	if(v*P->vertex(0)-C > -_EPS)
	{
		P_plus = P ;
		P_moins = NULL ;
	}
	else
	{
		P_moins = P ;
		P_plus = NULL ;
	}
}

void PrimitivePositioning::split(Segment *S,const NVector3& v,double C,Primitive * & P_plus,Primitive * & P_moins)
{
	vector<int> Signs ;
	vector<double> Zvals ;

	P_plus = NULL ;
	P_moins = NULL ;

	int Smin = 1 ;
	int Smax = -1 ;

	getsigns(S,v,C,Signs,Zvals,Smin,Smax,_EPS) ;

	size_t n = S->nbVertices() ;

	if((Smin == 0)&&(Smax == 0)) 	{ P_moins = S ; P_plus = NULL ; return ; }	// Polygone inclus dans le plan
	if(Smin == 1) 						{ P_plus = S ; P_moins = NULL ; return ; }	// Polygone tout positif
	if(Smax == -1) 					{ P_plus = NULL ; P_moins = S ; return ; }	// Polygone tout negatif

	if((Smin == -1)&&(Smax == 0)) { P_plus = NULL ; P_moins = S ; return ; }	// Polygone tout negatif ou null
	if((Smin == 0)&&(Smax == 1))  { P_plus = S ; P_moins = NULL ; return ; }	// Polygone tout positif ou null

	// Reste le cas Smin = -1 et Smax = 1. Il faut couper
	// On teste la coherence des signes.

	int nZero = 0 ;
	int nconsZero = 0 ;

	for(size_t i=0;i<n;i++)
	{
		if(Signs[i] == 0)
		{
			nZero++ ;

			if(Signs[(i+1)%n] == 0)
				nconsZero++ ;
		}
	}

	// Ils y a des imprecisions numeriques dues au fait que le poly estpres du plan.
	if((nZero > 2)||(nconsZero > 0)) { P_moins = S ; P_plus  = NULL ; return ; }

	double Z1 = Zvals[0] ;
	double Z2 = Zvals[1] ;

	double t = fabs(Z1/(Z2 - Z1)) ;

	if((t < 0.0)||(t > 1.0))
	{
		if(t > 1.0) t = 1.0 ;
		if(t < 0.0) t = 0.0 ;
	}

	Feedback3DColor newVertex = S->sommet3DColor(0) * (1-t) + S->sommet3DColor(1) * t ;

	if(Signs[0] < 0)
	{
		P_plus = new Segment(newVertex,S->sommet3DColor(1)) ;
		P_moins = new Segment(S->sommet3DColor(0),newVertex) ;
	}
	else
	{
		P_plus = new Segment(S->sommet3DColor(0),newVertex) ;
		P_moins = new Segment(newVertex,S->sommet3DColor(1)) ;
	}
}

// splits primitive P by plane of equation v.X=c. The upper part is setup in a new primitive called prim_up and
// the lower part is in prim_lo.

void PrimitivePositioning::splitPrimitive(Primitive *P,const NVector3& v,double c, Primitive *& prim_up,Primitive *& prim_lo)
{
	Polygone *p1 = dynamic_cast<Polygone *>(P) ; if(p1 != NULL) PrimitivePositioning::split(p1,v,c,prim_up,prim_lo) ;
	Segment  *p2 = dynamic_cast<Segment  *>(P) ; if(p2 != NULL) PrimitivePositioning::split(p2,v,c,prim_up,prim_lo) ;
	Point    *p3 = dynamic_cast<Point    *>(P) ; if(p3 != NULL) PrimitivePositioning::split(p3,v,c,prim_up,prim_lo) ;
}