C:/jkyprian/devel/lib3ds/lib3ds/vector.c

00001 /*
00002  * The 3D Studio File Format Library
00003  * Copyright (C) 1996-2007 by Jan Eric Kyprianidis <www.kyprianidis.com>
00004  * All rights reserved.
00005  *
00006  * This program is  free  software;  you can redistribute it and/or modify it
00007  * under the terms of the  GNU Lesser General Public License  as published by 
00008  * the  Free Software Foundation;  either version 2.1 of the License,  or (at 
00009  * your option) any later version.
00010  *
00011  * This  program  is  distributed in  the  hope that it will  be useful,  but
00012  * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
00013  * or  FITNESS FOR A  PARTICULAR PURPOSE.  See the  GNU Lesser General Public  
00014  * License for more details.
00015  *
00016  * You should  have received  a copy of the GNU Lesser General Public License
00017  * along with  this program;  if not, write to the  Free Software Foundation,
00018  * Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
00019  *
00020  * $Id: vector.c,v 1.12 2007/06/20 17:04:09 jeh Exp $
00021  */
00022 #include <lib3ds/vector.h>
00023 #include <math.h>
00024 
00025 
00044 void
00045 lib3ds_vector_zero(Lib3dsVector c)
00046 {
00047   int i;
00048   for (i=0; i<3; ++i) {
00049     c[i]=0.0f;
00050   }
00051 }
00052 
00053 
00062 void
00063 lib3ds_vector_copy(Lib3dsVector dest, Lib3dsVector src)
00064 {
00065   int i;
00066   for (i=0; i<3; ++i) {
00067     dest[i]=src[i];
00068   }
00069 }
00070 
00071 
00079 void
00080 lib3ds_vector_neg(Lib3dsVector c)
00081 {
00082   int i;
00083   for (i=0; i<3; ++i) {
00084     c[i]=-c[i];
00085   }
00086 }
00087 
00088 
00098 void
00099 lib3ds_vector_add(Lib3dsVector c, Lib3dsVector a, Lib3dsVector b)
00100 {
00101   int i;
00102   for (i=0; i<3; ++i) {
00103     c[i]=a[i]+b[i];
00104   }
00105 }
00106 
00107 
00117 void
00118 lib3ds_vector_sub(Lib3dsVector c, Lib3dsVector a, Lib3dsVector b)
00119 {
00120   int i;
00121   for (i=0; i<3; ++i) {
00122     c[i]=a[i]-b[i];
00123   }
00124 }
00125 
00126 
00135 void
00136 lib3ds_vector_scalar(Lib3dsVector c, Lib3dsFloat k)
00137 {
00138   int i;
00139   for (i=0; i<3; ++i) {
00140     c[i]*=k;
00141   }
00142 }
00143 
00144 
00154 void
00155 lib3ds_vector_cross(Lib3dsVector c, Lib3dsVector a, Lib3dsVector b)
00156 {
00157   c[0]=a[1]*b[2] - a[2]*b[1];
00158   c[1]=a[2]*b[0] - a[0]*b[2];
00159   c[2]=a[0]*b[1] - a[1]*b[0];
00160 }
00161 
00162 
00173 Lib3dsFloat
00174 lib3ds_vector_dot(Lib3dsVector a, Lib3dsVector b)
00175 {
00176   return(a[0]*b[0] + a[1]*b[1] + a[2]*b[2]);
00177 }
00178 
00179 
00191 Lib3dsFloat
00192 lib3ds_vector_squared(Lib3dsVector c)
00193 {
00194   return(c[0]*c[0] + c[1]*c[1] + c[2]*c[2]);
00195 }
00196 
00197 
00209 Lib3dsFloat
00210 lib3ds_vector_length(Lib3dsVector c)
00211 {
00212   return((Lib3dsFloat)sqrt(c[0]*c[0] + c[1]*c[1] + c[2]*c[2]));
00213 }
00214 
00215 
00225 void
00226 lib3ds_vector_normalize(Lib3dsVector c)
00227 {
00228   Lib3dsFloat l,m;
00229 
00230   l=(Lib3dsFloat)sqrt(c[0]*c[0] + c[1]*c[1] + c[2]*c[2]);
00231   if (fabs(l)<LIB3DS_EPSILON) {
00232     if ((c[0]>=c[1]) && (c[0]>=c[2])) {
00233       c[0]=1.0f;
00234       c[1]=c[2]=0.0f;
00235     }
00236     else
00237     if (c[1]>=c[2]) {
00238       c[1]=1.0f;
00239       c[0]=c[2]=0.0f;
00240     }
00241     else {
00242       c[2]=1.0f;
00243       c[0]=c[1]=0.0f;
00244     }
00245   }
00246   else {
00247     m=1.0f/l;
00248     c[0]*=m;
00249     c[1]*=m;
00250     c[2]*=m;
00251   }
00252 }
00253 
00254 
00267 void
00268 lib3ds_vector_normal(Lib3dsVector n, Lib3dsVector a, Lib3dsVector b, Lib3dsVector c)
00269 {
00270   Lib3dsVector p,q;
00271 
00272   lib3ds_vector_sub(p,c,b);
00273   lib3ds_vector_sub(q,a,b);
00274   lib3ds_vector_cross(n,p,q);
00275   lib3ds_vector_normalize(n);
00276 }
00277 
00278 
00291 void
00292 lib3ds_vector_transform(Lib3dsVector c, Lib3dsMatrix m, Lib3dsVector a)
00293 {
00294   c[0]= m[0][0]*a[0] + m[1][0]*a[1] + m[2][0]*a[2] + m[3][0];
00295   c[1]= m[0][1]*a[0] + m[1][1]*a[1] + m[2][1]*a[2] + m[3][1];
00296   c[2]= m[0][2]*a[0] + m[1][2]*a[1] + m[2][2]*a[2] + m[3][2];
00297 }
00298 
00299 
00314 void
00315 lib3ds_vector_cubic(Lib3dsVector c, Lib3dsVector a, Lib3dsVector p, Lib3dsVector q,
00316   Lib3dsVector b, Lib3dsFloat t)
00317 {
00318   Lib3dsDouble x,y,z,w;   
00319 
00320   x=2*t*t*t - 3*t*t + 1;
00321   y=-2*t*t*t + 3*t*t;
00322   z=t*t*t - 2*t*t + t;
00323   w=t*t*t - t*t;
00324   c[0]=(Lib3dsFloat)(x*a[0] + y*b[0] + z*p[0] + w*q[0]);
00325   c[1]=(Lib3dsFloat)(x*a[1] + y*b[1] + z*p[1] + w*q[1]);
00326   c[2]=(Lib3dsFloat)(x*a[2] + y*b[2] + z*p[2] + w*q[2]);
00327 }
00328 
00329 
00337 void 
00338 lib3ds_vector_min(Lib3dsVector c, Lib3dsVector a)
00339 {
00340   int i;
00341   for (i=0; i<3; ++i) {
00342     if (a[i]<c[i]) {
00343       c[i] = a[i];
00344     }
00345   }
00346 }
00347 
00348 
00356 void 
00357 lib3ds_vector_max(Lib3dsVector c, Lib3dsVector a)
00358 {
00359   int i;
00360   for (i=0; i<3; ++i) {
00361     if (a[i]>c[i]) {
00362       c[i] = a[i];
00363     }
00364   }
00365 }
00366 
00367 
00371 void
00372 lib3ds_vector_dump(Lib3dsVector c)
00373 {
00374   fprintf(stderr, "%f %f %f\n", c[0], c[1], c[2]);
00375 }
00376 

Hosted by
SourceForge.net Logo
Generated at Wed Jun 20 18:51:36 2007 by Doxygen 1.5.2