SPH
Point.h
Go to the documentation of this file.
1 #pragma once
2 
7 
8 #include "math/MathUtils.h"
10 
12 
16 struct PlotPoint : public OperatorTemplate<PlotPoint> {
17  Float x, y;
18 
19  PlotPoint() = default;
20 
21  PlotPoint(const Float x, const Float y)
22  : x(x)
23  , y(y) {}
24 
25  PlotPoint& operator+=(const PlotPoint& other) {
26  x += other.x;
27  y += other.y;
28  return *this;
29  }
30 
31  PlotPoint& operator*=(const Float value) {
32  x *= value;
33  y *= value;
34  return *this;
35  }
36 
37  bool operator==(const PlotPoint& other) const {
38  return x == other.x && y == other.y;
39  }
40 };
41 
43 struct ErrorPlotPoint : public PlotPoint {
45 };
46 
50 class AffineMatrix2 : public OperatorTemplate<AffineMatrix2> {
51 private:
52  Float data[6];
53 
54 public:
60  AffineMatrix2(const Float scale = 1._f, const PlotPoint translation = PlotPoint(0._f, 0._f))
61  : data{ scale, 0._f, translation.x, 0._f, scale, translation.y } {}
62 
64  AffineMatrix2(const Float xx,
65  const Float yx,
66  const Float xy,
67  const Float yy,
68  const Float tx = 0._f,
69  const Float ty = 0._f)
70  : data{ xx, yx, tx, xy, yy, ty } {}
71 
73  Float& operator()(const Size i, const Size j) {
74  SPH_ASSERT(i < 2 && j < 3);
75  return data[3 * i + j];
76  }
77 
79  Float operator()(const Size i, const Size j) const {
80  SPH_ASSERT(i < 2 && j < 3);
81  return data[3 * i + j];
82  }
83 
87  Float get(const Size i, const Size j) const {
88  return operator()(i, j);
89  }
90 
93  return PlotPoint(
94  get(0, 0) * p.x + get(0, 1) * p.y + get(0, 2), get(1, 0) * p.x + get(1, 1) * p.y + get(1, 2));
95  }
96 
101  return PlotPoint(get(0, 0) * p.x + get(0, 1) * p.y, get(1, 0) * p.x + get(1, 1) * p.y);
102  }
103 
108  const Float a = get(0, 0);
109  const Float b = get(0, 1);
110  const Float c = get(1, 0);
111  const Float d = get(1, 1);
112  const Float tx = get(0, 2);
113  const Float ty = get(1, 2);
114 
115  const Float det = a * d - b * c;
116  const Float detInv = 1._f / det;
117  SPH_ASSERT(det != 0._f);
118 
119  return AffineMatrix2(d * detInv,
120  -b * detInv,
121  -c * detInv,
122  a * detInv,
123  -(d * tx - b * ty) * detInv,
124  (c * tx - a * ty) * detInv);
125  }
126 
133  return AffineMatrix2(data[0], data[3], data[1], data[4], -data[2], -data[5]);
134  }
135 
137  static AffineMatrix2 rotate(const Float phi) {
138  const Float c = cos(phi);
139  const Float s = sin(phi);
140  return AffineMatrix2(c, s, -s, c);
141  }
142 
143  bool operator==(const AffineMatrix2& other) const {
144  for (Size i = 0; i < 6; ++i) {
145  if (data[i] != other.data[i]) {
146  return false;
147  }
148  }
149  return true;
150  }
151 };
152 
153 
#define SPH_ASSERT(x,...)
Definition: Assert.h:94
NAMESPACE_SPH_BEGIN
Definition: BarnesHut.cpp:13
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
Additional math routines (with more includes).
INLINE T sin(const T f)
Definition: MathUtils.h:296
INLINE T cos(const T f)
Definition: MathUtils.h:291
#define NAMESPACE_SPH_END
Definition: Object.h:12
Class defining additional operators from existing ones.
2D affine matrix
Definition: Point.h:50
Float get(const Size i, const Size j) const
Returns the given component of the matrix.
Definition: Point.h:87
PlotPoint transformPoint(const PlotPoint &p) const
Applies the affine transform on given point.
Definition: Point.h:92
AffineMatrix2(const Float scale=1._f, const PlotPoint translation=PlotPoint(0._f, 0._f))
Creates the matrix given a uniform scaling factor and a translation vector.
Definition: Point.h:60
static AffineMatrix2 rotate(const Float phi)
Create a rotation matrix.
Definition: Point.h:137
bool operator==(const AffineMatrix2 &other) const
Definition: Point.h:143
Float operator()(const Size i, const Size j) const
Returns the given component of the matrix.
Definition: Point.h:79
AffineMatrix2(const Float xx, const Float yx, const Float xy, const Float yy, const Float tx=0._f, const Float ty=0._f)
Creates the matrix from individual components.
Definition: Point.h:64
PlotPoint transformVector(const PlotPoint &p) const
Applies the transform on given vector.
Definition: Point.h:100
Float & operator()(const Size i, const Size j)
Returns the given component of the matrix.
Definition: Point.h:73
AffineMatrix2 transpose() const
Returns the transposed matrix.
Definition: Point.h:131
AffineMatrix2 inverse() const
Returns the inverse of the matrix.
Definition: Point.h:107
Point with error bars.
Definition: Point.h:43
Float dy
Definition: Point.h:44
Float dx
Definition: Point.h:44
Class defining additional operators from existing ones.
Point in 2D plot.
Definition: Point.h:16
PlotPoint & operator+=(const PlotPoint &other)
Definition: Point.h:25
Float y
Definition: Point.h:17
PlotPoint(const Float x, const Float y)
Definition: Point.h:21
PlotPoint & operator*=(const Float value)
Definition: Point.h:31
Float x
Definition: Point.h:17
PlotPoint()=default
bool operator==(const PlotPoint &other) const
Definition: Point.h:37