more constexpr

This commit is contained in:
BlackMATov
2020-11-24 22:27:22 +07:00
parent 75aed0e282
commit 0a26d1824e
3 changed files with 23 additions and 23 deletions

View File

@@ -17,7 +17,7 @@ namespace vmath_hpp
// identity
template < typename T >
mat<T, 4> identity() {
constexpr mat<T, 4> identity() {
return {
{1, 0, 0, 0},
{0, 1, 0, 0},
@@ -28,7 +28,7 @@ namespace vmath_hpp
// translate
template < typename T >
mat<T, 4> translate(T x, T y, T z) {
constexpr mat<T, 4> translate(T x, T y, T z) {
return {
{1, 0, 0, 0},
{0, 1, 0, 0},
@@ -37,17 +37,17 @@ namespace vmath_hpp
}
template < typename T >
mat<T, 4> translate(const vec<T, 3>& xyz) {
constexpr mat<T, 4> translate(const vec<T, 3>& xyz) {
return translate(xyz.x, xyz.y, xyz.z);
}
template < typename T >
mat<T, 4> translate(const mat<T, 4>& m, T x, T y, T z) {
constexpr mat<T, 4> translate(const mat<T, 4>& m, T x, T y, T z) {
return m * translate(x, y, z);
}
template < typename T >
mat<T, 4> translate(const mat<T, 4>& m, const vec<T, 3>& xyz) {
constexpr mat<T, 4> translate(const mat<T, 4>& m, const vec<T, 3>& xyz) {
return m * translate(xyz);
}
@@ -95,7 +95,7 @@ namespace vmath_hpp
// scale
template < typename T >
mat<T, 4> scale(T x, T y, T z) {
constexpr mat<T, 4> scale(T x, T y, T z) {
return {
{x, 0, 0, 0},
{0, y, 0, 0},
@@ -104,17 +104,17 @@ namespace vmath_hpp
}
template < typename T >
mat<T, 4> scale(const vec<T, 3>& xyz) {
constexpr mat<T, 4> scale(const vec<T, 3>& xyz) {
return scale(xyz.x, xyz.y, xyz.z);
}
template < typename T >
mat<T, 4> scale(const mat<T, 4>& m, T x, T y, T z) {
constexpr mat<T, 4> scale(const mat<T, 4>& m, T x, T y, T z) {
return m * scale(x, y, z);
}
template < typename T >
mat<T, 4> scale(const mat<T, 4>& m, const vec<T, 3>& xyz) {
constexpr mat<T, 4> scale(const mat<T, 4>& m, const vec<T, 3>& xyz) {
return m * scale(xyz);
}
}

View File

@@ -73,16 +73,16 @@ namespace
TEST_CASE("vmath/mat_ext") {
SECTION("identity") {
REQUIRE(vec4f(2.f,3.f,4.f,1.f) * identity<float>() == approx4(2.f,3.f,4.f,1.f));
REQUIRE(vec4f(2.f,3.f,4.f,1.f) * identity<float>() == approx4(2.f,3.f,4.f,1.f));
STATIC_REQUIRE(vec4f(2.f,3.f,4.f,1.f) * identity<float>() == approx4(2.f,3.f,4.f,1.f));
STATIC_REQUIRE(vec4f(2.f,3.f,4.f,1.f) * identity<float>() == approx4(2.f,3.f,4.f,1.f));
}
SECTION("translate") {
REQUIRE(vec4f(2.f,3.f,4.f,1.f) * translate(1.f,2.f,3.f) == approx4(3.f,5.f,7.f,1.f));
REQUIRE(vec4f(2.f,3.f,4.f,1.f) * translate(vec3f{1.f,2.f,3.f}) == approx4(3.f,5.f,7.f,1.f));
STATIC_REQUIRE(vec4f(2.f,3.f,4.f,1.f) * translate(1.f,2.f,3.f) == approx4(3.f,5.f,7.f,1.f));
STATIC_REQUIRE(vec4f(2.f,3.f,4.f,1.f) * translate(vec3f{1.f,2.f,3.f}) == approx4(3.f,5.f,7.f,1.f));
REQUIRE(vec4f(2.f,3.f,4.f,1.f) * translate(translate(1.f,2.f,3.f), 1.f,2.f,3.f) == approx4(4.f,7.f,10.f,1.f));
REQUIRE(vec4f(2.f,3.f,4.f,1.f) * translate(translate(1.f,2.f,3.f), vec3f{1.f,2.f,3.f}) == approx4(4.f,7.f,10.f,1.f));
STATIC_REQUIRE(vec4f(2.f,3.f,4.f,1.f) * translate(translate(1.f,2.f,3.f), 1.f,2.f,3.f) == approx4(4.f,7.f,10.f,1.f));
STATIC_REQUIRE(vec4f(2.f,3.f,4.f,1.f) * translate(translate(1.f,2.f,3.f), vec3f{1.f,2.f,3.f}) == approx4(4.f,7.f,10.f,1.f));
}
SECTION("rotate") {
@@ -94,10 +94,10 @@ TEST_CASE("vmath/mat_ext") {
}
SECTION("scale") {
REQUIRE(vec4f(2.f,3.f,4.f,1.f) * scale(2.f,3.f,4.f) == approx4(4.f,9.f,16.f,1.f));
REQUIRE(vec4f(2.f,3.f,4.f,1.f) * scale(vec3f{2.f,3.f,4.f}) == approx4(4.f,9.f,16.f,1.f));
STATIC_REQUIRE(vec4f(2.f,3.f,4.f,1.f) * scale(2.f,3.f,4.f) == approx4(4.f,9.f,16.f,1.f));
STATIC_REQUIRE(vec4f(2.f,3.f,4.f,1.f) * scale(vec3f{2.f,3.f,4.f}) == approx4(4.f,9.f,16.f,1.f));
REQUIRE(vec4f(2.f,3.f,4.f,1.f) * scale(scale(2.f,2.f,2.f), 2.f,3.f,4.f) == approx4(8.f,18.f,32.f,1.f));
REQUIRE(vec4f(2.f,3.f,4.f,1.f) * scale(scale(2.f,2.f,2.f), vec3f{2.f,3.f,4.f}) == approx4(8.f,18.f,32.f,1.f));
STATIC_REQUIRE(vec4f(2.f,3.f,4.f,1.f) * scale(scale(2.f,2.f,2.f), 2.f,3.f,4.f) == approx4(8.f,18.f,32.f,1.f));
STATIC_REQUIRE(vec4f(2.f,3.f,4.f,1.f) * scale(scale(2.f,2.f,2.f), vec3f{2.f,3.f,4.f}) == approx4(8.f,18.f,32.f,1.f));
}
}

View File

@@ -213,11 +213,11 @@ TEST_CASE("vmath/mat_fun") {
STATIC_REQUIRE(inverse(mat3i()) == mat3i());
STATIC_REQUIRE(inverse(mat4i()) == mat4i());
REQUIRE(inverse(mat2f(0.5)) == mat2f(2.f));
REQUIRE(inverse(mat3f(0.5)) == mat3f(2.f));
REQUIRE(inverse(mat4f(0.5)) == mat4f(2.f));
STATIC_REQUIRE(inverse(mat2f(0.5)) == mat2f(2.f));
STATIC_REQUIRE(inverse(mat3f(0.5)) == mat3f(2.f));
STATIC_REQUIRE(inverse(mat4f(0.5)) == mat4f(2.f));
REQUIRE(inverse(translate(1.f,2.f,3.f)) == approx4x4(translate(-1.f,-2.f,-3.f)));
STATIC_REQUIRE(inverse(translate(1.f,2.f,3.f)) == approx4x4(translate(-1.f,-2.f,-3.f)));
REQUIRE(inverse(rotate(0.5f,normalize(vec3f{1.f,2.f,3.f}))) == approx4x4(rotate(-0.5f,normalize(vec3f{1.f,2.f,3.f}))));
REQUIRE(inverse(mat3f(rotate(0.5f,normalize(vec3f{1.f,2.f,3.f})))) == approx3x3(mat3f(rotate(-0.5f,normalize(vec3f{1.f,2.f,3.f})))));