0024177: Eliminate CLang compiler warning -Wlogical-op-parentheses (&& within ||)
[occt.git] / src / BRepFill / BRepFill_TrimEdgeTool.cxx
1 // Created on: 1995-04-24
2 // Created by: Bruno DUMORTIER
3 // Copyright (c) 1995-1999 Matra Datavision
4 // Copyright (c) 1999-2012 OPEN CASCADE SAS
5 //
6 // The content of this file is subject to the Open CASCADE Technology Public
7 // License Version 6.5 (the "License"). You may not use the content of this file
8 // except in compliance with the License. Please obtain a copy of the License
9 // at http://www.opencascade.org and read it completely before using this file.
10 //
11 // The Initial Developer of the Original Code is Open CASCADE S.A.S., having its
12 // main offices at: 1, place des Freres Montgolfier, 78280 Guyancourt, France.
13 //
14 // The Original Code and all software distributed under the License is
15 // distributed on an "AS IS" basis, without warranty of any kind, and the
16 // Initial Developer hereby disclaims all such warranties, including without
17 // limitation, any warranties of merchantability, fitness for a particular
18 // purpose or non-infringement. Please see the License for the specific terms
19 // and conditions governing the rights and limitations under the License.
20
21
22
23 #include <BRepFill_TrimEdgeTool.ixx>
24 #include <BRep_Tool.hxx>
25 #include <Bisector_BisecAna.hxx>
26 #include <Geom2d_Curve.hxx>
27 #include <Geom2d_TrimmedCurve.hxx>
28 #include <Geom2d_CartesianPoint.hxx>
29 #include <Geom_Curve.hxx>
30 #include <GeomProjLib.hxx>
31 #include <Geom_TrimmedCurve.hxx>
32 #include <Geom2dAPI_ProjectPointOnCurve.hxx>
33 #include <Geom2dInt_GInter.hxx>
34 #include <gp_Pnt.hxx>
35 #include <TopLoc_Location.hxx>
36 #include <TopoDS.hxx>
37 #include <Precision.hxx>
38 #include <IntRes2d_IntersectionPoint.hxx>
39 #include <IntRes2d_IntersectionSegment.hxx>
40
41 #include <StdFail_NotDone.hxx>
42
43 #ifdef DRAW
44 #include <DrawTrSurf.hxx>
45 #include <DBRep.hxx>
46 static Standard_Boolean Affich       = Standard_False;
47 #endif
48
49
50 //=======================================================================
51 //function : SimpleExpression
52 //purpose  : 
53 //=======================================================================
54
55 static void SimpleExpression (const Bisector_Bisec&        B, 
56                                     Handle(Geom2d_Curve)&  Bis)
57 {
58   Bis = B.Value();
59
60   Handle(Standard_Type) BT = Bis->DynamicType();
61   if (BT == STANDARD_TYPE(Geom2d_TrimmedCurve)) {
62     Handle(Geom2d_TrimmedCurve) TrBis 
63       = Handle(Geom2d_TrimmedCurve)::DownCast(Bis);
64     Handle(Geom2d_Curve) BasBis = TrBis->BasisCurve();
65     BT = BasBis->DynamicType();
66     if (BT == STANDARD_TYPE(Bisector_BisecAna)) {
67       Bis = Handle(Bisector_BisecAna)::DownCast(BasBis)->Geom2dCurve();
68       Bis = new Geom2d_TrimmedCurve (Bis,
69                                      TrBis->FirstParameter(),
70                                      TrBis->LastParameter());
71     }
72   }
73 }
74
75
76 //=======================================================================
77 //function : BRepFill_TrimEdgeTool
78 //purpose  : 
79 //=======================================================================
80
81 BRepFill_TrimEdgeTool::BRepFill_TrimEdgeTool()
82 {
83 }
84
85
86 //=======================================================================
87 //function : BRepFill_TrimEdgeTool
88 //purpose  : 
89 //=======================================================================
90
91 BRepFill_TrimEdgeTool::BRepFill_TrimEdgeTool
92 (const Bisector_Bisec& Bisec,
93  const Handle(Geom2d_Geometry)& S1,
94  const Handle(Geom2d_Geometry)& S2,
95  const Standard_Real   Offset) :
96 myOffset(Offset),
97 myBisec(Bisec)
98 {
99   isPoint1 = (S1->DynamicType() == STANDARD_TYPE(Geom2d_CartesianPoint));
100   isPoint2 = (S2->DynamicType() == STANDARD_TYPE(Geom2d_CartesianPoint));
101
102 // return geometries of shapes.
103 //  Standard_Real f,l;
104   if (isPoint1) {
105     myP1 = Handle(Geom2d_Point)::DownCast(S1)->Pnt2d();
106   }
107   else {
108     myC1 = Handle(Geom2d_Curve)::DownCast(S1);
109 #ifdef DRAW
110     if ( Affich) {
111 //POP pour NT
112       char* myC1name = "myC1";
113       DrawTrSurf::Set(myC1name,myC1);
114 //      DrawTrSurf::Set("myC1",myC1);
115     }
116 #endif
117   }
118   if (isPoint2) {
119     myP2 = Handle(Geom2d_Point)::DownCast(S2)->Pnt2d();
120   }
121   else {
122     myC2 = Handle(Geom2d_Curve)::DownCast(S2);
123 #ifdef DRAW
124     if ( Affich) {
125       char* myC2name = "myC2";
126       DrawTrSurf::Set(myC2name,myC2);
127 //      DrawTrSurf::Set("myC2",myC2);
128     }
129 #endif
130   }
131   // return the simple expression of the bissectrice
132   Handle(Geom2d_Curve) Bis;
133   SimpleExpression(myBisec, Bis);
134   myBis = Geom2dAdaptor_Curve(Bis);
135 #ifdef DRAW
136   if ( Affich) {
137       char* myBisname = "myBis";
138       DrawTrSurf::Set(myBisname,Bis);
139   }
140 #endif
141
142 }
143
144 //=======================================================================
145 //function : Bubble
146 //purpose  : Order the sequence of points by increasing x. 
147 //=======================================================================
148
149 static void Bubble(TColgp_SequenceOfPnt& Seq) 
150 {
151   Standard_Boolean Invert = Standard_True;
152   Standard_Integer NbPoints = Seq.Length();
153   while (Invert) {
154     Invert = Standard_False;
155     for ( Standard_Integer i = 1; i < NbPoints; i++) {
156       gp_Pnt P1 = Seq.Value(i);
157       gp_Pnt P2 = Seq.Value(i+1);
158       if (P2.X()<P1.X())  {
159         Seq.Exchange(i,i+1);
160         Invert = Standard_True;
161       }
162     }
163   }
164 }
165
166
167 //=======================================================================
168 //function : EvalParameters  
169 //purpose  : 
170 //=======================================================================
171
172 static void EvalParameters(const Geom2dAdaptor_Curve& Bis,
173                            const Geom2dAdaptor_Curve& AC,
174                                  TColgp_SequenceOfPnt& Params)
175 {
176   Geom2dInt_GInter Intersector;
177   Standard_Real Tol = Precision::Confusion();
178 //  Standard_Real TolC = 1.e-9;
179
180   Geom2dAdaptor_Curve CBis(Bis);
181   Geom2dAdaptor_Curve CAC (AC);
182
183   //Intersector = Geom2dInt_GInter(CBis, CAC, TolC, Tol);
184   Intersector = Geom2dInt_GInter(CAC, CBis, Tol, Tol);
185
186   Standard_Integer NbPoints, NbSegments;
187   Standard_Real U1, U2;
188   gp_Pnt P;
189
190   if ( !Intersector.IsDone()) {
191     StdFail_NotDone::Raise("BRepFill_TrimSurfaceTool::IntersectWith");
192   }
193   
194   NbPoints = Intersector.NbPoints();
195   
196   if (NbPoints > 0) {
197     for ( Standard_Integer i = 1; i <= NbPoints; i++) {
198       U1 = Intersector.Point(i).ParamOnSecond();
199       U2 = Intersector.Point(i).ParamOnFirst();
200       P = gp_Pnt(U1,U2,0.);
201       Params.Append(P);
202     }
203     
204   }
205   
206   NbSegments = Intersector.NbSegments();
207   
208   if (NbSegments > 0) {
209     IntRes2d_IntersectionSegment Seg;
210     for ( Standard_Integer i = 1; i <= NbSegments; i++) {
211       Seg = Intersector.Segment(i);
212       U1  = Seg.FirstPoint().ParamOnSecond();
213       Standard_Real Ulast = Seg.LastPoint().ParamOnSecond();
214       if ( Abs(U1    - CBis.FirstParameter()) <= Tol &&
215            Abs(Ulast - CBis.LastParameter())  <= Tol    ) {
216         P = gp_Pnt(U1,Seg.FirstPoint().ParamOnFirst(),0.);
217         Params.Append(P);
218         P = gp_Pnt(Ulast,Seg.LastPoint().ParamOnFirst(),0.);
219         Params.Append(P);
220       }
221       else {
222         U1 += Seg.LastPoint().ParamOnSecond();
223         U1 /= 2.;
224         U2  = Seg.FirstPoint().ParamOnFirst();
225         U2 += Seg.LastPoint().ParamOnFirst();
226         U2 /= 2.;
227         P = gp_Pnt(U1,U2,0.);
228         Params.Append(P);
229       }
230     }
231   }
232
233   // Order the sequence by growing parameter on the bissectrice.
234   Bubble( Params);
235 }
236                            
237 static void EvalParametersBis(const Geom2dAdaptor_Curve& Bis,
238                               const Geom2dAdaptor_Curve& AC,
239                               TColgp_SequenceOfPnt& Params,
240                               const Standard_Real Tol)
241 {
242   Geom2dInt_GInter Intersector;
243   Standard_Real TolC = Tol;
244   
245   Geom2dAdaptor_Curve CBis(Bis);
246   Geom2dAdaptor_Curve CAC (AC);
247
248   Intersector = Geom2dInt_GInter(CAC, CBis, TolC, Tol);
249
250   Standard_Integer NbPoints, NbSegments;
251   Standard_Real U1, U2;
252   gp_Pnt P;
253
254   if ( !Intersector.IsDone()) {
255     StdFail_NotDone::Raise("BRepFill_TrimSurfaceTool::IntersectWith");
256   }
257   
258   NbPoints = Intersector.NbPoints();
259   
260   if (NbPoints > 0) {
261     for ( Standard_Integer i = 1; i <= NbPoints; i++) {
262       U1 = Intersector.Point(i).ParamOnSecond();
263       U2 = Intersector.Point(i).ParamOnFirst();
264       P = gp_Pnt(U1,U2,0.);
265       Params.Append(P);
266     }
267     
268   }
269   
270   NbSegments = Intersector.NbSegments();
271   
272   if (NbSegments > 0) {
273     IntRes2d_IntersectionSegment Seg;
274     for ( Standard_Integer i = 1; i <= NbSegments; i++) {
275       Seg = Intersector.Segment(i);
276       U1  = Seg.FirstPoint().ParamOnSecond();
277       Standard_Real Ulast = Seg.LastPoint().ParamOnSecond();
278       if ( Abs(U1    - CBis.FirstParameter()) <= Tol &&
279            Abs(Ulast - CBis.LastParameter())  <= Tol    ) {
280         P = gp_Pnt(U1,Seg.FirstPoint().ParamOnFirst(),0.);
281         Params.Append(P);
282         P = gp_Pnt(Ulast,Seg.LastPoint().ParamOnFirst(),0.);
283         Params.Append(P);
284       }
285       else {
286         U1 += Seg.LastPoint().ParamOnSecond();
287         U1 /= 2.;
288         U2  = Seg.FirstPoint().ParamOnFirst();
289         U2 += Seg.LastPoint().ParamOnFirst();
290         U2 /= 2.;
291         P = gp_Pnt(U1,U2,0.);
292         Params.Append(P);
293       }
294     }
295   }
296
297   // Order the sequence by parameter growing on the bissectrice.
298   Bubble( Params);
299 }
300
301
302 //=======================================================================
303 //function : IntersectWith
304 //purpose  : 
305 //=======================================================================
306
307 void BRepFill_TrimEdgeTool::IntersectWith(const TopoDS_Edge& Edge1,
308                                           const TopoDS_Edge& Edge2,
309                                                 TColgp_SequenceOfPnt& Params)
310 {
311   Params.Clear();
312
313   // return curves associated to edges.
314   TopLoc_Location L;
315   Standard_Real   f,l;
316   Handle(Geom_Surface) Surf;
317
318   Handle(Geom2d_Curve) C1;
319   BRep_Tool::CurveOnSurface(Edge1,C1,Surf,L,f,l);
320   Geom2dAdaptor_Curve AC1(C1,f,l);
321
322   Handle(Geom2d_Curve) C2;
323   BRep_Tool::CurveOnSurface(Edge2,C2,Surf,L,f,l);
324   Geom2dAdaptor_Curve AC2(C2,f,l);
325
326 #ifdef DRAW
327   if ( Affich) {
328     f = AC1.FirstParameter();
329     l = AC1.LastParameter();
330     char* CURVE1name = "CURVE1";
331     DrawTrSurf::Set(CURVE1name, new Geom2d_TrimmedCurve(C1,f,l));
332     f = AC2.FirstParameter();
333     l = AC2.LastParameter();
334     char* CURVE2name = "CURVE2";
335     DrawTrSurf::Set(CURVE2name, new Geom2d_TrimmedCurve(C2,f,l));
336     f = myBis.FirstParameter();
337     l = myBis.LastParameter();
338     char* bisname = "BIS";
339     DrawTrSurf::Set(bisname, new Geom2d_TrimmedCurve(myBis.Curve(),f,l));
340     char* Edge1name = "E1";
341     DBRep::Set(Edge1name, Edge1);
342     char* Edge2name = "E2";
343     DBRep::Set(Edge2name, Edge2);
344
345   }
346 #endif
347   
348   // Calculate intersection
349   TColgp_SequenceOfPnt Points2;
350   gp_Pnt PSeq;
351
352   EvalParameters (myBis,AC1,Params);
353   EvalParameters (myBis,AC2,Points2);
354
355
356   Standard_Integer SeanceDeRattrapage=0;
357   Standard_Real TolInit= 1.e-9;
358   Standard_Integer nn = 7;
359
360   if((AC1.GetType() != GeomAbs_Circle && AC1.GetType() != GeomAbs_Line) ||
361      (AC2.GetType() != GeomAbs_Circle && AC2.GetType() != GeomAbs_Line)) {
362
363     TolInit = 1.e-8;
364     nn = 6;
365   }
366   
367   while (     SeanceDeRattrapage < nn // TolInit <= 0.01
368          && ( Points2.Length() != Params.Length() || 
369              (Points2.Length() == 0 && Params.Length() == 0) ) ) {
370
371 #ifdef DEB
372     cout << "BRepFill_TrimEdgeTool: incoherent intersection. Try with a greater tolerance" << endl;
373 #endif
374
375     Params.Clear();
376     Points2.Clear();
377     
378     TolInit*=10.0;
379     
380     EvalParametersBis(myBis,AC1,Params,TolInit);
381     EvalParametersBis(myBis,AC2,Points2,TolInit); 
382     SeanceDeRattrapage++;
383   }
384
385 #ifdef DEB
386   if(SeanceDeRattrapage != 0) cout << "SeanceDeRattrapage = " << SeanceDeRattrapage << endl;
387   if(SeanceDeRattrapage == nn) { 
388     cout << "BRepFill_TrimEdgeTool: incoherent intersection" << endl;
389   }
390 #endif
391
392
393   if(Params.Length() == 0 && Points2.Length() == 1) {
394
395     //cout << "Params.Length() == 0 && Points2.Length() == 1" << endl;
396     Standard_Real dmin;
397     Standard_Real tBis = Points2(1).X();
398     gp_Pnt2d PBis = myBis.Value(tBis);
399
400     Standard_Real t = AC1.FirstParameter();
401     gp_Pnt2d PC = AC1.Value(t);
402     dmin = PC.SquareDistance(PBis);
403     gp_Pnt P(tBis, t, 0.);
404     Params.Append(P);
405
406     t = AC1.LastParameter();
407     PC = AC1.Value(t);
408     if(dmin > PC.SquareDistance(PBis)) {
409       P.SetY(t);
410       Params.SetValue(1,P);
411     }
412   }
413   else if(Params.Length() == 1 && Points2.Length() == 0) {
414
415     //cout << "Params.Length() == 1 && Points2.Length() == 0" << endl;
416     Standard_Real dmin;
417     Standard_Real tBis = Params(1).X();
418     gp_Pnt2d PBis = myBis.Value(tBis);
419
420     Standard_Real t = AC2.FirstParameter();
421     gp_Pnt2d PC = AC2.Value(t);
422     dmin = PC.SquareDistance(PBis);
423     gp_Pnt P(tBis, t, 0.);
424     Points2.Append(P);
425
426     t = AC2.LastParameter();
427     PC = AC2.Value(t);
428     if(dmin > PC.SquareDistance(PBis)) {
429       P.SetY(t);
430       Points2.SetValue(1,P);
431     }
432   }
433
434   // small manipulation to remove incorrect intersections:
435   // return only common intersections (same parameter
436   // on the bissectrice.).
437   // The tolerance can be eventually changed.
438
439   gp_Pnt P1,P2;
440   Standard_Real Tol = 4 * 100 * Precision::PConfusion();
441   Standard_Integer i = 1;
442   Standard_Integer NbPoints = Params.Length();
443
444   if(NbPoints == 1 && Points2.Length() == 1) {
445     //cout << "NbPoints == 1 && Points2.Length() == 1" << endl;
446     for ( i = 1; i <= NbPoints; i++) {
447       PSeq = Params(i);
448       PSeq.SetZ((Points2.Value(i)).Y());
449       Params.SetValue(i,PSeq);
450     }
451     return;
452   }    
453
454   i = 1;
455   while ( i <= Min( Params.Length(), Points2.Length())) {
456     P1 = Params(i);
457     P2 = Points2(i);
458     Standard_Real P1xP2x=Abs( P1.X() - P2.X());
459
460     if ( P1xP2x > Tol ) {
461 #ifdef DEB
462       cout << "BRepFill_TrimEdgeTool: no same parameter on the bissectrice" << endl;
463 #endif
464       if(P1xP2x>TolInit) { 
465 #ifdef DEB
466       cout << "BRepFill_TrimEdgeTool: Continue somehow" << endl;
467 #endif  
468       i++;
469       }
470       else { 
471         if ( P1.X() < P2.X()) Params.Remove(i);
472         else                  Points2.Remove(i);
473       }
474     }
475     else i++;
476   }
477
478   if ( Params.Length() > Points2.Length()) {
479     Params.Remove(Points2.Length()+1, Params.Length());
480   }
481   else if ( Params.Length() < Points2.Length()) {
482     Points2.Remove(Params.Length()+1, Points2.Length());
483   }
484
485   NbPoints = Params.Length();
486   for ( i = 1; i <= NbPoints; i++) {
487     PSeq = Params(i);
488     PSeq.SetZ((Points2.Value(i)).Y());
489     Params.SetValue(i,PSeq);
490   }
491 }
492
493 //=======================================================================
494 //function : AddOrConfuse
495 //purpose  : the first or the last point of the bissectrice is on the 
496 //           parallel if it was not found in the intersections, 
497 //           it is projected on parallel lines and added in the parameters 
498 //=======================================================================
499
500 void BRepFill_TrimEdgeTool::AddOrConfuse(const Standard_Boolean  Start,
501                                          const TopoDS_Edge&      Edge1,
502                                          const TopoDS_Edge&      Edge2,
503                                          TColgp_SequenceOfPnt&   Params) 
504 const 
505 {
506   Standard_Boolean  ToProj = Standard_True;
507   gp_Pnt2d          PBis;
508   Standard_Real     Tol = 10*Precision::Confusion(); 
509
510   // return curves associated to edges.
511   TopLoc_Location L;
512   Standard_Real   f,l;
513   Handle(Geom_Surface) Surf;
514
515   Handle(Geom2d_Curve) C1;
516   BRep_Tool::CurveOnSurface(Edge1,C1,Surf,L,f,l);
517   Geom2dAdaptor_Curve AC1(C1,f,l);
518
519
520   if (Start) PBis = myBis.Value(myBis.FirstParameter());
521   else       PBis = myBis.Value(myBis.LastParameter ()); 
522
523   // Test if the end of the bissectrice is in the set of intersection points.
524   if (!Params.IsEmpty()) {
525     gp_Pnt2d P;
526     if (Start) P = AC1.Value(Params.First().Y());
527     else       P = AC1.Value(Params.Last ().Y()); 
528     ToProj     = !PBis.IsEqual(P,Tol);
529   }
530   
531   if (ToProj) {
532 #ifdef DEB
533     cout << " project extremity bissectrice on parallel."<<endl;
534 #endif
535
536     // Project point on parallels and add in Params
537
538     Standard_Real f2,l2;
539     Handle(Geom2d_Curve) C2;
540     BRep_Tool::CurveOnSurface(Edge2,C2,Surf,L,f2,l2);
541
542     Geom2dAPI_ProjectPointOnCurve Projector1(PBis,C1,f,l);    
543     Geom2dAPI_ProjectPointOnCurve Projector2(PBis,C2,f2,l2);
544
545     if (Projector1.NbPoints() == 0) {
546 #ifdef DEB
547       cout << "Failed projection in BRepFill_TrimEdgeTool::AddOrConfuse"<<endl;
548 #endif
549       return;
550     }
551     if (!Projector1.NearestPoint().IsEqual(PBis,Tol)) {
552 #ifdef DEB
553       cout <<"Incorrect solution in BRepFill_TrimEdgeTool::AddOrConfuse"<<endl;
554 #endif
555       return;
556     }
557     if (Projector2.NbPoints() == 0) {
558 #ifdef DEB
559       cout << "Failed projection in BRepFill_TrimEdgeTool::AddOrConfuse"<<endl;
560 #endif
561       return;
562     }
563     if (!Projector2.NearestPoint().IsEqual(PBis,Tol)) {
564 #ifdef DEB
565       cout <<" Mauvaisesolution dans BRepFill_TrimEdgeTool::AddOrConfuse"<<endl;
566 #endif
567       return;
568     }
569     gp_Pnt PInt (0,
570                  Projector1.LowerDistanceParameter(),
571                  Projector2.LowerDistanceParameter());
572     if (Start) {
573       PInt.SetX (myBis.FirstParameter());
574       Params.Prepend(PInt);
575     }
576     else {
577       PInt.SetX (myBis.LastParameter());
578       Params.Append(PInt);
579     }
580   }
581 }
582
583 //=======================================================================
584 //function : IsInside
585 //purpose  : 
586 //=======================================================================
587
588 Standard_Boolean BRepFill_TrimEdgeTool::IsInside(const gp_Pnt2d& P) const 
589 {
590 //  Modified by Sergey KHROMOV - Fri Sep 27 11:43:12 2002 Begin
591 //   Standard_Real Dist;
592   Standard_Real Dist = RealLast();
593 //  Modified by Sergey KHROMOV - Fri Sep 27 11:43:12 2002 End
594   if (isPoint1) 
595     Dist = P.Distance(myP1);
596   else if (isPoint2) 
597     Dist = P.Distance(myP2);
598   else {
599     Geom2dAPI_ProjectPointOnCurve Projector(P,myC1);
600     if (Projector.NbPoints() > 0) {
601       Dist = Projector.LowerDistance();
602     }
603 //  Modified by Sergey KHROMOV - Fri Sep 27 11:43:43 2002 Begin
604 //     else {
605 //       gp_Pnt2d PF = myC1->Value(myC1->FirstParameter());
606 //       gp_Pnt2d PL = myC1->Value(myC1->LastParameter());
607 //       Dist = Min (P.Distance(PF),P.Distance(PL));
608 //     }
609
610 // Check of distances between P and first and last point of the first curve
611 // should be performed in any case, despite of the results of projection.
612     gp_Pnt2d      PF       = myC1->Value(myC1->FirstParameter());
613     gp_Pnt2d      PL       = myC1->Value(myC1->LastParameter());
614     Standard_Real aDistMin = Min (P.Distance(PF),P.Distance(PL));
615
616     if (Dist > aDistMin)
617       Dist = aDistMin;
618 //  Modified by Sergey KHROMOV - Fri Sep 27 11:43:44 2002 End
619   }
620   
621 //  return (Dist < Abs(myOffset);
622 // return (Dist < Abs(myOffset) + Precision::Confusion());
623   return (Dist < Abs(myOffset) - Precision::Confusion());
624 }
625