MC_grid_3d_scalar.cpp
Go to the documentation of this file.00001
00002 #include <MC_grid_3d_scalar.hpp>
00003 #include <MC_potential_sphere.hpp>
00004
00005 namespace mesh_conv
00006 {
00007 MC_grid_3d_scalar::MC_grid_3d_scalar()
00008 :dimension_internal(MC_v3d(1,1,1)),offset_internal(MC_v3d(0,0,0))
00009 {}
00010
00011 MC_grid_3d_scalar::MC_grid_3d_scalar(const MC_int_vector& given_size,const MC_v3d& real_size,const MC_v3d& offset)
00012 :dimension_internal(real_size),offset_internal(offset)
00013 {
00014 potential=MC_grid_3d<float>(given_size);
00015 }
00016 const MC_int_vector& MC_grid_3d_scalar::size() const
00017 {return potential.size();}
00018 bool MC_grid_3d_scalar::is_within_bound(const MC_v3d& x) const
00019 {
00020 MC_v3d y=x-offset_internal;
00021 if(y[0]>=0 && y[0]<dimension_internal[0])
00022 if(y[1]>=0 && y[1]<dimension_internal[1])
00023 if(y[2]>=0 && y[2]<dimension_internal[2])
00024 return true;
00025 return false;
00026 }
00027 bool MC_grid_3d_scalar::is_within_bound_voxel(const MC_int_vector& x) const
00028 {
00029 MC_int_vector dim=size();
00030 if(x[0]>=0 && x[0]<dim[0])
00031 if(x[1]>=0 && x[1]<dim[1])
00032 if(x[2]>=0 && x[2]<dim[2])
00033 return true;
00034 return false;
00035 }
00036
00037 void MC_grid_3d_scalar::fill(const float& value)
00038 {
00039 potential.fill(value);
00040 }
00041 const MC_v3d& MC_grid_3d_scalar::dimension() const
00042 {
00043 return dimension_internal;
00044 }
00045 MC_v3d& MC_grid_3d_scalar::dimension()
00046 {
00047 return dimension_internal;
00048 }
00049
00050 const MC_v3d& MC_grid_3d_scalar::offset() const
00051 {
00052 return offset_internal;
00053 }
00054 MC_v3d& MC_grid_3d_scalar::offset()
00055 {
00056 return offset_internal;
00057 }
00058
00059 MC_v3d MC_grid_3d_scalar::convert_real_to_voxel_index(const MC_v3d& x) const
00060 {
00061 MC_v3d u;
00062 for(unsigned int k_dim=0;k_dim<3;++k_dim)
00063 u[k_dim]=(x[k_dim]-offset_internal[k_dim])/dimension_internal[k_dim]*size()[k_dim];
00064 return u;
00065 }
00066 MC_v3d MC_grid_3d_scalar::convert_voxel_index_to_real(const MC_v3d& u) const
00067 {
00068 MC_v3d x;
00069 for(unsigned int k_dim=0;k_dim<3;++k_dim)
00070 x[k_dim]=u[k_dim]/static_cast<double>(size()[k_dim])*dimension_internal[k_dim]+offset_internal[k_dim];
00071 return x;
00072 }
00073
00074 const float& MC_grid_3d_scalar::get(const MC_int_vector& u) const
00075 {
00076 return potential.get(u);
00077 }
00078 float& MC_grid_3d_scalar::get(const MC_int_vector& u)
00079 {
00080 return potential.get(u);
00081 }
00082 void MC_grid_3d_scalar::set(const MC_int_vector& u,const float& value)
00083 {
00084 potential.get(u)=value;
00085 }
00086 float MC_grid_3d_scalar::operator()(const MC_v3d& x) const
00087 {
00088 return linear_interpolation(x);
00089 }
00090
00091 float MC_grid_3d_scalar::linear_interpolation(const MC_v3d& x) const
00092 {
00093 if(is_within_bound(x)==false)
00094 return 0.0;
00095
00096
00097
00098 MC_v3d u=convert_real_to_voxel_index(x);
00099 MC_int_vector u0=MC_int_vector(static_cast<int>(u[0]),
00100 static_cast<int>(u[1]),
00101 static_cast<int>(u[2])
00102 );
00103 MC_v3d alpha=u-MC_v3d(u0[0],u0[1],u0[2]);
00104
00105
00106
00107
00108 if(u0[0]==0 || u0[0]==size()[0]-1 || u0[1]==0 || u0[1]==size()[1]-1 || u0[2]==0 || u0[2]==size()[2]-1)
00109 return get(u0);
00110
00111
00112 float x000=get(u0+MC_int_vector(0,0,0));
00113 float x001=get(u0+MC_int_vector(0,0,1));
00114 float x010=get(u0+MC_int_vector(0,1,0));
00115 float x011=get(u0+MC_int_vector(0,1,1));
00116 float x100=get(u0+MC_int_vector(1,0,0));
00117 float x101=get(u0+MC_int_vector(1,0,1));
00118 float x110=get(u0+MC_int_vector(1,1,0));
00119 float x111=get(u0+MC_int_vector(1,1,1));
00120
00121 float y=x000*(1-alpha[0])*(1-alpha[1])*(1-alpha[2]) +
00122 x001*(1-alpha[0])*(1-alpha[1])*( alpha[2]) +
00123 x010*(1-alpha[0])*( alpha[1])*(1-alpha[2]) +
00124 x011*(1-alpha[0])*( alpha[1])*( alpha[2]) +
00125 x100*( alpha[0])*(1-alpha[1])*(1-alpha[2]) +
00126 x101*( alpha[0])*(1-alpha[1])*( alpha[2]) +
00127 x110*( alpha[0])*( alpha[1])*(1-alpha[2]) +
00128 x111*( alpha[0])*( alpha[1])*( alpha[2]) ;
00129 return y;
00130 }
00131
00132
00133 void MC_grid_3d_scalar::add_potential(const implicit::MC_potential_sphere& sphere_implicit)
00134 {
00135 const MC_v3d& center=sphere_implicit.sphere().center();
00136 const double& radius=sphere_implicit.sphere().radius();
00137
00138 MC_v3d center_voxel=convert_real_to_voxel_index(center);
00139 MC_int_vector center_voxel_integer=MC_int_vector(static_cast<int>(center_voxel[0]+0.5),static_cast<int>(center_voxel[1]+0.5),static_cast<int>(center_voxel[2]+0.5));
00140
00141
00142
00143
00144
00145
00146
00147
00148
00149 }
00150
00151 }