summaryrefslogtreecommitdiff
path: root/src/Select3D/Select3D_SensitiveTriangle.cxx
blob: b1a4a2b9a95b76d8c4edd15d7ed49a526b3cad00 (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
// File:    Select3D_SensitiveTriangle.cxx
// Created: Wed May 14 16:56:06 1997
// Author:  Robert COUBLANC
//      <rob@robox.paris1.matra-dtv.fr>


#include <Select3D_SensitiveTriangle.ixx>


#include <SelectBasics_BasicTool.hxx>
#include <gp_Pnt2d.hxx>
#include <gp_Pnt.hxx>
#include <gp_Dir2d.hxx>
#include <Precision.hxx>
#include <Bnd_Box.hxx>
#include <ElCLib.hxx>
#include <TopLoc_Location.hxx>

#include <CSLib_Class2d.hxx>

#define COORD(a,b) ((Select3D_Pnt*)mypolyg3d)[(a)].b
#define COORD2d(a,b) ((Select3D_Pnt2d*)mypolyg2d)[(a)].b

static Standard_Boolean S3D_Str_NearSegment (const gp_XY& p0, const gp_XY& p1, const gp_XY& TheP,
                                             const Standard_Real aTol, Standard_Real& aDMin)
{
  gp_XY V01(p1);
  V01 -= p0;

  gp_XY Vec(TheP);
  Vec -= p0;
  
  Standard_Real u = Vec*V01.Normalized();
  if(u<-aTol) return Standard_False;
  Standard_Real u1 = u-aTol;
  Standard_Real modmod = V01.SquareModulus();
  if(u1*u1> modmod) return Standard_False;

  gp_XY N01 (-V01.Y(),V01.X());
  N01.Normalize();
  aDMin = Abs (Vec * N01);
  return aDMin <= aTol;
}

//==================================================
// Function: 
// Purpose :
//==================================================

Select3D_SensitiveTriangle::
Select3D_SensitiveTriangle(const Handle(SelectBasics_EntityOwner)& OwnerId,
			   const gp_Pnt& P0,
			   const gp_Pnt& P1,
			   const gp_Pnt& P2,
			   const Select3D_TypeOfSensitivity aType):
Select3D_SensitivePoly(OwnerId,3),
mytype (aType)
{ 
  ((Select3D_Pnt*)mypolyg3d)[0] = P0;
  ((Select3D_Pnt*)mypolyg3d)[1] = P1;
  ((Select3D_Pnt*)mypolyg3d)[2] = P2;
}

//==================================================
// Function: 
// Purpose :
//==================================================

Standard_Boolean Select3D_SensitiveTriangle::
Matches(const Standard_Real X,
	const Standard_Real Y,
	const Standard_Real aTol,
	Standard_Real& DMin)
{
  Select3D_SensitiveEntity::Matches(X,Y,aTol,DMin);
  if(Bnd_Box2d(mybox2d).IsOut(gp_Pnt2d(X,Y))) return Standard_False;

  Standard_Integer Res;
  switch (mytype){
  case Select3D_TOS_BOUNDARY:
    Res = Status(X,Y,aTol,DMin);
    return Res== 1;
    break;
  case Select3D_TOS_INTERIOR:
    Res = Status(X,Y,aTol,DMin);
    return (Res==0 || Res == 1);
#ifndef DEB
  default:
    break;
#endif    
  }
  return Standard_True;
}

Standard_Boolean Select3D_SensitiveTriangle::
Matches (const Standard_Real XMin,
	 const Standard_Real YMin,
	 const Standard_Real XMax,
	 const Standard_Real YMax,
	 const Standard_Real aTol)
{
  Bnd_Box2d B;
  B.Update(Min(XMin,XMax)-aTol,
       Min(YMin,YMax)-aTol,
       Max(XMin,XMax)+aTol,
       Max(YMin,YMax)+aTol);
  for(Standard_Integer i=0;i<=2;i++){
    if(B.IsOut(((Select3D_Pnt2d*)mypolyg2d)[i])) 
      return Standard_False;}
  return Standard_True;
}


//=======================================================================
//function : Matches
//purpose  : 
//=======================================================================

Standard_Boolean Select3D_SensitiveTriangle::
Matches (const TColgp_Array1OfPnt2d& aPoly,
	 const Bnd_Box2d& aBox,
	 const Standard_Real aTol)
{ 
  Standard_Real Umin,Vmin,Umax,Vmax;
  aBox.Get(Umin,Vmin,Umax,Vmax);
  CSLib_Class2d aClassifier2d(aPoly,aTol,aTol,Umin,Vmin,Umax,Vmax);

  for(Standard_Integer i=0;i<=2;i++){
    Standard_Integer RES = aClassifier2d.SiDans(((Select3D_Pnt2d*)mypolyg2d)[i]);
    if(RES!=1) return Standard_False;
  }
  return Standard_True;
}



 void Select3D_SensitiveTriangle::Points3D(gp_Pnt& P0,gp_Pnt& P1,gp_Pnt& P2) const
{
  P0 = ((Select3D_Pnt*)mypolyg3d)[0];  P1 = ((Select3D_Pnt*)mypolyg3d)[1];  P2 = ((Select3D_Pnt*)mypolyg3d)[2];  
}

gp_Pnt Select3D_SensitiveTriangle::Center3D() const
{
  gp_XYZ CDG(((Select3D_Pnt*)mypolyg3d)[0]);
  CDG += ((Select3D_Pnt*)mypolyg3d)[1];
  CDG += ((Select3D_Pnt*)mypolyg3d)[2];
  CDG /=3.;
  return gp_Pnt(CDG);;
}      

gp_XY Select3D_SensitiveTriangle::Center2D() const
{
  return (gp_XY(((Select3D_Pnt2d*)mypolyg2d)[0])+gp_XY(((Select3D_Pnt2d*)mypolyg2d)[1])
          +gp_XY(((Select3D_Pnt2d*)mypolyg2d)[2]))/3.;
}

//=======================================================================
//function : Status
//purpose  : 0 = inside /1 = Boundary/ 2 = outside
//=======================================================================

Standard_Integer  Select3D_SensitiveTriangle::Status(const Standard_Real X,
						     const Standard_Real Y,
						     const Standard_Real aTol,
						     Standard_Real& DMin) const 
{
  
  return Status(((Select3D_Pnt2d*)mypolyg2d)[0],((Select3D_Pnt2d*)mypolyg2d)[1],
                ((Select3D_Pnt2d*)mypolyg2d)[2],gp_XY(X,Y),aTol,DMin);
  
}


//=======================================================================
//function : Status
//purpose  : 
//=======================================================================

Standard_Integer  Select3D_SensitiveTriangle::Status(const gp_XY& p0,
						     const gp_XY& p1,
						     const gp_XY& p2,
						     const gp_XY& TheP,
						     const Standard_Real aTol,
						     Standard_Real& DMin)
{

  Bnd_Box2d B;
  B.Update(p0.X(),p0.Y());B.Update(p1.X(),p1.Y());B.Update(p2.X(),p2.Y());
  B.Enlarge(aTol);
  if(B.IsOut(TheP)) return 2; 
  
  // the point is classified corresponding to demi-spaces limited
  // by each side of the triangle (with tolerance)
  gp_XY V01(p1);V01-=p0;
  gp_XY V02(p2);V02-=p0;
  gp_XY V12(p2);V12-=p1;


  // check these particular cases...
  // if one of vectors is almost null (2 points are mixed), 
  // leave at once (it is already in the bounding box, which is good...)
  
  DMin = aTol;
  
  if ( V01.SquareModulus() <= gp::Resolution() )
  {
    Standard_Real LV = V02.SquareModulus();
    if ( LV <= gp::Resolution())
      return 0; // 3 points are mixed, and TheP is in the bounding box B...

    if ( S3D_Str_NearSegment (p0, p2, TheP, aTol, DMin) )
      return 0;
    return 2;    
  }
  if ( V02.SquareModulus() <= gp::Resolution() )
  {
    if ( S3D_Str_NearSegment (p0, p1, TheP, aTol, DMin) )
      return 0;
    return 2;
  }
  if ( V12.SquareModulus() <= gp::Resolution() )
  {
    if ( S3D_Str_NearSegment (p0, p1, TheP, aTol, DMin) )
      return 0;
    return 2;
  }
  if ( V01.CrossMagnitude(V02) <= gp::Resolution() )
  {
    if ( S3D_Str_NearSegment (p0, p1, TheP, aTol, DMin) )
      return 0;
    return 2;
  }

  // oriented normal to p0p1...
  gp_Dir2d N (-V01.Y(), V01.X()); 
  Standard_Boolean Neg = (N * V02 < 0.);
  if ( Neg ) 
    N.Reverse();

  gp_XY Vec(TheP);
  Vec -= p0;
  
  Standard_Real aD1 = Vec * N.XY();
  if ( aD1 < -aTol ) 
    return 2;

  // oriented normal to p1p2...
  if(Neg)
    N.SetCoord(p2.Y()-p1.Y(),p1.X()-p2.X());
  else
    N.SetCoord(p1.Y()-p2.Y(),p2.X()-p1.X());
  
  Vec.SetCoord(TheP.X()-p1.X(),TheP.Y()-p1.Y());
  Standard_Real aD2 = Vec * N.XY();
  if ( aD2 < -aTol ) 
    return 2;   // outside

  // oriented normal to p2p0...
  // attention v20 (x0-x2)    =>   N y2-y0   => -N  y0-y2
  //               (y0-y2)           x0-x2          x2-x0
  if(Neg)
    N.SetCoord(p0.Y()-p2.Y(),p2.X()-p0.X());
  else
    N.SetCoord(p2.Y()-p0.Y(),p0.X()-p2.X());
  
  Vec.SetCoord(TheP.X()-p2.X(),TheP.Y()-p2.Y());
  Standard_Real aD3 = Vec * N.XY();
  if ( aD3 < -aTol ) 
    return 2;  // outside

  // compute 2d distance to triangle
  Standard_Real aD = Min (aD1, Min (aD2, aD3));
  DMin = ( aD < 0 ? -aD : 0. );
  return 0;
}
                             
//=======================================================================
//function : Dump
//purpose  : 
//=======================================================================

void Select3D_SensitiveTriangle::Dump(Standard_OStream& S,const Standard_Boolean FullDump) const 
{
  // general information....
  
  S<<"\tSensitiveTriangle 3D :\n";
  if(HasLocation())
    S<<"\t\tExisting Location"<<endl;

  S<<"\t\t P0 [ "<<COORD(0,x)<<" , "<<COORD(0,y) <<" , "<<COORD(0,z)<<" ]"<<endl;
  S<<"\t\t P1 [ "<<COORD(1,x)<<" , "<<COORD(1,y) <<" , "<<COORD(1,z)<<" ]"<<endl;
  S<<"\t\t P2 [ "<<COORD(2,x)<<" , "<<COORD(2,y) <<" , "<<COORD(2,z)<<" ]"<<endl;

  if(FullDump){
    S<<"\t\tProjected Points"<<endl;
    
    S<<"\t\t  0.[ "<<COORD2d(0,x)<<" , "<<COORD2d(0,y)<<" ]"<<endl;
    S<<"\t\t  1.[ "<<COORD2d(1,x)<<" , "<<COORD2d(1,y)<<" ]"<<endl;
    S<<"\t\t  2.[ "<<COORD2d(2,x)<<" , "<<COORD2d(2,y)<<" ]"<<endl;
//    S<<"\t\t\tOwner:"<<myOwnerId<<endl;
    Select3D_SensitiveEntity::DumpBox(S,mybox2d);
  
  }
}
//=======================================================================
//function : ComputeDepth
//purpose  : 
//=======================================================================
Standard_Real Select3D_SensitiveTriangle::ComputeDepth(const gp_Lin& EyeLine) const
{
  gp_Pnt P1(((Select3D_Pnt*)mypolyg3d)[0].x, ((Select3D_Pnt*)mypolyg3d)[0].y, ((Select3D_Pnt*)mypolyg3d)[0].z);
  gp_Pnt P2(((Select3D_Pnt*)mypolyg3d)[1].x, ((Select3D_Pnt*)mypolyg3d)[1].y, ((Select3D_Pnt*)mypolyg3d)[1].z);
  gp_Pnt P3(((Select3D_Pnt*)mypolyg3d)[2].x, ((Select3D_Pnt*)mypolyg3d)[2].y, ((Select3D_Pnt*)mypolyg3d)[2].z);

  gp_Trsf TheTrsf ;
  if(HasLocation()) 
    TheTrsf = Location().Transformation();
  
  if(TheTrsf.Form()!=gp_Identity){
    P1.Transform(TheTrsf);
    P2.Transform(TheTrsf);
    P3.Transform(TheTrsf);
  }
  
  Standard_Real prof(Precision::Infinite());
  // formula calculation of the point parameters on intersection
  // t = (P1P2 ^P1P3)* OP1  / ((P1P2^P1P3)*Dir)
  
  gp_Pnt Oye  = EyeLine.Location(); // origin of the target line eye/point...
  gp_Dir Dir  = EyeLine.Direction();
  
  gp_Vec P1P2 (P1,P2), P1P3(P1,P3);
  P1P2.Normalize();
  P1P3.Normalize();
  
  gp_Vec oP1(Oye,P1);
  Standard_Real val1 = oP1.DotCross(P1P2,P1P3);
  Standard_Real val2 = Dir.DotCross(P1P2,P1P3);
  
  if(Abs(val2)>Precision::Confusion())
    prof =val1/val2;
  
  if (prof==Precision::Infinite()){
    prof= ElCLib::Parameter(EyeLine,P1);
    prof = Min (prof, ElCLib::Parameter(EyeLine,P2));
    prof = Min (prof, ElCLib::Parameter(EyeLine,P3));
  }
  return prof;

}