1 | /** |
---|
2 | @file Vector4.inl |
---|
3 | |
---|
4 | @maintainer Morgan McGuire, matrix@graphics3d.com |
---|
5 | |
---|
6 | @created 2002-07-09 |
---|
7 | @edited 2003-02-10 |
---|
8 | */ |
---|
9 | |
---|
10 | //---------------------------------------------------------------------------- |
---|
11 | |
---|
12 | inline unsigned int hashCode(const G3D::Vector4& v) { |
---|
13 | return v.hashCode(); |
---|
14 | } |
---|
15 | |
---|
16 | namespace G3D { |
---|
17 | |
---|
18 | //---------------------------------------------------------------------------- |
---|
19 | inline Vector4::Vector4() { |
---|
20 | x = y = z = w = 0; |
---|
21 | } |
---|
22 | |
---|
23 | //---------------------------------------------------------------------------- |
---|
24 | |
---|
25 | inline Vector4::Vector4 (float fX, float fY, float fZ, float fW) { |
---|
26 | x = fX; |
---|
27 | y = fY; |
---|
28 | z = fZ; |
---|
29 | w = fW; |
---|
30 | } |
---|
31 | |
---|
32 | //---------------------------------------------------------------------------- |
---|
33 | inline Vector4::Vector4 (float afCoordinate[4]) { |
---|
34 | x = afCoordinate[0]; |
---|
35 | y = afCoordinate[1]; |
---|
36 | z = afCoordinate[2]; |
---|
37 | w = afCoordinate[3]; |
---|
38 | } |
---|
39 | |
---|
40 | //---------------------------------------------------------------------------- |
---|
41 | inline Vector4::Vector4(const Vector4& rkVector) { |
---|
42 | x = rkVector.x; |
---|
43 | y = rkVector.y; |
---|
44 | z = rkVector.z; |
---|
45 | w = rkVector.w; |
---|
46 | } |
---|
47 | //---------------------------------------------------------------------------- |
---|
48 | inline Vector4::Vector4(const Vector3& rkVector, float fW) { |
---|
49 | x = rkVector.x; |
---|
50 | y = rkVector.y; |
---|
51 | z = rkVector.z; |
---|
52 | w = fW; |
---|
53 | } |
---|
54 | |
---|
55 | //---------------------------------------------------------------------------- |
---|
56 | inline float& Vector4::operator[] (int i) { |
---|
57 | return ((float*)this)[i]; |
---|
58 | } |
---|
59 | |
---|
60 | //---------------------------------------------------------------------------- |
---|
61 | inline const float& Vector4::operator[] (int i) const { |
---|
62 | return ((float*)this)[i]; |
---|
63 | } |
---|
64 | |
---|
65 | //---------------------------------------------------------------------------- |
---|
66 | inline Vector4::operator float* () { |
---|
67 | return (float*)this; |
---|
68 | } |
---|
69 | |
---|
70 | inline Vector4::operator const float* () const { |
---|
71 | return (float*)this; |
---|
72 | } |
---|
73 | |
---|
74 | //---------------------------------------------------------------------------- |
---|
75 | inline Vector4& Vector4::operator= (const Vector4& rkVector) { |
---|
76 | x = rkVector.x; |
---|
77 | y = rkVector.y; |
---|
78 | z = rkVector.z; |
---|
79 | w = rkVector.w; |
---|
80 | return *this; |
---|
81 | } |
---|
82 | |
---|
83 | //---------------------------------------------------------------------------- |
---|
84 | inline bool Vector4::operator== (const Vector4& rkVector) const { |
---|
85 | return ( (x == rkVector.x) && (y == rkVector.y) && (z == rkVector.z) && (w == rkVector.w)); |
---|
86 | } |
---|
87 | |
---|
88 | //---------------------------------------------------------------------------- |
---|
89 | inline bool Vector4::operator!= (const Vector4& rkVector) const { |
---|
90 | return ( x != rkVector.x || y != rkVector.y || z != rkVector.z || w != rkVector.w); |
---|
91 | } |
---|
92 | |
---|
93 | //---------------------------------------------------------------------------- |
---|
94 | inline Vector4 Vector4::operator+ (const Vector4& rkVector) const { |
---|
95 | return Vector4(x + rkVector.x, y + rkVector.y, z + rkVector.z, w + rkVector.w); |
---|
96 | } |
---|
97 | |
---|
98 | //---------------------------------------------------------------------------- |
---|
99 | inline Vector4 Vector4::operator- (const Vector4& rkVector) const { |
---|
100 | return Vector4(x - rkVector.x, y - rkVector.y, z - rkVector.z, w - rkVector.w); |
---|
101 | } |
---|
102 | |
---|
103 | //---------------------------------------------------------------------------- |
---|
104 | inline Vector4 Vector4::operator* (float fScalar) const { |
---|
105 | return Vector4(fScalar*x, fScalar*y, fScalar*z, fScalar*w); |
---|
106 | } |
---|
107 | |
---|
108 | //---------------------------------------------------------------------------- |
---|
109 | inline Vector4 Vector4::operator- () const { |
---|
110 | return Vector4( -x, -y, -z, -w); |
---|
111 | } |
---|
112 | |
---|
113 | //---------------------------------------------------------------------------- |
---|
114 | inline Vector4& Vector4::operator+= (const Vector4& rkVector) { |
---|
115 | x += rkVector.x; |
---|
116 | y += rkVector.y; |
---|
117 | z += rkVector.z; |
---|
118 | w += rkVector.w; |
---|
119 | return *this; |
---|
120 | } |
---|
121 | |
---|
122 | //---------------------------------------------------------------------------- |
---|
123 | inline Vector4& Vector4::operator-= (const Vector4& rkVector) { |
---|
124 | x -= rkVector.x; |
---|
125 | y -= rkVector.y; |
---|
126 | z -= rkVector.z; |
---|
127 | w -= rkVector.w; |
---|
128 | return *this; |
---|
129 | } |
---|
130 | |
---|
131 | //---------------------------------------------------------------------------- |
---|
132 | |
---|
133 | inline Vector4 Vector4::lerp(const Vector4& v, float alpha) const { |
---|
134 | return (*this) + (v - *this) * alpha; |
---|
135 | } |
---|
136 | |
---|
137 | |
---|
138 | //---------------------------------------------------------------------------- |
---|
139 | inline Vector4& Vector4::operator*= (float fScalar) { |
---|
140 | x *= fScalar; |
---|
141 | y *= fScalar; |
---|
142 | z *= fScalar; |
---|
143 | w *= fScalar; |
---|
144 | return *this; |
---|
145 | } |
---|
146 | |
---|
147 | |
---|
148 | //---------------------------------------------------------------------------- |
---|
149 | inline float Vector4::dot(const Vector4& rkVector) const { |
---|
150 | return x*rkVector.x + y*rkVector.y + z*rkVector.z + w*rkVector.w; |
---|
151 | } |
---|
152 | |
---|
153 | //---------------------------------------------------------------------------- |
---|
154 | inline Vector4 Vector4::min(const Vector4 &v) const { |
---|
155 | return Vector4(G3D::min(v.x, x), G3D::min(v.y, y), G3D::min(v.z, z), G3D::min(v.w, w)); |
---|
156 | } |
---|
157 | |
---|
158 | //---------------------------------------------------------------------------- |
---|
159 | inline Vector4 Vector4::max(const Vector4 &v) const { |
---|
160 | return Vector4(G3D::max(v.x, x), G3D::max(v.y, y), G3D::max(v.z, z), G3D::max(v.w, w)); |
---|
161 | } |
---|
162 | |
---|
163 | //---------------------------------------------------------------------------- |
---|
164 | inline bool Vector4::isZero() const { |
---|
165 | return (x == 0.0f) && (y == 0.0f) && (z == 0.0f) && (w == 0.0f); |
---|
166 | } |
---|
167 | |
---|
168 | //---------------------------------------------------------------------------- |
---|
169 | |
---|
170 | inline bool Vector4::isFinite() const { |
---|
171 | return G3D::isFinite(x) && G3D::isFinite(y) && G3D::isFinite(z) && G3D::isFinite(w); |
---|
172 | } |
---|
173 | |
---|
174 | //---------------------------------------------------------------------------- |
---|
175 | |
---|
176 | inline bool Vector4::isUnit() const { |
---|
177 | return squaredLength() == 1.0; |
---|
178 | } |
---|
179 | |
---|
180 | //---------------------------------------------------------------------------- |
---|
181 | |
---|
182 | inline float Vector4::length() const { |
---|
183 | return sqrtf(squaredLength()); |
---|
184 | } |
---|
185 | |
---|
186 | //---------------------------------------------------------------------------- |
---|
187 | |
---|
188 | inline float Vector4::squaredLength() const { |
---|
189 | return x * x + y * y + z * z + w * w; |
---|
190 | } |
---|
191 | |
---|
192 | } |
---|
193 | |
---|