Image2Grid_impl.hpp

00001 // This file is part of the imaging2 class library.
00002 //
00003 // University of Innsbruck, Infmath Imaging, 2009.
00004 // http://infmath.uibk.ac.at
00005 //
00006 // All rights reserved.
00007 
00008 
00009 #ifndef FEM_IMAGE2GRID_IMPL_H
00010 #define FEM_IMAGE2GRID_IMPL_H
00011 
00012 #include <fem/Grid.hpp>
00013 
00014 #include <fem/fem_2d_square_types.hpp>
00015 #include <fem/fem_2d_triangle_types.hpp>
00016 #include <fem/fem_3d_cube_types.hpp>
00017 
00018 
00019 namespace imaging
00020 {
00021   namespace image2grid_impl
00022   {
00023     template <class fem_types>
00024     void populate_grid(Grid<fem_types> & grid, const ublas::fixed_vector<size_t, fem_types::data_dimension> & size);
00025     
00026     template <class vector_image_accessor_t, class fem_types>
00027     void construct_regular_2d_vertices(Grid<fem_types> & grid, const ublas::fixed_vector<size_t, 2> & size, const vector_image_accessor_t  & displacements)
00028     {
00029       typedef ublas::fixed_vector<float_t, 2> vertex_t;
00030       
00031       size_t n_x_vertices = size(0);
00032       size_t n_y_vertices = size(1);
00033   
00034       grid.set_regular(true);
00035   
00036       for(size_t i = 0; i < n_x_vertices; i++)
00037         for(size_t j = 0; j < n_y_vertices; ++j)
00038         {
00039           size_t vertex_index = i + j * n_x_vertices;
00040           vertex_t offset(displacements[ublas::fixed_vector<size_t, 2>(i, j)]);
00041           grid.set_vertex(vertex_index, vertex_t( .5 + float_t(i) + offset(0), .5 + float_t(j) + offset(1)));
00042         }  
00043     }
00044     
00045     template <class vector_image_accessor_t, class fem_types>
00046     void construct_regular_3d_vertices(Grid<fem_types> & grid, const ublas::fixed_vector<size_t, 3> & size, const vector_image_accessor_t  & displacements)
00047     {
00048       typedef ublas::fixed_vector<float_t, 3> vertex_t;
00049       
00050       size_t n_x_vertices = size(0);
00051       size_t n_y_vertices = size(1);
00052       size_t n_z_vertices = size(2);
00053       
00054       grid.set_regular(false);
00055       
00056       for(size_t i = 0; i < n_x_vertices; i++)
00057         for(size_t j = 0; j < n_y_vertices; ++j)
00058           for(size_t k = 0; k < n_z_vertices; ++k)
00059           {
00060             size_t vertex_index = i + j * n_x_vertices + k * n_x_vertices * n_y_vertices;
00061             vertex_t offset(displacements[ublas::fixed_vector<size_t, 3>(i, j, k)]);
00062             grid.set_vertex(vertex_index, vertex_t(0.5 + float_t(i) + offset(0), 
00063                                                    0.5 + float_t(j) + offset(1),
00064                                                    0.5 + float_t(k) + offset(2)));
00065           }  
00066     }
00067     
00068     template<class float_accessor_t> 
00069     void image2vector_2d(const ublas::fixed_vector<size_t, 2> size, const float_accessor_t & image, ublas::vector<float_t> & vector)
00070     {
00071       if(image.size() != size)
00072         throw Exception("Exception: Dimensions of image and grid do not agree in Image2Grid::image2vector().");
00073 
00074       size_t n_x_vertices = size(0);
00075       size_t n_y_vertices = size(1);
00076   
00077       vector.resize(n_x_vertices * n_y_vertices, false);
00078   
00079       for(size_t i = 0; i < n_x_vertices; i++)
00080         for(size_t j = 0; j < n_y_vertices; ++j)
00081         {
00082           size_t vertex_index = i + j * n_x_vertices;
00083           vector(vertex_index) = image[ublas::fixed_vector<size_t, 2>(i, j)];
00084         }
00085     }
00086      
00087     template<class float_accessor_t> 
00088     void image2boundary_vector_2d(const ublas::fixed_vector<size_t, 2> size, const float_accessor_t &image, ublas::mapped_vector<float_t> & vector)
00089     {
00090       if(image.size() != size)
00091         throw Exception("Exception: Dimensions of image and grid do not agree in Image2Grid::image2boundary_vector().");
00092   
00093       size_t n_x_vertices = size(0);
00094       size_t n_y_vertices = size(1);
00095   
00096       vector.resize(n_x_vertices * n_y_vertices, false);
00097   
00098       for(size_t i = 0; i < n_x_vertices; ++i)
00099         for(size_t j = 0; j < n_y_vertices; ++j)
00100         {
00101           size_t vertex_index = i + j * n_x_vertices;
00102           vector(vertex_index) = image[ublas::fixed_vector<size_t, 2>(i, j)];
00103   
00104           if(i > 0 && i < n_x_vertices - 1)
00105             j += n_y_vertices - 2;
00106         }
00107     }
00108     
00109     template <class float_accessor_t>
00110     void vector2image_2d(const ublas::fixed_vector<size_t, 2> size, const ublas::vector< float_t > &vector, float_accessor_t & image)
00111     {
00112       size_t n_x_vertices = size(0);
00113       size_t n_y_vertices = size(1);
00114   
00115       if(vector.size() != n_x_vertices * n_y_vertices)
00116         throw Exception("Exception: vector of wrong length in vector2image().");
00117       
00118       if(image.size() != size)
00119         throw Exception("Exception: image of wrong size in vector2image().");
00120   
00121       for(size_t i = 0; i < n_x_vertices; i++)
00122         for(size_t j = 0; j < n_y_vertices; ++j)
00123         {
00124           size_t vertex_index = i + j * n_x_vertices;
00125           image[ublas::fixed_vector<size_t, 2>(i, j)] = vector(vertex_index);
00126         }
00127     }
00128     
00129     template <class vector_image_accessor_t>
00130     void vector2vector_image_2d(const ublas::fixed_vector<size_t, 2> size, const ublas::vector< float_t > &vector, vector_image_accessor_t & vector_image)
00131     {
00132       const size_t n_x_vertices = size(0);
00133       const size_t n_y_vertices = size(1);
00134       const size_t dimension = vector_image_accessor_t::data_t::dimension;
00135   
00136       if(vector.size() != n_x_vertices * n_y_vertices * dimension)
00137         throw Exception("Exception: tla::vector of wrong length in vector2image().");
00138       
00139       if(vector_image.size() != size)
00140         throw Exception("Exception: vector_image of wrong size in vector2vector_image().");
00141   
00142       for(size_t i = 0; i < n_x_vertices; i++)
00143         for(size_t j = 0; j < n_y_vertices; ++j)
00144           for(size_t k = 0; k < dimension; ++k)
00145           {
00146             size_t vertex_index = dimension * ( i + j * n_x_vertices ) + k;
00147             vector_image[ublas::fixed_vector<size_t, 2>(i, j)](k) = vector(vertex_index);
00148           }
00149     }
00150     
00151     template<class float_accessor_t> 
00152     void image2vector_3d(const ublas::fixed_vector<size_t, 3> size, const float_accessor_t & image, ublas::vector<float_t> & vector)
00153     {
00154       if(image.size() != size)
00155         throw Exception("Exception: Dimensions of image and grid do not agree in Image2Grid::image2vector().");
00156       
00157       size_t n_x_vertices = size(0);
00158       size_t n_y_vertices = size(1);
00159       size_t n_z_vertices = size(2);
00160       
00161       vector.resize(n_x_vertices * n_y_vertices * n_z_vertices, false);
00162       
00163       for(size_t i = 0; i < n_x_vertices; i++)
00164         for(size_t j = 0; j < n_y_vertices; ++j)
00165           for (size_t k = 0; k < n_z_vertices; ++k)
00166           {
00167             size_t vertex_index = i + j * n_x_vertices + k * n_x_vertices * n_y_vertices;
00168             vector(vertex_index) = image[ublas::fixed_vector<size_t, 3>(i, j, k)];
00169           }
00170     }
00171      
00172     template<class float_accessor_t> 
00173     void image2boundary_vector_3d(const ublas::fixed_vector<size_t, 3> size, const float_accessor_t &image, ublas::mapped_vector<float_t> & vector)
00174     {
00175       if(image.size() != size)
00176         throw Exception("Exception: Dimensions of image and grid do not agree in Image2Grid::image2boundary_map().");
00177       
00178       size_t n_x_vertices = size(0);
00179       size_t n_y_vertices = size(1);
00180       size_t n_z_vertices = size(2);
00181      
00182       vector.resize(n_x_vertices * n_y_vertices * n_z_vertices, false);
00183       
00184       for(size_t i = 0; i < n_x_vertices; ++i)
00185         for(size_t j = 0; j < n_y_vertices; ++j)
00186           for(size_t k = 0; k < n_z_vertices; ++k)  
00187           {
00188             size_t vertex_index = i + j * n_x_vertices + k * n_x_vertices * n_y_vertices;
00189             vector(vertex_index) = image[ublas::fixed_vector<size_t, 3>(i, j, k)];
00190             if(i > 0 && i < n_x_vertices - 1 && j > 0 && j < n_y_vertices -1 )
00191               k += n_z_vertices - 2;
00192           }
00193     }
00194     
00195     template <class float_accessor_t>
00196     void vector2image_3d(const ublas::fixed_vector<size_t, 3> size, const ublas::vector< float_t > &vector, float_accessor_t & image)
00197     {
00198       size_t n_x_vertices = size(0);
00199       size_t n_y_vertices = size(1);
00200       size_t n_z_vertices = size(2);
00201       
00202       if(vector.size() != n_x_vertices * n_y_vertices * n_z_vertices)
00203         throw Exception("Exception: tla::vector of wrong length in vector2image().");
00204       
00205       if(image.size() != size)
00206         throw Exception("Exception: image of wrong size in vector2image().");
00207       
00208       for(size_t i = 0; i < n_x_vertices; i++)
00209         for(size_t j = 0; j < n_y_vertices; ++j)
00210           for(size_t k = 0; k < n_z_vertices; ++k) 
00211           {
00212             size_t vertex_index = i + j * n_x_vertices + k * n_x_vertices * n_y_vertices;
00213             image[ublas::fixed_vector<size_t, 3>(i, j, k)] = vector(vertex_index);
00214           }
00215     }
00216     
00217     template <class vector_image_accessor_t>
00218     void vector2vector_image_3d(const ublas::fixed_vector<size_t, 3> size, const ublas::vector< float_t > &vector, vector_image_accessor_t & vector_image)
00219     {
00220       const size_t n_x_vertices = size(0);
00221       const size_t n_y_vertices = size(1); 
00222       const size_t n_z_vertices = size(2);
00223       const size_t dimension = vector_image_accessor_t::data_t::dimension;
00224       
00225       if(vector.size() != n_x_vertices * n_y_vertices * n_z_vertices * dimension)
00226         throw Exception("Exception: tla::vector of wrong length in vector2image().");
00227       
00228       if(vector_image.size() != size)
00229         throw Exception("Exception: vector_image of wrong size in vector2vector_image().");
00230       
00231       for(size_t i = 0; i < n_x_vertices; i++)
00232         for(size_t j = 0; j < n_y_vertices; ++j)
00233           for(size_t k = 0; k < n_z_vertices; ++k)  
00234             for(size_t l = 0; l < dimension; ++l)
00235             {
00236               size_t vertex_index = dimension * ( i + j * n_x_vertices + k * n_x_vertices * n_y_vertices) + l;
00237               vector_image[ublas::fixed_vector<size_t, 3>(i, j, k)](l) = vector(vertex_index);
00238             }
00239     }
00240     
00241     
00242     template <>
00243     void populate_grid<fem_2d_triangle_types>(Grid<fem_2d_triangle_types> & grid, const ublas::fixed_vector<size_t, 2> & size);
00244     
00245     template <>
00246     void populate_grid<fem_2d_square_types>(Grid<fem_2d_square_types> & grid, const ublas::fixed_vector<size_t, 2> & size);
00247 
00248      template <>
00249     void populate_grid<fem_3d_cube_types>(Grid<fem_3d_cube_types> & grid, const ublas::fixed_vector<size_t, 3> & size);
00250     
00251     template <>
00252     void populate_grid<fem_3d_tetrahedra_types>(Grid<fem_3d_tetrahedra_types> & grid, const ublas::fixed_vector<size_t, 3> & size);
00253   }
00254 }
00255 
00256 
00257 #endif

Generated on Tue Feb 10 10:01:30 2009 for imaging2 by  doxygen 1.5.5