Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 10:03:19

0001 // Created on: 2013-12-20
0002 // Created by: Denis BOGOLEPOV
0003 // Copyright (c) 2013-2014 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 _BVH_TreeBase_Header
0017 #define _BVH_TreeBase_Header
0018 
0019 #include <BVH_Box.hxx>
0020 
0021 template<class T, int N> class BVH_Builder;
0022 
0023 //! A non-template class for using as base for BVH_TreeBase
0024 //! (just to have a named base class).
0025 class BVH_TreeBaseTransient : public Standard_Transient
0026 {
0027   DEFINE_STANDARD_RTTIEXT(BVH_TreeBaseTransient, Standard_Transient)
0028 protected:
0029   BVH_TreeBaseTransient() {}
0030 
0031   //! Dumps the content of me into the stream
0032   virtual void DumpJson (Standard_OStream& theOStream, Standard_Integer theDepth = -1) const { (void)theOStream; (void)theDepth; }
0033 
0034   //! Dumps the content of me into the stream
0035   virtual void DumpNode (const int theNodeIndex, Standard_OStream& theOStream, Standard_Integer theDepth) const
0036   { (void)theNodeIndex; (void)theOStream; (void)theDepth; }
0037 };
0038 
0039 //! Stores parameters of bounding volume hierarchy (BVH).
0040 //! Bounding volume hierarchy (BVH) organizes geometric objects in
0041 //! the tree based on spatial relationships. Each node in the tree
0042 //! contains an axis-aligned bounding box of all the objects below
0043 //! it. Bounding volume hierarchies are used in many algorithms to
0044 //! support efficient operations on the sets of geometric objects,
0045 //! such as collision detection, ray-tracing, searching of nearest
0046 //! objects, and view frustum culling.
0047 template<class T, int N>
0048 class BVH_TreeBase : public BVH_TreeBaseTransient
0049 {
0050   friend class BVH_Builder<T, N>;
0051 
0052 public: //! @name custom data types
0053 
0054   typedef typename BVH_Box<T, N>::BVH_VecNt BVH_VecNt;
0055 
0056 public: //! @name general methods
0057 
0058   //! Creates new empty BVH tree.
0059   BVH_TreeBase() : myDepth (0) { }
0060 
0061   //! Releases resources of BVH tree.
0062   virtual ~BVH_TreeBase() {}
0063 
0064   //! Returns depth (height) of BVH tree.
0065   int Depth() const
0066   {
0067     return myDepth;
0068   }
0069 
0070   //! Returns total number of BVH tree nodes.
0071   int Length() const
0072   {
0073     return BVH::Array<int, 4>::Size (myNodeInfoBuffer);
0074   }
0075 
0076 public: //! @name methods for accessing individual nodes
0077 
0078   //! Returns minimum point of the given node.
0079   BVH_VecNt& MinPoint (const int theNodeIndex)
0080   {
0081     return BVH::Array<T, N>::ChangeValue (myMinPointBuffer, theNodeIndex);
0082   }
0083 
0084   //! Returns maximum point of the given node.
0085   BVH_VecNt& MaxPoint (const int theNodeIndex)
0086   {
0087     return BVH::Array<T, N>::ChangeValue (myMaxPointBuffer, theNodeIndex);
0088   }
0089 
0090   //! Returns minimum point of the given node.
0091   const BVH_VecNt& MinPoint (const int theNodeIndex) const
0092   {
0093     return BVH::Array<T, N>::Value (myMinPointBuffer, theNodeIndex);
0094   }
0095 
0096   //! Returns maximum point of the given node.
0097   const BVH_VecNt& MaxPoint (const int theNodeIndex) const
0098   {
0099     return BVH::Array<T, N>::Value (myMaxPointBuffer, theNodeIndex);
0100   }
0101 
0102   //! Returns index of first primitive of the given leaf node.
0103   int& BegPrimitive (const int theNodeIndex)
0104   {
0105     return BVH::Array<int, 4>::ChangeValue (myNodeInfoBuffer, theNodeIndex).y();
0106   }
0107 
0108   //! Returns index of last primitive of the given leaf node.
0109   int& EndPrimitive (const int theNodeIndex)
0110   {
0111     return BVH::Array<int, 4>::ChangeValue (myNodeInfoBuffer, theNodeIndex).z();
0112   }
0113 
0114   //! Returns index of first primitive of the given leaf node.
0115   int BegPrimitive (const int theNodeIndex) const
0116   {
0117     return BVH::Array<int, 4>::Value (myNodeInfoBuffer, theNodeIndex).y();
0118   }
0119 
0120   //! Returns index of last primitive of the given leaf node.
0121   int EndPrimitive (const int theNodeIndex) const
0122   {
0123     return BVH::Array<int, 4>::Value (myNodeInfoBuffer, theNodeIndex).z();
0124   }
0125 
0126   //! Returns number of primitives in the given leaf node.
0127   int NbPrimitives (const int theNodeIndex) const
0128   {
0129     return EndPrimitive (theNodeIndex) - BegPrimitive (theNodeIndex) + 1;
0130   }
0131 
0132   //! Returns level (depth) of the given node.
0133   int& Level (const int theNodeIndex)
0134   {
0135     return BVH::Array<int, 4>::ChangeValue (myNodeInfoBuffer, theNodeIndex).w();
0136   }
0137 
0138   //! Returns level (depth) of the given node.
0139   int Level (const int theNodeIndex) const
0140   {
0141     return BVH::Array<int, 4>::Value (myNodeInfoBuffer, theNodeIndex).w();
0142   }
0143 
0144   //! Checks whether the given node is outer.
0145   bool IsOuter (const int theNodeIndex) const
0146   {
0147     return BVH::Array<int, 4>::Value (myNodeInfoBuffer, theNodeIndex).x() != 0;
0148   }
0149 
0150 public: //! @name methods for accessing serialized tree data
0151 
0152   //! Returns array of node data records.
0153   BVH_Array4i& NodeInfoBuffer()
0154   {
0155     return myNodeInfoBuffer;
0156   }
0157 
0158   //! Returns array of node data records.
0159   const BVH_Array4i& NodeInfoBuffer() const
0160   {
0161     return myNodeInfoBuffer;
0162   }
0163 
0164   //! Returns array of node minimum points.
0165   typename BVH::ArrayType<T, N>::Type& MinPointBuffer()
0166   {
0167     return myMinPointBuffer;
0168   }
0169 
0170   //! Returns array of node maximum points.
0171   typename BVH::ArrayType<T, N>::Type& MaxPointBuffer()
0172   {
0173     return myMaxPointBuffer;
0174   }
0175 
0176   //! Returns array of node minimum points.
0177   const typename BVH::ArrayType<T, N>::Type& MinPointBuffer() const
0178   {
0179     return myMinPointBuffer;
0180   }
0181 
0182   //! Returns array of node maximum points.
0183   const typename BVH::ArrayType<T, N>::Type& MaxPointBuffer() const
0184   {
0185     return myMaxPointBuffer;
0186   }
0187 
0188   //! Dumps the content of me into the stream
0189   virtual void DumpJson (Standard_OStream& theOStream, Standard_Integer theDepth = -1) const Standard_OVERRIDE
0190   {
0191     OCCT_DUMP_TRANSIENT_CLASS_BEGIN (theOStream)
0192     OCCT_DUMP_FIELD_VALUE_NUMERICAL (theOStream, myDepth)
0193     OCCT_DUMP_FIELD_VALUE_NUMERICAL (theOStream, Length())
0194 
0195     for (Standard_Integer aNodeIdx = 0; aNodeIdx < Length(); ++aNodeIdx)
0196     {
0197       DumpNode (aNodeIdx, theOStream, theDepth);
0198     }
0199   }
0200 
0201   //! Dumps the content of node into the stream
0202   virtual void DumpNode (const int theNodeIndex, Standard_OStream& theOStream, Standard_Integer theDepth) const Standard_OVERRIDE
0203   {
0204     OCCT_DUMP_CLASS_BEGIN (theOStream, BVH_TreeNode)
0205 
0206     OCCT_DUMP_FIELD_VALUE_NUMERICAL (theOStream, theNodeIndex)
0207 
0208     Bnd_Box aBndBox = BVH::ToBndBox (MinPoint (theNodeIndex), MaxPoint (theNodeIndex));
0209     Bnd_Box* aPointer = &aBndBox;
0210     OCCT_DUMP_FIELD_VALUES_DUMPED (theOStream, theDepth, aPointer)
0211 
0212     OCCT_DUMP_FIELD_VALUE_NUMERICAL (theOStream, BegPrimitive (theNodeIndex))
0213     OCCT_DUMP_FIELD_VALUE_NUMERICAL (theOStream, EndPrimitive (theNodeIndex))
0214     OCCT_DUMP_FIELD_VALUE_NUMERICAL (theOStream, Level (theNodeIndex))
0215     OCCT_DUMP_FIELD_VALUE_NUMERICAL (theOStream, IsOuter (theNodeIndex))
0216   }
0217 
0218 public: //! @name protected fields
0219 
0220   //! Array of node data records.
0221   BVH_Array4i myNodeInfoBuffer;
0222 
0223   //! Array of node minimum points.
0224   typename BVH::ArrayType<T, N>::Type myMinPointBuffer;
0225 
0226   //! Array of node maximum points.
0227   typename BVH::ArrayType<T, N>::Type myMaxPointBuffer;
0228 
0229   //! Current depth of BVH tree (set by builder).
0230   int myDepth;
0231 
0232 };
0233 
0234 //! Type corresponding to quad BVH.
0235 struct BVH_QuadTree { };
0236 
0237 //! Type corresponding to binary BVH.
0238 struct BVH_BinaryTree { };
0239 
0240 //! BVH tree with given arity (2 or 4).
0241 template<class T, int N, class Arity = BVH_BinaryTree>
0242 class BVH_Tree
0243 {
0244   // Invalid type
0245 };
0246 
0247 #endif // _BVH_TreeBase_Header