formatters for std::format and std::print

This commit is contained in:
Hartmut Seichter 2024-07-22 22:40:38 +02:00
parent 70d002f57c
commit 980ede013f
4 changed files with 23 additions and 11 deletions

View file

@ -23,7 +23,6 @@
#ifndef PW_CORE_GEOMETRY_HPP
#define PW_CORE_GEOMETRY_HPP
#include <cstddef>
#include <pw/core/aabb.hpp>
#include <pw/core/globals.hpp>
#include <pw/core/resource.hpp>

View file

@ -23,13 +23,10 @@
#ifndef PW_CORE_MATRIX_HPP
#define PW_CORE_MATRIX_HPP
#include "vector.hpp"
#include <cstddef>
#include <pw/core/globals.hpp>
#include <pw/core/math.hpp>
#include <pw/core/vector.hpp>
#include <print>
#include <type_traits>
#include <utility>
@ -52,7 +49,12 @@ struct matrix final {
using transpose_type = matrix<Scalar, Cols, Rows>;
using minor_type = matrix<Scalar, Cols - 1, Rows - 1>;
vector<vector<Scalar, Cols>, Rows> m_{};
using reference = row_type&;
using const_reference = const row_type&;
using pointer = row_type*;
using const_pointer = const row_type*;
vector<row_type, Rows> m_{};
auto&& data(this auto&& self) {
return std::forward<decltype(self)>(self).m_[0].data();
@ -200,6 +202,13 @@ struct matrix final {
std::make_index_sequence<ColsSlice>{}, c)...};
}(std::make_index_sequence<RowsSlice>{}); // rowwise expansion
}
//
// Iterators
//
constexpr const_pointer begin() const { return &m_[0]; }
constexpr const_pointer end() const { return &m_[Rows]; }
};
template <> constexpr float matrix<float, 1, 1>::determinant() const noexcept {
@ -215,7 +224,7 @@ template <typename Scalar, unsigned int Ra, unsigned int CaRb, unsigned int Cb>
constexpr auto operator*(const matrix<Scalar, Ra, CaRb>& A,
const matrix<Scalar, CaRb, Cb>& B) noexcept {
matrix<Scalar, Ra, Cb> result{};
[&]<std::size_t... Ia>(std::index_sequence<Ia...>) {
[&result, &A, &B]<std::size_t... Ia>(std::index_sequence<Ia...>) {
(
[&]<std::size_t... Ib>(auto&& Ai, std::index_sequence<Ib...>) {
(
@ -237,7 +246,7 @@ constexpr auto operator*(const matrix<ScalarA, Ra, Ca>& A,
// first step - should move to concepts to allow for std::array and
// std::span to be allowed
vector<std::common_type_t<ScalarA, ScalarB>, Ca> result{};
[&]<std::size_t... Ia>(std::index_sequence<Ia...>) {
[&result, &A, &B]<std::size_t... Ia>(std::index_sequence<Ia...>) {
(
[&]<std::size_t... Ib>(auto&& Ai, std::index_sequence<Ib...>) {
((result[Ib] += A[Ai][Ib] * B[Ib]), ...);

View file

@ -1,4 +1,5 @@
#include <pw/core/debug.hpp>
#include <pw/core/formatter.hpp>
#include <pw/core/matrix.hpp>
#include <pw/core/serialize.hpp>
#include <pw/core/vector.hpp>
@ -50,18 +51,18 @@ auto main() -> int {
auto m33 = pw::matrix<float, 3, 3>::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);
}

View file

@ -1,6 +1,7 @@
#include <pw/core/serialize.hpp>
#include <pw/core/vector.hpp>
#include <pw/core/formatter.hpp>
#include <print>
#include <tuple>
@ -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;
}