summaryrefslogtreecommitdiffstats
path: root/tksao/vector/vector.h
blob: f7461fc7cd6fb4d36d16dd541ae668d6d0150888 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
// Copyright (C) 1999-2018
// Smithsonian Astrophysical Observatory, Cambridge, MA, USA
// For conditions of distribution and use, see copyright notice in "copyright"

#ifndef __vector_h__
#define __vector_h__

#include <math.h>
#include <float.h>

#include <iostream>
using namespace std;

class Vector3d;
class Matrix;
class BBox;

class Vector {
 public:
  static int separator;
  static int unit;

  double v[3];

 public:
  Vector() 
    {v[0]=0; v[1]=0; v[2]=1;}
  Vector(double* f) 
    {v[0]=f[0]; v[1]=f[1]; v[2]=1;}
  Vector(double x, double y) 
    {v[0]=x; v[1]=y; v[2]=1;}

  Vector(const Vector& a) 
    {v[0]=a.v[0]; v[1]=a.v[1]; v[2]=a.v[2];}
  Vector& operator=(const Vector& a)
    {v[0]=a.v[0]; v[1]=a.v[1]; v[2]=a.v[2]; return *this;}

  Vector(const Vector3d&);
  Vector& operator=(const Vector3d&);

  const double& operator[](int i) const {return v[i];} // return element
  double& operator[](int i) {return v[i];} // return element
  double* vv() {return v;} // return vector

  Vector& origin()
    {v[0]=0; v[1]=0; v[2]=1; return *this;}

  Vector& operator+=(const Vector& a) // addition
    {v[0]+=a.v[0]; v[1]+=a.v[1]; return *this;}
  Vector& operator-=(const Vector& a) // subtraction
    {v[0]-=a.v[0]; v[1]-=a.v[1]; return *this;}
  Vector& operator*=(double f) // scalar multipy
    {v[0]*=f; v[1]*=f; return *this;}
  Vector& operator/=(double f) // scalar division
    {v[0]/=f; v[1]/=f; return *this;}
  Vector& operator*=(const Matrix& m); // vector multipy

  Vector abs() 
  {return Vector(fabs(v[0]),fabs(v[1]));}
  double angle() 
  {return atan2(v[1],v[0]);}
  Vector ceil() 
  {return Vector(::ceil(v[0]),::ceil(v[1]));}
  Vector floor() 
  {return Vector(::floor(v[0]),::floor(v[1]));}
  Vector invert() 
  {return Vector(1/v[0],1/v[1]);}
  double length() 
  {return sqrt(v[0]*v[0]+v[1]*v[1]);}
  double area()
  {return v[0]*v[1];}
  Vector round() 
  {return Vector((int)(v[0]+.5),(int)(v[1]+.5));}
  Vector normalize() 
  {double d = sqrt(v[0]*v[0]+v[1]*v[1]);
    return d ? Vector(v[0]/d,v[1]/d) : Vector();}

  // restrict vector by bbox
  Vector& clip(const BBox&);

  Vector TkCanvasPs(void* canvas);
};

// Vector separator(int)

struct _Setseparator {int _M_n;};
inline _Setseparator setseparator(int __n)
{
  _Setseparator __x;
  __x._M_n = __n;
  return __x;
}

template<class _CharT, class _Traits>
  inline basic_ostream<_CharT,_Traits>& 
  operator<<(basic_ostream<_CharT,_Traits>& __os, _Setseparator __f)
{ 
  __os.iword(Vector::separator) = __f._M_n;
  return __os; 
}

// Vector unit(int)

struct _Setunit {int _M_n;};
inline _Setunit setunit(int __n)
{
  _Setunit __x;
  __x._M_n = __n;
  return __x;
}

template<class _CharT, class _Traits>
  inline basic_ostream<_CharT,_Traits>& 
  operator<<(basic_ostream<_CharT,_Traits>& __os, _Setunit __f)
{ 
  __os.iword(Vector::unit) = __f._M_n;
  return __os; 
}

ostream& operator<<(ostream&, const Vector&);
istream& operator>>(istream&, Vector&);

inline Vector operator-(const Vector& a)
{return Vector(-a.v[0],-a.v[1]);}
inline Vector operator+(const Vector& a, const Vector& b) 
{return Vector(a) +=b;}
inline Vector operator-(const Vector& a, const Vector& b) 
{return Vector(a) -=b;}
inline Vector operator*(const Vector& a, double b)
{return Vector(a) *=b;}
inline Vector operator/(const Vector& a, double b)
{return Vector(a) /=b;}
inline Vector operator*(const Vector& v, const Matrix& m) 
{return Vector(v) *=m;}
inline double operator*(const Vector& a, const Vector& b) // dot product
{double r =0; r+=a.v[0]*b.v[0]; r+=a.v[1]*b.v[1]; return r;}

class Vertex {
public:
  Vector vector;

private:
  Vertex* next_;
  Vertex* previous_;

public:
  Vertex() 
    {next_=NULL; previous_=NULL;}
  Vertex(double x, double y) 
    {vector=Vector(x,y); next_=NULL; previous_=NULL;}
  Vertex(const Vector& a) 
    {vector=a; next_=NULL; previous_=NULL;}

  Vertex(const Vertex& a) 
    {vector=a.vector; next_=a.next_; previous_=a.previous_;}
  Vertex& operator=(const Vertex& a)
    {vector=a.vector; next_=a.next_; previous_=a.previous_; return *this;}
  
  Vertex* next() 
  {return next_;}
  Vertex* previous()
  {return previous_;}
  void setNext(Vertex* v)
  {next_=v;}
  void setPrevious(Vertex* v)
  {previous_=v;}
};
ostream& operator<<(ostream&, const Vertex&);

class Matrix {
 public:
  double m[3][3];

 public:
  Matrix()
    { m[0][0]=1; m[0][1]=0; m[0][2]=0; 
      m[1][0]=0; m[1][1]=1; m[1][2]=0; 
      m[2][0]=0; m[2][1]=0; m[2][2]=1;}
  Matrix(double a, double b, 
	 double c, double d, 
	 double e, double f)
    { m[0][0]=a; m[0][1]=b; m[0][2]=0; 
      m[1][0]=c; m[1][1]=d; m[1][2]=0; 
      m[2][0]=e; m[2][1]=f; m[2][2]=1;}
  Matrix(double a, double b, double c, 
	 double d, double e, double f, 
	 double g, double h, double i)
    { m[0][0]=a; m[0][1]=b; m[0][2]=c; 
      m[1][0]=d; m[1][1]=e; m[1][2]=f; 
      m[2][0]=g; m[2][1]=h; m[2][2]=i;}

  Matrix(const Matrix& a)
    { m[0][0]=a.m[0][0]; m[0][1]=a.m[0][1]; m[0][2]=a.m[0][2]; 
      m[1][0]=a.m[1][0]; m[1][1]=a.m[1][1]; m[1][2]=a.m[1][2]; 
      m[2][0]=a.m[2][0]; m[2][1]=a.m[2][1]; m[2][2]=a.m[2][2];}
  Matrix& operator=(const Matrix& a)
    { m[0][0]=a.m[0][0]; m[0][1]=a.m[0][1]; m[0][2]=a.m[0][2]; 
      m[1][0]=a.m[1][0]; m[1][1]=a.m[1][1]; m[1][2]=a.m[1][2]; 
      m[2][0]=a.m[2][0]; m[2][1]=a.m[2][1]; m[2][2]=a.m[2][2]; return *this;}
  
  double matrix(int i, int j) // return element
  {return m[i][j];}
  Vector operator[](int i) // return row
  {return Vector(m[i]);}
  double* mm() const // return matrix
  {return (double*)m;}

  Matrix& identity()
    { m[0][0]=1; m[0][1]=0; m[0][2]=0; 
      m[1][0]=0; m[1][1]=1; m[1][2]=0; 
      m[2][0]=0; m[2][1]=0; m[2][2]=1; return *this;}
  Matrix& operator*=(const Matrix&); // matrix multiply

  Matrix invert();
  Matrix cofactor();
  Matrix adjoint();
  double det();
};
ostream& operator<<(ostream&, const Matrix&);
istream& operator>>(istream&, Matrix&);

inline Matrix operator*(const Matrix& a, const Matrix& b) 
{return Matrix(a) *= b;}
inline Vector& Vector::operator*=(const Matrix& m)
{
  double vv[3];
  double* mm = (double*)(m.m);
  vv[0] = v[0]*mm[0] + v[1]*mm[3] + v[2]*mm[6]; 
  vv[1] = v[0]*mm[1] + v[1]*mm[4] + v[2]*mm[7]; 
  vv[2] = v[0]*mm[2] + v[1]*mm[5] + v[2]*mm[8];
  v[0] = vv[0];
  v[1] = vv[1];
  v[2] = vv[2];
  return *this;
}

class Translate : public Matrix {
public:
  Translate() 
    {};
  Translate(double x, double y) 
    {m[2][0]=x; m[2][1]=y;}
  Translate(const Vector& v) 
    {m[2][0]=v.v[0]; m[2][1]=v.v[1];}
  Translate(const Matrix& a) 
    {m[2][0] = a.m[2][0]; m[2][1] = a.m[2][1];}
};
ostream& operator<<(ostream&, const Translate&);
istream& operator>>(istream&, Translate&);

class Scale : public Matrix {
public:
  Scale() 
    {};
  Scale(double a) 
    {m[0][0]=a; m[1][1]=a;}
  Scale(double a, double b) 
    {m[0][0]=a; m[1][1]=b;}
  Scale(const Vector& v) 
    {m[0][0]=v.v[0]; m[1][1]=v.v[1];}
  Scale(const Matrix& a) 
    {m[0][0] = a.m[0][0]; m[1][1] = a.m[1][1];}
};
ostream& operator<<(ostream&, const Scale&);
istream& operator>>(istream&, Scale&);

class FlipX : public Matrix {
public:
  FlipX() 
    {m[0][0] = -1;}
};

class FlipY : public Matrix {
public:
  FlipY() 
    {m[1][1] = -1;}
};

class FlipXY : public Matrix {
public:
  FlipXY() 
    {m[0][0] = -1; m[1][1] = -1;}
};

class Rotate : public Matrix {
public:
  Rotate() 
    {};
  Rotate(double);
  Rotate(double a, double b, double c, double d)
    {m[0][0] = a; m[0][1] = b; m[1][0] = c; m[1][1] = d;}
  Rotate(const Matrix& a)
    { m[0][0]=a.m[0][0]; m[0][1]=a.m[0][1]; 
      m[1][0]=a.m[1][0]; m[1][1]=a.m[1][1];}
};
ostream& operator<<(ostream&, const Rotate&);
istream& operator>>(istream&, Rotate&);

class BBox {
public:
  Vector ll;
  Vector ur;

public:
  BBox() 
    {}
  BBox(double w, double h) 
    {ll.v[0] = 0; ll.v[1] = 0; ur.v[0] = w; ur.v[1] = h;}
  BBox(const Vector& v) 
    {ll=v; ur=v;}
  BBox(double, double, double, double);
  BBox(const Vector&, const Vector&);

  BBox(const BBox& a)
    {ll=a.ll; ur=a.ur;}
  BBox& operator=(const BBox& a)
    {ll=a.ll; ur=a.ur; return *this;}

  Vector lr() {return Vector(ur[0],ll[1]);}
  Vector ul() {return Vector(ll[0],ur[1]);}

  BBox& operator+=(const Vector& v) // addition
    {ll+=v; ur+=v; return *this;}
  BBox& operator-=(const Vector& a) // subtraction
    {ll-=a; ur-=a; return *this;}
  BBox& operator*=(const Matrix& m) // multiply
    {ll*=m; ur*=m; return *this;}

  Vector center() 
  {return (ur-ll)/2 + ll;}
  Vector size() 
  {return ur - ll;}
  int isEmpty() const 
  {Vector v = ur-ll; return (v[0]==0 && v[1]==0);}
  int isIn(const Vector&) const;
  int isIn(const BBox&) const;

  BBox& expand(double a) 
    {ll-=Vector(a,a); ur+=Vector(a,a); return *this;}
  BBox& expand(const Vector& v) 
    {ll-=v; ur+=v; return *this;}
  BBox& shrink(double a) 
    {ll+=Vector(a,a); ur-=Vector(a,a); return *this;}
  BBox& shrink(const Vector& v) 
    {ll+=v; ur-=v; return *this;}
  BBox& bound(BBox);
  BBox& bound(const Vector&);
};
ostream& operator<<(ostream&, const BBox&);

inline BBox operator+(const BBox& b, const Vector& v) {return BBox(b) += v;}
inline BBox operator-(const BBox& b, const Vector& v) {return BBox(b) -= v;}
inline BBox operator*(const BBox& b, const Matrix& m) {return BBox(b) *= m;}

BBox intersect(const BBox&, const BBox&);

// clip line to box from origin to width,height
int clip(Vector*, Vector*, int, int);
#endif