IMP  2.1.0
The Integrative Modeling Platform
grid_indexes.h
Go to the documentation of this file.
1 /**
2  * \file IMP/algebra/grid_indexes.h \brief A class to represent a voxel grid.
3  *
4  * Copyright 2007-2013 IMP Inventors. All rights reserved.
5  *
6  */
7 
8 #ifndef IMPALGEBRA_GRID_INDEXES_H
9 #define IMPALGEBRA_GRID_INDEXES_H
10 
11 #include <IMP/algebra/algebra_config.h>
13 #include "internal/vector.h"
14 #include "internal/grid_internal.h"
15 #include <IMP/base/types.h>
16 #include <IMP/base/Value.h>
17 #include <IMP/base/exception.h>
18 
19 IMPALGEBRA_BEGIN_NAMESPACE
20 
21 //! An index in an infinite grid on space
22 /* The index entries can be positive or negative and do not need to correspond
23  directly to cells in the grid. They get mapped on to actual grid cells
24  by various functions.
25  \see Grid3D
26 */
27 template <int D>
29  internal::VectorData<int, D, true> data_;
30  int compare(const ExtendedGridIndexD<D>& o) const {
31  if (D == -1) {
32  if (data_.get_dimension() == 0 && o.data_.get_dimension() == 0) {
33  return 0;
34  } else if (data_.get_dimension() == 0) {
35  return -1;
36  } else if (o.data_.get_dimension() == 0) {
37  return 1;
38  }
39  } else {
40  IMP_USAGE_CHECK(get_dimension() == o.get_dimension(),
41  "Dimensions don't match");
42  }
43  return internal::lexicographical_compare(begin(), end(), o.begin(),
44  o.end());
45  }
46 
47  public:
48 #if !defined(IMP_DOXYGEN) && !defined(SWIG)
49  struct Uninitialized {};
50  ExtendedGridIndexD(Uninitialized, int dim) : data_(dim) {}
51  struct Filled {};
52  ExtendedGridIndexD(Filled, int dim, int value) : data_(dim) {
53  std::fill(data_.get_data(), data_.get_data() + dim, value);
54  }
55  const internal::VectorData<int, D, true>& get_data() const { return data_; }
56  internal::VectorData<int, D, true>& access_data() { return data_; }
57 #endif
58 //! Create a grid cell from three arbitrary indexes
59 /** \note Only use this from python. */
60 #ifndef SWIG
61  IMP_DEPRECATED_ATTRIBUTE
62 #endif
63  explicit ExtendedGridIndexD(Ints vals) {
64  data_.set_coordinates(vals.begin(), vals.end());
65  }
66 #ifndef SWIG
67  template <class It>
68  ExtendedGridIndexD(It b, It e) {
69  data_.set_coordinates(b, e);
70  }
71 #endif
72  ExtendedGridIndexD(int x, int y, int z) {
73  IMP_USAGE_CHECK(D == 3, "Can only use explicit constructor in 3D");
74  int v[] = {x, y, z};
75  data_.set_coordinates(v, v + 3);
76  }
77  ExtendedGridIndexD() {}
78  unsigned int get_dimension() const { return data_.get_dimension(); }
79  IMP_COMPARISONS(ExtendedGridIndexD);
80  //! Get the ith component (i=0,1,2)
82  int, unsigned int, i < get_dimension(),
83  IMP_USAGE_CHECK(!data_.get_is_null(), "Using uninitialized grid index");
84  return data_.get_data()[i]);
85  IMP_SHOWABLE_INLINE(ExtendedGridIndexD, {
86  out << "(";
87  for (unsigned int i = 0; i < get_dimension(); ++i) {
88  out << operator[](i);
89  if (i != get_dimension() - 1)
90  out << ", ";
91  }
92  out << ")";
93  });
94 
95 #ifndef SWIG
96  typedef const int* iterator;
97  iterator begin() const { return data_.get_data(); }
98  iterator end() const { return data_.get_data() + get_dimension(); }
99 #endif
100 #ifndef IMP_DOXYGEN
101  unsigned int __len__() const { return get_dimension(); }
102 #endif
103  IMP_HASHABLE_INLINE(ExtendedGridIndexD,
104  return boost::hash_range(begin(), end()));
105  ExtendedGridIndexD<D> get_uniform_offset(int ii) const {
106  ExtendedGridIndexD<D> ret(typename ExtendedGridIndexD<D>::Filled(),
107  get_dimension(), 0);
108  for (unsigned int i = 0; i < get_dimension(); ++i) {
109  ret.access_data().get_data()[i] = operator[](i) + ii;
110  }
111  // std::cout << "Offset " << *this << " to get " << ret << std::endl;
112  return ret;
113  }
114  ExtendedGridIndexD<D> get_offset(int i, int j, int k) const {
115  IMP_USAGE_CHECK(D == 3, "Only for 3D");
116  int v[] = {operator[](0) + i, operator[](1) + j, operator[](2) + k};
117  ExtendedGridIndexD<D> ret(v, v + 3);
118  return ret;
119  }
120 };
121 
122 #if !defined(SWIG) && !defined(IMP_DOXYGEN)
123 template <int D>
124 inline std::size_t hash_value(const ExtendedGridIndexD<D>& ind) {
125  return ind.__hash__();
126 }
127 #endif
128 
129 //! Represent a real cell in a grid (one within the bounding box)
130 /* These indexes represent an actual cell in the grid.
131  They can only be constructed by the grid (since only it knows what
132  are the actual cells).
133  \see Grid3D
134 */
135 template <int D>
136 class GridIndexD : public base::Value {
137  internal::VectorData<int, D, true> data_;
138  int compare(const GridIndexD<D>& o) const {
139  if (D == -1) {
140  if (data_.get_dimension() == 0 && o.data_.get_dimension() == 0) {
141  return 0;
142  } else if (data_.get_dimension() == 0) {
143  return -1;
144  } else if (o.data_.get_dimension() == 0) {
145  return 1;
146  }
147  } else {
148  IMP_USAGE_CHECK(get_dimension() == o.get_dimension(),
149  "Dimensions don't match");
150  }
151  return internal::lexicographical_compare(begin(), end(), o.begin(),
152  o.end());
153  }
154 
155  public:
156 #if !defined(IMP_DOXYGEN) && !defined(SWIG)
157  const internal::VectorData<int, D, true>& get_data() const { return data_; }
158  internal::VectorData<int, D, true>& access_data() { return data_; }
159  struct Uninitialized {};
160  GridIndexD(Uninitialized, int dim) : data_(dim) {}
161  struct Filled {};
162  GridIndexD(Filled, int dim, int value) : data_(dim) {
163  std::fill(data_.get_data(), data_.get_data() + dim, value);
164  }
165 #endif
166  GridIndexD() {}
167 
168  unsigned int get_dimension() const { return data_.get_dimension(); }
169 
170 #ifndef IMP_DOXYGEN
171  //! Get the ith component (i=0,1,2)
173  int, unsigned int, i < get_dimension(),
174  IMP_USAGE_CHECK(!data_.get_is_null(), "Using uninitialized grid index");
175  return data_.get_data()[i]);
177  out << "(";
178  for (unsigned int i = 0; i < get_dimension(); ++i) {
179  out << operator[](i);
180  if (i != get_dimension() - 1)
181  out << ", ";
182  }
183  out << ")";
184  });
185 #ifndef SWIG
186  typedef const int* iterator;
187  iterator begin() const { return data_.get_data(); }
188  iterator end() const { return data_.get_data() + get_dimension(); }
189 /** \note Only use this from python. */
190 #ifndef SWIG
191  IMP_DEPRECATED_ATTRIBUTE
192 #endif
193  explicit GridIndexD(Ints vals) {
194  data_.set_coordinates(vals.begin(), vals.end());
195  }
196  template <class It>
197  GridIndexD(It b, It e) {
198  data_.set_coordinates(b, e);
199  }
200 #endif
201  unsigned int __len__() const { return get_dimension(); }
202 #endif
204  IMP_HASHABLE_INLINE(GridIndexD, return boost::hash_range(begin(), end()));
205 };
206 
207 #if !defined(SWIG) && !defined(IMP_DOXYGEN)
208 template <int D>
209 inline std::size_t hash_value(const GridIndexD<D>& ind) {
210  return ind.__hash__();
211 }
212 #endif
213 
214 #if !defined(IMP_DOXYGEN)
215 typedef GridIndexD<1> GridIndex1D;
216 typedef ExtendedGridIndexD<1> ExtendedGridIndex1D;
217 typedef base::Vector<GridIndex1D> GridIndex1Ds;
218 typedef base::Vector<ExtendedGridIndex1D> ExtendedGridIndex1Ds;
219 
220 typedef GridIndexD<2> GridIndex2D;
221 typedef ExtendedGridIndexD<2> ExtendedGridIndex2D;
222 typedef base::Vector<GridIndex2D> GridIndex2Ds;
223 typedef base::Vector<ExtendedGridIndex2D> ExtendedGridIndex2Ds;
224 
225 typedef GridIndexD<3> GridIndex3D;
226 typedef ExtendedGridIndexD<3> ExtendedGridIndex3D;
227 typedef base::Vector<GridIndex3D> GridIndex3Ds;
228 typedef base::Vector<ExtendedGridIndex3D> ExtendedGridIndex3Ds;
229 
230 typedef GridIndexD<4> GridIndex4D;
231 typedef ExtendedGridIndexD<4> ExtendedGridIndex4D;
232 typedef base::Vector<GridIndex4D> GridIndex4Ds;
233 typedef base::Vector<ExtendedGridIndex4D> ExtendedGridIndex4Ds;
234 
235 typedef GridIndexD<5> GridIndex5D;
236 typedef ExtendedGridIndexD<5> ExtendedGridIndex5D;
237 typedef base::Vector<GridIndex5D> GridIndex5Ds;
238 typedef base::Vector<ExtendedGridIndex5D> ExtendedGridIndex5Ds;
239 
240 typedef GridIndexD<6> GridIndex6D;
241 typedef ExtendedGridIndexD<6> ExtendedGridIndex6D;
242 typedef base::Vector<GridIndex6D> GridIndex6Ds;
243 typedef base::Vector<ExtendedGridIndex6D> ExtendedGridIndex6Ds;
244 
245 typedef GridIndexD<-1> GridIndexKD;
246 typedef ExtendedGridIndexD<-1> ExtendedGridIndexKD;
247 typedef base::Vector<GridIndexKD> GridIndexKDs;
248 typedef base::Vector<ExtendedGridIndexKD> ExtendedGridIndexKDs;
249 #endif
250 
251 IMPALGEBRA_END_NAMESPACE
252 
253 #endif /* IMPALGEBRA_GRID_INDEXES_H */
ExtendedGridIndexD(Ints vals)
Create a grid cell from three arbitrary indexes.
Definition: grid_indexes.h:63
Basic types used by IMP.
Represent a real cell in a grid (one within the bounding box)
Definition: grid_indexes.h:136
An index in an infinite grid on space.
Definition: grid_indexes.h:28
#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_HASHABLE_INLINE(name, hashret)
#define IMP_USAGE_CHECK(expr, message)
A runtime test for incorrect usage of a class or method.
Basic types used by IMP.
#define IMP_COMPARISONS(Name)
Implement comparison in a class using a compare function.
int compare(const VectorD< D > &a, const VectorD< D > &b)
lexicographic comparison of two vectors
Definition: VectorD.h:371
#define IMP_CONST_BRACKET(Value, Index, bounds_check_expr, expr)
Exception definitions and assertions.
#define IMP_BRACKET(Value, Index, bounds_check_expr, expr)