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