9#ifndef IMPEM_DENSITY_MAP_H
10#define IMPEM_DENSITY_MAP_H
12#include <IMP/em/em_config.h>
20#include <boost/scoped_array.hpp>
21#include <cereal/access.hpp>
22#include <cereal/types/base_class.hpp>
57IMPEMEXPORT DensityMap *
read_map(std::string filename);
62IMPEMEXPORT
void write_map(DensityMap *m, std::string filename,
74IMPEMEXPORT
void write_map(DensityMap *m, std::string filename);
97 IMP_NO_SWIG(
friend IMPEMEXPORT DensityMap *read_map(std::string filename,
99 IMP_NO_SWIG(
friend IMPEMEXPORT
void write_map(DensityMap *m,
100 std::string filename,
104 DensityMap(std::string name =
"DensityMap%1%");
123 inline bool is_normalized()
const {
return normalized_; }
140 return z * header_.get_nx() * header_.get_ny() + y * header_.get_nx() + x;
172 "invalid map index");
175 "locations should be calculated prior to calling this function");
179 bool is_xyz_ind_part_of_volume(
int ix,
int iy,
int iz)
const;
207 return get_value(point[0], point[1], point[2]);
222 void set_value(
float x,
float y,
float z,
double value);
236 header_.get_origin(2));
250#if !defined(IMP_DOXYGEN) && !defined(SWIG)
255 float *get_x_loc()
const {
257 "x location requested before being calculated");
264 float *get_y_loc()
const {
266 "y location requested before being calculated");
273 float *get_z_loc()
const {
275 "z location requested before being calculated");
279 double *get_data()
const {
return data_.get(); }
314 void add(
const DensityMap *other);
331 return header_.get_number_of_voxels();
352 void pad(
int nx,
int ny,
int nz,
float val = 0.0);
371 DensityMap *
pad_margin(
int mrg_x,
int mrg_y,
int mrg_z,
float val = 0.0);
398 bool keep_map_dimensions=
false);
436#if !defined(IMP_DOXYGEN) && !defined(SWIG)
445 void convolute_kernel(DensityMap *other,
double *kernel,
int dim_len);
453 void convolute_kernel(
double *kernel,
int dim_len) {
455 cmap->set_was_used(
true);
456 convolute_kernel(cmap, kernel, dim_len);
457 cmap =
static_cast<DensityMap *
>(
nullptr);
460 int lower_voxel_shift(
double loc,
double kdist,
double orig,
int ndim)
const;
461 int upper_voxel_shift(
double loc,
double kdist,
double orig,
int ndim)
const;
462 inline bool get_rms_calculated()
const {
return rms_calculated_; }
463 int get_dim_index_by_location(
float loc_val,
int ind)
const;
468 void reset_all_voxel2loc();
470 void allocated_data();
471 void float2real(
float *f_data, boost::scoped_array<double> &r_data);
472 void real2float(
double *r_data, boost::scoped_array<float> &f_data);
475 boost::scoped_array<double> data_;
476 bool data_allocated_;
481 boost::scoped_array<float>
x_loc_, y_loc_, z_loc_;
486 bool rms_calculated_;
488 friend class cereal::access;
490 template<
class Archive>
void serialize(Archive &ar) {
491 ar(cereal::base_class<Object>(
this),
496 if (std::is_base_of<cereal::detail::InputArchiveBase, Archive>::value) {
497 data_.reset(
new double[size]);
505 ar(cereal::binary_data(data_.get(), size *
sizeof(
double)));
509inline algebra::BoundingBoxD<3> get_bounding_box(
const DensityMap *m) {
510 const DensityHeader *h = m->get_header();
511 float hspace = m->get_spacing() / 2.0;
512 algebra::Vector3D lb = m->get_origin()
513 - algebra::Vector3D(hspace, hspace, hspace);
514 return algebra::BoundingBoxD<3>(
516 lb + algebra::Vector3D(m->get_spacing() * h->get_nx(),
517 m->get_spacing() * h->get_ny(),
518 m->get_spacing() * h->get_nz()));
521#if !defined(IMP_DOXYGEN) && !defined(SWIG)
536inline void calc_local_bounding_box(
const em::DensityMap *d_map,
double x,
537 double y,
double z,
float kdist,
int &iminx,
538 int &iminy,
int &iminz,
int &imaxx,
539 int &imaxy,
int &imaxz) {
540 const DensityHeader *h = d_map->get_header();
541 iminx = d_map->lower_voxel_shift(x, kdist, h->get_xorigin(), h->get_nx());
542 iminy = d_map->lower_voxel_shift(y, kdist, h->get_yorigin(), h->get_ny());
543 iminz = d_map->lower_voxel_shift(z, kdist, h->get_zorigin(), h->get_nz());
544 imaxx = d_map->upper_voxel_shift(x, kdist, h->get_xorigin(), h->get_nx());
545 imaxy = d_map->upper_voxel_shift(y, kdist, h->get_yorigin(), h->get_ny());
546 imaxz = d_map->upper_voxel_shift(z, kdist, h->get_zorigin(), h->get_nz());
595 DensityMap *into,
bool calc_rms =
true);
596IMPEMEXPORT
void get_transformed_into2(
const DensityMap *source,
600inline bool get_interiors_intersect(
const DensityMap *d1,
601 const DensityMap *d2) {
622 int nx_end,
int ny_start,
int ny_end,
623 int nz_start,
int nz_end);
631 float mas_threshold);
640 bool reverse =
false);
682template <
class S,
class V,
class E>
689 "The passed grid does not seem to have cubic voxels");
693 static_cast<unsigned int>(ret->get_header()->get_nx()),
694 "X voxels don't match");
696 static_cast<unsigned int>(ret->get_header()->get_ny()),
697 "Y voxels don't match");
699 static_cast<unsigned int>(ret->get_header()->get_nz()),
700 "Z voxels don't match");
701 for(
typename Grid::Index i : arg.get_all_indexes()) {
702 long vi = ret->xyz_ind2voxel(i[0], i[1], i[2]);
703 ret->set_value(vi, arg[vi]);
A bounding box in D dimensions.
An abstract class for reading a map.
A shared base class to help in debugging and things.
A nullptr-initialized pointer to an IMP Object.
#define IMP_USAGE_CHECK(expr, message)
A runtime test for incorrect usage of a class or method.
Common base class for heavy weight IMP objects.
An axis-aligned bounding box.
const VectorD< D > & get_unit_cell() const
Return the unit cell, relative to the origin.
A voxel grid in d-dimensional space.
Class for handling density maps.
void write_map(DensityMap *m, std::string filename, MapReaderWriter *writer)
Write a density map to a file using the given writer.
void reset_data(float value=0.0)
Set the density voxels to some value and reset the management flags.
DensityMap * get_resampled(DensityMap *input, double scaling)
Get a resampled version of the map.
float get_maximum_value_in_xy_plane(int z_ind)
Get the maximum value in a XY plane indicated by a Z index.
void add(const DensityMap *other)
Sums two grids; the result is kept in the map.
long get_voxel_by_location(float x, float y, float z) const
Calculate the voxel of a given location.
DensityMap * get_transformed(const DensityMap *input, const algebra::Transformation3D &tr, double threshold)
Return a new density map containing a rotated version of the old one.
void multiply(float factor)
Multiply in place each voxel in the map by the input factor.
DensityMap * read_map(std::string filename)
Read a density map from a file and return it.
void update_header()
update the header values – still in work
bool loc_calculated_
true if the locations have already been computed
void set_origin(float x, float y, float z)
Sets the origin of the header.
bool same_voxel_size(const DensityMap *other) const
Checks if two maps have the same voxel size.
double get_value(long index) const
Gets the value of the voxel at a given index.
bool is_part_of_volume(float x, float y, float z) const
Checks whether a given point is in the grid.
void pad(int nx, int ny, int nz, float val=0.0)
Increase the dimension of the map.
void add(Float d)
Add a number to every voxel in the map.
DensityMap * create_density_map(algebra::DenseGrid3D< float > &grid)
Return a density map with the values taken from the grid.
void calc_all_voxel2loc()
Calculates the coordinates that correspond to all voxels.
DensityMap * get_cropped(const algebra::BoundingBox3D &bb)
Create and return a new cropped map with the given bounding box extent.
DensityMap * get_binarized_interior(DensityMap *dmap)
Return a binarized map with 1 for voxels that are internal in the input map.
void set_void_map(int nx, int ny, int nz)
Set the map dimension and reset all voxels to 0.
void write_map(DensityMap *m, std::string filename)
Write a density map to a file.
long get_voxel_by_location(const algebra::Vector3D &v) const
Calculate the voxel of a given location.
void std_normalize()
Normalize the density voxels according to standard deviation (stdv).
long get_number_of_voxels() const
Get the number of map voxels.
long xyz_ind2voxel(int x, int y, int z) const
Calculate the voxel of a given xyz index.
double calcRMS()
Calculates RMSD and mean of a map values as stored in the header.
void set_value(float x, float y, float z, double value)
Set the value of the voxel at given coordinates.
float get_location_in_dim_by_voxel(long index, int dim) const
Calculate the location of a given voxel in a given dimension.
void copy_map(const DensityMap *other)
Copy map into this map.
void get_transformed_into(const DensityMap *source, const algebra::Transformation3D &tr, DensityMap *into, bool calc_rms=true)
Rotate a density map into another map.
double get_value(float x, float y, float z) const
Gets the value of the voxel located at (x,y,z).
Float get_spacing() const
Get the voxel size of the map.
boost::scoped_array< float > x_loc_
DensityMap * get_cropped(float threshold)
Create and return a new cropped map.
void set_value(long index, double value)
Set the value of the voxel at a given index.
DensityMap * read_map(std::string filename, MapReaderWriter *reader)
Read a density map from a file (using the given reader) and return it.
bool is_part_of_volume(const algebra::Vector3D &v) const
Checks whether a given point is in the grid.
float get_maximum_value_in_yz_plane(int x_ind)
Get the maximum value in a YZ plane indicated by a X index.
const DensityHeader * get_header() const
Returns a read-only pointer to the header of the map.
algebra::Vector3D get_centroid(double threshold=0.0) const
Get the centroid of all voxels with density above a given threshold.
float get_maximum_value_in_xz_plane(int y_ind)
Get the maximum value in a XZ plane indicated by a Y index.
DensityMap(const DensityHeader &header, std::string name="DensityMap%1%")
Construct a density map as instructed in the input header.
void update_voxel_size(float new_apix)
Update the voxel size of the map.
bool same_dimensions(const DensityMap *other) const
Checks if two maps have the same dimensions.
int get_dim_index_by_location(const algebra::Vector3D &v, int ind) const
Calculate dimension index of a given location.
bool same_origin(const DensityMap *other) const
Checks if two maps have the same origin.
DensityHeader * get_header_writable()
Returns a pointer to the header of the map in a writable version.
algebra::Vector3D get_location_by_voxel(long index) const
Calculate the location of a given voxel.
double get_min_value() const
Returns the value of the voxel with the lowest density.
DensityMap * pad_margin(int mrg_x, int mrg_y, int mrg_z, float val=0.0)
Create a new padded map.
void pick_max(const DensityMap *other)
Pick the max value between two corresponding voxels between two maps.
std::string get_locations_string(float t)
Print the locations of all voxels with value above a given threshold.
double get_density(const DensityMap *m, const algebra::Vector3D &v)
Get density value at point v, interpolating linearly from the sample values.
DensityMap * create_density_map(algebra::DenseGrid3D< double > &grid)
Return a density map with the values taken from the grid.
DensityMap * get_cropped(Particles ps, double distance, bool inverse=false, bool keep_map_dimensions=false)
Create and return a new cropped map based on particles.
double get_max_value() const
Returns the value of the voxel with the highest density.
void add_to_map(DensityMap *dm, const Particles &pis)
Rasterize the particles into an existing density map.
DensityMap * get_transformed(DensityMap *input, const algebra::Transformation3D &tr)
Return a new density map containing a rotated version of the old one.
The base class to handle reading and writing of density maps.
#define IMP_FUNCTION_LOG
Beginning logging for a non-member function.
BoundingBoxD< 3 > BoundingBox3D
Typedef for Python.
double get_sum(const DensityMap *m1)
Return the sum of all voxels.
DensityMap * get_segment(DensityMap *map_to_segment, int nx_start, int nx_end, int ny_start, int ny_end, int nz_start, int nz_end)
Get a segment of the map according to xyz indexes.
DensityMap * get_threshold_map(const DensityMap *orig_map, float threshold)
Return a map with 0 for all voxels below the threshold.
algebra::BoundingBoxD< 3 > get_bounding_box(const DensityMap *m, Float threshold)
Get the bounding box for a map.
DensityMap * create_density_map(const DensityMap *other)
Create a copy of another map.
DensityMap * interpolate_map(DensityMap *in_map, double new_spacing)
Return a new map with an updated spacing.
IMP::Vector< IMP::Pointer< DensityMap > > DensityMaps
DensityMap * get_segment_by_masking(DensityMap *map_to_segment, DensityMap *mask, float mas_threshold)
Get a segment of the map covered by another map.
Float approximate_molecular_mass(DensityMap *m, Float threshold)
Estimate the molecular mass from a map.
DensityMap * binarize(DensityMap *orig_map, float threshold, bool reverse=false)
Return a map with 0 for all voxels below the threshold and 1 for those above.
algebra::GridD< 3, algebra::DenseGridStorageD< 3, float >, float > get_grid(DensityMap *in_map)
Return a dense grid containing the voxels of the passed density map.
double convolute(const DensityMap *m1, const DensityMap *m2)
Return a convolution between density maps m1 and m2.
DensityMap * multiply(const DensityMap *m1, const DensityMap *m2)
Return a density map for which voxel i contains the result of m1[i]*m2[i].
DensityMap * get_max_map(DensityMaps maps)
Return a map where each voxel is the maximum value from the input maps.
Vector< VectorD< 3 > > Vector3Ds
Vector< Pointer< Particle > > Particles
A list of reference-counted Particle objects.
double Float
Basic floating-point value (could be float, double...).
#define IMP_OBJECTS(Name, PluralName)
Define the types for storing lists of object pointers.
#define IMP_OBJECT_METHODS(Name)
Define the basic things needed by any Object.
All grids that are in the Python API should be defined here.
A smart pointer to a reference counted object.
O * release()
Relinquish control of the raw pointer stored in the Pointer.
#define IMP_NO_SWIG(x)
Hide the line when SWIG is compiled or parses it.