1 // Created on: 1997-10-13
2 // Created by: Roman BORISOV
3 // Copyright (c) 1997-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
9 // under the terms of the GNU Lesser General Public 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.
17 #include <BRepAlgo_NormalProjection.ixx>
18 #include <ProjLib_CompProjectedCurve.hxx>
19 #include <TopTools_HSequenceOfShape.hxx>
20 #include <TopExp_Explorer.hxx>
22 #include <BRepAdaptor_Curve.hxx>
23 #include <BRepAdaptor_HCurve.hxx>
24 #include <BRepAdaptor_Surface.hxx>
25 #include <BRepAdaptor_HSurface.hxx>
26 #include <ProjLib_HCompProjectedCurve.hxx>
27 #include <TopoDS_Edge.hxx>
28 #include <TopoDS_Shape.hxx>
29 #include <Approx_CurveOnSurface.hxx>
31 #include <BRep_Builder.hxx>
32 #include <BRepLib_MakeVertex.hxx>
33 #include <BRepLib_MakeWire.hxx>
34 #include <Geom2dAdaptor_HCurve.hxx>
35 #include <Geom2d_Curve.hxx>
36 #include <Geom_BSplineCurve.hxx>
37 #include <GeomAdaptor.hxx>
38 #include <BRepLib_MakeEdge.hxx>
39 #include <BRepAlgo_BooleanOperations.hxx>
40 #include <TopOpeBRepBuild_HBuilder.hxx>
41 #include <BRepTopAdaptor_FClass2d.hxx>
42 #include <Precision.hxx>
43 #include <BRepAlgo_SequenceOfSequenceOfInteger.hxx>
45 #include <BRepTools.hxx>
46 #include <TColgp_Array1OfPnt2d.hxx>
47 #include <TColStd_Array1OfReal.hxx>
48 #include <TColStd_Array1OfInteger.hxx>
49 #include <Geom2d_TrimmedCurve.hxx>
50 #include <Geom2d_BSplineCurve.hxx>
51 #include <TopTools_ListIteratorOfListOfShape.hxx>
53 #ifdef __OCC_DEBUG_CHRONO
54 #include <OSD_Timer.hxx>
56 OSD_Chronometer chr_total, chr_init, chr_approx, chr_booltool;
58 Standard_Real t_total, t_init, t_approx, t_booltool;
59 Standard_IMPORT Standard_Real t_init_point, t_dicho_bound;
60 Standard_IMPORT Standard_Integer init_point_count, dicho_bound_count;
62 void InitChron(OSD_Chronometer& ch)
68 void ResultChron( OSD_Chronometer & ch, Standard_Real & time)
77 //=======================================================================
78 //function : BRepAlgo_NormalProjection
80 //=======================================================================
82 BRepAlgo_NormalProjection::BRepAlgo_NormalProjection()
83 : myWith3d(Standard_True)
87 BB.MakeCompound(TopoDS::Compound(myToProj));
88 myFaceBounds=Standard_True;
93 //=======================================================================
94 //function : BRepAlgo_NormalProjection
96 //=======================================================================
98 BRepAlgo_NormalProjection::BRepAlgo_NormalProjection(const TopoDS_Shape& S)
99 : myWith3d(Standard_True)
102 BB.MakeCompound(TopoDS::Compound(myToProj));
108 //=======================================================================
111 //=======================================================================
113 void BRepAlgo_NormalProjection::Init(const TopoDS_Shape& S)
118 //=======================================================================
121 //=======================================================================
123 void BRepAlgo_NormalProjection::Add(const TopoDS_Shape& ToProj)
126 BB.Add(myToProj, ToProj);
129 //=======================================================================
130 //function : SetParams
132 //=======================================================================
134 void BRepAlgo_NormalProjection::SetParams(const Standard_Real Tol3D,
135 const Standard_Real Tol2D,
136 const GeomAbs_Shape InternalContinuity,
137 const Standard_Integer MaxDegree,
138 const Standard_Integer MaxSeg)
142 myContinuity = InternalContinuity;
143 myMaxDegree = MaxDegree;
147 //=======================================================================
148 //function : SetDefaultParams
150 //=======================================================================
152 void BRepAlgo_NormalProjection::SetDefaultParams()
155 myTol2d = Pow(myTol3d, 2./3);
156 myContinuity = GeomAbs_C2;
161 //=======================================================================
162 //function : SetLimits
164 //=======================================================================
166 void BRepAlgo_NormalProjection::SetLimit(const Standard_Boolean FaceBounds)
168 myFaceBounds = FaceBounds;
171 //=======================================================================
172 //function : SetMaxDistance
174 //=======================================================================
176 void BRepAlgo_NormalProjection::SetMaxDistance(const Standard_Real MaxDist)
181 //=======================================================================
182 //function : Compute3d
184 //=======================================================================
186 void BRepAlgo_NormalProjection::Compute3d(const Standard_Boolean With3d)
191 //=======================================================================
194 //=======================================================================
196 void BRepAlgo_NormalProjection::Build()
198 #ifdef __OCC_DEBUG_CHRONO
199 Standard_Integer init_count = 0, approx_count = 0, booltool_count = 0;
206 init_point_count = 0;
209 dicho_bound_count = 0;
211 InitChron(chr_total);
213 myIsDone = Standard_False;
214 ProjLib_CompProjectedCurve Projector;
215 Handle(TopTools_HSequenceOfShape) Edges = new TopTools_HSequenceOfShape();
216 Handle(TopTools_HSequenceOfShape) Faces = new TopTools_HSequenceOfShape();
217 TopTools_ListOfShape DescenList;
218 Standard_Integer NbEdges = 0, NbFaces = 0, i, j, k;
219 TopExp_Explorer ExpOfWire, ExpOfShape;
220 Standard_Real Udeb, Ufin;
221 TopoDS_Shape VertexRes;
222 Standard_Boolean Only3d, Only2d, Elementary;
224 // for isoparametric cases
225 TColgp_Array1OfPnt2d Poles(1, 2);
226 TColStd_Array1OfReal Knots(1, 2);
227 TColStd_Array1OfInteger Mults(1,2);
228 Standard_Integer Deg;
234 for(ExpOfWire.Init(myToProj, TopAbs_EDGE);
236 ExpOfWire.Next(), NbEdges++) {
237 Edges->Append(ExpOfWire.Current());
240 for(ExpOfShape.Init(myShape, TopAbs_FACE);
242 ExpOfShape.Next(), NbFaces++) {
243 Faces->Append(ExpOfShape.Current());
247 BB.MakeCompound(TopoDS::Compound(myRes));
248 BB.MakeCompound(TopoDS::Compound(VertexRes));
249 Standard_Boolean YaVertexRes = Standard_False;
251 for(i = 1; i <= NbEdges; i++){
253 BRepAdaptor_Curve cur(TopoDS::Edge(Edges->Value(i)));
254 Handle(BRepAdaptor_HCurve) hcur = new BRepAdaptor_HCurve();
256 Elementary = IsElementary(cur);
257 for(j = 1; j <= NbFaces; j++){
258 BRepAdaptor_Surface sur(TopoDS::Face(Faces->Value(j)));
259 Handle(BRepAdaptor_HSurface) hsur = new BRepAdaptor_HSurface();
262 // computation of TolU and TolV
264 Standard_Real TolU, TolV;
266 TolU = hsur->UResolution(myTol3d)/20;
267 TolV = hsur->VResolution(myTol3d)/20;
269 #ifdef __OCC_DEBUG_CHRONO
273 ProjLib_CompProjectedCurve(hsur, hcur, TolU, TolV, myMaxDist);
274 #ifdef __OCC_DEBUG_CHRONO
275 ResultChron(chr_init,t_init);
279 Handle(ProjLib_HCompProjectedCurve) HProjector =
280 new ProjLib_HCompProjectedCurve();
281 HProjector->Set(Projector);
283 Standard_Boolean Degenerated = Standard_False;
284 gp_Pnt2d P2d, Pdeb, Pfin;
286 Standard_Real UIso, VIso;
288 Handle(Adaptor2d_HCurve2d) HPCur;
289 Handle(Geom2d_Curve) PCur2d; // Only for isoparametric projection
291 for(k = 1; k <= Projector.NbCurves(); k++){
292 if(Projector.IsSinglePnt(k, P2d)){
294 cout << "Projection of edge "<<i<<" on face "<<j;
295 cout << " is punctual"<<endl<<endl;
297 Projector.GetSurface()->D0(P2d.X(), P2d.Y(), P);
298 prj = BRepLib_MakeVertex(P).Shape();
299 DescenList.Append(prj);
300 BB.Add(VertexRes, prj);
301 YaVertexRes = Standard_True;
303 myAncestorMap.Bind(prj, Edges->Value(i));
306 Only2d = Only3d = Standard_False;
307 Projector.Bounds(k, Udeb, Ufin);
309 /**************************************************************/
310 if (Projector.IsUIso(k, UIso)) {
312 cout << "Projection of edge "<<i<<" on face "<<j;
313 cout << " is U-isoparametric"<<endl<<endl;
315 Projector.D0(Udeb, Pdeb);
316 Projector.D0(Ufin, Pfin);
321 Handle(Geom2d_BSplineCurve) BS2d =
322 new Geom2d_BSplineCurve(Poles, Knots, Mults, Deg);
323 PCur2d = new Geom2d_TrimmedCurve( BS2d, Udeb, Ufin);
324 HPCur = new Geom2dAdaptor_HCurve(PCur2d);
325 Only3d = Standard_True;
327 else if (Projector.IsVIso(k, VIso)) {
329 cout << "Projection of edge "<<i<<" on face "<<j;
330 cout << " is V-isoparametric"<<endl<<endl;
332 Projector.D0(Udeb, Pdeb);
333 Projector.D0(Ufin, Pfin);
338 Handle(Geom2d_BSplineCurve) BS2d =
339 new Geom2d_BSplineCurve(Poles, Knots, Mults, Deg);
340 PCur2d = new Geom2d_TrimmedCurve(BS2d, Udeb, Ufin);
341 HPCur = new Geom2dAdaptor_HCurve(PCur2d);
342 Only3d = Standard_True;
344 else HPCur = HProjector;
346 if((myWith3d == Standard_False || Elementary) &&
347 (Projector.MaxDistance(k) <= myTol3d) )
348 Only2d = Standard_True;
350 if(Only2d && Only3d) {
351 BRepLib_MakeEdge MKed(GeomAdaptor::MakeCurve(hcur->Curve()),
354 BB.UpdateEdge(TopoDS::Edge(prj),
356 TopoDS::Face(Faces->Value(j)),
358 BB.UpdateVertex(TopExp::FirstVertex(TopoDS::Edge(prj)),myTol3d);
359 BB.UpdateVertex(TopExp::LastVertex(TopoDS::Edge(prj)),myTol3d);
362 #ifdef __OCC_DEBUG_CHRONO
363 InitChron(chr_approx);
365 Approx_CurveOnSurface appr(HPCur, hsur, Udeb, Ufin, myTol3d,
366 myContinuity, myMaxDegree, myMaxSeg,
368 #ifdef __OCC_DEBUG_CHRONO
369 ResultChron(chr_approx,t_approx);
372 cout<<"Approximation.IsDone = "<<appr.IsDone()<<endl;
374 cout<<"MaxError3d = "<<appr.MaxError3d()<<endl<<endl;
376 cout<<"MaxError2dU = "<<appr.MaxError2dU()<<endl;
377 cout<<"MaxError2dV = "<<appr.MaxError2dV()<<endl<<endl;
382 if(!Only3d) PCur2d = appr.Curve2d();
384 BRepLib_MakeEdge MKed(GeomAdaptor::MakeCurve(hcur->Curve()),
389 // It is tested if the solution is not degenerated to set the
390 // flag on edge, one takes several points, checks if the cloud of
391 // points has less diameter than the tolerance 3D
392 Degenerated = Standard_True;
394 Handle(Geom_BSplineCurve) BS3d = Handle(Geom_BSplineCurve)::DownCast( appr.Curve3d());
395 gp_Pnt P1(0.,0.,0.),PP; // skl : I change "P" to "PP"
396 Standard_Integer NbPoint,ii ; // skl : I change "i" to "ii"
397 Standard_Real Par,DPar;
398 // start from 3 points to reject non degenerated edges
401 DPar = (BS3d->LastParameter()-BS3d->FirstParameter())/(NbPoint-1);
402 for (ii=0;ii<NbPoint;ii++)
404 Par=BS3d->FirstParameter()+ii*DPar;
406 P1.SetXYZ(P1.XYZ() + PP.XYZ()/NbPoint);
408 for (ii=0;ii<NbPoint && Degenerated ;ii++)
410 Par=BS3d->FirstParameter()+ii*DPar;
412 Dist=P1.Distance(PP);
414 Degenerated = Standard_False;
418 // if the test passes a more exact test with 10 points
420 P1.SetCoord(0.,0.,0.);
422 DPar = (BS3d->LastParameter()-BS3d->FirstParameter())/(NbPoint-1);
423 for (ii=0;ii<NbPoint;ii++)
425 Par=BS3d->FirstParameter()+ii*DPar;
427 P1.SetXYZ(P1.XYZ() + PP.XYZ()/NbPoint);
429 for (ii=0;ii<NbPoint && Degenerated ;ii++)
431 Par=BS3d->FirstParameter()+ii*DPar;
433 Dist=P1.Distance(PP);
435 Degenerated = Standard_False;
442 cout << "Projection of edge "<<i<<" on face "<<j;
443 cout << " is degenerated "<<endl<<endl;
447 BB.UpdateVertex(VV,P1,myTol3d);
448 BB.MakeEdge(TopoDS::Edge(prj));
449 BB.Add(TopoDS::Edge(prj),VV.Oriented(TopAbs_FORWARD));
450 BB.Add(TopoDS::Edge(prj),VV.Oriented(TopAbs_REVERSED));
451 BB.Degenerated(TopoDS::Edge(prj), Standard_True);
454 prj = BRepLib_MakeEdge(BS3d).Edge();
458 BB.UpdateEdge(TopoDS::Edge(prj),
460 TopoDS::Face(Faces->Value(j)),
462 BB.UpdateVertex(TopExp::FirstVertex(TopoDS::Edge(prj)),appr.MaxError3d());
463 BB.UpdateVertex(TopExp::LastVertex(TopoDS::Edge(prj)),appr.MaxError3d());
465 BB.Range(TopoDS::Edge(prj),
466 TopoDS::Face(Faces->Value(j)),
472 // Trimming edges by face bounds
473 // if the solution is degenerated, use of BoolTool is avoided
474 #ifdef __OCC_DEBUG_CHRONO
475 InitChron(chr_booltool);
478 BRepAlgo_BooleanOperations BoolTool;
479 BoolTool.Shapes2d(Faces->Value(j),prj);
481 Handle(TopOpeBRepBuild_HBuilder) HB;
482 TopTools_ListOfShape LS;
483 TopTools_ListIteratorOfListOfShape Iter;
484 HB = BoolTool.Builder();
486 if (HB->IsSplit(prj, TopAbs_IN))
487 LS = HB->Splits(prj, TopAbs_IN);
491 cout << " BooleanOperations :" << Iter.More()<<" solutions " << endl;
493 for(; Iter.More(); Iter.Next()) {
494 BB.Add(myRes, Iter.Value());
495 myAncestorMap.Bind(Iter.Value(), Edges->Value(i));
496 myCorresp.Bind(Iter.Value(),Faces->Value(j));
502 BRepTopAdaptor_FClass2d classifier(TopoDS::Face(Faces->Value(j)),
503 Precision::Confusion());
505 Standard_Real f = PCur2d->FirstParameter();
506 Standard_Real l = PCur2d->LastParameter();
507 Standard_Real pmil = (f + l )/2;
508 PCur2d->D0(pmil, Puv);
510 state = classifier.Perform(Puv);
511 if(state == TopAbs_IN || state == TopAbs_ON) {
513 DescenList.Append(prj);
514 myAncestorMap.Bind(prj, Edges->Value(i));
515 myCorresp.Bind(prj, Faces->Value(j));
521 cout << " BooleanOperations : no solution " << endl;
524 BRepTopAdaptor_FClass2d classifier(TopoDS::Face(Faces->Value(j)),
525 Precision::Confusion());
527 Standard_Real f = PCur2d->FirstParameter();
528 Standard_Real l = PCur2d->LastParameter();
529 Standard_Real pmil = (f + l )/2;
530 PCur2d->D0(pmil, Puv);
532 state = classifier.Perform(Puv);
533 if(state == TopAbs_IN || state == TopAbs_ON) {
535 DescenList.Append(prj);
536 myAncestorMap.Bind(prj, Edges->Value(i));
537 myCorresp.Bind(prj, Faces->Value(j));
539 #ifdef __OCC_DEBUG_CHRONO
540 ResultChron(chr_booltool,t_booltool);
547 DescenList.Append(prj);
548 myAncestorMap.Bind(prj, Edges->Value(i));
549 myCorresp.Bind(prj, Faces->Value(j));
554 myDescendants.Bind(Edges->Value(i), DescenList);
556 // JPI : eventual wire creation is reported in a specific method
557 // BuilWire that can be called by the user. Otherwise, the
558 // relations of map myAncestorMap, myCorresp will be lost.
560 if(YaVertexRes) BB.Add(myRes, VertexRes);
562 myIsDone = Standard_True;
564 #ifdef __OCC_DEBUG_CHRONO
565 ResultChron(chr_total,t_total);
567 cout<<"Build - Total time : "<<t_total<<" includes:" <<endl;
568 cout<<"- Projection : "<<t_init<<endl;
569 cout<<" -- Initial point search : "<<t_init_point<<endl;
570 cout<<" -- DichoBound search : "<<t_dicho_bound<<endl;
571 cout<<"- Approximation : "<<t_approx<<endl;
572 cout<<"- Boolean operation : "<<t_booltool<<endl;
573 cout<<"- Rest of time : "<<t_total-(t_init + t_approx + t_booltool )<<endl<<endl;
575 t_init /= init_count;
576 if (init_point_count != 0)
577 t_init_point /= init_point_count;
578 if (dicho_bound_count != 0)
579 t_dicho_bound /= dicho_bound_count;
580 if (approx_count != 0)
581 t_approx /= approx_count;
582 if (booltool_count != 0)
583 t_booltool /= booltool_count;
585 cout<<"Unitary average time : "<<endl;
586 cout<<"- Projection : "<<t_init<<endl;
587 cout<<" -- Initial point search: "<<t_init_point<<endl;
588 cout<<" -- DichoBound search : "<<t_dicho_bound<<endl;
589 cout<<"- Approximation : "<<t_approx<<endl;
590 cout<<"- Boolean operation :"<<t_booltool<<endl;
591 cout<<endl<<"Number of initial point computations is "<<init_point_count<<endl<<endl;
595 //=======================================================================
598 //=======================================================================
600 Standard_Boolean BRepAlgo_NormalProjection::IsDone() const
605 //=======================================================================
606 //function : Projection
608 //=======================================================================
610 const TopoDS_Shape& BRepAlgo_NormalProjection::Projection() const
615 //=======================================================================
616 //function : Ancestor
618 //=======================================================================
620 const TopoDS_Shape& BRepAlgo_NormalProjection::Ancestor(const TopoDS_Edge& E) const
622 return myAncestorMap.Find(E);
625 //=======================================================================
628 //=======================================================================
630 const TopoDS_Shape& BRepAlgo_NormalProjection::Couple(const TopoDS_Edge& E) const
632 return myCorresp.Find(E);
635 //=======================================================================
636 //function : Generated
638 //=======================================================================
640 const TopTools_ListOfShape& BRepAlgo_NormalProjection::Generated(const TopoDS_Shape& S)
642 return myDescendants.Find(S);
645 //=======================================================================
646 //function : IsElementary
648 //=======================================================================
650 Standard_Boolean BRepAlgo_NormalProjection::IsElementary(const Adaptor3d_Curve& C) const
652 GeomAbs_CurveType type;
657 case GeomAbs_Ellipse:
658 case GeomAbs_Hyperbola:
659 case GeomAbs_Parabola: return Standard_True;
660 default: return Standard_False;
663 //=======================================================================
664 //function : BuildWire
666 //=======================================================================
668 Standard_Boolean BRepAlgo_NormalProjection::BuildWire(TopTools_ListOfShape& ListOfWire) const
670 TopExp_Explorer ExpOfWire, ExpOfShape;
671 Standard_Boolean IsWire=Standard_False;
672 ExpOfShape.Init(myRes, TopAbs_EDGE);
673 if(ExpOfShape.More())
675 TopTools_ListOfShape List;
677 for ( ; ExpOfShape.More(); ExpOfShape.Next())
679 const TopoDS_Shape& CurE = ExpOfShape.Current();
686 const TopoDS_Shape& Wire = MW.Shape();
687 // If the resulting wire contains the same edge as at the beginning OK
688 // otherwise the result really consists of several wires.
689 TopExp_Explorer exp2(Wire,TopAbs_EDGE);
690 Standard_Integer NbEdges = 0;
691 for (;exp2.More(); exp2.Next()) NbEdges++;
692 if ( NbEdges == List.Extent())
694 ListOfWire.Append(Wire);
695 IsWire = Standard_True;