mirror of
https://github.com/BlackMATov/vmath.hpp.git
synced 2025-12-15 04:35:25 +07:00
cast_to
This commit is contained in:
@@ -9,5 +9,7 @@
|
||||
#include "vmath_fun.hpp"
|
||||
#include "vmath_mat.hpp"
|
||||
#include "vmath_mat_fun.hpp"
|
||||
#include "vmath_mat_ext.hpp"
|
||||
#include "vmath_vec.hpp"
|
||||
#include "vmath_vec_fun.hpp"
|
||||
#include "vmath_vec_ext.hpp"
|
||||
|
||||
19
headers/vmath.hpp/vmath_ext.hpp
Normal file
19
headers/vmath.hpp/vmath_ext.hpp
Normal file
@@ -0,0 +1,19 @@
|
||||
/*******************************************************************************
|
||||
* 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, by Matvey Cherevko (blackmatov@gmail.com)
|
||||
******************************************************************************/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "vmath_fwd.hpp"
|
||||
|
||||
#include "vmath_fun.hpp"
|
||||
|
||||
namespace vmath_hpp
|
||||
{
|
||||
template < typename To, typename From >
|
||||
constexpr To cast_to(From x) noexcept {
|
||||
return static_cast<To>(x);
|
||||
}
|
||||
}
|
||||
@@ -8,10 +8,24 @@
|
||||
|
||||
#include "vmath_fwd.hpp"
|
||||
|
||||
#include "vmath_ext.hpp"
|
||||
#include "vmath_fun.hpp"
|
||||
|
||||
#include "vmath_vec.hpp"
|
||||
#include "vmath_vec_fun.hpp"
|
||||
#include "vmath_vec_ext.hpp"
|
||||
|
||||
#include "vmath_mat.hpp"
|
||||
#include "vmath_mat_fun.hpp"
|
||||
|
||||
namespace vmath_hpp
|
||||
{
|
||||
template < typename To, typename From, std::size_t Size >
|
||||
constexpr mat<To, Size> cast_to(const mat<From, Size>& xs) {
|
||||
return detail::map([](const vec<From, Size>& x){ return cast_to<To>(x); }, xs);
|
||||
}
|
||||
}
|
||||
|
||||
namespace vmath_hpp
|
||||
{
|
||||
// identity
|
||||
|
||||
23
headers/vmath.hpp/vmath_vec_ext.hpp
Normal file
23
headers/vmath.hpp/vmath_vec_ext.hpp
Normal file
@@ -0,0 +1,23 @@
|
||||
/*******************************************************************************
|
||||
* 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, by Matvey Cherevko (blackmatov@gmail.com)
|
||||
******************************************************************************/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "vmath_fwd.hpp"
|
||||
|
||||
#include "vmath_ext.hpp"
|
||||
#include "vmath_fun.hpp"
|
||||
|
||||
#include "vmath_vec.hpp"
|
||||
#include "vmath_vec_fun.hpp"
|
||||
|
||||
namespace vmath_hpp
|
||||
{
|
||||
template < typename To, typename From, std::size_t Size >
|
||||
constexpr vec<To, Size> cast_to(const vec<From, Size>& xs) {
|
||||
return detail::map([](From x){ return cast_to<To>(x); }, xs);
|
||||
}
|
||||
}
|
||||
26
untests/vmath_ext_tests.cpp
Normal file
26
untests/vmath_ext_tests.cpp
Normal file
@@ -0,0 +1,26 @@
|
||||
/*******************************************************************************
|
||||
* 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, by Matvey Cherevko (blackmatov@gmail.com)
|
||||
******************************************************************************/
|
||||
|
||||
#include <vmath.hpp/vmath_ext.hpp>
|
||||
|
||||
#define CATCH_CONFIG_FAST_COMPILE
|
||||
#include <catch2/catch.hpp>
|
||||
|
||||
#include "vmath_tests.hpp"
|
||||
|
||||
namespace
|
||||
{
|
||||
using namespace vmath_hpp;
|
||||
using namespace vmath_tests;
|
||||
}
|
||||
|
||||
TEST_CASE("vmath/ext") {
|
||||
SECTION("cast_to") {
|
||||
constexpr auto i = cast_to<int>(1.5f);
|
||||
STATIC_REQUIRE(i == 1);
|
||||
STATIC_REQUIRE(std::is_same_v<decltype(i), const int>);
|
||||
}
|
||||
}
|
||||
@@ -18,6 +18,12 @@ namespace
|
||||
}
|
||||
|
||||
TEST_CASE("vmath/mat_ext") {
|
||||
SECTION("cast_to") {
|
||||
constexpr auto m = cast_to<int>(mat2f{1.5f});
|
||||
STATIC_REQUIRE(m == mat2i(1));
|
||||
STATIC_REQUIRE(std::is_same_v<decltype(m)::row_type, vec2i>);
|
||||
}
|
||||
|
||||
SECTION("identity") {
|
||||
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));
|
||||
|
||||
26
untests/vmath_vec_ext_tests.cpp
Normal file
26
untests/vmath_vec_ext_tests.cpp
Normal file
@@ -0,0 +1,26 @@
|
||||
/*******************************************************************************
|
||||
* 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, by Matvey Cherevko (blackmatov@gmail.com)
|
||||
******************************************************************************/
|
||||
|
||||
#include <vmath.hpp/vmath_vec_ext.hpp>
|
||||
|
||||
#define CATCH_CONFIG_FAST_COMPILE
|
||||
#include <catch2/catch.hpp>
|
||||
|
||||
#include "vmath_tests.hpp"
|
||||
|
||||
namespace
|
||||
{
|
||||
using namespace vmath_hpp;
|
||||
using namespace vmath_tests;
|
||||
}
|
||||
|
||||
TEST_CASE("vmath/vec_ext") {
|
||||
SECTION("cast_to") {
|
||||
constexpr auto v = cast_to<int>(vec2f{1.5f});
|
||||
STATIC_REQUIRE(v == vec2i(1));
|
||||
STATIC_REQUIRE(std::is_same_v<decltype(v)::value_type, int>);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user