summaryrefslogtreecommitdiff
path: root/src/IntTools/IntTools_ShrunkRange.cxx
blob: 8cb5dc351b0aae78643a191862318c395c22898d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
// File:	IntTools_ShrunkRange.cxx
// Created:	Sun Mar 11 10:38:43 2001
// Author:	Peter KURNEV
//		<pkv@irinox>
//

#include <IntTools_ShrunkRange.ixx>

#include <Precision.hxx>

#include <gp_Lin.hxx>
#include <gp.hxx>
#include <gp_Circ.hxx>

#include <ElCLib.hxx>

#include <Geom_Curve.hxx>

#include <BRep_Tool.hxx>
#include <BRepBuilderAPI_MakeVertex.hxx>
#include <BRepAdaptor_Curve.hxx>
#include <BndLib_Add3dCurve.hxx>

#include <IntTools_Tools.hxx>

//=======================================================================
//function : IntTools_ShrunkRange
//purpose  : 
//=======================================================================
  IntTools_ShrunkRange::IntTools_ShrunkRange ()
{
}

//=======================================================================
//function : IntTools_ShrunkRange
//purpose  : 
//=======================================================================
  IntTools_ShrunkRange::IntTools_ShrunkRange (const TopoDS_Edge& aE,
					      const TopoDS_Vertex& aV1,
					      const TopoDS_Vertex& aV2,
					      const IntTools_Range& aR,
					      const IntTools_Context& aCtx)
{
  myEdge=aE;
  myV1=aV1;
  myV2=aV2;
  myRange=aR;
  myCtx=(IntTools_PContext)&aCtx;
  myIsDone=Standard_False;
  myErrorStatus=1;
  Perform();
}
//=======================================================================
//function : Edge
//purpose  : 
//=======================================================================
  const TopoDS_Edge& IntTools_ShrunkRange::Edge() const
{
  return myEdge;
}
//=======================================================================
//function : ShrunkRange
//purpose  : 
//=======================================================================
  const IntTools_Range& IntTools_ShrunkRange::ShrunkRange() const
{
  return myShrunkRange;
}
//=======================================================================
//function : BndBox
//purpose  : 
//=======================================================================
  const Bnd_Box& IntTools_ShrunkRange::BndBox() const
{
  return myBndBox;
}
//=======================================================================
//function : IsDone
//purpose  : 
//=======================================================================
  Standard_Boolean IntTools_ShrunkRange::IsDone() const
{
  return myIsDone;
}
//=======================================================================
//function : ErrorStatus
//purpose  : 
//=======================================================================
  Standard_Integer IntTools_ShrunkRange::ErrorStatus() const
{
  return myErrorStatus;
}
//=======================================================================
//function : Perform
//purpose  : 
//=======================================================================
  void IntTools_ShrunkRange::Perform()
{
  Standard_Real aCF, aCL, aTolE, aTolV1, aTolV2, t1, t11, t1C, t2, t12, t2C;
  Standard_Real aCoeff, dt1, dt2, aR;
  Standard_Integer pri;
  Standard_Boolean bInf1, bInf2;
  GeomAbs_CurveType aCurveType;
  Handle(Geom_Curve) aC;
  //
  aTolE =BRep_Tool::Tolerance(myEdge);
  aTolV1=BRep_Tool::Tolerance(myV1);
  aTolV2=BRep_Tool::Tolerance(myV2);
  //xf
  //dt1=aCoeff*(aTolV1+aTolE);
  //dt2=aCoeff*(aTolV2+aTolE);
  //xt
  myRange.Range (t1, t2);
  //
  BRepAdaptor_Curve aBAC(myEdge);
  aCurveType=aBAC.GetType();
  //
  aC=BRep_Tool::Curve(myEdge, aCF, aCL);
  BRep_Tool::Range(myEdge, aCF, aCL);
  //
  if (t1 < aCF || t2 > aCL) {
    myErrorStatus=2;
    return;
  }
  //
  if (t1 > t2 ) {
    myErrorStatus=3;
    return;
  }
  //
  aCoeff=2.;
  // xf
  if (aCurveType==GeomAbs_Line) {
    Standard_Real aTV1, aTV2, aEps;
    gp_Pnt aPV1, aPV2, aPC1, aPC2;
    gp_Lin aL;
    //
    aEps=Precision::Confusion();
    aEps=aEps*aEps;//1.e-14;
    aL=aBAC.Line();
    //
    aPV1=BRep_Tool::Pnt(myV1);
    aTV1=ElCLib::Parameter(aL, aPV1);
    //
    aPV2=BRep_Tool::Pnt(myV2);
    aTV2=ElCLib::Parameter(aL, aPV2);
    //
    if (fabs(aTV1-aCF)<aEps && fabs(aTV2-aCL)<aEps) {
      aCoeff=1.;
    }
  }
  //
  dt1=aCoeff*(aTolV1+aTolE);
  dt2=aCoeff*(aTolV2+aTolE);
  // xt
  //
  if (aCurveType==GeomAbs_Line) {
    Standard_Real dt1x, dt2x;

    dt1x = aBAC.Resolution(dt1);
    t11=t1+dt1x;
    
    dt2x = aBAC.Resolution(dt2);
    t12=t2-dt2x;

    if (t11>t2 || t12<t1) {
      t1C=t1;
      t2C=t2;
      myShrunkRange.SetFirst(t1C);
      myShrunkRange.SetLast (t2C);
      //
      // BndBox
      Standard_Real ddx=aTolE;//1.e-12;
      BndLib_Add3dCurve::Add (aBAC, t1C, t2C, ddx, myBndBox);
      
      myErrorStatus=6;
      myIsDone=Standard_True;
      return;
    }
  }
  //
  if (aCurveType==GeomAbs_Circle) {
    gp_Circ aCrc=aBAC.Circle();
    aR=aCrc.Radius();
    t1C=t1+dt1/aR;
    t2C=t2-dt2/aR;
  }

  else {
    //
    // Vertex1 => t1C
    gp_Pnt aP1,aP11;
    aC->D0 (t1, aP1);
    //
    bInf1=Precision::IsNegativeInfinite(t1);
    if (bInf1) {
      t1C=t1;
    }
    //
    else {
      Standard_Real d1 = aCoeff*(aTolV1+aTolE);
      //       dt1 = aBAC.Resolution(d1);
      //
      gp_Vec aD1vec1;
      gp_Pnt aPoint;
      aBAC.D1(t1, aPoint, aD1vec1);
      Standard_Real ad1length1 = aD1vec1.Magnitude();
      Standard_Boolean bTryOtherPoints = Standard_False;
      dt1 = (t2 - t1) * 0.5;

      if(ad1length1 > 1.e-12) {
	dt1 = d1 / ad1length1;

	if(dt1  > (t2 - t1)) {
	  // bad parametrization, big tolerance or too small range
	  bTryOtherPoints = Standard_True;
	}
      }
      else {
	bTryOtherPoints = Standard_True;
      }

      if(bTryOtherPoints) {
	Standard_Integer nbsamples = 5;
	Standard_Integer ii = 0;
	Standard_Real adelta = (t2 - t1) / (nbsamples + 1);
	Standard_Boolean bFound = Standard_False;

	for(ii = 1; ii <= nbsamples; ii++) {
	  Standard_Real aparameter = t1 + (adelta * ii);
	  gp_Pnt aPoint2;
	  aBAC.D1(aparameter, aPoint2, aD1vec1);

	  if(aPoint.Distance(aPoint2) < d1)
	    dt1 = adelta * ii;
	  ad1length1 = aD1vec1.Magnitude();

	  if(ad1length1 > 1.e-12) {
	    dt1 = d1 / ad1length1;

	    if(dt1 < (t2 - t1)) {
	      bFound = Standard_True;
	      break;
	    }
	  }
	}

	if(!bFound) {
	  if(dt1 > (t2 - t1)) {
	    dt1 = aBAC.Resolution(d1);
	  }
	}
      }
      //

      t11=t1+dt1;
      aC->D0 (t11, aP11);
      
      gp_Vec aV11(aP1, aP11);
      // avoid exception if aP1 == aP11
      if (aV11.SquareMagnitude() < gp::Resolution())
        t1C = t1;
      else {
        gp_Dir aD11(aV11);

        gp_Pnt aP1L;
        //
        aP1L.SetCoord (aP1.X()+d1*aD11.X(),
                       aP1.Y()+d1*aD11.Y(),
                       aP1.Z()+d1*aD11.Z());

        BRepBuilderAPI_MakeVertex aMV1(aP1L);
        const TopoDS_Vertex& aV1L=aMV1.Vertex();
        //
        pri=myCtx->ComputeVE (aV1L, myEdge, t1C);
        //
        if (pri==-3) {
          //modified by NIZNHY-PKV Tue Apr  6 14:06:29 2010
	  t1C = t1;
	  //myErrorStatus=4;
	  //return;
	  //modified by NIZNHY-PKV Tue Apr  6 14:06:31 2010
          
        }
      }
    }
    //
    // Vertex2 => t2C
    gp_Pnt aP2, aP12;
    aC->D0 (t2, aP2);
    //
    bInf2=Precision::IsPositiveInfinite(t2);
    if (bInf2) {
      t2C=t2;
    }
    //
    else {
      Standard_Real d2 = aCoeff*(aTolV2+aTolE);
      //       dt2 = aBAC.Resolution(d2);

      //
      gp_Vec aD1vec2;
      gp_Pnt aPoint;
      aBAC.D1(t2, aPoint, aD1vec2);
      Standard_Real ad1length2 = aD1vec2.Magnitude();
      Standard_Boolean bTryOtherPoints = Standard_False;
      dt2 = (t2 - t1) * 0.5;

      if(ad1length2 > 1.e-12) {
	dt2 = d2 / ad1length2;

	if(dt2 > (t2 - t1)) {
	  bTryOtherPoints = Standard_True;
	}
      }
      else {
	bTryOtherPoints = Standard_True;
      }

      if(bTryOtherPoints) {
      	Standard_Integer nbsamples = 5;
	Standard_Integer ii = 0;
	Standard_Real adelta = (t2 - t1) / (nbsamples + 1);
	Standard_Boolean bFound = Standard_False;

	for(ii = 1; ii <= nbsamples; ii++) {
	  Standard_Real aparameter = t2 - (adelta * ii);
	  gp_Pnt aPoint2;
	  aBAC.D1(aparameter, aPoint2, aD1vec2);

	  if(aPoint.Distance(aPoint2) < d2)
	    dt2 = adelta * ii;
	  ad1length2 = aD1vec2.Magnitude();

	  if(ad1length2 > 1.e-12) {
	    dt2 = d2 / ad1length2;

	    if(dt2 < (t2 - t1)) {
	      bFound = Standard_True;
	      break;
	    }
	  }
	}

	if(!bFound) {
	  if(dt2 > (t2 - t1)) {
	    dt2 = aBAC.Resolution(d2);
	  }
	}
      }
      //
      
      t12=t2-dt2;
      aC->D0 (t12, aP12);
      
      gp_Vec aV12(aP2, aP12);
      // avoid exception if aP1 == aP11
      if (aV12.SquareMagnitude() < gp::Resolution())
        t2C = t2;
      else {
        gp_Dir aD12(aV12);

        gp_Pnt aP2L;
        //
        aP2L.SetCoord (aP2.X()+d2*aD12.X(),
                       aP2.Y()+d2*aD12.Y(),
                       aP2.Z()+d2*aD12.Z());

        BRepBuilderAPI_MakeVertex aMV2(aP2L);
        const TopoDS_Vertex& aV2L=aMV2.Vertex();
        //
        pri=myCtx->ComputeVE (aV2L, myEdge, t2C);
        //
        if (pri==-3) {
	  //modified by NIZNHY-PKV Tue Apr  6 14:07:34 2010f
	  t2C = t2;
          //myErrorStatus=5;
          //return;
	  //modified by NIZNHY-PKV Tue Apr  6 14:07:39 2010t
        }
      }
    }
  } // else {


  if (t1C>t2){
    t1C=0.5*(t2+t1);
    t2C=t1C+0.1*(t2-t1C);
  }
  
  if (t1C>t2C) {
    t2C=t1C+0.1*(t2-t1C);
  }

  myShrunkRange.SetFirst(t1C);
  myShrunkRange.SetLast (t2C);
  //
  // BndBox
  Standard_Real ddx=aTolE;//1.e-12;
  BndLib_Add3dCurve::Add (aBAC, t1C, t2C, ddx, myBndBox);
  // 
  // Ok
  myErrorStatus=0;
  myIsDone=Standard_True;
}

//=======================================================================
//function : SetShrunkRange
//purpose  : 
//=======================================================================
  void IntTools_ShrunkRange::SetShrunkRange(const IntTools_Range& aR) 
{
  Standard_Real f, l;
  aR.Range(f, l);
  
  myShrunkRange.SetFirst(f);
  myShrunkRange.SetLast (l);
  
  BRepAdaptor_Curve aBAC(myEdge);
  BndLib_Add3dCurve::Add (aBAC, f, l, 0., myBndBox);
}

/////////////////////////////////////////////////////////////////////////
//
//            myErrorStatus :
//
// 1- Nothing has been done
// 2- The source range is out of the edge's range
// 3- t1 < t2 for source range
// 4- Can not project V1L  to the Edge;
// 5- Can not project V2L  to the Edge;
// 6- for obtained shrunk range [t11, t12] ->  t11>t2 || t12<t1;