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