Fermat
bintree_node.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2010-2018, 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 
48 
53 
56 struct leaf_index_tag {};
57 
60 struct leaf_range_tag {};
61 
62 template <typename leaf_type_tag>
63 struct Bintree_node {};
64 
72 template <>
74 {
75  typedef leaf_index_tag node_tag;
76 
77  static const uint32 kInvalid = uint32(-1);
78 
81  CUGAR_HOST_DEVICE Bintree_node() {}
82 
88  CUGAR_HOST_DEVICE Bintree_node(bool child0, bool child1, uint32 index) :
89  m_packed_info( (child0 ? 1u : 0u) | (child1 ? 2u : 0u) | (index << 2) ) {}
90 
93  CUGAR_HOST_DEVICE uint32 is_leaf() const
94  {
95  return (m_packed_info & 3u) == 0u;
96  }
99  CUGAR_HOST_DEVICE uint32 get_child_index() const
100  {
101  return m_packed_info >> 2u;
102  }
105  CUGAR_HOST_DEVICE uint32 get_leaf_index() const
106  {
107  return m_packed_info >> 2u;
108  }
111  CUGAR_HOST_DEVICE uint32 get_child_count() const
112  {
113  return ((m_packed_info & 1u) ? 1u : 0u) +
114  ((m_packed_info & 2u) ? 1u : 0u);
115  }
119  CUGAR_HOST_DEVICE uint32 get_child(const uint32 i) const
120  {
121  return get_child_index() + i;
122  }
126  CUGAR_HOST_DEVICE bool has_child(const uint32 i) const
127  {
128  return m_packed_info & (1u << i) ? true : false;
129  }
132  CUGAR_HOST_DEVICE uint32 get_left() const
133  {
134  return has_child(0) ? get_child_index() : kInvalid;
135  }
138  CUGAR_HOST_DEVICE uint32 get_right() const
139  {
140  return has_child(1) ? get_child_index() + (has_child(0) ? 1u : 0u) : kInvalid;
141  }
142 
143  uint32 m_packed_info;
144 };
145 
154 template <>
156 {
157  typedef leaf_range_tag node_tag;
158 
159  static const uint32 kInvalid = uint32(-1);
160 
163  CUGAR_HOST_DEVICE Bintree_node() {}
164 
170  CUGAR_HOST_DEVICE Bintree_node(bool child0, bool child1, uint32 index, const uint32 range_size = 0) :
171  m_packed_info( (child0 ? 1u : 0u) | (child1 ? 2u : 0u) | (index << 2) ), m_range_size(range_size) {}
172 
177  CUGAR_HOST_DEVICE Bintree_node(const uint32 leaf_begin, const uint32 leaf_end) :
178  m_packed_info( (leaf_begin << 2) ), m_range_size( leaf_end - leaf_begin ) {}
179 
182  CUGAR_HOST_DEVICE uint32 is_leaf() const
183  {
184  return (m_packed_info & 3u) == 0u;
185  }
188  CUGAR_HOST_DEVICE uint32 get_child_index() const
189  {
190  return m_packed_info >> 2u;
191  }
194  CUGAR_HOST_DEVICE uint32 get_leaf_begin() const
195  {
196  return m_packed_info >> 2u;
197  }
200  CUGAR_HOST_DEVICE uint32 get_range_size() const
201  {
202  return m_range_size;
203  }
206  CUGAR_HOST_DEVICE uint2 get_leaf_range() const
207  {
208  const uint32 leaf_begin = m_packed_info >> 2u;
209  return make_uint2( leaf_begin, leaf_begin + m_range_size );
210  }
213  CUGAR_HOST_DEVICE uint32 get_leaf_size() const
214  {
215  return m_range_size;
216  }
219  CUGAR_HOST_DEVICE uint32 get_child_count() const
220  {
221  return ((m_packed_info & 1u) ? 1u : 0u) +
222  ((m_packed_info & 2u) ? 1u : 0u);
223  }
227  CUGAR_HOST_DEVICE uint32 get_child(const uint32 i) const
228  {
229  return get_child_index() + i;
230  }
234  CUGAR_HOST_DEVICE bool has_child(const uint32 i) const
235  {
236  return m_packed_info & (1u << i) ? true : false;
237  }
240  CUGAR_HOST_DEVICE uint32 get_left() const
241  {
242  return has_child(0) ? get_child_index() : kInvalid;
243  }
246  CUGAR_HOST_DEVICE uint32 get_right() const
247  {
248  return has_child(1) ? get_child_index() + (has_child(0) ? 1u : 0u) : kInvalid;
249  }
250 
251  uint32 m_packed_info;
252  uint32 m_range_size;
253 };
254 CUGAR_ALIGN_END(8)
255 
256 
258 } // namespace cugar
CUGAR_HOST_DEVICE uint32 get_child_index() const
Definition: bintree_node.h:99
CUGAR_HOST_DEVICE uint32 get_child_count() const
Definition: bintree_node.h:111
Definition: bintree_node.h:56
CUGAR_HOST_DEVICE Bintree_node()
Definition: bintree_node.h:81
CUGAR_HOST_DEVICE Bintree_node(bool child0, bool child1, uint32 index)
Definition: bintree_node.h:88
CUGAR_HOST_DEVICE uint32 get_leaf_index() const
Definition: bintree_node.h:105
CUGAR_HOST_DEVICE uint32 get_left() const
Definition: bintree_node.h:132
CUGAR_HOST_DEVICE bool has_child(const uint32 i) const
Definition: bintree_node.h:126
Define a vector_view POD type and plain_view() for std::vector.
Definition: diff.h:38
Definition: bintree_node.h:60
CUGAR_HOST_DEVICE uint32 is_leaf() const
Definition: bintree_node.h:93
struct CUGAR_ALIGN_BEGIN(8) Bintree_node< leaf_range_tag >
Definition: bintree_node.h:155
Definition: bintree_node.h:63
CUGAR_HOST_DEVICE uint32 get_right() const
Definition: bintree_node.h:138
CUGAR_HOST_DEVICE uint32 get_child(const uint32 i) const
Definition: bintree_node.h:119