0026344: Visualization - provide a support of zoom persistent selection
[occt.git] / src / SelectMgr / SelectMgr_SelectableObjectSet.cxx
1 // Created on: 2014-08-15
2 // Created by: Varvara POSKONINA
3 // Copyright (c) 2005-2014 OPEN CASCADE SAS
4 //
5 // This file is part of Open CASCADE Technology software library.
6 //
7 // This library is free software; you can redistribute it and/or modify it under
8 // the terms of the GNU Lesser General Public License version 2.1 as published
9 // by the Free Software Foundation, with special exception defined in the file
10 // OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
11 // distribution for complete text of the license and disclaimer of any warranty.
12 //
13 // Alternatively, this file may be used under the terms of Open CASCADE
14 // commercial license or contractual agreement.
15
16 #include <Bnd_Box.hxx>
17 #include <BVH_BinnedBuilder.hxx>
18
19 #include <SelectMgr_SelectableObjectSet.hxx>
20
21 //=======================================================================
22 // function : SelectMgr_SelectableObjectSet
23 // purpose  : Creates new empty objects set and initializes BVH tree
24 //            builder to Binned builder with 1 element per list
25 //=======================================================================
26 SelectMgr_SelectableObjectSet::SelectMgr_SelectableObjectSet()
27 {
28   myBuilder = new BVH_BinnedBuilder<Standard_Real, 3, 32> (1, 32, Standard_False);
29 }
30
31 //=======================================================================
32 // function : Append
33 // purpose  : Adds new object to the set and marks BVH tree for rebuild
34 //=======================================================================
35 Standard_Boolean SelectMgr_SelectableObjectSet::Append (const Handle(SelectMgr_SelectableObject)& theObject)
36 {
37   Standard_Integer aSize = Size();
38
39   if (aSize < myObjects.Add (theObject))
40   {
41     MarkDirty();
42
43     return Standard_True;
44   }
45
46   return Standard_False;
47 }
48
49 //=======================================================================
50 // function : Remove
51 // purpose  : Removes object theObject from set and marks BVH tree for
52 //            rebuild
53 //=======================================================================
54 Standard_Boolean SelectMgr_SelectableObjectSet::Remove (const Handle(SelectMgr_SelectableObject)& theObject)
55 {
56   const Standard_Integer anIndex = myObjects.FindIndex (theObject);
57
58   if (anIndex != 0)
59   {
60     if (anIndex != Size())
61     {
62       Swap (anIndex - 1, Size() - 1);
63     }
64
65     myObjects.RemoveLast();
66
67     MarkDirty();
68
69     return Standard_True;
70   }
71
72   return Standard_False;
73 }
74
75 //=======================================================================
76 // function : Box
77 // purpose  : Returns bounding box of object with index theIndex
78 //=======================================================================
79 Select3D_BndBox3d SelectMgr_SelectableObjectSet::Box (const Standard_Integer theIndex) const
80 {
81   const Handle(SelectMgr_SelectableObject)& anObject = GetObjectById (theIndex);
82   Bnd_Box aBox;
83   anObject->BoundingBox (aBox);
84   if (aBox.IsVoid())
85     return Select3D_BndBox3d();
86
87   return Select3D_BndBox3d (SelectMgr_Vec3 (aBox.CornerMin().X(), aBox.CornerMin().Y(), aBox.CornerMin().Z()),
88                             SelectMgr_Vec3 (aBox.CornerMax().X(), aBox.CornerMax().Y(), aBox.CornerMax().Z()));
89 }
90
91 //=======================================================================
92 // function : Center
93 // purpose  : Returns center of object with index theIndex in the set
94 //            along the given axis theAxis
95 //=======================================================================
96 Standard_Real SelectMgr_SelectableObjectSet::Center (const Standard_Integer theIndex,
97                                                      const Standard_Integer theAxis) const
98 {
99   Select3D_BndBox3d aBndBox = Box (theIndex);
100
101   return (aBndBox.CornerMin()[theAxis] +
102           aBndBox.CornerMax()[theAxis]) * 0.5;
103 }
104
105 //=======================================================================
106 // function : Swap
107 // purpose  : Swaps items with indexes theIndex1 and theIndex2 in the set
108 //=======================================================================
109 void SelectMgr_SelectableObjectSet::Swap (const Standard_Integer theIndex1,
110                                           const Standard_Integer theIndex2)
111 {
112   const Standard_Integer aIndex1 = theIndex1 + 1;
113   const Standard_Integer aIndex2 = theIndex2 + 1;
114
115   Handle(SelectMgr_SelectableObject) anObject1 = myObjects.FindKey (aIndex1);
116   Handle(SelectMgr_SelectableObject) anObject2 = myObjects.FindKey (aIndex2);
117
118   myObjects.Substitute (aIndex1, EMPTY_OBJ);
119   myObjects.Substitute (aIndex2, anObject1);
120   myObjects.Substitute (aIndex1, anObject2);
121 }
122
123 //=======================================================================
124 // function : Size
125 // purpose  : Returns size of objects set
126 //=======================================================================
127 Standard_Integer SelectMgr_SelectableObjectSet::Size() const
128 {
129   return myObjects.Size();
130 }