From: hackbard Date: Tue, 5 Dec 2006 15:48:45 +0000 (+0000) Subject: moved functions to header file and made them static inlines X-Git-Url: https://hackdaworld.org/gitweb/?a=commitdiff_plain;h=01be83f52829176f95a7fa6bb38a9f3518d44aa9;p=physik%2Fposic.git moved functions to header file and made them static inlines --- diff --git a/math/math.c b/math/math.c deleted file mode 100644 index 54d79fd..0000000 --- a/math/math.c +++ /dev/null @@ -1,100 +0,0 @@ -/* - * math.c - basic vector calculations - * - * author: Frank Zirkelbach - * - */ - - -#include -#include - -#include "math.h" - -int v3_add(t_3dvec *sum,t_3dvec *a,t_3dvec *b) { - - sum->x=a->x+b->x; - sum->y=a->y+b->y; - sum->z=a->z+b->z; - - return 0; -} - -int v3_sub(t_3dvec *sum,t_3dvec *a,t_3dvec *b) { - - sum->x=a->x-b->x; - sum->y=a->y-b->y; - sum->z=a->z-b->z; - - return 0; -} - -int v3_scale(t_3dvec *result,t_3dvec *a,double s) { - - result->x=s*a->x; - result->y=s*a->y; - result->z=s*a->z; - - return 0; -} - -int v3_zero(t_3dvec *vec) { - - vec->x=.0; - vec->y=.0; - vec->z=.0; - - return 0; -} - -int v3_set(t_3dvec *vec,double *ptr) { - - memcpy(vec,ptr,sizeof(t_3dvec)); - - return 0; -} - -int v3_copy(t_3dvec *trg,t_3dvec *src) { - - memcpy(trg,src,sizeof(t_3dvec)); - - return 0; -} - -int v3_cmp(t_3dvec *a,t_3dvec *b) { - - return(memcmp(a,b,sizeof(t_3dvec))); -} - -double v3_scalar_product(t_3dvec *a,t_3dvec *b) { - - return(a->x*b->x+a->y*b->y+a->z*b->z); -} - -double v3_absolute_square(t_3dvec *a) { - - return(a->x*a->x+a->y*a->y+a->z*a->z); -} - -double v3_norm(t_3dvec *a) { - - return(sqrt(v3_absolute_square(a))); -} - -int v3_per_bound(t_3dvec *a,t_3dvec *dim) { - - double x,y,z; - - x=0.5*dim->x; - y=0.5*dim->y; - z=0.5*dim->z; - - if(a->x>=x) a->x-=dim->x; - else if(-a->x>x) a->x+=dim->x; - if(a->y>=y) a->y-=dim->y; - else if(-a->y>y) a->y+=dim->y; - if(a->z>=z) a->z-=dim->z; - else if(-a->z>z) a->z+=dim->z; - - return 0; -} diff --git a/math/math.h b/math/math.h index 8bbcc6c..8ff1bec 100644 --- a/math/math.h +++ b/math/math.h @@ -8,6 +8,9 @@ #ifndef MATH_H #define MATH_H +#include +#include + /* datatypes */ typedef struct s_3dvec { @@ -18,16 +21,106 @@ typedef struct s_3dvec { /* function prototypes */ -int v3_add(t_3dvec *sum,t_3dvec *a,t_3dvec *b); -int v3_sub(t_3dvec *sum,t_3dvec *a,t_3dvec *b); -int v3_scale(t_3dvec *result,t_3dvec *a,double s); -int v3_zero(t_3dvec *vec); -int v3_set(t_3dvec *vec,double *ptr); -int v3_copy(t_3dvec *trg,t_3dvec *src); -int v3_cmp(t_3dvec *a,t_3dvec *b); -double v3_scalar_product(t_3dvec *a,t_3dvec *b); -double v3_absolute_square(t_3dvec *a); -double v3_norm(t_3dvec *a); -int v3_per_bound(t_3dvec *a,t_3dvec *dim); +static inline int v3_add(t_3dvec *sum,t_3dvec *a,t_3dvec *b) __attribute__((always_inline)); +static inline int v3_sub(t_3dvec *sum,t_3dvec *a,t_3dvec *b) __attribute__((always_inline)); +static inline int v3_scale(t_3dvec *result,t_3dvec *a,double s) __attribute__((always_inline)); +static inline int v3_zero(t_3dvec *vec) __attribute__((always_inline)); +static inline int v3_set(t_3dvec *vec,double *ptr) __attribute__((always_inline)); +static inline int v3_copy(t_3dvec *trg,t_3dvec *src) __attribute__((always_inline)); +static inline int v3_cmp(t_3dvec *a,t_3dvec *b) __attribute__((always_inline)); +static inline double v3_scalar_product(t_3dvec *a,t_3dvec *b) __attribute__((always_inline)); +static inline double v3_absolute_square(t_3dvec *a) __attribute__((always_inline)); +static inline double v3_norm(t_3dvec *a) __attribute__((always_inline)); +static inline int v3_per_bound(t_3dvec *a,t_3dvec *dim) __attribute__((always_inline)); + +/* function definitions */ + +static inline int v3_add(t_3dvec *sum,t_3dvec *a,t_3dvec *b) { + + sum->x=a->x+b->x; + sum->y=a->y+b->y; + sum->z=a->z+b->z; + + return 0; +} + +static inline int v3_sub(t_3dvec *sum,t_3dvec *a,t_3dvec *b) { + + sum->x=a->x-b->x; + sum->y=a->y-b->y; + sum->z=a->z-b->z; + + return 0; +} + +static inline int v3_scale(t_3dvec *result,t_3dvec *a,double s) { + + result->x=s*a->x; + result->y=s*a->y; + result->z=s*a->z; + + return 0; +} + +static inline int v3_zero(t_3dvec *vec) { + + vec->x=.0; + vec->y=.0; + vec->z=.0; + + return 0; +} + +static inline int v3_set(t_3dvec *vec,double *ptr) { + + memcpy(vec,ptr,sizeof(t_3dvec)); + + return 0; +} + +static inline int v3_copy(t_3dvec *trg,t_3dvec *src) { + + memcpy(trg,src,sizeof(t_3dvec)); + + return 0; +} + +static inline int v3_cmp(t_3dvec *a,t_3dvec *b) { + + return(memcmp(a,b,sizeof(t_3dvec))); +} + +static inline double v3_scalar_product(t_3dvec *a,t_3dvec *b) { + + return(a->x*b->x+a->y*b->y+a->z*b->z); +} + +static double v3_absolute_square(t_3dvec *a) { + + return(a->x*a->x+a->y*a->y+a->z*a->z); +} + +static inline double v3_norm(t_3dvec *a) { + + return(sqrt(a->x*a->x+a->y*a->y+a->z*a->z)); +} + +static inline int v3_per_bound(t_3dvec *a,t_3dvec *dim) { + + double x,y,z; + + x=0.5*dim->x; + y=0.5*dim->y; + z=0.5*dim->z; + + if(a->x>=x) a->x-=dim->x; + else if(-a->x>x) a->x+=dim->x; + if(a->y>=y) a->y-=dim->y; + else if(-a->y>y) a->y+=dim->y; + if(a->z>=z) a->z-=dim->z; + else if(-a->z>z) a->z+=dim->z; + + return 0; +} #endif