Minsky
geometry.h
Go to the documentation of this file.
1 /*
2  @copyright Steve Keen 2013
3  @author Russell Standish
4  This file is part of Minsky.
5 
6  Minsky is free software: you can redistribute it and/or modify it
7  under the terms of the GNU General Public License as published by
8  the Free Software Foundation, either version 3 of the License, or
9  (at your option) any later version.
10 
11  Minsky 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
14  GNU General Public License for more details.
15 
16  You should have received a copy of the GNU General Public License
17  along with Minsky. If not, see <http://www.gnu.org/licenses/>.
18 */
19 
21 
22 #ifndef GEOMETRY_H
23 #define GEOMETRY_H
24 #include <cmath>
25 
26 #include <boost/geometry/geometries/point_xy.hpp>
27 
28 #ifndef M_PI
29 #define M_PI 3.14159265358979323846
30 #endif
31 
32 namespace minsky
33 {
34  typedef boost::geometry::model::d2::point_xy<float> Point;
35 
36  template <class T> inline T sqr(T x) {return x*x;}
37 
38 #ifndef M_PI
39  static const float M_PI = 3.1415926535f;
40 #endif
41 
44  class Rotate
45  {
46  float angle; // in radians
47  float ca, sa;
48  float x0, y0;
49  static constexpr float rad=M_PI/180.0;
50  public:
51  Rotate(float rot, float x0, float y0):
52  angle(rot*rad), ca(std::cos(angle)), sa(std::sin(angle)), x0(x0), y0(y0) {}
53  bool initialisedFrom(float rot, float x, float y) const
54  {return x==x0 && y==y0 && angle==rot*rad;}
56  Point operator()(float x1, float y1) const {
57  return Point(x(x1,y1),y(x1,y1));}
58  Point operator()(const Point& p) const {return operator()(p.x(), p.y());}
59  float x(float x, float y) const {return ca*(x-x0)-sa*(y-y0)+x0;}
60  float y(float x, float y) const {return sa*(x-x0)+ca*(y-y0)+y0;}
61  };
62 
63  // class that reflects about the vertical axis, rotated by angle
64  class Reflect
65  {
66  float xx, xy, yx, yy; // matrix components
67  float x0, y0;
68  public:
69  Reflect(float angle=0, float x0=0, float y0=1): x0(x0), y0(y0) {
70  Rotate r(angle,0,0);
71  Point c1=r(-1,0), c2=r(0,1); // columns of UR
72  Point r1=r(c1.x(), c2.x()), r2=r(c1.y(),c2.y()); // rows of (URU^-1)^T
73  xx=r1.x();
74  xy=r1.y();
75  yx=r2.x();
76  yy=r2.y();
77  }
78  float x(float x, float y) const {
79  return xx*(x-x0)+yx*(y-y0)+x0;
80  }
81  float y(float x, float y) const {
82  return xy*(x-x0)+yy*(y-y0)+y0;
83  }
84  };
85 
87  inline double clamp360(double x)
88  {
89  // adjust x to be positive before taking modulus
90  if (x<0)
91  x+=(size_t(-x)/360+1)*360;
92  return std::fmod(x,360);
93  }
94 
96  inline int quadrant(double x)
97  {
98  return int(clamp360(x+45)/90);
99  }
100 
102  inline bool flipped(double rotation)
103  {
104  const double fm=std::fmod(rotation,360);
105  return std::abs(fm)>90 && std::abs(fm)<270;
106  }
107 }
108 
109 #endif
#define M_PI
some useful geometry types, defined from boost::geometry
Definition: geometry.h:29
Expr sin(const Expr &x)
Definition: expr.h:131
Expr cos(const Expr &x)
Definition: expr.h:137
bool initialisedFrom(float rot, float x, float y) const
Definition: geometry.h:53
static constexpr float rad
Definition: geometry.h:49
Point operator()(const Point &p) const
Definition: geometry.h:58
STL namespace.
float y(float x, float y) const
Definition: geometry.h:60
Rotate(float rot, float x0, float y0)
Definition: geometry.h:51
rotate (x,y) by rot (in degrees) around the origin (x0, y0) can be used for rotating multiple points ...
Definition: geometry.h:44
float angle
Definition: geometry.h:46
Point operator()(float x1, float y1) const
rotate (x1,y1)
Definition: geometry.h:56
bool flipped(double rotation)
returns if the angle (in degrees) is in the second or third quadrant
Definition: geometry.h:102
float x(float x, float y) const
Definition: geometry.h:78
Reflect(float angle=0, float x0=0, float y0=1)
Definition: geometry.h:69
int quadrant(double x)
return quadrant x is in: 0=[-45,45),1=[45,135), etc
Definition: geometry.h:96
Creation and access to the minskyTCL_obj object, which has code to record whenever Minsky&#39;s state cha...
Definition: constMap.h:22
boost::geometry::model::d2::point_xy< float > Point
Definition: geometry.h:34
T sqr(T x)
Definition: geometry.h:36
double clamp360(double x)
return x modulo 360
Definition: geometry.h:87
float y(float x, float y) const
Definition: geometry.h:81
float x(float x, float y) const
Definition: geometry.h:59