1 // Created on: 2001-03-11
2 // Created by: Peter KURNEV
3 // Copyright (c) 2001-2012 OPEN CASCADE SAS
5 // The content of this file is subject to the Open CASCADE Technology Public
6 // License Version 6.5 (the "License"). You may not use the content of this file
7 // except in compliance with the License. Please obtain a copy of the License
8 // at http://www.opencascade.org and read it completely before using this file.
10 // The Initial Developer of the Original Code is Open CASCADE S.A.S., having its
11 // main offices at: 1, place des Freres Montgolfier, 78280 Guyancourt, France.
13 // The Original Code and all software distributed under the License is
14 // distributed on an "AS IS" basis, without warranty of any kind, and the
15 // Initial Developer hereby disclaims all such warranties, including without
16 // limitation, any warranties of merchantability, fitness for a particular
17 // purpose or non-infringement. Please see the License for the specific terms
18 // and conditions governing the rights and limitations under the License.
21 #include <IntTools_ShrunkRange.ixx>
23 #include <Precision.hxx>
27 #include <gp_Circ.hxx>
31 #include <Geom_Curve.hxx>
33 #include <BRep_Tool.hxx>
34 #include <BRepBuilderAPI_MakeVertex.hxx>
35 #include <BRepAdaptor_Curve.hxx>
36 #include <BndLib_Add3dCurve.hxx>
38 #include <IntTools_Tools.hxx>
40 //=======================================================================
41 //function : IntTools_ShrunkRange
43 //=======================================================================
44 IntTools_ShrunkRange::IntTools_ShrunkRange ()
48 //=======================================================================
49 //function : IntTools_ShrunkRange
51 //=======================================================================
52 IntTools_ShrunkRange::IntTools_ShrunkRange (const TopoDS_Edge& aE,
53 const TopoDS_Vertex& aV1,
54 const TopoDS_Vertex& aV2,
55 const IntTools_Range& aR,
56 const Handle(IntTools_Context)& aCtx)
63 myIsDone=Standard_False;
67 //=======================================================================
68 //function : SetContext
70 //=======================================================================
71 void IntTools_ShrunkRange::SetContext(const Handle(IntTools_Context)& aContext)
75 //=======================================================================
78 //=======================================================================
79 const Handle(IntTools_Context)& IntTools_ShrunkRange::Context()const
83 //=======================================================================
86 //=======================================================================
87 const TopoDS_Edge& IntTools_ShrunkRange::Edge() const
91 //=======================================================================
92 //function : ShrunkRange
94 //=======================================================================
95 const IntTools_Range& IntTools_ShrunkRange::ShrunkRange() const
99 //=======================================================================
102 //=======================================================================
103 const Bnd_Box& IntTools_ShrunkRange::BndBox() const
107 //=======================================================================
110 //=======================================================================
111 Standard_Boolean IntTools_ShrunkRange::IsDone() const
115 //=======================================================================
116 //function : ErrorStatus
118 //=======================================================================
119 Standard_Integer IntTools_ShrunkRange::ErrorStatus() const
121 return myErrorStatus;
123 //=======================================================================
126 //=======================================================================
127 void IntTools_ShrunkRange::Perform()
129 Standard_Real aCF, aCL, aTolE, aTolV1, aTolV2, t1, t11, t1C, t2, t12, t2C;
130 Standard_Real aCoeff, dt1, dt2, aR;
131 Standard_Integer pri;
132 Standard_Boolean bInf1, bInf2;
133 GeomAbs_CurveType aCurveType;
134 Handle(Geom_Curve) aC;
136 if (myContext.IsNull()) {
137 myContext=new IntTools_Context;
140 aTolE =BRep_Tool::Tolerance(myEdge);
141 aTolV1=BRep_Tool::Tolerance(myV1);
142 aTolV2=BRep_Tool::Tolerance(myV2);
144 myRange.Range (t1, t2);
146 BRepAdaptor_Curve aBAC(myEdge);
147 aCurveType=aBAC.GetType();
149 aC=BRep_Tool::Curve(myEdge, aCF, aCL);
150 BRep_Tool::Range(myEdge, aCF, aCL);
152 if (t1 < aCF || t2 > aCL) {
164 if (aCurveType==GeomAbs_Line) {
165 Standard_Real aTV1, aTV2, aEps;
166 gp_Pnt aPV1, aPV2, aPC1, aPC2;
169 aEps=Precision::Confusion();
170 aEps=aEps*aEps;//1.e-14;
173 aPV1=BRep_Tool::Pnt(myV1);
174 aTV1=ElCLib::Parameter(aL, aPV1);
176 aPV2=BRep_Tool::Pnt(myV2);
177 aTV2=ElCLib::Parameter(aL, aPV2);
179 if (fabs(aTV1-aCF)<aEps && fabs(aTV2-aCL)<aEps) {
184 dt1=aCoeff*(aTolV1+aTolE);
185 dt2=aCoeff*(aTolV2+aTolE);
188 if (aCurveType==GeomAbs_Line) {
189 Standard_Real dt1x, dt2x;
191 dt1x = aBAC.Resolution(dt1);
194 dt2x = aBAC.Resolution(dt2);
197 if (t11>t2 || t12<t1) {
200 myShrunkRange.SetFirst(t1C);
201 myShrunkRange.SetLast (t2C);
204 Standard_Real ddx=aTolE;//1.e-12;
205 BndLib_Add3dCurve::Add (aBAC, t1C, t2C, ddx, myBndBox);
208 myIsDone=Standard_True;
213 if (aCurveType==GeomAbs_Circle) {
214 gp_Circ aCrc=aBAC.Circle();
226 bInf1=Precision::IsNegativeInfinite(t1);
232 Standard_Real d1 = aCoeff*(aTolV1+aTolE);
233 // dt1 = aBAC.Resolution(d1);
237 aBAC.D1(t1, aPoint, aD1vec1);
238 Standard_Real ad1length1 = aD1vec1.Magnitude();
239 Standard_Boolean bTryOtherPoints = Standard_False;
240 dt1 = (t2 - t1) * 0.5;
242 if(ad1length1 > 1.e-12) {
243 dt1 = d1 / ad1length1;
245 if(dt1 > (t2 - t1)) {
246 // bad parametrization, big tolerance or too small range
247 bTryOtherPoints = Standard_True;
251 bTryOtherPoints = Standard_True;
254 if(bTryOtherPoints) {
255 Standard_Integer nbsamples = 5;
256 Standard_Integer ii = 0;
257 Standard_Real adelta = (t2 - t1) / (nbsamples + 1);
258 Standard_Boolean bFound = Standard_False;
260 for(ii = 1; ii <= nbsamples; ii++) {
261 Standard_Real aparameter = t1 + (adelta * ii);
263 aBAC.D1(aparameter, aPoint2, aD1vec1);
265 if(aPoint.Distance(aPoint2) < d1)
267 ad1length1 = aD1vec1.Magnitude();
269 if(ad1length1 > 1.e-12) {
270 dt1 = d1 / ad1length1;
272 if(dt1 < (t2 - t1)) {
273 bFound = Standard_True;
280 if(dt1 > (t2 - t1)) {
281 dt1 = aBAC.Resolution(d1);
290 gp_Vec aV11(aP1, aP11);
291 // avoid exception if aP1 == aP11
292 if (aV11.SquareMagnitude() < gp::Resolution())
299 aP1L.SetCoord (aP1.X()+d1*aD11.X(),
301 aP1.Z()+d1*aD11.Z());
303 BRepBuilderAPI_MakeVertex aMV1(aP1L);
304 const TopoDS_Vertex& aV1L=aMV1.Vertex();
306 pri=myContext->ComputeVE (aV1L, myEdge, t1C);
318 bInf2=Precision::IsPositiveInfinite(t2);
324 Standard_Real d2 = aCoeff*(aTolV2+aTolE);
325 // dt2 = aBAC.Resolution(d2);
330 aBAC.D1(t2, aPoint, aD1vec2);
331 Standard_Real ad1length2 = aD1vec2.Magnitude();
332 Standard_Boolean bTryOtherPoints = Standard_False;
333 dt2 = (t2 - t1) * 0.5;
335 if(ad1length2 > 1.e-12) {
336 dt2 = d2 / ad1length2;
338 if(dt2 > (t2 - t1)) {
339 bTryOtherPoints = Standard_True;
343 bTryOtherPoints = Standard_True;
346 if(bTryOtherPoints) {
347 Standard_Integer nbsamples = 5;
348 Standard_Integer ii = 0;
349 Standard_Real adelta = (t2 - t1) / (nbsamples + 1);
350 Standard_Boolean bFound = Standard_False;
352 for(ii = 1; ii <= nbsamples; ii++) {
353 Standard_Real aparameter = t2 - (adelta * ii);
355 aBAC.D1(aparameter, aPoint2, aD1vec2);
357 if(aPoint.Distance(aPoint2) < d2)
359 ad1length2 = aD1vec2.Magnitude();
361 if(ad1length2 > 1.e-12) {
362 dt2 = d2 / ad1length2;
364 if(dt2 < (t2 - t1)) {
365 bFound = Standard_True;
372 if(dt2 > (t2 - t1)) {
373 dt2 = aBAC.Resolution(d2);
382 gp_Vec aV12(aP2, aP12);
383 // avoid exception if aP1 == aP11
384 if (aV12.SquareMagnitude() < gp::Resolution())
391 aP2L.SetCoord (aP2.X()+d2*aD12.X(),
393 aP2.Z()+d2*aD12.Z());
395 BRepBuilderAPI_MakeVertex aMV2(aP2L);
396 const TopoDS_Vertex& aV2L=aMV2.Vertex();
398 pri=myContext->ComputeVE (aV2L, myEdge, t2C);
410 t2C=t1C+0.1*(t2-t1C);
414 t2C=t1C+0.1*(t2-t1C);
417 myShrunkRange.SetFirst(t1C);
418 myShrunkRange.SetLast (t2C);
421 Standard_Real ddx=aTolE;//1.e-12;
422 BndLib_Add3dCurve::Add (aBAC, t1C, t2C, ddx, myBndBox);
426 myIsDone=Standard_True;
429 //=======================================================================
430 //function : SetShrunkRange
432 //=======================================================================
433 void IntTools_ShrunkRange::SetShrunkRange(const IntTools_Range& aR)
438 myShrunkRange.SetFirst(f);
439 myShrunkRange.SetLast (l);
441 BRepAdaptor_Curve aBAC(myEdge);
442 BndLib_Add3dCurve::Add (aBAC, f, l, 0., myBndBox);
445 /////////////////////////////////////////////////////////////////////////
449 // 1- Nothing has been done
450 // 2- The source range is out of the edge's range
451 // 3- t1 < t2 for source range
452 // 4- Can not project V1L to the Edge;
453 // 5- Can not project V2L to the Edge;
454 // 6- for obtained shrunk range [t11, t12] -> t11>t2 || t12<t1;