ldpk
ldpk_vec2d.h
Go to the documentation of this file.
1 #ifndef ldpk_vec2d_sdv
2 #define ldpk_vec2d_sdv
3 
4 #include <ldpk/ldpk_error.h>
5 #include <iostream>
6 #include <cmath>
7 
10 
11 namespace ldpk
12  {
20  class vec2d
21  {
22  private:
23  double _x0,_x1;
24  public:
26 
27  vec2d():_x0(0),_x1(0)
29  { }
31  vec2d(const vec2d& a):_x0(a._x0),_x1(a._x1)
32  { }
34  vec2d(double x0,double x1):_x0(x0),_x1(x1)
35  { }
37 
39  vec2d& operator += (const vec2d& a)
40  { _x0 += a._x0;_x1 += a._x1;return *this; }
41  vec2d operator + (const vec2d& a) const
42  { vec2d r(*this);return r += a; }
43  vec2d& operator -= (const vec2d& a)
44  { _x0 -= a._x0;_x1 -= a._x1;return *this; }
45  vec2d operator - (const vec2d& a) const
46  { vec2d r(*this);return r -= a; }
47  vec2d& operator *= (double q)
48  { _x0 *= q;_x1 *= q;return *this; }
49  vec2d operator * (double q) const
50  { vec2d r(*this);return r *= q; }
51  friend vec2d operator * (double q,const vec2d& a)
52  { vec2d r(a);return r *= q; }
53  vec2d& operator /= (double q)
54  { _x0 /= q;_x1 /= q;return *this; }
55  vec2d operator / (double q) const
56  { vec2d r(*this);return r /= q; }
57  vec2d operator - ()
58  { return vec2d(-_x0,-_x1); }
60 
62  const double& operator [] (int i) const
63  { return *(&_x0 + i); }
64  double& operator [] (int i)
65  { return *(&_x0 + i); }
67 
69  friend double dotsq(const vec2d& a)
71  { return a[0] * a[0] + a[1] * a[1]; }
73  friend double dot(const vec2d& a,const vec2d& b)
74  { return a[0] * b[0] + a[1] * b[1]; }
76  friend double norm2(const vec2d& a)
77  { return ::sqrt(dotsq(a)); }
79  friend vec2d unit(const vec2d& v)
80  { return v / norm2(v); }
82 
84  friend std::ostream& operator << (std::ostream& cout,const vec2d& a)
85  { return cout << a[0] << " " << a[1]; }
86  friend std::istream& operator >> (std::istream& cin,vec2d& a)
87  { return cin >> a[0] >> a[1]; }
89  };
91  class mat2d
92  {
93  private:
94  vec2d _x0,_x1;
95  public:
97 
98  mat2d():_x0(1,0),_x1(0,1)
100  { }
102  mat2d(const mat2d& a):_x0(a[0]),_x1(a[1])
103  { }
105  mat2d(double s):_x0(s,0),_x1(0,s)
106  { }
108  mat2d(double a00,double a01,double a10,double a11):_x0(a00,a01),_x1(a10,a11)
109  { }
111  mat2d(const vec2d& x0,const vec2d& x1):_x0(x0),_x1(x1)
112  { }
114 
116  mat2d& operator += (const mat2d& a)
117  { _x0 += a._x0;_x1 += a._x1;return *this; }
118  mat2d operator + (const mat2d& a) const
119  { mat2d r(*this);return r += a; }
120  mat2d& operator -= (const mat2d& a)
121  { _x0 -= a._x0;_x1 -= a._x1;return *this; }
122  mat2d operator - (const mat2d& a) const
123  { mat2d r(*this);return r -= a; }
124  mat2d& operator *= (double q)
125  { _x0 *= q;_x1 *= q;return *this; }
126  mat2d operator * (double q) const
127  { mat2d r(*this);return r *= q; }
128  friend mat2d operator * (double q,const mat2d& a)
129  { mat2d r(a);return r *= q; }
131  mat2d operator * (const mat2d& a) const
132  {
133  const mat2d& t(*this);
134  return mat2d( t[0][0] * a[0][0] + t[0][1] * a[1][0],
135  t[0][0] * a[0][1] + t[0][1] * a[1][1],
136  t[1][0] * a[0][0] + t[1][1] * a[1][0],
137  t[1][0] * a[0][1] + t[1][1] * a[1][1]);
138  }
139  mat2d& operator /= (double q)
140  { _x0 /= q;_x1 /= q;return *this; }
141  mat2d operator / (double q) const
142  { mat2d r(*this);return r /= q; }
143  mat2d operator - ()
144  { return mat2d(-_x0,-_x1); }
146  vec2d operator * (const vec2d& a) const
147  { return vec2d(dot(_x0,a),dot(_x1,a)); }
149 
151  const vec2d& operator [] (int i) const
152  { return *(&_x0 + i); }
153  vec2d& operator [] (int i)
154  { return *(&_x0 + i); }
156 
158  friend double det(const mat2d& a)
160  { return a[0][0] * a[1][1] - a[0][1] * a[1][0]; }
162  friend double tr(const mat2d& a)
163  { return a[0][0] + a[1][1]; }
165  friend mat2d trans(const mat2d& a)
166  { return mat2d(a[0][0],a[1][0],a[0][1],a[1][1]); }
168  friend mat2d invert(const mat2d& a)
169  { return mat2d(a[1][1],-a[0][1],-a[1][0],a[0][0]) / det(a); }
171 
173  friend std::ostream& operator << (std::ostream& cout,const mat2d& a)
174  { return cout << a[0] << " " << a[1]; }
175  friend std::istream& operator >> (std::istream& cin,mat2d& a)
176  { return cin >> a[0] >> a[1]; }
178  };
179 
181 
182  inline mat2d ten(const vec2d& a,const vec2d& b)
184  { return mat2d(a[0] * b[0],a[0] * b[1],a[1] * b[0],a[1] * b[1]); }
186  inline mat2d tensq(const vec2d& a)
187  { return mat2d(a[0] * a[0],a[0] * a[1],a[1] * a[0],a[1] * a[1]); }
189  template <class VEC2>
192  class box
193  {
194  public:
195  typedef VEC2 vec2_type;
196  typedef box<VEC2> this_type;
197  private:
198  vec2_type _a,_b;
199  public:
200  box():_a(HUGE_VAL,HUGE_VAL),_b(-HUGE_VAL,-HUGE_VAL)
201  { }
202  void reset()
203  {
204  _a = vec2_type(HUGE_VAL,HUGE_VAL);
205  _b = vec2_type(-HUGE_VAL,-HUGE_VAL);
206  }
207  const vec2_type& a() const
208  { return _a; }
209  const vec2_type& b() const
210  { return _b; }
211  bool is_empty() const
212  { return (_b[0] < _a[0]) || (_b[1] < _a[1]); }
213  void extend_x(double x)
214  {
215  if(x < _a[0]) _a[0] = x;
216  if(x > _b[0]) _b[0] = x;
217  }
218  void extend_y(double y)
219  {
220  if(y < _a[1]) _a[1] = y;
221  if(y > _b[1]) _b[1] = y;
222  }
223  void extend(int i,double q)
224  {
225  if(i == 0) extend_x(q);
226  if(i == 1) extend_y(q);
227  }
228  void extend(const vec2_type& p)
229  {
230  extend_x(p[0]);
231  extend_y(p[1]);
232  }
233  this_type& merge(const this_type& q)
234  {
235  extend(q.a());
236  extend(q.b());
237  return *this;
238  }
239  friend this_type merge(const this_type& q,const this_type& r)
240  {
241  this_type s(q);
242  return s.merge(r);
243  }
244  };
245  }
246 
247 #endif
mat2d tensq(const vec2d &a)
Tensor (dyadic) product square.
Definition: ldpk_vec2d.h:186
Exception classes for ldpk.
friend double norm2(const vec2d &a)
Euclidian norm.
Definition: ldpk_vec2d.h:76
mat2d(const mat2d &a)
Copy constructor.
Definition: ldpk_vec2d.h:102
friend vec2d unit(const vec2d &v)
Unit vector.
Definition: ldpk_vec2d.h:79
mat2d(double s)
Scalar matrix.
Definition: ldpk_vec2d.h:105
vec2d()
Default: null vector.
Definition: ldpk_vec2d.h:28
friend double det(const mat2d &a)
Determinant.
Definition: ldpk_vec2d.h:159
mat2d(const vec2d &x0, const vec2d &x1)
Constructing by components (row vectors)
Definition: ldpk_vec2d.h:111
vec2d(const vec2d &a)
Copy constructor.
Definition: ldpk_vec2d.h:31
mat2d(double a00, double a01, double a10, double a11)
Constructing by components (numbers)
Definition: ldpk_vec2d.h:108
A simple box class for double precision points in 2d. We will extend this as needed.
Definition: ldpk_vec2d.h:192
A class for double-valued 2x2-matrices. The matrix class for ldpk::vec2d.
Definition: ldpk_vec2d.h:91
mat2d()
Default: null matrix.
Definition: ldpk_vec2d.h:99
friend mat2d invert(const mat2d &a)
Inverse.
Definition: ldpk_vec2d.h:168
mat2d ten(const vec2d &a, const vec2d &b)
Tensor (dyadic) product of vectors.
Definition: ldpk_vec2d.h:183
vec2d(double x0, double x1)
Constructing by components.
Definition: ldpk_vec2d.h:34
A class for two-dimensional double-valued vectors We have added this class and ldpk::mat2d in order t...
Definition: ldpk_vec2d.h:20
friend double tr(const mat2d &a)
Trace.
Definition: ldpk_vec2d.h:162
friend double dot(const vec2d &a, const vec2d &b)
Inner product.
Definition: ldpk_vec2d.h:73
friend double dotsq(const vec2d &a)
Inner product square.
Definition: ldpk_vec2d.h:70
friend mat2d trans(const mat2d &a)
Transposed.
Definition: ldpk_vec2d.h:165