1 // Created on: 1993-08-10
2 // Created by: Christophe MARION
3 // Copyright (c) 1993-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.
18 #include <AppDef_BSplineCompute.hxx>
19 #include <AppDef_MultiLine.hxx>
20 #include <AppDef_MultiPointConstraint.hxx>
21 #include <AppParCurves_MultiBSpCurve.hxx>
22 #include <BRep_Builder.hxx>
23 #include <BRep_CurveRepresentation.hxx>
24 #include <BRep_ListIteratorOfListOfCurveRepresentation.hxx>
25 #include <BRep_TEdge.hxx>
26 #include <BRep_Tool.hxx>
27 #include <BRepAdaptor_Curve.hxx>
28 #include <BRepAdaptor_Curve2d.hxx>
29 #include <BRepAdaptor_HCurve2d.hxx>
30 #include <BRepApprox_Approx.hxx>
31 #include <BRepApprox_ApproxLine.hxx>
32 #include <BRepExtrema_ExtPC.hxx>
33 #include <BRepTopAdaptor_HVertex.hxx>
34 #include <BRepTopAdaptor_Tool.hxx>
35 #include <BRepTopAdaptor_TopolTool.hxx>
36 #include <Contap_Contour.hxx>
37 #include <Contap_Line.hxx>
38 #include <Contap_Point.hxx>
39 #include <Extrema_LocateExtPC.hxx>
40 #include <Geom2d_BSplineCurve.hxx>
41 #include <Geom2d_Curve.hxx>
42 #include <Geom_BSplineCurve.hxx>
43 #include <Geom_Circle.hxx>
44 #include <Geom_Line.hxx>
45 #include <Geom_Surface.hxx>
46 #include <GeomProjLib.hxx>
47 #include <HLRTopoBRep_Data.hxx>
48 #include <HLRTopoBRep_DSFiller.hxx>
49 #include <HLRTopoBRep_FaceIsoLiner.hxx>
50 #include <math_Vector.hxx>
51 #include <Precision.hxx>
52 #include <Standard_ProgramError.hxx>
53 #include <TColgp_Array1OfPnt.hxx>
54 #include <TColgp_Array1OfPnt2d.hxx>
55 #include <TColStd_Array1OfInteger.hxx>
56 #include <TColStd_Array1OfReal.hxx>
58 #include <TopExp_Explorer.hxx>
60 #include <TopoDS_Edge.hxx>
61 #include <TopoDS_Face.hxx>
62 #include <TopoDS_Shape.hxx>
63 #include <TopoDS_Vertex.hxx>
64 #include <TopTools_ListIteratorOfListOfShape.hxx>
71 static Standard_Boolean IntLineRisesFromRegularity(const TopoDS_Edge& anIntLine,
72 const TopoDS_Edge& anEdge,
74 const TopTools_ListOfShape& aList)
76 TopoDS_Vertex Ver [2];
77 TopExp::Vertices(anIntLine, Ver[0], Ver[1]);
79 //find min param and max param
80 Standard_Real MinPar = RealLast(), MaxPar = RealFirst();
81 TopTools_ListIteratorOfListOfShape itl(aList);
82 for (; itl.More(); itl.Next())
84 const TopoDS_Edge& anOutLine = TopoDS::Edge(itl.Value());
85 Standard_Real aFirst, aLast;
86 BRep_Tool::Range(anOutLine, aFirst, aLast);
93 Standard_Real theTol = BRep_Tool::Tolerance(anEdge);
94 Standard_Real ParamTol = Precision::Confusion();
96 Standard_Integer i, j;
97 for (i = 0; i < 2; i++)
99 BRepExtrema_ExtPC anExtPC(Ver[i], anEdge);
100 if (!anExtPC.IsDone())
102 Standard_Integer NbExt = anExtPC.NbExt();
105 Standard_Integer jmin = 1;
106 for (j = 2; j <= NbExt; j++)
107 if (anExtPC.SquareDistance(j) < anExtPC.SquareDistance(jmin))
109 Standard_Real aDist = anExtPC.SquareDistance(jmin);
114 Standard_Real theParam = anExtPC.Parameter(jmin);
115 if (theParam > MinPar + ParamTol &&
116 theParam < MaxPar - ParamTol)
117 return Standard_True;
120 return Standard_False;
124 //=======================================================================
126 //purpose : explore the faces and insert them
127 //=======================================================================
129 void HLRTopoBRep_DSFiller::Insert (const TopoDS_Shape& S,
131 HLRTopoBRep_Data& DS,
132 BRepTopAdaptor_MapOfShapeTool& MST,
133 const Standard_Integer nbIso)
135 TopTools_MapOfShape ShapeMap;
136 TopExp_Explorer ex(S,TopAbs_FACE);
138 Standard_Boolean withPCurve = Standard_True; // instead of nbIso != 0;
139 Standard_Integer f = 0;
142 if (ShapeMap.Add(ex.Current())) {
144 TopoDS_Face S1 = TopoDS::Face(ex.Current());
145 S1.Orientation(TopAbs_FORWARD);
146 Handle(BRepTopAdaptor_TopolTool) Domain;
147 Handle(Adaptor3d_HSurface) Surface;
148 if(MST.IsBound(S1)) {
149 BRepTopAdaptor_Tool& BRT = MST.ChangeFind(S1);
150 Domain = BRT.GetTopolTool();
151 Surface = BRT.GetSurface();
154 BRepTopAdaptor_Tool BRT(S1,Precision::PConfusion());
156 Domain = BRT.GetTopolTool();
157 Surface = BRT.GetSurface();
159 FO.Perform(Surface, Domain);
162 InsertFace(f,S1,FO,DS,withPCurve);
164 if (nbIso != 0) HLRTopoBRep_FaceIsoLiner::Perform(f,S1,DS,nbIso);
171 //=======================================================================
172 //function : InsertFace
173 //purpose : private, insert the outlines of a face
174 //=======================================================================
176 void HLRTopoBRep_DSFiller::InsertFace (const Standard_Integer /*FI*/,
177 const TopoDS_Face& F,
179 HLRTopoBRep_Data& DS,
180 const Standard_Boolean withPCurve)
182 // Insert the intersections of FO in DS
184 const Standard_Real tol = BRep_Tool::Tolerance(F);
185 TopTools_ListOfShape& IntL = DS.AddIntL(F);
186 TopTools_ListOfShape& OutL = DS.AddOutL(F);
190 TopTools_MapOfShape VM;
191 TopExp_Explorer ex(F,TopAbs_EDGE);
193 const TopoDS_Edge& E = TopoDS::Edge(ex.Current());
194 if (BRep_Tool::IsClosed(E,F)) {
195 TopExp::Vertices(E,VF,VL);
203 const Standard_Integer NbLines = FO.NbLines();
204 Standard_Integer CurLine;
205 for (CurLine = 1; CurLine <= NbLines; CurLine++)
207 const Contap_Line& Line = FO.Line(CurLine);
208 const Standard_Integer NbPoints = Line.NbVertex();
209 Standard_Integer CurPoint;
210 if (Line.TypeContour() == Contap_Restriction)
212 // OutLine on restriction
213 TopoDS_Edge E = (*(BRepAdaptor_Curve2d*)&(Line.Arc()->Curve2d())).Edge();
215 TopExp::Vertices(E,VF,VL);
216 // insert the Internal points.
218 for (CurPoint = 1; CurPoint <= NbPoints; CurPoint++) {
219 Contap_Point P = Line.Vertex(CurPoint);
220 if (P.IsInternal()) {
221 if (P.Value().IsEqual(BRep_Tool::Pnt(VF),BRep_Tool::Tolerance(VF))) {
222 if (P.Value().IsEqual(BRep_Tool::Pnt(VL),BRep_Tool::Tolerance(VL))) {
223 InsertVertex(P,tol,E,DS);
231 for (CurPoint = 1; CurPoint <= NbPoints; CurPoint++) {
233 const Contap_Point PF = Line.Vertex(CurPoint);
234 if (PF.IsInternal() && CurPoint != 1)
237 VF = MakeVertex(PF,tol,DS);
238 const Standard_Real parF = PF.ParameterOnLine();
240 if (CurPoint < NbPoints) {
241 const Contap_Point PL = Line.Vertex(CurPoint+1);
242 VL = MakeVertex(PL,tol,DS);
243 const Standard_Real parL = PL.ParameterOnLine();
245 if( (parL-parF) > Precision::PConfusion() ) {
247 Handle(Geom_Curve) C;
248 Handle(Geom2d_Curve) C2d;
249 Standard_Real first = parF;
250 Standard_Real last = parL;
251 Standard_Boolean InsuffisantNumberOfPoints=Standard_False;
253 switch (Line.TypeContour()) {
257 C = new Geom_Line(Line.Line());
259 Handle(Geom_Surface) S = BRep_Tool::Surface(F);
260 Standard_Real Tol = 1e-7;
261 C2d = GeomProjLib::Curve2d(C,first,last,S,Tol);
268 C = new Geom_Circle(Line.Circle());
271 Handle(Geom_Surface) S = BRep_Tool::Surface(F,Loc);
272 if (!Loc.IsIdentity()) {
273 S = Handle(Geom_Surface)::DownCast(S->Transformed(Loc.Transformation()));
275 Standard_Real Tol = 1e-7;
276 C2d = GeomProjLib::Curve2d(C,first,last,S,Tol);
281 case Contap_Walking :
284 Standard_Integer ipF = Standard_Integer(parF);
285 Standard_Integer ipL = Standard_Integer(parL);
288 InsuffisantNumberOfPoints=Standard_True;
289 //cout<<"\n !! Pb ds HLRTopoBRep_DSFiller.cxx (Contour App Nbp <3)"<<endl;
292 else if(ipL-ipF < 6) {
293 // compute the tangents
294 Contap_SurfFunction& SFunc =
295 FO.SurfaceFunction();
297 Standard_Boolean isTg1,isTg2;
300 math_Vector UV(1,2),F(1,1);
302 Line.Point(ipF).ParametersOnS2(UV(1),UV(2));
304 isTg1 = SFunc.IsTangent();
306 tg1 = SFunc.Direction3d();
307 if (withPCurve) uv1 = SFunc.Direction2d();
310 Line.Point(ipL).ParametersOnS2(UV(1),UV(2));
312 isTg2 = SFunc.IsTangent();
314 tg2 = SFunc.Direction3d();
315 if (withPCurve) uv2 = SFunc.Direction2d();
318 Standard_Integer nbp = ipL - ipF + 1;
319 AppDef_MultiLine MLine(nbp);
320 Standard_Integer nb2d = 0;
321 if (withPCurve) nb2d = 1;
323 for (Standard_Integer i = 1; i <= nbp; i++) {
324 AppDef_MultiPointConstraint MP(1, nb2d);
325 MP.SetPoint(1,Line.Point(i + ipF - 1).Value());
327 Line.Point(i + ipF - 1).ParametersOnS2(UV(1),UV(2));
328 MP.SetPoint2d(2,gp_Pnt2d(UV(1),UV(2)));
331 if (i == 1 && !isTg1) {
333 if (withPCurve) MP.SetTang2d(2,uv1);
335 if (i == nbp && !isTg2) {
337 if (withPCurve) MP.SetTang2d(2,uv2);
339 MLine.SetValue(i,MP);
341 AppDef_BSplineCompute interp;
342 interp.Interpol(MLine);
343 AppParCurves_MultiBSpCurve TheCurve = interp.Value();
344 Standard_Integer Degree = TheCurve.Degree();
345 TColgp_Array1OfPnt Poles(1,TheCurve.NbPoles());
346 TheCurve.Curve(1,Poles);
347 C = new Geom_BSplineCurve(Poles,
349 TheCurve.Multiplicities(),
352 TColgp_Array1OfPnt2d Pol2d(1,TheCurve.NbPoles());
353 TheCurve.Curve(2,Pol2d);
354 C2d = new Geom2d_BSplineCurve(Pol2d,
356 TheCurve.Multiplicities(),
363 else if(ipL-ipF < 5) {
364 const Standard_Integer nbp = ipL - ipF + 1;
365 TColStd_Array1OfReal knots(1,nbp);
366 TColStd_Array1OfInteger mults(1,nbp);
367 TColgp_Array1OfPnt Points(1,nbp);
369 for(Standard_Integer i=1;i<=nbp;i++) {
370 knots.SetValue(i,(Standard_Real)i);
372 Points.SetValue(i,Line.Point(i+ipF-1).Value());
374 mults(1)=mults(nbp)=2;
375 C = new Geom_BSplineCurve(Points,knots,mults,1);
378 TColgp_Array1OfPnt2d Points2d(1,nbp);
379 for(Standard_Integer i=1;i<=nbp;i++) {
381 Line.Point(i+ipF-1).ParametersOnS2(u,v);
382 Points2d.SetValue(i,gp_Pnt2d(u,v));
384 C2d = new Geom2d_BSplineCurve(Points2d,knots,mults,1);
390 const Standard_Integer nbp = ipL - ipF + 1;
391 TColStd_Array1OfReal knots(1,nbp);
392 TColStd_Array1OfInteger mults(1,nbp);
393 TColgp_Array1OfPnt Points(1,nbp);
395 Standard_Real Maxx,Maxy,Maxz,Maxu,Maxv;
396 Standard_Real Minx,Miny,Minz,Minu,Minv;
397 Maxx=Maxy=Maxz=Maxu=Maxv=-RealLast();
398 Minx=Miny=Minz=Minu=Minv=RealLast();
400 for(Standard_Integer i=1;i<=nbp;i++) {
401 knots.SetValue(i,(Standard_Real)i);
403 const gp_Pnt& P= Line.Point(i+ipF-1).Value();
404 if(P.X()<Minx) Minx=P.X();
405 if(P.Y()<Miny) Miny=P.Y();
406 if(P.Z()<Minz) Minz=P.Z();
407 if(P.X()>Maxx) Maxx=P.X();
408 if(P.Y()>Maxy) Maxy=P.Y();
409 if(P.Z()>Maxz) Maxz=P.Z();
410 Points.SetValue(i,P);
412 mults(1)=mults(nbp)=2;
413 Handle(Geom_BSplineCurve) AppC;
414 Handle(Geom2d_BSplineCurve) AppC2d;
415 AppC = new Geom_BSplineCurve(Points,knots,mults,1);
418 TColgp_Array1OfPnt2d Points2d(1,nbp);
419 for(Standard_Integer i=1;i<=nbp;i++) {
421 Line.Point(i+ipF-1).ParametersOnS2(u,v);
426 Points2d.SetValue(i,gp_Pnt2d(u,v));
428 AppC2d = new Geom2d_BSplineCurve(Points2d,knots,mults,1);
433 Handle(BRepApprox_ApproxLine) AppLine;
434 Handle(Geom2d_BSplineCurve) CNull;
435 AppLine = new BRepApprox_ApproxLine(AppC,AppC2d,CNull);
437 Standard_Integer dmin=4,dmax=8,niter=0;
438 Standard_Boolean tg= Standard_False;
439 BRepApprox_Approx Approx;
440 Standard_Real TOL3d,TOL2d,TOL=0.0001;
442 Maxx-=Minx; Maxy-=Miny; Maxz-=Minz;
443 Maxu-=Minu; Maxv-=Minv;
444 if(Maxy>Maxx) Maxx=Maxy;
445 if(Maxz>Maxx) Maxx=Maxy;
446 if(Maxv>Maxu) Maxu=Maxv;
448 TOL3d=TOL*Maxx; if(TOL3d<1e-12) TOL3d=1e-12; else if(TOL3d>0.1) TOL3d=0.1;
449 TOL2d=TOL*Maxu; if(TOL2d<1e-12) TOL2d=1e-12; else if(TOL2d>0.1) TOL2d=0.1;
451 //-- cout<<"\nHLRTopoBRep_DSFiller : nbp="<<nbp<<" Tol3d="<<TOL3d<<" Tol2d="<<TOL2d<<endl;
453 Approx.SetParameters(TOL3d,TOL2d,dmin,dmax,niter,tg);
454 Approx.Perform(AppLine,Standard_True,Standard_True,Standard_False,1,nbp);
455 if (!Approx.IsDone()) {
462 const AppParCurves_MultiBSpCurve& AppVal = Approx.Value(1);
463 TColgp_Array1OfPnt poles3d(1,AppVal.NbPoles());
464 AppVal.Curve(1,poles3d);
465 C = new Geom_BSplineCurve(poles3d,AppVal.Knots(),AppVal.Multiplicities(),AppVal.Degree());
467 const AppParCurves_MultiBSpCurve& AppVal2 = Approx.Value(2);
468 TColgp_Array1OfPnt2d poles2d(1,AppVal2.NbPoles());
469 AppVal2.Curve(2,poles2d);
470 C2d = new Geom2d_BSplineCurve(poles2d,AppVal2.Knots(),AppVal2.Multiplicities(),AppVal2.Degree());
471 first = C2d->FirstParameter();
472 last = C2d->LastParameter();
478 case Contap_Restriction :
480 Standard_ProgramError::Raise("HLRTopoBRep_DSFiller::InsertFace : Restriction");
485 // compute the PCurve
487 if (!InsuffisantNumberOfPoints) {
491 VF.Orientation(TopAbs_FORWARD);
492 VL.Orientation(TopAbs_REVERSED);
495 B.Range(E,first,last);
498 B.UpdateEdge(E,C2d,F,BRep_Tool::Tolerance(F));
501 // add the edge in the DS
511 //jgv: correction of internal outlines: remove those that rise from middle of boundary outlines
512 TopTools_ListIteratorOfListOfShape itl(IntL);
515 TopoDS_Edge anIntLine = TopoDS::Edge(itl.Value());
516 Standard_Real found = Standard_False;
517 TopExp_Explorer Explo(F, TopAbs_EDGE);
518 for (; Explo.More(); Explo.Next())
520 TopoDS_Edge anEdge = TopoDS::Edge(Explo.Current());
521 if (!BRep_Tool::HasContinuity(anEdge))
524 TopLoc_Location RegLoc;
525 Standard_Real fpar, lpar;
526 Handle(Geom_Curve) RegCurve = BRep_Tool::Curve(anEdge, RegLoc, fpar, lpar);
527 TopTools_ListOfShape thelist;
528 TopTools_ListIteratorOfListOfShape itoutl(OutL);
529 for (; itoutl.More(); itoutl.Next())
531 TopoDS_Edge anOutLine = TopoDS::Edge(itoutl.Value());
532 TopLoc_Location aLoc;
533 Standard_Real aFirst, aLast;
534 Handle(Geom_Curve) aCurve = BRep_Tool::Curve(anOutLine, aLoc, aFirst, aLast);
535 if (aCurve == RegCurve && aLoc == RegLoc)
536 thelist.Append(anOutLine);
539 if (thelist.IsEmpty())
542 if (IntLineRisesFromRegularity(anIntLine, anEdge, F, thelist))
545 found = Standard_True;
553 ///////////////////////////////////////////////////
556 //=======================================================================
557 //function : MakeVertex
558 //purpose : private, make a vertex from an intersection point
559 //=======================================================================
562 HLRTopoBRep_DSFiller::MakeVertex (const Contap_Point& P,
563 const Standard_Real tol,
564 HLRTopoBRep_Data& DS)
569 V = Handle(BRepTopAdaptor_HVertex)::DownCast(P.Vertex())->Vertex();
573 // if on arc, insert in the DS
575 const TopoDS_Edge& E =
576 (*(BRepAdaptor_Curve2d*)&((P.Arc())->Curve2d())).Edge();
577 Standard_Real Par = P.ParameterOnArc();
578 const gp_Pnt& P3d = P.Value();
580 for (DS.InitVertex(E); DS.MoreVertex(); DS.NextVertex()) {
581 TopoDS_Vertex curV = DS.Vertex();
582 Standard_Real curP = DS.Parameter();
583 const gp_Pnt& PPP=BRep_Tool::Pnt(curV);
584 Standard_Real TTT=BRep_Tool::Tolerance(curV);
585 if (P3d.IsEqual(PPP,TTT)) {
589 else if (Par < curP) {
590 B.MakeVertex(V,P.Value(),tol);
591 DS.InsertBefore(V,Par);
595 if (!DS.MoreVertex()) {
596 B.MakeVertex(V,P.Value(),tol);
601 // if internal create a vertex and insert in the DS
603 B.MakeVertex(V,P.Value(),tol);
613 //=======================================================================
614 //function : InsertVertex
615 //purpose : private, insert a vertex from an internal intersection point
617 //=======================================================================
620 HLRTopoBRep_DSFiller::InsertVertex (const Contap_Point& P,
621 const Standard_Real tol,
622 const TopoDS_Edge& E,
623 HLRTopoBRep_Data& DS)
629 V = Handle(BRepTopAdaptor_HVertex)::DownCast(P.Vertex())->Vertex();
632 Standard_Real Par = P.ParameterOnLine();
634 for (DS.InitVertex(E); DS.MoreVertex(); DS.NextVertex()) {
635 TopoDS_Vertex curV = DS.Vertex();
636 Standard_Real curP = DS.Parameter();
637 if (P.Value().IsEqual(BRep_Tool::Pnt(curV),
638 BRep_Tool::Tolerance(curV))) {
642 else if (Par < curP) {
643 B.MakeVertex(V,P.Value(),tol);
644 DS.InsertBefore(V,Par);
648 if (!DS.MoreVertex()) {
649 B.MakeVertex(V,P.Value(),tol);
656 //=======================================================================
657 //function : ProcessEdges
658 //purpose : private, split edges with outline vertices
659 //=======================================================================
661 void HLRTopoBRep_DSFiller::ProcessEdges (HLRTopoBRep_Data& DS)
665 TopoDS_Vertex VF,VL,VI;
666 Standard_Real PF,PL,PI;
668 for (DS.InitEdge(); DS.MoreEdge(); DS.NextEdge()) {
669 TopoDS_Edge E = DS.Edge();
670 TopTools_ListOfShape& SplE = DS.AddSplE(E);
671 VF = TopExp::FirstVertex(E);
672 VL = TopExp::LastVertex(E);
673 BRep_Tool::Range(E,PF,PL);
674 VF.Orientation(TopAbs_FORWARD);
675 VL.Orientation(TopAbs_REVERSED);
677 for (DS.InitVertex(E); DS.MoreVertex(); DS.NextVertex()) {
680 VI.Orientation(TopAbs_REVERSED);
683 newE.Orientation(TopAbs_FORWARD);
685 B.UpdateVertex(VF,PF,newE,BRep_Tool::Tolerance(VF));
687 B.UpdateVertex(VI,PI,newE,BRep_Tool::Tolerance(VI));
688 newE.Orientation(E.Orientation());
692 VF.Orientation(TopAbs_FORWARD);
696 newE.Orientation(TopAbs_FORWARD);
698 B.UpdateVertex(VF,PF,newE,BRep_Tool::Tolerance(VF));
700 B.UpdateVertex(VL,PL,newE,BRep_Tool::Tolerance(VL));
701 newE.Orientation(E.Orientation());