2014-12-17 05:38:14 +00:00
|
|
|
// Licensed under GPLv2 or any later version
|
2014-07-27 18:07:45 +00:00
|
|
|
// Refer to the license.txt file included.
|
|
|
|
|
|
|
|
// Copyright 2014 Tony Wasserka
|
|
|
|
// All rights reserved.
|
|
|
|
//
|
|
|
|
// Redistribution and use in source and binary forms, with or without
|
|
|
|
// modification, are permitted provided that the following conditions are met:
|
|
|
|
//
|
|
|
|
// * Redistributions of source code must retain the above copyright
|
|
|
|
// notice, this list of conditions and the following disclaimer.
|
|
|
|
// * Redistributions in binary form must reproduce the above copyright
|
|
|
|
// notice, this list of conditions and the following disclaimer in the
|
|
|
|
// documentation and/or other materials provided with the distribution.
|
|
|
|
// * Neither the name of the owner nor the names of its contributors may
|
|
|
|
// be used to endorse or promote products derived from this software
|
|
|
|
// without specific prior written permission.
|
|
|
|
//
|
|
|
|
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
|
|
|
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
|
|
|
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
|
|
|
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
|
|
|
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
|
|
|
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
|
|
|
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
|
|
|
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
|
|
|
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|
|
|
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
|
|
|
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include <cmath>
|
2022-08-21 09:22:58 +00:00
|
|
|
#include <cstring>
|
2017-08-29 17:59:54 +00:00
|
|
|
#include <type_traits>
|
2019-08-08 04:25:24 +00:00
|
|
|
#include <boost/serialization/access.hpp>
|
2014-07-27 18:07:45 +00:00
|
|
|
|
2019-02-27 03:38:34 +00:00
|
|
|
namespace Common {
|
2014-07-27 18:07:45 +00:00
|
|
|
|
2016-09-18 00:38:01 +00:00
|
|
|
template <typename T>
|
|
|
|
class Vec2;
|
|
|
|
template <typename T>
|
|
|
|
class Vec3;
|
|
|
|
template <typename T>
|
|
|
|
class Vec4;
|
2014-07-27 18:07:45 +00:00
|
|
|
|
2016-09-18 00:38:01 +00:00
|
|
|
template <typename T>
|
2014-07-27 18:07:45 +00:00
|
|
|
class Vec2 {
|
2019-08-08 04:25:24 +00:00
|
|
|
friend class boost::serialization::access;
|
2019-12-27 21:07:29 +00:00
|
|
|
template <class Archive>
|
|
|
|
void serialize(Archive& ar, const unsigned int file_version) {
|
|
|
|
ar& x;
|
|
|
|
ar& y;
|
2019-08-08 04:25:24 +00:00
|
|
|
}
|
|
|
|
|
2014-07-27 18:07:45 +00:00
|
|
|
public:
|
2014-08-23 11:48:00 +00:00
|
|
|
T x;
|
|
|
|
T y;
|
2014-07-27 18:07:45 +00:00
|
|
|
|
2016-09-18 00:38:01 +00:00
|
|
|
T* AsArray() {
|
|
|
|
return &x;
|
|
|
|
}
|
2014-07-27 18:07:45 +00:00
|
|
|
|
2022-08-21 09:22:58 +00:00
|
|
|
const T* AsArray() const {
|
|
|
|
return &x;
|
|
|
|
}
|
|
|
|
|
2018-08-08 01:32:05 +00:00
|
|
|
constexpr Vec2() = default;
|
|
|
|
constexpr Vec2(const T& x_, const T& y_) : x(x_), y(y_) {}
|
2014-07-27 18:07:45 +00:00
|
|
|
|
2016-09-18 00:38:01 +00:00
|
|
|
template <typename T2>
|
2020-08-31 19:06:16 +00:00
|
|
|
[[nodiscard]] constexpr Vec2<T2> Cast() const {
|
2018-08-08 01:32:05 +00:00
|
|
|
return Vec2<T2>(static_cast<T2>(x), static_cast<T2>(y));
|
2014-07-27 18:07:45 +00:00
|
|
|
}
|
|
|
|
|
2020-08-31 19:06:16 +00:00
|
|
|
[[nodiscard]] static constexpr Vec2 AssignToAll(const T& f) {
|
2018-08-08 01:32:05 +00:00
|
|
|
return Vec2{f, f};
|
2014-07-27 18:07:45 +00:00
|
|
|
}
|
|
|
|
|
2020-08-31 19:06:16 +00:00
|
|
|
[[nodiscard]] constexpr Vec2<decltype(T{} + T{})> operator+(const Vec2& other) const {
|
2018-08-08 01:32:05 +00:00
|
|
|
return {x + other.x, y + other.y};
|
2014-07-27 18:07:45 +00:00
|
|
|
}
|
2018-08-08 01:32:05 +00:00
|
|
|
constexpr Vec2& operator+=(const Vec2& other) {
|
2016-09-18 00:38:01 +00:00
|
|
|
x += other.x;
|
|
|
|
y += other.y;
|
2018-08-08 01:32:05 +00:00
|
|
|
return *this;
|
2014-07-27 18:07:45 +00:00
|
|
|
}
|
2020-08-31 19:06:16 +00:00
|
|
|
[[nodiscard]] constexpr Vec2<decltype(T{} - T{})> operator-(const Vec2& other) const {
|
2018-08-08 01:32:05 +00:00
|
|
|
return {x - other.x, y - other.y};
|
2014-07-27 18:07:45 +00:00
|
|
|
}
|
2018-08-08 01:32:05 +00:00
|
|
|
constexpr Vec2& operator-=(const Vec2& other) {
|
2016-09-18 00:38:01 +00:00
|
|
|
x -= other.x;
|
|
|
|
y -= other.y;
|
2018-08-08 01:32:05 +00:00
|
|
|
return *this;
|
2014-07-27 18:07:45 +00:00
|
|
|
}
|
2017-07-11 16:51:29 +00:00
|
|
|
|
2017-08-29 17:59:54 +00:00
|
|
|
template <typename U = T>
|
2020-08-31 19:06:16 +00:00
|
|
|
[[nodiscard]] constexpr Vec2<std::enable_if_t<std::is_signed_v<U>, U>> operator-() const {
|
2018-08-08 01:32:05 +00:00
|
|
|
return {-x, -y};
|
2014-07-27 18:07:45 +00:00
|
|
|
}
|
2020-08-31 19:06:16 +00:00
|
|
|
[[nodiscard]] constexpr Vec2<decltype(T{} * T{})> operator*(const Vec2& other) const {
|
2018-08-08 01:32:05 +00:00
|
|
|
return {x * other.x, y * other.y};
|
2014-07-27 18:07:45 +00:00
|
|
|
}
|
2018-08-08 01:32:05 +00:00
|
|
|
|
2016-09-18 00:38:01 +00:00
|
|
|
template <typename V>
|
2020-08-31 19:06:16 +00:00
|
|
|
[[nodiscard]] constexpr Vec2<decltype(T{} * V{})> operator*(const V& f) const {
|
2018-08-08 01:32:05 +00:00
|
|
|
return {x * f, y * f};
|
2014-07-27 18:07:45 +00:00
|
|
|
}
|
2018-08-08 01:32:05 +00:00
|
|
|
|
2016-09-18 00:38:01 +00:00
|
|
|
template <typename V>
|
2018-08-08 01:32:05 +00:00
|
|
|
constexpr Vec2& operator*=(const V& f) {
|
2017-09-26 23:26:09 +00:00
|
|
|
*this = *this * f;
|
2018-08-08 01:32:05 +00:00
|
|
|
return *this;
|
2014-07-27 18:07:45 +00:00
|
|
|
}
|
2018-08-08 01:32:05 +00:00
|
|
|
|
2016-09-18 00:38:01 +00:00
|
|
|
template <typename V>
|
2020-08-31 19:06:16 +00:00
|
|
|
[[nodiscard]] constexpr Vec2<decltype(T{} / V{})> operator/(const V& f) const {
|
2018-08-08 01:32:05 +00:00
|
|
|
return {x / f, y / f};
|
2014-07-27 18:07:45 +00:00
|
|
|
}
|
2018-08-08 01:32:05 +00:00
|
|
|
|
2016-09-18 00:38:01 +00:00
|
|
|
template <typename V>
|
2018-08-08 01:32:05 +00:00
|
|
|
constexpr Vec2& operator/=(const V& f) {
|
2014-07-27 18:07:45 +00:00
|
|
|
*this = *this / f;
|
2018-08-08 01:32:05 +00:00
|
|
|
return *this;
|
2014-07-27 18:07:45 +00:00
|
|
|
}
|
|
|
|
|
2020-08-31 19:06:16 +00:00
|
|
|
[[nodiscard]] constexpr T Length2() const {
|
2016-09-18 00:38:01 +00:00
|
|
|
return x * x + y * y;
|
2014-07-27 18:07:45 +00:00
|
|
|
}
|
|
|
|
|
2022-08-21 09:22:58 +00:00
|
|
|
[[nodiscard]] constexpr bool operator!=(const Vec2& other) const {
|
|
|
|
return std::memcmp(AsArray(), other.AsArray(), sizeof(Vec2)) != 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
[[nodiscard]] constexpr bool operator==(const Vec2& other) const {
|
|
|
|
return std::memcmp(AsArray(), other.AsArray(), sizeof(Vec2)) == 0;
|
|
|
|
}
|
|
|
|
|
2014-07-27 18:07:45 +00:00
|
|
|
// Only implemented for T=float
|
2020-08-31 19:06:16 +00:00
|
|
|
[[nodiscard]] float Length() const;
|
2022-11-09 22:14:28 +00:00
|
|
|
float Normalize(); // returns the previous length, which is often useful
|
2014-07-27 18:07:45 +00:00
|
|
|
|
2020-08-31 19:06:16 +00:00
|
|
|
[[nodiscard]] constexpr T& operator[](std::size_t i) {
|
2014-07-27 18:07:45 +00:00
|
|
|
return *((&x) + i);
|
|
|
|
}
|
2020-08-31 19:06:16 +00:00
|
|
|
[[nodiscard]] constexpr const T& operator[](std::size_t i) const {
|
2014-07-27 18:07:45 +00:00
|
|
|
return *((&x) + i);
|
|
|
|
}
|
|
|
|
|
2018-08-08 01:32:05 +00:00
|
|
|
constexpr void SetZero() {
|
2016-09-18 00:38:01 +00:00
|
|
|
x = 0;
|
|
|
|
y = 0;
|
2014-07-27 18:07:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Common aliases: UV (texel coordinates), ST (texture coordinates)
|
2020-08-31 19:06:16 +00:00
|
|
|
[[nodiscard]] constexpr T& u() {
|
2016-09-18 00:38:01 +00:00
|
|
|
return x;
|
|
|
|
}
|
2020-08-31 19:06:16 +00:00
|
|
|
[[nodiscard]] constexpr T& v() {
|
2016-09-18 00:38:01 +00:00
|
|
|
return y;
|
|
|
|
}
|
2020-08-31 19:06:16 +00:00
|
|
|
[[nodiscard]] constexpr T& s() {
|
2016-09-18 00:38:01 +00:00
|
|
|
return x;
|
|
|
|
}
|
2020-08-31 19:06:16 +00:00
|
|
|
[[nodiscard]] constexpr T& t() {
|
2016-09-18 00:38:01 +00:00
|
|
|
return y;
|
|
|
|
}
|
2014-07-27 18:07:45 +00:00
|
|
|
|
2020-08-31 19:06:16 +00:00
|
|
|
[[nodiscard]] constexpr const T& u() const {
|
2016-09-18 00:38:01 +00:00
|
|
|
return x;
|
|
|
|
}
|
2020-08-31 19:06:16 +00:00
|
|
|
[[nodiscard]] constexpr const T& v() const {
|
2016-09-18 00:38:01 +00:00
|
|
|
return y;
|
|
|
|
}
|
2020-08-31 19:06:16 +00:00
|
|
|
[[nodiscard]] constexpr const T& s() const {
|
2016-09-18 00:38:01 +00:00
|
|
|
return x;
|
|
|
|
}
|
2020-08-31 19:06:16 +00:00
|
|
|
[[nodiscard]] constexpr const T& t() const {
|
2016-09-18 00:38:01 +00:00
|
|
|
return y;
|
|
|
|
}
|
2014-07-27 18:07:45 +00:00
|
|
|
|
|
|
|
// swizzlers - create a subvector of specific components
|
2020-08-31 19:06:16 +00:00
|
|
|
[[nodiscard]] constexpr Vec2 yx() const {
|
2016-09-18 00:38:01 +00:00
|
|
|
return Vec2(y, x);
|
|
|
|
}
|
2020-08-31 19:06:16 +00:00
|
|
|
[[nodiscard]] constexpr Vec2 vu() const {
|
2016-09-18 00:38:01 +00:00
|
|
|
return Vec2(y, x);
|
|
|
|
}
|
2020-08-31 19:06:16 +00:00
|
|
|
[[nodiscard]] constexpr Vec2 ts() const {
|
2016-09-18 00:38:01 +00:00
|
|
|
return Vec2(y, x);
|
|
|
|
}
|
2014-07-27 18:07:45 +00:00
|
|
|
};
|
|
|
|
|
2016-09-18 00:38:01 +00:00
|
|
|
template <typename T, typename V>
|
2020-08-31 19:06:16 +00:00
|
|
|
[[nodiscard]] constexpr Vec2<T> operator*(const V& f, const Vec2<T>& vec) {
|
2016-09-18 00:38:01 +00:00
|
|
|
return Vec2<T>(f * vec.x, f * vec.y);
|
2014-07-27 18:07:45 +00:00
|
|
|
}
|
|
|
|
|
2018-08-07 18:15:56 +00:00
|
|
|
using Vec2f = Vec2<float>;
|
2022-08-21 09:22:58 +00:00
|
|
|
using Vec2i = Vec2<int>;
|
|
|
|
using Vec2u = Vec2<unsigned int>;
|
2014-07-27 18:07:45 +00:00
|
|
|
|
2016-12-11 21:28:55 +00:00
|
|
|
template <>
|
|
|
|
inline float Vec2<float>::Length() const {
|
|
|
|
return std::sqrt(x * x + y * y);
|
|
|
|
}
|
|
|
|
|
|
|
|
template <>
|
|
|
|
inline float Vec2<float>::Normalize() {
|
|
|
|
float length = Length();
|
|
|
|
*this /= length;
|
|
|
|
return length;
|
|
|
|
}
|
|
|
|
|
2016-09-18 00:38:01 +00:00
|
|
|
template <typename T>
|
|
|
|
class Vec3 {
|
2019-08-08 04:25:24 +00:00
|
|
|
friend class boost::serialization::access;
|
2019-12-27 21:07:29 +00:00
|
|
|
template <class Archive>
|
|
|
|
void serialize(Archive& ar, const unsigned int file_version) {
|
|
|
|
ar& x;
|
|
|
|
ar& y;
|
|
|
|
ar& z;
|
2019-08-08 04:25:24 +00:00
|
|
|
}
|
|
|
|
|
2014-07-27 18:07:45 +00:00
|
|
|
public:
|
2014-08-23 11:48:00 +00:00
|
|
|
T x;
|
|
|
|
T y;
|
|
|
|
T z;
|
2014-07-27 18:07:45 +00:00
|
|
|
|
2016-09-18 00:38:01 +00:00
|
|
|
T* AsArray() {
|
|
|
|
return &x;
|
|
|
|
}
|
2014-07-27 18:07:45 +00:00
|
|
|
|
2022-08-21 09:22:58 +00:00
|
|
|
const T* AsArray() const {
|
|
|
|
return &x;
|
|
|
|
}
|
|
|
|
|
2018-08-08 01:32:05 +00:00
|
|
|
constexpr Vec3() = default;
|
|
|
|
constexpr Vec3(const T& x_, const T& y_, const T& z_) : x(x_), y(y_), z(z_) {}
|
2014-07-27 18:07:45 +00:00
|
|
|
|
2016-09-18 00:38:01 +00:00
|
|
|
template <typename T2>
|
2020-08-31 19:06:16 +00:00
|
|
|
[[nodiscard]] constexpr Vec3<T2> Cast() const {
|
2018-08-08 01:32:05 +00:00
|
|
|
return Vec3<T2>(static_cast<T2>(x), static_cast<T2>(y), static_cast<T2>(z));
|
2014-07-27 18:07:45 +00:00
|
|
|
}
|
|
|
|
|
2020-08-31 19:06:16 +00:00
|
|
|
[[nodiscard]] static constexpr Vec3 AssignToAll(const T& f) {
|
2018-08-08 01:32:05 +00:00
|
|
|
return Vec3(f, f, f);
|
2014-07-27 18:07:45 +00:00
|
|
|
}
|
|
|
|
|
2020-08-31 19:06:16 +00:00
|
|
|
[[nodiscard]] constexpr Vec3<decltype(T{} + T{})> operator+(const Vec3& other) const {
|
2018-08-08 01:32:05 +00:00
|
|
|
return {x + other.x, y + other.y, z + other.z};
|
2014-07-27 18:07:45 +00:00
|
|
|
}
|
|
|
|
|
2018-08-08 01:32:05 +00:00
|
|
|
constexpr Vec3& operator+=(const Vec3& other) {
|
2016-09-18 00:38:01 +00:00
|
|
|
x += other.x;
|
|
|
|
y += other.y;
|
|
|
|
z += other.z;
|
2018-08-08 01:32:05 +00:00
|
|
|
return *this;
|
2014-07-27 18:07:45 +00:00
|
|
|
}
|
2018-08-08 01:32:05 +00:00
|
|
|
|
2020-08-31 19:06:16 +00:00
|
|
|
[[nodiscard]] constexpr Vec3<decltype(T{} - T{})> operator-(const Vec3& other) const {
|
2018-08-08 01:32:05 +00:00
|
|
|
return {x - other.x, y - other.y, z - other.z};
|
2014-07-27 18:07:45 +00:00
|
|
|
}
|
2018-08-08 01:32:05 +00:00
|
|
|
|
|
|
|
constexpr Vec3& operator-=(const Vec3& other) {
|
2016-09-18 00:38:01 +00:00
|
|
|
x -= other.x;
|
|
|
|
y -= other.y;
|
|
|
|
z -= other.z;
|
2018-08-08 01:32:05 +00:00
|
|
|
return *this;
|
2014-07-27 18:07:45 +00:00
|
|
|
}
|
2017-07-11 16:51:29 +00:00
|
|
|
|
2017-08-29 17:59:54 +00:00
|
|
|
template <typename U = T>
|
2020-08-31 19:06:16 +00:00
|
|
|
[[nodiscard]] constexpr Vec3<std::enable_if_t<std::is_signed_v<U>, U>> operator-() const {
|
2018-08-08 01:32:05 +00:00
|
|
|
return {-x, -y, -z};
|
2014-07-27 18:07:45 +00:00
|
|
|
}
|
2018-08-08 01:32:05 +00:00
|
|
|
|
2020-08-31 19:06:16 +00:00
|
|
|
[[nodiscard]] constexpr Vec3<decltype(T{} * T{})> operator*(const Vec3& other) const {
|
2018-08-08 01:32:05 +00:00
|
|
|
return {x * other.x, y * other.y, z * other.z};
|
2014-07-27 18:07:45 +00:00
|
|
|
}
|
2018-08-08 01:32:05 +00:00
|
|
|
|
2016-09-18 00:38:01 +00:00
|
|
|
template <typename V>
|
2020-08-31 19:06:16 +00:00
|
|
|
[[nodiscard]] constexpr Vec3<decltype(T{} * V{})> operator*(const V& f) const {
|
2018-08-08 01:32:05 +00:00
|
|
|
return {x * f, y * f, z * f};
|
2014-07-27 18:07:45 +00:00
|
|
|
}
|
2018-08-08 01:32:05 +00:00
|
|
|
|
2016-09-18 00:38:01 +00:00
|
|
|
template <typename V>
|
2018-08-08 01:32:05 +00:00
|
|
|
constexpr Vec3& operator*=(const V& f) {
|
2017-09-26 23:26:09 +00:00
|
|
|
*this = *this * f;
|
2018-08-08 01:32:05 +00:00
|
|
|
return *this;
|
2014-07-27 18:07:45 +00:00
|
|
|
}
|
2016-09-18 00:38:01 +00:00
|
|
|
template <typename V>
|
2020-08-31 19:06:16 +00:00
|
|
|
[[nodiscard]] constexpr Vec3<decltype(T{} / V{})> operator/(const V& f) const {
|
2018-08-08 01:32:05 +00:00
|
|
|
return {x / f, y / f, z / f};
|
2014-07-27 18:07:45 +00:00
|
|
|
}
|
2018-08-08 01:32:05 +00:00
|
|
|
|
2016-09-18 00:38:01 +00:00
|
|
|
template <typename V>
|
2018-08-08 01:32:05 +00:00
|
|
|
constexpr Vec3& operator/=(const V& f) {
|
2014-07-27 18:07:45 +00:00
|
|
|
*this = *this / f;
|
2018-08-08 01:32:05 +00:00
|
|
|
return *this;
|
2014-07-27 18:07:45 +00:00
|
|
|
}
|
|
|
|
|
2022-08-21 09:22:58 +00:00
|
|
|
[[nodiscard]] constexpr bool operator!=(const Vec3& other) const {
|
|
|
|
return std::memcmp(AsArray(), other.AsArray(), sizeof(Vec3)) != 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
[[nodiscard]] constexpr bool operator==(const Vec3& other) const {
|
|
|
|
return std::memcmp(AsArray(), other.AsArray(), sizeof(Vec3)) == 0;
|
|
|
|
}
|
|
|
|
|
2020-08-31 19:06:16 +00:00
|
|
|
[[nodiscard]] constexpr T Length2() const {
|
2016-09-18 00:38:01 +00:00
|
|
|
return x * x + y * y + z * z;
|
2014-07-27 18:07:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Only implemented for T=float
|
2020-08-31 19:06:16 +00:00
|
|
|
[[nodiscard]] float Length() const;
|
|
|
|
[[nodiscard]] Vec3 Normalized() const;
|
2022-11-09 22:14:28 +00:00
|
|
|
float Normalize(); // returns the previous length, which is often useful
|
2014-07-27 18:07:45 +00:00
|
|
|
|
2020-08-31 19:06:16 +00:00
|
|
|
[[nodiscard]] constexpr T& operator[](std::size_t i) {
|
2014-07-27 18:07:45 +00:00
|
|
|
return *((&x) + i);
|
|
|
|
}
|
2018-08-08 01:32:05 +00:00
|
|
|
|
2020-08-31 19:06:16 +00:00
|
|
|
[[nodiscard]] constexpr const T& operator[](std::size_t i) const {
|
2014-07-27 18:07:45 +00:00
|
|
|
return *((&x) + i);
|
|
|
|
}
|
|
|
|
|
2018-08-08 01:32:05 +00:00
|
|
|
constexpr void SetZero() {
|
2016-09-18 00:38:01 +00:00
|
|
|
x = 0;
|
|
|
|
y = 0;
|
|
|
|
z = 0;
|
2014-07-27 18:07:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Common aliases: UVW (texel coordinates), RGB (colors), STQ (texture coordinates)
|
2020-08-31 19:06:16 +00:00
|
|
|
[[nodiscard]] constexpr T& u() {
|
2016-09-18 00:38:01 +00:00
|
|
|
return x;
|
|
|
|
}
|
2020-08-31 19:06:16 +00:00
|
|
|
[[nodiscard]] constexpr T& v() {
|
2016-09-18 00:38:01 +00:00
|
|
|
return y;
|
|
|
|
}
|
2020-08-31 19:06:16 +00:00
|
|
|
[[nodiscard]] constexpr T& w() {
|
2016-09-18 00:38:01 +00:00
|
|
|
return z;
|
|
|
|
}
|
2014-07-27 18:07:45 +00:00
|
|
|
|
2020-08-31 19:06:16 +00:00
|
|
|
[[nodiscard]] constexpr T& r() {
|
2016-09-18 00:38:01 +00:00
|
|
|
return x;
|
|
|
|
}
|
2020-08-31 19:06:16 +00:00
|
|
|
[[nodiscard]] constexpr T& g() {
|
2016-09-18 00:38:01 +00:00
|
|
|
return y;
|
|
|
|
}
|
2020-08-31 19:06:16 +00:00
|
|
|
[[nodiscard]] constexpr T& b() {
|
2016-09-18 00:38:01 +00:00
|
|
|
return z;
|
|
|
|
}
|
2014-07-27 18:07:45 +00:00
|
|
|
|
2020-08-31 19:06:16 +00:00
|
|
|
[[nodiscard]] constexpr T& s() {
|
2016-09-18 00:38:01 +00:00
|
|
|
return x;
|
|
|
|
}
|
2020-08-31 19:06:16 +00:00
|
|
|
[[nodiscard]] constexpr T& t() {
|
2016-09-18 00:38:01 +00:00
|
|
|
return y;
|
|
|
|
}
|
2020-08-31 19:06:16 +00:00
|
|
|
[[nodiscard]] constexpr T& q() {
|
2016-09-18 00:38:01 +00:00
|
|
|
return z;
|
|
|
|
}
|
2014-07-27 18:07:45 +00:00
|
|
|
|
2020-08-31 19:06:16 +00:00
|
|
|
[[nodiscard]] constexpr const T& u() const {
|
2016-09-18 00:38:01 +00:00
|
|
|
return x;
|
|
|
|
}
|
2020-08-31 19:06:16 +00:00
|
|
|
[[nodiscard]] constexpr const T& v() const {
|
2016-09-18 00:38:01 +00:00
|
|
|
return y;
|
|
|
|
}
|
2020-08-31 19:06:16 +00:00
|
|
|
[[nodiscard]] constexpr const T& w() const {
|
2016-09-18 00:38:01 +00:00
|
|
|
return z;
|
|
|
|
}
|
2014-07-27 18:07:45 +00:00
|
|
|
|
2020-08-31 19:06:16 +00:00
|
|
|
[[nodiscard]] constexpr const T& r() const {
|
2016-09-18 00:38:01 +00:00
|
|
|
return x;
|
|
|
|
}
|
2020-08-31 19:06:16 +00:00
|
|
|
[[nodiscard]] constexpr const T& g() const {
|
2016-09-18 00:38:01 +00:00
|
|
|
return y;
|
|
|
|
}
|
2020-08-31 19:06:16 +00:00
|
|
|
[[nodiscard]] constexpr const T& b() const {
|
2016-09-18 00:38:01 +00:00
|
|
|
return z;
|
|
|
|
}
|
2014-07-27 18:07:45 +00:00
|
|
|
|
2020-08-31 19:06:16 +00:00
|
|
|
[[nodiscard]] constexpr const T& s() const {
|
2016-09-18 00:38:01 +00:00
|
|
|
return x;
|
|
|
|
}
|
2020-08-31 19:06:16 +00:00
|
|
|
[[nodiscard]] constexpr const T& t() const {
|
2016-09-18 00:38:01 +00:00
|
|
|
return y;
|
|
|
|
}
|
2020-08-31 19:06:16 +00:00
|
|
|
[[nodiscard]] constexpr const T& q() const {
|
2016-09-18 00:38:01 +00:00
|
|
|
return z;
|
|
|
|
}
|
2014-07-27 18:07:45 +00:00
|
|
|
|
2016-09-18 00:38:01 +00:00
|
|
|
// swizzlers - create a subvector of specific components
|
|
|
|
// e.g. Vec2 uv() { return Vec2(x,y); }
|
|
|
|
// _DEFINE_SWIZZLER2 defines a single such function, DEFINE_SWIZZLER2 defines all of them for all
|
|
|
|
// component names (x<->r) and permutations (xy<->yx)
|
|
|
|
#define _DEFINE_SWIZZLER2(a, b, name) \
|
2023-09-27 16:42:19 +00:00
|
|
|
[[nodiscard]] constexpr Vec2<T> name() const { return Vec2<T>(a, b); }
|
2016-09-18 00:38:01 +00:00
|
|
|
#define DEFINE_SWIZZLER2(a, b, a2, b2, a3, b3, a4, b4) \
|
|
|
|
_DEFINE_SWIZZLER2(a, b, a##b); \
|
|
|
|
_DEFINE_SWIZZLER2(a, b, a2##b2); \
|
|
|
|
_DEFINE_SWIZZLER2(a, b, a3##b3); \
|
|
|
|
_DEFINE_SWIZZLER2(a, b, a4##b4); \
|
|
|
|
_DEFINE_SWIZZLER2(b, a, b##a); \
|
|
|
|
_DEFINE_SWIZZLER2(b, a, b2##a2); \
|
|
|
|
_DEFINE_SWIZZLER2(b, a, b3##a3); \
|
2014-08-23 11:48:00 +00:00
|
|
|
_DEFINE_SWIZZLER2(b, a, b4##a4)
|
2014-07-27 18:07:45 +00:00
|
|
|
|
|
|
|
DEFINE_SWIZZLER2(x, y, r, g, u, v, s, t);
|
|
|
|
DEFINE_SWIZZLER2(x, z, r, b, u, w, s, q);
|
|
|
|
DEFINE_SWIZZLER2(y, z, g, b, v, w, t, q);
|
|
|
|
#undef DEFINE_SWIZZLER2
|
|
|
|
#undef _DEFINE_SWIZZLER2
|
|
|
|
};
|
|
|
|
|
2016-09-18 00:38:01 +00:00
|
|
|
template <typename T, typename V>
|
2020-08-31 19:06:16 +00:00
|
|
|
[[nodiscard]] constexpr Vec3<T> operator*(const V& f, const Vec3<T>& vec) {
|
2016-09-18 00:38:01 +00:00
|
|
|
return Vec3<T>(f * vec.x, f * vec.y, f * vec.z);
|
2014-07-27 18:07:45 +00:00
|
|
|
}
|
|
|
|
|
2016-09-18 00:38:01 +00:00
|
|
|
template <>
|
2014-08-23 11:48:00 +00:00
|
|
|
inline float Vec3<float>::Length() const {
|
|
|
|
return std::sqrt(x * x + y * y + z * z);
|
|
|
|
}
|
|
|
|
|
2016-09-18 00:38:01 +00:00
|
|
|
template <>
|
2014-08-23 11:48:00 +00:00
|
|
|
inline Vec3<float> Vec3<float>::Normalized() const {
|
|
|
|
return *this / Length();
|
|
|
|
}
|
|
|
|
|
2016-12-11 21:28:55 +00:00
|
|
|
template <>
|
|
|
|
inline float Vec3<float>::Normalize() {
|
|
|
|
float length = Length();
|
|
|
|
*this /= length;
|
|
|
|
return length;
|
|
|
|
}
|
|
|
|
|
2018-08-07 18:15:56 +00:00
|
|
|
using Vec3f = Vec3<float>;
|
2022-08-21 09:22:58 +00:00
|
|
|
using Vec3i = Vec3<int>;
|
|
|
|
using Vec3u = Vec3<unsigned int>;
|
2014-07-27 18:07:45 +00:00
|
|
|
|
2016-09-18 00:38:01 +00:00
|
|
|
template <typename T>
|
|
|
|
class Vec4 {
|
2019-08-08 04:25:24 +00:00
|
|
|
friend class boost::serialization::access;
|
2019-12-27 21:07:29 +00:00
|
|
|
template <class Archive>
|
|
|
|
void serialize(Archive& ar, const unsigned int file_version) {
|
|
|
|
ar& x;
|
|
|
|
ar& y;
|
|
|
|
ar& z;
|
|
|
|
ar& w;
|
2019-08-08 04:25:24 +00:00
|
|
|
}
|
|
|
|
|
2014-07-27 18:07:45 +00:00
|
|
|
public:
|
2014-08-23 11:48:00 +00:00
|
|
|
T x;
|
|
|
|
T y;
|
|
|
|
T z;
|
|
|
|
T w;
|
2014-07-27 18:07:45 +00:00
|
|
|
|
2016-09-18 00:38:01 +00:00
|
|
|
T* AsArray() {
|
|
|
|
return &x;
|
|
|
|
}
|
2014-07-27 18:07:45 +00:00
|
|
|
|
2022-08-21 09:22:58 +00:00
|
|
|
const T* AsArray() const {
|
|
|
|
return &x;
|
|
|
|
}
|
|
|
|
|
2018-08-08 01:32:05 +00:00
|
|
|
constexpr Vec4() = default;
|
|
|
|
constexpr Vec4(const T& x_, const T& y_, const T& z_, const T& w_)
|
|
|
|
: x(x_), y(y_), z(z_), w(w_) {}
|
2014-07-27 18:07:45 +00:00
|
|
|
|
2016-09-18 00:38:01 +00:00
|
|
|
template <typename T2>
|
2020-08-31 19:06:16 +00:00
|
|
|
[[nodiscard]] constexpr Vec4<T2> Cast() const {
|
2018-08-08 01:32:05 +00:00
|
|
|
return Vec4<T2>(static_cast<T2>(x), static_cast<T2>(y), static_cast<T2>(z),
|
|
|
|
static_cast<T2>(w));
|
2014-07-27 18:07:45 +00:00
|
|
|
}
|
|
|
|
|
2020-08-31 19:06:16 +00:00
|
|
|
[[nodiscard]] static constexpr Vec4 AssignToAll(const T& f) {
|
2018-08-08 01:32:05 +00:00
|
|
|
return Vec4(f, f, f, f);
|
2014-07-27 18:07:45 +00:00
|
|
|
}
|
|
|
|
|
2020-08-31 19:06:16 +00:00
|
|
|
[[nodiscard]] constexpr Vec4<decltype(T{} + T{})> operator+(const Vec4& other) const {
|
2018-08-08 01:32:05 +00:00
|
|
|
return {x + other.x, y + other.y, z + other.z, w + other.w};
|
2014-07-27 18:07:45 +00:00
|
|
|
}
|
|
|
|
|
2018-08-08 01:32:05 +00:00
|
|
|
constexpr Vec4& operator+=(const Vec4& other) {
|
2016-09-18 00:38:01 +00:00
|
|
|
x += other.x;
|
|
|
|
y += other.y;
|
|
|
|
z += other.z;
|
|
|
|
w += other.w;
|
2018-08-08 01:32:05 +00:00
|
|
|
return *this;
|
2014-07-27 18:07:45 +00:00
|
|
|
}
|
2018-08-08 01:32:05 +00:00
|
|
|
|
2020-08-31 19:06:16 +00:00
|
|
|
[[nodiscard]] constexpr Vec4<decltype(T{} - T{})> operator-(const Vec4& other) const {
|
2018-08-08 01:32:05 +00:00
|
|
|
return {x - other.x, y - other.y, z - other.z, w - other.w};
|
2014-07-27 18:07:45 +00:00
|
|
|
}
|
2018-08-08 01:32:05 +00:00
|
|
|
|
|
|
|
constexpr Vec4& operator-=(const Vec4& other) {
|
2016-09-18 00:38:01 +00:00
|
|
|
x -= other.x;
|
|
|
|
y -= other.y;
|
|
|
|
z -= other.z;
|
|
|
|
w -= other.w;
|
2018-08-08 01:32:05 +00:00
|
|
|
return *this;
|
2014-07-27 18:07:45 +00:00
|
|
|
}
|
2017-07-11 17:08:56 +00:00
|
|
|
|
2017-08-29 17:59:54 +00:00
|
|
|
template <typename U = T>
|
2020-08-31 19:06:16 +00:00
|
|
|
[[nodiscard]] constexpr Vec4<std::enable_if_t<std::is_signed_v<U>, U>> operator-() const {
|
2018-08-08 01:32:05 +00:00
|
|
|
return {-x, -y, -z, -w};
|
2014-07-27 18:07:45 +00:00
|
|
|
}
|
2018-08-08 01:32:05 +00:00
|
|
|
|
2020-08-31 19:06:16 +00:00
|
|
|
[[nodiscard]] constexpr Vec4<decltype(T{} * T{})> operator*(const Vec4& other) const {
|
2018-08-08 01:32:05 +00:00
|
|
|
return {x * other.x, y * other.y, z * other.z, w * other.w};
|
2014-07-27 18:07:45 +00:00
|
|
|
}
|
2018-08-08 01:32:05 +00:00
|
|
|
|
2016-09-18 00:38:01 +00:00
|
|
|
template <typename V>
|
2020-08-31 19:06:16 +00:00
|
|
|
[[nodiscard]] constexpr Vec4<decltype(T{} * V{})> operator*(const V& f) const {
|
2018-08-08 01:32:05 +00:00
|
|
|
return {x * f, y * f, z * f, w * f};
|
2014-07-27 18:07:45 +00:00
|
|
|
}
|
2018-08-08 01:32:05 +00:00
|
|
|
|
2016-09-18 00:38:01 +00:00
|
|
|
template <typename V>
|
2018-08-08 01:32:05 +00:00
|
|
|
constexpr Vec4& operator*=(const V& f) {
|
2017-09-26 23:26:09 +00:00
|
|
|
*this = *this * f;
|
2018-08-08 01:32:05 +00:00
|
|
|
return *this;
|
2014-07-27 18:07:45 +00:00
|
|
|
}
|
2018-08-08 01:32:05 +00:00
|
|
|
|
2016-09-18 00:38:01 +00:00
|
|
|
template <typename V>
|
2020-08-31 19:06:16 +00:00
|
|
|
[[nodiscard]] constexpr Vec4<decltype(T{} / V{})> operator/(const V& f) const {
|
2018-08-08 01:32:05 +00:00
|
|
|
return {x / f, y / f, z / f, w / f};
|
2014-07-27 18:07:45 +00:00
|
|
|
}
|
2018-08-08 01:32:05 +00:00
|
|
|
|
2016-09-18 00:38:01 +00:00
|
|
|
template <typename V>
|
2018-08-08 01:32:05 +00:00
|
|
|
constexpr Vec4& operator/=(const V& f) {
|
2014-07-27 18:07:45 +00:00
|
|
|
*this = *this / f;
|
2018-08-08 01:32:05 +00:00
|
|
|
return *this;
|
2014-07-27 18:07:45 +00:00
|
|
|
}
|
|
|
|
|
2022-08-21 09:22:58 +00:00
|
|
|
[[nodiscard]] constexpr bool operator!=(const Vec4& other) const {
|
|
|
|
return std::memcmp(AsArray(), other.AsArray(), sizeof(Vec4)) != 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
[[nodiscard]] constexpr bool operator==(const Vec4& other) const {
|
|
|
|
return std::memcmp(AsArray(), other.AsArray(), sizeof(Vec4)) == 0;
|
|
|
|
}
|
|
|
|
|
2020-08-31 19:06:16 +00:00
|
|
|
[[nodiscard]] constexpr T Length2() const {
|
2016-09-18 00:38:01 +00:00
|
|
|
return x * x + y * y + z * z + w * w;
|
2014-07-27 18:07:45 +00:00
|
|
|
}
|
|
|
|
|
2020-08-31 19:06:16 +00:00
|
|
|
[[nodiscard]] constexpr T& operator[](std::size_t i) {
|
2014-07-27 18:07:45 +00:00
|
|
|
return *((&x) + i);
|
|
|
|
}
|
2018-08-08 01:32:05 +00:00
|
|
|
|
2020-08-31 19:06:16 +00:00
|
|
|
[[nodiscard]] constexpr const T& operator[](std::size_t i) const {
|
2014-07-27 18:07:45 +00:00
|
|
|
return *((&x) + i);
|
|
|
|
}
|
|
|
|
|
2018-08-08 01:32:05 +00:00
|
|
|
constexpr void SetZero() {
|
2016-03-18 05:49:00 +00:00
|
|
|
x = 0;
|
|
|
|
y = 0;
|
|
|
|
z = 0;
|
|
|
|
w = 0;
|
2014-07-27 18:07:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Common alias: RGBA (colors)
|
2020-08-31 19:06:16 +00:00
|
|
|
[[nodiscard]] constexpr T& r() {
|
2016-09-18 00:38:01 +00:00
|
|
|
return x;
|
|
|
|
}
|
2020-08-31 19:06:16 +00:00
|
|
|
[[nodiscard]] constexpr T& g() {
|
2016-09-18 00:38:01 +00:00
|
|
|
return y;
|
|
|
|
}
|
2020-08-31 19:06:16 +00:00
|
|
|
[[nodiscard]] constexpr T& b() {
|
2016-09-18 00:38:01 +00:00
|
|
|
return z;
|
|
|
|
}
|
2020-08-31 19:06:16 +00:00
|
|
|
[[nodiscard]] constexpr T& a() {
|
2016-09-18 00:38:01 +00:00
|
|
|
return w;
|
|
|
|
}
|
|
|
|
|
2020-08-31 19:06:16 +00:00
|
|
|
[[nodiscard]] constexpr const T& r() const {
|
2016-09-18 00:38:01 +00:00
|
|
|
return x;
|
|
|
|
}
|
2020-08-31 19:06:16 +00:00
|
|
|
[[nodiscard]] constexpr const T& g() const {
|
2016-09-18 00:38:01 +00:00
|
|
|
return y;
|
|
|
|
}
|
2020-08-31 19:06:16 +00:00
|
|
|
[[nodiscard]] constexpr const T& b() const {
|
2016-09-18 00:38:01 +00:00
|
|
|
return z;
|
|
|
|
}
|
2020-08-31 19:06:16 +00:00
|
|
|
[[nodiscard]] constexpr const T& a() const {
|
2016-09-18 00:38:01 +00:00
|
|
|
return w;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Swizzlers - Create a subvector of specific components
|
|
|
|
// e.g. Vec2 uv() { return Vec2(x,y); }
|
|
|
|
|
|
|
|
// _DEFINE_SWIZZLER2 defines a single such function
|
|
|
|
// DEFINE_SWIZZLER2_COMP1 defines one-component functions for all component names (x<->r)
|
|
|
|
// DEFINE_SWIZZLER2_COMP2 defines two component functions for all component names (x<->r) and
|
|
|
|
// permutations (xy<->yx)
|
|
|
|
#define _DEFINE_SWIZZLER2(a, b, name) \
|
2023-09-27 16:42:19 +00:00
|
|
|
[[nodiscard]] constexpr Vec2<T> name() const { return Vec2<T>(a, b); }
|
2016-09-18 00:38:01 +00:00
|
|
|
#define DEFINE_SWIZZLER2_COMP1(a, a2) \
|
|
|
|
_DEFINE_SWIZZLER2(a, a, a##a); \
|
2015-02-03 23:24:29 +00:00
|
|
|
_DEFINE_SWIZZLER2(a, a, a2##a2)
|
2016-09-18 00:38:01 +00:00
|
|
|
#define DEFINE_SWIZZLER2_COMP2(a, b, a2, b2) \
|
|
|
|
_DEFINE_SWIZZLER2(a, b, a##b); \
|
|
|
|
_DEFINE_SWIZZLER2(a, b, a2##b2); \
|
|
|
|
_DEFINE_SWIZZLER2(b, a, b##a); \
|
2014-08-23 11:48:00 +00:00
|
|
|
_DEFINE_SWIZZLER2(b, a, b2##a2)
|
2014-07-27 18:07:45 +00:00
|
|
|
|
2015-02-03 23:24:29 +00:00
|
|
|
DEFINE_SWIZZLER2_COMP2(x, y, r, g);
|
|
|
|
DEFINE_SWIZZLER2_COMP2(x, z, r, b);
|
|
|
|
DEFINE_SWIZZLER2_COMP2(x, w, r, a);
|
|
|
|
DEFINE_SWIZZLER2_COMP2(y, z, g, b);
|
|
|
|
DEFINE_SWIZZLER2_COMP2(y, w, g, a);
|
|
|
|
DEFINE_SWIZZLER2_COMP2(z, w, b, a);
|
|
|
|
DEFINE_SWIZZLER2_COMP1(x, r);
|
|
|
|
DEFINE_SWIZZLER2_COMP1(y, g);
|
|
|
|
DEFINE_SWIZZLER2_COMP1(z, b);
|
|
|
|
DEFINE_SWIZZLER2_COMP1(w, a);
|
|
|
|
#undef DEFINE_SWIZZLER2_COMP1
|
|
|
|
#undef DEFINE_SWIZZLER2_COMP2
|
2014-07-27 18:07:45 +00:00
|
|
|
#undef _DEFINE_SWIZZLER2
|
|
|
|
|
2016-09-18 00:38:01 +00:00
|
|
|
#define _DEFINE_SWIZZLER3(a, b, c, name) \
|
2023-09-27 16:42:19 +00:00
|
|
|
[[nodiscard]] constexpr Vec3<T> name() const { return Vec3<T>(a, b, c); }
|
2016-09-18 00:38:01 +00:00
|
|
|
#define DEFINE_SWIZZLER3_COMP1(a, a2) \
|
|
|
|
_DEFINE_SWIZZLER3(a, a, a, a##a##a); \
|
2015-02-03 23:24:29 +00:00
|
|
|
_DEFINE_SWIZZLER3(a, a, a, a2##a2##a2)
|
2016-09-18 00:38:01 +00:00
|
|
|
#define DEFINE_SWIZZLER3_COMP3(a, b, c, a2, b2, c2) \
|
|
|
|
_DEFINE_SWIZZLER3(a, b, c, a##b##c); \
|
|
|
|
_DEFINE_SWIZZLER3(a, c, b, a##c##b); \
|
|
|
|
_DEFINE_SWIZZLER3(b, a, c, b##a##c); \
|
|
|
|
_DEFINE_SWIZZLER3(b, c, a, b##c##a); \
|
|
|
|
_DEFINE_SWIZZLER3(c, a, b, c##a##b); \
|
|
|
|
_DEFINE_SWIZZLER3(c, b, a, c##b##a); \
|
|
|
|
_DEFINE_SWIZZLER3(a, b, c, a2##b2##c2); \
|
|
|
|
_DEFINE_SWIZZLER3(a, c, b, a2##c2##b2); \
|
|
|
|
_DEFINE_SWIZZLER3(b, a, c, b2##a2##c2); \
|
|
|
|
_DEFINE_SWIZZLER3(b, c, a, b2##c2##a2); \
|
|
|
|
_DEFINE_SWIZZLER3(c, a, b, c2##a2##b2); \
|
2014-08-23 11:48:00 +00:00
|
|
|
_DEFINE_SWIZZLER3(c, b, a, c2##b2##a2)
|
2014-07-27 18:07:45 +00:00
|
|
|
|
2015-02-03 23:24:29 +00:00
|
|
|
DEFINE_SWIZZLER3_COMP3(x, y, z, r, g, b);
|
|
|
|
DEFINE_SWIZZLER3_COMP3(x, y, w, r, g, a);
|
|
|
|
DEFINE_SWIZZLER3_COMP3(x, z, w, r, b, a);
|
|
|
|
DEFINE_SWIZZLER3_COMP3(y, z, w, g, b, a);
|
|
|
|
DEFINE_SWIZZLER3_COMP1(x, r);
|
|
|
|
DEFINE_SWIZZLER3_COMP1(y, g);
|
|
|
|
DEFINE_SWIZZLER3_COMP1(z, b);
|
|
|
|
DEFINE_SWIZZLER3_COMP1(w, a);
|
|
|
|
#undef DEFINE_SWIZZLER3_COMP1
|
|
|
|
#undef DEFINE_SWIZZLER3_COMP3
|
2014-07-27 18:07:45 +00:00
|
|
|
#undef _DEFINE_SWIZZLER3
|
|
|
|
};
|
|
|
|
|
2016-09-18 00:38:01 +00:00
|
|
|
template <typename T, typename V>
|
2020-08-31 19:06:16 +00:00
|
|
|
[[nodiscard]] constexpr Vec4<decltype(V{} * T{})> operator*(const V& f, const Vec4<T>& vec) {
|
2018-08-08 01:32:05 +00:00
|
|
|
return {f * vec.x, f * vec.y, f * vec.z, f * vec.w};
|
2014-07-27 18:07:45 +00:00
|
|
|
}
|
|
|
|
|
2018-08-07 18:15:56 +00:00
|
|
|
using Vec4f = Vec4<float>;
|
2022-08-21 09:22:58 +00:00
|
|
|
using Vec4i = Vec4<int>;
|
|
|
|
using Vec4u = Vec4<unsigned int>;
|
2014-07-27 18:07:45 +00:00
|
|
|
|
2016-09-18 00:38:01 +00:00
|
|
|
template <typename T>
|
2018-08-08 01:32:05 +00:00
|
|
|
constexpr decltype(T{} * T{} + T{} * T{}) Dot(const Vec2<T>& a, const Vec2<T>& b) {
|
2016-09-18 00:38:01 +00:00
|
|
|
return a.x * b.x + a.y * b.y;
|
2014-07-27 18:07:45 +00:00
|
|
|
}
|
|
|
|
|
2016-09-18 00:38:01 +00:00
|
|
|
template <typename T>
|
2020-08-31 19:06:16 +00:00
|
|
|
[[nodiscard]] constexpr decltype(T{} * T{} + T{} * T{}) Dot(const Vec3<T>& a, const Vec3<T>& b) {
|
2016-09-18 00:38:01 +00:00
|
|
|
return a.x * b.x + a.y * b.y + a.z * b.z;
|
2014-07-27 18:07:45 +00:00
|
|
|
}
|
|
|
|
|
2016-09-18 00:38:01 +00:00
|
|
|
template <typename T>
|
2020-08-31 19:06:16 +00:00
|
|
|
[[nodiscard]] constexpr decltype(T{} * T{} + T{} * T{}) Dot(const Vec4<T>& a, const Vec4<T>& b) {
|
2016-09-18 00:38:01 +00:00
|
|
|
return a.x * b.x + a.y * b.y + a.z * b.z + a.w * b.w;
|
2014-07-27 18:07:45 +00:00
|
|
|
}
|
|
|
|
|
2016-09-18 00:38:01 +00:00
|
|
|
template <typename T>
|
2020-08-31 19:06:16 +00:00
|
|
|
[[nodiscard]] constexpr Vec3<decltype(T{} * T{} - T{} * T{})> Cross(const Vec3<T>& a,
|
|
|
|
const Vec3<T>& b) {
|
2018-08-08 01:32:05 +00:00
|
|
|
return {a.y * b.z - a.z * b.y, a.z * b.x - a.x * b.z, a.x * b.y - a.y * b.x};
|
2014-07-27 18:07:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// linear interpolation via float: 0.0=begin, 1.0=end
|
2016-09-18 00:38:01 +00:00
|
|
|
template <typename X>
|
2023-09-27 16:42:19 +00:00
|
|
|
[[nodiscard]] constexpr decltype(X{} * float{} + X{} * float{}) Lerp(const X& begin, const X& end,
|
|
|
|
const float t) {
|
2016-09-18 00:38:01 +00:00
|
|
|
return begin * (1.f - t) + end * t;
|
2014-07-27 18:07:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// linear interpolation via int: 0=begin, base=end
|
2016-09-18 00:38:01 +00:00
|
|
|
template <typename X, int base>
|
2020-08-31 19:06:16 +00:00
|
|
|
[[nodiscard]] constexpr decltype((X{} * int{} + X{} * int{}) / base) LerpInt(const X& begin,
|
|
|
|
const X& end,
|
|
|
|
const int t) {
|
2016-09-18 00:38:01 +00:00
|
|
|
return (begin * (base - t) + end * t) / base;
|
2014-07-27 18:07:45 +00:00
|
|
|
}
|
|
|
|
|
2017-04-17 07:01:45 +00:00
|
|
|
// bilinear interpolation. s is for interpolating x00-x01 and x10-x11, and t is for the second
|
|
|
|
// interpolation.
|
|
|
|
template <typename X>
|
2020-08-31 19:06:16 +00:00
|
|
|
[[nodiscard]] constexpr auto BilinearInterp(const X& x00, const X& x01, const X& x10, const X& x11,
|
|
|
|
const float s, const float t) {
|
2017-04-17 07:01:45 +00:00
|
|
|
auto y0 = Lerp(x00, x01, s);
|
|
|
|
auto y1 = Lerp(x10, x11, s);
|
|
|
|
return Lerp(y0, y1, t);
|
|
|
|
}
|
|
|
|
|
2014-07-27 18:07:45 +00:00
|
|
|
// Utility vector factories
|
2016-09-18 00:38:01 +00:00
|
|
|
template <typename T>
|
2020-08-31 19:06:16 +00:00
|
|
|
[[nodiscard]] constexpr Vec2<T> MakeVec(const T& x, const T& y) {
|
2014-07-27 18:07:45 +00:00
|
|
|
return Vec2<T>{x, y};
|
|
|
|
}
|
|
|
|
|
2016-09-18 00:38:01 +00:00
|
|
|
template <typename T>
|
2020-08-31 19:06:16 +00:00
|
|
|
[[nodiscard]] constexpr Vec3<T> MakeVec(const T& x, const T& y, const T& z) {
|
2014-07-27 18:07:45 +00:00
|
|
|
return Vec3<T>{x, y, z};
|
|
|
|
}
|
|
|
|
|
2016-09-18 00:38:01 +00:00
|
|
|
template <typename T>
|
2020-08-31 19:06:16 +00:00
|
|
|
[[nodiscard]] constexpr Vec4<T> MakeVec(const T& x, const T& y, const Vec2<T>& zw) {
|
2014-08-12 18:04:28 +00:00
|
|
|
return MakeVec(x, y, zw[0], zw[1]);
|
|
|
|
}
|
|
|
|
|
2016-09-18 00:38:01 +00:00
|
|
|
template <typename T>
|
2020-08-31 19:06:16 +00:00
|
|
|
[[nodiscard]] constexpr Vec3<T> MakeVec(const Vec2<T>& xy, const T& z) {
|
2014-08-12 18:04:28 +00:00
|
|
|
return MakeVec(xy[0], xy[1], z);
|
|
|
|
}
|
|
|
|
|
2016-09-18 00:38:01 +00:00
|
|
|
template <typename T>
|
2020-08-31 19:06:16 +00:00
|
|
|
[[nodiscard]] constexpr Vec3<T> MakeVec(const T& x, const Vec2<T>& yz) {
|
2014-08-12 18:04:28 +00:00
|
|
|
return MakeVec(x, yz[0], yz[1]);
|
|
|
|
}
|
|
|
|
|
2016-09-18 00:38:01 +00:00
|
|
|
template <typename T>
|
2020-08-31 19:06:16 +00:00
|
|
|
[[nodiscard]] constexpr Vec4<T> MakeVec(const T& x, const T& y, const T& z, const T& w) {
|
2014-07-27 18:07:45 +00:00
|
|
|
return Vec4<T>{x, y, z, w};
|
|
|
|
}
|
|
|
|
|
2016-09-18 00:38:01 +00:00
|
|
|
template <typename T>
|
2020-08-31 19:06:16 +00:00
|
|
|
[[nodiscard]] constexpr Vec4<T> MakeVec(const Vec2<T>& xy, const T& z, const T& w) {
|
2014-08-12 18:04:28 +00:00
|
|
|
return MakeVec(xy[0], xy[1], z, w);
|
|
|
|
}
|
|
|
|
|
2016-09-18 00:38:01 +00:00
|
|
|
template <typename T>
|
2020-08-31 19:06:16 +00:00
|
|
|
[[nodiscard]] constexpr Vec4<T> MakeVec(const T& x, const Vec2<T>& yz, const T& w) {
|
2014-08-12 18:04:28 +00:00
|
|
|
return MakeVec(x, yz[0], yz[1], w);
|
|
|
|
}
|
|
|
|
|
|
|
|
// NOTE: This has priority over "Vec2<Vec2<T>> MakeVec(const Vec2<T>& x, const Vec2<T>& y)".
|
|
|
|
// Even if someone wanted to use an odd object like Vec2<Vec2<T>>, the compiler would error
|
|
|
|
// out soon enough due to misuse of the returned structure.
|
2016-09-18 00:38:01 +00:00
|
|
|
template <typename T>
|
2020-08-31 19:06:16 +00:00
|
|
|
[[nodiscard]] constexpr Vec4<T> MakeVec(const Vec2<T>& xy, const Vec2<T>& zw) {
|
2014-08-12 18:04:28 +00:00
|
|
|
return MakeVec(xy[0], xy[1], zw[0], zw[1]);
|
|
|
|
}
|
|
|
|
|
2016-09-18 00:38:01 +00:00
|
|
|
template <typename T>
|
2020-08-31 19:06:16 +00:00
|
|
|
[[nodiscard]] constexpr Vec4<T> MakeVec(const Vec3<T>& xyz, const T& w) {
|
2014-08-12 18:04:28 +00:00
|
|
|
return MakeVec(xyz[0], xyz[1], xyz[2], w);
|
|
|
|
}
|
|
|
|
|
2016-09-18 00:38:01 +00:00
|
|
|
template <typename T>
|
2020-08-31 19:06:16 +00:00
|
|
|
[[nodiscard]] constexpr Vec4<T> MakeVec(const T& x, const Vec3<T>& yzw) {
|
2014-08-12 18:04:28 +00:00
|
|
|
return MakeVec(x, yzw[0], yzw[1], yzw[2]);
|
|
|
|
}
|
|
|
|
|
2019-02-27 03:38:34 +00:00
|
|
|
} // namespace Common
|