vrecko
virtual reality framework
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
gm_vec4.h
Go to the documentation of this file.
1 // gmVector4.h - 4 element vector class
2 //
3 // libgm++: Graphics Math Library
4 // Ferdi Scheepers and Stephen F May
5 // 15 June 1994
6 // ----------------------------------
7 // modified: October 2002
8 // Pavel Kolcarek, kolcarek@fi.muni.cz
9 //
10 
11 #ifndef GM_VECTOR4_H
12 #define GM_VECTOR4_H
13 
14 #include <stdio.h>
15 #include <stdlib.h>
16 #include <assert.h>
17 #include <gm_utils.h>
18 
19 class gmVector4 {
20 
21 protected:
22  double v_[4];
23 
24 public:
25  gmVector4();
26  gmVector4(const gmVector4&);
27  gmVector4(double, double, double, double);
28 
29  // array access
30 
31  double& operator [](int);
32  const double& operator [](int) const;
33 
34  // assignment
35 
36  gmVector4& assign(double, double, double, double);
38 
39  // math operators
40 
43  gmVector4& operator *=(double);
44  gmVector4& operator /=(double);
45 
46  gmVector4 operator +(const gmVector4&) const;
47  gmVector4 operator -(const gmVector4&) const;
48  gmVector4 operator -() const;
49  gmVector4 operator *(double) const;
50  gmVector4 operator /(double) const;
51 
52 friend gmVector4 operator *(double, const gmVector4&);
53 
54  bool operator ==(const gmVector4&) const;
55  bool operator !=(const gmVector4&) const;
56 
57  // operations
58 
59  gmVector4& clamp(double, double);
60  double length() const;
61  double lengthSquared() const;
63 
64  void copyTo(float [4]) const;
65  void copyTo(double [4]) const;
66 
67 friend double distance(const gmVector4&, const gmVector4&);
68 friend double distanceSquared(const gmVector4&, const gmVector4&);
69 friend double dot(const gmVector4&, const gmVector4&);
70 friend gmVector4 lerp(double, const gmVector4&, const gmVector4&);
71 
72  // output
73 
74 friend ostream & operator << ( ostream &, const gmVector4 & );
75 };
76 
77 // CONSTRUCTORS
78 
80 {
81  v_[0] = v_[1] = v_[2] = v_[3] = 0;
82 }
83 
85 {
86  v_[0] = v.v_[0]; v_[1] = v.v_[1]; v_[2] = v.v_[2]; v_[3] = v.v_[3];
87 }
88 
89 inline gmVector4::gmVector4(double x, double y, double z, double a)
90 {
91  v_[0] = x; v_[1] = y; v_[2] = z; v_[3] = a;
92 }
93 
94 // ARRAY ACCESS
95 
96 inline double& gmVector4::operator [](int i)
97 {
98  assert(i == 0 || i == 1 || i == 2 || i == 3);
99  return v_[i];
100 }
101 
102 inline const double& gmVector4::operator [](int i) const
103 {
104  assert(i == 0 || i == 1 || i == 2 || i == 3);
105  return v_[i];
106 }
107 
108 // ASSIGNMENT
109 
110 inline gmVector4& gmVector4::assign(double x, double y, double z, double a)
111 {
112  v_[0] = x; v_[1] = y; v_[2] = z; v_[3] = a;
113  return *this;
114 }
115 
117 {
118  v_[0] = v[0]; v_[1] = v[1]; v_[2] = v[2]; v_[3] = v[3];
119  return *this;
120 }
121 
122 // MATH OPERATORS
123 
125 {
126  v_[0] += v[0]; v_[1] += v[1]; v_[2] += v[2]; v_[3] += v[3];
127  return *this;
128 }
129 
131 {
132  v_[0] -= v[0]; v_[1] -= v[1]; v_[2] -= v[2]; v_[3] -= v[3];
133  return *this;
134 }
135 
137 {
138  v_[0] *= c; v_[1] *= c; v_[2] *= c; v_[3] *= c;
139  return *this;
140 }
141 
143 {
144  assert(!gmIsZero(c));
145  v_[0] /= c; v_[1] /= c; v_[2] /= c; v_[3] /= c;
146  return *this;
147 }
148 
150 {
151  return gmVector4(v_[0] + v[0], v_[1] + v[1], v_[2] + v[2], v_[3] + v[3]);
152 }
153 
155 {
156  return gmVector4(v_[0] - v[0], v_[1] - v[1], v_[2] - v[2], v_[3] - v[3]);
157 }
158 
160 {
161  return gmVector4(-v_[0], -v_[1], -v_[2], -v_[3]);
162 }
163 
164 inline gmVector4 gmVector4::operator *(double c) const
165 {
166  return gmVector4(v_[0] * c, v_[1] * c, v_[2] * c, v_[3] * c);
167 }
168 
169 inline gmVector4 gmVector4::operator /(double c) const
170 {
171  assert(!gmIsZero(c));
172  return gmVector4(v_[0] / c, v_[1] / c, v_[2] / c, v_[3] / c);
173 }
174 
175 inline gmVector4 operator *(double c, const gmVector4& v)
176 {
177  return gmVector4(c * v[0], c * v[1], c * v[2], c * v[3]);
178 }
179 
180 inline bool gmVector4::operator ==(const gmVector4& v) const
181 {
182  return (gmFuzEQ(v_[0], v[0]) && gmFuzEQ(v_[1], v[1]) &&
183  gmFuzEQ(v_[2], v[2]) && gmFuzEQ(v_[3], v[3]));
184 }
185 
186 inline bool gmVector4::operator !=(const gmVector4& v) const
187 {
188  return (!(*this == v));
189 }
190 
191 // OPERATIONS
192 
193 inline gmVector4& gmVector4::clamp(double lo, double hi)
194 {
195  gmClamp(v_[0], lo, hi); gmClamp(v_[1], lo, hi);
196  gmClamp(v_[2], lo, hi); gmClamp(v_[3], lo, hi);
197  return *this;
198 }
199 
200 inline double gmVector4::length() const
201 {
202  return sqrt(gmSqr(v_[0]) + gmSqr(v_[1]) + gmSqr(v_[2]) + gmSqr(v_[3]));
203 }
204 
205 inline double gmVector4::lengthSquared() const
206 {
207  return gmSqr(v_[0]) + gmSqr(v_[1]) + gmSqr(v_[2]) + gmSqr(v_[3]);
208 }
209 
211 {
212  double len = length();
213  assert(!gmIsZero(len));
214  *this /= len;
215  return *this;
216 }
217 
218 inline void gmVector4::copyTo(float f[4]) const
219 {
220  f[0] = v_[0]; f[1] = v_[1]; f[2] = v_[2]; f[3] = v_[3];
221 }
222 
223 inline void gmVector4::copyTo(double f[4]) const
224 {
225  f[0] = v_[0]; f[1] = v_[1]; f[2] = v_[2]; f[3] = v_[3];
226 }
227 
228 inline double distance(const gmVector4& v1, const gmVector4& v2)
229 {
230  return sqrt(gmSqr(v1[0] - v2[0]) + gmSqr(v1[1] - v2[1]) +
231  gmSqr(v1[2] - v2[2]) + gmSqr(v1[3] - v2[3]));
232 }
233 
234 inline double distanceSquared(const gmVector4& v1, const gmVector4& v2)
235 {
236  return gmSqr(v1[0] - v2[0]) + gmSqr(v1[1] - v2[1]) +
237  gmSqr(v1[2] - v2[2]) + gmSqr(v1[3] - v2[3]);
238 }
239 
240 inline double dot(const gmVector4& v1, const gmVector4& v2)
241 {
242  return v1[0] * v2[0] + v1[1] * v2[1] + v1[2] * v2[2] + v1[3] * v2[3];
243 }
244 
245 inline gmVector4 lerp(double f, const gmVector4& v1, const gmVector4& v2)
246 {
247  return v1 + ((v2 - v1) * f);
248 }
249 
250 // OUTPUT
251 
252 inline ostream & operator << ( ostream& os, const gmVector4& v)
253 {
254  os << "< " << v[0] << " " << v[1] << " " << v[2] << " " << v[3] << " >";
255  return os;
256 }
257 
258 #endif