vector reciprocal

This commit is contained in:
BlackMATov
2020-11-27 03:46:16 +07:00
parent a8f242acd6
commit 20257bd412
5 changed files with 44 additions and 38 deletions

View File

@@ -337,6 +337,11 @@ namespace vmath_hpp
{ 0, 0, 1}};
}
template < typename T >
[[nodiscard]] constexpr mat<T, 3> shear(const mat<T, 3>& m, const vec<T, 2>& v) {
return m * shear(v);
}
template < typename T >
[[nodiscard]] constexpr mat<T, 3> shear_x(T y) {
return {
@@ -345,6 +350,11 @@ namespace vmath_hpp
{0, 0, 1}};
}
template < typename T >
[[nodiscard]] constexpr mat<T, 3> shear_x(const mat<T, 3>& m, T y) {
return m * shear_x(y);
}
template < typename T >
[[nodiscard]] constexpr mat<T, 3> shear_y(T x) {
return {
@@ -353,16 +363,6 @@ namespace vmath_hpp
{0, 0, 1}};
}
template < typename T >
[[nodiscard]] constexpr mat<T, 3> shear(const mat<T, 3>& m, const vec<T, 2>& v) {
return m * shear(v);
}
template < typename T >
[[nodiscard]] constexpr mat<T, 3> shear_x(const mat<T, 3>& m, T y) {
return m * shear_x(y);
}
template < typename T >
[[nodiscard]] constexpr mat<T, 3> shear_y(const mat<T, 3>& m, T x) {
return m * shear_y(x);

View File

@@ -20,12 +20,11 @@ namespace vmath_hpp::detail
class mat_base<T, 2> {
public:
using row_type = vec<T, 2>;
row_type rows[2];
row_type rows[2] = {
{1, 0},
{0, 1}};
public:
constexpr mat_base() : rows{
row_type{1, 0},
row_type{0, 1},
} {}
mat_base() = default;
constexpr explicit mat_base(T v)
: rows{
@@ -61,13 +60,12 @@ namespace vmath_hpp::detail
class mat_base<T, 3> {
public:
using row_type = vec<T, 3>;
row_type rows[3];
row_type rows[3] = {
{1, 0, 0},
{0, 1, 0},
{0, 0, 1}};
public:
constexpr mat_base() : rows{
row_type{1, 0, 0},
row_type{0, 1, 0},
row_type{0, 0, 1},
} {}
mat_base() = default;
constexpr explicit mat_base(T v)
: rows{
@@ -109,14 +107,13 @@ namespace vmath_hpp::detail
class mat_base<T, 4> {
public:
using row_type = vec<T, 4>;
row_type rows[4];
row_type rows[4] = {
{1, 0, 0, 0},
{0, 1, 0, 0},
{0, 0, 1, 0},
{0, 0, 0, 1}};
public:
constexpr mat_base() : rows{
row_type{1, 0, 0, 0},
row_type{0, 1, 0, 0},
row_type{0, 0, 1, 0},
row_type{0, 0, 0, 1},
} {}
mat_base() = default;
constexpr explicit mat_base(T v)
: rows{

View File

@@ -391,6 +391,11 @@ namespace vmath_hpp
return map([](T x) { return sign(x); }, xs);
}
template < typename T, std::size_t Size >
[[nodiscard]] constexpr vec<T, Size> reciprocal(const vec<T, Size>& xs) {
return map([](T x) { return reciprocal(x); }, xs);
}
template < typename T, std::size_t Size >
[[nodiscard]] vec<T, Size> floor(const vec<T, Size>& xs) {
return map([](T x) { return floor(x); }, xs);

View File

@@ -51,17 +51,20 @@ TEST_CASE("vmath/fun") {
}
SECTION("Common Functions") {
REQUIRE(abs(1) == 1);
REQUIRE(abs(-1) == 1);
REQUIRE(abs(1.f) == approx(1.f));
REQUIRE(abs(-1.f) == approx(1.f));
STATIC_REQUIRE(vmath_hpp::abs(1) == 1);
STATIC_REQUIRE(vmath_hpp::abs(-1) == 1);
STATIC_REQUIRE(vmath_hpp::abs(1.f) == approx(1.f));
STATIC_REQUIRE(vmath_hpp::abs(-1.f) == approx(1.f));
REQUIRE(sign(2) == 1);
REQUIRE(sign(-2) == -1);
REQUIRE(sign(0) == 0);
REQUIRE(sign(2.f) == approx(1.f));
REQUIRE(sign(-2.f) == approx(-1.f));
REQUIRE(sign(0.f) == approx(0.f));
STATIC_REQUIRE(sign(2) == 1);
STATIC_REQUIRE(sign(-2) == -1);
STATIC_REQUIRE(sign(0) == 0);
STATIC_REQUIRE(sign(2.f) == approx(1.f));
STATIC_REQUIRE(sign(-2.f) == approx(-1.f));
STATIC_REQUIRE(sign(0.f) == approx(0.f));
STATIC_REQUIRE(reciprocal(2.f) == approx(0.5f));
STATIC_REQUIRE(reciprocal(4.f) == approx(0.25f));
REQUIRE(floor(1.7f) == approx(1.f));
REQUIRE(trunc(1.7f) == approx(1.f));

View File

@@ -127,6 +127,7 @@ TEST_CASE("vmath/vec_fun") {
SECTION("Common Functions") {
STATIC_REQUIRE(abs(float2(1.f, -1.f)) == approx2(1.f,1.f));
STATIC_REQUIRE(sign(float3(1.f, -1.f, 0.f)) == approx3(1.f,-1.f,0.f));
STATIC_REQUIRE(reciprocal(float2(2.f, 4.f)) == approx2(0.5f,0.25f));
(void)floor(float2(1.f, -1.f));
(void)trunc(float2(1.f, -1.f));