/
volregrid.c
1140 lines (973 loc) · 40.2 KB
/
volregrid.c
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
/* volregrid.c */
/* */
/* Performs regridding on a series of input MINC volumes or raw input data */
/* */
/* Andrew Janke - a.janke@gmail.com */
/* Mark Griffin - mark.griffin@cmr.uq.edu.au */
/* Center for Magnetic Resonance */
/* The University of Queensland */
/* */
/* Copyright (C) 2003 Andrew Janke and Mark Griffin */
/* 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. */
/* */
/* You should have received a copy of the GNU General Public License */
/* along with this program; if not, write to the Free Software */
/* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
#include <math.h>
#include <float.h>
#include <sys/stat.h>
#include <ctype.h>
#include <minc.h>
#include <volume_io.h>
#include <ParseArgv.h>
#include <time_stamp.h>
#include <voxel_loop.h>
#include <gsl/gsl_sf_bessel.h>
#include "arb_path_io.h"
#include "minc_support.h"
#define SQR2(x) ((x)*(x))
#define SQR3(x) ((x)*(x)*(x))
#define DEF_BOOL -1
#define X_IDX 2
#define Y_IDX 1
#define Z_IDX 0
#define V_IDX 3
#define LARGE_INITIAL_WEIGHT DBL_MAX
/* permutation array for IDX's */
/* mapping world x(0), y(1) and z(2) to the correct index in the volume voxel order */
static int perm[3] = { X_IDX, Y_IDX, Z_IDX };
static char *std_dimorder[] = { MIzspace, MIyspace, MIxspace };
static char *std_dimorder_v[] = { MIzspace, MIyspace, MIxspace, MIvector_dimension };
/* enum for regridding types */
typedef enum {
UNSPECIFIED_FUNC = 0,
KAISERBESSEL_FUNC,
GAUSSIAN_FUNC,
NEAREST_FUNC,
LINEAR_FUNC
} Regrid_op;
/* function prototypes */
int read_config_file(char *filename, char *args[]);
int get_model_file_info(char *dst, char *key, char *nextArg);
void scale_volume(VIO_Volume * vol, double o_min, double o_max, double min, double max);
void regrid_point(VIO_Volume * totals, VIO_Volume * weights,
double x, double y, double z, int v_size, double *data_buf);
void regrid_loop(void *caller_data, long num_voxels,
int input_num_buffers, int input_vector_length,
double *input_data[],
int output_num_buffers, int output_vector_length,
double *output_data[], Loop_Info * loop_info);
void regrid_minc(char *in_fn, int buff_size,
VIO_Volume * totals, VIO_Volume * weights, int v_size,
double regrid_floor, double regrid_ceil);
void regrid_arb_path(char *coord_fn, char *data_fn, int buff_size,
VIO_Volume * totals, VIO_Volume * weights, int v_size,
double regrid_floor, double regrid_ceil);
void print_version_info(void);
/* argument variables and table */
static int verbose = FALSE;
static int clobber = FALSE;
static nc_type in_dtype = NC_FLOAT;
static int in_is_signed = FALSE;
static int max_buffer_size_in_kb = 4 * 1024;
static int vect_size = 1;
static char *weights_fn = NULL;
/* arb path variables */
static char *ap_coord_fn = NULL;
/* regridding options */
static double regrid_range[2] = { -DBL_MAX, DBL_MAX };
static double regrid_radius[3] = { 2.0, 2.0, 2.0 };
static Regrid_op regrid_type = GAUSSIAN_FUNC;
static double regrid_sigma[3] = { 1.0, 1.0, 1.0 };
/* output file parameters */
static char *out_config_fn = NULL;
static nc_type out_dtype = NC_UNSPECIFIED;
static int out_is_signed = DEF_BOOL;
static double out_range[2] = { -DBL_MAX, DBL_MAX };
Volume_Definition out_inf = {
3,
{MIxspace, MIyspace, MIzspace}, /* dimnames */
{Z_IDX, Y_IDX, X_IDX}, /* space to dim */
{X_IDX, Y_IDX, Z_IDX}, /* dim to space */
{100, 100, 100}, /* nelem */
{-50.0, -50.0, -50.0}, /* start */
{1.0, 1.0, 1.0}, /* step */
{{1.0, 0.0, 0.0}, /* direction cosines */
{0.0, 1.0, 0.0},
{0.0, 0.0, 1.0}},
{{1.0, 0.0, 0.0, -50.0}, /* voxel to world */
{0.0, 1.0, 0.0, -50.0},
{0.0, 0.0, 1.0, -50.0}}
};
static ArgvInfo argTable[] = {
{NULL, ARGV_HELP, (char *)NULL, (char *)NULL, "General options:"},
{"-version", ARGV_FUNC, (char *)print_version_info, (char *)NULL,
"print version info and exit"},
{"-verbose", ARGV_CONSTANT, (char *)TRUE, (char *)&verbose,
"Print out extra information."},
{"-clobber", ARGV_CONSTANT, (char *)TRUE, (char *)&clobber,
"Overwrite existing files."},
{"-max_buffer_size_in_kb", ARGV_INT, (char *)1, (char *)&max_buffer_size_in_kb,
"maximum size of internal buffers."},
{"-weights", ARGV_STRING, (char *)1, (char *)&weights_fn,
"<file.mnc> output weights to specified file"},
{NULL, ARGV_HELP, NULL, NULL, "\nRaw Infile Options"},
{"-byte", ARGV_CONSTANT, (char *)NC_BYTE, (char *)&in_dtype,
"Input data is byte data."},
{"-short", ARGV_CONSTANT, (char *)NC_SHORT, (char *)&in_dtype,
"Input data is short integer data."},
{"-int", ARGV_CONSTANT, (char *)NC_INT, (char *)&in_dtype,
"Input data is 32-bit integer"},
{"-float", ARGV_CONSTANT, (char *)NC_FLOAT, (char *)&in_dtype,
"Input data is single-precision data. (Default)"},
{"-double", ARGV_CONSTANT, (char *)NC_DOUBLE, (char *)&in_dtype,
"Input data is double-precision data."},
{"-signed", ARGV_CONSTANT, (char *)TRUE, (char *)&in_is_signed,
"Input data is signed integer data."},
{"-unsigned", ARGV_CONSTANT, (char *)FALSE, (char *)&in_is_signed,
"Input data is unsigned integer data. (Default)"},
{"-vector", ARGV_INT, (char *)1, (char *)&vect_size,
"Size of vector dimension of Input data."},
{NULL, ARGV_HELP, NULL, NULL, "\nOutfile Options"},
{"-outconfig", ARGV_STRING, (char *)1, (char *)&out_config_fn,
"Get the output geometry from the input filename (overrides args below)"},
{"-like", ARGV_FUNC, (char *)get_model_file_info, (char *)&out_inf,
"Specifies a model file for the output geometry."},
{"-obyte", ARGV_CONSTANT, (char *)NC_BYTE, (char *)&out_dtype,
"Write out byte data."},
{"-oshort", ARGV_CONSTANT, (char *)NC_SHORT, (char *)&out_dtype,
"Write out short integer data."},
{"-oint", ARGV_CONSTANT, (char *)NC_INT, (char *)&out_dtype,
"Write out 32-bit integer data"},
{"-ofloat", ARGV_CONSTANT, (char *)NC_FLOAT, (char *)&out_dtype,
"Write out single-precision data. (Default)"},
{"-odouble", ARGV_CONSTANT, (char *)NC_DOUBLE, (char *)&out_dtype,
"Write out double-precision data."},
{"-osigned", ARGV_CONSTANT, (char *)TRUE, (char *)&out_is_signed,
"Write signed integer data."},
{"-ounsigned", ARGV_CONSTANT, (char *)FALSE, (char *)&out_is_signed,
"Write unsigned integer data."},
{"-range", ARGV_FLOAT, (char *)2, (char *)out_range,
"Range to scale output values between (default = range of input data)."},
{"-xnelements", ARGV_INT, (char *)1, (char *)&out_inf.nelem[0],
"Number of samples in x dimension."},
{"-ynelements", ARGV_INT, (char *)1, (char *)&out_inf.nelem[1],
"Number of samples in y dimension."},
{"-znelements", ARGV_INT, (char *)1, (char *)&out_inf.nelem[2],
"Number of samples in z dimension."},
{"-xstart", ARGV_FLOAT, (char *)1, (char *)&out_inf.start[0],
"Starting coordinate for x dimension."},
{"-ystart", ARGV_FLOAT, (char *)1, (char *)&out_inf.start[1],
"Starting coordinate for y dimension."},
{"-zstart", ARGV_FLOAT, (char *)1, (char *)&out_inf.start[2],
"Starting coordinate for z dimension."},
{"-xstep", ARGV_FLOAT, (char *)1, (char *)&out_inf.step[0],
"Step size for x dimension."},
{"-ystep", ARGV_FLOAT, (char *)1, (char *)&out_inf.step[1],
"Step size for y dimension."},
{"-zstep", ARGV_FLOAT, (char *)1, (char *)&out_inf.step[2],
"Step size for z dimension."},
{"-xdircos", ARGV_FLOAT, (char *)3, (char *)out_inf.dircos[0],
"Direction cosines along the x dimension"},
{"-ydircos", ARGV_FLOAT, (char *)3, (char *)out_inf.dircos[1],
"Direction cosines along the y dimension"},
{"-zdircos", ARGV_FLOAT, (char *)3, (char *)out_inf.dircos[2],
"Direction cosines along the z dimension"},
{NULL, ARGV_HELP, NULL, NULL, "\nRegridding options"},
{"-regrid_floor", ARGV_FLOAT, (char *)1, (char *)®rid_range[0],
"Ignore input data below this value during regridding."},
{"-regrid_ceil", ARGV_FLOAT, (char *)1, (char *)®rid_range[1],
"Ignore input data above this value during regridding."},
{"-regrid_range", ARGV_FLOAT, (char *)2, (char *)regrid_range,
"Ignore input data outside the input range during regridding."},
{"-regrid_radius", ARGV_FLOAT, (char *)3, (char *)®rid_radius,
"Defines a 3d Window radius for regridding (in mm)."},
{"-kaiser_bessel", ARGV_CONSTANT, (char *)KAISERBESSEL_FUNC, (char *)®rid_type,
"Use a Kaiser-Bessel convolution kernel for the reconstruction."},
{"-gaussian", ARGV_CONSTANT, (char *)GAUSSIAN_FUNC, (char *)®rid_type,
"Use a Gaussian convolution kernel for the reconstruction. (Default)"},
{"-linear", ARGV_CONSTANT, (char *)LINEAR_FUNC, (char *)®rid_type,
"Use linear interpolation for the reconstruction."},
{"-nearest", ARGV_CONSTANT, (char *)NEAREST_FUNC, (char *)®rid_type,
"Use nearest neighbour reconstruction."},
{"-sigma", ARGV_FLOAT, (char *)3, (char *)®rid_sigma,
"3D sigma value for -gaussian and -kaiser_bessel functions"},
{NULL, ARGV_HELP, NULL, NULL, "\nArbitrary path Regridding options"},
{"-arb_path", ARGV_STRING, (char *)1, (char *)&ap_coord_fn,
"<file> Regrid data using an arbitrary path from the input file"},
{NULL, ARGV_HELP, NULL, NULL, ""},
{NULL, ARGV_END, NULL, NULL, NULL}
};
int main(int argc, char *argv[])
{
char **infiles;
int n_infiles;
char *out_fn;
char *history;
VIO_progress_struct progress;
VIO_Volume totals, weights;
int i, j, k, v;
double min, max;
double w_min, w_max;
long num_missed;
double weight, value;
double initial_weight;
VIO_Real dummy[3];
int sizes[MAX_VAR_DIMS];
double starts[MAX_VAR_DIMS];
double steps[MAX_VAR_DIMS];
long t = 0;
/* start the time counter */
current_realtime_seconds();
/* get the history string */
history = time_stamp(argc, argv);
/* get args */
if(ParseArgv(&argc, argv, argTable, 0) || (argc < 3)){
fprintf(stderr,
"\nUsage: %s [options] <in1.mnc> [<in2.mnc> [...]] <out.mnc>\n", argv[0]);
fprintf(stderr,
" %s [options] -arb_path pth.conf <infile.raw> <out.mnc>\n", argv[0]);
fprintf(stderr, " %s -help\n\n", argv[0]);
exit(EXIT_FAILURE);
}
/* get file names */
n_infiles = argc - 2;
infiles = (char **)malloc(sizeof(char *) * n_infiles);
for(i = 0; i < n_infiles; i++){
infiles[i] = argv[i + 1];
}
out_fn = argv[argc - 1];
/* check for infiles and outfile */
for(i = 0; i < n_infiles; i++){
if(!file_exists(infiles[i])){
fprintf(stderr, "%s: Couldn't find input file %s.\n\n", argv[0], infiles[i]);
exit(EXIT_FAILURE);
}
}
if(!clobber && file_exists(out_fn)){
fprintf(stderr, "%s: %s exists, -clobber to overwrite.\n\n", argv[0], out_fn);
exit(EXIT_FAILURE);
}
/* check for weights_fn if required */
if(weights_fn != NULL){
if(!clobber && file_exists(weights_fn)){
fprintf(stderr, "%s: %s exists, -clobber to overwrite.\n\n", argv[0],
weights_fn);
exit(EXIT_FAILURE);
}
}
/* set up parameters for reconstruction */
if(out_dtype == NC_UNSPECIFIED){
out_dtype = in_dtype;
}
if(out_is_signed == DEF_BOOL){
out_is_signed = in_is_signed;
}
/* check vector dimension size */
if(vect_size < 1){
fprintf(stderr, "%s: -vector (%d) must be 1 or greater.\n\n", argv[0], vect_size);
exit(EXIT_FAILURE);
}
/* check sigma */
if(regrid_sigma[0] <= 0 || regrid_sigma[1] <= 0 || regrid_sigma[2] <= 0 ){
fprintf(stderr, "%s: -sigma must be greater than 0\n\n", argv[0]);
exit(EXIT_FAILURE);
}
/* read in the output file config from a file is specified */
if(out_config_fn != NULL){
int ext_args_c;
char *ext_args[32]; /* max possible is 32 arguments */
ext_args_c = read_config_file(out_config_fn, ext_args);
if(ParseArgv(&ext_args_c, ext_args, argTable,
ARGV_DONT_SKIP_FIRST_ARG | ARGV_NO_LEFTOVERS | ARGV_NO_DEFAULTS)){
fprintf(stderr, "\nError in parameters in %s\n", out_config_fn);
exit(EXIT_FAILURE);
}
}
if(verbose){
fprintf_vol_def(stdout, &out_inf);
}
/* transpose the geometry arrays */
/* out_inf.*[] are in world xyz order, perm[] is the permutation
array to map world xyz to the right voxel order in the volume */
for(i = 0; i < WORLD_NDIMS; i++){
sizes[i] = out_inf.nelem[perm[i]]; /* sizes, starts, steps are in voxel volume order. */
starts[i] = out_inf.start[perm[i]];
steps[i] = out_inf.step[perm[i]];
}
sizes[WORLD_NDIMS] = vect_size;
/* create the totals volume */
totals = create_volume((vect_size > 1) ? 4 : 3,
(vect_size > 1) ? std_dimorder_v : std_dimorder,
out_dtype, out_is_signed, 0.0, 0.0);
set_volume_sizes(totals, sizes);
set_volume_starts(totals, starts);
set_volume_separations(totals, steps);
for(i = 0; i < WORLD_NDIMS; i++){
/* out_inf.dircos is in world x,y,z order, we have to use the perm array to
map each direction to the right voxel axis. */
set_volume_direction_cosine(totals, i, out_inf.dircos[perm[i]]);
}
alloc_volume_data(totals);
/* create the "weights" volume */
weights = create_volume(3, std_dimorder, out_dtype, out_is_signed, 0.0, 0.0);
set_volume_sizes(weights, sizes);
set_volume_starts(weights, starts);
set_volume_separations(weights, steps);
for(i = 0; i < WORLD_NDIMS; i++){
set_volume_direction_cosine(weights, i, out_inf.dircos[perm[i]]);
}
alloc_volume_data(weights);
/* down below in regrid_loop, Andrew makes a nasty direct reference to the
voxel_to_world transformation in the volume. This
transformation is not necessarily up to date, particularly when
non-default direction cosines are used. In volume_io, the
direction cosines are set and a FLAG is also set to indicate
that the voxel-to-world xform is not up to date. If the stanrd
volume_io general transform code is used, it checks internally
to see if the matrix is up to date, and if not it is recomputed.
So here, we'll (LC + MK) force an update by calling a general
transform. */
// convert_world_to_voxel(weights, (Real) 0, (Real) 0, (Real) 0, dummy);
// convert_world_to_voxel(totals, (Real) 0, (Real) 0, (Real) 0, dummy);
fprintf(stderr, "2Sizes: [%d:%d:%d] \n", sizes[perm[0]], sizes[perm[1]], sizes[perm[2]]);
/* initialize weights to be arbitray large value if using NEAREST */
/* volume interpolation else initialize all to zero */
if(regrid_type == NEAREST_FUNC && ap_coord_fn == NULL){
initial_weight = LARGE_INITIAL_WEIGHT;
}
else{
initial_weight = 0.0;
}
/* initialize weights and totals */
for(k = sizes[Z_IDX]; k--;){
for(j = sizes[Y_IDX]; j--;){
for(i = sizes[X_IDX]; i--;){
set_volume_real_value(weights, k, j, i, 0, 0, initial_weight);
for(v = vect_size; v--;){
set_volume_real_value(totals, k, j, i, v, 0, 0.0);
}
}
}
}
/* if regridding via an arbitrary path */
if(ap_coord_fn != NULL){
if(n_infiles > 1){
fprintf(stderr, "%s: arb_path only works for one input file (so far).\n\n",
argv[0]);
exit(EXIT_FAILURE);
}
/* print some pretty output */
if(verbose){
fprintf(stdout, " | Input data: %s\n", infiles[0]);
fprintf(stdout, " | Arb path: %s\n", ap_coord_fn);
fprintf(stdout, " | Output range: [%g:%g]\n", out_range[0], out_range[1]);
fprintf(stdout, " | Output file: %s\n", out_fn);
}
regrid_arb_path(ap_coord_fn, infiles[0], max_buffer_size_in_kb,
&totals, &weights, vect_size, regrid_range[0], regrid_range[1]);
}
/* else if regridding via a series of input minc file(s) */
else {
for(i = 0; i < n_infiles; i++){
if(verbose){
fprintf(stdout, " | Input file: %s\n", infiles[i]);
}
regrid_minc(infiles[i], max_buffer_size_in_kb,
&totals, &weights, vect_size, regrid_range[0], regrid_range[1]);
}
}
/* initialise min and max counters and divide totals/weights */
num_missed = 0;
min = get_volume_real_value(totals, 0, 0, 0, 0, 0);
max = get_volume_real_value(totals, 0, 0, 0, 0, 0);
w_min = get_volume_real_value(weights, 0, 0, 0, 0, 0);
w_max = get_volume_real_value(weights, 0, 0, 0, 0, 0);
initialize_progress_report(&progress, FALSE, out_inf.nelem[Z_IDX], "Dividing through");
for(i = sizes[perm[0]]; i--;){
for(j = sizes[perm[1]]; j--;){
for(k = sizes[perm[2]]; k--;){
weight = get_volume_real_value(weights, k, j, i, 0, 0);
if(weight < w_min){
w_min = weight;
}
else if(weight > w_max){
w_max = weight;
}
if(weight != 0){
for(v = vect_size; v--;){
value = get_volume_real_value(totals, k, j, i, v, 0) / weight;
if(value < min){
min = value;
}
else if(value > max){
max = value;
}
set_volume_real_value(totals, k, j, i, v, 0, value);
}
}
else {
num_missed++;
}
}
}
update_progress_report(&progress, k + 1);
}
terminate_progress_report(&progress);
/* set the volumes range */
if(verbose){
fprintf(stdout, " + data range: [%g:%g]\n", min, max);
fprintf(stdout, " + weight range: [%g:%g]\n", w_min, w_max);
}
set_volume_real_range(totals, min, max);
set_volume_real_range(weights, w_min, w_max);
if(num_missed > 0 && verbose){
int nvox;
nvox = out_inf.nelem[X_IDX] * out_inf.nelem[Y_IDX] * out_inf.nelem[Z_IDX];
fprintf(stdout,
"\n-regrid_radius possibly too small, no data in %ld/%d[%2.2f%%] voxels\n\n",
num_missed, nvox, ((float)num_missed / nvox * 100));
}
/* rescale data if required */
if(out_range[0] != -DBL_MAX && out_range[1] != DBL_MAX){
double o_min, o_max;
/* get the existing range */
get_volume_real_range(totals, &o_min, &o_max);
/* rescale it */
scale_volume(&totals, o_min, o_max, out_range[0], out_range[1]);
}
/* output the result */
if(verbose){
fprintf(stdout, " | Outputting %s...\n", out_fn);
}
if(output_volume(out_fn, out_dtype, out_is_signed,
0.0, 0.0, totals, history, NULL) != VIO_OK){
fprintf(stderr, "Problems outputing: %s\n\n", out_fn);
}
/* output weights volume if required */
if(weights_fn != NULL){
if(verbose){
fprintf(stdout, " | Outputting %s...\n", weights_fn);
}
if(output_volume(weights_fn, out_dtype, out_is_signed,
0.0, 0.0, weights, history, NULL) != VIO_OK){
fprintf(stderr, "Problems outputting: %s\n\n", weights_fn);
}
}
delete_volume(totals);
delete_volume(weights);
t = current_realtime_seconds();
printf("Total reconstruction time: %ld hours %ld minutes %ld seconds\n", t/3600, (t/60)%60, t%60);
return (EXIT_SUCCESS);
}
/* re-scale a volume between a new range */
/* rescaling is done based upon the formula: */
/* */
/* (max - min) */
/* x' = (x - o_min) * --------------- + min */
/* (o_max - o_min) */
/* */
/* or more succinctly (and speedily) */
/* */
/* x' = ax + b */
/* */
/* where */
/* (max - min) */
/* a = --------------- */
/* (o_max - o_min) */
/* */
/* b = min - (o_min * a) */
/* */
void scale_volume(VIO_Volume * vol, double o_min, double o_max, double min, double max)
{
double value, a, b;
int sizes[MAX_VAR_DIMS];
int i, j, k, v;
VIO_progress_struct progress;
get_volume_sizes(*vol, sizes);
/* rescale the volume */
a = (max - min) / (o_max - o_min);
b = min - (o_min * a);
initialize_progress_report(&progress, FALSE, sizes[Z_IDX], "Rescaling Volume");
for(k = sizes[Z_IDX]; k--;){
for(j = sizes[Y_IDX]; j--;){
for(i = sizes[X_IDX]; i--;){
for(v = sizes[V_IDX]; v--;){
value = (get_volume_real_value(*vol, k, j, i, v, 0) * a) + b;
set_volume_real_value(*vol, k, j, i, v, 0, value);
}
}
}
update_progress_report(&progress, k + 1);
}
terminate_progress_report(&progress);
if(verbose){
fprintf(stdout, " + rescaled data range: [%g:%g]\n", min, max);
}
set_volume_real_range(*vol, min, max);
}
/* struct for regriding using a minc volume */
typedef struct {
int file_ndims;
int space_to_dim[WORLD_NDIMS];
int dim_to_space[MAX_VAR_DIMS];
double voxel_to_world[WORLD_NDIMS][WORLD_NDIMS + 1];
double floor;
double ceil;
double *data_buf;
VIO_Volume *totals;
VIO_Volume *weights;
} Loop_Data;
/* voxel loop function for regrid_minc */
void regrid_loop(void *caller_data, long num_voxels,
int input_num_buffers, int input_vector_length,
double *input_data[],
int output_num_buffers, int output_vector_length,
double *output_data[], Loop_Info * loop_info)
{
long ivox;
long idx[MAX_VAR_DIMS];
int v, idim;
double voxel_coord[WORLD_NDIMS];
double world_coord[WORLD_NDIMS];
double value;
int valid;
/* get pointer to loop data */
Loop_Data *ld = (Loop_Data *) caller_data;
/* shut the compiler up - yes I _know_ I don't use these */
(void)input_num_buffers;
(void)output_num_buffers;
(void)output_vector_length;
(void)output_data;
/* for each (vector) voxel */
for(ivox = 0; ivox < num_voxels * (long)input_vector_length;
ivox += (long)input_vector_length){
/* figure out where we are in space */
get_info_voxel_index(loop_info, ivox, ld->file_ndims, idx);
/* convert voxel index to world co-ordinate */
for(idim = 0; idim < WORLD_NDIMS; idim++){
voxel_coord[idim] = idx[ld->space_to_dim[idim]];
}
transform_coord(&world_coord[0], ld->voxel_to_world, &voxel_coord[0]);
/* get the data */
valid = 0;
for(v = 0; v < input_vector_length; v++){
value = input_data[0][(ivox * (long)input_vector_length) + (long)v];
/* check if this point is valid */
if(value > ld->floor && value < ld->ceil){
valid = 1;
}
ld->data_buf[v] = value;
}
/* then regrid the point if valid */
if(valid){
regrid_point(ld->totals, ld->weights,
world_coord[0], world_coord[1], world_coord[2],
input_vector_length, ld->data_buf);
}
}
return;
}
/* regrid using a minc volume */
void regrid_minc(char *in_fn, int buffer_size,
VIO_Volume * totals, VIO_Volume * weights, int v_size,
double regrid_floor, double regrid_ceil)
{
Loop_Data ld;
Loop_Options *loop_opt;
int mincid;
/* Open the file to get some information */
mincid = miopen(in_fn, NC_NOWRITE);
ld.file_ndims = get_minc_ndims(mincid);
get_minc_spatial_dims(mincid, ld.space_to_dim, ld.dim_to_space);
get_minc_voxel_to_world(mincid, ld.voxel_to_world);
/* alloc space for data_buf */
ld.data_buf = (double *)malloc(sizeof(double) * v_size);
ld.floor = regrid_floor;
ld.ceil = regrid_ceil;
ld.totals = totals;
ld.weights = weights;
/* set up and do voxel_loop */
loop_opt = create_loop_options();
set_loop_first_input_mincid(loop_opt, mincid);
set_loop_verbose(loop_opt, verbose);
set_loop_buffer_size(loop_opt, (long)1024 * buffer_size);
voxel_loop(1, &in_fn, 0, NULL, NULL, loop_opt, regrid_loop, (void *)&ld);
free_loop_options(loop_opt);
/* tidy up */
free(ld.data_buf);
}
/* regrid a volume with an arbitrary path */
/* return resulting totals and weights volumes */
void regrid_arb_path(char *coord_fn, char *data_fn, int buff_size,
VIO_Volume * totals, VIO_Volume * weights, int v_size,
double regrid_floor, double regrid_ceil)
{
Coord_list coord_buf;
double *data_buf = NULL;
size_t data_buf_alloc_size = 0;
int sizes[MAX_VAR_DIMS];
int c, v;
int total_pts;
double value;
int valid;
/* global max-min counters */
double v_min, v_max;
double x_min, y_min, z_min;
double x_max, y_max, z_max;
/* loop max-min counters */
double l_v_min, l_v_max;
double l_x_min, l_y_min, l_z_min;
double l_x_max, l_y_max, l_z_max;
/* get volume info */
get_volume_sizes(*totals, sizes);
/* check for the config file */
if(!file_exists(coord_fn)){
fprintf(stderr, "Couldn't find config file %s.\n\n", coord_fn);
exit(EXIT_FAILURE);
}
/* initialise the parser with the config file */
if(!init_arb_path(coord_fn, data_fn)){
fprintf(stderr, "Failed to init arb_path, this isn't good\n");
exit(EXIT_FAILURE);
}
/* get some co-ordinates */
if(verbose){
fprintf(stdout, " + Doing arbitrary path (vector: %d)\n", v_size);
}
total_pts = 0;
v_min = DBL_MAX;
v_max = -DBL_MAX;
x_min = y_min = z_min = DBL_MAX;
x_max = y_max = z_max = -DBL_MAX;
coord_buf = get_some_arb_path_coords(buff_size);
while(coord_buf->n_pts != 0){
/* grow data_buf if we have to */
if(coord_buf->n_pts * v_size > data_buf_alloc_size){
data_buf_alloc_size = coord_buf->n_pts * v_size;
data_buf = realloc(data_buf, data_buf_alloc_size * sizeof(*data_buf));
}
/* get the data */
if(!get_some_arb_path_data
(data_buf, in_dtype, in_is_signed, coord_buf->n_pts, v_size)){
fprintf(stderr, "failed getting data\n");
exit(EXIT_FAILURE);
}
total_pts += coord_buf->n_pts;
if(verbose){
fprintf(stdout, " | %d co-ords total: %d ", coord_buf->n_pts, total_pts);
fflush(stdout);
}
/* regrid (do the nasty) */
l_v_min = DBL_MAX;
l_v_max = -DBL_MAX;
l_x_min = l_y_min = l_z_min = DBL_MAX;
l_x_max = l_y_max = l_z_max = -DBL_MAX;
for(c = 0; c < coord_buf->n_pts; c++){
/* check if this point is in range */
valid = 1;
for(v = 0; v < v_size; v++){
value = data_buf[(c * v_size) + v];
if(value < regrid_floor || value > regrid_ceil){
valid = 0;
}
else {
/* do range calculation */
if(verbose){
if(value > l_v_max){
l_v_max = value;
}
else if(value < l_v_min){
l_v_min = value;
}
}
}
// fprintf(stderr, "Value: %g valid %d\n", value, valid);
}
// fprintf(stderr, "VV %d [%g:%g:%g]\n", valid,
// coord_buf->pts[c].coord[0],
// coord_buf->pts[c].coord[1],
// coord_buf->pts[c].coord[2]
// );
if(valid){
regrid_point(totals, weights,
coord_buf->pts[c].coord[0],
coord_buf->pts[c].coord[1],
coord_buf->pts[c].coord[2], v_size, &data_buf[c * v_size]);
/* coord max-min storage */
if(verbose){
if(coord_buf->pts[c].coord[0] > l_x_max){
l_x_max = coord_buf->pts[c].coord[0];
}
else if(coord_buf->pts[c].coord[0] < l_x_min){
l_x_min = coord_buf->pts[c].coord[0];
}
if(coord_buf->pts[c].coord[1] > l_y_max){
l_y_max = coord_buf->pts[c].coord[1];
}
else if(coord_buf->pts[c].coord[1] < l_y_min){
l_y_min = coord_buf->pts[c].coord[1];
}
if(coord_buf->pts[c].coord[2] > l_z_max){
l_z_max = coord_buf->pts[c].coord[2];
}
else if(coord_buf->pts[c].coord[2] < l_z_min){
l_z_min = coord_buf->pts[c].coord[2];
}
}
}
}
if(verbose){
fprintf(stdout, " xyz [%4.1g:%4.1g:%4.1g] : [%4.1g:%4.1g:%4.1g] v: [%g:%g]\n",
l_x_min, l_y_min, l_z_min, l_x_max, l_y_max, l_z_max, l_v_min, l_v_max);
}
/* update global counters */
if(verbose){
if(l_v_min < v_min){
v_min = l_v_min;
}
if(l_v_max > v_max){
v_max = l_v_max;
}
if(l_x_min < x_min){
x_min = l_x_min;
}
if(l_y_min < y_min){
y_min = l_y_min;
}
if(l_z_min < z_min){
z_min = l_z_min;
}
if(l_x_max > x_max){
x_max = l_x_max;
}
if(l_y_max > y_max){
y_max = l_y_max;
}
if(l_z_max > z_max){
z_max = l_z_max;
}
}
/* get the next lot of co-ordinates */
coord_buf = get_some_arb_path_coords(buff_size);
}
if(verbose){
fprintf(stdout,
" | == global xyz [%4.1g:%4.1g:%4.1g] : [%4.1g:%4.1g:%4.1g] v: [%g:%g]\n",
x_min, y_min, z_min, x_max, y_max, z_max, v_min, v_max);
}
/* finish up */
end_arb_path();
}
/* regrid a point in a file using the input co-ordinate and data */
void regrid_point(VIO_Volume * totals, VIO_Volume * weights,
double x, double y, double z, int v_size, double *data_buf)
{
int sizes[MAX_VAR_DIMS];
VIO_Real steps[MAX_VAR_DIMS];
VIO_Real starts[MAX_VAR_DIMS];
int start_idx[3];
int stop_idx[3];
double value, weight;
double euc_dist;
double euc[3];
double c_pos[3];
int i, j, k, v;
double coord[3]; /* target point in mm coordinates, in X, Y, Z order */
VIO_Transform dircos, invdircos;
VIO_Vector vector;
VIO_Real dir[3];
/* the coord used below has to be in mm coordinates in the dircos space of the
target volume. Hence the manipulations with the vols direction_cosines */
make_identity_transform(&dircos);
get_volume_direction_cosine(*totals, perm[0], dir);
fill_Vector(vector, dir[0], dir[1], dir[2]);
set_transform_x_axis(&dircos, &vector);
get_volume_direction_cosine(*totals, perm[1], dir);
fill_Vector(vector, dir[0], dir[1], dir[2]);
set_transform_y_axis(&dircos, &vector);
get_volume_direction_cosine(*totals, perm[2], dir);
fill_Vector(vector, dir[0], dir[1], dir[2]);
set_transform_z_axis(&dircos, &vector);
for(i = 0; i < 4; i++){
for(j = 0; j < 4; j++){
Transform_elem(invdircos, i, j) = Transform_elem(dircos, j, i);
}
}
transform_point(&invdircos, x, y, z, &coord[0], &coord[1], &coord[2]);
get_volume_sizes(*totals, sizes); /* in volume voxel order, ie z,y,x with x fastest */
get_volume_separations(*totals, steps);
get_volume_starts(*totals, starts);
/* figure out the neighbouring voxels start and stop (in voxel co-ordinates) */
for(i = 0; i < 3; i++){ /* go through x, y and z */
start_idx[i] =
(int)rint((coord[i] - starts[perm[i]] - regrid_radius[i]) / steps[perm[i]]);
stop_idx[i] = start_idx[i] + rint((regrid_radius[i] * 2) / steps[perm[i]]);
/* flip if required */
if(start_idx[i] > stop_idx[i]){
value = start_idx[i];
start_idx[i] = stop_idx[i];
stop_idx[i] = value;
}
/* check that we aren't off the edge */
if(start_idx[i] < 0){
start_idx[i] = 0;
}
if(stop_idx[i] >= sizes[perm[i]]){
stop_idx[i] = sizes[perm[i]] - 1;
}
}
/* loop over the neighbours, getting euclidian distance */
c_pos[0] = starts[perm[0]] + (start_idx[0] * steps[perm[0]]);
for(i = start_idx[0]; i <= stop_idx[0]; i++){
euc[0] = fabs(c_pos[0] - coord[0]);
c_pos[1] = starts[perm[1]] + (start_idx[1] * steps[perm[1]]);
for(j = start_idx[1]; j <= stop_idx[1]; j++){
euc[1] = fabs(c_pos[1] - coord[1]);
c_pos[2] = starts[perm[2]] + (start_idx[2] * steps[perm[2]]);
for(k = start_idx[2]; k <= stop_idx[2]; k++){
euc[2] = fabs(c_pos[2] - coord[2]);
euc_dist = sqrt(SQR2(euc[0]) + SQR2(euc[1]) + SQR2(euc[2]));
if((regrid_radius[0] == 0 || euc[0] <= regrid_radius[0]) &&
(regrid_radius[1] == 0 || euc[1] <= regrid_radius[1]) &&
(regrid_radius[2] == 0 || euc[2] <= regrid_radius[2])){
/* calculate the weighting factor */
switch (regrid_type){
default:
fprintf(stderr, "Erk! unknown regrid_type. File: %s Line: %d\n",
__FILE__, __LINE__);
exit(EXIT_FAILURE);
break;
case NEAREST_FUNC:
case LINEAR_FUNC:
weight = euc_dist;
break;
case KAISERBESSEL_FUNC: