00001
00002
00003
00004
00005
00006
00007
00008
00009 #ifndef IMPEM_VOLUME_H
00010 #define IMPEM_VOLUME_H
00011
00012 #include "em_config.h"
00013 #include "ImageHeader.h"
00014
00015 #include "IMP/algebra/Matrix3D.h"
00016 #include "IMP/algebra/Vector3D.h"
00017 #include <typeinfo>
00018 #include <complex>
00019
00020 IMPEM_BEGIN_NAMESPACE
00021
00022
00023 template <typename T>
00024 class Volume
00025 {
00026 public:
00027
00028 Volume() {
00029 data_.set_start(0,0);data_.set_start(1,0);data_.set_start(2,0);
00030 name_ = "";
00031 locations_calculated_ = false;
00032 normalized_ = false;
00033 rms_calculated_ = false;
00034 }
00035
00036
00037 Volume(int Zdim, int Ydim, int Xdim) {
00038 data_.resize(Zdim, Ydim, Xdim);
00039 data_.set_start(0,0);data_.set_start(1,0);data_.set_start(2,0);
00040 header_.set_header();
00041 locations_calculated_ = false;
00042 normalized_ = false;
00043 rms_calculated_ = false;
00044 }
00045
00046
00047 IMP::algebra::Matrix3D<T>& get_data() {
00048 return data_;
00049 }
00050
00051
00052 ImageHeader& get_header() {
00053 return header_;
00054 }
00055
00056
00057
00058 void adjust_header() {
00059 if (typeid(T) == typeid(std::complex< double >)) {
00060 header_.set_image_type(ImageHeader::VOL_FOURIER);
00061 } else if (typeid(T) == typeid(double) ||
00062 typeid(T) == typeid(float)) {
00063 header_.set_image_type(ImageHeader::VOL_IMPEM);
00064 }
00065 header_.set_number_of_slices(data_.get_number_of_slices());
00066 header_.set_number_of_rows(data_.get_number_of_rows());
00067 header_.set_number_of_columns(data_.get_number_of_columns());
00068 header_.set_time();
00069 header_.set_date();
00070 header_.set_title(name_);
00071
00072 header_.set_header();
00073 }
00074
00075
00076
00077
00078
00079
00080
00081
00082 void read(String filename,bool skip_type_check,
00083 bool force_reversed,bool skip_extra_checkings) {
00084 std::ifstream in;
00085 in.open(filename.c_str(), std::ios::in | std::ios::binary);
00086 header_.read(in,skip_type_check,force_reversed,skip_extra_checkings);
00087 data_.resize((int)header_.get_number_of_slices(),
00088 (int)header_.get_number_of_rows(),
00089 (int)header_.get_number_of_columns());
00090 data_.read_binary(in);
00091 in.close();
00092 }
00093
00094
00095 void write(String filename, bool force_reversed = false) {
00096 adjust_header();
00097 std::ofstream out;
00098 out.open(filename.c_str(), std::ios::out | std::ios::binary);
00099 header_.write(out, force_reversed);
00100 data_.write_binary(out);
00101 out.close();
00102 }
00103
00104
00105
00106
00107
00108 void reset_data(float const val) {
00109 for(int i=data_.get_start(0);i<= data_.get_finish(0);i++) {
00110 for(int j=data_.get_start(1);j<= data_.get_finish(1);j++) {
00111 for(int k=data_.get_start(2);k<= data_.get_finish(2);k++) {
00112 data_(i,j,k)=val;
00113 }
00114 }
00115 }
00116 }
00117
00118
00119
00120 void compute_locations() {
00121 if (locations_calculated_)
00122 return;
00123
00124 int nz = (int)data_.get_number_of_slices();
00125 int ny = (int)data_.get_number_of_rows();
00126 int nx = (int)data_.get_number_of_columns();
00127 float pixel_size = header_.get_object_pixel_size();
00128
00129 locations_.resize(nz, ny, nx);
00130
00131 for (int k = 0;k <= nz;k++) {
00132 for (int j = 0;j <= ny;j++) {
00133 for (int i = 0;i <= nx;i++) {
00134 IMP::algebra::VectorD<3> p;
00135 p[2] = i * pixel_size + header_.get_xorigin();
00136 p[1] = j * pixel_size + header_.get_yorigin();
00137 p[0] = k * pixel_size + header_.get_zorigin();
00138 locations_(k, j, i) = p;
00139 }
00140 }
00141 }
00142 locations_calculated_ = true;
00143 }
00144
00145
00146 long get_number_of_voxels() const {
00147 return data_.get_number_of_slices() *
00148 data_.get_number_of_rows() * data_.get_number_of_columns();
00149 }
00150
00151
00152
00153
00154
00155
00156
00157
00158 bool is_xyz_ind_part_of_volume(int i,int j,int k) const {
00159 std::vector<int> v(3);
00160 v[0]=i;v[1]=j;v[2]=k;
00161 return data_.is_physical_element(v);
00162 }
00163
00164
00165
00166
00167
00168
00169
00170
00171 bool is_part_of_volume(T x,T y,T z) const {
00172 float pixel_size = header_.get_object_pixel_size();
00173 IMP::algebra::VectorD<3> origin(header_.get_xorigin(),
00174 header_.get_yorigin(),
00175 header_.get_zorigin());
00176 IMP::algebra::VectorD<3> v((T)data_.get_number_of_columns(),
00177 (T)data_.get_number_of_rows(),
00178 (T)data_.get_number_of_slices());
00179 IMP::algebra::VectorD<3> end= origin + pixel_size * v;
00180 for (int i = 0;i<3;i++) {
00181 if(v[i]<origin[i] || end[i]<v[i]) {
00182 return false;
00183 }
00184 }
00185 return true;
00186 }
00187
00188
00189
00190 protected:
00191
00192 String name_;
00193
00194 IMP::algebra::Matrix3D<T> data_;
00195
00196 ImageHeader header_;
00197
00198 IMP::algebra::Matrix3D<IMP::algebra::VectorD<3> > locations_;
00199
00200 bool locations_calculated_;
00201
00202 bool normalized_;
00203
00204 bool rms_calculated_;
00205
00206 };
00207
00208 IMPEM_END_NAMESPACE
00209 #endif