Grangeat-based 2D/3D image registration
Loading...
Searching...
No Matches
Texture2DCPU.h
Go to the documentation of this file.
1#pragma once
2
3#include "Texture.h"
4
5namespace reg23 {
6
15class Texture2DCPU : public Texture<2, int64_t, double> {
16public:
18
19 Texture2DCPU() = default;
20
27
28 // yes copy
29 Texture2DCPU(const Texture2DCPU &) = default;
30
31 Texture2DCPU &operator=(const Texture2DCPU &) = default;
32
33 // yes move
35
37
45 static Texture2DCPU FromTensor(const at::Tensor &image, VectorType spacing,
46 VectorType centrePosition = VectorType::Full(0),
48 return {image.contiguous().data_ptr<float>(), Vec<int64_t, 2>::FromIntArrayRef(image.sizes()).Flipped(),
49 std::move(spacing), std::move(centrePosition), std::move(addressModes)};
50 }
51
56 [[nodiscard]] __host__ __device__ float At(const SizeType &index) const {
57 if ((addressModes.X() == TextureAddressMode::ZERO && (index.X() < 0 || index.X() >= Size().X())) || (
58 addressModes.Y() == TextureAddressMode::ZERO && (index.Y() < 0 || index.Y() >= Size().Y()))) {
59 return 0.f;
60 }
61 // Uses wrapping for indices outside the texture.
62 return ptr[Modulo(index.Y(), Size().Y()) * Size().X() + Modulo(index.X(), Size().X())];
63 }
64
70 texCoord = texCoord * Size().StaticCast<double>() - .5;
71 const VectorType floored = texCoord.Apply<double>(&floor);
72 const SizeType index = floored.StaticCast<int64_t>();
74 const float r0 = (1.f - fractions.X()) * At(index) + fractions.X() * At({index.X() + 1, index.Y()});
75 const float r1 = (1.f - fractions.X()) * At({index.X(), index.Y() + 1}) + fractions.X() * At(
76 {index.X() + 1, index.Y() + 1});
77 return (1.f - fractions.Y()) * r0 + fractions.Y() * r1;
78 }
79
86 const VectorType sizeF = Size().StaticCast<double>();
87 texCoord = texCoord * sizeF - .5;
88 const VectorType floored = texCoord.Apply<double>(&floor);
89 const SizeType index = floored.StaticCast<int64_t>();
90 const float fVertical = texCoord.Y() - floored.Y();
91 return sizeF.X() * ((1.f - fVertical) * (At({index.X() + 1, index.Y()}) - At(index)) + fVertical * (
92 At({index.X() + 1, index.Y() + 1}) - At({index.X(), index.Y() + 1})));
93 }
94
101 const VectorType sizeF = Size().StaticCast<double>();
102 texCoord = texCoord * sizeF - .5;
103 const VectorType floored = texCoord.Apply<double>(&floor);
104 const SizeType index = floored.StaticCast<int64_t>();
105 const float fHorizontal = texCoord.X() - floored.X();
106 return sizeF.Y() * ((1.f - fHorizontal) * (At({index.X(), index.Y() + 1}) - At(index)) + fHorizontal * (
107 At({index.X() + 1, index.Y() + 1}) - At({index.X() + 1, index.Y()})));
108 }
109
116 const VectorType sizeF = Size().StaticCast<double>();
117 texCoord = texCoord * sizeF - .5;
118 const VectorType floored = texCoord.Apply<double>(&floor);
119 const SizeType index = floored.StaticCast<int64_t>();
120 const float fHorizontal = texCoord.X() - floored.X();
121 const float fVertical = texCoord.Y() - floored.Y();
122 return {
123 sizeF.X() * ((1.f - fVertical) * (At({index.X() + 1, index.Y()}) - At(index)) + fVertical * (
124 At({index.X() + 1, index.Y() + 1}) - At({index.X(), index.Y() + 1}))), //
125 sizeF.Y() * ((1.f - fHorizontal) * (At({index.X(), index.Y() + 1}) - At(index)) + fHorizontal * (
126 At({index.X() + 1, index.Y() + 1}) - At({index.X() + 1, index.Y()})))};
127 }
128
129private:
130 const float *ptr{};
131 AddressModeType addressModes{};
132};
133
134} // namespace reg23
#define __host__
Definition Global.h:17
#define __device__
Definition Global.h:22
A 2D texture stored for access by the CPU.
Definition Texture2DCPU.h:15
Texture2DCPU & operator=(const Texture2DCPU &)=default
static Texture2DCPU FromTensor(const at::Tensor &image, VectorType spacing, VectorType centrePosition=VectorType::Full(0), AddressModeType addressModes=AddressModeType::Full(TextureAddressMode::ZERO))
Definition Texture2DCPU.h:45
__host__ __device__ float At(const SizeType &index) const
Definition Texture2DCPU.h:56
__host__ __device__ float DSampleDY(VectorType texCoord) const
Definition Texture2DCPU.h:100
Texture2DCPU & operator=(Texture2DCPU &&)=default
__host__ __device__ float DSampleDX(VectorType texCoord) const
Definition Texture2DCPU.h:85
__host__ __device__ float Sample(VectorType texCoord) const
Definition Texture2DCPU.h:69
Texture2DCPU(const float *_ptr, SizeType _size, VectorType _spacing, VectorType _centrePosition=VectorType::Full(0), AddressModeType _addressModes=AddressModeType::Full(TextureAddressMode::ZERO))
Definition Texture2DCPU.h:21
Texture2DCPU(const Texture2DCPU &)=default
Texture2DCPU(Texture2DCPU &&)=default
__host__ __device__ VectorType DSampleDTexCoord(VectorType texCoord) const
Definition Texture2DCPU.h:115
Texture2DCPU()=default
A parent texture class containing template data and functionality.
Definition Texture.h:71
__host__ __device__ const SizeType & Size() const
Definition Texture.h:82
Vec< TextureAddressMode, dimensionality > AddressModeType
Definition Texture.h:78
__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__ __device__ constexpr Vec< newT, N > Apply(const std::function< newT(T)> &f) const
Map all elements with a common std::function mapping function.
Definition Vec.h:285
__host__ __device__ constexpr const T & X() const
Get a constant reference to the first element.
Definition Vec.h:417
__host__ __device__ constexpr const T & Y() const
Get a constant reference to the second element.
Definition Vec.h:427
__host__ static __device__ constexpr Vec Full(const double &value)
Construct a Vec, copying the given value into every element.
Definition Vec.h:64
__host__ __device__ T Modulo(const T &x, const T &y)
Modulo operation that respect the sign.
Definition Global.h:48
Vec< TextureAddressMode, DIMENSIONALITY > StringsToAddressModes(const std::array< std::string_view, DIMENSIONALITY > &strings)
Definition Texture.h:44
@ ZERO
Sampling locations outside texture coordinate range will be read as 0.
Definition GridSample3DCPU.cpp:6