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_NormalProjection.hxx>
26 #include <BRepAlgo_SequenceOfSequenceOfInteger.hxx>
27 #include <BRepAlgoAPI_Common.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 <TopTools_HSequenceOfShape.hxx>
55 #include <TopTools_ListIteratorOfListOfShape.hxx>
57 #ifdef OCCT_DEBUG_CHRONO
58 #include <OSD_Timer.hxx>
60 OSD_Chronometer chr_total, chr_init, chr_approx, chr_booltool;
62 Standard_Real t_total, t_init, t_approx, t_booltool;
63 Standard_IMPORT Standard_Real t_init_point, t_dicho_bound;
64 Standard_IMPORT Standard_Integer init_point_count, dicho_bound_count;
66 void InitChron(OSD_Chronometer& ch)
72 void ResultChron( OSD_Chronometer & ch, Standard_Real & time)
81 //=======================================================================
82 //function : BRepAlgo_NormalProjection
84 //=======================================================================
86 BRepAlgo_NormalProjection::BRepAlgo_NormalProjection()
87 : myWith3d(Standard_True)
91 BB.MakeCompound(TopoDS::Compound(myToProj));
92 myFaceBounds=Standard_True;
97 //=======================================================================
98 //function : BRepAlgo_NormalProjection
100 //=======================================================================
102 BRepAlgo_NormalProjection::BRepAlgo_NormalProjection(const TopoDS_Shape& S)
103 : myWith3d(Standard_True)
106 BB.MakeCompound(TopoDS::Compound(myToProj));
112 //=======================================================================
115 //=======================================================================
117 void BRepAlgo_NormalProjection::Init(const TopoDS_Shape& S)
122 //=======================================================================
125 //=======================================================================
127 void BRepAlgo_NormalProjection::Add(const TopoDS_Shape& ToProj)
130 BB.Add(myToProj, ToProj);
133 //=======================================================================
134 //function : SetParams
136 //=======================================================================
138 void BRepAlgo_NormalProjection::SetParams(const Standard_Real Tol3D,
139 const Standard_Real Tol2D,
140 const GeomAbs_Shape InternalContinuity,
141 const Standard_Integer MaxDegree,
142 const Standard_Integer MaxSeg)
146 myContinuity = InternalContinuity;
147 myMaxDegree = MaxDegree;
151 //=======================================================================
152 //function : SetDefaultParams
154 //=======================================================================
156 void BRepAlgo_NormalProjection::SetDefaultParams()
159 myTol2d = Pow(myTol3d, 2./3);
160 myContinuity = GeomAbs_C2;
165 //=======================================================================
166 //function : SetLimits
168 //=======================================================================
170 void BRepAlgo_NormalProjection::SetLimit(const Standard_Boolean FaceBounds)
172 myFaceBounds = FaceBounds;
175 //=======================================================================
176 //function : SetMaxDistance
178 //=======================================================================
180 void BRepAlgo_NormalProjection::SetMaxDistance(const Standard_Real MaxDist)
185 //=======================================================================
186 //function : Compute3d
188 //=======================================================================
190 void BRepAlgo_NormalProjection::Compute3d(const Standard_Boolean With3d)
195 //=======================================================================
198 //=======================================================================
200 void BRepAlgo_NormalProjection::Build()
202 #ifdef OCCT_DEBUG_CHRONO
203 Standard_Integer init_count = 0, approx_count = 0, booltool_count = 0;
210 init_point_count = 0;
213 dicho_bound_count = 0;
215 InitChron(chr_total);
217 myIsDone = Standard_False;
218 ProjLib_CompProjectedCurve Projector;
219 Handle(TopTools_HSequenceOfShape) Edges = new TopTools_HSequenceOfShape();
220 Handle(TopTools_HSequenceOfShape) Faces = new TopTools_HSequenceOfShape();
221 TopTools_ListOfShape DescenList;
222 Standard_Integer NbEdges = 0, NbFaces = 0, i, j, k;
223 TopExp_Explorer ExpOfWire, ExpOfShape;
224 Standard_Real Udeb, Ufin;
225 TopoDS_Shape VertexRes;
226 Standard_Boolean Only3d, Only2d, Elementary;
228 // for isoparametric cases
229 TColgp_Array1OfPnt2d Poles(1, 2);
230 TColStd_Array1OfReal Knots(1, 2);
231 TColStd_Array1OfInteger Mults(1,2);
232 Standard_Integer Deg;
238 for(ExpOfWire.Init(myToProj, TopAbs_EDGE);
240 ExpOfWire.Next(), NbEdges++) {
241 Edges->Append(ExpOfWire.Current());
244 for(ExpOfShape.Init(myShape, TopAbs_FACE);
246 ExpOfShape.Next(), NbFaces++) {
247 Faces->Append(ExpOfShape.Current());
251 BB.MakeCompound(TopoDS::Compound(myRes));
252 BB.MakeCompound(TopoDS::Compound(VertexRes));
253 Standard_Boolean YaVertexRes = Standard_False;
255 for(i = 1; i <= NbEdges; i++){
257 BRepAdaptor_Curve cur(TopoDS::Edge(Edges->Value(i)));
258 Handle(BRepAdaptor_HCurve) hcur = new BRepAdaptor_HCurve();
260 Elementary = IsElementary(cur);
261 for(j = 1; j <= NbFaces; j++){
262 BRepAdaptor_Surface sur(TopoDS::Face(Faces->Value(j)));
263 Handle(BRepAdaptor_HSurface) hsur = new BRepAdaptor_HSurface();
266 // computation of TolU and TolV
268 Standard_Real TolU, TolV;
270 TolU = hsur->UResolution(myTol3d)/20;
271 TolV = hsur->VResolution(myTol3d)/20;
273 #ifdef OCCT_DEBUG_CHRONO
277 ProjLib_CompProjectedCurve(hsur, hcur, TolU, TolV, myMaxDist);
278 #ifdef OCCT_DEBUG_CHRONO
279 ResultChron(chr_init,t_init);
283 Handle(ProjLib_HCompProjectedCurve) HProjector =
284 new ProjLib_HCompProjectedCurve();
285 HProjector->Set(Projector);
287 Standard_Boolean Degenerated = Standard_False;
288 gp_Pnt2d P2d, Pdeb, Pfin;
290 Standard_Real UIso, VIso;
292 Handle(Adaptor2d_HCurve2d) HPCur;
293 Handle(Geom2d_Curve) PCur2d; // Only for isoparametric projection
295 for(k = 1; k <= Projector.NbCurves(); k++){
296 if(Projector.IsSinglePnt(k, P2d)){
298 cout << "Projection of edge "<<i<<" on face "<<j;
299 cout << " is punctual"<<endl<<endl;
301 Projector.GetSurface()->D0(P2d.X(), P2d.Y(), P);
302 prj = BRepLib_MakeVertex(P).Shape();
303 DescenList.Append(prj);
304 BB.Add(VertexRes, prj);
305 YaVertexRes = Standard_True;
307 myAncestorMap.Bind(prj, Edges->Value(i));
310 Only2d = Only3d = Standard_False;
311 Projector.Bounds(k, Udeb, Ufin);
313 /**************************************************************/
314 if (Projector.IsUIso(k, UIso)) {
316 cout << "Projection of edge "<<i<<" on face "<<j;
317 cout << " is U-isoparametric"<<endl<<endl;
319 Projector.D0(Udeb, Pdeb);
320 Projector.D0(Ufin, Pfin);
325 Handle(Geom2d_BSplineCurve) BS2d =
326 new Geom2d_BSplineCurve(Poles, Knots, Mults, Deg);
327 PCur2d = new Geom2d_TrimmedCurve( BS2d, Udeb, Ufin);
328 HPCur = new Geom2dAdaptor_HCurve(PCur2d);
329 Only3d = Standard_True;
331 else if (Projector.IsVIso(k, VIso)) {
333 cout << "Projection of edge "<<i<<" on face "<<j;
334 cout << " is V-isoparametric"<<endl<<endl;
336 Projector.D0(Udeb, Pdeb);
337 Projector.D0(Ufin, Pfin);
342 Handle(Geom2d_BSplineCurve) BS2d =
343 new Geom2d_BSplineCurve(Poles, Knots, Mults, Deg);
344 PCur2d = new Geom2d_TrimmedCurve(BS2d, Udeb, Ufin);
345 HPCur = new Geom2dAdaptor_HCurve(PCur2d);
346 Only3d = Standard_True;
348 else HPCur = HProjector;
350 if((myWith3d == Standard_False || Elementary) &&
351 (Projector.MaxDistance(k) <= myTol3d) )
352 Only2d = Standard_True;
354 if(Only2d && Only3d) {
355 BRepLib_MakeEdge MKed(GeomAdaptor::MakeCurve(hcur->Curve()),
358 BB.UpdateEdge(TopoDS::Edge(prj),
360 TopoDS::Face(Faces->Value(j)),
362 BB.UpdateVertex(TopExp::FirstVertex(TopoDS::Edge(prj)),myTol3d);
363 BB.UpdateVertex(TopExp::LastVertex(TopoDS::Edge(prj)),myTol3d);
366 #ifdef OCCT_DEBUG_CHRONO
367 InitChron(chr_approx);
369 Approx_CurveOnSurface appr(HPCur, hsur, Udeb, Ufin, myTol3d,
370 myContinuity, myMaxDegree, myMaxSeg,
372 #ifdef OCCT_DEBUG_CHRONO
373 ResultChron(chr_approx,t_approx);
376 cout<<"Approximation.IsDone = "<<appr.IsDone()<<endl;
378 cout<<"MaxError3d = "<<appr.MaxError3d()<<endl<<endl;
380 cout<<"MaxError2dU = "<<appr.MaxError2dU()<<endl;
381 cout<<"MaxError2dV = "<<appr.MaxError2dV()<<endl<<endl;
386 if(!Only3d) PCur2d = appr.Curve2d();
388 BRepLib_MakeEdge MKed(GeomAdaptor::MakeCurve(hcur->Curve()),
393 // It is tested if the solution is not degenerated to set the
394 // flag on edge, one takes several points, checks if the cloud of
395 // points has less diameter than the tolerance 3D
396 Degenerated = Standard_True;
398 Handle(Geom_BSplineCurve) BS3d = appr.Curve3d();
399 gp_Pnt P1(0.,0.,0.),PP; // skl : I change "P" to "PP"
400 Standard_Integer NbPoint,ii ; // skl : I change "i" to "ii"
401 Standard_Real Par,DPar;
402 // start from 3 points to reject non degenerated edges
405 DPar = (BS3d->LastParameter()-BS3d->FirstParameter())/(NbPoint-1);
406 for (ii=0;ii<NbPoint;ii++)
408 Par=BS3d->FirstParameter()+ii*DPar;
410 P1.SetXYZ(P1.XYZ() + PP.XYZ()/NbPoint);
412 for (ii=0;ii<NbPoint && Degenerated ;ii++)
414 Par=BS3d->FirstParameter()+ii*DPar;
416 Dist=P1.Distance(PP);
418 Degenerated = Standard_False;
422 // if the test passes a more exact test with 10 points
424 P1.SetCoord(0.,0.,0.);
426 DPar = (BS3d->LastParameter()-BS3d->FirstParameter())/(NbPoint-1);
427 for (ii=0;ii<NbPoint;ii++)
429 Par=BS3d->FirstParameter()+ii*DPar;
431 P1.SetXYZ(P1.XYZ() + PP.XYZ()/NbPoint);
433 for (ii=0;ii<NbPoint && Degenerated ;ii++)
435 Par=BS3d->FirstParameter()+ii*DPar;
437 Dist=P1.Distance(PP);
439 Degenerated = Standard_False;
446 cout << "Projection of edge "<<i<<" on face "<<j;
447 cout << " is degenerated "<<endl<<endl;
451 BB.UpdateVertex(VV,P1,myTol3d);
452 BB.MakeEdge(TopoDS::Edge(prj));
453 BB.Add(TopoDS::Edge(prj),VV.Oriented(TopAbs_FORWARD));
454 BB.Add(TopoDS::Edge(prj),VV.Oriented(TopAbs_REVERSED));
455 BB.Degenerated(TopoDS::Edge(prj), Standard_True);
458 prj = BRepLib_MakeEdge(BS3d).Edge();
462 BB.UpdateEdge(TopoDS::Edge(prj),
464 TopoDS::Face(Faces->Value(j)),
466 BB.UpdateVertex(TopExp::FirstVertex(TopoDS::Edge(prj)),appr.MaxError3d());
467 BB.UpdateVertex(TopExp::LastVertex(TopoDS::Edge(prj)),appr.MaxError3d());
469 BB.Range(TopoDS::Edge(prj),
470 TopoDS::Face(Faces->Value(j)),
476 // Trimming edges by face bounds
477 // if the solution is degenerated, use of BoolTool is avoided
478 #ifdef OCCT_DEBUG_CHRONO
479 InitChron(chr_booltool);
482 // Perform Boolean COMMON operation to get parts of projected edge
484 BRepAlgoAPI_Common aCommon(Faces->Value(j), prj);
485 if (aCommon.IsDone()) {
486 const TopoDS_Shape& aRC = aCommon.Shape();
488 TopExp_Explorer aExpE(aRC, TopAbs_EDGE);
489 for (; aExpE.More(); aExpE.Next()) {
490 const TopoDS_Shape& aE = aExpE.Current();
492 myAncestorMap.Bind(aE, Edges->Value(i));
493 myCorresp.Bind(aE, Faces->Value(j));
497 // if the common operation has failed, try to classify the part
498 BRepTopAdaptor_FClass2d classifier(TopoDS::Face(Faces->Value(j)),
499 Precision::Confusion());
501 Standard_Real f = PCur2d->FirstParameter();
502 Standard_Real l = PCur2d->LastParameter();
503 Standard_Real pmil = (f + l )/2;
504 PCur2d->D0(pmil, Puv);
506 state = classifier.Perform(Puv);
507 if(state == TopAbs_IN || state == TopAbs_ON) {
509 DescenList.Append(prj);
510 myAncestorMap.Bind(prj, Edges->Value(i));
511 myCorresp.Bind(prj, Faces->Value(j));
517 cout << " BooleanOperations : no solution " << endl;
520 BRepTopAdaptor_FClass2d classifier(TopoDS::Face(Faces->Value(j)),
521 Precision::Confusion());
523 Standard_Real f = PCur2d->FirstParameter();
524 Standard_Real l = PCur2d->LastParameter();
525 Standard_Real pmil = (f + l )/2;
526 PCur2d->D0(pmil, Puv);
528 state = classifier.Perform(Puv);
529 if(state == TopAbs_IN || state == TopAbs_ON) {
531 DescenList.Append(prj);
532 myAncestorMap.Bind(prj, Edges->Value(i));
533 myCorresp.Bind(prj, Faces->Value(j));
535 #ifdef OCCT_DEBUG_CHRONO
536 ResultChron(chr_booltool,t_booltool);
543 DescenList.Append(prj);
544 myAncestorMap.Bind(prj, Edges->Value(i));
545 myCorresp.Bind(prj, Faces->Value(j));
550 myDescendants.Bind(Edges->Value(i), DescenList);
552 // JPI : eventual wire creation is reported in a specific method
553 // BuilWire that can be called by the user. Otherwise, the
554 // relations of map myAncestorMap, myCorresp will be lost.
556 if(YaVertexRes) BB.Add(myRes, VertexRes);
558 myIsDone = Standard_True;
560 #ifdef OCCT_DEBUG_CHRONO
561 ResultChron(chr_total,t_total);
563 cout<<"Build - Total time : "<<t_total<<" includes:" <<endl;
564 cout<<"- Projection : "<<t_init<<endl;
565 cout<<" -- Initial point search : "<<t_init_point<<endl;
566 cout<<" -- DichoBound search : "<<t_dicho_bound<<endl;
567 cout<<"- Approximation : "<<t_approx<<endl;
568 cout<<"- Boolean operation : "<<t_booltool<<endl;
569 cout<<"- Rest of time : "<<t_total-(t_init + t_approx + t_booltool )<<endl<<endl;
571 t_init /= init_count;
572 if (init_point_count != 0)
573 t_init_point /= init_point_count;
574 if (dicho_bound_count != 0)
575 t_dicho_bound /= dicho_bound_count;
576 if (approx_count != 0)
577 t_approx /= approx_count;
578 if (booltool_count != 0)
579 t_booltool /= booltool_count;
581 cout<<"Unitary average time : "<<endl;
582 cout<<"- Projection : "<<t_init<<endl;
583 cout<<" -- Initial point search: "<<t_init_point<<endl;
584 cout<<" -- DichoBound search : "<<t_dicho_bound<<endl;
585 cout<<"- Approximation : "<<t_approx<<endl;
586 cout<<"- Boolean operation :"<<t_booltool<<endl;
587 cout<<endl<<"Number of initial point computations is "<<init_point_count<<endl<<endl;
591 //=======================================================================
594 //=======================================================================
596 Standard_Boolean BRepAlgo_NormalProjection::IsDone() const
601 //=======================================================================
602 //function : Projection
604 //=======================================================================
606 const TopoDS_Shape& BRepAlgo_NormalProjection::Projection() const
611 //=======================================================================
612 //function : Ancestor
614 //=======================================================================
616 const TopoDS_Shape& BRepAlgo_NormalProjection::Ancestor(const TopoDS_Edge& E) const
618 return myAncestorMap.Find(E);
621 //=======================================================================
624 //=======================================================================
626 const TopoDS_Shape& BRepAlgo_NormalProjection::Couple(const TopoDS_Edge& E) const
628 return myCorresp.Find(E);
631 //=======================================================================
632 //function : Generated
634 //=======================================================================
636 const TopTools_ListOfShape& BRepAlgo_NormalProjection::Generated(const TopoDS_Shape& S)
638 return myDescendants.Find(S);
641 //=======================================================================
642 //function : IsElementary
644 //=======================================================================
646 Standard_Boolean BRepAlgo_NormalProjection::IsElementary(const Adaptor3d_Curve& C) const
648 GeomAbs_CurveType type;
653 case GeomAbs_Ellipse:
654 case GeomAbs_Hyperbola:
655 case GeomAbs_Parabola: return Standard_True;
656 default: return Standard_False;
659 //=======================================================================
660 //function : BuildWire
662 //=======================================================================
664 Standard_Boolean BRepAlgo_NormalProjection::BuildWire(TopTools_ListOfShape& ListOfWire) const
666 TopExp_Explorer ExpOfWire, ExpOfShape;
667 Standard_Boolean IsWire=Standard_False;
668 ExpOfShape.Init(myRes, TopAbs_EDGE);
669 if(ExpOfShape.More())
671 TopTools_ListOfShape List;
673 for ( ; ExpOfShape.More(); ExpOfShape.Next())
675 const TopoDS_Shape& CurE = ExpOfShape.Current();
682 const TopoDS_Shape& Wire = MW.Shape();
683 // If the resulting wire contains the same edge as at the beginning OK
684 // otherwise the result really consists of several wires.
685 TopExp_Explorer exp2(Wire,TopAbs_EDGE);
686 Standard_Integer NbEdges = 0;
687 for (;exp2.More(); exp2.Next()) NbEdges++;
688 if ( NbEdges == List.Extent())
690 ListOfWire.Append(Wire);
691 IsWire = Standard_True;