1 // Created on: 1993-12-15
2 // Created by: Remi LEQUETTE
3 // Copyright (c) 1993-1999 Matra Datavision
4 // Copyright (c) 1999-2012 OPEN CASCADE SAS
6 // The content of this file is subject to the Open CASCADE Technology Public
7 // License Version 6.5 (the "License"). You may not use the content of this file
8 // except in compliance with the License. Please obtain a copy of the License
9 // at http://www.opencascade.org and read it completely before using this file.
11 // The Initial Developer of the Original Code is Open CASCADE S.A.S., having its
12 // main offices at: 1, place des Freres Montgolfier, 78280 Guyancourt, France.
14 // The Original Code and all software distributed under the License is
15 // distributed on an "AS IS" basis, without warranty of any kind, and the
16 // Initial Developer hereby disclaims all such warranties, including without
17 // limitation, any warranties of merchantability, fitness for a particular
18 // purpose or non-infringement. Please see the License for the specific terms
19 // and conditions governing the rights and limitations under the License.
22 //pmn 26/09/97 Add parameters of approximation in BuildCurve3d
23 // Modified by skv - Thu Jun 3 12:39:19 2004 OCC5898
25 #include <BRepLib.ixx>
26 #include <BRepAdaptor_Surface.hxx>
27 #include <BRepAdaptor_HSurface.hxx>
28 #include <BRepAdaptor_HCurve2d.hxx>
29 #include <BRep_Tool.hxx>
30 #include <BRep_Builder.hxx>
31 #include <Geom_Surface.hxx>
32 #include <Geom_RectangularTrimmedSurface.hxx>
33 #include <Geom_Plane.hxx>
34 #include <Geom_Curve.hxx>
35 #include <Geom_BSplineCurve.hxx>
36 #include <Geom_TrimmedCurve.hxx>
37 #include <Geom2d_Curve.hxx>
38 #include <Geom2d_TrimmedCurve.hxx>
39 #include <Geom2d_BSplineCurve.hxx>
40 #include <GeomLib.hxx>
41 #include <TopExp_Explorer.hxx>
43 #include <TopoDS_Edge.hxx>
44 #include <TopoDS_Face.hxx>
45 #include <TopoDS_Vertex.hxx>
46 #include <TopTools_MapOfShape.hxx>
47 #include <TopTools_IndexedDataMapOfShapeListOfShape.hxx>
48 #include <TopTools_ListIteratorOfListOfShape.hxx>
53 #include <Standard_Real.hxx>
54 #include <Precision.hxx>
55 #include <BRep_GCurve.hxx>
56 #include <BRep_TEdge.hxx>
57 #include <BRep_TFace.hxx>
58 #include <AppParCurves_MultiCurve.hxx>
59 #include <AppParCurves_MultiBSpCurve.hxx>
60 #include <BRep_ListOfCurveRepresentation.hxx>
61 #include <BRep_CurveRepresentation.hxx>
62 #include <BRep_ListIteratorOfListOfCurveRepresentation.hxx>
63 #include <BRep_TVertex.hxx>
64 #include <AdvApprox_ApproxAFunction.hxx>
65 #include <Approx_SameParameter.hxx>
66 #include <TColgp_Array1OfPnt.hxx>
67 #include <TColgp_Array1OfPnt2d.hxx>
68 #include <TColStd_Array1OfReal.hxx>
69 #include <TColStd_MapOfTransient.hxx>
70 #include <GeomAdaptor_Curve.hxx>
71 #include <GeomAdaptor_HCurve.hxx>
72 #include <GeomAdaptor_Surface.hxx>
73 #include <GeomAdaptor_HSurface.hxx>
74 #include <Geom2dAdaptor_Curve.hxx>
75 #include <Geom2dAdaptor_HCurve.hxx>
76 #include <Geom2dAdaptor.hxx>
77 #include <Geom2dConvert.hxx>
78 #include <GCPnts_QuasiUniformDeflection.hxx>
79 #include <BSplCLib.hxx>
81 #include <Adaptor3d_CurveOnSurface.hxx>
82 #include <Extrema_LocateExtPC.hxx>
83 #include <ProjLib_ProjectedCurve.hxx>
84 #include <BRepClass3d_SolidClassifier.hxx>
85 #include <Bnd_Box.hxx>
86 #include <BRepBndLib.hxx>
87 #include <Approx_CurvilinearParameter.hxx>
88 #include <Geom_BSplineSurface.hxx>
91 // TODO - not thread-safe static variables
92 static Standard_Real thePrecision = Precision::Confusion();
93 static Handle(Geom_Plane) thePlane;
95 //=======================================================================
96 //function : Precision
98 //=======================================================================
100 void BRepLib::Precision(const Standard_Real P)
105 //=======================================================================
106 //function : Precision
108 //=======================================================================
110 Standard_Real BRepLib::Precision()
115 //=======================================================================
118 //=======================================================================
120 void BRepLib::Plane(const Handle(Geom_Plane)& P)
126 //=======================================================================
129 //=======================================================================
131 const Handle(Geom_Plane)& BRepLib::Plane()
133 if (thePlane.IsNull()) thePlane = new Geom_Plane(gp::XOY());
136 //=======================================================================
137 //function : CheckSameRange
139 //=======================================================================
141 Standard_Boolean BRepLib::CheckSameRange(const TopoDS_Edge& AnEdge,
142 const Standard_Real Tolerance)
144 Standard_Boolean IsSameRange = Standard_True,
145 first_time_in = Standard_True ;
147 BRep_ListIteratorOfListOfCurveRepresentation an_Iterator
148 ((*((Handle(BRep_TEdge)*)&AnEdge.TShape()))->ChangeCurves());
150 Standard_Real first, last;
152 Standard_Real current_first =0., current_last =0. ;
154 Standard_Real current_first,current_last ;
156 Handle(BRep_GCurve) geometric_representation_ptr ;
158 while (IsSameRange && an_Iterator.More()) {
159 geometric_representation_ptr =
160 Handle(BRep_GCurve)::DownCast(an_Iterator.Value());
161 if (!geometric_representation_ptr.IsNull()) {
163 first = geometric_representation_ptr->First();
164 last = geometric_representation_ptr->Last();
165 if (first_time_in ) {
166 current_first = first ;
167 current_last = last ;
168 first_time_in = Standard_False ;
171 IsSameRange = (Abs(current_first - first) <= Tolerance)
172 && (Abs(current_last -last) <= Tolerance ) ;
180 //=======================================================================
181 //function : SameRange
183 //=======================================================================
185 void BRepLib::SameRange(const TopoDS_Edge& AnEdge,
186 const Standard_Real Tolerance)
188 BRep_ListIteratorOfListOfCurveRepresentation an_Iterator
189 ((*((Handle(BRep_TEdge)*)&AnEdge.TShape()))->ChangeCurves());
191 Handle(Geom2d_Curve) Curve2dPtr, Curve2dPtr2, NewCurve2dPtr, NewCurve2dPtr2;
192 TopLoc_Location LocalLoc ;
194 Standard_Boolean first_time_in = Standard_True,
197 Handle(BRep_GCurve) geometric_representation_ptr ;
203 const Handle(Geom_Curve) C = BRep_Tool::Curve(AnEdge,
208 first_time_in = Standard_False ;
211 while (an_Iterator.More()) {
212 geometric_representation_ptr =
213 Handle(BRep_GCurve)::DownCast(an_Iterator.Value());
214 if (! geometric_representation_ptr.IsNull()) {
216 has_curve = Standard_False ;
217 first = geometric_representation_ptr->First();
218 last = geometric_representation_ptr->Last();
219 if (geometric_representation_ptr->IsCurveOnSurface()) {
220 Curve2dPtr = geometric_representation_ptr->PCurve() ;
221 has_curve = Standard_True ;
223 if (geometric_representation_ptr->IsCurveOnClosedSurface()) {
224 Curve2dPtr2 = geometric_representation_ptr->PCurve2() ;
225 has_closed_curve = Standard_True ;
227 if (has_curve || has_closed_curve) {
229 current_first = first ;
230 current_last = last ;
231 first_time_in = Standard_False ;
234 if (Abs(first - current_first) > Precision::Confusion() ||
235 Abs(last - current_last) > Precision::Confusion() )
239 GeomLib::SameRange(Tolerance,
241 geometric_representation_ptr->First(),
242 geometric_representation_ptr->Last(),
246 geometric_representation_ptr->PCurve(NewCurve2dPtr) ;
248 if (has_closed_curve)
250 GeomLib::SameRange(Tolerance,
252 geometric_representation_ptr->First(),
253 geometric_representation_ptr->Last(),
257 geometric_representation_ptr->PCurve2(NewCurve2dPtr2) ;
265 B.Range(TopoDS::Edge(AnEdge),
273 //=======================================================================
274 //function : EvaluateMaxSegment
275 //purpose : return MaxSegment to pass in approximation, if MaxSegment==0 provided
276 //=======================================================================
278 static Standard_Integer evaluateMaxSegment(const Standard_Integer aMaxSegment,
279 const Adaptor3d_CurveOnSurface& aCurveOnSurface)
281 if (aMaxSegment != 0) return aMaxSegment;
283 Handle(Adaptor3d_HSurface) aSurf = aCurveOnSurface.GetSurface();
284 Handle(Adaptor2d_HCurve2d) aCurv2d = aCurveOnSurface.GetCurve();
286 Standard_Real aNbSKnots = 0, aNbC2dKnots = 0;
288 if (aSurf->GetType() == GeomAbs_BSplineSurface) {
289 Handle(Geom_BSplineSurface) aBSpline = aSurf->BSpline();
290 aNbSKnots = Max(aBSpline->NbUKnots(), aBSpline->NbVKnots());
292 if (aCurv2d->GetType() == GeomAbs_BSplineCurve) {
293 aNbC2dKnots = aCurv2d->NbKnots();
295 Standard_Integer aReturn = (Standard_Integer) ( 30 + Max(aNbSKnots, aNbC2dKnots) ) ;
299 //=======================================================================
300 //function : BuildCurve3d
302 //=======================================================================
304 Standard_Boolean BRepLib::BuildCurve3d(const TopoDS_Edge& AnEdge,
305 const Standard_Real Tolerance,
306 const GeomAbs_Shape Continuity,
307 const Standard_Integer MaxDegree,
308 const Standard_Integer MaxSegment)
310 Standard_Integer //ErrorCode,
316 TopLoc_Location LocalLoc,L[2],LC;
317 Standard_Real f,l,fc,lc, first[2], last[2],
321 Handle(Geom2d_Curve) Curve2dPtr, Curve2dArray[2] ;
322 Handle(Geom_Surface) SurfacePtr, SurfaceArray[2] ;
324 Standard_Integer not_done ;
325 // if the edge has a 3d curve returns true
328 const Handle(Geom_Curve) C = BRep_Tool::Curve(AnEdge,LocalLoc,f,l);
330 return Standard_True;
332 // this should not exists but UpdateEdge makes funny things
333 // if the edge is not same range
335 if (! CheckSameRange(AnEdge,
336 Precision::Confusion())) {
343 // search a curve on a plane
344 Handle(Geom_Surface) S;
345 Handle(Geom2d_Curve) PC;
346 Standard_Integer i = 0;
347 Handle(Geom_Plane) P;
352 BRep_Tool::CurveOnSurface(AnEdge,PC,S,LocalLoc,f,l,i);
353 Handle(Geom_RectangularTrimmedSurface) RT =
354 Handle(Geom_RectangularTrimmedSurface)::DownCast(S);
356 P = Handle(Geom_Plane)::DownCast(S);
359 P = Handle(Geom_Plane)::DownCast(RT->BasisSurface());
361 not_done = ! (S.IsNull() || !P.IsNull()) ;
364 // compute the 3d curve
365 gp_Ax2 axes = P->Position().Ax2();
366 Handle(Geom_Curve) C3d = GeomLib::To3d(axes,PC);
368 return Standard_False;
370 Standard_Real First, Last;
373 Standard_Boolean is_closed ;
374 is_closed = AnEdge.Closed() ;
376 B.UpdateEdge(AnEdge,C3d,LocalLoc,0.0e0);
377 BRep_Tool::Range(AnEdge, S, LC, First, Last);
378 B.Range(AnEdge, First, Last); //Do not forget 3D range.(PRO6412)
379 TopoDS_Edge E = AnEdge ;
380 E.Closed(is_closed) ;
385 // compute the 3d curve using existing surface
389 if (!BRep_Tool::Degenerated(AnEdge)) {
391 for (ii = 0 ; ii < 3 ; ii++ ) {
392 BRep_Tool::CurveOnSurface(TopoDS::Edge(AnEdge),
400 if (!Curve2dPtr.IsNull() && jj < 2){
401 Curve2dArray[jj] = Curve2dPtr ;
402 SurfaceArray[jj] = SurfacePtr ;
411 Curve2dPtr = Curve2dArray[0] ;
412 SurfacePtr = SurfaceArray[0] ;
414 Geom2dAdaptor_Curve AnAdaptor3dCurve2d (Curve2dPtr, f, l) ;
415 GeomAdaptor_Surface AnAdaptor3dSurface (SurfacePtr) ;
416 Handle(Geom2dAdaptor_HCurve) AnAdaptor3dCurve2dPtr =
417 new Geom2dAdaptor_HCurve(AnAdaptor3dCurve2d) ;
418 Handle(GeomAdaptor_HSurface) AnAdaptor3dSurfacePtr =
419 new GeomAdaptor_HSurface (AnAdaptor3dSurface) ;
420 Adaptor3d_CurveOnSurface CurveOnSurface( AnAdaptor3dCurve2dPtr,
421 AnAdaptor3dSurfacePtr) ;
423 Handle(Geom_Curve) NewCurvePtr ;
425 GeomLib::BuildCurve3d(Tolerance,
434 evaluateMaxSegment(MaxSegment,CurveOnSurface)) ;
436 tolerance = BRep_Tool::Tolerance(AnEdge) ;
438 //max_deviation = Max(tolerance, max_deviation) ;
439 max_deviation = Max( tolerance, Tolerance );
440 if (NewCurvePtr.IsNull())
441 return Standard_False;
442 Standard_Boolean is_closed ;
443 is_closed = AnEdge.Closed() ;
444 B.UpdateEdge(TopoDS::Edge(AnEdge),
448 TopoDS_Edge E = AnEdge ;
449 E.Closed(is_closed) ;
452 // if there is only one curve on surface attached to the edge
453 // than it can be qualified sameparameter
455 B.SameParameter(TopoDS::Edge(AnEdge),
460 return Standard_False ;
464 return Standard_True;
466 //=======================================================================
467 //function : BuildCurves3d
469 //=======================================================================
471 Standard_Boolean BRepLib::BuildCurves3d(const TopoDS_Shape& S)
474 return BRepLib::BuildCurves3d(S,
478 //=======================================================================
479 //function : BuildCurves3d
481 //=======================================================================
483 Standard_Boolean BRepLib::BuildCurves3d(const TopoDS_Shape& S,
484 const Standard_Real Tolerance,
485 const GeomAbs_Shape Continuity,
486 const Standard_Integer MaxDegree,
487 const Standard_Integer MaxSegment)
489 Standard_Boolean boolean_value,
491 TopTools_MapOfShape a_counter ;
492 TopExp_Explorer ex(S,TopAbs_EDGE);
495 if (a_counter.Add(ex.Current())) {
497 BuildCurve3d(TopoDS::Edge(ex.Current()),
498 Tolerance, Continuity,
499 MaxDegree, MaxSegment);
500 ok = ok && boolean_value ;
506 //=======================================================================
507 //function : UpdateEdgeTolerance
509 //=======================================================================
511 Standard_Boolean BRepLib::UpdateEdgeTol(const TopoDS_Edge& AnEdge,
512 const Standard_Real MinToleranceRequested,
513 const Standard_Real MaxToleranceToCheck)
516 Standard_Integer curve_on_surface_index,
523 geom_reference_curve_flag = 0,
524 max_sampling_points = 90,
525 min_sampling_points = 30 ;
527 Standard_Real factor = 100.0e0,
528 // sampling_array[2],
533 coded_edge_tolerance,
534 edge_tolerance = 0.0e0 ;
535 Handle(TColStd_HArray1OfReal) parameters_ptr ;
536 Handle(BRep_GCurve) geometric_representation_ptr ;
538 if (BRep_Tool::Degenerated(AnEdge)) return Standard_False ;
539 coded_edge_tolerance = BRep_Tool::Tolerance(AnEdge) ;
540 if (coded_edge_tolerance > MaxToleranceToCheck) return Standard_False ;
542 const Handle(BRep_TEdge)& TE = *((Handle(BRep_TEdge)*)&AnEdge.TShape());
543 BRep_ListOfCurveRepresentation& list_curve_rep = TE->ChangeCurves() ;
544 BRep_ListIteratorOfListOfCurveRepresentation an_iterator(list_curve_rep),
545 second_iterator(list_curve_rep) ;
546 Handle(Geom2d_Curve) curve2d_ptr, new_curve2d_ptr;
547 Handle(Geom_Surface) surface_ptr ;
548 TopLoc_Location local_location ;
549 GCPnts_QuasiUniformDeflection a_sampler ;
550 GeomAdaptor_Curve geom_reference_curve ;
551 Adaptor3d_CurveOnSurface curve_on_surface_reference ;
552 Handle(Geom_Curve) C = BRep_Tool::Curve(AnEdge,
556 curve_on_surface_index = -1 ;
558 if (! local_location.IsIdentity()) {
559 C = Handle(Geom_Curve)::
560 DownCast(C-> Transformed(local_location.Transformation()) ) ;
562 geom_reference_curve.Load(C) ;
563 geom_reference_curve_flag = 1 ;
564 a_sampler.Initialize(geom_reference_curve,
565 MinToleranceRequested * factor,
571 curve_on_surface_index = 0 ;
573 while (not_done && an_iterator.More()) {
574 geometric_representation_ptr =
575 Handle(BRep_GCurve)::DownCast(second_iterator.Value());
576 if (!geometric_representation_ptr.IsNull()
577 && geometric_representation_ptr->IsCurveOnSurface()) {
578 curve2d_ptr = geometric_representation_ptr->PCurve() ;
579 local_location = geometric_representation_ptr->Location() ;
580 current_first = geometric_representation_ptr->First();
581 //first = geometric_representation_ptr->First();
582 current_last = geometric_representation_ptr->Last();
585 if (! local_location.IsIdentity() ) {
586 surface_ptr = Handle(Geom_Surface)::
587 DownCast( geometric_representation_ptr->Surface()->
588 Transformed(local_location.Transformation()) ) ;
592 geometric_representation_ptr->Surface() ;
596 curve_on_surface_index += 1 ;
598 Geom2dAdaptor_Curve AnAdaptor3dCurve2d (curve2d_ptr) ;
599 GeomAdaptor_Surface AnAdaptor3dSurface (surface_ptr) ;
600 Handle(Geom2dAdaptor_HCurve) AnAdaptor3dCurve2dPtr =
601 new Geom2dAdaptor_HCurve(AnAdaptor3dCurve2d) ;
602 Handle(GeomAdaptor_HSurface) AnAdaptor3dSurfacePtr =
603 new GeomAdaptor_HSurface (AnAdaptor3dSurface) ;
604 curve_on_surface_reference.Load( AnAdaptor3dCurve2dPtr) ;
605 curve_on_surface_reference.Load( AnAdaptor3dSurfacePtr) ;
606 a_sampler.Initialize(curve_on_surface_reference,
607 MinToleranceRequested * factor,
611 TColStd_Array1OfReal sampling_parameters(1,a_sampler.NbPoints()) ;
612 for (ii = 1 ; ii <= a_sampler.NbPoints() ; ii++) {
613 sampling_parameters(ii) = a_sampler.Parameter(ii) ;
615 if (a_sampler.NbPoints() < min_sampling_points) {
616 GeomLib::DensifyArray1OfReal(min_sampling_points,
620 else if (a_sampler.NbPoints() > max_sampling_points) {
621 GeomLib::RemovePointsFromArray(max_sampling_points,
628 new TColStd_HArray1OfReal(1,sampling_parameters.Length()) ;
629 for (ii = sampling_parameters.Lower() ; ii <= sampling_parameters.Upper() ; ii++) {
630 parameters_ptr->ChangeArray1()(jj) =
631 sampling_parameters(ii) ;
638 while (second_iterator.More()) {
639 geometric_representation_ptr =
640 Handle(BRep_GCurve)::DownCast(second_iterator.Value());
641 if (! geometric_representation_ptr.IsNull() &&
642 curve_index != curve_on_surface_index) {
644 has_curve = Standard_False ;
645 // first = geometric_representation_ptr->First();
646 // last = geometric_representation_ptr->Last();
647 local_location = geometric_representation_ptr->Location() ;
648 if (geometric_representation_ptr->IsCurveOnSurface()) {
649 curve2d_ptr = geometric_representation_ptr->PCurve() ;
650 has_curve = Standard_True ;
652 if (geometric_representation_ptr->IsCurveOnClosedSurface()) {
653 curve2d_ptr = geometric_representation_ptr->PCurve2() ;
654 has_closed_curve = Standard_True ;
659 if (! local_location.IsIdentity() ) {
660 surface_ptr = Handle(Geom_Surface)::
661 DownCast( geometric_representation_ptr->Surface()->
662 Transformed(local_location.Transformation()) ) ;
666 geometric_representation_ptr->Surface() ;
668 Geom2dAdaptor_Curve an_adaptor_curve2d (curve2d_ptr) ;
669 GeomAdaptor_Surface an_adaptor_surface(surface_ptr) ;
670 Handle(Geom2dAdaptor_HCurve) an_adaptor_curve2d_ptr =
671 new Geom2dAdaptor_HCurve(an_adaptor_curve2d) ;
672 Handle(GeomAdaptor_HSurface) an_adaptor_surface_ptr =
673 new GeomAdaptor_HSurface (an_adaptor_surface) ;
674 Adaptor3d_CurveOnSurface a_curve_on_surface(an_adaptor_curve2d_ptr,
675 an_adaptor_surface_ptr) ;
677 if (BRep_Tool::SameParameter(AnEdge)) {
679 GeomLib::EvalMaxParametricDistance(a_curve_on_surface,
680 geom_reference_curve,
681 MinToleranceRequested,
682 parameters_ptr->Array1(),
685 else if (geom_reference_curve_flag) {
686 GeomLib::EvalMaxDistanceAlongParameter(a_curve_on_surface,
687 geom_reference_curve,
688 MinToleranceRequested,
689 parameters_ptr->Array1(),
694 GeomLib::EvalMaxDistanceAlongParameter(a_curve_on_surface,
695 curve_on_surface_reference,
696 MinToleranceRequested,
697 parameters_ptr->Array1(),
700 max_distance *= safe_factor ;
701 edge_tolerance = Max(max_distance, edge_tolerance) ;
707 second_iterator.Next() ;
710 TE->Tolerance(edge_tolerance);
711 return Standard_True ;
714 //=======================================================================
715 //function : UpdateEdgeTolerance
717 //=======================================================================
719 Standard_Boolean BRepLib::UpdateEdgeTolerance(const TopoDS_Shape& S,
720 const Standard_Real MinToleranceRequested,
721 const Standard_Real MaxToleranceToCheck)
723 TopExp_Explorer ex(S,TopAbs_EDGE);
724 TopTools_MapOfShape a_counter ;
726 Standard_Boolean return_status = Standard_False,
730 if (a_counter.Add(ex.Current())) {
732 BRepLib::UpdateEdgeTol(TopoDS::Edge(ex.Current()),
733 MinToleranceRequested,
734 MaxToleranceToCheck) ;
735 if (local_flag && ! return_status) {
736 return_status = Standard_True ;
741 return return_status ;
744 //=======================================================================
745 //function : SetEdgeTol
747 //=======================================================================
749 static void SetEdgeTol(const TopoDS_Edge& E,
750 const TopoDS_Face& F)
753 const Handle(Geom_Surface)& S = BRep_Tool::Surface(F,L);
754 TopLoc_Location l = L.Predivided(E.Location());
756 const Handle(BRep_TEdge)& TE = *((Handle(BRep_TEdge)*)&E.TShape());
757 BRep_ListIteratorOfListOfCurveRepresentation itcr(TE->ChangeCurves());
759 while (itcr.More()) {
760 const Handle(BRep_CurveRepresentation)& cr = itcr.Value();
761 if(cr->IsCurveOnSurface(S,l)) return;
765 Handle(Geom_Plane) GP;
766 Handle(Geom_RectangularTrimmedSurface) GRTS;
767 GRTS = Handle(Geom_RectangularTrimmedSurface)::DownCast(S);
769 GP = Handle(Geom_Plane)::DownCast(GRTS->BasisSurface());
771 GP = Handle(Geom_Plane)::DownCast(S);
773 Handle(GeomAdaptor_HCurve) HC = new GeomAdaptor_HCurve();
774 Handle(GeomAdaptor_HSurface) HS = new GeomAdaptor_HSurface();
777 Standard_Real First, Last;
778 GeomAdaptor_Curve& GAC = HC->ChangeCurve();
779 GAC.Load(BRep_Tool::Curve(E,LC,First,Last));
780 LC = L.Predivided(LC);
782 if (!LC.IsIdentity()) {
783 GP = Handle(Geom_Plane)::DownCast(
784 GP->Transformed(LC.Transformation()));
786 GeomAdaptor_Surface& GAS = HS->ChangeSurface();
789 ProjLib_ProjectedCurve Proj(HS,HC);
790 Handle(Geom2d_Curve) pc = Geom2dAdaptor::MakeCurve(Proj);
792 gp_Pln pln = GAS.Plane();
793 Standard_Real d2 = 0.;
794 Standard_Integer nn = 23;
795 Standard_Real unsurnn = 1./nn;
796 for(Standard_Integer i = 0; i <= nn; i++){
797 Standard_Real t = unsurnn*i;
798 Standard_Real u = First*(1.-t) + Last*t;
799 gp_Pnt Pc3d = HC->Value(u);
800 gp_Pnt2d p2d = pc->Value(u);
801 gp_Pnt Pcons = ElSLib::Value(p2d.X(),p2d.Y(),pln);
802 Standard_Real temp = Pc3d.SquareDistance(Pcons);
803 if(temp > d2) d2 = temp;
806 TE->UpdateTolerance(d2);
809 //=======================================================================
810 //function : SameParameter
812 //=======================================================================
813 void BRepLib::SameParameter(const TopoDS_Shape& S,
814 const Standard_Real Tolerance,
815 const Standard_Boolean forced)
817 TopExp_Explorer ex(S,TopAbs_EDGE);
818 TopTools_MapOfShape Done;
822 if (Done.Add(ex.Current())) {
824 brB.SameRange(TopoDS::Edge(ex.Current()), Standard_False);
825 brB.SameParameter(TopoDS::Edge(ex.Current()), Standard_False);
827 BRepLib::SameParameter(TopoDS::Edge(ex.Current()),Tolerance);
833 BRepAdaptor_Surface BS;
834 for(ex.Init(S,TopAbs_FACE); ex.More(); ex.Next()){
835 const TopoDS_Face& curface = TopoDS::Face(ex.Current());
836 if(!Done.Add(curface)) continue;
837 BS.Initialize(curface);
838 if(BS.GetType() != GeomAbs_Plane) continue;
840 for(ex2.Init(curface,TopAbs_EDGE); ex2.More(); ex2.Next()){
841 const TopoDS_Edge& E = TopoDS::Edge(ex2.Current());
842 SetEdgeTol(E,curface);
845 BRepLib::UpdateTolerances(S);
848 //================================================================
849 //function : SameParameter
850 //WARNING : New spec DUB LBO 9/9/97.
851 // Recode in the edge the best tolerance found,
852 // for vertex extremities it is required to find something else
853 //================================================================
854 static Standard_Boolean EvalTol(const Handle(Geom2d_Curve)& pc,
855 const Handle(Geom_Surface)& s,
856 const GeomAdaptor_Curve& gac,
857 const Standard_Real tol,
858 Standard_Real& tolbail)
860 Standard_Integer ok = 0;
861 Standard_Real f = gac.FirstParameter();
862 Standard_Real l = gac.LastParameter();
863 Extrema_LocateExtPC Projector;
864 Projector.Initialize(gac,f,l,tol);
868 for(Standard_Integer i = 1; i <= 5; i++){
869 Standard_Real t = i/6.;
870 t = (1.-t) * f + t * l;
871 pc->Value(t).Coord(u,v);
873 Projector.Perform(p,t);
874 if (Projector.IsDone()) {
875 Standard_Real dist2 = Projector.SquareDistance();
876 if(dist2 > tolbail * tolbail) tolbail = sqrt (dist2);
883 static Standard_Real ComputeTol(const Handle(Adaptor3d_HCurve)& c3d,
884 const Handle(Adaptor2d_HCurve2d)& c2d,
885 const Handle(Adaptor3d_HSurface)& surf,
886 const Standard_Integer nbp)
890 TColStd_Array1OfReal dist(1,nbp+10);
894 Adaptor3d_CurveOnSurface cons(c2d,surf);
895 Standard_Real d2 = 0.;
896 Standard_Integer nn = nbp;
897 Standard_Real unsurnn = 1./nn;
898 Standard_Real first = c3d->FirstParameter();
899 Standard_Real last = c3d->LastParameter();
900 Standard_Integer i = 0;
901 for(i = 0; i <= nn; i++){
902 Standard_Real t = unsurnn*i;
903 Standard_Real u = first*(1.-t) + last*t;
904 gp_Pnt Pc3d = c3d->Value(u);
905 gp_Pnt Pcons = cons.Value(u);
906 if (Precision::IsInfinite(Pcons.X()) ||
907 Precision::IsInfinite(Pcons.Y()) ||
908 Precision::IsInfinite(Pcons.Z())) {
909 d2=Precision::Infinite();
912 Standard_Real temp = Pc3d.SquareDistance(Pcons);
918 if(temp > d2) d2 = temp;
922 Standard_Boolean ana = Standard_False;
923 Standard_Real D2 = 0;
924 Standard_Integer N1 = 0;
925 Standard_Integer N2 = 0;
926 Standard_Integer N3 = 0;
928 for( i = 1; i<= nbp+10; i++)
930 if( dist(i) < 1.0 ) N1++;
934 if( N1 > N2 && N2 != 0 ) N3 = 100*N2/(N1+N2);
935 if( N3 < 10 && N3 != 0 ) {
937 for( i = 1; i<= nbp+10; i++)
938 if( dist(i) > 0 && dist(i) < 1.0 )
939 if( dist(i) > D2 ) D2 = dist(i);
943 d2 = (!ana) ? 1.5*sqrt(d2) : 1.5*sqrt(D2);
944 if(d2<1.e-7) d2 = 1.e-7;
951 void BRepLib::SameParameter(const TopoDS_Edge& AnEdge,
952 const Standard_Real Tolerance)
954 if (BRep_Tool::SameParameter(AnEdge)) return;
956 const Standard_Integer NCONTROL = 22;
958 Handle(GeomAdaptor_HCurve) HC = new GeomAdaptor_HCurve();
959 Handle(Geom2dAdaptor_HCurve) HC2d = new Geom2dAdaptor_HCurve();
960 Handle(GeomAdaptor_HSurface) HS = new GeomAdaptor_HSurface();
961 GeomAdaptor_Curve& GAC = HC->ChangeCurve();
962 Geom2dAdaptor_Curve& GAC2d = HC2d->ChangeCurve2d();
963 GeomAdaptor_Surface& GAS = HS->ChangeSurface();
965 Standard_Real f3d =0.,l3d =0.;
967 Standard_Real f3d,l3d;
970 Handle(Geom_Curve) C3d;
972 const Handle(BRep_TEdge)& TE = *((Handle(BRep_TEdge)*) &AnEdge.TShape());
973 BRep_ListOfCurveRepresentation& CList = TE->ChangeCurves();
974 BRep_ListIteratorOfListOfCurveRepresentation It(CList);
976 Standard_Boolean NotDone = Standard_True;
978 while (NotDone && It.More()) {
979 Handle(BRep_GCurve) GCurve = Handle(BRep_GCurve)::DownCast(It.Value());
980 if (!GCurve.IsNull() && GCurve->IsCurve3D()) {
981 C3d = GCurve->Curve3D() ;
982 f3d = GCurve->First();
983 l3d = GCurve->Last();
984 L3d = GCurve->Location() ;
985 NotDone = Standard_False;
990 if(C3d.IsNull()) return;
992 // modified by NIZHNY-OCC486 Tue Aug 27 17:15:13 2002 :
993 Standard_Boolean m_TrimmedPeriodical = Standard_False;
994 Handle(Standard_Type) TheType = C3d->DynamicType();
995 if( TheType == STANDARD_TYPE(Geom_TrimmedCurve))
997 const Handle(Geom_Curve)& gtC = (*((Handle(Geom_TrimmedCurve)*)&C3d))->BasisCurve();
998 m_TrimmedPeriodical = gtC->IsPeriodic();
1000 // modified by NIZHNY-OCC486 Tue Aug 27 17:15:17 2002 .
1003 if(!C3d->IsPeriodic()) {
1004 Standard_Real Udeb = C3d->FirstParameter();
1005 Standard_Real Ufin = C3d->LastParameter();
1006 // modified by NIZHNY-OCC486 Tue Aug 27 17:17:14 2002 :
1007 //if (Udeb > f3d) f3d = Udeb;
1008 //if (l3d > Ufin) l3d = Ufin;
1009 if(!m_TrimmedPeriodical)
1011 if (Udeb > f3d) f3d = Udeb;
1012 if (l3d > Ufin) l3d = Ufin;
1014 // modified by NIZHNY-OCC486 Tue Aug 27 17:17:55 2002 .
1016 if(!L3d.IsIdentity()){
1017 C3d = Handle(Geom_Curve)::DownCast(C3d->Transformed(L3d.Transformation()));
1019 GAC.Load(C3d,f3d,l3d);
1021 Standard_Boolean IsSameP = 1;
1022 Standard_Real maxdist = 0.;
1024 // Modified by skv - Thu Jun 3 12:39:19 2004 OCC5898 Begin
1025 Standard_Real anEdgeTol = BRep_Tool::Tolerance(AnEdge);
1026 // Modified by skv - Thu Jun 3 12:39:20 2004 OCC5898 End
1027 Standard_Boolean SameRange = BRep_Tool::SameRange(AnEdge);
1028 Standard_Boolean YaPCu = Standard_False;
1029 It.Initialize(CList);
1032 Standard_Boolean isANA = Standard_False;
1033 Standard_Boolean isBSP = Standard_False;
1034 Handle(BRep_GCurve) GCurve = Handle(BRep_GCurve)::DownCast(It.Value());
1035 Handle(Geom2d_Curve) PC[2];
1036 Handle(Geom_Surface) S;
1037 if (!GCurve.IsNull() && GCurve->IsCurveOnSurface()) {
1038 YaPCu = Standard_True;
1039 PC[0] = GCurve->PCurve();
1040 TopLoc_Location PCLoc = GCurve->Location();
1041 S = GCurve->Surface();
1042 if (!PCLoc.IsIdentity() ) {
1043 S = Handle(Geom_Surface)::DownCast(S->Transformed(PCLoc.Transformation()));
1046 if (GCurve->IsCurveOnClosedSurface()) {
1047 PC[1] = GCurve->PCurve2();
1050 // Eval tol2d to compute SameRange
1051 Standard_Real UResol = GAS.UResolution(Tolerance);
1052 Standard_Real VResol = GAS.VResolution(Tolerance);
1053 Standard_Real Tol2d = Min(UResol, VResol);
1054 for(Standard_Integer i = 0; i < 2; i++){
1055 Handle(Geom2d_Curve) curPC = PC[i];
1056 Standard_Boolean updatepc = 0;
1057 if(curPC.IsNull()) break;
1059 GeomLib::SameRange(Tol2d,
1060 PC[i],GCurve->First(),GCurve->Last(),
1063 updatepc = (curPC != PC[i]);
1066 Standard_Boolean goodpc = 1;
1067 GAC2d.Load(curPC,f3d,l3d);
1069 Standard_Real error = ComputeTol(HC, HC2d, HS, NCONTROL);
1071 if(GAC2d.GetType() == GeomAbs_BSplineCurve &&
1072 GAC2d.Continuity() == GeomAbs_C0) {
1073 Handle(Geom2d_BSplineCurve) bs2d = GAC2d.BSpline();
1074 Handle(Geom2d_BSplineCurve) bs2dsov = bs2d;
1075 Standard_Real fC0 = bs2d->FirstParameter(), lC0 = bs2d->LastParameter();
1076 Standard_Boolean repar = Standard_True;
1077 gp_Pnt2d OriginPoint;
1078 bs2d->D0(fC0, OriginPoint);
1079 Geom2dConvert::C0BSplineToC1BSplineCurve(bs2d, Tol2d);
1080 isBSP = Standard_True;
1082 if(bs2d->IsPeriodic()) { // -------- IFV, Jan 2000
1083 gp_Pnt2d NewOriginPoint;
1084 bs2d->D0(bs2d->FirstParameter(), NewOriginPoint);
1085 if(Abs(OriginPoint.X() - NewOriginPoint.X()) > Precision::PConfusion() ||
1086 Abs(OriginPoint.Y() - NewOriginPoint.Y()) > Precision::PConfusion() ) {
1088 TColStd_Array1OfReal Knotbs2d (1, bs2d->NbKnots());
1089 bs2d->Knots(Knotbs2d);
1091 for(Standard_Integer Index = 1; Index <= bs2d->NbKnots(); Index++) {
1092 bs2d->D0(Knotbs2d(Index), NewOriginPoint);
1093 if(Abs(OriginPoint.X() - NewOriginPoint.X()) > Precision::PConfusion() ||
1094 Abs(OriginPoint.Y() - NewOriginPoint.Y()) > Precision::PConfusion() ) continue;
1096 bs2d->SetOrigin(Index);
1102 if(bs2d->Continuity() == GeomAbs_C0) {
1103 Standard_Real tolbail;
1104 if(EvalTol(curPC,S,GAC,Tolerance,tolbail)){
1106 Standard_Real UResbail = GAS.UResolution(tolbail);
1107 Standard_Real VResbail = GAS.VResolution(tolbail);
1108 Standard_Real Tol2dbail = Min(UResbail,VResbail);
1109 bs2d->D0(bs2d->FirstParameter(), OriginPoint);
1111 Standard_Integer nbp = bs2d->NbPoles();
1112 TColgp_Array1OfPnt2d poles(1,nbp);
1114 gp_Pnt2d p = poles(1), p1;
1115 Standard_Real d = Precision::Infinite();
1116 for(Standard_Integer ip = 2; ip <= nbp; ip++) {
1118 d = Min(d,p.SquareDistance(p1));
1123 Tol2dbail = Max(Min(Tol2dbail,d),Tol2d);
1125 Geom2dConvert::C0BSplineToC1BSplineCurve(bs2d,Tol2dbail);
1127 if(bs2d->IsPeriodic()) { // -------- IFV, Jan 2000
1128 gp_Pnt2d NewOriginPoint;
1129 bs2d->D0(bs2d->FirstParameter(), NewOriginPoint);
1130 if(Abs(OriginPoint.X() - NewOriginPoint.X()) > Precision::PConfusion() ||
1131 Abs(OriginPoint.Y() - NewOriginPoint.Y()) > Precision::PConfusion() ) {
1133 TColStd_Array1OfReal Knotbs2d (1, bs2d->NbKnots());
1134 bs2d->Knots(Knotbs2d);
1136 for(Standard_Integer Index = 1; Index <= bs2d->NbKnots(); Index++) {
1137 bs2d->D0(Knotbs2d(Index), NewOriginPoint);
1138 if(Abs(OriginPoint.X() - NewOriginPoint.X()) > Precision::PConfusion() ||
1139 Abs(OriginPoint.Y() - NewOriginPoint.Y()) > Precision::PConfusion() ) continue;
1141 bs2d->SetOrigin(Index);
1148 if(bs2d->Continuity() == GeomAbs_C0) {
1151 repar = Standard_False;
1159 Standard_Integer NbKnots = bs2d->NbKnots();
1160 TColStd_Array1OfReal Knots(1,NbKnots);
1162 // BSplCLib::Reparametrize(f3d,l3d,Knots);
1163 BSplCLib::Reparametrize(fC0,lC0,Knots);
1164 bs2d->SetKnots(Knots);
1165 GAC2d.Load(bs2d,f3d,l3d);
1167 Standard_Boolean updatepcsov = updatepc;
1168 updatepc = Standard_True;
1170 Standard_Real error1 = ComputeTol(HC, HC2d, HS, NCONTROL);
1171 if(error1 > error) {
1173 GAC2d.Load(bs2d,f3d,l3d);
1175 updatepc = updatepcsov;
1176 isANA = Standard_True;
1183 //check, if new BSpline "good" or not --------- IFV, Jan of 2000
1184 GeomAbs_Shape cont = bs2d->Continuity();
1185 Standard_Boolean IsBad = Standard_False;
1187 if(cont > GeomAbs_C0 && error > Max(1.e-3,Tolerance)) {
1188 Standard_Integer NbKnots = bs2d->NbKnots();
1189 TColStd_Array1OfReal Knots(1,NbKnots);
1191 Standard_Real critratio = 10.;
1192 Standard_Real dtprev = Knots(2) - Knots(1), dtratio = 1.;
1193 Standard_Real dtmin = dtprev;
1194 Standard_Real dtcur;
1195 for(Standard_Integer j = 2; j < NbKnots; j++) {
1196 dtcur = Knots(j+1) - Knots(j);
1197 dtmin = Min(dtmin, dtcur);
1201 if(dtcur > dtprev) dtratio = dtcur/dtprev;
1202 else dtratio = dtprev/dtcur;
1203 if(dtratio > critratio) {IsBad = Standard_True;}
1208 // To avoid failures in Approx_CurvilinearParameter
1209 bs2d->Resolution(Max(1.e-3,Tolerance), dtcur);
1210 if(dtmin < dtcur) IsBad = Standard_False;
1215 if(IsBad ) { //if BSpline "bad", try to reparametrize it
1216 // by its curve length
1218 // GeomAbs_Shape cont = bs2d->Continuity();
1219 if(cont > GeomAbs_C2) cont = GeomAbs_C2;
1220 Standard_Integer maxdeg = bs2d->Degree();
1221 if(maxdeg == 1) maxdeg = 14;
1222 Approx_CurvilinearParameter AppCurPar(HC2d, HS, Max(1.e-3,Tolerance),
1224 if(AppCurPar.IsDone() || AppCurPar.HasResult()) {
1225 bs2d = AppCurPar.Curve2d1();
1226 GAC2d.Load(bs2d,f3d,l3d);
1229 if(Abs(bs2d->FirstParameter() - fC0) > Tol2d ||
1230 Abs(bs2d->LastParameter() - lC0) > Tol2d ) {
1231 Standard_Integer NbKnots = bs2d->NbKnots();
1232 TColStd_Array1OfReal Knots(1,NbKnots);
1234 // BSplCLib::Reparametrize(f3d,l3d,Knots);
1235 BSplCLib::Reparametrize(fC0,lC0,Knots);
1236 bs2d->SetKnots(Knots);
1237 GAC2d.Load(bs2d,f3d,l3d);
1250 // Approx_SameParameter SameP(HC,HC2d,HS,Tolerance);
1251 Standard_Real aTol = (isANA && isBSP) ? 1.e-7 : Tolerance;
1252 Approx_SameParameter SameP(HC,HC2d,HS,aTol);
1254 if (SameP.IsSameParameter()) {
1255 maxdist = Max(maxdist,SameP.TolReached());
1257 if (i == 0) GCurve->PCurve(curPC);
1258 else GCurve->PCurve2(curPC);
1261 else if (SameP.IsDone()) {
1262 Standard_Real tolreached = SameP.TolReached();
1263 if(tolreached < error) {
1264 curPC = SameP.Curve2d();
1265 updatepc = Standard_True;
1266 maxdist = Max(maxdist,tolreached);
1269 maxdist = Max(maxdist, error);
1272 if (i == 0) GCurve->PCurve(curPC);
1273 else GCurve->PCurve2(curPC);
1281 // Modified by skv - Thu Jun 3 12:39:19 2004 OCC5898 Begin
1283 if (anEdgeTol > error) {
1284 maxdist = Max(maxdist, anEdgeTol);
1285 IsSameP = Standard_True;
1288 // Modified by skv - Thu Jun 3 12:39:20 2004 OCC5898 End
1293 B.Range(AnEdge,f3d,l3d);
1294 B.SameRange(AnEdge,Standard_True);
1296 // Reduce eventually the tolerance of the edge, as
1297 // all its representations are processed (except for some associated
1298 // to planes and not stored in the edge !)
1299 // The same cannot be done with vertices that cannot be enlarged
1302 // Avoid setting too small tolerances.
1303 maxdist = Max(maxdist,Precision::Confusion());
1304 TopoDS_Vertex V1,V2;
1305 TopExp::Vertices(AnEdge,V1,V2);
1307 B.UpdateVertex(V1,maxdist);
1309 B.UpdateVertex(V2,maxdist);
1310 TE->Modified(Standard_True);
1311 TE->Tolerance(maxdist);
1313 B.SameParameter(AnEdge,Standard_True);
1317 //=======================================================================
1318 //function : UpdateTolerances
1320 //=======================================================================
1321 void BRepLib::UpdateTolerances(const TopoDS_Shape& aShape,
1322 const Standard_Boolean verifyTolerance)
1325 // Harmonize tolerances
1326 // with rule Tolerance(VERTEX)>=Tolerance(EDGE)>=Tolerance(FACE)
1328 Standard_Real tol=0;
1329 if (verifyTolerance) {
1330 // Set tolerance to its minimum value
1331 Handle(Geom_Surface) S;
1335 Standard_Real aXmin, aYmin, aZmin, aXmax, aYmax, aZmax, dMax;
1336 for (ex.Init(aShape, TopAbs_FACE); ex.More(); ex.Next()) {
1337 const TopoDS_Face& curf=TopoDS::Face(ex.Current());
1338 S = BRep_Tool::Surface(curf, l);
1341 BRepBndLib::Add(curf,aB);
1342 if (S->DynamicType() == STANDARD_TYPE(Geom_RectangularTrimmedSurface)) {
1343 S = (*((Handle(Geom_RectangularTrimmedSurface)*)&S))->BasisSurface();
1345 GeomAdaptor_Surface AS(S);
1346 switch (AS.GetType()) {
1348 case GeomAbs_Cylinder:
1351 tol=Precision::Confusion();
1354 case GeomAbs_Sphere:
1357 tol=Precision::Confusion()*2;
1361 tol=Precision::Confusion()*4;
1363 if (!aB.IsWhole()) {
1364 aB.Get(aXmin, aYmin, aZmin, aXmax, aYmax, aZmax);
1366 if (!aB.IsOpenXmin() && !aB.IsOpenXmax()) dMax=aXmax-aXmin;
1367 if (!aB.IsOpenYmin() && !aB.IsOpenYmax()) aYmin=aYmax-aYmin;
1368 if (!aB.IsOpenZmin() && !aB.IsOpenZmax()) aZmin=aZmax-aZmin;
1369 if (aYmin>dMax) dMax=aYmin;
1370 if (aZmin>dMax) dMax=aZmin;
1372 // Do not process tolerances > 1.
1373 if (tol>1.) tol=0.99;
1375 const Handle(BRep_TFace)& Tf = *((Handle(BRep_TFace)*)&curf.TShape());
1382 TopTools_IndexedDataMapOfShapeListOfShape parents;
1383 TopExp::MapShapesAndAncestors(aShape, TopAbs_EDGE, TopAbs_FACE, parents);
1384 TopTools_ListIteratorOfListOfShape lConx;
1385 Standard_Integer iCur;
1386 for (iCur=1; iCur<=parents.Extent(); iCur++) {
1388 for (lConx.Initialize(parents(iCur)); lConx.More(); lConx.Next()) {
1389 tol=Max(tol, BRep_Tool::Tolerance(TopoDS::Face(lConx.Value())));
1391 // Update can only increase tolerance, so if the edge has a greater
1392 // tolerance than its faces it is not concerned
1393 B.UpdateEdge(TopoDS::Edge(parents.FindKey(iCur)), tol);
1396 //Vertices are processed
1398 TopExp::MapShapesAndAncestors(aShape, TopAbs_VERTEX, TopAbs_EDGE, parents);
1399 TColStd_MapOfTransient Initialized;
1400 TopTools_MapOfShape Done;
1401 Standard_Integer nbV = parents.Extent();
1402 for (iCur=1; iCur<=nbV; iCur++) {
1405 const TopoDS_Vertex& V = TopoDS::Vertex(parents.FindKey(iCur));
1407 box.Add(BRep_Tool::Pnt(V));
1409 for (lConx.Initialize(parents(iCur)); lConx.More(); lConx.Next()) {
1410 const TopoDS_Edge& E = TopoDS::Edge(lConx.Value());
1411 if(!Done.Add(E)) continue;
1412 tol=Max(tol, BRep_Tool::Tolerance(E));
1413 if(!BRep_Tool::SameRange(E)) continue;
1414 Standard_Real par = BRep_Tool::Parameter(V,E);
1415 Handle(BRep_TEdge)& TE = *((Handle(BRep_TEdge)*)&E.TShape());
1416 BRep_ListIteratorOfListOfCurveRepresentation itcr(TE->Curves());
1417 const TopLoc_Location& Eloc = E.Location();
1418 while (itcr.More()) {
1419 // For each CurveRepresentation, check the provided parameter
1420 const Handle(BRep_CurveRepresentation)& cr = itcr.Value();
1421 const TopLoc_Location& loc = cr->Location();
1422 TopLoc_Location L = (Eloc * loc);
1423 if (cr->IsCurve3D()) {
1424 const Handle(Geom_Curve)& C = cr->Curve3D();
1425 if (!C.IsNull()) { // edge non degenerated
1426 p3d = C->Value(par);
1427 p3d.Transform(L.Transformation());
1431 else if (cr->IsCurveOnSurface()) {
1432 const Handle(Geom_Surface)& Su = cr->Surface();
1433 const Handle(Geom2d_Curve)& PC = cr->PCurve();
1434 Handle(Geom2d_Curve) PC2;
1435 if (cr->IsCurveOnClosedSurface()) {
1436 PC2 = cr->PCurve2();
1438 gp_Pnt2d p2d = PC->Value(par);
1439 p3d = Su->Value(p2d.X(),p2d.Y());
1440 p3d.Transform(L.Transformation());
1442 if (!PC2.IsNull()) {
1443 p2d = PC2->Value(par);
1444 p3d = Su->Value(p2d.X(),p2d.Y());
1445 p3d.Transform(L.Transformation());
1452 Standard_Real aXmin, aYmin, aZmin, aXmax, aYmax, aZmax;
1453 box.Get(aXmin, aYmin, aZmin, aXmax, aYmax, aZmax);
1454 aXmax -= aXmin; aYmax -= aYmin; aZmax -= aZmin;
1455 tol = Max(tol,sqrt(aXmax*aXmax+aYmax*aYmax+aZmax*aZmax));
1456 tol += 2.*Epsilon(tol);
1457 if (verifyTolerance) {
1458 // ASet minimum value of the tolerance
1459 // Attention to sharing of the vertex by other shapes
1460 const Handle(BRep_TVertex)& TV = *((Handle(BRep_TVertex)*)&V.TShape());
1461 if (Initialized.Add(TV))
1464 B.UpdateVertex(V, tol);
1467 // Update can only increase tolerance, so if the edge has a greater
1468 // tolerance than its faces it is not concerned
1469 B.UpdateVertex(V, tol);
1474 //=======================================================================
1475 //function : OrientClosedSolid
1477 //=======================================================================
1478 Standard_Boolean BRepLib::OrientClosedSolid(TopoDS_Solid& solid)
1480 // Set material inside the solid
1481 BRepClass3d_SolidClassifier where(solid);
1482 where.PerformInfinitePoint(Precision::Confusion());
1483 if (where.State()==TopAbs_IN) {
1486 else if (where.State()==TopAbs_ON || where.State()==TopAbs_UNKNOWN)
1487 return Standard_False;
1489 return Standard_True;
1492 //=======================================================================
1493 //function : tgtfaces
1494 //purpose : check the angle at the border between two squares.
1495 // Two shares should have a shared front edge.
1496 //=======================================================================
1498 static Standard_Boolean tgtfaces(const TopoDS_Edge& Ed,
1499 const TopoDS_Face& F1,
1500 const TopoDS_Face& F2,
1501 const Standard_Real ta,
1502 const Standard_Boolean couture)
1506 BRepAdaptor_Surface aBAS1(F1,Standard_False);
1507 BRepAdaptor_Surface aBAS2(F2,Standard_False);
1508 Handle(BRepAdaptor_HSurface) HS1 = new BRepAdaptor_HSurface(aBAS1);
1509 Handle(BRepAdaptor_HSurface) HS2;
1510 if(couture) HS2 = HS1;
1511 else HS2 = new BRepAdaptor_HSurface(aBAS2);
1513 E.Orientation(TopAbs_FORWARD);
1514 Handle(BRepAdaptor_HCurve2d) HC2d1 = new BRepAdaptor_HCurve2d();
1515 HC2d1->ChangeCurve2d().Initialize(E,F1);
1516 if(couture) E.Orientation(TopAbs_REVERSED);
1517 Handle(BRepAdaptor_HCurve2d) HC2d2 = new BRepAdaptor_HCurve2d();
1518 HC2d2->ChangeCurve2d().Initialize(E,F2);
1519 Adaptor3d_CurveOnSurface C1(HC2d1,HS1);
1520 Adaptor3d_CurveOnSurface C2(HC2d2,HS2);
1522 Standard_Boolean rev1 = (F1.Orientation() == TopAbs_REVERSED);
1523 Standard_Boolean rev2 = (F2.Orientation() == TopAbs_REVERSED);
1524 Standard_Real f,l,eps, angmax = -M_PI;
1526 Standard_Real ang =0.;
1530 BRep_Tool::Range(E,f,l);
1531 Extrema_LocateExtPC ext;
1532 Standard_Boolean IsInitialized = Standard_False;
1535 f += eps; // to avoid calculations on
1536 l -= eps; // points of pointed squares.
1538 gp_Pnt pp1,pp2;//,PP;
1541 Standard_Real uu, vv, norm;
1544 Standard_Boolean Nok;
1545 for(i = 0; (i<= 20) && (angmax<=ta) ; i++){
1546 // First suppose that this is sameParameter
1547 Nok = Standard_True;
1550 HS1->D1(p.X(),p.Y(),pp1,du,dv);
1551 d1 = (du.Crossed(dv));
1552 norm = d1.Magnitude();
1553 if (norm > 1.e-12) d1 /= norm;
1554 else Nok=Standard_False;
1555 if(rev1) d1.Reverse();
1558 HS2->D1(p.X(), p.Y(), pp2, du, dv);
1559 d2 = (du.Crossed(dv));
1560 norm = d2.Magnitude();
1561 if (norm> 1.e-12) d2 /= norm;
1562 else Nok=Standard_False;
1563 if(rev2) d2.Reverse();
1564 if (Nok) ang = d1.Angle(d2);
1566 if (Nok &&(ang > ta)) { // Refine by projection
1567 if (! IsInitialized ) {
1568 ext.Initialize(C2,f,l,Precision::PConfusion());
1569 IsInitialized = Standard_True;
1572 if(ext.IsDone() && ext.IsMin()){
1573 Extrema_POnCurv poc = ext.Point();
1574 Standard_Real v = poc.Parameter();
1578 HS2->D1(p.X(), p.Y(), pp2, du, dv);
1579 d2 = (du.Crossed(dv));
1580 norm = d2.Magnitude();
1581 if (norm> 1.e-12) d2 /= norm;
1582 else Nok = Standard_False;
1583 if(rev2) d2.Reverse();
1584 if (Nok) ang = d1.Angle(d2);
1587 if(ang >= angmax) angmax = ang;
1590 return (angmax<=ta);
1595 //=======================================================================
1596 // function : EncodeRegularity
1597 // purpose : code the regularities on all edges of the shape, boundary of
1598 // two faces that do not have it.
1599 //=======================================================================
1601 void BRepLib::EncodeRegularity(const TopoDS_Shape& S,
1602 const Standard_Real TolAng)
1605 TopTools_IndexedDataMapOfShapeListOfShape M;
1606 TopExp::MapShapesAndAncestors(S,TopAbs_EDGE,TopAbs_FACE,M);
1607 TopTools_ListIteratorOfListOfShape It;
1610 Standard_Boolean found, couture;
1611 for(Standard_Integer i = 1; i <= M.Extent(); i++){
1612 TopoDS_Edge E = TopoDS::Edge(M.FindKey(i));
1613 found = Standard_False; couture = Standard_False;
1615 for(It.Initialize(M.FindFromIndex(i));It.More() && !found;It.Next()){
1616 if(F1.IsNull()) { F1 = TopoDS::Face(It.Value()); }
1618 if(!F1.IsSame(TopoDS::Face(It.Value()))){
1619 found = Standard_True;
1620 F2 = TopoDS::Face(It.Value());
1624 if (!found && !F1.IsNull()){//is it a sewing edge?
1625 TopAbs_Orientation orE = E.Orientation();
1627 for(Ex.Init(F1,TopAbs_EDGE);Ex.More() && !found;Ex.Next()){
1628 curE= TopoDS::Edge(Ex.Current());
1629 if(E.IsSame(curE) && orE != curE.Orientation()) {
1630 found = Standard_True;
1631 couture = Standard_True;
1637 if(BRep_Tool::Continuity(E,F1,F2)<=GeomAbs_C0){
1640 if(tgtfaces(E, F1, F2, TolAng, couture)){
1641 B.Continuity(E,F1,F2,GeomAbs_G1);
1644 catch(Standard_Failure)
1652 //=======================================================================
1653 // function : EncodeRegularity
1654 // purpose : code the regularity between 2 faces on an edge
1655 //=======================================================================
1657 void BRepLib::EncodeRegularity(TopoDS_Edge& E,
1658 const TopoDS_Face& F1,
1659 const TopoDS_Face& F2,
1660 const Standard_Real TolAng)
1663 if(BRep_Tool::Continuity(E,F1,F2)<=GeomAbs_C0){
1665 if( tgtfaces(E, F1, F2, TolAng, F1.IsEqual(F2))) {
1666 B.Continuity(E,F1,F2,GeomAbs_G1);
1669 catch(Standard_Failure)
1675 //=======================================================================
1676 //function : SortFaces
1678 //=======================================================================
1680 void BRepLib::SortFaces (const TopoDS_Shape& Sh,
1681 TopTools_ListOfShape& LF)
1684 TopTools_ListOfShape LTri,LPlan,LCyl,LCon,LSphere,LTor,LOther;
1685 TopExp_Explorer exp(Sh,TopAbs_FACE);
1687 Handle(Geom_Surface) S;
1689 for (; exp.More(); exp.Next()) {
1690 const TopoDS_Face& F = TopoDS::Face(exp.Current());
1691 S = BRep_Tool::Surface(F, l);
1693 if (S->DynamicType() == STANDARD_TYPE(Geom_RectangularTrimmedSurface)) {
1694 S = (*((Handle(Geom_RectangularTrimmedSurface)*)&S))->BasisSurface();
1696 GeomAdaptor_Surface AS(S);
1697 switch (AS.GetType()) {
1703 case GeomAbs_Cylinder:
1713 case GeomAbs_Sphere:
1727 else LTri.Append(F);
1729 LF.Append(LPlan); LF.Append(LCyl ); LF.Append(LCon); LF.Append(LSphere);
1730 LF.Append(LTor ); LF.Append(LOther); LF.Append(LTri);
1733 //=======================================================================
1734 //function : ReverseSortFaces
1736 //=======================================================================
1738 void BRepLib::ReverseSortFaces (const TopoDS_Shape& Sh,
1739 TopTools_ListOfShape& LF)
1742 TopTools_ListOfShape LTri,LPlan,LCyl,LCon,LSphere,LTor,LOther;
1743 TopExp_Explorer exp(Sh,TopAbs_FACE);
1745 Handle(Geom_Surface) S;
1747 for (; exp.More(); exp.Next()) {
1748 const TopoDS_Face& F = TopoDS::Face(exp.Current());
1749 S = BRep_Tool::Surface(F, l);
1751 if (S->DynamicType() == STANDARD_TYPE(Geom_RectangularTrimmedSurface)) {
1752 S = (*((Handle(Geom_RectangularTrimmedSurface)*)&S))->BasisSurface();
1754 GeomAdaptor_Surface AS(S);
1755 switch (AS.GetType()) {
1761 case GeomAbs_Cylinder:
1771 case GeomAbs_Sphere:
1785 else LTri.Append(F);
1787 LF.Append(LTri); LF.Append(LOther); LF.Append(LTor ); LF.Append(LSphere);
1788 LF.Append(LCon); LF.Append(LCyl ); LF.Append(LPlan);