0032337: Visualization - rename Overlaps() method in selection to more self-describab...
[occt.git] / src / SelectMgr / SelectMgr_SelectableObject.cxx
1 // Created on: 1995-02-20
2 // Created by: Mister rmi
3 // Copyright (c) 1995-1999 Matra Datavision
4 // Copyright (c) 1999-2014 OPEN CASCADE SAS
5 //
6 // This file is part of Open CASCADE Technology software library.
7 //
8 // This library is free software; you can redistribute it and/or modify it under
9 // the terms of the GNU Lesser General Public License version 2.1 as published
10 // by the Free Software Foundation, with special exception defined in the file
11 // OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
12 // distribution for complete text of the license and disclaimer of any warranty.
13 //
14 // Alternatively, this file may be used under the terms of Open CASCADE
15 // commercial license or contractual agreement.
16
17 #include <SelectMgr_SelectableObject.hxx>
18
19 #include <Graphic3d_AspectLine3d.hxx>
20 #include <Graphic3d_AspectMarker3d.hxx>
21 #include <Prs3d_Drawer.hxx>
22 #include <Prs3d_LineAspect.hxx>
23 #include <Prs3d_PlaneAspect.hxx>
24 #include <Prs3d_PointAspect.hxx>
25 #include <Prs3d_Presentation.hxx>
26 #include <PrsMgr_PresentationManager.hxx>
27 #include <Select3D_SensitiveEntity.hxx>
28 #include <SelectMgr_EntityOwner.hxx>
29 #include <SelectMgr_IndexedMapOfOwner.hxx>
30 #include <SelectMgr_Selection.hxx>
31 #include <SelectMgr_SelectionManager.hxx>
32 #include <Standard_NoSuchObject.hxx>
33 #include <Standard_NotImplemented.hxx>
34 #include <TopLoc_Location.hxx>
35
36 IMPLEMENT_STANDARD_RTTIEXT(SelectMgr_SelectableObject,PrsMgr_PresentableObject)
37
38 namespace
39 {
40   static const Handle(SelectMgr_Selection)   THE_NULL_SELECTION;
41   static const Handle(SelectMgr_EntityOwner) THE_NULL_ENTITYOWNER;
42 }
43
44 //==================================================
45 // Function: SelectMgr_SelectableObject
46 // Purpose :
47 //==================================================
48
49 SelectMgr_SelectableObject::SelectMgr_SelectableObject (const PrsMgr_TypeOfPresentation3d aTypeOfPresentation3d)
50 : PrsMgr_PresentableObject (aTypeOfPresentation3d),
51   myGlobalSelMode          (0),
52   myAutoHilight            (Standard_True)
53 {
54   //
55 }
56
57 //==================================================
58 // Function: Destructor
59 // Purpose : Clears all selections of the object
60 //==================================================
61 SelectMgr_SelectableObject::~SelectMgr_SelectableObject()
62 {
63   for (SelectMgr_SequenceOfSelection::Iterator aSelIter (myselections); aSelIter.More(); aSelIter.Next())
64   {
65     aSelIter.Value()->Clear();
66   }
67 }
68
69 //==================================================
70 // Function: RecomputePrimitives
71 // Purpose : IMPORTANT: Do not use this method to update
72 //           selection primitives except implementing custom
73 //           selection manager! This method does not take
74 //           into account necessary BVH updates, but may
75 //           invalidate the pointers it refers to.
76 //           TO UPDATE SELECTION properly from outside classes,
77 //           use method UpdateSelection.
78 //==================================================
79 void SelectMgr_SelectableObject::RecomputePrimitives()
80 {
81   for (SelectMgr_SequenceOfSelection::Iterator aSelIter (myselections); aSelIter.More(); aSelIter.Next())
82   {
83     RecomputePrimitives (aSelIter.Value()->Mode());
84   }
85 }
86
87 //==================================================
88 // Function: RecomputePrimitives
89 // Purpose : IMPORTANT: Do not use this method to update
90 //           selection primitives except implementing custom
91 //           selection manager! This method does not take
92 //           into account necessary BVH updates, but may
93 //           invalidate the pointers it refers to.
94 //           TO UPDATE SELECTION properly from outside classes,
95 //           use method UpdateSelection.
96 //==================================================
97 void SelectMgr_SelectableObject::RecomputePrimitives (const Standard_Integer theMode)
98 {
99   SelectMgr_SelectableObject* aSelParent = dynamic_cast<SelectMgr_SelectableObject* > (Parent());
100   for (SelectMgr_SequenceOfSelection::Iterator aSelIter (myselections); aSelIter.More(); aSelIter.Next())
101   {
102     const Handle(SelectMgr_Selection)& aSel = aSelIter.Value();
103     if (aSel->Mode() == theMode)
104     {
105       aSel->Clear();
106       ComputeSelection (aSel, theMode);
107       aSel->UpdateStatus (SelectMgr_TOU_Partial);
108       aSel->UpdateBVHStatus (SelectMgr_TBU_Renew);
109       if (theMode == 0 && aSelParent != NULL)
110       {
111         if (const Handle(SelectMgr_EntityOwner)& anAsmOwner = aSelParent->GetAssemblyOwner())
112         {
113           SetAssemblyOwner (anAsmOwner, theMode);
114         }
115       }
116       return;
117     }
118   }
119
120   Handle(SelectMgr_Selection) aNewSel = new SelectMgr_Selection (theMode);
121   ComputeSelection (aNewSel, theMode);
122
123   if (theMode == 0 && aSelParent != NULL)
124   {
125     if (const Handle(SelectMgr_EntityOwner)& anAsmOwner = aSelParent->GetAssemblyOwner())
126     {
127       SetAssemblyOwner (anAsmOwner, theMode);
128     }
129   }
130
131   aNewSel->UpdateStatus (SelectMgr_TOU_Partial);
132   aNewSel->UpdateBVHStatus (SelectMgr_TBU_Add);
133
134   myselections.Append (aNewSel);
135 }
136
137 //==================================================
138 // Function: ClearSelections
139 // Purpose :
140 //==================================================
141 void SelectMgr_SelectableObject::ClearSelections (const Standard_Boolean theToUpdate)
142 {
143   for (SelectMgr_SequenceOfSelection::Iterator aSelIter (myselections); aSelIter.More(); aSelIter.Next())
144   {
145     const Handle(SelectMgr_Selection)& aSel = aSelIter.Value();
146     aSel->Clear();
147     aSel->UpdateBVHStatus (SelectMgr_TBU_Remove);
148     if (theToUpdate)
149     {
150       aSel->UpdateStatus (SelectMgr_TOU_Full);
151     }
152   }
153 }
154
155
156 //==================================================
157 // Function: Selection
158 // Purpose :
159 //==================================================
160
161 const Handle(SelectMgr_Selection)& SelectMgr_SelectableObject::Selection (const Standard_Integer theMode) const
162 {
163   if (theMode == -1)
164   {
165     return THE_NULL_SELECTION;
166   }
167
168   for (SelectMgr_SequenceOfSelection::Iterator aSelIter (myselections); aSelIter.More(); aSelIter.Next())
169   {
170     const Handle(SelectMgr_Selection)& aSel = aSelIter.Value();
171     if (aSel->Mode() == theMode)
172     {
173       return aSel;
174     }
175   }
176   return THE_NULL_SELECTION;
177 }
178
179 //==================================================
180 // Function: AddSelection
181 // Purpose :
182 //==================================================
183
184 void SelectMgr_SelectableObject::AddSelection (const Handle(SelectMgr_Selection)& theSel,
185                                                const Standard_Integer theMode)
186 {
187   if(theSel->IsEmpty())
188   {
189     ComputeSelection(theSel, theMode);
190     theSel->UpdateStatus(SelectMgr_TOU_Partial);
191     theSel->UpdateBVHStatus (SelectMgr_TBU_Add);
192   }
193
194   Standard_Boolean isReplaced = Standard_False;
195   for (SelectMgr_SequenceOfSelection::Iterator aSelIter (myselections); aSelIter.More(); aSelIter.Next())
196   {
197     if (aSelIter.Value()->Mode() == theMode)
198     {
199       isReplaced = Standard_True;
200       myselections.Remove (aSelIter);
201       break;
202     }
203   }
204
205   myselections.Append (theSel);
206   if (isReplaced)
207   {
208     myselections.Last()->UpdateBVHStatus (SelectMgr_TBU_Renew);
209   }
210
211   if (theMode == 0)
212   {
213     SelectMgr_SelectableObject* aSelParent = dynamic_cast<SelectMgr_SelectableObject* > (Parent());
214     if (aSelParent != NULL)
215     {
216       if (const Handle(SelectMgr_EntityOwner)& anAsmOwner = aSelParent->GetAssemblyOwner())
217       {
218         SetAssemblyOwner (anAsmOwner, theMode);
219       }
220     }
221   }
222 }
223
224 //=======================================================================
225 //function : ReSetTransformation
226 //purpose  :
227 //=======================================================================
228 void SelectMgr_SelectableObject::ResetTransformation() 
229 {
230   for (SelectMgr_SequenceOfSelection::Iterator aSelIter (myselections); aSelIter.More(); aSelIter.Next())
231   {
232     const Handle(SelectMgr_Selection)& aSel = aSelIter.Value();
233     aSel->UpdateStatus (SelectMgr_TOU_Partial);
234     aSel->UpdateBVHStatus (SelectMgr_TBU_None);
235   }
236
237   PrsMgr_PresentableObject::ResetTransformation();
238 }
239
240 //=======================================================================
241 //function : UpdateTransformation
242 //purpose  :
243 //=======================================================================
244 void SelectMgr_SelectableObject::UpdateTransformation()
245 {
246   for (SelectMgr_SequenceOfSelection::Iterator aSelIter (myselections); aSelIter.More(); aSelIter.Next())
247   {
248     aSelIter.Value()->UpdateStatus (SelectMgr_TOU_Partial);
249   }
250
251   PrsMgr_PresentableObject::UpdateTransformation();
252   if (!mySelectionPrs.IsNull())
253   {
254     mySelectionPrs->SetTransformation (TransformationGeom());
255   }
256   if (!myHilightPrs.IsNull())
257   {
258     myHilightPrs->SetTransformation (TransformationGeom());
259   }
260 }
261
262 //=======================================================================
263 //function : UpdateTransformation
264 //purpose  :
265 //=======================================================================
266 void SelectMgr_SelectableObject::UpdateTransformations (const Handle(SelectMgr_Selection)& theSel)
267 {
268   const TopLoc_Location aSelfLocation (Transformation());
269   for (NCollection_Vector<Handle(SelectMgr_SensitiveEntity)>::Iterator aSelEntIter (theSel->Entities()); aSelEntIter.More(); aSelEntIter.Next())
270   {
271     if (const Handle(Select3D_SensitiveEntity)& aSensEntity = aSelEntIter.Value()->BaseSensitive())
272     {
273       if (const Handle(SelectMgr_EntityOwner)& aEOwner = aSensEntity->OwnerId())
274       {
275         aEOwner->SetLocation (aSelfLocation);
276       }
277     }
278   }
279 }
280
281 //=======================================================================
282 //function : HilightSelected
283 //purpose  :
284 //=======================================================================
285 void SelectMgr_SelectableObject::HilightSelected (const Handle(PrsMgr_PresentationManager)&,
286                                                   const SelectMgr_SequenceOfOwner&)
287 {
288   throw Standard_NotImplemented("SelectMgr_SelectableObject::HilightSelected");
289 }
290
291 //=======================================================================
292 //function : ClearSelected
293 //purpose  :
294 //=======================================================================
295 void SelectMgr_SelectableObject::ClearSelected()
296 {
297   if(!mySelectionPrs.IsNull())
298   {
299     mySelectionPrs->Clear();
300   }
301 }
302
303 //=======================================================================
304 //function : ClearDynamicHighlight
305 //purpose  :
306 //=======================================================================
307 void SelectMgr_SelectableObject::ClearDynamicHighlight (const Handle(PrsMgr_PresentationManager)& theMgr)
308 {
309   theMgr->ClearImmediateDraw();
310 }
311
312 //=======================================================================
313 //function : HilightOwnerWithColor
314 //purpose  :
315 //=======================================================================
316 void SelectMgr_SelectableObject::HilightOwnerWithColor (const Handle(PrsMgr_PresentationManager)&,
317                                                         const Handle(Prs3d_Drawer)&,
318                                                         const Handle(SelectMgr_EntityOwner)&)
319 {
320   throw Standard_NotImplemented("SelectMgr_SelectableObject::HilightOwnerWithColor");
321 }
322
323 //=======================================================================
324 //function : GetHilightPresentation
325 //purpose  :
326 //=======================================================================
327 Handle(Prs3d_Presentation) SelectMgr_SelectableObject::GetHilightPresentation (const Handle(PrsMgr_PresentationManager)& theMgr)
328 {
329   if (myHilightPrs.IsNull() && !theMgr.IsNull())
330   {
331     myHilightPrs = new Prs3d_Presentation (theMgr->StructureManager());
332     myHilightPrs->SetTransformPersistence (TransformPersistence());
333     myHilightPrs->SetClipPlanes (myClipPlanes);
334     myHilightPrs->SetTransformation (TransformationGeom());
335   }
336
337   return myHilightPrs;
338 }
339
340 //=======================================================================
341 //function : GetSelectPresentation
342 //purpose  :
343 //=======================================================================
344 Handle(Prs3d_Presentation) SelectMgr_SelectableObject::GetSelectPresentation (const Handle(PrsMgr_PresentationManager)& theMgr)
345 {
346   if (mySelectionPrs.IsNull() && !theMgr.IsNull())
347   {
348     mySelectionPrs = new Prs3d_Presentation (theMgr->StructureManager());
349     mySelectionPrs->SetTransformPersistence (TransformPersistence());
350     mySelectionPrs->SetClipPlanes (myClipPlanes);
351     mySelectionPrs->SetTransformation (TransformationGeom());
352   }
353
354   return mySelectionPrs;
355 }
356
357 //=======================================================================
358 //function : ErasePresentations
359 //purpose  :
360 //=======================================================================
361 void SelectMgr_SelectableObject::ErasePresentations (Standard_Boolean theToRemove)
362 {
363   if (!mySelectionPrs.IsNull())
364   {
365     mySelectionPrs->Erase();
366     if (theToRemove)
367     {
368       mySelectionPrs->Clear();
369       mySelectionPrs.Nullify();
370     }
371   }
372   if (!myHilightPrs.IsNull())
373   {
374     myHilightPrs->Erase();
375     if (theToRemove)
376     {
377       myHilightPrs->Clear();
378       myHilightPrs.Nullify();
379     }
380   }
381 }
382
383 //=======================================================================
384 //function : SetZLayer
385 //purpose  :
386 //=======================================================================
387 void SelectMgr_SelectableObject::SetZLayer (const Graphic3d_ZLayerId theLayerId)
388 {
389   // update own presentations
390   PrsMgr_PresentableObject::SetZLayer (theLayerId);
391
392   // update selection presentations
393   if (!mySelectionPrs.IsNull())
394     mySelectionPrs->SetZLayer (theLayerId);
395
396   if (!myHilightPrs.IsNull())
397     myHilightPrs->SetZLayer (theLayerId);
398
399   // update all entity owner presentations
400   for (SelectMgr_SequenceOfSelection::Iterator aSelIter (myselections); aSelIter.More(); aSelIter.Next())
401   {
402     const Handle(SelectMgr_Selection)& aSel = aSelIter.Value();
403     for (NCollection_Vector<Handle(SelectMgr_SensitiveEntity)>::Iterator aSelEntIter (aSel->Entities()); aSelEntIter.More(); aSelEntIter.Next())
404     {
405       if (const Handle(Select3D_SensitiveEntity)& aEntity = aSelEntIter.Value()->BaseSensitive())
406       {
407         if (const Handle(SelectMgr_EntityOwner)& aOwner = aEntity->OwnerId())
408         {
409           aOwner->SetZLayer (theLayerId);
410         }
411       }
412     }
413   }
414 }
415
416 //=======================================================================
417 //function : UpdateClipping
418 //purpose  :
419 //=======================================================================
420 void SelectMgr_SelectableObject::UpdateClipping()
421 {
422   PrsMgr_PresentableObject::UpdateClipping();
423   if (!mySelectionPrs.IsNull())
424   {
425     mySelectionPrs->SetClipPlanes (myClipPlanes);
426   }
427   if (!myHilightPrs.IsNull())
428   {
429     myHilightPrs->SetClipPlanes (myClipPlanes);
430   }
431 }
432
433 //=======================================================================
434 //function : updateSelection
435 //purpose  : Sets update status FULL to selections of the object. Must be
436 //           used as the only method of UpdateSelection from outer classes
437 //           to prevent BVH structures from being outdated.
438 //=======================================================================
439 void SelectMgr_SelectableObject::updateSelection (const Standard_Integer theMode)
440 {
441   if (theMode == -1)
442   {
443     for (SelectMgr_SequenceOfSelection::Iterator aSelIter (myselections); aSelIter.More(); aSelIter.Next())
444     {
445       const Handle(SelectMgr_Selection)& aSel = aSelIter.Value();
446       aSel->UpdateStatus (SelectMgr_TOU_Full);
447     }
448     return;
449   }
450
451   for (SelectMgr_SequenceOfSelection::Iterator aSelIter (myselections); aSelIter.More(); aSelIter.Next())
452   {
453     const Handle(SelectMgr_Selection)& aSel = aSelIter.Value();
454     if (aSel->Mode() == theMode)
455     {
456       aSel->UpdateStatus (SelectMgr_TOU_Full);
457       return;
458     }
459   }
460 }
461
462 //=======================================================================
463 //function : SetAssemblyOwner
464 //purpose  : Sets common entity owner for assembly sensitive object entities
465 //=======================================================================
466 void SelectMgr_SelectableObject::SetAssemblyOwner (const Handle(SelectMgr_EntityOwner)& theOwner,
467                                                    const Standard_Integer theMode)
468 {
469   if (theMode == -1)
470   {
471     for (SelectMgr_SequenceOfSelection::Iterator aSelIter (myselections); aSelIter.More(); aSelIter.Next())
472     {
473       const Handle(SelectMgr_Selection)& aSel = aSelIter.Value();
474       for (NCollection_Vector<Handle(SelectMgr_SensitiveEntity)>::Iterator aSelEntIter (aSel->Entities()); aSelEntIter.More(); aSelEntIter.Next())
475       {
476         aSelEntIter.Value()->BaseSensitive()->Set (theOwner);
477       }
478     }
479     return;
480   }
481
482   for (SelectMgr_SequenceOfSelection::Iterator aSelIter (myselections); aSelIter.More(); aSelIter.Next())
483   {
484     const Handle(SelectMgr_Selection)& aSel = aSelIter.Value();
485     if (aSel->Mode() == theMode)
486     {
487       for (NCollection_Vector<Handle(SelectMgr_SensitiveEntity)>::Iterator aSelEntIter (aSel->Entities()); aSelEntIter.More(); aSelEntIter.Next())
488       {
489         aSelEntIter.Value()->BaseSensitive()->Set (theOwner);
490       }
491       return;
492     }
493   }
494 }
495
496 //=======================================================================
497 //function : BndBoxOfSelected
498 //purpose  :
499 //=======================================================================
500 Bnd_Box SelectMgr_SelectableObject::BndBoxOfSelected (const Handle(SelectMgr_IndexedMapOfOwner)& theOwners)
501 {
502   if (theOwners->IsEmpty())
503     return Bnd_Box();
504
505   Bnd_Box aBnd;
506   for (SelectMgr_SequenceOfSelection::Iterator aSelIter (myselections); aSelIter.More(); aSelIter.Next())
507   {
508     const Handle(SelectMgr_Selection)& aSel = aSelIter.Value();
509     if (aSel->GetSelectionState() != SelectMgr_SOS_Activated)
510       continue;
511
512     for (NCollection_Vector<Handle(SelectMgr_SensitiveEntity)>::Iterator aSelEntIter (aSel->Entities()); aSelEntIter.More(); aSelEntIter.Next())
513     {
514       const Handle(SelectMgr_EntityOwner)& anOwner = aSelEntIter.Value()->BaseSensitive()->OwnerId();
515       if (theOwners->Contains (anOwner))
516       {
517         Select3D_BndBox3d aBox = aSelEntIter.Value()->BaseSensitive()->BoundingBox();
518         aBnd.Update (aBox.CornerMin().x(), aBox.CornerMin().y(), aBox.CornerMin().z(),
519                      aBox.CornerMax().x(), aBox.CornerMax().y(), aBox.CornerMax().z());
520       }
521     }
522   }
523
524   return aBnd;
525 }
526
527 //=======================================================================
528 //function : GlobalSelOwner
529 //purpose  : Returns entity owner corresponding to selection of the object as a whole
530 //=======================================================================
531 Handle(SelectMgr_EntityOwner) SelectMgr_SelectableObject::GlobalSelOwner() const
532 {
533   const Handle(SelectMgr_Selection)& aGlobalSel = Selection (myGlobalSelMode);
534   if (!aGlobalSel.IsNull()
535    && !aGlobalSel->IsEmpty())
536   {
537     return aGlobalSel->Entities().First()->BaseSensitive()->OwnerId();
538   }
539   return THE_NULL_ENTITYOWNER;
540 }
541
542 //=======================================================================
543 //function : GetAssemblyOwner
544 //purpose  :
545 //=======================================================================
546 const Handle(SelectMgr_EntityOwner)& SelectMgr_SelectableObject::GetAssemblyOwner() const
547 {
548   return THE_NULL_ENTITYOWNER;
549 }
550
551 // =======================================================================
552 // function : DumpJson
553 // purpose  :
554 // =======================================================================
555 void SelectMgr_SelectableObject::DumpJson (Standard_OStream& theOStream, Standard_Integer theDepth) const
556 {
557   OCCT_DUMP_TRANSIENT_CLASS_BEGIN (theOStream)
558
559   OCCT_DUMP_BASE_CLASS (theOStream, theDepth, PrsMgr_PresentableObject)
560
561   OCCT_DUMP_FIELD_VALUES_DUMPED (theOStream, theDepth, mySelectionPrs.get())
562   OCCT_DUMP_FIELD_VALUES_DUMPED (theOStream, theDepth, myHilightPrs.get())
563
564   for (SelectMgr_SequenceOfSelection::Iterator anIterator (myselections); anIterator.More(); anIterator.Next())
565   {
566     const Handle(SelectMgr_Selection)& aSelection = anIterator.Value();
567     OCCT_DUMP_FIELD_VALUES_DUMPED (theOStream, theDepth, aSelection.get())
568   }
569
570   OCCT_DUMP_FIELD_VALUE_NUMERICAL (theOStream, myGlobalSelMode)
571   OCCT_DUMP_FIELD_VALUE_NUMERICAL (theOStream, myAutoHilight)
572 }