|
| 1 | +#include <string> |
| 2 | +#include <fstream> |
| 3 | + |
| 4 | +#include <itkImage.h> |
| 5 | +#include <itkCovariantVector.h> |
| 6 | +#include <itkPasteImageFilter.h> |
| 7 | +#include <itkJoinSeriesImageFilter.h> |
| 8 | +#include <itkImageRegionIteratorWithIndex.h> |
| 9 | +#include <itkImageDuplicator.h> |
| 10 | + |
| 11 | +#include "rtkConstantImageSource.h" |
| 12 | +#include "rtkRayEllipsoidIntersectionImageFilter.h" |
| 13 | +#include "rtkDrawEllipsoidImageFilter.h" |
| 14 | +#include "rtkThreeDCircularProjectionGeometry.h" |
| 15 | +#ifdef RTK_USE_CUDA |
| 16 | +# include <itkCudaImage.h> |
| 17 | +#endif |
| 18 | + |
| 19 | +int |
| 20 | +main(int argc, char * argv[]) |
| 21 | +{ |
| 22 | + using PixelType = float; |
| 23 | + using DVFVectorType = itk::CovariantVector<PixelType, 3>; |
| 24 | + |
| 25 | +#ifdef RTK_USE_CUDA |
| 26 | + using VolumeSeriesType = itk::CudaImage<PixelType, 4>; |
| 27 | + using ProjectionStackType = itk::CudaImage<PixelType, 3>; |
| 28 | + using VolumeType = itk::CudaImage<PixelType, 3>; |
| 29 | + using DVFSequenceImageType = itk::CudaImage<DVFVectorType, 4>; |
| 30 | +#else |
| 31 | + using VolumeSeriesType = itk::Image<PixelType, 4>; |
| 32 | + using ProjectionStackType = itk::Image<PixelType, 3>; |
| 33 | + using VolumeType = itk::Image<PixelType, 3>; |
| 34 | + using DVFSequenceImageType = itk::Image<DVFVectorType, 4>; |
| 35 | +#endif |
| 36 | + using GeometryType = rtk::ThreeDCircularProjectionGeometry; |
| 37 | + constexpr unsigned int NumberOfProjectionImages = 64; |
| 38 | + |
| 39 | + /* ========================= |
| 40 | + * Constant volume source |
| 41 | + * ========================= */ |
| 42 | + using ConstantVolumeSourceType = rtk::ConstantImageSource<VolumeType>; |
| 43 | + auto volumeSource = ConstantVolumeSourceType::New(); |
| 44 | + |
| 45 | + auto origin = itk::MakePoint(-63., -31., -63.); |
| 46 | + auto spacing = itk::MakeVector(4., 4., 4.); |
| 47 | + auto size = itk::MakeSize(32, 16, 32); |
| 48 | + |
| 49 | + volumeSource->SetOrigin(origin); |
| 50 | + volumeSource->SetSpacing(spacing); |
| 51 | + volumeSource->SetSize(size); |
| 52 | + volumeSource->SetConstant(0.); |
| 53 | + volumeSource->Update(); |
| 54 | + |
| 55 | + /* ========================= |
| 56 | + * Projection accumulation |
| 57 | + * ========================= */ |
| 58 | + using ConstantProjectionSourceType = rtk::ConstantImageSource<ProjectionStackType>; |
| 59 | + auto projectionsSource = ConstantProjectionSourceType::New(); |
| 60 | + |
| 61 | + auto projOrigin = itk::MakePoint(-254., -254., -254.); |
| 62 | + auto projSpacing = itk::MakeVector(8., 8., 1.); |
| 63 | + auto projSize = itk::MakeSize(64, 64, NumberOfProjectionImages); |
| 64 | + |
| 65 | + projectionsSource->SetOrigin(projOrigin); |
| 66 | + projectionsSource->SetSpacing(projSpacing); |
| 67 | + projectionsSource->SetSize(projSize); |
| 68 | + projectionsSource->SetConstant(0.); |
| 69 | + projectionsSource->Update(); |
| 70 | + |
| 71 | + auto oneProjectionSource = ConstantProjectionSourceType::New(); |
| 72 | + projSize[2] = 1; |
| 73 | + oneProjectionSource->SetOrigin(projOrigin); |
| 74 | + oneProjectionSource->SetSpacing(projSpacing); |
| 75 | + oneProjectionSource->SetSize(projSize); |
| 76 | + oneProjectionSource->SetConstant(0.); |
| 77 | + |
| 78 | + using REIType = rtk::RayEllipsoidIntersectionImageFilter<VolumeType, ProjectionStackType>; |
| 79 | + using PasteType = itk::PasteImageFilter<ProjectionStackType, ProjectionStackType, ProjectionStackType>; |
| 80 | + |
| 81 | + /* Allocate explicit accumulator image */ |
| 82 | + auto accumulated = ProjectionStackType::New(); |
| 83 | + accumulated->SetOrigin(projectionsSource->GetOutput()->GetOrigin()); |
| 84 | + accumulated->SetSpacing(projectionsSource->GetOutput()->GetSpacing()); |
| 85 | + accumulated->SetDirection(projectionsSource->GetOutput()->GetDirection()); |
| 86 | + accumulated->SetRegions(projectionsSource->GetOutput()->GetLargestPossibleRegion()); |
| 87 | + accumulated->Allocate(); |
| 88 | + accumulated->FillBuffer(0.); |
| 89 | + |
| 90 | + auto paste = PasteType::New(); |
| 91 | + paste->SetDestinationImage(accumulated); |
| 92 | + |
| 93 | + itk::Index<3> destIndex; |
| 94 | + destIndex.Fill(0); |
| 95 | + |
| 96 | + // Create the signal file and the geometry, to be filled in the for loop |
| 97 | + std::string signalFileName = "four_d_signal.txt"; |
| 98 | + std::ofstream signalFile(signalFileName.c_str()); |
| 99 | + auto geometry = GeometryType::New(); |
| 100 | + |
| 101 | + for (unsigned int i = 0; i < NumberOfProjectionImages; ++i) |
| 102 | + { |
| 103 | + geometry->AddProjection(600., 1200., i * 360. / NumberOfProjectionImages, 0, 0, 0, 0, 20, 15); |
| 104 | + |
| 105 | + auto geom = GeometryType::New(); |
| 106 | + geom->AddProjection(600., 1200., i * 360. / NumberOfProjectionImages, 0, 0, 0, 0, 20, 15); |
| 107 | + |
| 108 | + auto e1 = REIType::New(); |
| 109 | + e1->SetInput(oneProjectionSource->GetOutput()); |
| 110 | + e1->SetGeometry(geom); |
| 111 | + e1->SetDensity(2.); |
| 112 | + e1->SetAxis(itk::MakeVector(60., 30., 60.)); |
| 113 | + e1->SetCenter(itk::MakeVector(0., 0., 0.)); |
| 114 | + e1->InPlaceOff(); |
| 115 | + e1->Update(); |
| 116 | + |
| 117 | + auto e2 = REIType::New(); |
| 118 | + e2->SetInput(e1->GetOutput()); |
| 119 | + e2->SetGeometry(geom); |
| 120 | + e2->SetDensity(-1.); |
| 121 | + e2->SetAxis(itk::MakeVector(8., 8., 8.)); |
| 122 | + auto center = itk::MakeVector(4 * (itk::Math::abs((4 + i) % 8 - 4.) - 2.), 0., 0.); |
| 123 | + e2->SetCenter(center); |
| 124 | + e2->InPlaceOff(); |
| 125 | + e2->Update(); |
| 126 | + |
| 127 | + paste->SetSourceImage(e2->GetOutput()); |
| 128 | + paste->SetSourceRegion(e2->GetOutput()->GetLargestPossibleRegion()); |
| 129 | + paste->SetDestinationIndex(destIndex); |
| 130 | + paste->Update(); |
| 131 | + |
| 132 | + accumulated = paste->GetOutput(); |
| 133 | + paste->SetDestinationImage(accumulated); |
| 134 | + |
| 135 | + destIndex[2]++; |
| 136 | + |
| 137 | + signalFile << (i % 8) / 8. << std::endl; |
| 138 | + } |
| 139 | + |
| 140 | + signalFile.close(); |
| 141 | + TRY_AND_EXIT_ON_ITK_EXCEPTION(itk::WriteImage(accumulated, "four_d_projections.mha")); |
| 142 | + TRY_AND_EXIT_ON_ITK_EXCEPTION(rtk::WriteGeometry(geometry, "four_d_geometry.xml")) |
| 143 | + |
| 144 | + /* ========================= |
| 145 | + * DVF & inverse DVF |
| 146 | + * ========================= */ |
| 147 | + auto fourDOrigin = itk::MakePoint(-63., -31., -63., 0.); |
| 148 | + auto fourDSpacing = itk::MakeVector(4., 4., 4., 1.); |
| 149 | + auto fourDSize = itk::MakeSize(32, 16, 32, 8); |
| 150 | + |
| 151 | + auto dvf = DVFSequenceImageType::New(); |
| 152 | + auto idvf = DVFSequenceImageType::New(); |
| 153 | + |
| 154 | + typename DVFSequenceImageType::RegionType region; |
| 155 | + auto dvfSize = itk::MakeSize(fourDSize[0], fourDSize[1], fourDSize[2], 2); |
| 156 | + region.SetSize(dvfSize); |
| 157 | + |
| 158 | + dvf->SetRegions(region); |
| 159 | + dvf->SetOrigin(fourDOrigin); |
| 160 | + dvf->SetSpacing(fourDSpacing); |
| 161 | + dvf->Allocate(); |
| 162 | + |
| 163 | + idvf->SetRegions(region); |
| 164 | + idvf->SetOrigin(fourDOrigin); |
| 165 | + idvf->SetSpacing(fourDSpacing); |
| 166 | + idvf->Allocate(); |
| 167 | + |
| 168 | + itk::ImageRegionIteratorWithIndex<DVFSequenceImageType> it(dvf, region); |
| 169 | + itk::ImageRegionIteratorWithIndex<DVFSequenceImageType> iit(idvf, region); |
| 170 | + |
| 171 | + DVFVectorType v; |
| 172 | + typename DVFSequenceImageType::IndexType centerIndex; |
| 173 | + centerIndex.Fill(0); |
| 174 | + centerIndex[0] = dvfSize[0] / 2; |
| 175 | + centerIndex[1] = dvfSize[1] / 2; |
| 176 | + centerIndex[2] = dvfSize[2] / 2; |
| 177 | + |
| 178 | + for (; !it.IsAtEnd(); ++it, ++iit) |
| 179 | + { |
| 180 | + v.Fill(0.); |
| 181 | + auto d = it.GetIndex() - centerIndex; |
| 182 | + if (0.3 * d[0] * d[0] + d[1] * d[1] + d[2] * d[2] < 40) |
| 183 | + v[0] = (it.GetIndex()[3] == 0 ? -8. : 8.); |
| 184 | + it.Set(v); |
| 185 | + iit.Set(-v); |
| 186 | + } |
| 187 | + |
| 188 | + TRY_AND_EXIT_ON_ITK_EXCEPTION(itk::WriteImage(dvf, "four_d_dvf.mha")); |
| 189 | + TRY_AND_EXIT_ON_ITK_EXCEPTION(itk::WriteImage(idvf, "four_d_idvf.mha")); |
| 190 | + |
| 191 | + /* ========================= |
| 192 | + * Ground truth |
| 193 | + * ========================= */ |
| 194 | + auto join = itk::JoinSeriesImageFilter<VolumeType, VolumeSeriesType>::New(); |
| 195 | + |
| 196 | + for (unsigned int t = 0; t < fourDSize[3]; ++t) |
| 197 | + { |
| 198 | + using DEType = rtk::DrawEllipsoidImageFilter<VolumeType, VolumeType>; |
| 199 | + |
| 200 | + auto de1 = DEType::New(); |
| 201 | + de1->SetInput(volumeSource->GetOutput()); |
| 202 | + de1->SetDensity(2.); |
| 203 | + de1->SetAxis(itk::MakeVector(60., 30., 60.)); |
| 204 | + de1->SetCenter(itk::MakeVector(0., 0., 0.)); |
| 205 | + de1->InPlaceOff(); |
| 206 | + de1->Update(); |
| 207 | + |
| 208 | + auto de2 = DEType::New(); |
| 209 | + de2->SetInput(de1->GetOutput()); |
| 210 | + de2->SetDensity(-1.); |
| 211 | + de2->SetAxis(itk::MakeVector(8., 8., 8.)); |
| 212 | + de2->SetCenter(itk::MakeVector(4 * (itk::Math::abs((4 + t) % 8 - 4.) - 2.), 0., 0.)); |
| 213 | + de2->InPlaceOff(); |
| 214 | + de2->Update(); |
| 215 | + |
| 216 | + using DuplicatorType = itk::ImageDuplicator<VolumeType>; |
| 217 | + auto duplicator = DuplicatorType::New(); |
| 218 | + duplicator->SetInputImage(de2->GetOutput()); |
| 219 | + duplicator->Update(); |
| 220 | + |
| 221 | + join->SetInput(t, duplicator->GetOutput()); |
| 222 | + } |
| 223 | + |
| 224 | + join->Update(); |
| 225 | + TRY_AND_EXIT_ON_ITK_EXCEPTION(itk::WriteImage(join->GetOutput(), "four_d_ground_truth.mha")); |
| 226 | + |
| 227 | + return EXIT_SUCCESS; |
| 228 | +} |
0 commit comments