vector relational functions

This commit is contained in:
BlackMATov
2020-11-22 00:51:31 +07:00
parent 09062d3988
commit ab23e30cd5
4 changed files with 140 additions and 0 deletions

View File

@@ -7,4 +7,5 @@
#pragma once
#include "vmath_mat.hpp"
#include "vmath_vec_ops.hpp"
#include "vmath_vec.hpp"

View File

@@ -6,7 +6,11 @@
#pragma once
#include <cassert>
#include <cmath>
#include <cstddef>
#include <functional>
#include <iterator>
#include <stdexcept>
#include <type_traits>

View File

@@ -0,0 +1,91 @@
/*******************************************************************************
* 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_vec.hpp"
namespace vmath_hpp::detail
{
template < typename T, std::size_t Size, typename F >
constexpr auto map(const vec<T, Size>& v, F&& f) {
vec<std::invoke_result_t<F,T>, Size> result(uninit);
for ( std::size_t i = 0; i < Size; ++i ) {
result[i] = f(v[i]);
}
return result;
}
template < typename T, typename U, std::size_t Size, typename F >
constexpr auto zip(const vec<T, Size>& l, const vec<U, Size>& r, F&& f) {
vec<std::invoke_result_t<F, T, U>, Size> result(uninit);
for ( std::size_t i = 0; i < Size; ++i ) {
result[i] = f(l[i], r[i]);
}
return result;
}
template < typename T, typename U, std::size_t Size, typename F >
constexpr auto fold(const vec<T, Size>& v, U init, F&& f) {
for ( std::size_t i = 0; i < Size; ++i ) {
init = f(init, v[i]);
}
return init;
}
}
//
// Vector Relational Functions
//
namespace vmath_hpp
{
template < typename T, std::size_t Size >
constexpr vec<bool, Size> less(const vec<T, Size>& x, const vec<T, Size>& y) {
return detail::zip(x, y, std::less<>());
}
template < typename T, std::size_t Size >
constexpr vec<bool, Size> less_equal(const vec<T, Size>& x, const vec<T, Size>& y) {
return detail::zip(x, y, std::less_equal<>());
}
template < typename T, std::size_t Size >
constexpr vec<bool, Size> greater(const vec<T, Size>& x, const vec<T, Size>& y) {
return detail::zip(x, y, std::greater<>());
}
template < typename T, std::size_t Size >
constexpr vec<bool, Size> greater_equal(const vec<T, Size>& x, const vec<T, Size>& y) {
return detail::zip(x, y, std::greater_equal<>());
}
template < typename T, std::size_t Size >
constexpr vec<bool, Size> equal(const vec<T, Size>& x, const vec<T, Size>& y) {
return detail::zip(x, y, std::equal_to<>());
}
template < typename T, std::size_t Size >
constexpr vec<bool, Size> not_equal(const vec<T, Size>& x, const vec<T, Size>& y) {
return detail::zip(x, y, std::not_equal_to<>());
}
template < std::size_t Size >
constexpr bool any(const vec<bool, Size>& x) {
return detail::fold(x, false, std::logical_or<>());
}
template < std::size_t Size >
constexpr bool all(const vec<bool, Size>& x) {
return detail::fold(x, true, std::logical_and<>());
}
template < std::size_t Size >
constexpr vec<bool, Size> not_(const vec<bool, Size>& x) {
return detail::map(x, std::logical_not<>());
}
}

View File

@@ -0,0 +1,44 @@
/*******************************************************************************
* 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_ops.hpp>
#define CATCH_CONFIG_FAST_COMPILE
#include <catch2/catch.hpp>
namespace
{
}
TEST_CASE("vmath/vec_ops") {
using namespace vmath_hpp;
SECTION("Vector Relational Functions") {
STATIC_REQUIRE(less(vec3i(1,1,1), vec3i(0,1,2)) == vec3b(false, false, true));
STATIC_REQUIRE(less_equal(vec3i(1,1,1), vec3i(0,1,2)) == vec3b(false, true, true));
STATIC_REQUIRE(greater(vec3i(1,1,1), vec3i(0,1,2)) == vec3b(true, false, false));
STATIC_REQUIRE(greater_equal(vec3i(1,1,1), vec3i(0,1,2)) == vec3b(true, true, false));
STATIC_REQUIRE(equal(vec3i(1,1,1), vec3i(0,1,2)) == vec3b(false, true, false));
STATIC_REQUIRE(not_equal(vec3i(1,1,1), vec3i(0,1,2)) == vec3b(true, false, true));
STATIC_REQUIRE_FALSE(any(vec2b(false, false)));
STATIC_REQUIRE(any(vec2b(true, false)));
STATIC_REQUIRE(any(vec2b(false, true)));
STATIC_REQUIRE(any(vec2b(true, true)));
STATIC_REQUIRE_FALSE(all(vec2b(false, false)));
STATIC_REQUIRE_FALSE(all(vec2b(true, false)));
STATIC_REQUIRE_FALSE(all(vec2b(false, true)));
STATIC_REQUIRE(all(vec2b(true, true)));
STATIC_REQUIRE(not_(vec2b(false, false)) == vec2b(true, true));
STATIC_REQUIRE(not_(vec2b(true, false)) == vec2b(false, true));
STATIC_REQUIRE(not_(vec2b(false, true)) == vec2b(true, false));
STATIC_REQUIRE(not_(vec2b(true, true)) == vec2b(false, false));
}
}