00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00026 #ifndef _MMC_MESH_UNIT_H
00027 #define _MMC_MESH_UNIT_H
00028
00029 #include <stdio.h>
00030 #include <math.h>
00031 #include "mcx_utils.h"
00032
00033 #ifdef MMC_USE_SSE
00034 #include <smmintrin.h>
00035 #endif
00036
00037 #ifdef MMC_LOGISTIC
00038 #include "logistic_rand.c"
00039 #elif defined MMC_SFMT
00040 #include "sfmt_rand.c"
00041 #else
00042 #include "posix_randr.c"
00043 #endif
00044
00045 #define MMC_UNDEFINED (3.40282347e+38F)
00046 #define R_RAND_MAX (1.f/RAND_MAX)
00047 #define TWO_PI (M_PI*2.0)
00048 #define EPS 1e-9f
00049 #define LOG_MT_MAX 22.1807097779182f
00050 #define R_MIN_MUS 1e9f
00051 #define R_C0 3.335640951981520e-12f //1/C0 in s/mm
00052
00053
00054
00063 typedef struct MMC_mesh{
00064 int nn;
00065 int ne;
00066 int prop;
00067 float3 *node;
00068 int4 *elem;
00069 int *type;
00070 int4 *facenb;
00071 medium *med;
00072 float *atte;
00073 float *weight;
00074 float *evol;
00075 float *nvol;
00076 } tetmesh;
00077
00078
00088 typedef struct MMC_raytracer{
00089 tetmesh *mesh;
00090 float3 *d;
00091 float3 *m;
00092 } raytracer;
00093
00094 void mesh_init(tetmesh *mesh);
00095 void mesh_init_from_cfg(tetmesh *mesh,Config *cfg);
00096 void mesh_loadnode(tetmesh *mesh,Config *cfg);
00097 void mesh_loadelem(tetmesh *mesh,Config *cfg);
00098 void mesh_loadfaceneighbor(tetmesh *mesh,Config *cfg);
00099 void mesh_loadmedia(tetmesh *mesh,Config *cfg);
00100 void mesh_loadelemvol(tetmesh *mesh,Config *cfg);
00101
00102 void mesh_clear(tetmesh *mesh);
00103 float mesh_normalize(tetmesh *mesh,Config *cfg, float Eabsorb, float Etotal);
00104 void mesh_build(tetmesh *mesh);
00105 void mesh_error(char *msg);
00106 void mesh_filenames(char *format,char *foutput,Config *cfg);
00107 void mesh_saveweight(tetmesh *mesh,Config *cfg);
00108
00109 void tracer_init(raytracer *tracer,tetmesh *mesh);
00110 void tracer_build(raytracer *tracer);
00111 void tracer_clear(raytracer *tracer);
00112 float mc_next_scatter(float g, float3 *dir,RandType *ran,RandType *ran0,Config *cfg);
00113
00114
00115 static inline void vec_add(float3 *a,float3 *b,float3 *res){
00116 res->x=a->x+b->x;
00117 res->y=a->y+b->y;
00118 res->z=a->z+b->z;
00119 }
00120 static inline void vec_diff(float3 *a,float3 *b,float3 *res){
00121 res->x=b->x-a->x;
00122 res->y=b->y-a->y;
00123 res->z=b->z-a->z;
00124 }
00125 static inline void vec_mult_add(float3 *a,float3 *b,float sa,float sb,float3 *res){
00126 res->x=sb*b->x+sa*a->x;
00127 res->y=sb*b->y+sa*a->y;
00128 res->z=sb*b->z+sa*a->z;
00129 }
00130 static inline void vec_cross(float3 *a,float3 *b,float3 *res){
00131 res->x=a->y*b->z-a->z*b->y;
00132 res->y=a->z*b->x-a->x*b->z;
00133 res->z=a->x*b->y-a->y*b->x;
00134 }
00135
00136 static inline void mmc_sincosf(float x, float * sine, float * cosine){
00137 #if defined(__GNUC__) && defined(__linux__)
00138 __builtin_sincosf(x, sine, cosine);
00139 #else
00140 *sine = sinf(x);
00141 *cosine = cosf(x);
00142 #endif
00143 }
00144
00145 #ifndef MMC_USE_SSE
00146 static inline float vec_dot(float3 *a,float3 *b){
00147 return a->x*b->x+a->y*b->y+a->z*b->z;
00148 }
00149 #else
00150
00151 #ifndef __SSE4_1__
00152 static inline float vec_dot(float3 *a,float3 *b){
00153 float dot;
00154 __m128 na,nb,res;
00155 na=_mm_load_ps(&a->x);
00156 nb=_mm_load_ps(&b->x);
00157 res=_mm_mul_ps(na,nb);
00158 res=_mm_hadd_ps(res,res);
00159 res=_mm_hadd_ps(res,res);
00160 _mm_store_ss(&dot,res);
00161 return dot;
00162 }
00163 #else
00164 static inline float vec_dot(float3 *a,float3 *b){
00165 float dot;
00166 __m128 na,nb,res;
00167 na=_mm_load_ps(&a->x);
00168 nb=_mm_load_ps(&b->x);
00169 res=_mm_dp_ps(na,nb,0x7f);
00170 _mm_store_ss(&dot,res);
00171 return dot;
00172 }
00173 #endif
00174
00175 #endif
00176
00177 static inline float pinner(float3 *Pd,float3 *Pm,float3 *Ad,float3 *Am){
00178 return vec_dot(Pd,Am)+vec_dot(Pm,Ad);
00179 }
00180
00181
00182 static inline float dist2(float3 *p0,float3 *p1){
00183 return (p1->x-p0->x)*(p1->x-p0->x)+(p1->y-p0->y)*(p1->y-p0->y)+(p1->z-p0->z)*(p1->z-p0->z);
00184 }
00185
00186 static inline float dist(float3 *p0,float3 *p1){
00187 return sqrt(dist2(p0,p1));
00188 }
00189 #endif