summaryrefslogtreecommitdiff
path: root/src/CSLib/CSLib_Class2d.cxx
blob: 49d7489fc9a93e15c9ed75263903a2eb6f16ee53 (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
// File:	CSLib_Class2d.cxx
// Created:	Wed Mar  8 15:06:24 1995
// Author:	Laurent BUCHARD
//		<lbr@mastox>

//#define No_Standard_OutOfRange

#include <CSLib_Class2d.ixx>
#include <Standard_ConstructionError.hxx>

static inline 
  Standard_Real Transform2d(const Standard_Real u,
			    const Standard_Real umin,
			    const Standard_Real umaxmumin);

//=======================================================================
//function : CSLib_Class2d
//purpose  : 
//=======================================================================
CSLib_Class2d::CSLib_Class2d(const TColgp_Array1OfPnt2d& TP2d,
			     const Standard_Real aTolu,
			     const Standard_Real aTolv,
			     const Standard_Real umin,
			     const Standard_Real vmin,
			     const Standard_Real umax,
			     const Standard_Real vmax) 
{
  Umin=umin;
  Vmin=vmin;
  Umax=umax;
  Vmax=vmax;
  //
  if(umax<=umin || vmax<=vmin) { 
    MyPnts2dX=NULL;
    MyPnts2dY=NULL;
    N=0;
  }
  //
  else {
    Standard_Integer i, iLower;
    Standard_Real du,dv,*Pnts2dX,*Pnts2dY, aPrc;   
    //
    aPrc=1.e-10;
    N = TP2d.Length();
    Tolu = aTolu;
    Tolv = aTolv;
    MyPnts2dX = new Standard_Real [N+1];
    MyPnts2dY = new Standard_Real [N+1];
    du=umax-umin;
    dv=vmax-vmin;
    Pnts2dX = (Standard_Real *)MyPnts2dX;
    Pnts2dY = (Standard_Real *)MyPnts2dY;
    //
    iLower=TP2d.Lower();
    for(i = 0; i<N; ++i) { 
      const gp_Pnt2d& aP2D=TP2d(i+iLower);
      Pnts2dX[i] = Transform2d(aP2D.X(), umin, du);
      Pnts2dY[i] = Transform2d(aP2D.Y(), vmin, dv);
    }
    Pnts2dX[N]=Pnts2dX[0];
    Pnts2dY[N]=Pnts2dY[0];
    //
    if(du>aPrc) {
      Tolu/=du;
    }
    if(dv>aPrc) {
      Tolv/=dv;
    }
  }
}
//=======================================================================
//function : Destroy
//purpose  : 
//=======================================================================
void CSLib_Class2d::Destroy() { 
  if(MyPnts2dX) { 
    delete [] (Standard_Real *)MyPnts2dX;
    MyPnts2dX=NULL;
  }
  if(MyPnts2dY) { 
    delete [] (Standard_Real *)MyPnts2dY;
    MyPnts2dY=NULL;
  }
}

//-- Attention   Tableau de 0 ------> N + 1 
//--                        P1 ..... Pn P1
//--
//--     1  2  3
//--     4  0  5
//--     6  7  8
//-- 
//=======================================================================
//function : SiDans
//purpose  : 
//=======================================================================
Standard_Integer CSLib_Class2d::SiDans(const gp_Pnt2d& P) const
{ 
  if(!N) {
    return 0;
  }
  //
  Standard_Real x,y, aTolu, aTolv;
  //
  x = P.X(); y = P.Y();
  aTolu=Tolu*(Umax-Umin);
  aTolv=Tolv*(Vmax-Vmin);
  //
  if(Umin<Umax && Vmin<Vmax)    {
    if( ( x<(Umin-aTolu) ) || 
       ( x>(Umax+aTolu) ) || 
       ( y<(Vmin-aTolv) ) || 
       ( y>(Vmax+aTolv) ) ) {
      return -1;
    }
    x=Transform2d(x,Umin,Umax-Umin);
    y=Transform2d(y,Vmin,Vmax-Vmin);
  }


  Standard_Integer res = InternalSiDansOuOn(x,y);
  if(res==-1) {     //-- on est peut etre ON 
    return 0;
  }
  if(Tolu || Tolv) {
    if(res != InternalSiDans(x-Tolu,y-Tolv)) return 0;
    if(res != InternalSiDans(x+Tolu,y-Tolv)) return 0;
    if(res != InternalSiDans(x-Tolu,y+Tolv)) return 0;
    if(res != InternalSiDans(x+Tolu,y+Tolv)) return 0; 
  }
  //
  return((res)? 1: -1);
}
//=======================================================================
//function : SiDans_OnMode
//purpose  : 
//=======================================================================
Standard_Integer CSLib_Class2d::SiDans_OnMode(const gp_Pnt2d& P,
					      const Standard_Real Tol) const
{ 
  if(!N){
    return 0;
  }
  //
  Standard_Real x,y, aTolu, aTolv;
  //
  x = P.X(); y = P.Y();
  aTolu=Tol; 
  aTolv=Tol; 

  //-- ****** A FAIRE PLUS TARD, ESTIMER EN CHAQUE POINT la Tol2d en fct de la Tol3d *****
  if(Umin<Umax && Vmin<Vmax) { 
    if(x<(Umin-aTolu) || (x>Umax+aTolu) || 
       (y<Vmin-aTolv) || (y>Vmax+aTolv)) {
      return -1;
    }
    x=Transform2d(x,Umin,Umax-Umin);
    y=Transform2d(y,Vmin,Vmax-Vmin);
  }
  //
  Standard_Integer res = InternalSiDansOuOn(x,y);
  if(aTolu || aTolv) {
    if(res != InternalSiDans(x-aTolu,y-aTolv)) return 0;
    if(res != InternalSiDans(x+aTolu,y-aTolv)) return 0;
    if(res != InternalSiDans(x-aTolu,y+aTolv)) return 0;
    if(res != InternalSiDans(x+aTolu,y+aTolv)) return 0; 
  }
  return((res)? 1: -1);
}
//=======================================================================
//function : InternalSiDans
//purpose  : 
//=======================================================================
Standard_Integer CSLib_Class2d::InternalSiDans(const Standard_Real Px,
					       const Standard_Real Py) const
{ 
  Standard_Integer nbc, i, ip1, SH, NH;
  Standard_Real *Pnts2dX, *Pnts2dY;
  Standard_Real  x, y, nx, ny;
  //
  nbc = 0;
  i   = 0;
  ip1 = 1;
  Pnts2dX = (Standard_Real *)MyPnts2dX;
  Pnts2dY = (Standard_Real *)MyPnts2dY;
  x   = (Pnts2dX[i]-Px);
  y   = (Pnts2dY[i]-Py);
  SH  = (y<0.)? -1 : 1;
  //
  for(i=0; i<N ; i++,ip1++) { 
    nx = Pnts2dX[ip1] - Px;
    ny = Pnts2dY[ip1] - Py;
    
    NH = (ny<0.)? -1 : 1;
    if(NH!=SH) { 
      if(x>0. && nx>0.) {
	nbc++;
      }
      else { 
	if(x>0.0 || nx>0.) { 
	  if((x-y*(nx-x)/(ny-y))>0.) {
	    nbc++;
	  }
	}
      }
      SH = NH;
    }
    x = nx; y = ny;
  }
  return(nbc&1);
}
//modified by NIZNHY-PKV Fri Jan 15 09:03:48 2010f
//=======================================================================
//function : InternalSiDansOuOn
//purpose  :  meme code que ci-dessus + test sur ON (return(-1) dans ce cas
//=======================================================================
Standard_Integer CSLib_Class2d::InternalSiDansOuOn(const Standard_Real Px,
						   const Standard_Real Py) const 
{ 
  Standard_Integer nbc, i, ip1, SH, NH, iRet;
  Standard_Real *Pnts2dX, *Pnts2dY;
  Standard_Real x, y, nx, ny, aX;
  Standard_Real aYmin;
  //
  nbc = 0;
  i   = 0;
  ip1 = 1;
  Pnts2dX = (Standard_Real *)MyPnts2dX;
  Pnts2dY = (Standard_Real *)MyPnts2dY;
  x   = (Pnts2dX[i]-Px);
  y   = (Pnts2dY[i]-Py);
  aYmin=y;
  SH  = (y<0.)? -1 : 1;
  for(i=0; i<N ; i++, ip1++) { 
   
    nx = Pnts2dX[ip1] - Px;
    ny = Pnts2dY[ip1] - Py;
    //-- le 14 oct 97 
    if(nx<Tolu && nx>-Tolu && ny<Tolv && ny>-Tolv) { 
      iRet=-1;
      return iRet;
    }
    //find Y coordinate of polyline for current X gka
    //in order to detect possible status ON
    Standard_Real aDx = (Pnts2dX[ip1] - Pnts2dX[ip1-1]);
    if( (Pnts2dX[ip1-1] - Px) * nx < 0.)
    {
     
      Standard_Real aCurPY =  Pnts2dY[ip1] - (Pnts2dY[ip1] - Pnts2dY[ip1-1])/aDx *nx;
      Standard_Real aDeltaY = aCurPY - Py;
      if(aDeltaY >= -Tolv && aDeltaY <= Tolv)
      {
         iRet=-1;
         return iRet;
      }
    }
    //
      
    NH = (ny<0.)? -1 : 1;
    if(NH!=SH) { 
      if(x>0. && nx>0.) {
	nbc++;
      }
      else { 
	if(x>0. || nx>0.) { 
	  aX=x-y*(nx-x)/(ny-y);
	  if(aX>0.){
	    nbc++;
	  }
	}
      }
      SH = NH;
    }
    else {// y has same sign as ny  
      if (ny<aYmin) {
	aYmin=ny;
      }
    }
    x = nx; y = ny; 
  }// for(i=0; i<N ; i++,ip1++) { 
 
  iRet=nbc&1;
  return iRet;
}
//modified by NIZNHY-PKV Fri Jan 15 09:03:55 2010t
//=======================================================================
//function : Copy
//purpose  : 
//=======================================================================
const CSLib_Class2d& CSLib_Class2d::Copy(const CSLib_Class2d& ) const 
{ 
  cerr<<"Copy not allowed in CSLib_Class2d"<<endl;
  Standard_ConstructionError::Raise();
  return *this;
}
//=======================================================================
//function : Transform2d
//purpose  : 
//=======================================================================
Standard_Real Transform2d(const Standard_Real u,
			  const Standard_Real umin,
			  const Standard_Real umaxmumin) 
{ 
  if(umaxmumin>1e-10) { 
    Standard_Real U = (u-umin)/umaxmumin;
    return U;
  }
  else { 
    return u;
  }
}