0022550: Fixing data races
[occt.git] / src / BRepLib / BRepLib.cxx
CommitLineData
7fd59977 1// File: BRepLib.cxx
2// Created: Wed Dec 15 17:53:58 1993
3// Author: Remi LEQUETTE
4// <rle@zerox>
5
0d969553 6// History: pmn 26/09/97 Add parameters of approximation in BuildCurve3d
7fd59977 7// Modified by skv - Thu Jun 3 12:39:19 2004 OCC5898
8
9#include <BRepLib.ixx>
10#include <BRepAdaptor_Surface.hxx>
11#include <BRepAdaptor_HSurface.hxx>
12#include <BRepAdaptor_HCurve2d.hxx>
13#include <BRep_Tool.hxx>
14#include <BRep_Builder.hxx>
15#include <Geom_Surface.hxx>
16#include <Geom_RectangularTrimmedSurface.hxx>
17#include <Geom_Plane.hxx>
18#include <Geom_Curve.hxx>
19#include <Geom_BSplineCurve.hxx>
20#include <Geom_TrimmedCurve.hxx>
21#include <Geom2d_Curve.hxx>
22#include <Geom2d_TrimmedCurve.hxx>
23#include <Geom2d_BSplineCurve.hxx>
24#include <GeomLib.hxx>
25#include <TopExp_Explorer.hxx>
26#include <TopoDS.hxx>
27#include <TopoDS_Edge.hxx>
28#include <TopoDS_Face.hxx>
29#include <TopoDS_Vertex.hxx>
30#include <TopTools_MapOfShape.hxx>
31#include <TopTools_IndexedDataMapOfShapeListOfShape.hxx>
32#include <TopTools_ListIteratorOfListOfShape.hxx>
33#include <TopExp.hxx>
34#include <gp.hxx>
35#include <gp_Ax2.hxx>
36#include <gp_Pln.hxx>
37#include <Standard_Real.hxx>
38#include <Precision.hxx>
39#include <BRep_GCurve.hxx>
40#include <BRep_TEdge.hxx>
41#include <BRep_TFace.hxx>
42#include <AppParCurves_MultiCurve.hxx>
43#include <AppParCurves_MultiBSpCurve.hxx>
44#include <BRep_ListOfCurveRepresentation.hxx>
45#include <BRep_CurveRepresentation.hxx>
46#include <BRep_ListIteratorOfListOfCurveRepresentation.hxx>
47#include <BRep_TVertex.hxx>
41194117 48#include <AdvApprox_ApproxAFunction.hxx>
7fd59977 49#include <Approx_SameParameter.hxx>
50#include <TColgp_Array1OfPnt.hxx>
51#include <TColgp_Array1OfPnt2d.hxx>
52#include <TColStd_Array1OfReal.hxx>
7fd59977 53#include <TColStd_MapOfTransient.hxx>
54#include <GeomAdaptor_Curve.hxx>
55#include <GeomAdaptor_HCurve.hxx>
56#include <GeomAdaptor_Surface.hxx>
57#include <GeomAdaptor_HSurface.hxx>
58#include <Geom2dAdaptor_Curve.hxx>
59#include <Geom2dAdaptor_HCurve.hxx>
60#include <Geom2dAdaptor.hxx>
61#include <Geom2dConvert.hxx>
62#include <GCPnts_QuasiUniformDeflection.hxx>
63#include <BSplCLib.hxx>
64#include <ElSLib.hxx>
65#include <Adaptor3d_CurveOnSurface.hxx>
66#include <Extrema_LocateExtPC.hxx>
67#include <ProjLib_ProjectedCurve.hxx>
68#include <BRepClass3d_SolidClassifier.hxx>
69#include <Bnd_Box.hxx>
70#include <BRepBndLib.hxx>
71#include <Approx_CurvilinearParameter.hxx>
72#include <Geom_BSplineSurface.hxx>
73
7fd59977 74
41194117 75// TODO - not thread-safe static variables
7fd59977 76static Standard_Real thePrecision = Precision::Confusion();
77static Handle(Geom_Plane) thePlane;
78
79//=======================================================================
80//function : Precision
81//purpose :
82//=======================================================================
83
84void BRepLib::Precision(const Standard_Real P)
85{
86 thePrecision = P;
87}
88
89//=======================================================================
90//function : Precision
91//purpose :
92//=======================================================================
93
94Standard_Real BRepLib::Precision()
95{
96 return thePrecision;
97}
98
99//=======================================================================
100//function : Plane
101//purpose :
102//=======================================================================
103
104void BRepLib::Plane(const Handle(Geom_Plane)& P)
105{
106 thePlane = P;
107}
108
109
110//=======================================================================
111//function : Plane
112//purpose :
113//=======================================================================
114
115const Handle(Geom_Plane)& BRepLib::Plane()
116{
117 if (thePlane.IsNull()) thePlane = new Geom_Plane(gp::XOY());
118 return thePlane;
119}
120//=======================================================================
121//function : CheckSameRange
122//purpose :
123//=======================================================================
124
125Standard_Boolean BRepLib::CheckSameRange(const TopoDS_Edge& AnEdge,
126 const Standard_Real Tolerance)
127{
128 Standard_Boolean IsSameRange = Standard_True,
129 first_time_in = Standard_True ;
130
131 BRep_ListIteratorOfListOfCurveRepresentation an_Iterator
132 ((*((Handle(BRep_TEdge)*)&AnEdge.TShape()))->ChangeCurves());
133
134 Standard_Real first, last;
135#ifndef DEB
136 Standard_Real current_first =0., current_last =0. ;
137#else
138 Standard_Real current_first,current_last ;
139#endif
140 Handle(BRep_GCurve) geometric_representation_ptr ;
141
142 while (IsSameRange && an_Iterator.More()) {
143 geometric_representation_ptr =
144 Handle(BRep_GCurve)::DownCast(an_Iterator.Value());
145 if (!geometric_representation_ptr.IsNull()) {
146
147 first = geometric_representation_ptr->First();
148 last = geometric_representation_ptr->Last();
149 if (first_time_in ) {
150 current_first = first ;
151 current_last = last ;
152 first_time_in = Standard_False ;
153 }
154 else {
155 IsSameRange = (Abs(current_first - first) <= Tolerance)
156 && (Abs(current_last -last) <= Tolerance ) ;
157 }
158 }
159 an_Iterator.Next() ;
160 }
161 return IsSameRange ;
162}
163
164//=======================================================================
165//function : SameRange
166//purpose :
167//=======================================================================
168
169void BRepLib::SameRange(const TopoDS_Edge& AnEdge,
170 const Standard_Real Tolerance)
171{
172 BRep_ListIteratorOfListOfCurveRepresentation an_Iterator
173 ((*((Handle(BRep_TEdge)*)&AnEdge.TShape()))->ChangeCurves());
174
175 Handle(Geom2d_Curve) Curve2dPtr, Curve2dPtr2, NewCurve2dPtr, NewCurve2dPtr2;
176 TopLoc_Location LocalLoc ;
177
178 Standard_Boolean first_time_in = Standard_True,
179 has_curve,
180 has_closed_curve ;
181 Handle(BRep_GCurve) geometric_representation_ptr ;
182 Standard_Real first,
183 current_first,
184 last,
185 current_last ;
186
187 const Handle(Geom_Curve) C = BRep_Tool::Curve(AnEdge,
188 LocalLoc,
189 current_first,
190 current_last);
191 if (!C.IsNull()) {
192 first_time_in = Standard_False ;
193 }
194
195 while (an_Iterator.More()) {
196 geometric_representation_ptr =
197 Handle(BRep_GCurve)::DownCast(an_Iterator.Value());
198 if (! geometric_representation_ptr.IsNull()) {
199 has_closed_curve =
200 has_curve = Standard_False ;
201 first = geometric_representation_ptr->First();
202 last = geometric_representation_ptr->Last();
203 if (geometric_representation_ptr->IsCurveOnSurface()) {
204 Curve2dPtr = geometric_representation_ptr->PCurve() ;
205 has_curve = Standard_True ;
206 }
207 if (geometric_representation_ptr->IsCurveOnClosedSurface()) {
208 Curve2dPtr2 = geometric_representation_ptr->PCurve2() ;
209 has_closed_curve = Standard_True ;
210 }
211 if (has_curve || has_closed_curve) {
212 if (first_time_in) {
213 current_first = first ;
214 current_last = last ;
215 first_time_in = Standard_False ;
216 }
217
218 if (Abs(first - current_first) > Precision::Confusion() ||
219 Abs(last - current_last) > Precision::Confusion() )
220 {
221 if (has_curve)
222 {
223 GeomLib::SameRange(Tolerance,
224 Curve2dPtr,
225 geometric_representation_ptr->First(),
226 geometric_representation_ptr->Last(),
227 current_first,
228 current_last,
229 NewCurve2dPtr);
230 geometric_representation_ptr->PCurve(NewCurve2dPtr) ;
231 }
232 if (has_closed_curve)
233 {
234 GeomLib::SameRange(Tolerance,
235 Curve2dPtr2,
236 geometric_representation_ptr->First(),
237 geometric_representation_ptr->Last(),
238 current_first,
239 current_last,
240 NewCurve2dPtr2);
241 geometric_representation_ptr->PCurve2(NewCurve2dPtr2) ;
242 }
243 }
244 }
245 }
246 an_Iterator.Next() ;
247 }
248 BRep_Builder B;
249 B.Range(TopoDS::Edge(AnEdge),
250 current_first,
251 current_last) ;
252
253 B.SameRange(AnEdge,
254 Standard_True) ;
255}
256
257//=======================================================================
258//function : EvaluateMaxSegment
259//purpose : return MaxSegment to pass in approximation, if MaxSegment==0 provided
260//=======================================================================
261
262static Standard_Integer evaluateMaxSegment(const Standard_Integer aMaxSegment,
263 const Adaptor3d_CurveOnSurface& aCurveOnSurface)
264{
265 if (aMaxSegment != 0) return aMaxSegment;
266
267 Handle(Adaptor3d_HSurface) aSurf = aCurveOnSurface.GetSurface();
268 Handle(Adaptor2d_HCurve2d) aCurv2d = aCurveOnSurface.GetCurve();
269
270 Standard_Real aNbSKnots = 0, aNbC2dKnots = 0;
271
272 if (aSurf->GetType() == GeomAbs_BSplineSurface) {
273 Handle(Geom_BSplineSurface) aBSpline = aSurf->BSpline();
274 aNbSKnots = Max(aBSpline->NbUKnots(), aBSpline->NbVKnots());
275 }
276 if (aCurv2d->GetType() == GeomAbs_BSplineCurve) {
277 aNbC2dKnots = aCurv2d->NbKnots();
278 }
279 Standard_Integer aReturn = (Standard_Integer) ( 30 + Max(aNbSKnots, aNbC2dKnots) ) ;
280 return aReturn;
281}
282
283//=======================================================================
284//function : BuildCurve3d
285//purpose :
286//=======================================================================
287
288Standard_Boolean BRepLib::BuildCurve3d(const TopoDS_Edge& AnEdge,
289 const Standard_Real Tolerance,
290 const GeomAbs_Shape Continuity,
291 const Standard_Integer MaxDegree,
292 const Standard_Integer MaxSegment)
293{
294 Standard_Integer //ErrorCode,
295// ReturnCode = 0,
296 ii,
297// num_knots,
298 jj;
299
300 TopLoc_Location LocalLoc,L[2],LC;
301 Standard_Real f,l,fc,lc, first[2], last[2],
302 tolerance,
303 max_deviation,
304 average_deviation ;
305 Handle(Geom2d_Curve) Curve2dPtr, Curve2dArray[2] ;
306 Handle(Geom_Surface) SurfacePtr, SurfaceArray[2] ;
307
308 Standard_Integer not_done ;
309 // if the edge has a 3d curve returns true
310
311
312 const Handle(Geom_Curve) C = BRep_Tool::Curve(AnEdge,LocalLoc,f,l);
313 if (!C.IsNull())
314 return Standard_True;
315//
316// this should not exists but UpdateEdge makes funny things
317// if the edge is not same range
318//
319 if (! CheckSameRange(AnEdge,
320 Precision::Confusion())) {
321 SameRange(AnEdge,
322 Tolerance) ;
323 }
324
325
326
327 // search a curve on a plane
328 Handle(Geom_Surface) S;
329 Handle(Geom2d_Curve) PC;
330 Standard_Integer i = 0;
331 Handle(Geom_Plane) P;
332 not_done = 1 ;
333
334 while (not_done) {
335 i++;
336 BRep_Tool::CurveOnSurface(AnEdge,PC,S,LocalLoc,f,l,i);
337 Handle(Geom_RectangularTrimmedSurface) RT =
338 Handle(Geom_RectangularTrimmedSurface)::DownCast(S);
339 if ( RT.IsNull()) {
340 P = Handle(Geom_Plane)::DownCast(S);
341 }
342 else {
343 P = Handle(Geom_Plane)::DownCast(RT->BasisSurface());
344 }
345 not_done = ! (S.IsNull() || !P.IsNull()) ;
346 }
347 if (! P.IsNull()) {
348 // compute the 3d curve
349 gp_Ax2 axes = P->Position().Ax2();
350 Handle(Geom_Curve) C3d = GeomLib::To3d(axes,PC);
dfc06d1f
A
351 if (C3d.IsNull())
352 return Standard_False;
7fd59977 353 // update the edge
354 Standard_Real First, Last;
355
356 BRep_Builder B;
357 Standard_Boolean is_closed ;
358 is_closed = AnEdge.Closed() ;
359
360 B.UpdateEdge(AnEdge,C3d,LocalLoc,0.0e0);
361 BRep_Tool::Range(AnEdge, S, LC, First, Last);
0d969553 362 B.Range(AnEdge, First, Last); //Do not forget 3D range.(PRO6412)
7fd59977 363 TopoDS_Edge E = AnEdge ;
364 E.Closed(is_closed) ;
365
366 }
367 else {
368 //
369 // compute the 3d curve using existing surface
370 //
371 fc = f ;
372 lc = l ;
373 if (!BRep_Tool::Degenerated(AnEdge)) {
374 jj = 0 ;
375 for (ii = 0 ; ii < 3 ; ii++ ) {
376 BRep_Tool::CurveOnSurface(TopoDS::Edge(AnEdge),
377 Curve2dPtr,
378 SurfacePtr,
379 LocalLoc,
380 fc,
381 lc,
382 ii) ;
383
384 if (!Curve2dPtr.IsNull() && jj < 2){
385 Curve2dArray[jj] = Curve2dPtr ;
386 SurfaceArray[jj] = SurfacePtr ;
387 L[jj] = LocalLoc ;
388 first[jj] = fc ;
389 last[jj] = lc ;
390 jj += 1 ;
391 }
392 }
393 f = first[0] ;
394 l = last[0] ;
395 Curve2dPtr = Curve2dArray[0] ;
396 SurfacePtr = SurfaceArray[0] ;
397
398 Geom2dAdaptor_Curve AnAdaptor3dCurve2d (Curve2dPtr, f, l) ;
399 GeomAdaptor_Surface AnAdaptor3dSurface (SurfacePtr) ;
400 Handle(Geom2dAdaptor_HCurve) AnAdaptor3dCurve2dPtr =
401 new Geom2dAdaptor_HCurve(AnAdaptor3dCurve2d) ;
402 Handle(GeomAdaptor_HSurface) AnAdaptor3dSurfacePtr =
403 new GeomAdaptor_HSurface (AnAdaptor3dSurface) ;
404 Adaptor3d_CurveOnSurface CurveOnSurface( AnAdaptor3dCurve2dPtr,
405 AnAdaptor3dSurfacePtr) ;
406
407 Handle(Geom_Curve) NewCurvePtr ;
408
409 GeomLib::BuildCurve3d(Tolerance,
410 CurveOnSurface,
411 f,
412 l,
413 NewCurvePtr,
414 max_deviation,
415 average_deviation,
416 Continuity,
417 MaxDegree,
418 evaluateMaxSegment(MaxSegment,CurveOnSurface)) ;
419 BRep_Builder B;
420 tolerance = BRep_Tool::Tolerance(AnEdge) ;
421 //Patch
422 //max_deviation = Max(tolerance, max_deviation) ;
423 max_deviation = Max( tolerance, Tolerance );
dfc06d1f
A
424 if (NewCurvePtr.IsNull())
425 return Standard_False;
7fd59977 426 Standard_Boolean is_closed ;
427 is_closed = AnEdge.Closed() ;
428 B.UpdateEdge(TopoDS::Edge(AnEdge),
429 NewCurvePtr,
430 L[0],
431 max_deviation) ;
432 TopoDS_Edge E = AnEdge ;
433 E.Closed(is_closed) ;
434 if (jj == 1 ) {
435//
436// if there is only one curve on surface attached to the edge
437// than it can be qualified sameparameter
438//
439 B.SameParameter(TopoDS::Edge(AnEdge),
440 Standard_True) ;
441 }
442 }
443 else {
444 return Standard_False ;
445 }
446
447 }
448 return Standard_True;
449}
450//=======================================================================
451//function : BuildCurves3d
452//purpose :
453//=======================================================================
454
455Standard_Boolean BRepLib::BuildCurves3d(const TopoDS_Shape& S)
456
457{
458 return BRepLib::BuildCurves3d(S,
459 1.0e-5) ;
460}
461
462//=======================================================================
463//function : BuildCurves3d
464//purpose :
465//=======================================================================
466
467Standard_Boolean BRepLib::BuildCurves3d(const TopoDS_Shape& S,
468 const Standard_Real Tolerance,
469 const GeomAbs_Shape Continuity,
470 const Standard_Integer MaxDegree,
471 const Standard_Integer MaxSegment)
472{
473 Standard_Boolean boolean_value,
474 ok = Standard_True;
475 TopTools_MapOfShape a_counter ;
476 TopExp_Explorer ex(S,TopAbs_EDGE);
477
478 while (ex.More()) {
479 if (a_counter.Add(ex.Current())) {
480 boolean_value =
481 BuildCurve3d(TopoDS::Edge(ex.Current()),
482 Tolerance, Continuity,
483 MaxDegree, MaxSegment);
484 ok = ok && boolean_value ;
485 }
486 ex.Next();
487 }
488 return ok;
489}
490//=======================================================================
491//function : UpdateEdgeTolerance
492//purpose :
493//=======================================================================
494
495Standard_Boolean BRepLib::UpdateEdgeTol(const TopoDS_Edge& AnEdge,
496 const Standard_Real MinToleranceRequested,
497 const Standard_Real MaxToleranceToCheck)
498 {
499
500 Standard_Integer curve_on_surface_index,
501 curve_index,
502 not_done,
503 has_closed_curve,
504 has_curve,
505 jj,
506 ii,
507 geom_reference_curve_flag = 0,
508 max_sampling_points = 90,
509 min_sampling_points = 30 ;
510
511 Standard_Real factor = 100.0e0,
512 // sampling_array[2],
513 safe_factor = 1.4e0,
514 current_last,
515 current_first,
516 max_distance,
517 coded_edge_tolerance,
518 edge_tolerance = 0.0e0 ;
519 Handle(TColStd_HArray1OfReal) parameters_ptr ;
520 Handle(BRep_GCurve) geometric_representation_ptr ;
521
522 if (BRep_Tool::Degenerated(AnEdge)) return Standard_False ;
523 coded_edge_tolerance = BRep_Tool::Tolerance(AnEdge) ;
524 if (coded_edge_tolerance > MaxToleranceToCheck) return Standard_False ;
525
526 const Handle(BRep_TEdge)& TE = *((Handle(BRep_TEdge)*)&AnEdge.TShape());
527 BRep_ListOfCurveRepresentation& list_curve_rep = TE->ChangeCurves() ;
528 BRep_ListIteratorOfListOfCurveRepresentation an_iterator(list_curve_rep),
529 second_iterator(list_curve_rep) ;
530 Handle(Geom2d_Curve) curve2d_ptr, new_curve2d_ptr;
531 Handle(Geom_Surface) surface_ptr ;
532 TopLoc_Location local_location ;
533 GCPnts_QuasiUniformDeflection a_sampler ;
534 GeomAdaptor_Curve geom_reference_curve ;
535 Adaptor3d_CurveOnSurface curve_on_surface_reference ;
536 Handle(Geom_Curve) C = BRep_Tool::Curve(AnEdge,
537 local_location,
538 current_first,
539 current_last);
540 curve_on_surface_index = -1 ;
541 if (!C.IsNull()) {
542 if (! local_location.IsIdentity()) {
543 C = Handle(Geom_Curve)::
544 DownCast(C-> Transformed(local_location.Transformation()) ) ;
545 }
546 geom_reference_curve.Load(C) ;
547 geom_reference_curve_flag = 1 ;
548 a_sampler.Initialize(geom_reference_curve,
549 MinToleranceRequested * factor,
550 current_first,
551 current_last) ;
552 }
553 else {
554 not_done = 1 ;
555 curve_on_surface_index = 0 ;
556
557 while (not_done && an_iterator.More()) {
558 geometric_representation_ptr =
559 Handle(BRep_GCurve)::DownCast(second_iterator.Value());
560 if (!geometric_representation_ptr.IsNull()
561 && geometric_representation_ptr->IsCurveOnSurface()) {
562 curve2d_ptr = geometric_representation_ptr->PCurve() ;
563 local_location = geometric_representation_ptr->Location() ;
564 current_first = geometric_representation_ptr->First();
565 //first = geometric_representation_ptr->First();
566 current_last = geometric_representation_ptr->Last();
567 // must be inverted
568 //
569 if (! local_location.IsIdentity() ) {
570 surface_ptr = Handle(Geom_Surface)::
571 DownCast( geometric_representation_ptr->Surface()->
572 Transformed(local_location.Transformation()) ) ;
573 }
574 else {
575 surface_ptr =
576 geometric_representation_ptr->Surface() ;
577 }
578 not_done = 0 ;
579 }
580 curve_on_surface_index += 1 ;
581 }
582 Geom2dAdaptor_Curve AnAdaptor3dCurve2d (curve2d_ptr) ;
583 GeomAdaptor_Surface AnAdaptor3dSurface (surface_ptr) ;
584 Handle(Geom2dAdaptor_HCurve) AnAdaptor3dCurve2dPtr =
585 new Geom2dAdaptor_HCurve(AnAdaptor3dCurve2d) ;
586 Handle(GeomAdaptor_HSurface) AnAdaptor3dSurfacePtr =
587 new GeomAdaptor_HSurface (AnAdaptor3dSurface) ;
588 curve_on_surface_reference.Load( AnAdaptor3dCurve2dPtr) ;
589 curve_on_surface_reference.Load( AnAdaptor3dSurfacePtr) ;
590 a_sampler.Initialize(curve_on_surface_reference,
591 MinToleranceRequested * factor,
592 current_first,
593 current_last) ;
594 }
595 TColStd_Array1OfReal sampling_parameters(1,a_sampler.NbPoints()) ;
596 for (ii = 1 ; ii <= a_sampler.NbPoints() ; ii++) {
597 sampling_parameters(ii) = a_sampler.Parameter(ii) ;
598 }
599 if (a_sampler.NbPoints() < min_sampling_points) {
600 GeomLib::DensifyArray1OfReal(min_sampling_points,
601 sampling_parameters,
602 parameters_ptr) ;
603 }
604 else if (a_sampler.NbPoints() > max_sampling_points) {
605 GeomLib::RemovePointsFromArray(max_sampling_points,
606 sampling_parameters,
607 parameters_ptr) ;
608 }
609 else {
610 jj = 1 ;
611 parameters_ptr =
612 new TColStd_HArray1OfReal(1,sampling_parameters.Length()) ;
613 for (ii = sampling_parameters.Lower() ; ii <= sampling_parameters.Upper() ; ii++) {
614 parameters_ptr->ChangeArray1()(jj) =
615 sampling_parameters(ii) ;
616 jj +=1 ;
617 }
618 }
619
620 curve_index = 0 ;
621
622 while (second_iterator.More()) {
623 geometric_representation_ptr =
624 Handle(BRep_GCurve)::DownCast(second_iterator.Value());
625 if (! geometric_representation_ptr.IsNull() &&
626 curve_index != curve_on_surface_index) {
627 has_closed_curve =
628 has_curve = Standard_False ;
629// first = geometric_representation_ptr->First();
630// last = geometric_representation_ptr->Last();
631 local_location = geometric_representation_ptr->Location() ;
632 if (geometric_representation_ptr->IsCurveOnSurface()) {
633 curve2d_ptr = geometric_representation_ptr->PCurve() ;
634 has_curve = Standard_True ;
635 }
636 if (geometric_representation_ptr->IsCurveOnClosedSurface()) {
637 curve2d_ptr = geometric_representation_ptr->PCurve2() ;
638 has_closed_curve = Standard_True ;
639 }
640
641 if (has_curve ||
642 has_closed_curve) {
643 if (! local_location.IsIdentity() ) {
644 surface_ptr = Handle(Geom_Surface)::
645 DownCast( geometric_representation_ptr->Surface()->
646 Transformed(local_location.Transformation()) ) ;
647 }
648 else {
649 surface_ptr =
650 geometric_representation_ptr->Surface() ;
651 }
652 Geom2dAdaptor_Curve an_adaptor_curve2d (curve2d_ptr) ;
653 GeomAdaptor_Surface an_adaptor_surface(surface_ptr) ;
654 Handle(Geom2dAdaptor_HCurve) an_adaptor_curve2d_ptr =
655 new Geom2dAdaptor_HCurve(an_adaptor_curve2d) ;
656 Handle(GeomAdaptor_HSurface) an_adaptor_surface_ptr =
657 new GeomAdaptor_HSurface (an_adaptor_surface) ;
658 Adaptor3d_CurveOnSurface a_curve_on_surface(an_adaptor_curve2d_ptr,
659 an_adaptor_surface_ptr) ;
660
661 if (BRep_Tool::SameParameter(AnEdge)) {
662
663 GeomLib::EvalMaxParametricDistance(a_curve_on_surface,
664 geom_reference_curve,
665 MinToleranceRequested,
666 parameters_ptr->Array1(),
667 max_distance) ;
668 }
669 else if (geom_reference_curve_flag) {
670 GeomLib::EvalMaxDistanceAlongParameter(a_curve_on_surface,
671 geom_reference_curve,
672 MinToleranceRequested,
673 parameters_ptr->Array1(),
674 max_distance) ;
675 }
676 else {
677
678 GeomLib::EvalMaxDistanceAlongParameter(a_curve_on_surface,
679 curve_on_surface_reference,
680 MinToleranceRequested,
681 parameters_ptr->Array1(),
682 max_distance) ;
683 }
684 max_distance *= safe_factor ;
685 edge_tolerance = Max(max_distance, edge_tolerance) ;
686 }
687
688
689 }
690 curve_index += 1 ;
691 second_iterator.Next() ;
692 }
693
694 TE->Tolerance(edge_tolerance);
695 return Standard_True ;
696
697 }
698//=======================================================================
699//function : UpdateEdgeTolerance
700//purpose :
701//=======================================================================
702
703Standard_Boolean BRepLib::UpdateEdgeTolerance(const TopoDS_Shape& S,
704 const Standard_Real MinToleranceRequested,
705 const Standard_Real MaxToleranceToCheck)
706{
707 TopExp_Explorer ex(S,TopAbs_EDGE);
708 TopTools_MapOfShape a_counter ;
709
710 Standard_Boolean return_status = Standard_False,
711 local_flag ;
712
713 while (ex.More()) {
714 if (a_counter.Add(ex.Current())) {
715 local_flag =
716 BRepLib::UpdateEdgeTol(TopoDS::Edge(ex.Current()),
717 MinToleranceRequested,
718 MaxToleranceToCheck) ;
719 if (local_flag && ! return_status) {
720 return_status = Standard_True ;
721 }
722 }
723 ex.Next();
724 }
725 return return_status ;
726}
727
728//=======================================================================
729//function : SetEdgeTol
730//purpose :
731//=======================================================================
732
733static void SetEdgeTol(const TopoDS_Edge& E,
734 const TopoDS_Face& F)
735{
736 TopLoc_Location L;
737 const Handle(Geom_Surface)& S = BRep_Tool::Surface(F,L);
738 TopLoc_Location l = L.Predivided(E.Location());
739
740 const Handle(BRep_TEdge)& TE = *((Handle(BRep_TEdge)*)&E.TShape());
741 BRep_ListIteratorOfListOfCurveRepresentation itcr(TE->ChangeCurves());
742
743 while (itcr.More()) {
744 const Handle(BRep_CurveRepresentation)& cr = itcr.Value();
745 if(cr->IsCurveOnSurface(S,l)) return;
746 itcr.Next();
747 }
748
749 Handle(Geom_Plane) GP;
750 Handle(Geom_RectangularTrimmedSurface) GRTS;
751 GRTS = Handle(Geom_RectangularTrimmedSurface)::DownCast(S);
752 if(!GRTS.IsNull())
753 GP = Handle(Geom_Plane)::DownCast(GRTS->BasisSurface());
754 else
755 GP = Handle(Geom_Plane)::DownCast(S);
756
41194117
K
757 Handle(GeomAdaptor_HCurve) HC = new GeomAdaptor_HCurve();
758 Handle(GeomAdaptor_HSurface) HS = new GeomAdaptor_HSurface();
7fd59977 759
760 TopLoc_Location LC;
761 Standard_Real First, Last;
762 GeomAdaptor_Curve& GAC = HC->ChangeCurve();
763 GAC.Load(BRep_Tool::Curve(E,LC,First,Last));
764 LC = L.Predivided(LC);
765
766 if (!LC.IsIdentity()) {
767 GP = Handle(Geom_Plane)::DownCast(
768 GP->Transformed(LC.Transformation()));
769 }
770 GeomAdaptor_Surface& GAS = HS->ChangeSurface();
771 GAS.Load(GP);
772
773 ProjLib_ProjectedCurve Proj(HS,HC);
774 Handle(Geom2d_Curve) pc = Geom2dAdaptor::MakeCurve(Proj);
775
776 gp_Pln pln = GAS.Plane();
777 Standard_Real d2 = 0.;
778 Standard_Integer nn = 23;
779 Standard_Real unsurnn = 1./nn;
780 for(Standard_Integer i = 0; i <= nn; i++){
781 Standard_Real t = unsurnn*i;
782 Standard_Real u = First*(1.-t) + Last*t;
783 gp_Pnt Pc3d = HC->Value(u);
784 gp_Pnt2d p2d = pc->Value(u);
785 gp_Pnt Pcons = ElSLib::Value(p2d.X(),p2d.Y(),pln);
786 Standard_Real temp = Pc3d.SquareDistance(Pcons);
787 if(temp > d2) d2 = temp;
788 }
789 d2 = 1.5*sqrt(d2);
790 TE->UpdateTolerance(d2);
791}
792
793//=======================================================================
794//function : SameParameter
795//purpose :
796//=======================================================================
797void BRepLib::SameParameter(const TopoDS_Shape& S,
798 const Standard_Real Tolerance,
799 const Standard_Boolean forced)
800{
801 TopExp_Explorer ex(S,TopAbs_EDGE);
802 TopTools_MapOfShape Done;
803 BRep_Builder brB;
804
805 while (ex.More()) {
806 if (Done.Add(ex.Current())) {
807 if (forced) {
808 brB.SameRange(TopoDS::Edge(ex.Current()), Standard_False);
809 brB.SameParameter(TopoDS::Edge(ex.Current()), Standard_False);
810 }
811 BRepLib::SameParameter(TopoDS::Edge(ex.Current()),Tolerance);
812 }
813 ex.Next();
814 }
815
816 Done.Clear();
817 BRepAdaptor_Surface BS;
818 for(ex.Init(S,TopAbs_FACE); ex.More(); ex.Next()){
819 const TopoDS_Face& curface = TopoDS::Face(ex.Current());
820 if(!Done.Add(curface)) continue;
821 BS.Initialize(curface);
822 if(BS.GetType() != GeomAbs_Plane) continue;
823 TopExp_Explorer ex2;
824 for(ex2.Init(curface,TopAbs_EDGE); ex2.More(); ex2.Next()){
825 const TopoDS_Edge& E = TopoDS::Edge(ex2.Current());
826 SetEdgeTol(E,curface);
827 }
828 }
829 BRepLib::UpdateTolerances(S);
830}
831
0d969553 832//================================================================
7fd59977 833//function : SameParameter
0d969553
Y
834//WARNING : New spec DUB LBO 9/9/97.
835// Recode in the edge the best tolerance found,
836// for vertex extremities it is required to find something else
837//================================================================
7fd59977 838static Standard_Boolean EvalTol(const Handle(Geom2d_Curve)& pc,
839 const Handle(Geom_Surface)& s,
840 const GeomAdaptor_Curve& gac,
841 const Standard_Real tol,
842 Standard_Real& tolbail)
843{
844 Standard_Integer ok = 0;
845 Standard_Real f = gac.FirstParameter();
846 Standard_Real l = gac.LastParameter();
847 Extrema_LocateExtPC Projector;
848 Projector.Initialize(gac,f,l,tol);
849 Standard_Real u,v;
850 gp_Pnt p;
851 tolbail = tol;
852 for(Standard_Integer i = 1; i <= 5; i++){
853 Standard_Real t = i/6.;
854 t = (1.-t) * f + t * l;
855 pc->Value(t).Coord(u,v);
856 p = s->Value(u,v);
857 Projector.Perform(p,t);
858 if (Projector.IsDone()) {
859 Standard_Real dist2 = Projector.SquareDistance();
860 if(dist2 > tolbail * tolbail) tolbail = sqrt (dist2);
861 ok++;
862 }
863 }
864 return (ok > 2);
865}
866
867static Standard_Real ComputeTol(const Handle(Adaptor3d_HCurve)& c3d,
868 const Handle(Adaptor2d_HCurve2d)& c2d,
869 const Handle(Adaptor3d_HSurface)& surf,
870 const Standard_Integer nbp)
871
872{
873
874 TColStd_Array1OfReal dist(1,nbp+10);
875 dist.Init(-1.);
876
877
878 Adaptor3d_CurveOnSurface cons(c2d,surf);
879 Standard_Real d2 = 0.;
880 Standard_Integer nn = nbp;
881 Standard_Real unsurnn = 1./nn;
882 Standard_Real first = c3d->FirstParameter();
883 Standard_Real last = c3d->LastParameter();
884 Standard_Integer i = 0;
885 for(i = 0; i <= nn; i++){
886 Standard_Real t = unsurnn*i;
887 Standard_Real u = first*(1.-t) + last*t;
888 gp_Pnt Pc3d = c3d->Value(u);
889 gp_Pnt Pcons = cons.Value(u);
890 if (Precision::IsInfinite(Pcons.X()) ||
891 Precision::IsInfinite(Pcons.Y()) ||
892 Precision::IsInfinite(Pcons.Z())) {
893 d2=Precision::Infinite();
894 break;
895 }
896 Standard_Real temp = Pc3d.SquareDistance(Pcons);
897
898
899 dist(i+1) = temp;
900
901
902 if(temp > d2) d2 = temp;
903 }
904
905
906 Standard_Boolean ana = Standard_False;
907 Standard_Real D2 = 0;
908 Standard_Integer N1 = 0;
909 Standard_Integer N2 = 0;
910 Standard_Integer N3 = 0;
911
912 for( i = 1; i<= nbp+10; i++)
913 if( dist(i) > 0 ) {
914 if( dist(i) < 1.0 ) N1++;
915 else N2++;
916 }
917
918 if( N1 > N2 && N2 != 0 ) N3 = 100*N2/(N1+N2);
919 if( N3 < 10 && N3 != 0 ) {
920 ana = Standard_True;
921 for( i = 1; i<= nbp+10; i++)
922 if( dist(i) > 0 && dist(i) < 1.0 )
923 if( dist(i) > D2 ) D2 = dist(i);
924 }
925
926 //d2 = 1.5*sqrt(d2);
927 d2 = (!ana) ? 1.5*sqrt(d2) : 1.5*sqrt(D2);
928 if(d2<1.e-7) d2 = 1.e-7;
929
930 return d2;
931}
932
933
934
935void BRepLib::SameParameter(const TopoDS_Edge& AnEdge,
936 const Standard_Real Tolerance)
937{
938 if (BRep_Tool::SameParameter(AnEdge)) return;
939
940 const Standard_Integer NCONTROL = 22;
941
41194117
K
942 Handle(GeomAdaptor_HCurve) HC = new GeomAdaptor_HCurve();
943 Handle(Geom2dAdaptor_HCurve) HC2d = new Geom2dAdaptor_HCurve();
944 Handle(GeomAdaptor_HSurface) HS = new GeomAdaptor_HSurface();
7fd59977 945 GeomAdaptor_Curve& GAC = HC->ChangeCurve();
946 Geom2dAdaptor_Curve& GAC2d = HC2d->ChangeCurve2d();
947 GeomAdaptor_Surface& GAS = HS->ChangeSurface();
948#ifndef DEB
949 Standard_Real f3d =0.,l3d =0.;
950#else
951 Standard_Real f3d,l3d;
952#endif
953 TopLoc_Location L3d;
954 Handle(Geom_Curve) C3d;
955
956 const Handle(BRep_TEdge)& TE = *((Handle(BRep_TEdge)*) &AnEdge.TShape());
957 BRep_ListOfCurveRepresentation& CList = TE->ChangeCurves();
958 BRep_ListIteratorOfListOfCurveRepresentation It(CList);
959
960 Standard_Boolean NotDone = Standard_True;
961
962 while (NotDone && It.More()) {
963 Handle(BRep_GCurve) GCurve = Handle(BRep_GCurve)::DownCast(It.Value());
964 if (!GCurve.IsNull() && GCurve->IsCurve3D()) {
965 C3d = GCurve->Curve3D() ;
966 f3d = GCurve->First();
967 l3d = GCurve->Last();
968 L3d = GCurve->Location() ;
969 NotDone = Standard_False;
970 }
971 It.Next() ;
972 }
973
974 if(C3d.IsNull()) return;
975
976 // modified by NIZHNY-OCC486 Tue Aug 27 17:15:13 2002 :
977 Standard_Boolean m_TrimmedPeriodical = Standard_False;
978 Handle(Standard_Type) TheType = C3d->DynamicType();
979 if( TheType == STANDARD_TYPE(Geom_TrimmedCurve))
980 {
981 const Handle(Geom_Curve)& gtC = (*((Handle(Geom_TrimmedCurve)*)&C3d))->BasisCurve();
982 m_TrimmedPeriodical = gtC->IsPeriodic();
983 }
984 // modified by NIZHNY-OCC486 Tue Aug 27 17:15:17 2002 .
985
986 BRep_Builder B;
987 if(!C3d->IsPeriodic()) {
988 Standard_Real Udeb = C3d->FirstParameter();
989 Standard_Real Ufin = C3d->LastParameter();
990 // modified by NIZHNY-OCC486 Tue Aug 27 17:17:14 2002 :
991 //if (Udeb > f3d) f3d = Udeb;
992 //if (l3d > Ufin) l3d = Ufin;
993 if(!m_TrimmedPeriodical)
994 {
995 if (Udeb > f3d) f3d = Udeb;
996 if (l3d > Ufin) l3d = Ufin;
997 }
998 // modified by NIZHNY-OCC486 Tue Aug 27 17:17:55 2002 .
999 }
1000 if(!L3d.IsIdentity()){
1001 C3d = Handle(Geom_Curve)::DownCast(C3d->Transformed(L3d.Transformation()));
1002 }
1003 GAC.Load(C3d,f3d,l3d);
1004
1005 Standard_Boolean IsSameP = 1;
1006 Standard_Real maxdist = 0.;
1007
1008// Modified by skv - Thu Jun 3 12:39:19 2004 OCC5898 Begin
1009 Standard_Real anEdgeTol = BRep_Tool::Tolerance(AnEdge);
1010// Modified by skv - Thu Jun 3 12:39:20 2004 OCC5898 End
1011 Standard_Boolean SameRange = BRep_Tool::SameRange(AnEdge);
1012 Standard_Boolean YaPCu = Standard_False;
1013 It.Initialize(CList);
1014
1015 while (It.More()) {
1016 Standard_Boolean isANA = Standard_False;
1017 Standard_Boolean isBSP = Standard_False;
1018 Handle(BRep_GCurve) GCurve = Handle(BRep_GCurve)::DownCast(It.Value());
1019 Handle(Geom2d_Curve) PC[2];
1020 Handle(Geom_Surface) S;
1021 if (!GCurve.IsNull() && GCurve->IsCurveOnSurface()) {
1022 YaPCu = Standard_True;
1023 PC[0] = GCurve->PCurve();
1024 TopLoc_Location PCLoc = GCurve->Location();
1025 S = GCurve->Surface();
1026 if (!PCLoc.IsIdentity() ) {
1027 S = Handle(Geom_Surface)::DownCast(S->Transformed(PCLoc.Transformation()));
1028 }
1029 GAS.Load(S);
1030 if (GCurve->IsCurveOnClosedSurface()) {
1031 PC[1] = GCurve->PCurve2();
1032 }
1033
1034 // Eval tol2d to compute SameRange
1035 Standard_Real UResol = GAS.UResolution(Tolerance);
1036 Standard_Real VResol = GAS.VResolution(Tolerance);
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;
1042 if(!SameRange){
1043 GeomLib::SameRange(Tol2d,
1044 PC[i],GCurve->First(),GCurve->Last(),
1045 f3d,l3d,curPC);
1046
1047 updatepc = (curPC != PC[i]);
1048
1049 }
1050 Standard_Boolean goodpc = 1;
1051 GAC2d.Load(curPC,f3d,l3d);
1052
1053 Standard_Real error = ComputeTol(HC, HC2d, HS, NCONTROL);
1054
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;
1065
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() ) {
1071
1072 TColStd_Array1OfReal Knotbs2d (1, bs2d->NbKnots());
1073 bs2d->Knots(Knotbs2d);
1074
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;
1079
1080 bs2d->SetOrigin(Index);
1081 break;
1082 }
1083 }
1084 }
1085
1086 if(bs2d->Continuity() == GeomAbs_C0) {
1087 Standard_Real tolbail;
1088 if(EvalTol(curPC,S,GAC,Tolerance,tolbail)){
1089 bs2d = bs2dsov;
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);
1094
1095 Standard_Integer nbp = bs2d->NbPoles();
1096 TColgp_Array1OfPnt2d poles(1,nbp);
1097 bs2d->Poles(poles);
1098 gp_Pnt2d p = poles(1), p1;
1099 Standard_Real d = Precision::Infinite();
1100 for(Standard_Integer ip = 2; ip <= nbp; ip++) {
1101 p1 = poles(ip);
1102 d = Min(d,p.SquareDistance(p1));
1103 p = p1;
1104 }
1105 d = sqrt(d)*.1;
1106
1107 Tol2dbail = Max(Min(Tol2dbail,d),Tol2d);
1108
1109 Geom2dConvert::C0BSplineToC1BSplineCurve(bs2d,Tol2dbail);
1110
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() ) {
1116
1117 TColStd_Array1OfReal Knotbs2d (1, bs2d->NbKnots());
1118 bs2d->Knots(Knotbs2d);
1119
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;
1124
1125 bs2d->SetOrigin(Index);
1126 break;
1127 }
1128 }
1129 }
1130
1131
1132 if(bs2d->Continuity() == GeomAbs_C0) {
1133 goodpc = 1;
1134 bs2d = bs2dsov;
1135 repar = Standard_False;
1136 }
1137 }
1138 else goodpc = 0;
1139 }
1140
1141 if(goodpc){
1142 if(repar) {
1143 Standard_Integer NbKnots = bs2d->NbKnots();
1144 TColStd_Array1OfReal Knots(1,NbKnots);
1145 bs2d->Knots(Knots);
1146 // BSplCLib::Reparametrize(f3d,l3d,Knots);
1147 BSplCLib::Reparametrize(fC0,lC0,Knots);
1148 bs2d->SetKnots(Knots);
1149 GAC2d.Load(bs2d,f3d,l3d);
1150 curPC = bs2d;
1151 Standard_Boolean updatepcsov = updatepc;
1152 updatepc = Standard_True;
1153
1154 Standard_Real error1 = ComputeTol(HC, HC2d, HS, NCONTROL);
1155 if(error1 > error) {
1156 bs2d = bs2dsov;
1157 GAC2d.Load(bs2d,f3d,l3d);
1158 curPC = bs2d;
1159 updatepc = updatepcsov;
1160 isANA = Standard_True;
1161 }
1162 else {
1163 error = error1;
1164 }
1165 }
1166
1167 //check, if new BSpline "good" or not --------- IFV, Jan of 2000
1168 GeomAbs_Shape cont = bs2d->Continuity();
1169 Standard_Boolean IsBad = Standard_False;
1170
1171 if(cont > GeomAbs_C0 && error > Max(1.e-3,Tolerance)) {
1172 Standard_Integer NbKnots = bs2d->NbKnots();
1173 TColStd_Array1OfReal Knots(1,NbKnots);
1174 bs2d->Knots(Knots);
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);
1182
1183 if(IsBad) continue;
1184
1185 if(dtcur > dtprev) dtratio = dtcur/dtprev;
1186 else dtratio = dtprev/dtcur;
1187 if(dtratio > critratio) {IsBad = Standard_True;}
1188 dtprev = dtcur;
1189
1190 }
1191 if(IsBad) {
1192 // To avoid failures in Approx_CurvilinearParameter
1193 bs2d->Resolution(Max(1.e-3,Tolerance), dtcur);
1194 if(dtmin < dtcur) IsBad = Standard_False;
1195 }
1196 }
1197
1198
1199 if(IsBad ) { //if BSpline "bad", try to reparametrize it
1200 // by its curve length
1201
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),
1207 cont, maxdeg, 10);
1208 if(AppCurPar.IsDone() || AppCurPar.HasResult()) {
1209 bs2d = AppCurPar.Curve2d1();
1210 GAC2d.Load(bs2d,f3d,l3d);
1211 curPC = bs2d;
1212
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);
1217 bs2d->Knots(Knots);
1218// BSplCLib::Reparametrize(f3d,l3d,Knots);
1219 BSplCLib::Reparametrize(fC0,lC0,Knots);
1220 bs2d->SetKnots(Knots);
1221 GAC2d.Load(bs2d,f3d,l3d);
1222 curPC = bs2d;
1223
1224 }
1225 }
1226 }
1227
1228
1229 }
1230 }
1231
1232
1233 if(goodpc){
1234// Approx_SameParameter SameP(HC,HC2d,HS,Tolerance);
1235 Standard_Real aTol = (isANA && isBSP) ? 1.e-7 : Tolerance;
1236 Approx_SameParameter SameP(HC,HC2d,HS,aTol);
1237
1238 if (SameP.IsSameParameter()) {
1239 maxdist = Max(maxdist,SameP.TolReached());
1240 if(updatepc){
1241 if (i == 0) GCurve->PCurve(curPC);
1242 else GCurve->PCurve2(curPC);
1243 }
1244 }
1245 else if (SameP.IsDone()) {
1246 Standard_Real tolreached = SameP.TolReached();
1247 if(tolreached < error) {
1248 curPC = SameP.Curve2d();
1249 updatepc = Standard_True;
1250 maxdist = Max(maxdist,tolreached);
1251 }
1252 else {
1253 maxdist = Max(maxdist, error);
1254 }
1255 if(updatepc){
1256 if (i == 0) GCurve->PCurve(curPC);
1257 else GCurve->PCurve2(curPC);
1258 }
1259 }
1260 else IsSameP = 0;
1261
1262 }
1263 else IsSameP = 0;
1264
1265// Modified by skv - Thu Jun 3 12:39:19 2004 OCC5898 Begin
1266 if (!IsSameP) {
1267 if (anEdgeTol > error) {
1268 maxdist = Max(maxdist, anEdgeTol);
1269 IsSameP = Standard_True;
1270 }
1271 }
1272// Modified by skv - Thu Jun 3 12:39:20 2004 OCC5898 End
1273 }
1274 }
1275 It.Next() ;
1276 }
1277 B.Range(AnEdge,f3d,l3d);
1278 B.SameRange(AnEdge,Standard_True);
1279 if ( IsSameP) {
0d969553
Y
1280 // Reduce eventually the tolerance of the edge, as
1281 // all its representations are processed (except for some associated
1282 // to planes and not stored in the edge !)
1283 // The same cannot be done with vertices that cannot be enlarged
1284 // or left as is.
7fd59977 1285 if (YaPCu) {
0d969553 1286 // Avoid setting too small tolerances.
7fd59977 1287 maxdist = Max(maxdist,Precision::Confusion());
1288 TopoDS_Vertex V1,V2;
1289 TopExp::Vertices(AnEdge,V1,V2);
1290 if (!V1.IsNull())
1291 B.UpdateVertex(V1,maxdist);
1292 if (!V2.IsNull())
1293 B.UpdateVertex(V2,maxdist);
1294 TE->Modified(Standard_True);
1295 TE->Tolerance(maxdist);
1296 }
1297 B.SameParameter(AnEdge,Standard_True);
1298 }
1299}
1300
1301//=======================================================================
1302//function : UpdateTolerances
1303//purpose :
1304//=======================================================================
1305void BRepLib::UpdateTolerances(const TopoDS_Shape& aShape,
1306 const Standard_Boolean verifyTolerance)
1307{
1308
0d969553
Y
1309// Harmonize tolerances
1310// with rule Tolerance(VERTEX)>=Tolerance(EDGE)>=Tolerance(FACE)
7fd59977 1311 BRep_Builder B;
1312 Standard_Real tol=0;
1313 if (verifyTolerance) {
0d969553 1314 // Set tolerance to its minimum value
7fd59977 1315 Handle(Geom_Surface) S;
1316 TopLoc_Location l;
1317 TopExp_Explorer ex;
1318 Bnd_Box aB;
1319 Standard_Real aXmin, aYmin, aZmin, aXmax, aYmax, aZmax, dMax;
1320 for (ex.Init(aShape, TopAbs_FACE); ex.More(); ex.Next()) {
1321 const TopoDS_Face& curf=TopoDS::Face(ex.Current());
1322 S = BRep_Tool::Surface(curf, l);
1323 if (!S.IsNull()) {
1324 aB.SetVoid();
1325 BRepBndLib::Add(curf,aB);
1326 if (S->DynamicType() == STANDARD_TYPE(Geom_RectangularTrimmedSurface)) {
1327 S = (*((Handle(Geom_RectangularTrimmedSurface)*)&S))->BasisSurface();
1328 }
1329 GeomAdaptor_Surface AS(S);
1330 switch (AS.GetType()) {
1331 case GeomAbs_Plane:
1332 case GeomAbs_Cylinder:
1333 case GeomAbs_Cone:
1334 {
1335 tol=Precision::Confusion();
1336 break;
1337 }
1338 case GeomAbs_Sphere:
1339 case GeomAbs_Torus:
1340 {
1341 tol=Precision::Confusion()*2;
1342 break;
1343 }
1344 default:
1345 tol=Precision::Confusion()*4;
1346 }
1347 if (!aB.IsWhole()) {
1348 aB.Get(aXmin, aYmin, aZmin, aXmax, aYmax, aZmax);
1349 dMax=1.;
1350 if (!aB.IsOpenXmin() && !aB.IsOpenXmax()) dMax=aXmax-aXmin;
1351 if (!aB.IsOpenYmin() && !aB.IsOpenYmax()) aYmin=aYmax-aYmin;
1352 if (!aB.IsOpenZmin() && !aB.IsOpenZmax()) aZmin=aZmax-aZmin;
1353 if (aYmin>dMax) dMax=aYmin;
1354 if (aZmin>dMax) dMax=aZmin;
1355 tol=tol*dMax;
0d969553 1356 // Do not process tolerances > 1.
7fd59977 1357 if (tol>1.) tol=0.99;
1358 }
1359 const Handle(BRep_TFace)& Tf = *((Handle(BRep_TFace)*)&curf.TShape());
1360 Tf->Tolerance(tol);
1361 }
1362 }
1363 }
1364
0d969553 1365 //Process edges
7fd59977 1366 TopTools_IndexedDataMapOfShapeListOfShape parents;
1367 TopExp::MapShapesAndAncestors(aShape, TopAbs_EDGE, TopAbs_FACE, parents);
1368 TopTools_ListIteratorOfListOfShape lConx;
1369 Standard_Integer iCur;
1370 for (iCur=1; iCur<=parents.Extent(); iCur++) {
1371 tol=0;
1372 for (lConx.Initialize(parents(iCur)); lConx.More(); lConx.Next()) {
1373 tol=Max(tol, BRep_Tool::Tolerance(TopoDS::Face(lConx.Value())));
1374 }
0d969553
Y
1375 // Update can only increase tolerance, so if the edge has a greater
1376 // tolerance than its faces it is not concerned
7fd59977 1377 B.UpdateEdge(TopoDS::Edge(parents.FindKey(iCur)), tol);
1378 }
1379
0d969553 1380 //Vertices are processed
7fd59977 1381 parents.Clear();
1382 TopExp::MapShapesAndAncestors(aShape, TopAbs_VERTEX, TopAbs_EDGE, parents);
1383 TColStd_MapOfTransient Initialized;
1384 TopTools_MapOfShape Done;
1385 Standard_Integer nbV = parents.Extent();
1386 for (iCur=1; iCur<=nbV; iCur++) {
1387 tol=0;
1388 Done.Clear();
1389 const TopoDS_Vertex& V = TopoDS::Vertex(parents.FindKey(iCur));
1390 Bnd_Box box;
1391 box.Add(BRep_Tool::Pnt(V));
1392 gp_Pnt p3d;
1393 for (lConx.Initialize(parents(iCur)); lConx.More(); lConx.Next()) {
1394 const TopoDS_Edge& E = TopoDS::Edge(lConx.Value());
1395 if(!Done.Add(E)) continue;
1396 tol=Max(tol, BRep_Tool::Tolerance(E));
1397 if(!BRep_Tool::SameRange(E)) continue;
1398 Standard_Real par = BRep_Tool::Parameter(V,E);
1399 Handle(BRep_TEdge)& TE = *((Handle(BRep_TEdge)*)&E.TShape());
1400 BRep_ListIteratorOfListOfCurveRepresentation itcr(TE->Curves());
1401 const TopLoc_Location& Eloc = E.Location();
1402 while (itcr.More()) {
0d969553 1403 // For each CurveRepresentation, check the provided parameter
7fd59977 1404 const Handle(BRep_CurveRepresentation)& cr = itcr.Value();
1405 const TopLoc_Location& loc = cr->Location();
1406 TopLoc_Location L = (Eloc * loc);
1407 if (cr->IsCurve3D()) {
1408 const Handle(Geom_Curve)& C = cr->Curve3D();
0d969553 1409 if (!C.IsNull()) { // edge non degenerated
7fd59977 1410 p3d = C->Value(par);
1411 p3d.Transform(L.Transformation());
1412 box.Add(p3d);
1413 }
1414 }
1415 else if (cr->IsCurveOnSurface()) {
1416 const Handle(Geom_Surface)& Su = cr->Surface();
1417 const Handle(Geom2d_Curve)& PC = cr->PCurve();
1418 Handle(Geom2d_Curve) PC2;
1419 if (cr->IsCurveOnClosedSurface()) {
1420 PC2 = cr->PCurve2();
1421 }
1422 gp_Pnt2d p2d = PC->Value(par);
1423 p3d = Su->Value(p2d.X(),p2d.Y());
1424 p3d.Transform(L.Transformation());
1425 box.Add(p3d);
1426 if (!PC2.IsNull()) {
1427 p2d = PC2->Value(par);
1428 p3d = Su->Value(p2d.X(),p2d.Y());
1429 p3d.Transform(L.Transformation());
1430 box.Add(p3d);
1431 }
1432 }
1433 itcr.Next();
1434 }
1435 }
1436 Standard_Real aXmin, aYmin, aZmin, aXmax, aYmax, aZmax;
1437 box.Get(aXmin, aYmin, aZmin, aXmax, aYmax, aZmax);
1438 aXmax -= aXmin; aYmax -= aYmin; aZmax -= aZmin;
1439 tol = Max(tol,sqrt(aXmax*aXmax+aYmax*aYmax+aZmax*aZmax));
1440 tol += 2.*Epsilon(tol);
1441 if (verifyTolerance) {
0d969553
Y
1442 // ASet minimum value of the tolerance
1443 // Attention to sharing of the vertex by other shapes
7fd59977 1444 const Handle(BRep_TVertex)& TV = *((Handle(BRep_TVertex)*)&V.TShape());
1445 if (Initialized.Add(TV))
1446 TV->Tolerance(tol);
1447 else
1448 B.UpdateVertex(V, tol);
1449 }
1450 else {
0d969553
Y
1451 // Update can only increase tolerance, so if the edge has a greater
1452 // tolerance than its faces it is not concerned
7fd59977 1453 B.UpdateVertex(V, tol);
1454 }
1455 }
1456}
1457
1458//=======================================================================
1459//function : OrientClosedSolid
1460//purpose :
1461//=======================================================================
1462Standard_Boolean BRepLib::OrientClosedSolid(TopoDS_Solid& solid)
1463{
0d969553 1464// Set material inside the solid
7fd59977 1465 BRepClass3d_SolidClassifier where(solid);
1466 where.PerformInfinitePoint(Precision::Confusion());
1467 if (where.State()==TopAbs_IN) {
1468 solid.Reverse();
1469 }
1470 else if (where.State()==TopAbs_ON || where.State()==TopAbs_UNKNOWN)
1471 return Standard_False;
1472
1473 return Standard_True;
1474}
1475
1476//=======================================================================
1477//function : tgtfaces
0d969553
Y
1478//purpose : check the angle at the border between two squares.
1479// Two shares should have a shared front edge.
7fd59977 1480//=======================================================================
1481
1482static Standard_Boolean tgtfaces(const TopoDS_Edge& Ed,
1483 const TopoDS_Face& F1,
1484 const TopoDS_Face& F2,
1485 const Standard_Real ta,
1486 const Standard_Boolean couture)
1487{
1488 Standard_Real u;
1489 TopoDS_Edge E = Ed;
1490 BRepAdaptor_Surface aBAS1(F1,Standard_False);
1491 BRepAdaptor_Surface aBAS2(F2,Standard_False);
1492 Handle(BRepAdaptor_HSurface) HS1 = new BRepAdaptor_HSurface(aBAS1);
1493 Handle(BRepAdaptor_HSurface) HS2;
1494 if(couture) HS2 = HS1;
1495 else HS2 = new BRepAdaptor_HSurface(aBAS2);
1496
1497 E.Orientation(TopAbs_FORWARD);
1498 Handle(BRepAdaptor_HCurve2d) HC2d1 = new BRepAdaptor_HCurve2d();
1499 HC2d1->ChangeCurve2d().Initialize(E,F1);
1500 if(couture) E.Orientation(TopAbs_REVERSED);
1501 Handle(BRepAdaptor_HCurve2d) HC2d2 = new BRepAdaptor_HCurve2d();
1502 HC2d2->ChangeCurve2d().Initialize(E,F2);
1503 Adaptor3d_CurveOnSurface C1(HC2d1,HS1);
1504 Adaptor3d_CurveOnSurface C2(HC2d2,HS2);
1505
1506 Standard_Boolean rev1 = (F1.Orientation() == TopAbs_REVERSED);
1507 Standard_Boolean rev2 = (F2.Orientation() == TopAbs_REVERSED);
c6541a0c 1508 Standard_Real f,l,eps, angmax = -M_PI;
7fd59977 1509#ifndef DEB
1510 Standard_Real ang =0.;
1511#else
1512 Standard_Real ang;
1513#endif
1514 BRep_Tool::Range(E,f,l);
1515 Extrema_LocateExtPC ext;
1516 Standard_Boolean IsInitialized = Standard_False;
1517
1518 eps = (l - f)/100.;
0d969553
Y
1519 f += eps; // to avoid calculations on
1520 l -= eps; // points of pointed squares.
7fd59977 1521 gp_Pnt2d p;
1522 gp_Pnt pp1,pp2;//,PP;
1523 gp_Vec du,dv;
1524 gp_Vec d1,d2;
1525 Standard_Real uu, vv, norm;
1526
1527 Standard_Integer i;
1528 Standard_Boolean Nok;
1529 for(i = 0; (i<= 20) && (angmax<=ta) ; i++){
0d969553 1530 // First suppose that this is sameParameter
7fd59977 1531 Nok = Standard_True;
1532 u = f + (l-f)*i/20;
1533 HC2d1->D0(u,p);
1534 HS1->D1(p.X(),p.Y(),pp1,du,dv);
1535 d1 = (du.Crossed(dv));
1536 norm = d1.Magnitude();
1537 if (norm > 1.e-12) d1 /= norm;
1538 else Nok=Standard_False;
1539 if(rev1) d1.Reverse();
1540
1541 HC2d2->D0(u,p);
1542 HS2->D1(p.X(), p.Y(), pp2, du, dv);
1543 d2 = (du.Crossed(dv));
1544 norm = d2.Magnitude();
1545 if (norm> 1.e-12) d2 /= norm;
1546 else Nok=Standard_False;
1547 if(rev2) d2.Reverse();
1548 if (Nok) ang = d1.Angle(d2);
1549
0d969553 1550 if (Nok &&(ang > ta)) { // Refine by projection
7fd59977 1551 if (! IsInitialized ) {
1552 ext.Initialize(C2,f,l,Precision::PConfusion());
1553 IsInitialized = Standard_True;
1554 }
1555 ext.Perform(pp1,u);
1556 if(ext.IsDone() && ext.IsMin()){
1557 Extrema_POnCurv poc = ext.Point();
1558 Standard_Real v = poc.Parameter();
1559
1560 HC2d2->D0(v,p);
1561 p.Coord(uu,vv);
1562 HS2->D1(p.X(), p.Y(), pp2, du, dv);
1563 d2 = (du.Crossed(dv));
1564 norm = d2.Magnitude();
1565 if (norm> 1.e-12) d2 /= norm;
1566 else Nok = Standard_False;
1567 if(rev2) d2.Reverse();
1568 if (Nok) ang = d1.Angle(d2);
1569 }
1570 }
1571 if(ang >= angmax) angmax = ang;
1572 }
1573
1574 return (angmax<=ta);
1575
1576}
1577
1578
1579//=======================================================================
1580// function : EncodeRegularity
0d969553
Y
1581// purpose : code the regularities on all edges of the shape, boundary of
1582// two faces that do not have it.
7fd59977 1583//=======================================================================
1584
1585void BRepLib::EncodeRegularity(const TopoDS_Shape& S,
1586 const Standard_Real TolAng)
1587{
1588 BRep_Builder B;
1589 TopTools_IndexedDataMapOfShapeListOfShape M;
1590 TopExp::MapShapesAndAncestors(S,TopAbs_EDGE,TopAbs_FACE,M);
1591 TopTools_ListIteratorOfListOfShape It;
1592 TopExp_Explorer Ex;
1593 TopoDS_Face F1,F2;
1594 Standard_Boolean found, couture;
1595 for(Standard_Integer i = 1; i <= M.Extent(); i++){
1596 TopoDS_Edge E = TopoDS::Edge(M.FindKey(i));
1597 found = Standard_False; couture = Standard_False;
1598 F1.Nullify();
1599 for(It.Initialize(M.FindFromIndex(i));It.More() && !found;It.Next()){
1600 if(F1.IsNull()) { F1 = TopoDS::Face(It.Value()); }
1601 else {
1602 if(!F1.IsSame(TopoDS::Face(It.Value()))){
1603 found = Standard_True;
1604 F2 = TopoDS::Face(It.Value());
1605 }
1606 }
1607 }
0d969553 1608 if (!found && !F1.IsNull()){//is it a sewing edge?
7fd59977 1609 TopAbs_Orientation orE = E.Orientation();
1610 TopoDS_Edge curE;
1611 for(Ex.Init(F1,TopAbs_EDGE);Ex.More() && !found;Ex.Next()){
1612 curE= TopoDS::Edge(Ex.Current());
1613 if(E.IsSame(curE) && orE != curE.Orientation()) {
1614 found = Standard_True;
1615 couture = Standard_True;
1616 F2 = F1;
1617 }
1618 }
1619 }
1620 if(found){
1621 if(BRep_Tool::Continuity(E,F1,F2)<=GeomAbs_C0){
041bfce9 1622
1623 try {
1624 if(tgtfaces(E, F1, F2, TolAng, couture)){
1625 B.Continuity(E,F1,F2,GeomAbs_G1);
1626 }
1627 }
1628 catch(Standard_Failure)
1629 {
1630 }
7fd59977 1631 }
1632 }
1633 }
1634}
1635
1636//=======================================================================
1637// function : EncodeRegularity
0d969553 1638// purpose : code the regularity between 2 faces on an edge
7fd59977 1639//=======================================================================
1640
1641void BRepLib::EncodeRegularity(TopoDS_Edge& E,
1642 const TopoDS_Face& F1,
1643 const TopoDS_Face& F2,
1644 const Standard_Real TolAng)
1645{
1646 BRep_Builder B;
1647 if(BRep_Tool::Continuity(E,F1,F2)<=GeomAbs_C0){
041bfce9 1648 try {
1649 if( tgtfaces(E, F1, F2, TolAng, F1.IsEqual(F2))) {
1650 B.Continuity(E,F1,F2,GeomAbs_G1);
1651 }
1652 }
1653 catch(Standard_Failure)
1654 {
1655 }
7fd59977 1656 }
1657}
1658
1659//=======================================================================
1660//function : SortFaces
1661//purpose :
1662//=======================================================================
1663
1664void BRepLib::SortFaces (const TopoDS_Shape& Sh,
1665 TopTools_ListOfShape& LF)
1666{
1667 LF.Clear();
1668 TopTools_ListOfShape LTri,LPlan,LCyl,LCon,LSphere,LTor,LOther;
1669 TopExp_Explorer exp(Sh,TopAbs_FACE);
1670 TopLoc_Location l;
1671 Handle(Geom_Surface) S;
1672
1673 for (; exp.More(); exp.Next()) {
1674 const TopoDS_Face& F = TopoDS::Face(exp.Current());
1675 S = BRep_Tool::Surface(F, l);
1676 if (!S.IsNull()) {
1677 if (S->DynamicType() == STANDARD_TYPE(Geom_RectangularTrimmedSurface)) {
1678 S = (*((Handle(Geom_RectangularTrimmedSurface)*)&S))->BasisSurface();
1679 }
1680 GeomAdaptor_Surface AS(S);
1681 switch (AS.GetType()) {
1682 case GeomAbs_Plane:
1683 {
1684 LPlan.Append(F);
1685 break;
1686 }
1687 case GeomAbs_Cylinder:
1688 {
1689 LCyl.Append(F);
1690 break;
1691 }
1692 case GeomAbs_Cone:
1693 {
1694 LCon.Append(F);
1695 break;
1696 }
1697 case GeomAbs_Sphere:
1698 {
1699 LSphere.Append(F);
1700 break;
1701 }
1702 case GeomAbs_Torus:
1703 {
1704 LTor.Append(F);
1705 break;
1706 }
1707 default:
1708 LOther.Append(F);
1709 }
1710 }
1711 else LTri.Append(F);
1712 }
1713 LF.Append(LPlan); LF.Append(LCyl ); LF.Append(LCon); LF.Append(LSphere);
1714 LF.Append(LTor ); LF.Append(LOther); LF.Append(LTri);
1715}
1716
1717//=======================================================================
1718//function : ReverseSortFaces
1719//purpose :
1720//=======================================================================
1721
1722void BRepLib::ReverseSortFaces (const TopoDS_Shape& Sh,
1723 TopTools_ListOfShape& LF)
1724{
1725 LF.Clear();
1726 TopTools_ListOfShape LTri,LPlan,LCyl,LCon,LSphere,LTor,LOther;
1727 TopExp_Explorer exp(Sh,TopAbs_FACE);
1728 TopLoc_Location l;
1729 Handle(Geom_Surface) S;
1730
1731 for (; exp.More(); exp.Next()) {
1732 const TopoDS_Face& F = TopoDS::Face(exp.Current());
1733 S = BRep_Tool::Surface(F, l);
1734 if (!S.IsNull()) {
1735 if (S->DynamicType() == STANDARD_TYPE(Geom_RectangularTrimmedSurface)) {
1736 S = (*((Handle(Geom_RectangularTrimmedSurface)*)&S))->BasisSurface();
1737 }
1738 GeomAdaptor_Surface AS(S);
1739 switch (AS.GetType()) {
1740 case GeomAbs_Plane:
1741 {
1742 LPlan.Append(F);
1743 break;
1744 }
1745 case GeomAbs_Cylinder:
1746 {
1747 LCyl.Append(F);
1748 break;
1749 }
1750 case GeomAbs_Cone:
1751 {
1752 LCon.Append(F);
1753 break;
1754 }
1755 case GeomAbs_Sphere:
1756 {
1757 LSphere.Append(F);
1758 break;
1759 }
1760 case GeomAbs_Torus:
1761 {
1762 LTor.Append(F);
1763 break;
1764 }
1765 default:
1766 LOther.Append(F);
1767 }
1768 }
1769 else LTri.Append(F);
1770 }
1771 LF.Append(LTri); LF.Append(LOther); LF.Append(LTor ); LF.Append(LSphere);
1772 LF.Append(LCon); LF.Append(LCyl ); LF.Append(LPlan);
1773
1774}
1775
1776