0029938: Visualization - SelectMgr_ViewerSelector::PickedPoint() should return point...
[occt.git] / src / Select3D / Select3D_SensitiveWire.cxx
CommitLineData
b311480e 1// Created on: 1996-10-17
2// Created by: Odile OLIVIER
3// Copyright (c) 1996-1999 Matra Datavision
973c2be1 4// Copyright (c) 1999-2014 OPEN CASCADE SAS
b311480e 5//
973c2be1 6// This file is part of Open CASCADE Technology software library.
b311480e 7//
d5f74e42 8// This library is free software; you can redistribute it and/or modify it under
9// the terms of the GNU Lesser General Public License version 2.1 as published
973c2be1 10// by the Free Software Foundation, with special exception defined in the file
11// OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
12// distribution for complete text of the license and disclaimer of any warranty.
b311480e 13//
973c2be1 14// Alternatively, this file may be used under the terms of Open CASCADE
15// commercial license or contractual agreement.
7fd59977 16
f751596e 17#include <Select3D_SensitiveWire.hxx>
7fd59977 18#include <Select3D_SensitiveEntity.hxx>
7fd59977 19#include <Precision.hxx>
f751596e 20#include <TopLoc_Location.hxx>
7fd59977 21
f751596e 22#include <Select3D_SensitiveSegment.hxx>
7fd59977 23
7fd59977 24
92efcf78 25IMPLEMENT_STANDARD_RTTIEXT(Select3D_SensitiveWire,Select3D_SensitiveSet)
26
7fd59977 27//=====================================================
f751596e 28// Function : Select3D_SensitiveWire
29// Purpose :
7fd59977 30//=====================================================
f751596e 31Select3D_SensitiveWire::Select3D_SensitiveWire (const Handle(SelectBasics_EntityOwner)& theOwnerId)
32: Select3D_SensitiveSet (theOwnerId),
33 myCenter (0.0, 0.0, 0.0)
7fd59977 34{}
35
36//=====================================================
37// Function : Add
38// Purpose :
39//=====================================================
f751596e 40void Select3D_SensitiveWire::Add (const Handle(Select3D_SensitiveEntity)& theSensitive)
7fd59977 41{
f751596e 42 if (!theSensitive.IsNull())
43 myEntities.Append (theSensitive);
44
45 Select3D_BndBox3d aBndBox = theSensitive->BoundingBox();
46 myBndBox.Combine (aBndBox);
47 myCenter.ChangeCoord() += theSensitive->CenterOfGeometry().XYZ();
48 if (myEntities.Length() != 1)
49 myCenter.ChangeCoord().Divide (2.0);
50 myEntityIndexes.Append (myEntities.Length() - 1);
7fd59977 51}
52
53//=======================================================================
f751596e 54// function : NbSubElements
55// purpose : Returns the amount of sub-entities
7fd59977 56//=======================================================================
f751596e 57Standard_Integer Select3D_SensitiveWire::NbSubElements()
7fd59977 58{
f751596e 59 return myEntities.Length();
7fd59977 60}
ac04d101 61
7fd59977 62//=======================================================================
f751596e 63// function : Size
64// purpose : Returns the length of vector of sensitive entities
7fd59977 65//=======================================================================
f751596e 66Standard_Integer Select3D_SensitiveWire::Size() const
7fd59977 67{
f751596e 68 return myEntities.Length();
7fd59977 69}
ac04d101 70
f751596e 71//=======================================================================
72// function : Box
73// purpose : Returns bounding box of sensitive entity with index theIdx
74//=======================================================================
75Select3D_BndBox3d Select3D_SensitiveWire::Box (const Standard_Integer theIdx) const
7fd59977 76{
f751596e 77 const Standard_Integer aSensitiveIdx = myEntityIndexes.Value (theIdx);
78 return myEntities.Value (aSensitiveIdx)->BoundingBox();
7fd59977 79}
80
f751596e 81//=======================================================================
82// function : Center
83// purpose : Returns geometry center of sensitive entity with index
84// theIdx in the vector along the given axis theAxis
85//=======================================================================
86Standard_Real Select3D_SensitiveWire::Center (const Standard_Integer theIdx,
87 const Standard_Integer theAxis) const
7fd59977 88{
f751596e 89 const Standard_Integer aSensitiveIdx = myEntityIndexes.Value (theIdx);
90 const gp_Pnt& aCenter = myEntities.Value (aSensitiveIdx)->CenterOfGeometry();
91 Standard_Real aCenterCoord = 0.0;
92 aCenterCoord = theAxis == 0 ? aCenter.X() : (theAxis == 1 ? aCenter.Y() : aCenter.Z());
4952a30a 93
f751596e 94 return aCenterCoord;
7fd59977 95}
96
f751596e 97//=======================================================================
98// function : Swap
99// purpose : Swaps items with indexes theIdx1 and theIdx2 in the vector
100//=======================================================================
101void Select3D_SensitiveWire::Swap (const Standard_Integer theIdx1,
102 const Standard_Integer theIdx2)
7fd59977 103{
f751596e 104 const Standard_Integer aSensitiveIdx1 = myEntityIndexes.Value (theIdx1);
105 const Standard_Integer aSensitiveIdx2 = myEntityIndexes.Value (theIdx2);
106 myEntityIndexes.ChangeValue (theIdx1) = aSensitiveIdx2;
107 myEntityIndexes.ChangeValue (theIdx2) = aSensitiveIdx1;
7fd59977 108}
109
f751596e 110// =======================================================================
111// function : overlapsElement
112// purpose : Checks whether the entity with index theIdx overlaps the
113// current selecting volume
114// =======================================================================
115Standard_Boolean Select3D_SensitiveWire::overlapsElement (SelectBasics_SelectingVolumeManager& theMgr,
116 Standard_Integer theElemIdx,
17017555 117 SelectBasics_PickResult& thePickResult)
7fd59977 118{
f751596e 119 const Standard_Integer aSensitiveIdx = myEntityIndexes.Value (theElemIdx);
120 const Handle(SelectBasics_SensitiveEntity)& aSeg = myEntities.Value (aSensitiveIdx);
17017555 121 return aSeg->Matches (theMgr, thePickResult);
7fd59977 122}
123
2157d6ac 124// =======================================================================
125// function : elementIsInside
126// purpose :
127// =======================================================================
128Standard_Boolean Select3D_SensitiveWire::elementIsInside (SelectBasics_SelectingVolumeManager& theMgr,
129 const Standard_Integer theElemIdx)
130{
131 SelectBasics_PickResult aMatchResult;
132 return myEntities.Value (myEntityIndexes.Value (theElemIdx))->Matches (theMgr, aMatchResult);
133}
134
f751596e 135// =======================================================================
136// function : distanceToCOG
137// purpose : Calculates distance from the 3d projection of used-picked
138// screen point to center of the geometry
139// =======================================================================
140Standard_Real Select3D_SensitiveWire::distanceToCOG (SelectBasics_SelectingVolumeManager& theMgr)
7fd59977 141{
f751596e 142 return theMgr.DistToGeometryCenter (myCenter);
7fd59977 143}
144
145//=======================================================================
146//function : GetConnected
4952a30a 147//purpose :
7fd59977 148//=======================================================================
ac04d101 149
f751596e 150Handle(Select3D_SensitiveEntity) Select3D_SensitiveWire::GetConnected()
7fd59977 151{
f751596e 152 Handle(Select3D_SensitiveWire) aNewEntity = new Select3D_SensitiveWire (myOwnerId);
153 for (Standard_Integer anEntityIdx = 0; anEntityIdx < myEntities.Length(); anEntityIdx++)
154 aNewEntity->Add (myEntities(anEntityIdx)->GetConnected());
7fd59977 155
f751596e 156 return aNewEntity;
7fd59977 157}
158
7fd59977 159//=======================================================================
160//function : GetEdges
4952a30a 161//purpose : returns the sensitive edges stored in this wire
7fd59977 162//=======================================================================
f751596e 163const NCollection_Vector<Handle(Select3D_SensitiveEntity)>& Select3D_SensitiveWire::GetEdges()
7fd59977 164{
f751596e 165 return myEntities;
7fd59977 166}
167
168//=============================================================================
169// Function : GetLastDetected
4952a30a 170// Purpose :
7fd59977 171//=============================================================================
172Handle(Select3D_SensitiveEntity) Select3D_SensitiveWire::GetLastDetected() const
173{
174 Handle(Select3D_SensitiveEntity) aRes;
175
f751596e 176 if (myDetectedIdx >= 0 && myDetectedIdx < myEntities.Length())
177 {
178 const Standard_Integer aSensitiveIdx = myEntityIndexes.Value (myDetectedIdx);
179 aRes = myEntities.Value (aSensitiveIdx);
180 }
7fd59977 181
182 return aRes;
183}
ac04d101
SA
184
185//=======================================================================
186//function : Set
187//purpose :
188//=======================================================================
f751596e 189void Select3D_SensitiveWire::Set (const Handle(SelectBasics_EntityOwner)& theOwnerId)
ac04d101 190{
f751596e 191 Select3D_SensitiveEntity::Set (theOwnerId);
ac04d101
SA
192
193 // Set TheOwnerId for each element of sensitive wire
f751596e 194 for (Standard_Integer anEntityIdx = 0; anEntityIdx < myEntities.Length(); ++anEntityIdx)
ac04d101 195 {
f751596e 196 myEntities.Value (anEntityIdx)->Set (theOwnerId);
ac04d101
SA
197 }
198}
f751596e 199
200//=======================================================================
201// function : BoundingBox
202// purpose : Returns bounding box of the wire. If location
203// transformation is set, it will be applied
204//=======================================================================
205Select3D_BndBox3d Select3D_SensitiveWire::BoundingBox()
206{
207 if (myBndBox.IsValid())
208 return myBndBox;
209
210 for (Standard_Integer aSensitiveIdx = 0; aSensitiveIdx < myEntities.Length(); ++aSensitiveIdx)
211 {
212 myBndBox.Combine (myEntities.Value (aSensitiveIdx)->BoundingBox());
213 }
214
215 return myBndBox;
216}
217
218//=======================================================================
219// function : CenterOfGeometry
220// purpose : Returns center of the wire. If location transformation
221// is set, it will be applied
222//=======================================================================
223gp_Pnt Select3D_SensitiveWire::CenterOfGeometry() const
224{
225 return myCenter;
226}