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
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.
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.
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.
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>
35 #include <TopLoc_Location.hxx>
37 #include <Precision.hxx>
38 #include <IntRes2d_IntersectionPoint.hxx>
39 #include <IntRes2d_IntersectionSegment.hxx>
41 #include <StdFail_NotDone.hxx>
44 #include <DrawTrSurf.hxx>
46 static Standard_Boolean Affich = Standard_False;
50 //=======================================================================
51 //function : SimpleExpression
53 //=======================================================================
55 static void SimpleExpression (const Bisector_Bisec& B,
56 Handle(Geom2d_Curve)& Bis)
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());
76 //=======================================================================
77 //function : BRepFill_TrimEdgeTool
79 //=======================================================================
81 BRepFill_TrimEdgeTool::BRepFill_TrimEdgeTool()
86 //=======================================================================
87 //function : BRepFill_TrimEdgeTool
89 //=======================================================================
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) :
99 isPoint1 = (S1->DynamicType() == STANDARD_TYPE(Geom2d_CartesianPoint));
100 isPoint2 = (S2->DynamicType() == STANDARD_TYPE(Geom2d_CartesianPoint));
102 // return geometries of shapes.
103 // Standard_Real f,l;
105 myP1 = Handle(Geom2d_Point)::DownCast(S1)->Pnt2d();
108 myC1 = Handle(Geom2d_Curve)::DownCast(S1);
112 char* myC1name = "myC1";
113 DrawTrSurf::Set(myC1name,myC1);
114 // DrawTrSurf::Set("myC1",myC1);
119 myP2 = Handle(Geom2d_Point)::DownCast(S2)->Pnt2d();
122 myC2 = Handle(Geom2d_Curve)::DownCast(S2);
125 char* myC2name = "myC2";
126 DrawTrSurf::Set(myC2name,myC2);
127 // DrawTrSurf::Set("myC2",myC2);
131 // return the simple expression of the bissectrice
132 Handle(Geom2d_Curve) Bis;
133 SimpleExpression(myBisec, Bis);
134 myBis = Geom2dAdaptor_Curve(Bis);
137 char* myBisname = "myBis";
138 DrawTrSurf::Set(myBisname,Bis);
144 //=======================================================================
146 //purpose : Order the sequence of points by increasing x.
147 //=======================================================================
149 static void Bubble(TColgp_SequenceOfPnt& Seq)
151 Standard_Boolean Invert = Standard_True;
152 Standard_Integer NbPoints = Seq.Length();
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);
160 Invert = Standard_True;
167 //=======================================================================
168 //function : EvalParameters
170 //=======================================================================
172 static void EvalParameters(const Geom2dAdaptor_Curve& Bis,
173 const Geom2dAdaptor_Curve& AC,
174 TColgp_SequenceOfPnt& Params)
176 Geom2dInt_GInter Intersector;
177 Standard_Real Tol = Precision::Intersection();
178 // Standard_Real TolC = 1.e-9;
180 Geom2dAdaptor_Curve CBis(Bis);
181 Geom2dAdaptor_Curve CAC (AC);
183 if(AC.GetType() != GeomAbs_Circle || AC.GetType() != GeomAbs_Line) {
187 //Intersector = Geom2dInt_GInter(CBis, CAC, TolC, Tol);
188 Intersector = Geom2dInt_GInter(CAC, CBis, Tol, Tol);
190 Standard_Integer NbPoints, NbSegments;
191 Standard_Real U1, U2;
194 if ( !Intersector.IsDone()) {
195 StdFail_NotDone::Raise("BRepFill_TrimSurfaceTool::IntersectWith");
198 NbPoints = Intersector.NbPoints();
201 for ( Standard_Integer i = 1; i <= NbPoints; i++) {
202 U1 = Intersector.Point(i).ParamOnSecond();
203 U2 = Intersector.Point(i).ParamOnFirst();
204 P = gp_Pnt(U1,U2,0.);
210 NbSegments = Intersector.NbSegments();
212 if (NbSegments > 0) {
213 IntRes2d_IntersectionSegment Seg;
214 for ( Standard_Integer i = 1; i <= NbSegments; i++) {
215 Seg = Intersector.Segment(i);
216 U1 = Seg.FirstPoint().ParamOnSecond();
217 Standard_Real Ulast = Seg.LastPoint().ParamOnSecond();
218 if ( Abs(U1 - CBis.FirstParameter()) <= Tol &&
219 Abs(Ulast - CBis.LastParameter()) <= Tol ) {
220 P = gp_Pnt(U1,Seg.FirstPoint().ParamOnFirst(),0.);
222 P = gp_Pnt(Ulast,Seg.LastPoint().ParamOnFirst(),0.);
226 U1 += Seg.LastPoint().ParamOnSecond();
228 U2 = Seg.FirstPoint().ParamOnFirst();
229 U2 += Seg.LastPoint().ParamOnFirst();
231 P = gp_Pnt(U1,U2,0.);
237 // Order the sequence by growing parameter on the bissectrice.
241 static void EvalParametersBis(const Geom2dAdaptor_Curve& Bis,
242 const Geom2dAdaptor_Curve& AC,
243 TColgp_SequenceOfPnt& Params,
244 const Standard_Real Tol)
246 Geom2dInt_GInter Intersector;
247 Standard_Real TolC = Tol;
249 Geom2dAdaptor_Curve CBis(Bis);
250 Geom2dAdaptor_Curve CAC (AC);
252 Intersector = Geom2dInt_GInter(CAC, CBis, TolC, Tol);
254 Standard_Integer NbPoints, NbSegments;
255 Standard_Real U1, U2;
258 if ( !Intersector.IsDone()) {
259 StdFail_NotDone::Raise("BRepFill_TrimSurfaceTool::IntersectWith");
262 NbPoints = Intersector.NbPoints();
265 for ( Standard_Integer i = 1; i <= NbPoints; i++) {
266 U1 = Intersector.Point(i).ParamOnSecond();
267 U2 = Intersector.Point(i).ParamOnFirst();
268 P = gp_Pnt(U1,U2,0.);
274 NbSegments = Intersector.NbSegments();
276 if (NbSegments > 0) {
277 IntRes2d_IntersectionSegment Seg;
278 for ( Standard_Integer i = 1; i <= NbSegments; i++) {
279 Seg = Intersector.Segment(i);
280 U1 = Seg.FirstPoint().ParamOnSecond();
281 Standard_Real Ulast = Seg.LastPoint().ParamOnSecond();
282 if ( Abs(U1 - CBis.FirstParameter()) <= Tol &&
283 Abs(Ulast - CBis.LastParameter()) <= Tol ) {
284 P = gp_Pnt(U1,Seg.FirstPoint().ParamOnFirst(),0.);
286 P = gp_Pnt(Ulast,Seg.LastPoint().ParamOnFirst(),0.);
290 U1 += Seg.LastPoint().ParamOnSecond();
292 U2 = Seg.FirstPoint().ParamOnFirst();
293 U2 += Seg.LastPoint().ParamOnFirst();
295 P = gp_Pnt(U1,U2,0.);
301 // Order the sequence by parameter growing on the bissectrice.
306 //=======================================================================
307 //function : IntersectWith
309 //=======================================================================
311 void BRepFill_TrimEdgeTool::IntersectWith(const TopoDS_Edge& Edge1,
312 const TopoDS_Edge& Edge2,
313 TColgp_SequenceOfPnt& Params)
317 // return curves associated to edges.
320 Handle(Geom_Surface) Surf;
322 Handle(Geom2d_Curve) C1;
323 BRep_Tool::CurveOnSurface(Edge1,C1,Surf,L,f,l);
324 Geom2dAdaptor_Curve AC1(C1,f,l);
326 Handle(Geom2d_Curve) C2;
327 BRep_Tool::CurveOnSurface(Edge2,C2,Surf,L,f,l);
328 Geom2dAdaptor_Curve AC2(C2,f,l);
332 f = AC1.FirstParameter();
333 l = AC1.LastParameter();
334 char* CURVE1name = "CURVE1";
335 DrawTrSurf::Set(CURVE1name, new Geom2d_TrimmedCurve(C1,f,l));
336 f = AC2.FirstParameter();
337 l = AC2.LastParameter();
338 char* CURVE2name = "CURVE2";
339 DrawTrSurf::Set(CURVE2name, new Geom2d_TrimmedCurve(C2,f,l));
340 f = myBis.FirstParameter();
341 l = myBis.LastParameter();
342 char* bisname = "BIS";
343 DrawTrSurf::Set(bisname, new Geom2d_TrimmedCurve(myBis.Curve(),f,l));
344 char* Edge1name = "E1";
345 DBRep::Set(Edge1name, Edge1);
346 char* Edge2name = "E2";
347 DBRep::Set(Edge2name, Edge2);
352 // Calculate intersection
353 TColgp_SequenceOfPnt Points2;
356 EvalParameters (myBis,AC1,Params);
357 EvalParameters (myBis,AC2,Points2);
360 Standard_Integer SeanceDeRattrapage=0;
361 Standard_Real TolInit= 1.e-9;
362 Standard_Integer nn = 7;
364 if(AC1.GetType() != GeomAbs_Circle ||
365 AC1.GetType() != GeomAbs_Line ||
366 AC2.GetType() != GeomAbs_Circle ||
367 AC2.GetType() != GeomAbs_Line ) {
373 while ( SeanceDeRattrapage < nn // TolInit <= 0.01
374 && ( Points2.Length() != Params.Length() ||
375 (Points2.Length() == 0 && Params.Length() == 0) ) ) {
378 cout << "BRepFill_TrimEdgeTool: incoherent intersection. Try with a greater tolerance" << endl;
386 EvalParametersBis(myBis,AC1,Params,TolInit);
387 EvalParametersBis(myBis,AC2,Points2,TolInit);
388 SeanceDeRattrapage++;
392 if(SeanceDeRattrapage != 0) cout << "SeanceDeRattrapage = " << SeanceDeRattrapage << endl;
393 if(SeanceDeRattrapage == nn) {
394 cout << "BRepFill_TrimEdgeTool: incoherent intersection" << endl;
399 if(Params.Length() == 0 && Points2.Length() == 1) {
401 //cout << "Params.Length() == 0 && Points2.Length() == 1" << endl;
403 Standard_Real tBis = Points2(1).X();
404 gp_Pnt2d PBis = myBis.Value(tBis);
406 Standard_Real t = AC1.FirstParameter();
407 gp_Pnt2d PC = AC1.Value(t);
408 dmin = PC.SquareDistance(PBis);
409 gp_Pnt P(tBis, t, 0.);
412 t = AC1.LastParameter();
414 if(dmin > PC.SquareDistance(PBis)) {
416 Params.SetValue(1,P);
419 else if(Params.Length() == 1 && Points2.Length() == 0) {
421 //cout << "Params.Length() == 1 && Points2.Length() == 0" << endl;
423 Standard_Real tBis = Params(1).X();
424 gp_Pnt2d PBis = myBis.Value(tBis);
426 Standard_Real t = AC2.FirstParameter();
427 gp_Pnt2d PC = AC2.Value(t);
428 dmin = PC.SquareDistance(PBis);
429 gp_Pnt P(tBis, t, 0.);
432 t = AC2.LastParameter();
434 if(dmin > PC.SquareDistance(PBis)) {
436 Points2.SetValue(1,P);
440 // small manipulation to remove incorrect intersections:
441 // return only common intersections (same parameter
442 // on the bissectrice.).
443 // The tolerance can be eventually changed.
446 Standard_Real Tol = 4 * 100 * Precision::PConfusion();
447 Standard_Integer i = 1;
448 Standard_Integer NbPoints = Params.Length();
450 if(NbPoints == 1 && Points2.Length() == 1) {
451 //cout << "NbPoints == 1 && Points2.Length() == 1" << endl;
452 for ( i = 1; i <= NbPoints; i++) {
454 PSeq.SetZ((Points2.Value(i)).Y());
455 Params.SetValue(i,PSeq);
461 while ( i <= Min( Params.Length(), Points2.Length())) {
464 Standard_Real P1xP2x=Abs( P1.X() - P2.X());
466 if ( P1xP2x > Tol ) {
468 cout << "BRepFill_TrimEdgeTool: no same parameter on the bissectrice" << endl;
472 cout << "BRepFill_TrimEdgeTool: Continue somehow" << endl;
477 if ( P1.X() < P2.X()) Params.Remove(i);
478 else Points2.Remove(i);
484 if ( Params.Length() > Points2.Length()) {
485 Params.Remove(Points2.Length()+1, Params.Length());
487 else if ( Params.Length() < Points2.Length()) {
488 Points2.Remove(Params.Length()+1, Points2.Length());
491 NbPoints = Params.Length();
492 for ( i = 1; i <= NbPoints; i++) {
494 PSeq.SetZ((Points2.Value(i)).Y());
495 Params.SetValue(i,PSeq);
499 //=======================================================================
500 //function : AddOrConfuse
501 //purpose : the first or the last point of the bissectrice is on the
502 // parallel if it was not found in the intersections,
503 // it is projected on parallel lines and added in the parameters
504 //=======================================================================
506 void BRepFill_TrimEdgeTool::AddOrConfuse(const Standard_Boolean Start,
507 const TopoDS_Edge& Edge1,
508 const TopoDS_Edge& Edge2,
509 TColgp_SequenceOfPnt& Params)
512 Standard_Boolean ToProj = Standard_True;
514 Standard_Real Tol = 10*Precision::Confusion();
516 // return curves associated to edges.
519 Handle(Geom_Surface) Surf;
521 Handle(Geom2d_Curve) C1;
522 BRep_Tool::CurveOnSurface(Edge1,C1,Surf,L,f,l);
523 Geom2dAdaptor_Curve AC1(C1,f,l);
526 if (Start) PBis = myBis.Value(myBis.FirstParameter());
527 else PBis = myBis.Value(myBis.LastParameter ());
529 // Test if the end of the bissectrice is in the set of intersection points.
530 if (!Params.IsEmpty()) {
532 if (Start) P = AC1.Value(Params.First().Y());
533 else P = AC1.Value(Params.Last ().Y());
534 ToProj = !PBis.IsEqual(P,Tol);
539 cout << " project extremity bissectrice on parallel."<<endl;
542 // Project point on parallels and add in Params
545 Handle(Geom2d_Curve) C2;
546 BRep_Tool::CurveOnSurface(Edge2,C2,Surf,L,f2,l2);
548 Geom2dAPI_ProjectPointOnCurve Projector1(PBis,C1,f,l);
549 Geom2dAPI_ProjectPointOnCurve Projector2(PBis,C2,f2,l2);
551 if (Projector1.NbPoints() == 0) {
553 cout << "Failed projection in BRepFill_TrimEdgeTool::AddOrConfuse"<<endl;
557 if (!Projector1.NearestPoint().IsEqual(PBis,Tol)) {
559 cout <<"Incorrect solution in BRepFill_TrimEdgeTool::AddOrConfuse"<<endl;
563 if (Projector2.NbPoints() == 0) {
565 cout << "Failed projection in BRepFill_TrimEdgeTool::AddOrConfuse"<<endl;
569 if (!Projector2.NearestPoint().IsEqual(PBis,Tol)) {
571 cout <<" Mauvaisesolution dans BRepFill_TrimEdgeTool::AddOrConfuse"<<endl;
576 Projector1.LowerDistanceParameter(),
577 Projector2.LowerDistanceParameter());
579 PInt.SetX (myBis.FirstParameter());
580 Params.Prepend(PInt);
583 PInt.SetX (myBis.LastParameter());
589 //=======================================================================
590 //function : IsInside
592 //=======================================================================
594 Standard_Boolean BRepFill_TrimEdgeTool::IsInside(const gp_Pnt2d& P) const
596 // Modified by Sergey KHROMOV - Fri Sep 27 11:43:12 2002 Begin
597 // Standard_Real Dist;
598 Standard_Real Dist = RealLast();
599 // Modified by Sergey KHROMOV - Fri Sep 27 11:43:12 2002 End
601 Dist = P.Distance(myP1);
603 Dist = P.Distance(myP2);
605 Geom2dAPI_ProjectPointOnCurve Projector(P,myC1);
606 if (Projector.NbPoints() > 0) {
607 Dist = Projector.LowerDistance();
609 // Modified by Sergey KHROMOV - Fri Sep 27 11:43:43 2002 Begin
611 // gp_Pnt2d PF = myC1->Value(myC1->FirstParameter());
612 // gp_Pnt2d PL = myC1->Value(myC1->LastParameter());
613 // Dist = Min (P.Distance(PF),P.Distance(PL));
616 // Check of distances between P and first and last point of the first curve
617 // should be performed in any case, despite of the results of projection.
618 gp_Pnt2d PF = myC1->Value(myC1->FirstParameter());
619 gp_Pnt2d PL = myC1->Value(myC1->LastParameter());
620 Standard_Real aDistMin = Min (P.Distance(PF),P.Distance(PL));
624 // Modified by Sergey KHROMOV - Fri Sep 27 11:43:44 2002 End
627 // return (Dist < Abs(myOffset);
628 // return (Dist < Abs(myOffset) + Precision::Confusion());
629 return (Dist < Abs(myOffset) - Precision::Confusion());