Fermat
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Modules Pages
bvh3d.h
1 /*
2  * Copyright (c) 2010-2011, 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 <sandbox/linalg/vector.h>
35 #include <sandbox/linalg/bbox.h>
36 #include <vector>
37 #include <stack>
38 
39 namespace sandbox {
40 
41 struct Indexed_leaf_bvh_tag;
42 struct Compact_bvh_tag;
43 struct Compact_bvh_3d_tag;
44 
45 template <typename tag> struct Bvh_node {};
46 
50 template <>
51 struct Bvh_node<Indexed_leaf_bvh_tag>
52 {
53  typedef Indexed_leaf_bvh_tag Tag;
54 
55  typedef uint32 Type;
56  const static uint32 kLeaf = (1u << 31u);
57  const static uint32 kInternal = 0x00000000u;
58  const static uint32 kInvalid = uint32(-1);
59 
62  SANDBOX_HOST_DEVICE Bvh_node() {}
68  SANDBOX_HOST_DEVICE Bvh_node(const Type type, const uint32 index, const uint32 skip_node);
72  SANDBOX_HOST_DEVICE void set_type(const Type type);
76  SANDBOX_HOST_DEVICE void set_index(const uint32 index);
77 
80  SANDBOX_HOST_DEVICE bool is_leaf() const { return (m_packed_data & kLeaf) != 0u; }
83  SANDBOX_HOST_DEVICE uint32 get_index() const { return m_packed_data & (~kLeaf); }
86  SANDBOX_HOST_DEVICE uint32 get_leaf_index() const { return m_packed_data & (~kLeaf); }
87 
90  SANDBOX_HOST_DEVICE uint32 get_child_count() const { return 2u; }
94  SANDBOX_HOST_DEVICE uint32 get_child(const uint32 i) const { return get_index() + i; }
95 
100  static SANDBOX_HOST_DEVICE uint32 packed_data(const Type type, const uint32 index)
101  {
102  return (uint32(type) | index);
103  }
108  static SANDBOX_HOST_DEVICE void set_type(uint32& packed_data, const Type type)
109  {
110  packed_data &= ~kLeaf;
111  packed_data |= uint32(type);
112  }
117  static SANDBOX_HOST_DEVICE void set_index(uint32& packed_data, const uint32 index)
118  {
119  packed_data &= kLeaf;
120  packed_data |= index;
121  }
122  static SANDBOX_HOST_DEVICE bool is_leaf(const uint32 packed_data) { return (packed_data & kLeaf) != 0u; }
123  static SANDBOX_HOST_DEVICE uint32 get_index(const uint32 packed_data) { return packed_data & (~kLeaf); }
124 
125  uint32 m_packed_data; // child index
126 };
127 
131 template <>
132 struct Bvh_node<Compact_bvh_tag>
133 {
134  typedef Compact_bvh_tag Tag;
135  typedef uint32 Type;
136  const static uint32 kLeaf = (1u << 31u);
137  const static uint32 kInternal = 0x00000000u;
138  const static uint32 kInvalid = uint32(-1);
139 
142  SANDBOX_HOST_DEVICE Bvh_node() {}
148  SANDBOX_HOST_DEVICE Bvh_node(const Type type, const uint32 index, const uint32 range_size);
152  SANDBOX_HOST_DEVICE void set_type(const Type type);
156  SANDBOX_HOST_DEVICE void set_index(const uint32 index);
160  SANDBOX_HOST_DEVICE void set_range_size(const uint32 range_size) { m_data = range_size; }
161 
164  SANDBOX_HOST_DEVICE bool is_leaf() const { return (m_packed_data & kLeaf) != 0u; }
167  SANDBOX_HOST_DEVICE uint32 get_index() const { return m_packed_data & (~kLeaf); }
170  SANDBOX_HOST_DEVICE uint32 get_leaf_index() const { return m_packed_data & (~kLeaf); }
173  SANDBOX_HOST_DEVICE uint32 get_range_size() const { return m_data; }
174 
177  SANDBOX_HOST_DEVICE uint32 get_child_count() const { return 2u; }
181  SANDBOX_HOST_DEVICE uint32 get_child(const uint32 i) const { return get_index() + i; }
182 
187  static SANDBOX_HOST_DEVICE uint32 packed_data(const Type type, const uint32 index)
188  {
189  return (uint32(type) | index);
190  }
195  static SANDBOX_HOST_DEVICE void set_type(uint32& packed_data, const Type type)
196  {
197  packed_data &= ~kLeaf;
198  packed_data |= uint32(type);
199  }
204  static SANDBOX_HOST_DEVICE void set_index(uint32& packed_data, const uint32 index)
205  {
206  packed_data &= kLeaf;
207  packed_data |= index;
208  }
209  static SANDBOX_HOST_DEVICE bool is_leaf(const uint32 packed_data) { return (packed_data & kLeaf) != 0u; }
210  static SANDBOX_HOST_DEVICE uint32 get_index(const uint32 packed_data) { return packed_data & (~kLeaf); }
211 
212  uint32 m_packed_data; // child index
213  uint32 m_data; // additional data
214 };
215 
219 template <>
220 struct Bvh_node<Compact_bvh_3d_tag> : public Bvh_node<Compact_bvh_tag>
221 {
222  typedef Compact_bvh_3d_tag Tag;
223 
224  float3 m_bbox0; // first bbox bound
225  float3 m_bbox1; // second bbox bound
226 };
227 
231 
232 } // namespace sandbox
233 
234 #include <sandbox/bvh/bvh3d_inline.h>
SANDBOX_HOST_DEVICE Bvh_node()
Definition: bvh3d.h:142
SANDBOX_HOST_DEVICE Bvh_node()
Definition: bvh3d.h:62
SANDBOX_HOST_DEVICE bool is_leaf() const
Definition: bvh3d.h:164
Definition: bvh3d.h:39
SANDBOX_HOST_DEVICE void set_range_size(const uint32 range_size)
Definition: bvh3d.h:160
static SANDBOX_HOST_DEVICE void set_index(uint32 &packed_data, const uint32 index)
Definition: bvh3d.h:204
Definition: bvh3d.h:45
SANDBOX_HOST_DEVICE bool is_leaf() const
Definition: bvh3d.h:80
static SANDBOX_HOST_DEVICE void set_type(uint32 &packed_data, const Type type)
Definition: bvh3d.h:195
static SANDBOX_HOST_DEVICE void set_index(uint32 &packed_data, const uint32 index)
Definition: bvh3d.h:117
static SANDBOX_HOST_DEVICE void set_type(uint32 &packed_data, const Type type)
Definition: bvh3d.h:108