MFMv2.0.10
Movable Feast Machine Simulator 2.0.10
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
Point.h
Go to the documentation of this file.
1 /* -*- mode:C++ -*-
2  Point.h Two-dimensional integral vector
3  Copyright (C) 2014 The Regents of the University of New Mexico. All rights reserved.
4 
5  This library is free software; you can redistribute it and/or
6  modify it under the terms of the GNU Lesser General Public
7  License as published by the Free Software Foundation; either
8  version 2.1 of the License, or (at your option) any later version.
9 
10  This library is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  Lesser General Public License for more details.
14 
15  You should have received a copy of the GNU General Public License
16  along with this library; if not, write to the Free Software
17  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301
18  USA
19 */
20 
28 #ifndef POINT_H
29 #define POINT_H
30 
31 #include <stdlib.h>
32 #include "itype.h"
33 #include "Random.h"
34 #include "Util.h" /* For MAX and MIN */
35 
36 namespace MFM
37 {
38 
44  template <class T>
45  class Point
46  {
47  private:
51  T m_x, m_y;
52 
53  public:
54 
59  Point();
60 
69  Point(T x, T y);
70 
77  Point(const Point<T> & p);
78 
90  Point(Random& random, T maxX, T maxY);
91 
95  ~Point() { }
96 
102  T GetX() const;
103 
109  T GetY() const;
110 
120  u32 GetManhattanLength() const;
121 
128  u32 GetMaximumLength() const;
129 
136  double GetEuclideanLength() const;
137 
141  void Clear()
142  {
143  m_x = m_y = 0;
144  }
145 
152  void Add(const Point<T>& offset);
153 
162  void Add(T x, T y);
163 
170  void Subtract(const Point<T>& offset);
171 
180  void Subtract(T x, T y);
181 
189  void Multiply(T scalar);
190 
198  void Divide(T scalar);
199 
207  void Mod(T scalar);
208 
214  void SetX(T x);
215 
221  void SetY(T y);
222 
230  void Set(T x, T y);
231 
238  void Set(const Point<T>& copyFrom);
239 
248  bool Equals(const Point<T>& rhs) const;
249 
260  bool BoundedBelow(const Point<T>& bound) const;
261 
272  bool BoundedAbove(const Point<T>& bound) const;
273 
293  bool BoundedBy(const Point<T>& lowerBound,const Point<T>& upperBound) const;
294 
302  bool IsZero() const
303  {
304  return m_x == 0 && m_y == 0;
305  }
306 
315  void Parse(char* buffer);
316 
329  friend Point<T> max(const Point<T> & lhs,const Point<T> & rhs)
330  {
331  T x = MAX(lhs.m_x, rhs.m_x);
332  T y = MAX(lhs.m_y, rhs.m_y);
333  return Point<T>(x,y);
334  }
335 
348  friend Point<T> min(const Point<T> & lhs,const Point<T> & rhs)
349  {
350  T x = MIN(lhs.m_x, rhs.m_x);
351  T y = MIN(lhs.m_y, rhs.m_y);
352  return Point<T>(x,y);
353  }
354 
355 
357 
365  Point<T>& operator=(const Point<T> & rhs);
366 
376  {
377  Add(rhs);
378  return *this;
379  }
380 
390  {
391  Subtract(rhs);
392  return *this;
393  }
394 
404  Point<T>& operator*=(const s32 rhs)
405  {
406  Multiply(rhs);
407  return *this;
408  }
409 
419  Point<T>& operator/=(const s32 rhs)
420  {
421  Divide(rhs);
422  return *this;
423  }
424 
436  friend Point<T> operator+(const Point<T> & lhs,const Point<T> & rhs)
437  {
438  Point<T> ret(lhs);
439  ret.Add(rhs);
440  return ret;
441  }
442 
456  friend Point<T> operator-(const Point<T> & lhs,const Point<T> & rhs)
457  {
458  Point<T> ret(lhs);
459  ret.Subtract(rhs);
460  return ret;
461  }
462 
474  friend bool operator==(const Point<T> & lhs,const Point<T> & rhs)
475  {
476  return lhs.Equals(rhs);
477  }
478 
490  friend bool operator!=(const Point<T> & lhs,const Point<T> & rhs)
491  {
492  return !(lhs==rhs);
493  }
494 
508  friend Point<T> operator*(const Point<T> & lhs,const s32 rhs)
509  {
510  Point<T> ret(lhs);
511  ret.Multiply(rhs);
512  return ret;
513  }
514 
526  friend Point<T> operator/(const Point<T> & lhs,const s32 rhs)
527  {
528  Point<T> ret(lhs);
529  ret.Divide(rhs);
530  return ret;
531  }
532 
545  friend Point<T> operator%(const Point<T> & lhs,const s32 rhs)
546  {
547  Point<T> ret(lhs);
548  ret.Mod(rhs);
549  return ret;
550  }
551  };
552 
561  typedef Point<s32> SPoint;
562 
566  typedef Point<u32> UPoint;
567 
571  typedef Point <s16> SSPoint;
572 
576  typedef Point <u16> USPoint;
577 
587  UPoint MakeUnsigned(const SPoint & spoint);
588 
596  inline bool CanMakeUnsigned(const SPoint & spoint)
597  {
598  return spoint.GetX() >= 0 && spoint.GetY() >= 0;
599  }
600 
611  SPoint MakeSigned(const UPoint & upoint);
612 
620  inline bool CanMakeSigned(const UPoint & upoint)
621  {
622  return upoint.GetX() <= (u32) S32_MAX && upoint.GetY() <= (u32) S32_MAX;
623  }
624 
625 } /* namespace MFM */
626 
627 #include "Point.tcc"
628 
629 #endif /*POINT_H*/
Point< T > & operator+=(const Point< T > &rhs)
Definition: Point.h:375
void Multiply(T scalar)
Definition: Point.tcc:92
Definition: Random.h:45
void Mod(T scalar)
Definition: Point.tcc:106
friend Point< T > min(const Point< T > &lhs, const Point< T > &rhs)
Definition: Point.h:348
void Divide(T scalar)
Definition: Point.tcc:99
void Clear()
Definition: Point.h:141
bool Equals(const Point< T > &rhs) const
Definition: Point.tcc:205
bool BoundedBelow(const Point< T > &bound) const
Definition: Point.tcc:211
bool BoundedBy(const Point< T > &lowerBound, const Point< T > &upperBound) const
Definition: Point.tcc:223
void Set(T x, T y)
Definition: Point.tcc:183
~Point()
Definition: Point.h:95
Point< T > & operator*=(const s32 rhs)
Definition: Point.h:404
T GetY() const
Definition: Point.tcc:40
friend bool operator!=(const Point< T > &lhs, const Point< T > &rhs)
Definition: Point.h:490
friend bool operator==(const Point< T > &lhs, const Point< T > &rhs)
Definition: Point.h:474
Point()
Definition: Point.tcc:10
u32 GetManhattanLength() const
Definition: Point.tcc:46
friend Point< T > operator*(const Point< T > &lhs, const s32 rhs)
Definition: Point.h:508
void Subtract(const Point< T > &offset)
Definition: Point.tcc:78
u32 GetMaximumLength() const
Definition: Point.tcc:52
void SetX(T x)
Definition: Point.tcc:164
void Add(const Point< T > &offset)
Definition: Point.tcc:64
friend Point< T > operator/(const Point< T > &lhs, const s32 rhs)
Definition: Point.h:526
friend Point< T > operator+(const Point< T > &lhs, const Point< T > &rhs)
Definition: Point.h:436
bool BoundedAbove(const Point< T > &bound) const
Definition: Point.tcc:217
bool IsZero() const
Definition: Point.h:302
Point< T > & operator/=(const s32 rhs)
Definition: Point.h:419
friend Point< T > operator%(const Point< T > &lhs, const s32 rhs)
Definition: Point.h:545
void Parse(char *buffer)
Definition: Point.tcc:145
#define S32_MAX
Definition: itype.h:86
void SetY(T y)
Definition: Point.tcc:170
double GetEuclideanLength() const
Definition: Point.tcc:58
friend Point< T > operator-(const Point< T > &lhs, const Point< T > &rhs)
Definition: Point.h:456
Point< T > & operator=(const Point< T > &rhs)
Definition: Point.tcc:197
friend Point< T > max(const Point< T > &lhs, const Point< T > &rhs)
Definition: Point.h:329
Definition: Point.h:45
Point< T > & operator-=(const Point< T > &rhs)
Definition: Point.h:389
T GetX() const
Definition: Point.tcc:34