Debian Codegraph @

tools/physicalModeling/mesh2faust/vega/libraries/include/vec3.h

raw symbols SCIP
1 /*
2 * Copyright (c) 2008, Carnegie Mellon University
3 * All rights reserved.
4 *
5 * Code author: Jernej Barbic
6 * Research: Jernej Barbic, Doug L. James
7 * Funding: NSF, Link Foundation
8 *
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions are met:
11 * * Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * * Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * * Neither the name of Carnegie Mellon University, nor the
17 * names of its contributors may be used to endorse or promote products
18 * derived from this software without specific prior written permission.
19 *
20 * THIS SOFTWARE IS PROVIDED BY CARNEGIE MELLON UNIVERSITY ``AS IS'' AND ANY
21 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
22 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE* DISCLAIMED. IN NO EVENT SHALL CARNEGIE MELLON UNIVERSITY BE LIABLE FOR ANY
23 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
24 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
25 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
26 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
28 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29
30 A simple class for vector algebra on 3D vectors
31 (summation, normalization, dot product, cross product, etc.).
32 */
33
34 #ifndef _MINIVEC3_H_
35 #define _MINIVEC3_H_
36
37 #include <stdio.h>
38 #include <math.h>
39 #include <ostream>
40
41 // forward declarations for external helper functions
42 template <typename real> class Vec3;
43 template <typename real> inline Vec3<real> operator* (real alpha, Vec3<real> vec);
44 template <typename real> inline Vec3<real> operator/ (real alpha, Vec3<real> vec);
45 template <typename real> inline real dot(const Vec3<real> & vec1, const Vec3<real> & vec2);
46 template <typename real> inline Vec3<real> cross(const Vec3<real> & vec1, const Vec3<real> & vec2); // cross product
47 template <typename real> inline real len(const Vec3<real> & vec); // length of vector
48 template <typename real> inline real len2(const Vec3<real> & vec); // length^2 of vector
49 template <typename real> inline Vec3<real> norm(const Vec3<real> & vec); // returns normalized vector (unit length)
50 template <typename real> inline std::ostream & operator << (std::ostream & s, const Vec3<real> & v);
51
52 typedef Vec3<double> Vec3d;
53 typedef Vec3<float> Vec3f;
54
55 template <class real>
56 class Vec3
57 {
58 public:
59 inline Vec3() {}
60 inline Vec3(real x, real y, real z) { elt[0]=x; elt[1]=y; elt[2]=z; }
61 inline Vec3(real entry); // create a vector with all entries "entry" (can create zero vector for entry=0.0)
62 inline Vec3(const real * vec); // create a vector from the array of three reals pointed to by "vec"
63 inline Vec3(const Vec3 & vec);
64
65 inline void set(real x0, real x1, real x2); // assign vector [x0, x1, x2]
66 inline void set(real value); // set all elements to value
67
68 inline Vec3 & operator=(const Vec3 & source);
69 inline bool operator==(const Vec3 & vec) const;
70
71 inline const Vec3 operator+ (const Vec3 & vec) const;
72 inline Vec3 & operator+= (const Vec3 & vec);
73
74 inline const Vec3 operator- (const Vec3 & vec) const;
75 inline Vec3 & operator-= (const Vec3 & vec);
76
77 inline const Vec3 operator* (real scalar) const;
78 inline Vec3 & operator*= (real scalar);
79
80 inline Vec3 operator/ (real scalar) const;
81 inline Vec3 & operator/= (real scalar);
82
83 inline void normalize(); // normalize itself
84
85 void print() const; // print the vector out
86
87 inline real & operator[] (int index); // v[i] returns i-th entry of v
88 inline const real & operator[] (int index) const;
89
90 // finds a unit vector orthogonal to this vector
91 Vec3 findOrthonormalVector() const;
92
93 // copies the vector into an array of length 3
94 inline void convertToArray(real * vecArray) const;
95
96 protected:
97 real elt[3];
98 };
99
100 // === below is the implementation ===
101
102 template <class real>
103 inline Vec3<real>::Vec3(real entry)
104 {
105 elt[0] = entry;
106 elt[1] = entry;
107 elt[2] = entry;
108 }
109
110 template <class real>
111 inline Vec3<real>::Vec3(const real * vec)
112 {
113 elt[0] = vec[0];
114 elt[1] = vec[1];
115 elt[2] = vec[2];
116 }
117
118 template <class real>
119 inline Vec3<real>::Vec3(const Vec3<real> & vec)
120 {
121 elt[0] = vec.elt[0];
122 elt[1] = vec.elt[1];
123 elt[2] = vec.elt[2];
124 }
125
126 template <class real>
127 inline Vec3<real> & Vec3<real>::operator=(const Vec3<real> & source)
128 {
129 elt[0] = source.elt[0];
130 elt[1] = source.elt[1];
131 elt[2] = source.elt[2];
132
133 return *this;
134 }
135
136 template <class real>
137 inline bool Vec3<real>::operator==(const Vec3<real> & vec) const
138 {
139 return ((elt[0] == vec[0]) &&
140 (elt[1] == vec[1]) &&
141 (elt[2] == vec[2]));
142 }
143
144 template <class real>
145 inline Vec3<real> operator* (real scalar, const Vec3<real> & vec)
146 {
147 Vec3<real> result = vec;
148 result.elt[0] *= scalar;
149 result.elt[1] *= scalar;
150 result.elt[2] *= scalar;
151
152 return result;
153 }
154
155 template <class real>
156 inline Vec3<real> operator/ (real scalar, const Vec3<real> & vec)
157 {
158 Vec3<real> result = vec;
159 result.elt[0] /= scalar;
160 result.elt[1] /= scalar;
161 result.elt[2] /= scalar;
162
163 return result;
164 }
165
166 template <class real>
167 inline const Vec3<real> Vec3<real>::operator+ (const Vec3<real> & vec) const
168 {
169 Vec3<real> sum = *this;
170 sum.elt[0] += vec.elt[0];
171 sum.elt[1] += vec.elt[1];
172 sum.elt[2] += vec.elt[2];
173
174 return sum;
175 }
176
177 template <class real>
178 inline Vec3<real> & Vec3<real>::operator+= (const Vec3<real> & vec)
179 {
180 elt[0] += vec.elt[0];
181 elt[1] += vec.elt[1];
182 elt[2] += vec.elt[2];
183
184 return *this;
185 }
186
187 template <class real>
188 inline const Vec3<real> Vec3<real>::operator- (const Vec3<real> & vec) const
189 {
190 Vec3<real> sum = *this;
191 sum.elt[0] -= vec.elt[0];
192 sum.elt[1] -= vec.elt[1];
193 sum.elt[2] -= vec.elt[2];
194
195 return sum;
196 }
197
198 template <class real>
199 inline Vec3<real> & Vec3<real>::operator-= (const Vec3<real> & vec)
200 {
201 elt[0] -= vec.elt[0];
202 elt[1] -= vec.elt[1];
203 elt[2] -= vec.elt[2];
204
205 return *this;
206 }
207
208 template <class real>
209 inline real & Vec3<real>::operator[] (int index)
210 {
211 return elt[index];
212 }
213
214 template <class real>
215 inline const real & Vec3<real>::operator[] (int index) const
216 {
217 return elt[index];
218 }
219
220 template <class real>
221 inline real dot(const Vec3<real> & vec1, const Vec3<real> & vec2)
222 {
223 return (vec1[0] * vec2[0] + vec1[1] * vec2[1] + vec1[2] * vec2[2]);
224 }
225
226 template <class real>
227 inline Vec3<real> cross(const Vec3<real> & vec1, const Vec3<real> & vec2)
228 {
229 Vec3<real> result(vec1[1] * vec2[2] - vec2[1] * vec1[2],
230 -vec1[0] * vec2[2] + vec2[0] * vec1[2],
231 vec1[0] * vec2[1] - vec2[0] * vec1[1]);
232
233 return result;
234 }
235
236 template <class real>
237 inline Vec3<real> norm(const Vec3<real> & vec)
238 {
239 real norm2 = dot(vec,vec);
240 Vec3<real> result = vec;
241 result *= 1.0 / sqrt(norm2);
242
243 return result;
244 }
245
246 template <class real>
247 inline Vec3<real> & Vec3<real>::operator*= (real scalar)
248 {
249 elt[0] *= scalar;
250 elt[1] *= scalar;
251 elt[2] *= scalar;
252 return *this;
253 }
254
255 template <class real>
256 inline const Vec3<real> Vec3<real>::operator* (real scalar) const
257 {
258 return (Vec3<real>(elt[0]*scalar, elt[1]*scalar, elt[2]*scalar));
259 }
260
261 template <class real>
262 inline Vec3<real> Vec3<real>::operator/ (real scalar) const
263 {
264 return (Vec3<real>(elt[0]/scalar, elt[1]/scalar, elt[2]/scalar));
265 }
266
267 template <class real>
268 inline Vec3<real> & Vec3<real>::operator/= (real scalar)
269 {
270 elt[0] /= scalar;
271 elt[1] /= scalar;
272 elt[2] /= scalar;
273 return *this;
274 }
275
276 template <class real>
277 inline real len(const Vec3<real> & vec)
278 {
279 return(sqrt(dot(vec,vec)));
280 }
281
282 template <class real>
283 inline real len2(const Vec3<real> & vec)
284 {
285 return(dot(vec,vec));
286 }
287
288 template <class real>
289 inline std::ostream & operator << (std::ostream & s, const Vec3<real> & v)
290 {
291 real a = v[0];
292 real b = v[1];
293 real c = v[2];
294
295 return(s << '[' << a << ' ' << b << ' ' << c << ']');
296 }
297
298 template <class real>
299 inline void Vec3<real>::convertToArray(real * vecArray) const
300 {
301 vecArray[0] = elt[0];
302 vecArray[1] = elt[1];
303 vecArray[2] = elt[2];
304 }
305
306 template <class real>
307 inline void Vec3<real>::normalize()
308 {
309 real invMag = 1.0 / sqrt(elt[0]*elt[0] + elt[1]*elt[1] + elt[2]*elt[2]);
310 elt[0] *= invMag;
311 elt[1] *= invMag;
312 elt[2] *= invMag;
313 }
314
315 template <class real>
316 inline void Vec3<real>::print() const
317 {
318 real a = elt[0];
319 real b = elt[1];
320 real c = elt[2];
321
322 printf("[%G %G %G]\n", a, b, c);
323 }
324
325 template <class real>
326 inline void Vec3<real>::set(real x0, real x1, real x2) // assign vector [x0, x1, x2]
327 {
328 elt[0] = x0;
329 elt[1] = x1;
330 elt[2] = x2;
331 }
332
333 template <class real>
334 inline void Vec3<real>::set(real value) // set all elements to value
335 {
336 elt[0] = value;
337 elt[1] = value;
338 elt[2] = value;
339 }
340
341 // Given an input vector v, find a unit vector that is orthogonal to it
342 template <class real>
343 Vec3<real> Vec3<real>::findOrthonormalVector() const
344 {
345 // find smallest abs component of v
346 int smallestIndex = 0;
347 for(int dim=1; dim<3; dim++)
348 if (fabs(elt[dim]) < fabs(elt[smallestIndex]))
349 smallestIndex = dim;
350
351 Vec3<real> axis(0.0, 0.0, 0.0);
352 axis[smallestIndex] = 1.0;
353
354 // this cross-product will be non-zero (as long as v is not zero)
355 Vec3<real> result = norm(cross(elt, axis));
356 return result;
357 }
358
359 #endif
360