-
Notifications
You must be signed in to change notification settings - Fork 234
Expand file tree
/
Copy pathbackprojector.cpp
More file actions
2667 lines (2285 loc) · 84.5 KB
/
Copy pathbackprojector.cpp
File metadata and controls
2667 lines (2285 loc) · 84.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/***************************************************************************
*
* Author: "Sjors H.W. Scheres"
* MRC Laboratory of Molecular Biology
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* This complete copyright notice must be included in any revised version of the
* source code. Additional authorship citations may be added, but existing
* author citations must be preserved.
***************************************************************************/
/*
* backprojector.cpp
*
* Created on: 24 Aug 2010
* Author: scheres
*/
#include "src/backprojector.h"
#ifdef TIMING
#define RCTIC(timer,label) (timer.tic(label))
#define RCTOC(timer,label) (timer.toc(label))
#else
#define RCTIC(timer,label)
#define RCTOC(timer,label)
#endif
void BackProjector::initialiseDataAndWeight(int current_size)
{
initialiseData(current_size);
weight.resize(data);
}
void BackProjector::initZeros(int current_size)
{
initialiseDataAndWeight(current_size);
data.initZeros();
weight.initZeros();
}
void BackProjector::backproject2Dto3D(const MultidimArray<Complex > &f2d,
const Matrix2D<RFLOAT> &A,
const MultidimArray<RFLOAT> *Mweight,
RFLOAT r_ewald_sphere, bool is_positive_curvature,
Matrix2D<RFLOAT>* magMatrix)
{
RFLOAT m00, m10, m01, m11;
if (magMatrix != 0)
{
m00 = (*magMatrix)(0,0);
m10 = (*magMatrix)(1,0);
m01 = (*magMatrix)(0,1);
m11 = (*magMatrix)(1,1);
}
else
{
m00 = 1.0;
m10 = 0.0;
m01 = 0.0;
m11 = 1.0;
}
// Use the inverse matrix
Matrix2D<RFLOAT> Ainv;
Ainv = A.inv();
// Go from the 2D slice coordinates to the 3D coordinates
Ainv *= (RFLOAT)padding_factor; // take scaling into account directly
// max_r2 and min_r2_nn are defined in 3D-space
const int max_r2 = ROUND(r_max * padding_factor) * ROUND(r_max * padding_factor);
const int min_r2_nn = ROUND(r_min_nn * padding_factor) * ROUND(r_min_nn * padding_factor);
// precalculated coefficients for ellipse determination (see further down)
// first, make sure A contains 2D distortion (lowercase 2D, uppercase 3D):
const RFLOAT Am_Xx = Ainv(0,0) * m00 + Ainv(0,1) * m10;
const RFLOAT Am_Xy = Ainv(0,0) * m01 + Ainv(0,1) * m11;
const RFLOAT Am_Yx = Ainv(1,0) * m00 + Ainv(1,1) * m10;
const RFLOAT Am_Yy = Ainv(1,0) * m01 + Ainv(1,1) * m11;
const RFLOAT Am_Zx = Ainv(2,0) * m00 + Ainv(2,1) * m10;
const RFLOAT Am_Zy = Ainv(2,0) * m01 + Ainv(2,1) * m11;
// next, precompute (Am)^t Am into AtA:
const RFLOAT AtA_xx = Am_Xx * Am_Xx + Am_Yx * Am_Yx + Am_Zx * Am_Zx;
const RFLOAT AtA_xy = Am_Xx * Am_Xy + Am_Yx * Am_Yy + Am_Zx * Am_Zy;
const RFLOAT AtA_yy = Am_Xy * Am_Xy + Am_Yy * Am_Yy + Am_Zy * Am_Zy;
const RFLOAT AtA_xy2 = AtA_xy * AtA_xy;
//#define DEBUG_BACKP
#ifdef DEBUG_BACKP
std::cerr << " XSIZE(f2d)= "<< XSIZE(f2d) << std::endl;
std::cerr << " YSIZE(f2d)= "<< YSIZE(f2d) << std::endl;
std::cerr << " XSIZE(data)= "<< XSIZE(data) << std::endl;
std::cerr << " YSIZE(data)= "<< YSIZE(data) << std::endl;
std::cerr << " STARTINGX(data)= "<< STARTINGX(data) << std::endl;
std::cerr << " STARTINGY(data)= "<< STARTINGY(data) << std::endl;
std::cerr << " STARTINGZ(data)= "<< STARTINGZ(data) << std::endl;
std::cerr << " r_max= "<< r_max << std::endl;
std::cerr << " Ainv= " << Ainv << std::endl;
#endif
// precalculate inverse of Ewald sphere diameter
RFLOAT inv_diam_ewald = (r_ewald_sphere > 0.0)? 1.0 / (2.0 * r_ewald_sphere) : 0.0;
if (!is_positive_curvature)
{
inv_diam_ewald *= -1.0;
}
const int s = YSIZE(f2d);
const int sh = XSIZE(f2d);
for (int i = 0; i < s; i++)
{
int y, first_allowed_x;
if (i < sh)
{
y = i;
first_allowed_x = 0;
}
else
{
y = i - s;
// x == 0 plane is stored twice in the FFTW format. Don't set it twice in backprojection!
first_allowed_x = 1;
}
// Only iterate over the ellipse in the 2D-image corresponding to the sphere in 3D.
// Find the x-range inside that ellipse for every given y:
// |A*v|^2 <= R^2 (for v = (x,y)^t)
// = v^t A^t A v =: v^t AtA v
// <=>
// (AtA_xx) x^2 + (2 AtA_xy y) x + (AtA_yy y^2 - R^2) <= 0 (quadratic eq. in x)
// <=>
// x in [q - d, q + d],
// where: q := -AtA_xy y / AtA_xx,
// d := sqrt((AtA_xy y)^2 - AtA_xx (AtA_yy y^2 - R^2)) / AtA_xx
RFLOAT discr = AtA_xy2 * y * y - AtA_xx * (AtA_yy * y * y - max_r2);
if (discr < 0.0) continue; // no points inside ellipse for this y
RFLOAT d = sqrt(discr) / AtA_xx;
RFLOAT q = - AtA_xy * y / AtA_xx;
int first_x = CEIL(q - d);
int last_x = FLOOR(q + d);
if (first_x < first_allowed_x) first_x = first_allowed_x;
if (last_x > sh - 1) last_x = sh - 1;
for (int x = first_x; x <= last_x; x++)
{
// Get the value from the input image
Complex my_val = DIRECT_A2D_ELEM(f2d, i, x);
RFLOAT my_weight;
// Get the weight
if (Mweight != NULL)
{
my_weight = DIRECT_A2D_ELEM(*Mweight, i, x);
}
else
{
my_weight = 1.0;
}
if (my_weight <= 0.) continue;
/*
In our implementation, (x, y) are not scaled because:
x_on_ewald = x * r / sqrt(x * x + y * y + r * r)
= x / sqrt(1 + (x * x + y * y) / (r * r))
~ x * (1 - (x * x + y * y) / (2 * r * r) + O(1/r^4)) # binomial expansion
= x + O(1/r^2)
same for y_on_ewald
z_on_ewald = r - r * r / sqrt(x * x + y * y + r * r)
~ r - r * (1 - (x * x + y * y) / (2 * r * r) + O(1/r^4)) # binomial expansion
= (x * x + y * y) / (2 * r) + O(1/r^3)
The error is < 0.0005 reciprocal voxel even for extreme cases
like 200kV, 1500 A particle, 1 A / pix.
*/
// Get logical coordinates in the 3D map.
// Make sure that the Ewald sphere is spherical even under anisotropic mag
// by first undistorting (x,y) to obtain the true frequencies (xu,yu)
RFLOAT xu = m00 * x + m01 * y;
RFLOAT yu = m10 * x + m11 * y;
RFLOAT z_on_ewaldp = inv_diam_ewald * (xu * xu + yu * yu);
RFLOAT xp = Ainv(0,0) * xu + Ainv(0,1) * yu + Ainv(0,2) * z_on_ewaldp;
RFLOAT yp = Ainv(1,0) * xu + Ainv(1,1) * yu + Ainv(1,2) * z_on_ewaldp;
RFLOAT zp = Ainv(2,0) * xu + Ainv(2,1) * yu + Ainv(2,2) * z_on_ewaldp;
double r2_3D = xp*xp + yp*yp + zp*zp;
// redundant:
if (r2_3D > max_r2)
{
continue;
}
if (interpolator == TRILINEAR || r2_3D < min_r2_nn)
{
bool is_neg_x;
// Only asymmetric half is stored
if (xp < 0)
{
// Get complex conjugated hermitian symmetry pair
xp = -xp;
yp = -yp;
zp = -zp;
is_neg_x = true;
}
else
{
is_neg_x = false;
}
// Trilinear interpolation (with physical coords)
// Subtract STARTINGY and STARTINGZ to accelerate access to data (STARTINGX=0)
// In that way use DIRECT_A3D_ELEM, rather than A3D_ELEM
int x0 = FLOOR(xp);
RFLOAT fx = xp - x0;
int x1 = x0 + 1;
int y0 = FLOOR(yp);
RFLOAT fy = yp - y0;
y0 -= STARTINGY(data);
int y1 = y0 + 1;
int z0 = FLOOR(zp);
RFLOAT fz = zp - z0;
z0 -= STARTINGZ(data);
int z1 = z0 + 1;
if (x0 < 0 || x0+1 >= data.xdim
|| y0 < 0 || y0+1 >= data.ydim
|| z0 < 0 || z0+1 >= data.zdim)
{
continue;
}
RFLOAT mfx = 1. - fx;
RFLOAT mfy = 1. - fy;
RFLOAT mfz = 1. - fz;
RFLOAT dd000 = mfz * mfy * mfx;
RFLOAT dd001 = mfz * mfy * fx;
RFLOAT dd010 = mfz * fy * mfx;
RFLOAT dd011 = mfz * fy * fx;
RFLOAT dd100 = fz * mfy * mfx;
RFLOAT dd101 = fz * mfy * fx;
RFLOAT dd110 = fz * fy * mfx;
RFLOAT dd111 = fz * fy * fx;
if (is_neg_x)
{
my_val = conj(my_val);
}
// Store slice in 3D weighted sum
DIRECT_A3D_ELEM(data, z0, y0, x0) += dd000 * my_val;
DIRECT_A3D_ELEM(data, z0, y0, x1) += dd001 * my_val;
DIRECT_A3D_ELEM(data, z0, y1, x0) += dd010 * my_val;
DIRECT_A3D_ELEM(data, z0, y1, x1) += dd011 * my_val;
DIRECT_A3D_ELEM(data, z1, y0, x0) += dd100 * my_val;
DIRECT_A3D_ELEM(data, z1, y0, x1) += dd101 * my_val;
DIRECT_A3D_ELEM(data, z1, y1, x0) += dd110 * my_val;
DIRECT_A3D_ELEM(data, z1, y1, x1) += dd111 * my_val;
// Store corresponding weights
DIRECT_A3D_ELEM(weight, z0, y0, x0) += dd000 * my_weight;
DIRECT_A3D_ELEM(weight, z0, y0, x1) += dd001 * my_weight;
DIRECT_A3D_ELEM(weight, z0, y1, x0) += dd010 * my_weight;
DIRECT_A3D_ELEM(weight, z0, y1, x1) += dd011 * my_weight;
DIRECT_A3D_ELEM(weight, z1, y0, x0) += dd100 * my_weight;
DIRECT_A3D_ELEM(weight, z1, y0, x1) += dd101 * my_weight;
DIRECT_A3D_ELEM(weight, z1, y1, x0) += dd110 * my_weight;
DIRECT_A3D_ELEM(weight, z1, y1, x1) += dd111 * my_weight;
} // endif TRILINEAR
else if (interpolator == NEAREST_NEIGHBOUR )
{
int x0 = ROUND(xp);
int y0 = ROUND(yp);
int z0 = ROUND(zp);
bool is_neg_x;
if (x0 < 0)
{
// Get complex conjugated hermitian symmetry pair
x0 = -x0;
y0 = -y0;
z0 = -z0;
is_neg_x = true;
}
else
{
is_neg_x = false;
}
const int xr = x0 - STARTINGX(data);
const int yr = y0 - STARTINGY(data);
const int zr = z0 - STARTINGZ(data);
if (xr < 0 || xr >= data.xdim
|| yr < 0 || yr >= data.ydim
|| zr < 0 || zr >= data.zdim)
{
continue;
}
if (is_neg_x)
{
DIRECT_A3D_ELEM(data, zr, yr, xr) += conj(my_val);
DIRECT_A3D_ELEM(weight, zr, yr, xr) += my_weight;
}
else
{
DIRECT_A3D_ELEM(data, zr, yr, xr) += my_val;
DIRECT_A3D_ELEM(weight, zr, yr, xr) += my_weight;
}
} // endif NEAREST_NEIGHBOUR
else
{
REPORT_ERROR("FourierInterpolator::backproject%%ERROR: unrecognized interpolator ");
}
} // endif x-loop
} // endif y-loop
}
void BackProjector::backproject1Dto2D(const MultidimArray<Complex > &f1d,
const Matrix2D<RFLOAT> &A,
const MultidimArray<RFLOAT> *Mweight)
{
Matrix2D<RFLOAT> Ainv = A.inv();
Ainv *= (RFLOAT)padding_factor; // take scaling into account directly
const int r_max_src = XSIZE(f1d) - 1;
const int r_max_ref = r_max * padding_factor;
const int r_max_ref_2 = r_max_ref * r_max_ref;
// currently not used for some reason
//const int r_min_NN_ref_2 = r_min_nn * r_min_nn * padding_factor * padding_factor;
for (int x = 0; x <= r_max_src; x++)
{
RFLOAT my_weight;
if (Mweight != NULL)
{
my_weight = DIRECT_A1D_ELEM(*Mweight, x);
if (my_weight <= 0.) continue;
}
else
{
my_weight = 1.;
}
Complex my_val = DIRECT_A1D_ELEM(f1d, x);
// Get logical coordinates in the 3D map
RFLOAT xp = Ainv(0,0) * x;
RFLOAT yp = Ainv(1,0) * x;
const RFLOAT r_ref_2 = xp*xp + yp*yp;
if (r_ref_2 > r_max_ref_2) continue;
if (interpolator == TRILINEAR /* && r_ref_2 < r_min_NN_ref_2*/)
{
// Only asymmetric half is stored
const bool is_neg_x = xp < 0;
if (is_neg_x)
{
// Get complex conjugated hermitian symmetry pair
xp = -xp;
yp = -yp;
}
// Trilinear interpolation (with physical coords)
// Subtract STARTINGY to accelerate access to data (STARTINGX=0)
// In that way use DIRECT_A2D_ELEM, rather than A2D_ELEM
const int x0 = FLOOR(xp);
const RFLOAT fx = xp - x0;
const int x1 = x0 + 1;
int y0 = FLOOR(yp);
const RFLOAT fy = yp - y0;
y0 -= STARTINGY(data);
const int y1 = y0 + 1;
const RFLOAT mfx = 1. - fx;
const RFLOAT mfy = 1. - fy;
const RFLOAT dd00 = mfy * mfx;
const RFLOAT dd01 = mfy * fx;
const RFLOAT dd10 = fy * mfx;
const RFLOAT dd11 = fy * fx;
if (is_neg_x)
{
my_val = conj(my_val);
}
// Store slice in 3D weighted sum
DIRECT_A2D_ELEM(data, y0, x0) += dd00 * my_val;
DIRECT_A2D_ELEM(data, y0, x1) += dd01 * my_val;
DIRECT_A2D_ELEM(data, y1, x0) += dd10 * my_val;
DIRECT_A2D_ELEM(data, y1, x1) += dd11 * my_val;
// Store corresponding weights
DIRECT_A2D_ELEM(weight, y0, x0) += dd00 * my_weight;
DIRECT_A2D_ELEM(weight, y0, x1) += dd01 * my_weight;
DIRECT_A2D_ELEM(weight, y1, x0) += dd10 * my_weight;
DIRECT_A2D_ELEM(weight, y1, x1) += dd11 * my_weight;
} // endif TRILINEAR
else if (interpolator == NEAREST_NEIGHBOUR )
{
const int x0 = ROUND(xp);
const int y0 = ROUND(yp);
if (x0 < 0)
{
A2D_ELEM(data, -y0, -x0) += conj(my_val);
A2D_ELEM(weight, -y0, -x0) += my_weight;
}
else
{
A2D_ELEM(data, y0, x0) += my_val;
A2D_ELEM(weight, y0, x0) += my_weight;
}
} // endif NEAREST_NEIGHBOUR
else
{
REPORT_ERROR("FourierInterpolator::backproject1Dto2D%%ERROR: unrecognized interpolator ");
}
} // endif x-loop
}
void BackProjector::backrotate2D(const MultidimArray<Complex > &f2d,
const Matrix2D<RFLOAT> &A,
const MultidimArray<RFLOAT> *Mweight,
Matrix2D<RFLOAT>* magMatrix)
{
Matrix2D<RFLOAT> Ainv = A.inv();
Ainv *= (RFLOAT)padding_factor; // take scaling into account directly
RFLOAT m00, m10, m01, m11;
if (magMatrix != 0)
{
m00 = (*magMatrix)(0,0);
m10 = (*magMatrix)(1,0);
m01 = (*magMatrix)(0,1);
m11 = (*magMatrix)(1,1);
}
else
{
m00 = 1.0;
m10 = 0.0;
m01 = 0.0;
m11 = 1.0;
}
const int r_max_ref = r_max * padding_factor;
const int r_max_ref_2 = r_max_ref * r_max_ref;
int min_r2_nn = r_min_nn * r_min_nn * padding_factor * padding_factor;
// precalculated coefficients for ellipse determination (see further down)
// first, make sure A contains 2D distortion (lowercase 2D, uppercase 3D):
const RFLOAT Am_Xx = Ainv(0,0) * m00 + Ainv(0,1) * m10;
const RFLOAT Am_Xy = Ainv(0,0) * m01 + Ainv(0,1) * m11;
const RFLOAT Am_Yx = Ainv(1,0) * m00 + Ainv(1,1) * m10;
const RFLOAT Am_Yy = Ainv(1,0) * m01 + Ainv(1,1) * m11;
// next, precompute (Am)^t Am into AtA:
const RFLOAT AtA_xx = Am_Xx * Am_Xx + Am_Yx * Am_Yx;
const RFLOAT AtA_xy = Am_Xx * Am_Xy + Am_Yx * Am_Yy;
const RFLOAT AtA_yy = Am_Xy * Am_Xy + Am_Yy * Am_Yy;
const RFLOAT AtA_xy2 = AtA_xy * AtA_xy;
//#define DEBUG_BACKROTATE
#ifdef DEBUG_BACKROTATE
std::cerr << " XSIZE(f2d)= "<< XSIZE(f2d) << std::endl;
std::cerr << " YSIZE(f2d)= "<< YSIZE(f2d) << std::endl;
std::cerr << " XSIZE(data)= "<< XSIZE(data) << std::endl;
std::cerr << " YSIZE(data)= "<< YSIZE(data) << std::endl;
std::cerr << " STARTINGX(data)= "<< STARTINGX(data) << std::endl;
std::cerr << " STARTINGY(data)= "<< STARTINGY(data) << std::endl;
std::cerr << " STARTINGZ(data)= "<< STARTINGZ(data) << std::endl;
std::cerr << " max_r= "<< r_max << std::endl;
std::cerr << " Ainv= " << Ainv << std::endl;
#endif
const int s = YSIZE(f2d);
const int sh = XSIZE(f2d);
for (int i = 0; i < s; i++)
{
int y, first_allowed_x;
if (i < sh)
{
y = i;
first_allowed_x = 0;
}
else
{
y = i - s;
// x == 0 plane is stored twice in the FFTW format. Don't set it twice in backprojection!
first_allowed_x = 1;
}
// Only iterate over the ellipse in the 2D-image corresponding to the sphere in 3D.
// Find the x-range inside that ellipse for every given y:
// |A*v|^2 <= R^2 (for v = (x,y)^t)
// = v^t A^t A v =: v^t AtA v
// <=>
// (AtA_xx) x^2 + (2 AtA_xy y) x + (AtA_yy y^2 - R^2) <= 0 (quadratic eq. in x)
// <=>
// x in [q - d, q + d],
// where: q := -AtA_xy y / AtA_xx,
// d := sqrt((AtA_xy y)^2 - AtA_xx (AtA_yy y^2 - R^2)) / AtA_xx
RFLOAT discr = AtA_xy2 * y*y - AtA_xx * (AtA_yy * y*y - r_max_ref_2);
if (discr < 0.0) continue; // no points inside ellipse for this y
RFLOAT d = sqrt(discr) / AtA_xx;
RFLOAT q = - AtA_xy * y / AtA_xx;
int first_x = CEIL(q - d);
int last_x = FLOOR(q + d);
if (first_x < first_allowed_x) first_x = first_allowed_x;
if (last_x > sh - 1) last_x = sh - 1;
for (int x = first_x; x <= last_x; x++)
{
RFLOAT my_weight;
if (Mweight != NULL)
{
my_weight = DIRECT_A2D_ELEM(*Mweight, i, x);
if (my_weight <= 0.f) continue;
}
else
{
my_weight = 1.;
}
// Get the relevant value in the input image
Complex my_val = DIRECT_A2D_ELEM(f2d, i, x);
// Get logical coordinates in the 3D map
RFLOAT xu = m00 * x + m01 * y;
RFLOAT yu = m10 * x + m11 * y;
RFLOAT xp = Ainv(0,0) * xu + Ainv(0,1) * yu;
RFLOAT yp = Ainv(1,0) * xu + Ainv(1,1) * yu;
RFLOAT r_ref_2 = xp * xp + yp * yp;
if (interpolator == TRILINEAR || r_ref_2 < min_r2_nn)
{
const bool is_neg_x = xp < 0;
// Only asymmetric half is stored
if (is_neg_x)
{
// Get complex conjugated hermitian symmetry pair
xp = -xp;
yp = -yp;
}
// Trilinear interpolation (with physical coords)
// Subtract STARTINGY to accelerate access to data (STARTINGX=0)
// In that way use DIRECT_A2D_ELEM, rather than A2D_ELEM
const int x0 = FLOOR(xp);
const RFLOAT fx = xp - x0;
const int x1 = x0 + 1;
int y0 = FLOOR(yp);
const RFLOAT fy = yp - y0;
y0 -= STARTINGY(data);
const int y1 = y0 + 1;
const RFLOAT mfx = 1. - fx;
const RFLOAT mfy = 1. - fy;
const RFLOAT dd00 = mfy * mfx;
const RFLOAT dd01 = mfy * fx;
const RFLOAT dd10 = fy * mfx;
const RFLOAT dd11 = fy * fx;
if (is_neg_x)
{
my_val = conj(my_val);
}
// Store slice in 3D weighted sum
DIRECT_A2D_ELEM(data, y0, x0) += dd00 * my_val;
DIRECT_A2D_ELEM(data, y0, x1) += dd01 * my_val;
DIRECT_A2D_ELEM(data, y1, x0) += dd10 * my_val;
DIRECT_A2D_ELEM(data, y1, x1) += dd11 * my_val;
// Store corresponding weights
DIRECT_A2D_ELEM(weight, y0, x0) += dd00 * my_weight;
DIRECT_A2D_ELEM(weight, y0, x1) += dd01 * my_weight;
DIRECT_A2D_ELEM(weight, y1, x0) += dd10 * my_weight;
DIRECT_A2D_ELEM(weight, y1, x1) += dd11 * my_weight;
} // endif TRILINEAR
else if (interpolator == NEAREST_NEIGHBOUR )
{
const int x0 = ROUND(xp);
const int y0 = ROUND(yp);
if (x0 < 0)
{
A2D_ELEM(data, -y0, -x0) += conj(my_val);
A2D_ELEM(weight, -y0, -x0) += my_weight;
}
else
{
A2D_ELEM(data, y0, x0) += my_val;
A2D_ELEM(weight, y0, x0) += my_weight;
}
} // endif NEAREST_NEIGHBOUR
else
{
REPORT_ERROR("FourierInterpolator::backrotate2D%%ERROR: unrecognized interpolator ");
}
} // endif x-loop
} // endif y-loop
}
void BackProjector::backrotate3D(const MultidimArray<Complex > &f3d,
const Matrix2D<RFLOAT> &A,
const MultidimArray<RFLOAT> *Mweight)
{
// f3d should already be in the right size (ori_size,orihalfdim)
// AND the points outside max_r should already be zero.
Matrix2D<RFLOAT> Ainv = A.inv();
Ainv *= (RFLOAT)padding_factor; // take scaling into account directly
const int r_max_src = XSIZE(f3d) - 1;
const int r_max_src_2 = r_max_src * r_max_src;
const int r_max_ref = r_max * padding_factor;
const int r_max_ref_2 = r_max_ref * r_max_ref;
const int r_min_NN_ref_2 = r_min_nn * r_min_nn * padding_factor * padding_factor;
//#define DEBUG_BACKROTATE
#ifdef DEBUG_BACKROTATE
std::cerr << " XSIZE(f3d)= "<< XSIZE(f3d) << std::endl;
std::cerr << " YSIZE(f3d)= "<< YSIZE(f3d) << std::endl;
std::cerr << " XSIZE(data)= "<< XSIZE(data) << std::endl;
std::cerr << " YSIZE(data)= "<< YSIZE(data) << std::endl;
std::cerr << " STARTINGX(data)= "<< STARTINGX(data) << std::endl;
std::cerr << " STARTINGY(data)= "<< STARTINGY(data) << std::endl;
std::cerr << " STARTINGZ(data)= "<< STARTINGZ(data) << std::endl;
std::cerr << " max_r= "<< r_max << std::endl;
std::cerr << " Ainv= " << Ainv << std::endl;
#endif
for (int k = 0; k < ZSIZE(f3d); k++)
{
int z, x_min;
// Don't search beyond square with side max_r
if (k <= r_max_src)
{
z = k;
x_min = 0;
}
else
{
z = k - ZSIZE(f3d);
/// TODO: still check this better in the 3D case!!!
// x==0 (y,z)-plane is stored twice in the FFTW format. Don't set it twice in BACKPROJECTION!
x_min = 1;
}
int z2 = z * z;
for (int i = 0; i < YSIZE(f3d); i++)
{
int y = (i <= r_max_src)? i : i - YSIZE(f3d);
int y2 = y * y;
const RFLOAT yz2 = y2 + z2;
// avoid negative square root
if (yz2 > r_max_src_2) continue;
const int x_max = FLOOR(sqrt(r_max_src_2 - yz2));
for (int x = x_min; x <= x_max; x++)
{
// Get logical coordinates in the 3D map
RFLOAT xp = Ainv(0,0) * x + Ainv(0,1) * y + Ainv(0,2) * z;
RFLOAT yp = Ainv(1,0) * x + Ainv(1,1) * y + Ainv(1,2) * z;
RFLOAT zp = Ainv(2,0) * x + Ainv(2,1) * y + Ainv(2,2) * z;
const int r_ref_2 = xp*xp + yp*yp + zp*zp;
if (r_ref_2 > r_max_ref_2) continue;
RFLOAT my_weight;
// Get the weight
if (Mweight != NULL)
{
my_weight = DIRECT_A3D_ELEM(*Mweight, k, i, x);
if (my_weight <= 0.) continue;
}
else
{
my_weight = 1.;
}
Complex my_val = DIRECT_A3D_ELEM(f3d, k, i, x);
if (interpolator == TRILINEAR || r_ref_2 < r_min_NN_ref_2)
{
// Only asymmetric half is stored
bool is_neg_x = xp < 0;
if (is_neg_x)
{
// Get complex conjugated hermitian symmetry pair
xp = -xp;
yp = -yp;
zp = -zp;
}
// Trilinear interpolation (with physical coords)
// Subtract STARTINGY to accelerate access to data (STARTINGX=0)
// In that way use DIRECT_A3D_ELEM, rather than A3D_ELEM
const int x0 = FLOOR(xp);
const RFLOAT fx = xp - x0;
const int x1 = x0 + 1;
int y0 = FLOOR(yp);
const RFLOAT fy = yp - y0;
y0 -= STARTINGY(data);
const int y1 = y0 + 1;
int z0 = FLOOR(zp);
const RFLOAT fz = zp - z0;
z0 -= STARTINGZ(data);
const int z1 = z0 + 1;
const RFLOAT mfx = 1. - fx;
const RFLOAT mfy = 1. - fy;
const RFLOAT mfz = 1. - fz;
const RFLOAT dd000 = mfz * mfy * mfx;
const RFLOAT dd001 = mfz * mfy * fx;
const RFLOAT dd010 = mfz * fy * mfx;
const RFLOAT dd011 = mfz * fy * fx;
const RFLOAT dd100 = fz * mfy * mfx;
const RFLOAT dd101 = fz * mfy * fx;
const RFLOAT dd110 = fz * fy * mfx;
const RFLOAT dd111 = fz * fy * fx;
if (is_neg_x)
{
my_val = conj(my_val);
}
// Store slice in 3D weighted sum
DIRECT_A3D_ELEM(data, z0, y0, x0) += dd000 * my_val;
DIRECT_A3D_ELEM(data, z0, y0, x1) += dd001 * my_val;
DIRECT_A3D_ELEM(data, z0, y1, x0) += dd010 * my_val;
DIRECT_A3D_ELEM(data, z0, y1, x1) += dd011 * my_val;
DIRECT_A3D_ELEM(data, z1, y0, x0) += dd100 * my_val;
DIRECT_A3D_ELEM(data, z1, y0, x1) += dd101 * my_val;
DIRECT_A3D_ELEM(data, z1, y1, x0) += dd110 * my_val;
DIRECT_A3D_ELEM(data, z1, y1, x1) += dd111 * my_val;
// Store corresponding weights
DIRECT_A3D_ELEM(weight, z0, y0, x0) += dd000 * my_weight;
DIRECT_A3D_ELEM(weight, z0, y0, x1) += dd001 * my_weight;
DIRECT_A3D_ELEM(weight, z0, y1, x0) += dd010 * my_weight;
DIRECT_A3D_ELEM(weight, z0, y1, x1) += dd011 * my_weight;
DIRECT_A3D_ELEM(weight, z1, y0, x0) += dd100 * my_weight;
DIRECT_A3D_ELEM(weight, z1, y0, x1) += dd101 * my_weight;
DIRECT_A3D_ELEM(weight, z1, y1, x0) += dd110 * my_weight;
DIRECT_A3D_ELEM(weight, z1, y1, x1) += dd111 * my_weight;
} // endif TRILINEAR
else if (interpolator == NEAREST_NEIGHBOUR )
{
const int x0 = ROUND(xp);
const int y0 = ROUND(yp);
const int z0 = ROUND(zp);
if (x0 < 0)
{
A3D_ELEM(data, -z0, -y0, -x0) += conj(my_val);
A3D_ELEM(weight, -z0, -y0, -x0) += my_weight;
}
else
{
A3D_ELEM(data, z0, y0, x0) += my_val;
A3D_ELEM(weight, z0, y0, x0) += my_weight;
}
} // endif NEAREST_NEIGHBOUR
else
{
REPORT_ERROR("BackProjector::backrotate3D%%ERROR: unrecognized interpolator ");
}
} // endif x-loop
} // endif y-loop
} // endif z-loop
}
void BackProjector::getLowResDataAndWeight(MultidimArray<Complex > &lowres_data, MultidimArray<RFLOAT> &lowres_weight,
int lowres_r_max)
{
const int lowres_r2_max = ROUND(padding_factor * lowres_r_max) * ROUND(padding_factor * lowres_r_max);
const int lowres_pad_size = 2 * (ROUND(padding_factor * lowres_r_max) + 1) + 1;
// Check lowres_r_max is not too big
if (lowres_r_max > r_max)
REPORT_ERROR("BackProjector::getLowResDataAndWeight%%ERROR: lowres_r_max is bigger than r_max");
// Initialize lowres_data and low_res_weight arrays
lowres_data.clear();
lowres_weight.clear();
if (ref_dim == 2)
{
lowres_data.resize(lowres_pad_size, lowres_pad_size / 2 + 1);
lowres_weight.resize(lowres_pad_size, lowres_pad_size / 2 + 1);
}
else
{
lowres_data.resize(lowres_pad_size, lowres_pad_size, lowres_pad_size / 2 + 1);
lowres_weight.resize(lowres_pad_size, lowres_pad_size, lowres_pad_size / 2 + 1);
}
lowres_data.setXmippOrigin();
lowres_data.xinit=0;
lowres_weight.setXmippOrigin();
lowres_weight.xinit=0;
// fill lowres arrays with relevant values
FOR_ALL_ELEMENTS_IN_ARRAY3D(lowres_data)
{
if (k*k + i*i + j*j <= lowres_r2_max)
{
A3D_ELEM(lowres_data, k, i, j) = A3D_ELEM(data, k , i, j);
A3D_ELEM(lowres_weight, k, i, j) = A3D_ELEM(weight, k , i, j);
}
}
}
void BackProjector::setLowResDataAndWeight(MultidimArray<Complex > &lowres_data, MultidimArray<RFLOAT> &lowres_weight,
int lowres_r_max)
{
const int lowres_r2_max = ROUND(padding_factor * lowres_r_max) * ROUND(padding_factor * lowres_r_max);
const int lowres_pad_size = 2 * (ROUND(padding_factor * lowres_r_max) + 1) + 1;
// Check lowres_r_max is not too big
if (lowres_r_max > r_max)
REPORT_ERROR("BackProjector::getLowResDataAndWeight%%ERROR: lowres_r_max is bigger than r_max");
// Check sizes of lowres_data and lowres_weight
if (YSIZE(lowres_data) != lowres_pad_size || XSIZE(lowres_data) != lowres_pad_size / 2 + 1 ||
(ref_dim ==3 && ZSIZE(lowres_data) != lowres_pad_size) )
REPORT_ERROR("BackProjector::setLowResDataAndWeight%%ERROR: lowres_data is not of expected size...");
if (YSIZE(lowres_weight) != lowres_pad_size || XSIZE(lowres_weight) != lowres_pad_size / 2 + 1 ||
(ref_dim ==3 && ZSIZE(lowres_weight) != lowres_pad_size) )
REPORT_ERROR("BackProjector::setLowResDataAndWeight%%ERROR: lowres_weight is not of expected size...");
// Re-set origin to the expected place
lowres_data.setXmippOrigin();
lowres_data.xinit=0;
lowres_weight.setXmippOrigin();
lowres_weight.xinit=0;
// Overwrite data and weight with the lowres arrays
FOR_ALL_ELEMENTS_IN_ARRAY3D(lowres_data)
{
if (k*k + i*i + j*j <= lowres_r2_max)
{
A3D_ELEM(data, k, i, j) = A3D_ELEM(lowres_data, k , i, j);
A3D_ELEM(weight, k, i, j) = A3D_ELEM(lowres_weight, k , i, j);
}
}
}
void BackProjector::getDownsampledAverage(MultidimArray<Complex>& avg, bool divide) const
{
MultidimArray<RFLOAT> down_weight;
// Pre-set down_data and down_weight sizes
const int down_size = 2 * (r_max + 1) + 1;
// Short side of data array
switch (ref_dim)
{
case 2:
avg.initZeros(down_size, down_size / 2 + 1);
break;
case 3:
avg.initZeros(down_size, down_size, down_size / 2 + 1);
break;
default:
REPORT_ERROR("BackProjector::getDownsampledAverage%%ERROR: Dimension of the data array should be 2 or 3");
}
// Set origin in the y.z-center, but on the left side for x.
avg.setXmippOrigin();
avg.xinit=0;
// Resize down_weight the same as down_data
down_weight.initZeros(avg);
// Now calculate the down-sized sum
int kp, ip, jp;
FOR_ALL_ELEMENTS_IN_ARRAY3D(data)
{
kp = ROUND((RFLOAT)k/padding_factor);
ip = ROUND((RFLOAT)i/padding_factor);
jp = ROUND((RFLOAT)j/padding_factor);
// TMP
//#define CHECK_SIZE
#ifdef CHECK_SIZE
if (kp > FINISHINGZ(avg) || ip > FINISHINGY(avg) || jp > FINISHINGX(avg) ||
kp < STARTINGZ(avg) || ip < STARTINGY(avg) || jp < STARTINGX(avg))
{
std::cerr << " kp= " << kp << " ip= " << ip << " jp= " << jp << std::endl;
avg.printShape();
REPORT_ERROR("BackProjector::getDownsampledAverage: indices out of range");
}
#endif
A3D_ELEM(avg, kp, ip, jp) += A3D_ELEM(data, k , i, j);
A3D_ELEM(down_weight, kp, ip, jp) += (divide? A3D_ELEM(weight, k , i, j) : 1.0);
}
// Calculate the straightforward average in the downsampled arrays
FOR_ALL_DIRECT_ELEMENTS_IN_MULTIDIMARRAY(avg)
{
if (DIRECT_MULTIDIM_ELEM(down_weight, n) > 0.)
{
DIRECT_MULTIDIM_ELEM(avg, n) /= DIRECT_MULTIDIM_ELEM(down_weight, n);
}
else
{
DIRECT_MULTIDIM_ELEM(avg, n) = 0.;
}
}
}
void BackProjector::calculateDownSampledFourierShellCorrelation(const MultidimArray<Complex>& avg1,
const MultidimArray<Complex>& avg2,
MultidimArray<RFLOAT>& fsc) const
{
if (!avg1.sameShape(avg2))
REPORT_ERROR("ERROR BackProjector::calculateDownSampledFourierShellCorrelation: two arrays have different sizes");