0024623: Visualization - improve selection mechanism
[occt.git] / src / Select3D / Select3D_SensitiveBox.cxx
1 // Created on: 1995-04-13
2 // Created by: Robert COUBLANC
3 // Copyright (c) 1995-1999 Matra Datavision
4 // Copyright (c) 1999-2014 OPEN CASCADE SAS
5 //
6 // This file is part of Open CASCADE Technology software library.
7 //
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
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.
13 //
14 // Alternatively, this file may be used under the terms of Open CASCADE
15 // commercial license or contractual agreement.
16
17 #include <Select3D_SensitiveBox.hxx>
18 #include <gp_Pnt2d.hxx>
19 #include <gp_Pnt.hxx>
20 #include <Bnd_Box.hxx>
21 #include <ElCLib.hxx>
22
23 IMPLEMENT_STANDARD_HANDLE (Select3D_SensitiveBox, Select3D_SensitiveEntity)
24 IMPLEMENT_STANDARD_RTTIEXT(Select3D_SensitiveBox, Select3D_SensitiveEntity)
25
26 //==================================================
27 // Function: Select3D_SensitiveBox
28 // Purpose :
29 //==================================================
30 Select3D_SensitiveBox::Select3D_SensitiveBox (const Handle(SelectBasics_EntityOwner)& theOwnerId,
31                                               const Bnd_Box& theBox)
32 : Select3D_SensitiveEntity (theOwnerId)
33 {
34   Standard_Real aXMax, aYMax, aZMax;
35   Standard_Real aXMin, aYMin, aZMin;
36   theBox.Get (aXMin, aYMin, aZMin, aXMax, aYMax, aZMax);
37   myBox = Select3D_BndBox3d (SelectMgr_Vec3 (aXMin, aYMin, aZMin),
38                              SelectMgr_Vec3 (aXMax, aYMax, aZMax));
39   myCenter3d = (gp_XYZ (aXMin, aYMin, aZMin) + gp_XYZ (aXMax, aYMax, aZMax))
40                 * (1.0 / 2.0);
41 }
42
43 //==================================================
44 // Function: Select3D_SensitiveBox
45 // Purpose :
46 //==================================================
47
48 Select3D_SensitiveBox::Select3D_SensitiveBox (const Handle(SelectBasics_EntityOwner)& theOwnerId,
49                                               const Standard_Real theXMin,
50                                               const Standard_Real theYMin,
51                                               const Standard_Real theZMin,
52                                               const Standard_Real theXMax,
53                                               const Standard_Real theYMax,
54                                               const Standard_Real theZMax)
55 : Select3D_SensitiveEntity (theOwnerId)
56 {
57   myBox = Select3D_BndBox3d (SelectMgr_Vec3 (theXMin, theYMin, theZMin),
58                              SelectMgr_Vec3 (theXMax, theYMax, theZMax));
59   myCenter3d = (gp_XYZ (theXMin, theYMin, theZMin) + gp_XYZ (theXMax, theYMax, theZMax))
60                 * (1.0 / 2.0);
61 }
62
63 //=======================================================================
64 // function : NbSubElements
65 // purpose  : Returns the amount of sub-entities in sensitive
66 //=======================================================================
67 Standard_Integer Select3D_SensitiveBox::NbSubElements()
68 {
69   return 1;
70 }
71
72 //=======================================================================
73 //function : GetConnected
74 //purpose  : 
75 //=======================================================================
76
77 Handle(Select3D_SensitiveEntity) Select3D_SensitiveBox::GetConnected()
78 {
79   Bnd_Box aBox;
80   aBox.Update (myBox.CornerMin().x(), myBox.CornerMin().y(), myBox.CornerMin().z(),
81                myBox.CornerMax().x(), myBox.CornerMax().y(), myBox.CornerMax().z());
82   Handle(Select3D_SensitiveBox) aNewEntity =  new Select3D_SensitiveBox (myOwnerId, aBox);
83
84   return aNewEntity;
85 }
86
87 //=======================================================================
88 // function : Matches
89 // purpose  : Checks whether the box overlaps current selecting volume
90 //=======================================================================
91 Standard_Boolean Select3D_SensitiveBox::Matches (SelectBasics_SelectingVolumeManager& theMgr,
92                                                  SelectBasics_PickResult& thePickResult)
93 {
94   Standard_Real aDepth     = RealLast();
95   Standard_Real aDistToCOG = RealLast();
96   Standard_Boolean isMatched = theMgr.Overlaps (myBox, aDepth);
97
98   if (isMatched)
99   {
100     aDistToCOG = theMgr.DistToGeometryCenter (myCenter3d);
101   }
102
103   thePickResult = SelectBasics_PickResult (aDepth, aDistToCOG);
104
105   return isMatched;
106 }
107
108 //=======================================================================
109 // function : CenterOfGeometry
110 // purpose  : Returns center of the box. If location transformation
111 //            is set, it will be applied
112 //=======================================================================
113 gp_Pnt Select3D_SensitiveBox::CenterOfGeometry() const
114 {
115   return myCenter3d;
116 }
117
118 //=======================================================================
119 // function : BoundingBox
120 // purpose  : Returns coordinates of the box. If location transformation
121 //            is set, it will be applied
122 //=======================================================================
123 Select3D_BndBox3d Select3D_SensitiveBox::BoundingBox()
124 {
125   return myBox;
126 }
127
128 //=======================================================================
129 // function : Box
130 // purpose  :
131 //=======================================================================
132 Bnd_Box Select3D_SensitiveBox::Box() const
133 {
134   Bnd_Box aBox;
135   aBox.Update (myBox.CornerMin().x(), myBox.CornerMin().y(), myBox.CornerMin().z(),
136                myBox.CornerMax().x(), myBox.CornerMax().y(), myBox.CornerMax().z());
137
138   return aBox;
139 }