0030180: Data Exchange - VrmlAPI_Writer is expected to return export state
[occt.git] / src / BVH / BVH_Tree.hxx
CommitLineData
3c4e78f2 1// Created on: 2013-12-20
2// Created by: Denis BOGOLEPOV
d5f74e42 3// Copyright (c) 2013-2014 OPEN CASCADE SAS
3c4e78f2 4//
5// This file is part of Open CASCADE Technology software library.
6//
d5f74e42 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
3c4e78f2 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
f2474958 16#ifndef _BVH_TreeBase_Header
17#define _BVH_TreeBase_Header
3c4e78f2 18
19#include <BVH_Box.hxx>
20
fc73a202 21template<class T, int N> class BVH_Builder;
22
f5b72419 23//! A non-template class for using as base for BVH_TreeBase
24//! (just to have a named base class).
25class BVH_TreeBaseTransient : public Standard_Transient
26{
27 DEFINE_STANDARD_RTTIEXT(BVH_TreeBaseTransient, Standard_Transient)
28protected:
29 BVH_TreeBaseTransient() {}
30};
31
3c4e78f2 32//! Stores parameters of bounding volume hierarchy (BVH).
33//! Bounding volume hierarchy (BVH) organizes geometric objects in
34//! the tree based on spatial relationships. Each node in the tree
35//! contains an axis-aligned bounding box of all the objects below
36//! it. Bounding volume hierarchies are used in many algorithms to
37//! support efficient operations on the sets of geometric objects,
38//! such as collision detection, ray-tracing, searching of nearest
39//! objects, and view frustum culling.
40template<class T, int N>
f5b72419 41class BVH_TreeBase : public BVH_TreeBaseTransient
3c4e78f2 42{
fc73a202 43 friend class BVH_Builder<T, N>;
44
f2474958 45public: //! @name custom data types
3c4e78f2 46
47 typedef typename BVH_Box<T, N>::BVH_VecNt BVH_VecNt;
48
f2474958 49public: //! @name general methods
3c4e78f2 50
fc73a202 51 //! Creates new empty BVH tree.
f2474958 52 BVH_TreeBase() : myDepth (0) { }
53
54 //! Releases resources of BVH tree.
e28f12b3 55 virtual ~BVH_TreeBase() {}
f2474958 56
57 //! Returns depth (height) of BVH tree.
58 int Depth() const
59 {
60 return myDepth;
61 }
62
63 //! Returns total number of BVH tree nodes.
64 int Length() const
fc73a202 65 {
f2474958 66 return BVH::Array<int, 4>::Size (myNodeInfoBuffer);
fc73a202 67 }
68
f2474958 69public: //! @name methods for accessing individual nodes
65578e1c 70
3c4e78f2 71 //! Returns minimum point of the given node.
f2474958 72 BVH_VecNt& MinPoint (const int theNodeIndex)
3c4e78f2 73 {
679d3878 74 return BVH::Array<T, N>::ChangeValue (myMinPointBuffer, theNodeIndex);
3c4e78f2 75 }
76
77 //! Returns maximum point of the given node.
f2474958 78 BVH_VecNt& MaxPoint (const int theNodeIndex)
3c4e78f2 79 {
679d3878 80 return BVH::Array<T, N>::ChangeValue (myMaxPointBuffer, theNodeIndex);
3c4e78f2 81 }
82
83 //! Returns minimum point of the given node.
f2474958 84 const BVH_VecNt& MinPoint (const int theNodeIndex) const
3c4e78f2 85 {
679d3878 86 return BVH::Array<T, N>::Value (myMinPointBuffer, theNodeIndex);
3c4e78f2 87 }
88
89 //! Returns maximum point of the given node.
f2474958 90 const BVH_VecNt& MaxPoint (const int theNodeIndex) const
3c4e78f2 91 {
679d3878 92 return BVH::Array<T, N>::Value (myMaxPointBuffer, theNodeIndex);
3c4e78f2 93 }
94
3c4e78f2 95 //! Returns index of first primitive of the given leaf node.
f2474958 96 int& BegPrimitive (const int theNodeIndex)
3c4e78f2 97 {
f2474958 98 return BVH::Array<int, 4>::ChangeValue (myNodeInfoBuffer, theNodeIndex).y();
3c4e78f2 99 }
100
f2474958 101 //! Returns index of last primitive of the given leaf node.
102 int& EndPrimitive (const int theNodeIndex)
3c4e78f2 103 {
f2474958 104 return BVH::Array<int, 4>::ChangeValue (myNodeInfoBuffer, theNodeIndex).z();
3c4e78f2 105 }
106
f2474958 107 //! Returns index of first primitive of the given leaf node.
108 int BegPrimitive (const int theNodeIndex) const
3c4e78f2 109 {
f2474958 110 return BVH::Array<int, 4>::Value (myNodeInfoBuffer, theNodeIndex).y();
3c4e78f2 111 }
112
113 //! Returns index of last primitive of the given leaf node.
f2474958 114 int EndPrimitive (const int theNodeIndex) const
3c4e78f2 115 {
f2474958 116 return BVH::Array<int, 4>::Value (myNodeInfoBuffer, theNodeIndex).z();
3c4e78f2 117 }
118
f2474958 119 //! Returns number of primitives in the given leaf node.
120 int NbPrimitives (const int theNodeIndex) const
3c4e78f2 121 {
122 return EndPrimitive (theNodeIndex) - BegPrimitive (theNodeIndex) + 1;
123 }
124
125 //! Returns level (depth) of the given node.
f2474958 126 int& Level (const int theNodeIndex)
3c4e78f2 127 {
f2474958 128 return BVH::Array<int, 4>::ChangeValue (myNodeInfoBuffer, theNodeIndex).w();
3c4e78f2 129 }
130
131 //! Returns level (depth) of the given node.
f2474958 132 int Level (const int theNodeIndex) const
3c4e78f2 133 {
f2474958 134 return BVH::Array<int, 4>::Value (myNodeInfoBuffer, theNodeIndex).w();
3c4e78f2 135 }
136
f2474958 137 //! Checks whether the given node is outer.
138 bool IsOuter (const int theNodeIndex) const
3c4e78f2 139 {
f2474958 140 return BVH::Array<int, 4>::Value (myNodeInfoBuffer, theNodeIndex).x() != 0;
3c4e78f2 141 }
142
f2474958 143public: //! @name methods for accessing serialized tree data
3c4e78f2 144
f2474958 145 //! Returns array of node data records.
146 BVH_Array4i& NodeInfoBuffer()
3c4e78f2 147 {
f2474958 148 return myNodeInfoBuffer;
3c4e78f2 149 }
150
f2474958 151 //! Returns array of node data records.
152 const BVH_Array4i& NodeInfoBuffer() const
fc73a202 153 {
f2474958 154 return myNodeInfoBuffer;
fc73a202 155 }
156
f2474958 157 //! Returns array of node minimum points.
3a7a7013 158 typename BVH::ArrayType<T, N>::Type& MinPointBuffer()
3c4e78f2 159 {
160 return myMinPointBuffer;
161 }
162
f2474958 163 //! Returns array of node maximum points.
3a7a7013 164 typename BVH::ArrayType<T, N>::Type& MaxPointBuffer()
3c4e78f2 165 {
166 return myMaxPointBuffer;
167 }
168
f2474958 169 //! Returns array of node minimum points.
170 const typename BVH::ArrayType<T, N>::Type& MinPointBuffer() const
3c4e78f2 171 {
f2474958 172 return myMinPointBuffer;
3c4e78f2 173 }
174
f2474958 175 //! Returns array of node maximum points.
176 const typename BVH::ArrayType<T, N>::Type& MaxPointBuffer() const
3c4e78f2 177 {
f2474958 178 return myMaxPointBuffer;
3c4e78f2 179 }
180
f2474958 181public: //! @name protected fields
3c4e78f2 182
f2474958 183 //! Array of node data records.
184 BVH_Array4i myNodeInfoBuffer;
3c4e78f2 185
186 //! Array of node minimum points.
3a7a7013 187 typename BVH::ArrayType<T, N>::Type myMinPointBuffer;
3c4e78f2 188
189 //! Array of node maximum points.
3a7a7013 190 typename BVH::ArrayType<T, N>::Type myMaxPointBuffer;
3c4e78f2 191
f2474958 192 //! Current depth of BVH tree (set by builder).
193 int myDepth;
3c4e78f2 194
f2474958 195};
fc73a202 196
f2474958 197//! Type corresponding to quad BVH.
198struct BVH_QuadTree { };
199
200//! Type corresponding to binary BVH.
201struct BVH_BinaryTree { };
202
203//! BVH tree with given arity (2 or 4).
204template<class T, int N, class Arity = BVH_BinaryTree>
205class BVH_Tree
206{
207 // Invalid type
3c4e78f2 208};
209
f2474958 210#endif // _BVH_TreeBase_Header