Fermat
kd_node.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2010-2016, NVIDIA Corporation
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 NVIDIA Corporation 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 
32 #pragma once
33 
34 #include <cugar/basic/types.h>
35 
36 namespace cugar {
37 
98 
103 struct Kd_node
111 {
112  static const uint32 kInvalid = uint32(-1);
113 
116  CUGAR_HOST_DEVICE Kd_node() {}
117 
121  CUGAR_HOST_DEVICE Kd_node(uint32 index) :
122  m_packed_info( 7u | (index << 3) ) {}
123 
129  CUGAR_HOST_DEVICE Kd_node(const uint32 split_dim, const float split_plane, uint32 index) :
130  m_packed_info( split_dim | (index << 3) ),
131  m_split_plane( split_plane ) {}
132 
136  CUGAR_HOST_DEVICE Kd_node(const uint32 packed_info, const float split_plane) :
137  m_packed_info( packed_info ),
138  m_split_plane(split_plane) {}
139 
142  CUGAR_HOST_DEVICE uint32 is_leaf() const
143  {
144  return (m_packed_info & 7u) == 7u;
145  }
148  CUGAR_HOST_DEVICE uint32 get_child_offset() const
149  {
150  return m_packed_info >> 3u;
151  }
154  CUGAR_HOST_DEVICE uint32 get_leaf_index() const
155  {
156  return m_packed_info >> 3u;
157  }
161  CUGAR_HOST_DEVICE uint32 get_child(const uint32 i) const
162  {
163  return get_child_offset() + i;
164  }
168  CUGAR_HOST_DEVICE bool has_child(const uint32 i) const
169  {
170  return is_leaf() ? false : true;
171  }
174  CUGAR_HOST_DEVICE uint32 get_left() const
175  {
176  return get_child_offset();
177  }
180  CUGAR_HOST_DEVICE uint32 get_right() const
181  {
182  return get_child_offset() + 1u;
183  }
184 
187  CUGAR_HOST_DEVICE uint32 get_split_dim() const { return (m_packed_info & 7u); }
188 
191  CUGAR_HOST_DEVICE float get_split_plane() const { return m_split_plane; }
192 
193  CUGAR_HOST_DEVICE
194  static Kd_node load(const Kd_node* node)
195  {
196  #if defined(CUGAR_DEVICE_COMPILATION)
197  const uint2 u = *reinterpret_cast<const uint2*>(node);
198  return Kd_node(u.x,__uint_as_float(u.y));
199  #else
200  return *node;
201  #endif
202  }
203 
204  CUGAR_HOST_DEVICE
205  static Kd_node load_ldg(const Kd_node* node)
206  {
207  #if defined(CUGAR_DEVICE_COMPILATION)
208  const uint2 u = __ldg(reinterpret_cast<const uint2*>(node));
209  return Kd_node(u.x,__uint_as_float(u.y));
210  #else
211  return *node;
212  #endif
213  }
214 
215  uint32 m_packed_info;
216  float m_split_plane;
217 };
218 
222 } // namespace cugar
CUGAR_HOST_DEVICE uint32 get_child(const uint32 i) const
Definition: kd_node.h:161
CUGAR_HOST_DEVICE uint32 is_leaf() const
Definition: kd_node.h:142
Definition: kd_node.h:110
CUGAR_HOST_DEVICE bool has_child(const uint32 i) const
Definition: kd_node.h:168
CUGAR_HOST_DEVICE Kd_node(uint32 index)
Definition: kd_node.h:121
CUGAR_HOST_DEVICE uint32 get_split_dim() const
Definition: kd_node.h:187
CUGAR_HOST_DEVICE Kd_node()
Definition: kd_node.h:116
Define a vector_view POD type and plain_view() for std::vector.
Definition: diff.h:38
CUGAR_HOST_DEVICE uint32 get_leaf_index() const
Definition: kd_node.h:154
CUGAR_HOST_DEVICE float get_split_plane() const
Definition: kd_node.h:191
CUGAR_HOST_DEVICE uint32 get_right() const
Definition: kd_node.h:180
CUGAR_HOST_DEVICE Kd_node(const uint32 split_dim, const float split_plane, uint32 index)
Definition: kd_node.h:129
CUGAR_HOST_DEVICE Kd_node(const uint32 packed_info, const float split_plane)
Definition: kd_node.h:136
CUGAR_HOST_DEVICE uint32 get_left() const
Definition: kd_node.h:174
CUGAR_HOST_DEVICE uint32 get_child_offset() const
Definition: kd_node.h:148