26f186ab5e2685867d38453968012257fce60145
[occt.git] / src / SelectMgr / SelectMgr_SensitiveEntitySet.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 <SelectMgr_SensitiveEntitySet.hxx>
17
18 #include <BVH_BinnedBuilder.hxx>
19
20 #include <Select3D_SensitiveEntity.hxx>
21 #include <SelectMgr_SensitiveEntity.hxx>
22
23 //=======================================================================
24 // function : SelectMgr_SensitiveEntitySet
25 // purpose  :
26 //=======================================================================
27 SelectMgr_SensitiveEntitySet::SelectMgr_SensitiveEntitySet()
28 {
29   myBuilder = new BVH_BinnedBuilder<Standard_Real, 3, 4> (1, 32, Standard_True);
30 }
31
32 //=======================================================================
33 // function : Append
34 // purpose  : Adds new entity to the set and marks BVH tree for rebuild
35 //=======================================================================
36 void SelectMgr_SensitiveEntitySet::Append (const Handle(SelectMgr_SensitiveEntity)& theEntity)
37 {
38   if (!theEntity->BaseSensitive()->IsKind ("Select3D_SensitiveEntity"))
39   {
40     theEntity->ResetSelectionActiveStatus();
41     return;
42   }
43   mySensitives.Add (theEntity);
44   MarkDirty();
45 }
46
47 //=======================================================================
48 // function : Append
49 // purpose  : Adds every entity of selection theSelection to the set
50 //            and marks BVH tree for rebuild
51 //=======================================================================
52 void SelectMgr_SensitiveEntitySet::Append (const Handle(SelectMgr_Selection)& theSelection)
53 {
54   for (theSelection->Init(); theSelection->More(); theSelection->Next())
55   {
56     if (!theSelection->Sensitive()->BaseSensitive()->IsKind ("Select3D_SensitiveEntity"))
57     {
58       theSelection->Sensitive()->ResetSelectionActiveStatus();
59       continue;
60     }
61     mySensitives.Add (theSelection->Sensitive());
62   }
63   MarkDirty();
64 }
65
66 //=======================================================================
67 // function : Remove
68 // purpose  : Removes every entity of selection theSelection from the set
69 //            and marks BVH tree for rebuild
70 //=======================================================================
71 void SelectMgr_SensitiveEntitySet::Remove (const Handle(SelectMgr_Selection)& theSelection)
72 {
73   for (theSelection->Init(); theSelection->More(); theSelection->Next())
74   {
75     Standard_Integer anEntIdx = mySensitives.FindIndex (theSelection->Sensitive());
76     if (!anEntIdx)
77       continue;
78
79     if (anEntIdx != mySensitives.Size())
80     {
81       Swap (anEntIdx - 1, mySensitives.Size() - 1);
82     }
83
84     mySensitives.RemoveLast();
85   }
86
87   MarkDirty();
88 }
89
90 //=======================================================================
91 // function : Box
92 // purpose  : Returns bounding box of entity with index theIdx
93 //=======================================================================
94 Select3D_BndBox3d SelectMgr_SensitiveEntitySet::Box (const Standard_Integer theIndex) const
95 {
96   return GetSensitiveById (theIndex)->BaseSensitive()->BoundingBox();
97 }
98
99 //=======================================================================
100 // function : Center
101 // purpose  : Returns geometry center of sensitive entity index theIdx
102 //            along the given axis theAxis
103 //=======================================================================
104 Standard_Real SelectMgr_SensitiveEntitySet::Center (const Standard_Integer theIndex,
105                                                     const Standard_Integer theAxis) const
106 {
107   const Handle(SelectBasics_SensitiveEntity)& aBasicEntity =
108     GetSensitiveById (theIndex)->BaseSensitive();
109   Handle(Select3D_SensitiveEntity) aSensitive (Handle(Select3D_SensitiveEntity)::DownCast (aBasicEntity));
110   const gp_Pnt aCenter = aSensitive->CenterOfGeometry();
111   Standard_Real aCenterCoord = 0.0;
112   aCenterCoord = theAxis == 0 ? aCenter.X() :
113     (theAxis == 1 ? aCenter.Y() : aCenter.Z());
114
115   return aCenterCoord;
116 }
117
118 //=======================================================================
119 // function : Swap
120 // purpose  : Swaps items with indexes theIdx1 and theIdx2
121 //=======================================================================
122 void SelectMgr_SensitiveEntitySet::Swap (const Standard_Integer theIndex1,
123                                          const Standard_Integer theIndex2)
124 {
125   mySensitives.Swap (theIndex1 + 1, theIndex2 + 1);
126 }
127
128 //=======================================================================
129 // function : Size
130 // purpose  : Returns the amount of entities
131 //=======================================================================
132 Standard_Integer SelectMgr_SensitiveEntitySet::Size() const
133 {
134   return mySensitives.Size();
135 }
136
137 //=======================================================================
138 // function : GetSensitiveById
139 // purpose  : Returns the entity with index theIndex in the set
140 //=======================================================================
141 const Handle(SelectMgr_SensitiveEntity)& SelectMgr_SensitiveEntitySet::GetSensitiveById
142   (const Standard_Integer theIndex) const
143 {
144   return mySensitives.FindKey (theIndex + 1);
145 }