1 // Created on: 1993-12-15
2 // Created by: Remi LEQUETTE
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.
17 //pmn 26/09/97 Add parameters of approximation in BuildCurve3d
18 // Modified by skv - Thu Jun 3 12:39:19 2004 OCC5898
20 #include <BRepLib.ixx>
21 #include <BRepAdaptor_Surface.hxx>
22 #include <BRepAdaptor_HSurface.hxx>
23 #include <BRepAdaptor_HCurve2d.hxx>
24 #include <BRep_Tool.hxx>
25 #include <BRep_Builder.hxx>
26 #include <Geom_Surface.hxx>
27 #include <Geom_RectangularTrimmedSurface.hxx>
28 #include <Geom_Plane.hxx>
29 #include <Geom_Curve.hxx>
30 #include <Geom_BSplineCurve.hxx>
31 #include <Geom_TrimmedCurve.hxx>
32 #include <Geom2d_Curve.hxx>
33 #include <Geom2d_TrimmedCurve.hxx>
34 #include <Geom2d_BSplineCurve.hxx>
35 #include <GeomLib.hxx>
36 #include <TopExp_Explorer.hxx>
38 #include <TopoDS_Edge.hxx>
39 #include <TopoDS_Face.hxx>
40 #include <TopoDS_Vertex.hxx>
41 #include <TopTools_MapOfShape.hxx>
42 #include <TopTools_IndexedDataMapOfShapeListOfShape.hxx>
43 #include <TopTools_ListIteratorOfListOfShape.hxx>
48 #include <Standard_Real.hxx>
49 #include <Precision.hxx>
50 #include <BRep_GCurve.hxx>
51 #include <BRep_TEdge.hxx>
52 #include <BRep_TFace.hxx>
53 #include <AppParCurves_MultiCurve.hxx>
54 #include <AppParCurves_MultiBSpCurve.hxx>
55 #include <BRep_ListOfCurveRepresentation.hxx>
56 #include <BRep_CurveRepresentation.hxx>
57 #include <BRep_ListIteratorOfListOfCurveRepresentation.hxx>
58 #include <BRep_TVertex.hxx>
59 #include <AdvApprox_ApproxAFunction.hxx>
60 #include <Approx_SameParameter.hxx>
61 #include <TColgp_Array1OfPnt.hxx>
62 #include <TColgp_Array1OfPnt2d.hxx>
63 #include <TColStd_Array1OfReal.hxx>
64 #include <TColStd_MapOfTransient.hxx>
65 #include <GeomAdaptor_Curve.hxx>
66 #include <GeomAdaptor_HCurve.hxx>
67 #include <GeomAdaptor_Surface.hxx>
68 #include <GeomAdaptor_HSurface.hxx>
69 #include <Geom2dAdaptor_Curve.hxx>
70 #include <Geom2dAdaptor_HCurve.hxx>
71 #include <Geom2dAdaptor.hxx>
72 #include <Geom2dConvert.hxx>
73 #include <GCPnts_QuasiUniformDeflection.hxx>
74 #include <BSplCLib.hxx>
76 #include <Adaptor3d_CurveOnSurface.hxx>
77 #include <Extrema_LocateExtPC.hxx>
78 #include <ProjLib_ProjectedCurve.hxx>
79 #include <BRepClass3d_SolidClassifier.hxx>
80 #include <Bnd_Box.hxx>
81 #include <BRepBndLib.hxx>
82 #include <Approx_CurvilinearParameter.hxx>
83 #include <Geom_BSplineSurface.hxx>
85 #include <Poly_Triangulation.hxx>
86 #include <TShort_HArray1OfShortReal.hxx>
87 #include <GeomLProp_SLProps.hxx>
88 #include <Poly_PolygonOnTriangulation.hxx>
90 // TODO - not thread-safe static variables
91 static Standard_Real thePrecision = Precision::Confusion();
92 static Handle(Geom_Plane) thePlane;
94 //=======================================================================
95 //function : Precision
97 //=======================================================================
99 void BRepLib::Precision(const Standard_Real P)
104 //=======================================================================
105 //function : Precision
107 //=======================================================================
109 Standard_Real BRepLib::Precision()
114 //=======================================================================
117 //=======================================================================
119 void BRepLib::Plane(const Handle(Geom_Plane)& P)
125 //=======================================================================
128 //=======================================================================
130 const Handle(Geom_Plane)& BRepLib::Plane()
132 if (thePlane.IsNull()) thePlane = new Geom_Plane(gp::XOY());
135 //=======================================================================
136 //function : CheckSameRange
138 //=======================================================================
140 Standard_Boolean BRepLib::CheckSameRange(const TopoDS_Edge& AnEdge,
141 const Standard_Real Tolerance)
143 Standard_Boolean IsSameRange = Standard_True,
144 first_time_in = Standard_True ;
146 BRep_ListIteratorOfListOfCurveRepresentation an_Iterator
147 ((*((Handle(BRep_TEdge)*)&AnEdge.TShape()))->ChangeCurves());
149 Standard_Real first, last;
150 Standard_Real current_first =0., current_last =0. ;
151 Handle(BRep_GCurve) geometric_representation_ptr ;
153 while (IsSameRange && an_Iterator.More()) {
154 geometric_representation_ptr =
155 Handle(BRep_GCurve)::DownCast(an_Iterator.Value());
156 if (!geometric_representation_ptr.IsNull()) {
158 first = geometric_representation_ptr->First();
159 last = geometric_representation_ptr->Last();
160 if (first_time_in ) {
161 current_first = first ;
162 current_last = last ;
163 first_time_in = Standard_False ;
166 IsSameRange = (Abs(current_first - first) <= Tolerance)
167 && (Abs(current_last -last) <= Tolerance ) ;
175 //=======================================================================
176 //function : SameRange
178 //=======================================================================
180 void BRepLib::SameRange(const TopoDS_Edge& AnEdge,
181 const Standard_Real Tolerance)
183 BRep_ListIteratorOfListOfCurveRepresentation an_Iterator
184 ((*((Handle(BRep_TEdge)*)&AnEdge.TShape()))->ChangeCurves());
186 Handle(Geom2d_Curve) Curve2dPtr, Curve2dPtr2, NewCurve2dPtr, NewCurve2dPtr2;
187 TopLoc_Location LocalLoc ;
189 Standard_Boolean first_time_in = Standard_True,
192 Handle(BRep_GCurve) geometric_representation_ptr ;
198 const Handle(Geom_Curve) C = BRep_Tool::Curve(AnEdge,
203 first_time_in = Standard_False ;
206 while (an_Iterator.More()) {
207 geometric_representation_ptr =
208 Handle(BRep_GCurve)::DownCast(an_Iterator.Value());
209 if (! geometric_representation_ptr.IsNull()) {
211 has_curve = Standard_False ;
212 first = geometric_representation_ptr->First();
213 last = geometric_representation_ptr->Last();
214 if (geometric_representation_ptr->IsCurveOnSurface()) {
215 Curve2dPtr = geometric_representation_ptr->PCurve() ;
216 has_curve = Standard_True ;
218 if (geometric_representation_ptr->IsCurveOnClosedSurface()) {
219 Curve2dPtr2 = geometric_representation_ptr->PCurve2() ;
220 has_closed_curve = Standard_True ;
222 if (has_curve || has_closed_curve) {
224 current_first = first ;
225 current_last = last ;
226 first_time_in = Standard_False ;
229 if (Abs(first - current_first) > Precision::Confusion() ||
230 Abs(last - current_last) > Precision::Confusion() )
234 GeomLib::SameRange(Tolerance,
236 geometric_representation_ptr->First(),
237 geometric_representation_ptr->Last(),
241 geometric_representation_ptr->PCurve(NewCurve2dPtr) ;
243 if (has_closed_curve)
245 GeomLib::SameRange(Tolerance,
247 geometric_representation_ptr->First(),
248 geometric_representation_ptr->Last(),
252 geometric_representation_ptr->PCurve2(NewCurve2dPtr2) ;
260 B.Range(TopoDS::Edge(AnEdge),
268 //=======================================================================
269 //function : EvaluateMaxSegment
270 //purpose : return MaxSegment to pass in approximation, if MaxSegment==0 provided
271 //=======================================================================
273 static Standard_Integer evaluateMaxSegment(const Standard_Integer aMaxSegment,
274 const Adaptor3d_CurveOnSurface& aCurveOnSurface)
276 if (aMaxSegment != 0) return aMaxSegment;
278 Handle(Adaptor3d_HSurface) aSurf = aCurveOnSurface.GetSurface();
279 Handle(Adaptor2d_HCurve2d) aCurv2d = aCurveOnSurface.GetCurve();
281 Standard_Real aNbSKnots = 0, aNbC2dKnots = 0;
283 if (aSurf->GetType() == GeomAbs_BSplineSurface) {
284 Handle(Geom_BSplineSurface) aBSpline = aSurf->BSpline();
285 aNbSKnots = Max(aBSpline->NbUKnots(), aBSpline->NbVKnots());
287 if (aCurv2d->GetType() == GeomAbs_BSplineCurve) {
288 aNbC2dKnots = aCurv2d->NbKnots();
290 Standard_Integer aReturn = (Standard_Integer) ( 30 + Max(aNbSKnots, aNbC2dKnots) ) ;
294 //=======================================================================
295 //function : BuildCurve3d
297 //=======================================================================
299 Standard_Boolean BRepLib::BuildCurve3d(const TopoDS_Edge& AnEdge,
300 const Standard_Real Tolerance,
301 const GeomAbs_Shape Continuity,
302 const Standard_Integer MaxDegree,
303 const Standard_Integer MaxSegment)
305 Standard_Integer //ErrorCode,
311 TopLoc_Location LocalLoc,L[2],LC;
312 Standard_Real f,l,fc,lc, first[2], last[2],
316 Handle(Geom2d_Curve) Curve2dPtr, Curve2dArray[2] ;
317 Handle(Geom_Surface) SurfacePtr, SurfaceArray[2] ;
319 Standard_Integer not_done ;
320 // if the edge has a 3d curve returns true
323 const Handle(Geom_Curve) C = BRep_Tool::Curve(AnEdge,LocalLoc,f,l);
325 return Standard_True;
327 // this should not exists but UpdateEdge makes funny things
328 // if the edge is not same range
330 if (! CheckSameRange(AnEdge,
331 Precision::Confusion())) {
338 // search a curve on a plane
339 Handle(Geom_Surface) S;
340 Handle(Geom2d_Curve) PC;
341 Standard_Integer i = 0;
342 Handle(Geom_Plane) P;
347 BRep_Tool::CurveOnSurface(AnEdge,PC,S,LocalLoc,f,l,i);
348 Handle(Geom_RectangularTrimmedSurface) RT =
349 Handle(Geom_RectangularTrimmedSurface)::DownCast(S);
351 P = Handle(Geom_Plane)::DownCast(S);
354 P = Handle(Geom_Plane)::DownCast(RT->BasisSurface());
356 not_done = ! (S.IsNull() || !P.IsNull()) ;
359 // compute the 3d curve
360 gp_Ax2 axes = P->Position().Ax2();
361 Handle(Geom_Curve) C3d = GeomLib::To3d(axes,PC);
363 return Standard_False;
365 Standard_Real First, Last;
368 B.UpdateEdge(AnEdge,C3d,LocalLoc,0.0e0);
369 BRep_Tool::Range(AnEdge, S, LC, First, Last);
370 B.Range(AnEdge, First, Last); //Do not forget 3D range.(PRO6412)
374 // compute the 3d curve using existing surface
378 if (!BRep_Tool::Degenerated(AnEdge)) {
380 for (ii = 0 ; ii < 3 ; ii++ ) {
381 BRep_Tool::CurveOnSurface(TopoDS::Edge(AnEdge),
389 if (!Curve2dPtr.IsNull() && jj < 2){
390 Curve2dArray[jj] = Curve2dPtr ;
391 SurfaceArray[jj] = SurfacePtr ;
400 Curve2dPtr = Curve2dArray[0] ;
401 SurfacePtr = SurfaceArray[0] ;
403 Geom2dAdaptor_Curve AnAdaptor3dCurve2d (Curve2dPtr, f, l) ;
404 GeomAdaptor_Surface AnAdaptor3dSurface (SurfacePtr) ;
405 Handle(Geom2dAdaptor_HCurve) AnAdaptor3dCurve2dPtr =
406 new Geom2dAdaptor_HCurve(AnAdaptor3dCurve2d) ;
407 Handle(GeomAdaptor_HSurface) AnAdaptor3dSurfacePtr =
408 new GeomAdaptor_HSurface (AnAdaptor3dSurface) ;
409 Adaptor3d_CurveOnSurface CurveOnSurface( AnAdaptor3dCurve2dPtr,
410 AnAdaptor3dSurfacePtr) ;
412 Handle(Geom_Curve) NewCurvePtr ;
414 GeomLib::BuildCurve3d(Tolerance,
423 evaluateMaxSegment(MaxSegment,CurveOnSurface)) ;
425 tolerance = BRep_Tool::Tolerance(AnEdge) ;
427 //max_deviation = Max(tolerance, max_deviation) ;
428 max_deviation = Max( tolerance, Tolerance );
429 if (NewCurvePtr.IsNull())
430 return Standard_False;
431 B.UpdateEdge(TopoDS::Edge(AnEdge),
437 // if there is only one curve on surface attached to the edge
438 // than it can be qualified sameparameter
440 B.SameParameter(TopoDS::Edge(AnEdge),
445 return Standard_False ;
449 return Standard_True;
451 //=======================================================================
452 //function : BuildCurves3d
454 //=======================================================================
456 Standard_Boolean BRepLib::BuildCurves3d(const TopoDS_Shape& S)
459 return BRepLib::BuildCurves3d(S,
463 //=======================================================================
464 //function : BuildCurves3d
466 //=======================================================================
468 Standard_Boolean BRepLib::BuildCurves3d(const TopoDS_Shape& S,
469 const Standard_Real Tolerance,
470 const GeomAbs_Shape Continuity,
471 const Standard_Integer MaxDegree,
472 const Standard_Integer MaxSegment)
474 Standard_Boolean boolean_value,
476 TopTools_MapOfShape a_counter ;
477 TopExp_Explorer ex(S,TopAbs_EDGE);
480 if (a_counter.Add(ex.Current())) {
482 BuildCurve3d(TopoDS::Edge(ex.Current()),
483 Tolerance, Continuity,
484 MaxDegree, MaxSegment);
485 ok = ok && boolean_value ;
491 //=======================================================================
492 //function : UpdateEdgeTolerance
494 //=======================================================================
496 Standard_Boolean BRepLib::UpdateEdgeTol(const TopoDS_Edge& AnEdge,
497 const Standard_Real MinToleranceRequested,
498 const Standard_Real MaxToleranceToCheck)
501 Standard_Integer curve_on_surface_index,
508 geom_reference_curve_flag = 0,
509 max_sampling_points = 90,
510 min_sampling_points = 30 ;
512 Standard_Real factor = 100.0e0,
513 // sampling_array[2],
518 coded_edge_tolerance,
519 edge_tolerance = 0.0e0 ;
520 Handle(TColStd_HArray1OfReal) parameters_ptr ;
521 Handle(BRep_GCurve) geometric_representation_ptr ;
523 if (BRep_Tool::Degenerated(AnEdge)) return Standard_False ;
524 coded_edge_tolerance = BRep_Tool::Tolerance(AnEdge) ;
525 if (coded_edge_tolerance > MaxToleranceToCheck) return Standard_False ;
527 const Handle(BRep_TEdge)& TE = *((Handle(BRep_TEdge)*)&AnEdge.TShape());
528 BRep_ListOfCurveRepresentation& list_curve_rep = TE->ChangeCurves() ;
529 BRep_ListIteratorOfListOfCurveRepresentation an_iterator(list_curve_rep),
530 second_iterator(list_curve_rep) ;
531 Handle(Geom2d_Curve) curve2d_ptr, new_curve2d_ptr;
532 Handle(Geom_Surface) surface_ptr ;
533 TopLoc_Location local_location ;
534 GCPnts_QuasiUniformDeflection a_sampler ;
535 GeomAdaptor_Curve geom_reference_curve ;
536 Adaptor3d_CurveOnSurface curve_on_surface_reference ;
537 Handle(Geom_Curve) C = BRep_Tool::Curve(AnEdge,
541 curve_on_surface_index = -1 ;
543 if (! local_location.IsIdentity()) {
544 C = Handle(Geom_Curve)::
545 DownCast(C-> Transformed(local_location.Transformation()) ) ;
547 geom_reference_curve.Load(C) ;
548 geom_reference_curve_flag = 1 ;
549 a_sampler.Initialize(geom_reference_curve,
550 MinToleranceRequested * factor,
556 curve_on_surface_index = 0 ;
558 while (not_done && an_iterator.More()) {
559 geometric_representation_ptr =
560 Handle(BRep_GCurve)::DownCast(second_iterator.Value());
561 if (!geometric_representation_ptr.IsNull()
562 && geometric_representation_ptr->IsCurveOnSurface()) {
563 curve2d_ptr = geometric_representation_ptr->PCurve() ;
564 local_location = geometric_representation_ptr->Location() ;
565 current_first = geometric_representation_ptr->First();
566 //first = geometric_representation_ptr->First();
567 current_last = geometric_representation_ptr->Last();
570 if (! local_location.IsIdentity() ) {
571 surface_ptr = Handle(Geom_Surface)::
572 DownCast( geometric_representation_ptr->Surface()->
573 Transformed(local_location.Transformation()) ) ;
577 geometric_representation_ptr->Surface() ;
581 curve_on_surface_index += 1 ;
583 Geom2dAdaptor_Curve AnAdaptor3dCurve2d (curve2d_ptr) ;
584 GeomAdaptor_Surface AnAdaptor3dSurface (surface_ptr) ;
585 Handle(Geom2dAdaptor_HCurve) AnAdaptor3dCurve2dPtr =
586 new Geom2dAdaptor_HCurve(AnAdaptor3dCurve2d) ;
587 Handle(GeomAdaptor_HSurface) AnAdaptor3dSurfacePtr =
588 new GeomAdaptor_HSurface (AnAdaptor3dSurface) ;
589 curve_on_surface_reference.Load (AnAdaptor3dCurve2dPtr, AnAdaptor3dSurfacePtr);
590 a_sampler.Initialize(curve_on_surface_reference,
591 MinToleranceRequested * factor,
595 TColStd_Array1OfReal sampling_parameters(1,a_sampler.NbPoints()) ;
596 for (ii = 1 ; ii <= a_sampler.NbPoints() ; ii++) {
597 sampling_parameters(ii) = a_sampler.Parameter(ii) ;
599 if (a_sampler.NbPoints() < min_sampling_points) {
600 GeomLib::DensifyArray1OfReal(min_sampling_points,
604 else if (a_sampler.NbPoints() > max_sampling_points) {
605 GeomLib::RemovePointsFromArray(max_sampling_points,
612 new TColStd_HArray1OfReal(1,sampling_parameters.Length()) ;
613 for (ii = sampling_parameters.Lower() ; ii <= sampling_parameters.Upper() ; ii++) {
614 parameters_ptr->ChangeArray1()(jj) =
615 sampling_parameters(ii) ;
622 while (second_iterator.More()) {
623 geometric_representation_ptr =
624 Handle(BRep_GCurve)::DownCast(second_iterator.Value());
625 if (! geometric_representation_ptr.IsNull() &&
626 curve_index != curve_on_surface_index) {
628 has_curve = Standard_False ;
629 // first = geometric_representation_ptr->First();
630 // last = geometric_representation_ptr->Last();
631 local_location = geometric_representation_ptr->Location() ;
632 if (geometric_representation_ptr->IsCurveOnSurface()) {
633 curve2d_ptr = geometric_representation_ptr->PCurve() ;
634 has_curve = Standard_True ;
636 if (geometric_representation_ptr->IsCurveOnClosedSurface()) {
637 curve2d_ptr = geometric_representation_ptr->PCurve2() ;
638 has_closed_curve = Standard_True ;
643 if (! local_location.IsIdentity() ) {
644 surface_ptr = Handle(Geom_Surface)::
645 DownCast( geometric_representation_ptr->Surface()->
646 Transformed(local_location.Transformation()) ) ;
650 geometric_representation_ptr->Surface() ;
652 Geom2dAdaptor_Curve an_adaptor_curve2d (curve2d_ptr) ;
653 GeomAdaptor_Surface an_adaptor_surface(surface_ptr) ;
654 Handle(Geom2dAdaptor_HCurve) an_adaptor_curve2d_ptr =
655 new Geom2dAdaptor_HCurve(an_adaptor_curve2d) ;
656 Handle(GeomAdaptor_HSurface) an_adaptor_surface_ptr =
657 new GeomAdaptor_HSurface (an_adaptor_surface) ;
658 Adaptor3d_CurveOnSurface a_curve_on_surface(an_adaptor_curve2d_ptr,
659 an_adaptor_surface_ptr) ;
661 if (BRep_Tool::SameParameter(AnEdge)) {
663 GeomLib::EvalMaxParametricDistance(a_curve_on_surface,
664 geom_reference_curve,
665 MinToleranceRequested,
666 parameters_ptr->Array1(),
669 else if (geom_reference_curve_flag) {
670 GeomLib::EvalMaxDistanceAlongParameter(a_curve_on_surface,
671 geom_reference_curve,
672 MinToleranceRequested,
673 parameters_ptr->Array1(),
678 GeomLib::EvalMaxDistanceAlongParameter(a_curve_on_surface,
679 curve_on_surface_reference,
680 MinToleranceRequested,
681 parameters_ptr->Array1(),
684 max_distance *= safe_factor ;
685 edge_tolerance = Max(max_distance, edge_tolerance) ;
691 second_iterator.Next() ;
694 TE->Tolerance(edge_tolerance);
695 return Standard_True ;
698 //=======================================================================
699 //function : UpdateEdgeTolerance
701 //=======================================================================
703 Standard_Boolean BRepLib::UpdateEdgeTolerance(const TopoDS_Shape& S,
704 const Standard_Real MinToleranceRequested,
705 const Standard_Real MaxToleranceToCheck)
707 TopExp_Explorer ex(S,TopAbs_EDGE);
708 TopTools_MapOfShape a_counter ;
710 Standard_Boolean return_status = Standard_False,
714 if (a_counter.Add(ex.Current())) {
716 BRepLib::UpdateEdgeTol(TopoDS::Edge(ex.Current()),
717 MinToleranceRequested,
718 MaxToleranceToCheck) ;
719 if (local_flag && ! return_status) {
720 return_status = Standard_True ;
725 return return_status ;
728 //=======================================================================
729 //function : SetEdgeTol
731 //=======================================================================
733 static void SetEdgeTol(const TopoDS_Edge& E,
734 const TopoDS_Face& F)
737 const Handle(Geom_Surface)& S = BRep_Tool::Surface(F,L);
738 TopLoc_Location l = L.Predivided(E.Location());
740 const Handle(BRep_TEdge)& TE = *((Handle(BRep_TEdge)*)&E.TShape());
741 BRep_ListIteratorOfListOfCurveRepresentation itcr(TE->ChangeCurves());
743 while (itcr.More()) {
744 const Handle(BRep_CurveRepresentation)& cr = itcr.Value();
745 if(cr->IsCurveOnSurface(S,l)) return;
749 Handle(Geom_Plane) GP;
750 Handle(Geom_RectangularTrimmedSurface) GRTS;
751 GRTS = Handle(Geom_RectangularTrimmedSurface)::DownCast(S);
753 GP = Handle(Geom_Plane)::DownCast(GRTS->BasisSurface());
755 GP = Handle(Geom_Plane)::DownCast(S);
757 Handle(GeomAdaptor_HCurve) HC = new GeomAdaptor_HCurve();
758 Handle(GeomAdaptor_HSurface) HS = new GeomAdaptor_HSurface();
761 Standard_Real First, Last;
762 GeomAdaptor_Curve& GAC = HC->ChangeCurve();
763 GAC.Load(BRep_Tool::Curve(E,LC,First,Last));
764 LC = L.Predivided(LC);
766 if (!LC.IsIdentity()) {
767 GP = Handle(Geom_Plane)::DownCast(
768 GP->Transformed(LC.Transformation()));
770 GeomAdaptor_Surface& GAS = HS->ChangeSurface();
773 ProjLib_ProjectedCurve Proj(HS,HC);
774 Handle(Geom2d_Curve) pc = Geom2dAdaptor::MakeCurve(Proj);
776 gp_Pln pln = GAS.Plane();
777 Standard_Real d2 = 0.;
778 Standard_Integer nn = 23;
779 Standard_Real unsurnn = 1./nn;
780 for(Standard_Integer i = 0; i <= nn; i++){
781 Standard_Real t = unsurnn*i;
782 Standard_Real u = First*(1.-t) + Last*t;
783 gp_Pnt Pc3d = HC->Value(u);
784 gp_Pnt2d p2d = pc->Value(u);
785 gp_Pnt Pcons = ElSLib::Value(p2d.X(),p2d.Y(),pln);
786 Standard_Real eps = Max(Pc3d.XYZ().SquareModulus(), Pcons.XYZ().SquareModulus());
788 Standard_Real temp = Pc3d.SquareDistance(Pcons);
793 if(temp > d2) d2 = temp;
796 TE->UpdateTolerance(d2);
799 //=======================================================================
800 //function : SameParameter
802 //=======================================================================
803 void BRepLib::SameParameter(const TopoDS_Shape& S,
804 const Standard_Real Tolerance,
805 const Standard_Boolean forced)
807 TopExp_Explorer ex(S,TopAbs_EDGE);
808 TopTools_MapOfShape Done;
812 if (Done.Add(ex.Current())) {
814 brB.SameRange(TopoDS::Edge(ex.Current()), Standard_False);
815 brB.SameParameter(TopoDS::Edge(ex.Current()), Standard_False);
817 BRepLib::SameParameter(TopoDS::Edge(ex.Current()),Tolerance);
823 BRepAdaptor_Surface BS;
824 for(ex.Init(S,TopAbs_FACE); ex.More(); ex.Next()){
825 const TopoDS_Face& curface = TopoDS::Face(ex.Current());
826 if(!Done.Add(curface)) continue;
827 BS.Initialize(curface);
828 if(BS.GetType() != GeomAbs_Plane) continue;
830 for(ex2.Init(curface,TopAbs_EDGE); ex2.More(); ex2.Next()){
831 const TopoDS_Edge& E = TopoDS::Edge(ex2.Current());
832 SetEdgeTol(E,curface);
835 BRepLib::UpdateTolerances(S);
838 //================================================================
839 //function : SameParameter
840 //WARNING : New spec DUB LBO 9/9/97.
841 // Recode in the edge the best tolerance found,
842 // for vertex extremities it is required to find something else
843 //================================================================
844 static Standard_Boolean EvalTol(const Handle(Geom2d_Curve)& pc,
845 const Handle(Geom_Surface)& s,
846 const GeomAdaptor_Curve& gac,
847 const Standard_Real tol,
848 Standard_Real& tolbail)
850 Standard_Integer ok = 0;
851 Standard_Real f = gac.FirstParameter();
852 Standard_Real l = gac.LastParameter();
853 Extrema_LocateExtPC Projector;
854 Projector.Initialize(gac,f,l,tol);
858 for(Standard_Integer i = 1; i <= 5; i++){
859 Standard_Real t = i/6.;
860 t = (1.-t) * f + t * l;
861 pc->Value(t).Coord(u,v);
863 Projector.Perform(p,t);
864 if (Projector.IsDone()) {
865 Standard_Real dist2 = Projector.SquareDistance();
866 if(dist2 > tolbail * tolbail) tolbail = sqrt (dist2);
873 static Standard_Real ComputeTol(const Handle(Adaptor3d_HCurve)& c3d,
874 const Handle(Adaptor2d_HCurve2d)& c2d,
875 const Handle(Adaptor3d_HSurface)& surf,
876 const Standard_Integer nbp)
880 TColStd_Array1OfReal dist(1,nbp+10);
883 Adaptor3d_CurveOnSurface cons(c2d,surf);
884 Standard_Real d2 = 0.;
885 Standard_Real first = c3d->FirstParameter();
886 Standard_Real last = c3d->LastParameter();
887 Standard_Integer i = 0;
888 for(i = 0; i <= nbp; i++){
889 const Standard_Real t = IntToReal(i)/IntToReal(nbp);
890 const Standard_Real u = first*(1.-t) + last*t;
891 gp_Pnt Pc3d = c3d->Value(u);
892 gp_Pnt Pcons = cons.Value(u);
893 if (Precision::IsInfinite(Pcons.X()) ||
894 Precision::IsInfinite(Pcons.Y()) ||
895 Precision::IsInfinite(Pcons.Z())) {
896 d2=Precision::Infinite();
899 Standard_Real temp = Pc3d.SquareDistance(Pcons);
905 if(temp > d2) d2 = temp;
909 Standard_Boolean ana = Standard_False;
910 Standard_Real D2 = 0;
911 Standard_Integer N1 = 0;
912 Standard_Integer N2 = 0;
913 Standard_Integer N3 = 0;
915 for( i = 1; i<= nbp+10; i++)
917 if( dist(i) < 1.0 ) N1++;
921 if( N1 > N2 && N2 != 0 ) N3 = 100*N2/(N1+N2);
922 if( N3 < 10 && N3 != 0 ) {
924 for( i = 1; i<= nbp+10; i++)
925 if( dist(i) > 0 && dist(i) < 1.0 )
926 if( dist(i) > D2 ) D2 = dist(i);
930 d2 = (!ana) ? 1.5*sqrt(d2) : 1.5*sqrt(D2);
931 if(d2<1.e-7) d2 = 1.e-7;
936 void BRepLib::SameParameter(const TopoDS_Edge& AnEdge,
937 const Standard_Real Tolerance)
939 if (BRep_Tool::SameParameter(AnEdge)) return;
941 const Standard_Integer NCONTROL = 22;
943 Handle(GeomAdaptor_HCurve) HC = new GeomAdaptor_HCurve();
944 Handle(Geom2dAdaptor_HCurve) HC2d = new Geom2dAdaptor_HCurve();
945 Handle(GeomAdaptor_HSurface) HS = new GeomAdaptor_HSurface();
946 GeomAdaptor_Curve& GAC = HC->ChangeCurve();
947 Geom2dAdaptor_Curve& GAC2d = HC2d->ChangeCurve2d();
948 GeomAdaptor_Surface& GAS = HS->ChangeSurface();
949 Standard_Real f3d =0.,l3d =0.;
951 Handle(Geom_Curve) C3d;
953 const Handle(BRep_TEdge)& TE = *((Handle(BRep_TEdge)*) &AnEdge.TShape());
954 BRep_ListOfCurveRepresentation& CList = TE->ChangeCurves();
955 BRep_ListIteratorOfListOfCurveRepresentation It(CList);
957 Standard_Boolean NotDone = Standard_True;
959 while (NotDone && It.More()) {
960 Handle(BRep_GCurve) GCurve = Handle(BRep_GCurve)::DownCast(It.Value());
961 if (!GCurve.IsNull() && GCurve->IsCurve3D()) {
962 C3d = GCurve->Curve3D() ;
963 f3d = GCurve->First();
964 l3d = GCurve->Last();
965 L3d = GCurve->Location() ;
966 NotDone = Standard_False;
971 if(C3d.IsNull()) return;
973 // modified by NIZHNY-OCC486 Tue Aug 27 17:15:13 2002 :
974 Standard_Boolean m_TrimmedPeriodical = Standard_False;
975 Handle(Standard_Type) TheType = C3d->DynamicType();
976 if( TheType == STANDARD_TYPE(Geom_TrimmedCurve))
978 Handle(Geom_Curve) gtC (Handle(Geom_TrimmedCurve)::DownCast (C3d)->BasisCurve());
979 m_TrimmedPeriodical = gtC->IsPeriodic();
981 // modified by NIZHNY-OCC486 Tue Aug 27 17:15:17 2002 .
984 if(!C3d->IsPeriodic()) {
985 Standard_Real Udeb = C3d->FirstParameter();
986 Standard_Real Ufin = C3d->LastParameter();
987 // modified by NIZHNY-OCC486 Tue Aug 27 17:17:14 2002 :
988 //if (Udeb > f3d) f3d = Udeb;
989 //if (l3d > Ufin) l3d = Ufin;
990 if(!m_TrimmedPeriodical)
992 if (Udeb > f3d) f3d = Udeb;
993 if (l3d > Ufin) l3d = Ufin;
995 // modified by NIZHNY-OCC486 Tue Aug 27 17:17:55 2002 .
997 if(!L3d.IsIdentity()){
998 C3d = Handle(Geom_Curve)::DownCast(C3d->Transformed(L3d.Transformation()));
1000 GAC.Load(C3d,f3d,l3d);
1002 Standard_Boolean IsSameP = 1;
1003 Standard_Real maxdist = 0.;
1005 // Modified by skv - Thu Jun 3 12:39:19 2004 OCC5898 Begin
1006 Standard_Real anEdgeTol = BRep_Tool::Tolerance(AnEdge);
1007 // Modified by skv - Thu Jun 3 12:39:20 2004 OCC5898 End
1008 Standard_Boolean SameRange = BRep_Tool::SameRange(AnEdge);
1009 Standard_Boolean YaPCu = Standard_False;
1010 It.Initialize(CList);
1013 Standard_Boolean isANA = Standard_False;
1014 Standard_Boolean isBSP = Standard_False;
1015 Handle(BRep_GCurve) GCurve = Handle(BRep_GCurve)::DownCast(It.Value());
1016 Handle(Geom2d_Curve) PC[2];
1017 Handle(Geom_Surface) S;
1018 if (!GCurve.IsNull() && GCurve->IsCurveOnSurface()) {
1019 YaPCu = Standard_True;
1020 PC[0] = GCurve->PCurve();
1021 TopLoc_Location PCLoc = GCurve->Location();
1022 S = GCurve->Surface();
1023 if (!PCLoc.IsIdentity() ) {
1024 S = Handle(Geom_Surface)::DownCast(S->Transformed(PCLoc.Transformation()));
1028 if (GCurve->IsCurveOnClosedSurface()) {
1029 PC[1] = GCurve->PCurve2();
1032 // Eval tol2d to compute SameRange
1033 Standard_Real UResol = Max(GAS.UResolution(Tolerance), Precision::PConfusion());
1034 Standard_Real VResol = Max(GAS.VResolution(Tolerance), Precision::PConfusion());
1035 Standard_Real Tol2d = Min(UResol, VResol);
1036 for(Standard_Integer i = 0; i < 2; i++){
1037 Handle(Geom2d_Curve) curPC = PC[i];
1038 Standard_Boolean updatepc = 0;
1039 if(curPC.IsNull()) break;
1041 GeomLib::SameRange(Tol2d,
1042 PC[i],GCurve->First(),GCurve->Last(),
1045 updatepc = (curPC != PC[i]);
1048 Standard_Boolean goodpc = 1;
1049 GAC2d.Load(curPC,f3d,l3d);
1051 Standard_Real error = ComputeTol(HC, HC2d, HS, NCONTROL);
1053 if(GAC2d.GetType() == GeomAbs_BSplineCurve &&
1054 GAC2d.Continuity() == GeomAbs_C0) {
1055 Handle(Geom2d_BSplineCurve) bs2d = GAC2d.BSpline();
1056 Handle(Geom2d_BSplineCurve) bs2dsov = bs2d;
1057 Standard_Real fC0 = bs2d->FirstParameter(), lC0 = bs2d->LastParameter();
1058 Standard_Boolean repar = Standard_True;
1059 gp_Pnt2d OriginPoint;
1060 bs2d->D0(fC0, OriginPoint);
1061 Geom2dConvert::C0BSplineToC1BSplineCurve(bs2d, Tol2d);
1062 isBSP = Standard_True;
1064 if(bs2d->IsPeriodic()) { // -------- IFV, Jan 2000
1065 gp_Pnt2d NewOriginPoint;
1066 bs2d->D0(bs2d->FirstParameter(), NewOriginPoint);
1067 if(Abs(OriginPoint.X() - NewOriginPoint.X()) > Precision::PConfusion() ||
1068 Abs(OriginPoint.Y() - NewOriginPoint.Y()) > Precision::PConfusion() ) {
1070 TColStd_Array1OfReal Knotbs2d (1, bs2d->NbKnots());
1071 bs2d->Knots(Knotbs2d);
1073 for(Standard_Integer Index = 1; Index <= bs2d->NbKnots(); Index++) {
1074 bs2d->D0(Knotbs2d(Index), NewOriginPoint);
1075 if(Abs(OriginPoint.X() - NewOriginPoint.X()) > Precision::PConfusion() ||
1076 Abs(OriginPoint.Y() - NewOriginPoint.Y()) > Precision::PConfusion() ) continue;
1078 bs2d->SetOrigin(Index);
1084 if(bs2d->Continuity() == GeomAbs_C0) {
1085 Standard_Real tolbail;
1086 if(EvalTol(curPC,S,GAC,Tolerance,tolbail)){
1088 Standard_Real UResbail = GAS.UResolution(tolbail);
1089 Standard_Real VResbail = GAS.VResolution(tolbail);
1090 Standard_Real Tol2dbail = Min(UResbail,VResbail);
1091 bs2d->D0(bs2d->FirstParameter(), OriginPoint);
1093 Standard_Integer nbp = bs2d->NbPoles();
1094 TColgp_Array1OfPnt2d poles(1,nbp);
1096 gp_Pnt2d p = poles(1), p1;
1097 Standard_Real d = Precision::Infinite();
1098 for(Standard_Integer ip = 2; ip <= nbp; ip++) {
1100 d = Min(d,p.SquareDistance(p1));
1105 Tol2dbail = Max(Min(Tol2dbail,d),Tol2d);
1107 Geom2dConvert::C0BSplineToC1BSplineCurve(bs2d,Tol2dbail);
1109 if(bs2d->IsPeriodic()) { // -------- IFV, Jan 2000
1110 gp_Pnt2d NewOriginPoint;
1111 bs2d->D0(bs2d->FirstParameter(), NewOriginPoint);
1112 if(Abs(OriginPoint.X() - NewOriginPoint.X()) > Precision::PConfusion() ||
1113 Abs(OriginPoint.Y() - NewOriginPoint.Y()) > Precision::PConfusion() ) {
1115 TColStd_Array1OfReal Knotbs2d (1, bs2d->NbKnots());
1116 bs2d->Knots(Knotbs2d);
1118 for(Standard_Integer Index = 1; Index <= bs2d->NbKnots(); Index++) {
1119 bs2d->D0(Knotbs2d(Index), NewOriginPoint);
1120 if(Abs(OriginPoint.X() - NewOriginPoint.X()) > Precision::PConfusion() ||
1121 Abs(OriginPoint.Y() - NewOriginPoint.Y()) > Precision::PConfusion() ) continue;
1123 bs2d->SetOrigin(Index);
1130 if(bs2d->Continuity() == GeomAbs_C0) {
1133 repar = Standard_False;
1141 Standard_Integer NbKnots = bs2d->NbKnots();
1142 TColStd_Array1OfReal Knots(1,NbKnots);
1144 // BSplCLib::Reparametrize(f3d,l3d,Knots);
1145 BSplCLib::Reparametrize(fC0,lC0,Knots);
1146 bs2d->SetKnots(Knots);
1147 GAC2d.Load(bs2d,f3d,l3d);
1149 Standard_Boolean updatepcsov = updatepc;
1150 updatepc = Standard_True;
1152 Standard_Real error1 = ComputeTol(HC, HC2d, HS, NCONTROL);
1153 if(error1 > error) {
1155 GAC2d.Load(bs2d,f3d,l3d);
1157 updatepc = updatepcsov;
1158 isANA = Standard_True;
1165 //check, if new BSpline "good" or not --------- IFV, Jan of 2000
1166 GeomAbs_Shape cont = bs2d->Continuity();
1167 Standard_Boolean IsBad = Standard_False;
1169 if(cont > GeomAbs_C0 && error > Max(1.e-3,Tolerance)) {
1170 Standard_Integer NbKnots = bs2d->NbKnots();
1171 TColStd_Array1OfReal Knots(1,NbKnots);
1173 Standard_Real critratio = 10.;
1174 Standard_Real dtprev = Knots(2) - Knots(1), dtratio = 1.;
1175 Standard_Real dtmin = dtprev;
1176 Standard_Real dtcur;
1177 for(Standard_Integer j = 2; j < NbKnots; j++) {
1178 dtcur = Knots(j+1) - Knots(j);
1179 dtmin = Min(dtmin, dtcur);
1183 if(dtcur > dtprev) dtratio = dtcur/dtprev;
1184 else dtratio = dtprev/dtcur;
1185 if(dtratio > critratio) {IsBad = Standard_True;}
1190 // To avoid failures in Approx_CurvilinearParameter
1191 bs2d->Resolution(Max(1.e-3,Tolerance), dtcur);
1192 if(dtmin < dtcur) IsBad = Standard_False;
1197 if(IsBad ) { //if BSpline "bad", try to reparametrize it
1198 // by its curve length
1200 // GeomAbs_Shape cont = bs2d->Continuity();
1201 if(cont > GeomAbs_C2) cont = GeomAbs_C2;
1202 Standard_Integer maxdeg = bs2d->Degree();
1203 if(maxdeg == 1) maxdeg = 14;
1204 Approx_CurvilinearParameter AppCurPar(HC2d, HS, Max(1.e-3,Tolerance),
1206 if(AppCurPar.IsDone() || AppCurPar.HasResult()) {
1207 bs2d = AppCurPar.Curve2d1();
1208 GAC2d.Load(bs2d,f3d,l3d);
1211 if(Abs(bs2d->FirstParameter() - fC0) > Tol2d ||
1212 Abs(bs2d->LastParameter() - lC0) > Tol2d ) {
1213 Standard_Integer NbKnots = bs2d->NbKnots();
1214 TColStd_Array1OfReal Knots(1,NbKnots);
1216 // BSplCLib::Reparametrize(f3d,l3d,Knots);
1217 BSplCLib::Reparametrize(fC0,lC0,Knots);
1218 bs2d->SetKnots(Knots);
1219 GAC2d.Load(bs2d,f3d,l3d);
1232 // Approx_SameParameter SameP(HC,HC2d,HS,Tolerance);
1233 Standard_Real aTol = (isANA && isBSP) ? 1.e-7 : Tolerance;
1234 const Handle(Adaptor3d_HCurve)& aHCurv = HC; // to avoid ambiguity
1235 const Handle(Adaptor2d_HCurve2d)& aHCurv2d = HC2d; // to avoid ambiguity
1236 Approx_SameParameter SameP(aHCurv,aHCurv2d,HS,aTol);
1238 if (SameP.IsSameParameter()) {
1239 maxdist = Max(maxdist,SameP.TolReached());
1241 if (i == 0) GCurve->PCurve(curPC);
1242 else GCurve->PCurve2(curPC);
1245 else if (SameP.IsDone()) {
1246 Standard_Real tolreached = SameP.TolReached();
1247 if(tolreached < error) {
1248 curPC = SameP.Curve2d();
1249 updatepc = Standard_True;
1250 maxdist = Max(maxdist,tolreached);
1253 maxdist = Max(maxdist, error);
1256 if (i == 0) GCurve->PCurve(curPC);
1257 else GCurve->PCurve2(curPC);
1265 // Modified by skv - Thu Jun 3 12:39:19 2004 OCC5898 Begin
1267 if (anEdgeTol > error) {
1268 maxdist = Max(maxdist, anEdgeTol);
1269 IsSameP = Standard_True;
1272 // Modified by skv - Thu Jun 3 12:39:20 2004 OCC5898 End
1277 B.Range(AnEdge,f3d,l3d);
1278 B.SameRange(AnEdge,Standard_True);
1280 // Reduce eventually the tolerance of the edge, as
1281 // all its representations are processed (except for some associated
1282 // to planes and not stored in the edge !)
1283 // The same cannot be done with vertices that cannot be enlarged
1286 // Avoid setting too small tolerances.
1287 maxdist = Max(maxdist,Precision::Confusion());
1288 TopoDS_Vertex V1,V2;
1289 TopExp::Vertices(AnEdge,V1,V2);
1291 B.UpdateVertex(V1,maxdist);
1293 B.UpdateVertex(V2,maxdist);
1294 TE->Modified(Standard_True);
1295 TE->Tolerance(maxdist);
1297 B.SameParameter(AnEdge,Standard_True);
1301 //=======================================================================
1302 //function : UpdateTolerances
1304 //=======================================================================
1305 void BRepLib::UpdateTolerances(const TopoDS_Shape& aShape,
1306 const Standard_Boolean verifyTolerance)
1309 // Harmonize tolerances
1310 // with rule Tolerance(VERTEX)>=Tolerance(EDGE)>=Tolerance(FACE)
1312 Standard_Real tol=0;
1313 if (verifyTolerance) {
1314 // Set tolerance to its minimum value
1315 Handle(Geom_Surface) S;
1319 Standard_Real aXmin, aYmin, aZmin, aXmax, aYmax, aZmax, dMax;
1320 for (ex.Init(aShape, TopAbs_FACE); ex.More(); ex.Next()) {
1321 const TopoDS_Face& curf=TopoDS::Face(ex.Current());
1322 S = BRep_Tool::Surface(curf, l);
1325 BRepBndLib::Add(curf,aB);
1326 if (S->DynamicType() == STANDARD_TYPE(Geom_RectangularTrimmedSurface)) {
1327 S = Handle(Geom_RectangularTrimmedSurface)::DownCast (S)->BasisSurface();
1329 GeomAdaptor_Surface AS(S);
1330 switch (AS.GetType()) {
1332 case GeomAbs_Cylinder:
1335 tol=Precision::Confusion();
1338 case GeomAbs_Sphere:
1341 tol=Precision::Confusion()*2;
1345 tol=Precision::Confusion()*4;
1347 if (!aB.IsWhole()) {
1348 aB.Get(aXmin, aYmin, aZmin, aXmax, aYmax, aZmax);
1350 if (!aB.IsOpenXmin() && !aB.IsOpenXmax()) dMax=aXmax-aXmin;
1351 if (!aB.IsOpenYmin() && !aB.IsOpenYmax()) aYmin=aYmax-aYmin;
1352 if (!aB.IsOpenZmin() && !aB.IsOpenZmax()) aZmin=aZmax-aZmin;
1353 if (aYmin>dMax) dMax=aYmin;
1354 if (aZmin>dMax) dMax=aZmin;
1356 // Do not process tolerances > 1.
1357 if (tol>1.) tol=0.99;
1359 const Handle(BRep_TFace)& Tf = *((Handle(BRep_TFace)*)&curf.TShape());
1366 TopTools_IndexedDataMapOfShapeListOfShape parents;
1367 TopExp::MapShapesAndAncestors(aShape, TopAbs_EDGE, TopAbs_FACE, parents);
1368 TopTools_ListIteratorOfListOfShape lConx;
1369 Standard_Integer iCur;
1370 for (iCur=1; iCur<=parents.Extent(); iCur++) {
1372 for (lConx.Initialize(parents(iCur)); lConx.More(); lConx.Next()) {
1373 tol=Max(tol, BRep_Tool::Tolerance(TopoDS::Face(lConx.Value())));
1375 // Update can only increase tolerance, so if the edge has a greater
1376 // tolerance than its faces it is not concerned
1377 B.UpdateEdge(TopoDS::Edge(parents.FindKey(iCur)), tol);
1380 //Vertices are processed
1382 TopExp::MapShapesAndAncestors(aShape, TopAbs_VERTEX, TopAbs_EDGE, parents);
1383 TColStd_MapOfTransient Initialized;
1384 TopTools_MapOfShape Done;
1385 Standard_Integer nbV = parents.Extent();
1386 for (iCur=1; iCur<=nbV; iCur++) {
1389 const TopoDS_Vertex& V = TopoDS::Vertex(parents.FindKey(iCur));
1391 box.Add(BRep_Tool::Pnt(V));
1393 for (lConx.Initialize(parents(iCur)); lConx.More(); lConx.Next()) {
1394 const TopoDS_Edge& E = TopoDS::Edge(lConx.Value());
1395 if(!Done.Add(E)) continue;
1396 tol=Max(tol, BRep_Tool::Tolerance(E));
1397 if(!BRep_Tool::SameRange(E)) continue;
1398 Standard_Real par = BRep_Tool::Parameter(V,E);
1399 Handle(BRep_TEdge)& TE = *((Handle(BRep_TEdge)*)&E.TShape());
1400 BRep_ListIteratorOfListOfCurveRepresentation itcr(TE->Curves());
1401 const TopLoc_Location& Eloc = E.Location();
1402 while (itcr.More()) {
1403 // For each CurveRepresentation, check the provided parameter
1404 const Handle(BRep_CurveRepresentation)& cr = itcr.Value();
1405 const TopLoc_Location& loc = cr->Location();
1406 TopLoc_Location L = (Eloc * loc);
1407 if (cr->IsCurve3D()) {
1408 const Handle(Geom_Curve)& C = cr->Curve3D();
1409 if (!C.IsNull()) { // edge non degenerated
1410 p3d = C->Value(par);
1411 p3d.Transform(L.Transformation());
1415 else if (cr->IsCurveOnSurface()) {
1416 const Handle(Geom_Surface)& Su = cr->Surface();
1417 const Handle(Geom2d_Curve)& PC = cr->PCurve();
1418 Handle(Geom2d_Curve) PC2;
1419 if (cr->IsCurveOnClosedSurface()) {
1420 PC2 = cr->PCurve2();
1422 gp_Pnt2d p2d = PC->Value(par);
1423 p3d = Su->Value(p2d.X(),p2d.Y());
1424 p3d.Transform(L.Transformation());
1426 if (!PC2.IsNull()) {
1427 p2d = PC2->Value(par);
1428 p3d = Su->Value(p2d.X(),p2d.Y());
1429 p3d.Transform(L.Transformation());
1436 Standard_Real aXmin, aYmin, aZmin, aXmax, aYmax, aZmax;
1437 box.Get(aXmin, aYmin, aZmin, aXmax, aYmax, aZmax);
1438 aXmax -= aXmin; aYmax -= aYmin; aZmax -= aZmin;
1439 tol = Max(tol,sqrt(aXmax*aXmax+aYmax*aYmax+aZmax*aZmax));
1440 tol += 2.*Epsilon(tol);
1441 if (verifyTolerance) {
1442 // ASet minimum value of the tolerance
1443 // Attention to sharing of the vertex by other shapes
1444 const Handle(BRep_TVertex)& TV = *((Handle(BRep_TVertex)*)&V.TShape());
1445 if (Initialized.Add(TV))
1448 B.UpdateVertex(V, tol);
1451 // Update can only increase tolerance, so if the edge has a greater
1452 // tolerance than its faces it is not concerned
1453 B.UpdateVertex(V, tol);
1458 //=======================================================================
1459 //function : OrientClosedSolid
1461 //=======================================================================
1462 Standard_Boolean BRepLib::OrientClosedSolid(TopoDS_Solid& solid)
1464 // Set material inside the solid
1465 BRepClass3d_SolidClassifier where(solid);
1466 where.PerformInfinitePoint(Precision::Confusion());
1467 if (where.State()==TopAbs_IN) {
1470 else if (where.State()==TopAbs_ON || where.State()==TopAbs_UNKNOWN)
1471 return Standard_False;
1473 return Standard_True;
1476 //=======================================================================
1477 //function : tgtfaces
1478 //purpose : check the angle at the border between two squares.
1479 // Two shares should have a shared front edge.
1480 //=======================================================================
1482 static Standard_Boolean tgtfaces(const TopoDS_Edge& Ed,
1483 const TopoDS_Face& F1,
1484 const TopoDS_Face& F2,
1485 const Standard_Real ta,
1486 const Standard_Boolean couture)
1488 // Check if pcurves exist on both faces of edge
1489 Standard_Real aFirst,aLast;
1490 Handle(Geom2d_Curve) aCurve;
1491 aCurve = BRep_Tool::CurveOnSurface(Ed,F1,aFirst,aLast);
1493 return Standard_False;
1494 aCurve = BRep_Tool::CurveOnSurface(Ed,F2,aFirst,aLast);
1496 return Standard_False;
1500 BRepAdaptor_Surface aBAS1(F1,Standard_False);
1501 BRepAdaptor_Surface aBAS2(F2,Standard_False);
1502 Handle(BRepAdaptor_HSurface) HS1 = new BRepAdaptor_HSurface(aBAS1);
1503 Handle(BRepAdaptor_HSurface) HS2;
1504 if(couture) HS2 = HS1;
1505 else HS2 = new BRepAdaptor_HSurface(aBAS2);
1507 E.Orientation(TopAbs_FORWARD);
1508 Handle(BRepAdaptor_HCurve2d) HC2d1 = new BRepAdaptor_HCurve2d();
1509 HC2d1->ChangeCurve2d().Initialize(E,F1);
1510 if(couture) E.Orientation(TopAbs_REVERSED);
1511 Handle(BRepAdaptor_HCurve2d) HC2d2 = new BRepAdaptor_HCurve2d();
1512 HC2d2->ChangeCurve2d().Initialize(E,F2);
1513 Adaptor3d_CurveOnSurface C1(HC2d1,HS1);
1514 Adaptor3d_CurveOnSurface C2(HC2d2,HS2);
1516 Standard_Boolean rev1 = (F1.Orientation() == TopAbs_REVERSED);
1517 Standard_Boolean rev2 = (F2.Orientation() == TopAbs_REVERSED);
1518 Standard_Real f,l,eps, angmax = -M_PI;
1519 Standard_Real ang =0.;
1520 BRep_Tool::Range(E,f,l);
1521 Extrema_LocateExtPC ext;
1522 Standard_Boolean IsInitialized = Standard_False;
1525 f += eps; // to avoid calculations on
1526 l -= eps; // points of pointed squares.
1528 gp_Pnt pp1,pp2;//,PP;
1531 Standard_Real uu, vv, norm;
1534 Standard_Boolean Nok;
1535 for(i = 0; (i<= 20) && (angmax<=ta) ; i++){
1536 // First suppose that this is sameParameter
1537 Nok = Standard_True;
1540 HS1->D1(p.X(),p.Y(),pp1,du,dv);
1541 d1 = (du.Crossed(dv));
1542 norm = d1.Magnitude();
1543 if (norm > 1.e-12) d1 /= norm;
1544 else Nok=Standard_False;
1545 if(rev1) d1.Reverse();
1548 HS2->D1(p.X(), p.Y(), pp2, du, dv);
1549 d2 = (du.Crossed(dv));
1550 norm = d2.Magnitude();
1551 if (norm> 1.e-12) d2 /= norm;
1552 else Nok=Standard_False;
1553 if(rev2) d2.Reverse();
1554 if (Nok) ang = d1.Angle(d2);
1556 if (Nok &&(ang > ta)) { // Refine by projection
1557 if (! IsInitialized ) {
1558 ext.Initialize(C2,f,l,Precision::PConfusion());
1559 IsInitialized = Standard_True;
1562 if(ext.IsDone() && ext.IsMin()){
1563 Extrema_POnCurv poc = ext.Point();
1564 Standard_Real v = poc.Parameter();
1568 HS2->D1(p.X(), p.Y(), pp2, du, dv);
1569 d2 = (du.Crossed(dv));
1570 norm = d2.Magnitude();
1571 if (norm> 1.e-12) d2 /= norm;
1572 else Nok = Standard_False;
1573 if(rev2) d2.Reverse();
1574 if (Nok) ang = d1.Angle(d2);
1577 if(ang >= angmax) angmax = ang;
1580 return (angmax<=ta);
1585 //=======================================================================
1586 // function : EncodeRegularity
1587 // purpose : code the regularities on all edges of the shape, boundary of
1588 // two faces that do not have it.
1589 //=======================================================================
1591 void BRepLib::EncodeRegularity(const TopoDS_Shape& S,
1592 const Standard_Real TolAng)
1595 TopTools_IndexedDataMapOfShapeListOfShape M;
1596 TopExp::MapShapesAndAncestors(S,TopAbs_EDGE,TopAbs_FACE,M);
1597 TopTools_ListIteratorOfListOfShape It;
1600 Standard_Boolean found, couture;
1601 for(Standard_Integer i = 1; i <= M.Extent(); i++){
1602 TopoDS_Edge E = TopoDS::Edge(M.FindKey(i));
1603 found = Standard_False; couture = Standard_False;
1605 for(It.Initialize(M.FindFromIndex(i));It.More() && !found;It.Next()){
1606 if(F1.IsNull()) { F1 = TopoDS::Face(It.Value()); }
1608 if(!F1.IsSame(TopoDS::Face(It.Value()))){
1609 found = Standard_True;
1610 F2 = TopoDS::Face(It.Value());
1614 if (!found && !F1.IsNull()){//is it a sewing edge?
1615 TopAbs_Orientation orE = E.Orientation();
1617 for(Ex.Init(F1,TopAbs_EDGE);Ex.More() && !found;Ex.Next()){
1618 curE= TopoDS::Edge(Ex.Current());
1619 if(E.IsSame(curE) && orE != curE.Orientation()) {
1620 found = Standard_True;
1621 couture = Standard_True;
1627 if(BRep_Tool::Continuity(E,F1,F2)<=GeomAbs_C0){
1630 if(tgtfaces(E, F1, F2, TolAng, couture)){
1631 B.Continuity(E,F1,F2,GeomAbs_G1);
1634 catch(Standard_Failure)
1642 //=======================================================================
1643 // function : EncodeRegularity
1644 // purpose : code the regularity between 2 faces on an edge
1645 //=======================================================================
1647 void BRepLib::EncodeRegularity(TopoDS_Edge& E,
1648 const TopoDS_Face& F1,
1649 const TopoDS_Face& F2,
1650 const Standard_Real TolAng)
1653 if(BRep_Tool::Continuity(E,F1,F2)<=GeomAbs_C0){
1655 if( tgtfaces(E, F1, F2, TolAng, F1.IsEqual(F2))) {
1656 B.Continuity(E,F1,F2,GeomAbs_G1);
1659 catch(Standard_Failure)
1665 //=======================================================================
1666 // function : EnsureNormalConsistency
1667 // purpose : Corrects the normals in Poly_Triangulation of faces.
1668 // Returns TRUE if any correction is done.
1669 //=======================================================================
1670 Standard_Boolean BRepLib::
1671 EnsureNormalConsistency(const TopoDS_Shape& theShape,
1672 const Standard_Real theAngTol,
1673 const Standard_Boolean theForceComputeNormals)
1675 const Standard_Real aThresDot = cos(theAngTol);
1677 Standard_Boolean aRetVal = Standard_False, isNormalsFound = Standard_False;
1679 // compute normals if they are absent
1680 TopExp_Explorer anExpFace(theShape,TopAbs_FACE);
1681 for (; anExpFace.More(); anExpFace.Next())
1683 const TopoDS_Face& aFace = TopoDS::Face(anExpFace.Current());
1684 const Handle(Geom_Surface) aSurf = BRep_Tool::Surface(aFace);
1687 TopLoc_Location aLoc;
1688 const Handle(Poly_Triangulation)& aPT = BRep_Tool::Triangulation(aFace, aLoc);
1691 if (!theForceComputeNormals && aPT->HasNormals())
1693 isNormalsFound = Standard_True;
1697 GeomLProp_SLProps aSLP(aSurf, 2, Precision::Confusion());
1698 const Standard_Integer anArrDim = 3*aPT->NbNodes();
1699 Handle(TShort_HArray1OfShortReal) aNormArr = new TShort_HArray1OfShortReal(1, anArrDim);
1700 Standard_Integer anNormInd = aNormArr->Lower();
1701 for(Standard_Integer i = aPT->UVNodes().Lower(); i <= aPT->UVNodes().Upper(); i++)
1703 const gp_Pnt2d &aP2d = aPT->UVNodes().Value(i);
1704 aSLP.SetParameters(aP2d.X(), aP2d.Y());
1706 gp_XYZ aNorm(0.,0.,0.);
1707 if(!aSLP.IsNormalDefined())
1710 cout << "BRepLib::EnsureNormalConsistency(): Cannot find normal!" << endl;
1715 aNorm = aSLP.Normal().XYZ();
1716 if (aFace.Orientation() == TopAbs_REVERSED)
1719 aNormArr->ChangeValue(anNormInd++) = static_cast<Standard_ShortReal>(aNorm.X());
1720 aNormArr->ChangeValue(anNormInd++) = static_cast<Standard_ShortReal>(aNorm.Y());
1721 aNormArr->ChangeValue(anNormInd++) = static_cast<Standard_ShortReal>(aNorm.Z());
1724 aRetVal = Standard_True;
1725 isNormalsFound = Standard_True;
1726 aPT->SetNormals(aNormArr);
1735 TopTools_IndexedDataMapOfShapeListOfShape aMapEF;
1736 TopExp::MapShapesAndAncestors(theShape,TopAbs_EDGE,TopAbs_FACE,aMapEF);
1737 for(Standard_Integer anInd = 1; anInd <= aMapEF.Extent(); anInd++)
1739 const TopoDS_Edge& anEdg = TopoDS::Edge(aMapEF.FindKey(anInd));
1740 const TopTools_ListOfShape& anEdgList = aMapEF.FindFromIndex(anInd);
1741 if (anEdgList.Extent() != 2)
1743 TopTools_ListIteratorOfListOfShape anItF(anEdgList);
1744 const TopoDS_Face aFace1 = TopoDS::Face(anItF.Value());
1746 const TopoDS_Face aFace2 = TopoDS::Face(anItF.Value());
1747 TopLoc_Location aLoc1, aLoc2;
1748 const Handle(Poly_Triangulation)& aPT1 = BRep_Tool::Triangulation(aFace1, aLoc1);
1749 const Handle(Poly_Triangulation)& aPT2 = BRep_Tool::Triangulation(aFace2, aLoc2);
1751 if(aPT1.IsNull() || aPT2.IsNull())
1754 if(!aPT1->HasNormals() || !aPT2->HasNormals())
1757 const Handle(Poly_PolygonOnTriangulation)& aPTEF1 =
1758 BRep_Tool::PolygonOnTriangulation(anEdg, aPT1, aLoc1);
1759 const Handle(Poly_PolygonOnTriangulation)& aPTEF2 =
1760 BRep_Tool::PolygonOnTriangulation(anEdg, aPT2, aLoc2);
1762 TShort_Array1OfShortReal& aNormArr1 = aPT1->ChangeNormals();
1763 TShort_Array1OfShortReal& aNormArr2 = aPT2->ChangeNormals();
1765 for(Standard_Integer anEdgNode = aPTEF1->Nodes().Lower();
1766 anEdgNode <= aPTEF1->Nodes().Upper(); anEdgNode++)
1769 const Standard_Integer aFNodF1 = aPTEF1->Nodes().Value(anEdgNode);
1770 const Standard_Integer aFNodF2 = aPTEF2->Nodes().Value(anEdgNode);
1772 const Standard_Integer aFNorm1FirstIndex = aNormArr1.Lower() + 3*
1773 (aFNodF1 - aPT1->Nodes().Lower());
1774 const Standard_Integer aFNorm2FirstIndex = aNormArr2.Lower() + 3*
1775 (aFNodF2 - aPT2->Nodes().Lower());
1777 gp_XYZ aNorm1(aNormArr1.Value(aFNorm1FirstIndex),
1778 aNormArr1.Value(aFNorm1FirstIndex+1),
1779 aNormArr1.Value(aFNorm1FirstIndex+2));
1780 gp_XYZ aNorm2(aNormArr2.Value(aFNorm2FirstIndex),
1781 aNormArr2.Value(aFNorm2FirstIndex+1),
1782 aNormArr2.Value(aFNorm2FirstIndex+2));
1783 const Standard_Real aDot = aNorm1 * aNorm2;
1785 if(aDot > aThresDot)
1787 gp_XYZ aNewNorm = (aNorm1 + aNorm2).Normalized();
1788 aNormArr1.ChangeValue(aFNorm1FirstIndex) =
1789 aNormArr2.ChangeValue(aFNorm2FirstIndex) =
1790 static_cast<Standard_ShortReal>(aNewNorm.X());
1791 aNormArr1.ChangeValue(aFNorm1FirstIndex+1) =
1792 aNormArr2.ChangeValue(aFNorm2FirstIndex+1) =
1793 static_cast<Standard_ShortReal>(aNewNorm.Y());
1794 aNormArr1.ChangeValue(aFNorm1FirstIndex+2) =
1795 aNormArr2.ChangeValue(aFNorm2FirstIndex+2) =
1796 static_cast<Standard_ShortReal>(aNewNorm.Z());
1797 aRetVal = Standard_True;
1805 //=======================================================================
1806 //function : SortFaces
1808 //=======================================================================
1810 void BRepLib::SortFaces (const TopoDS_Shape& Sh,
1811 TopTools_ListOfShape& LF)
1814 TopTools_ListOfShape LTri,LPlan,LCyl,LCon,LSphere,LTor,LOther;
1815 TopExp_Explorer exp(Sh,TopAbs_FACE);
1817 Handle(Geom_Surface) S;
1819 for (; exp.More(); exp.Next()) {
1820 const TopoDS_Face& F = TopoDS::Face(exp.Current());
1821 S = BRep_Tool::Surface(F, l);
1823 if (S->DynamicType() == STANDARD_TYPE(Geom_RectangularTrimmedSurface)) {
1824 S = Handle(Geom_RectangularTrimmedSurface)::DownCast (S)->BasisSurface();
1826 GeomAdaptor_Surface AS(S);
1827 switch (AS.GetType()) {
1833 case GeomAbs_Cylinder:
1843 case GeomAbs_Sphere:
1857 else LTri.Append(F);
1859 LF.Append(LPlan); LF.Append(LCyl ); LF.Append(LCon); LF.Append(LSphere);
1860 LF.Append(LTor ); LF.Append(LOther); LF.Append(LTri);
1863 //=======================================================================
1864 //function : ReverseSortFaces
1866 //=======================================================================
1868 void BRepLib::ReverseSortFaces (const TopoDS_Shape& Sh,
1869 TopTools_ListOfShape& LF)
1872 TopTools_ListOfShape LTri,LPlan,LCyl,LCon,LSphere,LTor,LOther;
1873 TopExp_Explorer exp(Sh,TopAbs_FACE);
1875 Handle(Geom_Surface) S;
1877 for (; exp.More(); exp.Next()) {
1878 const TopoDS_Face& F = TopoDS::Face(exp.Current());
1879 S = BRep_Tool::Surface(F, l);
1881 if (S->DynamicType() == STANDARD_TYPE(Geom_RectangularTrimmedSurface)) {
1882 S = Handle(Geom_RectangularTrimmedSurface)::DownCast (S)->BasisSurface();
1884 GeomAdaptor_Surface AS(S);
1885 switch (AS.GetType()) {
1891 case GeomAbs_Cylinder:
1901 case GeomAbs_Sphere:
1915 else LTri.Append(F);
1917 LF.Append(LTri); LF.Append(LOther); LF.Append(LTor ); LF.Append(LSphere);
1918 LF.Append(LCon); LF.Append(LCyl ); LF.Append(LPlan);