emdata.h

Go to the documentation of this file.
00001 
00005 /*
00006  * Author: Steven Ludtke, 04/10/2003 (sludtke@bcm.edu)
00007  * Copyright (c) 2000-2006 Baylor College of Medicine
00008  *
00009  * This software is issued under a joint BSD/GNU license. You may use the
00010  * source code in this file under either license. However, note that the
00011  * complete EMAN2 and SPARX software packages have some GPL dependencies,
00012  * so you are responsible for compliance with the licenses of these packages
00013  * if you opt to use BSD licensing. The warranty disclaimer below holds
00014  * in either instance.
00015  *
00016  * This complete copyright notice must be included in any revised version of the
00017  * source code. Additional authorship citations may be added, but existing
00018  * author citations must be preserved.
00019  *
00020  * This program is free software; you can redistribute it and/or modify
00021  * it under the terms of the GNU General Public License as published by
00022  * the Free Software Foundation; either version 2 of the License, or
00023  * (at your option) any later version.
00024  *
00025  * This program is distributed in the hope that it will be useful,
00026  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00027  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
00028  * GNU General Public License for more details.
00029  *
00030  * You should have received a copy of the GNU General Public License
00031  * along with this program; if not, write to the Free Software
00032  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
00033  *
00034  * */
00035 
00036 #ifndef eman__emdata_h__
00037 #define eman__emdata_h__ 1
00038 
00039 #ifdef _WIN32
00040         #pragma warning(disable:4819)
00041 #endif  //_WIN32
00042 
00043 #include <cfloat>
00044 #include <complex>
00045 #include <fstream>
00046 
00047 #include "sparx/fundamentals.h"
00048 #include "emutil.h"
00049 #include "util.h"
00050 #include "sparx/emarray.h"
00051 #include "geometry.h"
00052 #include "transform.h"
00053 #ifdef EMAN2_USING_CUDA
00054 #include <cuda_runtime_api.h>
00055 #include "cuda/cuda_util.h"
00056 #endif // EMAN2_USING_CUDA
00057 using std::string;
00058 using std::vector;
00059 using std::map;
00060 //using std::complex;   //comment this out for conflict with ACML
00061 using std::ostream;
00062 
00063 #include <utility>
00064 using std::pair;
00065 
00066 namespace EMAN
00067 {
00068         class ImageIO;
00069         class Ctf;
00070         class XYData;
00071         class Transform3D;
00072         class Transform;
00073         class GLUtil;
00074 
00075         typedef boost::multi_array_ref<float, 2> MArray2D;
00076         typedef boost::multi_array_ref<float, 3> MArray3D;
00077         typedef boost::multi_array_ref<std::complex<float>, 2> MCArray2D;
00078         typedef boost::multi_array_ref<std::complex<float>, 3> MCArray3D;
00079         typedef boost::multi_array<int, 2> MIArray2D;
00080         typedef boost::multi_array<int, 3> MIArray3D;
00081 
00088         class EMData
00089         {
00090                 friend class GLUtil;
00091 
00093                 #include "emdata_io.h"
00094 
00096                 #include "emdata_metadata.h"
00097 
00099                 #include "emdata_modular.h"
00100 
00102                 #include "emdata_transform.h"
00103 
00105                 #include "emdata_core.h"
00106 
00108                 #include "sparx/emdata_sparx.h"
00109 #ifdef EMAN2_USING_CUDA
00110 
00111                 #include "emdata_cuda.h"
00112 #endif // EMAN2_USING_CUDA
00113 
00114         static int totalalloc;
00115         public:
00116                 enum FFTPLACE { FFT_OUT_OF_PLACE, FFT_IN_PLACE };
00117                 enum WINDOWPLACE { WINDOW_OUT_OF_PLACE, WINDOW_IN_PLACE };
00118 
00120                 EMData();
00121                 ~ EMData();
00122 
00126                 explicit EMData(const string& filename, int image_index=0);
00127 
00134                 EMData(int nx, int ny, int nz=1, bool is_real=true);
00135 
00145                 EMData(float* data, const int nx, const int ny, const int nz, const Dict& attr_dict = Dict());
00146 
00152                 EMData(const EMData& that);
00153 
00159                 EMData& operator=(const EMData& that);
00160 
00161 
00170                 EMData *get_clip(const Region & area, const float fill = 0) const;
00171 
00178                 void clip_inplace(const Region & area,const float& fill_value=0);
00179 
00184                 EMData *get_top_half() const;
00185 
00186 
00195                 EMData *get_rotated_clip(const Transform & xform, const IntSize &size, float scale=1.0);
00196 
00207                 EMData* window_center(int l);
00208 
00209 
00224                 float *setup4slice(bool redo = true);
00225 
00226 
00230                 void scale(float scale_factor);
00231 
00232 
00238                 void translate(float dx, float dy, float dz);
00239 
00240 
00244                 void translate(const Vec3f &translation);
00245 
00246 
00253                 void translate(int dx, int dy, int dz);
00254 
00255 
00260                 void translate(const Vec3i &translation);
00261 
00262 
00267                 void rotate(const Transform3D & t);
00268 
00269 
00276                 void rotate(float az, float alt, float phi);
00277 
00278 
00283                 void rotate_translate(const Transform3D & t);
00284 
00288                 inline void transform(const Transform& t) {
00289                         ENTERFUNC;
00290                         process_inplace("math.transform",Dict("transform",(Transform*)(&t)));
00291                         //update(); no need, process_inplace did it
00292                         EXITFUNC;
00293                 }
00294 
00299                 inline void rotate_translate(const Transform & t) {
00300                         cout << "Deprecation warning. Please consider using EMData::transform() instead " << endl;
00301                         transform(t); }
00302 
00312                 void rotate_translate(float az, float alt, float phi, float dx, float dy, float dz);
00313 
00314 
00327                 void rotate_translate(float az, float alt, float phi, float dx, float dy,
00328                                                           float dz, float pdx, float pdy, float pdz);
00329 
00330 
00336                 void rotate_x(int dx);
00337 
00338 
00343                 inline void rotate_180() {
00344                         ENTERFUNC;
00345                         process_inplace("math.rotate.180",Dict());
00346                         EXITFUNC;
00347                 }
00348 
00349 
00363                 double dot_rotate_translate(EMData * with, float dx, float dy, float da,const bool mirror=false);
00364 
00365 
00376                 EMData *little_big_dot(EMData * little_img, bool do_sigma = false);
00377 
00378 
00391                 EMData *do_radon();
00392 
00393 
00409                 EMData *calc_ccf(EMData * with, fp_flag fpflag = CIRCULANT, bool center=false);
00410 
00423                 void zero_corner_circulant(const int radius = 0);
00424 
00443                 EMData *calc_ccfx( EMData * const with, int y0 = 0, int y1 = -1, bool nosum = false);
00444 
00445 
00454                 EMData *make_rotational_footprint(bool unwrap = true);
00455                 EMData *make_rotational_footprint_e1(bool unwrap = true);
00456                 EMData *make_rotational_footprint_cmc(bool unwrap = true);
00457 
00474                 EMData *make_footprint(int type=0);
00475 
00476 
00489                 EMData *calc_mutual_correlation(EMData * with, bool tocorner = false, EMData * filter = 0);
00490 
00491 
00507                 EMData *unwrap(int r1 = -1, int r2 = -1, int xs = -1, int dx = 0,
00508                                                            int dy = 0, bool do360 = false, bool weight_radial=true) const;
00509 
00510 
00518                 void apply_radial_func(float x0, float dx, vector < float >array, bool interp = true);
00519 
00520 
00532                 vector < float >calc_radial_dist(int n, float x0, float dx,bool inten);
00533 
00534 
00546                 vector < float >calc_radial_dist(int n, float x0, float dx, int nwedge, bool inten);
00547 
00548 
00552                 void cconj();
00553 
00554 
00562                 void add_incoherent(EMData * obj);
00563 
00564 
00576                 vector <float> calc_hist(int hist_size = 128, float hist_min = 0, float hist_max = 0, const float& brt = 0.0f, const float& cont = 1.0f);
00577 
00578 
00590                 vector<float> calc_az_dist(int n, float a0, float da, float rmin,
00591                                                                    float rmax);
00592 
00593 #if 0
00594                 void calc_rcf(EMData * with, vector < float >&sum_array);
00595 #endif
00596 
00609                 float calc_dist(EMData * second_img, int y_index = 0) const;
00610 
00627                 EMData *calc_flcf(EMData * with);
00628 
00647                 EMData *calc_fast_sigma_image( EMData* mask);
00648 
00654                 EMData *convolute(EMData * with);
00655 
00656 #if 0
00657                 void create_ctf_map(CtfMapType type, XYData * sf = 0);
00658 #endif
00659 
00660 
00682                 void common_lines(EMData * image1, EMData * image2, int mode = 0,
00683                                                   int steps = 180, bool horizontal = false);
00684 
00695                 void common_lines_real(EMData * image1, EMData * image2,
00696                                                            int steps = 180, bool horizontal = false);
00697 
00711                 void cut_slice(const EMData * const map, const Transform& tr, bool interpolate = true);
00712 
00713 #ifdef EMAN2_USING_CUDA
00714 
00723                 EMData* cut_slice_cuda(const Transform& tr);
00724 #endif // EMAN2_USING_CUDA
00725 
00739                 void uncut_slice(EMData * const map, const Transform& tr) const;
00740 
00744                 int getResolution() const {
00745                         int resolution = 0;
00746                         int num = 1;
00747                         while(num < get_xsize()) {
00748                                 resolution++;
00749                                 num = 1 << resolution;
00750                         }
00751 
00752                         return resolution;
00753                 }
00754 
00757                 void debug_print_parms()
00758                 {
00759                         std::cout << "Printing EMData params" << std::endl;
00760                         for ( Dict::const_iterator it = attr_dict.begin(); it != attr_dict.end(); ++it )
00761                         {
00762                                 std::cout << (it->first) << " " << (it->second).to_str() << std::endl;
00763                         }
00764                         std::cout << "Done printing EMData params" << std::endl;
00765                 }
00766 
00772                 void set_xyz_origin(float origin_x, float origin_y, float origin_z);
00773         private:
00780                 enum EMDataFlags {
00781 //                      EMDATA_COMPLEX = 1 << 1,
00782 //                      EMDATA_RI = 1 << 2,            // real/imaginary or amp/phase
00783                         EMDATA_BUSY = 1 << 3,      // someone is modifying data, NO LONGER USED
00784                         EMDATA_HASCTFF = 1 << 4,   // has CTF info in the image file
00785                         EMDATA_NEEDUPD = 1 << 5,   // needs a real update
00786 //                      EMDATA_COMPLEXX = 1 << 6,  // 1D fft's in X
00787                         EMDATA_FLIP = 1 << 7,      // is the image flipped
00788                         EMDATA_PAD = 1 << 8,       // is the image fft padded
00789                         EMDATA_FFTODD = 1 << 9,    // is the (real-space) nx odd
00790                         EMDATA_SHUFFLE = 1 << 10,  // fft been shuffled? (so O is centered) PRB
00791                         EMDATA_FH = 1 << 11,        // is the complex image a FH image
00792                         EMDATA_CPU_NEEDS_UPDATE = 1 << 12, // CUDA related: is the CPU version of the image out out data
00793                         EMDATA_GPU_NEEDS_UPDATE = 1 << 13, // CUDA related: is the GPU version of the image out out data
00794                         EMDATA_GPU_RO_NEEDS_UPDATE = 1 << 14 // // CUDA related: is the GPU RO version of the image out out data
00795                 };
00796 
00797                 void update_stat() const;
00798                 void save_byteorder_to_dict(ImageIO * imageio);
00799 
00800         private:
00802                 mutable Dict attr_dict;
00804                 mutable float *rdata;
00806                 float *supp;
00807 
00810                 //Ctf *ctf;
00811 
00813                 mutable int flags;
00814                 // Incremented every time the image changes
00815                 int changecount;
00817                 int nx, ny, nz, nxy;
00819                 int xoff, yoff, zoff;
00820 
00822                 Vec3f all_translation;
00823 //              Vec3f all_rotation;    /** rotation (az, alt, phi) from the original locaton*/
00824 
00825                 string path;
00826                 int pathnum;
00827 
00829                 mutable EMData* rot_fp;
00830 
00831                 // Clip inplace variables is a local class used from convenience in EMData::clip_inplace
00832                 // Added by d.woolford
00833                 class ClipInplaceVariables
00834                 {
00835                         public:
00836                                 ClipInplaceVariables(const int p_nx, const int p_ny, const int p_nz, const int n_nx, const int n_ny, const int n_nz,const int xtrans, const int ytrans, const int ztrans) :
00837                                         prv_nx(p_nx), prv_ny(p_ny), prv_nz(p_nz), new_nx(n_nx), new_ny(n_ny), new_nz(n_nz), xshift(xtrans), yshift(ytrans), zshift(ztrans),
00838                                  x_iter(prv_nx), y_iter(prv_ny), z_iter(prv_nz), new_z_top(0), new_z_bottom(0),  new_y_back(0), new_y_front(0),new_x_left(0), new_x_right(0),
00839                                 prv_z_top(0), prv_z_bottom(0), prv_y_back(0), prv_y_front(0), prv_x_left(0), prv_x_right(0)
00840                         {
00841                                 if ( xtrans > 0 ) x_iter -= xtrans;
00842                                 if ( x_iter < 0 ) x_iter = 0;
00843                                 if ( ytrans > 0 ) y_iter -= ytrans;
00844                                 if ( y_iter < 0 ) y_iter = 0;
00845                                 if ( ztrans > 0 ) z_iter -= ztrans;
00846                                 if ( z_iter < 0 ) z_iter = 0;
00847 
00848                                 // Get the depth in the new volume where slices are inserted
00849                                 // if this value is zero it means that the last z-slice in the new
00850                                 // volume contains image data
00851                                 if ( (new_nz + ztrans) > prv_nz ) new_z_top = new_nz + ztrans - prv_nz;
00852                                 if ( (new_ny + ytrans) > prv_ny ) new_y_back = new_ny + ytrans - prv_ny;
00853                                 if ( (new_nx + xtrans) > prv_nx ) new_x_right = new_nx + xtrans - prv_nx;
00854 
00855                                 if ( (new_nz + ztrans) < prv_nz )
00856                                 {
00857                                         prv_z_top = prv_nz - new_nz - ztrans;
00858                                         z_iter -= prv_z_top;
00859                                 }
00860                                 if ( (new_ny + ytrans) < prv_ny )
00861                                 {
00862                                         prv_y_back = prv_ny - new_ny - ytrans;
00863                                         y_iter -= prv_y_back;
00864                                 }
00865                                 if ( (new_nx + xtrans) < prv_nx )
00866                                 {
00867                                         prv_x_right = prv_nx - new_nx - xtrans;
00868                                         x_iter -= prv_x_right;
00869                                 }
00870 
00871                                 if ( xtrans > 0 ) prv_x_left = xtrans;
00872                                 if ( ytrans > 0 ) prv_y_front = ytrans;
00873                                 if ( ztrans > 0 ) prv_z_bottom = ztrans;
00874 
00875                                 if ( xtrans < 0 ) new_x_left = -xtrans;
00876                                 if ( ytrans < 0 ) new_y_front = -ytrans;
00877                                 if ( ztrans < 0 ) new_z_bottom = -ztrans;
00878 
00879                         }
00880                         ~ClipInplaceVariables() {}
00881 
00882                         int prv_nx, prv_ny, prv_nz, new_nx, new_ny, new_nz;
00883                         int xshift, yshift, zshift;
00884                         int x_iter, y_iter, z_iter;
00885                         int new_z_top, new_z_bottom, new_y_back, new_y_front, new_x_left, new_x_right;
00886                         int prv_z_top, prv_z_bottom,  prv_y_back, prv_y_front, prv_x_left, prv_x_right;
00887                 };
00888 
00889 
00890 
00891                 //              /**  Do the Fourier Harmonic Transform  PRB
00892                 //               * Takes a real image, returns the FH
00893                 //               * Sets the EMDATA_FH switch to indicate that it is an FH image
00894                 //               * @exception ImageFormatException If the image is not a square real odd image.
00895                 //               * @return the FH image.
00896                 //               */
00897                 //              EMData* do_FH();
00898 
00899                 //              /**   Do the Inverse Fourier Harmonic Transform   PRB
00900                 //               * Takes an FH image, returns a square  complex image with odd sides
00901                 //               * @exception ImageFormatException If the image is not the FH of something
00902                 //               * @return a square complex image with odd sides
00903                 //               */
00904                 //              EMData* do_FH2F();
00905 
00906 
00907 
00908                 //              /** Caclulates normalization and phase residual for a slice in
00909                 //               * an already existing volume. phase residual is calculated
00910                 //               * using only the inner 1/2 of the fourier sphere. Both the
00911                 //               * slice image and this image must be in complex image format.
00912                 //               *
00913                 //               * @param slice An slice image to be normalized.
00914                 //               * @param orient Orientation of the slice.
00915                 //               * @exception ImageFormatException If the images are not complex.
00916                 //               * @exception ImageDimensionException If the image is 3D.
00917                 //               * @return A float number pair (result, phase-residual).
00918                 //               */
00919                 //              FloatPoint normalize_slice(EMData * slice, const Transform3D & orient);
00920 
00921                 //              /** Caclulates normalization and phase residual for a slice in
00922                 //               * an already existing volume. phase residual is calculated
00923                 //               * using only the inner 1/2 of the fourier sphere. Both the
00924                 //               * slice image and this image must be in complex image format.
00925                 //               *
00926                 //               * @param slice An slice image to be normalized.
00927                 //               * @param alt Orientation euler angle alt (in EMAN convention).
00928                 //               * @param az  Orientation euler angle az  (in EMAN convention).
00929                 //               * @param phi Orientation euler angle phi (in EMAN convention).
00930                 //               * @exception ImageFormatException If the images are not complex.
00931                 //               * @exception ImageDimensionException If the image is 3D.
00932                 //               * @return A float number pair (result, phase-residual).
00933                 //               */
00934                 //              FloatPoint normalize_slice(EMData * slice, float az, float alt, float phi);
00935 
00936                 //              /** Get the normalization and phase residual values
00937                 //               * Used for normalizaton and error measurement when 2D slices are inserted into a 3D volume of Fourier pixels
00938                 //               * Originally added for use by the FourierReconstructor object
00939                 //               * @return the normalization const (pair.first) and the phase residual (pair.second)
00940                 //               * @param slice -the slice to be inserted into the 3D volume
00941                 //               * @param euler - the euler angle orientation of the slice
00942                 //               * @exception ImageDimensionException If this image is not 3D.ImageFormatException
00943                 //               * @exception ImageFormatException If this image is not complex
00944                 //               * @exception ImageFormatException If the slice not complex
00945                 //               */
00946                 //              pair<float, float> get_normalization_and_phaseres( const EMData* const slice, const Transform3D& euler );
00947 
00948                 //              /** cut a 2D slice out of a this 3D image and return it
00949                 //               * An alternative to cut_slice
00950                 //               * @param tr orientation of the slice as encapsulated in a Transform object
00951                 //               * @exception ImageDimensionException If this image is not 3D.
00952                 //               * @exception ImageFormatException If this image is complex
00953                 //               * @author David Woolford (adapted from an original version by Steve Ludtke)
00954                 //               * @date Feb 2009
00955                 //               */
00956                 //              EMData* get_cut_slice(const Transform& tr);
00957 
00958         };
00959 
00960 
00961         EMData * operator+(const EMData & em, float n);
00962         EMData * operator-(const EMData & em, float n);
00963         EMData * operator*(const EMData & em, float n);
00964         EMData * operator/(const EMData & em, float n);
00965 
00966         EMData * operator+(float n, const EMData & em);
00967         EMData * operator-(float n, const EMData & em);
00968         EMData * operator*(float n, const EMData & em);
00969         EMData * operator/(float n, const EMData & em);
00970 
00971         EMData * rsub(const EMData & em, float n);
00972         EMData * rdiv(const EMData & em, float n);
00973 
00974         EMData * operator+(const EMData & a, const EMData & b);
00975         EMData * operator-(const EMData & a, const EMData & b);
00976         EMData * operator*(const EMData & a, const EMData & b);
00977         EMData * operator/(const EMData & a, const EMData & b);
00978 
00979 
00980 /*   Next  is Modified by PRB      Transform3D::EMAN,
00981         inline Transform3D EMData::get_transform() const
00982         {
00983                 return Transform3D((float)attr_dict["euler_alt"],
00984                                    (float)attr_dict["euler_az"],
00985                                    (float)attr_dict["euler_phi"]);
00986         }
00987 */
00988 
00989 
00990 }
00991 
00992 
00993 #endif
00994 
00995 /* vim: set ts=4 noet nospell: */

Generated on Sat Nov 21 02:19:15 2009 for EMAN2 by  doxygen 1.5.6