7fd59977 |
1 | // Copyright: Matra-Datavision 1995 |
2 | // File: Select3D_SensitiveFace.cxx |
3 | // Created: Mon Mar 27 10:15:15 1995 |
4 | // Author: Robert COUBLANC |
5 | // <rob> |
6 | //Modif on jun-24-97 : introduction de CSLib_Class2d de LBR |
7 | // pour teste si on est dedans ou dehors... |
8 | //Modif on jul-21-97 : changement en harray1 pour eventuelles connexions ... |
9 | |
10 | #include <Select3D_SensitiveFace.ixx> |
11 | #include <SelectBasics_BasicTool.hxx> |
12 | #include <gp_Pnt2d.hxx> |
13 | #include <gp_Pnt.hxx> |
14 | #include <Precision.hxx> |
15 | #include <ElCLib.hxx> |
16 | |
17 | #include <CSLib_Class2d.hxx> |
18 | |
19 | |
20 | #define AutoInterMask 0x01 |
21 | #define AutoComputeMask 0x02 |
22 | // Standard_True if the flag is one |
23 | #define AutoInterFlag(aflag) ( aflag & AutoInterMask ) |
24 | #define AutoComputeFlag(aflag) ( aflag & AutoComputeMask ) |
25 | // set the flag to one |
26 | #define SetAutoInterFlag(aflag) ( aflag = aflag & AutoInterMask) |
27 | #define SetAutoComputeFlag(aflag) ( aflag = aflag & AutoComputeMask) |
28 | // Initialize flags |
29 | #define AutoInitFlags(aflag) (aflag = 0) |
30 | |
31 | //================================================== |
81bba717 |
32 | // Function: Hide this constructor to the next version... |
33 | // Purpose : simply avoid interfering with the version update |
7fd59977 |
34 | //================================================== |
35 | |
36 | Select3D_SensitiveFace:: |
37 | Select3D_SensitiveFace(const Handle(SelectBasics_EntityOwner)& OwnerId, |
38 | const TColgp_Array1OfPnt& ThePoints, |
39 | const Select3D_TypeOfSensitivity aType): |
40 | Select3D_SensitivePoly(OwnerId, ThePoints), |
41 | mytype (aType), |
42 | myDetectedIndex(-1) |
43 | { |
44 | AutoInitFlags(myautointer); |
45 | } |
46 | |
47 | //================================================== |
48 | // Function: |
49 | // Purpose : |
50 | //================================================== |
51 | |
52 | Select3D_SensitiveFace:: |
53 | Select3D_SensitiveFace(const Handle(SelectBasics_EntityOwner)& OwnerId, |
54 | const Handle(TColgp_HArray1OfPnt)& ThePoints, |
55 | const Select3D_TypeOfSensitivity aType): |
56 | Select3D_SensitivePoly(OwnerId, ThePoints), |
57 | mytype (aType), |
58 | myDetectedIndex(-1) |
59 | { |
60 | AutoInitFlags(myautointer); |
61 | } |
62 | |
63 | //================================================== |
64 | // Function: |
65 | // Purpose : |
66 | //================================================== |
67 | |
68 | Standard_Boolean Select3D_SensitiveFace:: |
69 | Matches(const Standard_Real X, |
70 | const Standard_Real Y, |
71 | const Standard_Real aTol, |
72 | Standard_Real& DMin) |
73 | { |
74 | #ifndef DEB |
75 | Standard_Real DMin2 = 0.; |
76 | #else |
77 | Standard_Real DMin2; |
78 | #endif |
79 | Standard_Real Xmin,Ymin,Xmax,Ymax; |
80 | if(!Bnd_Box2d(mybox2d).IsVoid()){ |
81 | Bnd_Box2d(mybox2d).Get(Xmin,Ymin,Xmax,Ymax); |
82 | DMin2 = gp_XY(Xmax-Xmin,Ymax-Ymin).SquareModulus(); |
83 | } |
81bba717 |
84 | // calculation of a criterion of minimum distance... |
85 | // from start Dmin = size of the bounding box 2D, |
86 | // then min. distance of the polyhedron or cdg... |
7fd59977 |
87 | |
88 | Standard_Real aTol2 = aTol*aTol; |
89 | gp_XY CDG(0.,0.); |
90 | // for(Standard_Integer I=1;I<=Nbp-1;I++){ |
91 | Standard_Integer I; |
92 | for(I=1;I<mynbpoints-1;I++){ |
93 | CDG+=((Select3D_Pnt2d*)mypolyg2d)[I-1]; |
94 | } |
95 | |
96 | if(mynbpoints>1){ |
97 | CDG/= (mynbpoints-1); |
98 | } |
99 | DMin2=Min(DMin2,gp_XY(CDG.X()-X,CDG.Y()-Y).SquareModulus()); |
100 | DMin = Sqrt(DMin2); |
101 | |
102 | |
103 | Standard_Boolean isplane2d(Standard_True); |
104 | |
105 | for( I=1;I<mynbpoints-1;I++){ |
106 | gp_XY V1(((Select3D_Pnt2d*)mypolyg2d)[I]),V(X,Y); |
107 | V1-=((Select3D_Pnt2d*)mypolyg2d)[I-1]; |
108 | V-=((Select3D_Pnt2d*)mypolyg2d)[I-1]; |
109 | Standard_Real Vector = V1^V; |
110 | Standard_Real V1V1 = V1.SquareModulus(); |
111 | DMin2 = |
112 | (V1V1 <=aTol2) ? |
81bba717 |
113 | Min(DMin2,V.SquareModulus()): // if the segment is too small... |
7fd59977 |
114 | Min(DMin2,Vector*Vector/V1V1); |
115 | //cdg ... |
116 | gp_XY PlaneTest(CDG);PlaneTest-=((Select3D_Pnt2d*)mypolyg2d)[I-1]; |
117 | Standard_Real valtst = PlaneTest^V1; |
118 | if(isplane2d && Abs(valtst)>aTol) isplane2d=Standard_False; |
119 | } |
81bba717 |
120 | |
121 | if(isplane2d) { |
122 | Select3D_SensitiveEntity::Matches(X,Y,aTol,DMin); |
123 | |
124 | return Standard_True; |
7fd59977 |
125 | } |
126 | //detection d'une auto - intersection dans le polygon 2D; si oui on sort |
127 | // if (!AutoComputeFlag(myautointer)) { |
128 | // if(mypolyg2d.Length()>4) { |
129 | // if (SelectBasics_BasicTool::AutoInter(mypolyg2d)) { |
130 | // SetAutoInterFlag(myautointer); |
131 | // } |
132 | // } |
133 | // SetAutoComputeFlag(myautointer); |
134 | // } |
135 | // if (AutoInterFlag(myautointer)) return Standard_True; |
136 | // // |
137 | |
81bba717 |
138 | //otherwise it is checked if the point is in the face... |
7fd59977 |
139 | TColgp_Array1OfPnt2d aArrayOf2dPnt(1, mynbpoints); |
140 | Points2D(aArrayOf2dPnt); |
141 | CSLib_Class2d TheInOutTool(aArrayOf2dPnt,aTol,aTol,Xmin,Ymin,Xmax,Ymax); |
142 | Standard_Integer TheStat = TheInOutTool.SiDans(gp_Pnt2d(X,Y)); |
143 | |
144 | Standard_Boolean res(Standard_False); |
145 | switch(TheStat){ |
146 | case 0: |
147 | res = Standard_True; |
148 | case 1: |
149 | { |
150 | if(mytype!=Select3D_TOS_BOUNDARY) |
151 | res = Standard_True; |
152 | } |
153 | } |
81bba717 |
154 | if(res) |
155 | Select3D_SensitiveEntity::Matches(X,Y,aTol,DMin); |
156 | |
157 | return res; |
7fd59977 |
158 | } |
159 | |
160 | //======================================================================= |
161 | //function : Matches |
162 | //purpose : |
163 | //======================================================================= |
164 | |
165 | Standard_Boolean Select3D_SensitiveFace:: |
166 | Matches (const Standard_Real XMin, |
167 | const Standard_Real YMin, |
168 | const Standard_Real XMax, |
169 | const Standard_Real YMax, |
170 | const Standard_Real aTol) |
171 | { |
172 | Bnd_Box2d BoundBox; |
173 | BoundBox.Update(XMin-aTol,YMin-aTol,XMax+aTol,YMax+aTol); |
174 | |
175 | for(Standard_Integer j=1;j<=mynbpoints-1;j++){ |
176 | if(BoundBox.IsOut(((Select3D_Pnt2d*)mypolyg2d)[j-1])) return Standard_False; |
177 | } |
178 | return Standard_True; |
179 | } |
180 | |
181 | //======================================================================= |
182 | //function : Matches |
183 | //purpose : |
184 | //======================================================================= |
185 | |
186 | Standard_Boolean Select3D_SensitiveFace:: |
187 | Matches (const TColgp_Array1OfPnt2d& aPoly, |
188 | const Bnd_Box2d& aBox, |
189 | const Standard_Real aTol) |
190 | { |
191 | Standard_Real Umin,Vmin,Umax,Vmax; |
192 | aBox.Get(Umin,Vmin,Umax,Vmax); |
193 | Standard_Real Tolu,Tolv; |
194 | Tolu = 1e-7; |
195 | Tolv = 1e-7; |
196 | CSLib_Class2d aClassifier2d(aPoly,aTol,aTol,Umin,Vmin,Umax,Vmax); |
197 | |
198 | for(Standard_Integer j=1;j<=mynbpoints;j++){ |
199 | Standard_Integer RES = aClassifier2d.SiDans(((Select3D_Pnt2d*)mypolyg2d)[j-1]); |
200 | if(RES!=1) return Standard_False; |
201 | } |
202 | return Standard_True; |
203 | } |
204 | |
205 | |
206 | //======================================================================= |
207 | //function : Dump |
208 | //purpose : |
209 | //======================================================================= |
210 | void Select3D_SensitiveFace::Dump(Standard_OStream& S,const Standard_Boolean FullDump) const |
211 | { |
212 | S<<"\tSensitiveFace 3D :"<<endl;; |
213 | if(HasLocation()) |
214 | S<<"\t\tExisting Location"<<endl; |
215 | |
216 | if(mytype==Select3D_TOS_BOUNDARY) |
217 | S<<"\t\tSelection Of Bounding Polyline Only"<<endl; |
218 | |
219 | if(FullDump){ |
220 | S<<"\t\tNumber Of Points :"<<mynbpoints<<endl; |
221 | |
222 | // S<<"\t\t\tOwner:"<<myOwnerId<<endl; |
223 | Select3D_SensitiveEntity::DumpBox(S,mybox2d); |
224 | } |
225 | } |
226 | |
227 | //======================================================================= |
228 | //function : ComputeDepth |
229 | //purpose : |
230 | //======================================================================= |
231 | Standard_Real Select3D_SensitiveFace::ComputeDepth(const gp_Lin& EyeLine) const |
232 | { |
81bba717 |
233 | Standard_Real val(Precision::Infinite()); |
234 | for(Standard_Integer i=0;i<mynbpoints-1;i++) |
235 | val = Min(val,ElCLib::Parameter(EyeLine,((Select3D_Pnt*)mypolyg3d)[i])); |
236 | return val; |
7fd59977 |
237 | } |