-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathheat_equation.cxx
1010 lines (893 loc) · 40 KB
/
heat_equation.cxx
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
// example: ./HeatEquation -s 3 -q 3 -n -i 10 data/data_1wm_2ven_3gm.nii /tmp/ -t 4 3 0 2 100.99
// Requires lots of iterations >1000 until convergence
// Would benefit from low-res initialization and successive upsampling strategy
// Would benefit from multi-core/openMP/TBB
// Would benefit from switch between single point floating and double resolution
// Would benefit from implementation as an itk filter such as itk::GradientMagnitudeImageFilter
// compute unit normal and unit bi-normal vector to the unit tangent vector
// we can get faster if we only process the bounding box that contains the changing voxel
// can be calculated once and provided to oneStep as i,j,k,highi,highj,highk
// see http://citeseerx.ist.psu.edu/viewdoc/download?doi=10.1.1.136.6443&rep=rep1&type=pdf
// see https://www.ncbi.nlm.nih.gov/pmc/articles/PMC3068613/
#include "itkGradientImageFilter.h"
#include "itkGradientMagnitudeImageFilter.h"
#include "itkGradientRecursiveGaussianImageFilter.h"
#include "itkImage.h"
#include "itkImageFileReader.h"
#include "itkImageFileWriter.h"
#include "itkNearestNeighborInterpolateImageFunction.h"
#include "itkResampleImageFilter.h"
#include "json.hpp"
#include "metaCommand.h"
#include <boost/chrono.hpp>
#include <boost/chrono/chrono_io.hpp>
#include <boost/filesystem.hpp>
#include <iomanip>
#include <iostream>
#include <map>
#include <omp.h>
#define VERSION_MAJOR 0
#define VERSION_MINOR 0
#define VERSION_PATCH 10
using json = nlohmann::json;
using namespace boost::filesystem;
// save some stats in a result JSON file for provenance
json resultJSON;
// internal storage for the temperature field
std::vector<float> output; // output buffer
std::vector<float> tmpData;
std::vector<int> data; // labels
#define toindex(x, y, z) (size_t)(((z)*dims[0] * dims[1]) + ((y)*dims[0]) + (x))
constexpr unsigned int ImageDimension = 3;
using PixelType = unsigned short;
typedef itk::Image<unsigned short, ImageDimension> ImageType;
typedef itk::ImageFileReader<ImageType> ImageReaderType;
using OutputPixelType = float;
using OutputImageType = itk::Image<OutputPixelType, ImageDimension>;
typedef itk::ImageFileReader<OutputImageType> OutputReaderType;
typedef itk::CovariantVector<OutputPixelType, ImageDimension> GradientPixelType;
typedef itk::Image<GradientPixelType, ImageDimension> GradientImageType;
typedef itk::SmartPointer<GradientImageType> GradientImagePointer;
typedef itk::GradientRecursiveGaussianImageFilter<OutputImageType, GradientImageType> GradientImageFilterType;
typedef typename GradientImageFilterType::Pointer GradientImageFilterPointer;
typedef itk::GradientMagnitudeImageFilter<OutputImageType, OutputImageType> GradientMagnitudeImageFilterType;
typedef typename GradientMagnitudeImageFilterType::Pointer GradientMagnitudeImageFilterPointer;
// compute the magnitude of the gradient field
OutputImageType::Pointer computeMagGradField(OutputImageType::Pointer input) {
// compute the gradient field
GradientMagnitudeImageFilterPointer gmfilter = GradientMagnitudeImageFilterType::New();
gmfilter->SetInput(input);
// gmfilter->SetSigma(1.0f);
gmfilter->Update();
OutputImageType::Pointer gmimage = gmfilter->GetOutput();
return gmimage;
// We know what the max and min values for the gradient magnitude are. They are defined
// by the temperature values we have set in the input.
}
// perform one simulation step, assume that data ends up in output (uses tmpData as temp storage)
double oneStep(ImageType::SizeType dims, std::map<int, float> temperatureByMaterial, unsigned long *boundingBox) {
float omega = 0.1;
size_t count = 0;
bool zeroSpecified = false;
if (temperatureByMaterial.find(0) != temperatureByMaterial.end()) {
zeroSpecified = true;
}
// omp_set_num_threads(5);
// printf(" Number of processors available = %d\n", omp_get_num_procs());
// printf(" Number of threads = %d\n", omp_get_max_threads());
// printf(" Number of threads used = %d\n", omp_get_num_threads());
size_t ind111, ind101, ind121, ind011, ind211, ind110, ind112;
float val111, val101, val121, val011, val211, val110, val112;
float result;
int i, j, k;
int im = boundingBox[0];
int jm = boundingBox[1];
int km = boundingBox[2];
int highi = boundingBox[3];
int highj = boundingBox[4];
int highk = boundingBox[5];
if (im < 1)
im = 1;
if (jm < 1)
jm = 1;
if (km < 1)
km = 1;
if (highi > dims[0] - 1)
highi = dims[0] - 1;
if (highj > dims[1] - 1)
highj = dims[1] - 1;
if (highk > dims[2] - 1)
highk = dims[2] - 1;
#pragma omp parallel for collapse(3) private(i, j, count, ind111, ind101, ind121, ind011, ind211, ind110, ind112, val111, val101, val121, val011, val211, \
val110, val112, result) schedule(dynamic, 4)
for (k = km; k < highk; k++) {
for (j = jm; j < highj; j++) {
for (i = im; i < highi; i++) {
// ok what tissue type is this cell?
// we only care for either being Exterior or something else
count = toindex(i, j, k);
if (data[count] != 0 || zeroSpecified) { // do something (not exterior)
// what are the values at the stencil around the current location?
// 001 (101) 201
// (011) (111) (211)
// 021 (121) 221
// and one above and one below that
// 110, 112
result = 0.0f;
if (temperatureByMaterial.find(data[count]) != temperatureByMaterial.end()) { // if the temperature is set, do not change it
result = temperatureByMaterial[data[count]];
} else { // otherwise compute the new temperature by the stencil values
ind111 = count;
ind101 = toindex(i, j - 1, k);
ind121 = toindex(i, j + 1, k);
ind011 = toindex(i - 1, j, k);
ind211 = toindex(i + 1, j, k);
ind110 = toindex(i, j, k - 1);
ind112 = toindex(i, j, k + 1);
val111 = output[ind111];
val101 = output[ind101];
val121 = output[ind121];
val011 = output[ind011];
val211 = output[ind211];
val110 = output[ind110];
val112 = output[ind112];
// repulsive borders (to exterior)
if (data[ind101] == 0)
val101 = val121;
if (data[ind121] == 0)
val121 = val101;
if (data[ind011] == 0)
val011 = val211;
if (data[ind211] == 0)
val211 = val011;
if (data[ind110] == 0)
val110 = val112;
if (data[ind112] == 0)
val112 = val110;
result = (1.0 - 6.0 * omega) * val111 + omega * (val101 + val121 + val011 + val211 + val110 + val112);
// fprintf(stdout, "label %d = %f\n", data[count], result);
}
tmpData[(size_t)count] = result;
} else {
tmpData[(size_t)count] = 0.0f;
}
}
}
}
// calculate the (absolute) difference between the two fields - todo: use as convergence criteria
double diff = 0.0;
size_t c = output.size() - 1;
while (c >= 0) {
diff += fabs(output[c] - tmpData[c]);
if (c == 0) {
// underflow of c here!
break;
}
c = c - 1;
}
// copy the tmpData to the output
output = tmpData; // should copy the data after the iteration
// memcpy(output->lattice.dataPtr(), tmpData.dataPtr(), dims[0] * dims[1] * dims[2] * 4); // float
// copy
return diff;
}
std::string beautify_duration(boost::chrono::seconds input_seconds) {
using namespace boost::chrono;
typedef duration<int, boost::ratio<86400>> days;
auto d = duration_cast<days>(input_seconds);
input_seconds -= d;
auto h = duration_cast<hours>(input_seconds);
input_seconds -= h;
auto m = duration_cast<minutes>(input_seconds);
input_seconds -= m;
auto s = duration_cast<seconds>(input_seconds);
auto dc = d.count();
auto hc = h.count();
auto mc = m.count();
auto sc = s.count();
std::stringstream ss;
ss.fill('0');
if (dc) {
ss << d.count() << "d";
}
if (dc || hc) {
if (dc) {
ss << std::setw(2);
} // pad if second set of numbers
ss << h.count() << "h";
}
if (dc || hc || mc) {
if (dc || hc) {
ss << std::setw(2);
}
ss << m.count() << "m";
}
if (dc || hc || mc || sc) {
if (dc || hc || mc) {
ss << std::setw(2);
}
ss << s.count() << 's';
}
return ss.str();
}
int main(int argc, char *argv[]) {
itk::MultiThreaderBase::SetGlobalMaximumNumberOfThreads(4);
std::stringstream version_number;
version_number << VERSION_MAJOR << "." << VERSION_MINOR << "." << VERSION_PATCH;
const std::string VERSION_NO = version_number.str();
MetaCommand command;
command.SetAuthor("Hauke Bartsch");
command.SetVersion(VERSION_NO.c_str());
command.SetDescription("Simulation of the heat equation. Use as in: ./HeatEquation -s 2 -n -q 3 -i 2000 wm.seg.nii /tmp/ "
"-t 4 4 0 1 0.99. Specify the -t option at the end.");
command.SetCategory("MRI");
command.AddField("infile", "Input mask in nifti or other file format understood by itk", MetaCommand::STRING, true);
command.AddField("outdir", "Output directory", MetaCommand::STRING, true);
command.SetOption("Temperatures", "t", false,
"Specify the temperature per label as <N> [<label value> <temperature>], with "
"N the number of label and temperature values such as in '-t "
"4 0 0.0 1 100.0'. A label that is not specified will be "
"assumed to be variable and used for the computation (if not label 0).");
command.SetOptionLongTag("Temperatures", "temperature-label-pairs");
command.AddOptionField("Temperatures", "temperature", MetaCommand::LIST, true);
command.SetOption("Iterations", "i", false,
"Specify the number of iterations (default 1) the code is run. Suggested is to use a "
"large number of iterations like 2000 (see section about speed up). Convergence can be "
"monitored using the change value printed for each iteration (sum of absolute differences).");
command.AddOptionField("Iterations", "iterations", MetaCommand::INT, true);
// supersample the input (2 means 4 times more voxel)
command.SetOption("SuperSample", "s", false,
"Specify the number up-sampling steps using nearest neighboor interpolation (0 or 1 have no effect, 2 doubles the resolution 0.5 half's "
"the resolution).");
command.AddOptionField("SuperSample", "supersample", MetaCommand::FLOAT, true);
// quantize the output temperature
command.SetOption("Quantize", "q", false, "Quantize the output into N different regions of equal volume.");
command.AddOptionField("Quantize", "quantize", MetaCommand::INT, true);
command.SetOption("UnitNormalVector", "n", false,
"Export the unit normal vector and the unit binormal vector per voxel "
"(exported gradient field is the tangent vector) in nrrd format or in another specified vector format (see -f).");
command.SetOption(
"InitField", "c", false,
"Initialize the temperature field with this volume. This option together with the super sample option can be used to speed up convergence if a sequence "
"of small to large volumes is created where each stage is initialized with the temperature field calculated from the previous stage.");
command.AddOptionField("InitField", "initfield", MetaCommand::STRING, true);
command.SetOption("VectorFileFormat", "f", false,
"Specify the file format for the vector fields. Can be -f nrrd or -f vtk or another format supported by itk.");
command.AddOptionField("VectorFileFormat", "vectorfileformat", MetaCommand::STRING, true);
if (!command.Parse(argc, argv)) {
return 1;
}
std::string input = command.GetValueAsString("infile");
std::string outdir = command.GetValueAsString("outdir");
// fprintf(stdout, "input: \"%s\"\n", input.c_str());
// fprintf(stdout, "outdir: \"%s\"\n", outdir.c_str());
if (!boost::filesystem::exists(input)) {
std::cout << "Could not find the input file " << input << "..." << std::endl;
exit(1);
}
std::string initfield;
bool useInitField = false;
if (command.GetOptionWasSet("InitField")) {
initfield = command.GetValueAsString("InitField", "initfield");
useInitField = true;
}
std::string vectorfileextension = "nrrd";
if (command.GetOptionWasSet("VectorFileFormat")) {
if (command.GetValueAsString("VectorFileFormat", "vectorfileformat") == "nii") {
fprintf(stdout, "Warning: No nii support for vector fields, will use nrrd instead.\n");
} else {
vectorfileextension = command.GetValueAsString("VectorFileFormat", "vectorfileformat");
}
}
float supersampling = 0;
if (command.GetOptionWasSet("SuperSample"))
supersampling = command.GetValueAsFloat("SuperSample", "supersample");
if (supersampling < 0) {
fprintf(stdout, "Error: don't know how to supersample with negative values...\n");
exit(-1);
}
int quantize = -1; // don't quantize
if (command.GetOptionWasSet("Quantize"))
quantize = command.GetValueAsInt("Quantize", "quantize");
// todo: instead of number of iterations it would be good to have convergence error (might require
// double computations)
int iterations = 1;
if (command.GetOptionWasSet("Iterations"))
iterations = command.GetValueAsInt("Iterations", "iterations");
// computed using finite differences and cross-product
bool unitNormal = false;
if (command.GetOptionWasSet("UnitNormalVector"))
unitNormal = true;
std::map<int, float> temperatureByMaterial;
std::vector<int> labels;
std::vector<float> temperatures;
std::string temp_str = ""; // > 0
if (command.GetOptionWasSet("Temperatures")) {
std::list<std::string> thresholds = command.GetValueAsList("Temperatures");
std::list<std::string>::iterator it;
if (thresholds.size() % 2 != 0) {
fprintf(stdout,
"Error: should be an even number of threshold values and temperatures but found %lu "
"entries.\n",
thresholds.size());
exit(-1);
}
// fprintf(stdout, "found %lu temperature arguments\n", thresholds.size());
resultJSON["temperatures"] = json::array();
// std::list<std::string>::iterator it;
for (it = thresholds.begin(); it != thresholds.end(); it++) {
// append to labels and temperatures
int mat = atoi((*it).c_str());
float temp = 0.0;
if (it != thresholds.end()) {
it++;
temp = atof((*it).c_str());
}
temperatureByMaterial.insert(std::make_pair(mat, temp));
json v;
v["label"] = mat;
v["temperature"] = temp;
resultJSON["temperatures"].push_back(v);
}
}
bool verbose = false;
if (command.GetOptionWasSet("Verbose"))
verbose = true;
// store information in the result json file
resultJSON["command_line"] = json::array();
for (int i = 0; i < argc; i++) {
resultJSON["command_line"].push_back(std::string(argv[i]));
}
path p(input);
std::string fn = p.filename().string();
size_t lastdot = fn.find_last_of(".");
std::string output_filename;
if (lastdot == std::string::npos)
output_filename = fn + "_temperature.nii";
else
output_filename = fn.substr(0, lastdot) + "_temperature.nii";
std::string output_filename2;
if (lastdot == std::string::npos)
output_filename2 = fn + "_gradient." + vectorfileextension;
else
output_filename2 = fn.substr(0, lastdot) + "_gradient." + vectorfileextension;
std::string output_filename3;
if (lastdot == std::string::npos)
output_filename3 = fn + "_gradient_normal." + vectorfileextension;
else
output_filename3 = fn.substr(0, lastdot) + "_gradient_normal." + vectorfileextension;
std::string output_filename4;
if (lastdot == std::string::npos)
output_filename4 = fn + "_gradient_binormal." + vectorfileextension;
else
output_filename4 = fn.substr(0, lastdot) + "_gradient_binormal." + vectorfileextension;
resultJSON["output_temperature"] = outdir + "/" + output_filename;
resultJSON["output_gradient"] = outdir + "/" + output_filename2;
ImageReaderType::Pointer imageReader = ImageReaderType::New();
imageReader->SetFileName(input);
imageReader->Update();
ImageType::Pointer inputVol = imageReader->GetOutput();
ImageType::SpacingType spacing = inputVol->GetSpacing();
ImageType::RegionType region = inputVol->GetLargestPossibleRegion();
ImageType::SizeType dims = region.GetSize();
ImageType::PointType origin = inputVol->GetOrigin();
// do supersampling if required - can be support sub-sampling as well?
// that would make it easy to implement a staged computation across an image pyramid
if (supersampling > 0) {
resultJSON["SuperSamplingFactor"] = supersampling;
using ResampleFilterType = itk::ResampleImageFilter<ImageType, ImageType>;
ResampleFilterType::Pointer resampler = ResampleFilterType::New();
using TransformType = itk::IdentityTransform<double, 3>;
TransformType::Pointer transform = TransformType::New();
transform->SetIdentity();
resampler->SetTransform(transform);
// using InterpolatorType = itk::LinearInterpolateImageFunction<ImageType, double>;
using InterpolatorType = itk::NearestNeighborInterpolateImageFunction<ImageType, double>;
InterpolatorType::Pointer interpolator = InterpolatorType::New();
resampler->SetInterpolator(interpolator);
resampler->SetDefaultPixelValue(0); // highlight regions without source
OutputImageType::SpacingType spacingOut;
spacingOut[0] = spacing[0] / supersampling;
spacingOut[1] = spacing[1] / supersampling;
spacingOut[2] = spacing[2] / supersampling;
resultJSON["OutputSpacing"] = json::array();
resultJSON["OutputSpacing"].push_back(spacingOut[0]);
resultJSON["OutputSpacing"].push_back(spacingOut[1]);
resultJSON["OutputSpacing"].push_back(spacingOut[2]);
resampler->SetOutputSpacing(spacingOut);
resampler->SetOutputOrigin(inputVol->GetOrigin());
resampler->SetOutputDirection(inputVol->GetDirection());
ImageType::SizeType size;
size[0] = static_cast<itk::SizeValueType>(dims[0] * supersampling);
size[1] = static_cast<itk::SizeValueType>(dims[1] * supersampling);
size[2] = static_cast<itk::SizeValueType>(dims[2] * supersampling);
resultJSON["OutputSize"] = json::array();
resultJSON["OutputSize"].push_back(size[0]);
resultJSON["OutputSize"].push_back(size[1]);
resultJSON["OutputSize"].push_back(size[2]);
resampler->SetSize(size);
resampler->SetInput(inputVol);
resampler->Update();
inputVol = resampler->GetOutput();
}
spacing = inputVol->GetSpacing();
region = inputVol->GetLargestPossibleRegion();
dims = region.GetSize();
origin = inputVol->GetOrigin();
// compute the bounding box and the dimensions
float bb[6];
bb[0] = origin[0];
bb[2] = origin[1];
bb[4] = origin[2];
bb[1] = bb[0] + spacing[0] * (dims[0] - 1);
bb[3] = bb[2] + spacing[1] * (dims[1] - 1);
bb[5] = bb[4] + spacing[2] * (dims[2] - 1);
// fprintf(stdout, "BoundingBox: %f %f %f %f %f %f\n", bb[0], bb[1], bb[2], bb[3], bb[4], bb[5]);
// copy the data into the data and tmpData buffers
output.resize(dims[0] * dims[1] * dims[2]); // the output temperature as float
if (useInitField) {
fprintf(stdout, "copy initial temperature values from init field using resampling...\n");
// regardless of the resolution of the input file we need to resample it to the output file (and copy to output)
OutputReaderType::Pointer initReader = OutputReaderType::New();
initReader->SetFileName(initfield);
initReader->Update();
// after supersampling inputVol has the resolution of the output we need
ImageType::SpacingType spacing = inputVol->GetSpacing();
ImageType::RegionType region = inputVol->GetLargestPossibleRegion();
ImageType::SizeType dims = region.GetSize();
ImageType::PointType origin = inputVol->GetOrigin();
using OutputResampleFilterType = itk::ResampleImageFilter<OutputImageType, OutputImageType>;
OutputResampleFilterType::Pointer resampler2 = OutputResampleFilterType::New();
using TransformType = itk::IdentityTransform<double, 3>;
// keep the same transformation as the input
TransformType::Pointer transform = TransformType::New();
transform->SetIdentity();
resampler2->SetTransform(transform);
// we should use a better interpolator here (cubic)
using InterpolatorType = itk::LinearInterpolateImageFunction<OutputImageType, double>;
// using InterpolatorType = itk::NearestNeighborInterpolateImageFunction<ImageType, double>;
InterpolatorType::Pointer interpolator = InterpolatorType::New();
resampler2->SetInterpolator(interpolator);
resampler2->SetDefaultPixelValue(-10); // highlight regions without source
resampler2->SetOutputSpacing(spacing);
resampler2->SetOutputOrigin(inputVol->GetOrigin());
resampler2->SetOutputDirection(inputVol->GetDirection());
resampler2->SetSize(dims);
resampler2->SetInput(initReader->GetOutput());
resampler2->Update();
// now copy to data to the output array and use it during the iterations
OutputImageType::Pointer initVol = resampler2->GetOutput();
OutputImageType::RegionType initRegion = initVol->GetLargestPossibleRegion();
itk::ImageRegionIterator<OutputImageType> initIterator(initVol, initRegion);
// todo: we should make sure that the fixed temperature regions have the correct initial values
while (!initIterator.IsAtEnd()) {
OutputImageType::IndexType pixelIndex = initIterator.GetIndex();
size_t counter = toindex(pixelIndex[0], pixelIndex[1], pixelIndex[2]); // slow but correct
if (counter > 0 && counter < output.size() - 1) {
output[counter] = initIterator.Get();
}
++initIterator;
}
// we should free the temporary resampled volume here again - hopefully that happens on its own after this block
} else {
std::fill(output.begin(), output.end(), 0.0);
}
unsigned long boundingBox[6] = {dims[0], dims[1], dims[2], 0, 0, 0}; // i,j,k,highi,highj,highk
data.resize(dims[0] * dims[1] * dims[2]); // the labels as int
itk::ImageRegionIterator<ImageType> volIterator(inputVol, region);
size_t counter = 0;
while (!volIterator.IsAtEnd()) {
data[counter] = volIterator.Get();
ImageType::IndexType pixelIndex = volIterator.GetIndex();
if (data[counter] != 0 && temperatureByMaterial.find(data[counter]) == temperatureByMaterial.end()) {
// at this index we would need to compute a value
if (boundingBox[0] > pixelIndex[0])
boundingBox[0] = pixelIndex[0];
if (boundingBox[1] > pixelIndex[1])
boundingBox[1] = pixelIndex[1];
if (boundingBox[2] > pixelIndex[2])
boundingBox[2] = pixelIndex[2];
if (boundingBox[3] < pixelIndex[0])
boundingBox[3] = pixelIndex[0];
if (boundingBox[4] < pixelIndex[1])
boundingBox[4] = pixelIndex[1];
if (boundingBox[5] < pixelIndex[2])
boundingBox[5] = pixelIndex[2];
}
counter++;
++volIterator;
}
tmpData.resize(dims[0] * dims[1] * dims[2]); // temporary temperatures as float
std::fill(tmpData.begin(), tmpData.end(), 0.0);
fprintf(stdout, "bounding box limited computation %lu, %lu, %lu .. %lu, %lu, %lu\n", boundingBox[0], boundingBox[1], boundingBox[2], boundingBox[3],
boundingBox[4], boundingBox[5]);
// run the iterations
boost::chrono::system_clock::time_point start = boost::chrono::system_clock::now();
for (int i = 0; i < iterations; i++) {
// single iteration
// boost::chrono::system_clock::time_point begin = boost::chrono::system_clock::now();
fprintf(stdout, "step: %d/%d", i + 1, iterations);
double change = oneStep(dims, temperatureByMaterial, boundingBox);
// boost::chrono::system_clock::time_point end = boost::chrono::system_clock::now();
// float howmanymoreseconds = boost::chrono::duration_cast<boost::chrono::seconds>(end - begin).count() * (iterations - (i + 1));
boost::chrono::duration<double> sec = boost::chrono::system_clock::now() - start;
float predicted = sec.count() / (float)i; // time per iteration
fprintf(stdout, " change: %g (%.2f, done in: %.2f)\n", change, predicted * i, predicted * (iterations - i));
if (change == 0) {
break; // early stopping
}
}
// create the output object and save
OutputImageType::Pointer outVol = OutputImageType::New();
outVol->SetRegions(region);
outVol->Allocate();
outVol->SetOrigin(inputVol->GetOrigin());
outVol->SetSpacing(inputVol->GetSpacing());
outVol->SetDirection(inputVol->GetDirection());
itk::ImageRegionIterator<OutputImageType> outIterator(outVol, region);
counter = 0;
while (!outIterator.IsAtEnd()) {
outIterator.Set(output[counter]);
counter++;
++outIterator;
}
// export the potential field
typedef itk::ImageFileWriter<OutputImageType> WriterType;
WriterType::Pointer writer = WriterType::New();
// check if that directory exists, create before writing
writer->SetFileName(resultJSON["output_temperature"]);
writer->SetInput(outVol);
// check if that directory exists, create before writing
std::string p_txt = resultJSON["output_temperature"];
path out_p(p_txt);
create_directories(out_p.parent_path());
std::cout << "Writing the temperature field as ";
std::cout << resultJSON["output_temperature"] << std::endl;
try {
writer->Update();
} catch (itk::ExceptionObject &ex) {
std::cout << ex << std::endl;
return EXIT_FAILURE;
}
// compute the gradient field
GradientImageFilterPointer gfilter = GradientImageFilterType::New();
gfilter->SetInput(outVol);
gfilter->SetSigma(1.0f);
gfilter->Update();
GradientImagePointer gimage = gfilter->GetOutput();
GradientImagePointer gimage_for_export = gimage;
// mask the gradient field using the fixed temperature values
if (1) { // mask the gradient field, set all vectors to 0 that have a fixed temperature
GradientImageType::Pointer gimage_masked = GradientImageType::New();
gimage_masked->SetRegions(region);
gimage_masked->Allocate();
gimage_masked->SetOrigin(inputVol->GetOrigin());
gimage_masked->SetSpacing(inputVol->GetSpacing());
gimage_masked->SetDirection(inputVol->GetDirection());
itk::ImageRegionIterator<GradientImageType> oIterator(gimage_masked, region);
itk::ImageRegionIterator<GradientImageType> iIterator(gimage, region);
itk::ImageRegionIterator<ImageType> maskIterator(inputVol, region);
using VectorType = itk::CovariantVector<double, 3>;
while (!oIterator.IsAtEnd() && !maskIterator.IsAtEnd() && !iIterator.IsAtEnd()) {
if (temperatureByMaterial.find(maskIterator.Get()) != temperatureByMaterial.end()) {
// this is a voxel with a fixed temperature, set gradient to 0
VectorType erg;
erg[0] = 0;
erg[1] = 0;
erg[2] = 0;
oIterator.Set(erg); // set to 0
} else {
oIterator.Set(iIterator.Get()); // set to input
}
++oIterator;
++iIterator;
++maskIterator;
}
gimage_for_export = gimage_masked;
}
// export the gradient of the potential field
typedef itk::ImageFileWriter<GradientImageType> GradientWriterType;
GradientWriterType::Pointer writer2 = GradientWriterType::New();
// check if that directory exists, create before writing
writer2->SetFileName(resultJSON["output_gradient"]); // this is the tangent unit vector
writer2->SetInput(gimage_for_export);
std::cout << "Writing the gradient of the temperature field as ";
std::cout << resultJSON["output_gradient"] << std::endl;
try {
writer2->Update();
} catch (itk::ExceptionObject &ex) {
std::cout << ex << std::endl;
return EXIT_FAILURE;
}
// quantize the output temperature
if (quantize > 0) {
std::string output_filename3;
if (lastdot == std::string::npos)
output_filename3 = fn + "_temperature_quantized.nii";
else
output_filename3 = fn.substr(0, lastdot) + "_temperature_quantized.nii";
resultJSON["output_temperature_quantized"] = outdir + "/" + output_filename3;
// what is the temperature range we need to quantize?
OutputImageType::Pointer outQuant = OutputImageType::New();
outQuant->SetRegions(region);
outQuant->Allocate();
outQuant->SetOrigin(inputVol->GetOrigin());
outQuant->SetSpacing(inputVol->GetSpacing());
outQuant->SetDirection(inputVol->GetDirection());
itk::ImageRegionIterator<OutputImageType> oIterator(outQuant, region);
// compute the quartiles for all voxel in the non-zero non-fixed temperature voxel
int h_size = 200;
std::vector<size_t> histogram(h_size);
std::map<int, float>::iterator mit;
float maxTemp, minTemp; // histogram maps from max to min temperature
for (mit = temperatureByMaterial.begin(); mit != temperatureByMaterial.end(); mit++) {
if (mit == temperatureByMaterial.begin()) {
maxTemp = minTemp = mit->second;
}
if (mit->second > maxTemp)
maxTemp = mit->second;
if (mit->second < minTemp)
minTemp = mit->second;
}
resultJSON["temperature_range_specified"] = json::array();
resultJSON["temperature_range_specified"].push_back(minTemp);
resultJSON["temperature_range_specified"].push_back(maxTemp);
itk::ImageRegionIterator<OutputImageType> temperatureIterator(outVol, region);
itk::ImageRegionIterator<ImageType> maskIterator(inputVol, region);
while (!temperatureIterator.IsAtEnd() && !maskIterator.IsAtEnd()) {
if (maskIterator.Get() != 0) {
if (temperatureByMaterial.find(maskIterator.Get()) == temperatureByMaterial.end()) {
// this label does not have a fixed temperature, lets use it
int idx = ((temperatureIterator.Get() - minTemp) / (maxTemp - minTemp)) * (h_size - 1);
if (idx < 0)
idx = 0;
if (idx > h_size - 1)
idx = h_size - 1;
histogram[idx]++;
}
}
++temperatureIterator;
++maskIterator;
}
// now compute the normalized cummulative histogram
std::vector<double> cum_hist(h_size);
double sum = 0.0;
for (int i = 0; i < h_size; i++) {
sum += histogram[i];
cum_hist[i] = histogram[i];
if (i > 0) {
cum_hist[i] = cum_hist[i] + cum_hist[i - 1];
}
}
for (int i = 0; i < h_size; i++) {
cum_hist[i] /= sum;
}
std::vector<double> quartiles(quantize - 1); // we got one less border than we have quantiles
// now set the threshold temperature for each quartile range
for (int i = 0; i < quantize - 1; i++) {
// what is the first temperature where we reach the current quantile?
double quant_step = (i + 1) * (1.0 / quantize); // lower border of quantile
for (int j = 0; j < h_size; j++) {
if (cum_hist[j] >= quant_step) {
quartiles[i] = ((float)j / (h_size - 1.0)) * (maxTemp - minTemp) + minTemp; // temperature at this index
break;
}
}
}
json ar = json::array();
for (int i = 0; i < quartiles.size(); i++) {
ar.push_back(quartiles[i]);
}
resultJSON["output_temperature_quantized_thresholds"] = ar;
temperatureIterator.GoToBegin();
maskIterator.GoToBegin();
// itk::ImageRegionIterator<ImageType> maskIterator(inputVol, region);
itk::ImageRegionIterator<OutputImageType> outputIterator(outQuant, region);
while (!temperatureIterator.IsAtEnd() && !maskIterator.IsAtEnd() && !outputIterator.IsAtEnd()) {
outputIterator.Set(0); // outside
if (maskIterator.Get() != 0) {
if (temperatureByMaterial.find(maskIterator.Get()) == temperatureByMaterial.end()) {
// this label does not have a fixed temperature, lets use it
// what is the quantile for this voxel?
int q = 1;
for (int i = quartiles.size() - 1; i >= 0; i--) {
if (temperatureIterator.Get() > quartiles[i]) {
q = i + 2; // start counting from 1
break;
}
}
outputIterator.Set(q);
}
}
++temperatureIterator;
++maskIterator;
++outputIterator;
}
// export the quantized temperature field
typedef itk::ImageFileWriter<OutputImageType> WriterType;
WriterType::Pointer writer = WriterType::New();
// check if that directory exists, create before writing
writer->SetFileName(resultJSON["output_temperature_quantized"]);
writer->SetInput(outQuant);
std::cout << "Writing the temperature field as ";
std::cout << resultJSON["output_temperature_quantized"] << std::endl;
try {
writer->Update();
} catch (itk::ExceptionObject &ex) {
std::cout << ex << std::endl;
return EXIT_FAILURE;
}
}
// Unit Normal Vector calculation (and computation of the unit binormal)
if (unitNormal) {
resultJSON["output_gradient_normal"] = outdir + "/" + output_filename3;
resultJSON["output_gradient_binormal"] = outdir + "/" + output_filename4;
fprintf(stdout, "compute unit normal vector direction for each voxel using finite differences...\n");
// the tangent vector is in gimage, walk through the different voxel in x, y, z and compute
// finite differences GradientPixelType as location at each point
GradientImageType::Pointer outUN = GradientImageType::New();
outUN->SetRegions(region);
outUN->Allocate();
outUN->SetOrigin(inputVol->GetOrigin());
outUN->SetSpacing(inputVol->GetSpacing());
outUN->SetDirection(inputVol->GetDirection());
GradientImageType::Pointer outUBN = GradientImageType::New();
outUBN->SetRegions(region);
outUBN->Allocate();
outUBN->SetOrigin(inputVol->GetOrigin());
outUBN->SetSpacing(inputVol->GetSpacing());
outUBN->SetDirection(inputVol->GetDirection());
itk::ImageRegionIterator<GradientImageType> tangentIterator(gimage, region);
itk::ImageRegionIterator<GradientImageType> unIterator(outUN, region);
itk::ImageRegionIterator<GradientImageType> ubnIterator(outUBN, region);
itk::ImageRegionIterator<ImageType> maskIterator(inputVol, region);
using PointType = itk::Point<GradientPixelType, 3>;
using VectorType = itk::CovariantVector<double, 3>;
GradientImageType::IndexType pixelIndex;
using PointType = itk::Point<GradientPixelType, 3>;
ImageType::IndexType idxPoint1;
ImageType::IndexType idxPoint2;
double dsx = inputVol->GetSpacing()[0] * 2; // we move over the middle pixel so dT/ds
double dsy = inputVol->GetSpacing()[1] * 2;
double dsz = inputVol->GetSpacing()[2] * 2;
while (!tangentIterator.IsAtEnd() && !maskIterator.IsAtEnd() && !unIterator.IsAtEnd() && !ubnIterator.IsAtEnd()) {
GradientPixelType p = tangentIterator.Get();
std::vector<float> vec(3);
ImageType::IndexType pixelIndex = tangentIterator.GetIndex();
VectorType point0 = gimage->GetPixel(pixelIndex); // pull the data at this pixel location
//
// x - direction
//
double ds = dsx;
if (pixelIndex[0] - 1 < 0) {
// substitute center pixel
idxPoint1 = {{pixelIndex[0], pixelIndex[1], pixelIndex[2]}};
ds /= 2.0; // only half the distance
} else {
idxPoint1 = {{pixelIndex[0] - 1, pixelIndex[1], pixelIndex[2]}};
}
if (pixelIndex[0] + 1 >= dims[0]) {
// substitute center pixel
idxPoint2 = {{pixelIndex[0], pixelIndex[1], pixelIndex[2]}};
} else {
idxPoint2 = {{pixelIndex[0] + 1, pixelIndex[1], pixelIndex[2]}};
}
// compute the magnitude of the difference at these locations
VectorType point1 = gimage->GetPixel(idxPoint1); // pull the data at this pixel location
VectorType point2 = gimage->GetPixel(idxPoint2); // pull the data at this pixel location
vec[0] = (point1[0] - point2[0]) * (point1[0] - point2[0]) + (point1[1] - point2[1]) * (point1[1] - point2[1]) +
(point1[2] - point2[2]) * (point1[2] - point2[2]);
vec[0] /= ds;
//
// y - direction
//
ds = dsy;
if (pixelIndex[1] - 1 < 0) {
// substitute center pixel
idxPoint1 = {{pixelIndex[0], pixelIndex[1], pixelIndex[2]}};
ds /= 2.0;
} else {
idxPoint1 = {{pixelIndex[0], pixelIndex[1] - 1, pixelIndex[2]}};
}
if (pixelIndex[1] + 1 >= dims[1]) {
// substitute center pixel
idxPoint2 = {{pixelIndex[0], pixelIndex[1], pixelIndex[2]}};
} else {
idxPoint2 = {{pixelIndex[0], pixelIndex[1] + 1, pixelIndex[2]}};
}
// compute the magnitude of the difference at these locations
point1 = gimage->GetPixel(idxPoint1); // pull the data at this pixel location
point2 = gimage->GetPixel(idxPoint2); // pull the data at this pixel location
vec[1] = (point1[0] - point2[0]) * (point1[0] - point2[0]) + (point1[1] - point2[1]) * (point1[1] - point2[1]) +
(point1[2] - point2[2]) * (point1[2] - point2[2]);
vec[1] /= ds;
//
// z - direction
//
ds = dsz;
if (pixelIndex[2] - 1 < 0) {
// substitute center pixel
idxPoint1 = {{pixelIndex[0], pixelIndex[1], pixelIndex[2]}};
ds /= 2.0;
} else {
idxPoint1 = {{pixelIndex[0], pixelIndex[1], pixelIndex[2] - 1}};
}
if (pixelIndex[2] + 1 >= dims[2]) {
// substitute center pixel
idxPoint2 = {{pixelIndex[0], pixelIndex[1], pixelIndex[2]}};
} else {
idxPoint2 = {{pixelIndex[0], pixelIndex[1], pixelIndex[2] + 1}};
}
// compute the magnitude of the difference at these locations
point1 = gimage->GetPixel(idxPoint1); // pull the data at this pixel location
point2 = gimage->GetPixel(idxPoint2); // pull the data at this pixel location
vec[2] = (point1[0] - point2[0]) * (point1[0] - point2[0]) + (point1[1] - point2[1]) * (point1[1] - point2[1]) +
(point1[2] - point2[2]) * (point1[2] - point2[2]);
vec[2] /= ds;
// fprintf(stdout, "%f %f %f\n", vec[0], vec[1], vec[2]);
// scale the result vector to length 1
double vec_size = sqrt(vec[0] * vec[0] + vec[1] * vec[1] + vec[2] * vec[2]);
if (vec_size > 0) {
vec[0] /= vec_size;
vec[1] /= vec_size;
vec[2] /= vec_size;
}
VectorType erg;
erg[0] = vec[0];
erg[1] = vec[1];
erg[2] = vec[2];
unIterator.Set(erg);
std::vector<double> vec2(3); // compute the binormal vector as the cross-product
vec2[0] = vec[1] * point0[2] - vec[2] * point0[1];
vec2[1] = vec[2] * point0[0] - vec[0] * point0[2];
vec2[2] = vec[0] * point0[1] - vec[1] * point0[0];
erg[0] = vec2[0];
erg[1] = vec2[1];
erg[2] = vec2[2];
ubnIterator.Set(erg);
++unIterator;
++ubnIterator;
++tangentIterator;
++maskIterator;
}
typedef itk::ImageFileWriter<GradientImageType> GradientWriterType;
GradientWriterType::Pointer writer3 = GradientWriterType::New();
// check if that directory exists, create before writing
writer3->SetFileName(resultJSON["output_gradient_normal"]);
writer3->SetInput(outUN);
std::cout << "Writing the unit normal of the gradient of the temperature field as ";
std::cout << resultJSON["output_gradient_normal"] << std::endl;
try {
writer3->Update();
} catch (itk::ExceptionObject &ex) {
std::cout << ex << std::endl;
return EXIT_FAILURE;
}
GradientWriterType::Pointer writer4 = GradientWriterType::New();
// check if that directory exists, create before writing
writer4->SetFileName(resultJSON["output_gradient_binormal"]);
writer4->SetInput(outUBN);
std::cout << "Writing the unit binormal of the gradient of the temperature field as ";
std::cout << resultJSON["output_gradient_binormal"] << std::endl;
try {
writer4->Update();
} catch (itk::ExceptionObject &ex) {
std::cout << ex << std::endl;
return EXIT_FAILURE;
}
}
std::ostringstream o;
std::string si(outdir + "/" + output_filename);
si.erase(std::remove(si.begin(), si.end(), '\"'), si.end());
lastdot = si.find_last_of(".");
if (lastdot == std::string::npos)
si = si + ".json";
else