68 for(
unsigned int i=0; i< vertices.
dim; i++)
70 pool->
push(generateAndSort<float, unsigned int>, vertices, indices_sorted, values_sorted, i);
81 for(
unsigned int i=0; i<vertices.
dim;i++)
83 free(values_sorted[i].elements);
102 int first_split_dim = -1;
103 float best_deviation = -1.0;
105 for(
int i=0; i<V.
dim; i++)
107 float deviation = V.
elements[
static_cast<unsigned int>(
109 - V.
elements[
static_cast<unsigned int>(
112 if(deviation > best_deviation)
114 best_deviation = deviation;
123 max_tree_depth =
static_cast<int>( log2f(V.
width - 1 ) + 2.0 ) ;
130 size = V.
width * 2 - 1;
133 this->
m_values->elements = (
float*)malloc(
sizeof(
float) * size );
137 unsigned int size_splits = size - V.
width;
139 this->
m_splits->elements = (
unsigned char*)malloc(
sizeof(
unsigned char) * size_splits );
140 this->
m_splits->width = size_splits;
147 max_dim, value_ptr, splits_ptr ,size, max_tree_depth, 0, 0);
156 float split_value,
unsigned int split_index,
157 std::list<unsigned int>& critical_indices_left,
158 std::list<unsigned int>& critical_indices_right)
160 critical_indices_left.push_back( sorted_indices.
elements[split_index] );
162 unsigned int iterator;
164 for(iterator = split_index-1;
165 iterator < sorted_indices.
width 169 critical_indices_left.push_back( sorted_indices.
elements[iterator] );
173 for(iterator = split_index+1;
174 iterator < sorted_indices.
width 178 critical_indices_right.push_back( sorted_indices.
elements[iterator] );
185 float split_value,
unsigned int split_index,
186 std::unordered_set<unsigned int>& critical_indices_left,
187 std::unordered_set<unsigned int>& critical_indices_right)
190 critical_indices_left.insert(sorted_indices.
elements[split_index]);
192 unsigned int iterator;
194 for(iterator = split_index-1;
195 iterator < sorted_indices.
width 199 critical_indices_left.insert( sorted_indices.
elements[iterator] );
203 for(iterator = split_index+1;
204 iterator < sorted_indices.
width 208 critical_indices_right.insert( sorted_indices.
elements[iterator] );
216 int size,
int max_tree_depth,
int position,
int current_depth)
218 int left = position*2+1;
219 int right = position*2+2;
222 if( sorted_indices[current_dim].width <= 1 )
224 values->
elements[position] =
static_cast<float>(sorted_indices[current_dim].
elements[0] );
227 unsigned int indices_size = sorted_indices[current_dim].
width;
229 unsigned int v = pow( 2, static_cast<int>( log2(indices_size-1) ) );
230 unsigned int left_size = indices_size - v/2;
237 unsigned int right_size = indices_size - left_size;
241 unsigned int split_index =
static_cast<unsigned int>(
242 sorted_indices[current_dim].
elements[left_size-1] + 0.5
245 float split_value = V.
elements[split_index * V.
dim + current_dim ];
248 std::unordered_set<unsigned int> critical_indices_left;
249 std::unordered_set<unsigned int> critical_indices_right;
252 current_dim, split_value, left_size-1,
253 critical_indices_left, critical_indices_right);
256 values->
elements[ position ] = split_value;
257 splits->
elements[ position ] =
static_cast<unsigned char>(current_dim);
265 int next_dim_left = -1;
266 int next_dim_right = -1;
267 float biggest_deviation_left = -1.0;
268 float biggest_deviation_right = -1.0;
270 for(
int i=0; i<max_dim; i++ )
273 sorted_indices_left[i].
width = left_size;
274 sorted_indices_left[i].
dim = 1;
275 sorted_indices_left[i].
elements = (
unsigned int*)malloc( left_size *
sizeof(
unsigned int) );
277 sorted_indices_right[i].
width = right_size;
278 sorted_indices_right[i].
dim = 1;
279 sorted_indices_right[i].
elements = (
unsigned int*)malloc( right_size *
sizeof(
unsigned int) );
281 float deviation_left;
282 float deviation_right;
285 if( i == current_dim ){
286 splitPointArray<unsigned int>( sorted_indices[i],
287 sorted_indices_left[i],
288 sorted_indices_right[i]);
291 deviation_left = fabs(V.
elements[sorted_indices_left[i].elements[left_size - 1] * V.
dim + i ]
292 - V.
elements[sorted_indices_left[i].elements[0] * V.
dim + i ] );
293 deviation_right = fabs( V.
elements[ sorted_indices_right[i].
elements[right_size - 1] * V.
dim + i ]
301 splitPointArrayWithValueSet<float, unsigned int>(V, sorted_indices[i],
302 sorted_indices_left[i], sorted_indices_right[i],
303 current_dim, split_value,
304 deviation_left, deviation_right, i,
305 critical_indices_left, critical_indices_right);
309 if(deviation_left > biggest_deviation_left )
311 biggest_deviation_left = deviation_left;
315 if(deviation_right > biggest_deviation_right )
317 biggest_deviation_right = deviation_right;
327 next_dim_left, max_dim, values, splits, size, max_tree_depth,
328 left, current_depth + 1);
331 next_dim_right, max_dim, values, splits, size, max_tree_depth,
332 right, current_depth +1);
336 values, splits, size, max_tree_depth, left, current_depth + 1);
339 values, splits, size, max_tree_depth, right, current_depth +1);
344 for(
int i=0; i<max_dim; i++) {
345 free(sorted_indices[i].elements );
347 free(sorted_indices);
static void fillCriticalIndicesSet(const LBPointArray< float > &V, LBPointArray< unsigned int > &sorted_indices, unsigned int current_dim, float split_value, unsigned int split_index, std::unordered_set< unsigned int > &critical_indices_left, std::unordered_set< unsigned int > &critical_indices_right)
static int getNumThreads()
Returns the number of supported threads (or 1 if OpenMP is not supported)
static ctpl::thread_pool * pool
auto push(F &&f, Rest &&... rest) -> std::future< decltype(f(0, rest...))>
boost::shared_ptr< LBPointArray< unsigned char > > getKdTreeSplits()
boost::shared_ptr< LBPointArray< float > > m_values
void generateKdTree(LBPointArray< float > &vertices)
static int st_num_threads
static void fillCriticalIndices(const LBPointArray< float > &V, LBPointArray< unsigned int > &sorted_indices, unsigned int current_dim, float split_value, unsigned int split_index, std::list< unsigned int > &critical_indices_left, std::list< unsigned int > &critical_indices_right)
static void generateKdTreeRecursive(int id, LBPointArray< float > &V, LBPointArray< unsigned int > *sorted_indices, int current_dim, int max_dim, LBPointArray< float > *values, LBPointArray< unsigned char > *splits, int size, int max_tree_depth, int position, int current_depth)
static int st_depth_threads
void generateKdTreeArray(LBPointArray< float > &V, LBPointArray< unsigned int > *sorted_indices, int max_dim)
Private.
boost::shared_ptr< LBPointArray< float > > getKdTreeValues()
boost::shared_ptr< LBPointArray< unsigned char > > m_splits
void stop(bool isWait=false)
LBKdTree(LBPointArray< float > &vertices, int num_threads=8)
Public.