/* * Copyright (C) 2021 Christopher J. Howard * * This file is part of Antkeeper source code. * * Antkeeper source code is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * Antkeeper source code is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with Antkeeper source code. If not, see . */ #ifndef ANTKEEPER_MATH_VECTOR_TYPE_HPP #define ANTKEEPER_MATH_VECTOR_TYPE_HPP #include #include namespace math { /** * An `N`-dimensional Euclidean vector. * * @tparam T Vector component type. * @tparam N Number of dimensions. */ template struct vector { typedef T scalar_type; typedef std::array array_type; scalar_type components[N]; inline constexpr scalar_type& operator[](std::size_t i) noexcept { return components[i]; } inline constexpr const scalar_type& operator[](std::size_t i) const noexcept { return components[i]; } inline constexpr operator array_type&() noexcept { return reinterpret_cast(components[0]); } inline constexpr operator const array_type&() const noexcept { return reinterpret_cast(components[0]); } inline constexpr const scalar_type* data() const noexcept { return components; }; inline constexpr scalar_type* data() noexcept { return components; }; inline constexpr std::size_t size() const noexcept { return N; }; }; template struct vector { typedef T scalar_type; typedef std::array array_type; scalar_type x; inline constexpr scalar_type& operator[](std::size_t i) noexcept { return *((&x) + i); } inline constexpr const scalar_type& operator[](std::size_t i) const noexcept { return *((&x) + i); } inline constexpr operator array_type&() noexcept { return reinterpret_cast(x); } inline constexpr operator const array_type&() const noexcept { return reinterpret_cast(x); } inline constexpr const scalar_type* data() const noexcept { return &x; }; inline constexpr scalar_type* data() noexcept { return &x; }; inline constexpr std::size_t size() const noexcept { return 1; }; }; template struct vector { typedef T scalar_type; typedef std::array array_type; scalar_type x; scalar_type y; inline constexpr scalar_type& operator[](std::size_t i) noexcept { return *((&x) + i); } inline constexpr const scalar_type& operator[](std::size_t i) const noexcept { return *((&x) + i); } inline constexpr operator array_type&() noexcept { return reinterpret_cast(x); } inline constexpr operator const array_type&() const noexcept { return reinterpret_cast(x); } inline constexpr const scalar_type* data() const noexcept { return &x; }; inline constexpr scalar_type* data() noexcept { return &x; }; inline constexpr std::size_t size() const noexcept { return 2; }; }; template struct vector { typedef T scalar_type; typedef std::array array_type; scalar_type x; scalar_type y; scalar_type z; inline constexpr scalar_type& operator[](std::size_t i) noexcept { return *((&x) + i); } inline constexpr const scalar_type& operator[](std::size_t i) const noexcept { return *((&x) + i); } inline constexpr operator array_type&() noexcept { return reinterpret_cast(x); } inline constexpr operator const array_type&() const noexcept { return reinterpret_cast(x); } inline constexpr const scalar_type* data() const noexcept { return &x; }; inline constexpr scalar_type* data() noexcept { return &x; }; inline constexpr std::size_t size() const noexcept { return 3; }; }; template struct vector { typedef T scalar_type; typedef std::array array_type; scalar_type x; scalar_type y; scalar_type z; scalar_type w; inline constexpr scalar_type& operator[](std::size_t i) noexcept { return *((&x) + i); } inline constexpr const scalar_type& operator[](std::size_t i) const noexcept { return *((&x) + i); } inline constexpr operator array_type&() noexcept { return reinterpret_cast(x); } inline constexpr operator const array_type&() const noexcept { return reinterpret_cast(x); } inline constexpr const scalar_type* data() const noexcept { return &x; }; inline constexpr scalar_type* data() noexcept { return &x; }; inline constexpr std::size_t size() const noexcept { return 4; }; }; /// 2D vector. template using vector2 = vector; /// 3D vector. template using vector3 = vector; /// 4D vector. template using vector4 = vector; } // namespace math #endif // ANTKEEPER_MATH_VECTOR_TYPE_HPP