9#ifndef IMPALGEBRA_GRID_EMBEDDINGS_H
10#define IMPALGEBRA_GRID_EMBEDDINGS_H
12#include <IMP/algebra/algebra_config.h>
18#include <boost/iterator/transform_iterator.hpp>
19#include <cereal/access.hpp>
26IMPALGEBRA_BEGIN_NAMESPACE
30class DefaultEmbeddingD {
36 friend class cereal::access;
38 template<
class Archive>
void save(Archive &ar)
const {
39 ar(origin_, unit_cell_);
42 template<
class Archive>
void load(Archive &ar) {
43 ar(origin_, unit_cell_);
44 set_unit_cell(unit_cell_);
49 for (
unsigned int i = 0; i < get_dimension(); ++i) {
55 VectorD<D> get_uniform_offset(
const O &v0,
double o)
const {
56 Floats ret(get_dimension());
57 for (
unsigned int i = 0; i < get_dimension(); ++i) {
63 Floats nuc(bb.get_dimension());
64 for (
unsigned int i = 0; i < bb.get_dimension(); ++i) {
65 double side = bb.get_corner_1()[i] - bb.get_corner_0()[i];
67 nuc[i] = side / ns[i];
69 set_unit_cell(
VectorD<D>(nuc.begin(), nuc.end()));
70 set_origin(bb.get_corner_0());
78 DefaultEmbeddingD() {}
79 void set_origin(
const VectorD<D> &o) { origin_ = o; }
80 const VectorD<D> get_origin()
const {
return origin_; }
81 unsigned int get_dimension()
const {
return get_origin().get_dimension(); }
84 Floats iuc(o.get_dimension());
85 for (
unsigned int i = 0; i < get_dimension(); ++i) {
86 iuc[i] = 1.0 / unit_cell_[i];
88 inverse_unit_cell_ =
VectorD<D>(iuc.begin(), iuc.end());
92 const VectorD<D> &get_inverse_unit_cell()
const {
return inverse_unit_cell_; }
108 origin_.get_dimension());
109 for (
unsigned int i = 0; i < get_dimension(); ++i) {
110 double d = o[i] - origin_[i];
111 double fi = d * inverse_unit_cell_[i];
112 ret.access_data().get_data()[i] =
static_cast<int>(std::floor(fi));
118 origin_.get_dimension());
119 for (
unsigned int i = 0; i < get_dimension(); ++i) {
120 double d = o[i] - origin_[i];
121 double fi = d * inverse_unit_cell_[i];
122 ret.access_data().get_data()[i] =
static_cast<int>(std::floor(fi));
130 VectorD<D> get_center(
const ExtendedGridIndexD<D> &ei)
const {
132 get_elementwise_product(get_unit_cell(), get_uniform_offset(ei, .5));
134 VectorD<D> get_center(
const GridIndexD<D> &ei)
const {
144 BoundingBoxD<D> get_bounding_box(
const ExtendedGridIndexD<D> &ei)
const {
145 return BoundingBoxD<D>(
150 BoundingBoxD<D> get_bounding_box(
const GridIndexD<D> &ei)
const {
151 return BoundingBoxD<D>(
158 <<
" unit cell: " << unit_cell_);
161#if !defined(IMP_DOXYGEN)
162typedef DefaultEmbeddingD<1> DefaultEmbedding1D;
163typedef Vector<DefaultEmbedding1D> DefaultEmbedding1Ds;
165typedef DefaultEmbeddingD<2> DefaultEmbedding2D;
166typedef Vector<DefaultEmbedding2D> DefaultEmbedding2Ds;
168typedef DefaultEmbeddingD<3> DefaultEmbedding3D;
169typedef Vector<DefaultEmbedding3D> DefaultEmbedding3Ds;
171typedef DefaultEmbeddingD<4> DefaultEmbedding4D;
172typedef Vector<DefaultEmbedding4D> DefaultEmbedding4Ds;
174typedef DefaultEmbeddingD<5> DefaultEmbedding5D;
175typedef Vector<DefaultEmbedding5D> DefaultEmbedding5Ds;
177typedef DefaultEmbeddingD<6> DefaultEmbedding6D;
178typedef Vector<DefaultEmbedding6D> DefaultEmbedding6Ds;
180typedef DefaultEmbeddingD<-1> DefaultEmbeddingKD;
181typedef Vector<DefaultEmbeddingKD> DefaultEmbeddingKDs;
191 friend class cereal::access;
193 template<
class Archive>
void serialize(Archive &ar) {
194 ar(origin_, unit_cell_, base_);
198 VectorD<D> get_coordinates(
const O &index)
const {
200 for (
unsigned int i = 0; i < unit_cell_.get_dimension(); ++i) {
204 "Log grid axis must have positive index.");
205 ret[i] += unit_cell_[i] * (1.0 - std::pow(base_[i], index[i])) /
208 ret[i] += unit_cell_[i] * index[i];
214 VectorD<D> get_uniform_offset(
const O &v0,
double o)
const {
215 Floats ret(get_dimension());
216 for (
unsigned int i = 0; i < get_dimension(); ++i) {
222 Floats nuc(bb.get_dimension());
223 for (
unsigned int i = 0; i < bb.get_dimension(); ++i) {
224 double side = bb.get_corner_1()[i] - bb.get_corner_0()[i];
226 nuc[i] = side / ns[i];
228 set_unit_cell(
VectorD<D>(nuc.begin(), nuc.end()));
229 set_origin(bb.get_corner_0());
236 set_unit_cell(cell, base);
251 const Ints &counts,
bool bound_centers =
false) {
252 set_origin(bb.get_corner_0());
254 for (
unsigned int i = 0; i < bases.get_dimension(); ++i) {
256 "LogEmbedding base #" << i <<
" cannot be negative",
260 cell[i] = (bb.get_corner_1()[i]
261 - bb.get_corner_0()[i]) * (bases[i] - 1) /
262 (std::pow(bases[i], counts[i]) - 1.0);
264 cell[i] = (bb.get_corner_1()[i] - bb.get_corner_0()[i]) / counts[i];
267 .9 * cell[i] < bb.get_corner_1()[i] - bb.get_corner_0()[i],
268 "Too large a cell side");
271 set_unit_cell(cell, bases);
276 get_uniform_offset(
GridIndexD<D>(counts.begin(), counts.end()), -.5));
277 VectorD<D> extents = upper_corner - lower_corner;
280 for (
unsigned int i = 0; i < uc.get_dimension(); ++i) {
282 (bb.get_corner_1()[i] - bb.get_corner_0()[i]) / extents[i] * uc[i];
284 orig[i] = bb.get_corner_0()[i] - .5 * uc[i];
288 orig[i] = bb.get_corner_0()[i] -
289 uc[i] * (1.0 - std::pow(bases[i], .5)) / (1.0 - bases[i]);
293 set_unit_cell(uc, bases);
300 void set_origin(
const VectorD<D> &o) { origin_ = o; }
301 const VectorD<D> get_origin()
const {
return origin_; }
302 unsigned int get_dimension()
const {
return get_origin().get_dimension(); }
303 void set_unit_cell(
const VectorD<D> &o,
const VectorD<D> &base) {
307 void set_unit_cell(
const VectorD<D> &o) { unit_cell_ = o; }
322 origin_.get_dimension());
323 for (
unsigned int i = 0; i < get_dimension(); ++i) {
324 double fi = (o[i] - origin_[i]) / unit_cell_[i];
330 double arg = 1.0 + fi * (base_[i] - 1.0);
332 li = std::log(arg) / std::log(base_[i]);
336 li = (base_[i] > 1 ? -1. : 1.)
337 * std::numeric_limits<double>::infinity();
342 if (li < std::numeric_limits<int>::min()) {
343 li = std::numeric_limits<int>::min();
344 }
else if (li > std::numeric_limits<int>::max()) {
345 li = std::numeric_limits<int>::max();
347 ret.access_data().get_data()[i] =
static_cast<int>(li);
359 VectorD<D> get_center(
const ExtendedGridIndexD<D> &ei)
const {
360 return get_coordinates(get_uniform_offset(ei, .5));
362 VectorD<D> get_center(
const GridIndexD<D> &ei)
const {
363 return get_coordinates(get_uniform_offset(ei, .5));
371 BoundingBoxD<D> get_bounding_box(
const ExtendedGridIndexD<D> &ei)
const {
372 return BoundingBoxD<D>(get_coordinates(ei),
373 get_coordinates(get_uniform_offset(ei, 1)));
375 BoundingBoxD<D> get_bounding_box(
const GridIndexD<D> &ei)
const {
376 return get_bounding_box(ExtendedGridIndexD<D>(ei.begin(), ei.end()));
380 <<
" base: " << base_);
383#if !defined(IMP_DOXYGEN)
384typedef LogEmbeddingD<1> LogEmbedding1D;
385typedef Vector<LogEmbedding1D> LogEmbedding1Ds;
387typedef LogEmbeddingD<2> LogEmbedding2D;
388typedef Vector<LogEmbedding2D> LogEmbedding2Ds;
390typedef LogEmbeddingD<3> LogEmbedding3D;
391typedef Vector<LogEmbedding3D> LogEmbedding3Ds;
393typedef LogEmbeddingD<4> LogEmbedding4D;
394typedef Vector<LogEmbedding4D> LogEmbedding4Ds;
396typedef LogEmbeddingD<5> LogEmbedding5D;
397typedef Vector<LogEmbedding5D> LogEmbedding5Ds;
399typedef LogEmbeddingD<6> LogEmbedding6D;
400typedef Vector<LogEmbedding6D> LogEmbedding6Ds;
402typedef LogEmbeddingD<-1> LogEmbeddingKD;
403typedef Vector<LogEmbeddingKD> LogEmbeddingKDs;
406IMPALGEBRA_END_NAMESPACE
A bounding box in D dimensions.
A class for storing lists of IMP items.
Helper macros for throwing and handling exceptions.
#define IMP_INTERNAL_CHECK(expr, message)
An assertion to check for internal errors in IMP. An IMP::ErrorException will be thrown.
#define IMP_ALWAYS_CHECK(condition, message, exception_name)
Throw an exception if a check fails.
#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.
An exception for an invalid value being passed to IMP.
A Cartesian vector in D-dimensions.
An axis-aligned bounding box.
ExtendedGridIndexD< D > get_extended_index(const VectorD< D > &o) const
Return the index that would contain the voxel if the grid extended there.
const VectorD< D > & get_unit_cell() const
Return the unit cell, relative to the origin.
An index in an infinite grid on space.
Represent a real cell in a grid (one within the bounding box).
LogEmbeddingD(const BoundingBoxD< D > &bb, const VectorD< D > &bases, const Ints &counts, bool bound_centers=false)
const VectorD< D > & get_unit_cell() const
Return the unit cell, relative to the origin.
ExtendedGridIndexD< D > get_extended_index(const VectorD< D > &o) const
Return the index that would contain the voxel if the grid extended there.
Exception definitions and assertions.
A class to represent a voxel grid.
IMP::Vector< Int > Ints
Standard way to pass a bunch of Int values.
VectorD< D > get_elementwise_product(const VectorD< D > &a, const VectorD< D > &b)
Return the vector that is the elementwise product of the two.
IMP::Vector< Float > Floats
Standard way to pass a bunch of Float values.
#define IMP_SHOWABLE_INLINE(Name, how_to_show)
Declare the methods needed by an object that can be printed.