summaryrefslogtreecommitdiff
path: root/cad/plugins/NanoVision-1/src/Plugins/RenderingEngines/OpenGL/GLT/vector3.cpp
blob: 5664e6a6866b5b6777f2ca12b18094b0cf3a3b26 (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
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
#include "glt_vector3.h"

/*! \file 
	\ingroup Math
*/

#include "glt_matrix4.h"

#include "glt_gl.h"
#include "glt_glu.h"
#include "glt_viewport.h"

#include "glt_string.h"
#include <cassert>
#include <cmath>

#include <iostream>
#include <algorithm>
using namespace std;

const Vector VectorX(1.0,0.0,0.0);
const Vector VectorY(0.0,1.0,0.0);
const Vector VectorZ(0.0,0.0,1.0);
const Vector Vector0(0.0,0.0,0.0);
const Vector Vector1(1.0,1.0,1.0);

Vector::Vector()
{
	_vector[0] = _vector[1] = _vector[2] = 0.0;
}

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

Vector::Vector(const real x, const real y, const real z)
{
	_vector[0] = x;
	_vector[1] = y;
	_vector[2] = z;
}

Vector::Vector(const float *v)
{
	_vector[0] = v[0];
	_vector[1] = v[1];
	_vector[2] = v[2];
}

Vector::Vector(const double *v)
{
	_vector[0] = v[0];
	_vector[1] = v[1];
	_vector[2] = v[2];
}

Vector::Vector(const string &str)
{
	#ifndef NDEBUG
	const int n = 
	#endif
		atoc(str,atof,"+-eE.0123456789",_vector+0,_vector+3);
		
	assert(n==3);
}

const real &
Vector::operator[](const int i) const
{
	assert(i>=0 && i<3);
	return _vector[i];
}

real &
Vector::operator[](const int i)
{
	assert(i>=0 && i<3);
	return _vector[i];
}

// Vector::operator real* (void)
// {
// 	return (real *) _vector;
// }

      real &Vector::x()       { return _vector[0]; }
const real &Vector::x() const { return _vector[0]; }
      real &Vector::y()       { return _vector[1]; }
const real &Vector::y() const { return _vector[1]; }
      real &Vector::z()       { return _vector[2]; }
const real &Vector::z() const { return _vector[2]; }

bool
Vector::operator==(const Vector &v) const
{
	return 
		_vector[0] == v[0] &&
		_vector[1] == v[1] &&
		_vector[2] == v[2];
}

bool 
Vector::operator!=(const Vector &v) const
{
	return 
		_vector[0] != v[0] ||
		_vector[1] != v[1] ||
		_vector[2] != v[2];
}

bool
Vector::operator==(const real &a) const
{
	return 
		_vector[0] == a &&
		_vector[1] == a &&
		_vector[2] == a;
}

bool 
Vector::operator< (const Vector &v) const
{
	if (x()!=v.x()) return x()<v.x();
	if (y()!=v.y()) return y()<v.y();
	return z()<v.z();
}

bool 
Vector::operator> (const Vector &v) const
{
	if (x()!=v.x()) return x()>v.x();
	if (y()!=v.y()) return y()>v.y();
	return z()>v.z();
}

ostream &
Vector::writePov(ostream &os) const
{
	os << "< ";
	os << x() << ',';
	os << y() << ',';
	os << z() << " >";

	return os;
}

Vector
Vector::operator-() const
{
	return Vector(-_vector[0], -_vector[1], -_vector[2]);
}

void 
Vector::scale(const real &x)
{
	_vector[0] *= x;
	_vector[1] *= x;
	_vector[2] *= x;
}

void 
Vector::scale(const Vector &x)
{
	_vector[0] *= x._vector[0];
	_vector[1] *= x._vector[1];
	_vector[2] *= x._vector[2];
}

void
Vector::normalize()
{
	const real mag = sqrt(norm());

	if (mag == 0.0)
		return;

	const real magInv = 1.0/mag;

	_vector[0] *= magInv;
	_vector[1] *= magInv;
	_vector[2] *= magInv;
}

void 
Vector::abs()
{
	_vector[0] = fabs(_vector[0]);
	_vector[1] = fabs(_vector[1]);
	_vector[2] = fabs(_vector[2]);
}

int
Vector::dominant() const
{
	const real x = fabs(_vector[0]);
	const real y = fabs(_vector[1]);
	const real z = fabs(_vector[2]);

	if (x>y && x>z)
		return 0;
	else
		if (y>z)
			return 1;
		else
			return 2;
}

real
Vector::norm() const
{
	return ((*this)*(*this));
}

real
Vector::length() const
{
	return sqrt((*this)*(*this));
}

bool
Vector::project(const GltMatrix &model,const GltMatrix &proj,const GltViewport &view)
{
	// TODO - No need to copy from x,y,z if real is GLdouble
	
	GLdouble x,y,z;
	GLint ret = gluProject(_vector[0],_vector[1],_vector[2],model,proj,view,&x,&y,&z);

	_vector[0] = x;
	_vector[1] = y;
	_vector[2] = z;

	return ret==GL_TRUE;
}

Vector &
Vector::operator=(const Vector &x)
{
	_vector[0] = x[0];
	_vector[1] = x[1];
	_vector[2] = x[2];

	return *this;
}

Vector &
Vector::operator=(const float *x)
{
	_vector[0] = x[0];
	_vector[1] = x[1];
	_vector[2] = x[2];

	return *this;
}

Vector &
Vector::operator=(const double *x)
{
	_vector[0] = x[0];
	_vector[1] = x[1];
	_vector[2] = x[2];

	return *this;
}

real
Vector::operator*(const Vector &x) const
{
	return _vector[0]*x[0] + _vector[1]*x[1] + _vector[2]*x[2];
}

Vector & 
Vector::operator+=(const Vector &v)
{
	_vector[0] += v[0];
	_vector[1] += v[1];
	_vector[2] += v[2];

	return *this;
}

Vector & 
Vector::operator-=(const Vector &x)
{
	_vector[0] -= x[0];
	_vector[1] -= x[1];
	_vector[2] -= x[2];

	return *this;
}

Vector &
Vector::operator*=(const real &x)
{
	_vector[0] *= x;
	_vector[1] *= x;
	_vector[2] *= x;

	return *this;
}

Vector &
Vector::operator*=(const GltMatrix &m)
{
	return operator=(m*(*this));
}

real
Vector::dist(const Vector &x) const
{
	return ((*this)-x).norm();
}

Vector &
Vector::vmin(const Vector &v)
{
	_vector[0] = MIN(_vector[0],v[0]);
	_vector[1] = MIN(_vector[1],v[1]);
	_vector[2] = MIN(_vector[2],v[2]);
	return *this;
}

Vector &
Vector::vmax(const Vector &v)
{
	_vector[0] = MAX(_vector[0],v[0]);
	_vector[1] = MAX(_vector[1],v[1]);
	_vector[2] = MAX(_vector[2],v[2]);
	return *this;
}

/////////////////////////
//
// OpenGL

#include "glt_gl.h"

void
Vector::glVertex() const
{
	#ifdef GLT_FAST_FLOAT
	glVertex3fv(_vector);
	#else
	glVertex3dv(_vector);
	#endif
}

void
Vector::glNormal() const
{
	#ifdef GLT_FAST_FLOAT
	glNormal3fv(_vector);
	#else
	glNormal3dv(_vector);
	#endif
}

void
Vector::glColor() const
{
	#ifdef GLT_FAST_FLOAT
	glColor3fv(_vector);
	#else
	glColor3dv(_vector);
	#endif
}

void
Vector::glTexCoord() const
{
	#ifdef GLT_FAST_FLOAT
	glTexCoord3fv(_vector);
	#else
	glTexCoord3dv(_vector);
	#endif
}

void
Vector::glLight(GLenum light, GLenum pname) const
{
#ifdef GLT_FAST_FLOAT
    glLightfv(light, pname, _vector);
#else
    GLfloat params[4];
    params[0] = _vector[0];
    params[1] = _vector[1];
    params[2] = _vector[2];
    params[3] = _vector[3];
    glLightfv(light, pname, params);
#endif
}


////////////////////////// F R I E N D S

/*!
	\brief Output vector to stream
	\ingroup Math
*/

ostream &
operator<<(ostream &os, const Vector &x)
{
	os << x[0] << '\t';
	os << x[1] << '\t';
	os << x[2];

	return os;
}

/*!
	\brief Input vector from stream
	\ingroup Math
*/

istream &
operator>>(istream &is, Vector &x)
{
	is >> x[0];
	is >> x[1];
	is >> x[2];

	return is;
}

/*!
	\brief Vector scale
	\ingroup Math
*/

Vector
operator*(const real x, const Vector &v)
{
	return v*x;
}

/*!
	\brief Vector scale
	\ingroup Math
*/

Vector
operator*(const Vector &v, const real x)
{
	return Vector( x*v._vector[0], x*v._vector[1], x*v._vector[2]);
}

/*!
	\brief Vector inverse scale
	\ingroup Math
*/

Vector
operator/(const Vector &v, const real x)
{
	assert(x != 0.0);
	const real inv = 1.0/x;
	return v*inv;
}

/*!
	\brief Vector addition
	\ingroup Math
*/

Vector
operator+(const Vector &v1, const Vector &v2)
{
	return Vector(v1[0] + v2[0], v1[1] + v2[1], v1[2] + v2[2]);
}

/*!
	\brief Vector difference
	\ingroup Math
*/

Vector
operator-(const Vector &v1, const Vector &v2)
{
	return Vector(v1[0] - v2[0], v1[1] - v2[1], v1[2] - v2[2]);
}

/*!
	\brief Vector cross product
	\ingroup Math
*/

Vector 
xProduct(const Vector &v1, const Vector &v2)
{
	return Vector(v1[1]*v2[2] - v1[2]*v2[1],
				  v1[2]*v2[0] - v1[0]*v2[2],
				  v1[0]*v2[1] - v1[1]*v2[0]);
}

/*!
	\brief Calculate normal from three points in plane
	\ingroup Math
*/

Vector 
planeNormal(const Vector &v1, const Vector &v2,const Vector &v3)
{
	return xProduct(v2-v1,v3-v1);
}

/*!
	\brief Location on unit sphere
	\ingroup Math
*/

Vector 
polar(const real lat,const real longitude)
{
	return 
		Vector(
			cos(lat*M_PI_DEG) * cos(longitude*M_PI_DEG),
			sin(lat*M_PI_DEG),
			cos(lat*M_PI_DEG) * sin(longitude*M_PI_DEG)
		);
}

/*!
	\brief Create orthogonal co-ordinate system, based on a
	\ingroup Math
*/

void orthogonalSystem(Vector &a,Vector &b,Vector &c)
{
	a.normalize();

	if (fabs(a.z())>0.8)
	{
		b = xProduct(a,VectorY);
		c = xProduct(a,b);
	}
	else
	{
		b = xProduct(a,VectorZ);
		c = xProduct(a,b);
	}
	
	b.normalize();
	c.normalize();
}