ʻOhana
Population structure, admixture history, and selection using learning methods.
jade.vec2.hpp
1 /* -------------------------------------------------------------------------
2  Ohana
3  Copyright (c) 2015-2020 Jade Cheng (\___/)
4  Jade Cheng <info@jade-cheng.com> (='.'=)
5  ------------------------------------------------------------------------- */
6 
7 #ifndef JADE_VEC2_HPP__
8 #define JADE_VEC2_HPP__
9 
10 #include "jade.system.hpp"
11 
12 namespace jade
13 {
14  ///
15  /// A template for a class that implements rank-two vector operations.
16  ///
17  template <typename TValue>
18  class basic_vec2 {
19  public:
20  /// The value type.
21  typedef TValue value_type;
22 
23  value_type x; ///< The X coordinate.
24  value_type y; ///< The Y coordinate.
25 
26  ///
27  /// Implements a new insatnce of the class.
28  ///
29  inline basic_vec2()
30  : x (0)
31  , y (0)
32  {
33  }
34 
35  ///
36  /// Implements a new insatnce of the class with the specified values.
37  ///
38  inline basic_vec2(
39  const value_type x_, ///< The initial X coordinate.
40  const value_type y_) ///< The initial Y coordinate.
41  : x (x_)
42  , y (y_)
43  {
44  }
45 
46  ///
47  /// \return The cross product for two vectors.
48  ///
49  inline static value_type cross(
50  const basic_vec2 & lhs, ///< The first vector.
51  const basic_vec2 & rhs) ///< The second vector.
52  {
53  return (lhs.x * rhs.y) - (lhs.y * rhs.x);
54  }
55 
56  ///
57  /// \return The distance between two vectors.
58  ///
59  inline static value_type distance(
60  const basic_vec2 & a, ///< The first vector.
61  const basic_vec2 & b) ///< The second vector.
62  {
63  return (a - b).get_length();
64  }
65 
66  ///
67  /// \return The distance between two vectors.
68  ///
70  const basic_vec2 & a, ///< The first vector.
71  const basic_vec2 & b) ///< The second vector.
72  {
73  return (a - b).get_length_squared();
74  }
75 
76  ///
77  /// \return The dot product of two vectors.
78  ///
79  inline static basic_vec2 dot(
80  const basic_vec2 & a, ///< The first vector.
81  const basic_vec2 & b) ///< The second vector.
82  {
83  return basic_vec2(a.x * b.x, a.y * b.y);
84  }
85 
86  ///
87  /// \return The length of the vector.
88  ///
89  inline value_type get_length() const
90  {
91  return std::sqrt(get_length_squared());
92  }
93 
94  ///
95  /// \return The length of the vector.
96  ///
98  {
99  return (x * x) + (y * y);
100  }
101 
102  ///
103  /// \return A new vector from linearly interpolating between two
104  /// other vectors.
105  ///
106  inline static basic_vec2 lerp(
107  const basic_vec2 & src, ///< The first vector.
108  const basic_vec2 & dst, ///< The second vector.
109  const value_type percent) ///< The percentage.
110  {
111  return src + (dst - src) * percent;
112  }
113 
114  ///
115  /// \return The maximum of two vectors.
116  ///
117  inline static basic_vec2 max(
118  const basic_vec2 & a, ///< The first vector.
119  const basic_vec2 & b) ///< The second vector.
120  {
121  return basic_vec2(std::max(a.x, b.x), std::max(a.y, b.y));
122  }
123 
124  ///
125  /// \return The minimum of two vectors.
126  ///
127  inline static basic_vec2 min(
128  const basic_vec2 & a, ///< The first vector.
129  const basic_vec2 & b) ///< The second vector.
130  {
131  return basic_vec2(std::min(a.x, b.x), std::min(a.y, b.y));
132  }
133 
134  ///
135  /// \return The normal for the specified vector.
136  ///
137  inline static basic_vec2 normalize(
138  const basic_vec2 & v) ///< The vector.
139  {
140  return v / v.get_length();
141  }
142 
143  ///
144  /// \return This instance after the operation with the specified value.
145  ///
147  const basic_vec2 & rhs) ///< The right operand.
148  {
149  x += rhs.x;
150  y += rhs.y;
151  return *this;
152  }
153 
154  ///
155  /// \return This instance after the operation with the specified value.
156  ///
158  const value_type & rhs) ///< The right operand.
159  {
160  x += rhs;
161  y += rhs;
162  return *this;
163  }
164 
165  ///
166  /// \return This instance after the operation with the specified value.
167  ///
169  const basic_vec2 & rhs) ///< The right operand.
170  {
171  x -= rhs.x;
172  y -= rhs.y;
173  return *this;
174  }
175 
176  ///
177  /// \return This instance after the operation with the specified value.
178  ///
180  const value_type & rhs) ///< The right operand.
181  {
182  x -= rhs;
183  y -= rhs;
184  return *this;
185  }
186 
187  ///
188  /// \return This instance after the operation with the specified value.
189  ///
191  const value_type & rhs) ///< The right operand.
192  {
193  x *= rhs;
194  y *= rhs;
195  return *this;
196  }
197 
198  ///
199  /// \return This instance after the operation with the specified value.
200  ///
202  const value_type & rhs) ///< The right operand.
203  {
204  x /= rhs;
205  y /= rhs;
206  return *this;
207  }
208 
209  ///
210  /// \returns A new vector based on the specified operator.
211  ///
213  const value_type & rhs) ///< The right operand.
214  const
215  {
216  return basic_vec2(*this) += rhs;
217  }
218 
219  ///
220  /// \returns A new vector based on the specified operator.
221  ///
223  const basic_vec2 & rhs) ///< The right operand.
224  const
225  {
226  return basic_vec2(*this) += rhs;
227  }
228 
229  ///
230  /// \returns A new vector based on the specified operator.
231  ///
233  const value_type & rhs) ///< The right operand.
234  const
235  {
236  return basic_vec2(*this) -= rhs;
237  }
238 
239  ///
240  /// \returns A new vector based on the specified operator.
241  ///
243  const basic_vec2 & rhs) ///< The right operand.
244  const
245  {
246  return basic_vec2(*this) -= rhs;
247  }
248 
249  ///
250  /// \returns A new vector based on the specified operator.
251  ///
253  const value_type & rhs) ///< The right operand.
254  const
255  {
256  return basic_vec2(*this) *= rhs;
257  }
258 
259  ///
260  /// \returns A new vector based on the specified operator.
261  ///
263  const value_type & rhs) ///< The right operand.
264  const
265  {
266  return basic_vec2(*this) /= rhs;
267  }
268  };
269 }
270 
271 ///
272 /// \returns A new vector based on the product of a value and another vector.
273 ///
274 template <typename TValue>
275 jade::basic_vec2<TValue> operator * (
276  const TValue & lhs, ///< The left operand.
277  const jade::basic_vec2<TValue> & rhs) ///< The left operand.
278 {
279  return rhs * lhs;
280 }
281 
282 ///
283 /// Reads the specified vector from the input stream.
284 ///
285 /// \return The input stream.
286 ///
287 template <typename TValue>
288 std::istream & operator << (
289  std::istream & lhs, ///< The input stream.
290  jade::basic_vec2<TValue> & rhs) ///< The vector.
291 {
292  return lhs >> rhs.x >> rhs.y;
293 }
294 
295 ///
296 /// Writes the specified vector to the output stream.
297 ///
298 /// \return The output stream.
299 ///
300 template <typename TValue>
301 std::ostream & operator << (
302  std::ostream & lhs, ///< The output stream.
303  const jade::basic_vec2<TValue> & rhs) ///< The vector.
304 {
305  return lhs << rhs.x << ' ' << rhs.y;
306 }
307 
308 #endif // JADE_VEC2_HPP__
jade::basic_vec2::get_length_squared
value_type get_length_squared() const
Definition: jade.vec2.hpp:97
jade::basic_vec2
A template for a class that implements rank-two vector operations.
Definition: jade.vec2.hpp:18
jade::basic_vec2::normalize
static basic_vec2 normalize(const basic_vec2 &v)
Definition: jade.vec2.hpp:137
jade::basic_vec2::operator/=
basic_vec2 & operator/=(const value_type &rhs)
Definition: jade.vec2.hpp:201
jade::basic_vec2::cross
static value_type cross(const basic_vec2 &lhs, const basic_vec2 &rhs)
Definition: jade.vec2.hpp:49
jade::basic_vec2::x
value_type x
The X coordinate.
Definition: jade.vec2.hpp:23
jade::basic_vec2::operator-=
basic_vec2 & operator-=(const basic_vec2 &rhs)
Definition: jade.vec2.hpp:168
jade::basic_vec2::basic_vec2
basic_vec2(const value_type x_, const value_type y_)
Implements a new insatnce of the class with the specified values.
Definition: jade.vec2.hpp:38
jade::basic_vec2::get_length
value_type get_length() const
Definition: jade.vec2.hpp:89
jade::basic_vec2::max
static basic_vec2 max(const basic_vec2 &a, const basic_vec2 &b)
Definition: jade.vec2.hpp:117
jade::basic_vec2::distance
static value_type distance(const basic_vec2 &a, const basic_vec2 &b)
Definition: jade.vec2.hpp:59
jade::basic_vec2::dot
static basic_vec2 dot(const basic_vec2 &a, const basic_vec2 &b)
Definition: jade.vec2.hpp:79
jade::basic_vec2::value_type
TValue value_type
The value type.
Definition: jade.vec2.hpp:21
jade::basic_vec2::operator+=
basic_vec2 & operator+=(const basic_vec2 &rhs)
Definition: jade.vec2.hpp:146
jade::basic_vec2::y
value_type y
The Y coordinate.
Definition: jade.vec2.hpp:24
jade::basic_vec2::operator+
basic_vec2 operator+(const value_type &rhs) const
Definition: jade.vec2.hpp:212
jade::basic_vec2::min
static basic_vec2 min(const basic_vec2 &a, const basic_vec2 &b)
Definition: jade.vec2.hpp:127
jade::basic_vec2::operator/
basic_vec2 operator/(const value_type &rhs) const
Definition: jade.vec2.hpp:262
jade::basic_vec2::operator*=
basic_vec2 & operator*=(const value_type &rhs)
Definition: jade.vec2.hpp:190
jade::basic_vec2::distance_squared
static value_type distance_squared(const basic_vec2 &a, const basic_vec2 &b)
Definition: jade.vec2.hpp:69
jade::basic_vec2::basic_vec2
basic_vec2()
Implements a new insatnce of the class.
Definition: jade.vec2.hpp:29
jade::basic_vec2::lerp
static basic_vec2 lerp(const basic_vec2 &src, const basic_vec2 &dst, const value_type percent)
Definition: jade.vec2.hpp:106
jade::basic_vec2::operator*
basic_vec2 operator*(const value_type &rhs) const
Definition: jade.vec2.hpp:252
jade::basic_vec2::operator-
basic_vec2 operator-(const value_type &rhs) const
Definition: jade.vec2.hpp:232