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_ErrorHandler.hxx>
75 #include <Standard_Real.hxx>
76 #include <TColgp_Array1OfPnt.hxx>
77 #include <TColgp_Array1OfPnt2d.hxx>
78 #include <TColStd_Array1OfReal.hxx>
79 #include <TColStd_MapOfTransient.hxx>
81 #include <TopExp_Explorer.hxx>
83 #include <TopoDS_Edge.hxx>
84 #include <TopoDS_Face.hxx>
85 #include <TopoDS_Shape.hxx>
86 #include <TopoDS_Solid.hxx>
87 #include <TopoDS_Vertex.hxx>
88 #include <TopTools_IndexedDataMapOfShapeListOfShape.hxx>
89 #include <TopTools_ListIteratorOfListOfShape.hxx>
90 #include <TopTools_MapOfShape.hxx>
91 #include <TShort_HArray1OfShortReal.hxx>
92 #include <TColgp_Array1OfXY.hxx>
93 #include <BRepTools_ReShape.hxx>
94 #include <TopTools_DataMapOfShapeReal.hxx>
95 #include <TopoDS_LockedShape.hxx>
99 // TODO - not thread-safe static variables
100 static Standard_Real thePrecision = Precision::Confusion();
101 static Handle(Geom_Plane) thePlane;
103 static void InternalUpdateTolerances(const TopoDS_Shape& theOldShape,
104 const Standard_Boolean IsVerifyTolerance, const Standard_Boolean IsMutableInput, BRepTools_ReShape& theReshaper);
106 //=======================================================================
107 // function: BRepLib_ComparePoints
108 // purpose: implementation of IsLess() function for two points
109 //=======================================================================
110 struct BRepLib_ComparePoints {
111 bool operator()(const gp_Pnt& theP1, const gp_Pnt& theP2)
113 for (Standard_Integer i = 1; i <= 3; ++i) {
114 if (theP1.Coord(i) < theP2.Coord(i)) {
115 return Standard_True;
117 else if (theP1.Coord(i) > theP2.Coord(i)) {
118 return Standard_False;
121 return Standard_False;
126 //=======================================================================
127 //function : Precision
129 //=======================================================================
131 void BRepLib::Precision(const Standard_Real P)
136 //=======================================================================
137 //function : Precision
139 //=======================================================================
141 Standard_Real BRepLib::Precision()
146 //=======================================================================
149 //=======================================================================
151 void BRepLib::Plane(const Handle(Geom_Plane)& P)
157 //=======================================================================
160 //=======================================================================
162 const Handle(Geom_Plane)& BRepLib::Plane()
164 if (thePlane.IsNull()) thePlane = new Geom_Plane(gp::XOY());
167 //=======================================================================
168 //function : CheckSameRange
170 //=======================================================================
172 Standard_Boolean BRepLib::CheckSameRange(const TopoDS_Edge& AnEdge,
173 const Standard_Real Tolerance)
175 Standard_Boolean IsSameRange = Standard_True,
176 first_time_in = Standard_True ;
178 BRep_ListIteratorOfListOfCurveRepresentation an_Iterator
179 ((*((Handle(BRep_TEdge)*)&AnEdge.TShape()))->ChangeCurves());
181 Standard_Real first, last;
182 Standard_Real current_first =0., current_last =0. ;
183 Handle(BRep_GCurve) geometric_representation_ptr ;
185 while (IsSameRange && an_Iterator.More()) {
186 geometric_representation_ptr =
187 Handle(BRep_GCurve)::DownCast(an_Iterator.Value());
188 if (!geometric_representation_ptr.IsNull()) {
190 first = geometric_representation_ptr->First();
191 last = geometric_representation_ptr->Last();
192 if (first_time_in ) {
193 current_first = first ;
194 current_last = last ;
195 first_time_in = Standard_False ;
198 IsSameRange = (Abs(current_first - first) <= Tolerance)
199 && (Abs(current_last -last) <= Tolerance ) ;
207 //=======================================================================
208 //function : SameRange
210 //=======================================================================
212 void BRepLib::SameRange(const TopoDS_Edge& AnEdge,
213 const Standard_Real Tolerance)
215 BRep_ListIteratorOfListOfCurveRepresentation an_Iterator
216 ((*((Handle(BRep_TEdge)*)&AnEdge.TShape()))->ChangeCurves());
218 Handle(Geom2d_Curve) Curve2dPtr, Curve2dPtr2, NewCurve2dPtr, NewCurve2dPtr2;
219 TopLoc_Location LocalLoc ;
221 Standard_Boolean first_time_in = Standard_True,
224 Handle(BRep_GCurve) geometric_representation_ptr ;
230 const Handle(Geom_Curve) C = BRep_Tool::Curve(AnEdge,
235 first_time_in = Standard_False ;
238 while (an_Iterator.More()) {
239 geometric_representation_ptr =
240 Handle(BRep_GCurve)::DownCast(an_Iterator.Value());
241 if (! geometric_representation_ptr.IsNull()) {
243 has_curve = Standard_False ;
244 first = geometric_representation_ptr->First();
245 last = geometric_representation_ptr->Last();
246 if (geometric_representation_ptr->IsCurveOnSurface()) {
247 Curve2dPtr = geometric_representation_ptr->PCurve() ;
248 has_curve = Standard_True ;
250 if (geometric_representation_ptr->IsCurveOnClosedSurface()) {
251 Curve2dPtr2 = geometric_representation_ptr->PCurve2() ;
252 has_closed_curve = Standard_True ;
254 if (has_curve || has_closed_curve) {
256 current_first = first ;
257 current_last = last ;
258 first_time_in = Standard_False ;
261 if (Abs(first - current_first) > Precision::Confusion() ||
262 Abs(last - current_last) > Precision::Confusion() )
266 GeomLib::SameRange(Tolerance,
268 geometric_representation_ptr->First(),
269 geometric_representation_ptr->Last(),
273 geometric_representation_ptr->PCurve(NewCurve2dPtr) ;
275 if (has_closed_curve)
277 GeomLib::SameRange(Tolerance,
279 geometric_representation_ptr->First(),
280 geometric_representation_ptr->Last(),
284 geometric_representation_ptr->PCurve2(NewCurve2dPtr2) ;
292 B.Range(TopoDS::Edge(AnEdge),
300 //=======================================================================
301 //function : EvaluateMaxSegment
302 //purpose : return MaxSegment to pass in approximation, if MaxSegment==0 provided
303 //=======================================================================
305 static Standard_Integer evaluateMaxSegment(const Standard_Integer aMaxSegment,
306 const Adaptor3d_CurveOnSurface& aCurveOnSurface)
308 if (aMaxSegment != 0) return aMaxSegment;
310 Handle(Adaptor3d_HSurface) aSurf = aCurveOnSurface.GetSurface();
311 Handle(Adaptor2d_HCurve2d) aCurv2d = aCurveOnSurface.GetCurve();
313 Standard_Real aNbSKnots = 0, aNbC2dKnots = 0;
315 if (aSurf->GetType() == GeomAbs_BSplineSurface) {
316 Handle(Geom_BSplineSurface) aBSpline = aSurf->BSpline();
317 aNbSKnots = Max(aBSpline->NbUKnots(), aBSpline->NbVKnots());
319 if (aCurv2d->GetType() == GeomAbs_BSplineCurve) {
320 aNbC2dKnots = aCurv2d->NbKnots();
322 Standard_Integer aReturn = (Standard_Integer) ( 30 + Max(aNbSKnots, aNbC2dKnots) ) ;
326 //=======================================================================
327 //function : BuildCurve3d
329 //=======================================================================
331 Standard_Boolean BRepLib::BuildCurve3d(const TopoDS_Edge& AnEdge,
332 const Standard_Real Tolerance,
333 const GeomAbs_Shape Continuity,
334 const Standard_Integer MaxDegree,
335 const Standard_Integer MaxSegment)
337 Standard_Integer //ErrorCode,
343 TopLoc_Location LocalLoc,L[2],LC;
344 Standard_Real f,l,fc,lc, first[2], last[2],
348 Handle(Geom2d_Curve) Curve2dPtr, Curve2dArray[2] ;
349 Handle(Geom_Surface) SurfacePtr, SurfaceArray[2] ;
351 Standard_Integer not_done ;
352 // if the edge has a 3d curve returns true
355 const Handle(Geom_Curve) C = BRep_Tool::Curve(AnEdge,LocalLoc,f,l);
357 return Standard_True;
359 // this should not exists but UpdateEdge makes funny things
360 // if the edge is not same range
362 if (! CheckSameRange(AnEdge,
363 Precision::Confusion())) {
370 // search a curve on a plane
371 Handle(Geom_Surface) S;
372 Handle(Geom2d_Curve) PC;
373 Standard_Integer i = 0;
374 Handle(Geom_Plane) P;
379 BRep_Tool::CurveOnSurface(AnEdge,PC,S,LocalLoc,f,l,i);
380 Handle(Geom_RectangularTrimmedSurface) RT =
381 Handle(Geom_RectangularTrimmedSurface)::DownCast(S);
383 P = Handle(Geom_Plane)::DownCast(S);
386 P = Handle(Geom_Plane)::DownCast(RT->BasisSurface());
388 not_done = ! (S.IsNull() || !P.IsNull()) ;
391 // compute the 3d curve
392 gp_Ax2 axes = P->Position().Ax2();
393 Handle(Geom_Curve) C3d = GeomLib::To3d(axes,PC);
395 return Standard_False;
397 Standard_Real First, Last;
400 B.UpdateEdge(AnEdge,C3d,LocalLoc,0.0e0);
401 BRep_Tool::Range(AnEdge, S, LC, First, Last);
402 B.Range(AnEdge, First, Last); //Do not forget 3D range.(PRO6412)
406 // compute the 3d curve using existing surface
410 if (!BRep_Tool::Degenerated(AnEdge)) {
412 for (ii = 0 ; ii < 3 ; ii++ ) {
413 BRep_Tool::CurveOnSurface(TopoDS::Edge(AnEdge),
421 if (!Curve2dPtr.IsNull() && jj < 2){
422 Curve2dArray[jj] = Curve2dPtr ;
423 SurfaceArray[jj] = SurfacePtr ;
432 Curve2dPtr = Curve2dArray[0] ;
433 SurfacePtr = SurfaceArray[0] ;
435 Geom2dAdaptor_Curve AnAdaptor3dCurve2d (Curve2dPtr, f, l) ;
436 GeomAdaptor_Surface AnAdaptor3dSurface (SurfacePtr) ;
437 Handle(Geom2dAdaptor_HCurve) AnAdaptor3dCurve2dPtr =
438 new Geom2dAdaptor_HCurve(AnAdaptor3dCurve2d) ;
439 Handle(GeomAdaptor_HSurface) AnAdaptor3dSurfacePtr =
440 new GeomAdaptor_HSurface (AnAdaptor3dSurface) ;
441 Adaptor3d_CurveOnSurface CurveOnSurface( AnAdaptor3dCurve2dPtr,
442 AnAdaptor3dSurfacePtr) ;
444 Handle(Geom_Curve) NewCurvePtr ;
446 GeomLib::BuildCurve3d(Tolerance,
455 evaluateMaxSegment(MaxSegment,CurveOnSurface)) ;
457 tolerance = BRep_Tool::Tolerance(AnEdge) ;
459 //max_deviation = Max(tolerance, max_deviation) ;
460 max_deviation = Max( tolerance, Tolerance );
461 if (NewCurvePtr.IsNull())
462 return Standard_False;
463 B.UpdateEdge(TopoDS::Edge(AnEdge),
469 // if there is only one curve on surface attached to the edge
470 // than it can be qualified sameparameter
472 B.SameParameter(TopoDS::Edge(AnEdge),
477 return Standard_False ;
481 return Standard_True;
483 //=======================================================================
484 //function : BuildCurves3d
486 //=======================================================================
488 Standard_Boolean BRepLib::BuildCurves3d(const TopoDS_Shape& S)
491 return BRepLib::BuildCurves3d(S,
495 //=======================================================================
496 //function : BuildCurves3d
498 //=======================================================================
500 Standard_Boolean BRepLib::BuildCurves3d(const TopoDS_Shape& S,
501 const Standard_Real Tolerance,
502 const GeomAbs_Shape Continuity,
503 const Standard_Integer MaxDegree,
504 const Standard_Integer MaxSegment)
506 Standard_Boolean boolean_value,
508 TopTools_MapOfShape a_counter ;
509 TopExp_Explorer ex(S,TopAbs_EDGE);
512 if (a_counter.Add(ex.Current())) {
514 BuildCurve3d(TopoDS::Edge(ex.Current()),
515 Tolerance, Continuity,
516 MaxDegree, MaxSegment);
517 ok = ok && boolean_value ;
523 //=======================================================================
524 //function : UpdateEdgeTolerance
526 //=======================================================================
528 Standard_Boolean BRepLib::UpdateEdgeTol(const TopoDS_Edge& AnEdge,
529 const Standard_Real MinToleranceRequested,
530 const Standard_Real MaxToleranceToCheck)
533 Standard_Integer curve_on_surface_index,
540 geom_reference_curve_flag = 0,
541 max_sampling_points = 90,
542 min_sampling_points = 30 ;
544 Standard_Real factor = 100.0e0,
545 // sampling_array[2],
550 coded_edge_tolerance,
551 edge_tolerance = 0.0e0 ;
552 Handle(TColStd_HArray1OfReal) parameters_ptr ;
553 Handle(BRep_GCurve) geometric_representation_ptr ;
555 if (BRep_Tool::Degenerated(AnEdge)) return Standard_False ;
556 coded_edge_tolerance = BRep_Tool::Tolerance(AnEdge) ;
557 if (coded_edge_tolerance > MaxToleranceToCheck) return Standard_False ;
559 const Handle(BRep_TEdge)& TE = *((Handle(BRep_TEdge)*)&AnEdge.TShape());
560 BRep_ListOfCurveRepresentation& list_curve_rep = TE->ChangeCurves() ;
561 BRep_ListIteratorOfListOfCurveRepresentation an_iterator(list_curve_rep),
562 second_iterator(list_curve_rep) ;
563 Handle(Geom2d_Curve) curve2d_ptr, new_curve2d_ptr;
564 Handle(Geom_Surface) surface_ptr ;
565 TopLoc_Location local_location ;
566 GCPnts_QuasiUniformDeflection a_sampler ;
567 GeomAdaptor_Curve geom_reference_curve ;
568 Adaptor3d_CurveOnSurface curve_on_surface_reference ;
569 Handle(Geom_Curve) C = BRep_Tool::Curve(AnEdge,
573 curve_on_surface_index = -1 ;
575 if (! local_location.IsIdentity()) {
576 C = Handle(Geom_Curve)::
577 DownCast(C-> Transformed(local_location.Transformation()) ) ;
579 geom_reference_curve.Load(C) ;
580 geom_reference_curve_flag = 1 ;
581 a_sampler.Initialize(geom_reference_curve,
582 MinToleranceRequested * factor,
588 curve_on_surface_index = 0 ;
590 while (not_done && an_iterator.More()) {
591 geometric_representation_ptr =
592 Handle(BRep_GCurve)::DownCast(second_iterator.Value());
593 if (!geometric_representation_ptr.IsNull()
594 && geometric_representation_ptr->IsCurveOnSurface()) {
595 curve2d_ptr = geometric_representation_ptr->PCurve() ;
596 local_location = geometric_representation_ptr->Location() ;
597 current_first = geometric_representation_ptr->First();
598 //first = geometric_representation_ptr->First();
599 current_last = geometric_representation_ptr->Last();
602 if (! local_location.IsIdentity() ) {
603 surface_ptr = Handle(Geom_Surface)::
604 DownCast( geometric_representation_ptr->Surface()->
605 Transformed(local_location.Transformation()) ) ;
609 geometric_representation_ptr->Surface() ;
613 curve_on_surface_index += 1 ;
615 Geom2dAdaptor_Curve AnAdaptor3dCurve2d (curve2d_ptr) ;
616 GeomAdaptor_Surface AnAdaptor3dSurface (surface_ptr) ;
617 Handle(Geom2dAdaptor_HCurve) AnAdaptor3dCurve2dPtr =
618 new Geom2dAdaptor_HCurve(AnAdaptor3dCurve2d) ;
619 Handle(GeomAdaptor_HSurface) AnAdaptor3dSurfacePtr =
620 new GeomAdaptor_HSurface (AnAdaptor3dSurface) ;
621 curve_on_surface_reference.Load (AnAdaptor3dCurve2dPtr, AnAdaptor3dSurfacePtr);
622 a_sampler.Initialize(curve_on_surface_reference,
623 MinToleranceRequested * factor,
627 TColStd_Array1OfReal sampling_parameters(1,a_sampler.NbPoints()) ;
628 for (ii = 1 ; ii <= a_sampler.NbPoints() ; ii++) {
629 sampling_parameters(ii) = a_sampler.Parameter(ii) ;
631 if (a_sampler.NbPoints() < min_sampling_points) {
632 GeomLib::DensifyArray1OfReal(min_sampling_points,
636 else if (a_sampler.NbPoints() > max_sampling_points) {
637 GeomLib::RemovePointsFromArray(max_sampling_points,
644 new TColStd_HArray1OfReal(1,sampling_parameters.Length()) ;
645 for (ii = sampling_parameters.Lower() ; ii <= sampling_parameters.Upper() ; ii++) {
646 parameters_ptr->ChangeArray1()(jj) =
647 sampling_parameters(ii) ;
654 while (second_iterator.More()) {
655 geometric_representation_ptr =
656 Handle(BRep_GCurve)::DownCast(second_iterator.Value());
657 if (! geometric_representation_ptr.IsNull() &&
658 curve_index != curve_on_surface_index) {
660 has_curve = Standard_False ;
661 // first = geometric_representation_ptr->First();
662 // last = geometric_representation_ptr->Last();
663 local_location = geometric_representation_ptr->Location() ;
664 if (geometric_representation_ptr->IsCurveOnSurface()) {
665 curve2d_ptr = geometric_representation_ptr->PCurve() ;
666 has_curve = Standard_True ;
668 if (geometric_representation_ptr->IsCurveOnClosedSurface()) {
669 curve2d_ptr = geometric_representation_ptr->PCurve2() ;
670 has_closed_curve = Standard_True ;
675 if (! local_location.IsIdentity() ) {
676 surface_ptr = Handle(Geom_Surface)::
677 DownCast( geometric_representation_ptr->Surface()->
678 Transformed(local_location.Transformation()) ) ;
682 geometric_representation_ptr->Surface() ;
684 Geom2dAdaptor_Curve an_adaptor_curve2d (curve2d_ptr) ;
685 GeomAdaptor_Surface an_adaptor_surface(surface_ptr) ;
686 Handle(Geom2dAdaptor_HCurve) an_adaptor_curve2d_ptr =
687 new Geom2dAdaptor_HCurve(an_adaptor_curve2d) ;
688 Handle(GeomAdaptor_HSurface) an_adaptor_surface_ptr =
689 new GeomAdaptor_HSurface (an_adaptor_surface) ;
690 Adaptor3d_CurveOnSurface a_curve_on_surface(an_adaptor_curve2d_ptr,
691 an_adaptor_surface_ptr) ;
693 if (BRep_Tool::SameParameter(AnEdge)) {
695 GeomLib::EvalMaxParametricDistance(a_curve_on_surface,
696 geom_reference_curve,
697 MinToleranceRequested,
698 parameters_ptr->Array1(),
701 else if (geom_reference_curve_flag) {
702 GeomLib::EvalMaxDistanceAlongParameter(a_curve_on_surface,
703 geom_reference_curve,
704 MinToleranceRequested,
705 parameters_ptr->Array1(),
710 GeomLib::EvalMaxDistanceAlongParameter(a_curve_on_surface,
711 curve_on_surface_reference,
712 MinToleranceRequested,
713 parameters_ptr->Array1(),
716 max_distance *= safe_factor ;
717 edge_tolerance = Max(max_distance, edge_tolerance) ;
723 second_iterator.Next() ;
726 TE->Tolerance(edge_tolerance);
727 return Standard_True ;
730 //=======================================================================
731 //function : UpdateEdgeTolerance
733 //=======================================================================
734 Standard_Boolean BRepLib::UpdateEdgeTolerance(const TopoDS_Shape& S,
735 const Standard_Real MinToleranceRequested,
736 const Standard_Real MaxToleranceToCheck)
738 TopExp_Explorer ex(S,TopAbs_EDGE);
739 TopTools_MapOfShape a_counter ;
741 Standard_Boolean return_status = Standard_False,
745 if (a_counter.Add(ex.Current())) {
747 BRepLib::UpdateEdgeTol(TopoDS::Edge(ex.Current()),
748 MinToleranceRequested,
749 MaxToleranceToCheck) ;
750 if (local_flag && ! return_status) {
751 return_status = Standard_True ;
756 return return_status ;
759 //=======================================================================
760 //function : GetEdgeTol
762 //=======================================================================
763 static void GetEdgeTol(const TopoDS_Edge& theEdge,
764 const TopoDS_Face& theFace, Standard_Real& theEdTol)
767 const Handle(Geom_Surface)& S = BRep_Tool::Surface(theFace,L);
768 TopLoc_Location l = L.Predivided(theEdge.Location());
770 const Handle(BRep_TEdge)& TE = *((Handle(BRep_TEdge)*)&theEdge.TShape());
771 BRep_ListIteratorOfListOfCurveRepresentation itcr(TE->Curves());
773 while (itcr.More()) {
774 const Handle(BRep_CurveRepresentation)& cr = itcr.Value();
775 if(cr->IsCurveOnSurface(S,l)) return;
779 Handle(Geom_Plane) GP;
780 Handle(Geom_RectangularTrimmedSurface) GRTS;
781 GRTS = Handle(Geom_RectangularTrimmedSurface)::DownCast(S);
783 GP = Handle(Geom_Plane)::DownCast(GRTS->BasisSurface());
785 GP = Handle(Geom_Plane)::DownCast(S);
787 Handle(GeomAdaptor_HCurve) HC = new GeomAdaptor_HCurve();
788 Handle(GeomAdaptor_HSurface) HS = new GeomAdaptor_HSurface();
791 Standard_Real First, Last;
792 GeomAdaptor_Curve& GAC = HC->ChangeCurve();
793 GAC.Load(BRep_Tool::Curve(theEdge,LC,First,Last));
794 LC = L.Predivided(LC);
796 if (!LC.IsIdentity()) {
797 GP = Handle(Geom_Plane)::DownCast(
798 GP->Transformed(LC.Transformation()));
800 GeomAdaptor_Surface& GAS = HS->ChangeSurface();
803 ProjLib_ProjectedCurve Proj(HS,HC);
804 Handle(Geom2d_Curve) pc = Geom2dAdaptor::MakeCurve(Proj);
806 gp_Pln pln = GAS.Plane();
807 Standard_Real d2 = 0.;
808 Standard_Integer nn = 23;
809 Standard_Real unsurnn = 1./nn;
810 for(Standard_Integer i = 0; i <= nn; i++){
811 Standard_Real t = unsurnn*i;
812 Standard_Real u = First*(1.-t) + Last*t;
813 gp_Pnt Pc3d = HC->Value(u);
814 gp_Pnt2d p2d = pc->Value(u);
815 gp_Pnt Pcons = ElSLib::Value(p2d.X(),p2d.Y(),pln);
816 Standard_Real eps = Max(Pc3d.XYZ().SquareModulus(), Pcons.XYZ().SquareModulus());
818 Standard_Real temp = Pc3d.SquareDistance(Pcons);
823 if(temp > d2) d2 = temp;
829 //=======================================================================
830 //function : UpdTolMap
831 //purpose : Update map ShToTol (shape to tolerance)
832 //=======================================================================
833 static void UpdTolMap(const TopoDS_Shape& theSh, Standard_Real theNewTol,
834 TopTools_DataMapOfShapeReal& theShToTol)
836 TopAbs_ShapeEnum aSt = theSh.ShapeType();
837 Standard_Real aShTol;
838 if (aSt == TopAbs_VERTEX)
839 aShTol = BRep_Tool::Tolerance(TopoDS::Vertex(theSh));
840 else if (aSt == TopAbs_EDGE)
841 aShTol = BRep_Tool::Tolerance(TopoDS::Edge(theSh));
845 if (theNewTol > aShTol)
847 const Standard_Real* anOldtol = theShToTol.Seek(theSh);
849 theShToTol.Bind(theSh, theNewTol);
851 theShToTol(theSh) = Max(*anOldtol, theNewTol);
855 //=======================================================================
856 //function : UpdShTol
857 //purpose : Update vertices/edges/faces according to ShToTol map (create copies of necessary)
858 //=======================================================================
859 static void UpdShTol(const TopTools_DataMapOfShapeReal& theShToTol,
860 const Standard_Boolean IsMutableInput, BRepTools_ReShape& theReshaper,
861 Standard_Boolean theVForceUpdate)
864 TopTools_DataMapIteratorOfDataMapOfShapeReal SHToTolit(theShToTol);
865 for (;SHToTolit.More();SHToTolit.Next())
867 const TopoDS_Shape& aSh = SHToTolit.Key();
868 Standard_Real aTol = SHToTolit.Value();
871 const TopoDS_Shape& aVsh = theReshaper.Value(aSh);
872 Standard_Boolean UseOldSh = IsMutableInput || theReshaper.IsNewShape(aSh) || !aVsh.IsSame(aSh);
877 aNsh = aSh.EmptyCopied();
878 //add subshapes from the original shape
879 TopoDS_Iterator sit(aSh);
880 for (;sit.More();sit.Next())
881 aB.Add(aNsh, sit.Value());
883 aNsh.Free(aSh.Free());
884 aNsh.Checked(aSh.Checked());
885 aNsh.Orientable(aSh.Orientable());
886 aNsh.Closed(aSh.Closed());
887 aNsh.Infinite(aSh.Infinite());
888 aNsh.Convex(aSh.Convex());
892 switch (aSh.ShapeType())
896 aB.UpdateFace(TopoDS::Face(aNsh), aTol);
901 aB.UpdateEdge(TopoDS::Edge(aNsh), aTol);
906 const Handle(BRep_TVertex)& aTV = *((Handle(BRep_TVertex)*)&aNsh.TShape());
909 throw TopoDS_LockedShape("BRep_Builder::UpdateVertex");
912 aTV->Tolerance(aTol);
914 aTV->UpdateTolerance(aTol);
915 aTV->Modified(Standard_True);
923 theReshaper.Replace(aSh, aNsh);
927 //=======================================================================
928 //function : InternalSameParameter
930 //=======================================================================
931 static void InternalSameParameter(const TopoDS_Shape& theSh, BRepTools_ReShape& theReshaper,
932 const Standard_Real theTol, const Standard_Boolean IsForced, const Standard_Boolean IsMutableInput )
934 TopExp_Explorer ex(theSh,TopAbs_EDGE);
935 TopTools_MapOfShape Done;
937 TopTools_DataMapOfShapeReal aShToTol;
941 const TopoDS_Edge& aCE = TopoDS::Edge(ex.Current());
944 TopoDS_Edge aNE = TopoDS::Edge(theReshaper.Value(aCE));
945 Standard_Boolean UseOldEdge = IsMutableInput || theReshaper.IsNewShape(aCE) || !aNE.IsSame(aCE);
946 if (IsForced && (BRep_Tool::SameRange(aCE) || BRep_Tool::SameParameter(aCE)))
950 aNE = TopoDS::Edge(aCE.EmptyCopied());
951 TopoDS_Iterator sit(aCE);
952 for (;sit.More();sit.Next())
953 aB.Add(aNE, sit.Value());
954 theReshaper.Replace(aCE, aNE);
955 UseOldEdge = Standard_True;
957 aB.SameRange(aNE, Standard_False);
958 aB.SameParameter(aNE, Standard_False);
960 Standard_Real aNewTol = -1;
961 TopoDS_Edge aResEdge = BRepLib::SameParameter(aNE, theTol, aNewTol, UseOldEdge);
962 if (!UseOldEdge && !aResEdge.IsNull())
963 //NE have been empty-copied
964 theReshaper.Replace(aNE, aResEdge);
967 TopoDS_Vertex aV1, aV2;
968 TopExp::Vertices(aCE,aV1,aV2);
970 UpdTolMap(aV1, aNewTol, aShToTol);
972 UpdTolMap(aV2, aNewTol, aShToTol);
979 BRepAdaptor_Surface BS;
980 for(ex.Init(theSh,TopAbs_FACE); ex.More(); ex.Next()){
981 const TopoDS_Face& curface = TopoDS::Face(ex.Current());
982 if(!Done.Add(curface)) continue;
983 BS.Initialize(curface);
984 if(BS.GetType() != GeomAbs_Plane) continue;
986 for(ex2.Init(curface,TopAbs_EDGE); ex2.More(); ex2.Next()){
987 const TopoDS_Edge& E = TopoDS::Edge(ex2.Current());
988 TopoDS_Shape aNe = theReshaper.Value(E);
989 Standard_Real aNewEtol = -1;
990 GetEdgeTol(TopoDS::Edge(aNe), curface, aNewEtol);
991 if (aNewEtol >= 0) //not equal to -1
992 UpdTolMap(E, aNewEtol, aShToTol);
997 UpdShTol(aShToTol, IsMutableInput, theReshaper, Standard_False );
999 InternalUpdateTolerances(theSh, Standard_False, IsMutableInput, theReshaper );
1002 //================================================================
1003 //function : SameParameter
1004 //WARNING : New spec DUB LBO 9/9/97.
1005 // Recode in the edge the best tolerance found,
1006 // for vertex extremities it is required to find something else
1007 //================================================================
1008 void BRepLib::SameParameter(const TopoDS_Shape& S,
1009 const Standard_Real Tolerance,
1010 const Standard_Boolean forced)
1012 BRepTools_ReShape reshaper;
1013 InternalSameParameter( S, reshaper, Tolerance, forced, Standard_True);
1016 //=======================================================================
1017 //function : SameParameter
1019 //=======================================================================
1020 void BRepLib::SameParameter(const TopoDS_Shape& S, BRepTools_ReShape& theReshaper,
1021 const Standard_Real Tolerance, const Standard_Boolean forced )
1023 InternalSameParameter( S, theReshaper, Tolerance, forced, Standard_False);
1026 //=======================================================================
1027 //function : EvalTol
1029 //=======================================================================
1030 static Standard_Boolean EvalTol(const Handle(Geom2d_Curve)& pc,
1031 const Handle(Geom_Surface)& s,
1032 const GeomAdaptor_Curve& gac,
1033 const Standard_Real tol,
1034 Standard_Real& tolbail)
1036 Standard_Integer ok = 0;
1037 Standard_Real f = gac.FirstParameter();
1038 Standard_Real l = gac.LastParameter();
1039 Extrema_LocateExtPC Projector;
1040 Projector.Initialize(gac,f,l,tol);
1044 for(Standard_Integer i = 1; i <= 5; i++){
1045 Standard_Real t = i/6.;
1046 t = (1.-t) * f + t * l;
1047 pc->Value(t).Coord(u,v);
1049 Projector.Perform(p,t);
1050 if (Projector.IsDone()) {
1051 Standard_Real dist2 = Projector.SquareDistance();
1052 if(dist2 > tolbail * tolbail) tolbail = sqrt (dist2);
1059 //=======================================================================
1060 //function : ComputeTol
1062 //=======================================================================
1063 static Standard_Real ComputeTol(const Handle(Adaptor3d_HCurve)& c3d,
1064 const Handle(Adaptor2d_HCurve2d)& c2d,
1065 const Handle(Adaptor3d_HSurface)& surf,
1066 const Standard_Integer nbp)
1070 TColStd_Array1OfReal dist(1,nbp+10);
1073 //Adaptor3d_CurveOnSurface cons(c2d,surf);
1074 Standard_Real uf = surf->FirstUParameter(), ul = surf->LastUParameter(),
1075 vf = surf->FirstVParameter(), vl = surf->LastVParameter();
1076 Standard_Real du = 0.01 * (ul - uf), dv = 0.01 * (vl - vf);
1077 Standard_Boolean isUPeriodic = surf->IsUPeriodic(), isVPeriodic = surf->IsVPeriodic();
1078 Standard_Real DSdu = 1./surf->UResolution(1.), DSdv = 1./surf->VResolution(1.);
1079 Standard_Real d2 = 0.;
1080 Standard_Real first = c3d->FirstParameter();
1081 Standard_Real last = c3d->LastParameter();
1082 Standard_Real dapp = -1.;
1083 for (Standard_Integer i = 0; i <= nbp; ++i)
1085 const Standard_Real t = IntToReal(i)/IntToReal(nbp);
1086 const Standard_Real u = first*(1.-t) + last*t;
1087 gp_Pnt Pc3d = c3d->Value(u);
1088 gp_Pnt2d Puv = c2d->Value(u);
1091 if(Puv.X() < uf - du)
1093 dapp = Max(dapp, DSdu * (uf - Puv.X()));
1096 else if(Puv.X() > ul + du)
1098 dapp = Max(dapp, DSdu * (Puv.X() - ul));
1104 if(Puv.Y() < vf - dv)
1106 dapp = Max(dapp, DSdv * (vf - Puv.Y()));
1109 else if(Puv.Y() > vl + dv)
1111 dapp = Max(dapp, DSdv * (Puv.Y() - vl));
1115 gp_Pnt Pcons = surf->Value(Puv.X(), Puv.Y());
1116 if (Precision::IsInfinite(Pcons.X()) ||
1117 Precision::IsInfinite(Pcons.Y()) ||
1118 Precision::IsInfinite(Pcons.Z()))
1120 d2 = Precision::Infinite();
1123 Standard_Real temp = Pc3d.SquareDistance(Pcons);
1127 d2 = Max (d2, temp);
1130 if(Precision::IsInfinite(d2))
1141 Standard_Boolean ana = Standard_False;
1142 Standard_Real D2 = 0;
1143 Standard_Integer N1 = 0;
1144 Standard_Integer N2 = 0;
1145 Standard_Integer N3 = 0;
1147 for (Standard_Integer i = 1; i<= nbp+10; ++i)
1162 if (N1 > N2 && N2 != 0)
1164 N3 = 100*N2/(N1+N2);
1166 if (N3 < 10 && N3 != 0)
1168 ana = Standard_True;
1169 for (Standard_Integer i = 1; i <= nbp+10; ++i)
1171 if (dist(i) > 0 && dist(i) < 1.0)
1173 D2 = Max (D2, dist(i));
1178 //d2 = 1.5*sqrt(d2);
1179 d2 = (!ana) ? 1.5 * d2 : 1.5*sqrt(D2);
1180 d2 = Max (d2, 1.e-7);
1184 //=======================================================================
1185 //function : GetCurve3d
1187 //=======================================================================
1188 static void GetCurve3d(const TopoDS_Edge& theEdge, Handle(Geom_Curve)& theC3d, Standard_Real& theF3d,
1189 Standard_Real& theL3d, TopLoc_Location& theLoc3d, BRep_ListOfCurveRepresentation& theCList)
1191 const Handle(BRep_TEdge)& aTE = *((Handle(BRep_TEdge)*) &theEdge.TShape());
1192 theCList = aTE->ChangeCurves(); // current function (i.e. GetCurve3d()) will not change any of this curves
1193 BRep_ListIteratorOfListOfCurveRepresentation anIt(theCList);
1194 Standard_Boolean NotDone = Standard_True;
1195 while (NotDone && anIt.More()) {
1196 Handle(BRep_GCurve) GCurve = Handle(BRep_GCurve)::DownCast(anIt.Value());
1197 if (!GCurve.IsNull() && GCurve->IsCurve3D()) {
1198 theC3d = GCurve->Curve3D() ;
1199 theF3d = GCurve->First();
1200 theL3d = GCurve->Last();
1201 theLoc3d = GCurve->Location() ;
1202 NotDone = Standard_False;
1208 //=======================================================================
1209 //function : UpdateVTol
1211 //=======================================================================
1212 void UpdateVTol(const TopoDS_Vertex theV1, const TopoDS_Vertex& theV2, Standard_Real theTol)
1215 if (!theV1.IsNull())
1216 aB.UpdateVertex(theV1,theTol);
1217 if (!theV2.IsNull())
1218 aB.UpdateVertex(theV2,theTol);
1221 //=======================================================================
1222 //function : SameParameter
1224 //=======================================================================
1225 void BRepLib::SameParameter(const TopoDS_Edge& theEdge,
1226 const Standard_Real theTolerance)
1228 Standard_Real aNewTol = -1;
1229 SameParameter(theEdge, theTolerance, aNewTol, Standard_True);
1232 TopoDS_Vertex aV1, aV2;
1233 TopExp::Vertices(theEdge,aV1,aV2);
1234 UpdateVTol(aV1, aV2, aNewTol);
1238 //=======================================================================
1239 //function : SameParameter
1241 //=======================================================================
1242 TopoDS_Edge BRepLib::SameParameter(const TopoDS_Edge& theEdge,
1243 const Standard_Real theTolerance, Standard_Real& theNewTol, Standard_Boolean IsUseOldEdge)
1245 if (BRep_Tool::SameParameter(theEdge))
1246 return TopoDS_Edge();
1247 Standard_Real f3d =0.,l3d =0.;
1248 TopLoc_Location L3d;
1249 Handle(Geom_Curve) C3d;
1250 BRep_ListOfCurveRepresentation CList;
1251 GetCurve3d(theEdge, C3d, f3d, l3d, L3d, CList);
1253 return TopoDS_Edge();
1257 Handle(BRep_TEdge) aNTE;
1261 aNTE = *((Handle(BRep_TEdge)*) &theEdge.TShape());
1265 aNE = TopoDS::Edge(theEdge.EmptyCopied()); //will be modified a little bit later, so copy anyway
1266 GetCurve3d(aNE, C3d, f3d, l3d, L3d, CList); //C3d pointer and CList will be differ after copying
1267 aNTE = *((Handle(BRep_TEdge)*) &aNE.TShape());
1268 TopoDS_Iterator sit(theEdge);
1269 for (;sit.More();sit.Next()) //add vertices from old edge to the new ones
1270 B.Add(aNE, sit.Value());
1273 BRep_ListIteratorOfListOfCurveRepresentation It(CList);
1275 const Standard_Integer NCONTROL = 22;
1277 Handle(GeomAdaptor_HCurve) HC = new GeomAdaptor_HCurve();
1278 Handle(Geom2dAdaptor_HCurve) HC2d = new Geom2dAdaptor_HCurve();
1279 Handle(GeomAdaptor_HSurface) HS = new GeomAdaptor_HSurface();
1280 GeomAdaptor_Curve& GAC = HC->ChangeCurve();
1281 Geom2dAdaptor_Curve& GAC2d = HC2d->ChangeCurve2d();
1282 GeomAdaptor_Surface& GAS = HS->ChangeSurface();
1284 // modified by NIZHNY-OCC486 Tue Aug 27 17:15:13 2002 :
1285 Standard_Boolean m_TrimmedPeriodical = Standard_False;
1286 Handle(Standard_Type) TheType = C3d->DynamicType();
1287 if( TheType == STANDARD_TYPE(Geom_TrimmedCurve))
1289 Handle(Geom_Curve) gtC (Handle(Geom_TrimmedCurve)::DownCast (C3d)->BasisCurve());
1290 m_TrimmedPeriodical = gtC->IsPeriodic();
1292 // modified by NIZHNY-OCC486 Tue Aug 27 17:15:17 2002 .
1295 if(!C3d->IsPeriodic()) {
1296 Standard_Real Udeb = C3d->FirstParameter();
1297 Standard_Real Ufin = C3d->LastParameter();
1298 // modified by NIZHNY-OCC486 Tue Aug 27 17:17:14 2002 :
1299 //if (Udeb > f3d) f3d = Udeb;
1300 //if (l3d > Ufin) l3d = Ufin;
1301 if(!m_TrimmedPeriodical)
1303 if (Udeb > f3d) f3d = Udeb;
1304 if (l3d > Ufin) l3d = Ufin;
1306 // modified by NIZHNY-OCC486 Tue Aug 27 17:17:55 2002 .
1308 if(!L3d.IsIdentity()){
1309 C3d = Handle(Geom_Curve)::DownCast(C3d->Transformed(L3d.Transformation()));
1311 GAC.Load(C3d,f3d,l3d);
1313 Standard_Boolean IsSameP = 1;
1314 Standard_Real maxdist = 0.;
1316 // Modified by skv - Thu Jun 3 12:39:19 2004 OCC5898 Begin
1317 Standard_Real anEdgeTol = BRep_Tool::Tolerance(aNE);
1318 // Modified by skv - Thu Jun 3 12:39:20 2004 OCC5898 End
1319 Standard_Boolean SameRange = BRep_Tool::SameRange(aNE);
1320 Standard_Boolean YaPCu = Standard_False;
1321 const Standard_Real BigError = 1.e10;
1322 It.Initialize(CList);
1326 Standard_Boolean isANA = Standard_False;
1327 Standard_Boolean isBSP = Standard_False;
1328 Handle(BRep_GCurve) GCurve = Handle(BRep_GCurve)::DownCast(It.Value());
1329 Handle(Geom2d_Curve) PC[2];
1330 Handle(Geom_Surface) S;
1331 if (!GCurve.IsNull() && GCurve->IsCurveOnSurface()) {
1332 YaPCu = Standard_True;
1333 PC[0] = GCurve->PCurve();
1334 TopLoc_Location PCLoc = GCurve->Location();
1335 S = GCurve->Surface();
1336 if (!PCLoc.IsIdentity() ) {
1337 S = Handle(Geom_Surface)::DownCast(S->Transformed(PCLoc.Transformation()));
1341 if (GCurve->IsCurveOnClosedSurface()) {
1342 PC[1] = GCurve->PCurve2();
1345 // Eval tol2d to compute SameRange
1346 Standard_Real UResol = Max(GAS.UResolution(theTolerance), Precision::PConfusion());
1347 Standard_Real VResol = Max(GAS.VResolution(theTolerance), Precision::PConfusion());
1348 Standard_Real Tol2d = Min(UResol, VResol);
1349 for(Standard_Integer i = 0; i < 2; i++){
1350 Handle(Geom2d_Curve) curPC = PC[i];
1351 Standard_Boolean updatepc = 0;
1352 if(curPC.IsNull()) break;
1354 GeomLib::SameRange(Tol2d,
1355 PC[i],GCurve->First(),GCurve->Last(),
1358 updatepc = (curPC != PC[i]);
1361 Standard_Boolean goodpc = 1;
1362 GAC2d.Load(curPC,f3d,l3d);
1364 Standard_Real error = ComputeTol(HC, HC2d, HS, NCONTROL);
1366 if(error > BigError)
1372 if(GAC2d.GetType() == GeomAbs_BSplineCurve &&
1373 GAC2d.Continuity() == GeomAbs_C0) {
1374 Handle(Geom2d_BSplineCurve) bs2d = GAC2d.BSpline();
1375 Handle(Geom2d_BSplineCurve) bs2dsov = bs2d;
1376 Standard_Real fC0 = bs2d->FirstParameter(), lC0 = bs2d->LastParameter();
1377 Standard_Boolean repar = Standard_True;
1378 gp_Pnt2d OriginPoint;
1379 bs2d->D0(fC0, OriginPoint);
1380 Geom2dConvert::C0BSplineToC1BSplineCurve(bs2d, Tol2d);
1381 isBSP = Standard_True;
1383 if(bs2d->IsPeriodic()) { // -------- IFV, Jan 2000
1384 gp_Pnt2d NewOriginPoint;
1385 bs2d->D0(bs2d->FirstParameter(), NewOriginPoint);
1386 if(Abs(OriginPoint.X() - NewOriginPoint.X()) > Precision::PConfusion() ||
1387 Abs(OriginPoint.Y() - NewOriginPoint.Y()) > Precision::PConfusion() ) {
1389 TColStd_Array1OfReal Knotbs2d (1, bs2d->NbKnots());
1390 bs2d->Knots(Knotbs2d);
1392 for(Standard_Integer Index = 1; Index <= bs2d->NbKnots(); Index++) {
1393 bs2d->D0(Knotbs2d(Index), NewOriginPoint);
1394 if(Abs(OriginPoint.X() - NewOriginPoint.X()) > Precision::PConfusion() ||
1395 Abs(OriginPoint.Y() - NewOriginPoint.Y()) > Precision::PConfusion() ) continue;
1397 bs2d->SetOrigin(Index);
1403 if(bs2d->Continuity() == GeomAbs_C0) {
1404 Standard_Real tolbail;
1405 if(EvalTol(curPC,S,GAC,theTolerance,tolbail)){
1407 Standard_Real UResbail = GAS.UResolution(tolbail);
1408 Standard_Real VResbail = GAS.VResolution(tolbail);
1409 Standard_Real Tol2dbail = Min(UResbail,VResbail);
1410 bs2d->D0(bs2d->FirstParameter(), OriginPoint);
1412 Standard_Integer nbp = bs2d->NbPoles();
1413 TColgp_Array1OfPnt2d poles(1,nbp);
1415 gp_Pnt2d p = poles(1), p1;
1416 Standard_Real d = Precision::Infinite();
1417 for(Standard_Integer ip = 2; ip <= nbp; ip++) {
1419 d = Min(d,p.SquareDistance(p1));
1424 Tol2dbail = Max(Min(Tol2dbail,d),Tol2d);
1426 Geom2dConvert::C0BSplineToC1BSplineCurve(bs2d,Tol2dbail);
1428 if(bs2d->IsPeriodic()) { // -------- IFV, Jan 2000
1429 gp_Pnt2d NewOriginPoint;
1430 bs2d->D0(bs2d->FirstParameter(), NewOriginPoint);
1431 if(Abs(OriginPoint.X() - NewOriginPoint.X()) > Precision::PConfusion() ||
1432 Abs(OriginPoint.Y() - NewOriginPoint.Y()) > Precision::PConfusion() ) {
1434 TColStd_Array1OfReal Knotbs2d (1, bs2d->NbKnots());
1435 bs2d->Knots(Knotbs2d);
1437 for(Standard_Integer Index = 1; Index <= bs2d->NbKnots(); Index++) {
1438 bs2d->D0(Knotbs2d(Index), NewOriginPoint);
1439 if(Abs(OriginPoint.X() - NewOriginPoint.X()) > Precision::PConfusion() ||
1440 Abs(OriginPoint.Y() - NewOriginPoint.Y()) > Precision::PConfusion() ) continue;
1442 bs2d->SetOrigin(Index);
1449 if(bs2d->Continuity() == GeomAbs_C0) {
1452 repar = Standard_False;
1460 Standard_Integer NbKnots = bs2d->NbKnots();
1461 TColStd_Array1OfReal Knots(1,NbKnots);
1463 // BSplCLib::Reparametrize(f3d,l3d,Knots);
1464 BSplCLib::Reparametrize(fC0,lC0,Knots);
1465 bs2d->SetKnots(Knots);
1466 GAC2d.Load(bs2d,f3d,l3d);
1468 Standard_Boolean updatepcsov = updatepc;
1469 updatepc = Standard_True;
1471 Standard_Real error1 = ComputeTol(HC, HC2d, HS, NCONTROL);
1472 if(error1 > error) {
1474 GAC2d.Load(bs2d,f3d,l3d);
1476 updatepc = updatepcsov;
1477 isANA = Standard_True;
1484 //check, if new BSpline "good" or not --------- IFV, Jan of 2000
1485 GeomAbs_Shape cont = bs2d->Continuity();
1486 Standard_Boolean IsBad = Standard_False;
1488 if(cont > GeomAbs_C0 && error > Max(1.e-3,theTolerance)) {
1489 Standard_Integer NbKnots = bs2d->NbKnots();
1490 TColStd_Array1OfReal Knots(1,NbKnots);
1492 Standard_Real critratio = 10.;
1493 Standard_Real dtprev = Knots(2) - Knots(1), dtratio = 1.;
1494 Standard_Real dtmin = dtprev;
1495 Standard_Real dtcur;
1496 for(Standard_Integer j = 2; j < NbKnots; j++) {
1497 dtcur = Knots(j+1) - Knots(j);
1498 dtmin = Min(dtmin, dtcur);
1502 if(dtcur > dtprev) dtratio = dtcur/dtprev;
1503 else dtratio = dtprev/dtcur;
1504 if(dtratio > critratio) {IsBad = Standard_True;}
1509 // To avoid failures in Approx_CurvilinearParameter
1510 bs2d->Resolution(Max(1.e-3,theTolerance), dtcur);
1511 if(dtmin < dtcur) IsBad = Standard_False;
1516 if(IsBad ) { //if BSpline "bad", try to reparametrize it
1517 // by its curve length
1519 // GeomAbs_Shape cont = bs2d->Continuity();
1520 if(cont > GeomAbs_C2) cont = GeomAbs_C2;
1521 Standard_Integer maxdeg = bs2d->Degree();
1522 if(maxdeg == 1) maxdeg = 14;
1523 Approx_CurvilinearParameter AppCurPar(HC2d, HS, Max(1.e-3,theTolerance),
1525 if(AppCurPar.IsDone() || AppCurPar.HasResult()) {
1526 bs2d = AppCurPar.Curve2d1();
1527 GAC2d.Load(bs2d,f3d,l3d);
1530 if(Abs(bs2d->FirstParameter() - fC0) > Tol2d ||
1531 Abs(bs2d->LastParameter() - lC0) > Tol2d ) {
1532 Standard_Integer NbKnots = bs2d->NbKnots();
1533 TColStd_Array1OfReal Knots(1,NbKnots);
1535 // BSplCLib::Reparametrize(f3d,l3d,Knots);
1536 BSplCLib::Reparametrize(fC0,lC0,Knots);
1537 bs2d->SetKnots(Knots);
1538 GAC2d.Load(bs2d,f3d,l3d);
1551 // Approx_SameParameter SameP(HC,HC2d,HS,Tolerance);
1552 Standard_Real aTol = (isANA && isBSP) ? 1.e-7 : theTolerance;
1553 const Handle(Adaptor3d_HCurve)& aHCurv = HC; // to avoid ambiguity
1554 const Handle(Adaptor2d_HCurve2d)& aHCurv2d = HC2d; // to avoid ambiguity
1555 Approx_SameParameter SameP(aHCurv,aHCurv2d,HS,aTol);
1557 if (SameP.IsSameParameter()) {
1558 maxdist = Max(maxdist,SameP.TolReached());
1560 if (i == 0) GCurve->PCurve(curPC);
1561 else GCurve->PCurve2(curPC);
1564 else if (SameP.IsDone()) {
1565 Standard_Real tolreached = SameP.TolReached();
1566 if(tolreached <= error) {
1567 curPC = SameP.Curve2d();
1568 updatepc = Standard_True;
1569 maxdist = Max(maxdist,tolreached);
1572 maxdist = Max(maxdist, error);
1575 if (i == 0) GCurve->PCurve(curPC);
1576 else GCurve->PCurve2(curPC);
1581 //Approx_SameParameter has failed.
1582 //Consequently, the situation might be,
1583 //when 3D and 2D-curve do not have same-range.
1584 GeomLib::SameRange( Tol2d, PC[i],
1585 GCurve->First(), GCurve->Last(),
1588 if (i == 0) GCurve->PCurve(curPC);
1589 else GCurve->PCurve2(curPC);
1597 // Modified by skv - Thu Jun 3 12:39:19 2004 OCC5898 Begin
1599 if (anEdgeTol >= error) {
1600 maxdist = Max(maxdist, anEdgeTol);
1601 IsSameP = Standard_True;
1604 // Modified by skv - Thu Jun 3 12:39:20 2004 OCC5898 End
1609 B.Range(aNE,f3d,l3d);
1610 B.SameRange(aNE,Standard_True);
1612 // Reduce eventually the tolerance of the edge, as
1613 // all its representations are processed (except for some associated
1614 // to planes and not stored in the edge !)
1615 // The same cannot be done with vertices that cannot be enlarged
1618 // Avoid setting too small tolerances.
1619 maxdist = Max(maxdist,Precision::Confusion());
1620 theNewTol = maxdist;
1621 aNTE->Modified(Standard_True);
1622 aNTE->Tolerance(maxdist);
1624 B.SameParameter(aNE,Standard_True);
1630 //=======================================================================
1631 //function : InternalUpdateTolerances
1633 //=======================================================================
1634 static void InternalUpdateTolerances(const TopoDS_Shape& theOldShape,
1635 const Standard_Boolean IsVerifyTolerance, const Standard_Boolean IsMutableInput, BRepTools_ReShape& theReshaper)
1637 TopTools_DataMapOfShapeReal aShToTol;
1638 // Harmonize tolerances
1639 // with rule Tolerance(VERTEX)>=Tolerance(EDGE)>=Tolerance(FACE)
1640 Standard_Real tol=0;
1641 if (IsVerifyTolerance) {
1642 // Set tolerance to its minimum value
1643 Handle(Geom_Surface) S;
1647 Standard_Real aXmin, aYmin, aZmin, aXmax, aYmax, aZmax, dMax;
1648 for (ex.Init(theOldShape, TopAbs_FACE); ex.More(); ex.Next()) {
1649 const TopoDS_Face& curf=TopoDS::Face(ex.Current());
1650 S = BRep_Tool::Surface(curf, l);
1653 BRepBndLib::Add(curf,aB);
1654 if (S->DynamicType() == STANDARD_TYPE(Geom_RectangularTrimmedSurface)) {
1655 S = Handle(Geom_RectangularTrimmedSurface)::DownCast (S)->BasisSurface();
1657 GeomAdaptor_Surface AS(S);
1658 switch (AS.GetType()) {
1660 case GeomAbs_Cylinder:
1663 tol=Precision::Confusion();
1666 case GeomAbs_Sphere:
1669 tol=Precision::Confusion()*2;
1673 tol=Precision::Confusion()*4;
1675 if (!aB.IsWhole()) {
1676 aB.Get(aXmin, aYmin, aZmin, aXmax, aYmax, aZmax);
1678 if (!aB.IsOpenXmin() && !aB.IsOpenXmax()) dMax=aXmax-aXmin;
1679 if (!aB.IsOpenYmin() && !aB.IsOpenYmax()) aYmin=aYmax-aYmin;
1680 if (!aB.IsOpenZmin() && !aB.IsOpenZmax()) aZmin=aZmax-aZmin;
1681 if (aYmin>dMax) dMax=aYmin;
1682 if (aZmin>dMax) dMax=aZmin;
1684 // Do not process tolerances > 1.
1685 if (tol>1.) tol=0.99;
1687 aShToTol.Bind(curf, tol);
1693 TopTools_IndexedDataMapOfShapeListOfShape parents;
1694 TopExp::MapShapesAndAncestors(theOldShape, TopAbs_EDGE, TopAbs_FACE, parents);
1695 TopTools_ListIteratorOfListOfShape lConx;
1696 Standard_Integer iCur;
1697 for (iCur=1; iCur<=parents.Extent(); iCur++) {
1699 for (lConx.Initialize(parents(iCur)); lConx.More(); lConx.Next())
1701 const TopoDS_Face& FF = TopoDS::Face(lConx.Value());
1703 if (IsVerifyTolerance && aShToTol.IsBound(FF)) //first condition for speed-up
1704 Ftol = aShToTol(FF);
1706 Ftol = BRep_Tool::Tolerance(FF); //tolerance have not been updated
1709 // Update can only increase tolerance, so if the edge has a greater
1710 // tolerance than its faces it is not concerned
1711 const TopoDS_Edge& EK = TopoDS::Edge(parents.FindKey(iCur));
1712 if (tol > BRep_Tool::Tolerance(EK))
1713 aShToTol.Bind(EK, tol);
1716 //Vertices are processed
1717 const Standard_Real BigTol = 1.e10;
1720 TopExp::MapShapesAndUniqueAncestors(theOldShape, TopAbs_VERTEX, TopAbs_EDGE, parents);
1721 TColStd_MapOfTransient Initialized;
1722 Standard_Integer nbV = parents.Extent();
1723 for (iCur=1; iCur<=nbV; iCur++) {
1725 const TopoDS_Vertex& V = TopoDS::Vertex(parents.FindKey(iCur));
1727 box.Add(BRep_Tool::Pnt(V));
1729 for (lConx.Initialize(parents(iCur)); lConx.More(); lConx.Next()) {
1730 const TopoDS_Edge& E = TopoDS::Edge(lConx.Value());
1731 const Standard_Real* aNtol = aShToTol.Seek(E);
1732 tol=Max(tol, aNtol ? *aNtol : BRep_Tool::Tolerance(E));
1733 if(tol > BigTol) continue;
1734 if(!BRep_Tool::SameRange(E)) continue;
1735 Standard_Real par = BRep_Tool::Parameter(V,E);
1736 Handle(BRep_TEdge)& TE = *((Handle(BRep_TEdge)*)&E.TShape());
1737 BRep_ListIteratorOfListOfCurveRepresentation itcr(TE->Curves());
1738 const TopLoc_Location& Eloc = E.Location();
1739 while (itcr.More()) {
1740 // For each CurveRepresentation, check the provided parameter
1741 const Handle(BRep_CurveRepresentation)& cr = itcr.Value();
1742 const TopLoc_Location& loc = cr->Location();
1743 TopLoc_Location L = (Eloc * loc);
1744 if (cr->IsCurve3D()) {
1745 const Handle(Geom_Curve)& C = cr->Curve3D();
1746 if (!C.IsNull()) { // edge non degenerated
1747 p3d = C->Value(par);
1748 p3d.Transform(L.Transformation());
1752 else if (cr->IsCurveOnSurface()) {
1753 const Handle(Geom_Surface)& Su = cr->Surface();
1754 const Handle(Geom2d_Curve)& PC = cr->PCurve();
1755 Handle(Geom2d_Curve) PC2;
1756 if (cr->IsCurveOnClosedSurface()) {
1757 PC2 = cr->PCurve2();
1759 gp_Pnt2d p2d = PC->Value(par);
1760 p3d = Su->Value(p2d.X(),p2d.Y());
1761 p3d.Transform(L.Transformation());
1763 if (!PC2.IsNull()) {
1764 p2d = PC2->Value(par);
1765 p3d = Su->Value(p2d.X(),p2d.Y());
1766 p3d.Transform(L.Transformation());
1773 Standard_Real aXmin, aYmin, aZmin, aXmax, aYmax, aZmax;
1774 box.Get(aXmin, aYmin, aZmin, aXmax, aYmax, aZmax);
1775 aXmax -= aXmin; aYmax -= aYmin; aZmax -= aZmin;
1776 tol = Max(tol,sqrt(aXmax*aXmax+aYmax*aYmax+aZmax*aZmax));
1777 tol += 2.*Epsilon(tol);
1779 Standard_Real aVTol = BRep_Tool::Tolerance(V);
1780 Standard_Boolean anUpdTol = tol > aVTol;
1781 const Handle(BRep_TVertex)& aTV = *((Handle(BRep_TVertex)*)&V.TShape());
1782 Standard_Boolean toAdd = Standard_False;
1783 if (IsVerifyTolerance)
1785 // ASet minimum value of the tolerance
1786 // Attention to sharing of the vertex by other shapes
1787 toAdd = Initialized.Add(aTV) && aVTol != tol; //if Vtol == tol => no need to update toler
1789 //'Initialized' map is not used anywhere outside this block
1790 if (anUpdTol || toAdd)
1791 aShToTol.Bind(V, tol);
1794 UpdShTol(aShToTol, IsMutableInput, theReshaper, Standard_True);
1797 //=======================================================================
1798 //function : UpdateTolerances
1800 //=======================================================================
1801 void BRepLib::UpdateTolerances (const TopoDS_Shape& S, const Standard_Boolean verifyFaceTolerance)
1803 BRepTools_ReShape aReshaper;
1804 InternalUpdateTolerances(S, verifyFaceTolerance, Standard_True, aReshaper);
1807 //=======================================================================
1808 //function : UpdateTolerances
1810 //=======================================================================
1811 void BRepLib::UpdateTolerances (const TopoDS_Shape& S, BRepTools_ReShape& theReshaper, const Standard_Boolean verifyFaceTolerance )
1813 InternalUpdateTolerances(S, verifyFaceTolerance, Standard_False, theReshaper);
1816 //=======================================================================
1817 //function : UpdateInnerTolerances
1819 //=======================================================================
1820 void BRepLib::UpdateInnerTolerances(const TopoDS_Shape& aShape)
1822 TopTools_IndexedDataMapOfShapeListOfShape EFmap;
1823 TopExp::MapShapesAndAncestors(aShape, TopAbs_EDGE, TopAbs_FACE, EFmap);
1825 for (Standard_Integer i = 1; i <= EFmap.Extent(); i++)
1827 TopoDS_Edge anEdge = TopoDS::Edge(EFmap.FindKey(i));
1828 TopoDS_Vertex V1, V2;
1829 TopExp::Vertices(anEdge, V1, V2);
1830 Standard_Real fpar, lpar;
1831 BRep_Tool::Range(anEdge, fpar, lpar);
1832 Standard_Real TolEdge = BRep_Tool::Tolerance(anEdge);
1834 Handle(BRepAdaptor_HCurve) anHCurve = new BRepAdaptor_HCurve();
1835 anHCurve->ChangeCurve().Initialize(anEdge);
1837 Pnt1 = BRep_Tool::Pnt(V1);
1839 Pnt2 = BRep_Tool::Pnt(V2);
1841 if (!BRep_Tool::Degenerated(anEdge) &&
1842 EFmap(i).Extent() > 0)
1844 NCollection_Sequence<Handle(Adaptor3d_HCurve)> theRep;
1845 theRep.Append(anHCurve);
1846 TopTools_ListIteratorOfListOfShape itl(EFmap(i));
1847 for (; itl.More(); itl.Next())
1849 const TopoDS_Face& aFace = TopoDS::Face(itl.Value());
1850 Handle(BRepAdaptor_HCurve) anHCurvOnSurf = new BRepAdaptor_HCurve();
1851 anHCurvOnSurf->ChangeCurve().Initialize(anEdge, aFace);
1852 theRep.Append(anHCurvOnSurf);
1855 const Standard_Integer NbSamples = (BRep_Tool::SameParameter(anEdge))? 23 : 2;
1856 Standard_Real delta = (lpar - fpar)/(NbSamples-1);
1857 Standard_Real MaxDist = 0.;
1858 for (Standard_Integer j = 2; j <= theRep.Length(); j++)
1860 for (Standard_Integer k = 0; k <= NbSamples; k++)
1862 Standard_Real ParamOnCenter = (k == NbSamples)? lpar :
1864 gp_Pnt Center = theRep(1)->Value(ParamOnCenter);
1865 Standard_Real ParamOnCurve = (BRep_Tool::SameParameter(anEdge))? ParamOnCenter
1866 : ((k == 0)? theRep(j)->FirstParameter() : theRep(j)->LastParameter());
1867 gp_Pnt aPoint = theRep(j)->Value(ParamOnCurve);
1868 Standard_Real aDist = Center.Distance(aPoint);
1870 aDist += 2.*Epsilon(aDist);
1871 if (aDist > MaxDist)
1874 //Update tolerances of vertices
1875 if (k == 0 && !V1.IsNull())
1877 Standard_Real aDist1 = Pnt1.Distance(aPoint);
1878 aDist1 += 2.*Epsilon(aDist1);
1879 BB.UpdateVertex(V1, aDist1);
1881 if (k == NbSamples && !V2.IsNull())
1883 Standard_Real aDist2 = Pnt2.Distance(aPoint);
1884 aDist2 += 2.*Epsilon(aDist2);
1885 BB.UpdateVertex(V2, aDist2);
1889 BB.UpdateEdge(anEdge, MaxDist);
1891 TolEdge = BRep_Tool::Tolerance(anEdge);
1894 gp_Pnt End1 = anHCurve->Value(fpar);
1895 Standard_Real dist1 = Pnt1.Distance(End1);
1896 dist1 += 2.*Epsilon(dist1);
1897 BB.UpdateVertex(V1, Max(dist1, TolEdge));
1901 gp_Pnt End2 = anHCurve->Value(lpar);
1902 Standard_Real dist2 = Pnt2.Distance(End2);
1903 dist2 += 2.*Epsilon(dist2);
1904 BB.UpdateVertex(V2, Max(dist2, TolEdge));
1909 //=======================================================================
1910 //function : OrientClosedSolid
1912 //=======================================================================
1913 Standard_Boolean BRepLib::OrientClosedSolid(TopoDS_Solid& solid)
1915 // Set material inside the solid
1916 BRepClass3d_SolidClassifier where(solid);
1917 where.PerformInfinitePoint(Precision::Confusion());
1918 if (where.State()==TopAbs_IN) {
1921 else if (where.State()==TopAbs_ON || where.State()==TopAbs_UNKNOWN)
1922 return Standard_False;
1924 return Standard_True;
1927 // Structure for calculation of properties, necessary for decision about continuity
1928 class SurfaceProperties
1931 SurfaceProperties(const Handle(Geom_Surface)& theSurface,
1932 const gp_Trsf& theSurfaceTrsf,
1933 const Handle(Geom2d_Curve)& theCurve2D,
1934 const Standard_Boolean theReversed)
1935 : mySurfaceProps(theSurface, 2, Precision::Confusion()),
1936 mySurfaceTrsf(theSurfaceTrsf),
1937 myCurve2d(theCurve2D),
1938 myIsReversed(theReversed)
1941 // Calculate derivatives on surface related to the point on curve
1942 void Calculate(const Standard_Real theParamOnCurve)
1945 myCurve2d->D1(theParamOnCurve, aUV, myCurveTangent);
1946 mySurfaceProps.SetParameters(aUV.X(), aUV.Y());
1949 // Returns point just calculated
1951 { return mySurfaceProps.Value().Transformed(mySurfaceTrsf); }
1953 // Calculate a derivative orthogonal to curve's tangent vector
1957 // direction orthogonal to tangent vector of the curve
1958 gp_Vec2d anOrtho(-myCurveTangent.Y(), myCurveTangent.X());
1959 Standard_Real aLen = anOrtho.Magnitude();
1960 if (aLen < Precision::Confusion())
1966 aDeriv.SetLinearForm(anOrtho.X(), mySurfaceProps.D1U(),
1967 anOrtho.Y(), mySurfaceProps.D1V());
1968 return aDeriv.Transformed(mySurfaceTrsf);
1971 // Calculate principal curvatures, which consist of minimal and maximal normal curvatures and
1972 // the directions on the tangent plane (principal direction) where the extremums are reached
1973 void Curvature(gp_Dir& thePrincipalDir1, Standard_Real& theCurvature1,
1974 gp_Dir& thePrincipalDir2, Standard_Real& theCurvature2)
1976 mySurfaceProps.CurvatureDirections(thePrincipalDir1, thePrincipalDir2);
1977 theCurvature1 = mySurfaceProps.MaxCurvature();
1978 theCurvature2 = mySurfaceProps.MinCurvature();
1981 theCurvature1 = -theCurvature1;
1982 theCurvature2 = -theCurvature2;
1984 thePrincipalDir1.Transform(mySurfaceTrsf);
1985 thePrincipalDir2.Transform(mySurfaceTrsf);
1989 GeomLProp_SLProps mySurfaceProps; // properties calculator
1990 gp_Trsf mySurfaceTrsf;
1991 Handle(Geom2d_Curve) myCurve2d;
1992 Standard_Boolean myIsReversed; // the face based on the surface is reversed
1994 // tangent vector to Pcurve in UV
1995 gp_Vec2d myCurveTangent;
1998 //=======================================================================
1999 //function : tgtfaces
2000 //purpose : check the angle at the border between two squares.
2001 // Two shares should have a shared front edge.
2002 //=======================================================================
2003 static GeomAbs_Shape tgtfaces(const TopoDS_Edge& Ed,
2004 const TopoDS_Face& F1,
2005 const TopoDS_Face& F2,
2006 const Standard_Real theAngleTol)
2008 Standard_Boolean isSeam = F1.IsEqual(F2);
2012 // Check if pcurves exist on both faces of edge
2013 Standard_Real aFirst,aLast;
2014 E.Orientation(TopAbs_FORWARD);
2015 Handle(Geom2d_Curve) aCurve1 = BRep_Tool::CurveOnSurface(E, F1, aFirst, aLast);
2016 if(aCurve1.IsNull())
2020 E.Orientation(TopAbs_REVERSED);
2021 Handle(Geom2d_Curve) aCurve2 = BRep_Tool::CurveOnSurface(E, F2, aFirst, aLast);
2022 if(aCurve2.IsNull())
2025 TopLoc_Location aLoc1, aLoc2;
2026 Handle(Geom_Surface) aSurface1 = BRep_Tool::Surface(F1, aLoc1);
2027 const gp_Trsf& aSurf1Trsf = aLoc1.Transformation();
2028 Handle(Geom_Surface) aSurface2 = BRep_Tool::Surface(F2, aLoc2);
2029 const gp_Trsf& aSurf2Trsf = aLoc2.Transformation();
2031 if (aSurface1->IsKind(STANDARD_TYPE(Geom_RectangularTrimmedSurface)))
2032 aSurface1 = Handle(Geom_RectangularTrimmedSurface)::DownCast(aSurface1)->BasisSurface();
2033 if (aSurface2->IsKind(STANDARD_TYPE(Geom_RectangularTrimmedSurface)))
2034 aSurface2 = Handle(Geom_RectangularTrimmedSurface)::DownCast(aSurface2)->BasisSurface();
2036 // seam edge on elementary surface is always CN
2037 Standard_Boolean isElementary =
2038 (aSurface1->IsKind(STANDARD_TYPE(Geom_ElementarySurface)) &&
2039 aSurface2->IsKind(STANDARD_TYPE(Geom_ElementarySurface)));
2040 if (isSeam && isElementary)
2045 SurfaceProperties aSP1(aSurface1, aSurf1Trsf, aCurve1, F1.Orientation() == TopAbs_REVERSED);
2046 SurfaceProperties aSP2(aSurface2, aSurf2Trsf, aCurve2, F2.Orientation() == TopAbs_REVERSED);
2048 Standard_Real f, l, eps;
2049 BRep_Tool::Range(E,f,l);
2050 Extrema_LocateExtPC ext;
2051 Handle(BRepAdaptor_HCurve) aHC2;
2054 f += eps; // to avoid calculations on
2055 l -= eps; // points of pointed squares.
2057 const Standard_Real anAngleTol2 = theAngleTol * theAngleTol;
2059 gp_Vec aDer1, aDer2;
2061 Standard_Real aSqLen1, aSqLen2;
2062 gp_Dir aCrvDir1[2], aCrvDir2[2];
2063 Standard_Real aCrvLen1[2], aCrvLen2[2];
2065 GeomAbs_Shape aCont = (isElementary ? GeomAbs_CN : GeomAbs_C2);
2066 GeomAbs_Shape aCurCont;
2068 for (Standard_Integer i = 0; i <= 20 && aCont > GeomAbs_C0; i++)
2070 // First suppose that this is sameParameter
2073 // Check conditions for G1 and C1 continuity:
2074 // * calculate a derivative in tangent plane of each surface
2075 // orthogonal to curve's tangent vector
2076 // * continuity is C1 if the vectors are equal
2077 // * continuity is G1 if the vectors are just parallel
2078 aCurCont = GeomAbs_C0;
2083 aDer1 = aSP1.Derivative();
2084 aSqLen1 = aDer1.SquareMagnitude();
2085 aDer2 = aSP2.Derivative();
2086 aSqLen2 = aDer2.SquareMagnitude();
2087 Standard_Boolean isSmoothSuspect = (aDer1.CrossSquareMagnitude(aDer2) <= anAngleTol2 * aSqLen1 * aSqLen2);
2088 if (!isSmoothSuspect)
2090 // Refine by projection
2093 // adaptor for pcurve on the second surface
2094 aHC2 = new BRepAdaptor_HCurve(BRepAdaptor_Curve(E, F2));
2095 ext.Initialize(aHC2->Curve(), f, l, Precision::PConfusion());
2097 ext.Perform(aSP1.Value(), u);
2098 if (ext.IsDone() && ext.IsMin())
2100 const Extrema_POnCurv& poc = ext.Point();
2101 aSP2.Calculate(poc.Parameter());
2102 aDer2 = aSP2.Derivative();
2103 aSqLen2 = aDer2.SquareMagnitude();
2105 isSmoothSuspect = (aDer1.CrossSquareMagnitude(aDer2) <= anAngleTol2 * aSqLen1 * aSqLen2);
2107 if (isSmoothSuspect)
2109 aCurCont = GeomAbs_G1;
2110 if (Abs(Sqrt(aSqLen1) - Sqrt(aSqLen2)) < Precision::Confusion() &&
2111 aDer1.Dot(aDer2) > Precision::SquareConfusion()) // <= check vectors are codirectional
2112 aCurCont = GeomAbs_C1;
2117 if (aCont < GeomAbs_G2)
2118 continue; // no need further processing, because maximal continuity is less than G2
2120 // Check conditions for G2 and C2 continuity:
2121 // * calculate principal curvatures on each surface
2122 // * continuity is C2 if directions of principal curvatures are equal on differenct surfaces
2123 // * continuity is G2 if directions of principal curvatures are just parallel
2124 // and values of curvatures are the same
2125 aSP1.Curvature(aCrvDir1[0], aCrvLen1[0], aCrvDir1[1], aCrvLen1[1]);
2126 aSP2.Curvature(aCrvDir2[0], aCrvLen2[0], aCrvDir2[1], aCrvLen2[1]);
2127 for (Standard_Integer aStep = 0; aStep <= 1; ++aStep)
2129 if (aCrvDir1[0].XYZ().CrossSquareMagnitude(aCrvDir2[aStep].XYZ()) <= Precision::SquareConfusion() &&
2130 Abs(aCrvLen1[0] - aCrvLen2[aStep]) < Precision::Confusion() &&
2131 aCrvDir1[1].XYZ().CrossSquareMagnitude(aCrvDir2[1 - aStep].XYZ()) <= Precision::SquareConfusion() &&
2132 Abs(aCrvLen1[1] - aCrvLen2[1 - aStep]) < Precision::Confusion())
2134 if (aCurCont == GeomAbs_C1 &&
2135 aCrvDir1[0].Dot(aCrvDir2[aStep]) > Precision::Confusion() &&
2136 aCrvDir1[1].Dot(aCrvDir2[1 - aStep]) > Precision::Confusion())
2137 aCurCont = GeomAbs_C2;
2139 aCurCont = GeomAbs_G2;
2144 if (aCurCont < aCont)
2148 // according to the list of supported elementary surfaces,
2149 // if the continuity is C2, than it is totally CN
2150 if (isElementary && aCont == GeomAbs_C2)
2155 //=======================================================================
2156 // function : EncodeRegularity
2157 // purpose : Code the regularities on all edges of the shape, boundary of
2158 // two faces that do not have it.
2159 // Takes into account that compound may consists of same solid
2160 // placed with different transformations
2161 //=======================================================================
2162 static void EncodeRegularity(const TopoDS_Shape& theShape,
2163 const Standard_Real theTolAng,
2164 TopTools_MapOfShape& theMap,
2165 const TopTools_MapOfShape& theEdgesToEncode = TopTools_MapOfShape())
2167 TopoDS_Shape aShape = theShape;
2168 TopLoc_Location aNullLoc;
2169 aShape.Location(aNullLoc); // nullify location
2170 if (!theMap.Add(aShape))
2171 return; // do not need to process shape twice
2173 if (aShape.ShapeType() == TopAbs_COMPOUND ||
2174 aShape.ShapeType() == TopAbs_COMPSOLID)
2176 for (TopoDS_Iterator it(aShape); it.More(); it.Next())
2177 EncodeRegularity(it.Value(), theTolAng, theMap, theEdgesToEncode);
2184 TopTools_IndexedDataMapOfShapeListOfShape M;
2185 TopExp::MapShapesAndAncestors(aShape, TopAbs_EDGE, TopAbs_FACE, M);
2186 TopTools_ListIteratorOfListOfShape It;
2189 Standard_Boolean found;
2190 for (Standard_Integer i = 1; i <= M.Extent(); i++){
2191 TopoDS_Edge E = TopoDS::Edge(M.FindKey(i));
2192 if (!theEdgesToEncode.IsEmpty())
2194 // process only the edges from the list to update their regularity
2195 TopoDS_Shape aPureEdge = E.Located(aNullLoc);
2196 aPureEdge.Orientation(TopAbs_FORWARD);
2197 if (!theEdgesToEncode.Contains(aPureEdge))
2201 found = Standard_False;
2203 for (It.Initialize(M.FindFromIndex(i)); It.More() && !found; It.Next()){
2204 if (F1.IsNull()) { F1 = TopoDS::Face(It.Value()); }
2206 const TopoDS_Face& aTmpF2 = TopoDS::Face(It.Value());
2207 if (!F1.IsSame(aTmpF2)){
2208 found = Standard_True;
2213 if (!found && !F1.IsNull()){//is it a sewing edge?
2214 TopAbs_Orientation orE = E.Orientation();
2216 for (Ex.Init(F1, TopAbs_EDGE); Ex.More() && !found; Ex.Next()){
2217 curE = TopoDS::Edge(Ex.Current());
2218 if (E.IsSame(curE) && orE != curE.Orientation()) {
2219 found = Standard_True;
2225 BRepLib::EncodeRegularity(E, F1, F2, theTolAng);
2228 catch (Standard_Failure const& anException) {
2230 cout << "Warning: Exception in BRepLib::EncodeRegularity(): ";
2231 anException.Print(cout);
2238 //=======================================================================
2239 // function : EncodeRegularity
2240 // purpose : code the regularities on all edges of the shape, boundary of
2241 // two faces that do not have it.
2242 //=======================================================================
2244 void BRepLib::EncodeRegularity(const TopoDS_Shape& S,
2245 const Standard_Real TolAng)
2247 TopTools_MapOfShape aMap;
2248 ::EncodeRegularity(S, TolAng, aMap);
2251 //=======================================================================
2252 // function : EncodeRegularity
2253 // purpose : code the regularities on all edges in the list that do not
2254 // have it, and which are boundary of two faces on the shape.
2255 //=======================================================================
2257 void BRepLib::EncodeRegularity(const TopoDS_Shape& S,
2258 const TopTools_ListOfShape& LE,
2259 const Standard_Real TolAng)
2261 // Collect edges without location and orientation
2262 TopTools_MapOfShape aPureEdges;
2263 TopLoc_Location aNullLoc;
2264 TopTools_ListIteratorOfListOfShape anEdgeIt(LE);
2265 for (; anEdgeIt.More(); anEdgeIt.Next())
2267 TopoDS_Shape anEdge = anEdgeIt.Value();
2268 anEdge.Location(aNullLoc);
2269 anEdge.Orientation(TopAbs_FORWARD);
2270 aPureEdges.Add(anEdge);
2273 TopTools_MapOfShape aMap;
2274 ::EncodeRegularity(S, TolAng, aMap, aPureEdges);
2277 //=======================================================================
2278 // function : EncodeRegularity
2279 // purpose : code the regularity between 2 faces connected by edge
2280 //=======================================================================
2282 void BRepLib::EncodeRegularity(TopoDS_Edge& E,
2283 const TopoDS_Face& F1,
2284 const TopoDS_Face& F2,
2285 const Standard_Real TolAng)
2288 if(BRep_Tool::Continuity(E,F1,F2)<=GeomAbs_C0){
2290 GeomAbs_Shape aCont = tgtfaces(E, F1, F2, TolAng);
2291 B.Continuity(E,F1,F2,aCont);
2294 catch(Standard_Failure)
2297 cout << "Failure: Exception in BRepLib::EncodeRegularity" << endl;
2303 //=======================================================================
2304 // function : EnsureNormalConsistency
2305 // purpose : Corrects the normals in Poly_Triangulation of faces.
2306 // Returns TRUE if any correction is done.
2307 //=======================================================================
2308 Standard_Boolean BRepLib::
2309 EnsureNormalConsistency(const TopoDS_Shape& theShape,
2310 const Standard_Real theAngTol,
2311 const Standard_Boolean theForceComputeNormals)
2313 const Standard_Real aThresDot = cos(theAngTol);
2315 Standard_Boolean aRetVal = Standard_False, isNormalsFound = Standard_False;
2317 // compute normals if they are absent
2318 TopExp_Explorer anExpFace(theShape,TopAbs_FACE);
2319 for (; anExpFace.More(); anExpFace.Next())
2321 const TopoDS_Face& aFace = TopoDS::Face(anExpFace.Current());
2322 const Handle(Geom_Surface) aSurf = BRep_Tool::Surface(aFace);
2325 TopLoc_Location aLoc;
2326 const Handle(Poly_Triangulation)& aPT = BRep_Tool::Triangulation(aFace, aLoc);
2329 if (!theForceComputeNormals && aPT->HasNormals())
2331 isNormalsFound = Standard_True;
2335 GeomLProp_SLProps aSLP(aSurf, 2, Precision::Confusion());
2336 const Standard_Integer anArrDim = 3*aPT->NbNodes();
2337 Handle(TShort_HArray1OfShortReal) aNormArr = new TShort_HArray1OfShortReal(1, anArrDim);
2338 Standard_Integer anNormInd = aNormArr->Lower();
2339 for(Standard_Integer i = aPT->UVNodes().Lower(); i <= aPT->UVNodes().Upper(); i++)
2341 const gp_Pnt2d &aP2d = aPT->UVNodes().Value(i);
2342 aSLP.SetParameters(aP2d.X(), aP2d.Y());
2344 gp_XYZ aNorm(0.,0.,0.);
2345 if(!aSLP.IsNormalDefined())
2348 cout << "BRepLib::EnsureNormalConsistency(): Cannot find normal!" << endl;
2353 aNorm = aSLP.Normal().XYZ();
2354 if (aFace.Orientation() == TopAbs_REVERSED)
2357 aNormArr->ChangeValue(anNormInd++) = static_cast<Standard_ShortReal>(aNorm.X());
2358 aNormArr->ChangeValue(anNormInd++) = static_cast<Standard_ShortReal>(aNorm.Y());
2359 aNormArr->ChangeValue(anNormInd++) = static_cast<Standard_ShortReal>(aNorm.Z());
2362 aRetVal = Standard_True;
2363 isNormalsFound = Standard_True;
2364 aPT->SetNormals(aNormArr);
2373 TopTools_IndexedDataMapOfShapeListOfShape aMapEF;
2374 TopExp::MapShapesAndAncestors(theShape,TopAbs_EDGE,TopAbs_FACE,aMapEF);
2375 for(Standard_Integer anInd = 1; anInd <= aMapEF.Extent(); anInd++)
2377 const TopoDS_Edge& anEdg = TopoDS::Edge(aMapEF.FindKey(anInd));
2378 const TopTools_ListOfShape& anEdgList = aMapEF.FindFromIndex(anInd);
2379 if (anEdgList.Extent() != 2)
2381 TopTools_ListIteratorOfListOfShape anItF(anEdgList);
2382 const TopoDS_Face aFace1 = TopoDS::Face(anItF.Value());
2384 const TopoDS_Face aFace2 = TopoDS::Face(anItF.Value());
2385 TopLoc_Location aLoc1, aLoc2;
2386 const Handle(Poly_Triangulation)& aPT1 = BRep_Tool::Triangulation(aFace1, aLoc1);
2387 const Handle(Poly_Triangulation)& aPT2 = BRep_Tool::Triangulation(aFace2, aLoc2);
2389 if(aPT1.IsNull() || aPT2.IsNull())
2392 if(!aPT1->HasNormals() || !aPT2->HasNormals())
2395 const Handle(Poly_PolygonOnTriangulation)& aPTEF1 =
2396 BRep_Tool::PolygonOnTriangulation(anEdg, aPT1, aLoc1);
2397 const Handle(Poly_PolygonOnTriangulation)& aPTEF2 =
2398 BRep_Tool::PolygonOnTriangulation(anEdg, aPT2, aLoc2);
2400 TShort_Array1OfShortReal& aNormArr1 = aPT1->ChangeNormals();
2401 TShort_Array1OfShortReal& aNormArr2 = aPT2->ChangeNormals();
2403 if (aPTEF1->Nodes().Lower() != aPTEF2->Nodes().Lower() ||
2404 aPTEF1->Nodes().Upper() != aPTEF2->Nodes().Upper())
2407 for(Standard_Integer anEdgNode = aPTEF1->Nodes().Lower();
2408 anEdgNode <= aPTEF1->Nodes().Upper(); anEdgNode++)
2411 const Standard_Integer aFNodF1 = aPTEF1->Nodes().Value(anEdgNode);
2412 const Standard_Integer aFNodF2 = aPTEF2->Nodes().Value(anEdgNode);
2414 const Standard_Integer aFNorm1FirstIndex = aNormArr1.Lower() + 3*
2415 (aFNodF1 - aPT1->Nodes().Lower());
2416 const Standard_Integer aFNorm2FirstIndex = aNormArr2.Lower() + 3*
2417 (aFNodF2 - aPT2->Nodes().Lower());
2419 gp_XYZ aNorm1(aNormArr1.Value(aFNorm1FirstIndex),
2420 aNormArr1.Value(aFNorm1FirstIndex+1),
2421 aNormArr1.Value(aFNorm1FirstIndex+2));
2422 gp_XYZ aNorm2(aNormArr2.Value(aFNorm2FirstIndex),
2423 aNormArr2.Value(aFNorm2FirstIndex+1),
2424 aNormArr2.Value(aFNorm2FirstIndex+2));
2425 const Standard_Real aDot = aNorm1 * aNorm2;
2427 if(aDot > aThresDot)
2429 gp_XYZ aNewNorm = (aNorm1 + aNorm2).Normalized();
2430 aNormArr1.ChangeValue(aFNorm1FirstIndex) =
2431 aNormArr2.ChangeValue(aFNorm2FirstIndex) =
2432 static_cast<Standard_ShortReal>(aNewNorm.X());
2433 aNormArr1.ChangeValue(aFNorm1FirstIndex+1) =
2434 aNormArr2.ChangeValue(aFNorm2FirstIndex+1) =
2435 static_cast<Standard_ShortReal>(aNewNorm.Y());
2436 aNormArr1.ChangeValue(aFNorm1FirstIndex+2) =
2437 aNormArr2.ChangeValue(aFNorm2FirstIndex+2) =
2438 static_cast<Standard_ShortReal>(aNewNorm.Z());
2439 aRetVal = Standard_True;
2447 //=======================================================================
2448 //function : SortFaces
2450 //=======================================================================
2452 void BRepLib::SortFaces (const TopoDS_Shape& Sh,
2453 TopTools_ListOfShape& LF)
2456 TopTools_ListOfShape LTri,LPlan,LCyl,LCon,LSphere,LTor,LOther;
2457 TopExp_Explorer exp(Sh,TopAbs_FACE);
2459 Handle(Geom_Surface) S;
2461 for (; exp.More(); exp.Next()) {
2462 const TopoDS_Face& F = TopoDS::Face(exp.Current());
2463 S = BRep_Tool::Surface(F, l);
2465 if (S->DynamicType() == STANDARD_TYPE(Geom_RectangularTrimmedSurface)) {
2466 S = Handle(Geom_RectangularTrimmedSurface)::DownCast (S)->BasisSurface();
2468 GeomAdaptor_Surface AS(S);
2469 switch (AS.GetType()) {
2475 case GeomAbs_Cylinder:
2485 case GeomAbs_Sphere:
2499 else LTri.Append(F);
2501 LF.Append(LPlan); LF.Append(LCyl ); LF.Append(LCon); LF.Append(LSphere);
2502 LF.Append(LTor ); LF.Append(LOther); LF.Append(LTri);
2505 //=======================================================================
2506 //function : ReverseSortFaces
2508 //=======================================================================
2510 void BRepLib::ReverseSortFaces (const TopoDS_Shape& Sh,
2511 TopTools_ListOfShape& LF)
2514 // Use the allocator of the result LF for intermediate results
2515 TopTools_ListOfShape LTri(LF.Allocator()), LPlan(LF.Allocator()),
2516 LCyl(LF.Allocator()), LCon(LF.Allocator()), LSphere(LF.Allocator()),
2517 LTor(LF.Allocator()), LOther(LF.Allocator());
2518 TopExp_Explorer exp(Sh,TopAbs_FACE);
2521 for (; exp.More(); exp.Next()) {
2522 const TopoDS_Face& F = TopoDS::Face(exp.Current());
2523 const Handle(Geom_Surface)& S = BRep_Tool::Surface(F, l);
2525 GeomAdaptor_Surface AS(S);
2526 switch (AS.GetType()) {
2532 case GeomAbs_Cylinder:
2542 case GeomAbs_Sphere:
2556 else LTri.Append(F);
2558 LF.Append(LTri); LF.Append(LOther); LF.Append(LTor ); LF.Append(LSphere);
2559 LF.Append(LCon); LF.Append(LCyl ); LF.Append(LPlan);
2563 //=======================================================================
2564 // function: BoundingVertex
2566 //=======================================================================
2567 void BRepLib::BoundingVertex(const NCollection_List<TopoDS_Shape>& theLV,
2568 gp_Pnt& theNewCenter, Standard_Real& theNewTol)
2570 Standard_Integer aNb;
2578 Standard_Integer m, n;
2579 Standard_Real aR[2], dR, aD, aEps;
2580 TopoDS_Vertex aV[2];
2584 for (m=0; m<aNb; ++m) {
2586 *((TopoDS_Vertex*)(&theLV.First())):
2587 *((TopoDS_Vertex*)(&theLV.Last()));
2588 aP[m]=BRep_Tool::Pnt(aV[m]);
2589 aR[m]=BRep_Tool::Tolerance(aV[m]);
2599 dR=aR[m]-aR[n]; // dR >= 0.
2600 gp_Vec aVD(aP[m], aP[n]);
2603 if (aD<=dR || aD<aEps) {
2604 theNewCenter = aP[m];
2612 aRr=0.5*(aR[m]+aR[n]+aD);
2613 aXYZr=0.5*(aP[m].XYZ()+aP[n].XYZ()-aVD.XYZ()*(dR/aD));
2618 //aBB.MakeVertex (aVnew, aPr, aRr);
2621 }// else if (aNb==2) {
2623 else { // if (aNb>2)
2624 // compute the point
2626 // issue 0027540 - sum of doubles may depend on the order
2627 // of addition, thus sort the coordinates for stable result
2629 NCollection_Array1<gp_Pnt> aPoints(0, aNb-1);
2630 NCollection_List<TopoDS_Edge>::Iterator aIt(theLV);
2631 for (i = 0; aIt.More(); aIt.Next(), ++i) {
2632 const TopoDS_Vertex& aVi = *((TopoDS_Vertex*)(&aIt.Value()));
2633 gp_Pnt aPi = BRep_Tool::Pnt(aVi);
2637 std::sort(aPoints.begin(), aPoints.end(), BRepLib_ComparePoints());
2639 gp_XYZ aXYZ(0., 0., 0.);
2640 for (i = 0; i < aNb; ++i) {
2641 aXYZ += aPoints(i).XYZ();
2643 aXYZ.Divide((Standard_Real)aNb);
2647 // compute the tolerance for the new vertex
2648 Standard_Real aTi, aDi, aDmax;
2651 aIt.Initialize(theLV);
2652 for (; aIt.More(); aIt.Next()) {
2653 TopoDS_Vertex& aVi=*((TopoDS_Vertex*)(&aIt.Value()));
2654 gp_Pnt aPi=BRep_Tool::Pnt(aVi);
2655 aTi=BRep_Tool::Tolerance(aVi);
2656 aDi=aP.SquareDistance(aPi);