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>
21#include <boost/unordered_map.hpp>
26IMPALGEBRA_BEGIN_NAMESPACE
37template <
int D,
class VT>
38class DenseGridStorageD :
public BoundedGridRangeD<D> {
39 typedef boost::scoped_array<VT> Data;
45 unsigned int index(
const I &i)
const {
48 for (
int d =
static_cast<int>(i.get_dimension()) - 1; d >= 0; --d) {
49 unsigned int cur = i[d];
50 for (
int ld = d - 1; ld >= 0; --ld) {
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 {
return extent_; }
79 void copy_from(
const DenseGridStorageD &o) {
80 default_ = o.default_;
82 data_.reset(
new VT[extent_]);
83 std::copy(o.data_.get(), o.data_.get() + o.extent_, data_.get());
84 BoundedGridRangeD<D>::operator=(o);
86 void set_number_of_voxels(
Ints dims) {
88 for (
unsigned int i = 0; i < dims.size(); ++i) {
91 data_.reset(
new VT[extent_]);
92 std::fill(data_.get(), data_.get() + get_extent(), default_);
99 DenseGridStorageD(
const Ints &counts,
const VT &default_value = VT())
100 : BoundedGridRangeD<D>(counts), default_(default_value) {
101 set_number_of_voxels(counts);
110 IMP_BRACKET(VT,
unsigned int, i < extent_,
return data_[i]);
114 DenseGridStorageD(
const VT &default_value = VT())
115 : extent_(0), default_(default_value) {}
116 static bool get_is_dense() {
return true; }
120 const VT *get_raw_data()
const {
return data_.get(); }
121 VT *get_raw_data() {
return data_.get(); }
129#if !defined(IMP_DOXYGEN) && !defined(SWIG)
132 return operator[](gi);
136 return operator[](gi);
145 typedef VT *AllVoxelIterator;
146 typedef const VT *AllVoxelConstIterator;
147 AllVoxelIterator all_voxels_begin() {
return data_.get(); }
148 AllVoxelIterator all_voxels_end() {
return data_.get() + get_extent(); }
149 AllVoxelConstIterator all_voxels_begin()
const {
return data_.get(); }
150 AllVoxelConstIterator all_voxels_end()
const {
151 return data_.get() + get_extent();
155 return Vector<VT>(all_voxels_begin(), all_voxels_end());
158 template <
class Functor,
class Gr
id>
159 Functor apply(
const Grid &g,
const Functor &fi)
const {
161 typename Grid::ExtendedIndex lb(
typename Grid::ExtendedIndex::Filled(),
162 g.get_dimension(), 0);
164 typename Grid::Vector corner = g.get_bounding_box().get_corner_0();
165 typename Grid::Vector cell = g.get_unit_cell();
169 typename Grid::Index cur(
typename Grid::Index::Uninitialized(),
171 typename Grid::Vector center = corner;
172 internal::GridApplier<Functor, Grid, D - 1>::apply(g, lb, ub, corner, cell,
186template <
int D,
class VT,
class Base,
187 class Map =
typename boost::unordered_map<GridIndexD<D>, VT>>
188class SparseGridStorageD :
public Base {
192 typedef typename Data::const_iterator::value_type argument_type;
199 const SparseGridStorageD<D, VT, Base> *stor_;
200 ItHelper(
const SparseGridStorageD<D, VT, Base> *stor) : stor_(stor) {}
210 ItHelper() : stor_(
nullptr) {}
218 SparseGridStorageD(
const Ints &counts,
const VT &default_value)
219 : Base(counts), default_(default_value) {}
221 << data_.size() <<
" cells set");
229 void remove_voxel(
const GridIndexD<D> &gi) { data_.erase(gi); }
230#if !defined(SWIG) && !defined(IMP_DOXYGEN)
231 SparseGridStorageD(
const VT &def) : default_(def) {}
232 static bool get_is_dense() {
return false; }
233 using Base::get_number_of_voxels;
235 unsigned int get_number_of_voxels()
const {
return data_.size(); }
238 return data_.find(
GridIndexD<D>(i.begin(), i.end())) != data_.end();
246#if !defined(IMP_DOXYGEN) && !defined(SWIG)
249 typename Map::iterator it = data_.find(gi);
250 if (it == data_.end()) {
251 return data_.insert(std::make_pair(gi, default_)).first->second;
256 const VT &get_value_always(
const ExtendedGridIndexD<D> &i)
const {
257 GridIndexD<D> gi(i.begin(), i.end());
258 typename Map::const_iterator it = data_.find(gi);
259 if (it == data_.end()) {
275 return data_.find(i)->second);
284 typedef typename Data::const_iterator AllConstIterator;
285 AllConstIterator all_begin()
const {
return data_.begin(); }
286 AllConstIterator all_end()
const {
return data_.end(); }
287 typedef boost::transform_iterator<GetIndex, AllConstIterator>
289 AllIndexIterator all_indexes_begin()
const {
290 return boost::make_transform_iterator(all_begin(), GetIndex());
292 AllIndexIterator all_indexes_end()
const {
293 return boost::make_transform_iterator(all_end(), GetIndex());
317 typedef internal::GridIndexIterator<ExtendedGridIndexD<D>, ItHelper>
323 IndexIterator indexes_begin(
const ExtendedGridIndexD<D> &lb,
324 const ExtendedGridIndexD<D> &ub)
const {
326 ExtendedGridIndexD<D> eub = ub.get_uniform_offset(1);
327 if (!internal::get_is_non_empty(lb, eub)) {
328 return IndexIterator();
330 return IndexIterator(lb, eub, ItHelper(
this));
333 IndexIterator indexes_end(
const ExtendedGridIndexD<D> &,
334 const ExtendedGridIndexD<D> &)
const {
336 return IndexIterator();
341 const ExtendedGridIndexD<D> &lb,
const ExtendedGridIndexD<D> &ub)
const {
342 return Vector<GridIndexD<D>>(indexes_begin(lb, ub),
343 indexes_end(lb, ub));
347 template <
class Functor,
class Gr
id>
348 Functor apply(
const Grid &g, Functor f)
const {
349 for (
const auto &it : data_) {
350 f(g, it.first, g.get_center(it.first));
356 ExtendedGridIndexD<D> get_minimum_extended_index()
const {
358 GridIndexD<D> reti = data_.begin()->first;
359 ExtendedGridIndexD<D> ret(reti.begin(), reti.end());
360 for (
const auto &it : data_) {
361 for (
unsigned int i = 0; i < ret.get_dimension(); ++i) {
362 ret.access_data().get_data()[i] = std::min(ret[i], it.first[i]);
368 ExtendedGridIndexD<D> get_maximum_extended_index()
const {
370 GridIndexD<D> reti = data_.begin()->first;
371 ExtendedGridIndexD<D> ret(reti.begin(), reti.end());
372 for (
const auto &it : data_) {
373 for (
unsigned int i = 0; i < ret.get_dimension(); ++i) {
374 ret.access_data().get_data()[i] = std::max(ret[i], it.first[i]);
381IMPALGEBRA_END_NAMESPACE
A bounding box in D dimensions.
A class for storing lists of IMP items.
Macros to handle array indexing.
#define IMP_BRACKET(Value, Index, bounds_check_expr, expr)
Implement operator[] and at() for C++, and getitem for Python.
#define IMP_IF_CHECK(level)
Execute the code block if a certain level checks are on.
#define IMP_INTERNAL_CHECK(expr, message)
An assertion to check for internal errors in IMP. An IMP::ErrorException will be thrown.
#define IMP_FAILURE(message)
A runtime failure for IMP.
#define IMP_USAGE_CHECK(expr, message)
A runtime test for incorrect usage of a class or method.
A more IMP-like version of the std::vector.
ExtendedGridIndexD< D > get_end_index() const
Get the past-end voxel.
unsigned int get_number_of_voxels(unsigned int i) const
Return the number of voxels in a certain direction.
An index in an infinite grid on space.
Represent a real cell in a grid (one within the bounding box).
GridIndexD< D > add_voxel(const ExtendedGridIndexD< D > &i, const VT &gi)
Add a voxel to the storage, this voxel will now have a GridIndex3D.
GridIndexD< D > get_index(const ExtendedGridIndexD< D > &i) const
requires get_has_index(i) is true.
bool get_has_index(const ExtendedGridIndexD< D > &i) const
Return true if the voxel has been added.
A class to represent a voxel grid.
A class to represent a voxel grid.
IMP::Vector< Int > Ints
Standard way to pass a bunch of Int values.
ParticleIndexes get_indexes(const ParticlesTemp &ps)
Get the indexes from a list of particles.
#define IMP_SHOWABLE_INLINE(Name, how_to_show)
Declare the methods needed by an object that can be printed.
#define IMP_COPY_CONSTRUCTOR(Name, Base)
Use a copy_from method to create a copy constructor and operator=.
#define IMP_UNUSED(variable)