8 #ifndef IMPALGEBRA_GRID_D_H
9 #define IMPALGEBRA_GRID_D_H
11 #include <IMP/algebra/algebra_config.h>
19 #include <boost/iterator/transform_iterator.hpp>
25 IMPALGEBRA_BEGIN_NAMESPACE
79 class EmbeddingT=DefaultEmbeddingD<D> >
80 class GridD:
public Storage,
public EmbeddingT,
89 GetVoxel(
This *home): home_(home) {}
90 typedef Value& result_type;
92 result_type operator()(argument_type i)
const {
94 return home_->operator[](i);
98 struct ConstGetVoxel {
100 ConstGetVoxel(
const This *home): home_(home) {}
101 typedef const Value& result_type;
103 result_type operator()(argument_type i)
const {
105 return home_->operator[](i);
111 Floats ret(bb.get_dimension());
112 for (
unsigned int i=0; i< ret.size(); ++i) {
118 Ints get_ns(
const NS &ds,
121 for (
unsigned int i=0; i< ds.size(); ++i ) {
125 double d= bside/ds[i];
126 double cd= std::ceil(d);
127 dd[i]= std::max<int>(1,
static_cast<int>(cd));
133 typedef EmbeddingT Embedding;
139 Value default_value=Value()):
140 Storage(counts, default_value),
141 Embedding(bb.get_corner(0), get_sides(counts, bb)) {
149 const Value& default_value=Value()):
150 Storage(get_ns(
Floats(bb.get_dimension(), side), bb), default_value),
154 "This grid constructor can only be used with bounded grids.");
157 GridD(
const Storage&storage,
const Embedding &embed): Storage(storage),
164 const Value& default_value= Value()):
165 Storage(default_value),
Embedding(origin,
178 Storage::get_has_index(Embedding::get_extended_index(i)),
179 return Storage::operator[](
get_index(Embedding
180 ::get_extended_index(i))));
187 using Storage::__getitem__;
188 using Storage::__setitem__;
189 using Storage::operator[];
194 "add_voxel() only works on sparse grids.");
196 return Storage::add_voxel(ei, vt);
198 #if !defined(IMP_DOXYGEN) && !defined(SWIG)
199 Value &get_voxel_always(
const VectorD<D>& pt) {
200 ExtendedGridIndexD<D> ei= Embedding::get_extended_index(pt);
201 return Storage::get_voxel_always(ei);
204 get_value_always(
const VectorD<D>& pt)
const {
205 ExtendedGridIndexD<D> ei= Embedding::get_extended_index(pt);
206 return Storage::get_value_always(ei);
210 using Storage::get_has_index;
212 using Storage::add_voxel;
214 bool get_has_index(
const ExtendedGridIndexD<D>&i)
const;
215 GridIndexD<D>
get_index(
const ExtendedGridIndexD<D> &i)
const;
216 GridIndexD<D> add_voxel(
const ExtendedGridIndexD<D> &i,
224 using Embedding::get_extended_index;
232 return get_bounding_box(min)+get_bounding_box(max);
235 using Embedding::get_bounding_box;
237 BoundingBoxD<D> get_bounding_box(
const ExtendedGridIndexD<D> &i)
const;
238 BoundingBoxD<D> get_bounding_box(
const GridIndexD<D> &i)
const;
246 Floats nuc(bb3.get_dimension());
247 for (
unsigned int i=0; i< bb3.get_dimension(); ++i) {
250 nuc[i]= side/Storage::get_number_of_voxels(i);
252 Embedding::set_unit_cell(
VectorD<D>(nuc.begin(), nuc.end()));
265 <<
"only works on dense grids.");
269 ExtendedGridIndexD<D>
270 get_nearest_extended_index(
const VectorD<D>& pt)
const {
272 <<
"only works on bounded grids.");
273 ExtendedGridIndexD<D> ei= Embedding::get_extended_index(pt);
274 boost::scoped_array<int> is(
new int[pt.get_dimension()]);
275 for (
unsigned int i=0; i< pt.get_dimension(); ++i) {
276 is[i]= std::max(0, ei[i]);
277 is[i]= std::min<int>(Storage::get_number_of_voxels(i)-1, is[i]);
279 return ExtendedGridIndexD<D>(is.get(), is.get()+pt.get_dimension());
293 class VoxelConstIterator;
295 typedef boost::transform_iterator<GetVoxel, typename Storage::IndexIterator>
297 typedef boost::transform_iterator<ConstGetVoxel,
298 typename Storage::IndexIterator>
301 VoxelIterator voxels_begin(
const BoundingBoxD<D> &bb) {
302 return VoxelIterator(indexes_begin(bb), GetVoxel(
this));
304 VoxelIterator voxels_end(
const BoundingBoxD<D> &bb) {
307 return VoxelIterator(indexes_end(bb),
311 VoxelConstIterator voxels_begin(
const BoundingBoxD<D> &bb)
const {
312 return VoxelConstIterator(indexes_begin(bb),
313 ConstGetVoxel(
this));
315 VoxelConstIterator voxels_end(
const BoundingBoxD<D> &bb)
const {
316 return VoxelConstIterator(indexes_end(bb),
317 ConstGetVoxel(
this));
319 using Storage::indexes_begin;
320 using Storage::indexes_end;
321 typename Storage::IndexIterator
322 indexes_begin(
const BoundingBoxD<D> &bb)
const {
323 ExtendedGridIndexD<3> lb= get_extended_index(bb.get_corner(0));
324 ExtendedGridIndexD<3> ub= get_extended_index(bb.get_corner(1));
325 return Storage::indexes_begin(lb, ub);
327 typename Storage::IndexIterator indexes_end(
const BoundingBoxD<D> &)
const {
330 return Storage::indexes_end(ExtendedGridIndexD<3>(),
331 ExtendedGridIndexD<3>());
342 template <
class Functor>
343 Functor apply(
const Functor &f)
const {
344 return Storage::apply(*
this, f);
357 GridD<D, Storage, Value,
359 return g.get_bounding_box();
362 IMPALGEBRA_END_NAMESPACE