IMP logo
IMP Reference Guide  develop.7482b7c7af, 2026/12/05
The Integrative Modeling Platform
grid_storages.h
Go to the documentation of this file.
1/**
2 * \file IMP/algebra/grid_storages.h
3 * \brief Dense and sparse storage of the voxel data in a grid.
4 *
5 * Copyright 2007-2026 IMP Inventors. All rights reserved.
6 *
7 */
8
9#ifndef IMPALGEBRA_GRID_STORAGES_H
10#define IMPALGEBRA_GRID_STORAGES_H
11
12#include <IMP/algebra/algebra_config.h>
13#include <IMP/bracket_macros.h>
14#include <IMP/types.h>
15#include "internal/grid_apply.h"
16#include "grid_indexes.h"
17#include "grid_ranges.h"
18#include "Vector3D.h"
19#include "BoundingBoxD.h"
20#include <boost/iterator/transform_iterator.hpp>
21#include <boost/unordered_map.hpp>
22#include <IMP/Vector.h>
23
24#include <limits>
25
26IMPALGEBRA_BEGIN_NAMESPACE
27
28/** Store a grid as a densely packed set of voxels.
29 The mapping from GridIndex3D to index in the data array is:
30 \code
31 i[2]*BoundedGridRangeD::get_number_of_voxels(0)
32 *BoundedGridRange3D::get_number_of_voxels(1)
33 + i[1]*BoundedGridRange3D::get_number_of_voxels(0)+i[0]
34 \endcode
35 \see Grid3D
36*/
37template <int D, class VT>
38class DenseGridStorageD : public BoundedGridRangeD<D> {
39 typedef boost::scoped_array<VT> Data;
40 Data data_;
41 unsigned int extent_;
42 VT default_;
43
44 template <class I>
45 unsigned int index(const I &i) const {
46 unsigned int ii = 0;
47 // Use the index's dimension, as D is -1 for runtime-dimension grids
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) {
52 }
53 ii += cur;
54 }
56 if (D == 3) {
57 unsigned int check =
61 IMP_UNUSED(check);
62 IMP_USAGE_CHECK(check == ii, "Wrong value returned");
63 }
64 }
65 IMP_INTERNAL_CHECK(ii < get_extent(), "Invalid grid index " << i);
66 return ii;
67 }
68#ifndef SWIG
69 struct NonDefault {
70 VT default_;
71 NonDefault(const VT &def) : default_(def) {}
72 template <class P>
73 bool operator()(const P &def) const {
74 return def.second != default_;
75 }
76 };
77#endif
78 unsigned int get_extent() const { return extent_; }
79 void copy_from(const DenseGridStorageD &o) {
80 default_ = o.default_;
81 extent_ = o.extent_;
82 data_.reset(new VT[extent_]);
83 std::copy(o.data_.get(), o.data_.get() + o.extent_, data_.get());
84 BoundedGridRangeD<D>::operator=(o);
85 }
86 void set_number_of_voxels(Ints dims) {
87 extent_ = 1;
88 for (unsigned int i = 0; i < dims.size(); ++i) {
89 extent_ *= dims[i];
90 }
91 data_.reset(new VT[extent_]);
92 std::fill(data_.get(), data_.get() + get_extent(), default_);
93 // BoundedGridRangeD<D>::set_number_of_voxels(dims);
94 }
95
96 public:
97 IMP_COPY_CONSTRUCTOR(DenseGridStorageD, BoundedGridRangeD<D>);
98 typedef VT Value;
99 DenseGridStorageD(const Ints &counts, const VT &default_value = VT())
100 : BoundedGridRangeD<D>(counts), default_(default_value) {
101 set_number_of_voxels(counts);
102 }
103 IMP_BRACKET(VT, GridIndexD<D>, true, return data_[index(i)]);
104 /** \name Direct indexing
105 One can directly access a particular voxel based on its index
106 in the array of all voxels. This can be faster than using a
107 GridIndexD.
108 @{
109 */
110 IMP_BRACKET(VT, unsigned int, i < extent_, return data_[i]);
111/** @}
112 */
113#ifndef IMP_DOXYGEN
114 DenseGridStorageD(const VT &default_value = VT())
115 : extent_(0), default_(default_value) {}
116 static bool get_is_dense() { return true; }
117#endif
118 IMP_SHOWABLE_INLINE(DenseGridStorageD, IMP_UNUSED(out););
119#ifndef SWIG
120 const VT *get_raw_data() const { return data_.get(); }
121 VT *get_raw_data() { return data_.get(); }
122#endif
123
124#ifndef IMP_DOXYGEN
125 GridIndexD<D> add_voxel(const ExtendedGridIndexD<D> &, const VT &) {
126 IMP_FAILURE("Cannot add voxels to dense grid");
127 }
128#endif
129#if !defined(IMP_DOXYGEN) && !defined(SWIG)
130 VT &get_voxel_always(const ExtendedGridIndexD<D> &i) {
131 GridIndexD<D> gi(i.begin(), i.end());
132 return operator[](gi);
133 }
134 const VT &get_value_always(const ExtendedGridIndexD<D> &i) const {
135 GridIndexD<D> gi(i.begin(), i.end());
136 return operator[](gi);
137 }
138#endif
139
140/** \name All voxel iterators
141 The value type is VT.
142 @{
143*/
144#ifndef SWIG
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();
152 }
153#endif
154 Vector<VT> get_all_voxels() const {
155 return Vector<VT>(all_voxels_begin(), all_voxels_end());
156 }
157 /** @} */
158 template <class Functor, class Grid>
159 Functor apply(const Grid &g, const Functor &fi) const {
160 Functor f = fi;
161 typename Grid::ExtendedIndex lb(typename Grid::ExtendedIndex::Filled(),
162 g.get_dimension(), 0);
163 typename Grid::ExtendedIndex ub(BoundedGridRangeD<D>::get_end_index());
164 typename Grid::Vector corner = g.get_bounding_box().get_corner_0();
165 typename Grid::Vector cell = g.get_unit_cell();
166 // Size the index and center to the grid's dimension (this matters
167 // for runtime-dimension grids, where default construction gives
168 // zero-length storage)
169 typename Grid::Index cur(typename Grid::Index::Uninitialized(),
170 g.get_dimension());
171 typename Grid::Vector center = corner;
172 internal::GridApplier<Functor, Grid, D - 1>::apply(g, lb, ub, corner, cell,
173 cur, center, f);
174 return f;
175 }
176};
177
178/** Store a grid as a sparse set of voxels (only the voxels which have
179 been added are actually stored). The
180 get_has_index() functions allow one to tell if a voxel has been added.
181 \unstable{SparseGridStorageD}
182
183 Base should be one of BoundedGridRangeD or UnboundedGridRangeD.
184 \see Grid3D
185*/
186template <int D, class VT, class Base,
187 class Map = typename boost::unordered_map<GridIndexD<D>, VT>>
188class SparseGridStorageD : public Base {
189 typedef Map Data;
190 struct GetIndex {
191 typedef GridIndexD<D> result_type;
192 typedef typename Data::const_iterator::value_type argument_type;
193 template <class T>
194 GridIndexD<D> operator()(const T &t) const {
195 return t.first;
196 }
197 };
198 struct ItHelper {
199 const SparseGridStorageD<D, VT, Base> *stor_;
200 ItHelper(const SparseGridStorageD<D, VT, Base> *stor) : stor_(stor) {}
201 bool get_is_good(const ExtendedGridIndexD<D> &ei) {
202 /*std::cout << "Checking " << ei << " getting "
203 << stor_->get_has_index(ei) << std::endl;*/
204 return stor_->get_has_index(ei);
205 }
206 typedef GridIndexD<D> ReturnType;
207 ReturnType get_return(const ExtendedGridIndexD<D> &ei) const {
208 return stor_->get_index(ei);
209 }
210 ItHelper() : stor_(nullptr) {}
211 };
212
213 Data data_;
214 VT default_;
215
216 public:
217 typedef VT Value;
218 SparseGridStorageD(const Ints &counts, const VT &default_value)
219 : Base(counts), default_(default_value) {}
220 IMP_SHOWABLE_INLINE(SparseGridStorage3D, out << "Sparse grid with "
221 << data_.size() << " cells set");
222 //! Add a voxel to the storage, this voxel will now have a GridIndex3D
224 IMP_USAGE_CHECK(Base::get_has_index(i), "Out of grid domain " << i);
225 GridIndexD<D> ret(i.begin(), i.end());
226 data_[ret] = gi;
227 return ret;
228 }
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;
234#endif
235 unsigned int get_number_of_voxels() const { return data_.size(); }
236 //! Return true if the voxel has been added
238 return data_.find(GridIndexD<D>(i.begin(), i.end())) != data_.end();
239 }
240 //! requires get_has_index(i) is true.
242 IMP_USAGE_CHECK(get_has_index(i), "Index is not a valid "
243 << "voxel " << i);
244 return GridIndexD<D>(i.begin(), i.end());
245 }
246#if !defined(IMP_DOXYGEN) && !defined(SWIG)
247 VT &get_voxel_always(const ExtendedGridIndexD<D> &i) {
248 GridIndexD<D> gi(i.begin(), i.end());
249 typename Map::iterator it = data_.find(gi);
250 if (it == data_.end()) {
251 return data_.insert(std::make_pair(gi, default_)).first->second;
252 } else {
253 return it->second;
254 }
255 }
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()) {
260 return default_;
261 } else {
262 return it->second;
263 }
264 }
265#endif
266 /** \name Operator []
267 Operator[] isn't very useful at the moment as it can only
268 be used with a cell which has already been set. This
269 behavior/the existence of these functions is likely to change.
270 @{
271 */
272 IMP_BRACKET(VT, GridIndexD<D>, true,
273 IMP_USAGE_CHECK(data_.find(i) != data_.end(), "Invalid index "
274 << i);
275 return data_.find(i)->second);
276/** @} */
277
278/** \name Iterators through set cells
279 Iterate through the voxels which have been set. The value
280 type is a pair of GridIndex3D and VT.
281 @{
282*/
283#ifndef SWIG
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>
288 AllIndexIterator;
289 AllIndexIterator all_indexes_begin() const {
290 return boost::make_transform_iterator(all_begin(), GetIndex());
291 }
292 AllIndexIterator all_indexes_end() const {
293 return boost::make_transform_iterator(all_end(), GetIndex());
294 }
295#endif
296/** @} */
297
298/** \name Index Iterators
299
300 Iterate through a range of actual indexes. The value
301 type for the iterator is an GridIndex3D.
302
303 The range is defined by a pair of indexes. It includes
304 all indexes in the axis aligned box defined by lb
305 as the lower corner and the second as the ub. That is, if
306 lb is \f$(l_x, l_y, l_z)\f$ and ub is \f$(u_x, u_y, u_z)\f$,
307 then the range includes all
308 indexes \f$(i_x, i_y, i_z)\f$ such that \f$l_x \leq i_x \leq u_x\f$,
309 \f$l_y \leq i_y \leq u_y\f$
310 and \f$l_z \leq i_z \leq u_z\f$.
311
312 @{
313 */
314#ifndef SWIG
315#ifndef IMP_DOXYGEN
316
317 typedef internal::GridIndexIterator<ExtendedGridIndexD<D>, ItHelper>
318 IndexIterator;
319
320#else
321 class IndexIterator;
322#endif
323 IndexIterator indexes_begin(const ExtendedGridIndexD<D> &lb,
324 const ExtendedGridIndexD<D> &ub) const {
325 // ub is inclusive, so lb == ub is a range containing one voxel
326 ExtendedGridIndexD<D> eub = ub.get_uniform_offset(1);
327 if (!internal::get_is_non_empty(lb, eub)) {
328 return IndexIterator();
329 } else {
330 return IndexIterator(lb, eub, ItHelper(this));
331 }
332 }
333 IndexIterator indexes_end(const ExtendedGridIndexD<D> &,
334 const ExtendedGridIndexD<D> &) const {
335 // IMP_INTERNAL_CHECK(lb <= ub, "empty range");
336 return IndexIterator();
337 }
338#endif
339
340 Vector<GridIndexD<D>> get_indexes(
341 const ExtendedGridIndexD<D> &lb, const ExtendedGridIndexD<D> &ub) const {
342 return Vector<GridIndexD<D>>(indexes_begin(lb, ub),
343 indexes_end(lb, ub));
344 }
345 /** @} */
346
347 template <class Functor, class Grid>
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));
351 }
352 return f;
353 }
354#ifndef IMP_DOXYGEN
355 //! Return the index which has no lower index in each coordinate
356 ExtendedGridIndexD<D> get_minimum_extended_index() const {
357 IMP_USAGE_CHECK(!data_.empty(), "No voxels in grid.");
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]);
363 }
364 }
365 return ret;
366 }
367 //! Return the index that has no higher index in each coordinate
368 ExtendedGridIndexD<D> get_maximum_extended_index() const {
369 IMP_USAGE_CHECK(!data_.empty(), "No voxels in grid.");
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]);
375 }
376 }
377 return ret;
378 }
379#endif
380};
381IMPALGEBRA_END_NAMESPACE
382
383#endif /* IMPALGEBRA_GRID_STORAGES_H */
A bounding box in D dimensions.
A class for storing lists of IMP items.
Simple 3D vector class.
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.
Definition Vector.h:60
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.
@ USAGE
Definition enums.h:57
IMP::Vector< Int > Ints
Standard way to pass a bunch of Int values.
Definition types.h:48
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.
Basic types used by IMP.
#define IMP_COPY_CONSTRUCTOR(Name, Base)
Use a copy_from method to create a copy constructor and operator=.
#define IMP_UNUSED(variable)