0026029: Visualization - Poor performance of connected objects
[occt.git] / src / SelectMgr / SelectMgr_SensitiveEntitySet.cxx
CommitLineData
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
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//=======================================================================
27SelectMgr_SensitiveEntitySet::SelectMgr_SensitiveEntitySet()
28{
29 myBuilder = new BVH_BinnedBuilder<Standard_Real, 3, 32> (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//=======================================================================
7ab15952 36void SelectMgr_SensitiveEntitySet::Append (const Handle(SelectMgr_SensitiveEntity)& theEntity)
f751596e 37{
38 if (!theEntity->BaseSensitive()->IsKind ("Select3D_SensitiveEntity"))
39 {
40 theEntity->ResetSelectionActiveStatus();
41 return;
42 }
43 myEntities.Append (theEntity);
44 myEntityIdxs.Append (myEntities.Size());
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//=======================================================================
53void SelectMgr_SensitiveEntitySet::Append (const Handle(SelectMgr_Selection)& theSelection)
54{
55 for (theSelection->Init(); theSelection->More(); theSelection->Next())
56 {
57 if (!theSelection->Sensitive()->BaseSensitive()->IsKind ("Select3D_SensitiveEntity"))
58 {
59 theSelection->Sensitive()->ResetSelectionActiveStatus();
60 continue;
61 }
62 myEntities.Append (theSelection->Sensitive());
63 myEntityIdxs.Append (myEntities.Size());
64 }
65 MarkDirty();
66}
67
68//=======================================================================
69// function : Remove
70// purpose : Removes entity from the set and marks BVH tree for rebuild
71//=======================================================================
7ab15952 72void SelectMgr_SensitiveEntitySet::Remove (const Handle(SelectMgr_SensitiveEntity)& theEntity)
f751596e 73{
74 for (Standard_Integer anEntityIdx = 1; anEntityIdx <= myEntities.Size(); ++anEntityIdx)
75 {
76 if (myEntities.Value (anEntityIdx) == theEntity)
77 {
78 myEntities.Remove (anEntityIdx);
79 myEntityIdxs.Clear();
80 for (Standard_Integer anEntityIndexesIter = 1; anEntityIndexesIter <= myEntities.Size(); ++anEntityIndexesIter)
81 {
82 myEntityIdxs.Append (anEntityIndexesIter);
83 }
84 MarkDirty();
85 break;
86 }
87 }
88}
89
90//=======================================================================
91// function : Remove
92// purpose : Removes every entity of selection theSelection from the set
93// and marks BVH tree for rebuild
94//=======================================================================
95void SelectMgr_SensitiveEntitySet::Remove (const Handle(SelectMgr_Selection)& theSelection)
96{
97 for (theSelection->Init(); theSelection->More(); theSelection->Next())
98 {
99 for (Standard_Integer anEntityIdx = 1; anEntityIdx <= myEntities.Size(); ++anEntityIdx)
100 {
101 if (myEntities.Value (anEntityIdx) == theSelection->Sensitive())
102 {
103 myEntities.Remove (anEntityIdx);
104 MarkDirty();
105 }
106 }
107 }
108
109 if (BVH_Object<Standard_Real, 3>::myIsDirty)
110 {
111 myEntityIdxs.Clear();
112 for (Standard_Integer anEntityIdxsIter = 1; anEntityIdxsIter <= myEntities.Size(); ++anEntityIdxsIter)
113 {
114 myEntityIdxs.Append (anEntityIdxsIter);
115 }
116 }
117}
118
119//=======================================================================
120// function : Box
121// purpose : Returns bounding box of entity with index theIdx
122//=======================================================================
123Select3D_BndBox3d SelectMgr_SensitiveEntitySet::Box (const Standard_Integer theIndex) const
124{
125 Standard_Integer anEntityIdx = myEntityIdxs.Value (theIndex + 1);
126 return myEntities.Value (anEntityIdx)->BaseSensitive()->BoundingBox();
127}
128
129//=======================================================================
130// function : Center
131// purpose : Returns geometry center of sensitive entity index theIdx
132// along the given axis theAxis
133//=======================================================================
134Standard_Real SelectMgr_SensitiveEntitySet::Center (const Standard_Integer theIndex,
135 const Standard_Integer theAxis) const
136{
137 Standard_Integer anEntityIdx = myEntityIdxs.Value (theIndex + 1);
138 const Handle(SelectBasics_SensitiveEntity)& aBasicEntity =
139 myEntities.Value (anEntityIdx)->BaseSensitive();
140 const Handle(Select3D_SensitiveEntity)& aSensitive =
141 Handle(Select3D_SensitiveEntity)::DownCast (aBasicEntity);
142 const gp_Pnt aCenter = aSensitive->CenterOfGeometry();
143 Standard_Real aCenterCoord = 0.0;
144 aCenterCoord = theAxis == 0 ? aCenter.X() :
145 (theAxis == 1 ? aCenter.Y() : aCenter.Z());
146
147 return aCenterCoord;
148}
149
150//=======================================================================
151// function : Swap
152// purpose : Swaps items with indexes theIdx1 and theIdx2
153//=======================================================================
154void SelectMgr_SensitiveEntitySet::Swap (const Standard_Integer theIndex1,
155 const Standard_Integer theIndex2)
156{
157 Standard_Integer anEntityIdx1 = myEntityIdxs.Value (theIndex1 + 1);
158 Standard_Integer anEntityIdx2 = myEntityIdxs.Value (theIndex2 + 1);
159 myEntityIdxs.ChangeValue (theIndex1 + 1) = anEntityIdx2;
160 myEntityIdxs.ChangeValue (theIndex2 + 1) = anEntityIdx1;
161}
162
163//=======================================================================
164// function : Size
165// purpose : Returns the amount of entities
166//=======================================================================
167Standard_Integer SelectMgr_SensitiveEntitySet::Size() const
168{
169 return myEntityIdxs.Size();
170}
171
172//=======================================================================
173// function : GetSensitiveById
174// purpose : Returns the entity with index theIndex in the set
175//=======================================================================
7ab15952 176const Handle(SelectMgr_SensitiveEntity)& SelectMgr_SensitiveEntitySet::GetSensitiveById
f751596e 177 (const Standard_Integer theIndex) const
178{
179 Standard_Integer anIdx = myEntityIdxs.Value (theIndex + 1);
180 return myEntities.Value (anIdx);
181}