1 // Created on: 1996-09-03
2 // Created by: Yves FRICAUD
3 // Copyright (c) 1996-1999 Matra Datavision
4 // Copyright (c) 1999-2014 OPEN CASCADE SAS
6 // This file is part of Open CASCADE Technology software library.
8 // This library is free software; you can redistribute it and/or modify it under
9 // the terms of the GNU Lesser General Public License version 2.1 as published
10 // by the Free Software Foundation, with special exception defined in the file
11 // OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
12 // distribution for complete text of the license and disclaimer of any warranty.
14 // Alternatively, this file may be used under the terms of Open CASCADE
15 // commercial license or contractual agreement.
17 // Modified by skv - Wed Dec 24 18:08:39 2003 OCC4455
19 #include <Adaptor2d_HCurve2d.hxx>
20 #include <Adaptor3d_CurveOnSurface.hxx>
21 #include <Adaptor3d_HSurface.hxx>
22 #include <Bnd_Box.hxx>
23 #include <BndLib_Add3dCurve.hxx>
24 #include <BOPTools_AlgoTools.hxx>
25 #include <BRep_Builder.hxx>
26 #include <BRep_CurveRepresentation.hxx>
27 #include <BRep_GCurve.hxx>
28 #include <BRep_ListIteratorOfListOfCurveRepresentation.hxx>
29 #include <BRep_TEdge.hxx>
30 #include <BRep_Tool.hxx>
31 #include <BRepAdaptor_Curve.hxx>
32 #include <BRepAdaptor_Curve2d.hxx>
33 #include <BRepAdaptor_Surface.hxx>
34 #include <BRepAlgo_AsDes.hxx>
35 #include <BRepLib.hxx>
36 #include <BRepLib_MakeVertex.hxx>
37 #include <BRepOffset_Analyse.hxx>
38 #include <BRepOffset_Inter2d.hxx>
39 #include <BRepOffset_Offset.hxx>
40 #include <BRepOffset_Tool.hxx>
41 #include <BRepTools.hxx>
42 #include <BRepTools_WireExplorer.hxx>
43 #include <Geom2d_BezierCurve.hxx>
44 #include <Geom2d_BSplineCurve.hxx>
45 #include <Geom2d_Line.hxx>
46 #include <Geom2d_TrimmedCurve.hxx>
47 #include <Geom2dAdaptor_HCurve.hxx>
48 #include <Geom2dConvert_CompCurveToBSplineCurve.hxx>
49 #include <Geom2dInt_GInter.hxx>
50 #include <Geom_BSplineCurve.hxx>
51 #include <Geom_BSplineSurface.hxx>
52 #include <Geom_ConicalSurface.hxx>
53 #include <Geom_CylindricalSurface.hxx>
54 #include <Geom_Line.hxx>
55 #include <Geom_Plane.hxx>
56 #include <Geom_TrimmedCurve.hxx>
57 #include <GeomAdaptor_HSurface.hxx>
58 #include <GeomAdaptor_Surface.hxx>
59 #include <GeomAPI_ProjectPointOnCurve.hxx>
60 #include <GeomConvert_CompCurveToBSplineCurve.hxx>
61 #include <GeomLib.hxx>
62 #include <GeomProjLib.hxx>
64 #include <IntRes2d_IntersectionPoint.hxx>
65 #include <IntRes2d_IntersectionSegment.hxx>
66 #include <Precision.hxx>
67 #include <TColGeom2d_SequenceOfCurve.hxx>
68 #include <TColgp_Array1OfPnt2d.hxx>
69 #include <TColgp_SequenceOfPnt.hxx>
71 #include <TopExp_Explorer.hxx>
73 #include <TopoDS_Edge.hxx>
74 #include <TopoDS_Face.hxx>
75 #include <TopoDS_Iterator.hxx>
76 #include <TopoDS_Vertex.hxx>
77 #include <TopoDS_Wire.hxx>
78 #include <TopTools_DataMapIteratorOfDataMapOfShapeListOfShape.hxx>
79 #include <TopTools_ListIteratorOfListOfShape.hxx>
80 #include <TopTools_ListOfShape.hxx>
85 #include <Geom2d_BoundedCurve.hxx>
86 #include <Geom_BoundedSurface.hxx>
87 #include <Geom_BoundedCurve.hxx>
88 #include <BRep_CurveOnSurface.hxx>
89 #include <Geom_Surface.hxx>
90 Standard_Boolean Inter2dAffichInt2d;
91 static Standard_Integer NbF2d = 0;
92 static Standard_Integer NbE2d = 0;
93 static Standard_Integer NbNewVertices = 0;
96 //=======================================================================
97 //function : CommonVertex
99 //=======================================================================
101 static TopoDS_Vertex CommonVertex(TopoDS_Edge& E1,
104 TopoDS_Vertex V1[2],V2[2],V;
106 TopExp::Vertices(E1,V1[0],V1[1], Standard_True);
107 TopExp::Vertices(E2,V2[0],V2[1], Standard_True);
108 // The first edge is the current one, the second edge is the next one.
109 // We check last vertex of the first edge first.
110 if (V1[1].IsSame(V2[0]) || V1[1].IsSame(V2[1])) return V1[1];
111 if (V1[0].IsSame(V2[0]) || V1[0].IsSame(V2[1])) return V1[0];
116 static Standard_Boolean IsOrientationChanged(TopTools_IndexedMapOfShape& theMap,
117 const TopoDS_Edge& theEdge)
119 Standard_Boolean IsOrChanged = Standard_False;
121 if (!theMap.Contains(theEdge))
125 Standard_Integer anInd = theMap.FindIndex(theEdge);
126 const TopoDS_Shape& anEdge = theMap(anInd);
127 if (theEdge.Orientation() != anEdge.Orientation())
129 theMap.Substitute( anInd, theEdge );
130 IsOrChanged = Standard_True;
138 //=======================================================================
140 //purpose : Store the vertices <theLV> into AsDes for the edge <theEdge>.
141 // The vertices are added despite of the coincidence with
142 // already added vertices. When all vertices for all edges
143 // are added the coinciding chains of vertices should be fused
144 // using FuseVertices() method.
145 //=======================================================================
146 static void Store(const TopoDS_Edge& theEdge,
147 const TopTools_ListOfShape& theLV,
148 const Standard_Real theTol,
149 const Standard_Boolean IsToUpdate,
150 Handle(BRepAlgo_AsDes) theAsDes2d,
151 TopTools_IndexedDataMapOfShapeListOfShape& theDMVV)
153 const TopTools_ListOfShape& aLVEx = theAsDes2d->Descendant(theEdge);
154 if (!IsToUpdate && aLVEx.IsEmpty()) {
155 if (theLV.Extent()) theAsDes2d->Add(theEdge, theLV);
159 GeomAPI_ProjectPointOnCurve aProjPC;
161 Standard_Real aT1, aT2;
162 const Handle(Geom_Curve)& aC = BRep_Tool::Curve(theEdge, aT1, aT2);
163 aProjPC.Init(aC, aT1, aT2);
166 TopTools_MapOfShape aMV;
167 TopTools_ListIteratorOfListOfShape aIt(theLV);
168 for (; aIt.More(); aIt.Next()) {
169 const TopoDS_Vertex& aV = TopoDS::Vertex(aIt.Value());
174 const gp_Pnt& aP = BRep_Tool::Pnt(aV);
176 TopTools_ListOfShape aLVC;
177 TopTools_ListIteratorOfListOfShape aItEx(aLVEx);
178 for (; aItEx.More(); aItEx.Next()) {
179 const TopoDS_Vertex& aVEx = TopoDS::Vertex(aItEx.Value());
180 if (aV.IsSame(aVEx)) {
183 const gp_Pnt& aPEx = BRep_Tool::Pnt(aVEx);
184 if (aP.IsEqual(aPEx, theTol)) {
194 // get parameter of the vertex on the edge
196 if (!aProjPC.NbPoints()) {
200 if (aProjPC.LowerDistance() > theTol) {
204 Standard_Real aT = aProjPC.LowerDistanceParameter();
205 TopoDS_Shape aLocalShape = aV.Oriented(TopAbs_INTERNAL);
206 BRep_Builder().UpdateVertex(TopoDS::Vertex(aLocalShape), aT, theEdge, theTol);
209 BRep_Builder().UpdateVertex(aV, theTol);
213 TopTools_ListIteratorOfListOfShape aItLV(aLVC);
214 for (; aItLV.More(); aItLV.Next()) {
215 const TopoDS_Shape& aVC = aItLV.Value();
216 TopTools_ListOfShape* pLV = theDMVV.ChangeSeek(aVC);
218 pLV = &theDMVV(theDMVV.Add(aVC, TopTools_ListOfShape()));
223 TopTools_ListOfShape* pLV = theDMVV.ChangeSeek(aV);
225 pLV = &theDMVV(theDMVV.Add(aV, TopTools_ListOfShape()));
229 theAsDes2d->Add(theEdge, aV);
233 //=======================================================================
235 //purpose : Store the intersection vertices between two edges into AsDes
236 //=======================================================================
237 static void Store (const TopoDS_Edge& theE1,
238 const TopoDS_Edge& theE2,
239 const TopTools_ListOfShape& theLV1,
240 const TopTools_ListOfShape& theLV2,
241 const Standard_Real theTol,
242 Handle(BRepAlgo_AsDes) theAsDes2d,
243 TopTools_IndexedDataMapOfShapeListOfShape& theDMVV)
245 for (Standard_Integer i = 0; i < 2; ++i) {
246 const TopoDS_Edge& aE = !i ? theE1 : theE2;
247 const TopTools_ListOfShape& aLV = !i ? theLV1 : theLV2;
248 Store(aE, aLV, theTol, Standard_False, theAsDes2d, theDMVV);
252 //=======================================================================
253 //function : EdgeInter
255 //=======================================================================
257 static void EdgeInter(const TopoDS_Face& F,
258 const BRepAdaptor_Surface& BAsurf,
259 const TopoDS_Edge& E1,
260 const TopoDS_Edge& E2,
261 const Handle(BRepAlgo_AsDes)& AsDes,
263 Standard_Boolean WithOri,
264 TopTools_IndexedDataMapOfShapeListOfShape& aDMVV)
267 if (Inter2dAffichInt2d) {
269 sprintf(name,"E2d_%d_%d",NbF2d,NbE2d++);
271 sprintf(name,"E2d_%d_%d",NbF2d,NbE2d++);
279 Standard_Real f[3],l[3];
280 Standard_Real TolDub = 1.e-7;
283 BRep_Tool::Range(E1, f[1], l[1]);
284 BRep_Tool::Range(E2, f[2], l[2]);
286 BRepAdaptor_Curve CE1(E1,F);
287 BRepAdaptor_Curve CE2(E2,F);
289 TopoDS_Edge EI[3]; EI[1] = E1; EI[2] = E2;
290 TopTools_ListOfShape LV1;
291 TopTools_ListOfShape LV2;
295 if (!TopExp::CommonVertex( E1, E2, CV ))
297 BRepLib::BuildCurve3d(E1);
298 BRepLib::BuildCurve3d(E2);
300 Standard_Real TolSum = BRep_Tool::Tolerance(E1) + BRep_Tool::Tolerance(E2);
301 TolSum = Max( TolSum, 1.e-5 );
303 TColgp_SequenceOfPnt ResPoints;
304 TColStd_SequenceOfReal ResParamsOnE1, ResParamsOnE2;
306 Standard_Boolean WithDegen = BRep_Tool::Degenerated(E1) || BRep_Tool::Degenerated(E2);
310 Standard_Integer ideg = (BRep_Tool::Degenerated(E1))? 1 : 2;
311 TopoDS_Iterator iter( EI[ideg] );
314 const TopoDS_Vertex& vdeg = TopoDS::Vertex(iter.Value());
315 DegPoint = BRep_Tool::Pnt(vdeg);
319 BRepAdaptor_Curve CEdeg( EI[ideg], F );
320 DegPoint = CEdeg.Value( CEdeg.FirstParameter() );
324 Handle(Geom2d_Curve) pcurve1 = BRep_Tool::CurveOnSurface(E1, F, f[1], l[1]);
325 Handle(Geom2d_Curve) pcurve2 = BRep_Tool::CurveOnSurface(E2, F, f[2], l[2]);
326 Geom2dAdaptor_Curve GAC1(pcurve1, f[1], l[1]);
327 Geom2dAdaptor_Curve GAC2(pcurve2, f[2], l[2]);
328 Geom2dInt_GInter Inter2d( GAC1, GAC2, TolDub, TolDub );
329 for (i = 1; i <= Inter2d.NbPoints(); i++)
336 gp_Pnt2d P2d = Inter2d.Point(i).Value();
337 P3d = BAsurf.Value( P2d.X(), P2d.Y() );
339 ResPoints.Append( P3d );
340 ResParamsOnE1.Append( Inter2d.Point(i).ParamOnFirst() );
341 ResParamsOnE2.Append( Inter2d.Point(i).ParamOnSecond() );
344 for (i = 1; i <= ResPoints.Length(); i++)
346 Standard_Real aT1 = ResParamsOnE1(i); //ponc1.Parameter();
347 Standard_Real aT2 = ResParamsOnE2(i); //ponc2.Parameter();
348 if (Precision::IsInfinite(aT1) || Precision::IsInfinite(aT2))
351 std::cout << "Inter2d : Solution rejected due to infinite parameter"<<std::endl;
356 gp_Pnt P = ResPoints(i); //ponc1.Value();
357 TopoDS_Vertex aNewVertex = BRepLib_MakeVertex(P);
358 aNewVertex.Orientation(TopAbs_INTERNAL);
359 B.UpdateVertex( aNewVertex, aT1, E1, Tol );
360 B.UpdateVertex( aNewVertex, aT2, E2, Tol );
361 gp_Pnt P1 = CE1.Value(aT1);
362 gp_Pnt P2 = CE2.Value(aT2);
363 Standard_Real dist1, dist2, dist3;
364 dist1 = P1.Distance(P);
365 dist2 = P2.Distance(P);
366 dist3 = P1.Distance(P2);
367 dist1 = Max( dist1, dist2 );
368 dist1 = Max( dist1, dist3 );
369 B.UpdateVertex( aNewVertex, dist1 );
372 if (aT1 < f[1]-Tol || aT1 > l[1]+Tol)
374 std::cout << "out of limit"<<std::endl;
375 std::cout<<"aT1 = "<<aT1<<", f[1] = "<<f[1]<<", l[1] = "<<l[1]<<std::endl;
377 if (aT2 < f[2]-Tol || aT2 > l[2]+Tol)
379 std::cout << "out of limit"<<std::endl;
380 std::cout<<"aT2 = "<<aT2<<", f[2] = "<<f[2]<<", l[2] = "<<l[2]<<std::endl;
382 Standard_Real MilTol2 = 1000*Tol*Tol;
383 if (P1.SquareDistance(P) > MilTol2 || P2.SquareDistance(P) > MilTol2 || P1.Distance(P2) > 2.*Tol)
385 std::cout << "Inter2d : Solution rejected "<<std::endl;
386 std::cout<<"P = "<<P.X()<<" "<<P.Y()<<" "<<P.Z()<<std::endl;
387 std::cout<<"P1 = "<<P1.X()<<" "<<P1.Y()<<" "<<P1.Z()<<std::endl;
388 std::cout<<"P2 = "<<P2.X()<<" "<<P2.Y()<<" "<<P2.Z()<<std::endl;
389 std::cout<<"MaxDist = "<<dist1<<std::endl;
392 //define the orientation of a new vertex
393 TopAbs_Orientation OO1 = TopAbs_REVERSED;
394 TopAbs_Orientation OO2 = TopAbs_REVERSED;
397 BRepAdaptor_Curve2d PCE1( E1, F );
398 BRepAdaptor_Curve2d PCE2( E2, F );
400 gp_Vec2d V1, V2, V1or, V2or;
401 PCE1.D1( aT1, P2d1, V1 );
402 PCE2.D1( aT2, P2d2, V2 );
403 V1or = V1; V2or = V2;
404 if (E1.Orientation() == TopAbs_REVERSED) V1or.Reverse();
405 if (E2.Orientation() == TopAbs_REVERSED) V2or.Reverse();
406 Standard_Real CrossProd = V2or ^ V1;
408 if (Abs(CrossProd) <= gp::Resolution())
409 std::cout<<std::endl<<"CrossProd = "<<CrossProd<<std::endl;
412 OO1 = TopAbs_FORWARD;
413 CrossProd = V1or ^ V2;
415 OO2 = TopAbs_FORWARD;
417 LV1.Append( aNewVertex.Oriented(OO1) );
418 LV2.Append( aNewVertex.Oriented(OO2) );
422 //----------------------------------
424 //---------------------------------
426 Standard_Real TolConf = Tol;
427 TopoDS_Vertex V1[2],V2[2];
428 TopExp::Vertices(E1,V1[0],V1[1]);
429 TopExp::Vertices(E2,V2[0],V2[1]);
432 for (j = 0; j < 2; j++) {
433 if (V1[j].IsNull()) continue;
434 for (Standard_Integer k = 0; k < 2; k++) {
435 if (V2[k].IsNull()) continue;
436 if (V1[j].IsSame(V2[k])) {
437 if (AsDes->HasAscendant(V1[j])) {
442 gp_Pnt P1 = BRep_Tool::Pnt(V1[j]);
443 gp_Pnt P2 = BRep_Tool::Pnt(V2[k]);
444 Standard_Real Dist = P1.Distance(P2);
445 if (Dist < TolConf) {
447 Max(BRep_Tool::Tolerance(V1[j]), BRep_Tool::Tolerance(V2[k]));
448 TopoDS_Vertex V = BRepLib_MakeVertex(P1);
449 U1 = (j == 0) ? f[1] : l[1];
450 U2 = (k == 0) ? f[2] : l[2];
452 TopoDS_Shape aLocalShape = V.Oriented(TopAbs_INTERNAL);
453 B.UpdateVertex(TopoDS::Vertex(aLocalShape),U1,E1,aTol);
454 B.UpdateVertex(TopoDS::Vertex(aLocalShape),U2,E2,aTol);
456 LV1.Prepend(V.Oriented(V1[j].Orientation()));
457 LV2.Prepend(V.Oriented(V2[k].Orientation()));
462 Standard_Boolean AffichPurge = Standard_False;
464 if ( !LV1.IsEmpty()) {
465 //----------------------------------
466 // Remove all vertices.
467 // There can be doubles
468 //----------------------------------
469 TopTools_ListIteratorOfListOfShape it1LV1,it1LV2,it2LV1;
471 Standard_Boolean Purge = Standard_True;
475 Purge = Standard_False;
476 for (it1LV1.Initialize(LV1),it1LV2.Initialize(LV2);
477 it1LV1.More(); it1LV1.Next(),it1LV2.Next()) {
479 it2LV1.Initialize(LV1);
481 P1 = BRep_Tool::Pnt(TopoDS::Vertex(it1LV1.Value()));
482 P2 = BRep_Tool::Pnt(TopoDS::Vertex(it2LV1.Value()));
483 // Modified by skv - Thu Jan 22 18:19:04 2004 OCC4455 Begin
484 // if (P1.IsEqual(P2,10*Tol)) {
487 aTol = Max(BRep_Tool::Tolerance(TopoDS::Vertex(it1LV1.Value())),
488 BRep_Tool::Tolerance(TopoDS::Vertex(it2LV1.Value())));
489 if (P1.IsEqual(P2,aTol)) {
490 // Modified by skv - Thu Jan 22 18:19:05 2004 OCC4455 End
493 if (AffichPurge) std::cout <<"Doubles removed in EdgeInter."<<std::endl;
494 Purge = Standard_True;
504 //---------------------------------
505 // Vertex storage in DS.
506 //---------------------------------
507 Standard_Real TolStore = BRep_Tool::Tolerance(E1) + BRep_Tool::Tolerance(E2);
508 TolStore = Max(TolStore, 10.*Tol);
509 Store (E1,E2,LV1,LV2,TolStore,AsDes, aDMVV);
512 //=======================================================================
513 //function : EdgeInter
515 //=======================================================================
517 static void RefEdgeInter(const TopoDS_Face& F,
518 const BRepAdaptor_Surface& BAsurf,
519 const TopoDS_Edge& E1,
520 const TopoDS_Edge& E2,
521 const Handle(BRepAlgo_AsDes)& AsDes,
523 Standard_Boolean WithOri,
525 TopTools_IndexedDataMapOfShapeListOfShape& aDMVV,
526 Standard_Boolean& theCoincide)
529 if (Inter2dAffichInt2d) {
531 sprintf(name,"E2d_%d_%d",NbF2d,NbE2d++);
533 sprintf(name,"E2d_%d_%d",NbF2d,NbE2d++);
538 theCoincide = Standard_False;
543 Standard_Real f[3],l[3];
544 Standard_Real TolDub = 1.e-7;
547 //BRep_Tool::Range(E1, f[1], l[1]);
548 //BRep_Tool::Range(E2, f[2], l[2]);
550 BRepAdaptor_Curve CE1(E1,F);
551 BRepAdaptor_Curve CE2(E2,F);
553 TopoDS_Edge EI[3]; EI[1] = E1; EI[2] = E2;
554 TopTools_ListOfShape LV1;
555 TopTools_ListOfShape LV2;
558 BRepLib::BuildCurve3d(E1);
559 BRepLib::BuildCurve3d(E2);
561 Standard_Real TolSum = BRep_Tool::Tolerance(E1) + BRep_Tool::Tolerance(E2);
562 TolSum = Max( TolSum, 1.e-5 );
564 TColgp_SequenceOfPnt ResPoints;
565 TColStd_SequenceOfReal ResParamsOnE1, ResParamsOnE2;
567 Standard_Boolean WithDegen = BRep_Tool::Degenerated(E1) || BRep_Tool::Degenerated(E2);
571 Standard_Integer ideg = (BRep_Tool::Degenerated(E1))? 1 : 2;
572 TopoDS_Iterator iter( EI[ideg] );
575 const TopoDS_Vertex& vdeg = TopoDS::Vertex(iter.Value());
576 DegPoint = BRep_Tool::Pnt(vdeg);
580 BRepAdaptor_Curve CEdeg( EI[ideg], F );
581 DegPoint = CEdeg.Value( CEdeg.FirstParameter() );
585 Handle(Geom2d_Curve) pcurve1 = BRep_Tool::CurveOnSurface(E1, F, f[1], l[1]);
586 Handle(Geom2d_Curve) pcurve2 = BRep_Tool::CurveOnSurface(E2, F, f[2], l[2]);
587 Geom2dAdaptor_Curve GAC1(pcurve1, f[1], l[1]);
588 Geom2dAdaptor_Curve GAC2(pcurve2, f[2], l[2]);
589 if ((GAC1.GetType() == GeomAbs_Line) &&
590 (GAC2.GetType() == GeomAbs_Line))
592 // Just quickly check if lines coincide
593 if (GAC1.Line().Direction().IsParallel (GAC2.Line().Direction(), 1.e-8))
595 theCoincide = Standard_True;
600 Geom2dInt_GInter Inter2d( GAC1, GAC2, TolDub, TolDub );
602 if (!Inter2d.IsDone() || !Inter2d.NbPoints()) {
603 theCoincide = (Inter2d.NbSegments() &&
604 (GAC1.GetType() == GeomAbs_Line) &&
605 (GAC2.GetType() == GeomAbs_Line));
609 for (i = 1; i <= Inter2d.NbPoints(); i++)
616 gp_Pnt2d P2d = Inter2d.Point(i).Value();
617 P3d = BAsurf.Value( P2d.X(), P2d.Y() );
619 ResPoints.Append( P3d );
620 ResParamsOnE1.Append( Inter2d.Point(i).ParamOnFirst() );
621 ResParamsOnE2.Append( Inter2d.Point(i).ParamOnSecond() );
624 for (i = 1; i <= ResPoints.Length(); i++)
626 Standard_Real aT1 = ResParamsOnE1(i); //ponc1.Parameter();
627 Standard_Real aT2 = ResParamsOnE2(i); //ponc2.Parameter();
628 if (Precision::IsInfinite(aT1) || Precision::IsInfinite(aT2))
631 std::cout << "Inter2d : Solution rejected due to infinite parameter"<<std::endl;
636 gp_Pnt P = ResPoints(i); //ponc1.Value();
637 TopoDS_Vertex aNewVertex = BRepLib_MakeVertex(P);
638 aNewVertex.Orientation(TopAbs_INTERNAL);
639 B.UpdateVertex( aNewVertex, aT1, E1, Tol );
640 B.UpdateVertex( aNewVertex, aT2, E2, Tol );
641 gp_Pnt P1 = CE1.Value(aT1);
642 gp_Pnt P2 = CE2.Value(aT2);
643 Standard_Real dist1, dist2, dist3;
644 dist1 = P1.Distance(P);
645 dist2 = P2.Distance(P);
646 dist3 = P1.Distance(P2);
647 dist1 = Max( dist1, dist2 );
648 dist1 = Max( dist1, dist3 );
649 B.UpdateVertex( aNewVertex, dist1 );
652 if (aT1 < f[1]-Tol || aT1 > l[1]+Tol)
654 std::cout << "out of limit"<<std::endl;
655 std::cout<<"aT1 = "<<aT1<<", f[1] = "<<f[1]<<", l[1] = "<<l[1]<<std::endl;
657 if (aT2 < f[2]-Tol || aT2 > l[2]+Tol)
659 std::cout << "out of limit"<<std::endl;
660 std::cout<<"aT2 = "<<aT2<<", f[2] = "<<f[2]<<", l[2] = "<<l[2]<<std::endl;
662 Standard_Real MilTol2 = 1000*Tol*Tol;
663 if (P1.SquareDistance(P) > MilTol2 || P2.SquareDistance(P) > MilTol2 || P1.Distance(P2) > 2.*Tol)
665 std::cout << "Inter2d : Solution rejected"<<std::endl;
666 std::cout<<"P = "<<P.X()<<" "<<P.Y()<<" "<<P.Z()<<std::endl;
667 std::cout<<"P1 = "<<P1.X()<<" "<<P1.Y()<<" "<<P1.Z()<<std::endl;
668 std::cout<<"P2 = "<<P2.X()<<" "<<P2.Y()<<" "<<P2.Z()<<std::endl;
669 std::cout<<"MaxDist = "<<dist1<<std::endl;
672 //define the orientation of a new vertex
673 TopAbs_Orientation OO1 = TopAbs_REVERSED;
674 TopAbs_Orientation OO2 = TopAbs_REVERSED;
677 BRepAdaptor_Curve2d PCE1( E1, F );
678 BRepAdaptor_Curve2d PCE2( E2, F );
680 gp_Vec2d V1, V2, V1or, V2or;
681 PCE1.D1( aT1, P2d1, V1 );
682 PCE2.D1( aT2, P2d2, V2 );
683 V1or = V1; V2or = V2;
684 if (E1.Orientation() == TopAbs_REVERSED) V1or.Reverse();
685 if (E2.Orientation() == TopAbs_REVERSED) V2or.Reverse();
686 Standard_Real CrossProd = V2or ^ V1;
688 if (Abs(CrossProd) <= gp::Resolution())
689 std::cout<<std::endl<<"CrossProd = "<<CrossProd<<std::endl;
692 OO1 = TopAbs_FORWARD;
693 CrossProd = V1or ^ V2;
695 OO2 = TopAbs_FORWARD;
697 LV1.Append( aNewVertex.Oriented(OO1) );
698 LV2.Append( aNewVertex.Oriented(OO2) );
701 //----------------------------------
703 //---------------------------------
705 Standard_Real TolConf = Tol;
706 TopoDS_Vertex V1[2],V2[2];
707 TopExp::Vertices(E1,V1[0],V1[1]);
708 TopExp::Vertices(E2,V2[0],V2[1]);
711 for (j = 0; j < 2; j++) {
712 if (V1[j].IsNull()) continue;
713 for (Standard_Integer k = 0; k < 2; k++) {
714 if (V2[k].IsNull()) continue;
715 if (V1[j].IsSame(V2[k])) {
716 if (AsDes->HasAscendant(V1[j])) {
721 gp_Pnt P1 = BRep_Tool::Pnt(V1[j]);
722 gp_Pnt P2 = BRep_Tool::Pnt(V2[k]);
723 Standard_Real Dist = P1.Distance(P2);
724 if (Dist < TolConf) {
725 TopoDS_Vertex V = BRepLib_MakeVertex(P1);
726 U1 = (j == 0) ? f[1] : l[1];
727 U2 = (k == 0) ? f[2] : l[2];
728 TopoDS_Shape aLocalShape = V.Oriented(TopAbs_INTERNAL);
729 B.UpdateVertex(TopoDS::Vertex(aLocalShape),U1,E1,Tol);
730 B.UpdateVertex(TopoDS::Vertex(aLocalShape),U2,E2,Tol);
731 LV1.Prepend(V.Oriented(V1[j].Orientation()));
732 LV2.Prepend(V.Oriented(V2[k].Orientation()));
737 Standard_Boolean AffichPurge = Standard_False;
739 if ( !LV1.IsEmpty()) {
740 //----------------------------------
741 // Remove all vertices.
742 // there can be doubles
743 //----------------------------------
744 TopTools_ListIteratorOfListOfShape it1LV1,it1LV2,it2LV1;
746 Standard_Boolean Purge = Standard_True;
750 Purge = Standard_False;
751 for (it1LV1.Initialize(LV1),it1LV2.Initialize(LV2);
752 it1LV1.More(); it1LV1.Next(),it1LV2.Next()) {
754 it2LV1.Initialize(LV1);
756 P1 = BRep_Tool::Pnt(TopoDS::Vertex(it1LV1.Value()));
757 P2 = BRep_Tool::Pnt(TopoDS::Vertex(it2LV1.Value()));
758 if (P1.IsEqual(P2,10*Tol)) {
761 if (AffichPurge) std::cout <<"Doubles removed in EdgeInter."<<std::endl;
762 Purge = Standard_True;
772 //---------------------------------
773 // Vertex storage in SD.
774 //---------------------------------
775 ////-----------------------------------------------------
776 if(LV1.Extent() > 1) {
777 //std::cout << "IFV - RefEdgeInter: remove vertex" << std::endl;
778 Standard_Real dmin = RealLast();
780 for (it1LV1.Initialize(LV1); it1LV1.More(); it1LV1.Next()) {
781 gp_Pnt P = BRep_Tool::Pnt(TopoDS::Vertex(it1LV1.Value()));
782 Standard_Real d = P.SquareDistance(Pref);
785 Vmin = TopoDS::Vertex(it1LV1.Value());
788 for (it1LV1.Initialize(LV1),it1LV2.Initialize(LV2);
789 it1LV1.More(); it1LV1.Next(),it1LV2.Next()) {
790 if(!Vmin.IsSame(it1LV1.Value())) {
793 if(!it1LV1.More()) break;
798 ////-----------------------------------------------------
799 Standard_Real TolStore = BRep_Tool::Tolerance(E1) + BRep_Tool::Tolerance(E2);
800 TolStore = Max(TolStore, 10.*Tol);
801 Store (E1,E2,LV1,LV2,TolStore,AsDes, aDMVV);
806 //======================================================================
807 //function : EvaluateMaxSegment
808 //purpose : return MaxSegment to pass in approximation
809 //======================================================================
811 static Standard_Integer evaluateMaxSegment(const Adaptor3d_CurveOnSurface& aCurveOnSurface)
813 Handle(Adaptor3d_HSurface) aSurf = aCurveOnSurface.GetSurface();
814 Handle(Adaptor2d_HCurve2d) aCurv2d = aCurveOnSurface.GetCurve();
816 Standard_Real aNbSKnots = 0, aNbC2dKnots = 0;
818 if (aSurf->GetType() == GeomAbs_BSplineSurface) {
819 Handle(Geom_BSplineSurface) aBSpline = aSurf->BSpline();
820 aNbSKnots = Max(aBSpline->NbUKnots(), aBSpline->NbVKnots());
822 if (aCurv2d->GetType() == GeomAbs_BSplineCurve) {
823 aNbC2dKnots = aCurv2d->NbKnots();
825 Standard_Integer aReturn = (Standard_Integer) ( 30 + Max(aNbSKnots, aNbC2dKnots) ) ;
830 //=======================================================================
831 //function : ExtendPCurve
833 //=======================================================================
835 static Standard_Boolean ExtendPCurve(const Handle(Geom2d_Curve)& aPCurve,
836 const Standard_Real anEf,
837 const Standard_Real anEl,
838 const Standard_Real a2Offset,
839 Handle(Geom2d_Curve)& NewPCurve)
842 if (NewPCurve->IsInstance(STANDARD_TYPE(Geom2d_TrimmedCurve)))
843 NewPCurve = Handle(Geom2d_TrimmedCurve)::DownCast (NewPCurve)->BasisCurve();
845 Standard_Real FirstPar = NewPCurve->FirstParameter();
846 Standard_Real LastPar = NewPCurve->LastParameter();
848 if (NewPCurve->IsKind(STANDARD_TYPE(Geom2d_BoundedCurve)) &&
849 (FirstPar > anEf - a2Offset || LastPar < anEl + a2Offset))
851 if (NewPCurve->IsInstance(STANDARD_TYPE(Geom2d_BezierCurve)))
853 Handle(Geom2d_BezierCurve) aBezier = Handle(Geom2d_BezierCurve)::DownCast (NewPCurve);
854 if (aBezier->NbPoles() == 2)
856 TColgp_Array1OfPnt2d thePoles(1,2);
857 aBezier->Poles(thePoles);
858 gp_Vec2d aVec(thePoles(1), thePoles(2));
859 NewPCurve = new Geom2d_Line(thePoles(1), aVec);
860 return Standard_True;
863 else if (NewPCurve->IsInstance(STANDARD_TYPE(Geom2d_BSplineCurve)))
865 Handle(Geom2d_BSplineCurve) aBSpline = Handle(Geom2d_BSplineCurve)::DownCast (NewPCurve);
866 if (aBSpline->NbKnots() == 2 && aBSpline->NbPoles() == 2)
868 TColgp_Array1OfPnt2d thePoles(1,2);
869 aBSpline->Poles(thePoles);
870 gp_Vec2d aVec(thePoles(1), thePoles(2));
871 NewPCurve = new Geom2d_Line(thePoles(1), aVec);
872 return Standard_True;
877 FirstPar = aPCurve->FirstParameter();
878 LastPar = aPCurve->LastParameter();
879 Handle(Geom2d_TrimmedCurve) aTrCurve =
880 new Geom2d_TrimmedCurve(aPCurve, FirstPar, LastPar);
882 // The curve is not prolonged on begin or end.
883 // Trying to prolong it adding a segment to its bound.
888 Handle(Geom2d_Line) aLin;
889 Handle(Geom2d_TrimmedCurve) aSegment;
890 Geom2dConvert_CompCurveToBSplineCurve aCompCurve(aTrCurve, Convert_RationalC1);
891 Standard_Real aTol = Precision::Confusion();
892 Standard_Real aDelta = Max(a2Offset, 1.);
894 if (FirstPar > anEf - a2Offset) {
895 aPCurve->D1(FirstPar, aPBnd, aVBnd);
896 aDBnd.SetXY(aVBnd.XY());
897 aPBeg = aPBnd.Translated(gp_Vec2d(-aDelta*aDBnd.XY()));
898 aLin = new Geom2d_Line(aPBeg, aDBnd);
899 aSegment = new Geom2d_TrimmedCurve(aLin, 0, aDelta);
901 if (!aCompCurve.Add(aSegment, aTol))
902 return Standard_False;
905 if (LastPar < anEl + a2Offset) {
906 aPCurve->D1(LastPar, aPBeg, aVBnd);
907 aDBnd.SetXY(aVBnd.XY());
908 aLin = new Geom2d_Line(aPBeg, aDBnd);
909 aSegment = new Geom2d_TrimmedCurve(aLin, 0, aDelta);
911 if (!aCompCurve.Add(aSegment, aTol))
912 return Standard_False;
915 NewPCurve = aCompCurve.BSplineCurve();
916 return Standard_True;
919 //=======================================================================
920 //function : ExtentEdge
922 //=======================================================================
924 // Modified by skv - Fri Dec 26 17:00:55 2003 OCC4455 Begin
925 //static void ExtentEdge(const TopoDS_Edge& E,TopoDS_Edge& NE)
926 void BRepOffset_Inter2d::ExtentEdge(const TopoDS_Edge& E,TopoDS_Edge& NE, const Standard_Real theOffset)
928 //BRepLib::BuildCurve3d(E);
930 TopoDS_Shape aLocalShape = E.EmptyCopied();
933 Standard_Real a2Offset = 2.*Abs(theOffset);
935 Standard_Integer i, j;
937 BRep_Tool::Range(E, anEf, anEl);
938 NE = TopoDS::Edge(aLocalShape);
939 // NE = TopoDS::Edge(E.EmptyCopied());
940 // Enough for analytic edges, for general case reconstruct the
941 // geometry of the edge recalculating the intersection of surfaces.
943 //BRepLib::BuildCurve3d(E);
945 Standard_Integer NbPCurves = 0;
946 Standard_Real FirstParOnPC = RealFirst(), LastParOnPC = RealLast();
947 Handle(Geom2d_Curve) MinPC;
948 Handle(Geom_Surface) MinSurf;
949 TopLoc_Location MinLoc;
951 BRep_ListIteratorOfListOfCurveRepresentation itr( (Handle(BRep_TEdge)::DownCast(NE.TShape()))->ChangeCurves() );
952 for (; itr.More(); itr.Next())
954 Handle( BRep_CurveRepresentation ) CurveRep = itr.Value();
955 Standard_Real FirstPar, LastPar;
956 if (CurveRep->IsCurveOnSurface())
959 Handle(Geom2d_Curve) theCurve = CurveRep->PCurve();
960 FirstPar = theCurve->FirstParameter();
961 LastPar = theCurve->LastParameter();
963 if (theCurve->IsKind(STANDARD_TYPE(Geom2d_BoundedCurve)) &&
964 (FirstPar > anEf - a2Offset || LastPar < anEl + a2Offset))
966 Handle(Geom2d_Curve) NewPCurve;
967 if (ExtendPCurve(theCurve, anEf, anEl, a2Offset, NewPCurve))
969 CurveRep->PCurve(NewPCurve);
970 FirstPar = NewPCurve->FirstParameter();
971 LastPar = NewPCurve->LastParameter();
972 if (CurveRep->IsCurveOnClosedSurface())
974 Handle(Geom2d_Curve) PCurve2 = CurveRep->PCurve2();
975 if (ExtendPCurve(PCurve2, anEf, anEl, a2Offset, NewPCurve))
976 CurveRep->PCurve2(NewPCurve);
980 else if (theCurve->IsPeriodic())
982 Standard_Real delta = (theCurve->Period() - (anEl - anEf))*0.5;
984 FirstPar = anEf - delta;
985 LastPar = anEl + delta;
987 else if (theCurve->IsClosed())
988 LastPar -= 0.05*(LastPar - FirstPar);
990 //check FirstPar and LastPar: the pcurve should be in its surface
991 theCurve = CurveRep->PCurve();
992 Handle(Geom_Surface) theSurf = CurveRep->Surface();
993 Standard_Real Umin, Umax, Vmin, Vmax;
994 theSurf->Bounds(Umin, Umax, Vmin, Vmax);
995 TColGeom2d_SequenceOfCurve BoundLines;
996 if (!Precision::IsInfinite(Vmin))
998 Handle(Geom2d_Line) aLine = new Geom2d_Line(gp_Pnt2d( 0., Vmin ),
1000 BoundLines.Append(aLine);
1002 if (!Precision::IsInfinite(Umin))
1004 Handle(Geom2d_Line) aLine = new Geom2d_Line(gp_Pnt2d( Umin, 0. ),
1005 gp_Dir2d( 0., 1. ));
1006 BoundLines.Append(aLine);
1008 if (!Precision::IsInfinite(Vmax))
1010 Handle(Geom2d_Line) aLine = new Geom2d_Line(gp_Pnt2d( 0., Vmax ),
1011 gp_Dir2d( 1., 0. ));
1012 BoundLines.Append(aLine);
1014 if (!Precision::IsInfinite(Umax))
1016 Handle(Geom2d_Line) aLine = new Geom2d_Line(gp_Pnt2d( Umax, 0. ),
1017 gp_Dir2d( 0., 1. ));
1018 BoundLines.Append(aLine);
1021 TColStd_SequenceOfReal params;
1022 Geom2dInt_GInter IntCC;
1023 Geom2dAdaptor_Curve GAcurve(theCurve);
1024 for (i = 1; i <= BoundLines.Length(); i++)
1026 Geom2dAdaptor_Curve GAline( BoundLines(i) );
1027 IntCC.Perform( GAcurve, GAline, Precision::PConfusion(), Precision::PConfusion());
1030 for (j = 1; j <= IntCC.NbPoints(); j++)
1032 const IntRes2d_IntersectionPoint& ip = IntCC.Point(j);
1033 gp_Pnt2d aPoint = ip.Value();
1034 if (aPoint.X() >= Umin && aPoint.X() <= Umax &&
1035 aPoint.Y() >= Vmin && aPoint.Y() <= Vmax)
1036 params.Append( ip.ParamOnFirst() );
1038 for (j = 1; j <= IntCC.NbSegments(); j++)
1040 const IntRes2d_IntersectionSegment& is = IntCC.Segment(j);
1041 if (is.HasFirstPoint())
1043 const IntRes2d_IntersectionPoint& ip = is.FirstPoint();
1044 gp_Pnt2d aPoint = ip.Value();
1045 if (aPoint.X() >= Umin && aPoint.X() <= Umax &&
1046 aPoint.Y() >= Vmin && aPoint.Y() <= Vmax)
1047 params.Append( ip.ParamOnFirst() );
1049 if (is.HasLastPoint())
1051 const IntRes2d_IntersectionPoint& ip = is.LastPoint();
1052 gp_Pnt2d aPoint = ip.Value();
1053 if (aPoint.X() >= Umin && aPoint.X() <= Umax &&
1054 aPoint.Y() >= Vmin && aPoint.Y() <= Vmax)
1055 params.Append( ip.ParamOnFirst() );
1060 if (!params.IsEmpty())
1062 if (params.Length() == 1)
1064 gp_Pnt2d PntFirst = theCurve->Value(FirstPar);
1065 if (PntFirst.X() >= Umin && PntFirst.X() <= Umax &&
1066 PntFirst.Y() >= Vmin && PntFirst.Y() <= Vmax)
1068 if (LastPar > params(1))
1069 LastPar = params(1);
1071 else if (FirstPar < params(1))
1072 FirstPar = params(1);
1076 Standard_Real fpar = RealLast(), lpar = RealFirst();
1077 for (i = 1; i <= params.Length(); i++)
1079 if (params(i) < fpar)
1081 if (params(i) > lpar)
1084 if (FirstPar < fpar)
1090 //// end of check ////
1091 (Handle(BRep_GCurve)::DownCast(CurveRep))->SetRange( FirstPar, LastPar );
1092 //gp_Pnt2d Pfirst = theCurve->Value(FirstPar);
1093 //gp_Pnt2d Plast = theCurve->Value(LastPar);
1094 //(Handle(BRep_CurveOnSurface)::DownCast(CurveRep))->SetUVPoints( Pfirst, Plast );
1096 //update FirstParOnPC and LastParOnPC
1097 if (FirstPar > FirstParOnPC)
1099 FirstParOnPC = FirstPar;
1102 MinLoc = CurveRep->Location();
1104 if (LastPar < LastParOnPC)
1106 LastParOnPC = LastPar;
1109 MinLoc = CurveRep->Location();
1115 Handle(Geom_Curve) C3d = BRep_Tool::Curve( NE, f, l );
1118 MinLoc = E.Location() * MinLoc;
1121 if (MinPC->IsClosed())
1126 else if (C3d->IsPeriodic())
1128 Standard_Real delta = (C3d->Period() - (l - f))*0.5;
1133 else if (C3d->IsClosed())
1139 GeomAPI_ProjectPointOnCurve Projector;
1140 if (!Precision::IsInfinite(FirstParOnPC))
1142 gp_Pnt2d P2d1 = MinPC->Value(FirstParOnPC);
1143 gp_Pnt P1 = MinSurf->Value( P2d1.X(), P2d1.Y() );
1144 P1.Transform(MinLoc.Transformation());
1145 Projector.Init( P1, C3d );
1146 if (Projector.NbPoints() > 0)
1147 f = Projector.LowerDistanceParameter();
1150 std::cout<<"ProjectPointOnCurve not done"<<std::endl;
1153 if (!Precision::IsInfinite(LastParOnPC))
1155 gp_Pnt2d P2d2 = MinPC->Value(LastParOnPC);
1156 gp_Pnt P2 = MinSurf->Value( P2d2.X(), P2d2.Y() );
1157 P2.Transform(MinLoc.Transformation());
1158 Projector.Init( P2, C3d );
1159 if (Projector.NbPoints() > 0)
1160 l = Projector.LowerDistanceParameter();
1163 std::cout<<"ProjectPointOnCurve not done"<<std::endl;
1167 BB.Range( NE, f, l );
1168 if (!Precision::IsInfinite(f) && !Precision::IsInfinite(l))
1169 BRepLib::SameParameter( NE, Precision::Confusion(), Standard_True );
1171 else if (!BRep_Tool::Degenerated(E)) //no 3d curve
1173 MinSurf = Handle(Geom_Surface)::DownCast
1174 (MinSurf->Transformed(MinLoc.Transformation()));
1175 Standard_Real max_deviation = 0.;
1176 if (Precision::IsInfinite(FirstParOnPC) || Precision::IsInfinite(LastParOnPC))
1178 if (MinPC->IsInstance(STANDARD_TYPE(Geom2d_Line)))
1180 Standard_Boolean IsLine = Standard_False;
1181 if (MinSurf->IsInstance(STANDARD_TYPE(Geom_Plane)))
1182 IsLine = Standard_True;
1183 else if (MinSurf->IsInstance(STANDARD_TYPE(Geom_CylindricalSurface)) ||
1184 MinSurf->IsInstance(STANDARD_TYPE(Geom_ConicalSurface)))
1186 Handle(Geom2d_Line) theLine = Handle(Geom2d_Line)::DownCast (MinPC);
1187 gp_Dir2d LineDir = theLine->Direction();
1188 if (LineDir.IsParallel( gp::DY2d(), Precision::Angular() ))
1189 IsLine = Standard_True;
1193 gp_Pnt2d P2d1 = MinPC->Value(0.), P2d2 = MinPC->Value(1.);
1194 gp_Pnt P1 = MinSurf->Value(P2d1.X(), P2d1.Y());
1195 gp_Pnt P2 = MinSurf->Value(P2d2.X(), P2d2.Y());
1196 gp_Vec aVec(P1, P2);
1197 C3d = new Geom_Line( P1, aVec );
1203 Geom2dAdaptor_Curve AC2d( MinPC, FirstParOnPC, LastParOnPC );
1204 GeomAdaptor_Surface GAsurf( MinSurf );
1205 Handle(Geom2dAdaptor_HCurve) HC2d = new Geom2dAdaptor_HCurve( AC2d );
1206 Handle(GeomAdaptor_HSurface) HSurf = new GeomAdaptor_HSurface( GAsurf );
1207 Adaptor3d_CurveOnSurface ConS( HC2d, HSurf );
1208 Standard_Real /*max_deviation,*/ average_deviation;
1209 GeomAbs_Shape Continuity = GeomAbs_C1;
1210 Standard_Integer MaxDegree = 14;
1211 Standard_Integer MaxSegment = evaluateMaxSegment(ConS);
1212 GeomLib::BuildCurve3d(Precision::Confusion(),
1213 ConS, FirstParOnPC, LastParOnPC,
1214 C3d, max_deviation, average_deviation,
1215 Continuity, MaxDegree, MaxSegment);
1217 BB.UpdateEdge( NE, C3d, max_deviation );
1218 //BB.Range( NE, FirstParOnPC, LastParOnPC );
1219 Standard_Boolean ProjectionSuccess = Standard_True;
1221 //BRepLib::SameParameter( NE, Precision::Confusion(), Standard_True );
1222 for (itr.Initialize((Handle(BRep_TEdge)::DownCast(NE.TShape()))->ChangeCurves());
1226 Handle( BRep_CurveRepresentation ) CurveRep = itr.Value();
1227 Standard_Real FirstPar, LastPar;
1228 if (CurveRep->IsCurveOnSurface())
1230 Handle(Geom2d_Curve) theCurve = CurveRep->PCurve();
1231 Handle(Geom_Surface) theSurf = CurveRep->Surface();
1232 TopLoc_Location theLoc = CurveRep->Location();
1233 if (theCurve == MinPC && theSurf == MinSurf && theLoc == MinLoc)
1235 FirstPar = (Handle(BRep_GCurve)::DownCast(CurveRep))->First();
1236 LastPar = (Handle(BRep_GCurve)::DownCast(CurveRep))->Last();
1237 if (Abs(FirstPar - FirstParOnPC) > Precision::PConfusion() ||
1238 Abs(LastPar - LastParOnPC) > Precision::PConfusion())
1240 theLoc = E.Location() * theLoc;
1241 theSurf = Handle(Geom_Surface)::DownCast
1242 (theSurf->Transformed(theLoc.Transformation()));
1244 if (theCurve->IsInstance(STANDARD_TYPE(Geom2d_Line)) &&
1245 theSurf->IsKind(STANDARD_TYPE(Geom_BoundedSurface)))
1247 gp_Dir2d theDir = Handle(Geom2d_Line)::DownCast (theCurve)->Direction();
1248 if (theDir.IsParallel(gp::DX2d(), Precision::Angular()) ||
1249 theDir.IsParallel(gp::DY2d(), Precision::Angular()))
1251 Standard_Real U1, U2, V1, V2;
1252 theSurf->Bounds(U1, U2, V1, V2);
1253 gp_Pnt2d Origin = Handle(Geom2d_Line)::DownCast (theCurve)->Location();
1254 if (Abs(Origin.X()-U1) <= Precision::Confusion() ||
1255 Abs(Origin.X()-U2) <= Precision::Confusion() ||
1256 Abs(Origin.Y()-V1) <= Precision::Confusion() ||
1257 Abs(Origin.Y()-V2) <= Precision::Confusion())
1259 BRepLib::SameParameter( NE, Precision::Confusion(), Standard_True );
1265 Handle(Geom2d_Curve) ProjPCurve =
1266 GeomProjLib::Curve2d( C3d, FirstParOnPC, LastParOnPC, theSurf );
1267 if (ProjPCurve.IsNull())
1268 ProjectionSuccess = Standard_False;
1270 CurveRep->PCurve( ProjPCurve );
1274 if (ProjectionSuccess)
1275 BB.Range( NE, FirstParOnPC, LastParOnPC );
1278 BB.Range( NE, FirstParOnPC, LastParOnPC, Standard_True );
1279 BRepLib::SameParameter( NE, Precision::Confusion(), Standard_True );
1285 Standard_Real FirstPar = C3d->FirstParameter();
1286 Standard_Real LastPar = C3d->LastParameter();
1288 if (C3d->IsKind(STANDARD_TYPE(Geom_BoundedCurve)) &&
1289 (FirstPar > anEf - a2Offset || LastPar < anEl + a2Offset))
1291 Handle(Geom_TrimmedCurve) aTrCurve =
1292 new Geom_TrimmedCurve(C3d, FirstPar, LastPar);
1294 // The curve is not prolonged on begin or end.
1295 // Trying to prolong it adding a segment to its bound.
1300 Handle(Geom_Line) aLin;
1301 Handle(Geom_TrimmedCurve) aSegment;
1302 GeomConvert_CompCurveToBSplineCurve aCompCurve(aTrCurve, Convert_RationalC1);
1303 Standard_Real aTol = Precision::Confusion();
1304 Standard_Real aDelta = Max(a2Offset, 1.);
1306 if (FirstPar > anEf - a2Offset) {
1307 C3d->D1(FirstPar, aPBnd, aVBnd);
1308 aDBnd.SetXYZ(aVBnd.XYZ());
1309 aPBeg = aPBnd.Translated(gp_Vec(-aDelta*aDBnd.XYZ()));
1310 aLin = new Geom_Line(aPBeg, aDBnd);
1311 aSegment = new Geom_TrimmedCurve(aLin, 0, aDelta);
1313 if (!aCompCurve.Add(aSegment, aTol))
1317 if (LastPar < anEl + a2Offset) {
1318 C3d->D1(LastPar, aPBeg, aVBnd);
1319 aDBnd.SetXYZ(aVBnd.XYZ());
1320 aLin = new Geom_Line(aPBeg, aDBnd);
1321 aSegment = new Geom_TrimmedCurve(aLin, 0, aDelta);
1323 if (!aCompCurve.Add(aSegment, aTol))
1327 C3d = aCompCurve.BSplineCurve();
1328 FirstPar = C3d->FirstParameter();
1329 LastPar = C3d->LastParameter();
1330 BB.UpdateEdge(NE, C3d, Precision::Confusion());
1332 else if (C3d->IsPeriodic())
1334 Standard_Real delta = (C3d->Period() - (anEl - anEf))*0.5;
1336 FirstPar = anEf - delta;
1337 LastPar = anEl + delta;
1339 else if (C3d->IsClosed())
1340 LastPar -= 0.05*(LastPar - FirstPar);
1342 BB.Range( NE, FirstPar, LastPar );
1345 // Modified by skv - Fri Dec 26 17:00:57 2003 OCC4455 End
1348 //=======================================================================
1349 //function : UpdateVertex
1351 //=======================================================================
1353 static Standard_Boolean UpdateVertex(TopoDS_Vertex V,
1356 Standard_Real TolConf)
1358 BRepAdaptor_Curve OC(OE);
1359 BRepAdaptor_Curve NC(NE);
1360 Standard_Real Of = OC.FirstParameter(); Standard_Real Ol = OC.LastParameter();
1361 Standard_Real Nf = NC.FirstParameter(); Standard_Real Nl = NC.LastParameter();
1362 Standard_Real U = 0.;
1363 Standard_Real ParTol = Precision::PConfusion();
1364 gp_Pnt P = BRep_Tool::Pnt(V);
1365 Standard_Boolean OK = Standard_False;
1367 if (P.Distance(OC.Value(Of)) < TolConf) {
1368 if (Of >= Nf + ParTol && Of <= Nl + ParTol && P.Distance(NC.Value(Of)) < TolConf) {
1373 if (P.Distance(OC.Value(Ol)) < TolConf) {
1374 if (Ol >= Nf + ParTol && Ol <= Nl + ParTol && P.Distance(NC.Value(Ol)) < TolConf) {
1381 TopoDS_Shape aLocalShape = NE.Oriented(TopAbs_FORWARD);
1382 TopoDS_Edge EE = TopoDS::Edge(aLocalShape);
1383 // TopoDS_Edge EE = TopoDS::Edge(NE.Oriented(TopAbs_FORWARD));
1384 aLocalShape = V.Oriented(TopAbs_INTERNAL);
1385 B.UpdateVertex(TopoDS::Vertex(aLocalShape),
1386 U,NE,BRep_Tool::Tolerance(NE));
1387 // B.UpdateVertex(TopoDS::Vertex(V.Oriented(TopAbs_INTERNAL)),
1388 // U,NE,BRep_Tool::Tolerance(NE));
1393 //=======================================================================
1394 //function : Compute
1396 //=======================================================================
1398 void BRepOffset_Inter2d::Compute (const Handle(BRepAlgo_AsDes)& AsDes,
1399 const TopoDS_Face& F,
1400 const TopTools_IndexedMapOfShape& NewEdges,
1401 const Standard_Real Tol,
1402 TopTools_IndexedDataMapOfShapeListOfShape& theDMVV)
1409 //Do not intersect the edges of face
1410 TopTools_MapOfShape EdgesOfFace;
1411 TopExp_Explorer Explo( F, TopAbs_EDGE );
1412 for (; Explo.More(); Explo.Next())
1413 EdgesOfFace.Add( Explo.Current() );
1415 //-----------------------------------------------------------
1416 // calculate intersections2d on faces touched by
1418 //---------------------------------------------------------
1419 TopTools_ListIteratorOfListOfShape it1LE ;
1420 TopTools_ListIteratorOfListOfShape it2LE ;
1422 //-----------------------------------------------
1423 // Intersection of edges 2*2.
1424 //-----------------------------------------------
1425 const TopTools_ListOfShape& LE = AsDes->Descendant(F);
1426 TopoDS_Vertex V1,V2;
1427 Standard_Integer j, i = 1;
1428 BRepAdaptor_Surface BAsurf(F);
1430 for ( it1LE.Initialize(LE) ; it1LE.More(); it1LE.Next()) {
1431 const TopoDS_Edge& E1 = TopoDS::Edge(it1LE.Value());
1433 it2LE.Initialize(LE);
1435 while (j < i && it2LE.More()) {
1436 const TopoDS_Edge& E2 = TopoDS::Edge(it2LE.Value());
1437 //--------------------------------------------------------------
1438 // Intersections of New edges obtained by intersection
1439 // between them and with edges of restrictions
1440 //------------------------------------------------------
1441 if ( (!EdgesOfFace.Contains(E1) || !EdgesOfFace.Contains(E2)) &&
1442 (NewEdges.Contains(E1) || NewEdges.Contains(E2)) ) {
1443 TopoDS_Shape aLocalShape = F.Oriented(TopAbs_FORWARD);
1444 EdgeInter(TopoDS::Face(aLocalShape),BAsurf,E1,E2,AsDes,Tol,Standard_True, theDMVV);
1445 // EdgeInter(TopoDS::Face(F.Oriented(TopAbs_FORWARD)),E1,E2,AsDes,Tol,Standard_True);
1454 //=======================================================================
1455 //function : ConnexIntByInt
1457 //=======================================================================
1458 void BRepOffset_Inter2d::ConnexIntByInt
1459 (const TopoDS_Face& FI,
1460 BRepOffset_Offset& OFI,
1461 TopTools_DataMapOfShapeShape& MES,
1462 const TopTools_DataMapOfShapeShape& Build,
1463 const Handle(BRepAlgo_AsDes)& AsDes2d,
1464 const Standard_Real Offset,
1465 const Standard_Real Tol,
1466 const BRepOffset_Analyse& Analyse,
1467 TopTools_IndexedMapOfShape& FacesWithVerts,
1468 TopTools_IndexedDataMapOfShapeListOfShape& theDMVV)
1471 TopTools_DataMapOfShapeListOfShape MVE;
1472 BRepOffset_Tool::MapVertexEdges(FI,MVE);
1474 //---------------------
1475 // Extension of edges.
1476 //---------------------
1478 TopTools_DataMapIteratorOfDataMapOfShapeListOfShape it(MVE);
1479 for ( ; it.More(); it.Next()) {
1480 const TopTools_ListOfShape& L = it.Value();
1481 Standard_Boolean YaBuild = 0;
1482 TopTools_ListIteratorOfListOfShape itL(L);
1483 for (; itL.More(); itL.Next()) {
1484 YaBuild = Build.IsBound(itL.Value());
1488 for (itL.Initialize(L); itL.More(); itL.Next()) {
1489 const TopoDS_Edge& EI = TopoDS::Edge(itL.Value());
1490 if (EI.Orientation() != TopAbs_FORWARD &&
1491 EI.Orientation() != TopAbs_REVERSED)
1493 TopoDS_Shape aLocalShape = OFI.Generated(EI);
1494 const TopoDS_Edge& OE = TopoDS::Edge(aLocalShape);
1495 if (!MES.IsBound(OE) && !Build.IsBound(EI)) {
1496 ExtentEdge(OE,NE, Offset);
1503 TopoDS_Face FIO = TopoDS::Face(OFI.Face());
1504 if (MES.IsBound(FIO)) FIO = TopoDS::Face(MES(FIO));
1506 BRepAdaptor_Surface BAsurf(FIO);
1508 TopExp_Explorer exp(FI.Oriented(TopAbs_FORWARD),TopAbs_WIRE);
1509 for (; exp.More(); exp.Next()) {
1510 const TopoDS_Wire& W = TopoDS::Wire(exp.Current());
1511 BRepTools_WireExplorer wexp;
1512 Standard_Boolean end = Standard_False ;
1513 TopoDS_Edge FirstE,CurE,NextE;
1515 TopoDS_Shape aLocalWire = W .Oriented(TopAbs_FORWARD);
1516 TopoDS_Shape aLocalFace = FI.Oriented(TopAbs_FORWARD);
1517 wexp.Init(TopoDS::Wire(aLocalWire),TopoDS::Face(aLocalFace));
1519 continue; // Protection from case when explorer does not contain edges.
1520 CurE = FirstE = wexp.Current();
1521 TopTools_IndexedMapOfShape Edges;
1522 Standard_Boolean ToReverse1, ToReverse2;
1523 ToReverse1 = IsOrientationChanged(Edges, CurE);
1527 NextE = wexp.Current();
1530 NextE = FirstE; end = Standard_True;
1532 if (CurE.IsSame(NextE)) continue;
1534 ToReverse2 = IsOrientationChanged(Edges, NextE);
1536 TopoDS_Vertex Vref = CommonVertex(CurE, NextE);
1537 gp_Pnt Pref = BRep_Tool::Pnt(Vref);
1539 CurE = Analyse.EdgeReplacement (FI, CurE);
1540 NextE = Analyse.EdgeReplacement (FI, NextE);
1542 TopoDS_Shape aLocalShape = OFI.Generated(CurE);
1543 TopoDS_Edge CEO = TopoDS::Edge(aLocalShape);
1544 aLocalShape = OFI.Generated(NextE);
1545 TopoDS_Edge NEO = TopoDS::Edge(aLocalShape);
1546 //------------------------------------------
1547 // Inter processing of images of CurE NextE.
1548 //------------------------------------------
1549 TopTools_ListOfShape LV1,LV2;
1550 Standard_Boolean DoInter = 1;
1551 TopoDS_Shape NE1,NE2;
1553 if (Build.IsBound(CurE) && Build.IsBound(NextE)) {
1557 else if (Build.IsBound(CurE) && MES.IsBound(NEO)) {
1561 else if (Build.IsBound(NextE) && MES.IsBound(CEO)) {
1564 Standard_Boolean Tmp = ToReverse1;
1565 ToReverse1 = ToReverse2;
1572 //------------------------------------
1573 // NE1,NE2 can be a compound of Edges.
1574 //------------------------------------
1575 Standard_Boolean bCoincide;
1576 TopExp_Explorer Exp1, Exp2;
1577 for (Exp1.Init(NE1, TopAbs_EDGE); Exp1.More(); Exp1.Next()) {
1578 TopoDS_Edge aE1 = TopoDS::Edge(Exp1.Current());
1579 for (Exp2.Init(NE2, TopAbs_EDGE); Exp2.More(); Exp2.Next()) {
1580 TopoDS_Edge aE2 = TopoDS::Edge(Exp2.Current());
1582 //Correct orientation of edges
1587 //////////////////////////////
1589 RefEdgeInter(FIO, BAsurf, aE1, aE2, AsDes2d,
1590 Tol, Standard_True, Pref, theDMVV, bCoincide);
1594 // check if some of the offset edges have been
1595 // generated out of the common vertex
1596 if (Build.IsBound(Vref)) {
1597 FacesWithVerts.Add(FI);
1601 TopoDS_Vertex V = CommonVertex(CEO,NEO);
1604 if (MES.IsBound(CEO)) {
1605 UpdateVertex (V,CEO,TopoDS::Edge(MES(CEO)),Tol);
1606 AsDes2d->Add (MES(CEO),V);
1608 if (MES.IsBound(NEO)) {
1609 UpdateVertex (V,NEO,TopoDS::Edge(MES(NEO)),Tol);
1610 AsDes2d->Add (MES(NEO),V);
1614 CurE = wexp.Current();
1615 ToReverse1 = ToReverse2;
1620 //=======================================================================
1621 //function : ConnexIntByIntInVert
1622 //purpose : Intersection of the edges generated out of vertices
1623 //=======================================================================
1624 void BRepOffset_Inter2d::ConnexIntByIntInVert
1625 (const TopoDS_Face& FI,
1626 BRepOffset_Offset& OFI,
1627 TopTools_DataMapOfShapeShape& MES,
1628 const TopTools_DataMapOfShapeShape& Build,
1629 const Handle(BRepAlgo_AsDes)& AsDes,
1630 const Handle(BRepAlgo_AsDes)& AsDes2d,
1631 const Standard_Real Tol,
1632 const BRepOffset_Analyse& Analyse,
1633 TopTools_IndexedDataMapOfShapeListOfShape& theDMVV)
1635 TopoDS_Face FIO = TopoDS::Face(OFI.Face());
1636 if (MES.IsBound(FIO)) FIO = TopoDS::Face(MES(FIO));
1638 TopTools_MapOfShape aME;
1639 const TopTools_ListOfShape& aLE = AsDes->Descendant(FIO);
1640 TopTools_ListIteratorOfListOfShape aItLE(aLE);
1641 for (; aItLE.More(); aItLE.Next()) {
1642 const TopoDS_Shape& aE = aItLE.Value();
1646 BRepAdaptor_Surface BAsurf(FIO);
1648 TopExp_Explorer exp(FI.Oriented(TopAbs_FORWARD),TopAbs_WIRE);
1649 for (; exp.More(); exp.Next()) {
1650 const TopoDS_Wire& W = TopoDS::Wire(exp.Current());
1652 BRepTools_WireExplorer wexp;
1653 Standard_Boolean end = Standard_False ;
1654 TopoDS_Edge FirstE,CurE,NextE;
1656 TopoDS_Shape aLocalWire = W .Oriented(TopAbs_FORWARD);
1657 TopoDS_Shape aLocalFace = FI.Oriented(TopAbs_FORWARD);
1658 wexp.Init(TopoDS::Wire(aLocalWire),TopoDS::Face(aLocalFace));
1660 continue; // Protection from case when explorer does not contain edges.
1662 CurE = FirstE = wexp.Current();
1666 NextE = wexp.Current();
1669 NextE = FirstE; end = Standard_True;
1671 if (CurE.IsSame(NextE)) continue;
1673 TopoDS_Vertex Vref = CommonVertex(CurE, NextE);
1674 gp_Pnt Pref = BRep_Tool::Pnt(Vref);
1675 if (!Build.IsBound(Vref)) {
1680 CurE = Analyse.EdgeReplacement (FI, CurE);
1681 NextE = Analyse.EdgeReplacement (FI, NextE);
1683 TopoDS_Shape aLocalShape = OFI.Generated(CurE);
1684 TopoDS_Edge CEO = TopoDS::Edge(aLocalShape);
1685 aLocalShape = OFI.Generated(NextE);
1686 TopoDS_Edge NEO = TopoDS::Edge(aLocalShape);
1688 TopoDS_Shape NE1,NE2;
1690 if (Build.IsBound(CurE) && Build.IsBound(NextE)) {
1694 else if (Build.IsBound(CurE) && MES.IsBound(NEO)) {
1698 else if (Build.IsBound(NextE) && MES.IsBound(CEO)) {
1703 CurE = wexp.Current();
1707 TopExp_Explorer Exp1, Exp2;
1708 Standard_Boolean bCoincide;
1709 // intersect edges generated from vertex with the edges of the face
1710 TopoDS_Shape NE3 = Build(Vref);
1712 for (Exp2.Init(NE3, TopAbs_EDGE); Exp2.More(); Exp2.Next()) {
1713 const TopoDS_Edge& aE3 = *(TopoDS_Edge*)&Exp2.Current();
1714 if (!aME.Contains(aE3)) {
1718 // intersection with first edge
1719 for (Exp1.Init(NE1, TopAbs_EDGE); Exp1.More(); Exp1.Next()) {
1720 const TopoDS_Edge& aE1 = TopoDS::Edge(Exp1.Current());
1721 RefEdgeInter(FIO, BAsurf, aE1, aE3, AsDes2d,
1722 Tol, Standard_True, Pref, theDMVV, bCoincide);
1724 // in case of coincidence trim the edge E3 the same way as E1
1725 Store(aE3, AsDes2d->Descendant(aE1), Tol, Standard_True, AsDes2d, theDMVV);
1729 // intersection with second edge
1730 for (Exp1.Init(NE2, TopAbs_EDGE); Exp1.More(); Exp1.Next()) {
1731 const TopoDS_Edge& aE2 = TopoDS::Edge(Exp1.Current());
1732 RefEdgeInter(FIO, BAsurf, aE2, aE3, AsDes2d,
1733 Tol, Standard_True, Pref, theDMVV, bCoincide);
1735 // in case of coincidence trim the edge E3 the same way as E2
1736 Store(aE3, AsDes2d->Descendant(aE2), Tol, Standard_True, AsDes2d, theDMVV);
1740 // intersection of the edges generated from vertex
1742 for (Exp1.Init(NE3, TopAbs_EDGE); Exp1.More(); Exp1.Next()) {
1743 if (aE3.IsSame(Exp1.Current())) {
1748 for (Exp1.Next(); Exp1.More(); Exp1.Next()) {
1749 const TopoDS_Edge& aE3Next = TopoDS::Edge(Exp1.Current());
1750 if (aME.Contains(aE3Next)) {
1751 RefEdgeInter(FIO, BAsurf, aE3Next, aE3, AsDes2d,
1752 Tol, Standard_True, Pref, theDMVV, bCoincide);
1756 CurE = wexp.Current();
1761 //=======================================================================
1762 //function : MakeChain
1764 //=======================================================================
1765 static void MakeChain(const TopoDS_Shape& theV,
1766 const TopTools_IndexedDataMapOfShapeListOfShape& theDMVV,
1767 TopTools_MapOfShape& theMDone,
1768 TopTools_ListOfShape& theChain)
1770 if (theMDone.Add(theV)) {
1771 theChain.Append(theV);
1772 const TopTools_ListOfShape* pLV = theDMVV.Seek(theV);
1774 TopTools_ListIteratorOfListOfShape aIt(*pLV);
1775 for (; aIt.More(); aIt.Next()) {
1776 MakeChain(aIt.Value(), theDMVV, theMDone, theChain);
1782 //=======================================================================
1783 //function : FuseVertices
1785 //=======================================================================
1786 void BRepOffset_Inter2d::FuseVertices(const TopTools_IndexedDataMapOfShapeListOfShape& theDMVV,
1787 const Handle(BRepAlgo_AsDes)& theAsDes)
1790 TopTools_MapOfShape aMVDone;
1791 Standard_Integer i, aNb = theDMVV.Extent();
1792 for (i = 1; i <= aNb; ++i) {
1793 const TopoDS_Vertex& aV = TopoDS::Vertex(theDMVV.FindKey(i));
1795 // find chain of vertices
1796 TopTools_ListOfShape aLVChain;
1797 MakeChain(aV, theDMVV, aMVDone, aLVChain);
1799 if (aLVChain.Extent() < 2) {
1804 TopoDS_Vertex aVNew;
1805 BOPTools_AlgoTools::MakeVertex(aLVChain, aVNew);
1807 TopoDS_Vertex aVNewInt = TopoDS::Vertex(aVNew.Oriented(TopAbs_INTERNAL));
1809 TopTools_ListIteratorOfListOfShape aIt(aLVChain);
1810 for (; aIt.More(); aIt.Next()) {
1811 const TopoDS_Shape& aVOld = aIt.Value();
1812 // update the parameters on edges
1813 TopoDS_Vertex aVOldInt = TopoDS::Vertex(aVOld.Oriented(TopAbs_INTERNAL));
1814 const TopTools_ListOfShape& aLE = theAsDes->Ascendant(aVOld);
1816 TopTools_ListIteratorOfListOfShape aItLE(aLE);
1817 for (; aItLE.More(); aItLE.Next()) {
1818 const TopoDS_Edge& aE = TopoDS::Edge(aItLE.Value());
1819 Standard_Real aTolE = BRep_Tool::Tolerance(aE);
1820 Standard_Real aT = BRep_Tool::Parameter(aVOldInt, aE);
1821 aBB.UpdateVertex(aVNewInt, aT, aE, aTolE);
1823 // and replace the vertex
1824 theAsDes->Replace(aVOld, aVNew);