SPH
Point.h
Go to the documentation of this file.
1 #pragma once
2 
7 
8 #include "math/MathUtils.h"
9 #include "objects/Object.h"
10 #include <wx/gdicmn.h>
11 
13 
14 template <typename T, typename TDerived>
15 struct BasicPoint {
16  T x, y;
17 
18  BasicPoint() = default;
19 
20  BasicPoint(const T x, const T y)
21  : x(x)
22  , y(y) {}
23 
24  T& operator[](const Size index) {
25  SPH_ASSERT(index < 2);
26  return reinterpret_cast<T*>(this)[index];
27  }
28 
29  const T& operator[](const Size index) const {
30  SPH_ASSERT(index < 2);
31  return reinterpret_cast<const T*>(this)[index];
32  }
33 
34  TDerived& operator+=(const TDerived& other) {
35  x += other.x;
36  y += other.y;
37  return static_cast<TDerived&>(*this);
38  }
39 
40  TDerived& operator-=(const TDerived& other) {
41  x -= other.x;
42  y -= other.y;
43  return static_cast<TDerived&>(*this);
44  }
45 
46  TDerived& operator*=(const float factor) {
47  x = T(x * factor);
48  y = T(y * factor);
49  return static_cast<TDerived&>(*this);
50  }
51 
52  TDerived& operator/=(const float factor) {
53  x = T(x / factor);
54  y = T(y / factor);
55  return static_cast<TDerived&>(*this);
56  }
57 
58  TDerived operator+(const TDerived& other) const {
59  TDerived result(static_cast<const TDerived&>(*this));
60  result += other;
61  return result;
62  }
63 
64  TDerived operator-(const TDerived& other) const {
65  TDerived result(static_cast<const TDerived&>(*this));
66  result -= other;
67  return result;
68  }
69 
70  TDerived operator*(const float factor) const {
71  TDerived result(static_cast<const TDerived&>(*this));
72  result *= factor;
73  return result;
74  }
75 
76  TDerived operator/(const float factor) const {
77  TDerived result(static_cast<const TDerived&>(*this));
78  result /= factor;
79  return result;
80  }
81 
82  bool operator==(const TDerived& other) const {
83  return x == other.x && y == other.y;
84  }
85 
86  bool operator!=(const TDerived& other) const {
87  return !(*this == other);
88  }
89 
90  friend std::ostream& operator<<(std::ostream& stream, const BasicPoint& p) {
91  stream << p.x << " " << p.y;
92  return stream;
93  }
94 
95  friend std::istream& operator>>(std::istream& stream, BasicPoint& p) {
96  stream >> p.x >> p.y;
97  return stream;
98  }
99 };
100 
101 struct Pixel : public BasicPoint<int, Pixel> {
102  Pixel() = default;
103 
104  Pixel(const int x, const int y)
105  : BasicPoint<int, Pixel>(x, y) {}
106 
107  explicit Pixel(const wxPoint point)
108  : BasicPoint<int, Pixel>(point.x, point.y) {}
109 
110  explicit operator wxPoint() const {
111  return wxPoint(this->x, this->y);
112  }
113 };
114 
115 struct Coords : public BasicPoint<float, Coords> {
116  Coords() = default;
117 
118  Coords(const float x, const float y)
119  : BasicPoint<float, Coords>(x, y) {}
120 
121  explicit Coords(const Pixel p)
122  : BasicPoint<float, Coords>(p.x, p.y) {}
123 
126 
127  Coords operator*(const Coords& other) const {
128  Coords res = *this;
129  res.x *= other.x;
130  res.y *= other.y;
131  return res;
132  }
133 
134  Coords operator/(const Coords& other) const {
135  Coords res = *this;
136  SPH_ASSERT(other.x != 0.f && other.y != 0.f);
137  res.x /= other.x;
138  res.y /= other.y;
139  return res;
140  }
141 
142  explicit operator Pixel() const {
143  return Pixel(int(x), int(y));
144  }
145 
146  explicit operator wxPoint() const {
147  return wxPoint(int(x), int(y));
148  }
149 };
150 
151 template <typename T, typename TDerived>
153  return sqrt(float(sqr(p.x) + sqr(p.y)));
154 }
155 
#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
Additional math routines (with more includes).
constexpr INLINE T sqr(const T &f) noexcept
Return a squared value.
Definition: MathUtils.h:67
INLINE T sqrt(const T f)
Return a squared root of a value.
Definition: MathUtils.h:78
Common macros and basic objects.
#define INLINE
Macros for conditional compilation based on selected compiler.
Definition: Object.h:31
#define NAMESPACE_SPH_END
Definition: Object.h:12
INLINE float getLength(const BasicPoint< T, TDerived > &p)
Definition: Point.h:152
TDerived operator-(const TDerived &other) const
Definition: Point.h:64
TDerived operator/(const float factor) const
Definition: Point.h:76
const T & operator[](const Size index) const
Definition: Point.h:29
bool operator==(const TDerived &other) const
Definition: Point.h:82
TDerived & operator/=(const float factor)
Definition: Point.h:52
TDerived & operator+=(const TDerived &other)
Definition: Point.h:34
BasicPoint(const T x, const T y)
Definition: Point.h:20
TDerived & operator-=(const TDerived &other)
Definition: Point.h:40
TDerived operator*(const float factor) const
Definition: Point.h:70
BasicPoint()=default
TDerived operator+(const TDerived &other) const
Definition: Point.h:58
TDerived & operator*=(const float factor)
Definition: Point.h:46
friend std::ostream & operator<<(std::ostream &stream, const BasicPoint &p)
Definition: Point.h:90
T & operator[](const Size index)
Definition: Point.h:24
bool operator!=(const TDerived &other) const
Definition: Point.h:86
friend std::istream & operator>>(std::istream &stream, BasicPoint &p)
Definition: Point.h:95
Definition: Point.h:115
Coords operator*(const Coords &other) const
Definition: Point.h:127
Coords()=default
Coords operator/(const Coords &other) const
Definition: Point.h:134
Coords(const Pixel p)
Definition: Point.h:121
Coords(const float x, const float y)
Definition: Point.h:118
Definition: Point.h:101
Pixel()=default
Pixel(const int x, const int y)
Definition: Point.h:104
Pixel(const wxPoint point)
Definition: Point.h:107