0026195: Visualization - optimize selection algorithms
[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{
3bf9a45f 28 myBuilder = new BVH_BinnedBuilder<Standard_Real, 3, 4> (1, 32, Standard_True);
f751596e 29}
30
31//=======================================================================
32// function : Append
33// purpose : Adds new object to the set and marks BVH tree for rebuild
34//=======================================================================
825aa485 35Standard_Boolean SelectMgr_SelectableObjectSet::Append (const Handle(SelectMgr_SelectableObject)& theObject)
f751596e 36{
2cf5ec3f 37 Standard_Integer aSize = Size();
825aa485 38
2cf5ec3f 39 if (aSize < myObjects.Add (theObject))
d4aaad5b 40 {
41 MarkDirty();
825aa485 42
43 return Standard_True;
d4aaad5b 44 }
825aa485 45
46 return Standard_False;
f751596e 47}
48
49//=======================================================================
50// function : Remove
51// purpose : Removes object theObject from set and marks BVH tree for
52// rebuild
53//=======================================================================
825aa485 54Standard_Boolean SelectMgr_SelectableObjectSet::Remove (const Handle(SelectMgr_SelectableObject)& theObject)
f751596e 55{
d4aaad5b 56 const Standard_Integer anIndex = myObjects.FindIndex (theObject);
57
58 if (anIndex != 0)
f751596e 59 {
ec81011f 60 if (anIndex != Size())
61 {
62 Swap (anIndex - 1, Size() - 1);
63 }
d4aaad5b 64
65 myObjects.RemoveLast();
66
67 MarkDirty();
825aa485 68
69 return Standard_True;
f751596e 70 }
825aa485 71
72 return Standard_False;
f751596e 73}
74
75//=======================================================================
76// function : Box
77// purpose : Returns bounding box of object with index theIndex
78//=======================================================================
79Select3D_BndBox3d SelectMgr_SelectableObjectSet::Box (const Standard_Integer theIndex) const
80{
81 const Handle(SelectMgr_SelectableObject)& anObject = GetObjectById (theIndex);
82 Bnd_Box aBox;
83 anObject->BoundingBox (aBox);
b3c433fe 84 if (aBox.IsVoid())
85 return Select3D_BndBox3d();
86
f751596e 87 return Select3D_BndBox3d (SelectMgr_Vec3 (aBox.CornerMin().X(), aBox.CornerMin().Y(), aBox.CornerMin().Z()),
88 SelectMgr_Vec3 (aBox.CornerMax().X(), aBox.CornerMax().Y(), aBox.CornerMax().Z()));
89}
90
91//=======================================================================
92// function : Center
93// purpose : Returns center of object with index theIndex in the set
94// along the given axis theAxis
95//=======================================================================
96Standard_Real SelectMgr_SelectableObjectSet::Center (const Standard_Integer theIndex,
97 const Standard_Integer theAxis) const
98{
99 Select3D_BndBox3d aBndBox = Box (theIndex);
f751596e 100
d4aaad5b 101 return (aBndBox.CornerMin()[theAxis] +
102 aBndBox.CornerMax()[theAxis]) * 0.5;
f751596e 103}
104
105//=======================================================================
106// function : Swap
107// purpose : Swaps items with indexes theIndex1 and theIndex2 in the set
108//=======================================================================
109void SelectMgr_SelectableObjectSet::Swap (const Standard_Integer theIndex1,
110 const Standard_Integer theIndex2)
111{
3bf9a45f 112 myObjects.Swap (theIndex1 + 1, theIndex2 + 1);
f751596e 113}
114
115//=======================================================================
116// function : Size
117// purpose : Returns size of objects set
118//=======================================================================
119Standard_Integer SelectMgr_SelectableObjectSet::Size() const
120{
d4aaad5b 121 return myObjects.Size();
f751596e 122}