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