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 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 <Adaptor3d_Curve.hxx>
19 #include <Approx_CurveOnSurface.hxx>
20 #include <BRep_Builder.hxx>
21 #include <BRepAdaptor_Curve.hxx>
22 #include <BRepAdaptor_HCurve.hxx>
23 #include <BRepAdaptor_HSurface.hxx>
24 #include <BRepAdaptor_Surface.hxx>
25 #include <BRepAlgo_BooleanOperations.hxx>
26 #include <BRepAlgo_NormalProjection.hxx>
27 #include <BRepAlgo_SequenceOfSequenceOfInteger.hxx>
28 #include <BRepLib_MakeEdge.hxx>
29 #include <BRepLib_MakeVertex.hxx>
30 #include <BRepLib_MakeWire.hxx>
31 #include <BRepTools.hxx>
32 #include <BRepTopAdaptor_FClass2d.hxx>
33 #include <Geom2d_BSplineCurve.hxx>
34 #include <Geom2d_Curve.hxx>
35 #include <Geom2d_TrimmedCurve.hxx>
36 #include <Geom2dAdaptor_HCurve.hxx>
37 #include <Geom_BSplineCurve.hxx>
38 #include <GeomAdaptor.hxx>
39 #include <Precision.hxx>
40 #include <ProjLib_CompProjectedCurve.hxx>
41 #include <ProjLib_HCompProjectedCurve.hxx>
42 #include <Standard_ConstructionError.hxx>
43 #include <Standard_NoSuchObject.hxx>
44 #include <StdFail_NotDone.hxx>
45 #include <TColgp_Array1OfPnt2d.hxx>
46 #include <TColStd_Array1OfInteger.hxx>
47 #include <TColStd_Array1OfReal.hxx>
50 #include <TopExp_Explorer.hxx>
52 #include <TopoDS_Edge.hxx>
53 #include <TopoDS_Shape.hxx>
54 #include <TopOpeBRepBuild_HBuilder.hxx>
55 #include <TopTools_HSequenceOfShape.hxx>
56 #include <TopTools_ListIteratorOfListOfShape.hxx>
58 #ifdef OCCT_DEBUG_CHRONO
59 #include <OSD_Timer.hxx>
61 OSD_Chronometer chr_total, chr_init, chr_approx, chr_booltool;
63 Standard_Real t_total, t_init, t_approx, t_booltool;
64 Standard_IMPORT Standard_Real t_init_point, t_dicho_bound;
65 Standard_IMPORT Standard_Integer init_point_count, dicho_bound_count;
67 void InitChron(OSD_Chronometer& ch)
73 void ResultChron( OSD_Chronometer & ch, Standard_Real & time)
82 //=======================================================================
83 //function : BRepAlgo_NormalProjection
85 //=======================================================================
87 BRepAlgo_NormalProjection::BRepAlgo_NormalProjection()
88 : myWith3d(Standard_True)
92 BB.MakeCompound(TopoDS::Compound(myToProj));
93 myFaceBounds=Standard_True;
98 //=======================================================================
99 //function : BRepAlgo_NormalProjection
101 //=======================================================================
103 BRepAlgo_NormalProjection::BRepAlgo_NormalProjection(const TopoDS_Shape& S)
104 : myWith3d(Standard_True)
107 BB.MakeCompound(TopoDS::Compound(myToProj));
113 //=======================================================================
116 //=======================================================================
118 void BRepAlgo_NormalProjection::Init(const TopoDS_Shape& S)
123 //=======================================================================
126 //=======================================================================
128 void BRepAlgo_NormalProjection::Add(const TopoDS_Shape& ToProj)
131 BB.Add(myToProj, ToProj);
134 //=======================================================================
135 //function : SetParams
137 //=======================================================================
139 void BRepAlgo_NormalProjection::SetParams(const Standard_Real Tol3D,
140 const Standard_Real Tol2D,
141 const GeomAbs_Shape InternalContinuity,
142 const Standard_Integer MaxDegree,
143 const Standard_Integer MaxSeg)
147 myContinuity = InternalContinuity;
148 myMaxDegree = MaxDegree;
152 //=======================================================================
153 //function : SetDefaultParams
155 //=======================================================================
157 void BRepAlgo_NormalProjection::SetDefaultParams()
160 myTol2d = Pow(myTol3d, 2./3);
161 myContinuity = GeomAbs_C2;
166 //=======================================================================
167 //function : SetLimits
169 //=======================================================================
171 void BRepAlgo_NormalProjection::SetLimit(const Standard_Boolean FaceBounds)
173 myFaceBounds = FaceBounds;
176 //=======================================================================
177 //function : SetMaxDistance
179 //=======================================================================
181 void BRepAlgo_NormalProjection::SetMaxDistance(const Standard_Real MaxDist)
186 //=======================================================================
187 //function : Compute3d
189 //=======================================================================
191 void BRepAlgo_NormalProjection::Compute3d(const Standard_Boolean With3d)
196 //=======================================================================
199 //=======================================================================
201 void BRepAlgo_NormalProjection::Build()
203 #ifdef OCCT_DEBUG_CHRONO
204 Standard_Integer init_count = 0, approx_count = 0, booltool_count = 0;
211 init_point_count = 0;
214 dicho_bound_count = 0;
216 InitChron(chr_total);
218 myIsDone = Standard_False;
219 ProjLib_CompProjectedCurve Projector;
220 Handle(TopTools_HSequenceOfShape) Edges = new TopTools_HSequenceOfShape();
221 Handle(TopTools_HSequenceOfShape) Faces = new TopTools_HSequenceOfShape();
222 TopTools_ListOfShape DescenList;
223 Standard_Integer NbEdges = 0, NbFaces = 0, i, j, k;
224 TopExp_Explorer ExpOfWire, ExpOfShape;
225 Standard_Real Udeb, Ufin;
226 TopoDS_Shape VertexRes;
227 Standard_Boolean Only3d, Only2d, Elementary;
229 // for isoparametric cases
230 TColgp_Array1OfPnt2d Poles(1, 2);
231 TColStd_Array1OfReal Knots(1, 2);
232 TColStd_Array1OfInteger Mults(1,2);
233 Standard_Integer Deg;
239 for(ExpOfWire.Init(myToProj, TopAbs_EDGE);
241 ExpOfWire.Next(), NbEdges++) {
242 Edges->Append(ExpOfWire.Current());
245 for(ExpOfShape.Init(myShape, TopAbs_FACE);
247 ExpOfShape.Next(), NbFaces++) {
248 Faces->Append(ExpOfShape.Current());
252 BB.MakeCompound(TopoDS::Compound(myRes));
253 BB.MakeCompound(TopoDS::Compound(VertexRes));
254 Standard_Boolean YaVertexRes = Standard_False;
256 for(i = 1; i <= NbEdges; i++){
258 BRepAdaptor_Curve cur(TopoDS::Edge(Edges->Value(i)));
259 Handle(BRepAdaptor_HCurve) hcur = new BRepAdaptor_HCurve();
261 Elementary = IsElementary(cur);
262 for(j = 1; j <= NbFaces; j++){
263 BRepAdaptor_Surface sur(TopoDS::Face(Faces->Value(j)));
264 Handle(BRepAdaptor_HSurface) hsur = new BRepAdaptor_HSurface();
267 // computation of TolU and TolV
269 Standard_Real TolU, TolV;
271 TolU = hsur->UResolution(myTol3d)/20;
272 TolV = hsur->VResolution(myTol3d)/20;
274 #ifdef OCCT_DEBUG_CHRONO
278 ProjLib_CompProjectedCurve(hsur, hcur, TolU, TolV, myMaxDist);
279 #ifdef OCCT_DEBUG_CHRONO
280 ResultChron(chr_init,t_init);
284 Handle(ProjLib_HCompProjectedCurve) HProjector =
285 new ProjLib_HCompProjectedCurve();
286 HProjector->Set(Projector);
288 Standard_Boolean Degenerated = Standard_False;
289 gp_Pnt2d P2d, Pdeb, Pfin;
291 Standard_Real UIso, VIso;
293 Handle(Adaptor2d_HCurve2d) HPCur;
294 Handle(Geom2d_Curve) PCur2d; // Only for isoparametric projection
296 for(k = 1; k <= Projector.NbCurves(); k++){
297 if(Projector.IsSinglePnt(k, P2d)){
299 cout << "Projection of edge "<<i<<" on face "<<j;
300 cout << " is punctual"<<endl<<endl;
302 Projector.GetSurface()->D0(P2d.X(), P2d.Y(), P);
303 prj = BRepLib_MakeVertex(P).Shape();
304 DescenList.Append(prj);
305 BB.Add(VertexRes, prj);
306 YaVertexRes = Standard_True;
308 myAncestorMap.Bind(prj, Edges->Value(i));
311 Only2d = Only3d = Standard_False;
312 Projector.Bounds(k, Udeb, Ufin);
314 /**************************************************************/
315 if (Projector.IsUIso(k, UIso)) {
317 cout << "Projection of edge "<<i<<" on face "<<j;
318 cout << " is U-isoparametric"<<endl<<endl;
320 Projector.D0(Udeb, Pdeb);
321 Projector.D0(Ufin, Pfin);
326 Handle(Geom2d_BSplineCurve) BS2d =
327 new Geom2d_BSplineCurve(Poles, Knots, Mults, Deg);
328 PCur2d = new Geom2d_TrimmedCurve( BS2d, Udeb, Ufin);
329 HPCur = new Geom2dAdaptor_HCurve(PCur2d);
330 Only3d = Standard_True;
332 else if (Projector.IsVIso(k, VIso)) {
334 cout << "Projection of edge "<<i<<" on face "<<j;
335 cout << " is V-isoparametric"<<endl<<endl;
337 Projector.D0(Udeb, Pdeb);
338 Projector.D0(Ufin, Pfin);
343 Handle(Geom2d_BSplineCurve) BS2d =
344 new Geom2d_BSplineCurve(Poles, Knots, Mults, Deg);
345 PCur2d = new Geom2d_TrimmedCurve(BS2d, Udeb, Ufin);
346 HPCur = new Geom2dAdaptor_HCurve(PCur2d);
347 Only3d = Standard_True;
349 else HPCur = HProjector;
351 if((myWith3d == Standard_False || Elementary) &&
352 (Projector.MaxDistance(k) <= myTol3d) )
353 Only2d = Standard_True;
355 if(Only2d && Only3d) {
356 BRepLib_MakeEdge MKed(GeomAdaptor::MakeCurve(hcur->Curve()),
359 BB.UpdateEdge(TopoDS::Edge(prj),
361 TopoDS::Face(Faces->Value(j)),
363 BB.UpdateVertex(TopExp::FirstVertex(TopoDS::Edge(prj)),myTol3d);
364 BB.UpdateVertex(TopExp::LastVertex(TopoDS::Edge(prj)),myTol3d);
367 #ifdef OCCT_DEBUG_CHRONO
368 InitChron(chr_approx);
370 Approx_CurveOnSurface appr(HPCur, hsur, Udeb, Ufin, myTol3d,
371 myContinuity, myMaxDegree, myMaxSeg,
373 #ifdef OCCT_DEBUG_CHRONO
374 ResultChron(chr_approx,t_approx);
377 cout<<"Approximation.IsDone = "<<appr.IsDone()<<endl;
379 cout<<"MaxError3d = "<<appr.MaxError3d()<<endl<<endl;
381 cout<<"MaxError2dU = "<<appr.MaxError2dU()<<endl;
382 cout<<"MaxError2dV = "<<appr.MaxError2dV()<<endl<<endl;
387 if(!Only3d) PCur2d = appr.Curve2d();
389 BRepLib_MakeEdge MKed(GeomAdaptor::MakeCurve(hcur->Curve()),
394 // It is tested if the solution is not degenerated to set the
395 // flag on edge, one takes several points, checks if the cloud of
396 // points has less diameter than the tolerance 3D
397 Degenerated = Standard_True;
399 Handle(Geom_BSplineCurve) BS3d = appr.Curve3d();
400 gp_Pnt P1(0.,0.,0.),PP; // skl : I change "P" to "PP"
401 Standard_Integer NbPoint,ii ; // skl : I change "i" to "ii"
402 Standard_Real Par,DPar;
403 // start from 3 points to reject non degenerated edges
406 DPar = (BS3d->LastParameter()-BS3d->FirstParameter())/(NbPoint-1);
407 for (ii=0;ii<NbPoint;ii++)
409 Par=BS3d->FirstParameter()+ii*DPar;
411 P1.SetXYZ(P1.XYZ() + PP.XYZ()/NbPoint);
413 for (ii=0;ii<NbPoint && Degenerated ;ii++)
415 Par=BS3d->FirstParameter()+ii*DPar;
417 Dist=P1.Distance(PP);
419 Degenerated = Standard_False;
423 // if the test passes a more exact test with 10 points
425 P1.SetCoord(0.,0.,0.);
427 DPar = (BS3d->LastParameter()-BS3d->FirstParameter())/(NbPoint-1);
428 for (ii=0;ii<NbPoint;ii++)
430 Par=BS3d->FirstParameter()+ii*DPar;
432 P1.SetXYZ(P1.XYZ() + PP.XYZ()/NbPoint);
434 for (ii=0;ii<NbPoint && Degenerated ;ii++)
436 Par=BS3d->FirstParameter()+ii*DPar;
438 Dist=P1.Distance(PP);
440 Degenerated = Standard_False;
447 cout << "Projection of edge "<<i<<" on face "<<j;
448 cout << " is degenerated "<<endl<<endl;
452 BB.UpdateVertex(VV,P1,myTol3d);
453 BB.MakeEdge(TopoDS::Edge(prj));
454 BB.Add(TopoDS::Edge(prj),VV.Oriented(TopAbs_FORWARD));
455 BB.Add(TopoDS::Edge(prj),VV.Oriented(TopAbs_REVERSED));
456 BB.Degenerated(TopoDS::Edge(prj), Standard_True);
459 prj = BRepLib_MakeEdge(BS3d).Edge();
463 BB.UpdateEdge(TopoDS::Edge(prj),
465 TopoDS::Face(Faces->Value(j)),
467 BB.UpdateVertex(TopExp::FirstVertex(TopoDS::Edge(prj)),appr.MaxError3d());
468 BB.UpdateVertex(TopExp::LastVertex(TopoDS::Edge(prj)),appr.MaxError3d());
470 BB.Range(TopoDS::Edge(prj),
471 TopoDS::Face(Faces->Value(j)),
477 // Trimming edges by face bounds
478 // if the solution is degenerated, use of BoolTool is avoided
479 #ifdef OCCT_DEBUG_CHRONO
480 InitChron(chr_booltool);
483 BRepAlgo_BooleanOperations BoolTool;
484 BoolTool.Shapes2d(Faces->Value(j),prj);
486 Handle(TopOpeBRepBuild_HBuilder) HB;
487 TopTools_ListOfShape LS;
488 TopTools_ListIteratorOfListOfShape Iter;
489 HB = BoolTool.Builder();
491 if (HB->IsSplit(prj, TopAbs_IN))
492 LS = HB->Splits(prj, TopAbs_IN);
496 cout << " BooleanOperations :" << Iter.More()<<" solutions " << endl;
498 for(; Iter.More(); Iter.Next()) {
499 BB.Add(myRes, Iter.Value());
500 myAncestorMap.Bind(Iter.Value(), Edges->Value(i));
501 myCorresp.Bind(Iter.Value(),Faces->Value(j));
507 BRepTopAdaptor_FClass2d classifier(TopoDS::Face(Faces->Value(j)),
508 Precision::Confusion());
510 Standard_Real f = PCur2d->FirstParameter();
511 Standard_Real l = PCur2d->LastParameter();
512 Standard_Real pmil = (f + l )/2;
513 PCur2d->D0(pmil, Puv);
515 state = classifier.Perform(Puv);
516 if(state == TopAbs_IN || state == TopAbs_ON) {
518 DescenList.Append(prj);
519 myAncestorMap.Bind(prj, Edges->Value(i));
520 myCorresp.Bind(prj, Faces->Value(j));
526 cout << " BooleanOperations : no solution " << endl;
529 BRepTopAdaptor_FClass2d classifier(TopoDS::Face(Faces->Value(j)),
530 Precision::Confusion());
532 Standard_Real f = PCur2d->FirstParameter();
533 Standard_Real l = PCur2d->LastParameter();
534 Standard_Real pmil = (f + l )/2;
535 PCur2d->D0(pmil, Puv);
537 state = classifier.Perform(Puv);
538 if(state == TopAbs_IN || state == TopAbs_ON) {
540 DescenList.Append(prj);
541 myAncestorMap.Bind(prj, Edges->Value(i));
542 myCorresp.Bind(prj, Faces->Value(j));
544 #ifdef OCCT_DEBUG_CHRONO
545 ResultChron(chr_booltool,t_booltool);
552 DescenList.Append(prj);
553 myAncestorMap.Bind(prj, Edges->Value(i));
554 myCorresp.Bind(prj, Faces->Value(j));
559 myDescendants.Bind(Edges->Value(i), DescenList);
561 // JPI : eventual wire creation is reported in a specific method
562 // BuilWire that can be called by the user. Otherwise, the
563 // relations of map myAncestorMap, myCorresp will be lost.
565 if(YaVertexRes) BB.Add(myRes, VertexRes);
567 myIsDone = Standard_True;
569 #ifdef OCCT_DEBUG_CHRONO
570 ResultChron(chr_total,t_total);
572 cout<<"Build - Total time : "<<t_total<<" includes:" <<endl;
573 cout<<"- Projection : "<<t_init<<endl;
574 cout<<" -- Initial point search : "<<t_init_point<<endl;
575 cout<<" -- DichoBound search : "<<t_dicho_bound<<endl;
576 cout<<"- Approximation : "<<t_approx<<endl;
577 cout<<"- Boolean operation : "<<t_booltool<<endl;
578 cout<<"- Rest of time : "<<t_total-(t_init + t_approx + t_booltool )<<endl<<endl;
580 t_init /= init_count;
581 if (init_point_count != 0)
582 t_init_point /= init_point_count;
583 if (dicho_bound_count != 0)
584 t_dicho_bound /= dicho_bound_count;
585 if (approx_count != 0)
586 t_approx /= approx_count;
587 if (booltool_count != 0)
588 t_booltool /= booltool_count;
590 cout<<"Unitary average time : "<<endl;
591 cout<<"- Projection : "<<t_init<<endl;
592 cout<<" -- Initial point search: "<<t_init_point<<endl;
593 cout<<" -- DichoBound search : "<<t_dicho_bound<<endl;
594 cout<<"- Approximation : "<<t_approx<<endl;
595 cout<<"- Boolean operation :"<<t_booltool<<endl;
596 cout<<endl<<"Number of initial point computations is "<<init_point_count<<endl<<endl;
600 //=======================================================================
603 //=======================================================================
605 Standard_Boolean BRepAlgo_NormalProjection::IsDone() const
610 //=======================================================================
611 //function : Projection
613 //=======================================================================
615 const TopoDS_Shape& BRepAlgo_NormalProjection::Projection() const
620 //=======================================================================
621 //function : Ancestor
623 //=======================================================================
625 const TopoDS_Shape& BRepAlgo_NormalProjection::Ancestor(const TopoDS_Edge& E) const
627 return myAncestorMap.Find(E);
630 //=======================================================================
633 //=======================================================================
635 const TopoDS_Shape& BRepAlgo_NormalProjection::Couple(const TopoDS_Edge& E) const
637 return myCorresp.Find(E);
640 //=======================================================================
641 //function : Generated
643 //=======================================================================
645 const TopTools_ListOfShape& BRepAlgo_NormalProjection::Generated(const TopoDS_Shape& S)
647 return myDescendants.Find(S);
650 //=======================================================================
651 //function : IsElementary
653 //=======================================================================
655 Standard_Boolean BRepAlgo_NormalProjection::IsElementary(const Adaptor3d_Curve& C) const
657 GeomAbs_CurveType type;
662 case GeomAbs_Ellipse:
663 case GeomAbs_Hyperbola:
664 case GeomAbs_Parabola: return Standard_True;
665 default: return Standard_False;
668 //=======================================================================
669 //function : BuildWire
671 //=======================================================================
673 Standard_Boolean BRepAlgo_NormalProjection::BuildWire(TopTools_ListOfShape& ListOfWire) const
675 TopExp_Explorer ExpOfWire, ExpOfShape;
676 Standard_Boolean IsWire=Standard_False;
677 ExpOfShape.Init(myRes, TopAbs_EDGE);
678 if(ExpOfShape.More())
680 TopTools_ListOfShape List;
682 for ( ; ExpOfShape.More(); ExpOfShape.Next())
684 const TopoDS_Shape& CurE = ExpOfShape.Current();
691 const TopoDS_Shape& Wire = MW.Shape();
692 // If the resulting wire contains the same edge as at the beginning OK
693 // otherwise the result really consists of several wires.
694 TopExp_Explorer exp2(Wire,TopAbs_EDGE);
695 Standard_Integer NbEdges = 0;
696 for (;exp2.More(); exp2.Next()) NbEdges++;
697 if ( NbEdges == List.Extent())
699 ListOfWire.Append(Wire);
700 IsWire = Standard_True;