summaryrefslogtreecommitdiffstats
path: root/tksao/vector/vector3d.C
blob: ef57553d713d419ccb737d558c14b0ef35cff1fc (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
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
// Copyright (C) 1999-2018
// Smithsonian Astrophysical Observatory, Cambridge, MA, USA
// For conditions of distribution and use, see copyright notice in "copyright"

#include <tk.h>

#include "vector3d.h"
#include "vector.h"
#include "fuzzy.h"

// Vector3d

Vector3d::Vector3d(const Vector& a)
{
  v[0]=a.v[0];
  v[1]=a.v[1];
  v[2]=0;
  v[3]=1;
}

Vector3d::Vector3d(const Vector& a, double z)
{
  v[0]=a.v[0];
  v[1]=a.v[1];
  v[2]=z;
  v[3]=1;
}

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

Vector Vector3d::TkCanvasPs(void* canvas)
{
  return Vector(v[0], Tk_CanvasPsY((Tk_Canvas)canvas, v[1]));
}

ostream& operator<<(ostream& os, const Vector3d& v)
{
  unsigned char sep = (unsigned char)os.iword(Vector::separator);
  if (!sep)
    sep = ' ';

  unsigned char unit = (unsigned char)os.iword(Vector::unit);
  if (!unit)
    os << v.v[0] << sep << v.v[1] << sep << v.v[2];
  else
    os << v.v[0] << unit << v.v[1] << unit << v.v[2] << unit;

  // reset unit
  os.iword(Vector::unit) = '\0';

  return os;
}

istream& operator>>(istream& s, Vector3d& v)
{
  s >> v.v[0] >> v.v[1] >> v.v[2];
  return s;
}

// Vertex3d

ostream& operator<<(ostream& os, const Vertex3d& v)
{
  os << v.vector;
  return os;
}

// Matrix3d

Matrix3d& Matrix3d::operator*=(const Matrix3d& a)
{
  Matrix3d r;
  for (int ii=0; ii<4; ii++)
    for (int jj=0; jj<4; jj++)
      r.m[ii][jj] = 
	m[ii][0]*a.m[0][jj] + 
	m[ii][1]*a.m[1][jj] + 
	m[ii][2]*a.m[2][jj] + 
	m[ii][3]*a.m[3][jj];

  return *this=r;
}

Matrix3d::Matrix3d(const Matrix& a)
{ 
  m[0][0]=a.m[0][0];
  m[0][1]=a.m[0][1];
  m[0][2]=0;
  m[0][3]=0;

  m[1][0]=a.m[1][0];
  m[1][1]=a.m[1][1];
  m[1][2]=0;
  m[1][3]=0;

  m[2][0]=0;
  m[2][1]=0;
  m[2][2]=1;
  m[2][3]=0;
  
  m[3][0]=a.m[2][0];
  m[3][1]=a.m[2][1];
  m[3][2]=0;
  m[3][3]=1;
}

Matrix3d Matrix3d::invert()
{
  Matrix3d cc = this->cofactor();
  Matrix3d aa = cc.adjoint();
  double dd = m[0][0]*aa.m[0][0] + m[0][1]*aa.m[1][0] + 
    m[0][2]*aa.m[2][0] + m[0][3]*aa.m[3][0];

  Matrix3d rr;
  for (int ii=0; ii<4; ii++ )
    for (int jj=0; jj<4; jj++)
      rr.m[ii][jj] = aa.m[ii][jj]/dd;

  return rr;
}

Matrix3d Matrix3d::cofactor()
{
  Matrix3d rr;

  rr.m[0][0] = +det2d(m[1][1],m[1][2],m[1][3],
		      m[2][1],m[2][2],m[2][3],
		      m[3][1],m[3][2],m[3][3]);
  rr.m[0][1] = -det2d(m[1][0],m[1][2],m[1][3],
		      m[2][0],m[2][2],m[2][3],
		      m[3][0],m[3][2],m[3][3]);
  rr.m[0][2] = +det2d(m[1][0],m[1][1],m[1][3],
		      m[2][0],m[2][1],m[2][3],
		      m[3][0],m[3][1],m[3][3]);
  rr.m[0][3] = -det2d(m[1][0],m[1][1],m[1][2],
		      m[2][0],m[2][1],m[2][2],
		      m[3][0],m[3][1],m[3][2]);

  rr.m[1][0] = -det2d(m[0][1],m[0][2],m[0][3],
		      m[2][1],m[2][2],m[2][3],
		      m[3][1],m[3][2],m[3][3]);
  rr.m[1][1] = +det2d(m[0][0],m[0][2],m[0][3],
		      m[2][0],m[2][2],m[2][3],
		      m[3][0],m[3][2],m[3][3]);
  rr.m[1][2] = -det2d(m[0][0],m[0][1],m[0][3],
		      m[2][0],m[2][1],m[2][3],
		      m[3][0],m[3][1],m[3][3]);
  rr.m[1][3] = +det2d(m[0][0],m[0][1],m[0][2],
		      m[2][0],m[2][1],m[2][2],
		      m[3][0],m[3][1],m[3][2]);

  rr.m[2][0] = +det2d(m[0][1],m[0][2],m[0][3],
		      m[1][1],m[1][2],m[1][3],
		      m[3][1],m[3][2],m[3][3]);
  rr.m[2][1] = -det2d(m[0][0],m[0][2],m[0][3],
		      m[1][0],m[1][2],m[1][3],
		      m[3][0],m[3][2],m[3][3]);
  rr.m[2][2] = +det2d(m[0][0],m[0][1],m[0][3],
		      m[1][0],m[1][1],m[1][3],
		      m[3][0],m[3][1],m[3][3]);
  rr.m[2][3] = -det2d(m[0][0],m[0][1],m[0][2],
		      m[1][0],m[1][1],m[1][2],
		      m[3][0],m[3][1],m[3][2]);

  rr.m[3][0] = -det2d(m[0][1],m[0][2],m[0][3],
		      m[1][1],m[1][2],m[1][3],
		      m[2][1],m[2][2],m[2][3]);
  rr.m[3][1] = +det2d(m[0][0],m[0][2],m[0][3],
		      m[1][0],m[1][2],m[1][3],
		      m[2][0],m[2][2],m[2][3]);
  rr.m[3][2] = -det2d(m[0][0],m[0][1],m[0][3],
		      m[1][0],m[1][1],m[1][3],
		      m[2][0],m[2][1],m[2][3]);
  rr.m[3][3] = +det2d(m[0][0],m[0][1],m[0][2],
		      m[1][0],m[1][1],m[1][2],
		      m[2][0],m[2][1],m[2][2]);

  return rr;
}

Matrix3d Matrix3d::adjoint()
{
  Matrix3d rr;
  for (int ii=0; ii<4; ii++)
    for (int jj=0; jj<4; jj++)
      rr.m[jj][ii] = m[ii][jj];

  return rr;
}

double Matrix3d::det()
{
  Matrix3d cc = this->cofactor();
  Matrix3d aa = cc.adjoint();
  return m[0][0]*aa.m[0][0] + m[0][1]*aa.m[1][0] 
    + m[0][2]*aa.m[2][0] + m[0][3]*aa.m[3][0];
}

void Matrix3d::dump()
{
  for (int ii=0; ii<4; ii++) {
    for (int jj=0; jj<4; jj++)
      cerr << m[ii][jj] << ' ';
    cerr << endl;
  }
  cerr << endl;
}

ostream& operator<<(ostream& os, const Matrix3d& m)
{
  os << ' ';
  for (int ii=0; ii<4; ii++)
    for (int jj=0; jj<3; jj++)
      os << m.m[ii][jj] << ' ';

  return os;
}

istream& operator>>(istream& s, Matrix3d& m)
{
  for (int ii=0; ii<4; ii++ )
    for (int jj=0; jj<3; jj++)
      s >> m.m[ii][jj];

  return s;
}

// Translate3d

Translate3d::Translate3d(const Vector& v) 
{
  m[3][0]=v.v[0];
  m[3][1]=v.v[1];
  m[3][2]=0;
}

Translate3d::Translate3d(const Vector& v, double z) 
{
  m[3][0]=v.v[0]; 
  m[3][1]=v.v[1]; 
  m[3][2]=z;
}

ostream& operator<<(ostream& os, const Translate3d& m)
{
  os << ' ' << m.m[3][0] << ' ' << m.m[3][1] << ' ' << m.m[3][2] << ' ';
  return os;
}

istream& operator>>(istream& s, Translate3d& m)
{
  s >> m.m[3][0] >> m.m[3][1] >> m.m[3][2];
  return s;
}

// Scale3d

Scale3d::Scale3d(const Vector& v)
{
  m[0][0]=v.v[0]; m[1][1]=v.v[1]; m[2][2]=1;
}

Scale3d::Scale3d(const Vector& v, double c)
{
  m[0][0]=v.v[0]; m[1][1]=v.v[1]; m[2][2]=c;
}

ostream& operator<<(ostream& os, const Scale3d& m)
{
  os << ' ' << m.m[0][0] << ' ' << m.m[1][1] << ' ' << m.m[2][2] << ' ';
  return os;
}

istream& operator>>(istream& s, Scale3d& m)
{
  s >> m.m[0][0] >> m.m[1][1] >> m.m[2][2];
  return s;
}

// RotateX3d

RotateX3d::RotateX3d(double a) : Matrix3d()
{
  m[1][1] = cos(a);
  m[1][2] = sin(a);
  m[2][1] = -sin(a);
  m[2][2] = cos(a);

  // this fixes a problem with numbers too small and tring to invert the matrix
  tzero(&m[1][1]);
  tzero(&m[1][2]);
  tzero(&m[2][1]);
  tzero(&m[2][2]);
}

ostream& operator<<(ostream& os, const RotateX3d& m)
{
  os << ' ' << m.m[1][1] << ' ' << m.m[1][2]
    << ' ' << m.m[2][1] << ' ' << m.m[2][2] << ' ';
  return os;
}

istream& operator>>(istream& s, RotateX3d& m)
{
  s >> m.m[1][1] >> m.m[1][2] >> m.m[2][1] >> m.m[2][2];
  return s;
}

// RotateY3d

RotateY3d::RotateY3d(double a) : Matrix3d()
{
  m[0][0] = cos(a);
  m[0][2] = -sin(a);
  m[2][0] = sin(a);
  m[2][2] = cos(a);

  // this fixes a problem with numbers too small and tring to invert the matrix
  tzero(&m[0][0]);
  tzero(&m[0][2]);
  tzero(&m[2][0]);
  tzero(&m[2][2]);
}

ostream& operator<<(ostream& os, const RotateY3d& m)
{
  os << ' ' << m.m[0][0] << ' ' << m.m[0][2]
    << ' ' << m.m[2][0] << ' ' << m.m[2][2] << ' ';
  return os;
}

istream& operator>>(istream& s, RotateY3d& m)
{
  s >> m.m[0][0] >> m.m[0][2] >> m.m[2][0] >> m.m[2][2];
  return s;
}

// RotateZ3d

RotateZ3d::RotateZ3d(double a) : Matrix3d()
{
  m[0][0] = cos(a);
  m[0][1] = sin(a);
  m[1][0] = -sin(a);
  m[1][1] = cos(a);

  // this fixes a problem with numbers too small and tring to invert the matrix
  tzero(&m[0][0]);
  tzero(&m[0][1]);
  tzero(&m[1][0]);
  tzero(&m[1][1]);
}

ostream& operator<<(ostream& os, const RotateZ3d& m)
{
  os << ' ' << m.m[0][0] << ' ' << m.m[0][1]
    << ' ' << m.m[1][0] << ' ' << m.m[1][1] << ' ';
  return os;
}

istream& operator>>(istream& s, RotateZ3d& m)
{
  s >> m.m[0][0] >> m.m[0][1] >> m.m[1][0] >> m.m[1][1];
  return s;
}

// BBox3d

BBox3d::BBox3d(double a, double b, double c, double d, double e, double f)
{
  // we want a 'positive' cube
  ll.v[0] = a < d ? a : d;
  ll.v[1] = b < e ? b : e;
  ll.v[2] = c < f ? c : f;
  ur.v[0] = a < d ? d : a;
  ur.v[1] = b < e ? e : b;
  ur.v[2] = c < f ? f : c;
}

BBox3d::BBox3d(const Vector3d& l, const Vector3d& h)
{
  // we want a 'positive' cube
  ll.v[0] = l.v[0] < h.v[0] ? l.v[0] : h.v[0];
  ll.v[1] = l.v[1] < h.v[1] ? l.v[1] : h.v[1];
  ll.v[2] = l.v[2] < h.v[2] ? l.v[2] : h.v[2];
  ur.v[0] = l.v[0] < h.v[0] ? h.v[0] : l.v[0];
  ur.v[1] = l.v[1] < h.v[1] ? h.v[1] : l.v[1];
  ur.v[2] = l.v[2] < h.v[2] ? h.v[2] : l.v[2];
}

int BBox3d::isIn(const Vector3d& v) const
{
  return !(v.v[0] < ll.v[0] || v.v[1] < ll.v[1] || v.v[2] < ll.v[2] ||
	   v.v[0] > ur.v[0] || v.v[1] > ur.v[1] || v.v[2] > ur.v[2]);
}

double BBox3d::volume()
{
  Vector3d aa =ur-ll;
  return aa[0]*aa[1]*aa[2];
}

BBox3d& BBox3d::bound(const Vector3d& vv)
{
  // expand bbox3d to include vector3d
  if (vv.v[0] < ll[0])
    ll[0] = vv.v[0];
  if (vv.v[1] < ll[1])
    ll[1] = vv.v[1];
  if (vv.v[2] < ll[2])
    ll[2] = vv.v[2];
    
  if (vv.v[0] > ur[0])
    ur[0] = vv.v[0];
  if (vv.v[1] > ur[1])
    ur[1] = vv.v[1];
  if (vv.v[2] > ur[2])
    ur[2] = vv.v[2];

  return *this;
}

ostream& operator<<(ostream& os, const BBox3d& b)
{
  os << b.ll << ' ' << b.ur;
  return os;
}

// WorldToView

Matrix3d WorldToView3d(const Vector3d& cop, 
		       const Vector3d& vpn, 
		       const Vector3d& vup)
{
  Vector3d zv = ((Vector3d)vpn).normalize();
  Vector3d xv = cross(zv,(Vector3d&)vup).normalize();
  Vector3d yv = cross(xv,zv).normalize();

  return Translate3d(-cop) * 
    Matrix3d(xv[0],yv[0],zv[0],
	     xv[1],yv[1],zv[1],
	     xv[2],yv[2],zv[2],
	         0,    0,    0);
}

Matrix3d WorldToView3d(const Vector3d& cop, 
		       double head, double pitch, double bank)
{
  return Translate3d(-cop) *
    RotateY3d(head) * 
    RotateX3d(pitch) * 
    RotateZ3d(bank) *
    Scale3d(1,1,-1);
}

Matrix3d WorldToView3d(const Vector3d& cop, const Vector3d& vpn, double bank)
{
  Vector3d zv = -((Vector3d)vpn).normalize();
  double l=sqrt(zv[0]*zv[0]+zv[2]*zv[2]);

  return Translate3d(-cop) *
    RotateY3d(zv[2]/l,zv[0]/l,-zv[0]/l,zv[2]/l) * 
    RotateX3d(l,zv[1],-zv[1],l) * 
    RotateZ3d(bank) *
    Scale3d(1,1,-1);
}