SGSolve
sgpoint.hpp
1 // This file is part of the SGSolve library for stochastic games
2 // Copyright (C) 2019 Benjamin A. Brooks
3 //
4 // SGSolve free software: you can redistribute it and/or modify it
5 // under the terms of the GNU General Public License as published by
6 // the Free Software Foundation, either version 3 of the License, or
7 // (at your option) any later version.
8 //
9 // SGSolve is distributed in the hope that it will be useful, but
10 // WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 // General Public License for more details.
13 //
14 // You should have received a copy of the GNU General Public License
15 // along with this program. If not, see
16 // <http://www.gnu.org/licenses/>.
17 //
18 // Benjamin A. Brooks
19 // ben@benjaminbrooks.net
20 // Chicago, IL
21 
22 // Classes for storing data for stochastic games.
23 // BAB 9-25-2012
24 #ifndef _SGPOINT_HPP
25 #define _SGPOINT_HPP
26 
27 #include "sgcommon.hpp"
28 
30 
34 class SGPoint
35 {
36 protected:
37  vector<double> x;
39 public:
41  SGPoint(): x(2,0.0) {}
43  SGPoint(double _x): x(2,_x) {}
45  SGPoint(int n): x(n,0) {}
47  SGPoint(int n, double _x): x(n,_x) {}
49  SGPoint(vector<double> _x)
50  {
51  x = _x;
52  }
54  SGPoint(double _x0, double _x1): x(2,0.0)
55  { x[0] = _x0; x[1] = _x1; }
56 
59 
61  int size () const {return x.size();}
63  SGPoint getNormal() const;
65  double norm() const {return std::sqrt((*this)*(*this));}
67  double angle(const SGPoint& base) const;
69  static double distance(const SGPoint& p0,
70  const SGPoint& p1);
72  static SGPoint cross(const SGPoint& p0,
73  const SGPoint& p1);
75  bool rotateCCW(double pi);
77  bool rotateCW(double pi) {return rotateCCW(-pi);}
79  bool normalize();
81  bool anyNaN() const;
82 
84  void roundPoint(double tol);
85 
87  void max(const SGPoint & p);
89  void min(const SGPoint & p);
90 
92  void plusWithWeight(const SGPoint & p,double w);
93 
94  // Operators
96  inline double& operator[](int player)
97  {
98  // // player = player % x.size(); // Wrap around
99  // if(player < 0 || player >= x.size())
100  // throw(SGException(SG::OUT_OF_BOUNDS));
101 
102  return x[player];
103  }
105  inline const double& operator[](int player) const
106  {
107  // // player = player % x.size(); // Wrap around
108  // if(player < 0 || player >= x.size())
109  // throw(SGException(SG::OUT_OF_BOUNDS));
110 
111  return x[player];
112  }
114  SGPoint& operator=(const SGPoint & rhs);
116  SGPoint& operator=(double d);
118  SGPoint& operator+=(const SGPoint & rhs);
120  SGPoint& operator-=(const SGPoint & rhs);
122  SGPoint& operator-=(double d);
124  SGPoint& operator*=(double d);
126  SGPoint& operator/=(double d);
128  const SGPoint operator+(const SGPoint & rhs) const;
130  const SGPoint operator-(const SGPoint & rhs) const;
132  const SGPoint operator-(double d) const;
134  friend SGPoint operator*(double d,const SGPoint & point);
136  friend SGPoint operator*(const SGPoint & point,double d);
138  friend SGPoint operator/(const SGPoint & point,double d);
140  double operator*(const SGPoint & rhs) const; // dot product
142  bool operator==(const SGPoint & rhs) const;
144  bool operator!=(const SGPoint & rhs) const;
146  bool operator>=(const SGPoint & rhs) const;
148  bool operator>(const SGPoint & rhs) const;
150  bool operator<=(const SGPoint & rhs) const;
152  bool operator<(const SGPoint & rhs) const;
154  bool operator>=(double rhs) const;
156  bool operator>(double rhs) const;
158  bool operator<=(double rhs) const;
160  bool operator<(double rhs) const;
162  friend ostream& operator<<(ostream& out, const SGPoint& rhs);
163 
165 
167  static double invertSystem(SGPoint& x, const SGPoint&b,
168  const SGPoint& a0, const SGPoint& a1);
170 
173  static double intersectRay(SGPoint& intersection,
174  SGPoint& weights,
175  const SGPoint& pivot,
176  const SGPoint& direction,
177  const SGPoint& t0,
178  const SGPoint& t1);
180 
183  static double signedArea(const SGPoint& p0,
184  const SGPoint& p1,
185  const SGPoint& p2);
186 
188  template<class Archive>
189  void serialize(Archive &ar, const unsigned int version)
190  {
191  ar & x;
192  }
193 
194  friend class boost::serialization::access;
195  friend class SGTuple;
196 }; // SGPoint
197 
198 #endif
SGPoint::distance
static double distance(const SGPoint &p0, const SGPoint &p1)
Calculates distance between p0 and p1 in the sup-norm.
Definition: sgpoint.cpp:96
SGPoint::operator-=
SGPoint & operator-=(const SGPoint &rhs)
Augmented subtraction.
Definition: sgpoint.cpp:184
SGPoint::operator/
friend SGPoint operator/(const SGPoint &point, double d)
Right scalar division.
Definition: sgpoint.cpp:233
SGPoint::serialize
void serialize(Archive &ar, const unsigned int version)
Serialize an SGPoint.
Definition: sgpoint.hpp:189
SGPoint::operator[]
double & operator[](int player)
Access operator.
Definition: sgpoint.hpp:96
SGPoint::angle
double angle(const SGPoint &base) const
Returns the angle in radians relative to base.
Definition: sgpoint.cpp:43
SGPoint::operator/=
SGPoint & operator/=(double d)
Augmented scalar division.
Definition: sgpoint.cpp:206
SGPoint::operator*=
SGPoint & operator*=(double d)
Augmented scalar multiplication.
Definition: sgpoint.cpp:199
SGPoint::size
int size() const
Returns the number of coordinates.
Definition: sgpoint.hpp:61
SGPoint::SGPoint
SGPoint()
Default constructor that sets vector equal to zero.
Definition: sgpoint.hpp:41
SGPoint::SGPoint
SGPoint(int n)
Creates an n dimensional zero vector.
Definition: sgpoint.hpp:45
SGPoint::operator<
bool operator<(const SGPoint &rhs) const
Strictly less.
Definition: sgpoint.cpp:275
SGPoint::getNormal
SGPoint getNormal() const
Returns the counter-clockwise normal vector.
Definition: sgpoint.cpp:37
SGPoint::max
void max(const SGPoint &p)
Takes the pointwise minimum with another SGPoint.
Definition: sgpoint.cpp:119
SGPoint::intersectRay
static double intersectRay(SGPoint &intersection, SGPoint &weights, const SGPoint &pivot, const SGPoint &direction, const SGPoint &t0, const SGPoint &t1)
Calculates intersection of a segment with a ray.
SGPoint::SGPoint
SGPoint(vector< double > _x)
Creates an SGPoint from the two-vector _x.
Definition: sgpoint.hpp:49
SGPoint::signedArea
static double signedArea(const SGPoint &p0, const SGPoint &p1, const SGPoint &p2)
Calculates determinant.
Definition: sgpoint.cpp:332
SGPoint::norm
double norm() const
Returns the Euclidean norm.
Definition: sgpoint.hpp:65
SGPoint::operator-
const SGPoint operator-(const SGPoint &rhs) const
Vector subtraction.
Definition: sgpoint.cpp:219
SGPoint::rotateCW
bool rotateCW(double pi)
Rotates the point clockwise by pi radians.
Definition: sgpoint.hpp:77
SGPoint::invertSystem
static double invertSystem(SGPoint &x, const SGPoint &b, const SGPoint &a0, const SGPoint &a1)
Inverts a linear system.
Definition: sgpoint.cpp:315
SGPoint::operator+
const SGPoint operator+(const SGPoint &rhs) const
Vector addition.
Definition: sgpoint.cpp:216
SGPoint::operator*
friend SGPoint operator*(double d, const SGPoint &point)
Left-scalar multiplication.
Definition: sgpoint.cpp:227
SGPoint::operator[]
const double & operator[](int player) const
Constant access operator.
Definition: sgpoint.hpp:105
SGPoint::operator>
bool operator>(const SGPoint &rhs) const
Strictly greater.
Definition: sgpoint.cpp:265
SGPoint::roundPoint
void roundPoint(double tol)
Rounds off significant digits smaller than tol.
Definition: sgpoint.cpp:340
SGPoint::cross
static SGPoint cross(const SGPoint &p0, const SGPoint &p1)
Cross product for 3-vectors.
Definition: sgpoint.cpp:106
SGPoint::min
void min(const SGPoint &p)
Takes the pointwise minimum with another SGPoint.
Definition: sgpoint.cpp:126
SGPoint::operator<=
bool operator<=(const SGPoint &rhs) const
Less than or equal.
Definition: sgpoint.cpp:267
SGPoint::normalize
bool normalize()
Normalizes so that the norm is one.
Definition: sgpoint.cpp:82
SGPoint::x
vector< double > x
Definition: sgpoint.hpp:37
SGPoint
A vector in .
Definition: sgpoint.hpp:35
SGPoint::rotateCCW
bool rotateCCW(double pi)
Rotates the point clockwise by pi radians.
Definition: sgpoint.cpp:73
SGPoint::anyNaN
bool anyNaN() const
Checks if any of the entries are NaN.
Definition: sgpoint.cpp:27
SGPoint::operator==
bool operator==(const SGPoint &rhs) const
Equality.
Definition: sgpoint.cpp:244
SGPoint::SGPoint
SGPoint(double _x0, double _x1)
Creates an SGPoint with elements x and y.
Definition: sgpoint.hpp:54
SGPoint::SGPoint
SGPoint(int n, double _x)
Creates an n dimensional zero vector.
Definition: sgpoint.hpp:47
SGPoint::operator=
SGPoint & operator=(const SGPoint &rhs)
Assignment operator.
Definition: sgpoint.cpp:159
SGPoint::operator<<
friend ostream & operator<<(ostream &out, const SGPoint &rhs)
Output SGPoint to ostream.
Definition: sgpoint.cpp:296
SGPoint::~SGPoint
~SGPoint()
Destructor.
Definition: sgpoint.hpp:58
SGPoint::operator>=
bool operator>=(const SGPoint &rhs) const
Greater than or equal.
Definition: sgpoint.cpp:257
SGPoint::operator+=
SGPoint & operator+=(const SGPoint &rhs)
Augmented addition.
Definition: sgpoint.cpp:176
SGPoint::plusWithWeight
void plusWithWeight(const SGPoint &p, double w)
Plus with weight.
Definition: sgpoint.cpp:133
SGPoint::SGPoint
SGPoint(double _x)
Sets both elements of the vector equal to x.
Definition: sgpoint.hpp:43
SGPoint::operator!=
bool operator!=(const SGPoint &rhs) const
Not equls.
Definition: sgpoint.cpp:254
SGTuple
Definition: sgtuple.hpp:52