Compare commits

...

3 Commits

Author SHA1 Message Date
830c64b25a Add modules support
Signed-off-by: Slendi <slendi@socopon.com>
2025-12-28 00:24:52 +02:00
a6fe9c2f2c Add const accessors to Quaternion
Signed-off-by: Slendi <slendi@socopon.com>
2025-12-27 21:43:35 +02:00
75d5a197a9 Quick fix for deg/rad/turns functions
Signed-off-by: Slendi <slendi@socopon.com>
2025-12-27 21:36:38 +02:00
4 changed files with 93 additions and 16 deletions

View File

@@ -4,8 +4,14 @@ project(SmathExamples LANGUAGES CXX VERSION 0.1.0)
set(CMAKE_CXX_STANDARD 23) set(CMAKE_CXX_STANDARD 23)
set(CMAKE_CXX_STANDARD_REQUIRED ON) set(CMAKE_CXX_STANDARD_REQUIRED ON)
option(BUILD_EXAMPLES "Build example programs" ON) option(SMATH_BUILD_EXAMPLES "Build example programs" ON)
option(BUILD_TESTS "Build unit tests" ON) option(SMATH_BUILD_TESTS "Build unit tests" ON)
option(SMATH_BUILD_MODULES "Enable C++20 modules support" OFF)
if(SMATH_BUILD_MODULES)
message(STATUS "Building smath C++ module")
add_subdirectory(src/modules)
endif()
add_library(smath INTERFACE) add_library(smath INTERFACE)
target_include_directories(smath target_include_directories(smath
@@ -55,7 +61,7 @@ install(FILES
DESTINATION lib/cmake/smath DESTINATION lib/cmake/smath
) )
if(BUILD_EXAMPLES) if(SMATH_BUILD_EXAMPLES)
file(GLOB EXAMPLE_SOURCES "${CMAKE_SOURCE_DIR}/examples/*.cpp") file(GLOB EXAMPLE_SOURCES "${CMAKE_SOURCE_DIR}/examples/*.cpp")
foreach(EXAMPLE_FILE ${EXAMPLE_SOURCES}) foreach(EXAMPLE_FILE ${EXAMPLE_SOURCES})
get_filename_component(EXAMPLE_NAME ${EXAMPLE_FILE} NAME_WE) get_filename_component(EXAMPLE_NAME ${EXAMPLE_FILE} NAME_WE)
@@ -64,7 +70,7 @@ if(BUILD_EXAMPLES)
endforeach() endforeach()
endif() endif()
if(BUILD_TESTS) if(SMATH_BUILD_TESTS)
enable_testing() enable_testing()
find_package(GTest QUIET) find_package(GTest QUIET)

View File

@@ -577,42 +577,47 @@ using Vec2d = Vec<2, double>;
using Vec3d = Vec<3, double>; using Vec3d = Vec<3, double>;
using Vec4d = Vec<4, double>; using Vec4d = Vec<4, double>;
template<class T> constexpr auto deg(T const value) -> T template<class T> constexpr auto deg(T const value)
{ {
using R = std::common_type_t<T, double>;
if constexpr (detail::SMATH_ANGLE_UNIT_ID == detail::AngularUnit::Degrees) { if constexpr (detail::SMATH_ANGLE_UNIT_ID == detail::AngularUnit::Degrees) {
return value; return static_cast<R>(value);
} else if constexpr (detail::SMATH_ANGLE_UNIT_ID } else if constexpr (detail::SMATH_ANGLE_UNIT_ID
== detail::AngularUnit::Radians) { == detail::AngularUnit::Radians) {
return value * static_cast<T>(std::numbers::pi / 180.0); return static_cast<R>(value) * static_cast<R>(std::numbers::pi / 180.0);
} else if constexpr (detail::SMATH_ANGLE_UNIT_ID } else if constexpr (detail::SMATH_ANGLE_UNIT_ID
== detail::AngularUnit::Turns) { == detail::AngularUnit::Turns) {
return value / static_cast<T>(360.0); return static_cast<R>(value) / static_cast<R>(360.0);
} }
} }
template<class T> constexpr auto rad(T const value) -> T template<class T> constexpr auto rad(T const value)
{ {
using R = std::common_type_t<T, double>;
if constexpr (detail::SMATH_ANGLE_UNIT_ID == detail::AngularUnit::Degrees) { if constexpr (detail::SMATH_ANGLE_UNIT_ID == detail::AngularUnit::Degrees) {
return value * static_cast<T>(180.0 / std::numbers::pi); return static_cast<R>(value) * static_cast<R>(180.0 / std::numbers::pi);
} else if constexpr (detail::SMATH_ANGLE_UNIT_ID } else if constexpr (detail::SMATH_ANGLE_UNIT_ID
== detail::AngularUnit::Radians) { == detail::AngularUnit::Radians) {
return value; return static_cast<R>(value);
} else if constexpr (detail::SMATH_ANGLE_UNIT_ID } else if constexpr (detail::SMATH_ANGLE_UNIT_ID
== detail::AngularUnit::Turns) { == detail::AngularUnit::Turns) {
return value / (static_cast<T>(2.0) * static_cast<T>(std::numbers::pi)); return static_cast<R>(value)
/ (static_cast<R>(2.0) * static_cast<R>(std::numbers::pi));
} }
} }
template<class T> constexpr auto turns(T const value) -> T template<class T> constexpr auto turns(T const value)
{ {
using R = std::common_type_t<T, double>;
if constexpr (detail::SMATH_ANGLE_UNIT_ID == detail::AngularUnit::Degrees) { if constexpr (detail::SMATH_ANGLE_UNIT_ID == detail::AngularUnit::Degrees) {
return value * static_cast<T>(360.0); return static_cast<R>(value) * static_cast<R>(360.0);
} else if constexpr (detail::SMATH_ANGLE_UNIT_ID } else if constexpr (detail::SMATH_ANGLE_UNIT_ID
== detail::AngularUnit::Radians) { == detail::AngularUnit::Radians) {
return value * (static_cast<T>(2.0) * static_cast<T>(std::numbers::pi)); return static_cast<R>(value)
* (static_cast<R>(2.0) * static_cast<R>(std::numbers::pi));
} else if constexpr (detail::SMATH_ANGLE_UNIT_ID } else if constexpr (detail::SMATH_ANGLE_UNIT_ID
== detail::AngularUnit::Turns) { == detail::AngularUnit::Turns) {
return value; return static_cast<R>(value);
} }
} }
@@ -625,9 +630,13 @@ template<class T> struct Quaternion : Vec<4, T> {
constexpr Base const &vec() const noexcept { return *this; } constexpr Base const &vec() const noexcept { return *this; }
constexpr T &x() noexcept { return Base::x(); } constexpr T &x() noexcept { return Base::x(); }
constexpr T const &x() const noexcept { return Base::x(); }
constexpr T &y() noexcept { return Base::y(); } constexpr T &y() noexcept { return Base::y(); }
constexpr T const &y() const noexcept { return Base::y(); }
constexpr T &z() noexcept { return Base::z(); } constexpr T &z() noexcept { return Base::z(); }
constexpr T const &z() const noexcept { return Base::z(); }
constexpr T &w() noexcept { return Base::w(); } constexpr T &w() noexcept { return Base::w(); }
constexpr T const &w() const noexcept { return Base::w(); }
constexpr auto operator*(Quaternion const &rhs) const noexcept -> Quaternion constexpr auto operator*(Quaternion const &rhs) const noexcept -> Quaternion
{ {

View File

@@ -0,0 +1,8 @@
add_library(smath_modules)
target_sources(smath_modules PUBLIC FILE_SET CXX_MODULES FILES smath.cppm)
target_link_libraries(smath_modules PUBLIC smath::smath)
target_compile_features(smath_modules PUBLIC cxx_std_20)

54
src/modules/smath.cppm Normal file
View File

@@ -0,0 +1,54 @@
module;
#include "smath.hpp"
export module smath;
export namespace smath {
export using ::smath::Vec;
export using ::smath::VecOrScalar;
export using ::smath::Quaternion;
export using ::smath::Mat;
export using ::smath::Vec2;
export using ::smath::Vec3;
export using ::smath::Vec4;
export using ::smath::Vec2d;
export using ::smath::Vec3d;
export using ::smath::Vec4d;
export using ::smath::Mat2;
export using ::smath::Mat3;
export using ::smath::Mat4;
export using ::smath::Mat2d;
export using ::smath::Mat3d;
export using ::smath::Mat4d;
export using ::smath::swizzle;
export using ::smath::deg;
export using ::smath::rad;
export using ::smath::turns;
export using ::smath::pack_unorm4x8;
export using ::smath::pack_snorm4x8;
export using ::smath::unpack_unorm4x8;
export using ::smath::unpack_snorm4x8;
export using ::smath::operator*;
export using ::smath::translate;
export using ::smath::rotate;
export using ::smath::scale;
export using ::smath::shear_x;
export using ::smath::shear_y;
export using ::smath::shear_z;
export using ::smath::matrix_ortho3d;
export using ::smath::matrix_perspective;
export using ::smath::matrix_infinite_perspective;
export using ::smath::matrix_look_at;
}