Bsoft  1.9
Bernard's software package
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
img_interpolate.cpp
Go to the documentation of this file.
1 
9 #include "img_interpolate.h"
10 #include "math_util.h"
11 #include "utilities.h"
12 
13 // Declaration of global variables
14 extern int verbose; // Level of output to the screen
15 
16 
41  Vector3<double> origin, Vector3<double> translate,
42  Matrix3 mat, int fill_type, double fill)
43 {
44  if ( !p->data_pointer() ) return -1;
45 
46  if ( p->compound_type() > TSimple ) {
47  error_show("img_transform", __FILE__, __LINE__);
48  cerr << "Error: Interpolation not done on compound data types" << endl;
49  return -1;
50  }
51 
52  if ( fabs(scale.x()) < 0.001 || p->sizeX() < 2 ) scale[0] = 1;
53  if ( fabs(scale.y()) < 0.001 || p->sizeY() < 2 ) scale[1] = 1;
54  if ( fabs(scale.z()) < 0.001 || p->sizeZ() < 2 ) scale[2] = 1;
55 
56  if ( nusize.x() < 1 ) nusize[0] = (int) (fabs(scale.x())*p->sizeX());
57  if ( nusize.y() < 1 ) nusize[1] = (int) (fabs(scale.y())*p->sizeY());
58  if ( nusize.z() < 1 ) nusize[2] = (int) (fabs(scale.z())*p->sizeZ());
59 
60  unsigned long i, c, n, x, y, z;
61  double oldx, oldy, oldz, newx, newy, newz;
62  Vector3<double> old, nu, oldorigin, nuorigin;
63  View view;
64  Matrix3 viewmat;
65  Matrix3 affmat = mat/scale;
66  Vector3<double> nusampling(p->sampling()/scale.abs()), nurealsize(nusampling*nusize);
67 
68  p->change_type(Float);
69 
70  oldorigin = origin;
71  nuorigin = origin + translate;
72 
73  if ( verbose & VERB_FULL ) {
74  cout << endl << "Geometric transformation:" << endl;
75  cout << "Transformation origin: " << oldorigin << endl;
76  cout << "New origin: " << nuorigin << endl;
77  cout << "Scales: " << scale << endl;
78  cout << "New size: " << nusize << " voxels" << endl;
79  cout << " " << nurealsize << " A" << endl;
80  cout << "Matrix:" << endl << mat;
81  cout << "Fill value: " << fill << endl << endl;
82  }
83 
84  affmat = affmat.transpose();
85 
86 // cout << mat << endl;
87 
88  unsigned long nudatasize = p->images()*nusize.x()*nusize.y()*nusize.z()*p->channels();
89  float* nudata = new float[nudatasize];
90  for ( i=0; i<nudatasize; i++ ) nudata[i] = 0;
91 
92  if ( fill_type == FILL_AVERAGE ) fill = p->average();
93 
94  // Note: the matrix is used in a back-calculation of old coordinates corresponding to new
95  for ( i=n=0; n<p->images(); n++ ) {
96  if ( fill_type == FILL_BACKGROUND ) fill = p->image[n].background();
97  for ( z=0; z<nusize.z(); z++ ) {
98  nu[2] = z - nuorigin.z();
99  for ( y=0; y<nusize.y(); y++ ) {
100  nu[1] = y - nuorigin.y();
101  for ( x=0; x<nusize.x(); x++ ) {
102  nu[0] = x - nuorigin.x();
103  old = affmat * nu + oldorigin;
104  for ( c=0; c<p->channels(); c++, i++ )
105  nudata[i] += p->interpolate(c, old, n, fill);
106  }
107  }
108  }
109  p->image[n].origin(nuorigin);
110  view = p->image[n].view();
111  viewmat = view.matrix();
112  viewmat = viewmat * mat.transpose();
113  view = View(viewmat);
114  p->image[n].view(view);
115  }
116 
117  if ( verbose & VERB_DEBUG )
118  cout << "DEBUG img_transform: Interpolation done" << endl;
119 
120  p->size(nusize);
121  p->page_size(nusize);
122  p->sampling(nusampling);
123 
124  UnitCell uc(p->unit_cell());
125  uc.size(nurealsize.x(), nurealsize.y(), nurealsize.z());
126  p->unit_cell(uc);
127 
128  p->data_assign((unsigned char *) nudata);
129 
130  p->statistics();
131 
132  if ( verbose & VERB_DEBUG )
133  cout << "DEBUG img_transform: all done" << endl;
134 
135  return 0;
136 }
137 
154 {
155  Vector3<float> start;
156  Vector3<float> scale(1,1,1), axis(0,0,1);
157  Vector3<int> size(p->size());
158  Matrix3 mat = Matrix3(axis, angle);
159  Bimage* p1;
160 
161  p->change_type(Float);
162 
163  unsigned long n;
164 
165  for ( n=0; n<p->images(); n++ ) {
166 // p1 = p->extract(n);
167 // img_transform(p1, size, scale, origin[n], shift[n], mat, FILL_BACKGROUND, 0);
168  p1 = p->transform(n, size, scale, origin[n], shift[n], mat, FILL_BACKGROUND, 0);
169  p->replace(n, p1);
170  delete p1;
171  }
172 
173  return n;
174 }
175 
176 
void change_type(char letter)
Get the data type indicated by a single letter code.
Definition: Bimage.cpp:1408
Type z()
Definition: Vector3.h:141
void sizeZ(size_t nz)
Definition: Bimage.h:320
int replace(Bimage *img)
Replaces the data with that from the given image.
Definition: Bimage.cpp:2367
Matrix3 transpose() const
Definition: Matrix3.h:249
#define FILL_BACKGROUND
Definition: utilities.h:141
void view(double x, double y, double z, double a)
Definition: Bimage.h:135
Header file for general utilities.
#define VERB_DEBUG
Definition: utilities.h:104
double background()
Definition: Bimage.h:126
CompoundType compound_type()
Definition: Bimage.h:295
int verbose
Definition: Bimage.h:59
#define FILL_AVERAGE
Definition: utilities.h:140
void unit_cell(UnitCell u)
Definition: Bimage.h:447
int img_transform(Bimage *p, Vector3< int > nusize, Vector3< double > scale, Vector3< double > origin, Vector3< double > translate, Matrix3 mat, int fill_type, double fill)
Transforms an image by translation, rotation, scaling and skewing.
int error_show(const char *message, const char *file, int line)
Displays the error with file and line reference.
Definition: utilities.cpp:652
double average(size_t cc, double xf, double yf, double zf, size_t nn, double iscale)
Averages in the xy plane when scaling is less than 1.
Definition: Bimage.cpp:1118
double interpolate(size_t cc, double xx, double yy, double zz, size_t nn, double fill) const
Interpolates using a given location.
Definition: Bimage.cpp:1155
size_t statistics()
Calculates the statistics for an image.
Definition: Bimage.h:70
General image parameter class.
Definition: Bimage.h:152
Vector3 abs()
Definition: Vector3.h:177
size_t channels()
Definition: Bimage.h:313
void sampling(double s)
Definition: Bimage.h:402
Library routines for interpolating images.
unsigned char * data_assign(unsigned char *nudata)
Assign image data.
Definition: Bimage.cpp:544
Type x()
Definition: Vector3.h:139
int img_unique_shift_global_rotate(Bimage *p, Vector3< float > *origin, Vector3< float > *shift, float angle)
Shifts and rotates each image as defined in individual shift vectors.
Vector3< size_t > size()
Definition: Bimage.h:315
void origin(double x, double y, double z)
Definition: Bimage.h:132
size_t images()
Definition: Bimage.h:400
unsigned char * data_pointer()
Definition: Bimage.h:229
void sizeX(size_t nx)
Definition: Bimage.h:318
Matrix3 matrix()
Definition: View.h:137
void sizeY(size_t ny)
Definition: Bimage.h:319
Definition: View.h:26
void size(double a, double b, double c)
Definition: UnitCell.h:35
Vector3< size_t > page_size()
Definition: Bimage.h:393
int transform(Vector3< double > scale, Vector3< double > origin, Vector3< double > translate, Matrix3 mat, int fill_type, double fill)
Transforms an image by translation, rotation, scaling and skewing, in place.
#define VERB_FULL
Definition: utilities.h:101
Type y()
Definition: Vector3.h:140
Bsub_image * image
Definition: Bimage.h:183
Header file for general utilities.