9 #ifndef IMPALGEBRA_GRID_STORAGES_H
10 #define IMPALGEBRA_GRID_STORAGES_H
12 #include <IMP/algebra/algebra_config.h>
15 #include "internal/grid_apply.h"
20 #include <boost/iterator/transform_iterator.hpp>
26 IMPALGEBRA_BEGIN_NAMESPACE
37 template <
int D,
class VT>
39 typedef boost::scoped_array<VT> Data;
45 unsigned int index(
const I &i)
const {
47 for (
int d=D-1; d >=0; --d) {
48 unsigned int cur= i[d];
49 for (
int ld= d-1; ld >=0; --ld) {
56 unsigned int check= i[2]
71 NonDefault(
const VT &def): default_(def){}
73 bool operator()(
const P &def)
const {
74 return def.second != default_;
78 unsigned int get_extent()
const {
84 data_.reset(
new VT[extent_]);
85 std::copy(o.data_.get(), o.data_.get()+o.extent_,
89 void set_number_of_voxels(
Ints dims) {
91 for (
unsigned int i=0; i < dims.size(); ++i) {
94 data_.reset(
new VT[extent_]);
95 std::fill(data_.get(), data_.get()+get_extent(), default_);
103 default_(default_value) {
104 set_number_of_voxels(counts);
113 IMP_BRACKET(VT,
unsigned int, i<extent_,
return data_[i]);
118 extent_(0), default_(default_value) {
120 static bool get_is_dense() {
126 const VT* get_raw_data()
const {
139 #if !defined(IMP_DOXYGEN) && !defined(SWIG)
142 return operator[](gi);
146 return operator[](gi);
155 typedef VT* AllVoxelIterator;
156 typedef const VT* AllVoxelConstIterator;
157 AllVoxelIterator all_voxels_begin() {
160 AllVoxelIterator all_voxels_end() {
161 return data_.get()+get_extent();
163 AllVoxelConstIterator all_voxels_begin()
const {
166 AllVoxelConstIterator all_voxels_end()
const {
167 return data_.get()+get_extent();
175 template <
class Functor,
class Gr
id>
176 Functor apply(
const Grid &g,
const Functor &fi)
const {
178 typename Grid::ExtendedIndex lb(
Ints(g.get_dimension(),
181 typename Grid::Vector corner= g.get_bounding_box().get_corner(0);
182 typename Grid::Vector cell= g.get_unit_cell();
183 typename Grid::Index cur;
184 typename Grid::Vector center;
185 internal::GridApplier<Functor, Grid, D-1>::apply(g, lb, ub,
210 template <
int D,
class VT,
class Base,
216 typedef typename Data::const_iterator::value_type argument_type;
228 return stor_->get_has_index(ei);
232 return stor_->get_index(ei);
234 ItHelper(): stor_(
nullptr){}
242 const VT &default_value): Base(counts),
243 default_(default_value) {
246 << data_.size() <<
" cells set");
258 #if !defined(SWIG) && !defined(IMP_DOXYGEN)
259 SparseGridStorageD(
const VT &def): default_(def) {
261 static bool get_is_dense() {
264 using Base::get_number_of_voxels;
266 unsigned int get_number_of_voxels()
const {
271 return data_.find(
GridIndexD<D>(i.begin(), i.end())) != data_.end();
279 #if !defined(IMP_DOXYGEN) && !defined(SWIG)
282 typename Map::iterator it= data_.find(gi);
283 if (it == data_.end()) {
284 return data_.insert(std::make_pair(gi, default_)).first->second;
289 const VT &get_value_always(
const ExtendedGridIndexD<D> &i)
const {
290 GridIndexD<D> gi(i.begin(), i.end());
291 typename Map::const_iterator it= data_.find(gi);
292 if (it == data_.end()) {
307 "Invalid index " << i);
308 return data_.find(i)->second);
317 typedef typename Data::const_iterator AllConstIterator;
318 AllConstIterator all_begin()
const {
319 return data_.begin();
321 AllConstIterator all_end()
const {
326 base::Vector<GridIndexD<D> > get_all_indexes()
const {
327 return base::Vector<GridIndexD<D> >
328 (boost::make_transform_iterator(all_begin(), GetIndex()),
329 boost::make_transform_iterator(all_end(), GetIndex()));
354 typedef internal::GridIndexIterator<ExtendedGridIndexD<D>,
355 ItHelper > IndexIterator;
360 IndexIterator indexes_begin(
const ExtendedGridIndexD<D>& lb,
361 const ExtendedGridIndexD<D>& ub)
const {
362 ExtendedGridIndexD<D> eub=ub.get_offset(1,1,1);
364 return IndexIterator();
368 return IndexIterator(lb, eub, ItHelper(
this));
371 IndexIterator indexes_end(
const ExtendedGridIndexD<D>&,
372 const ExtendedGridIndexD<D>&)
const {
374 return IndexIterator();
378 base::Vector<GridIndexD<D> >
380 const ExtendedGridIndexD<D>& ub)
const {
381 return base::Vector<GridIndexD<D> >(indexes_begin(lb, ub),
382 indexes_end(lb, ub));
387 template <
class Functor,
class Gr
id>
388 Functor apply(
const Grid &g, Functor f)
const {
389 for (
typename Data::const_iterator it= data_.begin();
390 it != data_.end(); ++it) {
391 f(g, it->first, g.get_center(it->first));
402 for (
typename Data::const_iterator it= data_.begin();
403 it != data_.end(); ++it) {
404 for (
unsigned int i=0; i< ret.get_dimension(); ++i) {
405 ret.access_data().get_data()[i]
406 = std::min(ret[i], it->first[i]);
416 for (
typename Data::const_iterator it= data_.begin();
417 it != data_.end(); ++it) {
418 for (
unsigned int i=0; i< ret.get_dimension(); ++i) {
419 ret.access_data().get_data()[i]
420 = std::min(ret[i], it->first[i]);
426 IMPALGEBRA_END_NAMESPACE