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 <SelectBasics_BasicTool.hxx>
12 #include <gp_Pnt2d.hxx>
14 #include <Precision.hxx>
17 #include <CSLib_Class2d.hxx>
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)
29 #define AutoInitFlags(aflag) (aflag = 0)
31 //==================================================
32 // Function: Hide this constructor to the next version...
33 // Purpose : simply avoid interfering with the version update
34 //==================================================
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),
44 AutoInitFlags(myautointer);
47 //==================================================
50 //==================================================
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),
60 AutoInitFlags(myautointer);
63 //==================================================
66 //==================================================
68 Standard_Boolean Select3D_SensitiveFace::
69 Matches(const Standard_Real X,
70 const Standard_Real Y,
71 const Standard_Real aTol,
75 Standard_Real DMin2 = 0.;
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();
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...
88 Standard_Real aTol2 = aTol*aTol;
90 // for(Standard_Integer I=1;I<=Nbp-1;I++){
92 for(I=1;I<mynbpoints-1;I++){
93 CDG+=((Select3D_Pnt2d*)mypolyg2d)[I-1];
99 DMin2=Min(DMin2,gp_XY(CDG.X()-X,CDG.Y()-Y).SquareModulus());
103 Standard_Boolean isplane2d(Standard_True);
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();
113 Min(DMin2,V.SquareModulus()): // if the segment is too small...
114 Min(DMin2,Vector*Vector/V1V1);
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;
122 Select3D_SensitiveEntity::Matches(X,Y,aTol,DMin);
124 return Standard_True;
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);
133 // SetAutoComputeFlag(myautointer);
135 // if (AutoInterFlag(myautointer)) return Standard_True;
138 //otherwise it is checked if the point is in the face...
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));
144 Standard_Boolean res(Standard_False);
150 if(mytype!=Select3D_TOS_BOUNDARY)
155 Select3D_SensitiveEntity::Matches(X,Y,aTol,DMin);
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 val(Precision::Infinite());
234 for(Standard_Integer i=0;i<mynbpoints-1;i++)
235 val = Min(val,ElCLib::Parameter(EyeLine,((Select3D_Pnt*)mypolyg3d)[i]));