35 m_split_dim( split_dim ),
36 m_split_plane( split_plane )
50 return uint32( std::max_element( &v[0], &v[0] + DIM ) - &v[0] );
55 std::stack< std::pair<uint32,uint32> > node_stack;
57 node_stack.push( std::make_pair(0u,Bvh_node::kInvalid) );
59 while (!node_stack.empty())
61 const uint32 node_id = node_stack.top().first;
62 const uint32 skip_node = node_stack.top().second;
65 skip_nodes[node_id] = skip_node;
67 if (!nodes[node_id].is_leaf())
69 node_stack.push( std::make_pair(nodes[node_id].get_child(0u), nodes[node_id].get_child(1u)) );
70 node_stack.push( std::make_pair(nodes[node_id].get_child(1u), skip_node) );
76 template <
typename Iterator>
82 m_points.resize( end - begin );
85 for (Iterator it = begin; it != end; ++it, ++i)
87 m_points[i].m_bbox = *it;
88 m_points[i].m_index = i;
93 node.m_end = uint32(end - begin);
97 Node_stack node_stack;
98 node_stack.push( node );
100 Bvh_node root( Bvh_node::kLeaf, node.m_begin, node.m_end );
101 bvh->m_nodes.erase( bvh->m_nodes.begin(), bvh->m_nodes.end() );
102 bvh->m_nodes.push_back( root );
103 bvh->m_bboxes.resize( 1u );
105 while (node_stack.empty() ==
false)
107 const Node node = node_stack.top();
110 const uint32 node_index = node.m_node;
113 compute_bbox( node.m_begin, node.m_end, bbox );
115 bvh->m_bboxes[ node_index ] = bbox;
117 if (node.m_end - node.m_begin < m_max_leaf_size)
122 Bvh_node& bvh_node = bvh->m_nodes[ node_index ];
123 bvh_node =
Bvh_node( Bvh_node::kLeaf, node.m_begin, node.m_end );
132 const uint32 left_node_index = uint32( bvh->m_nodes.size() );
133 bvh->m_nodes.resize( left_node_index + 2 );
134 bvh->m_bboxes.resize( left_node_index + 2 );
136 Bvh_node& bvh_node = bvh->m_nodes[ node_index ];
137 bvh_node =
Bvh_node( Bvh_node::kInternal, left_node_index, node.m_end - node.m_begin );
140 const uint32 split_dim = largest_dim( bbox[1] - bbox[0] );
141 const float split_plane = (bbox[1][split_dim] + bbox[0][split_dim]) * 0.5f;
145 uint32 middle = uint32(
146 std::partition( &m_points[0] + node.m_begin, &m_points[0] + node.m_end, partitioner ) -
150 if (middle == node.m_begin ||
151 middle == node.m_end)
152 middle = (node.m_begin + node.m_end) / 2;
156 right_node.m_begin = middle;
157 right_node.m_end = node.m_end;
158 right_node.m_depth = node.m_depth + 1u;
159 right_node.m_node = left_node_index + 1u;
160 node_stack.push( right_node );
163 left_node.m_begin = node.m_begin;
164 left_node.m_end = middle;
165 left_node.m_depth = node.m_depth + 1u;
166 left_node.m_node = left_node_index;
167 node_stack.push( left_node );
172 template <u
int32 DIM>
179 for (uint32 i = begin; i < end; i++)
180 bbox.
insert( m_points[i].m_bbox );
186 const Bvh_node node = bvh.m_nodes[ node_index ];
190 return float( node.get_leaf_size() );
197 const Bbox3f bbox1 = bvh.m_bboxes[ node.get_child(0) ];
198 const Bbox3f bbox2 = bvh.m_bboxes[ node.get_child(1) ];
200 const Bbox3f bbox = bvh.m_bboxes[ node_index ];
202 return 2.0f + (
area( bbox1 ) * cost1 +
area( bbox2 ) * cost2) / std::max(
area( bbox ), 1.0e-6f );
CUGAR_HOST CUGAR_DEVICE void clear()
Definition: bbox_inline.h:85
thrust::device_vector< T >::iterator begin(thrust::device_vector< T > &vec)
Definition: thrust_view.h:89
Definition: bvh_node.h:45
void build_skip_nodes(const Bvh_node *nodes, uint32 *skip_nodes)
build skip nodes for a tree
Definition: bvh_inline.h:53
Define a vector_view POD type and plain_view() for std::vector.
Definition: diff.h:38
Definition: bvh_inline.h:31
void build(const Iterator begin, const Iterator end, Bvh< DIM > *bvh)
Definition: bvh_inline.h:77
CUGAR_HOST_DEVICE float area(const Bbox2f &bbox)
Definition: bbox_inline.h:134
CUGAR_HOST CUGAR_DEVICE void insert(const Vector_t &v)
Definition: bbox_inline.h:65
float compute_sah_cost(const Bvh< 3 > &bvh, uint32 node_index=0)
compute SAH cost of a subtree
Definition: bvh_inline.h:184