|
Blender
V2.59
|
00001 /* 00002 * $Id: math_vector_inline.c 37276 2011-06-06 22:10:05Z psy-fi $ 00003 * 00004 * ***** BEGIN GPL LICENSE BLOCK ***** 00005 * 00006 * This program is free software; you can redistribute it and/or 00007 * modify it under the terms of the GNU General Public License 00008 * as published by the Free Software Foundation; either version 2 00009 * of the License, or (at your option) any later version. 00010 * 00011 * This program is distributed in the hope that it will be useful, 00012 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00013 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00014 * GNU General Public License for more details. 00015 * 00016 * You should have received a copy of the GNU General Public License 00017 * along with this program; if not, write to the Free Software Foundation, 00018 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. 00019 * 00020 * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV. 00021 * All rights reserved. 00022 00023 * The Original Code is: some of this file. 00024 * 00025 * ***** END GPL LICENSE BLOCK ***** 00026 * */ 00027 00033 #include "BLI_math.h" 00034 00035 #ifndef BLI_MATH_VECTOR_INLINE_H 00036 #define BLI_MATH_VECTOR_INLINE_H 00037 00038 /********************************** Init *************************************/ 00039 00040 MINLINE void zero_v2(float r[2]) 00041 { 00042 r[0]= 0.0f; 00043 r[1]= 0.0f; 00044 } 00045 00046 MINLINE void zero_v3(float r[3]) 00047 { 00048 r[0]= 0.0f; 00049 r[1]= 0.0f; 00050 r[2]= 0.0f; 00051 } 00052 00053 MINLINE void zero_v4(float r[4]) 00054 { 00055 r[0]= 0.0f; 00056 r[1]= 0.0f; 00057 r[2]= 0.0f; 00058 r[3]= 0.0f; 00059 } 00060 00061 MINLINE void copy_v2_v2(float r[2], const float a[2]) 00062 { 00063 r[0]= a[0]; 00064 r[1]= a[1]; 00065 } 00066 00067 MINLINE void copy_v3_v3(float r[3], const float a[3]) 00068 { 00069 r[0]= a[0]; 00070 r[1]= a[1]; 00071 r[2]= a[2]; 00072 } 00073 00074 MINLINE void copy_v4_v4(float r[4], const float a[4]) 00075 { 00076 r[0]= a[0]; 00077 r[1]= a[1]; 00078 r[2]= a[2]; 00079 r[3]= a[3]; 00080 } 00081 00082 MINLINE void swap_v2_v2(float a[2], float b[2]) 00083 { 00084 SWAP(float, a[0], b[0]); 00085 SWAP(float, a[1], b[1]); 00086 } 00087 00088 MINLINE void swap_v3_v3(float a[3], float b[3]) 00089 { 00090 SWAP(float, a[0], b[0]); 00091 SWAP(float, a[1], b[1]); 00092 SWAP(float, a[2], b[2]); 00093 } 00094 00095 MINLINE void swap_v4_v4(float a[4], float b[4]) 00096 { 00097 SWAP(float, a[0], b[0]); 00098 SWAP(float, a[1], b[1]); 00099 SWAP(float, a[2], b[2]); 00100 SWAP(float, a[3], b[3]); 00101 } 00102 00103 /********************************* Arithmetic ********************************/ 00104 00105 MINLINE void add_v3_fl(float r[3], float f) 00106 { 00107 r[0] += f; 00108 r[1] += f; 00109 r[2] += f; 00110 } 00111 00112 MINLINE void add_v4_fl(float r[4], float f) 00113 { 00114 r[0] += f; 00115 r[1] += f; 00116 r[2] += f; 00117 r[3] += f; 00118 } 00119 00120 MINLINE void add_v2_v2(float *r, const float *a) 00121 { 00122 r[0] += a[0]; 00123 r[1] += a[1]; 00124 } 00125 00126 MINLINE void add_v2_v2v2(float *r, const float *a, const float *b) 00127 { 00128 r[0]= a[0] + b[0]; 00129 r[1]= a[1] + b[1]; 00130 } 00131 00132 MINLINE void add_v3_v3(float *r, const float *a) 00133 { 00134 r[0] += a[0]; 00135 r[1] += a[1]; 00136 r[2] += a[2]; 00137 } 00138 00139 MINLINE void add_v3_v3v3(float *r, const float *a, const float *b) 00140 { 00141 r[0]= a[0] + b[0]; 00142 r[1]= a[1] + b[1]; 00143 r[2]= a[2] + b[2]; 00144 } 00145 00146 MINLINE void sub_v2_v2(float *r, const float *a) 00147 { 00148 r[0] -= a[0]; 00149 r[1] -= a[1]; 00150 } 00151 00152 MINLINE void sub_v2_v2v2(float *r, const float *a, const float *b) 00153 { 00154 r[0]= a[0] - b[0]; 00155 r[1]= a[1] - b[1]; 00156 } 00157 00158 MINLINE void sub_v3_v3(float *r, const float *a) 00159 { 00160 r[0] -= a[0]; 00161 r[1] -= a[1]; 00162 r[2] -= a[2]; 00163 } 00164 00165 MINLINE void sub_v3_v3v3(float r[3], const float a[3], const float b[3]) 00166 { 00167 r[0]= a[0] - b[0]; 00168 r[1]= a[1] - b[1]; 00169 r[2]= a[2] - b[2]; 00170 } 00171 00172 MINLINE void sub_v4_v4(float r[4], const float a[4]) 00173 { 00174 r[0] -= a[0]; 00175 r[1] -= a[1]; 00176 r[2] -= a[2]; 00177 r[3] -= a[3]; 00178 } 00179 00180 MINLINE void sub_v4_v4v4(float r[3], const float a[3], const float b[3]) 00181 { 00182 r[0]= a[0] - b[0]; 00183 r[1]= a[1] - b[1]; 00184 r[2]= a[2] - b[2]; 00185 r[3]= a[3] - b[3]; 00186 } 00187 00188 00189 MINLINE void mul_v2_fl(float *v1, float f) 00190 { 00191 v1[0]*= f; 00192 v1[1]*= f; 00193 } 00194 00195 MINLINE void mul_v2_v2fl(float r[2], const float a[2], float f) 00196 { 00197 r[0]= a[0]*f; 00198 r[1]= a[1]*f; 00199 } 00200 00201 MINLINE void mul_v3_fl(float r[3], float f) 00202 { 00203 r[0] *= f; 00204 r[1] *= f; 00205 r[2] *= f; 00206 } 00207 00208 MINLINE void mul_v3_v3fl(float r[3], const float a[3], float f) 00209 { 00210 r[0]= a[0]*f; 00211 r[1]= a[1]*f; 00212 r[2]= a[2]*f; 00213 } 00214 00215 MINLINE void mul_v2_v2(float r[2], const float a[2]) 00216 { 00217 r[0] *= a[0]; 00218 r[1] *= a[1]; 00219 } 00220 00221 MINLINE void mul_v3_v3(float r[3], const float a[3]) 00222 { 00223 r[0] *= a[0]; 00224 r[1] *= a[1]; 00225 r[2] *= a[2]; 00226 } 00227 00228 MINLINE void mul_v4_fl(float r[4], float f) 00229 { 00230 r[0]*= f; 00231 r[1]*= f; 00232 r[2]*= f; 00233 r[3]*= f; 00234 } 00235 00236 MINLINE void madd_v2_v2fl(float r[2], const float a[2], float f) 00237 { 00238 r[0] += a[0]*f; 00239 r[1] += a[1]*f; 00240 } 00241 00242 MINLINE void madd_v3_v3fl(float r[3], const float a[3], float f) 00243 { 00244 r[0] += a[0]*f; 00245 r[1] += a[1]*f; 00246 r[2] += a[2]*f; 00247 } 00248 00249 MINLINE void madd_v3_v3v3(float r[3], const float a[3], const float b[3]) 00250 { 00251 r[0] += a[0]*b[0]; 00252 r[1] += a[1]*b[1]; 00253 r[2] += a[2]*b[2]; 00254 } 00255 00256 MINLINE void madd_v2_v2v2fl(float r[2], const float a[2], const float b[2], float f) 00257 { 00258 r[0] = a[0] + b[0]*f; 00259 r[1] = a[1] + b[1]*f; 00260 } 00261 00262 MINLINE void madd_v3_v3v3fl(float r[3], const float a[3], const float b[3], float f) 00263 { 00264 r[0] = a[0] + b[0]*f; 00265 r[1] = a[1] + b[1]*f; 00266 r[2] = a[2] + b[2]*f; 00267 } 00268 00269 MINLINE void madd_v3_v3v3v3(float r[3], const float a[3], const float b[3], const float c[3]) 00270 { 00271 r[0] = a[0] + b[0]*c[0]; 00272 r[1] = a[1] + b[1]*c[1]; 00273 r[2] = a[2] + b[2]*c[2]; 00274 } 00275 00276 MINLINE void madd_v4_v4fl(float r[4], const float a[4], float f) 00277 { 00278 r[0] += a[0]*f; 00279 r[1] += a[1]*f; 00280 r[2] += a[2]*f; 00281 r[3] += a[3]*f; 00282 } 00283 00284 MINLINE void mul_v3_v3v3(float *v, const float *v1, const float *v2) 00285 { 00286 v[0] = v1[0] * v2[0]; 00287 v[1] = v1[1] * v2[1]; 00288 v[2] = v1[2] * v2[2]; 00289 } 00290 00291 MINLINE void negate_v3(float r[3]) 00292 { 00293 r[0]= -r[0]; 00294 r[1]= -r[1]; 00295 r[2]= -r[2]; 00296 } 00297 00298 MINLINE void negate_v3_v3(float r[3], const float a[3]) 00299 { 00300 r[0]= -a[0]; 00301 r[1]= -a[1]; 00302 r[2]= -a[2]; 00303 } 00304 00305 MINLINE void negate_v4(float r[4]) 00306 { 00307 r[0]= -r[0]; 00308 r[1]= -r[1]; 00309 r[2]= -r[2]; 00310 r[3]= -r[3]; 00311 } 00312 00313 MINLINE void negate_v4_v4(float r[4], const float a[4]) 00314 { 00315 r[0]= -a[0]; 00316 r[1]= -a[1]; 00317 r[2]= -a[2]; 00318 r[3]= -a[3]; 00319 } 00320 00321 MINLINE float dot_v2v2(const float a[2], const float b[2]) 00322 { 00323 return a[0]*b[0] + a[1]*b[1]; 00324 } 00325 00326 MINLINE float dot_v3v3(const float a[3], const float b[3]) 00327 { 00328 return a[0]*b[0] + a[1]*b[1] + a[2]*b[2]; 00329 } 00330 00331 MINLINE float cross_v2v2(const float a[2], const float b[2]) 00332 { 00333 return a[0]*b[1] - a[1]*b[0]; 00334 } 00335 00336 MINLINE void cross_v3_v3v3(float r[3], const float a[3], const float b[3]) 00337 { 00338 r[0]= a[1]*b[2] - a[2]*b[1]; 00339 r[1]= a[2]*b[0] - a[0]*b[2]; 00340 r[2]= a[0]*b[1] - a[1]*b[0]; 00341 } 00342 00343 MINLINE void star_m3_v3(float mat[][3], float *vec) 00344 { 00345 mat[0][0]= mat[1][1]= mat[2][2]= 0.0; 00346 mat[0][1]= -vec[2]; 00347 mat[0][2]= vec[1]; 00348 mat[1][0]= vec[2]; 00349 mat[1][2]= -vec[0]; 00350 mat[2][0]= -vec[1]; 00351 mat[2][1]= vec[0]; 00352 } 00353 00354 /*********************************** Length **********************************/ 00355 00356 MINLINE float len_v2(const float v[2]) 00357 { 00358 return (float)sqrtf(v[0]*v[0] + v[1]*v[1]); 00359 } 00360 00361 MINLINE float len_v2v2(const float v1[2], const float v2[2]) 00362 { 00363 float x, y; 00364 00365 x = v1[0]-v2[0]; 00366 y = v1[1]-v2[1]; 00367 return (float)sqrtf(x*x+y*y); 00368 } 00369 00370 MINLINE float len_v3(const float a[3]) 00371 { 00372 return sqrtf(dot_v3v3(a, a)); 00373 } 00374 00375 MINLINE float len_squared_v2v2(const float a[3], const float b[3]) 00376 { 00377 float d[2]; 00378 00379 sub_v2_v2v2(d, b, a); 00380 return dot_v2v2(d, d); 00381 } 00382 00383 MINLINE float len_v3v3(const float a[3], const float b[3]) 00384 { 00385 float d[3]; 00386 00387 sub_v3_v3v3(d, b, a); 00388 return len_v3(d); 00389 } 00390 00391 MINLINE float len_squared_v3v3(const float a[3], const float b[3]) 00392 { 00393 float d[3]; 00394 00395 sub_v3_v3v3(d, b, a); 00396 return dot_v3v3(d, d); 00397 } 00398 00399 MINLINE float normalize_v2_v2(float r[2], const float a[2]) 00400 { 00401 float d= dot_v2v2(a, a); 00402 00403 if(d > 1.0e-35f) { 00404 d= sqrtf(d); 00405 mul_v2_v2fl(r, a, 1.0f/d); 00406 } else { 00407 zero_v2(r); 00408 d= 0.0f; 00409 } 00410 00411 return d; 00412 } 00413 00414 MINLINE float normalize_v2(float n[2]) 00415 { 00416 return normalize_v2_v2(n, n); 00417 } 00418 00419 MINLINE float normalize_v3_v3(float r[3], const float a[3]) 00420 { 00421 float d= dot_v3v3(a, a); 00422 00423 /* a larger value causes normalize errors in a 00424 scaled down models with camera xtreme close */ 00425 if(d > 1.0e-35f) { 00426 d= sqrtf(d); 00427 mul_v3_v3fl(r, a, 1.0f/d); 00428 } 00429 else { 00430 zero_v3(r); 00431 d= 0.0f; 00432 } 00433 00434 return d; 00435 } 00436 00437 MINLINE float normalize_v3(float n[3]) 00438 { 00439 return normalize_v3_v3(n, n); 00440 } 00441 00442 MINLINE void normal_short_to_float_v3(float out[3], const short in[3]) 00443 { 00444 out[0] = in[0]*(1.0f/32767.0f); 00445 out[1] = in[1]*(1.0f/32767.0f); 00446 out[2] = in[2]*(1.0f/32767.0f); 00447 } 00448 00449 MINLINE void normal_float_to_short_v3(short out[3], const float in[3]) 00450 { 00451 out[0] = (short)(in[0]*32767.0f); 00452 out[1] = (short)(in[1]*32767.0f); 00453 out[2] = (short)(in[2]*32767.0f); 00454 } 00455 00456 /********************************* Comparison ********************************/ 00457 00458 MINLINE int is_zero_v3(const float v[3]) 00459 { 00460 return (v[0] == 0 && v[1] == 0 && v[2] == 0); 00461 } 00462 00463 MINLINE int is_zero_v4(const float v[4]) 00464 { 00465 return (v[0] == 0 && v[1] == 0 && v[2] == 0 && v[3] == 0); 00466 } 00467 00468 MINLINE int is_one_v3(const float *v) 00469 { 00470 return (v[0] == 1 && v[1] == 1 && v[2] == 1); 00471 } 00472 00473 MINLINE int equals_v2v2(const float *v1, const float *v2) 00474 { 00475 return ((v1[0]==v2[0]) && (v1[1]==v2[1])); 00476 } 00477 00478 MINLINE int equals_v3v3(const float *v1, const float *v2) 00479 { 00480 return ((v1[0]==v2[0]) && (v1[1]==v2[1]) && (v1[2]==v2[2])); 00481 } 00482 00483 MINLINE int equals_v4v4(const float *v1, const float *v2) 00484 { 00485 return ((v1[0]==v2[0]) && (v1[1]==v2[1]) && (v1[2]==v2[2]) && (v1[3]==v2[3])); 00486 } 00487 00488 MINLINE int compare_v3v3(const float *v1, const float *v2, const float limit) 00489 { 00490 if(fabsf(v1[0]-v2[0])<limit) 00491 if(fabsf(v1[1]-v2[1])<limit) 00492 if(fabsf(v1[2]-v2[2])<limit) 00493 return 1; 00494 00495 return 0; 00496 } 00497 00498 MINLINE int compare_len_v3v3(const float *v1, const float *v2, const float limit) 00499 { 00500 float x,y,z; 00501 00502 x=v1[0]-v2[0]; 00503 y=v1[1]-v2[1]; 00504 z=v1[2]-v2[2]; 00505 00506 return ((x*x + y*y + z*z) < (limit*limit)); 00507 } 00508 00509 MINLINE int compare_v4v4(const float *v1, const float *v2, const float limit) 00510 { 00511 if(fabsf(v1[0]-v2[0])<limit) 00512 if(fabsf(v1[1]-v2[1])<limit) 00513 if(fabsf(v1[2]-v2[2])<limit) 00514 if(fabsf(v1[3]-v2[3])<limit) 00515 return 1; 00516 00517 return 0; 00518 } 00519 00520 MINLINE float line_point_side_v2(const float *l1, const float *l2, const float *pt) 00521 { 00522 return ((l1[0]-pt[0]) * (l2[1]-pt[1])) - 00523 ((l2[0]-pt[0]) * (l1[1]-pt[1])); 00524 } 00525 00526 #endif /* BLI_MATH_VECTOR_INLINE_H */ 00527