Support zero-dimensional points, matrices, and boxes
This commit is contained in:
parent
a4c1eb17e9
commit
5d968accdb
5 changed files with 42 additions and 25 deletions
|
|
@ -1,5 +1,6 @@
|
|||
#pragma once
|
||||
|
||||
#include <psemek/geom/detail/array.hpp>
|
||||
#include <psemek/geom/interval.hpp>
|
||||
#include <psemek/geom/point.hpp>
|
||||
|
||||
|
|
@ -9,7 +10,7 @@ namespace psemek::geom
|
|||
template <typename T, std::size_t N>
|
||||
struct box
|
||||
{
|
||||
interval<T> axes[N];
|
||||
detail::array<interval<T>, N>::type axes;
|
||||
|
||||
using point_type = point<T, N>;
|
||||
using vector_type = vector<T, N>;
|
||||
|
|
|
|||
33
libs/geom/include/psemek/geom/detail/array.hpp
Normal file
33
libs/geom/include/psemek/geom/detail/array.hpp
Normal file
|
|
@ -0,0 +1,33 @@
|
|||
#pragma once
|
||||
|
||||
#include <exception>
|
||||
|
||||
namespace psemek::geom::detail
|
||||
{
|
||||
|
||||
struct empty_array_exception
|
||||
: std::exception
|
||||
{
|
||||
char const * what() const noexcept
|
||||
{
|
||||
return "Attempt to index a zero vector";
|
||||
}
|
||||
};
|
||||
|
||||
template <typename T, std::size_t N>
|
||||
struct array
|
||||
{
|
||||
using type = T[N];
|
||||
};
|
||||
|
||||
template <typename T>
|
||||
struct array<T, 0>
|
||||
{
|
||||
struct type
|
||||
{
|
||||
T const & operator[](std::size_t) const { throw empty_array_exception{}; }
|
||||
T & operator[](std::size_t) { throw empty_array_exception{}; }
|
||||
};
|
||||
};
|
||||
|
||||
}
|
||||
|
|
@ -1,5 +1,6 @@
|
|||
#pragma once
|
||||
|
||||
#include <psemek/geom/detail/array.hpp>
|
||||
#include <psemek/geom/vector.hpp>
|
||||
|
||||
#include <algorithm>
|
||||
|
|
@ -15,7 +16,7 @@ namespace psemek::geom
|
|||
|
||||
using scalar_type = T;
|
||||
|
||||
T coords[R * C];
|
||||
detail::array<T, R * C>::type coords;
|
||||
|
||||
T * operator[](std::size_t i)
|
||||
{
|
||||
|
|
|
|||
|
|
@ -1,5 +1,6 @@
|
|||
#pragma once
|
||||
|
||||
#include <psemek/geom/detail/array.hpp>
|
||||
#include <psemek/geom/vector.hpp>
|
||||
|
||||
namespace psemek::geom
|
||||
|
|
@ -12,7 +13,7 @@ namespace psemek::geom
|
|||
|
||||
using scalar_type = T;
|
||||
|
||||
T coords[N];
|
||||
detail::array<T, N>::type coords;
|
||||
|
||||
point() = default;
|
||||
point(point const &) = default;
|
||||
|
|
|
|||
|
|
@ -1,5 +1,7 @@
|
|||
#pragma once
|
||||
|
||||
#include <psemek/geom/detail/array.hpp>
|
||||
|
||||
#include <cstddef>
|
||||
#include <utility>
|
||||
#include <type_traits>
|
||||
|
|
@ -9,27 +11,6 @@
|
|||
namespace psemek::geom
|
||||
{
|
||||
|
||||
namespace detail
|
||||
{
|
||||
|
||||
template <typename T, std::size_t N>
|
||||
struct empty_array_helper
|
||||
{
|
||||
using type = T[N];
|
||||
};
|
||||
|
||||
template <typename T>
|
||||
struct empty_array_helper<T, 0>
|
||||
{
|
||||
struct type
|
||||
{
|
||||
T const & operator[](std::size_t) const { throw std::runtime_error("Attempt to index a zero vector"); }
|
||||
T & operator[](std::size_t) { throw std::runtime_error("Attempt to index a zero vector"); }
|
||||
};
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
template <typename T, std::size_t N>
|
||||
struct vector
|
||||
{
|
||||
|
|
@ -37,7 +18,7 @@ namespace psemek::geom
|
|||
|
||||
using scalar_type = T;
|
||||
|
||||
detail::empty_array_helper<T, N>::type coords;
|
||||
detail::array<T, N>::type coords;
|
||||
|
||||
vector() = default;
|
||||
vector(vector const &) = default;
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue