Tactics: Western Philosophers Vs. Musicians  0.12
A turn-based tactical game combining rules and gameplay elements inspired by Final Fantasy Tactics and the Mayfair Exponential Game System. Unlike most games of this type, motion is in full, grid-less 3D.
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
MPoint.h
Go to the documentation of this file.
1 // Copyright (C) 2004-2012 Dylan Blair
3 //
4 // email: dblair@alumni.cs.utexas.edu
5 //
6 // This library is free software; you can redistribute it and/or
7 // modify it under the terms of the GNU Lesser General Public
8 // License as published by the Free Software Foundation; either
9 // version 2.1 of the License, or (at your option) any later version.
10 //
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 // Lesser General Public License for more details.
15 //
16 // You should have received a copy of the GNU Lesser General Public
17 // License along with this library; if not, write to the Free Software
18 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 
21 #ifndef MPOINT_H
22 #define MPOINT_H
23 
24 #include "MFun.h"
25 
26 #include <string.h> //for memset()
27 #include <algorithm> //for max()
28 
29 namespace OpenSkyNet {
30  namespace Math {
32  template<class T=float, unsigned char U=3>
33  class Point {
34  public:
36 
37  Point(const T& x_=0, const T& y_=0, const T& z_=0) {
38  assert(U >= 1);
39  memset(_dimensions,0,sizeof(T)*U);
40  switch (U) {
41  case 1:
42  _dimensions[0] = x_;
43  break;
44  case 2:
45  _dimensions[0] = x_;
46  _dimensions[1] = y_;
47  break;
48  default:
49  _dimensions[0] = x_;
50  _dimensions[1] = y_;
51  _dimensions[2] = z_;
52  break;
53  }
54  }
55 
56  Point(const Point<T>& p_) {
57  for (unsigned char i = 0; i < U; ++i)
58  _dimensions[i] = p_._dimensions[i];
59  }
60 
63  inline unsigned char getNumDimensions() const { return U; }
64  inline T& x() { return _dimensions[0]; }
65  inline T& y() { return _dimensions[1]; }
66  inline T& z() { return _dimensions[2]; }
67  inline T& r() { return _dimensions[0]; }
68  inline T& g() { return _dimensions[1]; }
69  inline T& b() { return _dimensions[2]; }
70  inline T& a() { return _dimensions[3]; }
71  inline T& operator[](int i_) { return _dimensions[i_]; }
72  inline const T& x() const { return _dimensions[0]; }
73  inline const T& y() const { return _dimensions[1]; }
74  inline const T& z() const { return _dimensions[2]; }
75  inline const T& r() const { return _dimensions[0]; }
76  inline const T& g() const { return _dimensions[1]; }
77  inline const T& b() const { return _dimensions[2]; }
78  inline const T& a() const { return _dimensions[3]; }
79  inline const T& operator[](int i_) const { return _dimensions[i_]; }
81 
85  inline Point<T> operator-() const {
86  Point<T> result(*this);
87  for (unsigned char i = 0; i < U; ++i)
88  result._dimensions[i] = -result._dimensions[i];
89  return result;
90  }
91 
93  inline Point<T> getAbs() const {
94  Point<T> result(*this);
95  for (unsigned char i = 0; i < U; ++i)
96  if (result._dimensions[i] < 0)
97  result._dimensions[i] = -result._dimensions[i];
98  return result;
99  }
100 
102  inline T getComponentSum() const {
103  T sum = 0;
104  for (unsigned char i = 0; i < U; ++i)
105  sum += _dimensions[i];
106  return sum;
107  }
108 
110  inline T getMax() const {
111  T max = _dimensions[0];
112  for (unsigned char i = 1; i < U; ++i)
113  if (_dimensions[i] > max)
114  max = _dimensions[i];
115  return max;
116  }
117 
119  inline T getMin() const {
120  T min = _dimensions[0];
121  for (unsigned char i = 1; i < U; ++i)
122  if (_dimensions[i] < min)
123  min = _dimensions[i];
124  return min;
125  }
126 
128  inline T getLenSqrd() const {
129  T length = 0;
130  for (unsigned char i = 0; i < U; ++i)
131  length += _dimensions[i]*_dimensions[i];
132  return length;
133  }
134 
136  inline T getLength() const { return (static_cast<T>(sqrt(getLenSqrd()))); }
137 
139  inline void normalize() {
140  T lensqrd = getLenSqrd();
141  if (lensqrd) {
142  T len = static_cast<T>(sqrt(lensqrd));
143  for (unsigned char i = 0; i < U; ++i)
144  _dimensions[i] /= len;
145  }
146  }
148 
152  inline bool equals(const Point<T>& rhs_, T radiusSqrd_) const {
153  if ((rhs_ - *this).getLenSqrd() > radiusSqrd_)
154  return false;
155  return true;
156  }
157 
159  inline Point<T,U>& operator=(const Point<T,U>& rhs_) {
160  for (unsigned char i = 0; i < U; ++i)
161  _dimensions[i] = rhs_._dimensions[i];
162  return *this;
163  }
164 
166  inline Point<T>& operator+=(const Point<T>& rhs_) {
167  for (unsigned char i = 0; i < U; ++i)
168  _dimensions[i] = _dimensions[i] + rhs_._dimensions[i];
169  return *this;
170  }
171 
173  inline Point<T>& operator+=(const T& rhs_) {
174  for (unsigned char i = 0; i < U; ++i)
175  _dimensions[i] = _dimensions[i] + rhs_;
176  return *this;
177  }
178 
180  inline Point<T>& operator-=(const Point<T>& rhs_) { return (*this)+=(-rhs_); }
181 
183  inline Point<T>& operator-=(const T& rhs_) {
184  for (unsigned char i = 0; i < U; ++i)
185  _dimensions[i] = _dimensions[i] - rhs_;
186  return *this;
187  }
188 
190  inline Point<T>& operator*=(const T& rhs_) {
191  for (unsigned char i = 0; i < U; ++i)
192  _dimensions[i] = _dimensions[i] * rhs_;
193  return *this;
194  }
195 
197  inline Point<T> getCross3(const Point<T>& rhs_) const {
198  return Point<T>(y()*rhs_.z() - z()*rhs_.y(), z()*rhs_.x() - x()*rhs_.z(), x()*rhs_.y() - y()*rhs_.x());
199  }
200 
202  inline T getDot3(const Point<T>& rhs_) const {
203  return (x()*rhs_.x() + y()*rhs_.y() + z()*rhs_.z());
204  }
205 
207  inline Point<T> getReflection3(const Point<T>& n_) const {
208  Point<T> projection(n_ * getDot3(n_));
209  return *this - (projection * 2.f);
210  }
212  };
213 
214  extern const Math::Point<> g_origin;
215  extern const Math::Point<> g_xAxis;
216  extern const Math::Point<> g_yAxis;
217  extern const Math::Point<> g_zAxis;
218 
220  template<class T, class U>
221  inline bool operator==(const Point<T>& lhs_, const Point<U>& rhs_) {
222  bool result = true;
223  for (unsigned char i = 0; i < lhs_.getNumDimensions(); ++i) {
224  if (lhs_._dimensions[i] != rhs_._dimensions[i]) {
225  result = false;
226  break;
227  }
228  }
229  return result;
230  }
231 
233  template<class T, class U>
234  inline bool operator!=(const Point<T>& lhs_, const Point<U>& rhs_) { return (!(lhs_ == rhs_)); }
235 
237  template<class T, class U>
238  inline Point<T> operator+(const Point<T>& lhs_, const Point<U>& rhs_) {
239  unsigned char dimensToAdd = lhs_.getNumDimensions();
240  Point<T> result;
241  for (unsigned char i = 0; i < dimensToAdd; ++i)
242  result._dimensions[i] = lhs_._dimensions[i] + rhs_._dimensions[i];
243  return result;
244  }
245 
247  template<class T, class U>
248  inline Point<T> operator-(const Point<T>& lhs_, const Point<U>& rhs_) { return (lhs_ + -rhs_); }
249 
251  template<class T, class U>
252  inline Point<T> operator*(const Point<T>& lhs_, const Point<U>& rhs_) {
253  unsigned char dimensToMul = lhs_.getNumDimensions();
254  Point<T> result;
255  for (unsigned char i = 0; i < dimensToMul; ++i)
256  result._dimensions[i] = lhs_._dimensions[i] * rhs_._dimensions[i];
257  return result;
258  }
259 
261  template<class T, class U>
262  inline Point<T> operator/(const Point<T>& lhs_, const Point<U>& rhs_) {
263  unsigned char dimensToDiv = lhs_.getNumDimensions();
264  Point<T> result;
265  for (unsigned char i = 0; i < dimensToDiv; ++i)
266  result._dimensions[i] = lhs_._dimensions[i] / rhs_._dimensions[i];
267  return result;
268  }
269 
271  template<class T, class U>
272  inline Point<T> operator*(const Point<T>& lhs_, const U& rhs_) {
273  Point<T> result;
274  for (unsigned char i = 0; i < lhs_.getNumDimensions(); ++i)
275  result._dimensions[i] = lhs_._dimensions[i] * rhs_;
276  return result;
277  }
278 
280  template<class T, class U>
281  inline Point<T> operator/(const Point<T>& lhs_, const U& rhs_) {
282  Point<T> result;
283  for (unsigned char i = 0; i < lhs_.getNumDimensions(); ++i)
284  result._dimensions[i] = lhs_._dimensions[i] / rhs_;
285  return result;
286  }
287 
289  template<class T, class U>
290  inline T getLength(const Point<T>& lhs_, const Point<U>& rhs_) { return (rhs_ - lhs_).getLength(); }
291 
297  template<class T, class U>
298  inline Point<T> interpolate(const Point<T>& lhs_, const Point<U>& rhs_, float amount_) {
299  Math::clamp<float>(amount_);
300  return Point<T>((lhs_ * (1.0f - amount_)) + (rhs_ * amount_));
301  }
302 
310  inline Point<float,2> RandPointInOrOnCircle(float radius_) {
311  Point<float,2> p;
312  const float radiusSqrd = radius_ * radius_;
313 
314  //the probability of needing more than k tries is (1-PI/4)^k. For k=20, this is ~ 0.00000000000004
315  for (int n = 0; n < 20; ++n) {
316  p.x() = radius_ * randNeg1Pos1();
317  p.y() = radius_ * randNeg1Pos1();
318  if (p.getLenSqrd() <= radiusSqrd)
319  return p;
320  }
321 
322  float rads = 2.0f * PI * rand01();
323  float dist = rand01() + rand01();
324  if (dist > 1.f)
325  dist = 2.f - dist;
326 
327  p.x() = radius_ * dist * cosf(rads);
328  p.y() = radius_ * dist * sinf(rads);
329 
330  return p;
331  }
332  }
333 }
334 
335 #endif //MPOINT_H
const T & r() const
Definition: MPoint.h:75
T getComponentSum() const
Definition: MPoint.h:102
const T & b() const
Definition: MPoint.h:77
T getMin() const
Definition: MPoint.h:119
Matrix3x3 operator*(const Matrix3x3 &lhs_, const Matrix3x3 &rhs_)
Definition: MMatrix3x3.h:208
Commmon math functions and constants.
T getLength() const
Definition: MPoint.h:136
const T & z() const
Definition: MPoint.h:74
void normalize()
Definition: MPoint.h:139
Point< T > getAbs() const
Definition: MPoint.h:93
Point< T, U > & operator=(const Point< T, U > &rhs_)
Definition: MPoint.h:159
const T & a() const
Definition: MPoint.h:78
T & y()
Definition: MPoint.h:65
T getLenSqrd() const
Definition: MPoint.h:128
T & x()
Definition: MPoint.h:64
Point< float, 2 > RandPointInOrOnCircle(float radius_)
Definition: MPoint.h:310
Point< T > & operator+=(const Point< T > &rhs_)
Definition: MPoint.h:166
T & operator[](int i_)
Definition: MPoint.h:71
const Point g_yAxis(0.0f, 1.0f, 0.0f)
Definition: MPoint.h:216
Point< T > operator-() const
Definition: MPoint.h:85
Point(const Point< T > &p_)
Definition: MPoint.h:56
const T & operator[](int i_) const
Definition: MPoint.h:79
Point< T > & operator-=(const T &rhs_)
Definition: MPoint.h:183
const T & x() const
Definition: MPoint.h:72
bool operator==(const Point< T > &lhs_, const Point< U > &rhs_)
Definition: MPoint.h:221
const Point g_origin
Definition: MPoint.cpp:5
Point< T > & operator-=(const Point< T > &rhs_)
Definition: MPoint.h:180
Point< T > & operator*=(const T &rhs_)
Definition: MPoint.h:190
T & r()
Definition: MPoint.h:67
bool equals(const Point< T > &rhs_, T radiusSqrd_) const
Definition: MPoint.h:152
bool operator!=(const Point< T > &lhs_, const Point< U > &rhs_)
Definition: MPoint.h:234
Point< T > operator+(const Point< T > &lhs_, const Point< U > &rhs_)
Definition: MPoint.h:238
Point< T > operator-(const Point< T > &lhs_, const Point< U > &rhs_)
Definition: MPoint.h:248
T & a()
Definition: MPoint.h:70
T & z()
Definition: MPoint.h:66
unsigned char getNumDimensions() const
Definition: MPoint.h:63
Definition: MPoint.h:33
const T & g() const
Definition: MPoint.h:76
Point< T > getReflection3(const Point< T > &n_) const
Definition: MPoint.h:207
const Point g_xAxis(1.0f, 0.0f, 0.0f)
Definition: MPoint.h:215
T getLength(const Point< T > &lhs_, const Point< U > &rhs_)
Definition: MPoint.h:290
Point< T > interpolate(const Point< T > &lhs_, const Point< U > &rhs_, float amount_)
Definition: MPoint.h:298
T & b()
Definition: MPoint.h:69
const T & y() const
Definition: MPoint.h:73
const Point g_zAxis(0.0f, 0.0f, 1.0f)
Definition: MPoint.h:217
float randNeg1Pos1()
Definition: MFun.h:60
T getDot3(const Point< T > &rhs_) const
Definition: MPoint.h:202
Point< T > getCross3(const Point< T > &rhs_) const
Definition: MPoint.h:197
Point< T > & operator+=(const T &rhs_)
Definition: MPoint.h:173
const float PI(3.14159265f)
T & g()
Definition: MPoint.h:68
Point< T > operator/(const Point< T > &lhs_, const Point< U > &rhs_)
Definition: MPoint.h:262
Point(const T &x_=0, const T &y_=0, const T &z_=0)
Definition: MPoint.h:37
T _dimensions[U]
Definition: MPoint.h:35
T getMax() const
Definition: MPoint.h:110
float rand01()
Definition: MFun.h:57