2 * The 3D Studio File Format Library
3 * Copyright (C) 1996-2001 by J.E. Hoffmann <je-h@gmx.net>
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU Lesser General Public License as published by
8 * the Free Software Foundation; either version 2.1 of the License, or (at
9 * your option) any later version.
11 * This program is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
13 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
14 * License for more details.
16 * You should have received a copy of the GNU Lesser General Public License
17 * along with this program; if not, write to the Free Software Foundation,
18 * Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 * $Id: vector.c,v 1.9 2004/11/16 07:41:44 efalk Exp $
23 #include <lib3ds/vector.h>
28 * \defgroup vector Vector Mathematics
30 * \author J.E. Hoffmann <je-h@gmx.net>
33 * \typedef Lib3dsVector
39 * Clear a vector to zero.
41 * \param c Vector to clear.
46 lib3ds_vector_zero(Lib3dsVector c)
58 * \param dest Destination vector.
59 * \param src Source vector.
64 lib3ds_vector_copy(Lib3dsVector dest, Lib3dsVector src)
76 * \param c Vector to negate.
81 lib3ds_vector_neg(Lib3dsVector c)
94 * \param a First addend.
95 * \param b Second addend.
100 lib3ds_vector_add(Lib3dsVector c, Lib3dsVector a, Lib3dsVector b)
103 for (i=0; i<3; ++i) {
110 * Subtract two vectors.
119 lib3ds_vector_sub(Lib3dsVector c, Lib3dsVector a, Lib3dsVector b)
122 for (i=0; i<3; ++i) {
129 * Multiply a vector by a scalar.
131 * \param c Vector to be multiplied.
137 lib3ds_vector_scalar(Lib3dsVector c, Lib3dsFloat k)
140 for (i=0; i<3; ++i) {
147 * Compute cross product.
150 * \param a First vector.
151 * \param b Second vector.
156 lib3ds_vector_cross(Lib3dsVector c, Lib3dsVector a, Lib3dsVector b)
158 c[0]=a[1]*b[2] - a[2]*b[1];
159 c[1]=a[2]*b[0] - a[0]*b[2];
160 c[2]=a[0]*b[1] - a[1]*b[0];
165 * Compute dot product.
167 * \param a First vector.
168 * \param b Second vector.
170 * \return Dot product.
175 lib3ds_vector_dot(Lib3dsVector a, Lib3dsVector b)
177 return(a[0]*b[0] + a[1]*b[1] + a[2]*b[2]);
182 * Compute square of vector.
184 * Computes x*x + y*y + z*z.
186 * \param c Vector to square.
188 * \return Square of vector.
193 lib3ds_vector_squared(Lib3dsVector c)
195 return(c[0]*c[0] + c[1]*c[1] + c[2]*c[2]);
200 * Compute length of vector.
202 * Computes |c| = sqrt(x*x + y*y + z*z)
204 * \param c Vector to compute.
206 * \return Length of vector.
211 lib3ds_vector_length(Lib3dsVector c)
213 return((Lib3dsFloat)sqrt(c[0]*c[0] + c[1]*c[1] + c[2]*c[2]));
218 * Normalize a vector.
220 * Scales a vector so that its length is 1.0.
222 * \param c Vector to normalize.
227 lib3ds_vector_normalize(Lib3dsVector c)
231 l=(Lib3dsFloat)sqrt(c[0]*c[0] + c[1]*c[1] + c[2]*c[2]);
232 if (fabs(l)<LIB3DS_EPSILON) {
233 if ((c[0]>=c[1]) && (c[0]>=c[2])) {
257 * Compute a vector normal to two line segments.
259 * Computes the normal vector to the lines b-a and b-c.
261 * \param n Returned normal vector.
262 * \param a Endpoint of first line.
263 * \param b Base point of both lines.
264 * \param c Endpoint of second line.
269 lib3ds_vector_normal(Lib3dsVector n, Lib3dsVector a, Lib3dsVector b, Lib3dsVector c)
273 lib3ds_vector_sub(p,c,b);
274 lib3ds_vector_sub(q,a,b);
275 lib3ds_vector_cross(n,p,q);
276 lib3ds_vector_normalize(n);
281 * Multiply a point by a transformation matrix.
283 * Applies the given transformation matrix to the given point. With some
284 * transformation matrices, a vector may also be transformed.
287 * \param m Transformation matrix.
288 * \param a Input point.
293 lib3ds_vector_transform(Lib3dsVector c, Lib3dsMatrix m, Lib3dsVector a)
295 c[0]= m[0][0]*a[0] + m[1][0]*a[1] + m[2][0]*a[2] + m[3][0];
296 c[1]= m[0][1]*a[0] + m[1][1]*a[1] + m[2][1]*a[2] + m[3][1];
297 c[2]= m[0][2]*a[0] + m[1][2]*a[1] + m[2][2]*a[2] + m[3][2];
302 * Compute a point on a cubic spline.
304 * Computes a point on a parametric Bezier spline.
307 * \param a First endpoint of the spline.
308 * \param p First tangent vector of the spline.
309 * \param q Second tangent vector of the spline.
310 * \param b Second endpoint of the spline.
311 * \param t Spline parameter [0. 1.]
316 lib3ds_vector_cubic(Lib3dsVector c, Lib3dsVector a, Lib3dsVector p, Lib3dsVector q,
317 Lib3dsVector b, Lib3dsFloat t)
319 Lib3dsDouble x,y,z,w;
321 x=2*t*t*t - 3*t*t + 1;
325 c[0]=(Lib3dsFloat)(x*a[0] + y*b[0] + z*p[0] + w*q[0]);
326 c[1]=(Lib3dsFloat)(x*a[1] + y*b[1] + z*p[1] + w*q[1]);
327 c[2]=(Lib3dsFloat)(x*a[2] + y*b[2] + z*p[2] + w*q[2]);
332 * c[i] = min(c[i], a[i]);
334 * Computes minimum values of x,y,z independently.
339 lib3ds_vector_min(Lib3dsVector c, Lib3dsVector a)
342 for (i=0; i<3; ++i) {
351 * c[i] = max(c[i], a[i]);
353 * Computes maximum values of x,y,z independently.
358 lib3ds_vector_max(Lib3dsVector c, Lib3dsVector a)
361 for (i=0; i<3; ++i) {
373 lib3ds_vector_dump(Lib3dsVector c)
375 fprintf(stderr, "%f %f %f\n", c[0], c[1], c[2]);