1 // File: BRepLib_FuseEdges.cxx
2 // Created: Thu Nov 26 10:25:50 1998
3 // Author: Jean-Michel BOULCOURT
4 // <jmb@coulox.paris1.matra-dtv.fr>
5 // Modif : Wed Jan 20 15:40:50 1999. In BuildListResultEdges, we
6 // in UpdatePcurve, problem with location of pcurve (mix between loc and locbid)
7 // Modif : Thu Jan 21 11:40:20 1999. Add trace context #if DEB
8 // add test to avoid loop while in NextConnexEdge (in case of a closed connex wire)
11 #include <BRepLib_FuseEdges.ixx>
13 #include <TopTools_ListOfShape.hxx>
14 #include <TopTools_ListIteratorOfListOfShape.hxx>
16 #include <TopTools_MapOfShape.hxx>
18 #include <TopTools_DataMapOfIntegerListOfShape.hxx>
19 #include <TopTools_DataMapIteratorOfDataMapOfIntegerListOfShape.hxx>
22 #include <TopExp_Explorer.hxx>
26 #include <BRep_Tool.hxx>
27 #include <BRep_Builder.hxx>
30 #include <GeomLib.hxx>
31 #include <Geom2d_Curve.hxx>
32 #include <Geom_Curve.hxx>
33 #include <Geom_BoundedCurve.hxx>
34 #include <Geom_Plane.hxx>
35 #include <Geom2d_TrimmedCurve.hxx>
36 #include <Geom_TrimmedCurve.hxx>
37 #include <Geom_Line.hxx>
38 #include <Geom_Circle.hxx>
39 #include <Geom_Ellipse.hxx>
40 #include <Geom_BSplineCurve.hxx>
41 #include <Geom_BezierCurve.hxx>
42 #include <TColgp_Array1OfPnt.hxx>
43 #include <TColStd_Array1OfReal.hxx>
44 #include <TColStd_Array1OfInteger.hxx>
47 #include <Precision.hxx>
48 #include <gp_Pnt2d.hxx>
49 #include <gp_Dir2d.hxx>
50 #include <gp_Vec2d.hxx>
51 #include <gp_Trsf2d.hxx>
53 #include <BRepTools_Substitution.hxx>
54 #include <BRepLib_MakeEdge.hxx>
55 #include <BRepLib.hxx>
56 #include <TopoDS_Face.hxx>
57 #include <TopoDS_Wire.hxx>
59 #include <Geom2dConvert_CompCurveToBSplineCurve.hxx>
60 #include <Geom2d_BSplineCurve.hxx>
61 #include <BSplCLib.hxx>
62 #include <GeomConvert_CompCurveToBSplineCurve.hxx>
63 #include <Extrema_LocateExtPC.hxx>
64 #include <GeomAdaptor_Curve.hxx>
65 #include <GeomConvert.hxx>
68 //Standard_IMPORT Standard_Boolean TopOpeBRepBuild_GettraceFE();
71 static void BCSmoothing(Handle(Geom_BSplineCurve)& theC,
72 const Standard_Integer theCont,
73 const Standard_Real theTol)
76 Standard_Integer aNbIter = 5;
77 Standard_Boolean bContinue = Standard_True;
78 Standard_Integer iter = 1;
79 TColStd_SequenceOfInteger aKnotIndex;
80 TColStd_SequenceOfReal aKnotIns;
82 while (bContinue && iter <= aNbIter) {
84 Standard_Integer aNbKnots = theC->NbKnots();
85 TColStd_Array1OfInteger aMults(1, aNbKnots);
86 TColStd_Array1OfReal aKnots(1, aNbKnots);
88 theC->Multiplicities(aMults);
90 Standard_Integer i, m = theC->Degree();
97 for(i = 2; i < aNbKnots; i++) {
99 if(!(theC->RemoveKnot(i, m, theTol))) {
100 aKnotIndex.Append(i);
105 //Prepare knots for inserting;
107 Standard_Integer aNbAdd = aKnotIndex.Length();
109 if(aNbAdd == 0) return;
113 Standard_Real aLastKnot = aKnots(1);
114 for(i = 1; i <= aNbAdd; i++) {
116 Standard_Integer anInd = aKnotIndex(i);
118 Standard_Real aK1 = 0.5*(aKnots(anInd) + aKnots(anInd-1));
119 if(Abs(aK1 - aLastKnot) > 1.e-3) {
120 aKnotIns.Append(aK1);
124 Standard_Real aK2 = 0.5*(aKnots(anInd+1) + aKnots(anInd));
126 if(Abs(aK2 - aLastKnot) > 1.e-3) {
127 aKnotIns.Append(aK2);
133 aNbAdd = aKnotIns.Length();
135 for(i = 1; i <= aNbAdd; i++) {
136 theC->InsertKnot(aKnotIns(i), m, 1.e-3);
143 if(iter > aNbIter) BCSmoothing(theC, theCont, 1.e10);
148 static void MakeClosedCurve(Handle(Geom_Curve)& C, const gp_Pnt& PF,
149 Standard_Real& f, Standard_Real& l)
151 Handle(Geom_BSplineCurve) aBC = (*((Handle(Geom_BSplineCurve)*)&C));
152 GeomAbs_Shape aCont = aBC->Continuity();
155 Standard_Integer fk = aBC->FirstUKnotIndex();
156 Standard_Integer lk = aBC->LastUKnotIndex();
158 Standard_Real eps = Precision::Confusion();
160 Standard_Real porig = 2.*l;
161 Standard_Real dmin = 1.e100, pmin = f;
162 for(k = fk; k <= lk; k++) {
163 gp_Pnt aP = aBC->Value(aBC->Knot(k));
164 Standard_Real d = PF.SquareDistance(aP);
165 if(PF.SquareDistance(aP) > eps) {
173 porig = aBC->Knot(k);
178 GeomAdaptor_Curve aGAC(aBC);
179 Extrema_LocateExtPC aPrj(PF, aGAC, pmin, Precision::PConfusion());
181 porig = aPrj.Point().Parameter();
184 Standard_ConstructionError::Raise("FuseEdges : Projection failed for closed curve");
188 aBC->SetOrigin(porig, Precision::PConfusion());
189 f = aBC->FirstParameter();
190 l = aBC->LastParameter();
192 if(aCont > GeomAbs_C0 && aBC->Continuity() == GeomAbs_C0) {
193 BCSmoothing(aBC, 1, Precision::Confusion());
196 f = C->FirstParameter();
197 l = C->LastParameter();
202 //=======================================================================
203 //function : BRepLib_FuseEdges
205 //=======================================================================
207 BRepLib_FuseEdges::BRepLib_FuseEdges(const TopoDS_Shape& theShape,
208 // const Standard_Boolean PerformNow)
209 const Standard_Boolean )
210 :myShape(theShape),myShapeDone(Standard_False),myEdgesDone(Standard_False),
211 myResultEdgesDone(Standard_False),myNbConnexEdge(0), myConcatBSpl(Standard_False)
213 // if (theShape.ShapeType() != TopAbs_SHELL && theShape.ShapeType() != TopAbs_SOLID)
214 // Standard_ConstructionError::Raise("FuseEdges");
215 Standard_NullObject_Raise_if(theShape.IsNull(),"FuseEdges");
220 //=======================================================================
221 //function : AvoidEdges
222 //purpose : set edges to avoid being fused
223 //=======================================================================
225 void BRepLib_FuseEdges::AvoidEdges(const TopTools_IndexedMapOfShape& theMapEdg)
227 myAvoidEdg = theMapEdg;
230 //=======================================================================
231 //function : SetConcatBSpl
233 //=======================================================================
235 void BRepLib_FuseEdges::SetConcatBSpl(const Standard_Boolean theConcatBSpl)
237 myConcatBSpl = theConcatBSpl;
240 //=======================================================================
242 //purpose : returns all the list of edges to be fused each list of the
243 // map represent a set of connex edges that can be fused.
244 //=======================================================================
246 void BRepLib_FuseEdges::Edges(TopTools_DataMapOfIntegerListOfShape& theMapLstEdg)
253 theMapLstEdg = myMapLstEdg;
257 //=======================================================================
258 //function : ResultEdges
259 //purpose : returns all the fused edges
260 //=======================================================================
262 void BRepLib_FuseEdges::ResultEdges(TopTools_DataMapOfIntegerShape& theMapEdg)
269 if (!myResultEdgesDone) {
270 BuildListResultEdges();
273 theMapEdg = myMapEdg;
276 //=======================================================================
278 //purpose : returns all the faces that have been modified after perform
279 //=======================================================================
281 void BRepLib_FuseEdges::Faces(TopTools_DataMapOfShapeShape& theMapFac)
288 if (!myResultEdgesDone) {
289 BuildListResultEdges();
296 theMapFac = myMapFaces;
300 //=======================================================================
301 //function : NbVertices
303 //=======================================================================
305 const Standard_Integer BRepLib_FuseEdges::NbVertices()
308 Standard_NullObject_Raise_if(myShape.IsNull(),"FuseEdges : No Shape");
309 Standard_Integer nbedges, nbvertices = 0;
315 if ((nbedges = myMapLstEdg.Extent()) > 0) {
317 TopTools_DataMapIteratorOfDataMapOfIntegerListOfShape itEdg;
318 for (itEdg.Initialize(myMapLstEdg); itEdg.More(); itEdg.Next()) {
319 const Standard_Integer& iLst = itEdg.Key();
320 const TopTools_ListOfShape& LmapEdg = myMapLstEdg.Find(iLst);
321 nbvertices += LmapEdg.Extent() - 1;
330 //=======================================================================
333 //=======================================================================
335 TopoDS_Shape& BRepLib_FuseEdges::Shape()
337 Standard_NullObject_Raise_if(myShape.IsNull(),"FuseEdges : No Shape");
343 if (!myResultEdgesDone) {
344 BuildListResultEdges();
356 //=======================================================================
357 //function : BuildListEdges
358 //purpose : Build the all the lists of edges that are to be fused
359 //=======================================================================
361 void BRepLib_FuseEdges::BuildListEdges()
365 //Standard_Boolean tFE = TopOpeBRepBuild_GettraceFE();
369 //if (tFE) cout<<endl<<"FuseEdges : BuildListEdges "<<endl;
372 //--------------------------------------------------------
373 // Step One : Build the map ancestors
374 //--------------------------------------------------------
378 myMapVerLstEdg.Clear();
379 myMapEdgLstFac.Clear();
381 BuildAncestors(myShape,TopAbs_VERTEX,TopAbs_EDGE,myMapVerLstEdg);
382 TopExp::MapShapesAndAncestors(myShape,TopAbs_EDGE,TopAbs_FACE,myMapEdgLstFac);
384 Standard_Integer iEdg;
385 TopTools_MapOfShape mapUniqEdg;
387 // for each edge of myMapEdgLstFac
388 for (iEdg = 1; iEdg <= myMapEdgLstFac.Extent(); iEdg++) {
389 const TopoDS_Shape& edgecur = myMapEdgLstFac.FindKey(iEdg);
390 TopTools_ListOfShape LstEdg;
392 // if edge not already treated
393 if (!mapUniqEdg.Contains(edgecur)
394 && (edgecur.Orientation() == TopAbs_FORWARD ||edgecur.Orientation() == TopAbs_REVERSED) ) {
395 if (myAvoidEdg.Contains(edgecur))
396 continue; // edge is not allowed to be fused
397 BuildListConnexEdge(edgecur, mapUniqEdg, LstEdg);
398 if (LstEdg.Extent() > 1) {
400 myMapLstEdg.Bind(myNbConnexEdge,LstEdg);
405 myEdgesDone = Standard_True;
406 myResultEdgesDone = Standard_False;
410 //=======================================================================
411 //function : BuildListResultEdges
412 //purpose : Build the result fused edges
413 //=======================================================================
415 void BRepLib_FuseEdges::BuildListResultEdges()
419 //Standard_Boolean tFE = TopOpeBRepBuild_GettraceFE();
423 //if (tFE) cout<<endl<<"FuseEdges : BuildListResultEdges "<<endl;
426 // if we have edges to fuse
427 if (myMapLstEdg.Extent() > 0) {
428 TopTools_DataMapIteratorOfDataMapOfIntegerListOfShape itLstEdg;
430 Handle(Geom_Curve) C;
437 for (itLstEdg.Initialize(myMapLstEdg); itLstEdg.More(); itLstEdg.Next()) {
438 const Standard_Integer& iLst = itLstEdg.Key();
439 const TopTools_ListOfShape& LmapEdg = myMapLstEdg.Find(iLst);
441 TopoDS_Edge& OldEdge = TopoDS::Edge(LmapEdg.First());
443 // the first edge of the list will be replaced by the result fusion edge
444 if (OldEdge.Orientation()==TopAbs_REVERSED) {
445 VL = TopExp::FirstVertex(TopoDS::Edge(LmapEdg.First()),Standard_True);
446 VF = TopExp::LastVertex(TopoDS::Edge(LmapEdg.Last()),Standard_True);
449 VF = TopExp::FirstVertex(TopoDS::Edge(LmapEdg.First()),Standard_True);
450 VL = TopExp::LastVertex(TopoDS::Edge(LmapEdg.Last()),Standard_True);
452 C = BRep_Tool::Curve(OldEdge,loc,f,l);
454 if (!loc.IsIdentity()) {
455 C = Handle(Geom_Curve)::DownCast(C->Transformed(loc.Transformation()));
457 // if the curve is trimmed we get the basis curve to fit the new vertices
458 // otherwise the makeedge will fail.
459 if (C->DynamicType() == STANDARD_TYPE(Geom_TrimmedCurve)) {
460 C = (*((Handle(Geom_TrimmedCurve)*)&C))->BasisCurve();
464 //Prepare common BSpline curve
465 if(C->DynamicType() == STANDARD_TYPE(Geom_BSplineCurve)) {
466 TopTools_ListIteratorOfListOfShape anEdgIter(LmapEdg);
467 Handle(Geom_TrimmedCurve) aTC = new Geom_TrimmedCurve(C, f, l);
468 GeomConvert_CompCurveToBSplineCurve Concat( aTC );
471 for(; anEdgIter.More(); anEdgIter.Next()) {
472 Handle(Geom_Curve) aC = BRep_Tool::Curve(TopoDS::Edge(anEdgIter.Value()), f, l);
473 aTC = new Geom_TrimmedCurve(aC, f, l);
474 if (!Concat.Add(aTC, Precision::Confusion())) {
475 // cannot merge curves
476 Standard_ConstructionError::Raise("FuseEdges : Concatenation failed");
479 C = Concat.BSplineCurve();
485 //if (tFE) cout<<endl<<"FuseEdges : Creating New Edge "<<endl;
490 Standard_Boolean isBSpline = C->DynamicType() == STANDARD_TYPE(Geom_BSplineCurve);
492 if(VF.IsSame(VL) && isBSpline) {
494 f = C->FirstParameter();
495 l = C->LastParameter();
496 gp_Pnt aPf = C->Value(f);
497 gp_Pnt aPl = C->Value(l);
498 if(aPf.Distance(aPl) > Precision::Confusion()) {
499 Standard_ConstructionError::Raise("FuseEdges : Curve must be closed");
501 gp_Pnt PF = BRep_Tool::Pnt(VF);
502 if(PF.Distance(aPf) > Precision::Confusion()) {
503 MakeClosedCurve(C, PF, f, l);
506 ME.Init(C, VF, VL, f, l);
508 Standard_ConstructionError::Raise("FuseEdges : MakeEdge failed for closed curve");
514 // BRepLib_MakeEdge ME(C,VF,VL);
517 // the MakeEdge has fails, one reason could be that the new Vertices are outside
518 // the curve which is not infinite and limited to old vertices
519 // we try to use ExtendCurveToPoint, then rebuild the NewEdge
522 //if (tFE) cout<<endl<<"FuseEdges : MakeEdge failed. Trying to Extend Curve "<<endl;
524 Handle(Geom_BoundedCurve) ExtC = Handle(Geom_BoundedCurve)::DownCast(C->Copy());
525 if (!ExtC.IsNull()) {
526 gp_Pnt PF = BRep_Tool::Pnt(VF);
527 gp_Pnt PL = BRep_Tool::Pnt(VL);
528 GeomLib::ExtendCurveToPoint(ExtC,PF,1,0);
529 GeomLib::ExtendCurveToPoint(ExtC,PL,1,1);
533 Standard_ConstructionError::Raise("FuseEdges : Fusion failed");
536 Standard_ConstructionError::Raise("FuseEdges : Fusion failed");
542 //if (tFE) cout<<endl<<"FuseEdges : Updating pcurve "<<endl;
544 if (UpdatePCurve(OldEdge,NewEdge,LmapEdg))
545 myMapEdg.Bind(iLst,NewEdge);
548 myResultEdgesDone = Standard_True;
553 //=======================================================================
556 //=======================================================================
558 void BRepLib_FuseEdges::Perform()
562 //Standard_Boolean tFE = TopOpeBRepBuild_GettraceFE();
565 if (!myResultEdgesDone) {
566 BuildListResultEdges();
570 //if (tFE) cout<<endl<<"FuseEdges : Perform "<<endl;
573 // if we have fused edges
574 if (myMapEdg.Extent() > 0) {
575 TopTools_DataMapIteratorOfDataMapOfIntegerListOfShape itLstEdg;
576 TopTools_ListOfShape EmptyList,EdgeToSubs;
577 BRepTools_Substitution Bsub;
579 for (itLstEdg.Initialize(myMapLstEdg); itLstEdg.More(); itLstEdg.Next()) {
580 const Standard_Integer& iLst = itLstEdg.Key();
581 if (!myMapEdg.IsBound(iLst))
583 const TopTools_ListOfShape& LmapEdg = myMapLstEdg.Find(iLst);
584 TopTools_ListIteratorOfListOfShape itEdg;
587 TopoDS_Edge& OldEdge = TopoDS::Edge(LmapEdg.First());
590 EdgeToSubs.Append(myMapEdg(iLst));
591 Bsub.Substitute(OldEdge,EdgeToSubs);
593 itEdg.Initialize(LmapEdg);
595 // the other edges of the list will be removed
596 while (itEdg.More() ) {
597 if (!OldEdge.IsSame(TopoDS::Edge(itEdg.Value()))) {
598 Bsub.Substitute(itEdg.Value(),EmptyList);
605 //if (tFE) cout<<endl<<"FuseEdges : Building New Shape "<<endl;
608 // perform the effective substitution
611 // before copying the resulting shape, map the modified faces into myMapFaces
612 TopExp_Explorer exp(myShape,TopAbs_FACE);
614 for (; exp.More(); exp.Next()) {
615 const TopoDS_Shape& facecur = exp.Current();
616 if (Bsub.IsCopied(facecur)) {
617 myMapFaces.Bind(facecur,(Bsub.Copy(facecur)).First());
621 if (Bsub.IsCopied(myShape)) {
622 myShape=(Bsub.Copy(myShape)).First();
626 //if (tFE) cout<<endl<<"FuseEdges : "<< NbVertices() <<" vertices removed"<<endl;
633 myShapeDone = Standard_True;
638 //=======================================================================
639 //function : BuildListConnexEdge
640 //purpose : giving one edge, build the list of connex edges which have
641 // vertices that have only two connex edges. All the edges that are addes
642 // to the list must be added also to the mapUniq, in order for the caller
643 // to not treat again theses edges.
644 // This list is always oriented in the "Forward" direction.
645 //=======================================================================
647 void BRepLib_FuseEdges::BuildListConnexEdge(const TopoDS_Shape& theEdge,
648 TopTools_MapOfShape& theMapUniq,
649 TopTools_ListOfShape& theLstEdg)
655 VL = TopExp::LastVertex(TopoDS::Edge(theEdge),Standard_True);
656 TopoDS_Shape edgeconnex;
657 TopoDS_Shape edgecur = theEdge;
659 theLstEdg.Append(edgecur);
660 theMapUniq.Add(edgecur);
661 TopAbs_Orientation ori2;
663 // we first build the list of edges connex to edgecur by looking from the last Vertex VL
664 while (NextConnexEdge(VL,edgecur,edgeconnex)) {
665 if (theMapUniq.Contains(edgeconnex)) {
668 theLstEdg.Append(edgeconnex);
669 edgecur = edgeconnex;
670 // here take care about internal or external edges. It is non-sense to build
671 // the connex list with such edges.
672 ori2 = edgecur.Orientation();
673 if (ori2 == TopAbs_EXTERNAL || ori2 == TopAbs_INTERNAL) {
676 VL = TopExp::LastVertex(TopoDS::Edge(edgecur),Standard_True);
677 theMapUniq.Add(edgecur);
681 VF = TopExp::FirstVertex(TopoDS::Edge(theEdge),Standard_True);
683 // then we build the list of edges connex to edgecur by looking from the first Vertex VF
684 while (NextConnexEdge(VF,edgecur,edgeconnex)) {
685 if (theMapUniq.Contains(edgeconnex)) {
688 theLstEdg.Prepend(edgeconnex);
689 edgecur = edgeconnex;
690 // here take care about internal or external edges. It is non-sense to build
691 // the connex list with such edges.
692 ori2 = edgecur.Orientation();
693 if (ori2 == TopAbs_EXTERNAL || ori2 == TopAbs_INTERNAL) {
696 VF = TopExp::FirstVertex(TopoDS::Edge(edgecur),Standard_True);
697 theMapUniq.Add(edgecur);
703 //=======================================================================
704 //function : NextConnexEdge
705 //purpose : Look for an edge connex to theEdge at theVertex.
706 // the connex edge must satisfies the following criteria :
707 // * theVertex must have exactly 2 connex edges.
708 // * the 2 connex edges must have exactly the 2 same connex faces
709 // * the 2 connex edges must lie on the same support.
710 //=======================================================================
712 Standard_Boolean BRepLib_FuseEdges::NextConnexEdge(const TopoDS_Vertex& theVertex,
713 const TopoDS_Shape& theEdge,
714 TopoDS_Shape& theEdgeConnex) const
717 const TopTools_ListOfShape& LmapEdg = myMapVerLstEdg.FindFromKey(theVertex);
718 Standard_Boolean HasConnex = Standard_True;
719 TopTools_ListIteratorOfListOfShape itEdg,itFac1,itFac2;
722 if (LmapEdg.Extent() == 2) {
723 itEdg.Initialize(LmapEdg);
724 theEdgeConnex = itEdg.Value();
725 if (theEdge.IsSame(theEdgeConnex) ) {
727 theEdgeConnex = itEdg.Value();
730 if (myAvoidEdg.Contains(theEdgeConnex))
731 HasConnex = Standard_False; // edge is not allowed to be fused
735 const TopTools_ListOfShape& LmapFac1 = myMapEdgLstFac.FindFromKey(theEdge);
736 const TopTools_ListOfShape& LmapFac2 = myMapEdgLstFac.FindFromKey(theEdgeConnex);
738 if (LmapFac1.Extent() == LmapFac2.Extent() && LmapFac1.Extent() < 3) {
739 itFac1.Initialize(LmapFac1);
741 // for each face in LmapFac1 we look in LmapFac2 if it exists
742 while (itFac1.More() && HasConnex) {
743 const TopoDS_Shape& face1 = itFac1.Value();
744 for (itFac2.Initialize(LmapFac2); itFac2.More(); itFac2.Next()) {
745 const TopoDS_Shape& face2 = itFac2.Value();
746 HasConnex = Standard_False;
747 if (face1.IsSame(face2)) {
748 HasConnex = Standard_True;
755 // 3rd condition : same suport
757 HasConnex = SameSupport(TopoDS::Edge(theEdge),TopoDS::Edge(theEdgeConnex));
761 HasConnex = Standard_False;
765 HasConnex = Standard_False;
772 //=======================================================================
773 //function : SameSupport
774 //purpose : Edges SameSupport ou pas
775 //=======================================================================
777 Standard_Boolean BRepLib_FuseEdges::SameSupport(const TopoDS_Edge& E1,
778 const TopoDS_Edge& E2) const
781 if (E1.IsNull() || E2.IsNull()) {
782 return Standard_False;
786 Handle(Geom_Curve) C1,C2;
788 Standard_Real f1,l1,f2,l2;
789 Handle(Standard_Type) typC1,typC2;
791 C1 = BRep_Tool::Curve(E1,loc,f1,l1);
792 //modified by NIZNHY-PKV Mon Nov 15 16:24:10 1999
793 //degenerated edges has no 3D curve
794 if(C1.IsNull()) return Standard_False;
796 if (!loc.IsIdentity()) {
797 Handle(Geom_Geometry) GG1 = C1->Transformed(loc.Transformation());
798 C1 = *((Handle(Geom_Curve)*)&GG1);
800 C2 = BRep_Tool::Curve(E2,loc,f2,l2);
801 //modified by NIZNHY-PKV Mon Nov 15 16:24:38 1999
802 //degenerated edges has no 3D curve
803 if(C2.IsNull()) return Standard_False;
805 if (!loc.IsIdentity()) {
806 Handle(Geom_Geometry) GG2 = C2->Transformed(loc.Transformation());
807 C2 = *((Handle(Geom_Curve)*)&GG2);
810 typC1 = C1->DynamicType();
811 typC2 = C2->DynamicType();
813 if (typC1 == STANDARD_TYPE(Geom_TrimmedCurve)) {
814 C1 = (*((Handle(Geom_TrimmedCurve)*)&C1))->BasisCurve();
815 typC1 = C1->DynamicType();
818 if (typC2 == STANDARD_TYPE(Geom_TrimmedCurve)) {
819 C2 = (*((Handle(Geom_TrimmedCurve)*)&C2))->BasisCurve();
820 typC2 = C2->DynamicType();
823 if (typC1 != typC2) {
824 return Standard_False;
827 if (typC1 != STANDARD_TYPE(Geom_Line) &&
828 typC1 != STANDARD_TYPE(Geom_Circle) &&
829 typC1 != STANDARD_TYPE(Geom_Ellipse) &&
830 typC1 != STANDARD_TYPE(Geom_BSplineCurve) &&
831 typC1 != STANDARD_TYPE(Geom_BezierCurve)) {
833 cout << " TopOpeBRepTool_FuseEdge : Type de Support non traite" << endl;
835 return Standard_False;
838 // On a presomption de confusion
839 const Standard_Real tollin = Precision::Confusion();
840 const Standard_Real tolang = Precision::Angular();
841 if (typC1 == STANDARD_TYPE(Geom_Line)) {
842 gp_Lin li1( (*((Handle(Geom_Line)*)&C1))->Lin());
843 gp_Lin li2( (*((Handle(Geom_Line)*)&C2))->Lin());
844 gp_Dir dir1(li1.Direction());
845 gp_Dir dir2(li2.Direction());
847 if ( dir1.IsParallel(dir2,tolang) ) {
848 // on verifie que l'on n'a pas de cas degenere. Par exemple E1 et E2 connexes
849 // mais bouclant l'un sur l'autre (cas tres rare)
850 gp_Pnt pf1 = BRep_Tool::Pnt(TopExp::FirstVertex(E1,Standard_True));
851 gp_Pnt pl1 = BRep_Tool::Pnt(TopExp::LastVertex(E1,Standard_True));
852 gp_Pnt pf2 = BRep_Tool::Pnt(TopExp::FirstVertex(E2,Standard_True));
853 gp_Pnt pl2 = BRep_Tool::Pnt(TopExp::LastVertex(E2,Standard_True));
854 if ( pl1.Distance(pf2) < tollin && pl2.Distance(pf1) < tollin)
855 return Standard_False;
857 return Standard_True;
859 return Standard_False;
861 else if (typC1 == STANDARD_TYPE(Geom_Circle)) {
862 gp_Circ ci1 = (*((Handle(Geom_Circle)*)&C1))->Circ();
863 gp_Circ ci2 = (*((Handle(Geom_Circle)*)&C2))->Circ();
864 if (Abs(ci1.Radius()-ci2.Radius()) <= tollin &&
865 ci1.Location().SquareDistance(ci2.Location()) <= tollin*tollin &&
866 ci1.Axis().IsParallel(ci2.Axis(),tolang) ) {
867 // Point debut, calage dans periode, et detection meme sens
868 return Standard_True;
870 return Standard_False;
872 else if (typC1 == STANDARD_TYPE(Geom_Ellipse)) {
873 gp_Elips ci1 = (*((Handle(Geom_Ellipse)*)&C1))->Elips();
874 gp_Elips ci2 = (*((Handle(Geom_Ellipse)*)&C2))->Elips();
876 if (Abs(ci1.MajorRadius()-ci2.MajorRadius()) <= tollin &&
877 Abs(ci1.MinorRadius()-ci2.MinorRadius()) <= tollin &&
878 ci1.Location().SquareDistance(ci2.Location()) <= tollin*tollin &&
879 ci1.Axis().IsParallel(ci2.Axis(),tolang) ) {
880 // Point debut, calage dans periode, et detection meme sens
881 return Standard_True;
883 return Standard_False;
885 else if (typC1 == STANDARD_TYPE(Geom_BSplineCurve)) {
888 //Check G1 continuity
889 gp_Pnt aPf1, aPl1, aPf2, aPl2;
890 gp_Vec aDf1, aDl1, aDf2, aDl2;
892 C1->D1(f1, aPf1, aDf1);
893 C1->D1(l1, aPl1, aDl1);
895 C2->D1(f2, aPf2, aDf2);
896 C2->D1(l2, aPl2, aDl2);
898 if(aPl1.Distance(aPf2) <= tollin && aDl1.IsParallel(aDf2, tolang)) return Standard_True;
899 if(aPl2.Distance(aPf1) <= tollin && aDl2.IsParallel(aDf1, tolang)) return Standard_True;
900 if(aPf1.Distance(aPf2) <= tollin && aDf1.IsParallel(aDf2, tolang)) return Standard_True;
901 if(aPl1.Distance(aPl2) <= tollin && aDl1.IsParallel(aDl2, tolang)) return Standard_True;
904 // we must ensure that before fuse two bsplines, the end of one curve does not
905 // corresponds to the beginning of the second.
906 // we could add a special treatment for periodic bspline. This is not done for the moment.
907 if (Abs(f2-l1) > tollin && Abs(f1-l2) > tollin ) {
908 return Standard_False;
911 Handle(Geom_BSplineCurve) B1 = *((Handle(Geom_BSplineCurve)*)&C1);
912 Handle(Geom_BSplineCurve) B2 = *((Handle(Geom_BSplineCurve)*)&C2);
914 Standard_Integer nbpoles = B1->NbPoles();
915 if (nbpoles != B2->NbPoles()) {
916 return Standard_False;
919 Standard_Integer nbknots = B1->NbKnots();
920 if (nbknots != B2->NbKnots()) {
921 return Standard_False;
924 TColgp_Array1OfPnt P1(1, nbpoles), P2(1, nbpoles);
928 Standard_Real tol3d = BRep_Tool::Tolerance(E1);
929 for (Standard_Integer p = 1; p <= nbpoles; p++) {
930 if ( (P1(p)).Distance(P2(p)) > tol3d) {
931 return Standard_False;
935 TColStd_Array1OfReal K1(1, nbknots), K2(1, nbknots);
939 TColStd_Array1OfInteger M1(1, nbknots), M2(1, nbknots);
940 B1->Multiplicities(M1);
941 B2->Multiplicities(M2);
943 for (Standard_Integer k = 1; k <= nbknots; k++) {
944 if ((K1(k)-K2(k)) > tollin) {
945 return Standard_False;
947 if (Abs(M1(k)-M2(k)) > tollin) {
948 return Standard_False;
952 if (!B1->IsRational()) {
953 if (B2->IsRational()) {
954 return Standard_False;
958 if (!B2->IsRational()) {
959 return Standard_False;
963 if (B1->IsRational()) {
964 TColStd_Array1OfReal W1(1, nbpoles), W2(1, nbpoles);
968 for (Standard_Integer w = 1; w <= nbpoles; w++) {
969 if (Abs(W1(w)-W2(w)) > tollin) {
970 return Standard_False;
974 return Standard_True;
976 else if (typC1 == STANDARD_TYPE(Geom_BezierCurve)) {
978 // we must ensure that before fuse two bezier, the end of one curve does not
979 // corresponds to the beginning of the second.
980 if (Abs(f2-l1) > tollin && Abs(f1-l2) > tollin ) {
981 return Standard_False;
984 Handle(Geom_BezierCurve) B1 = *((Handle(Geom_BezierCurve)*)&C1);
985 Handle(Geom_BezierCurve) B2 = *((Handle(Geom_BezierCurve)*)&C2);
987 Standard_Integer nbpoles = B1->NbPoles();
988 if (nbpoles != B2->NbPoles()) {
989 return Standard_False;
992 TColgp_Array1OfPnt P1(1, nbpoles), P2(1, nbpoles);
996 for (Standard_Integer p = 1; p <= nbpoles; p++) {
997 if ( (P1(p)).Distance(P2(p)) > tollin) {
998 return Standard_False;
1002 if (!B1->IsRational()) {
1003 if (B2->IsRational()) {
1004 return Standard_False;
1008 if (!B2->IsRational()) {
1009 return Standard_False;
1013 if (B1->IsRational()) {
1014 TColStd_Array1OfReal W1(1, nbpoles), W2(1, nbpoles);
1018 for (Standard_Integer w = 1; w <= nbpoles; w++) {
1019 if (Abs(W1(w)-W2(w)) > tollin) {
1020 return Standard_False;
1024 return Standard_True;
1026 return Standard_False;
1030 //=======================================================================
1031 //function : BuildAncestors
1032 //purpose : This function is like TopExp::MapShapesAndAncestors except
1033 // that in the list of shape we do not want duplicate shapes.
1034 // if this is useful for other purpose we should create a new method in
1036 //=======================================================================
1038 void BRepLib_FuseEdges::BuildAncestors
1039 (const TopoDS_Shape& S,
1040 const TopAbs_ShapeEnum TS,
1041 const TopAbs_ShapeEnum TA,
1042 TopTools_IndexedDataMapOfShapeListOfShape& M) const
1045 TopTools_MapOfShape mapDuplicate;
1046 TopTools_ListIteratorOfListOfShape it;
1047 Standard_Integer iSh;
1049 TopExp::MapShapesAndAncestors(S,TS,TA,M);
1051 // for each shape of M
1052 for (iSh = 1; iSh <= M.Extent(); iSh++) {
1053 TopTools_ListOfShape& Lsh = M(iSh);
1055 mapDuplicate.Clear();
1056 // we check for duplicate in the list of Shape
1058 while (it.More() ) {
1059 if (!mapDuplicate.Contains(it.Value())) {
1060 mapDuplicate.Add(it.Value());
1072 //=======================================================================
1073 //function : UpdatePCurve
1075 //=======================================================================
1077 Standard_Boolean BRepLib_FuseEdges::UpdatePCurve(const TopoDS_Edge& theOldEdge,
1078 TopoDS_Edge& theNewEdge,
1079 const TopTools_ListOfShape& theLstEdg) const
1083 // get the pcurve of edge to substitute (theOldEdge)
1084 // using CurveOnSurface with Index syntax, so we can update the pcurve
1087 Handle(Geom2d_Curve) Curv2d;
1088 Handle(Geom_Surface) Surf;
1089 TopLoc_Location loc,locbid;
1090 Standard_Real ef,el,cf,cl;
1091 Standard_Integer iedg = 1;
1093 // take care that we want only Pcurve that maps on the surface where the 3D edges lies.
1094 const TopTools_ListOfShape& LmapFac = myMapEdgLstFac.FindFromKey(theOldEdge);
1097 BRep_Tool::CurveOnSurface(theOldEdge,Curv2d,Surf,loc,cf,cl,iedg);
1099 Standard_Boolean pcurveRebuilt = Standard_False;
1101 while (!Curv2d.IsNull()) {
1103 // we look for a face that contains the same surface as the one that cames
1104 // from CurveOnSurface
1105 Standard_Boolean SameSurf = Standard_False;
1106 TopTools_ListIteratorOfListOfShape itFac;
1108 for (itFac.Initialize(LmapFac); itFac.More(); itFac.Next() ) {
1109 const TopoDS_Shape& face = itFac.Value();
1110 Handle (Geom_Surface) S = BRep_Tool::Surface(TopoDS::Face(face),locbid);
1112 SameSurf = Standard_True;
1119 BRep_Tool::Range(theNewEdge,ef,el);
1121 //modified by NIZNHY-PKV Mon Nov 15 14:59:48 1999 _from
1122 TopoDS_Edge aFEdge = theOldEdge;
1123 aFEdge.Orientation(TopAbs_FORWARD);
1125 // take care if the edge is on the closing curve of a closed surface. In that case
1126 // we get the second pcurve by reversing the edge and calling again CurveOnSurface method
1128 BRep_Tool::CurveOnSurface(aFEdge,Curv2d,Surf,loc,cf,cl,iedg);
1129 if (BRep_Tool::IsClosed(theOldEdge,Surf,loc)) {
1131 TopoDS_Face aFFace = TopoDS::Face(itFac.Value());
1132 aFFace.Orientation(TopAbs_FORWARD);
1133 Handle(Geom2d_Curve) Curv2dR = BRep_Tool::CurveOnSurface(aFEdge,
1135 if (Curv2d->DynamicType() == STANDARD_TYPE(Geom2d_TrimmedCurve))
1136 Curv2d = (*((Handle(Geom2d_TrimmedCurve)*)&Curv2d))->BasisCurve();
1137 if (Curv2dR->DynamicType() == STANDARD_TYPE(Geom2d_TrimmedCurve))
1138 Curv2dR = (*((Handle(Geom2d_TrimmedCurve)*)&Curv2dR))->BasisCurve();
1140 B.UpdateEdge (theNewEdge,Curv2d,Curv2dR,Surf,loc,BRep_Tool::Tolerance(theNewEdge));
1143 // update the new edge
1144 if (Curv2d->DynamicType() == STANDARD_TYPE(Geom2d_TrimmedCurve))
1145 Curv2d = (*((Handle(Geom2d_TrimmedCurve)*)&Curv2d))->BasisCurve();
1147 f = Curv2d->FirstParameter();
1148 l = Curv2d->LastParameter();
1149 if (l-f + 2.* Epsilon(l-f) < el-ef)
1151 Handle(Geom2d_BoundedCurve) bcurve = Handle(Geom2d_BoundedCurve)::DownCast(Curv2d);
1152 if (bcurve.IsNull())
1153 bcurve = new Geom2d_TrimmedCurve( Curv2d, cf, cl );
1154 Geom2dConvert_CompCurveToBSplineCurve Concat( bcurve );
1155 TopTools_ListIteratorOfListOfShape iter( theLstEdg );
1157 for (; iter.More(); iter.Next())
1159 TopoDS_Edge& E = TopoDS::Edge(iter.Value());
1160 Standard_Real first, last;
1161 Handle(Geom2d_Curve) C = BRep_Tool::CurveOnSurface( E, Surf, loc, first, last );
1162 Handle(Geom2d_BoundedCurve) BC = Handle(Geom2d_BoundedCurve)::DownCast(C);
1164 BC = new Geom2d_TrimmedCurve( C, first, last );
1165 if (!Concat.Add( BC, Precision::PConfusion() ))
1166 // cannot merge pcurves
1167 return Standard_False;
1169 Curv2d = Concat.BSplineCurve();
1171 // check that new curve 2d is same range
1172 Standard_Real first = Curv2d->FirstParameter();
1173 Standard_Real last = Curv2d->LastParameter();
1174 if (Abs (first - ef) > Precision::PConfusion() ||
1175 Abs (last - el) > Precision::PConfusion())
1177 Handle(Geom2d_BSplineCurve) bc = Handle(Geom2d_BSplineCurve)::DownCast(Curv2d);
1178 TColStd_Array1OfReal Knots (1, bc->NbKnots());
1180 BSplCLib::Reparametrize (ef, el, Knots);
1181 bc->SetKnots(Knots);
1183 pcurveRebuilt = Standard_True;
1186 B.UpdateEdge (theNewEdge,Curv2d,Surf,loc,BRep_Tool::Tolerance(theNewEdge));
1189 // the old pcurve range is cf,cl. The new 3d edge range is ef,el. if we want
1190 // the pcurve to be samerange we must adapt the parameter of the edge. In general
1191 // cases cf=ef and cl=el expect for periodic curve if the new edge is going over
1193 if(!pcurveRebuilt) {
1194 if (theOldEdge.Orientation()== TopAbs_REVERSED) {
1195 B.Range(theNewEdge,cl-el+ef,cl);
1198 B.Range(theNewEdge,cf,cf+el-ef);
1205 BRep_Tool::CurveOnSurface(theOldEdge,Curv2d,Surf,loc,cf,cl,iedg);
1210 // force same parameter
1211 B.SameParameter (theNewEdge, Standard_False);
1212 BRepLib::SameParameter (theNewEdge, BRep_Tool::Tolerance(theNewEdge));
1215 return Standard_True;