Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 10:04:53

0001 // Created on: 2016-02-20
0002 // Created by: Kirill Gavrilov
0003 // Copyright (c) 2016 OPEN CASCADE SAS
0004 //
0005 // This file is part of Open CASCADE Technology software library.
0006 //
0007 // This library is free software; you can redistribute it and/or modify it under
0008 // the terms of the GNU Lesser General Public License version 2.1 as published
0009 // by the Free Software Foundation, with special exception defined in the file
0010 // OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
0011 // distribution for complete text of the license and disclaimer of any warranty.
0012 //
0013 // Alternatively, this file may be used under the terms of Open CASCADE
0014 // commercial license or contractual agreement.
0015 
0016 #ifndef _Select3D_SensitivePrimitiveArray_Header
0017 #define _Select3D_SensitivePrimitiveArray_Header
0018 
0019 #include <Graphic3d_IndexBuffer.hxx>
0020 #include <Graphic3d_TypeOfPrimitiveArray.hxx>
0021 #include <Select3D_SensitiveSet.hxx>
0022 #include <Select3D_BVHIndexBuffer.hxx>
0023 #include <TColStd_HPackedMapOfInteger.hxx>
0024 
0025 //! Sensitive for triangulation or point set defined by Primitive Array.
0026 //! The primitives can be optionally combined into patches within BVH tree
0027 //! to reduce its building time in expense of extra traverse time.
0028 class Select3D_SensitivePrimitiveArray : public Select3D_SensitiveSet
0029 {
0030 
0031 public:
0032 
0033   //! Constructs an empty sensitive object.
0034   Standard_EXPORT Select3D_SensitivePrimitiveArray (const Handle(SelectMgr_EntityOwner)& theOwnerId);
0035 
0036   //! Return patch size limit (1 by default).
0037   Standard_Integer PatchSizeMax() const { return myPatchSizeMax; }
0038 
0039   //! Assign patch size limit.
0040   //! Should be set before initialization.
0041   void SetPatchSizeMax (const Standard_Integer thePatchSizeMax) { myPatchSizeMax = thePatchSizeMax; }
0042 
0043   //! Maximum allowed distance between consequential elements in patch (ShortRealLast() by default).
0044   //! Has no effect on indexed triangulation.
0045   float PatchDistance() const { return myPatchDistance; }
0046 
0047   //! Assign patch distance limit.
0048   //! Should be set before initialization.
0049   void SetPatchDistance (const float thePatchDistMax) { myPatchDistance = thePatchDistMax; }
0050 
0051   //! Initialize the sensitive object from triangualtion.
0052   //! The sub-triangulation can be specified by arguments theIndexLower and theIndexUpper
0053   //! (these are for iterating theIndices, not to restrict the actual index values!).
0054   //! @param theVerts        attributes array containing Graphic3d_TOA_POS with type Graphic3d_TOD_VEC3 or Graphic3d_TOD_VEC2
0055   //! @param theIndices      index array defining triangulation
0056   //! @param theInitLoc      location
0057   //! @param theIndexLower   the theIndices range - first value (inclusive), starting from 0 and multiple by 3
0058   //! @param theIndexUpper   the theIndices range - last  value (inclusive), upto theIndices->NbElements-1 and multiple by 3
0059   //! @param theToEvalMinMax compute bounding box within initialization
0060   //! @param theNbGroups     number of groups to split the vertex array into several parts
0061   Standard_EXPORT bool InitTriangulation (const Handle(Graphic3d_Buffer)&      theVerts,
0062                                           const Handle(Graphic3d_IndexBuffer)& theIndices,
0063                                           const TopLoc_Location&               theInitLoc,
0064                                           const Standard_Integer               theIndexLower,
0065                                           const Standard_Integer               theIndexUpper,
0066                                           const bool                           theToEvalMinMax = true,
0067                                           const Standard_Integer               theNbGroups = 1);
0068 
0069   //! Initialize the sensitive object from triangualtion.
0070   //! @param theVerts        attributes array containing Graphic3d_TOA_POS with type Graphic3d_TOD_VEC3 or Graphic3d_TOD_VEC2
0071   //! @param theIndices      index array defining triangulation
0072   //! @param theInitLoc      location
0073   //! @param theToEvalMinMax compute bounding box within initialization
0074   //! @param theNbGroups     number of groups to split the vertex array into several parts
0075   bool InitTriangulation (const Handle(Graphic3d_Buffer)&      theVerts,
0076                           const Handle(Graphic3d_IndexBuffer)& theIndices,
0077                           const TopLoc_Location&               theInitLoc,
0078                           const bool                           theToEvalMinMax = true,
0079                           const Standard_Integer               theNbGroups = 1)
0080   {
0081     const Standard_Integer anUpper = !theIndices.IsNull() ? (theIndices->NbElements - 1)
0082                                                           : (!theVerts.IsNull() ? (theVerts->NbElements - 1) : 0);
0083     return InitTriangulation (theVerts, theIndices, theInitLoc, 0, anUpper, theToEvalMinMax, theNbGroups);
0084   }
0085 
0086   //! Initialize the sensitive object from point set.
0087   //! The sub-set of points can be specified by arguments theIndexLower and theIndexUpper
0088   //! (these are for iterating theIndices, not to restrict the actual index values!).
0089   //! @param theVerts        attributes array containing Graphic3d_TOA_POS with type Graphic3d_TOD_VEC3 or Graphic3d_TOD_VEC2
0090   //! @param theIndices      index array defining points
0091   //! @param theInitLoc      location
0092   //! @param theIndexLower   the theIndices range - first value (inclusive), starting from 0
0093   //! @param theIndexUpper   the theIndices range - last  value (inclusive), upto theIndices->NbElements-1
0094   //! @param theToEvalMinMax compute bounding box within initialization
0095   //! @param theNbGroups     number of groups to split the vertex array into several parts
0096   Standard_EXPORT bool InitPoints (const Handle(Graphic3d_Buffer)&      theVerts,
0097                                    const Handle(Graphic3d_IndexBuffer)& theIndices,
0098                                    const TopLoc_Location&               theInitLoc,
0099                                    const Standard_Integer               theIndexLower,
0100                                    const Standard_Integer               theIndexUpper,
0101                                    const bool                           theToEvalMinMax = true,
0102                                    const Standard_Integer               theNbGroups = 1);
0103 
0104   //! Initialize the sensitive object from point set.
0105   //! @param theVerts        attributes array containing Graphic3d_TOA_POS with type Graphic3d_TOD_VEC3 or Graphic3d_TOD_VEC2
0106   //! @param theIndices      index array to define subset of points
0107   //! @param theInitLoc      location
0108   //! @param theToEvalMinMax compute bounding box within initialization
0109   //! @param theNbGroups     number of groups to split the vertex array into several parts
0110   bool InitPoints (const Handle(Graphic3d_Buffer)&      theVerts,
0111                    const Handle(Graphic3d_IndexBuffer)& theIndices,
0112                    const TopLoc_Location&               theInitLoc,
0113                    const bool                           theToEvalMinMax = true,
0114                    const Standard_Integer               theNbGroups = 1)
0115   {
0116     const Standard_Integer anUpper = !theIndices.IsNull() ? (theIndices->NbElements - 1)
0117                                                           : (!theVerts.IsNull() ? (theVerts->NbElements - 1) : 0);
0118     return InitPoints (theVerts, theIndices, theInitLoc, 0, anUpper, theToEvalMinMax, theNbGroups);
0119   }
0120 
0121   //! Initialize the sensitive object from point set.
0122   //! @param theVerts        attributes array containing Graphic3d_TOA_POS with type Graphic3d_TOD_VEC3 or Graphic3d_TOD_VEC2
0123   //! @param theInitLoc      location
0124   //! @param theToEvalMinMax compute bounding box within initialization
0125   //! @param theNbGroups     number of groups to split the vertex array into several parts
0126   bool InitPoints (const Handle(Graphic3d_Buffer)& theVerts,
0127                    const TopLoc_Location&          theInitLoc,
0128                    const bool                      theToEvalMinMax = true,
0129                    const Standard_Integer          theNbGroups = 1)
0130   {
0131     const Standard_Integer anUpper = !theVerts.IsNull() ? (theVerts->NbElements - 1) : 0;
0132     return InitPoints (theVerts, Handle(Graphic3d_IndexBuffer)(), theInitLoc, 0, anUpper, theToEvalMinMax, theNbGroups);
0133   }
0134 
0135   //! Assign new not transformed bounding box.
0136   void SetMinMax (double theMinX, double theMinY, double theMinZ,
0137                   double theMaxX, double theMaxY, double theMaxZ)
0138   {
0139     myBndBox = Select3D_BndBox3d (SelectMgr_Vec3 (theMinX, theMinY, theMinZ),
0140                                   SelectMgr_Vec3 (theMaxX, theMaxY, theMaxZ));
0141     if (!myGroups.IsNull())
0142     {
0143       for (Select3D_PrimArraySubGroupArray::Iterator aGroupIter (*myGroups); aGroupIter.More(); aGroupIter.Next())
0144       {
0145         aGroupIter.Value()->myBndBox = myBndBox;
0146       }
0147     }
0148   }
0149 
0150   //! Return flag to keep index of last topmost detected element, TRUE by default.
0151   bool ToDetectElements() const { return myToDetectElem; }
0152 
0153   //! Setup keeping of the index of last topmost detected element (axis picking).
0154   void SetDetectElements (bool theToDetect) { myToDetectElem = theToDetect; }
0155 
0156   //! Return flag to keep index map of last detected elements, FALSE by default (rectangle selection).
0157   bool ToDetectElementMap() const { return !myDetectedElemMap.IsNull(); }
0158 
0159   //! Setup keeping of the index map of last detected elements (rectangle selection).
0160   Standard_EXPORT void SetDetectElementMap (bool theToDetect);
0161 
0162   //! Return flag to keep index of last topmost detected node, FALSE by default.
0163   bool ToDetectNodes() const { return myToDetectNode; }
0164 
0165   //! Setup keeping of the index of last topmost detected node (for axis picking).
0166   void SetDetectNodes (bool theToDetect) { myToDetectNode = theToDetect; }
0167 
0168   //! Return flag to keep index map of last detected nodes, FALSE by default (rectangle selection).
0169   bool ToDetectNodeMap() const { return !myDetectedNodeMap.IsNull(); }
0170 
0171   //! Setup keeping of the index map of last detected nodes (rectangle selection).
0172   Standard_EXPORT void SetDetectNodeMap (bool theToDetect);
0173 
0174   //! Return flag to keep index of last topmost detected edge, FALSE by default.
0175   bool ToDetectEdges() const { return myToDetectEdge; }
0176 
0177   //! Setup keeping of the index of last topmost detected edge (axis picking).
0178   void SetDetectEdges (bool theToDetect) { myToDetectEdge = theToDetect; }
0179 
0180   //! Return last topmost detected element or -1 if undefined (axis picking).
0181   Standard_Integer LastDetectedElement() const { return myDetectedElem; }
0182 
0183   //! Return the index map of last detected elements (rectangle selection).
0184   const Handle(TColStd_HPackedMapOfInteger)& LastDetectedElementMap() const { return myDetectedElemMap; }
0185 
0186   //! Return last topmost detected node or -1 if undefined (axis picking).
0187   Standard_Integer LastDetectedNode() const { return myDetectedNode; }
0188 
0189   //! Return the index map of last detected nodes (rectangle selection).
0190   const Handle(TColStd_HPackedMapOfInteger)& LastDetectedNodeMap() const { return myDetectedNodeMap; }
0191 
0192   //! Return the first node of last topmost detected edge or -1 if undefined (axis picking).
0193   Standard_Integer LastDetectedEdgeNode1() const { return myDetectedEdgeNode1; }
0194 
0195   //! Return the second node of last topmost detected edge or -1 if undefined (axis picking).
0196   Standard_Integer LastDetectedEdgeNode2() const { return myDetectedEdgeNode2; }
0197 
0198   //! Dumps the content of me into the stream
0199   Standard_EXPORT virtual void DumpJson (Standard_OStream& theOStream, Standard_Integer theDepth = -1) const Standard_OVERRIDE;
0200 
0201 public:
0202 
0203   //! Checks whether the sensitive entity is overlapped by current selecting volume.
0204   Standard_EXPORT virtual Standard_Boolean Matches (SelectBasics_SelectingVolumeManager& theMgr,
0205                                                     SelectBasics_PickResult&             thePickResult) Standard_OVERRIDE;
0206 
0207   Standard_EXPORT virtual Handle(Select3D_SensitiveEntity) GetConnected() Standard_OVERRIDE;
0208 
0209   //! Returns the length of array of triangles or edges
0210   Standard_EXPORT virtual Standard_Integer Size() const Standard_OVERRIDE;
0211 
0212   //! Returns the amount of nodes in triangulation
0213   virtual Standard_Integer NbSubElements() const Standard_OVERRIDE
0214   {
0215     return !myGroups.IsNull() ? myGroups->Size() : myBvhIndices.NbElements;
0216   }
0217 
0218   //! Returns bounding box of triangle/edge with index theIdx
0219   Standard_EXPORT virtual Select3D_BndBox3d Box (const Standard_Integer theIdx) const Standard_OVERRIDE;
0220 
0221   //! Returns geometry center of triangle/edge with index theIdx
0222   //! in array along the given axis theAxis
0223   Standard_EXPORT virtual Standard_Real Center (const Standard_Integer theIdx,
0224                                                 const Standard_Integer theAxis) const Standard_OVERRIDE;
0225 
0226   //! Swaps items with indexes theIdx1 and theIdx2 in array
0227   Standard_EXPORT virtual void Swap (const Standard_Integer theIdx1,
0228                                      const Standard_Integer theIdx2) Standard_OVERRIDE;
0229 
0230   //! Returns bounding box of the triangulation. If location
0231   //! transformation is set, it will be applied
0232   Standard_EXPORT virtual Select3D_BndBox3d BoundingBox() Standard_OVERRIDE;
0233 
0234   //! Returns center of triangulation. If location transformation
0235   //! is set, it will be applied
0236   virtual gp_Pnt CenterOfGeometry() const Standard_OVERRIDE
0237   {
0238     return myCDG3D;
0239   }
0240 
0241   //! Returns true if the shape corresponding to the entity has init location
0242   virtual Standard_Boolean HasInitLocation() const Standard_OVERRIDE
0243   {
0244     return !myInitLocation.IsIdentity();
0245   }
0246 
0247   //! Returns inversed location transformation matrix if the shape corresponding
0248   //! to this entity has init location set. Otherwise, returns identity matrix.
0249   virtual gp_GTrsf InvInitLocation() const Standard_OVERRIDE
0250   {
0251     return myInvInitLocation;
0252   }
0253 
0254   //! Sets the owner for all entities in group
0255   Standard_EXPORT virtual void Set (const Handle(SelectMgr_EntityOwner)& theOwnerId) Standard_OVERRIDE;
0256 
0257   //! Builds BVH tree for sensitive set.
0258   Standard_EXPORT virtual void BVH() Standard_OVERRIDE;
0259 
0260 protected:
0261 
0262   //! Compute bounding box.
0263   Standard_EXPORT void computeBoundingBox();
0264 
0265   //! Inner function for transformation application to bounding
0266   //! box of the triangulation
0267   Standard_EXPORT Select3D_BndBox3d applyTransformation();
0268 
0269   //! Auxiliary getter.
0270   const Graphic3d_Vec3& getPosVec3 (const Standard_Integer theIndex) const
0271   {
0272     return *reinterpret_cast<const Graphic3d_Vec3* >(myPosData + myPosStride * theIndex);
0273   }
0274 
0275   //! Auxiliary getter.
0276   const Graphic3d_Vec2& getPosVec2 (const Standard_Integer theIndex) const
0277   {
0278     return *reinterpret_cast<const Graphic3d_Vec2* >(myPosData + myPosStride * theIndex);
0279   }
0280 
0281   //! Checks whether the element with index theIdx overlaps the current selecting volume
0282   Standard_EXPORT virtual Standard_Boolean overlapsElement (SelectBasics_PickResult& thePickResult,
0283                                                             SelectBasics_SelectingVolumeManager& theMgr,
0284                                                             Standard_Integer theElemIdx,
0285                                                             Standard_Boolean theIsFullInside) Standard_OVERRIDE;
0286 
0287   //! Calculates distance from the 3d projection of used-picked screen point to center of the geometry
0288   Standard_EXPORT virtual Standard_Real distanceToCOG (SelectBasics_SelectingVolumeManager& theMgr) Standard_OVERRIDE;
0289 
0290   //! Checks whether the entity with index theIdx is inside the current selecting volume
0291   Standard_EXPORT virtual Standard_Boolean elementIsInside (SelectBasics_SelectingVolumeManager& theMgr,
0292                                                             Standard_Integer theElemIdx,
0293                                                             Standard_Boolean theIsFullInside) Standard_OVERRIDE;
0294 
0295 private:
0296 
0297   typedef NCollection_Shared<NCollection_Array1<Handle(Select3D_SensitivePrimitiveArray)> > Select3D_PrimArraySubGroupArray;
0298   struct Select3D_SensitivePrimitiveArray_InitFunctor;
0299   struct Select3D_SensitivePrimitiveArray_BVHFunctor;
0300 
0301 private:
0302 
0303   Handle(Select3D_PrimArraySubGroupArray) myGroups;         //!< sub-groups of sensitive entities
0304 
0305   Handle(Graphic3d_Buffer)            myVerts;              //!< source data - nodes position
0306   Handle(Graphic3d_IndexBuffer)       myIndices;            //!< source data - primitive indexes
0307   const Standard_Byte*                myPosData;            //!< position vertex attribute data
0308   Standard_Size                       myPosStride;          //!< position vertex attribute stride in bytes
0309   Graphic3d_TypeOfPrimitiveArray      myPrimType;           //!< primitives type
0310   Standard_Integer                    myIndexLower;         //!< index range - first index in myIndices (inclusive)
0311   Standard_Integer                    myIndexUpper;         //!< index range - last  index in myIndices (inclusive)
0312   Standard_Integer                    myPatchSizeMax;       //!< patch size limit (1 by default)
0313   float                               myPatchDistance;      //!< distance between elements in patch
0314   bool                                myIs3d;               //!< flag indicating that position attribute has 3 components
0315   TopLoc_Location                     myInitLocation;
0316   gp_Pnt                              myCDG3D;              //!< Center of the whole triangulation
0317   Select3D_BVHIndexBuffer             myBvhIndices;         //!< Indexes of edges or triangles for BVH tree
0318   mutable Select3D_BndBox3d           myBndBox;             //!< Bounding box of the whole triangulation
0319   gp_GTrsf                            myInvInitLocation;
0320   Handle(TColStd_HPackedMapOfInteger) myDetectedElemMap;    //!< index map of last detected elements
0321   Handle(TColStd_HPackedMapOfInteger) myDetectedNodeMap;    //!< index map of last detected nodes
0322   Standard_Real                       myMinDepthElem;       //!< the depth of nearest detected element
0323   Standard_Real                       myMinDepthNode;       //!< the depth of nearest detected node
0324   Standard_Real                       myMinDepthEdge;       //!< the depth of nearest detected edge
0325   Standard_Integer                    myDetectedElem;       //!< index of last detected element
0326   Standard_Integer                    myDetectedNode;       //!< index of last detected node
0327   Standard_Integer                    myDetectedEdgeNode1;  //!< index of last detected edge node 1
0328   Standard_Integer                    myDetectedEdgeNode2;  //!< index of last detected edge node 2
0329   bool                                myToDetectElem;       //!< flag to keep info about last detected element
0330   bool                                myToDetectNode;       //!< flag to keep info about last detected node
0331   bool                                myToDetectEdge;       //!< flag to keep info about last detected edge
0332 
0333 public:
0334 
0335   DEFINE_STANDARD_RTTIEXT(Select3D_SensitivePrimitiveArray, Select3D_SensitiveSet)
0336 
0337 };
0338 
0339 DEFINE_STANDARD_HANDLE(Select3D_SensitivePrimitiveArray, Select3D_SensitiveSet)
0340 
0341 #endif // _Select3D_SensitivePrimitiveArray_Header