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 <Adaptor3d_CurveOnSurface.hxx>
21 #include <AdvApprox_ApproxAFunction.hxx>
22 #include <AppParCurves_MultiBSpCurve.hxx>
23 #include <AppParCurves_MultiCurve.hxx>
24 #include <Approx_CurvilinearParameter.hxx>
25 #include <Approx_SameParameter.hxx>
26 #include <Bnd_Box.hxx>
27 #include <BRep_Builder.hxx>
28 #include <BRep_CurveRepresentation.hxx>
29 #include <BRep_GCurve.hxx>
30 #include <BRep_ListIteratorOfListOfCurveRepresentation.hxx>
31 #include <BRep_ListOfCurveRepresentation.hxx>
32 #include <BRep_TEdge.hxx>
33 #include <BRep_TFace.hxx>
34 #include <BRep_Tool.hxx>
35 #include <BRep_TVertex.hxx>
36 #include <BRepAdaptor_HCurve.hxx>
37 #include <BRepAdaptor_HCurve2d.hxx>
38 #include <BRepAdaptor_HSurface.hxx>
39 #include <BRepAdaptor_Surface.hxx>
40 #include <BRepBndLib.hxx>
41 #include <BRepClass3d_SolidClassifier.hxx>
42 #include <BRepLib.hxx>
43 #include <BSplCLib.hxx>
45 #include <Extrema_LocateExtPC.hxx>
46 #include <GCPnts_QuasiUniformDeflection.hxx>
47 #include <Geom2d_BSplineCurve.hxx>
48 #include <Geom2d_Curve.hxx>
49 #include <Geom2d_TrimmedCurve.hxx>
50 #include <Geom2dAdaptor.hxx>
51 #include <Geom2dAdaptor_Curve.hxx>
52 #include <Geom2dAdaptor_HCurve.hxx>
53 #include <Geom2dConvert.hxx>
54 #include <Geom_BSplineCurve.hxx>
55 #include <Geom_BSplineSurface.hxx>
56 #include <Geom_Curve.hxx>
57 #include <Geom_Plane.hxx>
58 #include <Geom_RectangularTrimmedSurface.hxx>
59 #include <Geom_Surface.hxx>
60 #include <Geom_TrimmedCurve.hxx>
61 #include <GeomAdaptor_Curve.hxx>
62 #include <GeomAdaptor_HCurve.hxx>
63 #include <GeomAdaptor_HSurface.hxx>
64 #include <GeomAdaptor_Surface.hxx>
65 #include <GeomLib.hxx>
66 #include <GeomLProp_SLProps.hxx>
70 #include <Poly_PolygonOnTriangulation.hxx>
71 #include <Poly_Triangulation.hxx>
72 #include <Precision.hxx>
73 #include <ProjLib_ProjectedCurve.hxx>
74 #include <Standard_Real.hxx>
75 #include <TColgp_Array1OfPnt.hxx>
76 #include <TColgp_Array1OfPnt2d.hxx>
77 #include <TColStd_Array1OfReal.hxx>
78 #include <TColStd_MapOfTransient.hxx>
80 #include <TopExp_Explorer.hxx>
82 #include <TopoDS_Edge.hxx>
83 #include <TopoDS_Face.hxx>
84 #include <TopoDS_Shape.hxx>
85 #include <TopoDS_Solid.hxx>
86 #include <TopoDS_Vertex.hxx>
87 #include <TopTools_IndexedDataMapOfShapeListOfShape.hxx>
88 #include <TopTools_ListIteratorOfListOfShape.hxx>
89 #include <TopTools_MapOfShape.hxx>
90 #include <TShort_HArray1OfShortReal.hxx>
92 // TODO - not thread-safe static variables
93 static Standard_Real thePrecision = Precision::Confusion();
94 static Handle(Geom_Plane) thePlane;
96 //=======================================================================
97 //function : Precision
99 //=======================================================================
101 void BRepLib::Precision(const Standard_Real P)
106 //=======================================================================
107 //function : Precision
109 //=======================================================================
111 Standard_Real BRepLib::Precision()
116 //=======================================================================
119 //=======================================================================
121 void BRepLib::Plane(const Handle(Geom_Plane)& P)
127 //=======================================================================
130 //=======================================================================
132 const Handle(Geom_Plane)& BRepLib::Plane()
134 if (thePlane.IsNull()) thePlane = new Geom_Plane(gp::XOY());
137 //=======================================================================
138 //function : CheckSameRange
140 //=======================================================================
142 Standard_Boolean BRepLib::CheckSameRange(const TopoDS_Edge& AnEdge,
143 const Standard_Real Tolerance)
145 Standard_Boolean IsSameRange = Standard_True,
146 first_time_in = Standard_True ;
148 BRep_ListIteratorOfListOfCurveRepresentation an_Iterator
149 ((*((Handle(BRep_TEdge)*)&AnEdge.TShape()))->ChangeCurves());
151 Standard_Real first, last;
152 Standard_Real current_first =0., current_last =0. ;
153 Handle(BRep_GCurve) geometric_representation_ptr ;
155 while (IsSameRange && an_Iterator.More()) {
156 geometric_representation_ptr =
157 Handle(BRep_GCurve)::DownCast(an_Iterator.Value());
158 if (!geometric_representation_ptr.IsNull()) {
160 first = geometric_representation_ptr->First();
161 last = geometric_representation_ptr->Last();
162 if (first_time_in ) {
163 current_first = first ;
164 current_last = last ;
165 first_time_in = Standard_False ;
168 IsSameRange = (Abs(current_first - first) <= Tolerance)
169 && (Abs(current_last -last) <= Tolerance ) ;
177 //=======================================================================
178 //function : SameRange
180 //=======================================================================
182 void BRepLib::SameRange(const TopoDS_Edge& AnEdge,
183 const Standard_Real Tolerance)
185 BRep_ListIteratorOfListOfCurveRepresentation an_Iterator
186 ((*((Handle(BRep_TEdge)*)&AnEdge.TShape()))->ChangeCurves());
188 Handle(Geom2d_Curve) Curve2dPtr, Curve2dPtr2, NewCurve2dPtr, NewCurve2dPtr2;
189 TopLoc_Location LocalLoc ;
191 Standard_Boolean first_time_in = Standard_True,
194 Handle(BRep_GCurve) geometric_representation_ptr ;
200 const Handle(Geom_Curve) C = BRep_Tool::Curve(AnEdge,
205 first_time_in = Standard_False ;
208 while (an_Iterator.More()) {
209 geometric_representation_ptr =
210 Handle(BRep_GCurve)::DownCast(an_Iterator.Value());
211 if (! geometric_representation_ptr.IsNull()) {
213 has_curve = Standard_False ;
214 first = geometric_representation_ptr->First();
215 last = geometric_representation_ptr->Last();
216 if (geometric_representation_ptr->IsCurveOnSurface()) {
217 Curve2dPtr = geometric_representation_ptr->PCurve() ;
218 has_curve = Standard_True ;
220 if (geometric_representation_ptr->IsCurveOnClosedSurface()) {
221 Curve2dPtr2 = geometric_representation_ptr->PCurve2() ;
222 has_closed_curve = Standard_True ;
224 if (has_curve || has_closed_curve) {
226 current_first = first ;
227 current_last = last ;
228 first_time_in = Standard_False ;
231 if (Abs(first - current_first) > Precision::Confusion() ||
232 Abs(last - current_last) > Precision::Confusion() )
236 GeomLib::SameRange(Tolerance,
238 geometric_representation_ptr->First(),
239 geometric_representation_ptr->Last(),
243 geometric_representation_ptr->PCurve(NewCurve2dPtr) ;
245 if (has_closed_curve)
247 GeomLib::SameRange(Tolerance,
249 geometric_representation_ptr->First(),
250 geometric_representation_ptr->Last(),
254 geometric_representation_ptr->PCurve2(NewCurve2dPtr2) ;
262 B.Range(TopoDS::Edge(AnEdge),
270 //=======================================================================
271 //function : EvaluateMaxSegment
272 //purpose : return MaxSegment to pass in approximation, if MaxSegment==0 provided
273 //=======================================================================
275 static Standard_Integer evaluateMaxSegment(const Standard_Integer aMaxSegment,
276 const Adaptor3d_CurveOnSurface& aCurveOnSurface)
278 if (aMaxSegment != 0) return aMaxSegment;
280 Handle(Adaptor3d_HSurface) aSurf = aCurveOnSurface.GetSurface();
281 Handle(Adaptor2d_HCurve2d) aCurv2d = aCurveOnSurface.GetCurve();
283 Standard_Real aNbSKnots = 0, aNbC2dKnots = 0;
285 if (aSurf->GetType() == GeomAbs_BSplineSurface) {
286 Handle(Geom_BSplineSurface) aBSpline = aSurf->BSpline();
287 aNbSKnots = Max(aBSpline->NbUKnots(), aBSpline->NbVKnots());
289 if (aCurv2d->GetType() == GeomAbs_BSplineCurve) {
290 aNbC2dKnots = aCurv2d->NbKnots();
292 Standard_Integer aReturn = (Standard_Integer) ( 30 + Max(aNbSKnots, aNbC2dKnots) ) ;
296 //=======================================================================
297 //function : BuildCurve3d
299 //=======================================================================
301 Standard_Boolean BRepLib::BuildCurve3d(const TopoDS_Edge& AnEdge,
302 const Standard_Real Tolerance,
303 const GeomAbs_Shape Continuity,
304 const Standard_Integer MaxDegree,
305 const Standard_Integer MaxSegment)
307 Standard_Integer //ErrorCode,
313 TopLoc_Location LocalLoc,L[2],LC;
314 Standard_Real f,l,fc,lc, first[2], last[2],
318 Handle(Geom2d_Curve) Curve2dPtr, Curve2dArray[2] ;
319 Handle(Geom_Surface) SurfacePtr, SurfaceArray[2] ;
321 Standard_Integer not_done ;
322 // if the edge has a 3d curve returns true
325 const Handle(Geom_Curve) C = BRep_Tool::Curve(AnEdge,LocalLoc,f,l);
327 return Standard_True;
329 // this should not exists but UpdateEdge makes funny things
330 // if the edge is not same range
332 if (! CheckSameRange(AnEdge,
333 Precision::Confusion())) {
340 // search a curve on a plane
341 Handle(Geom_Surface) S;
342 Handle(Geom2d_Curve) PC;
343 Standard_Integer i = 0;
344 Handle(Geom_Plane) P;
349 BRep_Tool::CurveOnSurface(AnEdge,PC,S,LocalLoc,f,l,i);
350 Handle(Geom_RectangularTrimmedSurface) RT =
351 Handle(Geom_RectangularTrimmedSurface)::DownCast(S);
353 P = Handle(Geom_Plane)::DownCast(S);
356 P = Handle(Geom_Plane)::DownCast(RT->BasisSurface());
358 not_done = ! (S.IsNull() || !P.IsNull()) ;
361 // compute the 3d curve
362 gp_Ax2 axes = P->Position().Ax2();
363 Handle(Geom_Curve) C3d = GeomLib::To3d(axes,PC);
365 return Standard_False;
367 Standard_Real First, Last;
370 B.UpdateEdge(AnEdge,C3d,LocalLoc,0.0e0);
371 BRep_Tool::Range(AnEdge, S, LC, First, Last);
372 B.Range(AnEdge, First, Last); //Do not forget 3D range.(PRO6412)
376 // compute the 3d curve using existing surface
380 if (!BRep_Tool::Degenerated(AnEdge)) {
382 for (ii = 0 ; ii < 3 ; ii++ ) {
383 BRep_Tool::CurveOnSurface(TopoDS::Edge(AnEdge),
391 if (!Curve2dPtr.IsNull() && jj < 2){
392 Curve2dArray[jj] = Curve2dPtr ;
393 SurfaceArray[jj] = SurfacePtr ;
402 Curve2dPtr = Curve2dArray[0] ;
403 SurfacePtr = SurfaceArray[0] ;
405 Geom2dAdaptor_Curve AnAdaptor3dCurve2d (Curve2dPtr, f, l) ;
406 GeomAdaptor_Surface AnAdaptor3dSurface (SurfacePtr) ;
407 Handle(Geom2dAdaptor_HCurve) AnAdaptor3dCurve2dPtr =
408 new Geom2dAdaptor_HCurve(AnAdaptor3dCurve2d) ;
409 Handle(GeomAdaptor_HSurface) AnAdaptor3dSurfacePtr =
410 new GeomAdaptor_HSurface (AnAdaptor3dSurface) ;
411 Adaptor3d_CurveOnSurface CurveOnSurface( AnAdaptor3dCurve2dPtr,
412 AnAdaptor3dSurfacePtr) ;
414 Handle(Geom_Curve) NewCurvePtr ;
416 GeomLib::BuildCurve3d(Tolerance,
425 evaluateMaxSegment(MaxSegment,CurveOnSurface)) ;
427 tolerance = BRep_Tool::Tolerance(AnEdge) ;
429 //max_deviation = Max(tolerance, max_deviation) ;
430 max_deviation = Max( tolerance, Tolerance );
431 if (NewCurvePtr.IsNull())
432 return Standard_False;
433 B.UpdateEdge(TopoDS::Edge(AnEdge),
439 // if there is only one curve on surface attached to the edge
440 // than it can be qualified sameparameter
442 B.SameParameter(TopoDS::Edge(AnEdge),
447 return Standard_False ;
451 return Standard_True;
453 //=======================================================================
454 //function : BuildCurves3d
456 //=======================================================================
458 Standard_Boolean BRepLib::BuildCurves3d(const TopoDS_Shape& S)
461 return BRepLib::BuildCurves3d(S,
465 //=======================================================================
466 //function : BuildCurves3d
468 //=======================================================================
470 Standard_Boolean BRepLib::BuildCurves3d(const TopoDS_Shape& S,
471 const Standard_Real Tolerance,
472 const GeomAbs_Shape Continuity,
473 const Standard_Integer MaxDegree,
474 const Standard_Integer MaxSegment)
476 Standard_Boolean boolean_value,
478 TopTools_MapOfShape a_counter ;
479 TopExp_Explorer ex(S,TopAbs_EDGE);
482 if (a_counter.Add(ex.Current())) {
484 BuildCurve3d(TopoDS::Edge(ex.Current()),
485 Tolerance, Continuity,
486 MaxDegree, MaxSegment);
487 ok = ok && boolean_value ;
493 //=======================================================================
494 //function : UpdateEdgeTolerance
496 //=======================================================================
498 Standard_Boolean BRepLib::UpdateEdgeTol(const TopoDS_Edge& AnEdge,
499 const Standard_Real MinToleranceRequested,
500 const Standard_Real MaxToleranceToCheck)
503 Standard_Integer curve_on_surface_index,
510 geom_reference_curve_flag = 0,
511 max_sampling_points = 90,
512 min_sampling_points = 30 ;
514 Standard_Real factor = 100.0e0,
515 // sampling_array[2],
520 coded_edge_tolerance,
521 edge_tolerance = 0.0e0 ;
522 Handle(TColStd_HArray1OfReal) parameters_ptr ;
523 Handle(BRep_GCurve) geometric_representation_ptr ;
525 if (BRep_Tool::Degenerated(AnEdge)) return Standard_False ;
526 coded_edge_tolerance = BRep_Tool::Tolerance(AnEdge) ;
527 if (coded_edge_tolerance > MaxToleranceToCheck) return Standard_False ;
529 const Handle(BRep_TEdge)& TE = *((Handle(BRep_TEdge)*)&AnEdge.TShape());
530 BRep_ListOfCurveRepresentation& list_curve_rep = TE->ChangeCurves() ;
531 BRep_ListIteratorOfListOfCurveRepresentation an_iterator(list_curve_rep),
532 second_iterator(list_curve_rep) ;
533 Handle(Geom2d_Curve) curve2d_ptr, new_curve2d_ptr;
534 Handle(Geom_Surface) surface_ptr ;
535 TopLoc_Location local_location ;
536 GCPnts_QuasiUniformDeflection a_sampler ;
537 GeomAdaptor_Curve geom_reference_curve ;
538 Adaptor3d_CurveOnSurface curve_on_surface_reference ;
539 Handle(Geom_Curve) C = BRep_Tool::Curve(AnEdge,
543 curve_on_surface_index = -1 ;
545 if (! local_location.IsIdentity()) {
546 C = Handle(Geom_Curve)::
547 DownCast(C-> Transformed(local_location.Transformation()) ) ;
549 geom_reference_curve.Load(C) ;
550 geom_reference_curve_flag = 1 ;
551 a_sampler.Initialize(geom_reference_curve,
552 MinToleranceRequested * factor,
558 curve_on_surface_index = 0 ;
560 while (not_done && an_iterator.More()) {
561 geometric_representation_ptr =
562 Handle(BRep_GCurve)::DownCast(second_iterator.Value());
563 if (!geometric_representation_ptr.IsNull()
564 && geometric_representation_ptr->IsCurveOnSurface()) {
565 curve2d_ptr = geometric_representation_ptr->PCurve() ;
566 local_location = geometric_representation_ptr->Location() ;
567 current_first = geometric_representation_ptr->First();
568 //first = geometric_representation_ptr->First();
569 current_last = geometric_representation_ptr->Last();
572 if (! local_location.IsIdentity() ) {
573 surface_ptr = Handle(Geom_Surface)::
574 DownCast( geometric_representation_ptr->Surface()->
575 Transformed(local_location.Transformation()) ) ;
579 geometric_representation_ptr->Surface() ;
583 curve_on_surface_index += 1 ;
585 Geom2dAdaptor_Curve AnAdaptor3dCurve2d (curve2d_ptr) ;
586 GeomAdaptor_Surface AnAdaptor3dSurface (surface_ptr) ;
587 Handle(Geom2dAdaptor_HCurve) AnAdaptor3dCurve2dPtr =
588 new Geom2dAdaptor_HCurve(AnAdaptor3dCurve2d) ;
589 Handle(GeomAdaptor_HSurface) AnAdaptor3dSurfacePtr =
590 new GeomAdaptor_HSurface (AnAdaptor3dSurface) ;
591 curve_on_surface_reference.Load (AnAdaptor3dCurve2dPtr, AnAdaptor3dSurfacePtr);
592 a_sampler.Initialize(curve_on_surface_reference,
593 MinToleranceRequested * factor,
597 TColStd_Array1OfReal sampling_parameters(1,a_sampler.NbPoints()) ;
598 for (ii = 1 ; ii <= a_sampler.NbPoints() ; ii++) {
599 sampling_parameters(ii) = a_sampler.Parameter(ii) ;
601 if (a_sampler.NbPoints() < min_sampling_points) {
602 GeomLib::DensifyArray1OfReal(min_sampling_points,
606 else if (a_sampler.NbPoints() > max_sampling_points) {
607 GeomLib::RemovePointsFromArray(max_sampling_points,
614 new TColStd_HArray1OfReal(1,sampling_parameters.Length()) ;
615 for (ii = sampling_parameters.Lower() ; ii <= sampling_parameters.Upper() ; ii++) {
616 parameters_ptr->ChangeArray1()(jj) =
617 sampling_parameters(ii) ;
624 while (second_iterator.More()) {
625 geometric_representation_ptr =
626 Handle(BRep_GCurve)::DownCast(second_iterator.Value());
627 if (! geometric_representation_ptr.IsNull() &&
628 curve_index != curve_on_surface_index) {
630 has_curve = Standard_False ;
631 // first = geometric_representation_ptr->First();
632 // last = geometric_representation_ptr->Last();
633 local_location = geometric_representation_ptr->Location() ;
634 if (geometric_representation_ptr->IsCurveOnSurface()) {
635 curve2d_ptr = geometric_representation_ptr->PCurve() ;
636 has_curve = Standard_True ;
638 if (geometric_representation_ptr->IsCurveOnClosedSurface()) {
639 curve2d_ptr = geometric_representation_ptr->PCurve2() ;
640 has_closed_curve = Standard_True ;
645 if (! local_location.IsIdentity() ) {
646 surface_ptr = Handle(Geom_Surface)::
647 DownCast( geometric_representation_ptr->Surface()->
648 Transformed(local_location.Transformation()) ) ;
652 geometric_representation_ptr->Surface() ;
654 Geom2dAdaptor_Curve an_adaptor_curve2d (curve2d_ptr) ;
655 GeomAdaptor_Surface an_adaptor_surface(surface_ptr) ;
656 Handle(Geom2dAdaptor_HCurve) an_adaptor_curve2d_ptr =
657 new Geom2dAdaptor_HCurve(an_adaptor_curve2d) ;
658 Handle(GeomAdaptor_HSurface) an_adaptor_surface_ptr =
659 new GeomAdaptor_HSurface (an_adaptor_surface) ;
660 Adaptor3d_CurveOnSurface a_curve_on_surface(an_adaptor_curve2d_ptr,
661 an_adaptor_surface_ptr) ;
663 if (BRep_Tool::SameParameter(AnEdge)) {
665 GeomLib::EvalMaxParametricDistance(a_curve_on_surface,
666 geom_reference_curve,
667 MinToleranceRequested,
668 parameters_ptr->Array1(),
671 else if (geom_reference_curve_flag) {
672 GeomLib::EvalMaxDistanceAlongParameter(a_curve_on_surface,
673 geom_reference_curve,
674 MinToleranceRequested,
675 parameters_ptr->Array1(),
680 GeomLib::EvalMaxDistanceAlongParameter(a_curve_on_surface,
681 curve_on_surface_reference,
682 MinToleranceRequested,
683 parameters_ptr->Array1(),
686 max_distance *= safe_factor ;
687 edge_tolerance = Max(max_distance, edge_tolerance) ;
693 second_iterator.Next() ;
696 TE->Tolerance(edge_tolerance);
697 return Standard_True ;
700 //=======================================================================
701 //function : UpdateEdgeTolerance
703 //=======================================================================
705 Standard_Boolean BRepLib::UpdateEdgeTolerance(const TopoDS_Shape& S,
706 const Standard_Real MinToleranceRequested,
707 const Standard_Real MaxToleranceToCheck)
709 TopExp_Explorer ex(S,TopAbs_EDGE);
710 TopTools_MapOfShape a_counter ;
712 Standard_Boolean return_status = Standard_False,
716 if (a_counter.Add(ex.Current())) {
718 BRepLib::UpdateEdgeTol(TopoDS::Edge(ex.Current()),
719 MinToleranceRequested,
720 MaxToleranceToCheck) ;
721 if (local_flag && ! return_status) {
722 return_status = Standard_True ;
727 return return_status ;
730 //=======================================================================
731 //function : SetEdgeTol
733 //=======================================================================
735 static void SetEdgeTol(const TopoDS_Edge& E,
736 const TopoDS_Face& F)
739 const Handle(Geom_Surface)& S = BRep_Tool::Surface(F,L);
740 TopLoc_Location l = L.Predivided(E.Location());
742 const Handle(BRep_TEdge)& TE = *((Handle(BRep_TEdge)*)&E.TShape());
743 BRep_ListIteratorOfListOfCurveRepresentation itcr(TE->ChangeCurves());
745 while (itcr.More()) {
746 const Handle(BRep_CurveRepresentation)& cr = itcr.Value();
747 if(cr->IsCurveOnSurface(S,l)) return;
751 Handle(Geom_Plane) GP;
752 Handle(Geom_RectangularTrimmedSurface) GRTS;
753 GRTS = Handle(Geom_RectangularTrimmedSurface)::DownCast(S);
755 GP = Handle(Geom_Plane)::DownCast(GRTS->BasisSurface());
757 GP = Handle(Geom_Plane)::DownCast(S);
759 Handle(GeomAdaptor_HCurve) HC = new GeomAdaptor_HCurve();
760 Handle(GeomAdaptor_HSurface) HS = new GeomAdaptor_HSurface();
763 Standard_Real First, Last;
764 GeomAdaptor_Curve& GAC = HC->ChangeCurve();
765 GAC.Load(BRep_Tool::Curve(E,LC,First,Last));
766 LC = L.Predivided(LC);
768 if (!LC.IsIdentity()) {
769 GP = Handle(Geom_Plane)::DownCast(
770 GP->Transformed(LC.Transformation()));
772 GeomAdaptor_Surface& GAS = HS->ChangeSurface();
775 ProjLib_ProjectedCurve Proj(HS,HC);
776 Handle(Geom2d_Curve) pc = Geom2dAdaptor::MakeCurve(Proj);
778 gp_Pln pln = GAS.Plane();
779 Standard_Real d2 = 0.;
780 Standard_Integer nn = 23;
781 Standard_Real unsurnn = 1./nn;
782 for(Standard_Integer i = 0; i <= nn; i++){
783 Standard_Real t = unsurnn*i;
784 Standard_Real u = First*(1.-t) + Last*t;
785 gp_Pnt Pc3d = HC->Value(u);
786 gp_Pnt2d p2d = pc->Value(u);
787 gp_Pnt Pcons = ElSLib::Value(p2d.X(),p2d.Y(),pln);
788 Standard_Real eps = Max(Pc3d.XYZ().SquareModulus(), Pcons.XYZ().SquareModulus());
790 Standard_Real temp = Pc3d.SquareDistance(Pcons);
795 if(temp > d2) d2 = temp;
798 TE->UpdateTolerance(d2);
801 //=======================================================================
802 //function : SameParameter
804 //=======================================================================
805 void BRepLib::SameParameter(const TopoDS_Shape& S,
806 const Standard_Real Tolerance,
807 const Standard_Boolean forced)
809 TopExp_Explorer ex(S,TopAbs_EDGE);
810 TopTools_MapOfShape Done;
814 if (Done.Add(ex.Current())) {
816 brB.SameRange(TopoDS::Edge(ex.Current()), Standard_False);
817 brB.SameParameter(TopoDS::Edge(ex.Current()), Standard_False);
819 BRepLib::SameParameter(TopoDS::Edge(ex.Current()),Tolerance);
825 BRepAdaptor_Surface BS;
826 for(ex.Init(S,TopAbs_FACE); ex.More(); ex.Next()){
827 const TopoDS_Face& curface = TopoDS::Face(ex.Current());
828 if(!Done.Add(curface)) continue;
829 BS.Initialize(curface);
830 if(BS.GetType() != GeomAbs_Plane) continue;
832 for(ex2.Init(curface,TopAbs_EDGE); ex2.More(); ex2.Next()){
833 const TopoDS_Edge& E = TopoDS::Edge(ex2.Current());
834 SetEdgeTol(E,curface);
837 BRepLib::UpdateTolerances(S);
840 //================================================================
841 //function : SameParameter
842 //WARNING : New spec DUB LBO 9/9/97.
843 // Recode in the edge the best tolerance found,
844 // for vertex extremities it is required to find something else
845 //================================================================
846 static Standard_Boolean EvalTol(const Handle(Geom2d_Curve)& pc,
847 const Handle(Geom_Surface)& s,
848 const GeomAdaptor_Curve& gac,
849 const Standard_Real tol,
850 Standard_Real& tolbail)
852 Standard_Integer ok = 0;
853 Standard_Real f = gac.FirstParameter();
854 Standard_Real l = gac.LastParameter();
855 Extrema_LocateExtPC Projector;
856 Projector.Initialize(gac,f,l,tol);
860 for(Standard_Integer i = 1; i <= 5; i++){
861 Standard_Real t = i/6.;
862 t = (1.-t) * f + t * l;
863 pc->Value(t).Coord(u,v);
865 Projector.Perform(p,t);
866 if (Projector.IsDone()) {
867 Standard_Real dist2 = Projector.SquareDistance();
868 if(dist2 > tolbail * tolbail) tolbail = sqrt (dist2);
875 static Standard_Real ComputeTol(const Handle(Adaptor3d_HCurve)& c3d,
876 const Handle(Adaptor2d_HCurve2d)& c2d,
877 const Handle(Adaptor3d_HSurface)& surf,
878 const Standard_Integer nbp)
882 TColStd_Array1OfReal dist(1,nbp+10);
885 Adaptor3d_CurveOnSurface cons(c2d,surf);
886 Standard_Real d2 = 0.;
887 Standard_Real first = c3d->FirstParameter();
888 Standard_Real last = c3d->LastParameter();
889 Standard_Integer i = 0;
890 for(i = 0; i <= nbp; i++){
891 const Standard_Real t = IntToReal(i)/IntToReal(nbp);
892 const Standard_Real u = first*(1.-t) + last*t;
893 gp_Pnt Pc3d = c3d->Value(u);
894 gp_Pnt Pcons = cons.Value(u);
895 if (Precision::IsInfinite(Pcons.X()) ||
896 Precision::IsInfinite(Pcons.Y()) ||
897 Precision::IsInfinite(Pcons.Z())) {
898 d2=Precision::Infinite();
901 Standard_Real temp = Pc3d.SquareDistance(Pcons);
907 if(temp > d2) d2 = temp;
911 Standard_Boolean ana = Standard_False;
912 Standard_Real D2 = 0;
913 Standard_Integer N1 = 0;
914 Standard_Integer N2 = 0;
915 Standard_Integer N3 = 0;
917 for( i = 1; i<= nbp+10; i++)
919 if( dist(i) < 1.0 ) N1++;
923 if( N1 > N2 && N2 != 0 ) N3 = 100*N2/(N1+N2);
924 if( N3 < 10 && N3 != 0 ) {
926 for( i = 1; i<= nbp+10; i++)
927 if( dist(i) > 0 && dist(i) < 1.0 )
928 if( dist(i) > D2 ) D2 = dist(i);
932 d2 = (!ana) ? 1.5*sqrt(d2) : 1.5*sqrt(D2);
933 if(d2<1.e-7) d2 = 1.e-7;
938 void BRepLib::SameParameter(const TopoDS_Edge& AnEdge,
939 const Standard_Real Tolerance)
941 if (BRep_Tool::SameParameter(AnEdge)) return;
943 const Standard_Integer NCONTROL = 22;
945 Handle(GeomAdaptor_HCurve) HC = new GeomAdaptor_HCurve();
946 Handle(Geom2dAdaptor_HCurve) HC2d = new Geom2dAdaptor_HCurve();
947 Handle(GeomAdaptor_HSurface) HS = new GeomAdaptor_HSurface();
948 GeomAdaptor_Curve& GAC = HC->ChangeCurve();
949 Geom2dAdaptor_Curve& GAC2d = HC2d->ChangeCurve2d();
950 GeomAdaptor_Surface& GAS = HS->ChangeSurface();
951 Standard_Real f3d =0.,l3d =0.;
953 Handle(Geom_Curve) C3d;
955 const Handle(BRep_TEdge)& TE = *((Handle(BRep_TEdge)*) &AnEdge.TShape());
956 BRep_ListOfCurveRepresentation& CList = TE->ChangeCurves();
957 BRep_ListIteratorOfListOfCurveRepresentation It(CList);
959 Standard_Boolean NotDone = Standard_True;
961 while (NotDone && It.More()) {
962 Handle(BRep_GCurve) GCurve = Handle(BRep_GCurve)::DownCast(It.Value());
963 if (!GCurve.IsNull() && GCurve->IsCurve3D()) {
964 C3d = GCurve->Curve3D() ;
965 f3d = GCurve->First();
966 l3d = GCurve->Last();
967 L3d = GCurve->Location() ;
968 NotDone = Standard_False;
973 if(C3d.IsNull()) return;
975 // modified by NIZHNY-OCC486 Tue Aug 27 17:15:13 2002 :
976 Standard_Boolean m_TrimmedPeriodical = Standard_False;
977 Handle(Standard_Type) TheType = C3d->DynamicType();
978 if( TheType == STANDARD_TYPE(Geom_TrimmedCurve))
980 Handle(Geom_Curve) gtC (Handle(Geom_TrimmedCurve)::DownCast (C3d)->BasisCurve());
981 m_TrimmedPeriodical = gtC->IsPeriodic();
983 // modified by NIZHNY-OCC486 Tue Aug 27 17:15:17 2002 .
986 if(!C3d->IsPeriodic()) {
987 Standard_Real Udeb = C3d->FirstParameter();
988 Standard_Real Ufin = C3d->LastParameter();
989 // modified by NIZHNY-OCC486 Tue Aug 27 17:17:14 2002 :
990 //if (Udeb > f3d) f3d = Udeb;
991 //if (l3d > Ufin) l3d = Ufin;
992 if(!m_TrimmedPeriodical)
994 if (Udeb > f3d) f3d = Udeb;
995 if (l3d > Ufin) l3d = Ufin;
997 // modified by NIZHNY-OCC486 Tue Aug 27 17:17:55 2002 .
999 if(!L3d.IsIdentity()){
1000 C3d = Handle(Geom_Curve)::DownCast(C3d->Transformed(L3d.Transformation()));
1002 GAC.Load(C3d,f3d,l3d);
1004 Standard_Boolean IsSameP = 1;
1005 Standard_Real maxdist = 0.;
1007 // Modified by skv - Thu Jun 3 12:39:19 2004 OCC5898 Begin
1008 Standard_Real anEdgeTol = BRep_Tool::Tolerance(AnEdge);
1009 // Modified by skv - Thu Jun 3 12:39:20 2004 OCC5898 End
1010 Standard_Boolean SameRange = BRep_Tool::SameRange(AnEdge);
1011 Standard_Boolean YaPCu = Standard_False;
1012 It.Initialize(CList);
1015 Standard_Boolean isANA = Standard_False;
1016 Standard_Boolean isBSP = Standard_False;
1017 Handle(BRep_GCurve) GCurve = Handle(BRep_GCurve)::DownCast(It.Value());
1018 Handle(Geom2d_Curve) PC[2];
1019 Handle(Geom_Surface) S;
1020 if (!GCurve.IsNull() && GCurve->IsCurveOnSurface()) {
1021 YaPCu = Standard_True;
1022 PC[0] = GCurve->PCurve();
1023 TopLoc_Location PCLoc = GCurve->Location();
1024 S = GCurve->Surface();
1025 if (!PCLoc.IsIdentity() ) {
1026 S = Handle(Geom_Surface)::DownCast(S->Transformed(PCLoc.Transformation()));
1030 if (GCurve->IsCurveOnClosedSurface()) {
1031 PC[1] = GCurve->PCurve2();
1034 // Eval tol2d to compute SameRange
1035 Standard_Real UResol = Max(GAS.UResolution(Tolerance), Precision::PConfusion());
1036 Standard_Real VResol = Max(GAS.VResolution(Tolerance), Precision::PConfusion());
1037 Standard_Real Tol2d = Min(UResol, VResol);
1038 for(Standard_Integer i = 0; i < 2; i++){
1039 Handle(Geom2d_Curve) curPC = PC[i];
1040 Standard_Boolean updatepc = 0;
1041 if(curPC.IsNull()) break;
1043 GeomLib::SameRange(Tol2d,
1044 PC[i],GCurve->First(),GCurve->Last(),
1047 updatepc = (curPC != PC[i]);
1050 Standard_Boolean goodpc = 1;
1051 GAC2d.Load(curPC,f3d,l3d);
1053 Standard_Real error = ComputeTol(HC, HC2d, HS, NCONTROL);
1055 if(GAC2d.GetType() == GeomAbs_BSplineCurve &&
1056 GAC2d.Continuity() == GeomAbs_C0) {
1057 Handle(Geom2d_BSplineCurve) bs2d = GAC2d.BSpline();
1058 Handle(Geom2d_BSplineCurve) bs2dsov = bs2d;
1059 Standard_Real fC0 = bs2d->FirstParameter(), lC0 = bs2d->LastParameter();
1060 Standard_Boolean repar = Standard_True;
1061 gp_Pnt2d OriginPoint;
1062 bs2d->D0(fC0, OriginPoint);
1063 Geom2dConvert::C0BSplineToC1BSplineCurve(bs2d, Tol2d);
1064 isBSP = Standard_True;
1066 if(bs2d->IsPeriodic()) { // -------- IFV, Jan 2000
1067 gp_Pnt2d NewOriginPoint;
1068 bs2d->D0(bs2d->FirstParameter(), NewOriginPoint);
1069 if(Abs(OriginPoint.X() - NewOriginPoint.X()) > Precision::PConfusion() ||
1070 Abs(OriginPoint.Y() - NewOriginPoint.Y()) > Precision::PConfusion() ) {
1072 TColStd_Array1OfReal Knotbs2d (1, bs2d->NbKnots());
1073 bs2d->Knots(Knotbs2d);
1075 for(Standard_Integer Index = 1; Index <= bs2d->NbKnots(); Index++) {
1076 bs2d->D0(Knotbs2d(Index), NewOriginPoint);
1077 if(Abs(OriginPoint.X() - NewOriginPoint.X()) > Precision::PConfusion() ||
1078 Abs(OriginPoint.Y() - NewOriginPoint.Y()) > Precision::PConfusion() ) continue;
1080 bs2d->SetOrigin(Index);
1086 if(bs2d->Continuity() == GeomAbs_C0) {
1087 Standard_Real tolbail;
1088 if(EvalTol(curPC,S,GAC,Tolerance,tolbail)){
1090 Standard_Real UResbail = GAS.UResolution(tolbail);
1091 Standard_Real VResbail = GAS.VResolution(tolbail);
1092 Standard_Real Tol2dbail = Min(UResbail,VResbail);
1093 bs2d->D0(bs2d->FirstParameter(), OriginPoint);
1095 Standard_Integer nbp = bs2d->NbPoles();
1096 TColgp_Array1OfPnt2d poles(1,nbp);
1098 gp_Pnt2d p = poles(1), p1;
1099 Standard_Real d = Precision::Infinite();
1100 for(Standard_Integer ip = 2; ip <= nbp; ip++) {
1102 d = Min(d,p.SquareDistance(p1));
1107 Tol2dbail = Max(Min(Tol2dbail,d),Tol2d);
1109 Geom2dConvert::C0BSplineToC1BSplineCurve(bs2d,Tol2dbail);
1111 if(bs2d->IsPeriodic()) { // -------- IFV, Jan 2000
1112 gp_Pnt2d NewOriginPoint;
1113 bs2d->D0(bs2d->FirstParameter(), NewOriginPoint);
1114 if(Abs(OriginPoint.X() - NewOriginPoint.X()) > Precision::PConfusion() ||
1115 Abs(OriginPoint.Y() - NewOriginPoint.Y()) > Precision::PConfusion() ) {
1117 TColStd_Array1OfReal Knotbs2d (1, bs2d->NbKnots());
1118 bs2d->Knots(Knotbs2d);
1120 for(Standard_Integer Index = 1; Index <= bs2d->NbKnots(); Index++) {
1121 bs2d->D0(Knotbs2d(Index), NewOriginPoint);
1122 if(Abs(OriginPoint.X() - NewOriginPoint.X()) > Precision::PConfusion() ||
1123 Abs(OriginPoint.Y() - NewOriginPoint.Y()) > Precision::PConfusion() ) continue;
1125 bs2d->SetOrigin(Index);
1132 if(bs2d->Continuity() == GeomAbs_C0) {
1135 repar = Standard_False;
1143 Standard_Integer NbKnots = bs2d->NbKnots();
1144 TColStd_Array1OfReal Knots(1,NbKnots);
1146 // BSplCLib::Reparametrize(f3d,l3d,Knots);
1147 BSplCLib::Reparametrize(fC0,lC0,Knots);
1148 bs2d->SetKnots(Knots);
1149 GAC2d.Load(bs2d,f3d,l3d);
1151 Standard_Boolean updatepcsov = updatepc;
1152 updatepc = Standard_True;
1154 Standard_Real error1 = ComputeTol(HC, HC2d, HS, NCONTROL);
1155 if(error1 > error) {
1157 GAC2d.Load(bs2d,f3d,l3d);
1159 updatepc = updatepcsov;
1160 isANA = Standard_True;
1167 //check, if new BSpline "good" or not --------- IFV, Jan of 2000
1168 GeomAbs_Shape cont = bs2d->Continuity();
1169 Standard_Boolean IsBad = Standard_False;
1171 if(cont > GeomAbs_C0 && error > Max(1.e-3,Tolerance)) {
1172 Standard_Integer NbKnots = bs2d->NbKnots();
1173 TColStd_Array1OfReal Knots(1,NbKnots);
1175 Standard_Real critratio = 10.;
1176 Standard_Real dtprev = Knots(2) - Knots(1), dtratio = 1.;
1177 Standard_Real dtmin = dtprev;
1178 Standard_Real dtcur;
1179 for(Standard_Integer j = 2; j < NbKnots; j++) {
1180 dtcur = Knots(j+1) - Knots(j);
1181 dtmin = Min(dtmin, dtcur);
1185 if(dtcur > dtprev) dtratio = dtcur/dtprev;
1186 else dtratio = dtprev/dtcur;
1187 if(dtratio > critratio) {IsBad = Standard_True;}
1192 // To avoid failures in Approx_CurvilinearParameter
1193 bs2d->Resolution(Max(1.e-3,Tolerance), dtcur);
1194 if(dtmin < dtcur) IsBad = Standard_False;
1199 if(IsBad ) { //if BSpline "bad", try to reparametrize it
1200 // by its curve length
1202 // GeomAbs_Shape cont = bs2d->Continuity();
1203 if(cont > GeomAbs_C2) cont = GeomAbs_C2;
1204 Standard_Integer maxdeg = bs2d->Degree();
1205 if(maxdeg == 1) maxdeg = 14;
1206 Approx_CurvilinearParameter AppCurPar(HC2d, HS, Max(1.e-3,Tolerance),
1208 if(AppCurPar.IsDone() || AppCurPar.HasResult()) {
1209 bs2d = AppCurPar.Curve2d1();
1210 GAC2d.Load(bs2d,f3d,l3d);
1213 if(Abs(bs2d->FirstParameter() - fC0) > Tol2d ||
1214 Abs(bs2d->LastParameter() - lC0) > Tol2d ) {
1215 Standard_Integer NbKnots = bs2d->NbKnots();
1216 TColStd_Array1OfReal Knots(1,NbKnots);
1218 // BSplCLib::Reparametrize(f3d,l3d,Knots);
1219 BSplCLib::Reparametrize(fC0,lC0,Knots);
1220 bs2d->SetKnots(Knots);
1221 GAC2d.Load(bs2d,f3d,l3d);
1234 // Approx_SameParameter SameP(HC,HC2d,HS,Tolerance);
1235 Standard_Real aTol = (isANA && isBSP) ? 1.e-7 : Tolerance;
1236 const Handle(Adaptor3d_HCurve)& aHCurv = HC; // to avoid ambiguity
1237 const Handle(Adaptor2d_HCurve2d)& aHCurv2d = HC2d; // to avoid ambiguity
1238 Approx_SameParameter SameP(aHCurv,aHCurv2d,HS,aTol);
1240 if (SameP.IsSameParameter()) {
1241 maxdist = Max(maxdist,SameP.TolReached());
1243 if (i == 0) GCurve->PCurve(curPC);
1244 else GCurve->PCurve2(curPC);
1247 else if (SameP.IsDone()) {
1248 Standard_Real tolreached = SameP.TolReached();
1249 if(tolreached < error) {
1250 curPC = SameP.Curve2d();
1251 updatepc = Standard_True;
1252 maxdist = Max(maxdist,tolreached);
1255 maxdist = Max(maxdist, error);
1258 if (i == 0) GCurve->PCurve(curPC);
1259 else GCurve->PCurve2(curPC);
1264 //Approx_SameParameter has failed.
1265 //Consequently, the situation might be,
1266 //when 3D and 2D-curve do not have same-range.
1267 GeomLib::SameRange( Tol2d, PC[i],
1268 GCurve->First(), GCurve->Last(),
1271 if (i == 0) GCurve->PCurve(curPC);
1272 else GCurve->PCurve2(curPC);
1280 // Modified by skv - Thu Jun 3 12:39:19 2004 OCC5898 Begin
1282 if (anEdgeTol > error) {
1283 maxdist = Max(maxdist, anEdgeTol);
1284 IsSameP = Standard_True;
1287 // Modified by skv - Thu Jun 3 12:39:20 2004 OCC5898 End
1292 B.Range(AnEdge,f3d,l3d);
1293 B.SameRange(AnEdge,Standard_True);
1295 // Reduce eventually the tolerance of the edge, as
1296 // all its representations are processed (except for some associated
1297 // to planes and not stored in the edge !)
1298 // The same cannot be done with vertices that cannot be enlarged
1301 // Avoid setting too small tolerances.
1302 maxdist = Max(maxdist,Precision::Confusion());
1303 TopoDS_Vertex V1,V2;
1304 TopExp::Vertices(AnEdge,V1,V2);
1306 B.UpdateVertex(V1,maxdist);
1308 B.UpdateVertex(V2,maxdist);
1309 TE->Modified(Standard_True);
1310 TE->Tolerance(maxdist);
1312 B.SameParameter(AnEdge,Standard_True);
1316 //=======================================================================
1317 //function : UpdateTolerances
1319 //=======================================================================
1320 void BRepLib::UpdateTolerances(const TopoDS_Shape& aShape,
1321 const Standard_Boolean verifyTolerance)
1324 // Harmonize tolerances
1325 // with rule Tolerance(VERTEX)>=Tolerance(EDGE)>=Tolerance(FACE)
1327 Standard_Real tol=0;
1328 if (verifyTolerance) {
1329 // Set tolerance to its minimum value
1330 Handle(Geom_Surface) S;
1334 Standard_Real aXmin, aYmin, aZmin, aXmax, aYmax, aZmax, dMax;
1335 for (ex.Init(aShape, TopAbs_FACE); ex.More(); ex.Next()) {
1336 const TopoDS_Face& curf=TopoDS::Face(ex.Current());
1337 S = BRep_Tool::Surface(curf, l);
1340 BRepBndLib::Add(curf,aB);
1341 if (S->DynamicType() == STANDARD_TYPE(Geom_RectangularTrimmedSurface)) {
1342 S = Handle(Geom_RectangularTrimmedSurface)::DownCast (S)->BasisSurface();
1344 GeomAdaptor_Surface AS(S);
1345 switch (AS.GetType()) {
1347 case GeomAbs_Cylinder:
1350 tol=Precision::Confusion();
1353 case GeomAbs_Sphere:
1356 tol=Precision::Confusion()*2;
1360 tol=Precision::Confusion()*4;
1362 if (!aB.IsWhole()) {
1363 aB.Get(aXmin, aYmin, aZmin, aXmax, aYmax, aZmax);
1365 if (!aB.IsOpenXmin() && !aB.IsOpenXmax()) dMax=aXmax-aXmin;
1366 if (!aB.IsOpenYmin() && !aB.IsOpenYmax()) aYmin=aYmax-aYmin;
1367 if (!aB.IsOpenZmin() && !aB.IsOpenZmax()) aZmin=aZmax-aZmin;
1368 if (aYmin>dMax) dMax=aYmin;
1369 if (aZmin>dMax) dMax=aZmin;
1371 // Do not process tolerances > 1.
1372 if (tol>1.) tol=0.99;
1374 const Handle(BRep_TFace)& Tf = *((Handle(BRep_TFace)*)&curf.TShape());
1381 TopTools_IndexedDataMapOfShapeListOfShape parents;
1382 TopExp::MapShapesAndAncestors(aShape, TopAbs_EDGE, TopAbs_FACE, parents);
1383 TopTools_ListIteratorOfListOfShape lConx;
1384 Standard_Integer iCur;
1385 for (iCur=1; iCur<=parents.Extent(); iCur++) {
1387 for (lConx.Initialize(parents(iCur)); lConx.More(); lConx.Next()) {
1388 tol=Max(tol, BRep_Tool::Tolerance(TopoDS::Face(lConx.Value())));
1390 // Update can only increase tolerance, so if the edge has a greater
1391 // tolerance than its faces it is not concerned
1392 B.UpdateEdge(TopoDS::Edge(parents.FindKey(iCur)), tol);
1395 //Vertices are processed
1397 TopExp::MapShapesAndAncestors(aShape, TopAbs_VERTEX, TopAbs_EDGE, parents);
1398 TColStd_MapOfTransient Initialized;
1399 TopTools_MapOfShape Done;
1400 Standard_Integer nbV = parents.Extent();
1401 for (iCur=1; iCur<=nbV; iCur++) {
1404 const TopoDS_Vertex& V = TopoDS::Vertex(parents.FindKey(iCur));
1406 box.Add(BRep_Tool::Pnt(V));
1408 for (lConx.Initialize(parents(iCur)); lConx.More(); lConx.Next()) {
1409 const TopoDS_Edge& E = TopoDS::Edge(lConx.Value());
1410 if(!Done.Add(E)) continue;
1411 tol=Max(tol, BRep_Tool::Tolerance(E));
1412 if(!BRep_Tool::SameRange(E)) continue;
1413 Standard_Real par = BRep_Tool::Parameter(V,E);
1414 Handle(BRep_TEdge)& TE = *((Handle(BRep_TEdge)*)&E.TShape());
1415 BRep_ListIteratorOfListOfCurveRepresentation itcr(TE->Curves());
1416 const TopLoc_Location& Eloc = E.Location();
1417 while (itcr.More()) {
1418 // For each CurveRepresentation, check the provided parameter
1419 const Handle(BRep_CurveRepresentation)& cr = itcr.Value();
1420 const TopLoc_Location& loc = cr->Location();
1421 TopLoc_Location L = (Eloc * loc);
1422 if (cr->IsCurve3D()) {
1423 const Handle(Geom_Curve)& C = cr->Curve3D();
1424 if (!C.IsNull()) { // edge non degenerated
1425 p3d = C->Value(par);
1426 p3d.Transform(L.Transformation());
1430 else if (cr->IsCurveOnSurface()) {
1431 const Handle(Geom_Surface)& Su = cr->Surface();
1432 const Handle(Geom2d_Curve)& PC = cr->PCurve();
1433 Handle(Geom2d_Curve) PC2;
1434 if (cr->IsCurveOnClosedSurface()) {
1435 PC2 = cr->PCurve2();
1437 gp_Pnt2d p2d = PC->Value(par);
1438 p3d = Su->Value(p2d.X(),p2d.Y());
1439 p3d.Transform(L.Transformation());
1441 if (!PC2.IsNull()) {
1442 p2d = PC2->Value(par);
1443 p3d = Su->Value(p2d.X(),p2d.Y());
1444 p3d.Transform(L.Transformation());
1451 Standard_Real aXmin, aYmin, aZmin, aXmax, aYmax, aZmax;
1452 box.Get(aXmin, aYmin, aZmin, aXmax, aYmax, aZmax);
1453 aXmax -= aXmin; aYmax -= aYmin; aZmax -= aZmin;
1454 tol = Max(tol,sqrt(aXmax*aXmax+aYmax*aYmax+aZmax*aZmax));
1455 tol += 2.*Epsilon(tol);
1456 if (verifyTolerance) {
1457 // ASet minimum value of the tolerance
1458 // Attention to sharing of the vertex by other shapes
1459 const Handle(BRep_TVertex)& TV = *((Handle(BRep_TVertex)*)&V.TShape());
1460 if (Initialized.Add(TV))
1463 B.UpdateVertex(V, tol);
1466 // Update can only increase tolerance, so if the edge has a greater
1467 // tolerance than its faces it is not concerned
1468 B.UpdateVertex(V, tol);
1473 //=======================================================================
1474 //function : UpdateInnerTolerances
1476 //=======================================================================
1477 void BRepLib::UpdateInnerTolerances(const TopoDS_Shape& aShape)
1479 TopTools_IndexedDataMapOfShapeListOfShape EFmap;
1480 TopExp::MapShapesAndAncestors(aShape, TopAbs_EDGE, TopAbs_FACE, EFmap);
1482 for (Standard_Integer i = 1; i <= EFmap.Extent(); i++)
1484 TopoDS_Edge anEdge = TopoDS::Edge(EFmap.FindKey(i));
1485 TopoDS_Vertex V1, V2;
1486 TopExp::Vertices(anEdge, V1, V2);
1487 Standard_Real fpar, lpar;
1488 BRep_Tool::Range(anEdge, fpar, lpar);
1489 Standard_Real TolEdge = BRep_Tool::Tolerance(anEdge);
1491 Handle(BRepAdaptor_HCurve) anHCurve = new BRepAdaptor_HCurve();
1492 anHCurve->ChangeCurve().Initialize(anEdge);
1494 Pnt1 = BRep_Tool::Pnt(V1);
1496 Pnt2 = BRep_Tool::Pnt(V2);
1498 if (!BRep_Tool::Degenerated(anEdge) &&
1499 EFmap(i).Extent() > 0)
1501 NCollection_Sequence<Handle(Adaptor3d_HCurve)> theRep;
1502 theRep.Append(anHCurve);
1503 TopTools_ListIteratorOfListOfShape itl(EFmap(i));
1504 for (; itl.More(); itl.Next())
1506 const TopoDS_Face& aFace = TopoDS::Face(itl.Value());
1507 Handle(BRepAdaptor_HCurve) anHCurvOnSurf = new BRepAdaptor_HCurve();
1508 anHCurvOnSurf->ChangeCurve().Initialize(anEdge, aFace);
1509 theRep.Append(anHCurvOnSurf);
1512 const Standard_Integer NbSamples = (BRep_Tool::SameParameter(anEdge))? 23 : 2;
1513 Standard_Real delta = (lpar - fpar)/(NbSamples-1);
1514 Standard_Real MaxDist = 0.;
1515 for (Standard_Integer j = 2; j <= theRep.Length(); j++)
1517 for (Standard_Integer k = 0; k <= NbSamples; k++)
1519 Standard_Real ParamOnCenter = (k == NbSamples)? lpar :
1521 gp_Pnt Center = theRep(1)->Value(ParamOnCenter);
1522 Standard_Real ParamOnCurve = (BRep_Tool::SameParameter(anEdge))? ParamOnCenter
1523 : ((k == 0)? theRep(j)->FirstParameter() : theRep(j)->LastParameter());
1524 gp_Pnt aPoint = theRep(j)->Value(ParamOnCurve);
1525 Standard_Real aDist = Center.Distance(aPoint);
1527 aDist += 2.*Epsilon(aDist);
1528 if (aDist > MaxDist)
1531 //Update tolerances of vertices
1532 if (k == 0 && !V1.IsNull())
1534 Standard_Real aDist1 = Pnt1.Distance(aPoint);
1535 aDist1 += 2.*Epsilon(aDist1);
1536 BB.UpdateVertex(V1, aDist1);
1538 if (k == NbSamples && !V2.IsNull())
1540 Standard_Real aDist2 = Pnt2.Distance(aPoint);
1541 aDist2 += 2.*Epsilon(aDist2);
1542 BB.UpdateVertex(V2, aDist2);
1546 BB.UpdateEdge(anEdge, MaxDist);
1548 TolEdge = BRep_Tool::Tolerance(anEdge);
1551 gp_Pnt End1 = anHCurve->Value(fpar);
1552 Standard_Real dist1 = Pnt1.Distance(End1);
1553 dist1 += 2.*Epsilon(dist1);
1554 BB.UpdateVertex(V1, Max(dist1, TolEdge));
1558 gp_Pnt End2 = anHCurve->Value(lpar);
1559 Standard_Real dist2 = Pnt2.Distance(End2);
1560 dist2 += 2.*Epsilon(dist2);
1561 BB.UpdateVertex(V2, Max(dist2, TolEdge));
1566 //=======================================================================
1567 //function : OrientClosedSolid
1569 //=======================================================================
1570 Standard_Boolean BRepLib::OrientClosedSolid(TopoDS_Solid& solid)
1572 // Set material inside the solid
1573 BRepClass3d_SolidClassifier where(solid);
1574 where.PerformInfinitePoint(Precision::Confusion());
1575 if (where.State()==TopAbs_IN) {
1578 else if (where.State()==TopAbs_ON || where.State()==TopAbs_UNKNOWN)
1579 return Standard_False;
1581 return Standard_True;
1584 //=======================================================================
1585 //function : tgtfaces
1586 //purpose : check the angle at the border between two squares.
1587 // Two shares should have a shared front edge.
1588 //=======================================================================
1590 static Standard_Boolean tgtfaces(const TopoDS_Edge& Ed,
1591 const TopoDS_Face& F1,
1592 const TopoDS_Face& F2,
1593 const Standard_Real ta,
1594 const Standard_Boolean couture)
1596 // Check if pcurves exist on both faces of edge
1597 Standard_Real aFirst,aLast;
1598 Handle(Geom2d_Curve) aCurve;
1599 aCurve = BRep_Tool::CurveOnSurface(Ed,F1,aFirst,aLast);
1601 return Standard_False;
1602 aCurve = BRep_Tool::CurveOnSurface(Ed,F2,aFirst,aLast);
1604 return Standard_False;
1608 BRepAdaptor_Surface aBAS1(F1,Standard_False);
1609 BRepAdaptor_Surface aBAS2(F2,Standard_False);
1610 Handle(BRepAdaptor_HSurface) HS1 = new BRepAdaptor_HSurface(aBAS1);
1611 Handle(BRepAdaptor_HSurface) HS2;
1612 if(couture) HS2 = HS1;
1613 else HS2 = new BRepAdaptor_HSurface(aBAS2);
1615 E.Orientation(TopAbs_FORWARD);
1616 Handle(BRepAdaptor_HCurve2d) HC2d1 = new BRepAdaptor_HCurve2d();
1617 HC2d1->ChangeCurve2d().Initialize(E,F1);
1618 if(couture) E.Orientation(TopAbs_REVERSED);
1619 Handle(BRepAdaptor_HCurve2d) HC2d2 = new BRepAdaptor_HCurve2d();
1620 HC2d2->ChangeCurve2d().Initialize(E,F2);
1621 Adaptor3d_CurveOnSurface C1(HC2d1,HS1);
1622 Adaptor3d_CurveOnSurface C2(HC2d2,HS2);
1624 Standard_Boolean rev1 = (F1.Orientation() == TopAbs_REVERSED);
1625 Standard_Boolean rev2 = (F2.Orientation() == TopAbs_REVERSED);
1626 Standard_Real f,l,eps, angmax = -M_PI;
1627 Standard_Real ang =0.;
1628 BRep_Tool::Range(E,f,l);
1629 Extrema_LocateExtPC ext;
1630 Standard_Boolean IsInitialized = Standard_False;
1633 f += eps; // to avoid calculations on
1634 l -= eps; // points of pointed squares.
1636 gp_Pnt pp1,pp2;//,PP;
1639 Standard_Real uu, vv, norm;
1642 Standard_Boolean Nok;
1643 for(i = 0; (i<= 20) && (angmax<=ta) ; i++){
1644 // First suppose that this is sameParameter
1645 Nok = Standard_True;
1648 HS1->D1(p.X(),p.Y(),pp1,du,dv);
1649 d1 = (du.Crossed(dv));
1650 norm = d1.Magnitude();
1651 if (norm > 1.e-12) d1 /= norm;
1652 else Nok=Standard_False;
1653 if(rev1) d1.Reverse();
1656 HS2->D1(p.X(), p.Y(), pp2, du, dv);
1657 d2 = (du.Crossed(dv));
1658 norm = d2.Magnitude();
1659 if (norm> 1.e-12) d2 /= norm;
1660 else Nok=Standard_False;
1661 if(rev2) d2.Reverse();
1662 if (Nok) ang = d1.Angle(d2);
1664 if (Nok &&(ang > ta)) { // Refine by projection
1665 if (! IsInitialized ) {
1666 ext.Initialize(C2,f,l,Precision::PConfusion());
1667 IsInitialized = Standard_True;
1670 if(ext.IsDone() && ext.IsMin()){
1671 Extrema_POnCurv poc = ext.Point();
1672 Standard_Real v = poc.Parameter();
1676 HS2->D1(p.X(), p.Y(), pp2, du, dv);
1677 d2 = (du.Crossed(dv));
1678 norm = d2.Magnitude();
1679 if (norm> 1.e-12) d2 /= norm;
1680 else Nok = Standard_False;
1681 if(rev2) d2.Reverse();
1682 if (Nok) ang = d1.Angle(d2);
1685 if(ang >= angmax) angmax = ang;
1688 return (angmax<=ta);
1693 //=======================================================================
1694 // function : EncodeRegularity
1695 // purpose : code the regularities on all edges of the shape, boundary of
1696 // two faces that do not have it.
1697 //=======================================================================
1699 void BRepLib::EncodeRegularity(const TopoDS_Shape& S,
1700 const Standard_Real TolAng)
1703 TopTools_IndexedDataMapOfShapeListOfShape M;
1704 TopExp::MapShapesAndAncestors(S,TopAbs_EDGE,TopAbs_FACE,M);
1705 TopTools_ListIteratorOfListOfShape It;
1708 Standard_Boolean found, couture;
1709 for(Standard_Integer i = 1; i <= M.Extent(); i++){
1710 TopoDS_Edge E = TopoDS::Edge(M.FindKey(i));
1711 found = Standard_False; couture = Standard_False;
1713 for(It.Initialize(M.FindFromIndex(i));It.More() && !found;It.Next()){
1714 if(F1.IsNull()) { F1 = TopoDS::Face(It.Value()); }
1716 if(!F1.IsSame(TopoDS::Face(It.Value()))){
1717 found = Standard_True;
1718 F2 = TopoDS::Face(It.Value());
1722 if (!found && !F1.IsNull()){//is it a sewing edge?
1723 TopAbs_Orientation orE = E.Orientation();
1725 for(Ex.Init(F1,TopAbs_EDGE);Ex.More() && !found;Ex.Next()){
1726 curE= TopoDS::Edge(Ex.Current());
1727 if(E.IsSame(curE) && orE != curE.Orientation()) {
1728 found = Standard_True;
1729 couture = Standard_True;
1735 if(BRep_Tool::Continuity(E,F1,F2)<=GeomAbs_C0){
1738 if(tgtfaces(E, F1, F2, TolAng, couture)){
1739 B.Continuity(E,F1,F2,GeomAbs_G1);
1742 catch(Standard_Failure)
1750 //=======================================================================
1751 // function : EncodeRegularity
1752 // purpose : code the regularity between 2 faces on an edge
1753 //=======================================================================
1755 void BRepLib::EncodeRegularity(TopoDS_Edge& E,
1756 const TopoDS_Face& F1,
1757 const TopoDS_Face& F2,
1758 const Standard_Real TolAng)
1761 if(BRep_Tool::Continuity(E,F1,F2)<=GeomAbs_C0){
1763 if( tgtfaces(E, F1, F2, TolAng, F1.IsEqual(F2))) {
1764 B.Continuity(E,F1,F2,GeomAbs_G1);
1767 catch(Standard_Failure)
1773 //=======================================================================
1774 // function : EnsureNormalConsistency
1775 // purpose : Corrects the normals in Poly_Triangulation of faces.
1776 // Returns TRUE if any correction is done.
1777 //=======================================================================
1778 Standard_Boolean BRepLib::
1779 EnsureNormalConsistency(const TopoDS_Shape& theShape,
1780 const Standard_Real theAngTol,
1781 const Standard_Boolean theForceComputeNormals)
1783 const Standard_Real aThresDot = cos(theAngTol);
1785 Standard_Boolean aRetVal = Standard_False, isNormalsFound = Standard_False;
1787 // compute normals if they are absent
1788 TopExp_Explorer anExpFace(theShape,TopAbs_FACE);
1789 for (; anExpFace.More(); anExpFace.Next())
1791 const TopoDS_Face& aFace = TopoDS::Face(anExpFace.Current());
1792 const Handle(Geom_Surface) aSurf = BRep_Tool::Surface(aFace);
1795 TopLoc_Location aLoc;
1796 const Handle(Poly_Triangulation)& aPT = BRep_Tool::Triangulation(aFace, aLoc);
1799 if (!theForceComputeNormals && aPT->HasNormals())
1801 isNormalsFound = Standard_True;
1805 GeomLProp_SLProps aSLP(aSurf, 2, Precision::Confusion());
1806 const Standard_Integer anArrDim = 3*aPT->NbNodes();
1807 Handle(TShort_HArray1OfShortReal) aNormArr = new TShort_HArray1OfShortReal(1, anArrDim);
1808 Standard_Integer anNormInd = aNormArr->Lower();
1809 for(Standard_Integer i = aPT->UVNodes().Lower(); i <= aPT->UVNodes().Upper(); i++)
1811 const gp_Pnt2d &aP2d = aPT->UVNodes().Value(i);
1812 aSLP.SetParameters(aP2d.X(), aP2d.Y());
1814 gp_XYZ aNorm(0.,0.,0.);
1815 if(!aSLP.IsNormalDefined())
1818 cout << "BRepLib::EnsureNormalConsistency(): Cannot find normal!" << endl;
1823 aNorm = aSLP.Normal().XYZ();
1824 if (aFace.Orientation() == TopAbs_REVERSED)
1827 aNormArr->ChangeValue(anNormInd++) = static_cast<Standard_ShortReal>(aNorm.X());
1828 aNormArr->ChangeValue(anNormInd++) = static_cast<Standard_ShortReal>(aNorm.Y());
1829 aNormArr->ChangeValue(anNormInd++) = static_cast<Standard_ShortReal>(aNorm.Z());
1832 aRetVal = Standard_True;
1833 isNormalsFound = Standard_True;
1834 aPT->SetNormals(aNormArr);
1843 TopTools_IndexedDataMapOfShapeListOfShape aMapEF;
1844 TopExp::MapShapesAndAncestors(theShape,TopAbs_EDGE,TopAbs_FACE,aMapEF);
1845 for(Standard_Integer anInd = 1; anInd <= aMapEF.Extent(); anInd++)
1847 const TopoDS_Edge& anEdg = TopoDS::Edge(aMapEF.FindKey(anInd));
1848 const TopTools_ListOfShape& anEdgList = aMapEF.FindFromIndex(anInd);
1849 if (anEdgList.Extent() != 2)
1851 TopTools_ListIteratorOfListOfShape anItF(anEdgList);
1852 const TopoDS_Face aFace1 = TopoDS::Face(anItF.Value());
1854 const TopoDS_Face aFace2 = TopoDS::Face(anItF.Value());
1855 TopLoc_Location aLoc1, aLoc2;
1856 const Handle(Poly_Triangulation)& aPT1 = BRep_Tool::Triangulation(aFace1, aLoc1);
1857 const Handle(Poly_Triangulation)& aPT2 = BRep_Tool::Triangulation(aFace2, aLoc2);
1859 if(aPT1.IsNull() || aPT2.IsNull())
1862 if(!aPT1->HasNormals() || !aPT2->HasNormals())
1865 const Handle(Poly_PolygonOnTriangulation)& aPTEF1 =
1866 BRep_Tool::PolygonOnTriangulation(anEdg, aPT1, aLoc1);
1867 const Handle(Poly_PolygonOnTriangulation)& aPTEF2 =
1868 BRep_Tool::PolygonOnTriangulation(anEdg, aPT2, aLoc2);
1870 TShort_Array1OfShortReal& aNormArr1 = aPT1->ChangeNormals();
1871 TShort_Array1OfShortReal& aNormArr2 = aPT2->ChangeNormals();
1873 for(Standard_Integer anEdgNode = aPTEF1->Nodes().Lower();
1874 anEdgNode <= aPTEF1->Nodes().Upper(); anEdgNode++)
1877 const Standard_Integer aFNodF1 = aPTEF1->Nodes().Value(anEdgNode);
1878 const Standard_Integer aFNodF2 = aPTEF2->Nodes().Value(anEdgNode);
1880 const Standard_Integer aFNorm1FirstIndex = aNormArr1.Lower() + 3*
1881 (aFNodF1 - aPT1->Nodes().Lower());
1882 const Standard_Integer aFNorm2FirstIndex = aNormArr2.Lower() + 3*
1883 (aFNodF2 - aPT2->Nodes().Lower());
1885 gp_XYZ aNorm1(aNormArr1.Value(aFNorm1FirstIndex),
1886 aNormArr1.Value(aFNorm1FirstIndex+1),
1887 aNormArr1.Value(aFNorm1FirstIndex+2));
1888 gp_XYZ aNorm2(aNormArr2.Value(aFNorm2FirstIndex),
1889 aNormArr2.Value(aFNorm2FirstIndex+1),
1890 aNormArr2.Value(aFNorm2FirstIndex+2));
1891 const Standard_Real aDot = aNorm1 * aNorm2;
1893 if(aDot > aThresDot)
1895 gp_XYZ aNewNorm = (aNorm1 + aNorm2).Normalized();
1896 aNormArr1.ChangeValue(aFNorm1FirstIndex) =
1897 aNormArr2.ChangeValue(aFNorm2FirstIndex) =
1898 static_cast<Standard_ShortReal>(aNewNorm.X());
1899 aNormArr1.ChangeValue(aFNorm1FirstIndex+1) =
1900 aNormArr2.ChangeValue(aFNorm2FirstIndex+1) =
1901 static_cast<Standard_ShortReal>(aNewNorm.Y());
1902 aNormArr1.ChangeValue(aFNorm1FirstIndex+2) =
1903 aNormArr2.ChangeValue(aFNorm2FirstIndex+2) =
1904 static_cast<Standard_ShortReal>(aNewNorm.Z());
1905 aRetVal = Standard_True;
1913 //=======================================================================
1914 //function : SortFaces
1916 //=======================================================================
1918 void BRepLib::SortFaces (const TopoDS_Shape& Sh,
1919 TopTools_ListOfShape& LF)
1922 TopTools_ListOfShape LTri,LPlan,LCyl,LCon,LSphere,LTor,LOther;
1923 TopExp_Explorer exp(Sh,TopAbs_FACE);
1925 Handle(Geom_Surface) S;
1927 for (; exp.More(); exp.Next()) {
1928 const TopoDS_Face& F = TopoDS::Face(exp.Current());
1929 S = BRep_Tool::Surface(F, l);
1931 if (S->DynamicType() == STANDARD_TYPE(Geom_RectangularTrimmedSurface)) {
1932 S = Handle(Geom_RectangularTrimmedSurface)::DownCast (S)->BasisSurface();
1934 GeomAdaptor_Surface AS(S);
1935 switch (AS.GetType()) {
1941 case GeomAbs_Cylinder:
1951 case GeomAbs_Sphere:
1965 else LTri.Append(F);
1967 LF.Append(LPlan); LF.Append(LCyl ); LF.Append(LCon); LF.Append(LSphere);
1968 LF.Append(LTor ); LF.Append(LOther); LF.Append(LTri);
1971 //=======================================================================
1972 //function : ReverseSortFaces
1974 //=======================================================================
1976 void BRepLib::ReverseSortFaces (const TopoDS_Shape& Sh,
1977 TopTools_ListOfShape& LF)
1980 TopTools_ListOfShape LTri,LPlan,LCyl,LCon,LSphere,LTor,LOther;
1981 TopExp_Explorer exp(Sh,TopAbs_FACE);
1983 Handle(Geom_Surface) S;
1985 for (; exp.More(); exp.Next()) {
1986 const TopoDS_Face& F = TopoDS::Face(exp.Current());
1987 S = BRep_Tool::Surface(F, l);
1989 if (S->DynamicType() == STANDARD_TYPE(Geom_RectangularTrimmedSurface)) {
1990 S = Handle(Geom_RectangularTrimmedSurface)::DownCast (S)->BasisSurface();
1992 GeomAdaptor_Surface AS(S);
1993 switch (AS.GetType()) {
1999 case GeomAbs_Cylinder:
2009 case GeomAbs_Sphere:
2023 else LTri.Append(F);
2025 LF.Append(LTri); LF.Append(LOther); LF.Append(LTor ); LF.Append(LSphere);
2026 LF.Append(LCon); LF.Append(LCyl ); LF.Append(LPlan);