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) {
70 NonDefault(
const VT &def) : default_(def) {}
72 bool operator()(
const P &def)
const {
73 return def.second != default_;
77 unsigned int get_extent()
const {
return extent_; }
79 default_ = o.default_;
81 data_.reset(
new VT[extent_]);
82 std::copy(o.data_.get(), o.data_.get() + o.extent_, data_.get());
85 void set_number_of_voxels(
Ints dims) {
87 for (
unsigned int i = 0; i < dims.size(); ++i) {
90 data_.reset(
new VT[extent_]);
91 std::fill(data_.get(), data_.get() + get_extent(), default_);
100 set_number_of_voxels(counts);
109 IMP_BRACKET(VT,
unsigned int, i < extent_,
return data_[i]);
114 : extent_(0), default_(default_value) {}
115 static bool get_is_dense() {
return true; }
119 const VT *get_raw_data()
const {
return data_.get(); }
120 VT *get_raw_data() {
return data_.get(); }
128 #if !defined(IMP_DOXYGEN) && !defined(SWIG)
131 return operator[](gi);
135 return operator[](gi);
144 typedef VT *AllVoxelIterator;
145 typedef const VT *AllVoxelConstIterator;
146 AllVoxelIterator all_voxels_begin() {
return data_.get(); }
147 AllVoxelIterator all_voxels_end() {
return data_.get() + get_extent(); }
148 AllVoxelConstIterator all_voxels_begin()
const {
return data_.get(); }
149 AllVoxelConstIterator all_voxels_end()
const {
150 return data_.get() + get_extent();
157 template <
class Functor,
class Gr
id>
158 Functor apply(
const Grid &g,
const Functor &fi)
const {
160 typename Grid::ExtendedIndex lb(
typename Grid::ExtendedIndex::Filled(),
161 g.get_dimension(), 0);
163 typename Grid::Vector corner = g.get_bounding_box().get_corner(0);
164 typename Grid::Vector cell = g.get_unit_cell();
165 typename Grid::Index cur;
166 typename Grid::Vector center;
167 internal::GridApplier<Functor, Grid, D - 1>::apply(g, lb, ub, corner, cell,
181 template <
int D,
class VT,
class Base,
187 typedef typename Data::const_iterator::value_type argument_type;
199 return stor_->get_has_index(ei);
203 return stor_->get_index(ei);
205 ItHelper() : stor_(
nullptr) {}
214 : Base(counts), default_(default_value) {}
216 << data_.size() <<
" cells set");
224 void remove_voxel(
const GridIndexD<D> &gi) { data_.erase(gi); }
225 #if !defined(SWIG) && !defined(IMP_DOXYGEN)
226 SparseGridStorageD(
const VT &def) : default_(def) {}
227 static bool get_is_dense() {
return false; }
228 using Base::get_number_of_voxels;
230 unsigned int get_number_of_voxels()
const {
return data_.size(); }
233 return data_.find(
GridIndexD<D>(i.begin(), i.end())) != data_.end();
241 #if !defined(IMP_DOXYGEN) && !defined(SWIG)
244 typename Map::iterator it = data_.find(gi);
245 if (it == data_.end()) {
246 return data_.insert(std::make_pair(gi, default_)).first->second;
251 const VT &get_value_always(
const ExtendedGridIndexD<D> &i)
const {
252 GridIndexD<D> gi(i.begin(), i.end());
253 typename Map::const_iterator it = data_.find(gi);
254 if (it == data_.end()) {
268 VT, GridIndexD<D>,
true,
270 return data_.find(i)->second);
279 typedef typename Data::const_iterator AllConstIterator;
280 AllConstIterator all_begin()
const {
return data_.begin(); }
281 AllConstIterator all_end()
const {
return data_.end(); }
284 base::Vector<GridIndexD<D> > get_all_indexes()
const {
285 return base::Vector<GridIndexD<D> >(
286 boost::make_transform_iterator(all_begin(), GetIndex()),
287 boost::make_transform_iterator(all_end(), GetIndex()));
310 typedef internal::GridIndexIterator<ExtendedGridIndexD<D>, ItHelper>
316 IndexIterator indexes_begin(
const ExtendedGridIndexD<D> &lb,
317 const ExtendedGridIndexD<D> &ub)
const {
318 ExtendedGridIndexD<D> eub = ub.get_offset(1, 1, 1);
320 return IndexIterator();
323 return IndexIterator(lb, eub, ItHelper(
this));
326 IndexIterator indexes_end(
const ExtendedGridIndexD<D> &,
327 const ExtendedGridIndexD<D> &)
const {
329 return IndexIterator();
334 const ExtendedGridIndexD<D> &lb,
const ExtendedGridIndexD<D> &ub)
const {
335 return base::Vector<GridIndexD<D> >(indexes_begin(lb, ub),
336 indexes_end(lb, ub));
340 template <
class Functor,
class Gr
id>
341 Functor apply(
const Grid &g, Functor f)
const {
342 for (
typename Data::const_iterator it = data_.begin(); it != data_.end();
344 f(g, it->first, g.get_center(it->first));
354 for (
typename Data::const_iterator it = data_.begin(); it != data_.end();
356 for (
unsigned int i = 0; i < ret.get_dimension(); ++i) {
357 ret.access_data().get_data()[i] = std::min(ret[i], it->first[i]);
367 for (
typename Data::const_iterator it = data_.begin(); it != data_.end();
369 for (
unsigned int i = 0; i < ret.get_dimension(); ++i) {
370 ret.access_data().get_data()[i] = std::min(ret[i], it->first[i]);
376 IMPALGEBRA_END_NAMESPACE
ParticleIndexes get_indexes(const ParticlesTemp &ps)
ExtendedGridIndexD< D > get_maximum_extended_index() const
Return the index that has no higher index in each coordinate.
A class to represent a voxel grid.
Represent a real cell in a grid (one within the bounding box)
An index in an infinite grid on space.
#define IMP_UNUSED(variable)
#define IMP_SHOWABLE_INLINE(Name, how_to_show)
Declare the methods needed by an object that can be printed.
Various general useful macros for IMP.
#define IMP_COPY_CONSTRUCTOR(Name, Base)
Use a copy_from method to create a copy constructor and operator=.
#define IMP_USAGE_CHECK(expr, message)
A runtime test for incorrect usage of a class or method.
bool get_has_index(const ExtendedGridIndexD< D > &i) const
Return true if the voxel has been added.
#define IMP_INTERNAL_CHECK(expr, message)
An assertion to check for internal errors in IMP. An IMP::ErrorException will be thrown.
A bounding box in D dimensions.
GridIndexD< D > get_index(const ExtendedGridIndexD< D > &i) const
requires get_has_index(i) is true.
A class for storing lists of IMP items.
#define IMP_IF_CHECK(level)
Execute the code block if a certain level checks are on.
A class to represent a voxel grid.
#define IMP_FAILURE(message)
A runtime failure for IMP.
ExtendedGridIndexD< D > get_minimum_extended_index() const
Return the index which has no lower index in each coordinate.
#define IMP_BRACKET(Value, Index, bounds_check_expr, expr)
GridIndexD< D > add_voxel(const ExtendedGridIndexD< D > &i, const VT &gi)
Add a voxel to the storage, this voxel will now have a GridIndex3D.
Declare an efficient stl-compatible map.