Files
vmath.hpp/headers/vmath.hpp/vmath_qua_fun.hpp
2021-01-26 01:57:39 +07:00

196 lines
4.5 KiB
C++

/*******************************************************************************
* This file is part of the "https://github.com/blackmatov/vmath.hpp"
* For conditions of distribution and use, see copyright notice in LICENSE.md
* Copyright (C) 2020-2021, by Matvey Cherevko (blackmatov@gmail.com)
******************************************************************************/
#pragma once
#include "vmath_fwd.hpp"
#include "vmath_fun.hpp"
#include "vmath_qua.hpp"
#include "vmath_vec.hpp"
#include "vmath_vec_fun.hpp"
//
// Operators
//
namespace vmath_hpp
{
// +operator
template < typename T >
[[nodiscard]] constexpr qua<T> operator+(const qua<T>& xs) {
return xs;
}
// -operator
template < typename T >
[[nodiscard]] constexpr qua<T> operator-(const qua<T>& xs) {
return qua(-vec<T, 4>{xs});
}
// operator+
template < typename T >
[[nodiscard]] constexpr qua<T> operator+(const qua<T>& xs, const qua<T>& ys) {
return qua(vec{xs} + vec{ys});
}
// operator+=
template < typename T >
constexpr qua<T>& operator+=(qua<T>& xs, const qua<T>& ys) {
return (xs = (xs + ys));
}
// operator-
template < typename T >
[[nodiscard]] constexpr qua<T> operator-(const qua<T>& xs, const qua<T>& ys) {
return qua(vec{xs} - vec{ys});
}
// operator-=
template < typename T >
constexpr qua<T>& operator-=(qua<T>& xs, const qua<T>& ys) {
return (xs = (xs - ys));
}
// operator*
template < typename T >
[[nodiscard]] constexpr qua<T> operator*(const qua<T>& xs, T y) {
return qua(vec{xs} * y);
}
template < typename T >
[[nodiscard]] constexpr qua<T> operator*(T x, const qua<T>& ys) {
return qua(x * vec{ys});
}
template < typename T >
[[nodiscard]] constexpr vec<T, 3> operator*(const vec<T, 3>& xs, const qua<T>& ys) {
const vec qv2 = cross(ys.v, xs) * T(2);
return xs + qv2 * ys.s + cross(ys.v, qv2);
}
template < typename T >
[[nodiscard]] constexpr qua<T> operator*(const qua<T>& xs, const qua<T>& ys) {
return {
cross(ys.v, xs.v) + ys.s * xs.v + xs.s * ys.v,
ys.s * xs.s - dot(ys.v, xs.v)};
}
// operator*=
template < typename T >
constexpr qua<T>& operator*=(qua<T>& xs, T y) {
return (xs = (xs * y));
}
template < typename T >
constexpr vec<T, 3>& operator*=(vec<T, 3>& xs, const qua<T>& ys) {
return (xs = (xs * ys));
}
template < typename T >
constexpr qua<T>& operator*=(qua<T>& xs, const qua<T>& ys) {
return (xs = (xs * ys));
}
// operator/
template < typename T >
[[nodiscard]] constexpr qua<T> operator/(const qua<T>& xs, T y) {
return qua(vec{xs} / y);
}
template < typename T >
[[nodiscard]] constexpr qua<T> operator/(T x, const qua<T>& ys) {
return qua(x / vec{ys});
}
// operator/=
template < typename T >
constexpr qua<T>& operator/=(qua<T>& xs, T y) {
return (xs = (xs / y));
}
// operator==
template < typename T >
[[nodiscard]] constexpr bool operator==(const qua<T>& xs, const qua<T>& ys) {
return vec{xs} == vec{ys};
}
// operator!=
template < typename T >
[[nodiscard]] constexpr bool operator!=(const qua<T>& xs, const qua<T>& ys) {
return vec{xs} != vec{ys};
}
// operator<
template < typename T >
[[nodiscard]] constexpr bool operator<(const qua<T>& xs, const qua<T>& ys) {
return vec{xs} < vec{ys};
}
}
//
// Common Functions
//
namespace vmath_hpp
{
template < typename T >
[[nodiscard]] vec<bool, 4> isnan(const qua<T>& xs) {
return isnan(vec{xs});
}
template < typename T >
[[nodiscard]] vec<bool, 4> isinf(const qua<T>& xs) {
return isinf(vec{xs});
}
template < typename T >
[[nodiscard]] vec<bool, 4> isfinite(const qua<T>& xs) {
return isfinite(vec{xs});
}
}
//
// Geometric Functions
//
namespace vmath_hpp
{
template < typename T >
[[nodiscard]] constexpr T dot(const qua<T>& xs, const qua<T>& ys) {
return dot(vec{xs}, vec{ys});
}
template < typename T >
[[nodiscard]] T length(const qua<T>& xs) {
return length(vec{xs});
}
template < typename T >
[[nodiscard]] constexpr T length2(const qua<T>& xs) {
return length2(vec{xs});
}
template < typename T >
[[nodiscard]] constexpr qua<T> normalize(const qua<T>& xs) {
return qua(normalize(vec{xs}));
}
}