| 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 <Graphic3d_TransformPers.hxx> |
| 19 | #include <Select3D_SensitiveEntity.hxx> |
| 20 | #include <SelectMgr_SensitiveEntity.hxx> |
| 21 | |
| 22 | IMPLEMENT_STANDARD_RTTIEXT(SelectMgr_SensitiveEntitySet, BVH_PrimitiveSet3d) |
| 23 | |
| 24 | //======================================================================= |
| 25 | // function : SelectMgr_SensitiveEntitySet |
| 26 | // purpose : |
| 27 | //======================================================================= |
| 28 | SelectMgr_SensitiveEntitySet::SelectMgr_SensitiveEntitySet (const Handle(Select3D_BVHBuilder3d)& theBuilder) |
| 29 | : BVH_PrimitiveSet3d (theBuilder) |
| 30 | { |
| 31 | // |
| 32 | } |
| 33 | |
| 34 | //======================================================================= |
| 35 | // function : Append |
| 36 | // purpose : Adds new entity to the set and marks BVH tree for rebuild |
| 37 | //======================================================================= |
| 38 | void SelectMgr_SensitiveEntitySet::Append (const Handle(SelectMgr_SensitiveEntity)& theEntity) |
| 39 | { |
| 40 | if (!theEntity->BaseSensitive()->IsKind (STANDARD_TYPE(Select3D_SensitiveEntity))) |
| 41 | { |
| 42 | theEntity->ResetSelectionActiveStatus(); |
| 43 | return; |
| 44 | } |
| 45 | |
| 46 | const Standard_Integer anExtent = mySensitives.Extent(); |
| 47 | if (mySensitives.Add (theEntity) > anExtent) |
| 48 | { |
| 49 | addOwner (theEntity->BaseSensitive()->OwnerId()); |
| 50 | } |
| 51 | if (!theEntity->BaseSensitive()->TransformPersistence().IsNull()) |
| 52 | { |
| 53 | myHasEntityWithPersistence = Standard_True; |
| 54 | } |
| 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 | //======================================================================= |
| 63 | void SelectMgr_SensitiveEntitySet::Append (const Handle(SelectMgr_Selection)& theSelection) |
| 64 | { |
| 65 | for (NCollection_Vector<Handle(SelectMgr_SensitiveEntity)>::Iterator aSelEntIter (theSelection->Entities()); aSelEntIter.More(); aSelEntIter.Next()) |
| 66 | { |
| 67 | const Handle(SelectMgr_SensitiveEntity)& aSensEnt = aSelEntIter.Value(); |
| 68 | if (!aSensEnt->BaseSensitive()->IsKind (STANDARD_TYPE(Select3D_SensitiveEntity))) |
| 69 | { |
| 70 | aSensEnt->ResetSelectionActiveStatus(); |
| 71 | continue; |
| 72 | } |
| 73 | |
| 74 | const Standard_Integer anExtent = mySensitives.Extent(); |
| 75 | if (mySensitives.Add (aSensEnt) > anExtent) |
| 76 | { |
| 77 | addOwner (aSensEnt->BaseSensitive()->OwnerId()); |
| 78 | } |
| 79 | if (!aSensEnt->BaseSensitive()->TransformPersistence().IsNull()) |
| 80 | { |
| 81 | myHasEntityWithPersistence = Standard_True; |
| 82 | } |
| 83 | } |
| 84 | MarkDirty(); |
| 85 | } |
| 86 | |
| 87 | //======================================================================= |
| 88 | // function : Remove |
| 89 | // purpose : Removes every entity of selection theSelection from the set |
| 90 | // and marks BVH tree for rebuild |
| 91 | //======================================================================= |
| 92 | void SelectMgr_SensitiveEntitySet::Remove (const Handle(SelectMgr_Selection)& theSelection) |
| 93 | { |
| 94 | for (NCollection_Vector<Handle(SelectMgr_SensitiveEntity)>::Iterator aSelEntIter (theSelection->Entities()); aSelEntIter.More(); aSelEntIter.Next()) |
| 95 | { |
| 96 | const Standard_Integer anEntIdx = mySensitives.FindIndex (aSelEntIter.Value()); |
| 97 | if (anEntIdx == 0) |
| 98 | { |
| 99 | continue; |
| 100 | } |
| 101 | |
| 102 | if (anEntIdx != mySensitives.Size()) |
| 103 | { |
| 104 | Swap (anEntIdx - 1, mySensitives.Size() - 1); |
| 105 | } |
| 106 | |
| 107 | mySensitives.RemoveLast(); |
| 108 | removeOwner (aSelEntIter.Value()->BaseSensitive()->OwnerId()); |
| 109 | } |
| 110 | |
| 111 | MarkDirty(); |
| 112 | } |
| 113 | |
| 114 | //======================================================================= |
| 115 | // function : Box |
| 116 | // purpose : Returns bounding box of entity with index theIdx |
| 117 | //======================================================================= |
| 118 | Select3D_BndBox3d SelectMgr_SensitiveEntitySet::Box (const Standard_Integer theIndex) const |
| 119 | { |
| 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(); |
| 127 | } |
| 128 | |
| 129 | //======================================================================= |
| 130 | // function : Center |
| 131 | // purpose : Returns geometry center of sensitive entity index theIdx |
| 132 | // along the given axis theAxis |
| 133 | //======================================================================= |
| 134 | Standard_Real SelectMgr_SensitiveEntitySet::Center (const Standard_Integer theIndex, |
| 135 | const Standard_Integer theAxis) const |
| 136 | { |
| 137 | const Handle(Select3D_SensitiveEntity)& aSensitive = GetSensitiveById (theIndex)->BaseSensitive(); |
| 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 | //======================================================================= |
| 150 | void SelectMgr_SensitiveEntitySet::Swap (const Standard_Integer theIndex1, |
| 151 | const Standard_Integer theIndex2) |
| 152 | { |
| 153 | mySensitives.Swap (theIndex1 + 1, theIndex2 + 1); |
| 154 | } |
| 155 | |
| 156 | //======================================================================= |
| 157 | // function : Size |
| 158 | // purpose : Returns the amount of entities |
| 159 | //======================================================================= |
| 160 | Standard_Integer SelectMgr_SensitiveEntitySet::Size() const |
| 161 | { |
| 162 | return mySensitives.Size(); |
| 163 | } |
| 164 | |
| 165 | //======================================================================= |
| 166 | // function : GetSensitiveById |
| 167 | // purpose : Returns the entity with index theIndex in the set |
| 168 | //======================================================================= |
| 169 | const Handle(SelectMgr_SensitiveEntity)& SelectMgr_SensitiveEntitySet::GetSensitiveById |
| 170 | (const Standard_Integer theIndex) const |
| 171 | { |
| 172 | return mySensitives.FindKey (theIndex + 1); |
| 173 | } |
| 174 | |
| 175 | //======================================================================= |
| 176 | // function : addOwner |
| 177 | // purpose : |
| 178 | //======================================================================= |
| 179 | void 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 | //======================================================================= |
| 198 | void 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 | } |