SPH
NeighbourFinder.h
Go to the documentation of this file.
1 #pragma once
2 
7 
11 #include "objects/wrappers/Flags.h"
12 
14 
15 class IScheduler;
16 
21 
24 
25  bool operator!=(const NeighbourRecord& other) const {
26  return index != other.index || distanceSqr != other.distanceSqr;
27  }
28 
30  bool operator<(const NeighbourRecord& other) const {
31  return distanceSqr < other.distanceSqr;
32  }
33 };
34 
39 class IBasicFinder : public Polymorphic {
40 protected:
43 
44 public:
50  void build(IScheduler& scheduler, ArrayView<const Vector> points);
51 
62  virtual Size findAll(const Size index, const Float radius, Array<NeighbourRecord>& neighbours) const = 0;
63 
67  virtual Size findAll(const Vector& pos, const Float radius, Array<NeighbourRecord>& neighbours) const = 0;
68 
69 protected:
75  virtual void buildImpl(IScheduler& scheduler, ArrayView<const Vector> points) = 0;
76 };
77 
78 enum class FinderFlag {
80  MAKE_RANK = 1 << 0,
81 
83  SKIP_RANK = 0,
84 };
85 
87 template <typename TCompare>
88 static Order makeRank(const Size size, TCompare&& comp) {
89  Order tmp(size);
90  // sort using the given comparator
91  tmp.shuffle(comp);
92  // invert to get the rank
93  return tmp.getInverted();
94 }
95 
102 protected:
105 
106 public:
108  void build(IScheduler& scheduler,
111 
113  template <typename TCompare>
114  void buildWithRank(IScheduler& scheduler, ArrayView<const Vector> points, TCompare&& comp) {
115  values = points;
116  rank = makeRank(values.size(), comp);
117  this->buildImpl(scheduler, values);
118  }
119 
130  virtual Size findLowerRank(const Size index,
131  const Float radius,
132  Array<NeighbourRecord>& neighbours) const = 0;
133 };
134 
136 template <typename TDerived>
138 public:
139  virtual Size findAll(const Size index,
140  const Float radius,
141  Array<NeighbourRecord>& neighbours) const override {
142  neighbours.clear();
143  return static_cast<const TDerived*>(this)->template find<true>(
144  values[index], index, radius, neighbours);
145  }
146 
147  virtual Size findAll(const Vector& pos,
148  const Float radius,
149  Array<NeighbourRecord>& neighbours) const override {
150  neighbours.clear();
151  if (SPH_UNLIKELY(values.empty())) {
152  return 0._f;
153  }
154  // the index here is irrelevant, so let's use something that would cause assert in case we messed
155  // something up
156  const Size index = values.size();
157  return static_cast<const TDerived*>(this)->template find<true>(pos, index, radius, neighbours);
158  }
159 
160  virtual Size findLowerRank(const Size index,
161  const Float radius,
162  Array<NeighbourRecord>& neighbours) const override {
163  neighbours.clear();
164  return static_cast<const TDerived*>(this)->template find<false>(
165  values[index], index, radius, neighbours);
166  }
167 };
168 
Simple non-owning view of a container.
NAMESPACE_SPH_BEGIN
Definition: BarnesHut.cpp:13
const float radius
Definition: CurveDialog.cpp:18
Wrapper over enum allowing setting (and querying) individual bits of the stored value.
uint32_t Size
Integral type used to index arrays (by default).
Definition: Globals.h:16
double Float
Precision used withing the code. Use Float instead of float or double where precision is important.
Definition: Globals.h:13
FinderFlag
@ MAKE_RANK
Creates the ranks of particles. Without this flag, only the IBasicFinder interface can be used.
@ SKIP_RANK
The rank of particles is not created. 'Dummy' option that can be used to improve readability.
#define SPH_UNLIKELY(x)
Definition: Object.h:50
#define NAMESPACE_SPH_END
Definition: Object.h:12
Helper object defining permutation.
Basic vector algebra. Computations are accelerated using SIMD.
Object providing safe access to continuous memory of data.
Definition: ArrayView.h:17
void clear()
Removes all elements from the array, but does NOT release the memory.
Definition: Array.h:434
Helper template, allowing to define all three functions with a single function.
virtual Size findAll(const Vector &pos, const Float radius, Array< NeighbourRecord > &neighbours) const override
Finds all points within given radius from given position.
virtual Size findLowerRank(const Size index, const Float radius, Array< NeighbourRecord > &neighbours) const override
Finds all points within radius that have a lower rank in smoothing length.
virtual Size findAll(const Size index, const Float radius, Array< NeighbourRecord > &neighbours) const override
Finds all neighbours within given radius from the point given by index.
Wrapper of an integral value providing functions for reading and modifying individual bits.
Definition: Flags.h:20
Interface of objects finding neighbouring particles.
virtual Size findAll(const Vector &pos, const Float radius, Array< NeighbourRecord > &neighbours) const =0
Finds all points within given radius from given position.
ArrayView< const Vector > values
View of the source datapoints, updated every time (re)build is called.
virtual void buildImpl(IScheduler &scheduler, ArrayView< const Vector > points)=0
Builds finder from set of vectors.
void build(IScheduler &scheduler, ArrayView< const Vector > points)
Constructs the struct with an array of vectors.
virtual Size findAll(const Size index, const Float radius, Array< NeighbourRecord > &neighbours) const =0
Finds all neighbours within given radius from the point given by index.
Interface that allows unified implementation of sequential and parallelized versions of algorithms.
Definition: Scheduler.h:27
Extension of IBasicFinder, allowing to search only particles with lower rank in smoothing length.
Order rank
Ranks of particles according to their smoothing lengths.
void buildWithRank(IScheduler &scheduler, ArrayView< const Vector > points, TCompare &&comp)
Constructs the struct with custom predicate for ordering particles.
void build(IScheduler &scheduler, ArrayView< const Vector > points, Flags< FinderFlag > flags=FinderFlag::MAKE_RANK)
Constructs the struct with an array of vectors.
virtual Size findLowerRank(const Size index, const Float radius, Array< NeighbourRecord > &neighbours) const =0
Finds all points within radius that have a lower rank in smoothing length.
Permutation, i.e. (discrete) invertible function int->int.
Definition: Order.h:18
Holds information about a neighbour particles.
Size index
Index of particle in the storage.
bool operator<(const NeighbourRecord &other) const
Sort by the distance.
Float distanceSqr
Squared distance of the particle from the queried particle / position.
bool operator!=(const NeighbourRecord &other) const
Base class for all polymorphic objects.
Definition: Object.h:88