Grangeat-based 2D/3D image registration
Loading...
Searching...
No Matches
ProjectDRR.h
Go to the documentation of this file.
1
6#pragma once
7
8#include "Common.h"
9
10namespace reg23 {
11
27at::Tensor ProjectDRR_CPU(const at::Tensor &volume, const at::Tensor &voxelSpacing,
28 const at::Tensor &homographyMatrixInverse, double sourceDistance, int64_t outputWidth,
29 int64_t outputHeight, const at::Tensor &outputOffset, const at::Tensor &detectorSpacing);
30
35__host__ at::Tensor ProjectDRR_CUDA(const at::Tensor &volume, const at::Tensor &voxelSpacing,
36 const at::Tensor &homographyMatrixInverse, double sourceDistance,
37 int64_t outputWidth, int64_t outputHeight, const at::Tensor &outputOffset,
38 const at::Tensor &detectorSpacing);
39
54at::Tensor ProjectDRR_backward_CPU(const at::Tensor &volume, const at::Tensor &voxelSpacing,
55 const at::Tensor &homographyMatrixInverse, double sourceDistance,
56 int64_t outputWidth, int64_t outputHeight, const at::Tensor &outputOffset,
57 const at::Tensor &detectorSpacing, const at::Tensor &dLossDDRR);
58
63__host__ at::Tensor ProjectDRR_backward_CUDA(const at::Tensor &volume, const at::Tensor &voxelSpacing,
64 const at::Tensor &homographyMatrixInverse, double sourceDistance,
65 int64_t outputWidth, int64_t outputHeight, const at::Tensor &outputOffset,
66 const at::Tensor &detectorSpacing, const at::Tensor &dLossDDRR);
67
81__host__ at::Tensor ProjectDRRsBatched_CUDA(const at::Tensor &volume, const at::Tensor &voxelSpacing,
82 const at::Tensor &invHMatrices, double sourceDistance, int64_t outputWidth,
83 int64_t outputHeight, const at::Tensor &outputOffset,
84 const at::Tensor &detectorSpacing);
85
92template <typename texture_t> struct ProjectDRR {
93
94 static_assert(texture_t::DIMENSIONALITY == 3);
95
96 using IntType = typename texture_t::IntType;
97 using FloatType = typename texture_t::FloatType;
98 using SizeType = typename texture_t::SizeType;
99 using VectorType = typename texture_t::VectorType;
100 using AddressModeType = typename texture_t::AddressModeType;
101
111
112 __host__ static CommonData Common(const at::Tensor &volume, const at::Tensor &voxelSpacing,
113 const at::Tensor &homographyMatrixInverse, double sourceDistance,
114 const at::Tensor &outputOffset, const at::Tensor &detectorSpacing,
115 at::DeviceType device, std::optional<int64_t> samplesPerRay = std::nullopt) {
116 // volume should be a 3D tensor of floats on the chosen device
117 TORCH_CHECK(volume.sizes().size() == 3);
118 TORCH_CHECK(volume.dtype() == at::kFloat);
119 TORCH_INTERNAL_ASSERT(volume.device().type() == device);
120 // voxelSpacing should be a 1D tensor of 3 doubles
121 TORCH_CHECK(voxelSpacing.sizes() == at::IntArrayRef{3});
122 TORCH_CHECK(voxelSpacing.dtype() == at::kDouble);
123 // homographyMatrixInverse should be of size (4, 4), contain doubles and be on the chosen device
124 TORCH_CHECK(homographyMatrixInverse.sizes() == at::IntArrayRef({4, 4}));
125 TORCH_CHECK(homographyMatrixInverse.dtype() == at::kDouble);
126 TORCH_INTERNAL_ASSERT(homographyMatrixInverse.device().type() == device);
127 // outputOffset should be a 1D tensor of 2 doubles
128 TORCH_CHECK(outputOffset.sizes() == at::IntArrayRef{2});
129 TORCH_CHECK(outputOffset.dtype() == at::kDouble);
130 // detectorSpacing should be a 1D tensor of 2 doubles
131 TORCH_CHECK(detectorSpacing.sizes() == at::IntArrayRef{2});
132 TORCH_CHECK(detectorSpacing.dtype() == at::kDouble);
133
134 const int64_t samplesPerRayValue = samplesPerRay.value_or(
136
137 CommonData ret{};
138 ret.inputTexture = texture_t::FromTensor(volume, VectorType::FromTensor(voxelSpacing));
139 ret.homographyMatrixInverse = Vec<Vec<double, 4>, 4>::FromTensor2D(homographyMatrixInverse);
140
142 const VectorType volumeDiagonal = inputSize.StaticCast<FloatType>() * ret.inputTexture.Spacing();
144 const VectorType sourcePosition = {0.0, 0.0, sourceDistance};
145 ret.lambdaStart = MatMul(ret.homographyMatrixInverse, VecCat(sourcePosition, 1.0)).XYZ().Length() - 0.5 *
147 ret.stepSize = volumeDiagLength / static_cast<FloatType>(samplesPerRayValue);
148 ret.samplesPerRay = samplesPerRayValue;
149 ret.outputOffset = Vec<double, 2>::FromTensor(outputOffset);
150 ret.detectorSpacing = Vec<double, 2>::FromTensor(detectorSpacing);
151 return ret;
152 }
153};
154
155} // namespace reg23
General tools and structs.
#define __host__
Definition Global.h:17
A simple vector class derived from std::array<T, N>, providing overrides for all useful operators.
Definition Vec.h:21
__host__ __device__ constexpr Vec< newT, N > StaticCast() const
Construct a Vec with the elements form this one cast to a new type.
Definition Vec.h:237
__host__ static __device__ constexpr Vec FromIntArrayRef(const at::IntArrayRef &v)
Construct a Vec from an at::IntArrayRef (this is the type of at::Tensor::sizes())
Definition Vec.h:82
__host__ __device__ constexpr T Max() const
Find the largest element.
Definition Vec.h:195
static __host__ Vec FromTensor(const at::Tensor &t)
Construct a Vec from a 1D PyTorch tensor.
Definition Vec.h:98
__host__ __device__ constexpr Vec Flipped() const
Construct a Vec with the same elements, but in the reverse order.
Definition Vec.h:161
__host__ __device__ constexpr Vec< T,(Ns+...)> VecCat(const Vec< T, Ns > &... vecs)
reg23::Vec concatenation of any number of vectors
Definition Vec.h:841
__host__ __device__ constexpr Vec< T, R > MatMul(const Vec< Vec< T, R >, C > &lhs, const Vec< T, C > &rhs)
Matrix-vector multiplication of the Vec struct.
Definition Vec.h:894
__host__ at::Tensor ProjectDRRsBatched_CUDA(const at::Tensor &volume, const at::Tensor &voxelSpacing, const at::Tensor &invHMatrices, double sourceDistance, int64_t outputWidth, int64_t outputHeight, const at::Tensor &outputOffset, const at::Tensor &detectorSpacing)
An implementation similar to reg23::ProjectDRR_CUDA that evaluates projections for multiple transform...
__host__ at::Tensor ProjectDRR_backward_CUDA(const at::Tensor &volume, const at::Tensor &voxelSpacing, const at::Tensor &homographyMatrixInverse, double sourceDistance, int64_t outputWidth, int64_t outputHeight, const at::Tensor &outputOffset, const at::Tensor &detectorSpacing, const at::Tensor &dLossDDRR)
An implementation of reg23::ProjectDRR_backward_CPU that uses CUDA parallelisation.
__host__ at::Tensor ProjectDRR_CUDA(const at::Tensor &volume, const at::Tensor &voxelSpacing, const at::Tensor &homographyMatrixInverse, double sourceDistance, int64_t outputWidth, int64_t outputHeight, const at::Tensor &outputOffset, const at::Tensor &detectorSpacing)
An implementation of reg23::ProjectDRR_CPU that uses CUDA parallelisation.
at::Tensor ProjectDRR_backward_CPU(const at::Tensor &volume, const at::Tensor &voxelSpacing, const at::Tensor &homographyMatrixInverse, double sourceDistance, int64_t outputWidth, int64_t outputHeight, const at::Tensor &outputOffset, const at::Tensor &detectorSpacing, const at::Tensor &dLossDDRR)
Evaluate the derivative of some scalar loss that is a function of a DRR projected from the given volu...
Definition ProjectDRRCPU.cpp:47
at::Tensor ProjectDRR_CPU(const at::Tensor &volume, const at::Tensor &voxelSpacing, const at::Tensor &homographyMatrixInverse, double sourceDistance, int64_t outputWidth, int64_t outputHeight, const at::Tensor &outputOffset, const at::Tensor &detectorSpacing)
Generate a DRR from the given volume at the given transformation.
Definition ProjectDRRCPU.cpp:10
Vec< TextureAddressMode, DIMENSIONALITY > StringsToAddressModes(const std::array< std::string_view, DIMENSIONALITY > &strings)
Definition Texture.h:44
Definition GridSample3DCPU.cpp:6
Definition ProjectDRR.h:102
int64_t samplesPerRay
Definition ProjectDRR.h:109
Vec< double, 2 > outputOffset
Definition ProjectDRR.h:105
texture_t inputTexture
Definition ProjectDRR.h:103
Vec< double, 2 > detectorSpacing
Definition ProjectDRR.h:106
Vec< Vec< double, 4 >, 4 > homographyMatrixInverse
Definition ProjectDRR.h:104
FloatType stepSize
Definition ProjectDRR.h:108
double lambdaStart
Definition ProjectDRR.h:107
Definition ProjectDRR.h:92
static __host__ CommonData Common(const at::Tensor &volume, const at::Tensor &voxelSpacing, const at::Tensor &homographyMatrixInverse, double sourceDistance, const at::Tensor &outputOffset, const at::Tensor &detectorSpacing, at::DeviceType device, std::optional< int64_t > samplesPerRay=std::nullopt)
Definition ProjectDRR.h:112
typename texture_t::SizeType SizeType
Definition ProjectDRR.h:98
typename texture_t::VectorType VectorType
Definition ProjectDRR.h:99
typename texture_t::FloatType FloatType
Definition ProjectDRR.h:97
typename texture_t::IntType IntType
Definition ProjectDRR.h:96
typename texture_t::AddressModeType AddressModeType
Definition ProjectDRR.h:100