Structural deformable models
Point.h
Go to the documentation of this file.
1 /* -*- C++ -*- */
2 #ifndef _POINT2D_H_
3 #define _POINT2D_H_
4 
5 #ifdef WIN32
6 #include <windows.h>
7 #endif
8 #include <GL/gl.h>
9 #include <math.h>
10 #include <iostream>
11 //#include <cmath>
12 #include "common.h"
13 #include "mathutil.h"
14 
15 //2-dimensional point and vector
16 class Point2D {
17 public:
18  Point2D() : x(0), y(0) {};
19  Point2D(const Point2D & rhs) {
20  operator=(rhs); }
21  Point2D(float _x, float _y) : x(_x), y(_y) {}
22  Point2D(const float *val) {
23  operator=(val); }
24 
25  virtual ~Point2D() {};
26 
27  Point2D& operator=(const Point2D& rhs) {
28  x=rhs.x; y=rhs.y;
29  return *this;
30  }
31 
32  Point2D& operator=(float rhs) {
33  x = y = rhs;
34  return *this;
35  }
36 
37  Point2D& operator=(const float *rhs) {
38  this->x = rhs[0]; this->y = rhs[1];
39  return *this;
40  }
41  Point2D& operator+=(const Point2D& rhs) {
42  x+=rhs.x; y+=rhs.y;
43  return *this;
44  }
45  Point2D& operator-=(const Point2D& rhs) {
46  x-=rhs.x; y-=rhs.y;
47  return *this;
48  }
49 
50  Point2D& operator*=(const Point2D& rhs) {
51  x*=rhs.x; y*=rhs.y;
52  return *this;
53  }
54 
55  Point2D& operator*=(const float f) {
56  x*=f; y*=f;
57  return *this;
58  }
59 
60  Point2D& operator/=(const Point2D &rhs) {
61  x/=rhs.x; y/=rhs.y;
62  return *this;
63  }
64  Point2D& operator/=(const float f) {
65  x/=f; y/=f;
66  return *this;
67  }
68 
69  float operator*(const Point2D& rhs) const {
70  return x*rhs.x + y*rhs.y;
71  }
72 
73  const Point2D& const_times(const Point2D& rhs) const {
74  Point2D p(*this);
75  return p.times(rhs);
76  }
77 
78  Point2D& times(const Point2D& rhs) {
79  return *this *= rhs;
80  }
81 
82  float dot(const Point2D& rhs) const {
83  return x*rhs.x+y*rhs.y;
84  }
85  float crossZ(const Point2D& rhs) const {
86  return y*rhs.x-x*rhs.y;
87  }
88  float sinAngle(const Point2D& rhs) const {
89  // z-component of the cross product
90  return (x*rhs.y-y*rhs.x)/sqrt(norm2()*rhs.norm2());
91  }
92 
93  float angle(const Point2D& rhs=Point2D(1,0)) const {
94  float dv = crossZ(rhs);
95  float dh = dot(rhs);
96  return atan2(dv,dh);
97 #ifdef _THERE_IS_NO_ATAN2_
98  float dvp,dvn;
99  if(dv > 0.0) {
100  dvp = 1.0;
101  dvn = 0.0;
102  } else {
103  dvp = 0.0;
104  dvn = 1.0;
105  }
106  float dhn = (dh < 0) ? 1.0 : 0.0;
107  float psi;
108  if (dh == 0.0)
109  psi = M_PI_2 * (dvp + 3.0 * dvn);
110  else if (dv == 0.0)
111  psi = M_PI * dhn;
112  else
113  psi = atan(dv/dh) + M_PI * dhn * dvp
114  + (M_PI + M_PI * (dh > 0.0)) * dvn;
115  return psi * (180.0*M_1_PI);
116 #endif
117  }
118 
119  float sinAngleX() const {
120  return -y/norm();
121  }
122 
123  float angleX() const {
124  //return asin(sinAngleX());
125  return angle(Point2D(1,0));
126  }
127 
128 
129  Point2D operator*(float rhs) const {
130  Point2D p(*this);
131  p *= rhs;
132  return p;
133  }
134 
135  Point2D operator+(const Point2D &rhs) const {
136  Point2D p(*this);
137  p += rhs;
138  return p;
139  }
140 
141  Point2D operator-(const Point2D &rhs) const {
142  Point2D p(*this);
143  p -= rhs;
144  return p;
145  }
146 
147  Point2D operator/(const Point2D &rhs) const {
148  Point2D p(*this);
149  p /= rhs;
150  return p;
151  }
152 
153  Point2D operator/(float rhs) const {
154  Point2D p(*this);
155  p *= 1/rhs;
156  return p;
157  }
158 
160  float sum() const {
161  return x+y;
162  }
163 
165  float norm2() const {
166  return x*x+y*y;
167  }
168 
170  float norm() const {
171  return (float)sqrt(x*x+y*y);
172  }
173 
175  float normalize() {
176  float n = norm();
177  if (n>0.0f) operator*=(1/n);
178  return n;
179  }
180 
181  bool clamp(float x0,float y0,float x1,float y1)
182  {
183  return ::clamp(x,y,x0,y0,x1,y1);
184  }
185 
186  friend std::ostream& operator<<(std::ostream &os, const Point2D &p) {
187  os << p.x << " " << p.y;
188  return os;
189  }
190 
191  friend std::istream& operator>>(std::istream &is, Point2D &p) {
192  is >> p.x;
193  is >> p.y;
194  return is;
195  }
196 
198  void glVertex() const {
199  glVertex2f(x, y);
200  }
201 
202  const Point2D flipOrtho() const {
203  return Point2D(-y,x);
204  }
205 
206  const Point2D copyNormalized() const {
207  Point2D p(*this);
208  p.normalize();
209  return p;
210  }
211  const Point2D rotate(float angle) const {
212  float ca = cos(angle);
213  float sa = sin(angle);
214  return Point2D(x*ca-y*sa,x*sa+y*ca);
215  }
216  friend bool operator==(const Point2D& lhs, const Point2D& rhs) {
217  return lhs.x == rhs.x && lhs.y == rhs.y;
218  }
219  friend bool operator!=(const Point2D& lhs, const Point2D& rhs) {
220  return !operator==(lhs, rhs);
221  }
222 
223 public:
224  float x,y;
225 };
226 
227 //2D point with index
228 class IPoint2D : public Point2D
229 {
230 public:
231  IPoint2D() : Point2D() {};
232  IPoint2D(int _i, float _x, float _y) : Point2D(_x, _y), index(_i) {}
233  IPoint2D(int _i, const Point2D& p) {
234  operator=(p);
235  index = _i;
236  }
237  IPoint2D& operator= (const IPoint2D & rhs) {
238  Point2D::operator=(rhs);
239  index = rhs.index;
240  return *this;
241  }
242  IPoint2D& operator= (const Point2D & rhs) {
243  Point2D::operator=(rhs);
244  index = -1;
245  return *this;
246  }
247 public:
248  int index;
249 };
250 
251 typedef Point2D Point;
252 
253 #endif
Point2D & operator*=(const float f)
Definition: Point.h:55
Point2D & operator/=(const Point2D &rhs)
Definition: Point.h:60
IPoint2D(int _i, const Point2D &p)
Definition: Point.h:233
Point2D operator+(const Point2D &rhs) const
Definition: Point.h:135
IPoint2D()
Definition: Point.h:231
Point2D operator-(const Point2D &rhs) const
Definition: Point.h:141
const Point2D & const_times(const Point2D &rhs) const
Definition: Point.h:73
bool clamp(T &x, T &y, const T x0, const T y0, const T x1, const T y1)
Definition: mathutil.h:24
friend std::istream & operator>>(std::istream &is, Point2D &p)
Definition: Point.h:191
float y
Definition: Point.h:224
friend std::ostream & operator<<(std::ostream &os, const Point2D &p)
Definition: Point.h:186
float angleX() const
Definition: Point.h:123
Point2D(const Point2D &rhs)
Definition: Point.h:19
float norm() const
returns 2-norm aka length or absolute
Definition: Point.h:170
Point2D & operator*=(const Point2D &rhs)
Definition: Point.h:50
#define M_1_PI
Definition: mathutil.h:12
Point2D & operator=(float rhs)
Definition: Point.h:32
Point2D & operator=(const Point2D &rhs)
Definition: Point.h:27
Point2D Point
Definition: Point.h:251
Point2D(float _x, float _y)
Definition: Point.h:21
Point2D & operator-=(const Point2D &rhs)
Definition: Point.h:45
Point2D & operator+=(const Point2D &rhs)
Definition: Point.h:41
friend bool operator!=(const Point2D &lhs, const Point2D &rhs)
Definition: Point.h:219
virtual ~Point2D()
Definition: Point.h:25
float normalize()
normalizes the vector; returns old norm
Definition: Point.h:175
float sum() const
returns 1-norm
Definition: Point.h:160
float angle(const Point2D &rhs=Point2D(1, 0)) const
Definition: Point.h:93
Point2D operator/(float rhs) const
Definition: Point.h:153
Point2D()
Definition: Point.h:18
IPoint2D(int _i, float _x, float _y)
Definition: Point.h:232
#define M_PI
Definition: mathutil.h:9
const Point2D rotate(float angle) const
Definition: Point.h:211
Point2D operator*(float rhs) const
Definition: Point.h:129
bool clamp(float x0, float y0, float x1, float y1)
Definition: Point.h:181
const Point2D copyNormalized() const
Definition: Point.h:206
const Point2D flipOrtho() const
Definition: Point.h:202
int index
Definition: Point.h:248
float sinAngle(const Point2D &rhs) const
Definition: Point.h:88
Point2D(const float *val)
Definition: Point.h:22
Point2D & operator/=(const float f)
Definition: Point.h:64
friend bool operator==(const Point2D &lhs, const Point2D &rhs)
Definition: Point.h:216
Point2D & operator=(const float *rhs)
Definition: Point.h:37
float crossZ(const Point2D &rhs) const
Definition: Point.h:85
Point2D & times(const Point2D &rhs)
Definition: Point.h:78
float sinAngleX() const
Definition: Point.h:119
Definition: Point.h:16
float norm2() const
returns squared 2-norm
Definition: Point.h:165
float operator*(const Point2D &rhs) const
Definition: Point.h:69
float x
Definition: Point.h:224
void glVertex() const
Issue a glVertex2f call.
Definition: Point.h:198
float dot(const Point2D &rhs) const
Definition: Point.h:82
Point2D operator/(const Point2D &rhs) const
Definition: Point.h:147
DMatrix< T > & sqrt(DMatrix< T > &mat)
Definition: DMatrixUtil.h:81