xcal
基于 C++23 的现代图形渲染引擎
载入中...
搜索中...
未找到
show.hpp
浏览该文件的文档.
1// show.hpp
2#pragma once
3#ifndef SHOW_H
4#define SHOW_H
5#include <iostream>
6
7#include "../mobject/declaration.hpp"
8#include "./concepts.h" // IWYU pragma: keep
9
10namespace xcmath {
11namespace concepts {
12template <typename T>
13concept OstreamOverwrite = requires(T a) {
14 { std::cout << a } -> std::same_as<std::ostream&>;
15};
16} // namespace concepts
17
18template <typename T>
19 requires concepts::OstreamOverwrite<T> || requires(T x) {
20 { x.to_string() } -> std::same_as<std::string>;
21 }
22std::ostream& operator<<(std::ostream& os, const complex<T>& c) {
23 if constexpr (std::is_arithmetic_v<T>)
24 os << c.real << (c.imag >= 0 ? " + " : " - ") << "j" << abs(c.imag);
25 else if constexpr (concepts::OstreamOverwrite<T>)
26 os << c.real << " + " << "j(" << c.imag << ")";
27 else
28 os << c.real.to_string() << " + j(" << c.imag.to_string() << ")";
29 return os;
30}
31template <typename T, size_t _rows, size_t _cols>
32 requires concepts::OstreamOverwrite<T> || requires(T x) {
33 { x.to_string() } -> std::same_as<std::string>;
34 }
35std::ostream& operator<<(std::ostream& os, const mat<T, _rows, _cols>& m) {
36 size_t i = 0;
37 if constexpr (std::is_arithmetic_v<T> || concepts::OstreamOverwrite<T>) {
38 os << "[";
39 for (auto& r : m) {
40 if (i) os << " ";
41 os << r;
42 os << (++i < _rows ? ",\n" : "");
43 }
44 os << "]";
45 } else {
46 os << "[";
47 for (auto& r : m) {
48 if (i) os << " ";
49 os << r;
50 os << (++i < _rows ? ",\n" : "");
51 }
52 os << "]";
53 };
54 return os;
55}
56template <typename T, size_t _size>
57 requires concepts::OstreamOverwrite<T> || requires(T x) {
58 { x.to_string() } -> std::same_as<std::string>;
59 }
60std::ostream& operator<<(std::ostream& os, const vec<T, _size>& v) {
61 size_t i = 0;
62 if constexpr (std::is_arithmetic_v<T> || concepts::OstreamOverwrite<T>) {
63 os << "[";
64 for (auto& c : v) {
65 os << c << (++i < _size ? ", " : "");
66 }
67 os << "]";
68 } else {
69 os << "[";
70 for (auto& c : v) {
71 os << c.to_string() << (++i < _size ? ", " : "");
72 }
73 os << "]";
74 }
75 return os;
76}
77
78template <typename T>
79 requires concepts::OstreamOverwrite<T> || requires(T x) {
80 { x.to_string() } -> std::same_as<std::string>;
81 }
82std::ostream& operator<<(std::ostream& os, const quaternion<T>& q) {
83 if constexpr (std::is_arithmetic_v<T> || concepts::OstreamOverwrite<T>) {
84 constexpr auto fn = [](T x) { return x >= 0 ? " + " : " - "; };
85 os << q.r() << fn(q.i()) << abs(q.i()) << "i" << fn(q.j()) << abs(q.j())
86 << "j" << fn(q.k()) << abs(q.k()) << "k";
87 } else {
88 os << q.r().to_string() << " + " << q.i().to_string() << "i + "
89 << q.j().to_string() << "j + " << q.k().to_string() << "k";
90 }
91 return os;
92}
93
94} // namespace xcmath
95#endif // SHOW_H
Complex number class template
Definition complex.hpp:15
Matrix class template
Definition mat.hpp:27
Quaternion class template
constexpr _Tp & r()
Get scalar part of the quaternion
constexpr _Tp & i()
Get i-component of the quaternion
constexpr _Tp & k()
Get k-component of the quaternion
constexpr _Tp & j()
Get j-component of the quaternion
Vector class template
Definition vec.hpp:206
Compiler-specific type information handling for MSVC
Definition complex.hpp:12
std::ostream & operator<<(std::ostream &os, const complex< T > &c)
Definition show.hpp:22