| 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 | //======================================================================= |
| 27 | SelectMgr_SensitiveEntitySet::SelectMgr_SensitiveEntitySet() |
| 28 | { |
| 29 | myBuilder = new BVH_BinnedBuilder<Standard_Real, 3, 4> (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 | //======================================================================= |
| 36 | void SelectMgr_SensitiveEntitySet::Append (const Handle(SelectMgr_SensitiveEntity)& theEntity) |
| 37 | { |
| 38 | if (!theEntity->BaseSensitive()->IsKind ("Select3D_SensitiveEntity")) |
| 39 | { |
| 40 | theEntity->ResetSelectionActiveStatus(); |
| 41 | return; |
| 42 | } |
| 43 | mySensitives.Add (theEntity); |
| 44 | MarkDirty(); |
| 45 | } |
| 46 | |
| 47 | //======================================================================= |
| 48 | // function : Append |
| 49 | // purpose : Adds every entity of selection theSelection to the set |
| 50 | // and marks BVH tree for rebuild |
| 51 | //======================================================================= |
| 52 | void SelectMgr_SensitiveEntitySet::Append (const Handle(SelectMgr_Selection)& theSelection) |
| 53 | { |
| 54 | for (theSelection->Init(); theSelection->More(); theSelection->Next()) |
| 55 | { |
| 56 | if (!theSelection->Sensitive()->BaseSensitive()->IsKind ("Select3D_SensitiveEntity")) |
| 57 | { |
| 58 | theSelection->Sensitive()->ResetSelectionActiveStatus(); |
| 59 | continue; |
| 60 | } |
| 61 | mySensitives.Add (theSelection->Sensitive()); |
| 62 | } |
| 63 | MarkDirty(); |
| 64 | } |
| 65 | |
| 66 | //======================================================================= |
| 67 | // function : Remove |
| 68 | // purpose : Removes every entity of selection theSelection from the set |
| 69 | // and marks BVH tree for rebuild |
| 70 | //======================================================================= |
| 71 | void SelectMgr_SensitiveEntitySet::Remove (const Handle(SelectMgr_Selection)& theSelection) |
| 72 | { |
| 73 | for (theSelection->Init(); theSelection->More(); theSelection->Next()) |
| 74 | { |
| 75 | Standard_Integer anEntIdx = mySensitives.FindIndex (theSelection->Sensitive()); |
| 76 | if (!anEntIdx) |
| 77 | continue; |
| 78 | |
| 79 | if (anEntIdx != mySensitives.Size()) |
| 80 | { |
| 81 | Swap (anEntIdx - 1, mySensitives.Size() - 1); |
| 82 | } |
| 83 | |
| 84 | mySensitives.RemoveLast(); |
| 85 | } |
| 86 | |
| 87 | MarkDirty(); |
| 88 | } |
| 89 | |
| 90 | //======================================================================= |
| 91 | // function : Box |
| 92 | // purpose : Returns bounding box of entity with index theIdx |
| 93 | //======================================================================= |
| 94 | Select3D_BndBox3d SelectMgr_SensitiveEntitySet::Box (const Standard_Integer theIndex) const |
| 95 | { |
| 96 | return GetSensitiveById (theIndex)->BaseSensitive()->BoundingBox(); |
| 97 | } |
| 98 | |
| 99 | //======================================================================= |
| 100 | // function : Center |
| 101 | // purpose : Returns geometry center of sensitive entity index theIdx |
| 102 | // along the given axis theAxis |
| 103 | //======================================================================= |
| 104 | Standard_Real SelectMgr_SensitiveEntitySet::Center (const Standard_Integer theIndex, |
| 105 | const Standard_Integer theAxis) const |
| 106 | { |
| 107 | const Handle(SelectBasics_SensitiveEntity)& aBasicEntity = |
| 108 | GetSensitiveById (theIndex)->BaseSensitive(); |
| 109 | const Handle(Select3D_SensitiveEntity)& aSensitive = |
| 110 | Handle(Select3D_SensitiveEntity)::DownCast (aBasicEntity); |
| 111 | const gp_Pnt aCenter = aSensitive->CenterOfGeometry(); |
| 112 | Standard_Real aCenterCoord = 0.0; |
| 113 | aCenterCoord = theAxis == 0 ? aCenter.X() : |
| 114 | (theAxis == 1 ? aCenter.Y() : aCenter.Z()); |
| 115 | |
| 116 | return aCenterCoord; |
| 117 | } |
| 118 | |
| 119 | //======================================================================= |
| 120 | // function : Swap |
| 121 | // purpose : Swaps items with indexes theIdx1 and theIdx2 |
| 122 | //======================================================================= |
| 123 | void SelectMgr_SensitiveEntitySet::Swap (const Standard_Integer theIndex1, |
| 124 | const Standard_Integer theIndex2) |
| 125 | { |
| 126 | const Handle(SelectMgr_SensitiveEntity) anEntity1 = GetSensitiveById (theIndex1); |
| 127 | const Handle(SelectMgr_SensitiveEntity) anEntity2 = GetSensitiveById (theIndex2); |
| 128 | |
| 129 | mySensitives.Substitute (theIndex1 + 1, EMPTY_ENT); |
| 130 | mySensitives.Substitute (theIndex2 + 1, anEntity1); |
| 131 | mySensitives.Substitute (theIndex1 + 1, anEntity2); |
| 132 | } |
| 133 | |
| 134 | //======================================================================= |
| 135 | // function : Size |
| 136 | // purpose : Returns the amount of entities |
| 137 | //======================================================================= |
| 138 | Standard_Integer SelectMgr_SensitiveEntitySet::Size() const |
| 139 | { |
| 140 | return mySensitives.Size(); |
| 141 | } |
| 142 | |
| 143 | //======================================================================= |
| 144 | // function : GetSensitiveById |
| 145 | // purpose : Returns the entity with index theIndex in the set |
| 146 | //======================================================================= |
| 147 | const Handle(SelectMgr_SensitiveEntity)& SelectMgr_SensitiveEntitySet::GetSensitiveById |
| 148 | (const Standard_Integer theIndex) const |
| 149 | { |
| 150 | return mySensitives.FindKey (theIndex + 1); |
| 151 | } |