summaryrefslogtreecommitdiff
path: root/src/BRepClass3d/BRepClass3d_SClassifier.cxx
blob: 675bf81b33ed4470c507ad1f8adf0a7f0dcdffa6 (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
// File:	BRepClass3d_SClassifier.cxx
// Created:	Mon Jul 15 17:26:21 1996
// Author:	Laurent BUCHARD
//		<lbr@cracbox.paris1.matra-dtv.fr>


//  Modified by skv - Thu Sep  4 11:22:05 2003 OCC578

#include <BRepClass3d_SClassifier.ixx>

#include <gp_Pnt.hxx>
#include <gp_Lin.hxx>
#include <gp_Vec.hxx>
#include <BRepClass3d_Intersector3d.hxx>
#include <TopoDS.hxx>

#include <IntCurvesFace_Intersector.hxx>
// modified by NIZHNY-MKK  Mon Jun 21 15:13:40 2004
#include <Precision.hxx>
#include <ElCLib.hxx>
#include <Geom_Surface.hxx>
#include <BRep_Tool.hxx>

static
  void FaceNormal (const TopoDS_Face& aF,
		   const Standard_Real U,
		   const Standard_Real V,
		   gp_Dir& aDN);

static 
  Standard_Real GetAddToParam(const gp_Lin& L,const Standard_Real P,const Bnd_Box& B);



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


//=======================================================================
//function : BRepClass3d_SClassifier
//purpose  : 
//=======================================================================
BRepClass3d_SClassifier::BRepClass3d_SClassifier(BRepClass3d_SolidExplorer& S,
						 const gp_Pnt&  P,
						 const Standard_Real Tol) { 
  if(S.Reject(P)) { 
    myState=3; //-- in ds le cas solide sans face 
  }
  else { 
    Perform(S,P,Tol);
  }
}


//=======================================================================
//function : PerformInfinitePoint
//purpose  : 
//=======================================================================
void BRepClass3d_SClassifier::PerformInfinitePoint(BRepClass3d_SolidExplorer& aSE,
						   const Standard_Real /*Tol*/) {
  //-- Idee : On prend un point A dans la face1 et un point B dans la face B 
  //-- ( si on a une seule face , on prend 2 points dans la meme face.)
  //-- 
  //-- On intersecte la droite AB avec le solide et on s interesse a la transition du 
  //-- premier point. Si le solide a une seule face et que la droite AB ne le coupe pas 
  //-- on ne peut pas decider.

  if(aSE.Reject(gp_Pnt(0,0,0))) { 
    myState=3; //-- in ds le cas solide sans face 
    return;
  }
  //
  //------------------------------------------------------------
  // 1
  Standard_Boolean bFound, bFlag;
  Standard_Integer nump;
  Standard_Real aParam, aU1, aV1, aU2, aV2;
  gp_Pnt A,B;
  gp_Dir aDN1, aDN2;
  TopoDS_Face aF, aF1, aF2;
  //
  nump = 0;
  aParam = 0.5;
  myFace.Nullify();
  myState=2; 
  for(aSE.InitShell(); aSE.MoreShell() && nump<2;  aSE.NextShell()) { 
    for(aSE.InitFace();	aSE.MoreFace()  && nump<2; ) {
      TopoDS_Shape aLocalShape = aSE.CurrentFace();
      aF = TopoDS::Face(aLocalShape);
      aSE.NextFace();
      if(!nump) { 
	nump++;
	bFound=aSE.FindAPointInTheFace(aF, A, aU1, aV1, aParam);
	if (!bFound) {
	  return;
	}
	aF1=aF;
	if(!aSE.MoreFace()) { 
	  nump++;
	  bFound=aSE.FindAPointInTheFace(aF, B, aU2, aV2, aParam);
	  if (!bFound) {
	    return;
	  }
	  aF2=aF;
	}
      }// if(nump==0) {    
      else if(nump==1) { 
	bFound=aSE.FindAPointInTheFace(aF, B, aU2, aV2, aParam);
	if(!bFound) { 
	  return;
	} 
	aF2=aF;
	nump++;
      }
    }// for(aSE.InitFace();	aSE.MoreFace()  && nump<2; ) {
  }// for(aSE.InitShell(); aSE.MoreShell() && nump<2;  aSE.NextShell()) { 
  //
  //------------------------------------------------------------
  // 2
  Standard_Integer cpasbon;
  Standard_Real parmin, aD2, aSP;
  IntCurveSurface_TransitionOnCurve aTC;    
  TopAbs_State aState;
  //
  parmin = RealLast();
  //
  bFlag=Standard_False;
  if (aF1!=aF2) {
    FaceNormal(aF1, aU1, aV1, aDN1);
    FaceNormal(aF2, aU2, aV2, aDN2);
    aSP=1.-aDN1*aDN2;
    if (aSP < 1.e-5) {
      bFlag=!bFlag;
    }
  }
  //
  aD2=A.SquareDistance(B);
  if(aD2<0.000001 || bFlag) { 
    B.SetCoord(A.X()+1,A.Y()+1,A.Z()+1);
  }
  //
  cpasbon = 0;
  gp_Vec AB(A,B);
  //
  do { 
    switch (cpasbon) 
      {
      case 1 : AB.SetX(-AB.X());break;
      case 2 : AB.SetY(-AB.Y());break;
      case 3 : AB.SetZ(-AB.Z());break;
      case 4 : AB.SetY(-AB.Y());break;
      case 5 : AB.SetX(-AB.X());break;
      }
    gp_Lin L(A,gp_Dir(AB));    
    //-- cout<<"\npoint A "<<A.X()<<" "<<A.Y()<<" "<<A.Z()<<endl;
    //-- cout<<"\npoint B "<<B.X()<<" "<<B.Y()<<" "<<B.Z()<<endl;
    for(aSE.InitShell();aSE.MoreShell();aSE.NextShell()) { 
      if(aSE.RejectShell(L) == Standard_False) { 
	for(aSE.InitFace();aSE.MoreFace(); aSE.NextFace()) {
	  if(aSE.RejectFace(L) == Standard_False) { 
	    TopoDS_Shape aLocalShape = aSE.CurrentFace();
	    TopoDS_Face f = TopoDS::Face(aLocalShape);
	    IntCurvesFace_Intersector& Intersector3d = aSE.Intersector(f);
	    Intersector3d.Perform(L,-RealLast(),parmin); 

	    if(Intersector3d.IsDone()) { 
	      if(Intersector3d.NbPnt()) { 
		if(Intersector3d.WParameter(1) < parmin) {
		  aState=Intersector3d.State(1);
		  parmin = Intersector3d.WParameter(1);
		  if(aState==TopAbs_IN || aState==TopAbs_ON) { 
		    aTC=Intersector3d.Transition(1);
		    //-- The intersection point between the line and a face F 
		    // -- of the solid is in the face F 
		    if(aTC == IntCurveSurface_Out) { 
		      //-- The line is going from inside the solid to outside 
		      //-- the solid.
		      myState = 3; //-- IN --
		    }
		    else if(aTC == IntCurveSurface_In) { 
		      myState = 4; //-- OUT --
		    }
		    myFace  = f;
		  }
		  /*
		  else if(Intersector3d.State(1)==TopAbs_ON)  {
		    //-- The intersection point between the line and a face F 
		    //-- of the solid is in the face F 
		    if(Intersector3d.Transition(1) == IntCurveSurface_Out) { 
		      //-- The line is going from inside the solid to outside 
		      //-- the solid.
		      myState = 3; //-- IN --
		    }
		    else if(Intersector3d.Transition(1) == IntCurveSurface_In) { 
		      myState = 4; //-- OUT --
		    }
		    //-- myState = 2;
		    myFace  = f;
		  }
		  */
		}
		
		else { 
		  //-- No point has been found by the Intersector3d.
		  //-- Or a Point has been found with a greater parameter.
		}
	      }
	    }
	  }
	} //-- Exploration of the faces
      } //-- Shell has not been rejected
      else { 
	myState=1; 
      }
    } //-- Exploration of the shells
    cpasbon++;
  }
  while(cpasbon!=0 && cpasbon<5);
}
//=======================================================================
//function : Perform
//purpose  : 
//=======================================================================
void BRepClass3d_SClassifier::Perform(BRepClass3d_SolidExplorer& SolidExplorer,
				      const gp_Pnt&  P,
				      const Standard_Real Tol) 
{ 


  if(SolidExplorer.Reject(P)) { 
    myState=3; //-- in ds le cas solide sans face 
    return;
  }


  myFace.Nullify();
  myState = 0;
  if(SolidExplorer.Reject(P) == Standard_False) { 
    gp_Lin L;
    Standard_Real Par;
    //-- We compute the intersection betwwen the line builded in the Solid Explorer
    //-- and the shape.

    //-- --------------------------------------------------------------------------------
    //-- ON calcule l intersection avec la face la plus proche au sens des boites 
    //-- englobantes en priorite de facon a avoir un parmin le plus faible possible. 
    //-- optimisation pour assurer le plus de rejections possibles avec les autres 
    //-- faces. 
    Standard_Integer iFlag;
    //

    //  Modified by skv - Thu Sep  4 11:22:05 2003 OCC578 Begin
    //  If found line passes through a bound of any face, it means that the line
    //  is not found properly and it is necessary to repeat whole procedure.
    //  That's why the main loop while is added.
    Standard_Boolean isFaultyLine = Standard_True;
    Standard_Integer anIndFace    = 0;
    Standard_Real    parmin;

    while (isFaultyLine) {
      if (anIndFace == 0) {
	iFlag = SolidExplorer.Segment(P,L,Par);
      } else {
	iFlag = SolidExplorer.OtherSegment(P,L,Par);
      }

      Standard_Integer aCurInd = SolidExplorer.GetFaceSegmentIndex();

      if (aCurInd > anIndFace) {
	anIndFace = aCurInd;
      } else {
	myState = 1;

	return;
      }
      //  Modified by skv - Thu Sep  4 11:22:10 2003 OCC578 End

      if (iFlag==1) {
	// IsOnFace
	// iFlag==1 i.e face is Infinite
	myState=2; 

	return;
      }
      //SolidExplorer.Segment(P,L,Par);
      //
      //process results from uncorrected shells
      //
      //if(Par > 1.e+100 && L.Direction().IsParallel(gp_Dir(0.,0.,1.),1.e-8)) {
      if (iFlag==2) {
	myState = 4;
	return;
      }
      //-- BRepClass3d_Intersector3d Intersector3d;
    
      //  Modified by skv - Thu Sep  4 13:48:27 2003 OCC578 Begin
      // Check if the point is ON surface but OUT of the face. 
      // Just skip this face because it is bad for classification.
      if (iFlag == 3)
	continue;

      isFaultyLine = Standard_False;
//       Standard_Real parmin = RealLast();

//       for(SolidExplorer.InitShell();
// 	  SolidExplorer.MoreShell();
// 	  SolidExplorer.NextShell()) { 
      parmin = RealLast();

      for(SolidExplorer.InitShell();
	  SolidExplorer.MoreShell() && !isFaultyLine;
	  SolidExplorer.NextShell()) { 
//  Modified by skv - Thu Sep  4 13:48:27 2003 OCC578 End

	if(SolidExplorer.RejectShell(L) == Standard_False) { 

//  Modified by skv - Thu Sep  4 13:48:27 2003 OCC578 Begin
// 	  for(SolidExplorer.InitFace(); 
// 	      SolidExplorer.MoreFace(); 
// 	      SolidExplorer.NextFace()) {
	  for(SolidExplorer.InitFace(); 
	      SolidExplorer.MoreFace() && !isFaultyLine; 
	      SolidExplorer.NextFace()) {
//  Modified by skv - Thu Sep  4 13:48:27 2003 OCC578 End
	  
	    if(SolidExplorer.RejectFace(L) == Standard_False) { 
	    
	      //-- Intersector3d.Perform(L,Par,Tol,SolidExplorer.CurrentFace());
	      TopoDS_Shape aLocalShape = SolidExplorer.CurrentFace();
	      TopoDS_Face f = TopoDS::Face(aLocalShape);
	      //	    TopoDS_Face f = TopoDS::Face(SolidExplorer.CurrentFace());
	      IntCurvesFace_Intersector& Intersector3d = SolidExplorer.Intersector(f);

	      // MSV Oct 25, 2001: prolong segment, since there are cases when
	      // the intersector does not find intersection points with the original
	      // segment due to rough triangulation of a parametrized surface
	      Standard_Real addW = Max(10*Tol, 0.01*Par);
              Standard_Real AddW = addW;

	      Bnd_Box aBoxF = Intersector3d.Bounding();

              // MSV 23.09.2004: the box must be finite in order to
              // correctly prolong the segment to its bounds
              if (!aBoxF.IsVoid() && !aBoxF.IsWhole()) {
                Standard_Real aXmin, aYmin, aZmin, aXmax, aYmax, aZmax;
                aBoxF.Get(aXmin, aYmin, aZmin, aXmax, aYmax, aZmax);

                Standard_Real boxaddW = GetAddToParam(L,Par,aBoxF);
                addW = Max(addW,boxaddW);
              }

	      Standard_Real minW = -AddW;//-addW;
	      Standard_Real maxW = Min(Par*10,Par+addW);//Par+addW;
              //cout << "range [" << minW << "," << maxW << "]" << endl << endl;
	      Intersector3d.Perform(L,minW,maxW);
	      //Intersector3d.Perform(L,-Tol,Par+10.0*Tol);
	      if(Intersector3d.IsDone()) { 
		Standard_Integer i;
		for (i=1; i <= Intersector3d.NbPnt(); i++) { 
		  if(Abs(Intersector3d.WParameter(i)) < Abs(parmin)) {
 
		    parmin = Intersector3d.WParameter(i);
		    //  Modified by skv - Thu Sep  4 12:46:32 2003 OCC578 Begin
		    TopAbs_State aState = Intersector3d.State(i);
		    //  Modified by skv - Thu Sep  4 12:46:33 2003 OCC578 End
		    if(Abs(parmin)<=Tol) { 
		      myState = 2;
		      myFace  = f;
		    }
		    //  Modified by skv - Thu Sep  4 12:46:32 2003 OCC578 Begin
		    //  Treatment of case TopAbs_ON separately.

		    else if(aState==TopAbs_IN) { 
		    //  Modified by skv - Thu Sep  4 12:46:32 2003 OCC578 End

		      //-- The intersection point between the line and a face F 
		      // -- of the solid is in the face F 

		      IntCurveSurface_TransitionOnCurve tran = Intersector3d.Transition(i);
		      if (tran == IntCurveSurface_Tangent) {
#ifdef DEB
			cout<<"*Probleme ds BRepClass3d_SClassifier.cxx"<<endl;
#endif
			continue; // ignore this point
		      }
		      // if parmin is negative we should reverse transition
		      if (parmin < 0)
			tran = (tran == IntCurveSurface_Out 
				? IntCurveSurface_In : IntCurveSurface_Out);
		      if(tran == IntCurveSurface_Out) { 
			//-- The line is going from inside the solid to outside 
			//-- the solid.
			myState = 3; //-- IN --
		      }
		      else /* if(tran == IntCurveSurface_In) */ { 
			myState = 4; //-- OUT --
		      }
		      myFace  = f;
		    }
		    //  Modified by skv - Thu Sep  4 12:48:50 2003 OCC578 Begin
		    // If the state is TopAbs_ON, it is necessary to chose
		    // another line and to repeat the whole procedure.
		    else if(aState==TopAbs_ON) {
		      isFaultyLine = Standard_True;

		      break;
		    }
		    //  Modified by skv - Thu Sep  4 12:48:50 2003 OCC578 End
		  }
		  else { 
		    //-- No point has been found by the Intersector3d.
		    //-- Or a Point has been found with a greater parameter.
		  }
		} //-- loop by intersection points
	      } //-- Face has not been rejected
	      else { 
		myState = 1;
	      }
	    }
	  } //-- Exploration of the faces
	} //-- Shell has not been rejected
	else { 
	  myState=1; 
	}
      } //-- Exploration of the shells

      //  Modified by skv - Thu Sep  4 11:42:03 2003 OCC578 Begin
      // The end of main loop.
    }
    //  Modified by skv - Thu Sep  4 11:42:03 2003 OCC578 End

#ifdef DEB
    //#################################################
    SolidExplorer.DumpSegment(P,L,parmin,State());
    //#################################################
#endif

  } //-- Solid has not been rejected
  else { 
    myState = 1;
  }
}


TopAbs_State BRepClass3d_SClassifier::State() const { 
  if(myState==2)  return(TopAbs_ON);
  if(myState==4)        return(TopAbs_OUT);          //--
  else if(myState==3)   return(TopAbs_IN);           //-- 
  return(TopAbs_OUT);             
}

TopoDS_Face BRepClass3d_SClassifier::Face() const {  
  return(myFace);
}

Standard_Boolean BRepClass3d_SClassifier::Rejected() const { 
  return(myState==1); 
}

  
Standard_Boolean BRepClass3d_SClassifier::IsOnAFace() const { 
  return(myState==2);
}


void BRepClass3d_SClassifier::ForceIn() {
  myState=3;
}

void BRepClass3d_SClassifier::ForceOut() { 
  myState=4;
}

Standard_Real GetAddToParam(const gp_Lin&       L,
                            const Standard_Real P,
                            const Bnd_Box&      B)
{
  Standard_Real aXmin, aYmin, aZmin, aXmax, aYmax, aZmax;
  B.Get(aXmin, aYmin, aZmin, aXmax, aYmax, aZmax);
  Standard_Real x[2] = {aXmin,aXmax}, y[2] = {aYmin,aYmax}, z[2] = {aZmin,aZmax};
  Standard_Integer i = 0, j = 0, k = 0;
  Standard_Real Par = P;
  for(i = 0 ; i < 2; i++) {
    for(j = 0; j < 2; j++) {
      for(k = 0; k < 2; k++) {
        Standard_Real X = fabs(x[i]-L.Location().X());
        Standard_Real Y = fabs(y[j]-L.Location().Y());
        Standard_Real Z = fabs(z[k]-L.Location().Z());
        if(X < 1.e+20 && Y < 1.e+20 && Z < 1.e+20) {
          gp_Pnt aP(x[i],y[j],z[k]);
          Standard_Real par = ElCLib::Parameter(L,aP);
          if(par > Par)
            Par = par;
        }
        else
          return 1.e+20;
      }
    }
  }
  return Par - P;
}
//=======================================================================
//function : FaceNormal
//purpose  : 
//=======================================================================
void FaceNormal (const TopoDS_Face& aF,
		 const Standard_Real U,
		 const Standard_Real V,
		 gp_Dir& aDN)
{
  gp_Pnt aPnt ;
  gp_Vec aD1U, aD1V, aN;
  Handle(Geom_Surface) aS;

  aS=BRep_Tool::Surface(aF);
  aS->D1 (U, V, aPnt, aD1U, aD1V);
  aN=aD1U.Crossed(aD1V);
  aN.Normalize();  
  aDN.SetXYZ(aN.XYZ());
  if (aF.Orientation() == TopAbs_REVERSED){
    aDN.Reverse();
  }
  return;
}