Initial commit: Final state of the master project
This commit is contained in:
38
Research/scene/Material/NearestFinder.h
Normal file
38
Research/scene/Material/NearestFinder.h
Normal file
@@ -0,0 +1,38 @@
|
||||
#pragma once
|
||||
#include <vector>
|
||||
#include <assert.h>
|
||||
#include "../../core/CollectionHelper.h"
|
||||
#include "../../inc/tbb/parallel_for.h"
|
||||
|
||||
template<typename T>
|
||||
struct NearestFinder
|
||||
{
|
||||
T operator()(const T& source, const std::vector<T>& values) const
|
||||
{
|
||||
assert(!values.empty());
|
||||
float min = T::Distance(source, values[0]);
|
||||
size_t minIndex = 0;
|
||||
for (size_t i = 0; i < values.size(); i++)
|
||||
{
|
||||
float distance = T::Distance(source, values[i]);
|
||||
if (distance < min)
|
||||
{
|
||||
min = distance;
|
||||
minIndex = i;
|
||||
}
|
||||
}
|
||||
return values[minIndex];
|
||||
}
|
||||
};
|
||||
|
||||
template<typename T>
|
||||
struct ParallelNearestFinder
|
||||
{
|
||||
T operator()(const T& source, const std::vector<T>& values) const
|
||||
{
|
||||
assert(!values.empty());
|
||||
std::vector<float> distances(values.size());
|
||||
tbb::parallel_for(size_t(0), values.size(), [&](size_t i) { distances[i] = T::Distance(source, values[i]); });
|
||||
return values[CollectionHelper::MinIndex(distances)];
|
||||
}
|
||||
};
|
||||
Reference in New Issue
Block a user