0023604: Uninitialized variables in debug mode
[occt.git] / src / Select3D / Select3D_SensitiveFace.cxx
CommitLineData
b311480e 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
5//
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.
10//
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.
13//
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.
20
7fd59977 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 ...
24
25#include <Select3D_SensitiveFace.ixx>
4bf18dff 26#include <Select3D_Projector.hxx>
7fd59977 27#include <SelectBasics_BasicTool.hxx>
28#include <gp_Pnt2d.hxx>
29#include <gp_Pnt.hxx>
30#include <Precision.hxx>
31#include <ElCLib.hxx>
32
33#include <CSLib_Class2d.hxx>
34
35
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)
44// Initialize flags
45#define AutoInitFlags(aflag) (aflag = 0)
46
47//==================================================
81bba717 48// Function: Hide this constructor to the next version...
49// Purpose : simply avoid interfering with the version update
7fd59977 50//==================================================
51
52Select3D_SensitiveFace::
53Select3D_SensitiveFace(const Handle(SelectBasics_EntityOwner)& OwnerId,
ac04d101
SA
54 const TColgp_Array1OfPnt& ThePoints,
55 const Select3D_TypeOfSensitivity aType):
7fd59977 56Select3D_SensitivePoly(OwnerId, ThePoints),
57mytype (aType),
58myDetectedIndex(-1)
59{
60 AutoInitFlags(myautointer);
61}
62
63//==================================================
ac04d101 64// Function: Creation
7fd59977 65// Purpose :
66//==================================================
67
68Select3D_SensitiveFace::
69Select3D_SensitiveFace(const Handle(SelectBasics_EntityOwner)& OwnerId,
ac04d101
SA
70 const Handle(TColgp_HArray1OfPnt)& ThePoints,
71 const Select3D_TypeOfSensitivity aType):
7fd59977 72Select3D_SensitivePoly(OwnerId, ThePoints),
73mytype (aType),
74myDetectedIndex(-1)
75{
76 AutoInitFlags(myautointer);
77}
78
79//==================================================
ac04d101 80// Function: Matches
7fd59977 81// Purpose :
82//==================================================
83
84Standard_Boolean Select3D_SensitiveFace::
85Matches(const Standard_Real X,
ac04d101
SA
86 const Standard_Real Y,
87 const Standard_Real aTol,
88 Standard_Real& DMin)
7fd59977 89{
7fd59977 90 Standard_Real DMin2 = 0.;
7fd59977 91 Standard_Real Xmin,Ymin,Xmax,Ymax;
ac04d101
SA
92 if(!Bnd_Box2d(mybox2d).IsVoid())
93 {
7fd59977 94 Bnd_Box2d(mybox2d).Get(Xmin,Ymin,Xmax,Ymax);
95 DMin2 = gp_XY(Xmax-Xmin,Ymax-Ymin).SquareModulus();
96 }
81bba717 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...
7fd59977 100
101 Standard_Real aTol2 = aTol*aTol;
ceae62f0
A
102 Standard_Integer aSize = mypolyg.Size(), anIndex;
103 gp_XY CDG;
104 for(anIndex=0;anIndex<aSize;++anIndex)
ac04d101 105 {
ceae62f0 106 CDG+=mypolyg.Pnt2d(anIndex);
7fd59977 107 }
ceae62f0
A
108
109 if(aSize>1)
ac04d101 110 {
ceae62f0 111 CDG/=(aSize-1);
7fd59977 112 }
113 DMin2=Min(DMin2,gp_XY(CDG.X()-X,CDG.Y()-Y).SquareModulus());
114 DMin = Sqrt(DMin2);
ceae62f0 115
7fd59977 116 Standard_Boolean isplane2d(Standard_True);
ceae62f0
A
117
118 for(anIndex=1;anIndex<aSize;++anIndex)
ac04d101 119 {
ceae62f0
A
120 gp_XY V1(mypolyg.Pnt2d(anIndex)),V(X,Y);
121 V1-=mypolyg.Pnt2d(anIndex-1);
122 V-=mypolyg.Pnt2d(anIndex-1);
7fd59977 123 Standard_Real Vector = V1^V;
124 Standard_Real V1V1 = V1.SquareModulus();
125 DMin2 =
126 (V1V1 <=aTol2) ?
81bba717 127 Min(DMin2,V.SquareModulus()): // if the segment is too small...
7fd59977 128 Min(DMin2,Vector*Vector/V1V1);
129 //cdg ...
ceae62f0
A
130 gp_XY PlaneTest(CDG);
131 PlaneTest-=mypolyg.Pnt2d(anIndex-1);
7fd59977 132 Standard_Real valtst = PlaneTest^V1;
133 if(isplane2d && Abs(valtst)>aTol) isplane2d=Standard_False;
134 }
4bf18dff 135 if (isplane2d)
136 {
137 return Select3D_SensitiveEntity::Matches(X,Y,aTol,DMin);
7fd59977 138 }
7fd59977 139
81bba717 140 //otherwise it is checked if the point is in the face...
ceae62f0 141 TColgp_Array1OfPnt2d aArrayOf2dPnt(1, aSize);
7fd59977 142 Points2D(aArrayOf2dPnt);
143 CSLib_Class2d TheInOutTool(aArrayOf2dPnt,aTol,aTol,Xmin,Ymin,Xmax,Ymax);
144 Standard_Integer TheStat = TheInOutTool.SiDans(gp_Pnt2d(X,Y));
ceae62f0 145
7fd59977 146 Standard_Boolean res(Standard_False);
ac04d101
SA
147 switch(TheStat)
148 {
7fd59977 149 case 0:
150 res = Standard_True;
151 case 1:
152 {
ceae62f0 153 if(mytype!=Select3D_TOS_BOUNDARY)
ac04d101 154 res = Standard_True;
7fd59977 155 }
156 }
4bf18dff 157 if (res)
158 {
159 return Select3D_SensitiveEntity::Matches(X,Y,aTol,DMin);
160 }
161 return Standard_False;
7fd59977 162}
163
164//=======================================================================
165//function : Matches
166//purpose :
167//=======================================================================
168
169Standard_Boolean Select3D_SensitiveFace::
170Matches (const Standard_Real XMin,
ac04d101
SA
171 const Standard_Real YMin,
172 const Standard_Real XMax,
173 const Standard_Real YMax,
174 const Standard_Real aTol)
175{
7fd59977 176 Bnd_Box2d BoundBox;
177 BoundBox.Update(XMin-aTol,YMin-aTol,XMax+aTol,YMax+aTol);
ceae62f0
A
178
179 for(Standard_Integer anIndex=0;anIndex<mypolyg.Size();++anIndex)
ac04d101 180 {
ceae62f0
A
181 if(BoundBox.IsOut(mypolyg.Pnt2d(anIndex)))
182 return Standard_False;
7fd59977 183 }
184 return Standard_True;
185}
186
187//=======================================================================
188//function : Matches
189//purpose :
190//=======================================================================
191
192Standard_Boolean Select3D_SensitiveFace::
193Matches (const TColgp_Array1OfPnt2d& aPoly,
ac04d101
SA
194 const Bnd_Box2d& aBox,
195 const Standard_Real aTol)
196{
7fd59977 197 Standard_Real Umin,Vmin,Umax,Vmax;
198 aBox.Get(Umin,Vmin,Umax,Vmax);
199 Standard_Real Tolu,Tolv;
200 Tolu = 1e-7;
201 Tolv = 1e-7;
202 CSLib_Class2d aClassifier2d(aPoly,aTol,aTol,Umin,Vmin,Umax,Vmax);
203
ceae62f0
A
204 gp_Pnt2d aPnt2d;
205 for(Standard_Integer anIndex=0;anIndex<mypolyg.Size();++anIndex)
ac04d101 206 {
ceae62f0
A
207 Standard_Integer RES = aClassifier2d.SiDans(mypolyg.Pnt2d(anIndex));
208 if(RES!=1)
209 return Standard_False;
7fd59977 210 }
211 return Standard_True;
212}
213
7fd59977 214//=======================================================================
215//function : Dump
216//purpose :
217//=======================================================================
ac04d101 218
7fd59977 219void Select3D_SensitiveFace::Dump(Standard_OStream& S,const Standard_Boolean FullDump) const
220{
221 S<<"\tSensitiveFace 3D :"<<endl;;
222 if(HasLocation())
223 S<<"\t\tExisting Location"<<endl;
224
225 if(mytype==Select3D_TOS_BOUNDARY)
226 S<<"\t\tSelection Of Bounding Polyline Only"<<endl;
227
ac04d101
SA
228 if(FullDump)
229 {
ceae62f0 230 S<<"\t\tNumber Of Points :"<<mypolyg.Size()<<endl;
7fd59977 231 Select3D_SensitiveEntity::DumpBox(S,mybox2d);
232 }
233}
234
235//=======================================================================
236//function : ComputeDepth
237//purpose :
238//=======================================================================
ac04d101 239
7fd59977 240Standard_Real Select3D_SensitiveFace::ComputeDepth(const gp_Lin& EyeLine) const
241{
4bf18dff 242 Standard_Real aDepth = Precision::Infinite();
ceae62f0 243
4bf18dff 244 Standard_Real aDepthMin = !mylastprj.IsNull() ? mylastprj->DepthMin() : -Precision::Infinite();
245 Standard_Real aDepthMax = !mylastprj.IsNull() ? mylastprj->DepthMax() : Precision::Infinite();
246 Standard_Real aDepthTest;
ceae62f0
A
247
248 for (Standard_Integer anIndex = 0; anIndex < mypolyg.Size()-1; ++anIndex)
4bf18dff 249 {
ceae62f0 250 aDepthTest = ElCLib::Parameter (EyeLine, mypolyg.Pnt(anIndex));
4bf18dff 251 if (aDepthTest < aDepth && (aDepthTest > aDepthMin) && (aDepthTest < aDepthMax))
252 {
253 aDepth = aDepthTest;
254 }
255 }
256 return aDepth;
7fd59977 257}
ac04d101
SA
258
259//=======================================================================
260//function : GetConnected
261//purpose :
262//=======================================================================
263
264Handle(Select3D_SensitiveEntity) Select3D_SensitiveFace::GetConnected(const TopLoc_Location &theLocation)
265{
ceae62f0
A
266 // Create a copy of this
267 Standard_Integer aSize = mypolyg.Size();
268 TColgp_Array1OfPnt aPoints(1, aSize);
269 for (Standard_Integer anIndex = 1; anIndex <= aSize; ++anIndex)
ac04d101 270 {
ceae62f0 271 aPoints.SetValue(anIndex, mypolyg.Pnt(anIndex-1));
ac04d101
SA
272 }
273
ceae62f0
A
274 Handle(Select3D_SensitiveEntity) aNewEntity =
275 new Select3D_SensitiveFace(myOwnerId, aPoints, mytype);
ac04d101 276
ceae62f0
A
277 if (HasLocation())
278 aNewEntity->SetLocation(Location());
ac04d101
SA
279
280 aNewEntity->UpdateLocation(theLocation);
281
282 return aNewEntity;
283}