0024070: OpenGL capped object-level clipping planes
[occt.git] / src / Select3D / Select3D_SensitiveGroup.cxx
1 // Created on: 1998-04-16
2 // Created by: Robert COUBLANC
3 // Copyright (c) 1998-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
23 #include <Select3D_SensitiveGroup.ixx>
24 #include <Select3D_ListIteratorOfListOfSensitive.hxx>
25 #include <Precision.hxx>
26
27 //=======================================================================
28 //function : Creation
29 //purpose  : 
30 //=======================================================================
31 Select3D_SensitiveGroup::Select3D_SensitiveGroup(const Handle(SelectBasics_EntityOwner)& OwnerId,
32                                                  const Standard_Boolean MatchAll):
33 Select3D_SensitiveEntity(OwnerId),
34 myMustMatchAll(MatchAll)
35 {
36 }
37
38 //=======================================================================
39 //function : Creation
40 //purpose  : 
41 //=======================================================================
42
43 Select3D_SensitiveGroup::Select3D_SensitiveGroup(const Handle(SelectBasics_EntityOwner)& OwnerId,
44                                                  Select3D_ListOfSensitive& TheList, 
45                                                  const Standard_Boolean MatchAll):
46 Select3D_SensitiveEntity(OwnerId),
47 myMustMatchAll(MatchAll)
48 {
49   myList.Append(TheList);
50 }
51
52 //=======================================================================
53 //function : Add
54 //purpose  : No control of  entities inside 
55 //=======================================================================
56
57 void Select3D_SensitiveGroup::Add(Select3D_ListOfSensitive& LL) 
58 {myList.Append(LL);}
59
60 //=======================================================================
61 //function : Add
62 //purpose  : 
63 //=======================================================================
64
65 void Select3D_SensitiveGroup::Add(const Handle(Select3D_SensitiveEntity)& aSensitive) 
66 {
67   for(Select3D_ListIteratorOfListOfSensitive It(myList);It.More();It.Next())
68   {
69     if(It.Value()==aSensitive) return;
70   }
71   myList.Append(aSensitive);
72 }
73
74 //=======================================================================
75 //function : Remove
76 //purpose  : 
77 //=======================================================================
78
79 void Select3D_SensitiveGroup::Remove(const Handle(Select3D_SensitiveEntity)& aSensitive) 
80 {
81   for(Select3D_ListIteratorOfListOfSensitive It(myList);It.More();It.Next())
82   {
83     if(It.Value()==aSensitive)
84     {
85       myList.Remove(It);
86       return;
87     }
88   }
89 }
90
91 //=======================================================================
92 //function : IsIn
93 //purpose  : 
94 //=======================================================================
95
96 Standard_Boolean Select3D_SensitiveGroup::IsIn(const Handle(Select3D_SensitiveEntity)& aSensitive) const
97 {
98   for(Select3D_ListIteratorOfListOfSensitive It(myList);It.More();It.Next())
99   {
100     if(It.Value()==aSensitive)
101       return Standard_True;
102   }
103   return Standard_False;
104 }
105
106 //=======================================================================
107 //function : Clear
108 //purpose  : 
109 //=======================================================================
110
111 void Select3D_SensitiveGroup::Clear()
112 {myList.Clear();}
113
114 //=======================================================================
115 //function : Project
116 //purpose  : 
117 //=======================================================================
118
119 void Select3D_SensitiveGroup::Project(const Handle(Select3D_Projector)& aProjector) 
120 {
121   for(Select3D_ListIteratorOfListOfSensitive It(myList);It.More();It.Next()) 
122   {
123     It.Value()->Project(aProjector);
124   }
125 }
126
127 //=======================================================================
128 //function : Areas
129 //purpose  : 
130 //=======================================================================
131
132 void Select3D_SensitiveGroup::Areas(SelectBasics_ListOfBox2d& boxes) 
133 {
134   for(Select3D_ListIteratorOfListOfSensitive It(myList);It.More();It.Next()) 
135   {
136     It.Value()->Areas(boxes);
137   }
138 }
139
140 //=======================================================================
141 //function : GetConnected
142 //purpose  : 
143 //=======================================================================
144
145 Handle(Select3D_SensitiveEntity) Select3D_SensitiveGroup::GetConnected(const TopLoc_Location& aLocation) 
146 {
147   Handle(Select3D_SensitiveGroup) newgroup = new Select3D_SensitiveGroup(myOwnerId,myMustMatchAll);
148   Select3D_ListOfSensitive LL;
149   for(Select3D_ListIteratorOfListOfSensitive It(myList);It.More();It.Next()) 
150   {
151     LL.Append(It.Value()->GetConnected(aLocation));
152   }
153   newgroup->Add(LL);
154   return newgroup;
155 }
156
157 //=======================================================================
158 //function : SetLocation
159 //purpose  : 
160 //=======================================================================
161
162 void Select3D_SensitiveGroup::SetLocation(const TopLoc_Location& aLoc) 
163 {
164   if(aLoc.IsIdentity()) return;
165
166   for(Select3D_ListIteratorOfListOfSensitive It(myList);It.More();It.Next())
167   {
168     It.Value()->SetLocation(aLoc);
169   }
170
171   if(HasLocation())
172     if(aLoc == Location()) return;
173   
174   Select3D_SensitiveEntity::SetLocation(aLoc);
175   for(Select3D_ListIteratorOfListOfSensitive It(myList);It.More();It.Next()) 
176   {
177     if(It.Value()->HasLocation())
178     {
179       if(It.Value()->Location()!=aLoc) 
180         It.Value()->SetLocation(It.Value()->Location()*aLoc);
181     }
182     else
183       It.Value()->SetLocation(aLoc);
184   }
185 }
186
187 //=======================================================================
188 //function : ResetLocation
189 //purpose  : 
190 //=======================================================================
191
192 void Select3D_SensitiveGroup::ResetLocation() 
193 {
194  if(!HasLocation()) return;
195  for(Select3D_ListIteratorOfListOfSensitive It(myList);It.More();It.Next())
196  {
197    if(It.Value()->HasLocation() && It.Value()->Location()!=Location())
198      It.Value()->SetLocation(It.Value()->Location()*Location().Inverted());
199    else
200      It.Value()->ResetLocation();
201  }
202  Select3D_SensitiveEntity::ResetLocation();
203 }
204
205 //=======================================================================
206 //function : Matches
207 //purpose  : 
208 //=======================================================================
209
210 Standard_Boolean Select3D_SensitiveGroup::Matches (const SelectBasics_PickArgs& thePickArgs,
211                                                    Standard_Real& theMatchDMin,
212                                                    Standard_Real& theMatchDepth)
213 {
214   theMatchDMin = RealLast();
215   theMatchDepth = RealLast();
216   Standard_Real aChildDMin, aChildDepth;
217   Standard_Boolean isMatched = Standard_False;
218
219   Select3D_ListIteratorOfListOfSensitive anIt (myList);
220   for (; anIt.More(); anIt.Next())
221   {
222     Handle(SelectBasics_SensitiveEntity)& aChild = anIt.Value();
223     if (!aChild->Matches (thePickArgs, aChildDMin, aChildDepth))
224     {
225       continue;
226     }
227
228     if (!isMatched)
229     {
230       theMatchDMin = aChildDMin;
231       isMatched = Standard_True;
232     }
233
234     theMatchDepth = Min (aChildDepth, theMatchDepth);
235   }
236
237   return isMatched;
238 }
239
240 //=======================================================================
241 //function : Matches
242 //purpose  :  si on doit tout matcher, on ne repond oui que si toutes
243 //            les primitives repondent oui
244 //=======================================================================
245 Standard_Boolean Select3D_SensitiveGroup::Matches(const Standard_Real XMin, 
246                                                   const Standard_Real YMin, 
247                                                   const Standard_Real XMax, 
248                                                   const Standard_Real YMax, 
249                                                   const Standard_Real aTol) 
250 {
251   Standard_Boolean result(Standard_True);
252   
253   for(Select3D_ListIteratorOfListOfSensitive It(myList);It.More();It.Next())
254   {
255     if(It.Value()->Matches(XMin,YMin,XMax,YMax,aTol))
256     {
257       if(!myMustMatchAll)
258         return Standard_True;
259     }
260     // ca ne matches pas..
261     else 
262     {
263       if(myMustMatchAll) 
264         return Standard_False;
265       else 
266         result = Standard_False;
267     }
268   }
269   return result;
270 }
271
272 //=======================================================================
273 //function : Matches
274 //purpose  : 
275 //=======================================================================
276
277 Standard_Boolean Select3D_SensitiveGroup::
278 Matches (const TColgp_Array1OfPnt2d& aPoly,
279          const Bnd_Box2d& aBox,
280          const Standard_Real aTol)
281
282   Standard_Boolean result(Standard_True);
283   
284   for(Select3D_ListIteratorOfListOfSensitive It(myList);It.More();It.Next())
285   {
286     if(It.Value()->Matches(aPoly, aBox, aTol))
287     {
288       if(!myMustMatchAll) 
289         return Standard_True;
290     }
291     else 
292     {
293       if(myMustMatchAll) 
294         return Standard_False;
295       else 
296         result = Standard_False;
297     }
298   }
299   return result;
300 }
301
302 //=======================================================================
303 //function : MaxBoxes
304 //purpose  : 
305 //=======================================================================
306
307 Standard_Integer Select3D_SensitiveGroup::MaxBoxes() const
308 {
309   Standard_Integer nbboxes(0);
310   for(Select3D_ListIteratorOfListOfSensitive It(myList);It.More();It.Next()){
311     nbboxes+=It.Value()->MaxBoxes();
312   }
313   return nbboxes;
314 }
315
316 //=======================================================================
317 //function : Set
318 //purpose  : 
319 //=======================================================================
320
321 void Select3D_SensitiveGroup::Set 
322   (const Handle(SelectBasics_EntityOwner)& TheOwnerId)
323
324   Select3D_SensitiveEntity::Set(TheOwnerId);
325   // set TheOwnerId for each element of sensitive group
326   for(Select3D_ListIteratorOfListOfSensitive It(myList);It.More();It.Next())
327     It.Value()->Set(TheOwnerId);
328 }