0026139: AIS_InteractiveContext::Display performance regression
[occt.git] / src / SelectMgr / SelectMgr_SelectableObjectSet.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 <Bnd_Box.hxx>
17#include <BVH_BinnedBuilder.hxx>
18
19#include <SelectMgr_SelectableObjectSet.hxx>
20
21//=======================================================================
22// function : SelectMgr_SelectableObjectSet
23// purpose : Creates new empty objects set and initializes BVH tree
24// builder to Binned builder with 1 element per list
25//=======================================================================
26SelectMgr_SelectableObjectSet::SelectMgr_SelectableObjectSet()
27{
28 myBuilder = new BVH_BinnedBuilder<Standard_Real, 3, 32> (1, 32, Standard_False);
29}
30
31//=======================================================================
32// function : Append
33// purpose : Adds new object to the set and marks BVH tree for rebuild
34//=======================================================================
35void SelectMgr_SelectableObjectSet::Append (const Handle(SelectMgr_SelectableObject)& theObject)
36{
2cf5ec3f 37 Standard_Integer aSize = Size();
38 if (aSize < myObjects.Add (theObject))
d4aaad5b 39 {
40 MarkDirty();
41 }
f751596e 42}
43
44//=======================================================================
45// function : Remove
46// purpose : Removes object theObject from set and marks BVH tree for
47// rebuild
48//=======================================================================
49void SelectMgr_SelectableObjectSet::Remove (const Handle(SelectMgr_SelectableObject)& theObject)
50{
d4aaad5b 51 const Standard_Integer anIndex = myObjects.FindIndex (theObject);
52
53 if (anIndex != 0)
f751596e 54 {
ec81011f 55 if (anIndex != Size())
56 {
57 Swap (anIndex - 1, Size() - 1);
58 }
d4aaad5b 59
60 myObjects.RemoveLast();
61
62 MarkDirty();
f751596e 63 }
64}
65
66//=======================================================================
67// function : Box
68// purpose : Returns bounding box of object with index theIndex
69//=======================================================================
70Select3D_BndBox3d SelectMgr_SelectableObjectSet::Box (const Standard_Integer theIndex) const
71{
72 const Handle(SelectMgr_SelectableObject)& anObject = GetObjectById (theIndex);
73 Bnd_Box aBox;
74 anObject->BoundingBox (aBox);
b3c433fe 75 if (aBox.IsVoid())
76 return Select3D_BndBox3d();
77
f751596e 78 return Select3D_BndBox3d (SelectMgr_Vec3 (aBox.CornerMin().X(), aBox.CornerMin().Y(), aBox.CornerMin().Z()),
79 SelectMgr_Vec3 (aBox.CornerMax().X(), aBox.CornerMax().Y(), aBox.CornerMax().Z()));
80}
81
82//=======================================================================
83// function : Center
84// purpose : Returns center of object with index theIndex in the set
85// along the given axis theAxis
86//=======================================================================
87Standard_Real SelectMgr_SelectableObjectSet::Center (const Standard_Integer theIndex,
88 const Standard_Integer theAxis) const
89{
90 Select3D_BndBox3d aBndBox = Box (theIndex);
f751596e 91
d4aaad5b 92 return (aBndBox.CornerMin()[theAxis] +
93 aBndBox.CornerMax()[theAxis]) * 0.5;
f751596e 94}
95
96//=======================================================================
97// function : Swap
98// purpose : Swaps items with indexes theIndex1 and theIndex2 in the set
99//=======================================================================
100void SelectMgr_SelectableObjectSet::Swap (const Standard_Integer theIndex1,
101 const Standard_Integer theIndex2)
102{
d4aaad5b 103 const Standard_Integer aIndex1 = theIndex1 + 1;
104 const Standard_Integer aIndex2 = theIndex2 + 1;
105
106 Handle(SelectMgr_SelectableObject) anObject1 = myObjects.FindKey (aIndex1);
107 Handle(SelectMgr_SelectableObject) anObject2 = myObjects.FindKey (aIndex2);
108
109 myObjects.Substitute (aIndex1, EMPTY_OBJ);
110 myObjects.Substitute (aIndex2, anObject1);
111 myObjects.Substitute (aIndex1, anObject2);
f751596e 112}
113
114//=======================================================================
115// function : Size
116// purpose : Returns size of objects set
117//=======================================================================
118Standard_Integer SelectMgr_SelectableObjectSet::Size() const
119{
d4aaad5b 120 return myObjects.Size();
f751596e 121}
122
123//=======================================================================
124// function : GetObjectById
125// purpose : Returns object from set by theIndex given
126//=======================================================================
d4aaad5b 127const Handle(SelectMgr_SelectableObject)& SelectMgr_SelectableObjectSet::GetObjectById (const Standard_Integer theIndex) const
f751596e 128{
d4aaad5b 129 return myObjects.FindKey (theIndex + 1);
f751596e 130}
131
132//=======================================================================
133// function : Contains
134// purpose : Returns true if this objects set contains theObject given
135//=======================================================================
7ab15952 136Standard_Boolean SelectMgr_SelectableObjectSet::Contains (const Handle(SelectMgr_SelectableObject)& theObject) const
f751596e 137{
d4aaad5b 138 return myObjects.Contains (theObject);
f751596e 139}