Go to the documentation of this file.00001 #ifndef OPENTISSUE_CORE_GEOMETRY_GEOMETRY_SAMPLING_DIRECTIONS_H
00002 #define OPENTISSUE_CORE_GEOMETRY_GEOMETRY_SAMPLING_DIRECTIONS_H
00003
00004
00005
00006
00007
00008
00009
00010 #include <OpenTissue/configuration.h>
00011
00012 #include <OpenTissue/core/containers/mesh/polymesh/polymesh.h>
00013 #include <OpenTissue/core/containers/mesh/polymesh/util/polymesh_make_sphere.h>
00014 #include <OpenTissue/core/containers/mesh/common/util/mesh_make_sphere.h>
00015
00016 #include <boost/cast.hpp>
00017 #include <cmath>
00018 #include <vector>
00019
00020 namespace OpenTissue
00021 {
00022 namespace geometry
00023 {
00024
00025
00026 namespace detail
00027 {
00028
00032 class SamplingTypePicker
00033 {
00034 public:
00035
00036 typedef enum { sphere_type, random_type, tetrahedron_type, icosahedron_type } sampling_type;
00037
00038 protected:
00039
00040 static sampling_type & type()
00041 {
00042 static sampling_type tmp = icosahedron_type;
00043 return tmp;
00044 }
00045
00046 public:
00047
00048 static sampling_type get_type()
00049 {
00050 changed() = false;
00051 return type();
00052 }
00053
00054 static void set_type(sampling_type new_type)
00055 {
00056 changed() = true;
00057 type() = new_type;
00058 }
00059
00060 static bool & changed()
00061 {
00062 static bool tmp = true;
00063 return tmp;
00064 }
00065 };
00066
00070 template<typename vector3_type>
00071 class SamplingDirections
00072 {
00073 public:
00074
00075 typedef enum { sphere_type, random_type, tetrahedron_type, icosahedron_type} sampling_type;
00076
00077 protected:
00078
00079 typedef polymesh::PolyMesh<> mesh_type;
00080
00081 protected:
00082
00083 static std::vector<vector3_type> & dir()
00084 {
00085 static std::vector<vector3_type> m_dir;
00086 return m_dir;
00087 }
00088
00089 public:
00090
00091 SamplingDirections()
00092 {
00093 init();
00094 }
00095
00096 public:
00097
00098 void init()
00099 {
00100 using std::sqrt;
00101
00102 typedef typename vector3_type::value_type real_type;
00103
00104 bool changed = SamplingTypePicker::changed();
00105 SamplingTypePicker::sampling_type type = SamplingTypePicker::get_type();
00106
00107 if(!changed && !dir().empty() )
00108 return;
00109
00110 unsigned int max_samples = 0;
00111
00112 switch( type )
00113 {
00114 case SamplingTypePicker::icosahedron_type:
00115 {
00116 std::cout << "SamplingDirections::init(): Using icosahedron type" << std::endl;
00117 mesh_type sphere;
00118 make_sphere(1.0,3,sphere);
00119 max_samples = static_cast<unsigned int>( sphere.size_vertices() );
00120 dir().resize(max_samples);
00121 typename mesh_type::vertex_iterator v = sphere.vertex_begin();
00122 for(unsigned int k=0;k<max_samples;++k,++v)
00123 dir()[k] = unit(v->m_coord);
00124 }
00125 break;
00126 case SamplingTypePicker::tetrahedron_type:
00127 {
00128 std::cout << "SamplingDirections::init(): Using tetrahedron type" << std::endl;
00129
00130
00131 mesh_type sphere;
00132 make_sphere(1.0,4,sphere,true);
00133 max_samples = static_cast<unsigned int>( sphere.size_vertices() );
00134 dir().resize(max_samples);
00135 typename mesh_type::vertex_iterator v = sphere.vertex_begin();
00136 for(unsigned int k=0;k<max_samples;++k,++v)
00137 dir()[k] = unit(v->m_coord);
00138 }
00139 break;
00140 case SamplingTypePicker::random_type:
00141 {
00142 std::cout << "SamplingDirections::init(): Using random type" << std::endl;
00143
00144 max_samples = 642;
00145 dir().resize(max_samples);
00146 for(unsigned int k=0;k<max_samples;++k)
00147 {
00148 random(dir()[k],-1,1);
00149 dir()[k] = unit(dir()[k]);
00150 }
00151 }
00152 break;
00153 case SamplingTypePicker::sphere_type:
00154 {
00155 std::cout << "SamplingDirections::init(): Using sphere type" << std::endl;
00156
00157 mesh_type sphere;
00158 mesh::make_sphere(1.0,25,25,sphere);
00159 max_samples = static_cast<unsigned int>( sphere.size_vertices() );
00160 dir().resize(max_samples);
00161 typename mesh_type::vertex_iterator v = sphere.vertex_begin();
00162 for(unsigned int k=0;k<max_samples;++k,++v)
00163 dir()[k] = unit(v->m_coord);
00164 }
00165 break;
00166 };
00167 std::cout << "sample_directions::init(): sampling directions = " << max_samples << std::endl;
00168 }
00169
00170 std::size_t size() const { return dir().size(); }
00171
00172 vector3_type const & operator()(unsigned int idx) const { return dir()[idx]; }
00173
00174 };
00175
00176 }
00177
00178 }
00179 }
00180
00181
00182 #endif