SPH
LinkedList.h
Go to the documentation of this file.
1 #pragma once
2 
4 #include "objects/geometry/Box.h"
6 #include <algorithm>
7 
9 
10 /*class LinkedList : public INeighbourFinder {
11 private:
12  VectorOrder sortedIndices;
13  VectorOrder rank;
14 
15  class LookupMap : public Noncopyable {
16  private:
17  Array<Size> storage;
18  Size dimensionSize;
19 
20  INLINE Size map(const Indices& v) const {
21  return v[X] * sqr(dimensionSize) + v[Y] * dimensionSize + v[Z];
22  }
23 
24  public:
25  LookupMap() = default;
26 
27  LookupMap(const Size n)
28  : storage(pow<3>(n))
29  , dimensionSize(n) {
30  // clear all cells
31  storage.fill(0);
32  }
33 
34  LookupMap& operator=(LookupMap&& other) {
35  storage = std::move(other.storage);
36  dimensionSize = other.dimensionSize;
37  return *this;
38  }
39 
40  INLINE Size operator()(const Indices& v) const {
41  const Size idx = map(v);
42  SPH_ASSERT(unsigned(idx) < unsigned(storage.size()));
43  return storage[idx];
44  }
45 
46  INLINE Size& operator()(const Indices& v) {
47  const Size idx = map(v);
48  SPH_ASSERT(unsigned(idx) < unsigned(storage.size()));
49  return storage[idx];
50  }
51  };
52  LookupMap map;
53  Size cellCnt;
54  Array<Vector> lowerBounds;
55  Array<Vector> upperBounds;
56  Array<Size> linkedList;
57 
58 public:
59  LinkedList() {
60  Indices::init();
61  }
62 
63  virtual Size findNeighbours(const Size index,
64  const Float radius,
65  Array<NeighbourRecord>& neighbours,
66  Flags<FinderFlag> flags = EMPTY_FLAGS,
67  const Float UNUSED(error) = 0.f) const override {
68  neighbours.clear();
69  const Float cellCntSqrInv = 1._f / sqr(cellCnt);
70  const Box bounds(this->values[index] - Vector(radius), this->values[index] + Vector(radius));
71  Indices refRank = rank[index];
72  Indices lower(Vector(refRank) * cellCntSqrInv);
73  Indices upper = lower;
74  for (uint i = 0; i < 3; ++i) {
75  while (lower[i] > 0 && bounds.lower()[i] <= lowerBounds[lower[i]][i]) {
76  lower[i]--;
77  }
78  }
79  for (uint i = 0; i < 3; ++i) {
80  while (Size(upper[i]) < upperBounds.size() - 1 && bounds.upper()[i] <= upperBounds[upper[i]][i]) {
81  upper[i]++;
82  }
83  }
84  lower = max(lower, Indices(0));
85  upper = min(upper, Indices(upperBounds.size() - 1));
86  const Size refRankH =
87  flags.has(FinderFlag::FIND_ONLY_SMALLER_H) ? rank[index][H] : this->values.size();
88  for (int x = lower[X]; x <= upper[X]; ++x) {
89  for (int y = lower[Y]; y <= upper[Y]; ++y) {
90  for (int z = lower[Z]; z <= upper[Z]; ++z) {
91  const Indices idxs(x, y, z);
92  Size cell = map(idxs);
93  while (cell != 0) {
94  const Float lengthSqr = getSqrLength(this->values[cell] - this->values[index]);
95  if (Size(rank[cell][H]) < refRankH && lengthSqr < sqr(radius)) {
96  neighbours.push(NeighbourRecord{ cell, lengthSqr });
97  }
98  cell = linkedList[cell];
99  }
100  }
101  }
102  }
103  return neighbours.size();
104  }
105 
106 protected:
107  virtual void rebuildImpl(ArrayView<const Vector> points) override {
108  for (uint i = 0; i < 3; ++i) {
109  sortedIndices.shuffle(i, [&](Size idx1, Size idx2) { return points[idx1][i] < points[idx2][i]; });
110  }
112  sortedIndices.shuffle(H, [&](Size idx1, Size idx2) { return points[idx1][H] < points[idx2][H]; });
113  rank = sortedIndices.getInverted();
114  map = LookupMap(cellCnt);
115  lowerBounds.fill(Vector(INFTY));
116  upperBounds.fill(Vector(-INFTY));
117  const Float cellCntSqrInv = 1._f / sqr(cellCnt);
118 
119  for (Size idx = 0; idx < points.size(); ++idx) {
120  const Indices multiIdx(Vector(rank[idx]) * cellCntSqrInv);
121  Size& cell = map(multiIdx);
122  linkedList[idx] = cell;
123  cell = idx;
124 
126  for (uint i = 0; i < 3; ++i) {
127  Float& lb = lowerBounds[multiIdx[i]][i];
128  lb = min(lb, points[idx][i]);
129  Float& ub = upperBounds[multiIdx[i]][i];
130  ub = max(ub, points[idx][i]);
131  }
132  }
133  }
134 
135  virtual void buildImpl(ArrayView<const Vector> points) override {
136  sortedIndices = VectorOrder(points.size());
137  rank = VectorOrder(points.size());
138  linkedList.resize(points.size());
139  cellCnt = cbrt(points.size()) + 1;
140 
141  lowerBounds.resize(cellCnt);
142  upperBounds.resize(cellCnt);
143  rebuildImpl(points);
144  }
145 };*/
146 
NAMESPACE_SPH_BEGIN
Definition: BarnesHut.cpp:13
Object representing a three-dimensional axis-aligned box.
Ordinary iterator for custom containers.
#define NAMESPACE_SPH_END
Definition: Object.h:12