4131679d399180927595983b7652128c37ad63be
[occt.git] / src / Select3D / Select3D_SensitiveWire.cxx
1 // Created on: 1996-10-17
2 // Created by: Odile OLIVIER
3 // Copyright (c) 1996-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
21
22 #include <Select3D_SensitiveWire.ixx>
23 #include <SelectBasics_BasicTool.hxx>
24 #include <Select3D_SensitiveEntity.hxx>
25 #include <Select3D_SensitiveEntitySequence.hxx>
26 #include <SelectBasics_ListIteratorOfListOfBox2d.hxx>
27 #include <SelectBasics_ListOfBox2d.hxx>
28 #include <Precision.hxx>
29
30 #include <Bnd_Box2d.hxx>
31 #include <ElCLib.hxx>
32
33 //static Standard_Boolean debugmode=Standard_False;
34
35 //=====================================================
36 // Function : Create
37 // Purpose  :Constructor
38 //=====================================================
39
40 Select3D_SensitiveWire::
41 Select3D_SensitiveWire(const Handle(SelectBasics_EntityOwner)& OwnerId,
42                        const Standard_Integer /*MaxRect*/):
43 Select3D_SensitiveEntity(OwnerId),
44 myDetectedIndex(-1)
45 {}
46
47 //=====================================================
48 // Function : Add
49 // Purpose  :
50 //=====================================================
51
52 void Select3D_SensitiveWire
53 ::Add(const Handle(Select3D_SensitiveEntity)& aSensitive)
54 {
55   if(!aSensitive.IsNull()) 
56     mysensitive.Append(aSensitive); 
57 }
58
59 //=======================================================================
60 //function : SetLocation
61 //purpose  :
62 //=======================================================================
63
64 void Select3D_SensitiveWire::SetLocation(const TopLoc_Location& aLoc)
65 {
66
67   // evitons les problemes...
68   if(aLoc.IsIdentity()) return;
69
70   if(HasLocation())
71     if(aLoc == Location()) return;
72
73   Select3D_SensitiveEntity::SetLocation(aLoc);
74   for(Standard_Integer i=1;i<=mysensitive.Length();i++){
75     if(mysensitive(i)->HasLocation()){
76       if(mysensitive(i)->Location()!=aLoc) 
77         mysensitive(i)->SetLocation(mysensitive(i)->Location()*aLoc);
78     }
79     else
80       mysensitive(i)->SetLocation(aLoc);
81
82   }
83 }
84
85 //=======================================================================
86 //function : ResetLocation
87 //purpose  :
88 //=======================================================================
89
90 void Select3D_SensitiveWire::ResetLocation()
91 {
92   if(!HasLocation()) return;
93   for(Standard_Integer i=1;i<=mysensitive.Length();i++){
94     if(mysensitive(i)->HasLocation() && mysensitive(i)->Location()!=Location())
95       mysensitive(i)->SetLocation(mysensitive(i)->Location()*Location().Inverted());
96     else
97       mysensitive(i)->ResetLocation();
98
99   }
100   Select3D_SensitiveEntity::ResetLocation();
101 }
102
103 //=====================================================
104 // Function : Project
105 // Purpose  :
106 //=====================================================
107 void Select3D_SensitiveWire
108 ::Project(const Handle(Select3D_Projector)& aProj)
109 {
110   for(Standard_Integer i=1; i<=mysensitive.Length(); i++)
111     mysensitive(i)->Project(aProj);
112   Select3D_SensitiveEntity::Project(aProj);
113 }
114
115 //=====================================================
116 // Function : Areas
117 // Purpose  :
118 //=====================================================
119
120 void Select3D_SensitiveWire
121 ::Areas(SelectBasics_ListOfBox2d& theareas)
122 {
123   Bnd_Box2d BB; // en attendant un nouveau champ rob-18-jun-97
124   SelectBasics_ListOfBox2d BidL;
125   Standard_Integer i;
126   for (i=1; i<=mysensitive.Length(); i++)
127     mysensitive.Value(i)->Areas(BidL);
128
129   for(SelectBasics_ListIteratorOfListOfBox2d it(BidL);it.More();it.Next())
130     BB.Add(it.Value());
131
132   theareas.Append(BB);
133 }
134
135 //=====================================================
136 // Function : Matches
137 // Purpose  :
138 //=====================================================
139 Standard_Boolean Select3D_SensitiveWire
140 ::Matches(const Standard_Real X,
141           const Standard_Real Y,
142           const Standard_Real aTol,
143           Standard_Real& DMin)
144 {
145   Standard_Integer i;
146   Standard_Real Dcur;
147   DMin = Precision::Infinite();
148   Standard_Boolean IsTouched = Standard_False;
149   for (i=1; i<=mysensitive.Length(); i++) 
150   {
151     if (mysensitive.Value(i)->Matches(X,Y,aTol,Dcur)) 
152     {
153       IsTouched = Standard_True;
154       if(Dcur<=DMin)
155       { 
156         myDetectedIndex = i; 
157         DMin = Dcur;
158       }
159     }
160   }
161   if ( ! IsTouched )
162     return Standard_False;
163
164   // compute and validate the depth (::Depth()) along the eyeline
165   return Select3D_SensitiveEntity::Matches(X,Y,aTol,DMin);
166 }
167
168 //=====================================================
169 // Function : Matches
170 // Purpose  :
171 //=====================================================
172
173 Standard_Boolean Select3D_SensitiveWire::
174 Matches (const Standard_Real XMin,
175          const Standard_Real YMin,
176          const Standard_Real XMax,
177          const Standard_Real YMax,
178          const Standard_Real aTol)
179 {
180   Standard_Integer i;
181   for (i=1; i<=mysensitive.Length(); i++) 
182   {
183     if (!(mysensitive.Value(i)->Matches(XMin,YMin,XMax,YMax,aTol)))
184       return Standard_False;
185   }
186   return Standard_True;
187 }
188
189 //=======================================================================
190 //function : Matches
191 //purpose  :
192 //=======================================================================
193
194 Standard_Boolean Select3D_SensitiveWire::
195 Matches (const TColgp_Array1OfPnt2d& aPoly,
196          const Bnd_Box2d& aBox,
197          const Standard_Real aTol)
198 {
199   Standard_Integer i;
200   for (i=1; i<=mysensitive.Length(); i++) 
201   {
202     if (!(mysensitive.Value(i)->Matches(aPoly, aBox, aTol)))
203       return Standard_False;
204   }
205   return Standard_True;
206 }
207
208
209 //=====================================================
210 // Function : MaxBoxes
211 // Purpose  :
212 //=====================================================
213
214 Standard_Integer Select3D_SensitiveWire::
215 MaxBoxes () const
216 {
217   return 1;
218 }
219
220 //=======================================================================
221 //function : GetConnected
222 //purpose  :
223 //=======================================================================
224
225 Handle(Select3D_SensitiveEntity) Select3D_SensitiveWire::GetConnected(const TopLoc_Location& aLoc)
226 {
227   Handle(Select3D_SensitiveWire) SWIR = new Select3D_SensitiveWire(myOwnerId);
228   for(Standard_Integer i=1;i<=mysensitive.Length();i++)
229     SWIR->Add(mysensitive(i)->GetConnected(aLoc));
230
231   if(HasLocation())
232     SWIR->SetLocation(Location()*aLoc);
233   else
234     SWIR->SetLocation(aLoc);
235   return SWIR;
236 }
237
238
239 //=======================================================================
240 //function : Dump
241 //purpose  :
242 //=======================================================================
243
244 void Select3D_SensitiveWire::Dump(Standard_OStream& S,const Standard_Boolean FullDump) const
245 {
246   S<<"\tSensitiveWire 3D :"<<endl;;
247   if(HasLocation())
248     S<<"\t\tExisting Location"<<endl;
249   S<<"\t\tComposed Of "<<mysensitive.Length()<<" Sensitive Entities"<<endl;
250
251   for(Standard_Integer i=1;i<= mysensitive.Length();i++){
252     S<<"Sensitive #"<<i<<" : "<<endl;
253     mysensitive(i)->Dump(S,FullDump);}
254
255   S<<"\tEnd Of Sensitive Wire"<<endl;
256
257 }
258
259 //=======================================================================
260 //function : ComputeDepth
261 //purpose  :
262 //=======================================================================
263
264 Standard_Real Select3D_SensitiveWire::ComputeDepth(const gp_Lin& EyeLine) const
265 {
266
267   if(myDetectedIndex==-1)
268     // should be never called...
269     return Precision::Infinite();
270   return mysensitive(myDetectedIndex)->ComputeDepth(EyeLine);
271
272 }
273
274 //=======================================================================
275 //function : SetLastPrj
276 //purpose  :
277 //=======================================================================
278
279 void Select3D_SensitiveWire::SetLastPrj(const Handle(Select3D_Projector)& Prj)
280 {
281   Select3D_SensitiveEntity::SetLastPrj(Prj);
282   for(Standard_Integer i=1;i<=mysensitive.Length();i++)
283     mysensitive(i)->SetLastPrj(Prj);
284
285 }
286
287 //=======================================================================
288 //function : GetEdges
289 //purpose  : returns the sensitive edges stored in this wire
290 //=======================================================================
291
292 void Select3D_SensitiveWire::GetEdges( Select3D_SensitiveEntitySequence& theEdges )
293 {
294   theEdges.Clear();
295   theEdges.Append(mysensitive);
296 }
297
298 //=============================================================================
299 // Function : GetLastDetected
300 // Purpose  :
301 //=============================================================================
302
303 Handle(Select3D_SensitiveEntity) Select3D_SensitiveWire::GetLastDetected() const
304 {
305   Handle(Select3D_SensitiveEntity) aRes;
306
307   if ( myDetectedIndex >= 1 && myDetectedIndex <= mysensitive.Length() )
308     aRes = mysensitive.Value( myDetectedIndex );
309
310   return aRes;
311 }
312
313 //=======================================================================
314 //function : Set
315 //purpose  :
316 //=======================================================================
317
318 void Select3D_SensitiveWire::Set(const Handle_SelectBasics_EntityOwner &TheOwnerId) 
319 {
320   Select3D_SensitiveEntity::Set(TheOwnerId);
321
322   // Set TheOwnerId for each element of sensitive wire
323   for (Standard_Integer i = 1; i <= mysensitive.Length(); ++i) 
324   {
325     mysensitive.Value(i)->Set(TheOwnerId);
326   }
327 }