1 // File: BRepFill_CompatibleWires.cxx
2 // Created: Thu Jul 2 16:47:25 1998
3 // Author: Joelle CHAUVET
7 #include <BRepFill_CompatibleWires.ixx>
9 #include <BRepAdaptor_Curve.hxx>
11 #include <BRepFill.hxx>
12 #include <BRepLib.hxx>
13 #include <BRepLib_FindSurface.hxx>
14 #include <BRepLib_MakeWire.hxx>
15 #include <BRepLib_MakeEdge.hxx>
16 #include <BRepExtrema_DistShapeShape.hxx>
17 #include <Bnd_Box.hxx>
18 #include <BRepBndLib.hxx>
19 #include <BRep_Builder.hxx>
20 #include <BRep_Tool.hxx>
21 #include <BRepTools_WireExplorer.hxx>
22 #include <BRepLProp.hxx>
23 #include <BRepGProp.hxx>
24 #include <GProp_GProps.hxx>
25 #include <GProp_PrincipalProps.hxx>
26 #include <Geom_Surface.hxx>
27 #include <Geom_Plane.hxx>
28 #include <Precision.hxx>
31 #include <TopExp_Explorer.hxx>
33 #include <TopoDS_Wire.hxx>
34 #include <TopTools_ListIteratorOfListOfShape.hxx>
35 #include <TopTools_DataMapOfShapeListOfShape.hxx>
36 #include <TopTools_IndexedDataMapOfShapeListOfShape.hxx>
37 #include <TopTools_DataMapIteratorOfDataMapOfShapeListOfShape.hxx>
38 #include <TopTools_HSequenceOfShape.hxx>
39 #include <TopTools_SequenceOfShape.hxx>
40 #include <TopTools_ListOfShape.hxx>
46 #include <gp_Circ.hxx>
47 #include <gp_Elips.hxx>
48 #include <TColgp_HArray1OfVec.hxx>
49 #include <TColgp_HArray1OfPnt.hxx>
51 #include <TColStd_Array1OfInteger.hxx>
52 #include <TColStd_Array1OfReal.hxx>
53 #include <TColStd_SequenceOfReal.hxx>
56 static void EdgesFromVertex (const TopoDS_Wire& W,
57 const TopoDS_Vertex& V,
61 TopTools_IndexedDataMapOfShapeListOfShape Map;
62 TopExp::MapShapesAndAncestors(W,TopAbs_VERTEX,TopAbs_EDGE,Map);
64 const TopTools_ListOfShape& List = Map.FindFromKey(V);
65 TopoDS_Edge e1 = TopoDS::Edge(List.First());
66 TopoDS_Edge e2 = TopoDS::Edge(List. Last());
68 BRepTools_WireExplorer anExp;
69 Standard_Integer I1=0, I2=0, NE=0;
71 for(anExp.Init(W); anExp.More(); anExp.Next()) {
73 const TopoDS_Edge& ECur = anExp.Current();
74 if (e1.IsSame(ECur)) {
77 if (e2.IsSame(ECur)) {
83 // numeros consecutifs
94 // numeros non consecutifs sur un wire ferme
107 static void SeqOfVertices (const TopoDS_Wire& W,
108 TopTools_SequenceOfShape& S)
111 Standard_Integer jj, cpt = 0;
113 for (PE.Init(W,TopAbs_VERTEX); PE.More(); PE.Next()) {
115 Standard_Boolean trouve=Standard_False;
116 for (jj=1;jj<=S.Length() && (!trouve);jj++) {
117 if (S.Value(jj).IsSame(PE.Current())) trouve = Standard_True;
119 if (!trouve) S.Append(PE.Current());
124 static Standard_Boolean PlaneOfWire (const TopoDS_Wire& W, gp_Pln& P)
126 Standard_Boolean isplane = Standard_True;
127 BRepLib_FindSurface findPlanarSurf;
128 Handle(Geom_Surface) S;
132 BRepGProp::LinearProperties(W,GP);
133 gp_Pnt Bary = GP.CentreOfMass();
135 // blindage pour les cas particuliers : 1 seule edge cercle ou ellipse
136 // sur un wire ferme !
137 Standard_Integer nbEdges = 0;
138 BRepTools_WireExplorer anExp;
140 Standard_Boolean wClosed = W.Closed();
142 // on regarde quand meme si les vertex sont les memes.
143 TopoDS_Vertex V1, V2;
144 TopExp::Vertices(W,V1,V2);
145 if ( V1.IsSame(V2)) wClosed = Standard_True;
147 TopoDS_Edge Edge = TopoDS::Edge(anExp.Current());
148 Standard_Real first, last;
150 Handle(Geom_Curve) curv;
151 curv = BRep_Tool::Curve(Edge, loc, first, last);
153 Handle(Geom_Curve)::DownCast(curv->Transformed(loc.Transformation()));
155 GeomAdaptor_Curve AdC;
157 for(; anExp.More(); anExp.Next()) {
160 if ( nbEdges==1 && AdC.GetType() == GeomAbs_Circle ) {
161 Bary = AdC.Circle().Location();
163 if ( nbEdges==1 && AdC.GetType() == GeomAbs_Ellipse ) {
164 Bary = AdC.Ellipse().Location();
168 findPlanarSurf.Init(W, -1, Standard_True);
169 if ( findPlanarSurf.Found()) {
170 S = findPlanarSurf.Surface();
171 L = findPlanarSurf.Location();
172 if (!L.IsIdentity()) S = Handle(Geom_Surface)::
173 DownCast(S->Transformed(L.Transformation()));
174 P = (Handle(Geom_Plane)::DownCast(S))->Pln();
179 GProp_PrincipalProps Pp = GP.PrincipalProperties();
181 Standard_Real R1, R2, R3,Tol = Precision::Confusion();
182 Pp.RadiusOfGyration(R1,R2,R3);
183 Standard_Real RMax = Max(Max(R1,R2),R3);
184 if ( ( Abs(RMax-R1)<Tol && Abs(RMax-R2)<Tol )
185 || ( Abs(RMax-R1)<Tol && Abs(RMax-R3)<Tol )
186 || ( Abs(RMax-R2)<Tol && Abs(RMax-R3)<Tol ) )
187 isplane = Standard_False;
189 if (R1>=R2 && R1>=R3) {
190 Vec = Pp.FirstAxisOfInertia();
192 else if (R2>=R1 && R2>=R3) {
193 Vec = Pp.SecondAxisOfInertia();
195 else if (R3>=R1 && R3>=R2) {
196 Vec = Pp.ThirdAxisOfInertia();
199 if (R3<=R2 && R3<=R1) {
200 Vec = Pp.ThirdAxisOfInertia();
202 else if (R2<=R1 && R2<=R3) {
203 Vec = Pp.SecondAxisOfInertia();
205 else if (R1<=R2 && R1<=R3) {
206 Vec = Pp.FirstAxisOfInertia();
209 gp_Ax3 repere(Bary,NDir,XDir);
210 Geom_Plane GPlan(repere);
220 static void WireContinuity (const TopoDS_Wire& W,
221 GeomAbs_Shape& contW)
225 Standard_Boolean IsDegenerated = Standard_False;
227 BRepTools_WireExplorer anExp;
228 Standard_Integer nbEdges=0;
229 Handle(TopTools_HSequenceOfShape) Edges = new TopTools_HSequenceOfShape();
230 for(anExp.Init(W); anExp.More(); anExp.Next()) {
232 Edges->Append(anExp.Current());
233 if (BRep_Tool::Degenerated(anExp.Current())) IsDegenerated = Standard_True;
236 if (!IsDegenerated) {
238 Standard_Boolean testconti = Standard_True;
240 for (Standard_Integer j=1;j<=nbEdges;j++) {
242 TopoDS_Edge Edge1, Edge2;
245 Edge1 = TopoDS::Edge (Edges->Value(nbEdges));
246 Edge2 = TopoDS::Edge (Edges->Value(1));
249 Edge1 = TopoDS::Edge (Edges->Value(j));
250 Edge2 = TopoDS::Edge (Edges->Value(j+1));
253 TopoDS_Vertex V1,V2,Vbid;
254 TopExp::Vertices(Edge1,Vbid,V1,Standard_True);
255 TopExp::Vertices(Edge2,V2,Vbid,Standard_True);
256 Standard_Real U1 = BRep_Tool::Parameter(V1,Edge1);
257 Standard_Real U2 = BRep_Tool::Parameter(V2,Edge2);
258 BRepAdaptor_Curve Curve1(Edge1);
259 BRepAdaptor_Curve Curve2(Edge2);
260 Standard_Real Eps = BRep_Tool::Tolerance(V2) + BRep_Tool::Tolerance(V1);
263 testconti = Curve1.Value(U1).IsEqual(Curve2.Value(U2), Eps);
266 cont = BRepLProp::Continuity(Curve1,Curve2,U1,U2,
267 Eps, Precision::Angular());
268 if (cont <= contW) contW = cont;
275 static void TrimEdge (const TopoDS_Edge& CurrentEdge,
276 const TColStd_SequenceOfReal& CutValues,
277 const Standard_Real t0, const Standard_Real t1,
278 const Standard_Boolean SeqOrder,
279 TopTools_SequenceOfShape& S)
283 Standard_Integer j, ndec=CutValues.Length();
284 Standard_Real first,last,m0,m1;
285 Handle(Geom_Curve) C = BRep_Tool::Curve(CurrentEdge,first,last);
287 TopoDS_Vertex Vf,Vl,Vbid,V0,V1;
288 TopAbs_Orientation CurrentOrient = CurrentEdge.Orientation();
289 TopExp::Vertices(CurrentEdge,Vf,Vl);
293 // de first vers last
296 for (j=1; j<=ndec; j++) {
298 m1 = (CutValues.Value(j)-t0)*(last-first)/(t1-t0)+first;
299 TopoDS_Edge CutE = BRepLib_MakeEdge(C,V0,Vbid,m0,m1);
300 CutE.Orientation(CurrentOrient);
303 V0 = TopExp::LastVertex(CutE);
306 TopoDS_Edge LastE = BRepLib_MakeEdge(C,V0,Vl,m0,last);
307 LastE.Orientation(CurrentOrient);
313 // de last vers first
316 for (j=ndec; j>=1; j--) {
318 m0 = (CutValues.Value(j)-t0)*(last-first)/(t1-t0)+first;
319 TopoDS_Edge CutE = BRepLib_MakeEdge(C,Vbid,V1,m0,m1);
320 CutE.Orientation(CurrentOrient);
323 V1 = TopExp::FirstVertex(CutE);
326 TopoDS_Edge LastE = BRepLib_MakeEdge(C,Vf,V1,first,m1);
327 LastE.Orientation(CurrentOrient);
336 static Standard_Boolean SearchRoot (const TopoDS_Vertex& V,
337 const TopTools_DataMapOfShapeListOfShape& Map,
338 TopoDS_Vertex& VRoot)
340 Standard_Boolean trouve = Standard_False;
342 TopTools_DataMapIteratorOfDataMapOfShapeListOfShape it;
343 for (it.Initialize(Map); it.More(); it.Next()) {
344 const TopTools_ListOfShape & List = it.Value();
345 TopTools_ListIteratorOfListOfShape itL;
346 Standard_Boolean ilyest = Standard_False;
347 for (itL.Initialize(List); itL.More(); itL.Next()) {
348 TopoDS_Vertex Vcur = TopoDS::Vertex(itL.Value());
349 if (Vcur.IsSame(V)) {
350 ilyest = Standard_True;
355 trouve = Standard_True;
356 VRoot = TopoDS::Vertex(it.Key());
363 static Standard_Boolean SearchVertex (const TopTools_ListOfShape& List,
364 const TopoDS_Wire& W,
367 Standard_Boolean trouve = Standard_False;
369 TopTools_SequenceOfShape SeqV;
370 SeqOfVertices(W,SeqV);
371 for (Standard_Integer ii=1;ii<=SeqV.Length();ii++) {
372 TopoDS_Vertex Vi = TopoDS::Vertex(SeqV.Value(ii));
373 TopTools_ListIteratorOfListOfShape itL;
374 Standard_Boolean ilyest = Standard_False;
375 for (itL.Initialize(List); itL.More(); itL.Next()) {
376 TopoDS_Vertex Vcur = TopoDS::Vertex(itL.Value());
377 if (Vcur.IsSame(Vi)) {
378 ilyest = Standard_True;
383 trouve = Standard_True;
392 static Standard_Boolean EdgeIntersectOnWire (const gp_Pnt& P1,
394 Standard_Real percent,
395 const TopTools_DataMapOfShapeListOfShape& Map,
396 const TopoDS_Wire& W,
401 BRepTools_WireExplorer anExp;
403 // construction de l'edge d'intersection
404 Standard_Boolean NewVertex = Standard_False;
405 gp_Lin droite(P1,gp_Dir(gp_Vec(P1,P2)));
406 // ATTENTION : en toute rigueur, il faudrait construire une demi-droite
407 // mais il y a un bug dans BRepExtrema_DistShapeShape
408 // on se contente de 100 * la distance entre P1 et P2
409 // en esperant que ce soit suffisant jusqu'a ce que le bug
411 // Standard_Real dernierparam = Precision::Infinite();
412 // ATTENTION : le retour !!
413 // 100 c'est mieux que 10 mais quelquefois c'est trop !
414 // finalement, rien ne vaut une bonne boite d'encombrement
415 // Standard_Real dernierparam = 100 * P1.Distance(P2);
417 BRepBndLib::Add(W,B);
418 Standard_Real x1,x2,y1,y2,z1,z2;
419 B.Get(x1,y1,z1,x2,y2,z2);
420 gp_Pnt BP1(x1,y1,z1), BP2(x2,y2,z2);
421 Standard_Real diag = BP1.Distance(BP2);
422 Standard_Real dernierparam = diag;
423 BRepLib_MakeEdge ME(droite,0.,dernierparam);
424 TopoDS_Edge ECur = BRepLib_MakeEdge(droite,0.,P1.Distance(P2));
426 // calcul de l'intersection par BRepExtrema (point de distance mini)
427 BRepExtrema_DistShapeShape DSS(ME.Edge(),W);
429 // on choisit la solution la plus proche de P2
430 Standard_Integer isol = 1;
431 Standard_Real dss = P2.Distance(DSS.PointOnShape2(isol));
432 for (Standard_Integer iss=2; iss<=DSS.NbSolution(); iss++) {
433 if (dss>P2.Distance(DSS.PointOnShape2(iss))) {
434 dss = P2.Distance(DSS.PointOnShape2(iss));
441 DSS.PointOnShape2(isol);
442 // la solution est-elle un nouveau vertex ?
443 NewVertex = (DSS.SupportTypeShape2(isol) != BRepExtrema_IsVertex);
445 TopoDS_Shape aLocalShape = DSS.SupportOnShape2(isol);
446 TopoDS_Edge E = TopoDS::Edge(aLocalShape);
447 // TopoDS_Edge E = TopoDS::Edge(DSS.SupportOnShape2(isol));
448 Standard_Real tol = Precision::PConfusion();
449 Standard_Real first,last,param;
450 BRep_Tool::Range(E,first,last);
451 tol = Max(tol,percent*Abs(last-first));
452 DSS.ParOnEdgeS2(isol,param);
453 if (Abs(first-param)<tol) {
454 NewVertex = Standard_False;
455 Vsol = TopExp::FirstVertex(E);
457 else if (Abs(last-param)<tol) {
458 NewVertex = Standard_False;
459 Vsol = TopExp::LastVertex(E);
464 if (SearchRoot(Vsol,Map,VRoot)) NewVertex = Standard_True;
468 TopoDS_Shape aLocalShape = DSS.SupportOnShape2(isol);
469 Vsol = TopoDS::Vertex(aLocalShape);
470 // Vsol = TopoDS::Vertex(DSS.SupportOnShape2(isol));
473 // il faut decouper l'edge
475 TopoDS_Shape aLocalShape = DSS.SupportOnShape2(isol);
476 TopoDS_Edge E = TopoDS::Edge(aLocalShape);
477 // TopoDS_Edge E = TopoDS::Edge(DSS.SupportOnShape2(isol));
481 Precision::PConfusion();
482 Standard_Real first,last,param;
483 DSS.ParOnEdgeS2(isol,param);
484 BRep_Tool::Range(E,first,last);
486 for (anExp.Init(W); anExp.More(); anExp.Next()) {
487 if (E.IsSame(anExp.Current())) {
489 = (anExp.CurrentVertex().IsSame(TopExp::FirstVertex(E)));
490 TopTools_SequenceOfShape SE;
492 TColStd_SequenceOfReal SR;
495 TrimEdge(E,SR,first,last,SO,SE);
496 TopoDS_Vertex VV1,VV2;
497 TopExp::Vertices(TopoDS::Edge(SE.Value(1)),VV1,VV2);
498 if (TopExp::FirstVertex(E).IsSame(VV1)
499 || TopExp::LastVertex(E).IsSame(VV1)) {
502 if (TopExp::FirstVertex(E).IsSame(VV2)
503 || TopExp::LastVertex(E).IsSame(VV2)) {
506 for (Standard_Integer k=1; k<=SE.Length(); k++) {
507 MW.Add(TopoDS::Edge(SE.Value(k)));
511 MW.Add(anExp.Current());
528 static void Transform (const Standard_Boolean WithRotation,
537 Pnew = P.Translated (Pos1,Pos2);
538 gp_Vec axe1 = Ax1, axe2 = Ax2;
539 if (!axe1.IsParallel(axe2,1.e-4)) {
540 gp_Vec Vtrans(Pos1,Pos2),Vsign;
541 Standard_Real alpha,beta,sign=1;
542 alpha = Vtrans.Dot(axe1);
543 beta = Vtrans.Dot(axe2);
544 if (alpha<-1.e-7) axe1 *=-1;
545 if (beta<1.e-7) axe2 *=-1;
546 alpha = Vtrans.Dot(axe1);
547 beta = Vtrans.Dot(axe2);
548 gp_Vec norm2 = axe1 ^ axe2;
549 Vsign.SetLinearForm(Vtrans.Dot(axe1),axe2,-Vtrans.Dot(axe2),axe1);
550 alpha = Vsign.Dot(axe1);
551 beta = Vsign.Dot(axe2);
552 Standard_Boolean pasnul = (Abs(alpha)>1.e-4 && Abs(beta)>1.e-4);
553 if ( alpha*beta>0.0 && pasnul ) sign=-1;
554 gp_Ax1 Norm(Pos2,norm2);
555 Standard_Real ang = axe1.AngleWithRef(axe2,norm2);
557 if (ang>PI/2) ang = ang - PI;
558 if (ang<-PI/2) ang = ang + PI;
561 Pnew = Pnew.Rotated (Norm,ang);
565 static void BuildConnectedEdges(const TopoDS_Wire& aWire,
566 const TopoDS_Edge& StartEdge,
567 const TopoDS_Vertex& StartVertex,
568 TopTools_ListOfShape& ConnectedEdges)
570 TopTools_IndexedDataMapOfShapeListOfShape MapVE;
571 TopExp::MapShapesAndAncestors(aWire, TopAbs_VERTEX, TopAbs_EDGE, MapVE);
572 TopoDS_Edge CurEdge = StartEdge;
573 TopoDS_Vertex CurVertex = StartVertex;
574 TopoDS_Vertex Origin, V1, V2;
575 TopExp::Vertices(StartEdge, V1, V2);
576 Origin = (V1.IsSame(StartVertex))? V2 : V1;
580 TopTools_ListIteratorOfListOfShape itE( MapVE.FindFromKey(CurVertex) );
581 for (; itE.More(); itE.Next())
583 TopoDS_Edge anEdge = TopoDS::Edge(itE.Value());
584 if (!anEdge.IsSame(CurEdge))
586 ConnectedEdges.Append(anEdge);
587 TopExp::Vertices(anEdge, V1, V2);
588 CurVertex = (V1.IsSame(CurVertex))? V2 : V1;
593 if (CurVertex.IsSame(Origin))
598 //=======================================================================
599 //function : BRepFill_CompatibleWires
601 //=======================================================================
603 BRepFill_CompatibleWires::BRepFill_CompatibleWires()
604 :myIsDone(Standard_False)
609 //=======================================================================
610 //function : BRepFill_CompatibleWires
612 //=======================================================================
614 BRepFill_CompatibleWires::BRepFill_CompatibleWires(const TopTools_SequenceOfShape& Sections)
620 //=======================================================================
623 //=======================================================================
625 void BRepFill_CompatibleWires::Init(const TopTools_SequenceOfShape& Sections)
630 myIsDone = Standard_False;
636 //=======================================================================
637 //function : SetPercent
639 //=======================================================================
641 void BRepFill_CompatibleWires::SetPercent(const Standard_Real Percent)
643 if (0.<Percent && Percent<1.) myPercent = Percent;
648 //=======================================================================
651 //=======================================================================
653 Standard_Boolean BRepFill_CompatibleWires::IsDone() const
659 //=======================================================================
662 //=======================================================================
664 const TopTools_SequenceOfShape& BRepFill_CompatibleWires::Shape() const
670 //=======================================================================
671 //function : GeneratedShapes
673 //=======================================================================
675 const TopTools_ListOfShape& BRepFill_CompatibleWires::GeneratedShapes
676 (const TopoDS_Edge& SubSection) const
679 if (myMap.IsBound(SubSection)) {
680 return myMap(SubSection);
683 static TopTools_ListOfShape Empty;
689 //=======================================================================
692 //=======================================================================
694 void BRepFill_CompatibleWires::Perform (const Standard_Boolean WithRotation)
696 // compute origin and orientation on wires to avoid twisted results
697 // and update wires to have same number of edges
699 // determination de report:
700 // si le nombre d'elements est identique et si les wires ont des discontinuites
701 // en tangence, on n'effectue pas le report par abscisse curviligne, ni
702 Standard_Integer nbSects = myWork.Length(), i;
703 BRepTools_WireExplorer anExp;
704 Standard_Integer nbmax=0, nbmin=0;
705 TColStd_Array1OfInteger nbEdges(1,nbSects);
706 Standard_Boolean report;
707 GeomAbs_Shape contS=GeomAbs_CN;
709 for (i=1; i<=nbSects; i++) {
710 TopoDS_Shape aLocalShape = myWork(i).Oriented(TopAbs_FORWARD);
711 myWork(i) = TopoDS::Wire(aLocalShape);
712 // myWork(i) = TopoDS::Wire(myWork(i).Oriented(TopAbs_FORWARD));
713 TopoDS_Wire W = TopoDS::Wire(myWork(i));
714 WireContinuity(W,cont);
715 if (cont<contS) contS=cont;
717 for(anExp.Init(W); anExp.More(); anExp.Next() ) nbEdges(i)++;
718 if (i==1) nbmin = nbEdges(i);
719 if (nbmax<nbEdges(i)) nbmax = nbEdges(i);
720 if (nbmin>nbEdges(i)) nbmin = nbEdges(i);
722 // si on n'a pas le meme nombre d'elements ou si tous les wires sont au moins
723 // C1, on effectue le report par abscisse curviligne des decoupes sinon, on se
724 // fait un report vertex / Vertex
725 report = (nbmax != nbmin || contS >= GeomAbs_C1 );
727 // initialisation de la map
728 Standard_Integer nbE = 0;
729 TopTools_ListOfShape Empty;
730 for (i=1; i<=nbSects; i++) {
731 TopoDS_Wire W = TopoDS::Wire(myWork(i));
732 for(anExp.Init(W); anExp.More(); anExp.Next() ) {
733 TopoDS_Edge E = TopoDS::Edge(anExp.Current());
740 // sections ouvertes / sections fermees
741 // initialisation de myDegen1, myDegen2
742 Standard_Integer ideb=1, ifin=myWork.Length();
743 // on regarde si le premier wire est ponctuel
744 myDegen1 = Standard_True;
745 for(anExp.Init(TopoDS::Wire(myWork(ideb))); anExp.More(); anExp.Next()) {
746 myDegen1 = myDegen1 && (BRep_Tool::Degenerated(anExp.Current()));
748 if (myDegen1) ideb++;
749 // on regarde si le dernier wire est ponctuel
750 myDegen2 = Standard_True;
751 for(anExp.Init(TopoDS::Wire(myWork(ifin))); anExp.More(); anExp.Next()) {
752 myDegen2 = myDegen2 && (BRep_Tool::Degenerated(anExp.Current()));
754 if (myDegen2) ifin--;
756 Standard_Boolean wClosed, allClosed = Standard_True, allOpen = Standard_True;
757 for (i=ideb; i<=ifin; i++) {
758 wClosed = myWork(i).Closed();
760 // on regarde quand meme si les vertex sont les memes.
761 TopoDS_Vertex V1, V2;
762 TopExp::Vertices(TopoDS::Wire(myWork(i)),V1,V2);
763 if ( V1.IsSame(V2)) wClosed = Standard_True;
765 allClosed = (allClosed && wClosed);
766 allOpen = (allOpen && !wClosed);
770 // Toutes les sections sont fermees
772 // same number of elements
773 SameNumberByPolarMethod(WithRotation);
777 ComputeOrigin(Standard_False);
779 myIsDone = Standard_True;
782 // Toutes les sections sont ouvertes
785 // same number of elements
787 SameNumberByACR(report);
789 myIsDone = Standard_True;
792 // Il y a des sections ouvertes et des sections fermees :
794 Standard_DomainError::Raise("Sections must be all closed or all open");
802 //=======================================================================
803 //function : Generated
805 //=======================================================================
807 const TopTools_DataMapOfShapeListOfShape& BRepFill_CompatibleWires::Generated() const
813 //=======================================================================
814 //function : SameNumberByPolarMethod
816 //=======================================================================
818 void BRepFill_CompatibleWires::
819 SameNumberByPolarMethod(const Standard_Boolean WithRotation)
823 Standard_Integer NbSects=myWork.Length();
824 BRepTools_WireExplorer anExp;
825 TopoDS_Vertex V1, V2;
827 Standard_Boolean allClosed = Standard_True;
828 Standard_Integer i,ii,ideb=1,ifin=NbSects;
829 for (i=1; i<=NbSects; i++) {
830 allClosed = (allClosed && myWork(i).Closed());
833 Standard_NoSuchObject::Raise("BRepFill_CompatibleWires::SameNumberByPolarMethod : the wires must be closed");
835 // Nombre max de decoupes possibles
836 Standard_Integer NbMaxV = 0;
837 for (i=1; i<=NbSects; i++) {
838 for(anExp.Init(TopoDS::Wire(myWork(i))); anExp.More(); anExp.Next()) {
843 // sections ponctuelles, sections bouclantes ?
844 if (myDegen1) ideb++;
845 if (myDegen2) ifin--;
846 Standard_Boolean vClosed = (!myDegen1) && (!myDegen2)
847 && (myWork(ideb).IsSame(myWork(ifin)));
849 // construction des tableaux de plans des wires
851 Handle(TColgp_HArray1OfPnt) Pos
852 = new (TColgp_HArray1OfPnt) (1,NbSects);
853 Handle(TColgp_HArray1OfVec) Axe
854 = new (TColgp_HArray1OfVec) (1,NbSects);
855 for (i=ideb;i<=ifin;i++) {
856 if (PlaneOfWire(TopoDS::Wire(myWork(i)),P)) {
857 Pos->SetValue(i,P.Location());
858 Axe->SetValue(i,gp_Vec(P.Axis().Direction()));
861 TopTools_SequenceOfShape SeqV;
863 SeqOfVertices(TopoDS::Wire(myWork(1)),SeqV);
864 Pos->SetValue(1,BRep_Tool::Pnt(TopoDS::Vertex(SeqV.Value(1))));
865 Axe->SetValue(1,Axe->Value(ideb));
868 SeqOfVertices(TopoDS::Wire(myWork(NbSects)),SeqV);
869 Pos->SetValue(NbSects,BRep_Tool::Pnt(TopoDS::Vertex(SeqV.Value(1))));
870 Axe->SetValue(NbSects,Axe->Value(ifin));
873 // construction de RMap, map des reports du wire i vers le wire i-1
874 TopTools_DataMapOfShapeListOfShape RMap;
878 for (i=ifin; i>ideb; i--) {
880 const TopoDS_Wire& wire1 = TopoDS::Wire(myWork(i));
882 // sequence des vertex du premier wire
883 SeqOfVertices(wire1,SeqV);
884 if (SeqV.Length()>NbMaxV)
885 Standard_NoSuchObject::Raise("BRepFill::SameNumberByPolarMethod failed");
887 // extremite du premier wire
888 V1 = TopoDS::Vertex(SeqV.Value(1));
891 const TopoDS_Wire& wire2 =
893 TopoDS::Wire(myWork(i-1));
894 // boucle sur les vertex de wire1
895 for (ii=1;ii<=SeqV.Length();ii++) {
897 TopoDS_Vertex Vi = TopoDS::Vertex(SeqV.Value(ii));
899 // init de RMap pour Vi
900 TopTools_ListOfShape Init;
904 // il faut chercher l'intersection Vi - wire2
905 gp_Pnt Pi = BRep_Tool::Pnt(Vi);
907 // on ramene Pi dans le plan courant
909 Transform(WithRotation,Pi,
910 Pos->Value(i),Axe->Value(i),
911 Pos->Value(i-1),Axe->Value(i-1),Pnew);
913 // calcul de l'intersection
914 TopoDS_Shape Support;
915 Standard_Boolean NewVertex;
918 if (Pnew.Distance(Pos->Value(i-1))>Precision::Confusion()) {
919 Standard_Real percent = myPercent;
920 NewVertex = EdgeIntersectOnWire(Pos->Value(i-1),Pnew,percent,
921 RMap,TopoDS::Wire(myWork(i-1)),
923 if (NewVertex) myWork(i-1) = newwire;
924 RMap(Vi).Append(Vsol);
930 // initialisation de MapVLV, map des correspondances vertex - liste de vertex
931 TopTools_DataMapOfShapeListOfShape MapVLV;
932 SeqOfVertices(TopoDS::Wire(myWork(ideb)),SeqV);
933 Standard_Integer SizeMap = SeqV.Length();
935 for (ii=1;ii<=SizeMap;ii++) {
936 TopoDS_Vertex Vi = TopoDS::Vertex(SeqV.Value(ii));
937 TopTools_ListOfShape Init;
940 MapVLV.Bind(Vi,Init);
941 Standard_Integer NbV = 1;
944 Standard_Boolean tantque = SearchRoot(V0,RMap,V1);
946 MapVLV(Vi).Append(V1);
948 // test sur NbV necessaire pour les sections bouclantes
949 if (V1.IsSame(Vi) || NbV >= myWork.Length()) {
950 tantque = Standard_False;
954 tantque = SearchRoot(V0,RMap,V1);
960 for (i=ideb; i<ifin; i++) {
962 const TopoDS_Wire& wire1 = TopoDS::Wire(myWork(i));
964 // sequence des vertex du premier wire
965 SeqOfVertices(wire1,SeqV);
966 if ( SeqV.Length()>NbMaxV || SeqV.Length()>SizeMap )
967 Standard_NoSuchObject::Raise("BRepFill::SameNumberByPolarMethod failed");
969 // extremite du premier wire
970 V1 = TopoDS::Vertex(SeqV.Value(1));
973 const TopoDS_Wire& wire2 = TopoDS::Wire(myWork(i+1));
975 // boucle sur les vertex de wire1
976 for (ii=1;ii<=SeqV.Length();ii++) {
978 TopoDS_Vertex Vi = TopoDS::Vertex(SeqV.Value(ii));
981 Standard_Boolean intersect = Standard_True;
982 if (SearchRoot(Vi,MapVLV,VRoot)) {
983 const TopTools_ListOfShape& LVi = MapVLV(VRoot);
986 intersect = (!SearchVertex(LVi,wire2,VonW));
990 // il faut chercher l'intersection Vi - wire2
991 gp_Pnt Pi = BRep_Tool::Pnt(Vi);
993 // on ramene Pi dans le plan courant
995 Transform(WithRotation,Pi,
996 Pos->Value(i),Axe->Value(i),
997 Pos->Value(i+1),Axe->Value(i+1),Pnew);
999 // calcul de l'intersection
1000 TopoDS_Shape Support;
1001 Standard_Boolean NewVertex;
1003 TopoDS_Wire newwire;
1004 if (Pnew.Distance(Pos->Value(i+1))>Precision::Confusion()) {
1005 Standard_Real percent = myPercent;
1006 NewVertex = EdgeIntersectOnWire(Pos->Value(i+1),Pnew,percent,
1007 MapVLV,TopoDS::Wire(myWork(i+1)),
1009 MapVLV(VRoot).Append(Vsol);
1010 if (NewVertex) myWork(i+1) = newwire;
1017 // mise en ordre des wires en suivant MapVLV
1018 TopoDS_Wire wire = TopoDS::Wire(myWork(ideb));
1020 // sauf le dernier si les sections sont bouclantes
1021 Standard_Integer ibout = ifin;
1022 if (vClosed) ibout--;
1024 for ( i=ideb+1; i<=ibout; i++) {
1026 BRepLib_MakeWire MW;
1029 TopoDS_Edge ECur = anExp.Current();
1030 TopoDS_Vertex VF,VL;
1031 TopExp::Vertices(ECur,VF,VL,Standard_True);
1032 Standard_Real U1 = BRep_Tool::Parameter(VF,ECur);
1033 Standard_Real U2 = BRep_Tool::Parameter(VL,ECur);
1034 BRepAdaptor_Curve Curve(ECur);
1035 gp_Pnt PPs = Curve.Value(0.1*(U1+9*U2));
1036 TopTools_ListIteratorOfListOfShape itF(MapVLV(VF)),itL(MapVLV(VL));
1037 Standard_Integer rang = ideb;
1043 TopoDS_Vertex V1 = TopoDS::Vertex(itF.Value()), V2 = TopoDS::Vertex(itL.Value());
1045 Standard_Real scalmax=0.;
1046 TopoDS_Iterator itW( myWork(i) );
1048 for(; itW.More(); itW.Next())
1050 TopoDS_Edge E = TopoDS::Edge(itW.Value());
1051 TopoDS_Vertex VVF,VVL;
1052 TopExp::Vertices(E,VVF,VVL,Standard_True);
1054 // tri des edges candidates
1055 Standard_Real scal1,scal2;
1056 if ( (V1.IsSame(VVF)&&V2.IsSame(VVL)) || (V2.IsSame(VVF)&&V1.IsSame(VVL)) ) {
1057 Standard_Real U1 = BRep_Tool::Parameter(VVF,E);
1058 Standard_Real U2 = BRep_Tool::Parameter(VVL,E);
1059 BRepAdaptor_Curve Curve(E);
1060 gp_Pnt PP1 = Curve.Value(0.1*(U1+9*U2));
1061 gp_Pnt PP2 = Curve.Value(0.1*(9*U1+U2));
1063 for (rang=i;rang>ideb;rang--) {
1064 Transform(WithRotation, PP1,
1065 Pos->Value(rang), Axe->Value(rang),
1066 Pos->Value(rang-1), Axe->Value(rang-1), PP1);
1067 Transform(WithRotation, PP2,
1068 Pos->Value(rang), Axe->Value(rang),
1069 Pos->Value(rang-1), Axe->Value(rang-1), PP2);
1071 gp_Vec Ns(Pos->Value(ideb),PPs);
1072 Ns = Ns.Normalized();
1073 gp_Vec N1(Pos->Value(ideb),PP1);
1074 N1 = N1.Normalized();
1075 gp_Vec N2(Pos->Value(ideb),PP2);
1076 N2 = N2.Normalized();
1078 if (scal1>scalmax) {
1083 if (scal2>scalmax) {
1085 TopoDS_Shape aLocalShape = E.Reversed();
1086 Esol = TopoDS::Edge(aLocalShape);
1089 } //end of for(; itW.More(); itW.Next())
1092 TopTools_ListOfShape ConnectedEdges;
1093 BuildConnectedEdges( TopoDS::Wire(myWork(i)), Esol, V2, ConnectedEdges );
1095 TopTools_ListIteratorOfListOfShape itCE(ConnectedEdges);
1096 for(; anExp.More(), itCE.More(); anExp.Next(), itCE.Next())
1098 ECur = anExp.Current();
1099 TopExp::Vertices(ECur,VF,VL,Standard_True);
1100 U1 = BRep_Tool::Parameter(VF,ECur);
1101 U2 = BRep_Tool::Parameter(VL,ECur);
1102 Curve.Initialize(ECur);
1103 PPs = Curve.Value(0.1*(U1+9*U2));
1105 TopoDS_Edge E = TopoDS::Edge(itCE.Value());
1106 TopoDS_Vertex VVF,VVL;
1107 TopExp::Vertices(E,VVF,VVL,Standard_True);
1109 // tri des edges candidates
1110 Standard_Real scal1,scal2;
1111 U1 = BRep_Tool::Parameter(VVF,E);
1112 U2 = BRep_Tool::Parameter(VVL,E);
1113 Curve.Initialize(E);
1114 gp_Pnt PP1 = Curve.Value(0.1*(U1+9*U2));
1115 gp_Pnt PP2 = Curve.Value(0.1*(9*U1+U2));
1117 for (rang=i;rang>ideb;rang--) {
1118 Transform(WithRotation, PP1,
1119 Pos->Value(rang), Axe->Value(rang),
1120 Pos->Value(rang-1), Axe->Value(rang-1), PP1);
1121 Transform(WithRotation, PP2,
1122 Pos->Value(rang), Axe->Value(rang),
1123 Pos->Value(rang-1), Axe->Value(rang-1), PP2);
1125 gp_Vec Ns(Pos->Value(ideb),PPs);
1126 Ns = Ns.Normalized();
1127 gp_Vec N1(Pos->Value(ideb),PP1);
1128 N1 = N1.Normalized();
1129 gp_Vec N2(Pos->Value(ideb),PP2);
1130 N2 = N2.Normalized();
1137 myWork(i) = MW.Wire();
1140 // sections bouclantes ?
1141 if (vClosed) myWork(myWork.Length()) = myWork(1);
1143 // verification du nombre d'edges pour debug
1144 Standard_Integer nbmax=0, nbmin=0;
1145 for ( i=ideb; i<=ifin; i++) {
1146 Standard_Integer nbEdges=0;
1147 for(anExp.Init(TopoDS::Wire(myWork(i))); anExp.More(); anExp.Next()) {
1150 if (i==ideb) nbmin = nbEdges;
1151 if (nbmax<nbEdges) nbmax = nbEdges;
1152 if (nbmin>nbEdges) nbmin = nbEdges;
1155 Standard_NoSuchObject::Raise("BRepFill_CompatibleWires::SameNumberByPolarMethod failed");
1160 //=======================================================================
1161 //function : SameNumberByACR
1163 //=======================================================================
1165 void BRepFill_CompatibleWires::SameNumberByACR(const Standard_Boolean report)
1167 // find the dimension
1168 Standard_Integer ideb=1, ifin=myWork.Length();
1169 BRepTools_WireExplorer anExp;
1171 // sections ponctuelles, sections bouclantes ?
1172 if (myDegen1) ideb++;
1173 if (myDegen2) ifin--;
1174 Standard_Boolean vClosed = (!myDegen1) && (!myDegen2)
1175 && (myWork(ideb).IsSame(myWork(ifin)));
1177 Standard_Integer nbSects = myWork.Length(), i;
1178 Standard_Integer nbmax=0, nbmin=0;
1179 TColStd_Array1OfInteger nbEdges(1,nbSects);
1180 for (i=1; i<=nbSects; i++) {
1182 for(anExp.Init(TopoDS::Wire(myWork(i))); anExp.More(); anExp.Next()) {
1185 if (i==1) nbmin = nbEdges(i);
1186 if (nbmax<nbEdges(i)) nbmax = nbEdges(i);
1187 if (nbmin>nbEdges(i)) nbmin = nbEdges(i);
1193 if (report || nbmin<nbmax) {
1194 // insertion des decoupes
1195 Standard_Integer nbdec=(nbmax-1)*nbSects+1;
1196 Standard_Real tol = 0.01;
1197 TColStd_Array1OfReal dec(1,nbdec);
1200 // calcul du tableau des decoupes
1201 Standard_Integer j,k,l;
1202 for (i=1; i<=nbSects; i++) {
1204 const TopoDS_Wire& wire1 = TopoDS::Wire(myWork(i));
1205 Standard_Integer nbE = 0;
1206 for(anExp.Init(wire1); anExp.More(); anExp.Next()) {
1209 // longueur et ACR du wire
1210 TColStd_Array1OfReal ACR(0,nbE);
1212 BRepFill::ComputeACR(wire1, ACR);
1213 // insertion des ACR du wire dans le tableau des decoupes
1214 for (j=1; j<ACR.Length()-1; j++) {
1216 while (dec(k)<ACR(j)) {
1220 if (dec(k-1)+tol<ACR(j)&& ACR(j)+tol<dec(k)) {
1221 for (l=nbdec-1;l>=k;l--) {
1229 // tableau effectif des decoupes
1236 TColStd_Array1OfReal dec2(1,nbdec);
1237 for (k=1;k<=nbdec;k++) {
1241 // insertion des decoupes dans chaque wire
1242 for (i=1; i<=nbSects; i++) {
1243 const TopoDS_Wire& oldwire = TopoDS::Wire(myWork(i));
1244 TopoDS_Wire newwire = BRepFill::InsertACR(oldwire, dec2, tol);
1245 BRepTools_WireExplorer anExp1,anExp2;
1246 anExp1.Init(oldwire);
1247 anExp2.Init(newwire);
1248 for (;anExp1.More();anExp1.Next()) {
1249 const TopoDS_Edge& Ecur = anExp1.Current();
1250 if (!Ecur.IsSame(TopoDS::Edge(anExp2.Current()))) {
1251 TopTools_ListOfShape LE;
1254 const TopoDS_Vertex& V1 = anExp1.CurrentVertex();
1255 TopoDS_Vertex VF,VR;
1256 TopExp::Vertices(Ecur,VF,VR,Standard_True);
1257 if (V1.IsSame(VF)) P1 = BRep_Tool::Pnt(VR);
1258 if (V1.IsSame(VR)) P1 = BRep_Tool::Pnt(VF);
1259 TopoDS_Vertex V2 = anExp2.CurrentVertex();
1260 TopExp::Vertices(TopoDS::Edge(anExp2.Current()),
1261 VF,VR,Standard_True);
1262 if (V2.IsSame(VF)) P2 = BRep_Tool::Pnt(VR);
1263 if (V2.IsSame(VR)) P2 = BRep_Tool::Pnt(VF);
1264 while (P1.Distance(P2)>1.e-3) {
1265 LE.Append(anExp2.Current());
1267 V2 = anExp2.CurrentVertex();
1268 TopExp::Vertices(TopoDS::Edge(anExp2.Current()),
1269 VF,VR,Standard_True);
1270 if (V2.IsSame(VF)) P2 = BRep_Tool::Pnt(VR);
1271 if (V2.IsSame(VR)) P2 = BRep_Tool::Pnt(VF);
1272 if (P1.Distance(P2)<=1.e-3) {
1273 LE.Append(anExp2.Current());
1278 TopTools_DataMapIteratorOfDataMapOfShapeListOfShape itmap;
1279 //TopTools_ListIteratorOfListOfShape itlist;
1280 TopoDS_Edge Ancestor;
1281 Standard_Integer nbedge, nblist=0;
1282 Standard_Boolean found = Standard_False;
1284 for (itmap.Initialize(myMap);itmap.More()&&(!found);itmap.Next()) {
1286 TopTools_ListIteratorOfListOfShape itlist(itmap.Value());
1288 while (itlist.More()&&(!found)) {
1290 TopoDS_Edge ECur = TopoDS::Edge(itlist.Value());
1292 if (Ecur.IsSame(ECur)) {
1293 Ancestor = TopoDS::Edge(itmap.Key());
1294 found = Standard_True;
1295 myMap(Ancestor).InsertBefore(LE,itlist);
1296 myMap(Ancestor).Remove(itlist);
1298 if (itlist.More()) itlist.Next();
1309 myWork(i) = newwire;
1315 // sections bouclantes ?
1316 if (vClosed) myWork(myWork.Length()) = myWork(1);
1318 // verification du nombre d'edges pour debug
1320 for (i=ideb; i<=ifin; i++) {
1322 for(anExp.Init(TopoDS::Wire(myWork(i))); anExp.More(); anExp.Next()) {
1325 if (i==ideb) nbmin = nbEdges(i);
1326 if (nbmax<nbEdges(i)) nbmax = nbEdges(i);
1327 if (nbmin>nbEdges(i)) nbmin = nbEdges(i);
1330 Standard_NoSuchObject::Raise("BRepFill_CompatibleWires::SameNumberByACR failed");
1333 //=======================================================================
1334 //function : ComputeOrigin
1336 //=======================================================================
1338 void BRepFill_CompatibleWires::ComputeOrigin(const Standard_Boolean polar )
1340 // reorganize the wires respecting orientation and origin
1342 TopoDS_Vertex Vdeb, Vfin;
1343 gp_Pnt Pdeb, Psuiv, PPs;
1345 BRepTools_WireExplorer anExp;
1347 Standard_Boolean wClosed, allClosed = Standard_True;
1349 Standard_Integer NbSects = myWork.Length();
1350 Standard_Integer i, ideb=1,ifin=NbSects;
1352 // sections ponctuelles, sections bouclantes
1353 if (myDegen1) ideb++;
1354 if (myDegen2) ifin--;
1355 Standard_Boolean vClosed = (!myDegen1) && (!myDegen2)
1356 && (myWork(ideb).IsSame(myWork(ifin)));
1359 for (i=ideb; i<=ifin; i++) {
1360 wClosed = myWork(i).Closed();
1362 // on regarde quand meme si les vertex sont les memes.
1363 TopoDS_Vertex V1, V2;
1364 TopExp::Vertices(TopoDS::Wire(myWork(i)),V1,V2);
1365 if ( V1.IsSame(V2)) wClosed = Standard_True;
1367 allClosed = (allClosed && wClosed);
1370 for (i=ideb; i<=ifin; i++) {
1371 allClosed = (allClosed && myWork(i).Closed());
1375 Standard_NoSuchObject::Raise("BRepFill_CompatibleWires::ComputeOrigin : the wires must be closed");
1378 // Nombre max de decoupes possibles
1379 Standard_Integer NbMaxV = 0;
1380 for (i=1; i<=NbSects; i++) {
1381 for(anExp.Init(TopoDS::Wire(myWork(i))); anExp.More(); anExp.Next()) {
1386 // construction des tableaux de plans des wires
1388 Handle(TColgp_HArray1OfPnt) Pos
1389 = new (TColgp_HArray1OfPnt) (1,NbSects);
1390 Handle(TColgp_HArray1OfVec) Axe
1391 = new (TColgp_HArray1OfVec) (1,NbSects);
1392 for (i=ideb;i<=ifin;i++) {
1393 if (PlaneOfWire(TopoDS::Wire(myWork(i)),P)) {
1394 Pos->SetValue(i,P.Location());
1395 Axe->SetValue(i,gp_Vec(P.Axis().Direction()));
1398 TopTools_SequenceOfShape SeqV;
1400 SeqOfVertices(TopoDS::Wire(myWork(1)),SeqV);
1401 Pos->SetValue(1,BRep_Tool::Pnt(TopoDS::Vertex(SeqV.Value(1))));
1402 Axe->SetValue(1,Axe->Value(ideb));
1405 SeqOfVertices(TopoDS::Wire(myWork(NbSects)),SeqV);
1406 Pos->SetValue(NbSects,BRep_Tool::Pnt(TopoDS::Vertex(SeqV.Value(1))));
1407 Axe->SetValue(NbSects,Axe->Value(ifin));
1411 //Consider that all wires have same number of edges (polar==Standard_False)
1412 TopTools_SequenceOfShape PrevSeq;
1413 Standard_Integer theLength = 0;
1414 const TopoDS_Wire& wire = TopoDS::Wire( myWork(ideb) );
1415 for (anExp.Init(wire); anExp.More(); anExp.Next())
1417 PrevSeq.Append(anExp.CurrentVertex());
1421 for (i = ideb+1; i <= ifin; i++)
1423 const TopoDS_Wire& wire = TopoDS::Wire(myWork(i));
1424 TopoDS_Wire newwire;
1426 BB.MakeWire(newwire);
1428 TopTools_SequenceOfShape SeqVertices, SeqEdges;
1429 for (anExp.Init(wire); anExp.More(); anExp.Next())
1431 SeqVertices.Append( anExp.CurrentVertex() );
1432 SeqEdges.Append( anExp.Current() );
1435 Standard_Real MinSumDist = Precision::Infinite();
1436 Standard_Integer jmin, j, k, n;
1437 Standard_Boolean forward;
1438 if (i == myWork.Length() && myDegen2)
1440 // derniere section ponctuelle
1442 forward = Standard_True;
1445 for (j = 1; j <= theLength; j++)
1448 Standard_Real SumDist = 0.;
1449 for (k = j, n = 1; k <= theLength; k++, n++)
1451 const TopoDS_Vertex& Vprev = TopoDS::Vertex( PrevSeq(n) );
1452 gp_Pnt Pprev = BRep_Tool::Pnt(Vprev);
1453 const TopoDS_Vertex& V = TopoDS::Vertex( SeqVertices(k) );
1454 gp_Pnt P = BRep_Tool::Pnt(V);
1455 SumDist += Pprev.Distance(P);
1457 for (k = 1; k < j; k++, n++)
1459 const TopoDS_Vertex& Vprev = TopoDS::Vertex( PrevSeq(n) );
1460 gp_Pnt Pprev = BRep_Tool::Pnt(Vprev);
1461 const TopoDS_Vertex& V = TopoDS::Vertex( SeqVertices(k) );
1462 gp_Pnt P = BRep_Tool::Pnt(V);
1463 SumDist += Pprev.Distance(P);
1465 if (SumDist < MinSumDist)
1467 MinSumDist = SumDist;
1469 forward = Standard_True;
1474 for (k = j, n = 1; k >= 1; k--, n++)
1476 const TopoDS_Vertex& Vprev = TopoDS::Vertex( PrevSeq(n) );
1477 gp_Pnt Pprev = BRep_Tool::Pnt(Vprev);
1478 const TopoDS_Vertex& V = TopoDS::Vertex( SeqVertices(k) );
1479 gp_Pnt P = BRep_Tool::Pnt(V);
1480 SumDist += Pprev.Distance(P);
1482 for (k = theLength; k > j; k--, n++)
1484 const TopoDS_Vertex& Vprev = TopoDS::Vertex( PrevSeq(n) );
1485 gp_Pnt Pprev = BRep_Tool::Pnt(Vprev);
1486 const TopoDS_Vertex& V = TopoDS::Vertex( SeqVertices(k) );
1487 gp_Pnt P = BRep_Tool::Pnt(V);
1488 SumDist += Pprev.Distance(P);
1490 if (SumDist < MinSumDist)
1492 MinSumDist = SumDist;
1494 forward = Standard_False;
1501 for (j = jmin; j <= theLength; j++)
1503 BB.Add( newwire, TopoDS::Edge(SeqEdges(j)) );
1504 PrevSeq.Append( SeqVertices(j) );
1506 for (j = 1; j < jmin; j++)
1508 BB.Add( newwire, TopoDS::Edge(SeqEdges(j)) );
1509 PrevSeq.Append( SeqVertices(j) );
1514 for (j = jmin-1; j >= 1; j--)
1516 TopoDS_Shape aLocalShape = SeqEdges(j).Reversed();
1517 BB.Add( newwire, TopoDS::Edge(aLocalShape) );
1518 //PrevSeq.Append( SeqVertices(j) );
1520 for (j = theLength; j >= jmin; j--)
1522 TopoDS_Shape aLocalShape = SeqEdges(j).Reversed();
1523 BB.Add( newwire, TopoDS::Edge(aLocalShape) );
1524 //PrevSeq.Append( SeqVertices(j) );
1526 for (j = jmin; j >= 1; j--)
1527 PrevSeq.Append( SeqVertices(j) );
1528 for (j = theLength; j > jmin; j--)
1529 PrevSeq.Append( SeqVertices(j) );
1532 newwire.Closed( Standard_True );
1533 newwire.Orientation( TopAbs_FORWARD );
1534 myWork(i) = newwire;
1538 for ( i=ideb; i<=myWork.Length(); i++) {
1540 const TopoDS_Wire& wire = TopoDS::Wire(myWork(i));
1542 Standard_Integer nbEdges=0;
1543 for(anExp.Init(TopoDS::Wire(myWork(i))); anExp.More(); anExp.Next())
1545 TopExp::Vertices(wire,Vdeb,Vfin);
1546 Standard_Boolean wClosed = wire.Closed();
1548 // on regarde quand meme si les vertex sont les memes.
1549 if ( Vdeb.IsSame(Vfin)) wClosed = Standard_True;
1553 TopoDS_Vertex Vsuiv, VF, VR;
1554 TopoDS_Wire newwire;
1556 BW.MakeWire(newwire);
1559 const TopoDS_Edge Ecur = TopoDS::Edge(anExp.Current());
1560 TopExp::Vertices(Ecur,VF,VR);
1561 if (Vdeb.IsSame(VF)) Vsuiv=VR;
1562 else if (Vdeb.IsSame(VR)) Vsuiv=VF;
1564 // par defaut on prend l'origine sur cette arete
1565 if (VR.IsSame(TopoDS::Vertex(anExp.CurrentVertex()))) {
1574 Pdeb=BRep_Tool::Pnt(Vdeb);
1575 Psuiv=BRep_Tool::Pnt(Vsuiv);
1576 Standard_Real U1 = BRep_Tool::Parameter(Vdeb,Ecur);
1577 Standard_Real U2 = BRep_Tool::Parameter(Vsuiv,Ecur);
1578 BRepAdaptor_Curve Curve(Ecur);
1579 PPs = Curve.Value(0.25*(U1+3*U2));
1580 myWork(ideb) = wire;
1583 // on ramene Pdeb, Psuiv et PPs dans le plan courant
1584 gp_Pnt Pnew,Pnext,PPn;
1585 Transform(Standard_True,Pdeb,Pos->Value(i-1),Axe->Value(i-1),
1586 Pos->Value(i),Axe->Value(i),Pnew);
1587 Transform(Standard_True,Psuiv,Pos->Value(i-1),Axe->Value(i-1),
1588 Pos->Value(i),Axe->Value(i),Pnext);
1589 Transform(Standard_True,PPs,Pos->Value(i-1),Axe->Value(i-1),
1590 Pos->Value(i),Axe->Value(i),PPn);
1592 Standard_Real distmini,dist;
1593 Standard_Integer rang=0,rangdeb=0;
1594 TopoDS_Vertex Vmini;
1596 SeqOfVertices(wire,SeqV);
1597 if (SeqV.Length()>NbMaxV)
1598 Standard_NoSuchObject::Raise("BRepFill::ComputeOrigin failed");
1600 // choix du vertex le plus proche comme origine
1601 distmini = Precision::Infinite();
1602 for (Standard_Integer ii=1;ii<=SeqV.Length();ii++) {
1603 P1 = BRep_Tool::Pnt(TopoDS::Vertex(SeqV.Value(ii)));
1604 dist = P1.Distance(Pnew);
1605 if (dist<distmini) {
1607 Vmini = TopoDS::Vertex(SeqV.Value(ii));
1610 if (!Vmini.IsNull()) Pmini = BRep_Tool::Pnt(Vmini);
1614 // recherche du vertex correspondant a la projection conique
1615 Standard_Real angmin, angV, eta = Precision::Angular();
1616 TopoDS_Vertex Vopti;
1618 distmini = Precision::Infinite();
1619 gp_Dir dir0(gp_Vec(Pnew,P.Location()));
1620 for (Standard_Integer ii=1;ii<=SeqV.Length();ii++) {
1621 P1 = BRep_Tool::Pnt(TopoDS::Vertex(SeqV.Value(ii)));
1622 dist = Pnew.Distance(P1);
1623 if (dist<Precision::Confusion()) {
1627 gp_Dir dir1(gp_Vec(Pnew,P1));
1628 angV = dir1.Angle(dir0);
1630 if (angV>PI/2) angV = PI - angV;
1631 if (angmin>angV+eta) {
1634 Vopti = TopoDS::Vertex(SeqV.Value(ii));
1636 else if (Abs(angmin-angV)<eta) {
1637 if (dist<distmini) {
1640 Vopti = TopoDS::Vertex(SeqV.Value(ii));
1645 if (!Vopti.IsNull()) Popti = BRep_Tool::Pnt(Vopti);
1650 distmini = Precision::Infinite();
1651 for (anExp.Init(wire); anExp.More(); anExp.Next()) {
1652 TopoDS_Edge Ecur = anExp.Current();
1653 TopoDS_Vertex Vcur = anExp.CurrentVertex();
1654 TopExp::Vertices(Ecur,VF,VR);
1655 if (VF.IsSame(Vmini)) {
1656 P1 = BRep_Tool::Pnt(VR);
1657 dist = P1.Distance(Pnext);
1658 if (dist<=distmini) {
1663 if (VR.IsSame(Vmini)) {
1664 P1 = BRep_Tool::Pnt(VF);
1665 dist = P1.Distance(Pnext);
1666 if (dist<distmini) {
1673 // choix du sens de parcours en fonction de Pnext
1674 Standard_Boolean parcours = Standard_False;
1675 if (i==myWork.Length() && myDegen2) {
1676 // derniere section ponctuelle
1678 parcours = Standard_True;
1682 gp_Pnt Pbout = Pnext;
1684 TopoDS_Vertex V1,V2;
1685 EdgesFromVertex(wire,Vmini,E1,E2);
1687 TopExp::Vertices(E1,V1,V2,Standard_True);
1689 Standard_Real U1=0, U2=0;
1691 Standard_Real U1, U2;
1693 if (Vmini.IsSame(V1)) {
1694 P1 = BRep_Tool::Pnt(V2);
1695 U1 = 0.25*(BRep_Tool::Parameter(V1,E1)+3*BRep_Tool::Parameter(V2,E1));
1697 if (Vmini.IsSame(V2)) {
1698 P1 = BRep_Tool::Pnt(V1);
1699 U1 = 0.25*(3*BRep_Tool::Parameter(V1,E1)+BRep_Tool::Parameter(V2,E1));
1702 TopExp::Vertices(E2,V1,V2,Standard_True);
1703 if (Vmini.IsSame(V1)) {
1704 P2 = BRep_Tool::Pnt(V2);
1705 U2 = 0.25*(BRep_Tool::Parameter(V1,E2)+3*BRep_Tool::Parameter(V2,E2));
1707 if (Vmini.IsSame(V2)) {
1708 P2 = BRep_Tool::Pnt(V1);
1709 U2 = 0.25*(3*BRep_Tool::Parameter(V1,E2)+BRep_Tool::Parameter(V2,E2));
1712 if (Abs(Pbout.Distance(P1)-Pbout.Distance(P2))<Precision::Confusion()) {
1713 // cas limite ; on se decale un peu
1715 BRepAdaptor_Curve Curve1(E1);
1716 P1 = Curve1.Value(U1);
1717 BRepAdaptor_Curve Curve2(E2);
1718 P2 = Curve2.Value(U2);
1721 // calcul de rangdeb
1723 if (Pbout.Distance(P1)<Pbout.Distance(P2)){
1724 // P1 est plus proche; parcours = False
1725 parcours = Standard_False;
1727 for (anExp.Init(wire); anExp.More(); anExp.Next()) {
1729 TopoDS_Edge Ecur = anExp.Current();
1730 if (E1.IsSame(Ecur)) {
1734 BRepAdaptor_Curve Curve(E1);
1735 PPs = Curve.Value(U1);
1738 // P2 est plus proche; parcours = True
1739 parcours = Standard_True;
1741 for (anExp.Init(wire); anExp.More(); anExp.Next()) {
1743 TopoDS_Edge Ecur = anExp.Current();
1744 if (E2.IsSame(Ecur)) {
1748 BRepAdaptor_Curve Curve(E2);
1749 PPs = Curve.Value(U2);
1753 // reconstruction du wire a partir de rangdeb
1754 TopTools_SequenceOfShape SeqEdges;
1756 for (anExp.Init(TopoDS::Wire(wire)); anExp.More(); anExp.Next()) {
1757 SeqEdges.Append(anExp.Current());
1760 for (rang=rangdeb;rang<=nbEdges;rang++) {
1761 BW.Add(newwire,TopoDS::Edge(SeqEdges.Value(rang)));
1763 for (rang=1;rang<rangdeb;rang++) {
1764 BW.Add(newwire,TopoDS::Edge(SeqEdges.Value(rang)));
1768 for (rang=rangdeb;rang>=1;rang--) {
1769 TopoDS_Shape aLocalShape = SeqEdges.Value(rang).Reversed();
1770 BW.Add(newwire,TopoDS::Edge(aLocalShape));
1771 // BW.Add(newwire,TopoDS::Edge(SeqEdges.Value(rang).Reversed()));
1773 for (rang=nbEdges;rang>rangdeb;rang--) {
1774 TopoDS_Shape aLocalShape = SeqEdges.Value(rang).Reversed();
1775 BW.Add(newwire,TopoDS::Edge(aLocalShape));
1776 // BW.Add(newwire,TopoDS::Edge(SeqEdges.Value(rang).Reversed()));
1780 myWork(i) = newwire.Oriented(TopAbs_FORWARD);
1782 // on passe au wire suivant
1783 if (!Vmini.IsNull()) Pdeb=BRep_Tool::Pnt(Vmini);
1784 if (!Vsuiv.IsNull()) Psuiv=BRep_Tool::Pnt(Vsuiv);
1789 // sections bouclantes ?
1790 if (vClosed) myWork(myWork.Length()) = myWork(1);
1793 //=======================================================================
1794 //function : SearchOrigin
1796 //=======================================================================
1798 void BRepFill_CompatibleWires::SearchOrigin()
1800 // reorganize the open wires respecting orientation and origin
1804 TopoDS_Vertex Vdeb, Vfin;
1805 gp_Pnt Pdeb, Pfin;//,Psuiv;
1807 BRepTools_WireExplorer anExp;
1809 Standard_Boolean allOpen = Standard_True;
1810 Standard_Integer ideb=1, ifin=myWork.Length();
1811 if (myDegen1) ideb++;
1812 if (myDegen2) ifin--;
1813 Standard_Boolean vClosed = (!myDegen1) && (!myDegen2)
1814 && (myWork(ideb).IsSame(myWork(ifin)));
1816 // for (Standard_Integer i=ideb; i<=ifin; i++) {
1818 for (i=ideb; i<=ifin; i++) {
1819 allOpen = (allOpen && !myWork(i).Closed());
1822 Standard_NoSuchObject::Raise("BRepFill_CompatibleWires::SearchOrigin : the wires must be open");
1826 TopoDS_Wire wire1 = TopoDS::Wire(myWork(ideb));
1827 wire1.Orientation(TopAbs_FORWARD);
1828 TopExp::Vertices(wire1,Vdeb,Vfin);
1829 Pdeb = BRep_Tool::Pnt(Vdeb);
1830 Pfin = BRep_Tool::Pnt(Vfin);
1831 Standard_Boolean isline0 = (!PlaneOfWire(wire1,P0)), isline;
1832 myWork(ideb) = wire1;
1835 TopoDS_Edge E0 = anExp.Current(), E;
1837 for ( i=ideb+1; i<=ifin; i++) {
1839 TopoDS_Wire wire = TopoDS::Wire(myWork(i));
1840 wire.Orientation(TopAbs_FORWARD);
1842 TopTools_SequenceOfShape SeqEdges;
1844 Standard_Integer nbEdges=0;
1845 //OCC86 for(anExp.Init(wire); anExp.More(); anExp.Next()) {
1846 for(anExp.Init(wire), E = anExp.Current(); anExp.More(); anExp.Next()) {
1847 SeqEdges.Append(anExp.Current());
1850 TopExp::Vertices(wire,Vdeb,Vfin);
1851 isline = (!PlaneOfWire(wire,P));
1853 TopoDS_Vertex Vmini;
1854 TopoDS_Wire newwire;
1856 BW.MakeWire(newwire);
1857 Standard_Boolean parcours = Standard_True;
1859 if (isline0 || isline) {
1861 // cas particulier des segments de droite
1862 gp_Pnt P1 = BRep_Tool::Pnt(Vdeb),
1863 P2 = BRep_Tool::Pnt(Vfin);
1864 Standard_Real dist1, dist2;
1865 dist1 = Pdeb.Distance(P1)+Pfin.Distance(P2);
1866 dist2 = Pdeb.Distance(P2)+Pfin.Distance(P1);
1867 parcours = (dist2>=dist1);
1872 gp_Pnt P1 = BRep_Tool::Pnt(Vdeb), P1o = Pdeb,
1873 P2 = BRep_Tool::Pnt(Vfin), P2o = Pfin;
1874 /* // on ramene Pdeb dans le plan courant
1875 gp_Pnt Pnew = Pdeb.Translated (P0.Location(),P.Location());
1876 gp_Ax1 A0 = P0.Axis();
1877 gp_Ax1 A1 = P.Axis();
1879 if (!A0.IsParallel(A1,1.e-4)) {
1880 gp_Vec vec1(A0.Direction()), vec2(A1.Direction()),
1882 gp_Ax1 Norm(P.Location(),norm);
1883 Standard_Real ang = vec1.AngleWithRef(vec2,norm);
1888 if (Abs(ang-PI/2.0)<Precision::Angular()) {
1890 gp_Vec Vtrans(P0.Location(),P.Location()),Vsign;
1891 Standard_Real alpha,beta,sign=1;
1892 Vsign.SetLinearForm(Vtrans.Dot(vec1),vec2,-Vtrans.Dot(vec2),vec1);
1893 alpha = Vsign.Dot(vec1);
1894 beta = Vsign.Dot(vec2);
1895 Standard_Boolean pasnul = (Abs(alpha)>1.e-4 && Abs(beta)>1.e-4);
1896 if ( alpha*beta>0.0 && pasnul ) sign=-1;
1899 Pnew = Pnew.Rotated (Norm,ang);
1901 // choix entre Vdeb et Vfin
1902 Standard_Real dist = Pnew.Distance(P1);
1903 parcours = (dist<Pnew.Distance(P2));
1905 if(P1.IsEqual(P2,Precision::Confusion()) || P1o.IsEqual(P2o,Precision::Confusion())){
1906 BRepAdaptor_Curve Curve0(E0), Curve(E);
1907 Curve0.D0(Curve0.FirstParameter() + Precision::Confusion(), P2o);
1908 Curve.D0(Curve.FirstParameter() + Precision::Confusion(), P2);
1910 gp_Vec VDebFin0(P1o,P2o), VDebFin(P1,P2);
1911 Standard_Real AStraight = VDebFin0.Angle(VDebFin);
1912 parcours = (AStraight < PI/2.0? Standard_True: Standard_False);
1915 // reconstruction du wire
1916 Standard_Integer rang;
1918 for (rang=1;rang<=nbEdges;rang++) {
1919 TopoDS_Shape alocalshape = SeqEdges.Value(rang);
1920 BW.Add(newwire,TopoDS::Edge(alocalshape));
1921 // BW.Add(newwire,TopoDS::Edge(SeqEdges.Value(rang)));
1925 for (rang=nbEdges;rang>=1;rang--) {
1926 TopoDS_Shape alocalshape = SeqEdges.Value(rang).Reversed();
1927 BW.Add(newwire,TopoDS::Edge(alocalshape));
1928 // BW.Add(newwire,TopoDS::Edge(SeqEdges.Value(rang).Reversed()));
1932 // orientation du wire
1933 newwire.Oriented(TopAbs_FORWARD);
1934 myWork(i) = newwire;
1936 // on passe au wire suivant
1938 Pdeb = BRep_Tool::Pnt(Vdeb);
1939 Pfin = BRep_Tool::Pnt(Vfin);
1942 Pfin = BRep_Tool::Pnt(Vdeb);
1943 Pdeb = BRep_Tool::Pnt(Vfin);
1951 // sections bouclantes ?
1952 if (vClosed) myWork(myWork.Length()) = myWork(1);