PointHashGridSearcher2.h
Go to the documentation of this file.
1 /*************************************************************************
2 > File Name: PointHashGridSearcher2.h
3 > Project Name: CubbyFlow
4 > Author: Chan-Ho Chris Ohk
5 > Purpose: Hash grid-based 2-D point searcher.
6 > Created Time: 2017/05/22
7 > Copyright (c) 2018, Chan-Ho Chris Ohk
8 *************************************************************************/
9 #ifndef CUBBYFLOW_POINT_HASH_GRID_SEARCHER2_H
10 #define CUBBYFLOW_POINT_HASH_GRID_SEARCHER2_H
11 
13 #include <Core/Size/Size2.h>
14 
15 #include <vector>
16 
17 namespace CubbyFlow
18 {
27  {
28  public:
30 
31  class Builder;
32 
43  PointHashGridSearcher2(const Size2& resolution, double gridSpacing);
44 
56  PointHashGridSearcher2(size_t resolutionX, size_t resolutionY, double gridSpacing);
57 
60 
62  void Build(const ConstArrayAccessor1<Vector2D>& points) override;
63 
72  void ForEachNearbyPoint(
73  const Vector2D& origin,
74  double radius,
75  const ForEachNearbyPointFunc& callback) const override;
76 
86  bool HasNearbyPoint(const Vector2D& origin, double radius) const override;
87 
97  void Add(const Vector2D& point);
98 
107  const std::vector<std::vector<size_t>>& GetBuckets() const;
108 
116  size_t GetHashKeyFromBucketIndex(const Point2I& bucketIndex) const;
117 
125  Point2I GetBucketIndex(const Vector2D& position) const;
126 
133  PointNeighborSearcher2Ptr Clone() const override;
134 
137 
139  void Set(const PointHashGridSearcher2& other);
140 
142  void Serialize(std::vector<uint8_t>* buffer) const override;
143 
145  void Deserialize(const std::vector<uint8_t>& buffer) override;
146 
148  static Builder GetBuilder();
149 
150  private:
151  double m_gridSpacing = 1.0;
152  Point2I m_resolution = Point2I(1, 1);
153  std::vector<Vector2D> m_points;
154  std::vector<std::vector<size_t>> m_buckets;
155 
156  size_t GetHashKeyFromPosition(const Vector2D& position) const;
157 
158  void GetNearbyKeys(const Vector2D& position, size_t* nearbyKeys) const;
159  };
160 
162  using PointHashGridSearcher2Ptr = std::shared_ptr<PointHashGridSearcher2>;
163 
168  {
169  public:
171  Builder& WithResolution(const Size2& resolution);
172 
174  Builder& WithGridSpacing(double gridSpacing);
175 
178 
181 
184 
185  private:
186  Size2 m_resolution{ 64, 64 };
187  double m_gridSpacing = 1.0;
188  };
189 }
190 
191 #endif
Point2I GetBucketIndex(const Vector2D &position) const
std::shared_ptr< PointHashGridSearcher2 > PointHashGridSearcher2Ptr
Shared pointer for the PointHashGridSearcher2 type.
Definition: PointHashGridSearcher2.h:162
Front-end to create PointHashGridSearcher2 objects step by step.
Definition: PointHashGridSearcher2.h:167
Builder & WithGridSpacing(double gridSpacing)
Returns builder with grid spacing.
void Add(const Vector2D &point)
Adds a single point to the hash grid.
PointNeighborSearcher2Ptr Clone() const override
Creates a new instance of the object with same properties than original.
PointHashGridSearcher2 & operator=(const PointHashGridSearcher2 &other)
Assignment operator.
const std::vector< std::vector< size_t > > & GetBuckets() const
Returns the internal bucket.
Point2< ssize_t > Point2I
Integer-type 2D point.
Definition: Point2.h:312
2-D point class.
Definition: Point2.h:25
std::function< void(size_t, const Vector2D &)> ForEachNearbyPointFunc
Definition: PointNeighborSearcher2.h:34
#define CUBBYFLOW_NEIGHBOR_SEARCHER2_TYPE_NAME(DerivedClassName)
Definition: PointNeighborSearcher2.h:94
1-D read-only array accessor class.
Definition: ArrayAccessor1.h:185
void Set(const PointHashGridSearcher2 &other)
Copy from the other instance.
Abstract base class for 2-D point neighbor searcher builders.
Definition: PointNeighborSearcher2.h:82
Definition: pybind11Utils.h:24
Abstract base class for 2-D neighbor point searcher.
Definition: PointNeighborSearcher2.h:29
void Build(const ConstArrayAccessor1< Vector2D > &points) override
Builds internal acceleration structure for given points list.
void ForEachNearbyPoint(const Vector2D &origin, double radius, const ForEachNearbyPointFunc &callback) const override
PointHashGridSearcher2 Build() const
Builds PointHashGridSearcher2 instance.
void Deserialize(const std::vector< uint8_t > &buffer) override
Deserializes the neighbor searcher from the buffer.
bool HasNearbyPoint(const Vector2D &origin, double radius) const override
PointNeighborSearcher2Ptr BuildPointNeighborSearcher() const override
Returns shared pointer of PointNeighborSearcher2 type.
PointHashGridSearcher2(const Size2 &resolution, double gridSpacing)
Constructs hash grid with given resolution and grid spacing.
static Builder GetBuilder()
Returns builder fox PointHashGridSearcher2.
2-D vector class.
Definition: Vector2.h:26
std::shared_ptr< PointNeighborSearcher2 > PointNeighborSearcher2Ptr
Shared pointer for the PointNeighborSearcher2 type.
Definition: PointNeighborSearcher2.h:79
Builder & WithResolution(const Size2 &resolution)
Returns builder with resolution.
size_t GetHashKeyFromBucketIndex(const Point2I &bucketIndex) const
Hash grid-based 2-D point searcher.
Definition: PointHashGridSearcher2.h:26
void Serialize(std::vector< uint8_t > *buffer) const override
Serializes the neighbor searcher into the buffer.
PointHashGridSearcher2Ptr MakeShared() const
Builds shared pointer of PointHashGridSearcher2 instance.