1 // Created on: 1995-03-27
2 // Created by: Robert COUBLANC
3 // Copyright (c) 1995-1999 Matra Datavision
4 // Copyright (c) 1999-2012 OPEN CASCADE SAS
6 // The content of this file is subject to the Open CASCADE Technology Public
7 // License Version 6.5 (the "License"). You may not use the content of this file
8 // except in compliance with the License. Please obtain a copy of the License
9 // at http://www.opencascade.org and read it completely before using this file.
11 // The Initial Developer of the Original Code is Open CASCADE S.A.S., having its
12 // main offices at: 1, place des Freres Montgolfier, 78280 Guyancourt, France.
14 // The Original Code and all software distributed under the License is
15 // distributed on an "AS IS" basis, without warranty of any kind, and the
16 // Initial Developer hereby disclaims all such warranties, including without
17 // limitation, any warranties of merchantability, fitness for a particular
18 // purpose or non-infringement. Please see the License for the specific terms
19 // and conditions governing the rights and limitations under the License.
21 //Modif on jun-24-97 : introduction de CSLib_Class2d de LBR
22 // pour teste si on est dedans ou dehors...
23 //Modif on jul-21-97 : changement en harray1 pour eventuelles connexions ...
25 #include <Select3D_SensitiveFace.ixx>
26 #include <Select3D_Projector.hxx>
27 #include <SelectBasics_BasicTool.hxx>
28 #include <gp_Pnt2d.hxx>
30 #include <Precision.hxx>
33 #include <CSLib_Class2d.hxx>
36 #define AutoInterMask 0x01
37 #define AutoComputeMask 0x02
38 // Standard_True if the flag is one
39 #define AutoInterFlag(aflag) ( aflag & AutoInterMask )
40 #define AutoComputeFlag(aflag) ( aflag & AutoComputeMask )
41 // set the flag to one
42 #define SetAutoInterFlag(aflag) ( aflag = aflag & AutoInterMask)
43 #define SetAutoComputeFlag(aflag) ( aflag = aflag & AutoComputeMask)
45 #define AutoInitFlags(aflag) (aflag = 0)
47 //==================================================
48 // Function: Hide this constructor to the next version...
49 // Purpose : simply avoid interfering with the version update
50 //==================================================
52 Select3D_SensitiveFace::
53 Select3D_SensitiveFace(const Handle(SelectBasics_EntityOwner)& OwnerId,
54 const TColgp_Array1OfPnt& ThePoints,
55 const Select3D_TypeOfSensitivity aType):
56 Select3D_SensitivePoly(OwnerId, ThePoints),
60 AutoInitFlags(myautointer);
63 //==================================================
66 //==================================================
68 Select3D_SensitiveFace::
69 Select3D_SensitiveFace(const Handle(SelectBasics_EntityOwner)& OwnerId,
70 const Handle(TColgp_HArray1OfPnt)& ThePoints,
71 const Select3D_TypeOfSensitivity aType):
72 Select3D_SensitivePoly(OwnerId, ThePoints),
76 AutoInitFlags(myautointer);
79 //==================================================
82 //==================================================
84 Standard_Boolean Select3D_SensitiveFace::
85 Matches(const Standard_Real X,
86 const Standard_Real Y,
87 const Standard_Real aTol,
90 Standard_Real DMin2 = 0.;
91 Standard_Real Xmin,Ymin,Xmax,Ymax;
92 if(!Bnd_Box2d(mybox2d).IsVoid())
94 Bnd_Box2d(mybox2d).Get(Xmin,Ymin,Xmax,Ymax);
95 DMin2 = gp_XY(Xmax-Xmin,Ymax-Ymin).SquareModulus();
97 // calculation of a criterion of minimum distance...
98 // from start Dmin = size of the bounding box 2D,
99 // then min. distance of the polyhedron or cdg...
101 Standard_Real aTol2 = aTol*aTol;
102 Standard_Integer aSize = mypolyg.Size(), anIndex;
104 for(anIndex=0;anIndex<aSize;++anIndex)
106 CDG+=mypolyg.Pnt2d(anIndex);
113 DMin2=Min(DMin2,gp_XY(CDG.X()-X,CDG.Y()-Y).SquareModulus());
116 Standard_Boolean isplane2d(Standard_True);
118 for(anIndex=1;anIndex<aSize;++anIndex)
120 gp_XY V1(mypolyg.Pnt2d(anIndex)),V(X,Y);
121 V1-=mypolyg.Pnt2d(anIndex-1);
122 V-=mypolyg.Pnt2d(anIndex-1);
123 Standard_Real Vector = V1^V;
124 Standard_Real V1V1 = V1.SquareModulus();
127 Min(DMin2,V.SquareModulus()): // if the segment is too small...
128 Min(DMin2,Vector*Vector/V1V1);
130 gp_XY PlaneTest(CDG);
131 PlaneTest-=mypolyg.Pnt2d(anIndex-1);
132 Standard_Real valtst = PlaneTest^V1;
133 if(isplane2d && Abs(valtst)>aTol) isplane2d=Standard_False;
137 return Select3D_SensitiveEntity::Matches(X,Y,aTol,DMin);
140 //otherwise it is checked if the point is in the face...
141 TColgp_Array1OfPnt2d aArrayOf2dPnt(1, aSize);
142 Points2D(aArrayOf2dPnt);
143 CSLib_Class2d TheInOutTool(aArrayOf2dPnt,aTol,aTol,Xmin,Ymin,Xmax,Ymax);
144 Standard_Integer TheStat = TheInOutTool.SiDans(gp_Pnt2d(X,Y));
146 Standard_Boolean res(Standard_False);
153 if(mytype!=Select3D_TOS_BOUNDARY)
159 return Select3D_SensitiveEntity::Matches(X,Y,aTol,DMin);
161 return Standard_False;
164 //=======================================================================
167 //=======================================================================
169 Standard_Boolean Select3D_SensitiveFace::
170 Matches (const Standard_Real XMin,
171 const Standard_Real YMin,
172 const Standard_Real XMax,
173 const Standard_Real YMax,
174 const Standard_Real aTol)
177 BoundBox.Update(XMin-aTol,YMin-aTol,XMax+aTol,YMax+aTol);
179 for(Standard_Integer anIndex=0;anIndex<mypolyg.Size();++anIndex)
181 if(BoundBox.IsOut(mypolyg.Pnt2d(anIndex)))
182 return Standard_False;
184 return Standard_True;
187 //=======================================================================
190 //=======================================================================
192 Standard_Boolean Select3D_SensitiveFace::
193 Matches (const TColgp_Array1OfPnt2d& aPoly,
194 const Bnd_Box2d& aBox,
195 const Standard_Real aTol)
197 Standard_Real Umin,Vmin,Umax,Vmax;
198 aBox.Get(Umin,Vmin,Umax,Vmax);
199 CSLib_Class2d aClassifier2d(aPoly,aTol,aTol,Umin,Vmin,Umax,Vmax);
202 for(Standard_Integer anIndex=0;anIndex<mypolyg.Size();++anIndex)
204 Standard_Integer RES = aClassifier2d.SiDans(mypolyg.Pnt2d(anIndex));
206 return Standard_False;
208 return Standard_True;
211 //=======================================================================
214 //=======================================================================
216 void Select3D_SensitiveFace::Dump(Standard_OStream& S,const Standard_Boolean FullDump) const
218 S<<"\tSensitiveFace 3D :"<<endl;;
220 S<<"\t\tExisting Location"<<endl;
222 if(mytype==Select3D_TOS_BOUNDARY)
223 S<<"\t\tSelection Of Bounding Polyline Only"<<endl;
227 S<<"\t\tNumber Of Points :"<<mypolyg.Size()<<endl;
228 Select3D_SensitiveEntity::DumpBox(S,mybox2d);
232 //=======================================================================
233 //function : ComputeDepth
235 //=======================================================================
237 Standard_Real Select3D_SensitiveFace::ComputeDepth(const gp_Lin& EyeLine) const
239 Standard_Real aDepth = Precision::Infinite();
241 Standard_Real aDepthMin = !mylastprj.IsNull() ? mylastprj->DepthMin() : -Precision::Infinite();
242 Standard_Real aDepthMax = !mylastprj.IsNull() ? mylastprj->DepthMax() : Precision::Infinite();
243 Standard_Real aDepthTest;
245 for (Standard_Integer anIndex = 0; anIndex < mypolyg.Size()-1; ++anIndex)
247 aDepthTest = ElCLib::Parameter (EyeLine, mypolyg.Pnt(anIndex));
248 if (aDepthTest < aDepth && (aDepthTest > aDepthMin) && (aDepthTest < aDepthMax))
256 //=======================================================================
257 //function : GetConnected
259 //=======================================================================
261 Handle(Select3D_SensitiveEntity) Select3D_SensitiveFace::GetConnected(const TopLoc_Location &theLocation)
263 // Create a copy of this
264 Standard_Integer aSize = mypolyg.Size();
265 TColgp_Array1OfPnt aPoints(1, aSize);
266 for (Standard_Integer anIndex = 1; anIndex <= aSize; ++anIndex)
268 aPoints.SetValue(anIndex, mypolyg.Pnt(anIndex-1));
271 Handle(Select3D_SensitiveEntity) aNewEntity =
272 new Select3D_SensitiveFace(myOwnerId, aPoints, mytype);
275 aNewEntity->SetLocation(Location());
277 aNewEntity->UpdateLocation(theLocation);