0022717: Exception during sewing
[occt.git] / src / Select3D / Select3D_SensitiveCircle.cxx
CommitLineData
7fd59977 1// File: Select3D_SensitiveCircle.cxx
2// Created: Tue Feb 6 14:15:06 1996
3// Author: Robert COUBLANC
4// <rob@fidox>
5// Modified Tue Apr 14 1998 by rob : fix Bug : Case of Null Radius Circle...
6
7#include <Select3D_SensitiveCircle.ixx>
8#include <Precision.hxx>
9#include <gp_Lin2d.hxx>
10
11#include <CSLib_Class2d.hxx>
12#include <Select3D_SensitiveTriangle.hxx>
13#include <ElCLib.hxx>
14#include <Select3D_Pnt.hxx>
15#include <Select3D_Pnt2d.hxx>
329843e2 16#include <Select3D_Projector.hxx>
7fd59977 17
18//=======================================================================
19//function : Select3D_SensitiveCircle (constructeur)
81bba717 20//purpose : Definition of a sensitive circle
7fd59977 21//=======================================================================
22static Standard_Integer S3D_GetCircleNBPoints(const Handle(Geom_Circle)& C,
23 const Standard_Integer anInputNumber)
24{
25 if(C->Radius()>Precision::Confusion())
26 return 2*anInputNumber+1;
27 return 1;
28}
29static Standard_Integer S3D_GetArcNBPoints(const Handle(Geom_Circle)& C,
30 const Standard_Integer anInputNumber)
31{
32 if(C->Radius()>Precision::Confusion())
33 return 2*anInputNumber-1;
34 return 1;
35}
36
37Select3D_SensitiveCircle::
38Select3D_SensitiveCircle(const Handle(SelectBasics_EntityOwner)& OwnerId,
39 const Handle(Geom_Circle)& TheCircle,
40 const Standard_Boolean FilledCircle,
41 const Standard_Integer NbPoints):
42Select3D_SensitivePoly(OwnerId, S3D_GetCircleNBPoints(TheCircle,NbPoints)),
43myFillStatus(FilledCircle),
44myDetectedIndex(-1)
45{
46 if(mynbpoints!=1){
47 gp_Pnt p1,p2;//,pmid;
48 gp_Vec v1;//,v2;
49 Standard_Real ustart = TheCircle->FirstParameter(),uend = TheCircle->LastParameter();
50 Standard_Real du = (uend-ustart)/NbPoints;
51 Standard_Real R = TheCircle->Radius();
52 Standard_Integer rank = 1;
53 Standard_Real curu =ustart;
54 for(Standard_Integer i=1;i<=NbPoints;i++)
55 {
56 TheCircle->D1(curu,p1,v1);
57
58 v1.Normalize();
59 ((Select3D_Pnt*)mypolyg3d)[rank-1] = p1;
60 rank++;
61 p2 = gp_Pnt(p1.X()+v1.X()*tan(du/2.)*R,
62 p1.Y()+v1.Y()*tan(du/2.)*R,
63 p1.Z()+v1.Z()*tan(du/2.)*R);
64 ((Select3D_Pnt*)mypolyg3d)[rank-1] = p2;
65 rank++;
66 curu+=du;
67 }
329843e2
A
68 ((Select3D_Pnt*)mypolyg3d)[NbPoints*2] = ((Select3D_Pnt*)mypolyg3d)[0];
69 // Get myCenter3D
70 myCenter3D = TheCircle->Location();
7fd59977 71 }
72 // Radius = 0.0
73 else
329843e2 74 {
7fd59977 75 ((Select3D_Pnt*)mypolyg3d)[0] = TheCircle->Location();
329843e2
A
76 // Get myCenter3D
77 myCenter3D = ((Select3D_Pnt*)mypolyg3d)[0];
78 }
79
7fd59977 80}
81
82//=======================================================================
83//function : Select3D_SensitiveCircle (constructeur)
81bba717 84//purpose : Definition of a sensitive arc
7fd59977 85//=======================================================================
86Select3D_SensitiveCircle::
87Select3D_SensitiveCircle(const Handle(SelectBasics_EntityOwner)& OwnerId,
88 const Handle(Geom_Circle)& TheCircle,
89 const Standard_Real u1,
90 const Standard_Real u2,
91 const Standard_Boolean FilledCircle,
92 const Standard_Integer NbPoints):
93Select3D_SensitivePoly(OwnerId, S3D_GetArcNBPoints(TheCircle,NbPoints)),
94myFillStatus(FilledCircle),
95myDetectedIndex(-1)
96{
97
98 if(mynbpoints > 1){
99 gp_Pnt p1,p2;//,pmid;
100 gp_Vec v1;//,v2;
101
102 Standard_Real ustart = u1;
103 Standard_Real uend = u2;
104
105 if (u1 > u2) {ustart=u1;uend=u2;}
106
107 Standard_Real du = (uend-ustart)/(NbPoints-1);
108 Standard_Real R = TheCircle->Radius();
109 Standard_Integer rank = 1;
110 Standard_Real curu =ustart;
111
112
113
114 for(Standard_Integer i=1;i<=NbPoints-1;i++)
115 {
116 TheCircle->D1(curu,p1,v1);
117 v1.Normalize();
118 ((Select3D_Pnt*)mypolyg3d)[rank-1] = p1;
119 rank++;
120 p2 = gp_Pnt(p1.X()+v1.X()*tan(du/2.)*R,
121 p1.Y()+v1.Y()*tan(du/2.)*R,
122 p1.Z()+v1.Z()*tan(du/2.)*R);
123 ((Select3D_Pnt*)mypolyg3d)[rank-1] = p2;
124 rank++;
125 curu+=du;
126 }
127 TheCircle->D0(uend,p1);
329843e2
A
128 ((Select3D_Pnt*)mypolyg3d)[NbPoints*2-2] = p1;
129 // Get myCenter3D
130 myCenter3D = TheCircle->Location();
7fd59977 131 }
132 else
329843e2
A
133 {
134 ((Select3D_Pnt*)mypolyg3d)[0] = TheCircle->Location();
135 // Get myCenter3D
136 myCenter3D = ((Select3D_Pnt*)mypolyg3d)[0];
137 }
7fd59977 138}
139
140//=======================================================================
141//function : Select3D_SensitiveCircle
142//purpose :
143//=======================================================================
144Select3D_SensitiveCircle::Select3D_SensitiveCircle(const Handle(SelectBasics_EntityOwner)& OwnerId,
145 const Handle(TColgp_HArray1OfPnt)& Thepolyg3d,
146 const Standard_Boolean FilledCircle):
147Select3D_SensitivePoly(OwnerId, Thepolyg3d),
148myFillStatus(FilledCircle),
149myDetectedIndex(-1)
150{
329843e2
A
151 if (mynbpoints > 1)
152 ComputeCenter3D();
153 else
154 myCenter3D = ((Select3D_Pnt*)mypolyg3d)[0];
7fd59977 155}
329843e2 156
7fd59977 157Select3D_SensitiveCircle::Select3D_SensitiveCircle(const Handle(SelectBasics_EntityOwner)& OwnerId,
158 const TColgp_Array1OfPnt& Thepolyg3d,
159 const Standard_Boolean FilledCircle):
160Select3D_SensitivePoly(OwnerId, Thepolyg3d),
161myFillStatus(FilledCircle),
162myDetectedIndex(-1)
329843e2
A
163{
164 if (mynbpoints > 1)
165 ComputeCenter3D();
166 else
167 myCenter3D = ((Select3D_Pnt*)mypolyg3d)[0];
7fd59977 168}
169
170Standard_Boolean Select3D_SensitiveCircle::
171Matches(const Standard_Real X,
172 const Standard_Real Y,
173 const Standard_Real aTol,
174 Standard_Real& DMin)
175{
7fd59977 176
329843e2
A
177 if(mynbpoints>1)
178 {
179 Standard_Boolean Found = Standard_False;
7fd59977 180 Standard_Integer i = 0;
7fd59977 181
329843e2
A
182 if(!myFillStatus)
183 {
184 while(i < mynbpoints-2 && !Found)
185 {
186 Standard_Integer TheStat =
187 Select3D_SensitiveTriangle::Status(((Select3D_Pnt2d*)mypolyg2d)[i],
7fd59977 188 ((Select3D_Pnt2d*)mypolyg2d)[i+1],
189 ((Select3D_Pnt2d*)mypolyg2d)[i+2],
329843e2
A
190 gp_XY(X,Y),aTol,DMin);
191 Found = (TheStat != 2);
192 if(Found) myDetectedIndex = i;
193 i += 2;
7fd59977 194 }
195 }
329843e2
A
196 else
197 {
7fd59977 198 myDetectedIndex =-1;
7fd59977 199 Standard_Real Xmin,Ymin,Xmax,Ymax;
329843e2
A
200
201 // Get coordinates of the bounding box
202 Bnd_Box2d(mybox2d).Get(Xmin,Ymin,Xmax,Ymax);
203 TColgp_Array1OfPnt2d anArrayOf2dPnt(1, mynbpoints);
204
205 // Fill anArrayOf2dPnt with points from mypolig2d
206 Points2D(anArrayOf2dPnt);
7fd59977 207
329843e2
A
208 CSLib_Class2d anInOutTool(anArrayOf2dPnt,aTol,aTol,Xmin,Ymin,Xmax,Ymax);
209
210 // Method SiDans returns the status :
211 // 1 - the point is inside the circle
212 // 0 - the point is on the circle
213 // -1 - the point is outside the circle
214 Standard_Integer aStat = anInOutTool.SiDans(gp_Pnt2d(X,Y));
215 if(aStat != -1)
216 {
217 // Compute DMin (a distance between the center and the point)
218 DMin = gp_XY(myCenter2D.x - X, myCenter2D.y - Y).Modulus();
219 Select3D_SensitiveEntity::Matches(X,Y,aTol,DMin);
220 return Standard_True;
221 }
222 return Standard_False;
7fd59977 223 }
224 if(!Found)
225 myDetectedIndex=-1;
226 else
227 Select3D_SensitiveEntity::Matches(X,Y,aTol,DMin);
228
229 return Found;
230 }
231 return Standard_True;
232
233}
329843e2 234
7fd59977 235Standard_Boolean Select3D_SensitiveCircle::
236Matches(const Standard_Real XMin,
237 const Standard_Real YMin,
238 const Standard_Real XMax,
239 const Standard_Real YMax,
240 const Standard_Real aTol)
241{
242 myDetectedIndex =-1;
243 Bnd_Box2d abox;
244 abox.Update(Min(XMin,XMax),Min(YMin,YMax),Max(XMin,XMax),Max(YMin,YMax));
245 abox.Enlarge(aTol);
246 for(Standard_Integer i=0;i<mynbpoints;i++)
247 if(abox.IsOut(((Select3D_Pnt2d*)mypolyg2d)[i])) return Standard_False;
248
249 return Standard_True;
250}
251
252
253//=======================================================================
254//function : Matches
255//purpose :
256//=======================================================================
257
258Standard_Boolean Select3D_SensitiveCircle::
259Matches (const TColgp_Array1OfPnt2d& aPoly,
260 const Bnd_Box2d& aBox,
261 const Standard_Real aTol)
262{
263 Standard_Real Umin,Vmin,Umax,Vmax;
264 aBox.Get(Umin,Vmin,Umax,Vmax);
265 Standard_Real Tolu,Tolv;
266 Tolu = 1e-7;
267 Tolv = 1e-7;
268 CSLib_Class2d aClassifier2d(aPoly,aTol,aTol,Umin,Vmin,Umax,Vmax);
269
270 for(Standard_Integer j=1;j<=mynbpoints;j++){
271 Standard_Integer RES = aClassifier2d.SiDans(((Select3D_Pnt2d*)mypolyg2d)[j-1]);
272 if(RES!=1) return Standard_False;
273 }
274 return Standard_True;
275}
276
277
278
279
280void Select3D_SensitiveCircle::
281ArrayBounds(Standard_Integer & Low,
282 Standard_Integer & Up) const
283{
284 Low = 0;
285 Up = mynbpoints-1;
286}
287
288
289gp_Pnt Select3D_SensitiveCircle::
290GetPoint3d(const Standard_Integer Rank) const
291{
292 if(Rank>=0&& Rank<mynbpoints)
293 return ((Select3D_Pnt*)mypolyg3d)[Rank];
294 return ((Select3D_Pnt*)mypolyg3d)[0];
295}
296
297
298//=======================================================================
299//function : Dump
300//purpose :
301//=======================================================================
302
303void Select3D_SensitiveCircle::Dump(Standard_OStream& S,const Standard_Boolean FullDump) const
304{
305 // Standard_Integer rank(1);
306 gp_XYZ CDG(0.,0.,0.);
307
308 S<<"\tSensitiveCircle 3D :";
309
310 Standard_Boolean isclosed = 1== mynbpoints;
311 if(isclosed)
312 S<<"(Closed Circle)"<<endl;
313 else
314 S<<"(Arc Of Circle)"<<endl;
315
316 if(HasLocation())
317 S<<"\t\tExisting Location"<<endl;
318
319
320 if(FullDump){
321 Standard_Integer EndIndex = isclosed? mynbpoints-2 : mynbpoints-1, nbpt(0);
322 for(Standard_Integer i=0;i<EndIndex;i+=2){
323 CDG +=((Select3D_Pnt*)mypolyg3d)[i];
324 nbpt++;
325 }
326
327 CDG/=nbpt;
328
329 Standard_Real R = (CDG-((Select3D_Pnt*)mypolyg3d)[0]).Modulus();
330
331 S<<"\t\t Center : ("<<CDG.X()<<" , "<<CDG.Y()<<" , "<<CDG.Z()<<" )"<<endl;
332 S<<"\t\t Radius :"<<R<<endl;
333
334 }
335}
336Standard_Real Select3D_SensitiveCircle::ComputeDepth(const gp_Lin& EyeLine) const
337{
338 gp_Pnt CDG;
339 if(myDetectedIndex==-1){
340 gp_XYZ CurCoord(((Select3D_Pnt*)mypolyg3d)[0]);
341 Standard_Boolean isclosed = 1==mynbpoints;
342 Standard_Integer EndIndex = isclosed ? mynbpoints-2 : mynbpoints-1, nbpt(0);
343 for(Standard_Integer i=1;i<EndIndex;i+=2){
344 CurCoord +=((Select3D_Pnt*)mypolyg3d)[i];
345 nbpt++;
346 }
347 CDG.SetXYZ(CurCoord);
348 }
349 else{
350 gp_XYZ CurCoord(((Select3D_Pnt*)mypolyg3d)[myDetectedIndex]);
351 CurCoord+=((Select3D_Pnt*)mypolyg3d)[myDetectedIndex+1];
352 CurCoord+=((Select3D_Pnt*)mypolyg3d)[myDetectedIndex+2];
353 CDG.SetXYZ(CurCoord);
354 }
355
356 return ElCLib::Parameter(EyeLine,CDG);
357
358}
329843e2
A
359
360void Select3D_SensitiveCircle::Project(const Handle_Select3D_Projector &aProjector)
361{
362 Select3D_SensitivePoly::Project(aProjector);
363 // Project the center of the circle
364 gp_Pnt2d aCenter;
365 aProjector->Project(myCenter3D, aCenter);
366 myCenter2D = aCenter;
367}
368
369void Select3D_SensitiveCircle::ComputeCenter3D()
370{
371 gp_XYZ aCenter(0., 0., 0.);
372 if (mynbpoints > 1)
373 {
374 // The mass of points system
375 Standard_Integer aMass = mynbpoints - 1;
376 // Find the circle barycenter
377 for (Standard_Integer i = 0; i < mynbpoints-1; ++i)
378 {
379 aCenter += ((Select3D_Pnt*)mypolyg3d)[i];
380 }
381 myCenter3D = aCenter / aMass;
382 }
383 else if (mynbpoints == 1)
384 {
385 myCenter3D = ((Select3D_Pnt*)mypolyg3d)[0];
386 }
387 // bad case! there are no points in mypolyg3d
388 // It can lead to incorrect computation of
389 // parameter DMin in method Matches.
390 // In spite of this myCenter3D isn't left uninitialized
391 else
392 {
393 myCenter3D = aCenter;
394 }
395}