111 if( bb1.maxi().x() < bb2.mini().x() || bb1.mini().x() > bb2.maxi().x())
return Independent ;
112 if( bb1.maxi().y() < bb2.mini().y() || bb1.mini().y() > bb2.maxi().y())
return Independent ;
118 return computeRelativePosition( dynamic_cast<const Polygone *>(p1),
dynamic_cast<const Polygone *
>(p2)) ;
120 return computeRelativePosition( dynamic_cast<const Polygone *>(p1),
dynamic_cast<const Segment *
>(p2)) ;
122 return computeRelativePosition( dynamic_cast<const Polygone *>(p1),dynamic_cast<const Point *>(p2)) ;
125 return inverseRP(computeRelativePosition( dynamic_cast<const Polygone *>(p2),dynamic_cast<const Segment *>(p1))) ;
127 return computeRelativePosition( dynamic_cast<const Segment *>(p1),
dynamic_cast<const Segment *
>(p2)) ;
132 return inverseRP(computeRelativePosition( dynamic_cast<const Polygone *>(p2),dynamic_cast<const Point *>(p1))) ;
143 if(pointOutOfPolygon_XY(P->
vertex(0),Q,(double)_EPS))
164 vector<double> intersections ;
166 if(!pointOutOfPolygon_XY(S->
vertex(0),P,_EPS)) intersections.push_back(0.0);
167 if(!pointOutOfPolygon_XY(S->
vertex(1),P,_EPS)) intersections.push_back(1.0);
173 intersections.push_back(t1) ;
181 for(
unsigned int j=0;j<intersections.size();++j)
183 tmin = min(tmin,intersections[j]) ;
184 tmax = max(tmax,intersections[j]) ;
187 if(tmax - tmin < 2*_EPS)
193 int res = Independent ;
195 for(
unsigned int k=0;k<intersections.size();++k)
199 if(P->
equation(v) < -_EPS) res |= Lower ;
200 if(P->
equation(v) > _EPS) res |= Upper ;
203 if(intersections.size() > 1 && res == Independent)
234 int res = Independent ;
250 if(P1->
normal().
z() == 0.0)
throw runtime_error(
"could not project point. Unexpected case !") ;
251 if(P2->
normal().
z() == 0.0)
throw runtime_error(
"could not project point. Unexpected case !") ;
261 if(P1->
equation(v2) < -_EPS) res |= Lower ;
262 if(P1->
equation(v2) > _EPS) res |= Upper ;
263 if(P2->
equation(v1) < -_EPS) res |= Upper ;
264 if(P2->
equation(v1) > _EPS) res |= Lower ;
278 -(
double)_EPS,t1,t2 ))
307 for(
int j=0;j<nq;j++)
312 double Z = (q1-p)^(q2-p) ;
318 if((MaxZ <= -I_EPS*I_EPS)||(MinZ >= I_EPS*I_EPS))
330 case Independent:
return Independent ;
331 case Lower:
return Upper ;
332 case Upper:
return Lower ;
333 case Upper | Lower:
return Upper | Lower ;
335 throw runtime_error(
"Unexpected value.") ;
347 double & t1,
double & t2)
358 double a2 = -(Q2y - P2y) ;
359 double b2 = (Q2x - P2x) ;
360 double c2 = P2x*a2+P2y*b2 ;
362 double a1 = -(Q1y - P1y) ;
363 double b1 = (Q1x - P1x) ;
364 double c1 = P1x*a1+P1y*b1 ;
366 double d2 = a2*(Q1x-P1x)+b2*(Q1y-P1y) ;
367 double d1 = a1*(Q2x-P2x)+b1*(Q2y-P2y) ;
369 if((fabs(d2) <= fabs(I_EPS))||(fabs(d1) <= fabs(I_EPS)))
371 if(fabs(a2*P1x + b2*P1y - c2) >= I_EPS)
378 tP1 = (P2x-P1x)/(Q1x-P1x) ;
379 tQ1 = (Q2x-P1x)/(Q1x-P1x) ;
383 tP1 = (P2y-P1y)/(Q1y-P1y) ;
384 tQ1 = (Q2y-P1y)/(Q1y-P1y) ;
389 printf(
"IntersectSegments2D:: Error ! One segment has length 0\n") ;
390 printf(
"This special case is not treated yet.\n") ;
395 double tPQM = max(tP1,tQ1) ;
396 double tPQm = min(tP1,tQ1) ;
398 if(( tPQM < -I_EPS) || (tPQm > 1.0+I_EPS))
410 t2 = (P1x-P2x)/(Q2x-P2x) ;
412 t2 = (P1y-P2y)/(Q2y-P2y) ;
416 printf(
"IntersectSegments2D:: Error ! One segment has length 0\n") ;
417 printf(
"This special case is not treated yet.\n") ;
427 t2 = (c1 - a1*P2x - b1*P2y)/d1 ;
428 t1 = (c2 - a2*P1x - b2*P1y)/d2 ;
430 if((t2 > 1+I_EPS)||(t2 < -I_EPS)||(t1 > 1+I_EPS)||(t1 < -I_EPS))
462 vector<int>& signs,vector<double>& zvals,
int& Smin,
int& Smax,
double I_EPS)
465 throw runtime_error(
"Null primitive in getsigns !") ;
480 double Z = P->
vertex(i) * v - C ;
482 if(Z > zmax) zmax = Z ;
483 if(Z < zmin) zmin = Z ;
492 if(zvals[j] < -I_EPS)
494 else if(zvals[j] > I_EPS)
499 if(Smin > signs[j]) Smin = signs[j] ;
500 if(Smax < signs[j]) Smax = signs[j] ;
507 vector<double> Zvals ;
515 getsigns(P,v,C,Signs,Zvals,Smin,Smax,_EPS) ;
519 if((Smin == 0)&&(Smax == 0)){ P_moins = P ; P_plus = NULL ; return ; }
520 if(Smin == 1) { P_plus = P ; P_moins = NULL ; return ; }
521 if(Smax == -1) { P_plus = NULL ; P_moins = P ; return ; }
523 if((Smin == -1)&&(Smax == 0)) { P_plus = NULL ; P_moins = P ; return ; }
524 if((Smin == 0)&&(Smax == 1)) { P_plus = P ; P_moins = NULL ; return ; }
528 vector<Feedback3DColor> Ps ;
529 vector<Feedback3DColor> Ms ;
542 if(Signs[(i+1)%n] == 0)
548 if((nZero > 2)||(nconsZero > 0)) { P_moins = P ; P_plus = NULL ; return ; }
550 int dep=0 ;
while(Signs[dep] == 0) dep++ ;
551 int prev_sign = Signs[dep] ;
553 for(
int j=1;j<=n;j++)
555 int sign = Signs[(j+dep)%n] ;
557 if(sign == prev_sign)
562 else if(sign == -prev_sign)
567 double Z1 = Zvals[(j+dep-1)%n] ;
568 double Z2 = Zvals[(j+dep)%n] ;
570 double t = fabs(Z1/(Z2 - Z1)) ;
572 if((t < 0.0)||(t > 1.0))
574 if(t > 1.0) t = 1.0 ;
575 if(t < 0.0) t = 0.0 ;
579 Ps.push_back(newVertex) ;
580 Ms.push_back(newVertex) ;
594 Ps.push_back(newVertex) ;
595 Ms.push_back(newVertex) ;
597 prev_sign = -prev_sign ;
601 if(Ps.size() > 100 || Ms.size() > 100 )
602 printf(
"Primitive::split: Error. nPs = %d, nMs = %d.\n",
int(Ps.size()),
int(Ms.size())) ;
607 P_plus =
new Point(Ps[0]) ;
608 else if(Ps.size() == 2)
609 P_plus =
new Segment(Ps[0],Ps[1]) ;
614 P_moins =
new Point(Ms[0]) ;
615 else if(Ms.size() == 2)
616 P_moins =
new Segment(Ms[0],Ms[1]) ;
623 if(v*P->
vertex(0)-C > -_EPS)
638 vector<double> Zvals ;
646 getsigns(S,v,C,Signs,Zvals,Smin,Smax,_EPS) ;
650 if((Smin == 0)&&(Smax == 0)) { P_moins = S ; P_plus = NULL ; return ; }
651 if(Smin == 1) { P_plus = S ; P_moins = NULL ; return ; }
652 if(Smax == -1) { P_plus = NULL ; P_moins = S ; return ; }
654 if((Smin == -1)&&(Smax == 0)) { P_plus = NULL ; P_moins = S ; return ; }
655 if((Smin == 0)&&(Smax == 1)) { P_plus = S ; P_moins = NULL ; return ; }
669 if(Signs[(i+1)%n] == 0)
675 if((nZero > 2)||(nconsZero > 0)) { P_moins = S ; P_plus = NULL ; return ; }
677 double Z1 = Zvals[0] ;
678 double Z2 = Zvals[1] ;
680 double t = fabs(Z1/(Z2 - Z1)) ;
682 if((t < 0.0)||(t > 1.0))
684 if(t > 1.0) t = 1.0 ;
685 if(t < 0.0) t = 0.0 ;
virtual unsigned int nbVertices() const
static gpc_polygon createGPCPolygon_XY(const Polygone *P)
static void splitPrimitive(Primitive *P, const NVector3 &v, double c, Primitive *&prim_up, Primitive *&prim_lo)
gpc_vertex_list * contour
void gpc_polygon_clip(gpc_op op, gpc_polygon *subj, gpc_polygon *clip, gpc_polygon *result)
double equation(const Vector3 &p) const
virtual AxisAlignedBox_xyz bbox() const =0
virtual const Vector3 & vertex(int) const
static int computeRelativePosition(const Primitive *p1, const Primitive *p2)
static int inverseRP(int)
virtual const Vector3 & vertex(int) const
virtual const Feedback3DColor & sommet3DColor(int) const
static void getsigns(const Primitive *P, const NVector3 &v, double C, std::vector< int > &signs, std::vector< double > &zvals, int &Smin, int &Smax, double I_EPS)
static void split(Segment *S, const NVector3 &v, double C, Primitive *&P_plus, Primitive *&P_moins)
static bool pointOutOfPolygon_XY(const Vector3 &P, const Polygone *Q, double I_EPS)
const NVector3 & normal() const
virtual const Vector3 & vertex(int) const
virtual const Vector3 & vertex(int) const =0
void gpc_free_polygon(gpc_polygon *p)
virtual unsigned int nbVertices() const =0
void gpc_add_contour(gpc_polygon *p, gpc_vertex_list *new_contour, int hole)
static bool intersectSegments_XY(const Vector2 &P1, const Vector2 &Q1, const Vector2 &P2, const Vector2 &Q2, double I_EPS, double &t1, double &t2)
virtual const Feedback3DColor & sommet3DColor(int i) const
virtual unsigned int nbVertices() const