-
Notifications
You must be signed in to change notification settings - Fork 0
/
Vector4.h
198 lines (168 loc) · 5.55 KB
/
Vector4.h
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
/*
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 VECTOR4_H
#define VECTOR4_H
#include "VmUtil.h"
#include "Tuple4.h"
#include "Tuple3.h"
VM_BEGIN_NS
/**
* A 4 element vector that is represented by x,y,z,w coordinates.
* @version specification 1.1, implementation $Revision: 1.3 $, $Date: 1999/10/06 02:52:46 $
* @author Kenji hiranabe
*/
template<class T>
class Vector4 : public Tuple4<T> {
/*
* $Log: Vector4.h,v $
* Revision 1.3 1999/10/06 02:52:46 hiranabe
* Java3D 1.2 and namespace
*
* 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:
/**
* Constructs and initializes a Vector4 from the specified xyzw coordinates.
* @param x the x coordinate
* @param y the y coordinate
* @param z the z coordinate
* @param w the w coordinate
*/
Vector4(T x, T y, T z, T w): Tuple4<T>(x, y, z, w) { }
/**
* Constructs and initializes a Vector4 from the specified array of length 4.
* @param v the array of length 4 containing xyzw in order
*/
Vector4(const T v[]): Tuple4<T>(v) { }
/**
* Constructs and initializes a Vector4 from the specified Tuple4d.
* @param t1 the Tuple4d containing the initialization x y z w data
*/
Vector4(const Tuple4<T>& t1): Tuple4<T>(t1) { }
/**
* Constructs and initializes a Vector4 to (0,0,0,0).
*/
Vector4(): Tuple4<T>() { }
/**
* Constructs and initializes a Vector4 from the specified Tuple3.
* The x,y,z components of this point are set to the corresponding
* components
* of tuple t1. The w component of this point is set to 0.
*
* @param t1 the tuple to be copied
* @since Java3D 1.2
*/
Vector4(const Tuple3<T>& t1): Tuple4<T>(t1.x, t1.y, t1.z, 0) { }
/**
* Sets the x,y,z components of this point to the corresponding
* components of tuple t1. The w component of this point is set to 1.
*
* NOTE:
* This method's name should be 'set'. Newer compilers support
* 'using Tuple4<T>::set;' and use the name 'set'.
* but for now, renamed to 'set3'.
*
* @param t1 the tuple to be copied
* @since Java3D 1.2
*/
void set3(const Tuple3<T>& t1) {
set(t1.x, t1.y, t1.z, 0);
}
/**
* Returns the squared length of this vector.
* @return the squared length of this vector
*/
T lengthSquared() const {
return this->x*this->x + this->y*this->y + this->z*this->z + this->w*this->w;
}
/**
* Returns the length of this vector.
* @return the length of this vector
*/
T length() const {
return VmUtil<T>::sqrt(lengthSquared());
}
/**
* Computes the dot product of the this vector and vector v1.
* @param v1 the other vector
* @return the dot product of this vector and vector v1
*/
T dot(const Vector4& v1) const {
return this->x*v1.x + this->y*v1.y + this->z*v1.z + this->w*v1.w;
}
/**
* Sets the value of this vector to the normalization of vector v1.
* @param v1 the un-normalized vector
*/
void normalize(const Vector4& v1) {
Tuple4<T>::set(v1);
normalize();
}
/**
* Normalizes this vector in place.
*/
void normalize() {
T d = length();
// zero-div may occur.
this->x /= d;
this->y /= d;
this->z /= d;
this->w /= d;
}
/**
* Returns the (4-space) angle in radians between this vector and
* the vector parameter; the return value is constrained to the
* range [0,PI].
* @param v1 the other vector
* @return the angle in radians in the range [0,PI]
*/
T angle(const Vector4& v1) const {
// zero div may occur.
T d = dot(v1);
T v1_length = v1.length();
T v_length = length();
// numerically, domain error may occur
return T(acos(d/v1_length/v_length));
}
// copy constructor and operator = is made by complier
Vector4& operator=(const Tuple4<T>& t) {
Tuple4<T>::operator=(t);
return *this;
}
};
template <class T>
inline
VM_VECMATH_NS::Vector4<T> operator*(T s, const VM_VECMATH_NS::Vector4<T>& t1) {
return operator*(s, (const VM_VECMATH_NS::Tuple4<T>&)t1);
}
#ifdef VM_INCLUDE_IO
template <class T>
inline
std::ostream& operator<<(std::ostream& o, const VM_VECMATH_NS::Vector4<T>& t1) {
return operator<<(o, (const VM_VECMATH_NS::Tuple4<T>&)t1);
}
#endif
typedef Vector4<double> Vector4d;
typedef Vector4<float> Vector4f;
VM_END_NS
#endif /* VECTOR4_H */