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