0022777: Visualization - Unsafe way to get attribute values from MeshVS_Drawer
[occt.git] / src / MeshVS / MeshVS_SensitiveMesh.cxx
1 // Created on: 2007-01-29
2 // Created by: Sergey KOCHETKOV
3 // Copyright (c) 2007-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 <MeshVS_SensitiveMesh.hxx>
17
18 #include <TColgp_Array1OfPnt.hxx>
19 #include <TColStd_Array1OfReal.hxx>
20 #include <TColStd_HPackedMapOfInteger.hxx>
21 #include <Select3D_SensitiveEntity.hxx>
22 #include <TopLoc_Location.hxx>
23 #include <MeshVS_DataSource.hxx>
24 #include <MeshVS_MeshOwner.hxx>
25 #include <NCollection_Vec4.hxx>
26
27
28 IMPLEMENT_STANDARD_RTTIEXT(MeshVS_SensitiveMesh,Select3D_SensitiveEntity)
29
30 //=======================================================================
31 // name    : MeshVS_SensitiveMesh::MeshVS_SensitiveMesh
32 // Purpose :
33 //=======================================================================
34 MeshVS_SensitiveMesh::MeshVS_SensitiveMesh (const Handle(SelectMgr_EntityOwner)& theOwnerId,
35                                             const Standard_Integer theMode)
36 : Select3D_SensitiveEntity (theOwnerId)
37 {
38   myMode = theMode;
39   Handle(MeshVS_MeshOwner) anOwner = Handle(MeshVS_MeshOwner)::DownCast (OwnerId());
40   if( !anOwner.IsNull() )
41   {
42     Handle(MeshVS_DataSource) aDS = anOwner->GetDataSource();
43     if (!aDS.IsNull())
44     {
45       Bnd_Box aBox = aDS->GetBoundingBox();
46       Standard_Real aXMin, aYMin, aZMin;
47       Standard_Real aXMax, aYMax, aZMax;
48       aBox.Get (aXMin, aYMin, aZMin,
49                 aXMax, aYMax, aZMax);
50       Select3D_Vec3 aMinPnt (aXMin, aYMin, aZMin);
51       Select3D_Vec3 aMaxPnt (aXMax, aYMax, aZMax);
52       myBndBox = Select3D_BndBox3d (aMinPnt, aMaxPnt);
53     }
54   }
55 }
56
57 //================================================================
58 // Function : GetMode
59 // Purpose  :
60 //================================================================
61 Standard_Integer MeshVS_SensitiveMesh::GetMode() const
62 {
63   return myMode;
64 }
65
66 //=======================================================================
67 // name    : GetConnected
68 // Purpose :
69 //=======================================================================
70 Handle(Select3D_SensitiveEntity) MeshVS_SensitiveMesh::GetConnected()
71 {
72   Handle(MeshVS_SensitiveMesh) aMeshEnt = new MeshVS_SensitiveMesh (myOwnerId);
73   return aMeshEnt;
74 }
75
76 //=======================================================================
77 // function : NbSubElements
78 // purpose  : Returns the amount of mesh nodes
79 //=======================================================================
80 Standard_Integer MeshVS_SensitiveMesh::NbSubElements()
81 {
82   Handle(MeshVS_MeshOwner) anOwner = Handle(MeshVS_MeshOwner)::DownCast (OwnerId());
83   if (anOwner.IsNull())
84     return -1;
85   Handle(MeshVS_DataSource) aDataSource = anOwner->GetDataSource();
86   if (aDataSource.IsNull())
87     return -1;
88   return aDataSource->GetAllNodes().Extent();
89 }
90
91 //=======================================================================
92 // function : BoundingBox
93 // purpose  : Returns bounding box of mesh
94 //=======================================================================
95 Select3D_BndBox3d MeshVS_SensitiveMesh::BoundingBox()
96 {
97   return myBndBox;
98 }
99
100 //=======================================================================
101 // function : CenterOfGeometry
102 // purpose  : Returns center of mesh
103 //=======================================================================
104 gp_Pnt MeshVS_SensitiveMesh::CenterOfGeometry() const
105 {
106   if (!myBndBox.IsValid())
107     return gp_Pnt (0.0, 0.0, 0.0);
108
109   SelectMgr_Vec3 aCenter = (myBndBox.CornerMax() + myBndBox.CornerMin()) * 0.5;
110   return gp_Pnt (aCenter.x(), aCenter.y(), aCenter.z());
111 }