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