Euphoria
closestpoint.h
Go to the documentation of this file.
1 #pragma once
2 
3 
4 #include <tuple>
5 #include <optional>
6 
7 #include "assert/assert.h"
8 
9 namespace eu::core
10 {
11  // todo(Gustav): replace with a better search alghoritm, like k-d tree och oct/quad-tree
12  template
13  <
14  typename TVec,
15  typename TData,
16  typename TDistanceFunc,
17  typename TDistance
18  >
20  {
21  TDistanceFunc distance_func;
22  std::vector<std::tuple<TVec, TData>> points;
23 
24  ClosestPointCollector(TDistanceFunc a_distance_func)
25  : distance_func(a_distance_func)
26  {
27  }
28 
29  void
30  add(const TVec& v, const TData& data)
31  {
32  points.emplace_back(std::make_tuple(v, data));
33  }
34 
35  [[nodiscard]] TData
36  find_closest(const TVec& v)
37  {
38  struct DistanceData { TData data; TDistance distance; };
39  ASSERT(!points.empty());
40 
41  std::optional<DistanceData> result;
42  for(const auto& [vec, data]: points)
43  {
44  const auto this_dist = distance_func(v, vec);
45  if(result.has_value() == false || result->distance > this_dist)
46  {
47  result = {data, this_dist};
48  }
49  }
50 
51  ASSERT(result.has_value());
52  return result->data;
53  }
54  };
55 }
56 
#define ASSERT(x)
Definition: assert.h:29
ClosestPointCollector(TDistanceFunc a_distance_func)
Definition: closestpoint.h:24
std::vector< std::tuple< TVec, TData > > points
Definition: closestpoint.h:22
TData find_closest(const TVec &v)
Definition: closestpoint.h:36
void add(const TVec &v, const TData &data)
Definition: closestpoint.h:30