0027180: Visualization - improve selection logic of MeshVS_Mesh
[occt.git] / src / MeshVS / MeshVS_SensitivePolyhedron.cxx
1 // Created on: 2005-01-21
2 // Created by: Alexander SOLOVYOV
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 <MeshVS_SensitivePolyhedron.hxx>
17
18 #include <gp_Lin.hxx>
19 #include <MeshVS_HArray1OfSequenceOfInteger.hxx>
20 #include <MeshVS_Tool.hxx>
21 #include <NCollection_Vec4.hxx>
22 #include <Select3D_SensitiveEntity.hxx>
23 #include <SelectBasics_EntityOwner.hxx>
24 #include <TColgp_Array1OfPnt.hxx>
25 #include <TColgp_HArray1OfPnt.hxx>
26 #include <TColgp_Array1OfPnt2d.hxx>
27 #include <TColStd_SequenceOfInteger.hxx>
28
29
30 IMPLEMENT_STANDARD_RTTIEXT(MeshVS_SensitivePolyhedron,Select3D_SensitiveEntity)
31
32 //================================================================
33 // Function : Constructor MeshVS_SensitivePolyhedron
34 // Purpose  :
35 //================================================================
36 MeshVS_SensitivePolyhedron::MeshVS_SensitivePolyhedron (const Handle(SelectBasics_EntityOwner)& theOwner,
37                                                         const TColgp_Array1OfPnt& theNodes,
38                                                         const Handle(MeshVS_HArray1OfSequenceOfInteger)& theTopo)
39 : Select3D_SensitiveEntity (theOwner),
40   myTopo (theTopo)
41 {
42   Standard_Integer aPlaneLowIdx  = theTopo->Lower();
43   Standard_Integer aPlaneUpIdx  = theTopo->Upper();
44   Standard_Integer aNodesLowerIdx = theNodes.Lower();
45   myNodes = new TColgp_HArray1OfPnt (aNodesLowerIdx, theNodes.Upper());
46   myCenter = gp_XYZ (0.0, 0.0, 0.0);
47
48   for (Standard_Integer aPlaneIdx = aPlaneLowIdx; aPlaneIdx <= aPlaneUpIdx; ++aPlaneIdx)
49   {
50     Standard_Integer aVertNb = theTopo->Value (aPlaneIdx).Length();
51     Handle(TColgp_HArray1OfPnt) aVertArray = new TColgp_HArray1OfPnt (0, aVertNb - 1);
52     for (Standard_Integer aVertIdx = 1; aVertIdx <= aVertNb; ++aVertIdx)
53     {
54       Standard_Integer aNodeIdx = theTopo->Value (aPlaneIdx).Value (aVertIdx);
55       const gp_Pnt& aVert = theNodes.Value (aNodeIdx + aNodesLowerIdx);
56       aVertArray->SetValue (aVertIdx - 1, aVert);
57       myNodes->SetValue (aNodeIdx + aNodesLowerIdx, aVert);
58       myBndBox.Add (SelectMgr_Vec3 (aVert.X(), aVert.Y(), aVert.Z()));
59       myCenter += aVert.XYZ();
60     }
61
62     myTopology.Append (aVertArray);
63   }
64
65   myCenter.Divide (theNodes.Length());
66 }
67
68 //================================================================
69 // Function : GetConnected
70 // Purpose  :
71 //================================================================
72 Handle(Select3D_SensitiveEntity) MeshVS_SensitivePolyhedron::GetConnected()
73 {
74   Handle(MeshVS_SensitivePolyhedron) aNewEnt = new
75     MeshVS_SensitivePolyhedron (myOwnerId, myNodes->Array1(), myTopo);
76
77   return aNewEnt;
78 }
79
80 //=======================================================================
81 // function : Matches
82 // purpose  :
83 //=======================================================================
84 Standard_Boolean MeshVS_SensitivePolyhedron::Matches (SelectBasics_SelectingVolumeManager& theMgr,
85                                                       SelectBasics_PickResult& thePickResult)
86 {
87   Standard_Real aDepthMin = RealLast();
88   Standard_Real aDistToCOG = RealLast();
89
90   for (MeshVS_PolyhedronVertsIter aIter (myTopology); aIter.More(); aIter.Next())
91   {
92     Standard_Real aDepth = RealLast();
93     if (theMgr.Overlaps (aIter.Value(), Select3D_TOS_INTERIOR, aDepth))
94     {
95       aDepthMin = Min (aDepth, aDepthMin);
96     }
97   }
98
99   aDistToCOG = aDepthMin == RealLast() ? RealLast() : theMgr.DistToGeometryCenter (myCenter);
100   thePickResult = SelectBasics_PickResult (aDepthMin, aDistToCOG);
101
102   return aDepthMin != RealLast();
103 }
104
105 //=======================================================================
106 // function : NbSubElements
107 // purpose  : Returns the amount of nodes of polyhedron
108 //=======================================================================
109 Standard_Integer MeshVS_SensitivePolyhedron::NbSubElements()
110 {
111   return myNodes->Length();
112 }
113
114 //=======================================================================
115 // function : BoundingBox
116 // purpose  :
117 //=======================================================================
118 Select3D_BndBox3d MeshVS_SensitivePolyhedron::BoundingBox()
119 {
120   return myBndBox;
121 }
122
123 //=======================================================================
124 // function : CenterOfGeometry
125 // purpose  :
126 //=======================================================================
127 gp_Pnt MeshVS_SensitivePolyhedron::CenterOfGeometry() const
128 {
129   return myCenter;
130 }