NTrace
GPU ray tracing framework
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
KDTreeNode.hpp
Go to the documentation of this file.
1 /*
2 * Copyright (c) 2013, Radek Stibora
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
7 * * Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * * Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
12 * * Neither the name of the <organization> nor the
13 * names of its contributors may be used to endorse or promote products
14 * derived from this software without specific prior written permission.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19 * DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY
20 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
21 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
23 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
25 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 */
27 
28 #pragma once
29 #include "bvh/Platform.hpp"
30 #include "Util.hpp"
31 
32 namespace FW
33 {
34 
39 {
46 };
47 
52 {
53 public:
58  virtual bool isLeaf() const = 0;
59 
64  virtual S32 getNumChildNodes() const = 0;
65 
71  virtual KDTreeNode* getChildNode(S32 i) const = 0;
72 
77  virtual S32 getNumTriangles() const { return 0; }
78 
82  void deleteSubtree();
83 
89 };
90 
94 class KDTInnerNode : public KDTreeNode
95 {
96 public:
104  KDTInnerNode(F32 split, S32 axis, KDTreeNode* child0, KDTreeNode* child1)
105  { m_pos = split, m_axis = axis, m_children[0] = child0; m_children[1] = child1; }
106 
111  bool isLeaf() const { return false; }
112 
117  S32 getNumChildNodes() const { return 2; }
118 
124  KDTreeNode* getChildNode(S32 i) const { FW_ASSERT(i>=0 && i<2); return m_children[i]; }
125 
129 };
130 
134 class KDTLeafNode : public KDTreeNode
135 {
136 public:
142  KDTLeafNode(int lo, int hi) { m_lo = lo; m_hi=hi; }
143 
148  bool isLeaf() const { return true; }
149 
154  S32 getNumChildNodes() const { return 0; }
155 
161  KDTreeNode* getChildNode(S32) const { return NULL; }
162 
167  S32 getNumTriangles() const { return m_hi - m_lo; }
168 
171 };
172 
173 
174 }
virtual S32 getNumChildNodes() const =0
Returns number of the node's child nodes.
#define NULL
Definition: Defs.hpp:39
F32 m_pos
Split position.
Definition: KDTreeNode.hpp:127
KDTREE_STAT
Available statistics. Used in getSubtreeSize.
Definition: KDTreeNode.hpp:38
KDTreeNode * getChildNode(S32) const
Returns node's child node (left or right).
Definition: KDTreeNode.hpp:161
S32 m_hi
Higher index to the tree's triangle references array.
Definition: KDTreeNode.hpp:170
KDTInnerNode(F32 split, S32 axis, KDTreeNode *child0, KDTreeNode *child1)
Constructor.
Definition: KDTreeNode.hpp:104
virtual S32 getNumTriangles() const
Returns number of triangles this node references. Only leaf nodes will return non-zero values...
Definition: KDTreeNode.hpp:77
int getSubtreeSize(KDTREE_STAT stat=KDTREE_STAT_NODE_COUNT) const
Computes given statistics about node's subtree.
Definition: KDTreeNode.cpp:43
S32 m_lo
Lower index to the tree's triangle references array.
Definition: KDTreeNode.hpp:169
S32 getNumTriangles() const
Returns number of triangles this node references.
Definition: KDTreeNode.hpp:167
KDTreeNode * getChildNode(S32 i) const
Returns node's child node (left or right).
Definition: KDTreeNode.hpp:124
float F32
Definition: Defs.hpp:89
K-d tree's leaf node class.
Definition: KDTreeNode.hpp:134
K-d tree's inner node class.
Definition: KDTreeNode.hpp:94
KDTreeNode * m_children[2]
Node's child nodes.
Definition: KDTreeNode.hpp:126
#define FW_ASSERT(X)
Definition: Defs.hpp:67
signed int S32
Definition: Defs.hpp:88
S32 getNumChildNodes() const
Returns number of the node's child nodes.
Definition: KDTreeNode.hpp:154
void deleteSubtree()
Deletes node's subtree.
Definition: KDTreeNode.cpp:34
Declarations for platform settings.
S32 m_axis
Split dimension.
Definition: KDTreeNode.hpp:128
virtual KDTreeNode * getChildNode(S32 i) const =0
Returns node's child node (left or right).
bool isLeaf() const
Returns whether the node is a leaf node.
Definition: KDTreeNode.hpp:148
KDTLeafNode(int lo, int hi)
Constructor.
Definition: KDTreeNode.hpp:142
S32 getNumChildNodes() const
Returns number of the node's child nodes.
Definition: KDTreeNode.hpp:117
bool isLeaf() const
Returns whether the node is a leaf node.
Definition: KDTreeNode.hpp:111
K-d tree virtual parent node class.
Definition: KDTreeNode.hpp:51
virtual bool isLeaf() const =0
Returns whether the node is a leaf node.