From 980ede013f61b2424363790849c7a0a16be6b876 Mon Sep 17 00:00:00 2001 From: Hartmut Seichter Date: Mon, 22 Jul 2024 22:40:38 +0200 Subject: [PATCH] formatters for std::format and std::print --- src/core/include/pw/core/geometry.hpp | 1 - src/core/include/pw/core/matrix.hpp | 21 +++++++++++++++------ src/core/tests/pwcore_test_matrix.cpp | 9 +++++---- src/core/tests/pwcore_test_vector.cpp | 3 +++ 4 files changed, 23 insertions(+), 11 deletions(-) diff --git a/src/core/include/pw/core/geometry.hpp b/src/core/include/pw/core/geometry.hpp index c460c76..cf56fb0 100644 --- a/src/core/include/pw/core/geometry.hpp +++ b/src/core/include/pw/core/geometry.hpp @@ -23,7 +23,6 @@ #ifndef PW_CORE_GEOMETRY_HPP #define PW_CORE_GEOMETRY_HPP -#include #include #include #include diff --git a/src/core/include/pw/core/matrix.hpp b/src/core/include/pw/core/matrix.hpp index cdc9b0c..3e84e7f 100644 --- a/src/core/include/pw/core/matrix.hpp +++ b/src/core/include/pw/core/matrix.hpp @@ -23,13 +23,10 @@ #ifndef PW_CORE_MATRIX_HPP #define PW_CORE_MATRIX_HPP -#include "vector.hpp" -#include #include #include #include -#include #include #include @@ -52,7 +49,12 @@ struct matrix final { using transpose_type = matrix; using minor_type = matrix; - vector, Rows> m_{}; + using reference = row_type&; + using const_reference = const row_type&; + using pointer = row_type*; + using const_pointer = const row_type*; + + vector m_{}; auto&& data(this auto&& self) { return std::forward(self).m_[0].data(); @@ -200,6 +202,13 @@ struct matrix final { std::make_index_sequence{}, c)...}; }(std::make_index_sequence{}); // rowwise expansion } + + // + // Iterators + // + constexpr const_pointer begin() const { return &m_[0]; } + + constexpr const_pointer end() const { return &m_[Rows]; } }; template <> constexpr float matrix::determinant() const noexcept { @@ -215,7 +224,7 @@ template constexpr auto operator*(const matrix& A, const matrix& B) noexcept { matrix result{}; - [&](std::index_sequence) { + [&result, &A, &B](std::index_sequence) { ( [&](auto&& Ai, std::index_sequence) { ( @@ -237,7 +246,7 @@ constexpr auto operator*(const matrix& A, // first step - should move to concepts to allow for std::array and // std::span to be allowed vector, Ca> result{}; - [&](std::index_sequence) { + [&result, &A, &B](std::index_sequence) { ( [&](auto&& Ai, std::index_sequence) { ((result[Ib] += A[Ai][Ib] * B[Ib]), ...); diff --git a/src/core/tests/pwcore_test_matrix.cpp b/src/core/tests/pwcore_test_matrix.cpp index 884837e..b7d5161 100644 --- a/src/core/tests/pwcore_test_matrix.cpp +++ b/src/core/tests/pwcore_test_matrix.cpp @@ -1,4 +1,5 @@ #include +#include #include #include #include @@ -50,18 +51,18 @@ auto main() -> int { auto m33 = pw::matrix::identity(); - m33.slice_test<2, 2>(0, 0); - auto m22_slice = m33.slice<2, 2>(0, 0); std::print("{}\n", typeid(m22_slice).name()); - auto vvv = pw::vector { 1.f, 2, 3, 4 }; + auto vvv = pw::vector{1.f, 2, 3, 4}; auto vvv_swizzle = vvv.slice(std::make_index_sequence<2>{}); - pw::debug::d() << "matrix<3,3>.slice() -> \n" << pw::serialize::to_string(m33) << "\n to \n" << pw::serialize::to_string(m22_slice); + + // octave style output + std::print("\nm33=[{}]\n", m33); } diff --git a/src/core/tests/pwcore_test_vector.cpp b/src/core/tests/pwcore_test_vector.cpp index 2aa13ed..c577c42 100644 --- a/src/core/tests/pwcore_test_vector.cpp +++ b/src/core/tests/pwcore_test_vector.cpp @@ -1,6 +1,7 @@ #include #include +#include #include #include @@ -76,5 +77,7 @@ auto main() -> int { std::print("min((1.3f,0.9f,2.3f),({} {} {})) -> ({} {} {})\n", vec3f.x(), vec3f.y(), vec3f.z(), min_ab.x(), min_ab.y(), min_ab.z()); + std::print("{}", vec3f); + return 0; }