0033661: Data Exchange, Step Import - Tessellated GDTs are not imported
[occt.git] / src / Select3D / Select3D_SensitivePrimitiveArray.hxx
1 // Created on: 2016-02-20
2 // Created by: Kirill Gavrilov
3 // Copyright (c) 2016 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 #ifndef _Select3D_SensitivePrimitiveArray_Header
17 #define _Select3D_SensitivePrimitiveArray_Header
18
19 #include <Graphic3d_IndexBuffer.hxx>
20 #include <Graphic3d_TypeOfPrimitiveArray.hxx>
21 #include <Select3D_SensitiveSet.hxx>
22 #include <Select3D_BVHIndexBuffer.hxx>
23 #include <TColStd_HPackedMapOfInteger.hxx>
24
25 //! Sensitive for triangulation or point set defined by Primitive Array.
26 //! The primitives can be optionally combined into patches within BVH tree
27 //! to reduce its building time in expense of extra traverse time.
28 class Select3D_SensitivePrimitiveArray : public Select3D_SensitiveSet
29 {
30
31 public:
32
33   //! Constructs an empty sensitive object.
34   Standard_EXPORT Select3D_SensitivePrimitiveArray (const Handle(SelectMgr_EntityOwner)& theOwnerId);
35
36   //! Return patch size limit (1 by default).
37   Standard_Integer PatchSizeMax() const { return myPatchSizeMax; }
38
39   //! Assign patch size limit.
40   //! Should be set before initialization.
41   void SetPatchSizeMax (const Standard_Integer thePatchSizeMax) { myPatchSizeMax = thePatchSizeMax; }
42
43   //! Maximum allowed distance between consequential elements in patch (ShortRealLast() by default).
44   //! Has no effect on indexed triangulation.
45   float PatchDistance() const { return myPatchDistance; }
46
47   //! Assign patch distance limit.
48   //! Should be set before initialization.
49   void SetPatchDistance (const float thePatchDistMax) { myPatchDistance = thePatchDistMax; }
50
51   //! Initialize the sensitive object from triangualtion.
52   //! The sub-triangulation can be specified by arguments theIndexLower and theIndexUpper
53   //! (these are for iterating theIndices, not to restrict the actual index values!).
54   //! @param theVerts        attributes array containing Graphic3d_TOA_POS with type Graphic3d_TOD_VEC3 or Graphic3d_TOD_VEC2
55   //! @param theIndices      index array defining triangulation
56   //! @param theInitLoc      location
57   //! @param theIndexLower   the theIndices range - first value (inclusive), starting from 0 and multiple by 3
58   //! @param theIndexUpper   the theIndices range - last  value (inclusive), upto theIndices->NbElements-1 and multiple by 3
59   //! @param theToEvalMinMax compute bounding box within initialization
60   //! @param theNbGroups     number of groups to split the vertex array into several parts
61   Standard_EXPORT bool InitTriangulation (const Handle(Graphic3d_Buffer)&      theVerts,
62                                           const Handle(Graphic3d_IndexBuffer)& theIndices,
63                                           const TopLoc_Location&               theInitLoc,
64                                           const Standard_Integer               theIndexLower,
65                                           const Standard_Integer               theIndexUpper,
66                                           const bool                           theToEvalMinMax = true,
67                                           const Standard_Integer               theNbGroups = 1);
68
69   //! Initialize the sensitive object from triangualtion.
70   //! @param theVerts        attributes array containing Graphic3d_TOA_POS with type Graphic3d_TOD_VEC3 or Graphic3d_TOD_VEC2
71   //! @param theIndices      index array defining triangulation
72   //! @param theInitLoc      location
73   //! @param theToEvalMinMax compute bounding box within initialization
74   //! @param theNbGroups     number of groups to split the vertex array into several parts
75   bool InitTriangulation (const Handle(Graphic3d_Buffer)&      theVerts,
76                           const Handle(Graphic3d_IndexBuffer)& theIndices,
77                           const TopLoc_Location&               theInitLoc,
78                           const bool                           theToEvalMinMax = true,
79                           const Standard_Integer               theNbGroups = 1)
80   {
81     const Standard_Integer anUpper = !theIndices.IsNull() ? (theIndices->NbElements - 1)
82                                                           : (!theVerts.IsNull() ? (theVerts->NbElements - 1) : 0);
83     return InitTriangulation (theVerts, theIndices, theInitLoc, 0, anUpper, theToEvalMinMax, theNbGroups);
84   }
85
86   //! Initialize the sensitive object from point set.
87   //! The sub-set of points can be specified by arguments theIndexLower and theIndexUpper
88   //! (these are for iterating theIndices, not to restrict the actual index values!).
89   //! @param theVerts        attributes array containing Graphic3d_TOA_POS with type Graphic3d_TOD_VEC3 or Graphic3d_TOD_VEC2
90   //! @param theIndices      index array defining points
91   //! @param theInitLoc      location
92   //! @param theIndexLower   the theIndices range - first value (inclusive), starting from 0
93   //! @param theIndexUpper   the theIndices range - last  value (inclusive), upto theIndices->NbElements-1
94   //! @param theToEvalMinMax compute bounding box within initialization
95   //! @param theNbGroups     number of groups to split the vertex array into several parts
96   Standard_EXPORT bool InitPoints (const Handle(Graphic3d_Buffer)&      theVerts,
97                                    const Handle(Graphic3d_IndexBuffer)& theIndices,
98                                    const TopLoc_Location&               theInitLoc,
99                                    const Standard_Integer               theIndexLower,
100                                    const Standard_Integer               theIndexUpper,
101                                    const bool                           theToEvalMinMax = true,
102                                    const Standard_Integer               theNbGroups = 1);
103
104   //! Initialize the sensitive object from point set.
105   //! @param theVerts        attributes array containing Graphic3d_TOA_POS with type Graphic3d_TOD_VEC3 or Graphic3d_TOD_VEC2
106   //! @param theIndices      index array to define subset of points
107   //! @param theInitLoc      location
108   //! @param theToEvalMinMax compute bounding box within initialization
109   //! @param theNbGroups     number of groups to split the vertex array into several parts
110   bool InitPoints (const Handle(Graphic3d_Buffer)&      theVerts,
111                    const Handle(Graphic3d_IndexBuffer)& theIndices,
112                    const TopLoc_Location&               theInitLoc,
113                    const bool                           theToEvalMinMax = true,
114                    const Standard_Integer               theNbGroups = 1)
115   {
116     const Standard_Integer anUpper = !theIndices.IsNull() ? (theIndices->NbElements - 1)
117                                                           : (!theVerts.IsNull() ? (theVerts->NbElements - 1) : 0);
118     return InitPoints (theVerts, theIndices, theInitLoc, 0, anUpper, theToEvalMinMax, theNbGroups);
119   }
120
121   //! Initialize the sensitive object from point set.
122   //! @param theVerts        attributes array containing Graphic3d_TOA_POS with type Graphic3d_TOD_VEC3 or Graphic3d_TOD_VEC2
123   //! @param theInitLoc      location
124   //! @param theToEvalMinMax compute bounding box within initialization
125   //! @param theNbGroups     number of groups to split the vertex array into several parts
126   bool InitPoints (const Handle(Graphic3d_Buffer)& theVerts,
127                    const TopLoc_Location&          theInitLoc,
128                    const bool                      theToEvalMinMax = true,
129                    const Standard_Integer          theNbGroups = 1)
130   {
131     const Standard_Integer anUpper = !theVerts.IsNull() ? (theVerts->NbElements - 1) : 0;
132     return InitPoints (theVerts, Handle(Graphic3d_IndexBuffer)(), theInitLoc, 0, anUpper, theToEvalMinMax, theNbGroups);
133   }
134
135   //! Assign new not transformed bounding box.
136   void SetMinMax (double theMinX, double theMinY, double theMinZ,
137                   double theMaxX, double theMaxY, double theMaxZ)
138   {
139     myBndBox = Select3D_BndBox3d (SelectMgr_Vec3 (theMinX, theMinY, theMinZ),
140                                   SelectMgr_Vec3 (theMaxX, theMaxY, theMaxZ));
141     if (!myGroups.IsNull())
142     {
143       for (Select3D_PrimArraySubGroupArray::Iterator aGroupIter (*myGroups); aGroupIter.More(); aGroupIter.Next())
144       {
145         aGroupIter.Value()->myBndBox = myBndBox;
146       }
147     }
148   }
149
150   //! Return flag to keep index of last topmost detected element, TRUE by default.
151   bool ToDetectElements() const { return myToDetectElem; }
152
153   //! Setup keeping of the index of last topmost detected element (axis picking).
154   void SetDetectElements (bool theToDetect) { myToDetectElem = theToDetect; }
155
156   //! Return flag to keep index map of last detected elements, FALSE by default (rectangle selection).
157   bool ToDetectElementMap() const { return !myDetectedElemMap.IsNull(); }
158
159   //! Setup keeping of the index map of last detected elements (rectangle selection).
160   Standard_EXPORT void SetDetectElementMap (bool theToDetect);
161
162   //! Return flag to keep index of last topmost detected node, FALSE by default.
163   bool ToDetectNodes() const { return myToDetectNode; }
164
165   //! Setup keeping of the index of last topmost detected node (for axis picking).
166   void SetDetectNodes (bool theToDetect) { myToDetectNode = theToDetect; }
167
168   //! Return flag to keep index map of last detected nodes, FALSE by default (rectangle selection).
169   bool ToDetectNodeMap() const { return !myDetectedNodeMap.IsNull(); }
170
171   //! Setup keeping of the index map of last detected nodes (rectangle selection).
172   Standard_EXPORT void SetDetectNodeMap (bool theToDetect);
173
174   //! Return flag to keep index of last topmost detected edge, FALSE by default.
175   bool ToDetectEdges() const { return myToDetectEdge; }
176
177   //! Setup keeping of the index of last topmost detected edge (axis picking).
178   void SetDetectEdges (bool theToDetect) { myToDetectEdge = theToDetect; }
179
180   //! Return last topmost detected element or -1 if undefined (axis picking).
181   Standard_Integer LastDetectedElement() const { return myDetectedElem; }
182
183   //! Return the index map of last detected elements (rectangle selection).
184   const Handle(TColStd_HPackedMapOfInteger)& LastDetectedElementMap() const { return myDetectedElemMap; }
185
186   //! Return last topmost detected node or -1 if undefined (axis picking).
187   Standard_Integer LastDetectedNode() const { return myDetectedNode; }
188
189   //! Return the index map of last detected nodes (rectangle selection).
190   const Handle(TColStd_HPackedMapOfInteger)& LastDetectedNodeMap() const { return myDetectedNodeMap; }
191
192   //! Return the first node of last topmost detected edge or -1 if undefined (axis picking).
193   Standard_Integer LastDetectedEdgeNode1() const { return myDetectedEdgeNode1; }
194
195   //! Return the second node of last topmost detected edge or -1 if undefined (axis picking).
196   Standard_Integer LastDetectedEdgeNode2() const { return myDetectedEdgeNode2; }
197
198   //! Dumps the content of me into the stream
199   Standard_EXPORT virtual void DumpJson (Standard_OStream& theOStream, Standard_Integer theDepth = -1) const Standard_OVERRIDE;
200
201 public:
202
203   //! Checks whether the sensitive entity is overlapped by current selecting volume.
204   Standard_EXPORT virtual Standard_Boolean Matches (SelectBasics_SelectingVolumeManager& theMgr,
205                                                     SelectBasics_PickResult&             thePickResult) Standard_OVERRIDE;
206
207   Standard_EXPORT virtual Handle(Select3D_SensitiveEntity) GetConnected() Standard_OVERRIDE;
208
209   //! Returns the length of array of triangles or edges
210   Standard_EXPORT virtual Standard_Integer Size() const Standard_OVERRIDE;
211
212   //! Returns the amount of nodes in triangulation
213   virtual Standard_Integer NbSubElements() const Standard_OVERRIDE
214   {
215     return !myGroups.IsNull() ? myGroups->Size() : myBvhIndices.NbElements;
216   }
217
218   //! Returns bounding box of triangle/edge with index theIdx
219   Standard_EXPORT virtual Select3D_BndBox3d Box (const Standard_Integer theIdx) const Standard_OVERRIDE;
220
221   //! Returns geometry center of triangle/edge with index theIdx
222   //! in array along the given axis theAxis
223   Standard_EXPORT virtual Standard_Real Center (const Standard_Integer theIdx,
224                                                 const Standard_Integer theAxis) const Standard_OVERRIDE;
225
226   //! Swaps items with indexes theIdx1 and theIdx2 in array
227   Standard_EXPORT virtual void Swap (const Standard_Integer theIdx1,
228                                      const Standard_Integer theIdx2) Standard_OVERRIDE;
229
230   //! Returns bounding box of the triangulation. If location
231   //! transformation is set, it will be applied
232   Standard_EXPORT virtual Select3D_BndBox3d BoundingBox() Standard_OVERRIDE;
233
234   //! Returns center of triangulation. If location transformation
235   //! is set, it will be applied
236   virtual gp_Pnt CenterOfGeometry() const Standard_OVERRIDE
237   {
238     return myCDG3D;
239   }
240
241   //! Returns true if the shape corresponding to the entity has init location
242   virtual Standard_Boolean HasInitLocation() const Standard_OVERRIDE
243   {
244     return !myInitLocation.IsIdentity();
245   }
246
247   //! Returns inversed location transformation matrix if the shape corresponding
248   //! to this entity has init location set. Otherwise, returns identity matrix.
249   virtual gp_GTrsf InvInitLocation() const Standard_OVERRIDE
250   {
251     return myInvInitLocation;
252   }
253
254   //! Sets the owner for all entities in group
255   Standard_EXPORT virtual void Set (const Handle(SelectMgr_EntityOwner)& theOwnerId) Standard_OVERRIDE;
256
257   //! Builds BVH tree for sensitive set.
258   Standard_EXPORT virtual void BVH() Standard_OVERRIDE;
259
260 protected:
261
262   //! Compute bounding box.
263   Standard_EXPORT void computeBoundingBox();
264
265   //! Inner function for transformation application to bounding
266   //! box of the triangulation
267   Standard_EXPORT Select3D_BndBox3d applyTransformation();
268
269   //! Auxiliary getter.
270   const Graphic3d_Vec3& getPosVec3 (const Standard_Integer theIndex) const
271   {
272     return *reinterpret_cast<const Graphic3d_Vec3* >(myPosData + myPosStride * theIndex);
273   }
274
275   //! Auxiliary getter.
276   const Graphic3d_Vec2& getPosVec2 (const Standard_Integer theIndex) const
277   {
278     return *reinterpret_cast<const Graphic3d_Vec2* >(myPosData + myPosStride * theIndex);
279   }
280
281   //! Checks whether the element with index theIdx overlaps the current selecting volume
282   Standard_EXPORT virtual Standard_Boolean overlapsElement (SelectBasics_PickResult& thePickResult,
283                                                             SelectBasics_SelectingVolumeManager& theMgr,
284                                                             Standard_Integer theElemIdx,
285                                                             Standard_Boolean theIsFullInside) Standard_OVERRIDE;
286
287   //! Calculates distance from the 3d projection of used-picked screen point to center of the geometry
288   Standard_EXPORT virtual Standard_Real distanceToCOG (SelectBasics_SelectingVolumeManager& theMgr) Standard_OVERRIDE;
289
290   //! Checks whether the entity with index theIdx is inside the current selecting volume
291   Standard_EXPORT virtual Standard_Boolean elementIsInside (SelectBasics_SelectingVolumeManager& theMgr,
292                                                             Standard_Integer theElemIdx,
293                                                             Standard_Boolean theIsFullInside) Standard_OVERRIDE;
294
295 private:
296
297   typedef NCollection_Shared<NCollection_Array1<Handle(Select3D_SensitivePrimitiveArray)> > Select3D_PrimArraySubGroupArray;
298   struct Select3D_SensitivePrimitiveArray_InitFunctor;
299   struct Select3D_SensitivePrimitiveArray_BVHFunctor;
300
301 private:
302
303   Handle(Select3D_PrimArraySubGroupArray) myGroups;         //!< sub-groups of sensitive entities
304
305   Handle(Graphic3d_Buffer)            myVerts;              //!< source data - nodes position
306   Handle(Graphic3d_IndexBuffer)       myIndices;            //!< source data - primitive indexes
307   const Standard_Byte*                myPosData;            //!< position vertex attribute data
308   Standard_Size                       myPosStride;          //!< position vertex attribute stride in bytes
309   Graphic3d_TypeOfPrimitiveArray      myPrimType;           //!< primitives type
310   Standard_Integer                    myIndexLower;         //!< index range - first index in myIndices (inclusive)
311   Standard_Integer                    myIndexUpper;         //!< index range - last  index in myIndices (inclusive)
312   Standard_Integer                    myPatchSizeMax;       //!< patch size limit (1 by default)
313   float                               myPatchDistance;      //!< distance between elements in patch
314   bool                                myIs3d;               //!< flag indicating that position attribute has 3 components
315   TopLoc_Location                     myInitLocation;
316   gp_Pnt                              myCDG3D;              //!< Center of the whole triangulation
317   Select3D_BVHIndexBuffer             myBvhIndices;         //!< Indexes of edges or triangles for BVH tree
318   mutable Select3D_BndBox3d           myBndBox;             //!< Bounding box of the whole triangulation
319   gp_GTrsf                            myInvInitLocation;
320   Handle(TColStd_HPackedMapOfInteger) myDetectedElemMap;    //!< index map of last detected elements
321   Handle(TColStd_HPackedMapOfInteger) myDetectedNodeMap;    //!< index map of last detected nodes
322   Standard_Real                       myMinDepthElem;       //!< the depth of nearest detected element
323   Standard_Real                       myMinDepthNode;       //!< the depth of nearest detected node
324   Standard_Real                       myMinDepthEdge;       //!< the depth of nearest detected edge
325   Standard_Integer                    myDetectedElem;       //!< index of last detected element
326   Standard_Integer                    myDetectedNode;       //!< index of last detected node
327   Standard_Integer                    myDetectedEdgeNode1;  //!< index of last detected edge node 1
328   Standard_Integer                    myDetectedEdgeNode2;  //!< index of last detected edge node 2
329   bool                                myToDetectElem;       //!< flag to keep info about last detected element
330   bool                                myToDetectNode;       //!< flag to keep info about last detected node
331   bool                                myToDetectEdge;       //!< flag to keep info about last detected edge
332
333 public:
334
335   DEFINE_STANDARD_RTTIEXT(Select3D_SensitivePrimitiveArray, Select3D_SensitiveSet)
336
337 };
338
339 DEFINE_STANDARD_HANDLE(Select3D_SensitivePrimitiveArray, Select3D_SensitiveSet)
340
341 #endif // _Select3D_SensitivePrimitiveArray_Header