1 // Copyright: Matra-Datavision 1995
2 // File: Select3D_SensitiveFace.cxx
3 // Created: Mon Mar 27 10:15:15 1995
4 // Author: Robert COUBLANC
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 ...
10 #include <Select3D_SensitiveFace.ixx>
11 #include <Select3D_Projector.hxx>
12 #include <SelectBasics_BasicTool.hxx>
13 #include <gp_Pnt2d.hxx>
15 #include <Precision.hxx>
18 #include <CSLib_Class2d.hxx>
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)
30 #define AutoInitFlags(aflag) (aflag = 0)
32 //==================================================
33 // Function: Hide this constructor to the next version...
34 // Purpose : simply avoid interfering with the version update
35 //==================================================
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),
45 AutoInitFlags(myautointer);
48 //==================================================
51 //==================================================
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),
61 AutoInitFlags(myautointer);
64 //==================================================
67 //==================================================
69 Standard_Boolean Select3D_SensitiveFace::
70 Matches(const Standard_Real X,
71 const Standard_Real Y,
72 const Standard_Real aTol,
76 Standard_Real DMin2 = 0.;
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();
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...
89 Standard_Real aTol2 = aTol*aTol;
91 // for(Standard_Integer I=1;I<=Nbp-1;I++){
93 for(I=1;I<mynbpoints-1;I++){
94 CDG+=((Select3D_Pnt2d*)mypolyg2d)[I-1];
100 DMin2=Min(DMin2,gp_XY(CDG.X()-X,CDG.Y()-Y).SquareModulus());
104 Standard_Boolean isplane2d(Standard_True);
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();
114 Min(DMin2,V.SquareModulus()): // if the segment is too small...
115 Min(DMin2,Vector*Vector/V1V1);
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;
123 return Select3D_SensitiveEntity::Matches(X,Y,aTol,DMin);
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);
132 // SetAutoComputeFlag(myautointer);
134 // if (AutoInterFlag(myautointer)) return Standard_True;
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));
143 Standard_Boolean res(Standard_False);
149 if(mytype!=Select3D_TOS_BOUNDARY)
155 return Select3D_SensitiveEntity::Matches(X,Y,aTol,DMin);
157 return Standard_False;
160 //=======================================================================
163 //=======================================================================
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)
173 BoundBox.Update(XMin-aTol,YMin-aTol,XMax+aTol,YMax+aTol);
175 for(Standard_Integer j=1;j<=mynbpoints-1;j++){
176 if(BoundBox.IsOut(((Select3D_Pnt2d*)mypolyg2d)[j-1])) return Standard_False;
178 return Standard_True;
181 //=======================================================================
184 //=======================================================================
186 Standard_Boolean Select3D_SensitiveFace::
187 Matches (const TColgp_Array1OfPnt2d& aPoly,
188 const Bnd_Box2d& aBox,
189 const Standard_Real aTol)
191 Standard_Real Umin,Vmin,Umax,Vmax;
192 aBox.Get(Umin,Vmin,Umax,Vmax);
193 Standard_Real Tolu,Tolv;
196 CSLib_Class2d aClassifier2d(aPoly,aTol,aTol,Umin,Vmin,Umax,Vmax);
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;
202 return Standard_True;
206 //=======================================================================
209 //=======================================================================
210 void Select3D_SensitiveFace::Dump(Standard_OStream& S,const Standard_Boolean FullDump) const
212 S<<"\tSensitiveFace 3D :"<<endl;;
214 S<<"\t\tExisting Location"<<endl;
216 if(mytype==Select3D_TOS_BOUNDARY)
217 S<<"\t\tSelection Of Bounding Polyline Only"<<endl;
220 S<<"\t\tNumber Of Points :"<<mynbpoints<<endl;
222 // S<<"\t\t\tOwner:"<<myOwnerId<<endl;
223 Select3D_SensitiveEntity::DumpBox(S,mybox2d);
227 //=======================================================================
228 //function : ComputeDepth
230 //=======================================================================
231 Standard_Real Select3D_SensitiveFace::ComputeDepth(const gp_Lin& EyeLine) const
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++)
239 aDepthTest = ElCLib::Parameter (EyeLine, ((Select3D_Pnt* )mypolyg3d)[i]);
240 if (aDepthTest < aDepth && (aDepthTest > aDepthMin) && (aDepthTest < aDepthMax))