19 #ifndef SNARK_POINT_CLOUD_VOXELMAP_H
20 #define SNARK_POINT_CLOUD_VOXELMAP_H
22 #include <boost/array.hpp>
23 #include <boost/functional/hash.hpp>
24 #include <boost/unordered_map.hpp>
26 #include <comma/base/types.h>
32 template <
typename Array, std::
size_t Size >
33 struct array_hash :
public std::unary_function< Array, std::size_t >
35 std::size_t operator()( Array
const& array )
const
38 for( std::size_t i = 0; i < Size; ++i ) { boost::hash_combine( seed, array[i] ); }
49 template <
typename V,
unsigned int D,
typename P = Eigen::Matrix<
double, D, 1 > >
50 class voxel_map :
public boost::unordered_map< boost::array< comma::int32, D >, V, snark::array_hash< boost::array< comma::int32, D >, D > >
54 enum { dimensions = D };
66 typedef boost::unordered_map< index_type, voxel_type, snark::array_hash< index_type, D > >
base_type;
118 template <
typename V,
unsigned int D,
typename P >
121 , resolution_( resolution )
125 template <
typename V,
unsigned int D,
typename P >
127 : origin_( point_type::Zero() )
128 , resolution_( resolution )
132 template <
typename V,
unsigned int D,
typename P >
136 iterator it = this->base_type::find( index );
137 if( it != this->end() ) {
return it; }
138 return this->base_type::insert( std::make_pair( index,
voxel_type() ) ).first;
141 template <
typename V,
unsigned int D,
typename P >
142 inline std::pair< typename voxel_map< V, D, P >::iterator,
bool >
voxel_map< V, D, P >::insert(
const typename voxel_map< V, D, P >::point_type& point,
const typename voxel_map< V, D, P >::voxel_type& voxel )
144 return this->base_type::insert( std::make_pair( index_of( point ), voxel ) );
149 static int negative_flooring_ =
static_cast< int >( -1.5 ) == -1 ? -1 : static_cast< int >( -1.5 ) == -2 ? 0 : 0;
150 static int positive_flooring_ =
static_cast< int >( 1.5 ) == 1 ? 0 : static_cast< int >( 1.5 ) == 2 ? -1 : -1;
154 template <
typename V,
unsigned int D,
typename P >
155 inline typename voxel_map< V, D, P >::index_type
voxel_map< V, D, P >::index_of(
const typename voxel_map< V, D, P >::point_type& point,
const typename voxel_map< V, D, P >::point_type& origin,
const typename voxel_map< V, D, P >::point_type& resolution )
157 point_type diff = ( point - origin ).array() / resolution.array();
159 for(
unsigned int i = 0; i < dimensions; ++i )
163 if( diff[i] == d ) {
continue; }
164 index[i] += diff[i] < 0 ? impl::negative_flooring_ : ( d == 0 ? 0 : impl::positive_flooring_ );
169 template <
typename V,
unsigned int D,
typename P >
170 inline typename voxel_map< V, D, P >::index_type
voxel_map< V, D, P >::index_of(
const typename voxel_map< V, D, P >::point_type& point,
const typename voxel_map< V, D, P >::point_type& resolution )
172 return index_of( point, point_type::Zero(), resolution );
175 template <
typename V,
unsigned int D,
typename P >
178 return index_of( point, origin_, resolution_ );
181 template <
typename V,
unsigned int D,
typename P >
182 inline typename voxel_map< V, D, P >::iterator
voxel_map< V, D, P >::find(
const typename voxel_map< V, D, P >::point_type& point )
184 index_type i = index_of( point );
185 return this->base_type::find( i );
188 template <
typename V,
unsigned int D,
typename P >
189 inline typename voxel_map< V, D, P >::const_iterator
voxel_map< V, D, P >::find(
const typename voxel_map< V, D, P >::point_type& point )
const
191 index_type i = index_of( point );
192 return this->base_type::find( i );
195 template <
typename V,
unsigned int D,
typename P >
196 inline typename voxel_map< V, D, P >::iterator
voxel_map< V, D, P >::find(
const typename voxel_map< V, D, P >::index_type& index )
198 return this->base_type::find( index );
201 template <
typename V,
unsigned int D,
typename P >
202 inline typename voxel_map< V, D, P >::const_iterator
voxel_map< V, D, P >::find(
const typename voxel_map< V, D, P >::index_type& index )
const
204 return this->base_type::find( index );
207 template <
typename V,
unsigned int D,
typename P >
210 template <
typename V,
unsigned int D,
typename P >
215 #endif // SNARK_POINT_CLOUD_VOXELMAP_H