diff options
author | Brecht Van Lommel <brechtvanlommel@pandora.be> | 2010-01-22 14:10:24 +0300 |
---|---|---|
committer | Brecht Van Lommel <brechtvanlommel@pandora.be> | 2010-01-22 14:10:24 +0300 |
commit | 8ec59c7c687b9e50c48a9d455b31ff4c01df69d2 (patch) | |
tree | 32bf65aad65475c5dd9aeefa9067eba21e7324fb /source/blender/blenlib | |
parent | 2d2339a70992b819a23e8c71f68027022b395f46 (diff) |
Math Lib:
* inline some more functions, from math_base and math_vector
* also made some changes to the way inline is done so it can
work for more than one file
* reflect_v3_v3v3 requires input vectors to be normalized now.
* added rgb_to_grayscale
* added zero_v4, copy_v4_v4, swap_v4_v4, is_one_v3
* added box_clip_bounds_m4 to clip a bounding box against a
projection matrix
Diffstat (limited to 'source/blender/blenlib')
-rw-r--r-- | source/blender/blenlib/BLI_math_base.h | 30 | ||||
-rw-r--r-- | source/blender/blenlib/BLI_math_color.h | 2 | ||||
-rw-r--r-- | source/blender/blenlib/BLI_math_geom.h | 3 | ||||
-rw-r--r-- | source/blender/blenlib/BLI_math_inline.h | 53 | ||||
-rw-r--r-- | source/blender/blenlib/BLI_math_vector.h | 25 | ||||
-rw-r--r-- | source/blender/blenlib/intern/gsqueue.c | 11 | ||||
-rw-r--r-- | source/blender/blenlib/intern/math_base.c | 79 | ||||
-rw-r--r-- | source/blender/blenlib/intern/math_base_inline.c | 127 | ||||
-rw-r--r-- | source/blender/blenlib/intern/math_color.c | 6 | ||||
-rw-r--r-- | source/blender/blenlib/intern/math_geom.c | 37 | ||||
-rw-r--r-- | source/blender/blenlib/intern/math_vector.c | 46 | ||||
-rw-r--r-- | source/blender/blenlib/intern/math_vector_inline.c | 73 |
12 files changed, 342 insertions, 150 deletions
diff --git a/source/blender/blenlib/BLI_math_base.h b/source/blender/blenlib/BLI_math_base.h index af301386920..d9161aa2024 100644 --- a/source/blender/blenlib/BLI_math_base.h +++ b/source/blender/blenlib/BLI_math_base.h @@ -37,6 +37,7 @@ extern "C" { #endif #include <math.h> +#include "BLI_math_inline.h" #ifndef M_PI #define M_PI 3.14159265358979323846 @@ -130,23 +131,30 @@ extern "C" { #define CLAMP(a, b, c) if((a)<(b)) (a)=(b); else if((a)>(c)) (a)=(c) #endif +#ifdef BLI_MATH_INLINE +#include "intern/math_base_inline.c" +#endif + /******************************* Float ******************************/ -float sqrt3f(float f); -double sqrt3d(double d); +MINLINE float sqrt3f(float f); +MINLINE double sqrt3d(double d); + +MINLINE float saacosf(float f); +MINLINE float saasinf(float f); +MINLINE float sasqrtf(float f); +MINLINE float saacos(float fac); +MINLINE float saasin(float fac); +MINLINE float sasqrt(float fac); -float saacosf(float f); -float saasinf(float f); -float sasqrtf(float f); -float saacos(float fac); -float saasin(float fac); -float sasqrt(float fac); +MINLINE float interpf(float a, float b, float t); -float interpf(float a, float b, float t); +MINLINE float minf(float a, float b); +MINLINE float maxf(float a, float b); -float power_of_2(float f); +MINLINE float power_of_2(float f); -float shell_angle_to_dist(float angle); +MINLINE float shell_angle_to_dist(float angle); double double_round(double x, int ndigits); diff --git a/source/blender/blenlib/BLI_math_color.h b/source/blender/blenlib/BLI_math_color.h index 41cce59c2b9..1911220bf05 100644 --- a/source/blender/blenlib/BLI_math_color.h +++ b/source/blender/blenlib/BLI_math_color.h @@ -59,6 +59,8 @@ void rgb_to_hsv(float r, float g, float b, float *lh, float *ls, float *lv); unsigned int rgb_to_cpack(float r, float g, float b); unsigned int hsv_to_cpack(float h, float s, float v); +float rgb_to_grayscale(float rgb[3]); + /***************** Profile Transformations ********************/ void gamma_correct(float *c, float gamma); diff --git a/source/blender/blenlib/BLI_math_geom.h b/source/blender/blenlib/BLI_math_geom.h index f650a55d8ad..33d5c887b54 100644 --- a/source/blender/blenlib/BLI_math_geom.h +++ b/source/blender/blenlib/BLI_math_geom.h @@ -144,6 +144,9 @@ void perspective_m4(float mat[4][4], float left, float right, void orthographic_m4(float mat[4][4], float left, float right, float bottom, float top, float nearClip, float farClip); +int box_clip_bounds_m4(float boundbox[2][3], + float bounds[4], float winmat[4][4]); + /********************************** Mapping **********************************/ void map_to_tube(float *u, float *v, float x, float y, float z); diff --git a/source/blender/blenlib/BLI_math_inline.h b/source/blender/blenlib/BLI_math_inline.h new file mode 100644 index 00000000000..d742af26ac0 --- /dev/null +++ b/source/blender/blenlib/BLI_math_inline.h @@ -0,0 +1,53 @@ +/** + * $Id$ + * + * ***** BEGIN GPL LICENSE BLOCK ***** + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software Foundation, + * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + * + * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV. + * All rights reserved. + * + * The Original Code is: some of this file. + * + * ***** END GPL LICENSE BLOCK ***** + * */ + +#ifndef BLI_MATH_INLINE_H +#define BLI_MATH_INLINE_H + +#ifdef __cplusplus +extern "C" { +#endif + +/* add platform/compiler checks here if it is not supported */ +#define BLI_MATH_INLINE + +#ifdef BLI_MATH_INLINE +#ifdef _MSC_VER +#define MINLINE static __inline +#else +#define MINLINE static inline +#endif +#else +#define MINLINE +#endif + +#ifdef __cplusplus +} +#endif + +#endif /* BLI_MATH_INLINE_H */ + diff --git a/source/blender/blenlib/BLI_math_vector.h b/source/blender/blenlib/BLI_math_vector.h index 9fb9e36bd2b..125e02bb481 100644 --- a/source/blender/blenlib/BLI_math_vector.h +++ b/source/blender/blenlib/BLI_math_vector.h @@ -32,30 +32,23 @@ extern "C" { #endif -/* add platform/compiler checks here if it is not supported */ -#define BLI_MATH_INLINE - #ifdef BLI_MATH_INLINE -#ifdef _MSC_VER -#define MINLINE static __inline -#else -#define MINLINE static inline -#endif #include "intern/math_vector_inline.c" -#else -#define MINLINE #endif /************************************* Init ***********************************/ MINLINE void zero_v2(float r[2]); MINLINE void zero_v3(float r[3]); +MINLINE void zero_v4(float r[4]); MINLINE void copy_v2_v2(float r[2], const float a[2]); MINLINE void copy_v3_v3(float r[3], const float a[3]); +MINLINE void copy_v4_v4(float r[4], float a[4]); MINLINE void swap_v2_v2(float a[2], float b[2]); MINLINE void swap_v3_v3(float a[3], float b[3]); +MINLINE void swap_v4_v4(float a[4], float b[4]); /********************************* Arithmetic ********************************/ @@ -118,12 +111,14 @@ void mid_v3_v3v3(float r[3], float a[3], float b[3]); /********************************* Comparison ********************************/ -int is_zero_v3(float a[3]); -int equals_v3v3(float a[3], float b[3]); -int compare_v3v3(float a[3], float b[3], float limit); -int compare_len_v3v3(float a[3], float b[3], float limit); +MINLINE int is_zero_v3(float a[3]); +MINLINE int is_one_v3(float a[3]); + +MINLINE int equals_v3v3(float a[3], float b[3]); +MINLINE int compare_v3v3(float a[3], float b[3], float limit); +MINLINE int compare_len_v3v3(float a[3], float b[3], float limit); -int compare_v4v4(float a[4], float b[4], float limit); +MINLINE int compare_v4v4(float a[4], float b[4], float limit); /********************************** Angles ***********************************/ /* - angle with 2 arguments is angle between vector */ diff --git a/source/blender/blenlib/intern/gsqueue.c b/source/blender/blenlib/intern/gsqueue.c index fe89786b40d..973898016a4 100644 --- a/source/blender/blenlib/intern/gsqueue.c +++ b/source/blender/blenlib/intern/gsqueue.c @@ -60,6 +60,17 @@ int BLI_gsqueue_is_empty(GSQueue *gq) return (gq->head==NULL); } +int BLI_gsqueue_size(GSQueue *gq) +{ + GSQueueElem *elem; + int size= 0; + + for(elem=gq->head; elem; elem=elem->next) + size++; + + return size; +} + void BLI_gsqueue_peek(GSQueue *gq, void *item_r) { memcpy(item_r, &gq->head[1], gq->elem_size); diff --git a/source/blender/blenlib/intern/math_base.c b/source/blender/blenlib/intern/math_base.c index 3b63b033342..06a339aee8c 100644 --- a/source/blender/blenlib/intern/math_base.c +++ b/source/blender/blenlib/intern/math_base.c @@ -32,84 +32,6 @@ #include "BLI_math.h" -/* A few small defines. Keep'em local! */ -#define SMALL_NUMBER 1.e-8 - -float sqrt3f(float f) -{ - if(f==0.0) return 0; - if(f<0) return (float)(-exp(log(-f)/3)); - else return (float)(exp(log(f)/3)); -} - -double sqrt3d(double d) -{ - if(d==0.0) return 0; - if(d<0) return -exp(log(-d)/3); - else return exp(log(d)/3); -} - -float saacos(float fac) -{ - if(fac<= -1.0f) return (float)M_PI; - else if(fac>=1.0f) return 0.0; - else return (float)acos(fac); -} - -float saasin(float fac) -{ - if(fac<= -1.0f) return (float)-M_PI/2.0f; - else if(fac>=1.0f) return (float)M_PI/2.0f; - else return (float)asin(fac); -} - -float sasqrt(float fac) -{ - if(fac<=0.0) return 0.0; - return (float)sqrt(fac); -} - -float saacosf(float fac) -{ - if(fac<= -1.0f) return (float)M_PI; - else if(fac>=1.0f) return 0.0f; - else return (float)acosf(fac); -} - -float saasinf(float fac) -{ - if(fac<= -1.0f) return (float)-M_PI/2.0f; - else if(fac>=1.0f) return (float)M_PI/2.0f; - else return (float)asinf(fac); -} - -float sasqrtf(float fac) -{ - if(fac<=0.0) return 0.0; - return (float)sqrtf(fac); -} - -float interpf(float target, float origin, float fac) -{ - return (fac*target) + (1.0f-fac)*origin; -} - -/* useful to calculate an even width shell, by taking the angle between 2 planes. - * The return value is a scale on the offset. - * no angle between planes is 1.0, as the angle between the 2 planes approches 180d - * the distance gets very high, 180d would be inf, but this case isn't valid */ -float shell_angle_to_dist(const float angle) -{ - return (angle < SMALL_NUMBER) ? 1.0f : fabsf(1.0f / cosf(angle)); -} - -/* used for zoom values*/ -float power_of_2(float val) -{ - return (float)pow(2, ceil(log(val) / log(2))); -} - - /* WARNING: MSVC compiling hack for double_round() */ #if (WIN32 || WIN64) && !(FREE_WINDOWS) @@ -169,3 +91,4 @@ double double_round(double x, int ndigits) { /* if computation resulted in overflow, raise OverflowError */ return z; } + diff --git a/source/blender/blenlib/intern/math_base_inline.c b/source/blender/blenlib/intern/math_base_inline.c new file mode 100644 index 00000000000..71a0d2cc654 --- /dev/null +++ b/source/blender/blenlib/intern/math_base_inline.c @@ -0,0 +1,127 @@ +/** + * $Id$ + * + * ***** BEGIN GPL LICENSE BLOCK ***** + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software Foundation, + * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + * + * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV. + * All rights reserved. + * + * The Original Code is: some of this file. + * + * ***** END GPL LICENSE BLOCK ***** + * */ + +#include <float.h> +#include <math.h> +#include <stdio.h> +#include <stdlib.h> +#include <string.h> + +#include "BLI_math.h" + +#ifndef BLI_MATH_BASE_INLINE +#define BLI_MATH_BASE_INLINE + +/* A few small defines. Keep'em local! */ +#define SMALL_NUMBER 1.e-8 + +MINLINE float sqrt3f(float f) +{ + if(f==0.0) return 0; + if(f<0) return (float)(-exp(log(-f)/3)); + else return (float)(exp(log(f)/3)); +} + +MINLINE double sqrt3d(double d) +{ + if(d==0.0) return 0; + if(d<0) return -exp(log(-d)/3); + else return exp(log(d)/3); +} + +MINLINE float saacos(float fac) +{ + if(fac<= -1.0f) return (float)M_PI; + else if(fac>=1.0f) return 0.0; + else return (float)acos(fac); +} + +MINLINE float saasin(float fac) +{ + if(fac<= -1.0f) return (float)-M_PI/2.0f; + else if(fac>=1.0f) return (float)M_PI/2.0f; + else return (float)asin(fac); +} + +MINLINE float sasqrt(float fac) +{ + if(fac<=0.0) return 0.0; + return (float)sqrt(fac); +} + +MINLINE float saacosf(float fac) +{ + if(fac<= -1.0f) return (float)M_PI; + else if(fac>=1.0f) return 0.0f; + else return (float)acosf(fac); +} + +MINLINE float saasinf(float fac) +{ + if(fac<= -1.0f) return (float)-M_PI/2.0f; + else if(fac>=1.0f) return (float)M_PI/2.0f; + else return (float)asinf(fac); +} + +MINLINE float sasqrtf(float fac) +{ + if(fac<=0.0) return 0.0; + return (float)sqrtf(fac); +} + +MINLINE float interpf(float target, float origin, float fac) +{ + return (fac*target) + (1.0f-fac)*origin; +} + +/* useful to calculate an even width shell, by taking the angle between 2 planes. + * The return value is a scale on the offset. + * no angle between planes is 1.0, as the angle between the 2 planes approches 180d + * the distance gets very high, 180d would be inf, but this case isn't valid */ +MINLINE float shell_angle_to_dist(const float angle) +{ + return (angle < SMALL_NUMBER) ? 1.0f : fabsf(1.0f / cosf(angle * (M_PI/180.0f))); +} + +/* used for zoom values*/ +MINLINE float power_of_2(float val) +{ + return (float)pow(2, ceil(log(val) / log(2))); +} + +MINLINE float minf(float a, float b) +{ + return (a < b)? a: b; +} + +MINLINE float maxf(float a, float b) +{ + return (a > b)? a: b; +} + +#endif /* BLI_MATH_BASE_INLINE */ + diff --git a/source/blender/blenlib/intern/math_color.c b/source/blender/blenlib/intern/math_color.c index 044f1ca743e..fcc421e1289 100644 --- a/source/blender/blenlib/intern/math_color.c +++ b/source/blender/blenlib/intern/math_color.c @@ -386,6 +386,11 @@ int constrain_rgb(float *r, float *g, float *b) return 0; /* Color within RGB gamut */ } +float rgb_to_grayscale(float rgb[3]) +{ + return 0.3f*rgb[0] + 0.58f*rgb[1] + 0.12f*rgb[2]; +} + /* ********************************* lift/gamma/gain / ASC-CDL conversion ********************************* */ void lift_gamma_gain_to_asc_cdl(float *lift, float *gamma, float *gain, float *offset, float *slope, float *power) @@ -400,3 +405,4 @@ void lift_gamma_gain_to_asc_cdl(float *lift, float *gamma, float *gain, float *o power[c]= 1.0f/gamma[c]; } } + diff --git a/source/blender/blenlib/intern/math_geom.c b/source/blender/blenlib/intern/math_geom.c index a80f999cdbb..8fb7a0e1a55 100644 --- a/source/blender/blenlib/intern/math_geom.c +++ b/source/blender/blenlib/intern/math_geom.c @@ -1730,6 +1730,43 @@ void lookat_m4(float mat[][4],float vx, float vy, float vz, float px, float py, translate_m4(mat,-vx,-vy,-vz); /* translate viewpoint to origin */ } +int box_clip_bounds_m4(float boundbox[2][3], float bounds[4], float winmat[4][4]) +{ + float mat[4][4], vec[4]; + int a, fl, flag= -1; + + copy_m4_m4(mat, winmat); + + for(a=0; a<8; a++) { + vec[0]= (a & 1)? boundbox[0][0]: boundbox[1][0]; + vec[1]= (a & 2)? boundbox[0][1]: boundbox[1][1]; + vec[2]= (a & 4)? boundbox[0][2]: boundbox[1][2]; + vec[3]= 1.0; + mul_m4_v4(mat, vec); + + fl= 0; + if(bounds) { + if(vec[0] > bounds[1]*vec[3]) fl |= 1; + if(vec[0]< bounds[0]*vec[3]) fl |= 2; + if(vec[1] > bounds[3]*vec[3]) fl |= 4; + if(vec[1]< bounds[2]*vec[3]) fl |= 8; + } + else { + if(vec[0] < -vec[3]) fl |= 1; + if(vec[0] > vec[3]) fl |= 2; + if(vec[1] < -vec[3]) fl |= 4; + if(vec[1] > vec[3]) fl |= 8; + } + if(vec[2] < -vec[3]) fl |= 16; + if(vec[2] > vec[3]) fl |= 32; + + flag &= fl; + if(flag==0) return 0; + } + + return flag; +} + /********************************** Mapping **********************************/ void map_to_tube(float *u, float *v,float x, float y, float z) diff --git a/source/blender/blenlib/intern/math_vector.c b/source/blender/blenlib/intern/math_vector.c index bb7c65c4884..950d0bb21a5 100644 --- a/source/blender/blenlib/intern/math_vector.c +++ b/source/blender/blenlib/intern/math_vector.c @@ -84,50 +84,6 @@ void mid_v3_v3v3(float *v, float *v1, float *v2) v[2]= 0.5f*(v1[2] + v2[2]); } -/********************************* Comparison ********************************/ - -int is_zero_v3(float *v) -{ - return (v[0] == 0 && v[1] == 0 && v[2] == 0); -} - -int equals_v3v3(float *v1, float *v2) -{ - return ((v1[0]==v2[0]) && (v1[1]==v2[1]) && (v1[2]==v2[2])); -} - -int compare_v3v3(float *v1, float *v2, float limit) -{ - if(fabs(v1[0]-v2[0])<limit) - if(fabs(v1[1]-v2[1])<limit) - if(fabs(v1[2]-v2[2])<limit) - return 1; - - return 0; -} - -int compare_len_v3v3(float *v1, float *v2, float limit) -{ - float x,y,z; - - x=v1[0]-v2[0]; - y=v1[1]-v2[1]; - z=v1[2]-v2[2]; - - return ((x*x + y*y + z*z) < (limit*limit)); -} - -int compare_v4v4(float *v1, float *v2, float limit) -{ - if(fabs(v1[0]-v2[0])<limit) - if(fabs(v1[1]-v2[1])<limit) - if(fabs(v1[2]-v2[2])<limit) - if(fabs(v1[3]-v2[3])<limit) - return 1; - - return 0; -} - /********************************** Angles ***********************************/ /* Return the angle in radians between vecs 1-2 and 2-3 in radians @@ -297,8 +253,6 @@ void reflect_v3_v3v3(float *out, float *v1, float *v2) copy_v3_v3(vec, v1); copy_v3_v3(normal, v2); - normalize_v3(normal); - dot2 = 2 * dot_v3v3(vec, normal); reflect[0] = vec[0] - (dot2 * normal[0]); diff --git a/source/blender/blenlib/intern/math_vector_inline.c b/source/blender/blenlib/intern/math_vector_inline.c index 9011234dccb..2181150f640 100644 --- a/source/blender/blenlib/intern/math_vector_inline.c +++ b/source/blender/blenlib/intern/math_vector_inline.c @@ -45,6 +45,14 @@ MINLINE void zero_v3(float r[3]) r[2]= 0.0f; } +MINLINE void zero_v4(float r[4]) +{ + r[0]= 0.0f; + r[1]= 0.0f; + r[2]= 0.0f; + r[3]= 0.0f; +} + MINLINE void copy_v2_v2(float r[2], const float a[2]) { r[0]= a[0]; @@ -58,6 +66,14 @@ MINLINE void copy_v3_v3(float r[3], const float a[3]) r[2]= a[2]; } +MINLINE void copy_v4_v4(float r[4], float a[4]) +{ + r[0]= a[0]; + r[1]= a[1]; + r[2]= a[2]; + r[3]= a[3]; +} + MINLINE void swap_v2_v2(float a[2], float b[2]) { SWAP(float, a[0], b[0]); @@ -71,6 +87,14 @@ MINLINE void swap_v3_v3(float a[3], float b[3]) SWAP(float, a[2], b[2]); } +MINLINE void swap_v4_v4(float a[4], float b[4]) +{ + SWAP(float, a[0], b[0]); + SWAP(float, a[1], b[1]); + SWAP(float, a[2], b[2]); + SWAP(float, a[3], b[3]); +} + /********************************* Arithmetic ********************************/ MINLINE void add_v2_v2(float *r, float *a) @@ -338,5 +362,54 @@ MINLINE void normal_float_to_short_v3(short *out, float *in) out[2] = (short)(in[2]*32767.0f); } +/********************************* Comparison ********************************/ + +MINLINE int is_zero_v3(float *v) +{ + return (v[0] == 0 && v[1] == 0 && v[2] == 0); +} + +MINLINE int is_one_v3(float *v) +{ + return (v[0] == 1 && v[1] == 1 && v[2] == 1); +} + +MINLINE int equals_v3v3(float *v1, float *v2) +{ + return ((v1[0]==v2[0]) && (v1[1]==v2[1]) && (v1[2]==v2[2])); +} + +MINLINE int compare_v3v3(float *v1, float *v2, float limit) +{ + if(fabs(v1[0]-v2[0])<limit) + if(fabs(v1[1]-v2[1])<limit) + if(fabs(v1[2]-v2[2])<limit) + return 1; + + return 0; +} + +MINLINE int compare_len_v3v3(float *v1, float *v2, float limit) +{ + float x,y,z; + + x=v1[0]-v2[0]; + y=v1[1]-v2[1]; + z=v1[2]-v2[2]; + + return ((x*x + y*y + z*z) < (limit*limit)); +} + +MINLINE int compare_v4v4(float *v1, float *v2, float limit) +{ + if(fabs(v1[0]-v2[0])<limit) + if(fabs(v1[1]-v2[1])<limit) + if(fabs(v1[2]-v2[2])<limit) + if(fabs(v1[3]-v2[3])<limit) + return 1; + + return 0; +} + #endif /* BLI_MATH_VECTOR_INLINE */ |