f751596e |
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 <SelectMgr_SensitiveEntitySet.hxx> |
17 | |
f751596e |
18 | #include <Select3D_SensitiveEntity.hxx> |
19 | #include <SelectMgr_SensitiveEntity.hxx> |
20 | |
f5b72419 |
21 | IMPLEMENT_STANDARD_RTTIEXT(SelectMgr_SensitiveEntitySet, BVH_PrimitiveSet3d) |
22 | |
f751596e |
23 | //======================================================================= |
24 | // function : SelectMgr_SensitiveEntitySet |
25 | // purpose : |
26 | //======================================================================= |
f5b72419 |
27 | SelectMgr_SensitiveEntitySet::SelectMgr_SensitiveEntitySet (const Handle(Select3D_BVHBuilder3d)& theBuilder) |
28 | : BVH_PrimitiveSet3d (theBuilder) |
f751596e |
29 | { |
f5b72419 |
30 | // |
f751596e |
31 | } |
32 | |
33 | //======================================================================= |
34 | // function : Append |
35 | // purpose : Adds new entity to the set and marks BVH tree for rebuild |
36 | //======================================================================= |
7ab15952 |
37 | void SelectMgr_SensitiveEntitySet::Append (const Handle(SelectMgr_SensitiveEntity)& theEntity) |
f751596e |
38 | { |
f5b72419 |
39 | if (!theEntity->BaseSensitive()->IsKind (STANDARD_TYPE(Select3D_SensitiveEntity))) |
f751596e |
40 | { |
41 | theEntity->ResetSelectionActiveStatus(); |
42 | return; |
43 | } |
266877a7 |
44 | |
45 | const Standard_Integer anExtent = mySensitives.Extent(); |
46 | if (mySensitives.Add (theEntity) > anExtent) |
47 | { |
48 | addOwner (theEntity->BaseSensitive()->OwnerId()); |
49 | } |
f751596e |
50 | MarkDirty(); |
51 | } |
52 | |
53 | //======================================================================= |
54 | // function : Append |
55 | // purpose : Adds every entity of selection theSelection to the set |
56 | // and marks BVH tree for rebuild |
57 | //======================================================================= |
58 | void SelectMgr_SensitiveEntitySet::Append (const Handle(SelectMgr_Selection)& theSelection) |
59 | { |
b5cce1ab |
60 | for (NCollection_Vector<Handle(SelectMgr_SensitiveEntity)>::Iterator aSelEntIter (theSelection->Entities()); aSelEntIter.More(); aSelEntIter.Next()) |
f751596e |
61 | { |
b5cce1ab |
62 | if (!aSelEntIter.Value()->BaseSensitive()->IsKind (STANDARD_TYPE(Select3D_SensitiveEntity))) |
f751596e |
63 | { |
b5cce1ab |
64 | aSelEntIter.Value()->ResetSelectionActiveStatus(); |
f751596e |
65 | continue; |
66 | } |
266877a7 |
67 | |
68 | const Standard_Integer anExtent = mySensitives.Extent(); |
69 | if (mySensitives.Add (aSelEntIter.Value()) > anExtent) |
70 | { |
71 | addOwner (aSelEntIter.Value()->BaseSensitive()->OwnerId()); |
72 | } |
f751596e |
73 | } |
74 | MarkDirty(); |
75 | } |
76 | |
f751596e |
77 | //======================================================================= |
78 | // function : Remove |
79 | // purpose : Removes every entity of selection theSelection from the set |
80 | // and marks BVH tree for rebuild |
81 | //======================================================================= |
82 | void SelectMgr_SensitiveEntitySet::Remove (const Handle(SelectMgr_Selection)& theSelection) |
83 | { |
b5cce1ab |
84 | for (NCollection_Vector<Handle(SelectMgr_SensitiveEntity)>::Iterator aSelEntIter (theSelection->Entities()); aSelEntIter.More(); aSelEntIter.Next()) |
f751596e |
85 | { |
b5cce1ab |
86 | const Standard_Integer anEntIdx = mySensitives.FindIndex (aSelEntIter.Value()); |
87 | if (anEntIdx == 0) |
88 | { |
ec81011f |
89 | continue; |
b5cce1ab |
90 | } |
f751596e |
91 | |
ec81011f |
92 | if (anEntIdx != mySensitives.Size()) |
f751596e |
93 | { |
ec81011f |
94 | Swap (anEntIdx - 1, mySensitives.Size() - 1); |
f751596e |
95 | } |
ec81011f |
96 | |
97 | mySensitives.RemoveLast(); |
266877a7 |
98 | removeOwner (aSelEntIter.Value()->BaseSensitive()->OwnerId()); |
f751596e |
99 | } |
ec81011f |
100 | |
101 | MarkDirty(); |
f751596e |
102 | } |
103 | |
104 | //======================================================================= |
105 | // function : Box |
106 | // purpose : Returns bounding box of entity with index theIdx |
107 | //======================================================================= |
108 | Select3D_BndBox3d SelectMgr_SensitiveEntitySet::Box (const Standard_Integer theIndex) const |
109 | { |
ec81011f |
110 | return GetSensitiveById (theIndex)->BaseSensitive()->BoundingBox(); |
f751596e |
111 | } |
112 | |
113 | //======================================================================= |
114 | // function : Center |
115 | // purpose : Returns geometry center of sensitive entity index theIdx |
116 | // along the given axis theAxis |
117 | //======================================================================= |
118 | Standard_Real SelectMgr_SensitiveEntitySet::Center (const Standard_Integer theIndex, |
119 | const Standard_Integer theAxis) const |
120 | { |
0ef04197 |
121 | const Handle(Select3D_SensitiveEntity)& aSensitive = GetSensitiveById (theIndex)->BaseSensitive(); |
f751596e |
122 | const gp_Pnt aCenter = aSensitive->CenterOfGeometry(); |
123 | Standard_Real aCenterCoord = 0.0; |
124 | aCenterCoord = theAxis == 0 ? aCenter.X() : |
125 | (theAxis == 1 ? aCenter.Y() : aCenter.Z()); |
126 | |
127 | return aCenterCoord; |
128 | } |
129 | |
130 | //======================================================================= |
131 | // function : Swap |
132 | // purpose : Swaps items with indexes theIdx1 and theIdx2 |
133 | //======================================================================= |
134 | void SelectMgr_SensitiveEntitySet::Swap (const Standard_Integer theIndex1, |
135 | const Standard_Integer theIndex2) |
136 | { |
3bf9a45f |
137 | mySensitives.Swap (theIndex1 + 1, theIndex2 + 1); |
f751596e |
138 | } |
139 | |
140 | //======================================================================= |
141 | // function : Size |
142 | // purpose : Returns the amount of entities |
143 | //======================================================================= |
144 | Standard_Integer SelectMgr_SensitiveEntitySet::Size() const |
145 | { |
ec81011f |
146 | return mySensitives.Size(); |
f751596e |
147 | } |
148 | |
149 | //======================================================================= |
150 | // function : GetSensitiveById |
151 | // purpose : Returns the entity with index theIndex in the set |
152 | //======================================================================= |
7ab15952 |
153 | const Handle(SelectMgr_SensitiveEntity)& SelectMgr_SensitiveEntitySet::GetSensitiveById |
f751596e |
154 | (const Standard_Integer theIndex) const |
155 | { |
ec81011f |
156 | return mySensitives.FindKey (theIndex + 1); |
f751596e |
157 | } |
266877a7 |
158 | |
159 | //======================================================================= |
160 | // function : addOwner |
161 | // purpose : |
162 | //======================================================================= |
163 | void SelectMgr_SensitiveEntitySet::addOwner (const Handle(SelectMgr_EntityOwner)& theOwner) |
164 | { |
165 | if (!theOwner.IsNull()) |
166 | { |
167 | if (Standard_Integer* aNumber = myOwnersMap.ChangeSeek (theOwner)) |
168 | { |
169 | ++(*aNumber); |
170 | } |
171 | else |
172 | { |
173 | myOwnersMap.Bind (theOwner, 1); |
174 | } |
175 | } |
176 | } |
177 | |
178 | //======================================================================= |
179 | // function : removeOwner |
180 | // purpose : |
181 | //======================================================================= |
182 | void SelectMgr_SensitiveEntitySet::removeOwner (const Handle(SelectMgr_EntityOwner)& theOwner) |
183 | { |
184 | if (Standard_Integer* aNumber = !theOwner.IsNull() ? myOwnersMap.ChangeSeek (theOwner) : NULL) |
185 | { |
186 | if (--(*aNumber) == 0) |
187 | { |
188 | myOwnersMap.UnBind (theOwner); |
189 | } |
190 | } |
191 | } |