Grangeat-based 2D/3D image registration
Loading...
Searching...
No Matches
Common.h
Go to the documentation of this file.
1
6#pragma once
7
8#include "Vec.h"
9
10namespace reg23 {
11
18template <typename T> struct Linear2;
19
27template <typename T> struct Linear {
30
35 [[nodiscard]] __host__ __device__ T operator()(const T &x) const { return gradient * x + intercept; }
36
42 [[nodiscard]] __host__ __device__ Linear operator()(const Linear &other) const {
43 return {gradient * other.intercept + intercept, gradient * other.gradient};
44 }
45
51 [[nodiscard]] __host__ __device__ Linear2<T> operator()(const Linear2<T> &other) const;
52};
53
59template <typename T> struct Linear2 {
63
69 [[nodiscard]] __host__ __device__ T operator()(const T &x, const T &y) const {
70 return gradient1 * x + gradient2 * y + intercept;
71 }
72
78 [[nodiscard]] __host__ __device__ Linear2 operator()(const Linear<T> &other) const {
79 return {(gradient1 + gradient2) * other.intercept + intercept, gradient1 * other.gradient,
80 gradient2 * other.gradient};
81 }
82
88 [[nodiscard]] __host__ __device__ Linear2 operator()(const Linear2 &other) const {
89 const T gradientSum = gradient1 + gradient2;
90 return {gradientSum * other.intercept + intercept, gradientSum * other.gradient1,
91 gradientSum * other.gradient2};
92 }
93};
94
95template <typename T>
97 return {gradient * other.intercept + intercept, gradient * other.gradient1, gradient * other.gradient2};
98}
99
108template <typename T> [[nodiscard]] __host__ __device__ Vec<T, 3> UnflipSphericalCoordinate(const Vec<T, 3> &coordSph) {
109 constexpr T PI_T = static_cast<T>(M_PI);
110 const T theta_div = std::floor((coordSph.Y() + .5f * PI_T) / PI_T);
111 const bool theta_flip = static_cast<bool>(std::abs(static_cast<long>(theta_div)) % 2);
112 const T phi_div = std::floor((coordSph.Z() + .5f * PI_T) / PI_T);
113 const bool phi_flip = static_cast<bool>(std::abs(static_cast<long>(phi_div)) % 2);
114
115 T r_ret = coordSph.X();
116 T theta_ret = coordSph.Y() - PI_T * theta_div;
117 T phi_ret = coordSph.Z() - PI_T * phi_div;
118
119 if (phi_flip && !theta_flip) theta_ret *= -1;
120 if (phi_flip != theta_flip) r_ret *= -1;
121
122 return {r_ret, theta_ret, phi_ret};
123}
124
125#ifdef __CUDACC__
126
127template <typename T>
128__host__ cudaError_t CudaMemcpyToObjectSymbol(const T &symbol, T &src, cudaMemcpyKind kind = cudaMemcpyHostToDevice) {
129 return cudaMemcpyToSymbol(symbol, &src, sizeof(T), 0, kind);
130}
131
132#endif
133
138} // namespace reg23
#define __host__
Definition Global.h:17
#define __device__
Definition Global.h:22
A simple vector class derived from std::array<T, N>, providing overrides for all useful operators.
Definition Vec.h:21
__host__ __device__ constexpr const T & X() const
Get a constant reference to the first element.
Definition Vec.h:417
__host__ __device__ constexpr const T & Z() const
Get a constant reference to the third element.
Definition Vec.h:443
__host__ __device__ constexpr const T & Y() const
Get a constant reference to the second element.
Definition Vec.h:427
__host__ __device__ Vec< T, 3 > UnflipSphericalCoordinate(const Vec< T, 3 > &coordSph)
'Unflips' the given spherical coordinates so that theta and phi both lie between -pi/2 and pi/2
Definition Common.h:108
__host__ __device__ Linear2< T > operator()(const Linear2< T > &other) const
Definition Common.h:96
Definition GridSample3DCPU.cpp:6
A functor class that represents a linear transformation of two variables: intercept + gradient1 * x +...
Definition Common.h:59
__host__ __device__ Linear2 operator()(const Linear< T > &other) const
Definition Common.h:78
__host__ __device__ T operator()(const T &x, const T &y) const
Definition Common.h:69
__host__ __device__ Linear2 operator()(const Linear2 &other) const
Definition Common.h:88
T intercept
Definition Common.h:60
T gradient2
Definition Common.h:62
T gradient1
Definition Common.h:61
A functor class that represents a linear transformation: intercept + gradient * x.
Definition Common.h:27
T gradient
Definition Common.h:29
__host__ __device__ Linear operator()(const Linear &other) const
Definition Common.h:42
__host__ __device__ T operator()(const T &x) const
Definition Common.h:35
T intercept
Definition Common.h:28