ListQueryEngine2-Impl.h
Go to the documentation of this file.
1 /*************************************************************************
2 > File Name: ListQueryEngine2-Impl.h
3 > Project Name: CubbyFlow
4 > Author: Chan-Ho Chris Ohk
5 > Purpose: Ad-hoc list-based 2-D intersection/nearest-neighbor query engine.
6 > Created Time: 2017/10/14
7 > Copyright (c) 2018, Chan-Ho Chris Ohk
8 *************************************************************************/
9 #ifndef CUBBYFLOW_LIST_QUERY_ENGINE2_IMPL_H
10 #define CUBBYFLOW_LIST_QUERY_ENGINE2_IMPL_H
11 
12 namespace CubbyFlow
13 {
14  template <typename T>
15  void ListQueryEngine2<T>::Add(const T& item)
16  {
17  m_items.push_back(item);
18  }
19 
20  template <typename T>
21  void ListQueryEngine2<T>::Add(const std::vector<T>& items)
22  {
23  m_items.insert(m_items.end(), items.begin(), items.end());
24  }
25 
26  template <typename T>
28  const BoundingBox2D& box,
29  const BoxIntersectionTestFunc2<T>& testFunc) const
30  {
31  for (const auto& item : m_items)
32  {
33  if (testFunc(item, box))
34  {
35  return true;
36  }
37  }
38 
39  return false;
40  }
41 
42  template <typename T>
44  const Ray2D& ray, const RayIntersectionTestFunc2<T>& testFunc) const
45  {
46  for (const auto& item : m_items)
47  {
48  if (testFunc(item, ray))
49  {
50  return true;
51  }
52  }
53 
54  return false;
55  }
56 
57  template <typename T>
59  const BoundingBox2D& box, const BoxIntersectionTestFunc2<T>& testFunc,
60  const IntersectionVisitorFunc2<T>& visitorFunc) const
61  {
62  for (const auto& item : m_items)
63  {
64  if (testFunc(item, box))
65  {
66  visitorFunc(item);
67  }
68  }
69  }
70 
71  template <typename T>
73  const Ray2D& ray, const RayIntersectionTestFunc2<T>& testFunc,
74  const IntersectionVisitorFunc2<T>& visitorFunc) const
75  {
76  for (const auto& item : m_items)
77  {
78  if (testFunc(item, ray))
79  {
80  visitorFunc(item);
81  }
82  }
83  }
84 
85  template <typename T>
87  const Ray2D& ray, const GetRayIntersectionFunc2<T>& testFunc) const
88  {
90 
91  for (const auto& item : m_items)
92  {
93  double dist = testFunc(item, ray);
94  if (dist < best.distance)
95  {
96  best.distance = dist;
97  best.item = &item;
98  }
99  }
100 
101  return best;
102  }
103 
104  template <typename T>
106  const Vector2D& pt,
107  const NearestNeighborDistanceFunc2<T>& distanceFunc) const
108  {
110 
111  for (const auto& item : m_items)
112  {
113  double dist = distanceFunc(item, pt);
114  if (dist < best.distance)
115  {
116  best.item = &item;
117  best.distance = dist;
118  }
119  }
120 
121  return best;
122  }
123 }
124 
125 #endif
double distance
Definition: NearestNeighborQueryEngine2.h:21
Closest intersection query result.
Definition: IntersectionQueryEngine2.h:19
Class for 2-D ray.
Definition: Ray2.h:23
bool IsIntersects(const BoundingBox2D &box, const BoxIntersectionTestFunc2< T > &testFunc) const override
Returns true if given box intersects with any of the stored items.
Definition: ListQueryEngine2-Impl.h:27
const T * item
Definition: IntersectionQueryEngine2.h:21
ClosestIntersectionQueryResult2< T > GetClosestIntersection(const Ray2D &ray, const GetRayIntersectionFunc2< T > &testFunc) const override
Returns the closest intersection for given ray.
Definition: ListQueryEngine2-Impl.h:86
std::function< bool(const T &, const BoundingBox2D &)> BoxIntersectionTestFunc2
Box-item intersection test function.
Definition: IntersectionQueryEngine2.h:31
Nearest neighbor query result.
Definition: NearestNeighborQueryEngine2.h:18
const T * item
Definition: NearestNeighborQueryEngine2.h:20
2-D axis-aligned bounding box class.
Definition: BoundingBox2.h:44
Definition: pybind11Utils.h:24
NearestNeighborQueryResult2< T > GetNearestNeighbor(const Vector2D &pt, const NearestNeighborDistanceFunc2< T > &distanceFunc) const override
Returns the nearest neighbor for given point and distance measure function.
Definition: ListQueryEngine2-Impl.h:105
std::function< void(const T &)> IntersectionVisitorFunc2
Visitor function which is invoked for each intersecting item.
Definition: IntersectionQueryEngine2.h:43
double distance
Definition: IntersectionQueryEngine2.h:22
void ForEachIntersectingItem(const BoundingBox2D &box, const BoxIntersectionTestFunc2< T > &testFunc, const IntersectionVisitorFunc2< T > &visitorFunc) const override
Invokes visitorFunc for every intersecting items.
Definition: ListQueryEngine2-Impl.h:58
void Add(const T &item)
Adds an item to the container.
Definition: ListQueryEngine2-Impl.h:15
2-D vector class.
Definition: Vector2.h:26
std::function< double(const T &, const Ray2D &)> GetRayIntersectionFunc2
Ray-item closest intersection evaluation function.
Definition: IntersectionQueryEngine2.h:39
std::function< bool(const T &, const Ray2D &)> RayIntersectionTestFunc2
Ray-item intersection test function.
Definition: IntersectionQueryEngine2.h:35
std::function< double(const T &, const Vector2D &)> NearestNeighborDistanceFunc2
Nearest neighbor distance measure function.
Definition: NearestNeighborQueryEngine2.h:26