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>
66 //=======================================================================
68 //purpose : explore the faces and insert them
69 //=======================================================================
71 void HLRTopoBRep_DSFiller::Insert (const TopoDS_Shape& S,
74 BRepTopAdaptor_MapOfShapeTool& MST,
75 const Standard_Integer nbIso)
77 TopTools_MapOfShape ShapeMap;
78 TopExp_Explorer ex(S,TopAbs_FACE);
80 Standard_Boolean withPCurve = Standard_True; // instead of nbIso != 0;
81 Standard_Integer f = 0;
84 if (ShapeMap.Add(ex.Current())) {
86 TopoDS_Face S1 = TopoDS::Face(ex.Current());
87 S1.Orientation(TopAbs_FORWARD);
88 Handle(BRepTopAdaptor_TopolTool) Domain;
89 Handle(Adaptor3d_HSurface) Surface;
91 BRepTopAdaptor_Tool& BRT = MST.ChangeFind(S1);
92 Domain = BRT.GetTopolTool();
93 Surface = BRT.GetSurface();
96 BRepTopAdaptor_Tool BRT(S1,Precision::PConfusion());
98 Domain = BRT.GetTopolTool();
99 Surface = BRT.GetSurface();
101 FO.Perform(Surface, Domain);
104 InsertFace(f,S1,FO,DS,withPCurve);
106 if (nbIso != 0) HLRTopoBRep_FaceIsoLiner::Perform(f,S1,DS,nbIso);
113 //=======================================================================
114 //function : InsertFace
115 //purpose : private, insert the outlines of a face
116 //=======================================================================
118 void HLRTopoBRep_DSFiller::InsertFace (const Standard_Integer /*FI*/,
119 const TopoDS_Face& F,
121 HLRTopoBRep_Data& DS,
122 const Standard_Boolean withPCurve)
124 // Insert the intersections of FO in DS
126 const Standard_Real tol = BRep_Tool::Tolerance(F);
127 TopTools_ListOfShape& IntL = DS.AddIntL(F);
128 TopTools_ListOfShape& OutL = DS.AddOutL(F);
132 TopTools_MapOfShape VM;
133 TopExp_Explorer ex(F,TopAbs_EDGE);
135 const TopoDS_Edge& E = TopoDS::Edge(ex.Current());
136 if (BRep_Tool::IsClosed(E,F)) {
137 TopExp::Vertices(E,VF,VL);
145 const Standard_Integer NbLines = FO.NbLines();
146 Standard_Integer CurLine;
147 for (CurLine = 1; CurLine <= NbLines; CurLine++)
149 const Contap_Line& Line = FO.Line(CurLine);
150 const Standard_Integer NbPoints = Line.NbVertex();
151 Standard_Integer CurPoint;
152 if (Line.TypeContour() == Contap_Restriction)
154 // OutLine on restriction
155 TopoDS_Edge E = (*(BRepAdaptor_Curve2d*)&(Line.Arc()->Curve2d())).Edge();
157 TopExp::Vertices(E,VF,VL);
158 // insert the Internal points.
160 for (CurPoint = 1; CurPoint <= NbPoints; CurPoint++) {
161 Contap_Point P = Line.Vertex(CurPoint);
162 if (P.IsInternal()) {
163 if (P.Value().IsEqual(BRep_Tool::Pnt(VF),BRep_Tool::Tolerance(VF))) {
164 if (P.Value().IsEqual(BRep_Tool::Pnt(VL),BRep_Tool::Tolerance(VL))) {
165 InsertVertex(P,tol,E,DS);
173 for (CurPoint = 1; CurPoint <= NbPoints; CurPoint++) {
175 const Contap_Point PF = Line.Vertex(CurPoint);
176 if (PF.IsInternal() && CurPoint != 1)
179 VF = MakeVertex(PF,tol,DS);
180 const Standard_Real parF = PF.ParameterOnLine();
182 if (CurPoint < NbPoints) {
183 const Contap_Point PL = Line.Vertex(CurPoint+1);
184 VL = MakeVertex(PL,tol,DS);
185 const Standard_Real parL = PL.ParameterOnLine();
187 if( (parL-parF) > Precision::PConfusion() ) {
189 Handle(Geom_Curve) C;
190 Handle(Geom2d_Curve) C2d;
191 Standard_Real first = parF;
192 Standard_Real last = parL;
193 Standard_Boolean InsuffisantNumberOfPoints=Standard_False;
195 switch (Line.TypeContour()) {
199 C = new Geom_Line(Line.Line());
201 Handle(Geom_Surface) S = BRep_Tool::Surface(F);
202 Standard_Real Tol = 1e-7;
203 C2d = GeomProjLib::Curve2d(C,first,last,S,Tol);
210 C = new Geom_Circle(Line.Circle());
213 Handle(Geom_Surface) S = BRep_Tool::Surface(F,Loc);
214 if (!Loc.IsIdentity()) {
215 S = Handle(Geom_Surface)::DownCast(S->Transformed(Loc.Transformation()));
217 Standard_Real Tol = 1e-7;
218 C2d = GeomProjLib::Curve2d(C,first,last,S,Tol);
223 case Contap_Walking :
226 Standard_Integer ipF = Standard_Integer(parF);
227 Standard_Integer ipL = Standard_Integer(parL);
230 InsuffisantNumberOfPoints=Standard_True;
231 //std::cout<<"\n !! Pb ds HLRTopoBRep_DSFiller.cxx (Contour App Nbp <3)"<<std::endl;
234 else if(ipL-ipF < 6) {
235 // compute the tangents
236 Contap_SurfFunction& SFunc =
237 FO.SurfaceFunction();
239 Standard_Boolean isTg1,isTg2;
242 math_Vector UV(1,2),F(1,1);
244 Line.Point(ipF).ParametersOnS2(UV(1),UV(2));
246 isTg1 = SFunc.IsTangent();
248 tg1 = SFunc.Direction3d();
249 if (withPCurve) uv1 = SFunc.Direction2d();
252 Line.Point(ipL).ParametersOnS2(UV(1),UV(2));
254 isTg2 = SFunc.IsTangent();
256 tg2 = SFunc.Direction3d();
257 if (withPCurve) uv2 = SFunc.Direction2d();
260 Standard_Integer nbp = ipL - ipF + 1;
261 AppDef_MultiLine MLine(nbp);
262 Standard_Integer nb2d = 0;
263 if (withPCurve) nb2d = 1;
265 for (Standard_Integer i = 1; i <= nbp; i++) {
266 AppDef_MultiPointConstraint MP(1, nb2d);
267 MP.SetPoint(1,Line.Point(i + ipF - 1).Value());
269 Line.Point(i + ipF - 1).ParametersOnS2(UV(1),UV(2));
270 MP.SetPoint2d(2,gp_Pnt2d(UV(1),UV(2)));
273 if (i == 1 && !isTg1) {
275 if (withPCurve) MP.SetTang2d(2,uv1);
277 if (i == nbp && !isTg2) {
279 if (withPCurve) MP.SetTang2d(2,uv2);
281 MLine.SetValue(i,MP);
283 AppDef_BSplineCompute interp;
284 interp.Interpol(MLine);
285 AppParCurves_MultiBSpCurve TheCurve = interp.Value();
286 Standard_Integer Degree = TheCurve.Degree();
287 TColgp_Array1OfPnt Poles(1,TheCurve.NbPoles());
288 TheCurve.Curve(1,Poles);
289 C = new Geom_BSplineCurve(Poles,
291 TheCurve.Multiplicities(),
294 TColgp_Array1OfPnt2d Pol2d(1,TheCurve.NbPoles());
295 TheCurve.Curve(2,Pol2d);
296 C2d = new Geom2d_BSplineCurve(Pol2d,
298 TheCurve.Multiplicities(),
305 else if(ipL-ipF < 5) {
306 const Standard_Integer nbp = ipL - ipF + 1;
307 TColStd_Array1OfReal knots(1,nbp);
308 TColStd_Array1OfInteger mults(1,nbp);
309 TColgp_Array1OfPnt Points(1,nbp);
311 for(Standard_Integer i=1;i<=nbp;i++) {
312 knots.SetValue(i,(Standard_Real)i);
314 Points.SetValue(i,Line.Point(i+ipF-1).Value());
316 mults(1)=mults(nbp)=2;
317 C = new Geom_BSplineCurve(Points,knots,mults,1);
320 TColgp_Array1OfPnt2d Points2d(1,nbp);
321 for(Standard_Integer i=1;i<=nbp;i++) {
323 Line.Point(i+ipF-1).ParametersOnS2(u,v);
324 Points2d.SetValue(i,gp_Pnt2d(u,v));
326 C2d = new Geom2d_BSplineCurve(Points2d,knots,mults,1);
332 const Standard_Integer nbp = ipL - ipF + 1;
333 TColStd_Array1OfReal knots(1,nbp);
334 TColStd_Array1OfInteger mults(1,nbp);
335 TColgp_Array1OfPnt Points(1,nbp);
337 Standard_Real Maxx,Maxy,Maxz,Maxu,Maxv;
338 Standard_Real Minx,Miny,Minz,Minu,Minv;
339 Maxx=Maxy=Maxz=Maxu=Maxv=-RealLast();
340 Minx=Miny=Minz=Minu=Minv=RealLast();
342 for(Standard_Integer i=1;i<=nbp;i++) {
343 knots.SetValue(i,(Standard_Real)i);
345 const gp_Pnt& P= Line.Point(i+ipF-1).Value();
346 if(P.X()<Minx) Minx=P.X();
347 if(P.Y()<Miny) Miny=P.Y();
348 if(P.Z()<Minz) Minz=P.Z();
349 if(P.X()>Maxx) Maxx=P.X();
350 if(P.Y()>Maxy) Maxy=P.Y();
351 if(P.Z()>Maxz) Maxz=P.Z();
352 Points.SetValue(i,P);
354 mults(1)=mults(nbp)=2;
355 Handle(Geom_BSplineCurve) AppC;
356 Handle(Geom2d_BSplineCurve) AppC2d;
357 AppC = new Geom_BSplineCurve(Points,knots,mults,1);
360 TColgp_Array1OfPnt2d Points2d(1,nbp);
361 for(Standard_Integer i=1;i<=nbp;i++) {
363 Line.Point(i+ipF-1).ParametersOnS2(u,v);
368 Points2d.SetValue(i,gp_Pnt2d(u,v));
370 AppC2d = new Geom2d_BSplineCurve(Points2d,knots,mults,1);
375 Handle(BRepApprox_ApproxLine) AppLine;
376 Handle(Geom2d_BSplineCurve) CNull;
377 AppLine = new BRepApprox_ApproxLine(AppC,AppC2d,CNull);
379 Standard_Integer dmin=4,dmax=8,niter=0;
380 Standard_Boolean tg= Standard_False;
381 BRepApprox_Approx Approx;
382 Standard_Real TOL3d,TOL2d,TOL=0.0001;
384 Maxx-=Minx; Maxy-=Miny; Maxz-=Minz;
385 Maxu-=Minu; Maxv-=Minv;
386 if(Maxy>Maxx) Maxx=Maxy;
387 if(Maxz>Maxx) Maxx=Maxy;
388 if(Maxv>Maxu) Maxu=Maxv;
390 TOL3d=TOL*Maxx; if(TOL3d<1e-12) TOL3d=1e-12; else if(TOL3d>0.1) TOL3d=0.1;
391 TOL2d=TOL*Maxu; if(TOL2d<1e-12) TOL2d=1e-12; else if(TOL2d>0.1) TOL2d=0.1;
393 //-- std::cout<<"\nHLRTopoBRep_DSFiller : nbp="<<nbp<<" Tol3d="<<TOL3d<<" Tol2d="<<TOL2d<<std::endl;
395 Approx.SetParameters(TOL3d, TOL2d, dmin, dmax, niter, 30, tg);
396 Approx.Perform(AppLine,Standard_True,Standard_True,Standard_False,1,nbp);
397 if (!Approx.IsDone()) {
404 const AppParCurves_MultiBSpCurve& AppVal = Approx.Value(1);
405 TColgp_Array1OfPnt poles3d(1,AppVal.NbPoles());
406 AppVal.Curve(1,poles3d);
407 C = new Geom_BSplineCurve(poles3d,AppVal.Knots(),AppVal.Multiplicities(),AppVal.Degree());
409 const AppParCurves_MultiBSpCurve& AppVal2 = Approx.Value(2);
410 TColgp_Array1OfPnt2d poles2d(1,AppVal2.NbPoles());
411 AppVal2.Curve(2,poles2d);
412 C2d = new Geom2d_BSplineCurve(poles2d,AppVal2.Knots(),AppVal2.Multiplicities(),AppVal2.Degree());
413 first = C2d->FirstParameter();
414 last = C2d->LastParameter();
420 case Contap_Restriction :
422 throw Standard_ProgramError("HLRTopoBRep_DSFiller::InsertFace : Restriction");
427 // compute the PCurve
429 if (!InsuffisantNumberOfPoints) {
433 VF.Orientation(TopAbs_FORWARD);
434 VL.Orientation(TopAbs_REVERSED);
437 B.Range(E,first,last);
440 B.UpdateEdge(E,C2d,F,BRep_Tool::Tolerance(F));
443 // add the edge in the DS
453 //Correction of internal outlines: unite coinciding vertices
454 const Standard_Real SqTol = tol*tol;
455 TopTools_ListIteratorOfListOfShape itl1(IntL);
456 for (; itl1.More(); itl1.Next())
458 TopoDS_Edge anIntLine = TopoDS::Edge(itl1.Value());
459 anIntLine.Orientation(TopAbs_FORWARD);
460 TopoDS_Vertex aVer [2];
461 TopExp::Vertices(anIntLine, aVer[0], aVer[1]);
462 TopTools_ListIteratorOfListOfShape itl2 = itl1;
463 for (; itl2.More(); itl2.Next())
465 TopoDS_Edge anIntLine2 = TopoDS::Edge(itl2.Value());
466 anIntLine2.Orientation(TopAbs_FORWARD);
467 if (anIntLine2.IsSame(anIntLine))
469 TopoDS_Vertex aVer2 [2];
470 TopExp::Vertices(anIntLine2, aVer2[0], aVer2[1]);
471 for (Standard_Integer i = 0; i < 2; i++)
473 if (i == 1 && aVer[0].IsSame(aVer[1]))
475 gp_Pnt Pnt1 = BRep_Tool::Pnt(aVer[i]);
476 for (Standard_Integer j = 0; j < 2; j++)
478 if (aVer[i].IsSame(aVer2[j]))
480 gp_Pnt Pnt2 = BRep_Tool::Pnt(aVer2[j]);
481 if (Pnt1.SquareDistance(Pnt2) <= SqTol)
484 aBB.Remove(anIntLine2, aVer2[j]);
485 aVer[i].Orientation((j==0)? TopAbs_FORWARD : TopAbs_REVERSED);
486 aBB.Add(anIntLine2, aVer[i]);
494 //=======================================================================
495 //function : MakeVertex
496 //purpose : private, make a vertex from an intersection point
497 //=======================================================================
500 HLRTopoBRep_DSFiller::MakeVertex (const Contap_Point& P,
501 const Standard_Real tol,
502 HLRTopoBRep_Data& DS)
507 V = Handle(BRepTopAdaptor_HVertex)::DownCast(P.Vertex())->Vertex();
511 // if on arc, insert in the DS
513 const TopoDS_Edge& E =
514 (*(BRepAdaptor_Curve2d*)&((P.Arc())->Curve2d())).Edge();
515 Standard_Real Par = P.ParameterOnArc();
516 const gp_Pnt& P3d = P.Value();
518 for (DS.InitVertex(E); DS.MoreVertex(); DS.NextVertex()) {
519 TopoDS_Vertex curV = DS.Vertex();
520 Standard_Real curP = DS.Parameter();
521 const gp_Pnt& PPP=BRep_Tool::Pnt(curV);
522 Standard_Real TTT=BRep_Tool::Tolerance(curV);
523 if (P3d.IsEqual(PPP,TTT)) {
527 else if (Par < curP) {
528 B.MakeVertex(V,P.Value(),tol);
529 DS.InsertBefore(V,Par);
533 if (!DS.MoreVertex()) {
534 B.MakeVertex(V,P.Value(),tol);
539 // if internal create a vertex and insert in the DS
541 B.MakeVertex(V,P.Value(),tol);
551 //=======================================================================
552 //function : InsertVertex
553 //purpose : private, insert a vertex from an internal intersection point
555 //=======================================================================
558 HLRTopoBRep_DSFiller::InsertVertex (const Contap_Point& P,
559 const Standard_Real tol,
560 const TopoDS_Edge& E,
561 HLRTopoBRep_Data& DS)
567 V = Handle(BRepTopAdaptor_HVertex)::DownCast(P.Vertex())->Vertex();
570 Standard_Real Par = P.ParameterOnLine();
572 for (DS.InitVertex(E); DS.MoreVertex(); DS.NextVertex()) {
573 TopoDS_Vertex curV = DS.Vertex();
574 Standard_Real curP = DS.Parameter();
575 if (P.Value().IsEqual(BRep_Tool::Pnt(curV),
576 BRep_Tool::Tolerance(curV))) {
580 else if (Par < curP) {
581 B.MakeVertex(V,P.Value(),tol);
582 DS.InsertBefore(V,Par);
586 if (!DS.MoreVertex()) {
587 B.MakeVertex(V,P.Value(),tol);
594 //=======================================================================
595 //function : ProcessEdges
596 //purpose : private, split edges with outline vertices
597 //=======================================================================
599 void HLRTopoBRep_DSFiller::ProcessEdges (HLRTopoBRep_Data& DS)
603 TopoDS_Vertex VF,VL,VI;
604 Standard_Real PF,PL,PI;
606 for (DS.InitEdge(); DS.MoreEdge(); DS.NextEdge()) {
607 TopoDS_Edge E = DS.Edge();
608 TopTools_ListOfShape& SplE = DS.AddSplE(E);
609 VF = TopExp::FirstVertex(E);
610 VL = TopExp::LastVertex(E);
611 BRep_Tool::Range(E,PF,PL);
612 VF.Orientation(TopAbs_FORWARD);
613 VL.Orientation(TopAbs_REVERSED);
615 for (DS.InitVertex(E); DS.MoreVertex(); DS.NextVertex()) {
618 VI.Orientation(TopAbs_REVERSED);
621 newE.Orientation(TopAbs_FORWARD);
623 B.UpdateVertex(VF,PF,newE,BRep_Tool::Tolerance(VF));
625 B.UpdateVertex(VI,PI,newE,BRep_Tool::Tolerance(VI));
626 newE.Orientation(E.Orientation());
630 VF.Orientation(TopAbs_FORWARD);
634 newE.Orientation(TopAbs_FORWARD);
636 B.UpdateVertex(VF,PF,newE,BRep_Tool::Tolerance(VF));
638 B.UpdateVertex(VL,PL,newE,BRep_Tool::Tolerance(VL));
639 newE.Orientation(E.Orientation());