summaryrefslogtreecommitdiff
path: root/src/BOP/BOP_WireEdgeClassifier.cxx
blob: d3b86c1ae0ecd14745d89a39e9ccbdab8906b847 (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
// File:	BOP_WireEdgeClassifier.cxx
// Created:	Thu Jun 17 18:52:50 1993
// Author:	Jean Yves LEBEY
//		<jyl@zerox>
//     modified by PKV
// ... and nobody played synthesizer .

//  Modified by skv - Thu Jul  6 12:01:17 2006 OCC12627

#include <BOP_WireEdgeClassifier.ixx>

#include <Standard_ProgramError.hxx>

#include <Precision.hxx>

#include <gp_Pnt.hxx>
#include <gp_Vec2d.hxx>

#include <Geom2d_Curve.hxx>
#include <Geom_Curve.hxx>
#include <Geom_Surface.hxx>
#include <Geom_TrimmedCurve.hxx>

#include <GeomAdaptor_Surface.hxx>

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

#include <TopLoc_Location.hxx>

#include <TopExp.hxx>
#include <TopExp_Explorer.hxx>
#include <TopTools_IndexedMapOfShape.hxx>

#include <BRep_Tool.hxx>
#include <BRepClass_FaceClassifier.hxx>
#include <BRep_Builder.hxx>
#include <BRepAdaptor_Curve2d.hxx>

#include <IntTools_Tools.hxx>

#include <BOPTools_Tools2D.hxx>
#include <BOPTools_Tools3D.hxx>
#include <IntTools_FClass2d.hxx>



static
  Standard_Real MinStepIn2d(const TopoDS_Edge& aE,
			    const TopoDS_Face& aF);

//=======================================================================
//function : BOP_WireEdgeClassifier::BOP_WireEdgeClassifier
//purpose  : 
//=======================================================================
  BOP_WireEdgeClassifier::BOP_WireEdgeClassifier (const TopoDS_Face& F,
						  const BOP_BlockBuilder& BB)
:
  BOP_CompositeClassifier(BB)
{
  myBCEdge.Face() = F;
}

//=======================================================================
//function : Compare
//purpose  : 
//=======================================================================
  TopAbs_State BOP_WireEdgeClassifier::Compare (const Handle(BOP_Loop)& L1,
						const Handle(BOP_Loop)& L2)
{ 
  TopAbs_State state = TopAbs_UNKNOWN;

  Standard_Boolean isshape1, isshape2, yena1 ;

  isshape1 = L1->IsShape();
  isshape2 = L2->IsShape();

  if      ( isshape2 && isshape1 )  { 
    // L1 is Shape , L2 is Shape
    const TopoDS_Shape& s1 = L1->Shape();
    const TopoDS_Shape& s2 = L2->Shape();
    state = CompareShapes(s1,s2);
  }

  else if ( isshape2 && !isshape1 ) { 
    // L1 is Block , L2 is Shape
    BOP_BlockIterator Bit1 = L1->BlockIterator();
    Bit1.Initialize();
    yena1 = Bit1.More();
    while (yena1) {
      const TopoDS_Shape& s1 = 
	((BOP_BlockBuilder*)myBlockBuilder)->Element(Bit1);
      const TopoDS_Shape& s2 = L2->Shape();
      state = CompareElementToShape(s1,s2);
      yena1 = Standard_False;
      if (state == TopAbs_UNKNOWN) { 
	if (Bit1.More()) Bit1.Next();
	yena1 = Bit1.More();
      }
    }
  }
  
  else if ( !isshape2 && isshape1 ) { 
    // L1 is Shape , L2 is Block
    const TopoDS_Shape& s1 = L1->Shape();
    ResetShape(s1);
    BOP_BlockIterator Bit2 = L2->BlockIterator();
    for (Bit2.Initialize(); Bit2.More(); Bit2.Next()) {
      const TopoDS_Shape& s2 = 
	((BOP_BlockBuilder*)myBlockBuilder)->Element(Bit2);
      CompareElement(s2);
    }
    state = State();
  }
  else if ( !isshape2 && !isshape1 ) { 
    // L1 is Block , L2 is Block
    if (state == TopAbs_UNKNOWN) {      
      BOP_BlockIterator Bit1 = L1->BlockIterator();
      Bit1.Initialize();
      yena1 = Bit1.More();
      while (yena1) {
	const TopoDS_Shape& s1 = 
	  ((BOP_BlockBuilder*)myBlockBuilder)->Element(Bit1);
	ResetElement(s1);
	BOP_BlockIterator Bit2 = L2->BlockIterator();
	for (Bit2.Initialize(); Bit2.More(); Bit2.Next()) {
	  const TopoDS_Shape& s2 = 
	    ((BOP_BlockBuilder*)myBlockBuilder)->Element(Bit2);
	  CompareElement(s2);
	}
	state = State();
	yena1 = Standard_False;
	if (state == TopAbs_UNKNOWN) { 
	  if (Bit1.More()) Bit1.Next();
	  yena1 = Bit1.More();
	}
      }
    } // if (state == TopAbs_UNKNOWN)

    if (state == TopAbs_UNKNOWN) {
      //
      TopoDS_Shape s1 = LoopToShape(L1); 
      if (s1.IsNull()) {
	return state;
      }
      TopoDS_Shape s2 = LoopToShape(L2); 
      if (s2.IsNull()) {
	return state;
      }
    } // if (state == TopAbs_UNKNOWN)

  } // else if ( !isshape2 && !isshape1 )
  return state;
}

//=======================================================================
//function : LoopToShape
//purpose  : 
//=======================================================================
  TopoDS_Shape BOP_WireEdgeClassifier::LoopToShape(const Handle(BOP_Loop)& L)
{
  myShape.Nullify();

  BOP_BlockIterator Bit = L->BlockIterator();
  Bit.Initialize();
  if ( !Bit.More() ) {
    return myShape;
  }

  TopoDS_Shape aLocalShape = myBCEdge.Face();
  const TopoDS_Face& F1 = TopoDS::Face(aLocalShape);
  // 
  aLocalShape = F1.EmptyCopied();
  TopoDS_Face F = TopoDS::Face(aLocalShape);
  //
  BRep_Builder BB; 
  TopoDS_Wire W; 
  BB.MakeWire(W);

  Standard_Real tolE, f,l,tolpc, tol;
  Standard_Boolean haspc ;

  for  (; Bit.More(); Bit.Next()) {
    const TopoDS_Edge& E = 
      TopoDS::Edge(((BOP_BlockBuilder*)myBlockBuilder)->Element(Bit));
    
    tolE = BRep_Tool::Tolerance(E);
    
    haspc = BOPTools_Tools2D::HasCurveOnSurface (E, F);
    if (!haspc) {
      Handle(Geom2d_Curve) C2D;
      BOPTools_Tools2D::CurveOnSurface (E, F, C2D, f, l, tolpc, Standard_False);

      if (!C2D.IsNull()) {
	tol = Max(tolpc,tolE);
	BB.UpdateEdge(E,C2D,F,tol);
      }
    }
    BB.Add(W,E);
  }
  BB.Add(F, W);

  myShape = F;
  return myShape;
}

//=======================================================================
//function : CompareShapes
//purpose  : 
//=======================================================================
  TopAbs_State  BOP_WireEdgeClassifier::CompareShapes (const TopoDS_Shape& B1, 
						       const TopoDS_Shape& B2)
{
  Standard_Boolean bBothWires;
  TopAbs_State aSt;
  TopExp_Explorer anExp1;
  
  aSt = TopAbs_UNKNOWN;
  bBothWires= (B2.ShapeType()==TopAbs_WIRE) && (B1.ShapeType()==TopAbs_WIRE);

  anExp1.Init (B1, TopAbs_EDGE);
  if ( !anExp1.More() ) {
    return aSt;
  }
  //
  for (; anExp1.More(); anExp1.Next() ) {
    const TopoDS_Edge& anE1 = TopoDS::Edge(anExp1.Current());
    ResetShape(anE1);
    //
    TopExp_Explorer anExp2(B2, TopAbs_EDGE);
    for(; anExp2.More(); anExp2.Next()) {
      const TopoDS_Edge& anE2 = TopoDS::Edge(anExp2.Current());
      //
      if(anE2.IsSame(anE1)) {
	//const TopoDS_Face& aF=myBCEdge.Face();
	if (bBothWires) {
	  aSt = TopAbs_OUT;
	  return aSt;
	}
      }
    } 
  }
  //
  TopoDS_Face aFN1, aFN2;
  gp_Pnt2d aP2DNear;
  gp_Pnt   aPNear;
  
  const TopoDS_Face& theFace =  myBCEdge.Face();
  const TopoDS_Wire& aW1=TopoDS::Wire(B1);
  const TopoDS_Wire& aW2=TopoDS::Wire(B2);
 
  IntTools_Tools::MakeFaceFromWireAndFace (aW1, theFace, aFN1);  
  IntTools_Tools::MakeFaceFromWireAndFace (aW2, theFace, aFN2);  
  
  anExp1.Init (aFN1, TopAbs_EDGE);
  if(anExp1.More()){
    const TopoDS_Edge& aE1=TopoDS::Edge(anExp1.Current());
    //
    // The block that choises step in 2D taking into account
    // tolerances of the Face and Edge on the Face in order to
    // obtain 2D point exactly INside of the Face
    //                             pkv207/A5
    //                             PKV Thu Jun 13 09:09:53 2002
    {
      Standard_Real aT, aT1, aT2, dt2D;

      BRep_Tool::Range(aE1, aT1, aT2);
      aT=BOPTools_Tools2D::IntermediatePoint(aT1, aT2);
      //
      TopoDS_Face aFF=aFN1;
      TopoDS_Edge aERight;
      
      aFF.Orientation(TopAbs_FORWARD);
      BOPTools_Tools3D::OrientEdgeOnFace (aE1, aFF, aERight);
      
      dt2D=MinStepIn2d(aERight, aFF);
      BOPTools_Tools3D::PointNearEdge (aERight, aFF, aT, dt2D, aP2DNear, aPNear);
    }
    
    //
//  Modified by skv - Thu Jul  6 12:01:17 2006 OCC12627 Begin
//     aSt=IntTools_Tools::ClassifyPointByFace (aFN2, aP2DNear);
    Standard_Real     aTol = Precision::PConfusion();
    IntTools_FClass2d aClass2d(aFN2, aTol);

    aSt = aClass2d.Perform(aP2DNear);
//  Modified by skv - Thu Jul  6 12:01:19 2006 OCC12627 End
    
    return aSt;
  }
  return aSt;
}


//=======================================================================
//function : CompareElementToShape
//purpose  : 
//=======================================================================
  TopAbs_State  BOP_WireEdgeClassifier::CompareElementToShape (const TopoDS_Shape& EE,
							       const TopoDS_Shape& B)
{
  // isEdge : edge E inits myPoint2d 
  ResetElement(EE);
  TopExp_Explorer Ex;
  Ex.Init(B,TopAbs_EDGE);
  for(; Ex.More(); Ex.Next()) {
    const TopoDS_Shape& E = Ex.Current();
    CompareElement(E);
  }
  TopAbs_State state = State();
  return state;
}

//=======================================================================
//function : ResetShape
//purpose  : 
//=======================================================================
  void  BOP_WireEdgeClassifier::ResetShape(const TopoDS_Shape& B)
{
  if (B.ShapeType() == TopAbs_EDGE) {
    ResetElement(B);
  }

  else {
    TopExp_Explorer ex(B,TopAbs_EDGE);
    if (ex.More())  {
      const TopoDS_Shape& E = ex.Current();
      ResetElement(E);
    }
  }
}
//=======================================================================
//function : ResetElement
//purpose  : 
//=======================================================================
  void  BOP_WireEdgeClassifier::ResetElement(const TopoDS_Shape& EE)
{
  const TopoDS_Edge& E = TopoDS::Edge(EE);
  const TopoDS_Face& F = myBCEdge.Face();

  Standard_Boolean haspc;
  Standard_Real f2, l2, tolpc, tolE, tol, par ;
  Handle(Geom2d_Curve) C2D;
  BRep_Builder BB; 

  haspc = BOPTools_Tools2D::HasCurveOnSurface(E, F); 
  if (!haspc) { 
    BOPTools_Tools2D::CurveOnSurface(E, F, C2D, f2, l2, tolpc, Standard_True);
    tolE = BRep_Tool::Tolerance(E); 
    tol = Max(tolE,tolpc); 
    BB.UpdateEdge(E,C2D,F,tol); 
  } 
  
  
  BOPTools_Tools2D::CurveOnSurface(E, F, C2D, f2, l2, tolpc, Standard_False);
  if (C2D.IsNull()) {
    Standard_ProgramError::Raise("WEC : ResetElement");
  }

  par = BOPTools_Tools2D::IntermediatePoint (f2, l2);
  myPoint2d = C2D->Value(par);
  
  myFirstCompare = Standard_True;
}


//=======================================================================
//function : CompareElement
//purpose  : 
//=======================================================================
  void BOP_WireEdgeClassifier::CompareElement(const TopoDS_Shape& EE)
{
  const TopoDS_Edge& E = TopoDS::Edge(EE);
  const TopoDS_Face& F = myBCEdge.Face();
  
  Standard_Real f2, l2, tolpc, tolE, tol, par, dist, tol2d;
  Standard_Boolean haspc;

  if (myFirstCompare) {
    
    Handle(Geom2d_Curve) C2D;
    BRep_Builder BB; 

    haspc = BOPTools_Tools2D::HasCurveOnSurface(E, F); 
    if (!haspc) { 
      BOPTools_Tools2D::CurveOnSurface(E, F, C2D, f2, l2, tolpc, Standard_True); 
      tolE = BRep_Tool::Tolerance(E); 
      tol = Max(tolE, tolpc); 
      BB.UpdateEdge(E,C2D,F,tol); 
    } 

    BOPTools_Tools2D::CurveOnSurface(E, F, C2D, f2, l2, tolpc, Standard_False);
    
    par = BOPTools_Tools2D::IntermediatePoint (f2, l2);
 
    gp_Pnt2d p2d = C2D->Value(par);
    
    gp_Vec2d v2d(myPoint2d,p2d);
    gp_Lin2d l2d(myPoint2d,v2d);
    dist  = myPoint2d.Distance(p2d);
    tol2d = Precision::PConfusion(); 

    myFPC.Reset(l2d,dist,tol2d);
    myFirstCompare = Standard_False;
    
  } // end of if (myFirstCompare)
  
  myBCEdge.Edge() = E;
  TopAbs_Orientation Eori = E.Orientation();
  myFPC.Compare(myBCEdge,Eori);
  //ZZ TopAbs_State state = myFPC.State();
}


//=======================================================================
//function : State
//purpose  : 
//=======================================================================
  TopAbs_State BOP_WireEdgeClassifier::State()
{
  TopAbs_State state = myFPC.State();
  return state;
}

//=======================================================================
//function : MinStepIn2d
//purpose  : 
//=======================================================================
Standard_Real MinStepIn2d(const TopoDS_Edge& aE,
			  const TopoDS_Face& aF)
{
  Standard_Real aDt2dMin, aDt2D, aTolE, aURes, aVRes;
  //
  aDt2dMin=1.e-5;
  //
  aTolE=BRep_Tool::Tolerance(aE);
  //
  Handle(Geom_Surface) aS=BRep_Tool::Surface(aF);
  GeomAdaptor_Surface aGAS(aS);
  if (aGAS.GetType()==GeomAbs_Plane) {
    aURes=aGAS.UResolution(aTolE);
    aVRes=aGAS.VResolution(aTolE);
    aDt2D=(aURes>aVRes) ? aURes : aVRes;
    //
    if (aDt2D > aDt2dMin) {
      aDt2D=aDt2dMin;
    }
  }
  else {
    aDt2D=aDt2dMin;
  }
  return aDt2D;
}