Bsoft  2.1.0
Bernard's software package
Bimage_transform.cpp
Go to the documentation of this file.
1 
9 #include "Bimage.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 
38  Vector3<double> origin, Vector3<double> translate,
39  Matrix3 mat, int fill_type, double fill)
40 {
41  long nn;
42  Bimage* p;
43 
44  for ( nn=0; nn<n; nn++ ) {
45  p = transform(nn, size(), scale, origin, translate, mat, fill_type, fill);
46  replace(nn, p);
47  delete p;
48  }
49 
50  return 0;
51 }
52 
76  Vector3<double> origin, Vector3<double> translate,
77  Matrix3 mat, int fill_type, double fill)
78 {
79  long nn;
80  Bimage* p;
81 
82  Bimage* pt = NULL;
83 
84  if ( n == 1 ) {
85  pt = transform(0, nusize, scale, origin, translate, mat, fill_type, fill);
86  } else {
87  pt = new Bimage(datatype, compoundtype, nusize, n);
88  for ( nn=0; nn<n; nn++ ) {
89  p = transform(nn, nusize, scale, origin, translate, mat, fill_type, fill);
90  pt->replace(nn, p);
91  pt->unit_cell(p->unit_cell());
92  delete p;
93  }
94  }
95 
96  for ( nn=0; nn<n; nn++ )
97  pt->image[nn].sampling(sampling(nn)/scale.abs());
98 
99  pt->statistics();
100 
101  return pt;
102 }
103 
104 void Bimage::transform_voxel(long i, Bimage* pt, long nn, Vector3<double> oldorigin,
105  Vector3<double> nuorigin, Matrix3 affmat, double fill)
106 {
107  Vector3<double> nu = pt->coordinates(i) - nuorigin;
108 
109  Vector3<double> old = affmat * nu + oldorigin;
110 
111  for ( long cc=0; cc<c; cc++, i++ )
112  pt->add(i, interpolate(cc, old, nn, fill));
113 }
114 
139  Vector3<double> origin, Vector3<double> translate,
140  Matrix3 mat, int fill_type, double fill)
141 {
142  if ( !d.uc ) return NULL;
143 
144  if ( compoundtype > TSimple ) {
145  error_show("Bimage::transform", __FILE__, __LINE__);
146  cerr << "Error: Interpolation not done on compound data types" << endl;
147  return NULL;
148  }
149 
150  if ( fabs(scale[0]) < 0.001 || x < 2 ) scale[0] = 1;
151  if ( fabs(scale[1]) < 0.001 || y < 2 ) scale[1] = 1;
152  if ( fabs(scale[2]) < 0.001 || z < 2 ) scale[2] = 1;
153 
154  if ( nusize[0] < 1 ) nusize[0] = (long) (fabs(scale[0])*x);
155  if ( nusize[1] < 1 ) nusize[1] = (long) (fabs(scale[1])*y);
156  if ( nusize[2] < 1 ) nusize[2] = (long) (fabs(scale[2])*z);
157  nusize = nusize.max(1);
158 
159  long i, cc, xx, yy, zz;
160  Vector3<double> old, nu, oldorigin, nuorigin;
161  View view;
162  Matrix3 viewmat;
163  Matrix3 affmat = mat/scale;
164  Vector3<double> nusampling(sampling(0)/scale.abs()), nurealsize(nusampling*nusize);
165 
166 // change_type(Float);
167 
168  oldorigin = origin;
169  nuorigin = origin + translate;
170 
171  if ( verbose & VERB_FULL ) {
172  cout << endl << "Geometric transformation:" << endl;
173  cout << "Transformation origin: " << oldorigin << endl;
174  cout << "New origin: " << nuorigin << endl;
175  cout << "Scales: " << scale << endl;
176  cout << "New size: " << nusize << " voxels" << endl;
177  cout << " " << nurealsize << " A" << endl;
178  cout << "Matrix:" << endl << mat;
179  cout << "Fill value: " << fill << endl << endl;
180  }
181 
182  affmat = affmat.transpose();
183 
184 // cout << affmat << endl;
185 // cout << "New size: " << nusize << " voxels" << endl;
186 
187  Bimage* pt = new Bimage(datatype, compoundtype, nusize, 1);
188  pt->sampling(nusampling);
189 
190  if ( fill_type == FILL_AVERAGE ) fill = avg;
191  if ( fill_type == FILL_BACKGROUND ) fill = image[nn].background();
192 
193  // Note: the matrix is used in a back-calculation of old coordinates corresponding to new
194  for ( i=zz=0; zz<pt->z; zz++ ) {
195  nu[2] = (double)zz - nuorigin[2];
196  for ( yy=0; yy<pt->y; yy++ ) {
197  nu[1] = (double)yy - nuorigin[1];
198  for ( xx=0; xx<pt->x; xx++ ) {
199  nu[0] = (double)xx - nuorigin[0];
200  old = affmat * nu + oldorigin;
201 // if ( old[1] < 0 )
202 // cout << old << endl;
203  for ( cc=0; cc<c; cc++, i++ )
204  pt->add(i, interpolate(cc, old, nn, fill));
205  }
206  }
207  }
208 /*
209 #ifdef HAVE_GCD
210  dispatch_apply(pt->data_size()/c, dispatch_get_global_queue(0, 0), ^(size_t i){
211  transform_voxel(i*c, pt, nn, oldorigin, nuorigin, affmat, fill);
212  });
213 #else
214 #pragma omp parallel for
215  for ( i=0; i<pt->data_size(); i+=c )
216  transform_voxel(i, pt, nn, oldorigin, nuorigin, affmat, fill);
217 #endif
218 */
219 
220  if ( verbose & VERB_DEBUG )
221  cout << "DEBUG Bimage::transform: view=" << image[nn].view() << endl;
222 
223  pt->image->origin(nuorigin);
224  view = image[nn].view();
225  viewmat = view.matrix();
226 // cout << viewmat << endl;
227  viewmat = viewmat * mat.transpose();
228 // cout << viewmat << endl;
229  view = View(viewmat);
230  pt->image->view(view);
231 
232  if ( verbose & VERB_DEBUG )
233  cout << "DEBUG Bimage::transform: view=" << view << endl;
234 
235  UnitCell uc(ucell);
236  uc.size(nurealsize[0], nurealsize[1], nurealsize[2]);
237  pt->unit_cell(uc);
238 
239  pt->statistics();
240 
241  if ( verbose & VERB_DEBUG )
242  cout << "DEBUG Bimage::transform: interpolation done" << endl;
243 
244  return pt;
245 }
246 
256 {
257  Vector3<double> scale(1,1,1);
258  Vector3<double> translate;
259 
260  Matrix3 mat = image->view().matrix();
261  mat = mat.transpose();
262 
263  return transform(scale, image->origin(), translate, mat, FILL_BACKGROUND, 0);
264 }
265 
274 int Bimage::rotate(double angle)
275 {
276  Vector3<double> axis(0,0,1);
277  Matrix3 mat = Matrix3(axis, angle);
278 
279  Vector3<double> scale(1,1,1);
280  Vector3<double> translate;
281 
282  return transform(scale, image->origin(), translate, mat, FILL_BACKGROUND, 0);
283 }
284 
294 int Bimage::rotate(Vector3<double> axis, double angle)
295 {
296  Matrix3 mat = Matrix3(axis, angle);
297 
298  Vector3<double> scale(1,1,1);
299  Vector3<double> translate;
300 
301  return transform(scale, image->origin(), translate, mat, FILL_BACKGROUND, 0);
302 }
303 
315 {
316  Vector3<double> translate;
317 
318  return rotate(translate, view);
319 }
320 
332 int Bimage::rotate(Vector3<double> translate, View view)
333 {
334  Vector3<double> scale(1,1,1);
335  Matrix3 mat = view.matrix();
336 
338 
339  return transform(scale, image->origin(), translate, mat, FILL_BACKGROUND, 0);
340 }
341 
352 {
353  Vector3<double> scale(1,1,1), translate;
354 
356 
357  return transform(scale, image->origin(), translate, mat, FILL_BACKGROUND, 0);
358 }
359 
372 {
373  Vector3<double> scale(1,1,1);
374 
376 
377  return transform(scale, image->origin(), translate, mat, FILL_BACKGROUND, 0);
378 }
379 
390 {
391  Vector3<double> scale(1,1,1);
392  Vector3<double> translate;
393 
394  Matrix3 mat = image->view().matrix();
395  mat = mat.transpose();
396 
397  return transform(nusize, scale, image->origin(), translate, mat, FILL_BACKGROUND, 0);
398 }
399 
409 Bimage* Bimage::rotate(Vector3<long> nusize, double angle)
410 {
411  Vector3<double> axis(0,0,1);
412  Matrix3 mat = Matrix3(axis, angle);
413 
414  Vector3<double> scale(1,1,1);
415  Vector3<double> translate;
416 
417  return transform(nusize, scale, image->origin(), translate, mat, FILL_BACKGROUND, 0);
418 }
419 
431 {
432  Matrix3 mat = Matrix3(axis, angle);
433 
434  Vector3<double> scale(1,1,1);
435  Vector3<double> translate;
436 
437  return transform(nusize, scale, image->origin(), translate, mat, FILL_BACKGROUND, 0);
438 }
439 
452 {
453  Vector3<double> translate;
454 
455  return rotate(nusize, translate, view);
456 }
457 
471 {
472  Vector3<double> scale(1,1,1);
473  Matrix3 mat = view.matrix();
474 
476 
477  return transform(nusize, scale, image->origin(), translate, mat, FILL_BACKGROUND, 0);
478 }
479 
491 {
492  Vector3<double> scale(1,1,1), translate;
493 
495 
496  return transform(nusize, scale, image->origin(), translate, mat, FILL_BACKGROUND, 0);
497 }
498 
513 {
514  p->rotate(image->origin() - origin, view);
515 
516  add(p);
517 
518  return 0;
519 }
520 
530 {
531  View* v;
532  long nviews(0);
533 
534  for ( v = views; v; v = v->next ) nviews++;
535 
536  if ( nviews < 1 ) return NULL;
537 
539 
540  Bimage* pori = copy(nviews);
541 
542  long nn(0);
543  Bimage* p;
544 
545  for ( v = views; v; v = v->next, ++nn ) {
546  p = rotate(size(), *v);
547  pori->replace(nn, p);
548  delete p;
549  pori->image[nn].view(*v);
550  pori->image[nn].origin(image->origin());
551  }
552 
553  return pori;
554 }
555 
564 {
565  Vector3<double> scale(1,1,1), shift;
566  Matrix3 mat(-1,0,0,0,-1,0,0,0,-1);
567  Bimage* p1;
568 
570 
571  long nn;
572 
573  for ( nn=0; nn<n; nn++ ) {
574  p1 = transform(nn, size(), scale, image[nn].origin(), shift, mat, FILL_BACKGROUND, 0);
575  replace(nn, p1);
576  delete p1;
577  }
578 
579  return nn;
580 }
581 
589 int Bimage::shift(Vector3<double> vec, int fill_type, double fill)
590 {
591  for ( long nn=0; nn<n; nn++ )
592  shift(nn, vec, fill_type, fill);
593 
594  return 0;
595 }
596 
605 int Bimage::shift(long nn, Vector3<double> vec, int fill_type, double fill)
606 {
607  if ( vec.length() == 0 ) return 0;
608 
609  if ( verbose & VERB_FULL )
610  cout << "Translate by: " << vec << endl;
611 
612  long i, j, xx, yy, zz, cc;
613  long imgsize = c*image_size();
614  vector<float> temp(imgsize);
615 
616  if ( fill_type == FILL_AVERAGE ) fill = avg;
617  if ( fill_type == FILL_BACKGROUND ) fill = background(nn);
618 
619  for ( j=zz=0; zz<z; zz++ ) {
620  for ( yy=0; yy<y; yy++ ) {
621  for ( xx=0; xx<x; xx++ ) {
622  for ( cc=0; cc<c; cc++, j++ ) {
623  temp[j] = interpolate(cc, xx-vec[0], yy-vec[1], zz-vec[2], nn, fill);
624  }
625  }
626  }
627  }
628 
629  for ( i=nn*imgsize, j=0; j<imgsize; i++, j++ ) set(i, temp[j]);
630 
631  image[nn].origin(image[nn].origin() + vec);
632 
633  return 0;
634 }
635 
642 {
643  for ( long nn=0; nn<n; nn++ )
644  shift_wrap(nn, vec);
645 
646  return 0;
647 }
648 
656 {
657  if ( vec.length() == 0 ) return 0;
658 
659  if ( verbose & VERB_FULL )
660  cout << "Translate by: " << vec << endl;
661 
662  long i, j, xx, yy, zz, cc;
663  long imgsize(c*image_size());
664  Vector3<double> loc;
665  vector<float> temp(imgsize);
666 
667 // cout << "start interpolating " << nn << endl;
668  for ( j=zz=0; zz<z; zz++ ) {
669  loc[2] = -vec[2] + zz;
670  for ( yy=0; yy<y; yy++ ) {
671  loc[1] = -vec[1] + yy;
672  for ( xx=0; xx<x; xx++ ) {
673  loc[0] = -vec[0] + xx;
674  for ( cc=0; cc<c; cc++, j++ )
675  temp[j] = interpolate_wrap(cc, loc, nn);
676  }
677  }
678  }
679 // cout << "done interpolating " << nn << endl;
680 
681  for ( i=nn*imgsize, j=0; j<imgsize; i++, j++ ) set(i, temp[j]);
682 
683  image[nn].origin(image[nn].origin() + vec);
684 
685  return 0;
686 }
687 
694 int Bimage::center(int fill_type, double fill)
695 {
697 
698  for ( long nn=0; nn<n; nn++ )
699  shift(nn, origin - image[nn].origin(), fill_type, fill);
700 
701  return 0;
702 }
703 
709 {
711 
712  for ( long nn=0; nn<n; nn++ )
713  shift_wrap(nn, origin - image[nn].origin());
714 
715  return 0;
716 }
717 
723 {
724  for ( long nn=0; nn<n; nn++ )
725  shift_wrap(nn, -image[nn].origin());
726 
727  return 0;
728 }
729 
736 {
737  if ( !pref ) return NULL;
738 
739  if ( image->origin().length() < 1 ) origin(size()/2);
740 
741 // double scale(sampling()[0]/pref->sampling()[0]);
742 // Vector3<double> vscale(scale, scale, scale);
743 // Vector3<double> origin(image->origin()), translate((pref->size() - size())/2);
744 // Matrix3 mat(1);
745 
746 // return transform(pref->size(), vscale, origin, translate, mat, FILL_BACKGROUND);
747  return fspace_resize(pref);
748 }
749 
759 Bimage* Bimage::scale_to_reference(Bimage* pref, Bimage* pmask, double scalemin,
760  double scalemax, double step)
761 {
762  if ( image->origin().length() < 1 ) origin(size()/2);
763 
764  double scale, scalebest(1), cc, ccbest(0);
765  Vector3<double> vscale, origin(image->origin()), translate((pref->size() - size())/2);
766  Matrix3 mat(1);
767  Bimage* pt;
768 
769  if ( verbose ) {
770  cout << "Finding the best scaling to the reference map:" << endl;
771  cout << "Scale range: " << scalemin << " - " << scalemax << endl;
772  cout << "Initial scale step size: " << step << endl << endl;
773  cout << "Scale\tSampling\tCC" << endl;
774  }
775 
776  while ( step > 0.0001 ) {
777  for ( scale=scalemin; scale<=scalemax+0.001; scale+=step ) {
778  vscale = Vector3<double>(scale, scale, scale);
779  pt = transform(pref->size(), vscale, origin, translate, mat, FILL_BACKGROUND);
780  cc = pt->correlate(pref, 0, pref->sizeX()/2, pmask);
781  if ( ccbest < cc ) {
782  ccbest = cc;
783  scalebest = scale;
784  }
785  cout << scale << tab << pt->sampling(0)[0] << tab << cc << endl;
786  delete pt;
787  }
788  scalemin = scalebest - 0.75*step;
789  scalemax = scalebest + 0.75*step;
790  step /= 2;
791  }
792 
793  if ( verbose ) {
794  cout << "Best scale: " << scalebest << " ("
795  << ccbest << ")" << endl;
796  cout << "Sampling correction: "
797  << pref->image->sampling()[0]*scalebest/image->sampling()[0] << endl << endl;
798  }
799 
800 // vscale = Vector3<double>(scalebest, scalebest, scalebest);
801 
802 // pt = transform(pref->size(), vscale, origin, translate, mat, FILL_BACKGROUND);
803 // pt->sampling(pref->sampling());
804 
805  sampling(pref->sampling(0) * scalebest);
806  pt = fspace_resize(pref);
807 
808  return pt;
809 }
810 
818 {
819  double step(pref->image->sampling()[0]/pref->x);
820  double scalemin(image->sampling()[0]/pref->image->sampling()[0] - 10*step);
821  double scalemax(image->sampling()[0]/pref->image->sampling()[0] + 10*step);
822 
823  return scale_to_reference(pref, pmask, scalemin, scalemax, step);
824 }
825 
Bimage::coordinates
void coordinates(long i, long &nx, long &ny, long &nz)
Definition: Bimage.h:429
Bimage::scale_to_same_size
Bimage * scale_to_same_size(Bimage *pref)
Scales and image to the given reference.
Definition: Bimage_transform.cpp:735
View::next
View * next
Definition: View.h:28
Bimage::mirror
int mirror()
Inverts/mirrors each image through its origin.
Definition: Bimage_transform.cpp:563
VERB_DEBUG
#define VERB_DEBUG
Definition: utilities.h:116
Bimage::shift
int shift(Vector3< double > vec, int fill_type=0, double fill=0)
Shifts an image.
Definition: Bimage_transform.cpp:589
Bimage::set
void set(long j, double v)
Sets a single value at the given index.
Definition: Bimage.cpp:1118
Matrix3::transpose
Matrix3 transpose() const
Definition: Matrix3.h:268
Bimage::Bimage
Bimage()
Initializes an image.
Definition: Bimage.cpp:43
Bimage::copy
Bimage * copy()
Copies the header information and data of an image into a new image structure.
Definition: Bimage.cpp:2702
Bimage::center
int center(int fill_type=0, double fill=0)
Centers an image.
Definition: Bimage_transform.cpp:694
Vector3< double >
Bimage::sizeX
void sizeX(long nx)
Definition: Bimage.h:390
Bimage::unit_cell
void unit_cell(UnitCell uc)
Definition: Bimage.h:608
TypePointer::uc
unsigned char * uc
Definition: Bimage.h:70
Bimage::scale_to_reference
Bimage * scale_to_reference(Bimage *pref, Bimage *pmask, double scalemin, double scalemax, double step)
Scales and image to the given reference, searching for the correct scale.
Definition: Bimage_transform.cpp:759
error_show
int error_show(string message, string file, int line)
Displays the error with file and line reference.
Definition: utilities.cpp:719
Bimage::background
double background(long nn)
Definition: Bimage.h:549
Bimage::size
Vector3< long > size() const
Definition: Bimage.h:386
UnitCell::size
void size(double a, double b, double c)
Definition: UnitCell.h:35
FILL_AVERAGE
#define FILL_AVERAGE
Definition: utilities.h:177
Bimage::transform
int transform(Vector3< double > scale, Vector3< double > origin, Vector3< double > translate, Matrix3 mat, int fill_type=0, double fill=0)
Transforms an image by translation, rotation, scaling and skewing, in place.
Definition: Bimage_transform.cpp:37
View
Definition: View.h:26
Bsub_image::view
void view(double x, double y, double z, double a)
Definition: Bimage.h:157
Bimage::shift_wrap
int shift_wrap(Vector3< double > vec)
Shifts an image.
Definition: Bimage_transform.cpp:641
Bimage::zero_origin
int zero_origin()
Shifts the origins to zero with wrapping.
Definition: Bimage_transform.cpp:722
Bimage::image
Bsub_image * image
Definition: Bimage.h:202
Bimage::rotate_and_add
int rotate_and_add(Bimage *p, Vector3< double > origin, View view)
Rotates an image to a specified view and adds it to another image.
Definition: Bimage_transform.cpp:512
Bsub_image::background
double background()
Definition: Bimage.h:125
Matrix3
Definition: Matrix3.h:18
Bsub_image::origin
void origin(double x, double y, double z)
Definition: Bimage.h:136
TSimple
@ TSimple
Definition: Bimage.h:59
Bimage::orient
Bimage * orient(View *views)
Calculates multiple copies oriented according to views.
Definition: Bimage_transform.cpp:529
Bimage::add
void add(long j, double v)
Definition: Bimage.h:321
verbose
int verbose
Bimage::correlate
double correlate(Bimage *p)
Calculates correlation coefficient between two images.
Definition: Bimage_correlate.cpp:36
Bimage::image_size
long image_size()
Definition: Bimage.h:290
Bimage::rotate
int rotate()
Rotates an image using parameters defined in the image in place.
Definition: Bimage_transform.cpp:255
Bimage
General image parameter class.
Definition: Bimage.h:177
Bimage::interpolate_wrap
double interpolate_wrap(long cc, double xx, double yy=0, double zz=0, long nn=0) const
Interpolates using a given location with wrapping.
Definition: Bimage.cpp:1524
Bimage::replace
int replace(Bimage *img)
Replaces the data with that from the given image.
Definition: Bimage.cpp:2830
Bimage.h
Header file for image class.
Bimage::center_wrap
int center_wrap()
Centers an image with wrapping.
Definition: Bimage_transform.cpp:708
utilities.h
Header file for general utilities.
tab
ostream & tab(ostream &out)
Definition: utilities.cpp:22
Bimage::interpolate
double interpolate(long cc, double xx, double yy=0, double zz=0, long nn=0, double fill=0) const
Interpolates using a given location.
Definition: Bimage.cpp:1414
Bimage::origin
void origin(double ox, double oy, double oz)
Definition: Bimage.h:570
Bimage::change_type
void change_type(char letter)
Get the data type indicated by a single letter code.
Definition: Bimage.cpp:1765
Bsub_image::sampling
void sampling(double x, double y, double z)
Definition: Bimage.h:127
Bimage::fill
void fill(double v)
Definition: Bimage.h:706
math_util.h
Header file for general utilities.
VERB_FULL
#define VERB_FULL
Definition: utilities.h:113
Bimage::sampling
vector< Vector3< double > > sampling()
Definition: Bimage.h:529
FILL_BACKGROUND
#define FILL_BACKGROUND
Definition: utilities.h:178
Bimage::sampling
void sampling(long nn, Vector3< double > u)
Definition: Bimage.h:514
UnitCell
Definition: UnitCell.h:22
Vector3::length
double length()
Definition: Vector3.h:222
Float
@ Float
Definition: utilities.h:161
Bimage::fspace_resize
int fspace_resize(double scale, double res_hi, double res_lo)
Resizes an image in frequency space to avoid interpolation.
Definition: Bimage_fspace.cpp:202
Bimage::view
void view(double vx, double vy, double vz, double va)
Definition: Bimage.h:596
Bimage::transform_voxel
void transform_voxel(long i, Bimage *pt, long nn, Vector3< double > oldorigin, Vector3< double > nuorigin, Matrix3 affmat, double fill)
Definition: Bimage_transform.cpp:104
Bimage::statistics
long statistics()
Calculates the statistics for an image.
Definition: Bimage_stats.cpp:22
Vector3::abs
Vector3 abs()
Definition: Vector3.h:201
Vector3::max
Type max()
Definition: Vector3.h:175