1 // File: HLRTopoBRep_DSFiller.cxx
2 // Created: Tue Aug 10 16:16:53 1993
3 // Author: Christophe MARION
6 #include <HLRTopoBRep_DSFiller.ixx>
10 #include <TopExp_Explorer.hxx>
11 #include <math_Vector.hxx>
12 #include <TColgp_Array1OfPnt.hxx>
13 #include <TColgp_Array1OfPnt2d.hxx>
14 #include <TColStd_Array1OfReal.hxx>
15 #include <TColStd_Array1OfInteger.hxx>
16 #include <Geom_Line.hxx>
17 #include <Geom_Circle.hxx>
18 #include <Geom_Surface.hxx>
19 #include <Geom_BSplineCurve.hxx>
20 #include <GeomProjLib.hxx>
21 #include <Geom2d_Curve.hxx>
22 #include <Geom2d_BSplineCurve.hxx>
23 #include <AppDef_BSplineCompute.hxx>
24 #include <AppDef_MultiLine.hxx>
25 #include <AppDef_MultiPointConstraint.hxx>
26 #include <AppParCurves_MultiBSpCurve.hxx>
27 #include <HLRTopoBRep_FaceIsoLiner.hxx>
28 #include <BRep_Tool.hxx>
29 #include <BRep_Builder.hxx>
30 #include <BRepAdaptor_Curve.hxx>
31 #include <BRepAdaptor_Curve2d.hxx>
32 #include <BRepAdaptor_HCurve2d.hxx>
33 #include <BRepTopAdaptor_HVertex.hxx>
34 #include <BRepTopAdaptor_TopolTool.hxx>
35 #include <BRepTopAdaptor_Tool.hxx>
36 #include <Contap_TheLineOfContour.hxx>
37 #include <Extrema_LocateExtPC.hxx>
38 #include <Standard_ProgramError.hxx>
39 #include <Precision.hxx>
40 #include <BRepApprox_ApproxLine.hxx>
41 #include <BRepApprox_Approx.hxx>
47 //=======================================================================
49 //purpose : explore the faces and insert them
50 //=======================================================================
52 void HLRTopoBRep_DSFiller::Insert (const TopoDS_Shape& S,
55 BRepTopAdaptor_MapOfShapeTool& MST,
56 const Standard_Integer nbIso)
58 TopTools_MapOfShape ShapeMap;
59 TopExp_Explorer ex(S,TopAbs_FACE);
61 Standard_Boolean withPCurve = Standard_True; // instead of nbIso != 0;
62 Standard_Integer f = 0;
65 if (ShapeMap.Add(ex.Current())) {
67 TopoDS_Face S1 = TopoDS::Face(ex.Current());
68 S1.Orientation(TopAbs_FORWARD);
69 Handle(BRepTopAdaptor_TopolTool) Domain;
70 Handle(Adaptor3d_HSurface) Surface;
72 BRepTopAdaptor_Tool& BRT = MST.ChangeFind(S1);
73 Domain = BRT.GetTopolTool();
74 Surface = BRT.GetSurface();
77 BRepTopAdaptor_Tool BRT(S1,Precision::PConfusion());
79 Domain = BRT.GetTopolTool();
80 Surface = BRT.GetSurface();
82 FO.Perform(Surface,Domain);
85 InsertFace(f,S1,FO,DS,withPCurve);
87 if (nbIso != 0) HLRTopoBRep_FaceIsoLiner::Perform(f,S1,DS,nbIso);
94 //=======================================================================
95 //function : InsertFace
96 //purpose : private, insert the outlines of a face
97 //=======================================================================
99 void HLRTopoBRep_DSFiller::InsertFace (const Standard_Integer FI,
100 const TopoDS_Face& F,
102 HLRTopoBRep_Data& DS,
103 const Standard_Boolean withPCurve)
105 // Insert the intersections of FO in DS
107 const Standard_Real tol = BRep_Tool::Tolerance(F);
108 TopTools_ListOfShape& IntL = DS.AddIntL(F);
109 TopTools_ListOfShape& OutL = DS.AddOutL(F);
113 TopTools_MapOfShape VM;
114 TopExp_Explorer ex(F,TopAbs_EDGE);
116 const TopoDS_Edge& E = TopoDS::Edge(ex.Current());
117 if (BRep_Tool::IsClosed(E,F)) {
118 TopExp::Vertices(E,VF,VL);
126 const Standard_Integer NbLines = FO.NbLines();
127 Standard_Integer CurLine = 1;
128 for (; CurLine <= NbLines; CurLine++)
130 const Contap_TheLineOfContour& Line = FO.Line(CurLine);
131 const Standard_Integer NbPoints = Line.NbVertex();
132 Standard_Integer CurPoint;
133 if (Line.TypeContour() == Contap_Restriction)
135 // OutLine on restriction
136 TopoDS_Edge E = (*(BRepAdaptor_Curve2d*)&(Line.Arc()->Curve2d())).Edge();
138 TopExp::Vertices(E,VF,VL);
139 // insert the Internal points.
141 for (CurPoint = 1; CurPoint <= NbPoints; CurPoint++) {
142 Contap_ThePointOfContour P = Line.Vertex(CurPoint);
143 if (P.IsInternal()) {
144 if (P.Value().IsEqual(BRep_Tool::Pnt(VF),BRep_Tool::Tolerance(VF))) {
145 if (P.Value().IsEqual(BRep_Tool::Pnt(VL),BRep_Tool::Tolerance(VL))) {
146 InsertVertex(P,tol,E,DS);
154 for (CurPoint = 1; CurPoint <= NbPoints; CurPoint++) {
156 const Contap_ThePointOfContour PF = Line.Vertex(CurPoint);
157 if (PF.IsInternal() && CurPoint != 1)
160 VF = MakeVertex(PF,tol,DS);
161 const Standard_Real parF = PF.ParameterOnLine();
163 if (CurPoint < NbPoints) {
164 const Contap_ThePointOfContour PL = Line.Vertex(CurPoint+1);
165 VL = MakeVertex(PL,tol,DS);
166 const Standard_Real parL = PL.ParameterOnLine();
168 if( (parL-parF) > Precision::PConfusion() ) {
170 Handle(Geom_Curve) C;
171 Handle(Geom2d_Curve) C2d;
172 Standard_Real first = parF;
173 Standard_Real last = parL;
174 Standard_Boolean InsuffisantNumberOfPoints=Standard_False;
176 switch (Line.TypeContour()) {
180 C = new Geom_Line(Line.Line());
182 Handle(Geom_Surface) S = BRep_Tool::Surface(F);
183 Standard_Real Tol = 1e-7;
184 C2d = GeomProjLib::Curve2d(C,first,last,S,Tol);
191 C = new Geom_Circle(Line.Circle());
194 Handle(Geom_Surface) S = BRep_Tool::Surface(F,Loc);
195 if (!Loc.IsIdentity()) {
196 S = Handle(Geom_Surface)::DownCast(S->Transformed(Loc.Transformation()));
198 Standard_Real Tol = 1e-7;
199 C2d = GeomProjLib::Curve2d(C,first,last,S,Tol);
204 case Contap_Walking :
207 Standard_Integer ipF = Standard_Integer(parF);
208 Standard_Integer ipL = Standard_Integer(parL);
211 InsuffisantNumberOfPoints=Standard_True;
212 //cout<<"\n !! Pb ds HLRTopoBRep_DSFiller.cxx (Contour App Nbp <3)"<<endl;
215 else if(ipL-ipF < 6) {
216 // compute the tangents
217 Contap_TheSurfFunctionOfContour& SFunc =
218 FO.SurfaceFunction();
220 Standard_Boolean isTg1,isTg2;
223 math_Vector UV(1,2),F(1,1);
225 Line.Point(ipF).ParametersOnS2(UV(1),UV(2));
227 isTg1 = SFunc.IsTangent();
229 tg1 = SFunc.Direction3d();
230 if (withPCurve) uv1 = SFunc.Direction2d();
233 Line.Point(ipL).ParametersOnS2(UV(1),UV(2));
235 isTg2 = SFunc.IsTangent();
237 tg2 = SFunc.Direction3d();
238 if (withPCurve) uv2 = SFunc.Direction2d();
241 Standard_Integer nbp = ipL - ipF + 1;
242 AppDef_MultiLine MLine(nbp);
243 Standard_Integer nb2d = 0;
244 if (withPCurve) nb2d = 1;
246 for (Standard_Integer i = 1; i <= nbp; i++) {
247 AppDef_MultiPointConstraint MP(1, nb2d);
248 MP.SetPoint(1,Line.Point(i + ipF - 1).Value());
250 Line.Point(i + ipF - 1).ParametersOnS2(UV(1),UV(2));
251 MP.SetPoint2d(2,gp_Pnt2d(UV(1),UV(2)));
254 if (i == 1 && !isTg1) {
256 if (withPCurve) MP.SetTang2d(2,uv1);
258 if (i == nbp && !isTg2) {
260 if (withPCurve) MP.SetTang2d(2,uv2);
262 MLine.SetValue(i,MP);
264 AppDef_BSplineCompute interp;
265 interp.Interpol(MLine);
266 AppParCurves_MultiBSpCurve TheCurve = interp.Value();
267 Standard_Integer Degree = TheCurve.Degree();
268 TColgp_Array1OfPnt Poles(1,TheCurve.NbPoles());
269 TheCurve.Curve(1,Poles);
270 C = new Geom_BSplineCurve(Poles,
272 TheCurve.Multiplicities(),
275 TColgp_Array1OfPnt2d Pol2d(1,TheCurve.NbPoles());
276 TheCurve.Curve(2,Pol2d);
277 C2d = new Geom2d_BSplineCurve(Pol2d,
279 TheCurve.Multiplicities(),
286 else if(ipL-ipF < 5) {
287 const Standard_Integer nbp = ipL - ipF + 1;
288 TColStd_Array1OfReal knots(1,nbp);
289 TColStd_Array1OfInteger mults(1,nbp);
290 TColgp_Array1OfPnt Points(1,nbp);
292 for(Standard_Integer i=1;i<=nbp;i++) {
293 knots.SetValue(i,(Standard_Real)i);
295 Points.SetValue(i,Line.Point(i+ipF-1).Value());
297 mults(1)=mults(nbp)=2;
298 C = new Geom_BSplineCurve(Points,knots,mults,1);
301 TColgp_Array1OfPnt2d Points2d(1,nbp);
302 for(Standard_Integer i=1;i<=nbp;i++) {
304 Line.Point(i+ipF-1).ParametersOnS2(u,v);
305 Points2d.SetValue(i,gp_Pnt2d(u,v));
307 C2d = new Geom2d_BSplineCurve(Points2d,knots,mults,1);
313 const Standard_Integer nbp = ipL - ipF + 1;
314 TColStd_Array1OfReal knots(1,nbp);
315 TColStd_Array1OfInteger mults(1,nbp);
316 TColgp_Array1OfPnt Points(1,nbp);
318 Standard_Real Maxx,Maxy,Maxz,Maxu,Maxv;
319 Standard_Real Minx,Miny,Minz,Minu,Minv;
320 Maxx=Maxy=Maxz=Maxu=Maxv=-RealLast();
321 Minx=Miny=Minz=Minu=Minv=RealLast();
323 for(Standard_Integer i=1;i<=nbp;i++) {
324 knots.SetValue(i,(Standard_Real)i);
326 const gp_Pnt& P= Line.Point(i+ipF-1).Value();
327 if(P.X()<Minx) Minx=P.X();
328 if(P.Y()<Miny) Miny=P.Y();
329 if(P.Z()<Minz) Minz=P.Z();
330 if(P.X()>Maxx) Maxx=P.X();
331 if(P.Y()>Maxy) Maxy=P.Y();
332 if(P.Z()>Maxz) Maxz=P.Z();
333 Points.SetValue(i,P);
335 mults(1)=mults(nbp)=2;
336 Handle(Geom_BSplineCurve) AppC;
337 Handle(Geom2d_BSplineCurve) AppC2d;
338 AppC = new Geom_BSplineCurve(Points,knots,mults,1);
341 TColgp_Array1OfPnt2d Points2d(1,nbp);
342 for(Standard_Integer i=1;i<=nbp;i++) {
344 Line.Point(i+ipF-1).ParametersOnS2(u,v);
349 Points2d.SetValue(i,gp_Pnt2d(u,v));
351 AppC2d = new Geom2d_BSplineCurve(Points2d,knots,mults,1);
356 Handle(BRepApprox_ApproxLine) AppLine;
357 Handle(Geom2d_BSplineCurve) CNull;
358 AppLine = new BRepApprox_ApproxLine(AppC,AppC2d,CNull);
360 Standard_Integer dmin=4,dmax=8,niter=0;
361 Standard_Boolean tg= Standard_False;
362 BRepApprox_Approx Approx;
363 Standard_Real TOL3d,TOL2d,TOL=0.0001;
365 Maxx-=Minx; Maxy-=Miny; Maxz-=Minz;
366 Maxu-=Minu; Maxv-=Minv;
367 if(Maxy>Maxx) Maxx=Maxy;
368 if(Maxz>Maxx) Maxx=Maxy;
369 if(Maxv>Maxu) Maxu=Maxv;
371 TOL3d=TOL*Maxx; if(TOL3d<1e-12) TOL3d=1e-12; else if(TOL3d>0.1) TOL3d=0.1;
372 TOL2d=TOL*Maxu; if(TOL2d<1e-12) TOL2d=1e-12; else if(TOL2d>0.1) TOL2d=0.1;
374 //-- cout<<"\nHLRTopoBRep_DSFiller : nbp="<<nbp<<" Tol3d="<<TOL3d<<" Tol2d="<<TOL2d<<endl;
376 Approx.SetParameters(TOL3d,TOL2d,dmin,dmax,niter,tg);
377 Approx.Perform(AppLine,Standard_True,Standard_True,Standard_False,1,nbp);
378 if (!Approx.IsDone()) {
385 const AppParCurves_MultiBSpCurve& AppVal = Approx.Value(1);
386 TColgp_Array1OfPnt poles3d(1,AppVal.NbPoles());
387 AppVal.Curve(1,poles3d);
388 C = new Geom_BSplineCurve(poles3d,AppVal.Knots(),AppVal.Multiplicities(),AppVal.Degree());
390 const AppParCurves_MultiBSpCurve& AppVal2 = Approx.Value(2);
391 TColgp_Array1OfPnt2d poles2d(1,AppVal2.NbPoles());
392 AppVal2.Curve(2,poles2d);
393 C2d = new Geom2d_BSplineCurve(poles2d,AppVal2.Knots(),AppVal2.Multiplicities(),AppVal2.Degree());
394 first = C2d->FirstParameter();
395 last = C2d->LastParameter();
401 case Contap_Restriction :
403 Standard_ProgramError::Raise("HLRTopoBRep_DSFiller::InsertFace : Restriction");
408 // compute the PCurve
410 if (!InsuffisantNumberOfPoints) {
414 VF.Orientation(TopAbs_FORWARD);
415 VL.Orientation(TopAbs_REVERSED);
418 B.Range(E,first,last);
422 B.UpdateEdge(E,C2d,F,BRep_Tool::Tolerance(F));
425 // add the edge in the DS
436 //=======================================================================
437 //function : MakeVertex
438 //purpose : private, make a vertex from an intersection point
439 //=======================================================================
442 HLRTopoBRep_DSFiller::MakeVertex (const Contap_ThePointOfContour& P,
443 const Standard_Real tol,
444 HLRTopoBRep_Data& DS)
449 V = Handle(BRepTopAdaptor_HVertex)::DownCast(P.Vertex())->Vertex();
453 // if on arc, insert in the DS
455 const TopoDS_Edge& E =
456 (*(BRepAdaptor_Curve2d*)&((P.Arc())->Curve2d())).Edge();
457 Standard_Real Par = P.ParameterOnArc();
458 const gp_Pnt& P3d = P.Value();
460 for (DS.InitVertex(E); DS.MoreVertex(); DS.NextVertex()) {
461 TopoDS_Vertex curV = DS.Vertex();
462 Standard_Real curP = DS.Parameter();
463 const gp_Pnt& PPP=BRep_Tool::Pnt(curV);
464 Standard_Real TTT=BRep_Tool::Tolerance(curV);
465 if (P3d.IsEqual(PPP,TTT)) {
469 else if (Par < curP) {
470 B.MakeVertex(V,P.Value(),tol);
471 DS.InsertBefore(V,Par);
475 if (!DS.MoreVertex()) {
476 B.MakeVertex(V,P.Value(),tol);
481 // if internal create a vertex and insert in the DS
483 B.MakeVertex(V,P.Value(),tol);
493 //=======================================================================
494 //function : InsertVertex
495 //purpose : private, insert a vertex from an internal intersection point
497 //=======================================================================
500 HLRTopoBRep_DSFiller::InsertVertex (const Contap_ThePointOfContour& P,
501 const Standard_Real tol,
502 const TopoDS_Edge& E,
503 HLRTopoBRep_Data& DS)
509 V = Handle(BRepTopAdaptor_HVertex)::DownCast(P.Vertex())->Vertex();
512 Standard_Real Par = P.ParameterOnLine();
514 for (DS.InitVertex(E); DS.MoreVertex(); DS.NextVertex()) {
515 TopoDS_Vertex curV = DS.Vertex();
516 Standard_Real curP = DS.Parameter();
517 if (P.Value().IsEqual(BRep_Tool::Pnt(curV),
518 BRep_Tool::Tolerance(curV))) {
522 else if (Par < curP) {
523 B.MakeVertex(V,P.Value(),tol);
524 DS.InsertBefore(V,Par);
528 if (!DS.MoreVertex()) {
529 B.MakeVertex(V,P.Value(),tol);
536 //=======================================================================
537 //function : ProcessEdges
538 //purpose : private, split edges with outline vertices
539 //=======================================================================
541 void HLRTopoBRep_DSFiller::ProcessEdges (HLRTopoBRep_Data& DS)
545 TopoDS_Vertex VF,VL,VI;
546 Standard_Real PF,PL,PI;
548 for (DS.InitEdge(); DS.MoreEdge(); DS.NextEdge()) {
549 TopoDS_Edge E = DS.Edge();
550 TopTools_ListOfShape& SplE = DS.AddSplE(E);
551 VF = TopExp::FirstVertex(E);
552 VL = TopExp::LastVertex(E);
553 BRep_Tool::Range(E,PF,PL);
554 VF.Orientation(TopAbs_FORWARD);
555 VL.Orientation(TopAbs_REVERSED);
557 for (DS.InitVertex(E); DS.MoreVertex(); DS.NextVertex()) {
560 VI.Orientation(TopAbs_REVERSED);
563 newE.Orientation(TopAbs_FORWARD);
565 B.UpdateVertex(VF,PF,newE,BRep_Tool::Tolerance(VF));
567 B.UpdateVertex(VI,PI,newE,BRep_Tool::Tolerance(VI));
568 newE.Orientation(E.Orientation());
572 VF.Orientation(TopAbs_FORWARD);
576 newE.Orientation(TopAbs_FORWARD);
578 B.UpdateVertex(VF,PF,newE,BRep_Tool::Tolerance(VF));
580 B.UpdateVertex(VL,PL,newE,BRep_Tool::Tolerance(VL));
581 newE.Orientation(E.Orientation());