NTrace
GPU ray tracing framework
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
KDTreeNode.cpp
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 #include "KDTreeNode.hpp"
29 
30 namespace FW
31 {
32 
33 
35 {
36  for(int i = 0; i < getNumChildNodes(); i++)
38 
39  delete this;
40 }
41 
42 
44 {
45  int cnt;
46  switch(stat)
47  {
48  default: FW_ASSERT(0); // unknown mode
49  case KDTREE_STAT_NODE_COUNT: cnt = 1; break;
50  case KDTREE_STAT_LEAF_COUNT: cnt = isLeaf() ? 1 : 0; break;
51  case KDTREE_STAT_INNER_COUNT: cnt = isLeaf() ? 0 : 1; break;
52  case KDTREE_STAT_TRIANGLE_COUNT: cnt = isLeaf() ? reinterpret_cast<const KDTLeafNode*>(this)->getNumTriangles() : 0; break;
53  case KDTREE_STAT_CHILDNODE_COUNT: cnt = getNumChildNodes(); break;
54  case KDTREE_STAT_EMPTYLEAF_COUNT: cnt = isLeaf() ? ((reinterpret_cast<const KDTLeafNode*>(this)->getNumTriangles() == 0) ? 1 : 0) : 0; break;
55  }
56 
57  if(!isLeaf())
58  {
59  for(int i=0;i<getNumChildNodes();i++)
60  cnt += getChildNode(i)->getSubtreeSize(stat);
61  }
62 
63  return cnt;
64 }
65 
66 
67 }
virtual S32 getNumChildNodes() const =0
Returns number of the node's child nodes.
KDTREE_STAT
Available statistics. Used in getSubtreeSize.
Definition: KDTreeNode.hpp:38
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
K-d tree's leaf node class.
Definition: KDTreeNode.hpp:134
#define FW_ASSERT(X)
Definition: Defs.hpp:67
void deleteSubtree()
Deletes node's subtree.
Definition: KDTreeNode.cpp:34
virtual KDTreeNode * getChildNode(S32 i) const =0
Returns node's child node (left or right).
virtual bool isLeaf() const =0
Returns whether the node is a leaf node.