Fermat
bvh_sah_builder.h
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 
28 #pragma once
29 
30 #include <cugar/bvh/bvh.h>
31 #include <algorithm>
32 #include <stack>
33 #include <limits>
34 
35 namespace cugar {
36 
39 
44 {
45 public:
46  typedef Vector3f vector_type;
47  typedef Bbox3f bbox_type;
48  typedef Bvh<3u> bvh_type;
49  typedef Bvh_node bvh_node_type;
50 
51  struct Stats
52  {
53  uint32 m_max_depth;
54  };
55 
58  m_max_leaf_size( 4u ),
59  m_force_splitting( true ),
60  m_force_alignment( false ),
61  m_single_axis_threshold( 1024*1024*64 ) {}
62 
64  void set_max_leaf_size(const uint32 max_leaf_size) { m_max_leaf_size = max_leaf_size; }
65 
67  void set_force_splitting(const bool flag) { m_force_splitting = flag; }
68 
70  void set_force_alignment(const bool flag) { m_force_alignment = flag; }
71 
73  void set_single_axis_threshold(const uint32 v) { m_single_axis_threshold = v; }
74 
82  template <typename Iterator>
83  void build(
84  Iterator begin,
85  Iterator end,
86  bvh_type* bvh,
87  Stats* stats = NULL);
88 
96  template <typename Iterator, typename CostIterator>
97  void build(
98  Iterator begin,
99  Iterator end,
100  CostIterator cost_begin,
101  bvh_type* bvh,
102  Stats* stats = NULL);
103 
105  uint32 index(const uint32 i) const { return m_entities[m_indices[0][i]].m_index; }
106 
107 private:
108  void build(
109  const uint32 n_entities,
110  bvh_type* bvh,
111  Stats* stats);
112 
113  class Predicate;
114  class IndexSortPredicate;
115 
116  struct Entity
117  {
118  bbox_type m_bbox;
119  uint32 m_index;
120  float m_cost;
121  };
122 
123  struct Node
124  {
125  bbox_type m_bbox;
126  uint32 m_begin;
127  uint32 m_end;
128  uint32 m_node;
129  uint32 m_depth;
130  };
131  typedef std::stack<Node> Node_stack;
132 
133  float area(const Bbox3f& bbox);
134 
135  void compute_bbox(
136  const uint32 begin,
137  const uint32 end,
138  bbox_type& bbox);
139 
140  bool find_largest_axis_split(
141  const Node& node,
142  uint32& pivot,
143  bbox_type& l_bb,
144  bbox_type& r_bb);
145 
146  bool find_best_split(
147  const Node& node,
148  uint32& pivot,
149  bbox_type& l_bb,
150  bbox_type& r_bb);
151 
152  struct Bvh_partitioner;
153 
154  uint32 m_max_leaf_size;
155  bool m_force_splitting;
156  bool m_force_alignment;
157  uint32 m_single_axis_threshold;
158  std::vector<int> m_indices[3]; // indices into entities, one vector per dimension
159  std::vector<Entity> m_entities;
160  std::vector<bbox_type> m_bboxes;
161  std::vector<float> m_costs;
162  std::vector<int> m_tmpInt; // temp array used during build
163  std::vector<uint8> m_tag_bits;
164 };
165 
167 
168 namespace deprecated {
169 
174 {
175 public:
176  typedef Vector3f vector_type;
177  typedef Bbox3f bbox_type;
178  typedef Bvh<3u> bvh_type;
179  typedef Bvh_node bvh_node_type;
180 
181  struct Stats
182  {
183  uint32 m_max_depth;
184  };
185 
188  m_max_leaf_size( 4u ),
189  m_force_splitting( true ),
190  m_force_alignment( false ),
191  m_partial_build( false ),
192  m_single_axis_threshold( 1024*1024*64 ) {}
193 
195  void set_max_leaf_size(const uint32 max_leaf_size) { m_max_leaf_size = max_leaf_size; }
196 
198  void set_force_splitting(const bool flag) { m_force_splitting = flag; }
199 
201  void set_force_alignment(const bool flag) { m_force_alignment = flag; }
202 
204  void set_partial_build(const bool flag) { m_partial_build = flag; }
205 
207  void set_single_axis_threshold(const uint32 v) { m_single_axis_threshold = v; }
208 
216  template <typename Iterator>
217  void build(
218  Iterator begin,
219  Iterator end,
220  Bvh_type* bvh);
221 
223  uint32 index(const uint32 i) const { return m_entities[i].m_index; }
224 
225 private:
226  class Predicate;
227  class IndexSortPredicate;
228 
229  struct Entity
230  {
231  bbox_type m_bbox;
232  uint32 m_index;
233  };
234 
235  struct Node
236  {
237  bbox_type m_bbox;
238  uint32 m_begin;
239  uint32 m_end;
240  uint32 m_node;
241  uint32 m_depth;
242  };
243  typedef std::stack<Node> Node_stack;
244 
245  float area(const Bbox3f& bbox);
246 
247  void compute_bbox(
248  const uint32 begin,
249  const uint32 end,
250  bbox_type& bbox);
251 
252  bool find_best_split(
253  const Node& node,
254  uint32& pivot,
255  bbox_type& l_bb,
256  bbox_type& r_bb);
257 
258  struct Bvh_partitioner;
259 
260  uint32 m_max_leaf_size;
261  bool m_force_splitting;
262  bool m_force_alignment;
263  bool m_partial_build;
264  uint32 m_single_axis_threshold;
265  std::vector<Entity> m_entities;
266  std::vector<bbox_type> m_bboxes;
267 };
268 
269 } // namespace deprecated
270 
271 } // namespace cugar
272 
273 #include <cugar/bvh/bvh_sah_builder_inline.h>
Entry point to the generic Bounding Volume Hierarchy library.
void set_single_axis_threshold(const uint32 v)
set single axis test threshold
Definition: bvh_sah_builder.h:207
void set_force_alignment(const bool flag)
set force &#39;max leaf size&#39;-aligned splits
Definition: bvh_sah_builder.h:70
thrust::device_vector< T >::iterator begin(thrust::device_vector< T > &vec)
Definition: thrust_view.h:89
Definition: bvh_node.h:45
void set_force_splitting(const bool flag)
set force splitting
Definition: bvh_sah_builder.h:198
void set_partial_build(const bool flag)
set partial build
Definition: bvh_sah_builder.h:204
Definition: bvh_sah_builder.h:43
Bvh_sah_builder()
constructor
Definition: bvh_sah_builder.h:187
Definition: bvh.h:84
Definition: bvh_sah_builder.h:173
void set_force_alignment(const bool flag)
set force &#39;max leaf size&#39;-aligned splits
Definition: bvh_sah_builder.h:201
Definition: bbox.h:59
void set_max_leaf_size(const uint32 max_leaf_size)
set bvh parameters
Definition: bvh_sah_builder.h:64
Define a vector_view POD type and plain_view() for std::vector.
Definition: diff.h:38
void set_single_axis_threshold(const uint32 v)
set single axis test threshold
Definition: bvh_sah_builder.h:73
Definition: bvh_sah_builder.h:51
Definition: bvh_sah_builder.h:181
void build(Iterator begin, Iterator end, bvh_type *bvh, Stats *stats=NULL)
Definition: bvh_sah_builder_inline.h:187
CUGAR_HOST_DEVICE float area(const Bbox2f &bbox)
Definition: bbox_inline.h:134
uint32 index(const uint32 i) const
remapped point index
Definition: bvh_sah_builder.h:223
void set_max_leaf_size(const uint32 max_leaf_size)
set bvh parameters
Definition: bvh_sah_builder.h:195
Bvh_sah_builder()
constructor
Definition: bvh_sah_builder.h:57
Definition: bvh_sah_builder_inline.h:35
Definition: bvh_sah_builder_inline.h:240
Definition: bvh_sah_builder_inline.h:597
uint32 index(const uint32 i) const
remapped point index
Definition: bvh_sah_builder.h:105
void set_force_splitting(const bool flag)
set force splitting
Definition: bvh_sah_builder.h:67