GRALE
vector2d.h
Go to the documentation of this file.
1 /*
2 
3  This file is a part of GRALE, a library to facilitate the simulation
4  and inversion of gravitational lenses.
5 
6  Copyright (C) 2008-2012 Jori Liesenborgs
7 
8  Contact: jori.liesenborgs@gmail.com
9 
10  This program is free software; you can redistribute it and/or modify
11  it under the terms of the GNU General Public License as published by
12  the Free Software Foundation; either version 2 of the License, or
13  (at your option) any later version.
14 
15  This program is distributed in the hope that it will be useful,
16  but WITHOUT ANY WARRANTY; without even the implied warranty of
17  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18  GNU General Public License for more details.
19 
20  You should have received a copy of the GNU General Public License
21  along with this program; if not, write to the Free Software
22  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
23 
24 */
25 
30 #ifndef GRALE_VECTOR2D_H
31 
32 #define GRALE_VECTOR2D_H
33 
34 #include "realtype.h"
35 
36 namespace grale
37 {
38 
40 template <class T>
41 class Vector2D
42 {
43 public:
44  Vector2D() { m_pos[0] = 0; m_pos[1] = 0; }
45  Vector2D(T x, T y) { m_pos[0] = x; m_pos[1] = y; }
46  T getX() const { return m_pos[0]; }
47  T getY() const { return m_pos[1]; }
48  T getLength() const
49  {
50  T absX = ABS(m_pos[0]);
51  T absY = ABS(m_pos[1]);
52 
53  if (absX > absY)
54  {
55  T tmp = absY/absX;
56  return absX*SQRT((T)1.0+tmp*tmp);
57  }
58  // absX <= absY
59  if (absY == 0) // => absx == 0
60  return 0;
61  T tmp = absX/absY;
62  return absY*SQRT(tmp*tmp+(T)1.0);
63  }
64  T getLengthSquared() const { return m_pos[0]*m_pos[0]+m_pos[1]*m_pos[1]; }
65  T getComponent(int i) const { if (i == 1) return m_pos[0]; return m_pos[1]; }
66  Vector2D &operator+=(const Vector2D v) { m_pos[0] += v.m_pos[0]; m_pos[1] += v.m_pos[1]; return *this; }
67  Vector2D &operator-=(const Vector2D v) { m_pos[0] -= v.m_pos[0]; m_pos[1] -= v.m_pos[1]; return *this; }
68  Vector2D &operator*=(T a) { m_pos[0] *= a; m_pos[1] *= a; return *this; }
69  Vector2D &operator/=(T a) { m_pos[0] /= a; m_pos[1] /= a; return *this; }
70  const T *getComponents() const { return m_pos; }
71  T *getComponents() { return m_pos; }
72 private:
73  T m_pos[2];
74 };
75 
76 template <class T>
77 inline Vector2D<T> operator+(Vector2D<T> v1, Vector2D<T> v2)
78 {
79  return Vector2D<T>(v1.getX()+v2.getX(), v1.getY()+v2.getY());
80 }
81 
82 template <class T>
83 inline Vector2D<T> operator-(Vector2D<T> v1, Vector2D<T> v2)
84 {
85  return Vector2D<T>(v1.getX()-v2.getX(), v1.getY()-v2.getY());
86 }
87 
88 template <class T>
89 inline Vector2D<T> operator*(Vector2D<T> v, T m)
90 {
91  return Vector2D<T>(v.getX()*m, v.getY()*m);
92 }
93 
94 template <class T>
95 inline Vector2D<T> operator*(T m, Vector2D<T> v)
96 {
97  return Vector2D<T>(v.getX()*m, v.getY()*m);
98 }
99 
100 template <class T>
101 inline Vector2D<T> operator/(Vector2D<T> v, T d)
102 {
103  return Vector2D<T>(v.getX()/d, v.getY()/d);
104 }
105 
106 template <class T>
107 inline T operator*(Vector2D<T> v1, Vector2D<T> v2)
108 {
109  return v1.getX()*v2.getX() + v1.getY()*v2.getY();
110 }
111 
114 {
115 public:
116  IntVector2D() { m_x = 0; m_y = 0; }
117  IntVector2D(int x, int y) { m_x = x; m_y = y; }
118  int getX() const { return m_x; }
119  int getY() const { return m_y; }
120  int getComponent(int i) const { if (i == 1) return m_x; return m_y; }
121  IntVector2D &operator+=(const IntVector2D v) { m_x += v.m_x; m_y += v.m_y; return *this; }
122  IntVector2D &operator-=(const IntVector2D v) { m_x -= v.m_x; m_y -= v.m_y; return *this; }
123  bool operator==(const IntVector2D v) { if (v.m_x == m_x && v.m_y == m_y) return true; return false; }
124 private:
125  int m_x, m_y;
126 };
127 
128 class IntLine2D
129 {
130 public:
131  IntLine2D() { }
132  IntLine2D(IntVector2D v, IntVector2D w) { m_v1 = v; m_v2 = w; }
133  ~IntLine2D() { }
134  IntVector2D getVertex1() const { return m_v1; }
135  IntVector2D getVertex2() const { return m_v2; }
136  void setVertices(IntVector2D v, IntVector2D w) { m_v1 = v; m_v2 = w; }
137 private:
138  IntVector2D m_v1, m_v2;
139 };
140 
141 class Vector2DPlus : public Vector2D<double>
142 {
143 public:
144  Vector2DPlus(double x = 0,double y = 0,double v = 0) : Vector2D<double>(x,y) { m_value = v; }
145  void setValue(double v) { m_value = v; }
146  double getValue() const { return m_value; }
147 private:
148  double m_value;
149 };
150 
151 class IntVector2DPlus : public IntVector2D
152 {
153 public:
154  IntVector2DPlus(int x = 0, int y = 0, int v = 0) : IntVector2D(x,y) { m_value = v; }
155  void getValue(int v) { m_value = v; }
156  int getValue() const { return m_value; }
157 private:
158  int m_value;
159 };
160 
161 } // end namespace
162 
163 #endif // VECTOR2D_H
164