Home | API | File List | Examples | Download

lib3ds_vector.c

00001 /*
00002     Copyright (C) 1996-2008 by Jan Eric Kyprianidis <www.kyprianidis.com>
00003     All rights reserved.
00004     
00005     This program is free  software: you can redistribute it and/or modify 
00006     it under the terms of the GNU Lesser General Public License as published 
00007     by the Free Software Foundation, either version 2.1 of the License, or 
00008     (at your option) any later version.
00009 
00010     Thisprogram  is  distributed in the hope that it will be useful, 
00011     but WITHOUT ANY WARRANTY; without even the implied warranty of 
00012     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 
00013     GNU Lesser General Public License for more details.
00014     
00015     You should  have received a copy of the GNU Lesser General Public License
00016     along with  this program; If not, see <http://www.gnu.org/licenses/>. 
00017 */
00018 
00022 #include "lib3ds_impl.h"
00023 
00024 
00025 void 
00026 lib3ds_vector_make(float c[3], float x, float y, float z) {
00027     c[0] = x;
00028     c[1] = y;
00029     c[2] = z;
00030 }
00031 
00032 
00033 void
00034 lib3ds_vector_zero(float c[3]) {
00035     int i;
00036     for (i = 0; i < 3; ++i) {
00037         c[i] = 0.0f;
00038     }
00039 }
00040 
00041 
00042 void
00043 lib3ds_vector_copy(float dst[3], float src[3]) {
00044     int i;
00045     for (i = 0; i < 3; ++i) {
00046         dst[i] = src[i];
00047     }
00048 }
00049 
00050 
00058 void
00059 lib3ds_vector_add(float c[3], float a[3], float b[3]) {
00060     int i;
00061     for (i = 0; i < 3; ++i) {
00062         c[i] = a[i] + b[i];
00063     }
00064 }
00065 
00066 
00074 void
00075 lib3ds_vector_sub(float c[3], float a[3], float b[3]) {
00076     int i;
00077     for (i = 0; i < 3; ++i) {
00078         c[i] = a[i] - b[i];
00079     }
00080 }
00081 
00082 
00090 void
00091 lib3ds_vector_scalar_mul(float c[3], float a[3], float k) {
00092     int i;
00093     for (i = 0; i < 3; ++i) {
00094         c[i] = a[i] * k;
00095     }
00096 }
00097 
00098 
00106 void
00107 lib3ds_vector_cross(float c[3], float a[3], float b[3]) {
00108     c[0] = a[1] * b[2] - a[2] * b[1];
00109     c[1] = a[2] * b[0] - a[0] * b[2];
00110     c[2] = a[0] * b[1] - a[1] * b[0];
00111 }
00112 
00113 
00122 float
00123 lib3ds_vector_dot(float a[3], float b[3]) {
00124     return(a[0]*b[0] + a[1]*b[1] + a[2]*b[2]);
00125 }
00126 
00127 
00137 float
00138 lib3ds_vector_length(float c[3]) {
00139     return((float)sqrt(c[0]*c[0] + c[1]*c[1] + c[2]*c[2]));
00140 }
00141 
00142 
00150 void
00151 lib3ds_vector_normalize(float c[3]) {
00152     float l, m;
00153 
00154     l = (float)sqrt(c[0] * c[0] + c[1] * c[1] + c[2] * c[2]);
00155     if (fabs(l) < LIB3DS_EPSILON) {
00156         if ((c[0] >= c[1]) && (c[0] >= c[2])) {
00157             c[0] = 1.0f;
00158             c[1] = c[2] = 0.0f;
00159         } else
00160             if (c[1] >= c[2]) {
00161                 c[1] = 1.0f;
00162                 c[0] = c[2] = 0.0f;
00163             } else {
00164                 c[2] = 1.0f;
00165                 c[0] = c[1] = 0.0f;
00166             }
00167     } else {
00168         m = 1.0f / l;
00169         c[0] *= m;
00170         c[1] *= m;
00171         c[2] *= m;
00172     }
00173 }
00174 
00175 
00186 void
00187 lib3ds_vector_normal(float n[3], float a[3], float b[3], float c[3]) {
00188     float p[3], q[3];
00189 
00190     lib3ds_vector_sub(p, c, b);
00191     lib3ds_vector_sub(q, a, b);
00192     lib3ds_vector_cross(n, p, q);
00193     lib3ds_vector_normalize(n);
00194 }
00195 
00196 
00207 void
00208 lib3ds_vector_transform(float c[3], float m[4][4], float a[3]) {
00209     c[0] = m[0][0] * a[0] + m[1][0] * a[1] + m[2][0] * a[2] + m[3][0];
00210     c[1] = m[0][1] * a[0] + m[1][1] * a[1] + m[2][1] * a[2] + m[3][1];
00211     c[2] = m[0][2] * a[0] + m[1][2] * a[1] + m[2][2] * a[2] + m[3][2];
00212 }
00213 
00214 
00220 void
00221 lib3ds_vector_min(float c[3], float a[3]) {
00222     int i;
00223     for (i = 0; i < 3; ++i) {
00224         if (a[i] < c[i]) {
00225             c[i] = a[i];
00226         }
00227     }
00228 }
00229 
00230 
00236 void
00237 lib3ds_vector_max(float c[3], float a[3]) {
00238     int i;
00239     for (i = 0; i < 3; ++i) {
00240         if (a[i] > c[i]) {
00241             c[i] = a[i];
00242         }
00243     }
00244 }
00245 
00246 
00247 void
00248 lib3ds_vector_dump(float c[3]) {
00249     fprintf(stderr, "%f %f %f\n", c[0], c[1], c[2]);
00250 }
00251