00001 #ifndef OPENTISSUE_CORE_CONTAINERS_GRID_UTIL_CROP_H
00002 #define OPENTISSUE_CORE_CONTAINERS_GRID_UTIL_CROP_H
00003
00004
00005
00006
00007
00008
00009
00010 #include <OpenTissue/configuration.h>
00011
00012 #include <cmath>
00013
00014 #include <OpenTissue/core/containers/grid/util/grid_idx2coord.h>
00015 #include <OpenTissue/core/math/math_vector3.h>
00016
00017 namespace OpenTissue
00018 {
00019 namespace grid
00020 {
00029 template < typename grid_type >
00030 inline void crop(grid_type const & M, grid_type & m, typename grid_type::value_type const & treshold)
00031 {
00032 using std::min;
00033 using std::max;
00034
00035 typedef typename grid_type::math_types math_types;
00036 typedef typename math_types::vector3_type vector3_type;
00037
00038 typedef OpenTissue::math::Vector3<size_t> index_vector;
00039
00040 index_vector min_idx( M.I(), M.J(), M.K() );
00041 index_vector max_idx( 0, 0, 0 );
00042
00043 for(size_t k=0; k<M.K(); ++k )
00044 for(size_t j=0; j<M.J(); ++j )
00045 for(size_t i=0; i<M.I(); ++i )
00046 {
00047 if ( M(i,j,k) > treshold )
00048 {
00049 min_idx = min( min_idx, index_vector(i,j,k) );
00050 max_idx = max( max_idx, index_vector(i,j,k) );
00051 }
00052 }
00053 index_vector new_dim = (max_idx - min_idx) + index_vector(1);
00054
00055 vector3_type min_coord,max_coord;
00056 idx2coord(M, min_idx(0), min_idx(1), min_idx(2),min_coord);
00057 idx2coord(M, max_idx(0), max_idx(1), max_idx(2),max_coord);
00058
00059 m.create( min_coord, max_coord, new_dim(0), new_dim(1), new_dim(2) );
00060
00061 size_t i_offset = min_idx(0);
00062 size_t j_offset = min_idx(1);
00063 size_t k_offset = min_idx(2);
00064 for(size_t k=0; k<m.K(); ++k )
00065 for(size_t j=0; j<m.J(); ++j )
00066 for(size_t i=0; i<m.I(); ++i )
00067 {
00068 m(i,j,k) = M( i+i_offset, j+j_offset, k+k_offset );
00069 }
00070 }
00071
00084 template < typename grid_type >
00085 inline void crop(
00086 grid_type const & M
00087 , grid_type & m
00088 , size_t min_i
00089 , size_t min_j
00090 , size_t min_k
00091 , size_t max_i
00092 , size_t max_j
00093 , size_t max_k
00094 )
00095 {
00096 typedef typename grid_type::math_types math_types;
00097 typedef typename math_types::vector3_type vector3_type;
00098 typedef typename grid_type::index_iterator index_iterator;
00099
00100 vector3_type min_coord,max_coord;
00101 idx2coord(M, min_i, min_j, min_k, min_coord);
00102 idx2coord(M, max_i, max_j, max_k, max_coord);
00103 m.create( min_coord, max_coord, max_i-min_i+1, max_j-min_j+1, max_k-min_k+1 );
00104
00105 for(size_t k=0; k<m.K(); ++k )
00106 for(size_t j=0; j<m.J(); ++j )
00107 for(size_t i=0; i<m.I(); ++i )
00108 {
00109 m(i,j,k) = M( i+min_i, j+min_j, k+min_k );
00110 }
00111 }
00112 }
00113 }
00114
00115
00116 #endif