Skip to content

Commit

Permalink
define stream insertion for blade, multivector
Browse files Browse the repository at this point in the history
Change-Id: Icfa9a1df6ac0864932e07902891bf3568941afea
  • Loading branch information
oliverlee committed Jan 12, 2024
1 parent 68c1042 commit d51c769
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 0 deletions.
38 changes: 38 additions & 0 deletions geometry/src/algebra.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
#include "geometry/src/detail/type_unique.hpp"

#include <cstddef>
#include <ostream>
#include <tuple>
#include <type_traits>
#include <utility>
Expand Down Expand Up @@ -280,6 +281,27 @@ struct algebra
{
return x.coefficient * y.coefficient * e<Is..., Js...>;
}

/// stream insertion
///
friend auto operator<<(std::ostream& os, blade x) -> std::enable_if_t<
(N < 10),
decltype(os << std::declval<const scalar_type&>(), os)>
{
if (sizeof...(Is) == 0) {
os << x.coefficient;
return os;
}

if (x.coefficient != scalar_type{1}) {
os << x.coefficient << "*";
}

os << "e";
std::ignore = ((os << Is, true) and ...);

return os;
}
};

template <class... Bs>
Expand Down Expand Up @@ -511,6 +533,22 @@ struct algebra
return ((x * get<B2s>(y)) + ...);
}
/// @}

/// stream insertion
///
friend auto operator<<(std::ostream& os, const multivector& x)
-> decltype(((os << std::declval<const Bs&>(), true) and ...), os)
{
auto first = true;

std::ignore =
((os << (std::exchange(first, false) ? "" : " + "),
os << get<Bs>(x),
true) and
...);

return os;
}
};
};

Expand Down
18 changes: 18 additions & 0 deletions test/algebra_blade_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#include "skytest/skytest.hpp"

#include <cstddef>
#include <sstream>
#include <tuple>
#include <type_traits>

Expand Down Expand Up @@ -113,4 +114,21 @@ auto main() -> int
"geometric product"_ctest = [] {
return expect(eq(6, (2 * e<1>)*(3 * e<1>)));
};

"printable"_test = [] {
static const auto to_string = [](auto blade) {
return (std::stringstream{} << blade).str();
};

return expect(
eq("0*e0", to_string(0 * e<0>)) and //
eq("e1", to_string(1 * e<1>)) and //
eq("2*e2", to_string(2 * e<2>)) and //
eq("3*e01", to_string(3 * e<0, 1>)) and //
eq("4*e12", to_string(4 * e<1, 2>)) and //
eq("5*e02", to_string(5 * e<0, 2>)) and //
eq("6*e012", to_string(6 * e<0, 1, 2>)) and //
eq("7", to_string(7 * e<>)) and //
eq("1", to_string(e<>)));
};
}
14 changes: 14 additions & 0 deletions test/algebra_multivector_test.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
#include "geometry/geometry.hpp"
#include "skytest/skytest.hpp"

#include <cstddef>
#include <sstream>
#include <tuple>

template <std::size_t... Is>
Expand Down Expand Up @@ -133,4 +135,16 @@ auto main() -> int

return expect(eq(z, x * y) and eq(-z, y * x));
};

"printable"_test = [] {
static const auto to_string = [](const auto& mvec) {
return (std::stringstream{} << mvec).str();
};

return expect(
eq("1", to_string(multivector(e<>))) and
eq("2*e0 + 3*e1 + 4*e2", to_string(2 * e<0> + 3 * e<1> + 4 * e<2>))

);
};
}

0 comments on commit d51c769

Please sign in to comment.