StrongTypes.h 24.9 KB
Newer Older
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
#pragma once

#include "elsaDefines.h"
#include "DataDescriptor.h"

#include <utility>
#include <cassert>
#include <array>

namespace elsa
{
    namespace geometry
    {
        class Degree;

        /**
         * @brief Class describing angles in Radians. Mostly used for parameter passing.
         * Has an implicit constructor from Degree, and converts to real_t implicitly as well
         */
        class Radian
        {
        public:
            /// Default constructor (sets 0 radians)
            constexpr Radian() : _radian(0) {}

            /// Constructor from real_t
            constexpr explicit Radian(real_t radian) : _radian(radian) {}

            /// Constructor (implicit) from Degree
            constexpr Radian(Degree d);

            /// Conversion to degree (as real_t)
            constexpr real_t to_degree() const { return _radian * 180 / pi_t; }

            /// Conversion (implicit) to real_t
            constexpr operator real_t() const { return _radian; }

        private:
            real_t _radian;
        };

        /**
         * @brief Class describing angles in Degree. Mostly used for parameter passing.
         * Has an implicit constructor from Radians, and converts to real_t implicitly as well
         */
        class Degree
        {
        public:
            /// Default constructor (sets 0 degrees)
            constexpr Degree() : _degree(0) {}

            /// Constructor from real_t
            constexpr explicit Degree(real_t degree) : _degree(degree) {}

            /// Constructor (implicit) from Radian
            constexpr Degree(Radian r);

            /// Conversion to radian (as real_t)
            constexpr real_t to_radian() const { return _degree * pi_t / 180; }

            /// Conversion (implicit) to real_t
            constexpr operator real_t() const { return _degree; }

        private:
            real_t _degree;
        };

        // Defined out of class, to access the to_... functions
        constexpr Radian::Radian(Degree d) : _radian(d.to_radian()) {}
        constexpr Degree::Degree(Radian r) : _degree(r.to_degree()) {}

        /// Strong type class to a specific angle alpha
        class Alpha : Radian
        {
        public:
            using Base = Radian;

            using Base::Base;
            using Base::to_degree;
            using Base::operator real_t;
        };

        /// Strong type class to a specific angle beta
        class Beta : Radian
        {
        public:
            using Base = Radian;

            using Base::Base;
            using Base::to_degree;
            using Base::operator real_t;
        };

        /// Strong type class to a specific angle gamma
        class Gamma : Radian
        {
        public:
            using Base = Radian;

            using Base::Base;
            using Base::to_degree;
            using Base::operator real_t;
        };

        namespace detail
        {
            /**
             * @brief Class to store a fixed length array of Radians. Ensures, that all entries are
             * convertible to radians
             */
            template <index_t Size>
            class RotationAngles
            {
            public:
                // Alias for Radian
                using Type = Radian;

            private:
                /// Alias for, enable if, all (the conjunction of) Ts... are convertible to Radian
                template <typename... Ts>
                using AllRadian = typename std::enable_if_t<
                    std::conjunction<std::is_convertible<Ts, Type>...>::value>;

            public:
                RotationAngles(const RotationAngles&) = default;
                RotationAngles& operator=(const RotationAngles&) = default;
                RotationAngles(RotationAngles&&) = default;
                RotationAngles& operator=(RotationAngles&&) = default;

                /// Construct array (expect it to be all convertible to Radians and number of
                /// arguments, equal to size)
                template <typename... Ts, typename = AllRadian<Ts...>,
                          typename = std::enable_if_t<(Size > 0) && (Size == (sizeof...(Ts)))>>
                constexpr RotationAngles(Ts&&... ts)
                    : RotationAngles(std::index_sequence_for<Ts...>{}, std::forward<Ts>(ts)...)
                {
                }

                /// Access operator
                constexpr Type operator[](index_t i) const { return _angles[i]; }

                /// get function (non-const reference) to enable structured bindings
                template <size_t I>
                Type& get() &
                {
                    return (std::get<I>(_angles));
                }

                /// get function (const reference) to enable structured bindings
                template <size_t I>
                const Type& get() const&
                {
                    return (std::get<I>(_angles));
                }

                /// get function (r-value reference) to enable structured bindings
                template <size_t I>
                Type&& get() &&
                {
                    return std::move(std::get<I>(_angles));
                }

            private:
                /// Helper constructor to init array
                template <std::size_t... Is, typename... Ts, typename = AllRadian<Ts...>>
                constexpr RotationAngles(std::index_sequence<Is...>, Ts&&... vals)
                {
                    // Fold expression expands to _angles[0] = vals0, _angles[1] = vals1, ...,
                    // _angles[k] = valsk;
                    (static_cast<void>(_angles[Is] = vals), ...);
                }

                std::array<Type, Size> _angles;
            };
        } // namespace detail

        /**
         * @brief Strong type class for 3D geometry, which expects 3 angles (alpha, beta, gamma).
         * Class is constructible from the previously defined Alpha, Beta, Gamma and only subsets of
         * it (i.e. only with Beta and Alpha)
         *
         */
        class RotationAngles3D : detail::RotationAngles<3>
        {
        public:
            using Base = detail::RotationAngles<3>;

        private:
            using Base::Base;

        public:
            using Base::get;
            using Base::operator[];

            /// Construction from Gamma
            constexpr RotationAngles3D(Gamma gamma)
                : Base(Radian{static_cast<real_t>(gamma)}, Radian{0}, Radian{0})
            {
            }

            /// Construction from Beta
            constexpr RotationAngles3D(Beta beta)
                : Base(Radian{0}, Radian{static_cast<real_t>(beta)}, Radian{0})
            {
            }

            /// Construction from Alpha
            constexpr RotationAngles3D(Alpha alpha)
                : Base(Radian{0}, Radian{0}, Radian{static_cast<real_t>(alpha)})
            {
            }

            /// Construction from Gamma and Beta
            constexpr RotationAngles3D(Gamma gamma, Beta beta)
                : Base(Radian{static_cast<real_t>(gamma)}, Radian{static_cast<real_t>(beta)},
                       Radian{0})
            {
            }

            /// Construction from Gamma and Alpha
            constexpr RotationAngles3D(Gamma gamma, Alpha alpha)
                : Base(Radian{static_cast<real_t>(gamma)}, Radian{0},
                       Radian{static_cast<real_t>(alpha)})
            {
            }

            /// Construction from Beta and Gamma
            constexpr RotationAngles3D(Beta beta, Gamma gamma) : RotationAngles3D(gamma, beta) {}

            /// Construction from Beta and Alpha
            constexpr RotationAngles3D(Beta beta, Alpha alpha)
                : Base(Radian{0}, Radian{static_cast<real_t>(beta)},
                       Radian{static_cast<real_t>(alpha)})
            {
            }

            /// Construction from Alpha and Beta
            constexpr RotationAngles3D(Alpha alpha, Gamma gamma) : RotationAngles3D(gamma, alpha) {}

            /// Construction from Alpha and Gamma
            constexpr RotationAngles3D(Alpha alpha, Beta beta) : RotationAngles3D(beta, alpha) {}

            /// Construction from Gamma, Beta and Alpha
            constexpr RotationAngles3D(Gamma gamma, Beta beta, Alpha alpha)
                : Base(Radian{static_cast<real_t>(gamma)}, Radian{static_cast<real_t>(beta)},
                       Radian{static_cast<real_t>(alpha)})
            {
            }

            /// Access to gamma
251
            constexpr Radian gamma() const { return operator[](0u); }
252
            /// Access to beta
253
            constexpr Radian beta() const { return operator[](1u); }
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
            /// Access to alpha
            constexpr Radian alpha() const { return operator[](2); }
        };

        namespace detail
        {
            /**
             * @brief Wrapper for real_t, used as base class for strong typing
             */
            class RealWrapper
            {
            public:
                constexpr RealWrapper() : _x(0) {}

                constexpr explicit RealWrapper(real_t x) : _x(x) {}

                constexpr operator real_t() { return _x; }

            private:
                real_t _x;
            };

            /**
             * @brief Class wrapping RealVector_t for strong typing, with a fixed size
             */
            template <index_t Size, typename Vector>
            class StaticVectorTemplate
            {
            private:
                using Scalar = typename Vector::Scalar;

                /// Alias for, enable if, all (the conjunction of) Ts... are convertible to Scalar
                template <typename... Ts>
                using AllScalar = typename std::enable_if_t<
                    std::conjunction<std::is_convertible<Ts, Scalar>...>::value>;

            public:
                /// Default constructor
                StaticVectorTemplate() : _vec(Size) {}

                StaticVectorTemplate(const StaticVectorTemplate&) = default;
                StaticVectorTemplate& operator=(const StaticVectorTemplate&) = default;
                StaticVectorTemplate(StaticVectorTemplate&&) = default;
                StaticVectorTemplate& operator=(StaticVectorTemplate&&) = default;

                /// Construct array (expect it to be all convertible to Scalar and number of
                /// arguments, equal to size)
                template <typename... Ts, typename = AllScalar<Ts...>,
                          typename = std::enable_if_t<(Size > 0) && (Size == (sizeof...(Ts) + 1))>>
                StaticVectorTemplate(Scalar x, Ts&&... ts) : _vec(Size)
                {
                    // Fold expression, which expands to _vec << x, t1, t2, t3, ..., tn;
                    ((_vec << x), ..., static_cast<Scalar>(ts));
                }

                /// Constructor from Vector
                StaticVectorTemplate(Vector vec) : _vec(vec)
                {
                    if (_vec.size() != Size)
                        throw std::invalid_argument(
                            "StaticVectorTemplate::Given argument vector is of the wrong size");
                }

                /// Access operator
                Scalar operator[](index_t i) const { return _vec[i]; }

                /// Access operator
                Scalar& operator[](index_t i) { return _vec[i]; }

                /// Conversion operator to Vector
                operator Vector() const { return _vec; }

                /// Conversion operator to Vector&& (rvalue reference)
                operator Vector &&() { return std::move(_vec); }

                /// Access to vector (const reference)
                const Vector& get() & { return _vec; }

                /// Access to vector (r-value reference)
                Vector&& get() && { return std::move(_vec); }

            private:
                Vector _vec;
            };

            template <index_t Size>
            using StaticRealVector = StaticVectorTemplate<Size, RealVector_t>;

            template <index_t Size>
            using StaticIndexVector = StaticVectorTemplate<Size, IndexVector_t>;
        } // namespace detail

        /// Strong Type for Geometry construction (Distance Source to Center of Rotation)
        class SourceToCenterOfRotation : detail::RealWrapper
        {
        public:
            using Base = detail::RealWrapper;

            using Base::Base;
            using Base::operator real_t;
        };

        /// Strong Type for Geometry construction (Distance Center of Rotation to Principal point)
        class CenterOfRotationToDetector : detail::RealWrapper
        {
        public:
            using Base = detail::RealWrapper;

            using Base::Base;
            using Base::operator real_t;
        };

        /// Strong Type for Geometry construction (1D Principal point offset)
        class PrincipalPointOffset : detail::RealWrapper
        {
        public:
            using Base = detail::RealWrapper;

            using Base::Base;
            using Base::operator real_t;
        };

        /// Strong Type for Geometry construction (2D Principal point offset)
        class PrincipalPointOffset2D : detail::StaticRealVector<2>
        {
        public:
            using Base = detail::StaticRealVector<2>;

            using Base::Base;
            using Base::operator[];
            using Base::operator=;
            // using Base::operator RealVector_t;
            using Base::get;
        };

        /// Strong Type for Geometry construction (nD Offset of Rotation axis)
        template <index_t Size>
        class RotationOffset : detail::StaticRealVector<Size>
        {
        public:
            using Base = detail::StaticRealVector<Size>;

            using Base::Base;
            using Base::operator[];
            using Base::operator=;
            // using Base::operator RealVector_t;
            using Base::get;
        };

        using RotationOffset1D = RotationOffset<1>; ///< Alias for 1D
        using RotationOffset2D = RotationOffset<2>; ///< Alias for 2D
        using RotationOffset3D = RotationOffset<3>; ///< Alias for 3D

        /// Strong Type for Geometry construction (nD Spacing)
        template <index_t Size>
        class Spacing : detail::StaticRealVector<Size>
        {
        public:
            using Base = detail::StaticRealVector<Size>;

            using Base::Base;
            using Base::operator[];
            using Base::operator=;
            // using Base::operator RealVector_t;
            using Base::get;
        };

        using Spacing1D = Spacing<1>; ///< Alias for 1D
        using Spacing2D = Spacing<2>; ///< Alias for 2D
        using Spacing3D = Spacing<3>; ///< Alias for 3D

        /// Strong type for Geometry construction (nD shift of origin)
        template <index_t Size>
        class OriginShift : detail::StaticRealVector<Size>
        {
        public:
            using Base = detail::StaticRealVector<Size>;

            using Base::Base;
            using Base::operator[];
            using Base::operator=;
            // using Base::operator RealVector_t;
            using Base::get;
        };

        using OriginShift1D = OriginShift<1>; ///< Alias for 1D
        using OriginShift2D = OriginShift<2>; ///< Alias for 2D
        using OriginShift3D = OriginShift<3>; ///< Alias for 3D

        /// Strong type for Geometry construction (nD coefficients)
        template <index_t Size>
        class Coefficients : detail::StaticIndexVector<Size>
        {
        public:
            using Base = detail::StaticIndexVector<Size>;

            using Base::Base;
            using Base::operator[];
            using Base::operator=;
            // using Base::operator IndexVector_t;
            using Base::get;
        };

        using Size1D = Coefficients<1>; ///< Alias for 1D
        using Size2D = Coefficients<2>; ///< Alias for 2D
        using Size3D = Coefficients<3>; ///< Alias for 3D

        namespace detail
        {
            /**
             * @brief Base type for strong typing volume and sinogram data
             *
             * @tparam Size Dimension of problem
             */
            template <index_t Size>
            class GeometryData
            {
                template <typename... Ts>
                using AllReal = typename std::enable_if_t<
                    std::conjunction<std::is_convertible<Ts, real_t>...>::value>;

            public:
                /// Alias for RealVector_t
                using Vector = RealVector_t;

                /// Default Constructor
                GeometryData() : _spacing(Size), _locationOfOrigin(Size) {}

                GeometryData(const GeometryData&) = default;
                GeometryData& operator=(const GeometryData&) = default;
                GeometryData(GeometryData&&) = default;
                GeometryData& operator=(GeometryData&&) = default;

                /// Constructor from Strong type Spacing and OriginShift
                GeometryData(Coefficients<Size> size)
                    : _spacing(Vector::Ones(Size)), _locationOfOrigin(Size)
                {
                    IndexVector_t coeffs = size.get();
                    _locationOfOrigin = static_cast<real_t>(0.5)
                                        * (coeffs.cast<real_t>().array() * _spacing.array());
                }

                /// Constructor from Strong type Spacing and OriginShift
                GeometryData(Coefficients<Size> size, Spacing<Size> spacing)
                    : _spacing(std::move(spacing.get())), _locationOfOrigin(Size)
                {
                    IndexVector_t coeffs = size.get();
                    _locationOfOrigin = static_cast<real_t>(0.5)
                                        * (coeffs.cast<real_t>().array() * _spacing.array());
                }

                /// Constructor from Strong type Spacing and OriginShift
                GeometryData(Spacing<Size> spacing, OriginShift<Size> origin)
                    : _spacing(std::move(spacing.get())), _locationOfOrigin(std::move(origin.get()))
                {
                }

                /// Constructor from RealVector_t for Spacing and Strong type OriginShift
                GeometryData(RealVector_t spacing, OriginShift<Size> origin)
                    : _spacing(std::move(spacing)), _locationOfOrigin(std::move(origin.get()))
                {
                    if (_spacing.size() != Size || _locationOfOrigin.size() != Size
                        || _spacing.size() != _locationOfOrigin.size())
                        throw std::invalid_argument(
                            "Spacing and Origin must have the same dimension");
                }

                /// Constructor from RealVector_t for origin shift and Strong type Spacing
                GeometryData(Spacing<Size> spacing, RealVector_t origin)
                    : _spacing(std::move(spacing.get())), _locationOfOrigin(std::move(origin))
                {
                    if (_spacing.size() != Size || _locationOfOrigin.size() != Size
                        || _spacing.size() != _locationOfOrigin.size())
                        throw std::invalid_argument(
                            "Spacing and Origin must have the same dimension");
                }

                /// Constructor from RealVector_t for spacing and origin shift
                GeometryData(RealVector_t spacing, RealVector_t origin)
                    : _spacing(std::move(spacing)), _locationOfOrigin(std::move(origin))
                {
                    if (_spacing.size() != Size || _locationOfOrigin.size() != Size
                        || _spacing.size() != _locationOfOrigin.size())
                        throw std::invalid_argument(
                            "Spacing and Origin must have the same dimension");
                }

                /// Getter for spacing (const reference)
                Vector getSpacing() const& { return _spacing; }

                /// Getter for spacing (r-value reference)
                Vector&& getSpacing() && { return std::move(_spacing); }

                /// Getter for origin shift/location of origin (const reference)
                Vector getLocationOfOrigin() const& { return _locationOfOrigin; }

                /// Getter for origin shift/location of origin (r-value reference)
                Vector&& getLocationOfOrigin() && { return std::move(_locationOfOrigin); }

                /// Get function (const reference overload) for structured bindings
                template <std::size_t N>
                decltype(auto) get() const&
                {
                    if constexpr (N == 0)
                        return (_spacing);
                    else if constexpr (N == 1)
                        return (_locationOfOrigin);
                }

                /// Get function (r-value reference overload) for structured bindings
                template <std::size_t N>
                decltype(auto) get() &&
                {
                    if constexpr (N == 0)
                        return std::move(_spacing);
                    else if constexpr (N == 1)
                        return std::move(_locationOfOrigin);
                }

            private:
                Vector _spacing = Vector::Zero(Size);
                Vector _locationOfOrigin = Vector::Zero(Size);
            };
        } // namespace detail

        /**
         * @brief Strong type for geometry data, describing volume/domain spacing and location of
         * origin.
         *
         * @tparam Size Dimension of problem
         *
         * We use private inheritance, to avoid retyping everything. The private members are made
         * public with using statements
         */
        template <index_t Size>
        class VolumeData : detail::GeometryData<Size>
        {
        public:
            using Base = detail::GeometryData<Size>;

            using Base::Base;
            using Base::getSpacing;
            using Base::getLocationOfOrigin;
            using Base::get;
        };

        using VolumeData2D = VolumeData<2>; ///< 2D volume data alias for 2D geometry
        using VolumeData3D = VolumeData<3>; ///< 3D volume data alias for 3D geometry

        /**
         * @brief Strong type for geometry data, describing sinogram/range spacing and location of
         * origin. Note sinogram data is expected to be 1 dimension less, than the actual problem!
         *
         * @tparam Size Dimension of problem
         *
         * We use private inheritance, to avoid retyping everything. The private members are made
         * public with using statements
         */
        template <index_t Size>
        class SinogramData : detail::GeometryData<Size>
        {
        public:
            using Base = detail::GeometryData<Size>;

            using Base::Base;
            using Base::getSpacing;
            using Base::getLocationOfOrigin;
            using Base::get;
        };

        using SinogramData2D = SinogramData<2>; ///< 2D sinogram data alias for 2D geometry
        using SinogramData3D = SinogramData<3>; ///< 3D sinogram data alias for 3D geometry

    } // namespace geometry
} // namespace elsa

/*
#davidfrank 2020-05-09
All of these functions are needed to enable structured bindings for SinogramData, VolumeData and
RotationAngles I didn't find a way to do it generally for all sizes (at least not with explicit
instantiation). This should be fine for now
*/

/// template specialization for class SinogramData (2D and 3D)
namespace std
{
    template <>
    struct tuple_size<elsa::geometry::SinogramData2D> : std::integral_constant<std::size_t, 2> {
    };

    template <std::size_t N>
    struct tuple_element<N, elsa::geometry::SinogramData2D> {
        using type = decltype(std::declval<elsa::geometry::SinogramData2D>().get<N>());
    };

    template <>
    struct tuple_size<elsa::geometry::SinogramData3D> : std::integral_constant<std::size_t, 2> {
    };

    template <std::size_t N>
    struct tuple_element<N, elsa::geometry::SinogramData3D> {
        using type = decltype(std::declval<elsa::geometry::SinogramData3D>().get<N>());
    };
} // namespace std

/// template specialization for class VolumeData (2D and 3D)
namespace std
{
    template <>
    struct tuple_size<elsa::geometry::VolumeData2D> : std::integral_constant<std::size_t, 2> {
    };

    template <std::size_t N>
    struct tuple_element<N, elsa::geometry::VolumeData2D> {
        using type = decltype(std::declval<elsa::geometry::VolumeData2D>().get<N>());
    };

    template <>
    struct tuple_size<elsa::geometry::VolumeData3D> : std::integral_constant<std::size_t, 2> {
    };

    template <std::size_t N>
    struct tuple_element<N, elsa::geometry::VolumeData3D> {
        using type = decltype(std::declval<elsa::geometry::VolumeData3D>().get<N>());
    };
} // namespace std

/// template specialization for class RotationAngles3D (2D and 3D)
namespace std
{
    template <>
    struct tuple_size<elsa::geometry::RotationAngles3D> : std::integral_constant<std::size_t, 3> {
    };

    template <std::size_t N>
    struct tuple_element<N, elsa::geometry::RotationAngles3D> {
        using type = decltype(std::declval<elsa::geometry::RotationAngles3D>().get<N>());
    };
692
} // namespace std