summaryrefslogtreecommitdiff
path: root/src/IntTools/IntTools_Tools.cxx
blob: d416fc9e127472bcebf5ae4f57aa42497c8853c0 (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
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
// File:	IntTools_Tools.cxx
// Created:	Thu Nov 16 11:42:23 2000
// Author:	Peter KURNEV
//		<pkv@irinox>
//

#include <IntTools_Tools.ixx>

#include <Precision.hxx>

#include <TopExp_Explorer.hxx>
#include <TopTools_IndexedDataMapOfShapeShape.hxx>

#include <TopoDS.hxx>
#include <TopoDS_Shape.hxx>
#include <TopoDS_Vertex.hxx>
#include <TopoDS_Edge.hxx>
#include <TopoDS_Face.hxx>
#include <TopoDS_Wire.hxx>
#include <TopLoc_Location.hxx>

#include <BRep_Builder.hxx>
#include <BRep_Tool.hxx>
#include <BRepAdaptor_Surface.hxx>
#include <BRepAdaptor_Curve.hxx>
#include <BRepAdaptor_Surface.hxx>

#include <gp_Pnt.hxx>
#include <gp_Pnt2d.hxx>
#include <gp.hxx>
#include <gp_Lin.hxx>
#include <gp_Dir.hxx>
#include <gp_Ax1.hxx>

#include <Geom_Curve.hxx>
#include <GeomAdaptor_Surface.hxx>
#include <Geom_Surface.hxx>
#include <GeomAPI_ProjectPointOnSurf.hxx>
#include <GeomAPI_ProjectPointOnCurve.hxx>
#include <GeomAdaptor_Curve.hxx>
#include <GeomAbs_CurveType.hxx>
#include <Geom_Line.hxx>
#include <Geom2d_Curve.hxx>
#include <Geom_BoundedCurve.hxx>
#include <Geom_Geometry.hxx>
#include <Geom_TrimmedCurve.hxx>
#include <Geom2d_TrimmedCurve.hxx>

#include <IntTools_FClass2d.hxx>
#include <IntTools_Curve.hxx>
#include <IntTools_SequenceOfCurves.hxx>

static
  void ParabolaTolerance(const Handle(Geom_Curve)& ,
			 const Standard_Real ,
			 const Standard_Real ,
			 const Standard_Real ,
			 Standard_Real& ,
			 Standard_Real& );

//=======================================================================
//function : HasInternalEdge
//purpose  : 
//=======================================================================
  Standard_Boolean IntTools_Tools::HasInternalEdge(const TopoDS_Wire& aW)
{
  Standard_Boolean bFlag=Standard_True;

  TopExp_Explorer anExp(aW, TopAbs_EDGE);
  for (; anExp.More(); anExp.Next()) {
    const TopoDS_Edge& aE=TopoDS::Edge(anExp.Current());
    TopAbs_Orientation anOr=aE.Orientation();
    if (anOr==TopAbs_INTERNAL) {
      return bFlag;
    }
  }
  return !bFlag;
}

//=======================================================================
//function : IsClosed
//purpose  : 
//=======================================================================
  Standard_Boolean IntTools_Tools::IsClosed (const Handle(Geom_Curve)& aC3D)
{
  Standard_Boolean bRet;
  Standard_Real aF, aL, aDist, aPC;
  gp_Pnt aP1, aP2;
  
  Handle (Geom_BoundedCurve) aGBC=
      Handle (Geom_BoundedCurve)::DownCast(aC3D);
  if (aGBC.IsNull()) {
    return Standard_False;
  }
  
  aF=aC3D->FirstParameter();
  aL=aC3D-> LastParameter();
  
  aC3D->D0(aF, aP1);
  aC3D->D0(aL, aP2);

  
  //
  //modified by NIZNHY-PKV Mon Jul 04 11:58:23 2011f
  aPC=Precision::Confusion();
  aPC=aPC*aPC;
  aDist=aP1.SquareDistance(aP2);
  bRet=aDist<aPC;
  return bRet;
  //
  //aDist=aP1.Distance(aP2);
  //return (aDist < Precision::Confusion()); 
  //modified by NIZNHY-PKV Mon Jul 04 11:59:50 2011t
}

//=======================================================================
//function : RejectLines
//purpose  : 
//=======================================================================
   void IntTools_Tools::RejectLines(const IntTools_SequenceOfCurves& aSIn,
				    IntTools_SequenceOfCurves& aSOut)
{
  Standard_Integer i, j, aNb;
  Standard_Boolean bFlag;
  Handle (Geom_Curve) aC3D;

  gp_Dir aD1, aD2;

  aSOut.Clear();

  aNb=aSIn.Length();
  for (i=1; i<=aNb; i++) {
    const IntTools_Curve& IC=aSIn(i);
    aC3D=IC.Curve();
    //
    Handle (Geom_TrimmedCurve) aGTC=
      Handle (Geom_TrimmedCurve)::DownCast(aC3D);
    
    if (!aGTC.IsNull()) {
      aC3D=aGTC->BasisCurve();
      IntTools_Curve* pIC=(IntTools_Curve*) &IC;
      pIC->SetCurve(aC3D);
    }
    //
    Handle (Geom_Line) aGLine=
      Handle (Geom_Line)::DownCast(aC3D);
    
    if (aGLine.IsNull()) {
      aSOut.Clear();
      for (j=1; j<=aNb; j++) {
	aSOut.Append(aSIn(j));
      }
      return;
    }
    //
    gp_Lin aLin=aGLine->Lin();
    aD2=aLin.Direction();
    if (i==1) {
      aSOut.Append(IC);
      aD1=aD2;
      continue;
    }
    
    bFlag=IntTools_Tools::IsDirsCoinside(aD1, aD2);
    if (!bFlag) {
      aSOut.Append(IC);
      return;
    }
  }
}

//=======================================================================
//function : IsDirsCoinside
//purpose  : 
//=======================================================================
  Standard_Boolean IntTools_Tools::IsDirsCoinside  (const gp_Dir& D1, const gp_Dir& D2)
{
  Standard_Boolean bFlag;
  gp_Pnt P1(D1.X(), D1.Y(), D1.Z());  
  gp_Pnt P2(D2.X(), D2.Y(), D2.Z());
  Standard_Real dLim=0.0002, d;
  d=P1.Distance (P2); 
  bFlag= (d<dLim || fabs (2.-d)<dLim); 
  return bFlag;
}
  
//=======================================================================
//function : IsDirsCoinside
//purpose  : 
//=======================================================================
  Standard_Boolean IntTools_Tools::IsDirsCoinside  (const gp_Dir& D1, 
						    const gp_Dir& D2,
						    const Standard_Real dLim)
{
  Standard_Boolean bFlag;
  Standard_Real d;
  //
  gp_Pnt P1(D1.X(), D1.Y(), D1.Z());  
  gp_Pnt P2(D2.X(), D2.Y(), D2.Z());
  d=P1.Distance (P2); 
  bFlag= (d<dLim || fabs (2.-d)<dLim); 
  return bFlag;
}
//=======================================================================
//function : SplitCurve
//purpose  : 
//=======================================================================
  Standard_Integer IntTools_Tools::SplitCurve(const IntTools_Curve& IC,
					      IntTools_SequenceOfCurves& aCvs)
{
  Handle (Geom_Curve) aC3D =IC.Curve();
  if(aC3D.IsNull())
    return 0;
  //
  Handle (Geom2d_Curve) aC2D1=IC.FirstCurve2d();
  Handle (Geom2d_Curve) aC2D2=IC.SecondCurve2d();
  Standard_Boolean bIsClosed;

  bIsClosed=IntTools_Tools::IsClosed(aC3D);
  if (!bIsClosed) {
    return 0;
  }

  Standard_Real aF, aL, aMid;
  
  //
  aF=aC3D->FirstParameter();
  aL=aC3D->LastParameter();
  aMid=0.5*(aF+aL);
  GeomAdaptor_Curve aGAC(aC3D);
  GeomAbs_CurveType aCT=aGAC.GetType();
  if (aCT==GeomAbs_BSplineCurve ||
      aCT==GeomAbs_BezierCurve) {
    //aMid=0.5*aMid;
    aMid=IntTools_Tools::IntermediatePoint(aF, aL);
  }
  //
  Handle(Geom_Curve) aC3DNewF, aC3DNewL;
  aC3DNewF =new Geom_TrimmedCurve  (aC3D, aF, aMid);
  aC3DNewL =new Geom_TrimmedCurve  (aC3D, aMid, aL);

  //
  Handle (Geom2d_Curve) aC2D1F, aC2D1L, aC2D2F, aC2D2L;
  //
  if(!aC2D1.IsNull()) {
    aC2D1F=new Geom2d_TrimmedCurve (aC2D1, aF, aMid);
    aC2D1L=new Geom2d_TrimmedCurve (aC2D1, aMid, aL);
  }

  if(!aC2D2.IsNull()) {
    aC2D2F=new Geom2d_TrimmedCurve (aC2D2, aF, aMid);
    aC2D2L=new Geom2d_TrimmedCurve (aC2D2, aMid, aL);
  }
  // 
  
  IntTools_Curve aIC1(aC3DNewF, aC2D1F, aC2D2F);
  IntTools_Curve aIC2(aC3DNewL, aC2D1L, aC2D2L);
  //
  aCvs.Append(aIC1);
  //
  aCvs.Append(aIC2);
  //
  return 2;
}

//=======================================================================
//function : IntermediatePoint
//purpose  : 
//=======================================================================
  Standard_Real IntTools_Tools::IntermediatePoint (const Standard_Real aFirst,
						   const Standard_Real aLast)
{
  //define parameter division number as 10*e^(-PI) = 0.43213918
  const Standard_Real PAR_T = 0.43213918;
  Standard_Real aParm;
  aParm=(1.-PAR_T)*aFirst + PAR_T*aLast;
  return aParm;
}

//=======================================================================
//function : IsVertex
//purpose  : 
//=======================================================================
  Standard_Boolean IntTools_Tools::IsVertex (const gp_Pnt& aP,
					     const Standard_Real aTolPV,
					     const TopoDS_Vertex& aV)
{
  Standard_Boolean bRet;
  Standard_Real aTolV, aD, dTol;
  gp_Pnt aPv; 
  
  aTolV=BRep_Tool::Tolerance(aV);
  //
  dTol=Precision::Confusion();
  aTolV=aTolV+aTolPV+dTol;
  //
  aPv=BRep_Tool::Pnt(aV);
  //
  //modified by NIZNHY-PKV Mon Jul 04 12:00:37 2011f
  aD=aPv.SquareDistance(aP);
  aTolV=aTolV*aTolV;
  bRet=(aD<=aTolV);
  return bRet;
  //
  //aD=aPv.Distance(aP);
  //return (aD<=aTolV);
  //modified by NIZNHY-PKV Mon Jul 04 12:00:40 2011t
}


//=======================================================================
//function : IsVertex
//purpose  : 
//=======================================================================
  Standard_Boolean IntTools_Tools::IsVertex (const IntTools_CommonPrt& aCmnPrt)
{
  Standard_Boolean anIsVertex;
  Standard_Real aParam;

  const TopoDS_Edge&    aE1=aCmnPrt.Edge1();
  const IntTools_Range& aR1=aCmnPrt.Range1();
  aParam=0.5*(aR1.First()+aR1.Last());
  anIsVertex=IntTools_Tools::IsVertex (aE1, aParam);
  
  if (anIsVertex) {
    return Standard_True;
  }

  const TopoDS_Edge&    aE2=aCmnPrt.Edge2();
  const IntTools_SequenceOfRanges& aRs2=aCmnPrt.Ranges2();
  const IntTools_Range& aR2=aRs2(1);
  aParam=0.5*(aR2.First()+aR2.Last());
  anIsVertex=IntTools_Tools::IsVertex (aE2, aParam);
  if (anIsVertex) {
    return Standard_True;
  }
  return Standard_False;
}

//=======================================================================
//function : IsVertex
//purpose  : 
//=======================================================================
  Standard_Boolean IntTools_Tools::IsVertex (const TopoDS_Edge& aE,
					     const TopoDS_Vertex& aV,
					     const Standard_Real t)
{
  Standard_Real aTolV, aTolV2, d2;
  gp_Pnt aPv, aPt; 
  
  BRepAdaptor_Curve aBAC(aE);
  aBAC.D0(t, aPt);

  aTolV=BRep_Tool::Tolerance(aV);
  aTolV2=aTolV*aTolV;
  aPv=BRep_Tool::Pnt(aV);
  d2=aPv.SquareDistance (aPt);
  if (d2 < aTolV2) {
    return Standard_True;
  }
  return Standard_False;
}
//=======================================================================
//function : IsVertex
//purpose  : 
//=======================================================================
  Standard_Boolean IntTools_Tools::IsVertex (const TopoDS_Edge& aE,
					     const Standard_Real t)
{
  Standard_Real aTolV, aTolV2, d2;
  TopoDS_Vertex aV;
  gp_Pnt aPv, aPt; 
  
  BRepAdaptor_Curve aBAC(aE);
  aBAC.D0(t, aPt);

  TopExp_Explorer anExp(aE, TopAbs_VERTEX);
  for (; anExp.More(); anExp.Next()) {
    aV=TopoDS::Vertex (anExp.Current());
    aTolV=BRep_Tool::Tolerance(aV);
    aTolV2=aTolV*aTolV;
    aTolV2=1.e-12;
     aPv=BRep_Tool::Pnt(aV);
     d2=aPv.SquareDistance (aPt);
     if (d2 < aTolV2) {
       return Standard_True;
     }
  }
  return Standard_False;
}


//=======================================================================
//function : ComputeVV
//purpose  : 
//=======================================================================
  Standard_Integer IntTools_Tools::ComputeVV(const TopoDS_Vertex& aV1, 
					     const TopoDS_Vertex& aV2)
{
  Standard_Real aTolV1, aTolV2, aTolSum, d;
  gp_Pnt aP1, aP2;

  aTolV1=BRep_Tool::Tolerance(aV1);
  aTolV2=BRep_Tool::Tolerance(aV2);
  aTolSum=aTolV1+aTolV2;
  
  aP1=BRep_Tool::Pnt(aV1);
  aP2=BRep_Tool::Pnt(aV2);
  //modified by NIZNHY-PKV Mon Jul 04 11:55:52 2011f
  aTolSum=aTolSum*aTolSum;
  d=aP1.SquareDistance(aP2);
  //d=aP1.Distance(aP2);
  //modified by NIZNHY-PKV Mon Jul 04 11:55:53 2011t
  if (d<aTolSum) {
    return 0;
  }
  return -1;
}

//=======================================================================
//function : MakeFaceFromWireAndFace
//purpose  : 
//=======================================================================
  void IntTools_Tools::MakeFaceFromWireAndFace(const TopoDS_Wire& aW,
					       const TopoDS_Face& aF,
					       TopoDS_Face& aFNew)
{
  TopoDS_Face aFF;
  aFF=aF;
  aFF.Orientation(TopAbs_FORWARD);
  aFNew=TopoDS::Face (aFF.EmptyCopied());
  BRep_Builder BB;
  BB.Add(aFNew, aW);
}

//=======================================================================
//function : ClassifyPointByFace
//purpose  : 
//=======================================================================
  TopAbs_State IntTools_Tools::ClassifyPointByFace(const TopoDS_Face& aF,
						   const gp_Pnt2d& aP2d)
{
  Standard_Real aFaceTolerance;
  TopAbs_State aState;
  
  aFaceTolerance=BRep_Tool::Tolerance(aF);
  IntTools_FClass2d aClass2d(aF, aFaceTolerance);
  aState=aClass2d.Perform(aP2d);
  
  return aState;
}

//=======================================================================
//function : IsMiddlePointsEqual
//purpose  : 
//=======================================================================
  Standard_Boolean IntTools_Tools::IsMiddlePointsEqual(const TopoDS_Edge& aE1,
						       const TopoDS_Edge& aE2)
						       
{
  Standard_Boolean bRet;
  Standard_Real f1, l1, m1, f2, l2, m2, aTol1, aTol2, aSumTol, aD2;
  gp_Pnt aP1, aP2;

  aTol1=BRep_Tool::Tolerance(aE1);
  Handle(Geom_Curve) C1=BRep_Tool::Curve(aE1, f1, l1);
  m1=0.5*(f1+l1);
  C1->D0(m1, aP1);

  aTol2=BRep_Tool::Tolerance(aE2);
  Handle(Geom_Curve) C2=BRep_Tool::Curve(aE2, f2, l2);
  m2=0.5*(f2+l2);
  C2->D0(m2, aP2);

  aSumTol=aTol1+aTol2;
  //modified by NIZNHY-PKV Mon Jul 04 12:02:20 2011f
  aSumTol=aSumTol*aSumTol;
  aD2=aP1.SquareDistance(aP2);
  bRet=aD2<aSumTol;
  return bRet;
  //
  //if (aP1.Distance(aP2) < aSumTol) {
  //  return Standard_True;
  //}
  //return Standard_False;
  //modified by NIZNHY-PKV Mon Jul 04 12:02:24 2011t
}

//=======================================================================
//function : CurveTolerance
//purpose  : 
//=======================================================================
  Standard_Real IntTools_Tools::CurveTolerance(const Handle(Geom_Curve)& aC3D,
					       const Standard_Real aTolBase)
{
  Standard_Real aTolReached, aTf, aTl, aTolMin, aTolMax;

  aTolReached=aTolBase;
  //
  if (aC3D.IsNull()) {
    return aTolReached;
  }
  //
  Handle(Geom_TrimmedCurve) aCT3D=Handle(Geom_TrimmedCurve)::DownCast(aC3D);
  if (aCT3D.IsNull()) {
    return aTolReached;
  }
  //
  aTolMin=aTolBase;
  aTolMax=aTolBase;
  //
  aTf=aCT3D->FirstParameter();
  aTl=aCT3D->LastParameter();
  //
  GeomAdaptor_Curve aGAC(aCT3D);
  GeomAbs_CurveType aCType=aGAC.GetType();
  //
  if (aCType==GeomAbs_Parabola) {
    Handle(Geom_Curve) aC3DBase=aCT3D->BasisCurve();
    ParabolaTolerance(aC3DBase, aTf, aTl, aTolBase, aTolMin, aTolMax);
    aTolReached=aTolMax;
  }
  //
  return aTolReached;
}

#include <Geom_Parabola.hxx>
#include <gp_Parab.hxx>
//=======================================================================
//function : ParabolaTolerance
//purpose  : 
//=======================================================================
void ParabolaTolerance(const Handle(Geom_Curve)& aC3D,
		       const Standard_Real aTf,
		       const Standard_Real aTl,
		       const Standard_Real aTol,
		       Standard_Real& aTolMin,
		       Standard_Real& aTolMax)
{
  
  aTolMin=aTol;
  aTolMax=aTol;

  Handle(Geom_Parabola) aGP=Handle(Geom_Parabola)::DownCast(aC3D);
  if (aGP.IsNull()){
    return;
  }

  Standard_Integer aNbPoints;
  Standard_Real aFocal, aX1, aX2, aTol1, aTol2;
  gp_Pnt aPf, aPl;
  gp_Parab aParab=aGP->Parab();
  gp_Ax1 aXAxis=aParab.XAxis();
  Handle(Geom_Line) aGAxis=new Geom_Line(aXAxis);

  aFocal=aGP->Focal();
  if (aFocal==0.) {
    return;
  }
  //
  // aTol1
  aTol1=aTol;
  aX1=0.;
  aGP->D0(aTf, aPf);
  GeomAPI_ProjectPointOnCurve aProj1(aPf, aGAxis);
  aNbPoints=aProj1.NbPoints();
  if (aNbPoints) {
    aX1=aProj1.LowerDistanceParameter();
  }
  if (aX1>=0.) {
    aTol1=aTol*sqrt(0.5*aX1/aFocal);
  }
  if (aTol1==0.) {
    aTol1=aTol;
  }
  //
  // aTol2
  aTol2=aTol;
  aX2=0.;
  aGP->D0(aTl, aPl);
  GeomAPI_ProjectPointOnCurve aProj2(aPl, aGAxis);
  aNbPoints=aProj2.NbPoints();
  if (aNbPoints) {
    aX2=aProj2.LowerDistanceParameter();
  }
  
  if (aX2>=0.) {
    aTol2=aTol*sqrt(0.5*aX2/aFocal);
  }
  if (aTol2==0.) {
    aTol2=aTol;
  }
  //
  aTolMax=(aTol1>aTol2) ? aTol1 : aTol2;
  aTolMin=(aTol1<aTol2) ? aTol1 : aTol2;
}