SPH
Volume.h
Go to the documentation of this file.
1 #pragma once
2 
4 #include "objects/geometry/Box.h"
5 
7 
8 template <typename TValue>
9 class Volume {
10 private:
11  Array<TValue> data;
12  Box box;
13  Size res = 0;
14 
15 public:
16  Volume() = default;
17 
18  Volume(const Box& box, const Size resolution)
19  : box(box)
20  , res(resolution) {
21  data.resize(pow<3>(res));
22  data.fill(TValue(0));
23  }
24 
25  TValue operator()(const Vector& r) const {
26  const Vector idxs = (r - box.lower()) / box.size() * res;
27  return data[map(clamp(idxs[X]), clamp(idxs[Y]), clamp(idxs[Z]))];
28  }
29 
30 
31  TValue& operator()(const Vector& r) {
32  const Vector idxs = (r - box.lower()) / box.size() * res;
33  return data[map(clamp(idxs[X]), clamp(idxs[Y]), clamp(idxs[Z]))];
34  }
35 
36  TValue& cell(const Size x, const Size y, const Size z) {
37  return data[map(x, y, z)];
38  }
39 
40  Size size() const {
41  return res;
42  }
43 
44  bool empty() const {
45  return res == 0;
46  }
47 
48 private:
49  Size clamp(const Float f) const {
50  return Sph::clamp(int(f), 0, int(res));
51  }
52 
53  Size map(const Size x, const Size y, const Size z) const {
54  return x + y * res + z * sqr(res);
55  }
56 };
57 
Generic dynamically allocated resizable storage.
NAMESPACE_SPH_BEGIN
Definition: BarnesHut.cpp:13
Object representing a three-dimensional axis-aligned box.
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
constexpr INLINE T clamp(const T &f, const T &f1, const T &f2)
Definition: MathBasic.h:35
constexpr INLINE T sqr(const T &f) noexcept
Return a squared value.
Definition: MathUtils.h:67
constexpr INLINE Float pow< 3 >(const Float v)
Definition: MathUtils.h:132
#define NAMESPACE_SPH_END
Definition: Object.h:12
@ Y
Definition: Vector.h:23
@ X
Definition: Vector.h:22
@ Z
Definition: Vector.h:24
void resize(const TCounter newSize)
Resizes the array to new size.
Definition: Array.h:215
void fill(const T &t)
Sets all elements of the array to given value.
Definition: Array.h:187
Helper object defining three-dimensional interval (box).
Definition: Box.h:17
INLINE const Vector & lower() const
Returns lower bounds of the box.
Definition: Box.h:82
INLINE Vector size() const
Returns box dimensions.
Definition: Box.h:106
Definition: Volume.h:9
bool empty() const
Definition: Volume.h:44
TValue operator()(const Vector &r) const
Definition: Volume.h:25
Size size() const
Definition: Volume.h:40
Volume()=default
Volume(const Box &box, const Size resolution)
Definition: Volume.h:18
TValue & cell(const Size x, const Size y, const Size z)
Definition: Volume.h:36
TValue & operator()(const Vector &r)
Definition: Volume.h:31