Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-06-13 08:29:58

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