1 // Created on: 1997-05-15
2 // Created by: Robert COUBLANC
3 // Copyright (c) 1997-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 #ifndef _Select3D_SensitiveTriangulation_Header
18 #define _Select3D_SensitiveTriangulation_Header
19 
20 #include <TopLoc_Location.hxx>
21 #include <gp_Trsf.hxx>
22 #include <gp_Pnt.hxx>
23 #include <TColStd_HArray1OfInteger.hxx>
24 #include <Select3D_SensitiveEntity.hxx>
25 #include <SelectMgr_SelectingVolumeManager.hxx>
26 #include <Select3D_SensitiveSet.hxx>
27 
28 class Poly_Triangle;
29 class Poly_Triangulation;
30 
31 //! A framework to define selection of a sensitive entity made of a set of triangles.
32 class Select3D_SensitiveTriangulation : public Select3D_SensitiveSet
33 {
34   DEFINE_STANDARD_RTTIEXT(Select3D_SensitiveTriangulation, Select3D_SensitiveSet)
35 public:
36 
37   //! Constructs a sensitive triangulation object defined by
38   //! the owner theOwnerId, the triangulation theTrg,
39   //! the location theInitLoc, and the flag theIsInterior.
40   Standard_EXPORT Select3D_SensitiveTriangulation (const Handle(SelectMgr_EntityOwner)& theOwnerId,
41                                                    const Handle(Poly_Triangulation)& theTrg,
42                                                    const TopLoc_Location& theInitLoc,
43                                                    const Standard_Boolean theIsInterior = Standard_True);
44 
45   //! Constructs a sensitive triangulation object defined by
46   //! the owner theOwnerId, the triangulation theTrg,
47   //! the location theInitLoc, the array of free edges
48   //! theFreeEdges, the center of gravity theCOG, and the flag theIsInterior.
49   //! As free edges and the center of gravity do not have
50   //! to be computed later, this syntax reduces computation time.
51   Standard_EXPORT Select3D_SensitiveTriangulation (const Handle(SelectMgr_EntityOwner)& theOwnerId,
52                                                    const Handle(Poly_Triangulation)& theTrg,
53                                                    const TopLoc_Location& theInitLoc,
54                                                    const Handle(TColStd_HArray1OfInteger)& theFreeEdges,
55                                                    const gp_Pnt& theCOG,
56                                                    const Standard_Boolean theIsInterior);
57 public:
58 
59   //! Get last detected triangle.
60   //! @param theTriangle [out] triangle node indexes
61   //! @return TRUE if defined
62   Standard_EXPORT bool LastDetectedTriangle (Poly_Triangle& theTriangle) const;
63 
64   //! Get last detected triangle.
65   //! @param theTriangle [out] triangle node indexes
66   //! @param theTriNodes [out] triangle nodes (with pre-applied transformation)
67   //! @return TRUE if defined
68   Standard_EXPORT bool LastDetectedTriangle (Poly_Triangle& theTriangle,
69                                              gp_Pnt theTriNodes[3]) const;
70 
71   //! Return index of last detected triangle within [1..NbTris] range, or -1 if undefined.
LastDetectedTriangleIndex() const72   Standard_Integer LastDetectedTriangleIndex() const
73   {
74     return (myDetectedIdx != -1 && mySensType == Select3D_TOS_INTERIOR && !myBVHPrimIndexes.IsNull())
75           ? myBVHPrimIndexes->Value (myDetectedIdx) + 1
76           : -1;
77   }
78 
79 public:
80 
81   //! Returns the amount of nodes in triangulation
82   Standard_EXPORT virtual Standard_Integer NbSubElements() const Standard_OVERRIDE;
83 
84   Standard_EXPORT Handle(Select3D_SensitiveEntity) GetConnected() Standard_OVERRIDE;
85 
Handle(Poly_Triangulation)86   const Handle(Poly_Triangulation)& Triangulation() const { return myTriangul; }
87 
88   //! Returns the length of array of triangles or edges
89   Standard_EXPORT virtual Standard_Integer Size() const Standard_OVERRIDE;
90 
91   //! Returns bounding box of triangle/edge with index theIdx
92   Standard_EXPORT virtual Select3D_BndBox3d Box (const Standard_Integer theIdx) const Standard_OVERRIDE;
93 
94   //! Returns geometry center of triangle/edge with index theIdx
95   //! in array along the given axis theAxis
96   Standard_EXPORT virtual Standard_Real Center (const Standard_Integer theIdx,
97                                                 const Standard_Integer theAxis) const Standard_OVERRIDE;
98 
99   //! Swaps items with indexes theIdx1 and theIdx2 in array
100   Standard_EXPORT virtual void Swap (const Standard_Integer theIdx1,
101                                      const Standard_Integer theIdx2) Standard_OVERRIDE;
102 
103   //! Returns bounding box of the triangulation. If location
104   //! transformation is set, it will be applied
105   Standard_EXPORT virtual Select3D_BndBox3d BoundingBox() Standard_OVERRIDE;
106 
107   //! Returns center of triangulation. If location transformation
108   //! is set, it will be applied
109   Standard_EXPORT virtual gp_Pnt CenterOfGeometry() const Standard_OVERRIDE;
110 
111   //! Returns true if the shape corresponding to the entity has init location
112   Standard_EXPORT virtual Standard_Boolean HasInitLocation() const Standard_OVERRIDE;
113 
114   //! Returns inversed location transformation matrix if the shape corresponding
115   //! to this entity has init location set. Otherwise, returns identity matrix.
116   Standard_EXPORT virtual gp_GTrsf InvInitLocation() const Standard_OVERRIDE;
117 
GetInitLocation() const118   const TopLoc_Location& GetInitLocation() const { return myInitLocation; }
119 
120   //! Dumps the content of me into the stream
121   Standard_EXPORT virtual void DumpJson (Standard_OStream& theOStream, Standard_Integer theDepth = -1) const Standard_OVERRIDE;
122 
123   //! Checks whether one or more entities of the set overlap current selecting volume.
124   Standard_EXPORT virtual Standard_Boolean Matches (SelectBasics_SelectingVolumeManager& theMgr,
125                                                     SelectBasics_PickResult& thePickResult) Standard_OVERRIDE;
126 
127 protected:
128 
129   //! Compute bounding box.
130   void computeBoundingBox();
131 
132   //! Inner function for transformation application to bounding
133   //! box of the triangulation
134   Select3D_BndBox3d applyTransformation();
135 
136 private:
137 
138   //! Checks whether the element with index theIdx overlaps the current selecting volume
139   Standard_EXPORT virtual Standard_Boolean overlapsElement (SelectBasics_PickResult& thePickResult,
140                                                             SelectBasics_SelectingVolumeManager& theMgr,
141                                                             Standard_Integer theElemIdx,
142                                                             Standard_Boolean theIsFullInside) Standard_OVERRIDE;
143 
144   //! Calculates distance from the 3d projection of used-picked screen point to center of the geometry
145   Standard_EXPORT virtual Standard_Real distanceToCOG (SelectBasics_SelectingVolumeManager& theMgr) Standard_OVERRIDE;
146 
147   //! Checks whether the entity with index theIdx is inside the current selecting volume
148   Standard_EXPORT virtual Standard_Boolean elementIsInside (SelectBasics_SelectingVolumeManager& theMgr,
149                                                             Standard_Integer theElemIdx,
150                                                             Standard_Boolean theIsFullInside) Standard_OVERRIDE;
151 
152 protected:
153 
154   Handle(Poly_Triangulation)       myTriangul;
155   TopLoc_Location                  myInitLocation;
156   gp_Pnt                           myCDG3D;              //!< Center of the whole triangulation
157   Handle(TColStd_HArray1OfInteger) myFreeEdges;
158   Standard_Boolean                 mySensType;            //!< Type of sensitivity: boundary or interior
159   Standard_Integer                 myPrimitivesNb;       //!< Amount of free edges or triangles depending on sensitivity type
160   Handle(TColStd_HArray1OfInteger) myBVHPrimIndexes;     //!< Indexes of edges or triangles for BVH build
161   mutable Select3D_BndBox3d        myBndBox;             //!< Bounding box of the whole triangulation
162   gp_GTrsf                         myInvInitLocation;
163 };
164 
165 DEFINE_STANDARD_HANDLE(Select3D_SensitiveTriangulation, Select3D_SensitiveSet)
166 
167 #endif // _Select3D_SensitiveTriangulation_Header
168