Fermat
bintree_writer.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 
33 #pragma once
34 
35 #include <cugar/basic/types.h>
36 #include <cugar/basic/vector.h>
38 #include <thrust/device_vector.h>
39 
40 namespace cugar {
41 
44 
47 template <typename node_type, typename node_tag> // leaf_index_tag | leaf_range_tag
49 
52 template <typename node_type>
54 {
57  CUGAR_HOST_DEVICE Bintree_writer_context() {}
60  CUGAR_HOST_DEVICE Bintree_writer_context(
61  node_type* nodes,
62  uint2* leaf_ranges = NULL,
63  uint32* leaf_pointers = NULL,
64  uint32* parents = NULL,
65  uint32* skip_nodes = NULL,
66  uint2* node_ranges = NULL) :
67  m_nodes(nodes), m_leaf_ranges(leaf_ranges), m_leaf_pointers(leaf_pointers), m_parents(parents), m_skip_nodes(skip_nodes), m_node_ranges(node_ranges) {}
68 
71  CUGAR_HOST_DEVICE void write_node(const uint32 node, const uint32 parent, bool p1, bool p2, const uint32 offset, const uint32 skip_node, const uint32 level, const uint32 begin, const uint32 end, const uint32 split_index)
72  {
73  m_nodes[ node ] = Bintree_node<leaf_index_tag>( p1, p2, offset );
74 
75  if (m_parents)
76  m_parents[ node ] = parent;
77 
78  if (m_skip_nodes)
79  m_skip_nodes[ node ] = skip_node;
80 
81  if (m_node_ranges)
82  m_node_ranges[ node ] = make_uint2( begin, end );
83  }
86  CUGAR_HOST_DEVICE void write_leaf(const uint32 leaf_index, const uint32 node_index, const uint32 begin, const uint32 end)
87  {
88  if (m_node_ranges)
89  m_node_ranges[ node_index ] = make_uint2( begin, end );
90 
91  if (m_leaf_ranges)
92  m_leaf_ranges[ leaf_index ] = make_uint2( begin, end );
93 
94  if (m_leaf_pointers)
95  m_leaf_pointers[ leaf_index ] = node_index;
96  }
97 
98  node_type* m_nodes;
99  uint2* m_leaf_ranges;
101  uint32* m_parents;
102  uint32* m_skip_nodes;
103  uint2* m_node_ranges;
104 };
105 
108 template <typename node_type>
110 {
113  CUGAR_HOST_DEVICE Bintree_writer_context() {}
116  CUGAR_HOST_DEVICE Bintree_writer_context(
117  node_type* nodes,
118  uint2* leaf_ranges = NULL,
119  uint32* leaf_pointers = NULL,
120  uint32* parents = NULL,
121  uint32* skip_nodes = NULL,
122  uint2* node_ranges = NULL) :
123  m_nodes(nodes), m_leaf_ranges(leaf_ranges), m_leaf_pointers(leaf_pointers), m_parents(parents), m_skip_nodes(skip_nodes), m_node_ranges(node_ranges) {}
124 
125 
128  CUGAR_HOST_DEVICE void write_node(const uint32 node, const uint32 parent, bool p1, bool p2, const uint32 offset, const uint32 skip_node, const uint32 level, const uint32 begin, const uint32 end, const uint32 split_index)
129  {
130  if (p1 || p2)
131  m_nodes[ node ] = Bintree_node<leaf_range_tag>( p1, p2, offset, end - begin );
132  else
133  m_nodes[ node ] = Bintree_node<leaf_range_tag>( begin, end );
134 
135  if (m_parents)
136  m_parents[ node ] = parent;
137 
138  if (m_skip_nodes)
139  m_skip_nodes[ node ] = skip_node;
140 
141  if (m_node_ranges)
142  m_node_ranges[ node ] = make_uint2( begin, end );
143  }
146  CUGAR_HOST_DEVICE void write_leaf(const uint32 leaf_index, const uint32 node_index, const uint32 begin, const uint32 end)
147  {
148  if (m_leaf_ranges)
149  m_leaf_ranges[ leaf_index ] = make_uint2( begin, end );
150 
151  if (m_leaf_pointers)
152  m_leaf_pointers[ leaf_index ] = node_index;
153 
154  if (m_node_ranges)
155  m_node_ranges[ node_index ] = make_uint2( begin, end );
156  }
157 
158  node_type* m_nodes;
159  uint2* m_leaf_ranges;
161  uint32* m_parents;
162  uint32* m_skip_nodes;
163  uint2* m_node_ranges;
164 };
165 
171 template <
172  typename node_type,
173  typename system_tag,
174  typename node_vector = vector<system_tag,node_type>,
175  typename range_vector = vector<system_tag,uint2>,
176  typename index_vector = vector<system_tag,uint32> > // leaf_index_tag | leaf_range_tag
178 {
179  typedef typename node_type::node_tag node_tag;
181 
187  node_vector* nodes = NULL,
188  range_vector* leaf_ranges = NULL,
189  index_vector* leaf_pointers = NULL,
190  index_vector* parents = NULL,
191  index_vector* skip_nodes = NULL,
192  range_vector* node_ranges = NULL) :
193  m_nodes( nodes ), m_leaf_ranges( m_leaf_ranges ), m_leaf_pointers( NULL ), m_parents( parents ), m_skip_nodes( skip_nodes ), m_node_ranges(node_ranges) {}
194 
195  void set_nodes(node_vector* nodes) { m_nodes = nodes; }
196  void set_parents(index_vector* parents) { m_parents = parents; }
197  void set_skip_nodes(index_vector* skip_nodes) { m_skip_nodes = skip_nodes; }
198  void set_leaf_pointers(index_vector* leaf_pointers) { m_leaf_pointers = leaf_pointers; }
199  void set_leaf_ranges(range_vector* leaf_ranges) { m_leaf_ranges = leaf_ranges; }
200  void set_node_ranges(range_vector* node_ranges) { m_node_ranges = node_ranges; }
201 
205  void reserve_nodes(const uint32 n)
206  {
207  if (m_nodes->size() < n) m_nodes->resize(n);
208  if (m_parents && m_parents->size() < n) m_parents->resize(n);
209  if (m_skip_nodes && m_skip_nodes->size() < n) m_skip_nodes->resize(n);
210  if (m_node_ranges && m_node_ranges->size() < n) m_node_ranges->resize(n);
211  }
212 
216  void reserve_leaves(const uint32 n)
217  {
218  if (m_leaf_ranges && m_leaf_ranges->size() < n) m_leaf_ranges->resize(n);
219  if (m_leaf_pointers && m_leaf_pointers->size() < n) m_leaf_pointers->resize(n);
220  }
221 
224  context_type get_context()
225  {
226  return context_type(
227  m_nodes ? raw_pointer( *m_nodes ) : NULL,
228  m_leaf_ranges ? raw_pointer( *m_leaf_ranges ) : NULL,
229  m_leaf_pointers ? raw_pointer( *m_leaf_pointers ) : NULL,
230  m_parents ? raw_pointer( *m_parents ) : NULL,
231  m_skip_nodes ? raw_pointer( *m_skip_nodes ) : NULL,
232  m_node_ranges ? raw_pointer( *m_node_ranges ) : NULL );
233  }
234 
235  node_vector* m_nodes;
236  range_vector* m_leaf_ranges;
237  index_vector* m_leaf_pointers;
238  index_vector* m_parents;
239  index_vector* m_skip_nodes;
240  range_vector* m_node_ranges;
241 };
242 
244 
245 } // namespace cugar
uint32 * m_leaf_pointers
leaf pointers
Definition: bintree_writer.h:100
uint32 * m_parents
parent pointers
Definition: bintree_writer.h:161
uint2 * m_leaf_ranges
leaf ranges
Definition: bintree_writer.h:159
node_type * m_nodes
node pointer
Definition: bintree_writer.h:158
thrust::device_vector< T >::iterator begin(thrust::device_vector< T > &vec)
Definition: thrust_view.h:89
Definition: bintree_node.h:56
CUGAR_HOST_DEVICE void write_node(const uint32 node, const uint32 parent, bool p1, bool p2, const uint32 offset, const uint32 skip_node, const uint32 level, const uint32 begin, const uint32 end, const uint32 split_index)
Definition: bintree_writer.h:128
CUGAR_HOST_DEVICE void write_node(const uint32 node, const uint32 parent, bool p1, bool p2, const uint32 offset, const uint32 skip_node, const uint32 level, const uint32 begin, const uint32 end, const uint32 split_index)
Definition: bintree_writer.h:71
uint32 * m_skip_nodes
skip nodes pointer
Definition: bintree_writer.h:162
T * raw_pointer(thrust::device_vector< T, Alloc > &vec)
Definition: thrust_view.h:69
uint2 * m_node_ranges
node ranges
Definition: bintree_writer.h:103
node_type * m_nodes
node pointer
Definition: bintree_writer.h:98
Definition: bintree_writer.h:48
uint32 * m_skip_nodes
skip nodes pointer
Definition: bintree_writer.h:102
Define CUDA based scan primitives.
void reserve_leaves(const uint32 n)
Definition: bintree_writer.h:216
void reserve_nodes(const uint32 n)
Definition: bintree_writer.h:205
Definition: bintree_writer.h:177
uint32 * m_leaf_pointers
leaf pointers
Definition: bintree_writer.h:160
CUGAR_HOST_DEVICE void write_leaf(const uint32 leaf_index, const uint32 node_index, const uint32 begin, const uint32 end)
Definition: bintree_writer.h:86
CUGAR_HOST_DEVICE Bintree_writer_context(node_type *nodes, uint2 *leaf_ranges=NULL, uint32 *leaf_pointers=NULL, uint32 *parents=NULL, uint32 *skip_nodes=NULL, uint2 *node_ranges=NULL)
Definition: bintree_writer.h:60
CUGAR_HOST_DEVICE void write_leaf(const uint32 leaf_index, const uint32 node_index, const uint32 begin, const uint32 end)
Definition: bintree_writer.h:146
Definition: vector.h:117
uint32 * m_parents
parent pointers
Definition: bintree_writer.h:101
Define a vector_view POD type and plain_view() for std::vector.
Definition: diff.h:38
Definition: bintree_node.h:60
context_type get_context()
Definition: bintree_writer.h:224
uint2 * m_node_ranges
node ranges
Definition: bintree_writer.h:163
CUGAR_HOST_DEVICE Bintree_writer_context()
Definition: bintree_writer.h:113
Definition: bintree_node.h:73
CUGAR_HOST_DEVICE Bintree_writer_context(node_type *nodes, uint2 *leaf_ranges=NULL, uint32 *leaf_pointers=NULL, uint32 *parents=NULL, uint32 *skip_nodes=NULL, uint2 *node_ranges=NULL)
Definition: bintree_writer.h:116
Bintree_writer(node_vector *nodes=NULL, range_vector *leaf_ranges=NULL, index_vector *leaf_pointers=NULL, index_vector *parents=NULL, index_vector *skip_nodes=NULL, range_vector *node_ranges=NULL)
Definition: bintree_writer.h:186
uint2 * m_leaf_ranges
leaf ranges
Definition: bintree_writer.h:99
CUGAR_HOST_DEVICE Bintree_writer_context()
Definition: bintree_writer.h:57