-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTuple4.h
More file actions
551 lines (493 loc) · 14.6 KB
/
Tuple4.h
File metadata and controls
551 lines (493 loc) · 14.6 KB
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
551
/*
Copyright (C) 1997,1998,1999
Kenji Hiranabe, Eiwa System Management, Inc.
This program is free software.
Implemented by Kenji Hiranabe(hiranabe@esm.co.jp),
conforming to the Java(TM) 3D API specification by Sun Microsystems.
Permission to use, copy, modify, distribute and sell this software
and its documentation for any purpose is hereby granted without fee,
provided that the above copyright notice appear in all copies and
that both that copyright notice and this permission notice appear
in supporting documentation. Kenji Hiranabe and Eiwa System Management,Inc.
makes no representations about the suitability of this software for any
purpose. It is provided "AS IS" with NO WARRANTY.
*/
#ifndef TUPLE4_H
#define TUPLE4_H
#include "VmUtil.h"
VM_BEGIN_NS
/**
* A generic 4 element tuple that is represented by
* x,y,z and w coordinates.
* @version specification 1.1, implementation $Revision: 1.5 $, $Date: 1999/10/22 08:37:34 $
* @author Kenji hiranabe
*/
template<class T>
class Tuple4 {
/*
* $Log: Tuple4.h,v $
* Revision 1.5 1999/10/22 08:37:34 hiranabe
* added operator=. work around for IRIX6.4 + MIPSPro7.2.
* Thanks > Katsuaki KAWACHI <kawachi@cim.pe.u-tokyo.ac.jp>
*
* Revision 1.4 1999/10/06 02:52:46 hiranabe
* Java3D 1.2 and namespace
*
* Revision 1.3 1999/06/07 12:47:35 hiranabe
* VM_INCLUDE_STRING -> VM_INCLUDE_TOSTRING
* thanks > kawachi@cim.pe.u-tokyo.ac.jp
*
* Revision 1.2 1999/05/26 00:59:37 hiranabe
* support Visual C++
*
* Revision 1.1 1999/03/04 11:07:09 hiranabe
* Initial revision
*
* Revision 1.1 1999/03/04 11:07:09 hiranabe
* Initial revision
*
*/
public:
/**
* the type for values
*/
typedef T value_type;
/**
* the type for index
*/
typedef size_t size_type;
/**
* dimension
*/
enum { DIMENSION = 4 };
/**
* The x coordinate.
*/
T x;
/**
* The y coordinate.
*/
T y;
/**
* The z coordinate.
*/
T z;
/**
* The w coordinate.
*/
T w;
/**
* Constructs and initializes a Tuple4 from the specified xyzw coordinates.
* @param xvalue the x coordinate
* @param yvalue the y coordinate
* @param zvalue the z coordinate
* @param wvalue the w coordinate
*/
Tuple4(T xvalue, T yvalue, T zvalue, T wvalue):
x(xvalue), y(yvalue), z(zvalue), w(wvalue) { }
/**
* Constructs and initializes a Tuple4 from the specified array.
* @param t the array of length 4 containing xyzw in order
*/
Tuple4(const T t[]): x(t[0]), y(t[1]), z(t[2]), w(t[3]) { }
/**
* Constructs and initializes a Tuple4 to (0,0,0,0).
*/
Tuple4(): x(0), y(0), z(0), w(0) { }
/**
* Sets the value of this tuple to the specified xyzw coordinates.
* @param xvalue the x coordinate
* @param yvalue the y coordinate
* @param zvalue the z coordinate
* @param wvalue the w coordinate
*/
void set(T xvalue, T yvalue, T zvalue, T wvalue) {
x = xvalue;
y = yvalue;
z = zvalue;
w = wvalue;
}
/**
* Sets the value of this tuple from the 4 values specified in the array.
* @param t the array of length 4 containing xyzw in order
*/
void set(const T t[]) {
x = t[0];
y = t[1];
z = t[2];
w = t[3];
}
/**
* Sets the value of this tuple to the value of tuple t1.
* @param t1 the tuple to be copied
*/
void set(const Tuple4& t1) {
x = t1.x;
y = t1.y;
z = t1.z;
w = t1.w;
}
/**
* Copies the value of the elements of this tuple into the array t[].
* @param t the array that will contain the values of the vector
*/
void get(T t[]) const {
t[0] = x;
t[1] = y;
t[2] = z;
t[3] = w;
}
/**
* Gets the value of this tuple and copies the values into the Tuple4.
* @param t Tuple4 object into which that values of this object are copied
*/
void get(Tuple4* t) const {
assert(t);
t->x = x;
t->y = y;
t->z = z;
t->w = w;
}
/**
* Sets the value of this tuple to the vector sum of tuples t1 and t2.
* @param t1 the first tuple
* @param t2 the second tuple
*/
void add(const Tuple4& t1, const Tuple4& t2) {
x = t1.x + t2.x;
y = t1.y + t2.y;
z = t1.z + t2.z;
w = t1.w + t2.w;
}
/**
* Sets the value of this tuple to the vector sum of itself and tuple t1.
* @param t1 the other tuple
*/
void add(const Tuple4& t1) {
x += t1.x;
y += t1.y;
z += t1.z;
w += t1.w;
}
/**
* Sets the value of this tuple to the vector difference of tuple t1 and t2 (this = t1 - t2).
* @param t1 the first tuple
* @param t2 the second tuple
*/
void sub(const Tuple4& t1, const Tuple4& t2) {
x = t1.x - t2.x;
y = t1.y - t2.y;
z = t1.z - t2.z;
w = t1.w - t2.w;
}
/**
* Sets the value of this tuple to the vector difference of itself and tuple t1 (this = this - t1).
* @param t1 the other tuple
*/
void sub(const Tuple4& t1) {
x -= t1.x;
y -= t1.y;
z -= t1.z;
w -= t1.w;
}
/**
* Sets the value of this tuple to the negation of tuple t1.
* @param t1 the source vector
*/
void negate(const Tuple4& t1) {
x = -t1.x;
y = -t1.y;
z = -t1.z;
w = -t1.w;
}
/**
* Negates the value of this vector in place.
*/
void negate() {
x = -x;
y = -y;
z = -z;
w = -w;
}
/**
* Sets the value of this tuple to the scalar multiplication of tuple t1.
* @param s the scalar value
* @param t1 the source tuple
*/
void scale(T s, const Tuple4& t1) {
x = s*t1.x;
y = s*t1.y;
z = s*t1.z;
w = s*t1.w;
}
/**
* Sets the value of this tuple to the scalar multiplication of itself.
* @param s the scalar value
*/
void scale(T s) {
x *= s;
y *= s;
z *= s;
w *= s;
}
/**
* Sets the value of this tuple to the scalar multiplication of tuple t1 and then
* adds tuple t2 (this = s*t1 + t2).
* @param s the scalar value
* @param t1 the tuple to be multipled
* @param t2 the tuple to be added
*/
void scaleAdd(T s, const Tuple4& t1, const Tuple4& t2) {
x = s*t1.x + t2.x;
y = s*t1.y + t2.y;
z = s*t1.z + t2.z;
w = s*t1.w + t2.w;
}
/**
* Sets the value of this tuple to the scalar multiplication of itself and then
* adds tuple t1 (this = s*this + t1).
* @param s the scalar value
* @param t1 the tuple to be added
*/
void scaleAdd(T s, const Tuple4& t1) {
x = s*x + t1.x;
y = s*y + t1.y;
z = s*z + t1.z;
w = s*z + t1.w;
}
/**
* Returns a hash number based on the data values in this object.
* Two different Tuple4 objects with identical data values
* (ie, returns true for equals(Tuple4) ) will return the same hash number.
* Two vectors with different data members may return the same hash value,
* although this is not likely.
*/
size_t hashCode() const {
return VmUtil<T>::hashCode(sizeof *this, this);
}
/**
* Returns true if all of the data members of Tuple4 t1 are equal to the corresponding
* data members in this
* @param t1 the vector with which the comparison is made.
*/
bool equals(const Tuple4& t1) const {
return x == t1.x && y == t1.y && z == t1.z && w == t1.w;
}
/**
* Returns true if the L-infinite distance between this tuple and tuple t1 is
* less than or equal to the epsilon parameter, otherwise returns false. The L-infinite
* distance is equal to MAX[abs(x1-x2), abs(y1-y2), abs(z1-z2), abs(w1-w2)].
* @param t1 the tuple to be compared to this tuple
* @param epsilon the threshold value
*/
bool epsilonEquals(const Tuple4& t1, T epsilon) const {
return (VmUtil<T>::abs(t1.x - x) <= epsilon) &&
(VmUtil<T>::abs(t1.y - y) <= epsilon) &&
(VmUtil<T>::abs(t1.z - z) <= epsilon) &&
(VmUtil<T>::abs(t1.w - w) <= epsilon);
}
/**
* Clamps the tuple parameter to the range [low, high] and places the values
* into this tuple.
* @param min the lowest value in the tuple after clamping
* @param max the highest value in the tuple after clamping
* @param t the source tuple, which will not be modified
*/
void clamp(T min, T max, const Tuple4& t) {
set(t);
clamp(min, max);
}
/**
* Clamps the minimum value of the tuple parameter to the min parameter
* and places the values into this tuple.
* @param min the lowest value in the tuple after clamping
* @parm t the source tuple, which will not be modified
*/
void clampMin(T min, const Tuple4& t) {
set(t);
clampMin(min);
}
/**
* Clamps the maximum value of the tuple parameter to the max parameter and
* places the values into this tuple.
* @param max the highest value in the tuple after clamping
* @param t the source tuple, which will not be modified
*/
void clampMax(T max, const Tuple4& t) {
set(t);
clampMax(max);
}
/**
* Sets each component of the tuple parameter to its absolute value and
* places the modified values into this tuple.
* @param t the source tuple, which will not be modified
*/
void absolute(const Tuple4& t) {
set(t);
absolute();
}
/**
* Clamps this tuple to the range [low, high].
* @param min the lowest value in this tuple after clamping
* @param max the highest value in this tuple after clamping
*/
void clamp(T min, T max) {
clampMin(min);
clampMax(max);
}
/**
* Clamps the minimum value of this tuple to the min parameter.
* @param min the lowest value in this tuple after clamping
*/
void clampMin(T min) {
if (x < min)
x = min;
if (y < min)
y = min;
if (z < min)
z = min;
if (w < min)
w = min;
}
/**
* Clamps the maximum value of this tuple to the max parameter.
* @param max the highest value in the tuple after clamping
*/
void clampMax(T max) {
if (x > max)
x = max;
if (y > max)
y = max;
if (z > max)
z = max;
if (w > max)
w = max;
}
/**
* Sets each component of this tuple to its absolute value.
*/
void absolute() {
if (x < 0.0)
x = -x;
if (y < 0.0)
y = -y;
if (z < 0.0)
z = -z;
if (w < 0.0)
w = -w;
}
/**
* Linearly interpolates between tuples t1 and t2 and places the
* result into this tuple: this = (1-alpha)*t1 + alpha*t2.
* @param t1 the first tuple
* @param t2 the second tuple
* @param alpha the alpha interpolation parameter
*/
void interpolate(const Tuple4& t1, const Tuple4& t2, T alpha) {
set(t1);
interpolate(t2, alpha);
}
/**
* Linearly interpolates between this tuple and tuple t1 and places the
* result into this tuple: this = (1-alpha)*this + alpha*t1.
* @param t1 the first tuple
* @param alpha the alpha interpolation parameter
*
*/
void interpolate(const Tuple4& t1, T alpha) {
T beta = 1 - alpha;
x = beta*x + alpha*t1.x;
y = beta*y + alpha*t1.y;
z = beta*z + alpha*t1.z;
w = beta*w + alpha*t1.w;
}
/**
* Returns a string that contains the values of this Tuple4. The form is (x,y,z,w).
* @return the String representation
*/
#ifdef VM_INCLUDE_TOSTRING
std::string toString() const {
VM_TOSTRING
}
#endif
// copy constructor and operator = is made by complier
bool operator==(const Tuple4& t1) const {
return equals(t1);
}
#ifdef VM_INCLUDE_SUBSCRIPTION_OPERATOR
T operator[](size_t index) const {
assert(index < (size_t)DIMENSION);
switch (index) {
case 0:
return x;
case 1:
return y;
case 2:
return z;
case 3:
return w;
default:
// error !
return 0;
}
}
T operator[](size_t index) const {
static T dummy;
assert(index < (size_t)DIMENSION);
switch (index) {
case 0:
return x;
case 1:
return y;
case 2:
return z;
case 3:
return w;
default:
// error !
return dummy;
}
}
#endif
Tuple4& operator=(const Tuple4& t1) {
set(t1);
return *this;
}
Tuple4& operator+=(const Tuple4& t1) {
add(t1);
return *this;
}
Tuple4& operator-=(const Tuple4& t1) {
sub(t1);
return *this;
}
Tuple4& operator*=(T s) {
scale(s);
return *this;
}
Tuple4 operator+(const Tuple4& t1) const {
return (Tuple4(*this)).operator+=(t1);
}
Tuple4 operator-(const Tuple4& t1) const {
return (Tuple4(*this)).operator-=(t1);
}
Tuple4 operator*(T s) const {
return (Tuple4(*this)).operator*=(s);
}
};
template <class T>
inline
VM_VECMATH_NS::Tuple4<T> operator*(T s, const VM_VECMATH_NS::Tuple4<T>& t1) {
return (VM_VECMATH_NS::Tuple4<T>(t1)).operator*=(s);
}
#ifdef VM_INCLUDE_IO
#include <iostream>
template <class T>
std::ostream& operator<<(std::ostream& o, const VM_VECMATH_NS::Tuple4<T>& t1) {
return o << "(" << t1.x << "," << t1.y << "," << t1.z << "," << t1.w << ")";
}
#endif
typedef Tuple4<double> Tuple4d;
typedef Tuple4<float> Tuple4f;
VM_END_NS
#endif /* TUPLE4_H */