b779e072734ad8ff5d491a261de814d72dd91da0
[occt.git] / src / Select3D / Select3D_SensitiveFace.cxx
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 <Select3D_Projector.hxx>
12 #include <SelectBasics_BasicTool.hxx>
13 #include <gp_Pnt2d.hxx>
14 #include <gp_Pnt.hxx>
15 #include <Precision.hxx>
16 #include <ElCLib.hxx>
17
18 #include <CSLib_Class2d.hxx>
19
20
21 #define AutoInterMask  0x01
22 #define AutoComputeMask  0x02
23 // Standard_True if the flag is one
24 #define AutoInterFlag(aflag)  ( aflag & AutoInterMask )
25 #define AutoComputeFlag(aflag)  ( aflag & AutoComputeMask )
26 // set the flag to one
27 #define SetAutoInterFlag(aflag)  ( aflag = aflag & AutoInterMask)
28 #define SetAutoComputeFlag(aflag)  ( aflag = aflag & AutoComputeMask)
29 // Initialize flags
30 #define AutoInitFlags(aflag) (aflag = 0)
31
32 //==================================================
33 // Function: Hide this constructor to the next version...
34 // Purpose : simply avoid interfering with the version update
35 //==================================================
36
37 Select3D_SensitiveFace::
38 Select3D_SensitiveFace(const Handle(SelectBasics_EntityOwner)& OwnerId,
39                const TColgp_Array1OfPnt& ThePoints,
40                const Select3D_TypeOfSensitivity aType):
41 Select3D_SensitivePoly(OwnerId, ThePoints),
42 mytype (aType),
43 myDetectedIndex(-1)
44 {
45   AutoInitFlags(myautointer);
46 }
47
48 //==================================================
49 // Function: 
50 // Purpose :
51 //==================================================
52
53 Select3D_SensitiveFace::
54 Select3D_SensitiveFace(const Handle(SelectBasics_EntityOwner)& OwnerId,
55                const Handle(TColgp_HArray1OfPnt)& ThePoints,
56                const Select3D_TypeOfSensitivity aType):
57 Select3D_SensitivePoly(OwnerId, ThePoints),
58 mytype (aType),
59 myDetectedIndex(-1)
60 {
61   AutoInitFlags(myautointer);
62 }
63
64 //==================================================
65 // Function: 
66 // Purpose :
67 //==================================================
68
69 Standard_Boolean Select3D_SensitiveFace::
70 Matches(const Standard_Real X,
71     const Standard_Real Y,
72     const Standard_Real aTol,
73     Standard_Real& DMin)
74 {
75 #ifndef DEB
76   Standard_Real DMin2 = 0.;
77 #else
78   Standard_Real DMin2;
79 #endif
80   Standard_Real Xmin,Ymin,Xmax,Ymax;
81   if(!Bnd_Box2d(mybox2d).IsVoid()){
82     Bnd_Box2d(mybox2d).Get(Xmin,Ymin,Xmax,Ymax);
83     DMin2 = gp_XY(Xmax-Xmin,Ymax-Ymin).SquareModulus();
84   }
85   // calculation of a criterion of minimum distance...
86   // from start Dmin = size of the bounding box 2D,
87   // then min. distance of the polyhedron or cdg...
88
89   Standard_Real aTol2 = aTol*aTol;
90   gp_XY CDG(0.,0.);
91 //  for(Standard_Integer I=1;I<=Nbp-1;I++){
92   Standard_Integer I;
93   for(I=1;I<mynbpoints-1;I++){
94     CDG+=((Select3D_Pnt2d*)mypolyg2d)[I-1];
95   }
96   
97   if(mynbpoints>1){
98     CDG/= (mynbpoints-1);
99   }
100   DMin2=Min(DMin2,gp_XY(CDG.X()-X,CDG.Y()-Y).SquareModulus());
101   DMin = Sqrt(DMin2);
102   
103   
104   Standard_Boolean isplane2d(Standard_True);
105   
106   for( I=1;I<mynbpoints-1;I++){
107     gp_XY V1(((Select3D_Pnt2d*)mypolyg2d)[I]),V(X,Y);
108     V1-=((Select3D_Pnt2d*)mypolyg2d)[I-1];
109     V-=((Select3D_Pnt2d*)mypolyg2d)[I-1];
110     Standard_Real Vector = V1^V;
111     Standard_Real V1V1 = V1.SquareModulus();
112     DMin2 = 
113       (V1V1 <=aTol2) ? 
114     Min(DMin2,V.SquareModulus()): // if the segment is too small...
115       Min(DMin2,Vector*Vector/V1V1);
116     //cdg ...
117     gp_XY PlaneTest(CDG);PlaneTest-=((Select3D_Pnt2d*)mypolyg2d)[I-1];
118     Standard_Real valtst = PlaneTest^V1;
119     if(isplane2d && Abs(valtst)>aTol) isplane2d=Standard_False;
120   }
121   if (isplane2d)
122   {
123     return Select3D_SensitiveEntity::Matches(X,Y,aTol,DMin);
124   }
125   //detection d'une auto - intersection dans le polygon 2D; si oui on sort
126 //    if (!AutoComputeFlag(myautointer)) {
127 //      if(mypolyg2d.Length()>4) {
128 //        if (SelectBasics_BasicTool::AutoInter(mypolyg2d)) {
129 //      SetAutoInterFlag(myautointer);
130 //        }
131 //      }
132 //      SetAutoComputeFlag(myautointer);
133 //    }
134 //   if (AutoInterFlag(myautointer)) return Standard_True;
135 // //  
136
137   //otherwise it is checked if the point is in the face...
138   TColgp_Array1OfPnt2d aArrayOf2dPnt(1, mynbpoints);
139   Points2D(aArrayOf2dPnt);
140   CSLib_Class2d TheInOutTool(aArrayOf2dPnt,aTol,aTol,Xmin,Ymin,Xmax,Ymax);
141   Standard_Integer TheStat = TheInOutTool.SiDans(gp_Pnt2d(X,Y));
142   
143   Standard_Boolean res(Standard_False);
144   switch(TheStat){
145   case 0:
146     res = Standard_True;
147   case 1:
148     {
149       if(mytype!=Select3D_TOS_BOUNDARY)
150         res = Standard_True;
151     }
152   }
153   if (res)
154   {
155     return Select3D_SensitiveEntity::Matches(X,Y,aTol,DMin);
156   }
157   return Standard_False;
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 {
233   Standard_Real aDepth = Precision::Infinite();
234   Standard_Real aDepthMin = !mylastprj.IsNull() ? mylastprj->DepthMin() : -Precision::Infinite();
235   Standard_Real aDepthMax = !mylastprj.IsNull() ? mylastprj->DepthMax() :  Precision::Infinite();
236   Standard_Real aDepthTest;
237   for (Standard_Integer i = 0; i < mynbpoints - 1; i++)
238   {
239     aDepthTest = ElCLib::Parameter (EyeLine, ((Select3D_Pnt* )mypolyg3d)[i]);
240     if (aDepthTest < aDepth && (aDepthTest > aDepthMin) && (aDepthTest < aDepthMax))
241     {
242       aDepth = aDepthTest;
243     }
244   }
245   return aDepth;
246 }