2 * ***** BEGIN GPL LICENSE BLOCK *****
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * as published by the Free Software Foundation; either version 2
7 * of the License, or (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software Foundation,
16 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
19 * All rights reserved.
21 * The Original Code is: some of this file.
23 * ***** END GPL LICENSE BLOCK *****
26 /** \file blender/blenlib/intern/math_vector_inline.c
30 #ifndef __MATH_VECTOR_INLINE_C__
31 #define __MATH_VECTOR_INLINE_C__
35 /********************************** Init *************************************/
37 MINLINE void zero_v2(float r[2])
43 MINLINE void zero_v3(float r[3])
50 MINLINE void zero_v4(float r[4])
58 MINLINE void copy_v2_v2(float r[2], const float a[2])
64 MINLINE void copy_v3_v3(float r[3], const float a[3])
71 MINLINE void copy_v4_v4(float r[4], const float a[4])
79 MINLINE void copy_v2_fl(float r[2], float f)
85 MINLINE void copy_v3_fl(float r[3], float f)
92 MINLINE void copy_v4_fl(float r[4], float f)
101 MINLINE void copy_v2_v2_char(char r[2], const char a[2])
107 MINLINE void copy_v3_v3_char(char r[3], const char a[3])
114 MINLINE void copy_v4_v4_char(char r[4], const char a[4])
123 MINLINE void zero_v3_int(int r[3])
130 MINLINE void copy_v2_v2_short(short r[2], const short a[2])
136 MINLINE void copy_v3_v3_short(short r[3], const short a[3])
143 MINLINE void copy_v4_v4_short(short r[4], const short a[4])
152 MINLINE void copy_v2_v2_int(int r[2], const int a[2])
158 MINLINE void copy_v3_v3_int(int r[3], const int a[3])
165 MINLINE void copy_v4_v4_int(int r[4], const int a[4])
173 /* double -> float */
174 MINLINE void copy_v2fl_v2db(float r[2], const double a[2])
180 MINLINE void copy_v3fl_v3db(float r[3], const double a[3])
187 MINLINE void copy_v4fl_v4db(float r[4], const double a[4])
195 /* float -> double */
196 MINLINE void copy_v2db_v2fl(double r[2], const float a[2])
202 MINLINE void copy_v3db_v3fl(double r[3], const float a[3])
209 MINLINE void copy_v4db_v4fl(double r[4], const float a[4])
217 MINLINE void swap_v2_v2(float a[2], float b[2])
219 SWAP(float, a[0], b[0]);
220 SWAP(float, a[1], b[1]);
223 MINLINE void swap_v3_v3(float a[3], float b[3])
225 SWAP(float, a[0], b[0]);
226 SWAP(float, a[1], b[1]);
227 SWAP(float, a[2], b[2]);
230 MINLINE void swap_v4_v4(float a[4], float b[4])
232 SWAP(float, a[0], b[0]);
233 SWAP(float, a[1], b[1]);
234 SWAP(float, a[2], b[2]);
235 SWAP(float, a[3], b[3]);
238 /* float args -> vec */
239 MINLINE void copy_v3_fl3(float v[3], float x, float y, float z)
246 MINLINE void copy_v4_fl4(float v[4], float x, float y, float z, float w)
254 /********************************* Arithmetic ********************************/
256 MINLINE void add_v2_fl(float r[2], float f)
263 MINLINE void add_v3_fl(float r[3], float f)
270 MINLINE void add_v4_fl(float r[4], float f)
278 MINLINE void add_v2_v2(float r[2], const float a[2])
284 MINLINE void add_v2_v2v2(float r[2], const float a[2], const float b[2])
290 MINLINE void add_v2_v2v2_int(int r[2], const int a[2], const int b[2])
296 MINLINE void add_v3_v3(float r[3], const float a[3])
303 MINLINE void add_v3_v3v3(float r[3], const float a[3], const float b[3])
310 MINLINE void add_v4_v4(float r[4], const float a[4])
318 MINLINE void add_v4_v4v4(float r[4], const float a[4], const float b[4])
326 MINLINE void sub_v2_v2(float r[2], const float a[2])
332 MINLINE void sub_v2_v2v2(float r[2], const float a[2], const float b[2])
338 MINLINE void sub_v2_v2v2_int(int r[2], const int a[2], const int b[2])
344 MINLINE void sub_v3_v3(float r[3], const float a[3])
351 MINLINE void sub_v3_v3v3(float r[3], const float a[3], const float b[3])
358 MINLINE void sub_v4_v4(float r[4], const float a[4])
366 MINLINE void sub_v4_v4v4(float r[4], const float a[4], const float b[4])
374 MINLINE void mul_v2_fl(float r[2], float f)
380 MINLINE void mul_v2_v2fl(float r[2], const float a[2], float f)
386 MINLINE void mul_v3_fl(float r[3], float f)
393 MINLINE void mul_v3_v3fl(float r[3], const float a[3], float f)
400 MINLINE void mul_v2_v2(float r[2], const float a[2])
406 MINLINE void mul_v3_v3(float r[3], const float a[3])
413 MINLINE void mul_v4_fl(float r[4], float f)
421 MINLINE void mul_v4_v4fl(float r[4], const float a[4], float f)
429 /* note: could add a matrix inline */
430 MINLINE float mul_project_m4_v3_zfac(float mat[4][4], const float co[3])
432 return (mat[0][3] * co[0]) +
433 (mat[1][3] * co[1]) +
434 (mat[2][3] * co[2]) + mat[3][3];
438 * Has the effect of mul_m3_v3(), on a single axis.
440 MINLINE float dot_m3_v3_row_x(float M[3][3], const float a[3])
442 return M[0][0] * a[0] + M[1][0] * a[1] + M[2][0] * a[2];
444 MINLINE float dot_m3_v3_row_y(float M[3][3], const float a[3])
446 return M[0][1] * a[0] + M[1][1] * a[1] + M[2][1] * a[2];
448 MINLINE float dot_m3_v3_row_z(float M[3][3], const float a[3])
450 return M[0][2] * a[0] + M[1][2] * a[1] + M[2][2] * a[2];
453 MINLINE void madd_v2_v2fl(float r[2], const float a[2], float f)
459 MINLINE void madd_v3_v3fl(float r[3], const float a[3], float f)
466 MINLINE void madd_v3_v3v3(float r[3], const float a[3], const float b[3])
473 MINLINE void madd_v2_v2v2fl(float r[2], const float a[2], const float b[2], float f)
475 r[0] = a[0] + b[0] * f;
476 r[1] = a[1] + b[1] * f;
479 MINLINE void madd_v3_v3v3fl(float r[3], const float a[3], const float b[3], float f)
481 r[0] = a[0] + b[0] * f;
482 r[1] = a[1] + b[1] * f;
483 r[2] = a[2] + b[2] * f;
486 MINLINE void madd_v3_v3v3v3(float r[3], const float a[3], const float b[3], const float c[3])
488 r[0] = a[0] + b[0] * c[0];
489 r[1] = a[1] + b[1] * c[1];
490 r[2] = a[2] + b[2] * c[2];
493 MINLINE void madd_v4_v4fl(float r[4], const float a[4], float f)
501 MINLINE void madd_v4_v4v4(float r[4], const float a[4], const float b[4])
509 MINLINE void mul_v3_v3v3(float r[3], const float v1[3], const float v2[3])
511 r[0] = v1[0] * v2[0];
512 r[1] = v1[1] * v2[1];
513 r[2] = v1[2] * v2[2];
516 MINLINE void negate_v2(float r[2])
522 MINLINE void negate_v2_v2(float r[2], const float a[2])
528 MINLINE void negate_v3(float r[3])
535 MINLINE void negate_v3_v3(float r[3], const float a[3])
542 MINLINE void negate_v4(float r[4])
550 MINLINE void negate_v4_v4(float r[4], const float a[4])
558 /* could add more... */
559 MINLINE void negate_v3_short(short r[3])
566 MINLINE float dot_v2v2(const float a[2], const float b[2])
568 return a[0] * b[0] + a[1] * b[1];
571 MINLINE float dot_v3v3(const float a[3], const float b[3])
573 return a[0] * b[0] + a[1] * b[1] + a[2] * b[2];
576 MINLINE float cross_v2v2(const float a[2], const float b[2])
578 return a[0] * b[1] - a[1] * b[0];
581 MINLINE void cross_v3_v3v3(float r[3], const float a[3], const float b[3])
583 BLI_assert(r != a && r != b);
584 r[0] = a[1] * b[2] - a[2] * b[1];
585 r[1] = a[2] * b[0] - a[0] * b[2];
586 r[2] = a[0] * b[1] - a[1] * b[0];
589 /* Newell's Method */
590 /* excuse this fairly specific function,
591 * its used for polygon normals all over the place
592 * could use a better name */
593 MINLINE void add_newell_cross_v3_v3v3(float n[3], const float v_prev[3], const float v_curr[3])
595 n[0] += (v_prev[1] - v_curr[1]) * (v_prev[2] + v_curr[2]);
596 n[1] += (v_prev[2] - v_curr[2]) * (v_prev[0] + v_curr[0]);
597 n[2] += (v_prev[0] - v_curr[0]) * (v_prev[1] + v_curr[1]);
600 MINLINE void star_m3_v3(float rmat[3][3], float a[3])
602 rmat[0][0] = rmat[1][1] = rmat[2][2] = 0.0;
611 /*********************************** Length **********************************/
613 MINLINE float len_squared_v2(const float v[2])
615 return v[0] * v[0] + v[1] * v[1];
618 MINLINE float len_squared_v3(const float v[3])
620 return v[0] * v[0] + v[1] * v[1] + v[2] * v[2];
623 MINLINE float len_manhattan_v2(const float v[2])
625 return fabsf(v[0]) + fabsf(v[1]);
628 MINLINE int len_manhattan_v2_int(const int v[2])
630 return ABS(v[0]) + ABS(v[1]);
633 MINLINE float len_manhattan_v3(const float v[3])
635 return fabsf(v[0]) + fabsf(v[1]) + fabsf(v[2]);
638 MINLINE float len_v2(const float v[2])
640 return sqrtf(v[0] * v[0] + v[1] * v[1]);
643 MINLINE float len_v2v2(const float v1[2], const float v2[2])
649 return sqrtf(x * x + y * y);
652 MINLINE float len_v3(const float a[3])
654 return sqrtf(dot_v3v3(a, a));
657 MINLINE float len_squared_v2v2(const float a[2], const float b[2])
661 sub_v2_v2v2(d, b, a);
662 return dot_v2v2(d, d);
665 MINLINE float len_squared_v3v3(const float a[3], const float b[3])
669 sub_v3_v3v3(d, b, a);
670 return dot_v3v3(d, d);
673 MINLINE float len_manhattan_v2v2(const float a[2], const float b[2])
677 sub_v2_v2v2(d, b, a);
678 return len_manhattan_v2(d);
681 MINLINE int len_manhattan_v2v2_int(const int a[2], const int b[2])
685 sub_v2_v2v2_int(d, b, a);
686 return len_manhattan_v2_int(d);
689 MINLINE float len_manhattan_v3v3(const float a[3], const float b[3])
693 sub_v3_v3v3(d, b, a);
694 return len_manhattan_v3(d);
697 MINLINE float len_v3v3(const float a[3], const float b[3])
701 sub_v3_v3v3(d, b, a);
705 MINLINE float normalize_v2_v2(float r[2], const float a[2])
707 float d = dot_v2v2(a, a);
711 mul_v2_v2fl(r, a, 1.0f / d);
721 MINLINE float normalize_v2(float n[2])
723 return normalize_v2_v2(n, n);
726 MINLINE float normalize_v3_v3(float r[3], const float a[3])
728 float d = dot_v3v3(a, a);
730 /* a larger value causes normalize errors in a
731 * scaled down models with camera extreme close */
734 mul_v3_v3fl(r, a, 1.0f / d);
744 MINLINE double normalize_v3_d(double n[3])
746 double d = n[0] * n[0] + n[1] * n[1] + n[2] * n[2];
748 /* a larger value causes normalize errors in a
749 * scaled down models with camera extreme close */
761 n[0] = n[1] = n[2] = 0;
768 MINLINE float normalize_v3(float n[3])
770 return normalize_v3_v3(n, n);
773 MINLINE void normal_short_to_float_v3(float out[3], const short in[3])
775 out[0] = in[0] * (1.0f / 32767.0f);
776 out[1] = in[1] * (1.0f / 32767.0f);
777 out[2] = in[2] * (1.0f / 32767.0f);
780 MINLINE void normal_float_to_short_v3(short out[3], const float in[3])
782 out[0] = (short) (in[0] * 32767.0f);
783 out[1] = (short) (in[1] * 32767.0f);
784 out[2] = (short) (in[2] * 32767.0f);
787 /********************************* Comparison ********************************/
790 MINLINE bool is_zero_v2(const float v[2])
792 return (v[0] == 0 && v[1] == 0);
795 MINLINE bool is_zero_v3(const float v[3])
797 return (v[0] == 0 && v[1] == 0 && v[2] == 0);
800 MINLINE bool is_zero_v4(const float v[4])
802 return (v[0] == 0 && v[1] == 0 && v[2] == 0 && v[3] == 0);
805 MINLINE bool is_finite_v2(const float v[2])
807 return (finite(v[0]) && finite(v[1]));
810 MINLINE bool is_finite_v3(const float v[3])
812 return (finite(v[0]) && finite(v[1]) && finite(v[2]));
815 MINLINE bool is_finite_v4(const float v[4])
817 return (finite(v[0]) && finite(v[1]) && finite(v[2]) && finite(v[3]));
820 MINLINE bool is_one_v3(const float v[3])
822 return (v[0] == 1 && v[1] == 1 && v[2] == 1);
825 MINLINE bool equals_v2v2(const float v1[2], const float v2[2])
827 return ((v1[0] == v2[0]) && (v1[1] == v2[1]));
830 MINLINE bool equals_v3v3(const float v1[3], const float v2[3])
832 return ((v1[0] == v2[0]) && (v1[1] == v2[1]) && (v1[2] == v2[2]));
835 MINLINE bool equals_v4v4(const float v1[4], const float v2[4])
837 return ((v1[0] == v2[0]) && (v1[1] == v2[1]) && (v1[2] == v2[2]) && (v1[3] == v2[3]));
840 MINLINE bool compare_v2v2(const float v1[2], const float v2[2], const float limit)
842 if (fabsf(v1[0] - v2[0]) < limit)
843 if (fabsf(v1[1] - v2[1]) < limit)
849 MINLINE bool compare_v3v3(const float v1[3], const float v2[3], const float limit)
851 if (fabsf(v1[0] - v2[0]) < limit)
852 if (fabsf(v1[1] - v2[1]) < limit)
853 if (fabsf(v1[2] - v2[2]) < limit)
859 MINLINE bool compare_len_v3v3(const float v1[3], const float v2[3], const float limit)
867 return ((x * x + y * y + z * z) < (limit * limit));
870 MINLINE bool compare_v4v4(const float v1[4], const float v2[4], const float limit)
872 if (fabsf(v1[0] - v2[0]) < limit)
873 if (fabsf(v1[1] - v2[1]) < limit)
874 if (fabsf(v1[2] - v2[2]) < limit)
875 if (fabsf(v1[3] - v2[3]) < limit)
881 MINLINE float line_point_side_v2(const float l1[2], const float l2[2], const float pt[2])
883 return (((l1[0] - pt[0]) * (l2[1] - pt[1])) -
884 ((l2[0] - pt[0]) * (l1[1] - pt[1])));
887 #endif /* __MATH_VECTOR_INLINE_C__ */