mirror of
https://github.com/slendidev/smath.git
synced 2026-03-17 02:26:50 +02:00
Add .clang-format and format codebase
Signed-off-by: Slendi <slendi@socopon.com>
This commit is contained in:
51
.clang-format
Normal file
51
.clang-format
Normal file
@@ -0,0 +1,51 @@
|
||||
---
|
||||
Language: Cpp
|
||||
BasedOnStyle: LLVM
|
||||
|
||||
IndentWidth: 4
|
||||
TabWidth: 4
|
||||
UseTab: ForIndentation
|
||||
|
||||
ColumnLimit: 80
|
||||
AlignAfterOpenBracket: DontAlign
|
||||
AlignOperands: false
|
||||
BreakBeforeBinaryOperators: All
|
||||
ContinuationIndentWidth: 4
|
||||
BreakBeforeBraces: Custom
|
||||
BraceWrapping:
|
||||
AfterCaseLabel: false
|
||||
AfterClass: true
|
||||
AfterControlStatement: Never
|
||||
AfterEnum: true
|
||||
AfterFunction: true
|
||||
AfterNamespace: true
|
||||
AfterStruct: true
|
||||
AfterUnion: true
|
||||
BeforeCatch: false
|
||||
BeforeElse: false
|
||||
SplitEmptyFunction: false
|
||||
SplitEmptyRecord: false
|
||||
SplitEmptyNamespace: false
|
||||
AllowShortIfStatementsOnASingleLine: false
|
||||
AllowShortLoopsOnASingleLine: false
|
||||
AllowShortFunctionsOnASingleLine: Inline
|
||||
AllowShortBlocksOnASingleLine: Empty
|
||||
SpaceInEmptyBlock: true
|
||||
BinPackArguments: false
|
||||
BinPackParameters: false
|
||||
Cpp11BracedListStyle: false
|
||||
SpaceBeforeCpp11BracedList: true
|
||||
IndentRequiresClause: false
|
||||
RequiresClausePosition: OwnLine
|
||||
|
||||
PointerAlignment: Right
|
||||
ReferenceAlignment: Right
|
||||
IndentAccessModifiers: false
|
||||
AccessModifierOffset: -4
|
||||
|
||||
SortIncludes: CaseSensitive
|
||||
SpaceAfterTemplateKeyword: false
|
||||
AlignEscapedNewlines: DontAlign
|
||||
AlignConsecutiveAssignments: false
|
||||
AlignConsecutiveDeclarations: false
|
||||
...
|
||||
@@ -13,7 +13,8 @@
|
||||
#include <smath.hpp>
|
||||
using smath::Vec2;
|
||||
|
||||
enum Color : uint8_t {
|
||||
enum Color : uint8_t
|
||||
{
|
||||
CLR_NONE = 0, // default
|
||||
CLR_AXES = 90, // bright black (gray)
|
||||
CLR_A = 32, // green
|
||||
@@ -22,18 +23,21 @@ enum Color : uint8_t {
|
||||
CLR_DOT = 36 // cyan
|
||||
};
|
||||
|
||||
struct Cell {
|
||||
struct Cell
|
||||
{
|
||||
char ch { ' ' };
|
||||
uint8_t color { CLR_NONE };
|
||||
int prio { 0 };
|
||||
};
|
||||
|
||||
struct Canvas {
|
||||
struct Canvas
|
||||
{
|
||||
int w, h;
|
||||
std::vector<Cell> pix;
|
||||
Canvas(int W, int H) : w(W), h(H), pix(W * H) { }
|
||||
|
||||
void put(int x, int y, char c, int prio, uint8_t color) {
|
||||
void put(int x, int y, char c, int prio, uint8_t color)
|
||||
{
|
||||
if (x < 0 || x >= w || y < 0 || y >= h)
|
||||
return;
|
||||
Cell &cell = pix[y * w + x];
|
||||
@@ -43,16 +47,19 @@ struct Canvas {
|
||||
cell.color = color;
|
||||
}
|
||||
}
|
||||
void hline(int y, char c, int prio, uint8_t color) {
|
||||
void hline(int y, char c, int prio, uint8_t color)
|
||||
{
|
||||
for (int x = 0; x < w; ++x)
|
||||
put(x, y, c, prio, color);
|
||||
}
|
||||
void vline(int x, char c, int prio, uint8_t color) {
|
||||
void vline(int x, char c, int prio, uint8_t color)
|
||||
{
|
||||
for (int y = 0; y < h; ++y)
|
||||
put(x, y, c, prio, color);
|
||||
}
|
||||
|
||||
void line_dir(int x0, int y0, int x1, int y1, int prio, uint8_t color) {
|
||||
void line_dir(int x0, int y0, int x1, int y1, int prio, uint8_t color)
|
||||
{
|
||||
int dx = std::abs(x1 - x0), sx = x0 < x1 ? 1 : -1;
|
||||
int dy = -std::abs(y1 - y0), sy = y0 < y1 ? 1 : -1;
|
||||
int err = dx + dy;
|
||||
@@ -92,7 +99,8 @@ struct Canvas {
|
||||
}
|
||||
}
|
||||
|
||||
void flush() const {
|
||||
void flush() const
|
||||
{
|
||||
uint8_t cur = 255;
|
||||
for (int y = 0; y < h; ++y) {
|
||||
for (int x = 0; x < w; ++x) {
|
||||
@@ -112,19 +120,23 @@ struct Canvas {
|
||||
}
|
||||
};
|
||||
|
||||
struct Mapper {
|
||||
struct Mapper
|
||||
{
|
||||
int W, H;
|
||||
double scale;
|
||||
int cx, cy;
|
||||
Mapper(int w, int h, double s) : W(w), H(h), scale(s), cx(w / 2), cy(h / 2) {}
|
||||
std::pair<int, int> map(Vec2 v) const {
|
||||
Mapper(int w, int h, double s) : W(w), H(h), scale(s), cx(w / 2), cy(h / 2)
|
||||
{ }
|
||||
std::pair<int, int> map(Vec2 v) const
|
||||
{
|
||||
int x = cx + (int)std::llround(v.x() * scale);
|
||||
int y = cy - (int)std::llround(v.y() * scale);
|
||||
return { x, y };
|
||||
}
|
||||
};
|
||||
|
||||
int main() {
|
||||
int main()
|
||||
{
|
||||
#if defined(_WIN32)
|
||||
HANDLE h = GetStdHandle(STD_OUTPUT_HANDLE);
|
||||
if (h != INVALID_HANDLE_VALUE) {
|
||||
@@ -151,19 +163,21 @@ int main() {
|
||||
std::println("a = {}", a);
|
||||
std::println("b = {}", b);
|
||||
std::println("proj_b(a) = p = {}", p);
|
||||
std::println("|a|={} |b|={} |p|={}", a.magnitude(), b.magnitude(),
|
||||
p.magnitude());
|
||||
std::println(
|
||||
"|a|={} |b|={} |p|={}", a.magnitude(), b.magnitude(), p.magnitude());
|
||||
double dot = a.dot(b);
|
||||
double ang = (a.magnitude() > 0 && b.magnitude() > 0)
|
||||
? std::acos(std::clamp(dot / (a.magnitude() * b.magnitude()),
|
||||
-1.0, 1.0)) *
|
||||
180.0 / M_PI
|
||||
? std::acos(
|
||||
std::clamp(dot / (a.magnitude() * b.magnitude()), -1.0, 1.0))
|
||||
* 180.0 / M_PI
|
||||
: 0.0;
|
||||
std::println("a·b={} angle(a,b)={} deg\n", dot, ang);
|
||||
|
||||
const int W = 73, H = 33;
|
||||
double maxr = std::max({(double)a.magnitude(), (double)b.magnitude(),
|
||||
(double)p.magnitude(), 1.0});
|
||||
double maxr = std::max({ (double)a.magnitude(),
|
||||
(double)b.magnitude(),
|
||||
(double)p.magnitude(),
|
||||
1.0 });
|
||||
double usable = 0.45 * std::min(W, H);
|
||||
double scale = usable / maxr;
|
||||
|
||||
|
||||
@@ -24,7 +24,8 @@
|
||||
|
||||
#include <smath.hpp>
|
||||
|
||||
auto main() -> int {
|
||||
auto main() -> int
|
||||
{
|
||||
using namespace smath;
|
||||
|
||||
Vec2d point;
|
||||
|
||||
@@ -19,7 +19,8 @@
|
||||
|
||||
#include <smath.hpp>
|
||||
|
||||
int main() {
|
||||
int main()
|
||||
{
|
||||
using namespace smath;
|
||||
Vec3 v { 1, 2, 3 };
|
||||
std::println("v: {}", v);
|
||||
|
||||
@@ -35,17 +35,20 @@
|
||||
#define SMATH_ANGLE_UNIT rad
|
||||
#endif // SMATH_ANGLE_UNIT
|
||||
|
||||
namespace smath {
|
||||
namespace smath
|
||||
{
|
||||
|
||||
template<std::size_t N, typename T>
|
||||
requires std::is_arithmetic_v<T> struct Vec;
|
||||
requires std::is_arithmetic_v<T>
|
||||
struct Vec;
|
||||
|
||||
namespace detail {
|
||||
namespace detail
|
||||
{
|
||||
|
||||
#define SMATH_STR(x) #x
|
||||
#define SMATH_XSTR(x) SMATH_STR(x)
|
||||
|
||||
consteval bool streq(const char *a, const char *b)
|
||||
consteval auto streq(const char *a, const char *b) -> bool
|
||||
{
|
||||
for (;; ++a, ++b) {
|
||||
if (*a != *b)
|
||||
@@ -55,13 +58,14 @@ consteval bool streq(const char *a, const char *b)
|
||||
}
|
||||
}
|
||||
|
||||
enum class AngularUnit {
|
||||
enum class AngularUnit
|
||||
{
|
||||
Radians,
|
||||
Degrees,
|
||||
Turns,
|
||||
};
|
||||
|
||||
consteval std::optional<AngularUnit> parse_unit(char const *s)
|
||||
consteval auto parse_unit(char const *s) -> std::optional<AngularUnit>
|
||||
{
|
||||
if (streq(s, "rad"))
|
||||
return AngularUnit::Radians;
|
||||
@@ -76,7 +80,8 @@ constexpr auto SMATH_ANGLE_UNIT_ID = parse_unit(SMATH_XSTR(SMATH_ANGLE_UNIT));
|
||||
static_assert(SMATH_ANGLE_UNIT_ID != std::nullopt,
|
||||
"Invalid SMATH_ANGLE_UNIT. Should be rad, deg, or turns.");
|
||||
|
||||
template<std::size_t N> struct FixedString {
|
||||
template<std::size_t N> struct FixedString
|
||||
{
|
||||
char data[N] {};
|
||||
static constexpr std::size_t size = N - 1;
|
||||
constexpr FixedString(char const (&s)[N])
|
||||
@@ -86,8 +91,10 @@ template<std::size_t N> struct FixedString {
|
||||
}
|
||||
constexpr char operator[](std::size_t i) const { return data[i]; }
|
||||
};
|
||||
template<class X> struct is_Vec : std::false_type { };
|
||||
template<std::size_t M, class U> struct is_Vec<Vec<M, U>> : std::true_type { };
|
||||
template<class X> struct is_Vec : std::false_type
|
||||
{ };
|
||||
template<std::size_t M, class U> struct is_Vec<Vec<M, U>> : std::true_type
|
||||
{ };
|
||||
template<class X>
|
||||
inline constexpr bool is_Vec_v = is_Vec<std::remove_cvref_t<X>>::value;
|
||||
template<class X>
|
||||
@@ -95,7 +102,8 @@ inline constexpr bool is_scalar_v
|
||||
= std::is_arithmetic_v<std::remove_cvref_t<X>>;
|
||||
template<class X> struct Vec_size;
|
||||
template<std::size_t M, class U>
|
||||
struct Vec_size<Vec<M, U>> : std::integral_constant<std::size_t, M> { };
|
||||
struct Vec_size<Vec<M, U>> : std::integral_constant<std::size_t, M>
|
||||
{ };
|
||||
|
||||
template<class T> constexpr auto pack_unorm8(T v) -> std::uint8_t
|
||||
{
|
||||
@@ -144,9 +152,11 @@ template<class T> constexpr auto unpack_snorm8(std::int8_t b) -> T
|
||||
} // namespace detail
|
||||
|
||||
template<std::size_t N, typename T = float>
|
||||
requires std::is_arithmetic_v<T> struct Vec : std::array<T, N> {
|
||||
requires std::is_arithmetic_v<T>
|
||||
struct Vec : std::array<T, N>
|
||||
{
|
||||
private:
|
||||
template<class X> static consteval std::size_t extent()
|
||||
template<class X> static consteval auto extent() -> std::size_t
|
||||
{
|
||||
if constexpr (detail::is_Vec_v<X>)
|
||||
return detail::Vec_size<std::remove_cvref_t<X>>::value;
|
||||
@@ -155,7 +165,7 @@ private:
|
||||
else
|
||||
return 0; // Should be unreachable
|
||||
}
|
||||
template<class... Args> static consteval std::size_t total_extent()
|
||||
template<class... Args> static consteval auto total_extent() -> std::size_t
|
||||
{
|
||||
return (extent<Args>() + ... + 0);
|
||||
}
|
||||
@@ -188,12 +198,9 @@ public:
|
||||
// NOTE: This can (probably) be improved with C++26 reflection in the
|
||||
// future.
|
||||
#define VEC_ACC(component, req, idx) \
|
||||
constexpr auto component() noexcept -> T & \
|
||||
requires(N >= req) \
|
||||
{ \
|
||||
constexpr auto component() noexcept -> T &requires(N >= req) { \
|
||||
return (*this)[idx]; \
|
||||
} \
|
||||
constexpr auto component() const -> T const & \
|
||||
} constexpr auto component() const->T const & \
|
||||
requires(N >= req) \
|
||||
{ \
|
||||
return (*this)[idx]; \
|
||||
@@ -225,7 +232,8 @@ public:
|
||||
((args = (*this)[Is]), ...);
|
||||
}
|
||||
|
||||
template<class... Args> constexpr void unpack(Args &...args) noexcept
|
||||
template<class... Args>
|
||||
constexpr auto unpack(Args &...args) noexcept -> void
|
||||
{
|
||||
unpack_impl(std::index_sequence_for<Args...> {}, args...);
|
||||
}
|
||||
@@ -393,11 +401,14 @@ public:
|
||||
}
|
||||
|
||||
template<typename U = T>
|
||||
requires(N == 3) constexpr auto cross(Vec const &r) const noexcept -> Vec
|
||||
requires(N == 3)
|
||||
constexpr auto cross(Vec const &r) const noexcept -> Vec
|
||||
{
|
||||
return { (*this)[1] * r[2] - (*this)[2] * r[1],
|
||||
return {
|
||||
(*this)[1] * r[2] - (*this)[2] * r[1],
|
||||
(*this)[2] * r[0] - (*this)[0] * r[2],
|
||||
(*this)[0] * r[1] - (*this)[1] * r[0] };
|
||||
(*this)[0] * r[1] - (*this)[1] * r[0],
|
||||
};
|
||||
}
|
||||
|
||||
constexpr auto distance(Vec const &r) const noexcept -> T
|
||||
@@ -425,8 +436,8 @@ public:
|
||||
|
||||
template<class U>
|
||||
requires(std::is_arithmetic_v<U> && N >= 1)
|
||||
constexpr explicit(!std::is_convertible_v<T, U>)
|
||||
operator Vec<N, U>() const noexcept
|
||||
constexpr explicit(
|
||||
!std::is_convertible_v<T, U>) operator Vec<N, U>() const noexcept
|
||||
{
|
||||
Vec<N, U> r {};
|
||||
for (std::size_t i = 0; i < N; ++i)
|
||||
@@ -444,26 +455,26 @@ public:
|
||||
}
|
||||
|
||||
private:
|
||||
constexpr void fill_one(std::size_t &i, T const &v) noexcept
|
||||
constexpr auto fill_one(std::size_t &i, T const &v) noexcept -> void
|
||||
{
|
||||
(*this)[i++] = v;
|
||||
}
|
||||
#ifdef SMATH_IMPLICIT_CONVERSIONS
|
||||
template<class U>
|
||||
requires std::is_arithmetic_v<U> && (!std::is_same_v<U, T>)constexpr void
|
||||
fill_one(std::size_t &i, const U &v) noexcept
|
||||
requires std::is_arithmetic_v<U> && (!std::is_same_v<U, T>)
|
||||
constexpr auto fill_one(std::size_t &i, const U &v) noexcept -> void
|
||||
{
|
||||
(*this)[i++] = static_cast<T>(v);
|
||||
}
|
||||
template<std::size_t M, class U>
|
||||
constexpr void fill_one(std::size_t &i, Vec<M, U> const &v) noexcept
|
||||
constexpr auto fill_one(std::size_t &i, Vec<M, U> const &v) noexcept -> void
|
||||
{
|
||||
for (std::size_t k = 0; k < M; ++k)
|
||||
(*this)[i++] = static_cast<T>(v[k]);
|
||||
}
|
||||
#endif // SMATH_IMPLICIT_CONVERSIONS
|
||||
template<std::size_t M>
|
||||
constexpr void fill_one(std::size_t &i, const Vec<M, T> &v) noexcept
|
||||
constexpr auto fill_one(std::size_t &i, const Vec<M, T> &v) noexcept -> void
|
||||
{
|
||||
for (std::size_t k = 0; k < M; ++k)
|
||||
(*this)[i++] = static_cast<T>(v[k]);
|
||||
@@ -497,7 +508,8 @@ template<std::size_t N, typename T = float>
|
||||
requires std::is_arithmetic_v<T>
|
||||
using VecOrScalar = std::conditional_t<N == 1, T, Vec<N, T>>;
|
||||
|
||||
namespace detail {
|
||||
namespace detail
|
||||
{
|
||||
|
||||
consteval auto char_to_idx(char c) -> std::size_t
|
||||
{
|
||||
@@ -627,9 +639,11 @@ template<class T> constexpr auto turns(T value)
|
||||
}
|
||||
}
|
||||
template<std::size_t R, std::size_t C, typename T>
|
||||
requires std::is_arithmetic_v<T> struct Mat;
|
||||
requires std::is_arithmetic_v<T>
|
||||
struct Mat;
|
||||
|
||||
template<class T> struct Quaternion : Vec<4, T> {
|
||||
template<class T> struct Quaternion : Vec<4, T>
|
||||
{
|
||||
using Base = Vec<4, T>;
|
||||
using Base::Base;
|
||||
using Base::operator=;
|
||||
@@ -759,7 +773,9 @@ requires std::is_floating_point_v<T>
|
||||
}
|
||||
|
||||
template<std::size_t R, std::size_t C, typename T = float>
|
||||
requires std::is_arithmetic_v<T> struct Mat : std::array<Vec<R, T>, C> {
|
||||
requires std::is_arithmetic_v<T>
|
||||
struct Mat : std::array<Vec<R, T>, C>
|
||||
{
|
||||
using Base = std::array<Vec<R, T>, C>;
|
||||
using Base::operator[];
|
||||
|
||||
@@ -792,10 +808,8 @@ requires std::is_arithmetic_v<T> struct Mat : std::array<Vec<R, T>, C> {
|
||||
template<typename... Cols>
|
||||
requires(sizeof...(Cols) == C
|
||||
&& (std::same_as<std::remove_cvref_t<Cols>, Vec<R, T>> && ...))
|
||||
constexpr Mat(Cols const &...cols) noexcept
|
||||
: Base { cols... }
|
||||
{
|
||||
}
|
||||
constexpr Mat(Cols const &...cols) noexcept : Base { cols... }
|
||||
{ }
|
||||
|
||||
constexpr auto col(std::size_t j) noexcept -> Vec<R, T> &
|
||||
{
|
||||
@@ -1100,8 +1114,12 @@ template<typename T>
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
[[nodiscard]] inline auto matrix_ortho3d(T const left, T const right,
|
||||
T const bottom, T const top, T const near, T const far,
|
||||
[[nodiscard]] inline auto matrix_ortho3d(T const left,
|
||||
T const right,
|
||||
T const bottom,
|
||||
T const top,
|
||||
T const near,
|
||||
T const far,
|
||||
bool const flip_z_axis = true) -> Mat<4, 4, T>
|
||||
{
|
||||
Mat<4, 4, T> res {};
|
||||
@@ -1148,8 +1166,9 @@ inline auto matrix_perspective(
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
[[nodiscard]] inline auto matrix_look_at(Vec<3, T> const eye,
|
||||
Vec<3, T> const center, Vec<3, T> const up) -> Mat<4, 4, T>
|
||||
[[nodiscard]] inline auto matrix_look_at(
|
||||
Vec<3, T> const eye, Vec<3, T> const center, Vec<3, T> const up)
|
||||
-> Mat<4, 4, T>
|
||||
{
|
||||
auto f = (center - eye).normalized_safe();
|
||||
auto s = f.cross(up).normalized_safe();
|
||||
@@ -1164,8 +1183,9 @@ template<typename T>
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
[[nodiscard]] inline auto matrix_infinite_perspective(T const fovy,
|
||||
T const aspect, T const znear, bool flip_z_axis = false) -> Mat<4, 4, T>
|
||||
[[nodiscard]] inline auto matrix_infinite_perspective(
|
||||
T const fovy, T const aspect, T const znear, bool flip_z_axis = false)
|
||||
-> Mat<4, 4, T>
|
||||
{
|
||||
Mat<4, 4, T> m {};
|
||||
|
||||
@@ -1186,12 +1206,11 @@ template<typename T>
|
||||
return m;
|
||||
}
|
||||
|
||||
|
||||
template<class Ext> struct interop_adapter;
|
||||
|
||||
template<class Ext>
|
||||
constexpr auto from_external(Ext const &ext)
|
||||
-> typename interop_adapter<Ext>::smath_type
|
||||
constexpr auto from_external(Ext const &ext) ->
|
||||
typename interop_adapter<Ext>::smath_type
|
||||
{
|
||||
return interop_adapter<Ext>::to_smath(ext);
|
||||
}
|
||||
@@ -1202,12 +1221,12 @@ constexpr auto to_external(SMathT const &value) -> Ext
|
||||
return interop_adapter<Ext>::from_smath(value);
|
||||
}
|
||||
|
||||
|
||||
} // namespace smath
|
||||
|
||||
template<std::size_t N, typename T>
|
||||
requires std::formattable<T, char>
|
||||
struct std::formatter<smath::Vec<N, T>> : std::formatter<T> {
|
||||
struct std::formatter<smath::Vec<N, T>> : std::formatter<T>
|
||||
{
|
||||
constexpr auto parse(std::format_parse_context &ctx)
|
||||
{
|
||||
return std::formatter<T>::parse(ctx);
|
||||
@@ -1230,12 +1249,14 @@ struct std::formatter<smath::Vec<N, T>> : std::formatter<T> {
|
||||
}
|
||||
};
|
||||
|
||||
namespace std {
|
||||
namespace std
|
||||
{
|
||||
template<size_t N, class T>
|
||||
struct tuple_size<smath::Vec<N, T>> : std::integral_constant<size_t, N> { };
|
||||
struct tuple_size<smath::Vec<N, T>> : std::integral_constant<size_t, N>
|
||||
{ };
|
||||
|
||||
template<size_t I, size_t N, class T>
|
||||
struct tuple_element<I, smath::Vec<N, T>> {
|
||||
template<size_t I, size_t N, class T> struct tuple_element<I, smath::Vec<N, T>>
|
||||
{
|
||||
static_assert(I < N);
|
||||
using type = T;
|
||||
};
|
||||
|
||||
@@ -2,14 +2,17 @@
|
||||
|
||||
#include <smath.hpp>
|
||||
|
||||
TEST(AngleReturnRadians, DegInput) {
|
||||
TEST(AngleReturnRadians, DegInput)
|
||||
{
|
||||
EXPECT_NEAR(smath::deg(180.0), std::numbers::pi, 1e-12);
|
||||
}
|
||||
|
||||
TEST(AngleReturnRadians, RadInput) {
|
||||
TEST(AngleReturnRadians, RadInput)
|
||||
{
|
||||
EXPECT_DOUBLE_EQ(smath::rad(std::numbers::pi), std::numbers::pi);
|
||||
}
|
||||
|
||||
TEST(AngleReturnRadians, TurnsInput) {
|
||||
TEST(AngleReturnRadians, TurnsInput)
|
||||
{
|
||||
EXPECT_NEAR(smath::turns(0.5), std::numbers::pi, 1e-12);
|
||||
}
|
||||
|
||||
@@ -4,22 +4,26 @@
|
||||
|
||||
#include <smath.hpp>
|
||||
|
||||
struct ExternalVec3f {
|
||||
struct ExternalVec3f
|
||||
{
|
||||
float x;
|
||||
float y;
|
||||
float z;
|
||||
};
|
||||
|
||||
struct ExternalMat2f {
|
||||
struct ExternalMat2f
|
||||
{
|
||||
float m00;
|
||||
float m01;
|
||||
float m10;
|
||||
float m11;
|
||||
};
|
||||
|
||||
namespace smath {
|
||||
namespace smath
|
||||
{
|
||||
|
||||
template<> struct interop_adapter<ExternalVec3f> {
|
||||
template<> struct interop_adapter<ExternalVec3f>
|
||||
{
|
||||
using smath_type = Vec<3, float>;
|
||||
|
||||
static constexpr auto to_smath(ExternalVec3f const &v) -> smath_type
|
||||
@@ -33,7 +37,8 @@ template<> struct interop_adapter<ExternalVec3f> {
|
||||
}
|
||||
};
|
||||
|
||||
template<> struct interop_adapter<ExternalMat2f> {
|
||||
template<> struct interop_adapter<ExternalMat2f>
|
||||
{
|
||||
using smath_type = Mat<2, 2, float>;
|
||||
|
||||
static constexpr auto to_smath(ExternalMat2f const &m) -> smath_type
|
||||
@@ -54,7 +59,8 @@ template<> struct interop_adapter<ExternalMat2f> {
|
||||
|
||||
} // namespace smath
|
||||
|
||||
TEST(Interop, FromExternalVec) {
|
||||
TEST(Interop, FromExternalVec)
|
||||
{
|
||||
ExternalVec3f ext { 1.0f, 2.0f, 3.0f };
|
||||
auto v = smath::from_external(ext);
|
||||
|
||||
@@ -64,7 +70,8 @@ TEST(Interop, FromExternalVec) {
|
||||
EXPECT_EQ(v[2], 3.0f);
|
||||
}
|
||||
|
||||
TEST(Interop, ToExternalVec) {
|
||||
TEST(Interop, ToExternalVec)
|
||||
{
|
||||
smath::Vec3 v { 4.0f, 5.0f, 6.0f };
|
||||
auto ext = smath::to_external<ExternalVec3f>(v);
|
||||
|
||||
@@ -73,7 +80,8 @@ TEST(Interop, ToExternalVec) {
|
||||
EXPECT_EQ(ext.z, 6.0f);
|
||||
}
|
||||
|
||||
TEST(Interop, RoundtripVec) {
|
||||
TEST(Interop, RoundtripVec)
|
||||
{
|
||||
ExternalVec3f ext { 7.0f, 8.0f, 9.0f };
|
||||
auto v = smath::from_external(ext);
|
||||
auto ext2 = smath::to_external<ExternalVec3f>(v);
|
||||
@@ -83,7 +91,8 @@ TEST(Interop, RoundtripVec) {
|
||||
EXPECT_EQ(ext2.z, 9.0f);
|
||||
}
|
||||
|
||||
TEST(Interop, MatrixConversion) {
|
||||
TEST(Interop, MatrixConversion)
|
||||
{
|
||||
ExternalMat2f ext { 1.0f, 2.0f, 3.0f, 4.0f };
|
||||
auto m = smath::from_external(ext);
|
||||
|
||||
|
||||
@@ -12,21 +12,24 @@ using smath::Vec3;
|
||||
using smath::Vec4;
|
||||
|
||||
template<class T>
|
||||
static void ExpectVecNear(const Vec<3, T> &a, const Vec<3, T> &b,
|
||||
T eps = T(1e-6)) {
|
||||
static void ExpectVecNear(
|
||||
const Vec<3, T> &a, const Vec<3, T> &b, T eps = T(1e-6))
|
||||
{
|
||||
for (int i = 0; i < 3; ++i)
|
||||
EXPECT_NEAR(double(a[i]), double(b[i]), double(eps));
|
||||
}
|
||||
|
||||
// Constructors and accessors
|
||||
TEST(Vec, DefaultZero) {
|
||||
TEST(Vec, DefaultZero)
|
||||
{
|
||||
Vec3 v;
|
||||
EXPECT_EQ(v[0], 0.0f);
|
||||
EXPECT_EQ(v[1], 0.0f);
|
||||
EXPECT_EQ(v[2], 0.0f);
|
||||
}
|
||||
|
||||
TEST(Vec, ScalarFillCtor) {
|
||||
TEST(Vec, ScalarFillCtor)
|
||||
{
|
||||
Vec4 v { 2.0f };
|
||||
EXPECT_EQ(v.x(), 2.0f);
|
||||
EXPECT_EQ(v.y(), 2.0f);
|
||||
@@ -34,7 +37,8 @@ TEST(Vec, ScalarFillCtor) {
|
||||
EXPECT_EQ(v.w(), 2.0f);
|
||||
}
|
||||
|
||||
TEST(Vec, VariadicCtorScalarsAndSubvectors) {
|
||||
TEST(Vec, VariadicCtorScalarsAndSubvectors)
|
||||
{
|
||||
Vec2 a { 1.0f, 2.0f };
|
||||
Vec2 b { 3.0f, 4.0f };
|
||||
Vec4 v { a, b };
|
||||
@@ -44,7 +48,8 @@ TEST(Vec, VariadicCtorScalarsAndSubvectors) {
|
||||
EXPECT_EQ(v.a(), 4.0f);
|
||||
}
|
||||
|
||||
TEST(Vec, NamedAccessorsAliases) {
|
||||
TEST(Vec, NamedAccessorsAliases)
|
||||
{
|
||||
Vec3 v { 1.0f, 2.0f, 3.0f };
|
||||
EXPECT_EQ(v.x(), v.r());
|
||||
EXPECT_EQ(v.y(), v.g());
|
||||
@@ -52,7 +57,8 @@ TEST(Vec, NamedAccessorsAliases) {
|
||||
}
|
||||
|
||||
// Arithmetic
|
||||
TEST(Vec, ElementwiseAndScalarOps) {
|
||||
TEST(Vec, ElementwiseAndScalarOps)
|
||||
{
|
||||
Vec3 a { 1.0f, 2.0f, 3.0f };
|
||||
Vec3 b { 4.0f, 5.0f, 6.0f };
|
||||
|
||||
@@ -84,7 +90,8 @@ TEST(Vec, ElementwiseAndScalarOps) {
|
||||
}
|
||||
|
||||
// Length, dot, cross, normalize
|
||||
TEST(Vec, MagnitudeAndDot) {
|
||||
TEST(Vec, MagnitudeAndDot)
|
||||
{
|
||||
Vec3 v { 3.0f, 4.0f, 12.0f };
|
||||
EXPECT_FLOAT_EQ(v.magnitude(), 13.0f);
|
||||
EXPECT_FLOAT_EQ(v.length(), 13.0f);
|
||||
@@ -93,7 +100,8 @@ TEST(Vec, MagnitudeAndDot) {
|
||||
EXPECT_FLOAT_EQ(v.dot(u), 27.0f);
|
||||
}
|
||||
|
||||
TEST(Vec, Cross3D) {
|
||||
TEST(Vec, Cross3D)
|
||||
{
|
||||
Vec3 x { 1.0f, 0.0f, 0.0f };
|
||||
Vec3 y { 0.0f, 1.0f, 0.0f };
|
||||
auto z = x.cross(y);
|
||||
@@ -102,7 +110,8 @@ TEST(Vec, Cross3D) {
|
||||
EXPECT_EQ(z[2], 1.0f);
|
||||
}
|
||||
|
||||
TEST(Vec, NormalizeAndSafeNormalize) {
|
||||
TEST(Vec, NormalizeAndSafeNormalize)
|
||||
{
|
||||
Vec3 v { 10.0f, 0.0f, 0.0f };
|
||||
auto n = v.normalized();
|
||||
auto ns = v.normalized_safe();
|
||||
@@ -115,7 +124,8 @@ TEST(Vec, NormalizeAndSafeNormalize) {
|
||||
EXPECT_EQ(zs[2], 0.0f);
|
||||
}
|
||||
|
||||
TEST(Vec, DistanceAndProjection) {
|
||||
TEST(Vec, DistanceAndProjection)
|
||||
{
|
||||
Vec3 a { 1.0f, 2.0f, 3.0f };
|
||||
Vec3 b { 4.0f, 6.0f, 3.0f };
|
||||
EXPECT_FLOAT_EQ(a.distance(b), 5.0f);
|
||||
@@ -126,7 +136,8 @@ TEST(Vec, DistanceAndProjection) {
|
||||
}
|
||||
|
||||
// Approx equal
|
||||
TEST(Vec, ApproxEqual) {
|
||||
TEST(Vec, ApproxEqual)
|
||||
{
|
||||
Vec3 a { 1.0f, 2.0f, 3.0f };
|
||||
Vec3 b { 1.0f + 1e-7f, 2.0f - 1e-7f, 3.0f };
|
||||
EXPECT_TRUE(a.approx_equal(b, 1e-6f));
|
||||
@@ -134,7 +145,8 @@ TEST(Vec, ApproxEqual) {
|
||||
}
|
||||
|
||||
// std::get & tuple interop
|
||||
TEST(Vec, StdGetAndTuple) {
|
||||
TEST(Vec, StdGetAndTuple)
|
||||
{
|
||||
Vec3 v { 7.0f, 8.0f, 9.0f };
|
||||
static_assert(std::tuple_size_v<Vec3> == 3);
|
||||
static_assert(std::is_same_v<std::tuple_element_t<1, Vec3>, float>);
|
||||
@@ -144,7 +156,8 @@ TEST(Vec, StdGetAndTuple) {
|
||||
}
|
||||
|
||||
// Swizzle
|
||||
TEST(Vec, SwizzleBasic) {
|
||||
TEST(Vec, SwizzleBasic)
|
||||
{
|
||||
const Vec3 v { 1.0f, 2.0f, 3.0f };
|
||||
|
||||
auto yz = smath::swizzle<"yz">(v);
|
||||
@@ -158,14 +171,16 @@ TEST(Vec, SwizzleBasic) {
|
||||
}
|
||||
|
||||
// std::formatter
|
||||
TEST(Vec, Formatter) {
|
||||
TEST(Vec, Formatter)
|
||||
{
|
||||
smath::Vec<3, int> vi { 1, 2, 3 };
|
||||
std::string s = std::format("{}", vi);
|
||||
EXPECT_EQ(s, "{1, 2, 3}");
|
||||
}
|
||||
|
||||
// Conversions
|
||||
TEST(Vec, ExplicitConversionBetweenScalarTypes) {
|
||||
TEST(Vec, ExplicitConversionBetweenScalarTypes)
|
||||
{
|
||||
smath::Vec<3, int> vi { 1, 2, 3 };
|
||||
smath::Vec<3, float> vf { vi };
|
||||
EXPECT_EQ(vf[0], 1.0f);
|
||||
|
||||
Reference in New Issue
Block a user