IMP logo
IMP Reference Guide  develop.7482b7c7af, 2026/12/05
The Integrative Modeling Platform
DensityMap.h
Go to the documentation of this file.
1/**
2 * \file IMP/em/DensityMap.h
3 * \brief Class for handling density maps.
4 *
5 * Copyright 2007-2026 IMP Inventors. All rights reserved.
6 *
7 */
8
9#ifndef IMPEM_DENSITY_MAP_H
10#define IMPEM_DENSITY_MAP_H
11#include <IMP/Pointer.h>
12#include <IMP/em/em_config.h>
13#include "DensityHeader.h"
14#include "MapReaderWriter.h"
15#include <IMP/Object.h>
19#include <IMP/Object.h>
20#include <boost/scoped_array.hpp>
21#include <cereal/access.hpp>
22#include <cereal/types/base_class.hpp>
23#include <iostream>
24#include <iomanip>
26#include <IMP/base_types.h>
27//#include <IMP/statistics/Histogram.h>
28
29IMPEM_BEGIN_NAMESPACE
30
31class DensityMap;
32
33//! Create a copy of another map
34IMPEMEXPORT DensityMap *create_density_map(const DensityMap *other);
35
36//! Create an empty density map from a bounding box
38 double spacing);
39
40//! Create an empty density map
41IMPEMEXPORT DensityMap *create_density_map(int nx, int ny, int nz,
42 double spacing);
43
44//! Read a density map from a file (using the given reader) and return it.
45/** \relates DensityMap
46 */
47IMPEMEXPORT DensityMap *read_map(std::string filename, MapReaderWriter *reader);
48
49//! Read a density map from a file and return it.
50/** Guess the file type from the file name. The file formats supported are:
51 - .mrc/.map
52 - .em
53 - .vol
54 - .xplor
55 \relates DensityMap
56*/
57IMPEMEXPORT DensityMap *read_map(std::string filename);
58
59//! Write a density map to a file using the given writer.
60/** \relates DensityMap
61*/
62IMPEMEXPORT void write_map(DensityMap *m, std::string filename,
63 MapReaderWriter *writer);
64
65//! Write a density map to a file.
66/** Guess the file type from the
67 file name. The file formats supported are:
68 - .mrc/.map
69 - .em
70 - .vol
71 - .xplor
72 \relates DensityMap
73*/
74IMPEMEXPORT void write_map(DensityMap *m, std::string filename);
75
76//! Get the bounding box for a map.
77/** \param[in] m a density map
78 \param[in] threshold find the bounding box for voxels
79 with value above the threshold
80 */
82 Float threshold);
83//! Estimate the molecular mass from a map.
84/** \param[in] m a density map
85 \param[in] threshold consider volume of only voxels above this threshold
86 \note The method assumes 1.21 cubic Å per Dalton (Harpaz 1994).
87 */
89
90//! Class for handling density maps.
91/** \note The location of a voxel is its center. That is important
92 for sampling function as well as for functions
93 like get_location_in_dim_by_voxel.
94 */
95class IMPEMEXPORT DensityMap : public IMP::Object {
97 IMP_NO_SWIG(friend IMPEMEXPORT DensityMap *read_map(std::string filename,
98 MapReaderWriter *reader));
99 IMP_NO_SWIG(friend IMPEMEXPORT void write_map(DensityMap *m,
100 std::string filename,
101 MapReaderWriter *writer));
102
103 public:
104 DensityMap(std::string name = "DensityMap%1%");
105 //! Construct a density map as instructed in the input header
106 DensityMap(const DensityHeader &header, std::string name = "DensityMap%1%");
107
108 //! Set the density voxels to some value and reset the management flags.
109 /** \param[in] value all of the density voxels will have this value
110 */
111 void reset_data(float value = 0.0);
112
113 //! Calculates RMSD and mean of a map values as stored in the header.
114 /** The header stores whether the map is normalized.
115 */
116 double calcRMS();
117
118 //! Normalize the density voxels according to standard deviation (stdv).
119 /** The mean is subtracted from the map, which is then divided by the stdv.
120 The normalization flag is set to avoid repeated computation. */
122
123 inline bool is_normalized() const { return normalized_; }
124
125 //! Calculate the location of a given voxel in a given dimension
126 /** \param[in] index The voxel index
127 \param[in] dim The dimension of interest (x:=0,y:=1,z:=2)
128 \return the location (x,y,z) (in angstroms) of a given voxel. If the
129 index is not part of the map, the function returns -1.
130 */
131 float get_location_in_dim_by_voxel(long index, int dim) const;
132
133 //! Calculate the voxel of a given xyz index
134 /** \param[in] x The voxel index on the x axis of the grid
135 \param[in] y The voxel index on the y axis of the grid
136 \param[in] z The voxel index on the z axis of the grid
137 \return the voxel index.
138 */
139 inline long xyz_ind2voxel(int x, int y, int z) const {
140 return z * header_.get_nx() * header_.get_ny() + y * header_.get_nx() + x;
141 }
142
143 //! Calculate the voxel of a given location
144 /** \param[in] x The position (in angstroms) of the x coordinate
145 \param[in] y The position (in angstroms) of the y coordinate
146 \param[in] z The position (in angstroms) of the z coordinate
147 \return the voxel index of a given position. If the position is out of
148 the boundaries of the map, the function returns -1.
149 */
150 long get_voxel_by_location(float x, float y, float z) const;
151
152 //! Calculate the voxel of a given location
153 /** \param[in] v The position (in angstroms)
154 \return the voxel index of a given position. If the position is out of
155 the boundaries of the map, the function returns -1.
156 */
158 return get_voxel_by_location(v[0], v[1], v[2]);
159 }
160 //! Calculate dimension index of a given location
161 /** \param[in] v The position (in angstroms)
162 \param[in] ind dimension index (X:0,Y:1 or Z:2)
163 */
164 int get_dim_index_by_location(const algebra::Vector3D &v, int ind) const;
165
166 //! Calculate the location of a given voxel.
167 /** \param[in] index The voxel index
168 \return the location (x,y,z) (in angstroms) of a given voxel.
169 */
171 IMP_USAGE_CHECK(index >= 0 && index < get_number_of_voxels(),
172 "invalid map index");
175 "locations should be calculated prior to calling this function");
176 return algebra::Vector3D(x_loc_[index], y_loc_[index], z_loc_[index]);
177 }
178
179 bool is_xyz_ind_part_of_volume(int ix, int iy, int iz) const;
180
181 //! Checks whether a given point is in the grid
182 /** \param[in] x The position (in angstroms) of the x coordinate
183 \param[in] y The position (in angstroms) of the y coordinate
184 \param[in] z The position (in angstroms) of the z coordinate
185 \return true if the point is part of the grid, false otherwise.
186 */
187 bool is_part_of_volume(float x, float y, float z) const;
188 //! Checks whether a given point is in the grid
189 /** \param[in] v The position (in angstroms)
190 \return true if the point is part of the grid, false otherwise.
191 */
193 return is_part_of_volume(v[0], v[1], v[2]);
194 }
195
196 //! Gets the value of the voxel located at (x,y,z)
197 /** \param[in] x The position (in angstroms) of the x coordinate
198 \param[in] y The position (in angstroms) of the y coordinate
199 \param[in] z The position (in angstroms) of the z coordinate
200 \return the value of the voxel located at (x,y,z)
201 \exception IndexException The point is not covered by the grid.
202 \note the value is not interpolated between this and neighboring
203 voxels. For that, see get_density().
204 */
205 double get_value(float x, float y, float z) const;
206 double get_value(const algebra::Vector3D &point) const {
207 return get_value(point[0], point[1], point[2]);
208 }
209
210 //! Gets the value of the voxel at a given index
211 /** \param[in] index voxel number in physical sense, NOT logical
212 */
213 double get_value(long index) const;
214
215 //! Set the value of the voxel at a given index
216 /** \param[in] index voxel number in physical sense, NOT logical
217 \param[in] value value
218 */
219 void set_value(long index, double value);
220
221 //! Set the value of the voxel at given coordinates
222 void set_value(float x, float y, float z, double value);
223
224 //! Sets the origin of the header
225 /** \param x the new x (angstroms)
226 \param y the new y (angstroms)
227 \param z the new z (angstroms)
228 */
229 void set_origin(float x, float y, float z);
230 void set_origin(const IMP::algebra::Vector3D &v) {
231 set_origin(v[0], v[1], v[2]);
232 }
233
234 algebra::Vector3D get_origin() const {
235 return algebra::Vector3D(header_.get_origin(0), header_.get_origin(1),
236 header_.get_origin(2));
237 }
238
239 algebra::Vector3D get_top() const {
240 return algebra::Vector3D(header_.get_top(0), header_.get_top(1),
241 header_.get_top(2));
242 }
243
244 //! Returns a read-only pointer to the header of the map
245 const DensityHeader *get_header() const { return &header_; }
246
247 //! Returns a pointer to the header of the map in a writable version
248 DensityHeader *get_header_writable() { return &header_; }
249
250#if !defined(IMP_DOXYGEN) && !defined(SWIG)
251 //! Returns the x-location of the map
252 /**
253 \exception InvalidStateException The locations have not been calculated.
254 */
255 float *get_x_loc() const {
256 IMP_USAGE_CHECK(loc_calculated_,
257 "x location requested before being calculated");
258 return x_loc_.get();
259 }
260 //! Returns the y-location of the map
261 /**
262 \exception InvalidStateException The locations have not been calculated.
263 */
264 float *get_y_loc() const {
265 IMP_USAGE_CHECK(loc_calculated_,
266 "y location requested before being calculated");
267 return y_loc_.get();
268 }
269 //! Returns the z-location of the map
270 /**
271 \exception InvalidStateException The locations have not been calculated.
272 */
273 float *get_z_loc() const {
274 IMP_USAGE_CHECK(loc_calculated_,
275 "z location requested before being calculated");
276 return z_loc_.get();
277 }
278
279 double *get_data() const { return data_.get(); }
280#endif
281
282 //! Checks if two maps have the same origin
283 /** \param[in] other the map to compare with
284 \return true if the two maps have the same origin
285 */
286 bool same_origin(const DensityMap *other) const;
287
288 //! Checks if two maps have the same dimensions
289 /** \param[in] other the map to compare with
290 \return true if the two maps have the same dimensions
291 */
292 bool same_dimensions(const DensityMap *other) const;
293
294 //! Checks if two maps have the same voxel size
295 /** \param[in] other the map to compare with
296 \return true if the two maps have the same voxel size
297 */
298 bool same_voxel_size(const DensityMap *other) const;
299 //! Get the centroid of all voxels with density above a given threshold
300 /** \param[in] threshold the input threshold
301 */
302 algebra::Vector3D get_centroid(double threshold = 0.0) const;
303 //! Returns the value of the voxel with the highest density.
304 double get_max_value() const;
305 //! Returns the value of the voxel with the lowest density.
306 double get_min_value() const;
307
308 //! Sums two grids; the result is kept in the map.
309 /** \param[in] other the other map
310 \note The shared extend is summed
311 \note The two maps should have the same voxelsize and other
312 should be contained within this map
313 */
314 void add(const DensityMap *other);
315
316 //! Add a number to every voxel in the map
317 /** \param[in] d Value to add
318
319 */
320 void add(Float d);
321
322 //! Pick the max value between two corresponding voxels between two maps
323 /** The result is kept in the map.
324 \param[in] other the other map
325 \note The two maps should have the same voxelsize and the same dimensions
326 */
327 void pick_max(const DensityMap *other);
328
329 //! Get the number of map voxels
330 long get_number_of_voxels() const {
331 return header_.get_number_of_voxels();
332 }
333
334 //! Set the map dimension and reset all voxels to 0
335 /** \param[in] nx x-dimension (voxels)
336 \param[in] ny y-dimension (voxels)
337 \param[in] nz z-dimension (voxels)
338 \note the origin and spacing remain unchanged
339 */
340 void set_void_map(int nx, int ny, int nz);
341
342 //! Increase the dimension of the map
343 /** The function pads zeroes to the right-upper section on the map.
344 The original content of the map will be in the lower XYZ part of the map.
345 \param[in] nx the number of voxels on the X axis
346 \param[in] ny the number of voxels on the Y axis
347 \param[in] nz the number of voxels on the Z axis
348 \param[in] val all additional voxels will have this value
349 \exception UsageException if the input numbers of x/y/z voxels are
350 smaller than those currently in the map
351 */
352 void pad(int nx, int ny, int nz, float val = 0.0);
353
354 //! Create a new padded map
355 /** Given this map of size [nx,ny,nz],
356 the new map is of size [2*mrg_x+nx,2*mrg_y+ny,2*mrg_z+nz].
357 The new map will consist of the values of the old map,
358 with a padded margin on all sides.
359
360 \param[in] mrg_x number of margin voxels to add on both right and
361 left on the X axis
362 \param[in] mrg_y number of margin voxels to add on both right and
363 left on the Y axis
364 \param[in] mrg_z number of margin voxels to add on both right and
365 left on the Z axis
366 \param[in] val ignored
367 \return the new padded map.
368 \exception UsageException if the input numbers of x/y/z voxels are
369 smaller than those currently in the map
370 */
371 DensityMap *pad_margin(int mrg_x, int mrg_y, int mrg_z, float val = 0.0);
372
373 //! Create and return a new cropped map
374 /** The margins are determined to be the bounding box
375 with density values below the input
376 \param[in] threshold used for cropping
377 \return the new cropped map.
378 */
379 DensityMap *get_cropped(float threshold);
380
381 //! Create and return a new cropped map based on particles
382 /** The map is cropped based on voxel proximity to a set of particles.
383 All voxel centers farther than [distance] away from any
384 particle center in [ps] will have their density value set to 0.0,
385 unless inverse is set to true in which case all voxel centers *within*
386 [distance] of a particle will be set to 0.0.
387
388 \param[in] ps List of particles used to crop map
389 \param[in] distance Distance from particles at which to crop map
390 \param[in] inverse If true, remove nearby density rather than distant
391 density
392 \param[in] keep_map_dimensions If false, crop the map to remove as many
393 voxels set to 0.0 as possible; otherwise, return a map the
394 same size as the input.
395 \return the new cropped map.
396 */
397 DensityMap *get_cropped(Particles ps, double distance, bool inverse=false,
398 bool keep_map_dimensions=false);
399
400 //! Create and return a new cropped map with the given bounding box extent
401 /** \param[in] bb the bounding box
402 \return the new cropped map.
403 \note If the input bounding box is larger than the density box,
404 it is snapped to the right size.
405 */
406 DensityMap *get_cropped(const algebra::BoundingBox3D &bb);
407
408 //! Get the maximum value in a XY plane indicated by a Z index
410 //! Get the maximum value in a XZ plane indicated by a Y index
412 //! Get the maximum value in a YZ plane indicated by a X index
414
415 //! Multiply in place each voxel in the map by the input factor
416 /** The result is kept in the map.
417 \param[in] factor the multiplication factor
418 */
419 void multiply(float factor);
420
421 //! Print the locations of all voxels with value above a given threshold
422 std::string get_locations_string(float t);
423
424 //! Update the voxel size of the map
425 void update_voxel_size(float new_apix);
426
427 //! Get the voxel size of the map
428 /** \note Use update_voxel_size() to set the spacing value.
429 */
430 Float get_spacing() const { return header_.get_spacing(); }
431 //! Calculates the coordinates that correspond to all voxels.
433 //! Copy map into this map
434 void copy_map(const DensityMap *other);
436#if !defined(IMP_DOXYGEN) && !defined(SWIG)
437 //! Convolute a kernel with a map and write results into current map
438 /**
439 \param[in] other the map to convolute. It must have the same dimensions
440 as the current map.
441 \param[in] kernel an array of kernel values. The data is in ZYX
442 order, Z is the slowest.
443 \param[in] dim_len the kernel array
444 */
445 void convolute_kernel(DensityMap *other, double *kernel, int dim_len);
446
447 //! Convolute a kernel with this map and write results to this map
448 /**
449 \param[in] kernel an array of kernel values. The data is in ZYX
450 order, Z is the slowest.
451 \param[in] dim_len the kernel array
452 */
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);
458 }
459#endif
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;
464
465 protected:
466 //!update the header values -- still in work
468 void reset_all_voxel2loc();
469
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);
473
474 DensityHeader header_; // holds all the info about the map
475 boost::scoped_array<double> data_; // the order is ZYX (Z-slowest)
476 bool data_allocated_;
477
478 //! Locations (centers) for each of the voxels of the map (they are
479 //! precomputed and each one is of size nvox, where nvox is the
480 //! size of the map)
481 boost::scoped_array<float> x_loc_, y_loc_, z_loc_;
482 //! true if the locations have already been computed
484
485 bool normalized_;
486 bool rms_calculated_;
487private:
488 friend class cereal::access;
489
490 template<class Archive> void serialize(Archive &ar) {
491 ar(cereal::base_class<Object>(this),
492 header_, data_allocated_, loc_calculated_, normalized_,
493 rms_calculated_);
494 long size = get_number_of_voxels();
495
496 if (std::is_base_of<cereal::detail::InputArchiveBase, Archive>::value) {
497 data_.reset(new double[size]);
498 if (loc_calculated_) {
499 // force recalculation of loc arrays
500 loc_calculated_ = false;
502 }
503 }
504
505 ar(cereal::binary_data(data_.get(), size * sizeof(double)));
506 }
507};
508
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>(
515 lb,
516 lb + algebra::Vector3D(m->get_spacing() * h->get_nx(),
517 m->get_spacing() * h->get_ny(),
518 m->get_spacing() * h->get_nz()));
519}
520
521#if !defined(IMP_DOXYGEN) && !defined(SWIG)
522//! Calculate a bounding box around a 3D point within the EM grid
523/**
524\param[in] d_map the density map
525\param[in] x coordinates of the point to sample around
526\param[in] y coordinates of the point to sample around
527\param[in] z coordinates of the point to sample around
528\param[in] kdist the length of the box
529\param[out] iminx the minimum index on the X axis of the output bounding box
530\param[out] iminy the minimum index on the Y axis of the output bounding box
531\param[out] iminz the minimum index on the Z axis of the output bounding box
532\param[out] imaxx the maximum index on the X axis of the output bounding box
533\param[out] imaxy the maximum index on the Y axis of the output bounding box
534\param[out] imaxz the maximum index on the Z axis of the output bounding box
535 */
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());
547}
548#endif
549
551
552//! Get density value at point v, interpolating linearly from the sample values.
553/** The resulting function is C0 over R3.
554 \relates DensityMap
555*/
556IMPEMEXPORT double get_density(const DensityMap *m, const algebra::Vector3D &v);
557
558//! Return a new density map containing a rotated version of the old one.
559/** Only voxels whose value is above threshold are considered when
560 computing the bounding box of the new map (set IMP::em::get_bounding_box()).
561 \relates DensityMap
562*/
563IMPEMEXPORT DensityMap *get_transformed(const DensityMap *input,
565 double threshold);
566
567//! Return a new density map containing a rotated version of the old one.
568/** The dimension of the new map is the same as the old one.
569 \relates DensityMap
570*/
571IMPEMEXPORT DensityMap *get_transformed(DensityMap *input,
572 const algebra::Transformation3D &tr);
573
574//! Get a resampled version of the map.
575/** The spacing is multiplied by scaling, i.e. scaling values greater
576 than 1 increase the voxel size.
577 \relates DensityMap
578*/
579IMPEMEXPORT DensityMap *get_resampled(DensityMap *input, double scaling);
580
581//! Rotate a density map into another map
582/** Any existing contents of the `into` map are replaced. It is resized to
583 cover the same region of space as the source map (but keeps its own
584 spacing). Any density that is transformed outside of this region is lost,
585 and so is not recovered by applying the inverse transformation.
586
587 \param[in] source the map to transform
588 \param[in] tr transform the from density map by this transformation
589 \param[out] into the map to transform into
590 \param[in] calc_rms if true RMS is calculated on the transformed map
591 \relates DensityMap
592*/
593IMPEMEXPORT void get_transformed_into(const DensityMap *source,
595 DensityMap *into, bool calc_rms = true);
596IMPEMEXPORT void get_transformed_into2(const DensityMap *source,
598 DensityMap *into);
599
600inline bool get_interiors_intersect(const DensityMap *d1,
601 const DensityMap *d2) {
602 return get_interiors_intersect(get_bounding_box(d1), get_bounding_box(d2));
603}
604#if 0
605//! Get a histogram of density values
606/**
607\param[in] dmap the density map to analyze
608\param[in] threshold only add voxels with value above this threshold
609 to the histogram
610\param[in] num_bins the number of bins to have in the histogram
611 \see DensityMap
612*/
613// IMPEMEXPORT statistics::Histogram
614// get_density_histogram(const DensityMap *dmap, float threshold,int num_bins);
615#endif
616
617//! Get a segment of the map according to xyz indexes
618/** \note the output map will cover the region
619 [[nx_start,nx_end],[]ny_start,ny_end,[nz_start,nz_end]]
620 */
621IMPEMEXPORT DensityMap *get_segment(DensityMap *map_to_segment, int nx_start,
622 int nx_end, int ny_start, int ny_end,
623 int nz_start, int nz_end);
624
625//! Get a segment of the map covered by the input points
626IMPEMEXPORT DensityMap *get_segment(DensityMap *map_to_segment,
627 algebra::Vector3Ds vecs, float dist);
628//! Get a segment of the map covered by another map
629IMPEMEXPORT DensityMap *get_segment_by_masking(DensityMap *map_to_segment,
630 DensityMap *mask,
631 float mas_threshold);
632
633//! Return a map with 0 for all voxels below the threshold and 1 for those above
634/** \param[in] orig_map the map to binarize
635 \param[in] threshold values below the threshold are set to 0 and 1 otherwise
636 \param[in] reverse if true values above the threshold
637 are set to 0 and 1 otherwise
638 */
639IMPEMEXPORT DensityMap *binarize(DensityMap *orig_map, float threshold,
640 bool reverse = false);
641
642//! Return a map with 0 for all voxels below the threshold
643/** \param[in] orig_map the map to threshold
644 \param[in] threshold values below the threshold are set to 0 and
645 left unchanged otherwise
646 */
647IMPEMEXPORT DensityMap *get_threshold_map(const DensityMap *orig_map,
648 float threshold);
649
650//! Return a density map for which voxel i contains the result of m1[i]*m2[i].
651/** The function assumes m1 and m2 are of the same dimensions.
652 */
653IMPEMEXPORT DensityMap *multiply(const DensityMap *m1, const DensityMap *m2);
654
655//! Return a convolution between density maps m1 and m2.
656/** The function assumes m1 and m2 are of the same dimensions.
657 */
658IMPEMEXPORT double convolute(const DensityMap *m1, const DensityMap *m2);
659
660//! Return the sum of all voxels
661IMPEMEXPORT double get_sum(const DensityMap *m1);
662
663//! Return a map where each voxel is the maximum value from the input maps.
664/** \note all input maps should have the same extent
665 */
667
668//! Return a new map with an updated spacing
669/** Input data is interpolated accordingly.
670 */
671IMPEMEXPORT
672DensityMap *interpolate_map(DensityMap *in_map, double new_spacing);
673
674//! Return a dense grid containing the voxels of the passed density map
675/** The returned grid has the same bounding box as the map.
676 */
677IMPEMEXPORT
679 DensityMap *in_map);
680
681//! Create a density map from an arbitrary IMP::algebra::GridD
682template <class S, class V, class E>
688 std::abs(arg.get_unit_cell()[0] - arg.get_unit_cell()[1]) < .01,
689 "The passed grid does not seem to have cubic voxels");
691 algebra::get_bounding_box(arg), arg.get_unit_cell()[0]);
692 IMP_USAGE_CHECK(arg.get_number_of_voxels(0) ==
693 static_cast<unsigned int>(ret->get_header()->get_nx()),
694 "X voxels don't match");
695 IMP_USAGE_CHECK(arg.get_number_of_voxels(1) ==
696 static_cast<unsigned int>(ret->get_header()->get_ny()),
697 "Y voxels don't match");
698 IMP_USAGE_CHECK(arg.get_number_of_voxels(2) ==
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]);
704 }
705 return ret.release();
706}
707
708//! Return a density map with the values taken from the grid.
709/** \relates DensityMap
710 */
711IMPEMEXPORT
713
714//! Return a density map with the values taken from the grid.
715/** \relates DensityMap
716 */
717IMPEMEXPORT
719
720
721//! Return a binarized map with 1 for voxels that are internal in the input map
722/** \relates DensityMap
723 */
724IMPEMEXPORT
725DensityMap *get_binarized_interior(DensityMap *dmap);
726
727//! Rasterize the particles into an existing density map.
728/** \relates DensityMap
729 */
730IMPEMEXPORT
731void add_to_map(DensityMap *dm, const Particles &pis); // defined in
732 // SampledDensityMap.cpp
733
734IMPEM_END_NAMESPACE
735
736#endif /* IMPEM_DENSITY_MAP_H */
A bounding box in D dimensions.
Metadata for a density file.
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.
Simple 3D transformation class.
Simple 3D vector class.
Basic types used by IMP.
#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.
Definition Object.h:111
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.
Definition GridD.h:81
Simple 3D transformation class.
Class for handling density maps.
Definition DensityMap.h:95
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
Definition DensityMap.h:483
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.
Definition DensityMap.h:157
void std_normalize()
Normalize the density voxels according to standard deviation (stdv).
long get_number_of_voxels() const
Get the number of map voxels.
Definition DensityMap.h:330
long xyz_ind2voxel(int x, int y, int z) const
Calculate the voxel of a given xyz index.
Definition DensityMap.h:139
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.
Definition DensityMap.h:430
boost::scoped_array< float > x_loc_
Definition DensityMap.h:481
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.
Definition DensityMap.h:192
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.
Definition DensityMap.h:245
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.
Definition DensityMap.h:248
algebra::Vector3D get_location_by_voxel(long index) const
Calculate the location of a given voxel.
Definition DensityMap.h:170
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.
Definition log_macros.h:293
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
Definition DensityMap.h:550
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
Definition VectorD.h:409
Vector< Pointer< Particle > > Particles
A list of reference-counted Particle objects.
Definition base_types.h:138
double Float
Basic floating-point value (could be float, double...).
Definition types.h:19
VectorD< 3 > Vector3D
Definition VectorD.h:407
#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.
Definition Pointer.h:88
O * release()
Relinquish control of the raw pointer stored in the Pointer.
A dense grid of values.
#define IMP_NO_SWIG(x)
Hide the line when SWIG is compiled or parses it.
Definition swig_macros.h:18