From 3b3c5a1a3161ce89a030490b64d0bc56c4a0698b Mon Sep 17 00:00:00 2001 From: "C. J. Howard" Date: Thu, 2 Feb 2023 20:55:54 +0800 Subject: [PATCH] Add nodiscard attribute to math functions. Add more math constants. Improve debug log setup. Fix material pass comparator --- CMakeLists.txt | 10 +- src/animation/spring.hpp | 2 +- src/debug/log.hpp | 12 +- src/game/ant/gene/loader/diet-loader.cpp | 2 +- .../ant/gene/loader/foraging-time-loader.cpp | 2 +- .../ant/gene/loader/founding-mode-loader.cpp | 2 +- src/game/ant/gene/loader/nest-site-loader.cpp | 2 +- src/game/state/boot.cpp | 69 +++--- src/game/system/astronomy.cpp | 4 +- src/geom/primitive/hypersphere.hpp | 2 +- src/geom/solid-angle.hpp | 2 +- src/main.cpp | 201 +++++++++++------- src/math/angles.hpp | 43 ++-- src/math/compile.hpp | 8 +- src/math/constants.hpp | 69 ------ src/math/hash/pcg.hpp | 32 +-- src/math/interpolation.hpp | 27 +-- src/math/literals.hpp | 32 +++ src/math/map.hpp | 2 +- src/math/math.hpp | 2 +- src/math/matrix.hpp | 126 +++++------ src/math/noise/fbm.hpp | 2 +- src/math/noise/simplex.hpp | 10 +- src/math/noise/voronoi.hpp | 10 +- src/math/numbers.hpp | 122 +++++++++++ src/math/polynomial.hpp | 8 +- src/math/projection.hpp | 12 +- src/math/quadrature.hpp | 32 ++- src/math/quaternion.hpp | 86 ++++---- src/math/random.hpp | 5 +- src/math/se3.hpp | 12 +- src/math/transform-functions.hpp | 8 +- src/math/transform-operators.hpp | 4 +- src/math/vector.hpp | 178 ++++++++-------- src/physics/gas/atmosphere.hpp | 2 +- src/physics/light/blackbody.hpp | 2 +- src/physics/light/phase.hpp | 2 +- src/physics/light/photometry.hpp | 2 +- src/physics/orbit/anomaly.hpp | 2 +- src/physics/orbit/elements.hpp | 2 +- src/physics/time/ut1.hpp | 2 +- src/physics/time/utc.hpp | 2 +- src/platform/windows/nvidia.cpp | 2 +- src/render/passes/material-pass.cpp | 10 +- src/render/passes/shadow-map-pass.cpp | 1 - src/render/renderer.cpp | 2 +- src/resources/model-loader.cpp | 2 +- src/scene/camera.cpp | 2 +- 48 files changed, 631 insertions(+), 544 deletions(-) delete mode 100644 src/math/constants.hpp create mode 100644 src/math/literals.hpp create mode 100644 src/math/numbers.hpp diff --git a/CMakeLists.txt b/CMakeLists.txt index ca91615..0b41e73 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,5 +1,4 @@ -cmake_minimum_required(VERSION 3.7) - +cmake_minimum_required(VERSION 3.25) option(VERSION_STRING "Project version string" "0.0.0") @@ -69,6 +68,11 @@ set(EXECUTABLE_TARGET ${PROJECT_NAME}-executable) add_executable(${EXECUTABLE_TARGET} ${SOURCE_FILES}) set_target_properties(${EXECUTABLE_TARGET} PROPERTIES OUTPUT_NAME ${PROJECT_NAME}) +if(MSVC) + # Select the static multithreaded MSVC runtime library + set_property(TARGET ${EXECUTABLE_TARGET} PROPERTY MSVC_RUNTIME_LIBRARY "MultiThreaded$<$:Debug>") +endif() + # Add compile definitions if(CMAKE_BUILD_TYPE STREQUAL "Debug") target_compile_definitions(${EXECUTABLE_TARGET} PRIVATE DEBUG) @@ -78,7 +82,7 @@ endif() # Set C++ standard set_target_properties(${EXECUTABLE_TARGET} PROPERTIES - CXX_STANDARD 20 + CXX_STANDARD 23 CXX_STANDARD_REQUIRED ON CXX_EXTENSIONS OFF) diff --git a/src/animation/spring.hpp b/src/animation/spring.hpp index d6331fc..30411f6 100644 --- a/src/animation/spring.hpp +++ b/src/animation/spring.hpp @@ -20,7 +20,7 @@ #ifndef ANTKEEPER_SPRING_HPP #define ANTKEEPER_SPRING_HPP -#include "math/constants.hpp" +#include "math/numbers.hpp" /** * Contains the variables required for numeric springing. diff --git a/src/debug/log.hpp b/src/debug/log.hpp index 2a3d9bb..79a349e 100644 --- a/src/debug/log.hpp +++ b/src/debug/log.hpp @@ -84,7 +84,7 @@ message(std::string_view, Args&&...) -> message; /** * Formats and logs a trace message. * - * @param Args Types of arguments to be formatted. + * @tparam Args Types of arguments to be formatted. */ template using trace = message; @@ -97,7 +97,7 @@ message(std::string_view, Args&&...) -> message; /** * Formats and logs a debug message. * - * @param Args Types of arguments to be formatted. + * @tparam Args Types of arguments to be formatted. */ template using debug = message; @@ -110,7 +110,7 @@ message(std::string_view, Args&&...) -> message; /** * Formats and logs an info message. * - * @param Args Types of arguments to be formatted. + * @tparam Args Types of arguments to be formatted. */ template using info = message; @@ -123,7 +123,7 @@ message(std::string_view, Args&&...) -> message; /** * Formats and logs a warning message. * - * @param Args Types of arguments to be formatted. + * @tparam Args Types of arguments to be formatted. */ template using warning = message; @@ -136,7 +136,7 @@ message(std::string_view, Args&&...) -> message; /** * Formats and logs an error message. * - * @param Args Types of arguments to be formatted. + * @tparam Args Types of arguments to be formatted. */ template using error = message; @@ -149,7 +149,7 @@ message(std::string_view, Args&&...) -> message; /** * Formats and logs a fatal error message. * - * @param Args Types of arguments to be formatted. + * @tparam Args Types of arguments to be formatted. */ template using fatal = message; diff --git a/src/game/ant/gene/loader/diet-loader.cpp b/src/game/ant/gene/loader/diet-loader.cpp index dd5c606..b478517 100644 --- a/src/game/ant/gene/loader/diet-loader.cpp +++ b/src/game/ant/gene/loader/diet-loader.cpp @@ -23,7 +23,7 @@ #include "resources/json.hpp" #include "game/ant/gene/diet.hpp" #include "math/angles.hpp" -#include "math/constants.hpp" +#include "math/numbers.hpp" #include using namespace game::ant; diff --git a/src/game/ant/gene/loader/foraging-time-loader.cpp b/src/game/ant/gene/loader/foraging-time-loader.cpp index 6d143b9..e8bde42 100644 --- a/src/game/ant/gene/loader/foraging-time-loader.cpp +++ b/src/game/ant/gene/loader/foraging-time-loader.cpp @@ -23,7 +23,7 @@ #include "resources/json.hpp" #include "game/ant/gene/foraging-time.hpp" #include "math/angles.hpp" -#include "math/constants.hpp" +#include "math/numbers.hpp" #include using namespace game::ant; diff --git a/src/game/ant/gene/loader/founding-mode-loader.cpp b/src/game/ant/gene/loader/founding-mode-loader.cpp index 6dffaa5..54e703f 100644 --- a/src/game/ant/gene/loader/founding-mode-loader.cpp +++ b/src/game/ant/gene/loader/founding-mode-loader.cpp @@ -23,7 +23,7 @@ #include "resources/json.hpp" #include "game/ant/gene/founding-mode.hpp" #include "math/angles.hpp" -#include "math/constants.hpp" +#include "math/numbers.hpp" #include using namespace game::ant; diff --git a/src/game/ant/gene/loader/nest-site-loader.cpp b/src/game/ant/gene/loader/nest-site-loader.cpp index 5e04068..21edcc4 100644 --- a/src/game/ant/gene/loader/nest-site-loader.cpp +++ b/src/game/ant/gene/loader/nest-site-loader.cpp @@ -23,7 +23,7 @@ #include "resources/json.hpp" #include "game/ant/gene/nest-site.hpp" #include "math/angles.hpp" -#include "math/constants.hpp" +#include "math/numbers.hpp" #include using namespace game::ant; diff --git a/src/game/state/boot.cpp b/src/game/state/boot.cpp index 984a7b6..303853e 100644 --- a/src/game/state/boot.cpp +++ b/src/game/state/boot.cpp @@ -103,35 +103,28 @@ boot::boot(game::context& ctx, int argc, char** argv): { // Boot process debug::log::trace("Booting up..."); - try - { - // Allocate application - ctx.app = new application(); - - // Parse command line options - parse_options(argc, argv); - - setup_resources(); - load_config(); - load_strings(); - setup_window(); - setup_rendering(); - setup_audio(); - setup_scenes(); - setup_animation(); - setup_entities(); - setup_systems(); - setup_controls(); - setup_ui(); - setup_debugging(); - setup_loop(); - ctx.active_ecoregion = nullptr; - } - catch (const std::exception& e) - { - debug::log::fatal("Boot up failed: unhandled exception: {}", e.what()); - throw e; - } + + // Allocate application + ctx.app = new application(); + + // Parse command line options + parse_options(argc, argv); + + setup_resources(); + load_config(); + load_strings(); + setup_window(); + setup_rendering(); + setup_audio(); + setup_scenes(); + setup_animation(); + setup_entities(); + setup_systems(); + setup_controls(); + setup_ui(); + setup_debugging(); + setup_loop(); + ctx.active_ecoregion = nullptr; debug::log::trace("Boot up complete"); @@ -147,19 +140,11 @@ boot::~boot() { debug::log::trace("Booting down..."); - try - { - shutdown_audio(); - - // Close application - delete ctx.app; - ctx.app = nullptr; - } - catch (const std::exception& e) - { - debug::log::fatal("Boot down failed: unhandled exception: {}", e.what()); - throw e; - } + shutdown_audio(); + + // Close application + delete ctx.app; + ctx.app = nullptr; debug::log::trace("Boot down complete"); } diff --git a/src/game/system/astronomy.cpp b/src/game/system/astronomy.cpp index aeec70b..6d32eed 100644 --- a/src/game/system/astronomy.cpp +++ b/src/game/system/astronomy.cpp @@ -291,13 +291,13 @@ void astronomy::update(double t, double dt) } // Measure luminance of observer reference body as seen by reflector - const double3 reflector_observer_luminance = observer_blackbody_illuminance * reference_body->albedo * observer_reflector_transmittance * reflector_observer_phase_factor * math::inverse_pi; + const double3 reflector_observer_luminance = observer_blackbody_illuminance * reference_body->albedo * observer_reflector_transmittance * reflector_observer_phase_factor * math::inv_pi; // Measure illuminance from observer reference body reaching reflector const double3 reflector_observer_illuminance = reflector_observer_luminance * reflector_observer_solid_angle; // Measure luminance of reflector as seen by observer - const double3 observer_reflector_luminance = (reflector_blackbody_illuminance * observer_reflector_phase_factor + reflector_observer_illuminance) * reflector.albedo * observer_reflector_transmittance * math::inverse_pi; + const double3 observer_reflector_luminance = (reflector_blackbody_illuminance * observer_reflector_phase_factor + reflector_observer_illuminance) * reflector.albedo * observer_reflector_transmittance * math::inv_pi; // Measure illuminance from reflector reaching observer const double3 observer_reflector_illuminance = observer_reflector_luminance * observer_reflector_solid_angle; diff --git a/src/geom/primitive/hypersphere.hpp b/src/geom/primitive/hypersphere.hpp index b7cf640..4239d0a 100644 --- a/src/geom/primitive/hypersphere.hpp +++ b/src/geom/primitive/hypersphere.hpp @@ -20,7 +20,7 @@ #ifndef ANTKEEPER_GEOM_PRIMITIVE_HYPERSPHERE_HPP #define ANTKEEPER_GEOM_PRIMITIVE_HYPERSPHERE_HPP -#include "math/constants.hpp" +#include "math/numbers.hpp" #include "math/vector.hpp" namespace geom { diff --git a/src/geom/solid-angle.hpp b/src/geom/solid-angle.hpp index 4ba8755..c8fbfac 100644 --- a/src/geom/solid-angle.hpp +++ b/src/geom/solid-angle.hpp @@ -20,7 +20,7 @@ #ifndef ANTKEEPER_GEOM_SOLID_ANGLE_HPP #define ANTKEEPER_GEOM_SOLID_ANGLE_HPP -#include "math/constants.hpp" +#include "math/numbers.hpp" #include namespace geom { diff --git a/src/main.cpp b/src/main.cpp index ac6ba1c..8a22637 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -32,84 +32,105 @@ int main(int argc, char* argv[]) { - // Set up logging to standard output on debug builds - #if !defined(NDEBUG) - - // Enable VT100 sequences for colored text - debug::console::enable_vt100(); - - // Subscribe log to cout function to message logged events - auto log_to_cout_subscription = debug::log::default_logger().get_message_logged_channel().subscribe - ( - [&](const auto& event) + // Enable VT100 sequences in console for colored text + debug::console::enable_vt100(); + + // Subscribe log to cout function to message logged events + auto log_to_cout_subscription = debug::log::default_logger().get_message_logged_channel().subscribe + ( + [&](const auto& event) + { + static const char* severities[] = { - static const char* severities[] = - { - "trace", - "debug", - "info", - "warning", - "error", - "fatal" - }; - - static const std::string colors[] = - { - std::format("{}", ansi::fg_white), - std::format("{}", ansi::fg_bright_blue), - std::format("{}", ansi::fg_bright_green), - std::format("{}", ansi::fg_yellow), - std::format("{}", ansi::fg_red), - std::format("{}{}", ansi::fg_white, ansi::bg_bright_red) - }; - - std::osyncstream(std::cout) << std::format - ( - "{}:{}:{}: {}{}: {}{}\n", - std::filesystem::path(event.location.file_name()).filename().string(), - event.location.line(), - event.location.column(), - colors[static_cast(event.severity)], - severities[static_cast(event.severity)], - event.message, - ansi::reset - ); - } - ); - #endif + "trace", + "debug", + "info", + "warning", + "error", + "fatal" + }; + + static const std::string colors[] = + { + std::format("{}", ansi::fg_white), + std::format("{}", ansi::fg_bright_blue), + std::format("{}", ansi::fg_bright_green), + std::format("{}", ansi::fg_yellow), + std::format("{}", ansi::fg_red), + std::format("{}{}", ansi::fg_white, ansi::bg_bright_red) + }; + + std::osyncstream(std::cout) << std::format + ( + "{}:{}:{}: {}{}: {}{}\n", + std::filesystem::path(event.location.file_name()).filename().string(), + event.location.line(), + event.location.column(), + colors[static_cast(event.severity)], + severities[static_cast(event.severity)], + event.message, + ansi::reset + ); + } + ); - // Determine path to log archive, then construct path if it doesn't exist + // Determine path to log archive const std::filesystem::path log_archive_path = get_config_path(config::application_name) / "logs"; - const bool log_archive_created = std::filesystem::create_directories(log_archive_path); - // If log archive already existed - if (!log_archive_created) + // Set up log archive + bool log_archive_exists = false; + try { - // Detect archived logs - std::set log_archive; - for (const auto& entry: std::filesystem::directory_iterator{log_archive_path}) + // Create log archive if it doesn't exist + if (std::filesystem::create_directories(log_archive_path)) { - if (entry.is_regular_file() && - entry.path().extension() == ".log") - { - log_archive.emplace(entry.path()); - } + debug::log::debug("Created log archive \"{}\"", log_archive_path.string()); } - - // Delete expired logs - if (!log_archive.empty()) + else { - for (std::size_t i = log_archive.size() + 1; i > config::debug_log_archive_capacity; --i) + // Clean pre-existing log archive + try { - std::filesystem::remove(*log_archive.begin()); - log_archive.erase(log_archive.begin()); + // Detect and sort archived logs + std::set log_archive; + for (const auto& entry: std::filesystem::directory_iterator{log_archive_path}) + { + if (entry.is_regular_file() && + entry.path().extension() == ".log") + { + log_archive.emplace(entry.path()); + } + } + + debug::log::debug("Detected {} archived log{} at \"{}\"", log_archive.size(), log_archive.size() != 1 ? "s" : "", log_archive_path.string()); + + // Delete expired logs + if (!log_archive.empty()) + { + for (std::size_t i = log_archive.size() + 1; i > config::debug_log_archive_capacity; --i) + { + std::filesystem::remove(*log_archive.begin()); + debug::log::debug("Deleted expired log file \"{}\"", log_archive.begin()->string()); + log_archive.erase(log_archive.begin()); + } + } + } + catch (const std::filesystem::filesystem_error& e) + { + debug::log::error("An error occured while cleaning the log archive \"{}\": {}", log_archive_path.string(), e.what()); } } + + log_archive_exists = true; + } + catch (const std::filesystem::filesystem_error& e) + { + debug::log::error("Failed to create log archive at \"{}\": {}", log_archive_path.string(), e.what()); } // Set up logging to file std::shared_ptr log_to_file_subscription; - if (config::debug_log_archive_capacity) + if (config::debug_log_archive_capacity && log_archive_exists) { // Determine log filename const auto time = std::chrono::floor(std::chrono::system_clock::now()); @@ -117,28 +138,50 @@ int main(int argc, char* argv[]) // Open log file std::filesystem::path log_filepath = log_archive_path / log_filename; + const std::string log_filepath_string = log_filepath.string(); auto log_filestream = std::make_shared(log_filepath); - // Write log file header - (*log_filestream) << "time\tfile\tline\tcolumn\tseverity\tmessage"; - - // Subscribe log to file function to message logged events - log_to_file_subscription = debug::log::default_logger().get_message_logged_channel().subscribe - ( - [log_filestream](const auto& event) + if (log_filestream->is_open()) + { + debug::log::debug("Opened log file \"{}\"", log_filepath_string); + + // Write log file header + (*log_filestream) << "time\tfile\tline\tcolumn\tseverity\tmessage"; + + if (log_filestream->good()) { - std::osyncstream(*log_filestream) << std::format + // Subscribe log to file function to message logged events + log_to_file_subscription = debug::log::default_logger().get_message_logged_channel().subscribe ( - "\n{0:%Y%m%d}T{0:%H%M%S}Z\t{1}\t{2}\t{3}\t{4}\t{5}", - std::chrono::floor(event.time), - std::filesystem::path(event.location.file_name()).filename().string(), - event.location.line(), - event.location.column(), - static_cast(event.severity), - event.message + [log_filestream](const auto& event) + { + std::osyncstream(*log_filestream) << std::format + ( + "\n{0:%Y%m%d}T{0:%H%M%S}Z\t{1}\t{2}\t{3}\t{4}\t{5}", + std::chrono::floor(event.time), + std::filesystem::path(event.location.file_name()).filename().string(), + event.location.line(), + event.location.column(), + static_cast(event.severity), + event.message + ); + } ); + + // Unsubscribe log to cout function from message logged events on release builds + #if defined(NDEBUG) + log_to_cout_subscription->unsubscribe(); + #endif + } + else + { + debug::log::error("Failed to write to log file \"{}\"", log_filepath_string); } - ); + } + else + { + debug::log::error("Failed to open log file \"{}\"", log_filepath_string); + } } // Log application name and version string diff --git a/src/math/angles.hpp b/src/math/angles.hpp index d304172..83c9ccc 100644 --- a/src/math/angles.hpp +++ b/src/math/angles.hpp @@ -20,19 +20,23 @@ #ifndef ANTKEEPER_MATH_ANGLES_HPP #define ANTKEEPER_MATH_ANGLES_HPP -#include "math/constants.hpp" +#include "math/numbers.hpp" #include namespace math { /** - * Converts an angle given in radians to degrees. + * Converts an angle from radians to degrees. * * @param radians Angle in radians. + * * @return Angle in degrees. */ template -T degrees(T radians); +[[nodiscard]] inline constexpr T degrees(T radians) noexcept +{ + return radians * rad2deg; +} /** * Converts an angle given in degrees to radians. @@ -41,7 +45,10 @@ T degrees(T radians); * @return Angle in degrees. */ template -T radians(T degrees); +[[nodiscard]] inline constexpr T radians(T degrees) noexcept +{ + return degrees * deg2rad; +} /** * Wraps an angle to [-180, 180]. @@ -50,7 +57,10 @@ T radians(T degrees); * @return Wrapped angle. */ template -T wrap_degrees(T degrees); +[[nodiscard]] inline constexpr T wrap_degrees(T degrees) +{ + return std::remainder(degrees, T(360)); +} /** * Wraps an angle to [-pi, pi]. @@ -59,28 +69,7 @@ T wrap_degrees(T degrees); * @return Wrapped angle. */ template -T wrap_radians(T radians); - -template -inline T degrees(T radians) -{ - return radians * T(180) / pi; -} - -template -inline T radians(T degrees) -{ - return degrees * pi / T(180); -} - -template -inline T wrap_degrees(T degrees) -{ - return std::remainder(degrees, T(360)); -} - -template -inline T wrap_radians(T radians) +[[nodiscard]] inline constexpr T wrap_radians(T radians) { return std::remainder(radians, two_pi); } diff --git a/src/math/compile.hpp b/src/math/compile.hpp index 8723016..ee84474 100644 --- a/src/math/compile.hpp +++ b/src/math/compile.hpp @@ -27,8 +27,6 @@ namespace math { /// Compile-time mathematical functions. namespace compile { - - /** * Compile-time `ceil(log2(x))` for unsigned integrals. * @@ -37,7 +35,7 @@ namespace compile { * @return `ceil(log2(x))`. */ template -consteval T ceil_log2(T x) noexcept +[[nodiscard]] consteval T ceil_log2(T x) noexcept { return (x <= T(1)) ? T(0) : ceil_log2((x + T(1)) / T(2)) + T(1); } @@ -50,7 +48,7 @@ consteval T ceil_log2(T x) noexcept * @return `exp2(x)`. */ template -consteval T exp2(T x) noexcept +[[nodiscard]] consteval T exp2(T x) noexcept { return (x) ? T(2) << (x - 1) : T(1); } @@ -64,7 +62,7 @@ consteval T exp2(T x) noexcept * @return `x^e`. */ template -consteval T pow(T x, T e) noexcept +[[nodiscard]] consteval T pow(T x, T e) noexcept { return (e == 0) ? T(1) : (x * pow(x, e - 1)); } diff --git a/src/math/constants.hpp b/src/math/constants.hpp deleted file mode 100644 index a6926e6..0000000 --- a/src/math/constants.hpp +++ /dev/null @@ -1,69 +0,0 @@ -/* - * Copyright (C) 2023 Christopher J. Howard - * - * This file is part of Antkeeper source code. - * - * Antkeeper source code is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * Antkeeper source code is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with Antkeeper source code. If not, see . - */ - -#ifndef ANTKEEPER_MATH_CONSTANTS_HPP -#define ANTKEEPER_MATH_CONSTANTS_HPP - -#include - -namespace math { - -/// Positive infinity. -template -constexpr T inf{std::numeric_limits::infinity()}; - -/// Pi. -template -constexpr T pi = T{3.1415926535897932384626433832795}; - -/// Pi * 2. -template -constexpr T two_pi = pi * T{2}; - -/// Pi * 4. -template -constexpr T four_pi = pi * T{4}; - -/// Pi / 2. -template -constexpr T half_pi = pi / T{2}; - -/// 1 / Pi. -template -constexpr T inverse_pi = T{1} / pi; - -/// sqrt(0.5) -template -constexpr T sqrt_half = T{0.70710678118654752440084436210485}; - -/// sqrt(2) -template -constexpr T sqrt_2 = T{1.4142135623730950488016887242097}; - -/// sqrt(3) -template -constexpr T sqrt_3 = T{1.7320508075688772935274463415059}; - -/// sqrt(5) -template -constexpr T sqrt_5 = T{2.2360679774997896964091736687313}; - -} // namespace math - -#endif // ANTKEEPER_MATH_CONSTANTS_HPP diff --git a/src/math/hash/pcg.hpp b/src/math/hash/pcg.hpp index 2e395db..71f841e 100644 --- a/src/math/hash/pcg.hpp +++ b/src/math/hash/pcg.hpp @@ -66,7 +66,7 @@ constexpr std::uint64_t mcg_multiplier = 12605985483714917081ULL; /// @private template -constexpr T pcg_uint(T x) noexcept +[[nodiscard]] constexpr T pcg_uint(T x) noexcept { static_assert(std::is_integral::value && std::is_unsigned::value); static_assert(sizeof(T) <= 8); @@ -78,7 +78,7 @@ constexpr T pcg_uint(T x) noexcept /// @private template -inline constexpr vector pcg_uvec1(vector x) noexcept +[[nodiscard]] inline constexpr vector pcg_uvec1(vector x) noexcept { static_assert(std::is_integral::value && std::is_unsigned::value); static_assert(sizeof(T) <= 8); @@ -90,7 +90,7 @@ inline constexpr vector pcg_uvec1(vector x) noexcept /// @private template -constexpr vector pcg_uvec2(vector x) noexcept +[[nodiscard]] constexpr vector pcg_uvec2(vector x) noexcept { static_assert(std::is_integral::value && std::is_unsigned::value); static_assert(sizeof(T) <= 8); @@ -114,7 +114,7 @@ constexpr vector pcg_uvec2(vector x) noexcept /// @private template -constexpr vector pcg_uvec3(vector x) noexcept +[[nodiscard]] constexpr vector pcg_uvec3(vector x) noexcept { static_assert(std::is_integral::value && std::is_unsigned::value); static_assert(sizeof(T) <= 8); @@ -138,7 +138,7 @@ constexpr vector pcg_uvec3(vector x) noexcept /// @private template -constexpr vector pcg_uvec4(vector x) noexcept +[[nodiscard]] constexpr vector pcg_uvec4(vector x) noexcept { static_assert(std::is_integral::value && std::is_unsigned::value); static_assert(sizeof(T) <= 8); @@ -178,58 +178,58 @@ constexpr vector pcg_uvec4(vector x) noexcept * @see Mark Jarzynski and Marc Olano, Hash Functions for GPU Rendering, Journal of Computer Graphics Techniques (JCGT), vol. 9, no. 3, 21-38, 2020. */ /// @{ -inline constexpr std::uint8_t pcg(std::uint8_t x) noexcept +[[nodiscard]] inline constexpr std::uint8_t pcg(std::uint8_t x) noexcept { return pcg_uint(x); } -inline constexpr std::uint16_t pcg(std::uint16_t x) noexcept +[[nodiscard]] inline constexpr std::uint16_t pcg(std::uint16_t x) noexcept { return pcg_uint(x); } -inline constexpr std::uint32_t pcg(std::uint32_t x) noexcept +[[nodiscard]] inline constexpr std::uint32_t pcg(std::uint32_t x) noexcept { return pcg_uint(x); } -inline constexpr std::uint64_t pcg(std::uint64_t x) noexcept +[[nodiscard]] inline constexpr std::uint64_t pcg(std::uint64_t x) noexcept { return pcg_uint(x); } -inline constexpr std::uint8_t pcg(std::int8_t x) noexcept +[[nodiscard]] inline constexpr std::uint8_t pcg(std::int8_t x) noexcept { return pcg_uint(static_cast(x)); } -inline constexpr std::uint16_t pcg(std::int16_t x) noexcept +[[nodiscard]] inline constexpr std::uint16_t pcg(std::int16_t x) noexcept { return pcg_uint(static_cast(x)); } -inline constexpr std::uint32_t pcg(std::int32_t x) noexcept +[[nodiscard]] inline constexpr std::uint32_t pcg(std::int32_t x) noexcept { return pcg_uint(static_cast(x)); } -inline constexpr std::uint64_t pcg(std::int64_t x) noexcept +[[nodiscard]] inline constexpr std::uint64_t pcg(std::int64_t x) noexcept { return pcg_uint(static_cast(x)); } -inline constexpr std::uint32_t pcg(float x) noexcept +[[nodiscard]] inline constexpr std::uint32_t pcg(float x) noexcept { return pcg_uint(static_cast(x)); } -inline constexpr std::uint64_t pcg(double x) noexcept +[[nodiscard]] inline constexpr std::uint64_t pcg(double x) noexcept { return pcg_uint(static_cast(x)); } template -inline constexpr vector, N> pcg(const vector& x) noexcept +[[nodiscard]] inline constexpr vector, N> pcg(const vector& x) noexcept { static_assert(N > 0 && N < 5, "PCG hash only supports vectors with 1-4 elements."); diff --git a/src/math/interpolation.hpp b/src/math/interpolation.hpp index e475c65..d7846fe 100644 --- a/src/math/interpolation.hpp +++ b/src/math/interpolation.hpp @@ -38,7 +38,10 @@ namespace math { * @tparam S Scalar type. */ template -T lerp(const T& x, const T& y, S a); +[[nodiscard]] constexpr T lerp(const T& x, const T& y, S a) +{ + return x + (y - x) * a; +} /** * Linearly interpolates between two angles, @p x and @p y. @@ -50,7 +53,10 @@ T lerp(const T& x, const T& y, S a); * @return Interpolated angle, in radians. */ template -T lerp_angle(T x, T y, T a); +[[nodiscard]] constexpr T lerp_angle(T x, T y, T a) +{ + return wrap_radians(x + wrap_radians(y - x) * a); +} /** * Logarithmically interpolates between @p x and @p y. @@ -61,22 +67,7 @@ T lerp_angle(T x, T y, T a); * @tparam S Scalar type. */ template -T log_lerp(const T& x, const T& y, S a); - -template -inline T lerp(const T& x, const T& y, S a) -{ - return x + (y - x) * a; -} - -template -T lerp_angle(T x, T y, T a) -{ - return wrap_radians(x + wrap_radians(y - x) * a); -} - -template -inline T log_lerp(const T& x, const T& y, S a) +[[nodiscard]] T log_lerp(const T& x, const T& y, S a) { //return std::exp(linear(std::log(x), std::log(y), a)); return x * std::pow(y / x, a); diff --git a/src/math/literals.hpp b/src/math/literals.hpp new file mode 100644 index 0000000..7e0b393 --- /dev/null +++ b/src/math/literals.hpp @@ -0,0 +1,32 @@ +/* + * Copyright (C) 2023 Christopher J. Howard + * + * This file is part of Antkeeper source code. + * + * Antkeeper source code is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * Antkeeper source code is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with Antkeeper source code. If not, see . + */ + +#ifndef ANTKEEPER_MATH_LITERALS_HPP +#define ANTKEEPER_MATH_LITERALS_HPP + +namespace math { + +/** + * User-defined math literals. + */ +namespace literals {} + +} // namespace math + +#endif // ANTKEEPER_MATH_LITERALS_HPP diff --git a/src/math/map.hpp b/src/math/map.hpp index 6a54782..deb14ab 100644 --- a/src/math/map.hpp +++ b/src/math/map.hpp @@ -34,7 +34,7 @@ namespace math { * @return Unclamped remapped value. */ template -constexpr T map(T x, T from_min, T from_max, T to_min, T to_max) noexcept +[[nodiscard]] constexpr T map(T x, T from_min, T from_max, T to_min, T to_max) noexcept { return to_min + (x - from_min) * (to_max - to_min) / (from_max - from_min); } diff --git a/src/math/math.hpp b/src/math/math.hpp index f10c76b..29bdd93 100644 --- a/src/math/math.hpp +++ b/src/math/math.hpp @@ -33,7 +33,7 @@ namespace math {} #include "math/transform-operators.hpp" #include "math/angles.hpp" -#include "math/constants.hpp" +#include "math/numbers.hpp" #include "math/quadrature.hpp" #include "math/interpolation.hpp" #include "math/map.hpp" diff --git a/src/math/matrix.hpp b/src/math/matrix.hpp index 43220eb..b5e282f 100644 --- a/src/math/matrix.hpp +++ b/src/math/matrix.hpp @@ -68,7 +68,7 @@ struct matrix /// @private template - inline constexpr matrix type_cast(std::index_sequence) const noexcept + [[nodiscard]] inline constexpr matrix type_cast(std::index_sequence) const noexcept { return {vector(columns[I])...}; } @@ -81,14 +81,14 @@ struct matrix * @return Matrix containing the type-casted elements. */ template - inline constexpr explicit operator matrix() const noexcept + [[nodiscard]] inline constexpr explicit operator matrix() const noexcept { return type_cast(std::make_index_sequence{}); } /// @private template - inline constexpr matrix size_cast(std::index_sequence) const noexcept + [[nodiscard]] inline constexpr matrix size_cast(std::index_sequence) const noexcept { if constexpr (O == M) return {((I < N) ? columns[I] : matrix::identity()[I]) ...}; @@ -105,7 +105,7 @@ struct matrix * @return *p* by *o* matrix. */ template - inline constexpr explicit operator matrix() const noexcept + [[nodiscard]] inline constexpr explicit operator matrix() const noexcept { return size_cast(std::make_index_sequence

{}); } @@ -123,19 +123,19 @@ struct matrix * @return Reference to the column vector at index @p i. */ /// @{ - inline constexpr column_vector_type& operator[](std::size_t i) noexcept + [[nodiscard]] inline constexpr column_vector_type& operator[](std::size_t i) noexcept { return columns[i]; } - inline constexpr const column_vector_type& operator[](std::size_t i) const noexcept + [[nodiscard]] inline constexpr const column_vector_type& operator[](std::size_t i) const noexcept { return columns[i]; } - inline constexpr column_vector_type& column(std::size_t i) noexcept + [[nodiscard]] inline constexpr column_vector_type& column(std::size_t i) noexcept { return columns[i]; } - inline constexpr const column_vector_type& column(std::size_t i) const noexcept + [[nodiscard]] inline constexpr const column_vector_type& column(std::size_t i) const noexcept { return columns[i]; } @@ -145,11 +145,11 @@ struct matrix * Returns a reference to the first column vector. */ /// @{ - inline constexpr column_vector_type& front() noexcept + [[nodiscard]] inline constexpr column_vector_type& front() noexcept { return columns[0]; } - inline constexpr const column_vector_type& front() const noexcept + [[nodiscard]] inline constexpr const column_vector_type& front() const noexcept { return columns[0]; } @@ -159,11 +159,11 @@ struct matrix * Returns a reference to the last column vector. */ /// @{ - inline constexpr column_vector_type& back() noexcept + [[nodiscard]] inline constexpr column_vector_type& back() noexcept { return columns[column_count - 1]; } - inline constexpr const column_vector_type& back() const noexcept + [[nodiscard]] inline constexpr const column_vector_type& back() const noexcept { return columns[column_count - 1]; } @@ -182,11 +182,11 @@ struct matrix * @return Reference to the element at column-major index @p i. */ /// @{ - inline constexpr T& element(std::size_t i) noexcept + [[nodiscard]] inline constexpr T& element(std::size_t i) noexcept { return columns[i / row_count][i % row_count]; } - inline constexpr const T& element(std::size_t i) const noexcept + [[nodiscard]] inline constexpr const T& element(std::size_t i) const noexcept { return columns[i / row_count][i % row_count]; } @@ -198,11 +198,11 @@ struct matrix * @warning If matrix::element_type is not a POD type, elements may not be stored contiguously. */ /// @{ - inline constexpr element_type* data() noexcept + [[nodiscard]] inline constexpr element_type* data() noexcept { return &columns[0][0]; }; - inline constexpr const element_type* data() const noexcept + [[nodiscard]] inline constexpr const element_type* data() const noexcept { return &columns[0][0]; }; @@ -217,15 +217,15 @@ struct matrix * Returns an iterator to the first column vector. */ /// @{ - inline constexpr column_vector_type* begin() noexcept + [[nodiscard]] inline constexpr column_vector_type* begin() noexcept { return columns; } - inline constexpr const column_vector_type* begin() const noexcept + [[nodiscard]] inline constexpr const column_vector_type* begin() const noexcept { return columns; } - inline constexpr const column_vector_type* cbegin() const noexcept + [[nodiscard]] inline constexpr const column_vector_type* cbegin() const noexcept { return columns; } @@ -235,15 +235,15 @@ struct matrix * Returns an iterator to the column vector following the last column vector. */ /// @{ - inline constexpr column_vector_type* end() noexcept + [[nodiscard]] inline constexpr column_vector_type* end() noexcept { return columns + column_count; } - inline constexpr const column_vector_type* end() const noexcept + [[nodiscard]] inline constexpr const column_vector_type* end() const noexcept { return columns + column_count; } - inline constexpr const column_vector_type* cend() const noexcept + [[nodiscard]] inline constexpr const column_vector_type* cend() const noexcept { return columns + column_count; } @@ -253,15 +253,15 @@ struct matrix * Returns a reverse iterator to the first column vector of the reversed matrix. */ /// @{ - inline constexpr std::reverse_iterator rbegin() noexcept + [[nodiscard]] inline constexpr std::reverse_iterator rbegin() noexcept { return std::reverse_iterator(columns + column_count); } - inline constexpr std::reverse_iterator rbegin() const noexcept + [[nodiscard]] inline constexpr std::reverse_iterator rbegin() const noexcept { return std::reverse_iterator(columns + column_count); } - inline constexpr std::reverse_iterator crbegin() const noexcept + [[nodiscard]] inline constexpr std::reverse_iterator crbegin() const noexcept { return std::reverse_iterator(columns + column_count); } @@ -271,15 +271,15 @@ struct matrix * Returns a reverse iterator to the column vector following the last column vector of the reversed matrix. */ /// @{ - inline constexpr std::reverse_iterator rend() noexcept + [[nodiscard]] inline constexpr std::reverse_iterator rend() noexcept { return std::reverse_iterator(columns); } - inline constexpr std::reverse_iterator rend() const noexcept + [[nodiscard]] inline constexpr std::reverse_iterator rend() const noexcept { return std::reverse_iterator(columns); } - inline constexpr std::reverse_iterator crend() const noexcept + [[nodiscard]] inline constexpr std::reverse_iterator crend() const noexcept { return std::reverse_iterator(columns); } @@ -293,7 +293,7 @@ struct matrix /** * Returns the number of elements in the matrix. */ - inline constexpr std::size_t size() const noexcept + [[nodiscard]] inline constexpr std::size_t size() const noexcept { return element_count; }; @@ -306,14 +306,14 @@ struct matrix /** * Returns a zero matrix, where every element is equal to zero. */ - static constexpr matrix zero() noexcept + [[nodiscard]] static constexpr matrix zero() noexcept { return {}; } /// @private template - static inline constexpr matrix one(std::index_sequence) noexcept + [[nodiscard]] static inline constexpr matrix one(std::index_sequence) noexcept { //return {column_vector_type::one() ...}; @@ -324,21 +324,21 @@ struct matrix /** * Returns a matrix of ones, where every element is equal to one. */ - static constexpr matrix one() noexcept + [[nodiscard]] static constexpr matrix one() noexcept { return one(std::make_index_sequence{}); } /// @private template - static inline constexpr column_vector_type identity_column(std::size_t i, std::index_sequence) noexcept + [[nodiscard]] static inline constexpr column_vector_type identity_column(std::size_t i, std::index_sequence) noexcept { return {(I == i ? T{1} : T{0}) ...}; } /// @private template - static inline constexpr matrix identity(std::index_sequence) noexcept + [[nodiscard]] static inline constexpr matrix identity(std::index_sequence) noexcept { return {identity_column(I, std::make_index_sequence{}) ...}; } @@ -346,7 +346,7 @@ struct matrix /** * Returns an identity matrix, with ones on the main diagonal and zeros elsewhere. */ - static constexpr matrix identity() noexcept + [[nodiscard]] static constexpr matrix identity() noexcept { return identity(std::make_index_sequence{}); } @@ -387,7 +387,7 @@ using matrix4x4 = matrix; * @return Sum of the two matrices. */ template -constexpr matrix add(const matrix& a, const matrix& b) noexcept; +[[nodiscard]] constexpr matrix add(const matrix& a, const matrix& b) noexcept; /** * Adds a matrix and a scalar. @@ -398,7 +398,7 @@ constexpr matrix add(const matrix& a, const matrix& b * @return Sum of the matrix and scalar. */ template -constexpr matrix add(const matrix& a, T b) noexcept; +[[nodiscard]] constexpr matrix add(const matrix& a, T b) noexcept; /** * Calculates the determinant of a square matrix. @@ -410,7 +410,7 @@ constexpr matrix add(const matrix& a, T b) noexcept; * @warning Currently only implemented for 2x2, 3x3, and 4x4 matrices. */ template -constexpr T determinant(const matrix& m) noexcept; +[[nodiscard]] constexpr T determinant(const matrix& m) noexcept; /** * Performs a component-wise multiplication of two matrices. @@ -421,7 +421,7 @@ constexpr T determinant(const matrix& m) noexcept; * @return Product of the component-wise multiplcation. */ template -constexpr matrix componentwise_mul(const matrix& a, const matrix& b) noexcept; +[[nodiscard]] constexpr matrix componentwise_mul(const matrix& a, const matrix& b) noexcept; /** * Divides a matrix by a matrix. @@ -432,7 +432,7 @@ constexpr matrix componentwise_mul(const matrix& a, const matr * @return Result of the division. */ template -constexpr matrix div(const matrix& a, const matrix& b) noexcept; +[[nodiscard]] constexpr matrix div(const matrix& a, const matrix& b) noexcept; /** * Divides a matrix by a scalar. @@ -443,7 +443,7 @@ constexpr matrix div(const matrix& a, const matrix& b * @return Result of the division. */ template -constexpr matrix div(const matrix& a, T b) noexcept; +[[nodiscard]] constexpr matrix div(const matrix& a, T b) noexcept; /** * Divides a scalar by a matrix. @@ -454,7 +454,7 @@ constexpr matrix div(const matrix& a, T b) noexcept; * @return Result of the division. */ template -constexpr matrix div(T a, const matrix& b) noexcept; +[[nodiscard]] constexpr matrix div(T a, const matrix& b) noexcept; /** * Extracts the Ith column from a matrix. @@ -470,13 +470,13 @@ constexpr matrix div(T a, const matrix& b) noexcept; */ /// @{ template -constexpr typename matrix::column_vector_type& get(math::matrix& m) noexcept; +[[nodiscard]] constexpr typename matrix::column_vector_type& get(math::matrix& m) noexcept; template -constexpr typename matrix::column_vector_type&& get(math::matrix&& m) noexcept; +[[nodiscard]] constexpr typename matrix::column_vector_type&& get(math::matrix&& m) noexcept; template -constexpr const typename matrix::column_vector_type& get(const math::matrix& m) noexcept; +[[nodiscard]] constexpr const typename matrix::column_vector_type& get(const math::matrix& m) noexcept; template -constexpr const typename matrix::column_vector_type&& get(const math::matrix&& m) noexcept; +[[nodiscard]] constexpr const typename matrix::column_vector_type&& get(const math::matrix&& m) noexcept; /// @} /** @@ -489,7 +489,7 @@ constexpr const typename matrix::column_vector_type&& get(const math::m * @warning Currently only implemented for 2x2, 3x3, and 4x4 matrices. */ template -constexpr matrix inverse(const matrix& m) noexcept; +[[nodiscard]] constexpr matrix inverse(const matrix& m) noexcept; /** * Creates a viewing transformation matrix. @@ -501,7 +501,7 @@ constexpr matrix inverse(const matrix& m) noexcept; * @return Viewing transformation matrix. */ template -constexpr matrix look_at(const vector& position, const vector& target, vector up); +[[nodiscard]] constexpr matrix look_at(const vector& position, const vector& target, vector up); /** * Multiplies two matrices @@ -517,7 +517,7 @@ constexpr matrix look_at(const vector& position, const vector -constexpr matrix mul(const matrix& a, const matrix& b) noexcept; +[[nodiscard]] constexpr matrix mul(const matrix& a, const matrix& b) noexcept; /** * Multiplies a matrix by a scalar. @@ -528,7 +528,7 @@ constexpr matrix mul(const matrix& a, const matrix& b * @return Product of the matrix and the scalar. */ template -constexpr matrix mul(const matrix& a, T b) noexcept; +[[nodiscard]] constexpr matrix mul(const matrix& a, T b) noexcept; /** * Calculates the product of a matrix and a row vector. @@ -539,7 +539,7 @@ constexpr matrix mul(const matrix& a, T b) noexcept; * @return Product of the matrix and the row vector. */ template -constexpr typename matrix::column_vector_type mul(const matrix& a, const typename matrix::row_vector_type& b) noexcept; +[[nodiscard]] constexpr typename matrix::column_vector_type mul(const matrix& a, const typename matrix::row_vector_type& b) noexcept; /** * Calculates the product of a column vector and a matrix. @@ -550,7 +550,7 @@ constexpr typename matrix::column_vector_type mul(const matrix * @return Product of the column vector and the matrix. */ template -constexpr typename matrix::row_vector_type mul(const typename matrix::column_vector_type& a, const matrix& b) noexcept; +[[nodiscard]] constexpr typename matrix::row_vector_type mul(const typename matrix::column_vector_type& a, const matrix& b) noexcept; /** * Constructs a rotation matrix. @@ -561,7 +561,7 @@ constexpr typename matrix::row_vector_type mul(const typename matrix -matrix rotate(T angle, const vector& axis); +[[nodiscard]] matrix rotate(T angle, const vector& axis); /** * Produces a matrix which rotates Cartesian coordinates about the x-axis by a given angle. @@ -571,7 +571,7 @@ matrix rotate(T angle, const vector& axis); * @return Rotation matrix. */ template -matrix3 rotate_x(T angle); +[[nodiscard]] matrix3 rotate_x(T angle); /** * Produces a matrix which rotates Cartesian coordinates about the y-axis by a given angle. @@ -581,7 +581,7 @@ matrix3 rotate_x(T angle); * @return Rotation matrix. */ template -matrix3 rotate_y(T angle); +[[nodiscard]] matrix3 rotate_y(T angle); /** * Produces a matrix which rotates Cartesian coordinates about the z-axis by a given angle. @@ -591,7 +591,7 @@ matrix3 rotate_y(T angle); * @return Rotation matrix. */ template -matrix3 rotate_z(T angle); +[[nodiscard]] matrix3 rotate_z(T angle); /** * Scales a matrix. @@ -602,7 +602,7 @@ matrix3 rotate_z(T angle); * @return Scaled matrix. */ template -constexpr matrix scale(const matrix& m, const vector& v); +[[nodiscard]] constexpr matrix scale(const matrix& m, const vector& v); /** * Subtracts a matrix from another matrix. @@ -613,7 +613,7 @@ constexpr matrix scale(const matrix& m, const vector& v) * @return Difference between the two matrices. */ template -constexpr matrix sub(const matrix& a, const matrix& b) noexcept; +[[nodiscard]] constexpr matrix sub(const matrix& a, const matrix& b) noexcept; /** * Subtracts a scalar from matrix. @@ -624,7 +624,7 @@ constexpr matrix sub(const matrix& a, const matrix& b * @return Difference between the matrix and scalar. */ template -constexpr matrix sub(const matrix& a, T b) noexcept; +[[nodiscard]] constexpr matrix sub(const matrix& a, T b) noexcept; /** * Subtracts a matrix from a scalar. @@ -635,7 +635,7 @@ constexpr matrix sub(const matrix& a, T b) noexcept; * @return Difference between the scalar and matrix. */ template -constexpr matrix sub(T a, const matrix& b) noexcept; +[[nodiscard]] constexpr matrix sub(T a, const matrix& b) noexcept; /** * Calculates the trace of a square matrix. @@ -645,7 +645,7 @@ constexpr matrix sub(T a, const matrix& b) noexcept; * @return Sum of elements on the main diagonal. */ template -constexpr T trace(const matrix& m) noexcept; +[[nodiscard]] constexpr T trace(const matrix& m) noexcept; /** * Translates a matrix. @@ -656,7 +656,7 @@ constexpr T trace(const matrix& m) noexcept; * @return Translated matrix. */ template -constexpr matrix translate(const matrix& m, const vector& v); +[[nodiscard]] constexpr matrix translate(const matrix& m, const vector& v); /** * Calculates the transpose of a matrix. @@ -666,7 +666,7 @@ constexpr matrix translate(const matrix& m, const vector * @return Transposed matrix. */ template -constexpr matrix transpose(const matrix& m) noexcept; +[[nodiscard]] constexpr matrix transpose(const matrix& m) noexcept; /// @private template diff --git a/src/math/noise/fbm.hpp b/src/math/noise/fbm.hpp index ab93bae..de1d889 100644 --- a/src/math/noise/fbm.hpp +++ b/src/math/noise/fbm.hpp @@ -44,7 +44,7 @@ namespace noise { * */ template -T fbm +[[nodiscard]] T fbm ( vector position, std::size_t octaves, diff --git a/src/math/noise/simplex.hpp b/src/math/noise/simplex.hpp index 65b3fca..e4f7bfb 100644 --- a/src/math/noise/simplex.hpp +++ b/src/math/noise/simplex.hpp @@ -51,7 +51,7 @@ constexpr std::size_t simplex_edge_count = (N > 1) ? N * simplex_corner_count -constexpr vector make_simplex_corner(std::size_t i, std::index_sequence) +[[nodiscard]] constexpr vector make_simplex_corner(std::size_t i, std::index_sequence) { return {((i >> I) % 2) * T{2} - T{1}...}; } @@ -62,7 +62,7 @@ constexpr vector make_simplex_corner(std::size_t i, std::index_sequence -constexpr std::array, simplex_corner_count> make_simplex_corners(std::index_sequence) +[[nodiscard]] constexpr std::array, simplex_corner_count> make_simplex_corners(std::index_sequence) { return {make_simplex_corner(I, std::make_index_sequence{})...}; } @@ -81,7 +81,7 @@ constexpr auto simplex_corners = make_simplex_corners(std::make_index_sequ * @private */ template -constexpr vector make_simplex_edge(std::size_t i, std::index_sequence) +[[nodiscard]] constexpr vector make_simplex_edge(std::size_t i, std::index_sequence) { std::size_t j = i / (simplex_edge_count / N); @@ -104,7 +104,7 @@ constexpr vector make_simplex_edge(std::size_t i, std::index_sequence -constexpr std::array, simplex_edge_count> make_simplex_edges(std::index_sequence) +[[nodiscard]] constexpr std::array, simplex_edge_count> make_simplex_edges(std::index_sequence) { if constexpr (N == 1) return std::array, simplex_edge_count>{vector{T{1}}, vector{T{-1}}}; @@ -138,7 +138,7 @@ constexpr auto simplex_edges = make_simplex_edges(std::make_index_sequence * @see https://math.stackexchange.com/questions/474638/radius-and-amplitude-of-kernel-for-simplex-noise/1901116 */ template -T simplex +[[nodiscard]] T simplex ( const vector& position, vector, N> (*hash)(const vector&) = &hash::pcg diff --git a/src/math/noise/voronoi.hpp b/src/math/noise/voronoi.hpp index 71ed0b5..8103b77 100644 --- a/src/math/noise/voronoi.hpp +++ b/src/math/noise/voronoi.hpp @@ -60,7 +60,7 @@ constexpr std::size_t kernel_size = 4 << std::max(0, (2 * (N - 1))) * @private */ template -constexpr vector kernel_offset(std::size_t i, std::index_sequence) +[[nodiscard]] constexpr vector kernel_offset(std::size_t i, std::index_sequence) { return {static_cast((I ? (i / (2 << std::max(0, 2 * I - 1))) : i) % 4)...}; } @@ -77,7 +77,7 @@ constexpr vector kernel_offset(std::size_t i, std::index_sequence) * @private */ template -constexpr std::array, kernel_size> generate_kernel(std::index_sequence) +[[nodiscard]] constexpr std::array, kernel_size> generate_kernel(std::index_sequence) { return {kernel_offset(I, std::make_index_sequence{})...}; } @@ -107,7 +107,7 @@ constexpr auto kernel = generate_kernel(std::make_index_sequence -std::tuple +[[nodiscard]] std::tuple < // F1 square distance to center T, @@ -197,7 +197,7 @@ f1 * @return Tuple containing the square Euclidean distance from @p position to the F1 cell center, the displacement vector from the input position to the F1 cell center, a hash value indicating the ID of the F1 cell, and the square Euclidean distance from @p position to the nearest edge. */ template -std::tuple +[[nodiscard]] std::tuple < // F1 square distance to center T, @@ -320,7 +320,7 @@ f1_edge * @return Tuple containing the square Euclidean distances, displacement vectors from the input position to the cell centers, and hash values indicating the cell IDs, for both the F1 and F2 cells. */ template -std::tuple +[[nodiscard]] std::tuple < // F1 square distance to center T, diff --git a/src/math/numbers.hpp b/src/math/numbers.hpp new file mode 100644 index 0000000..2cfaa9e --- /dev/null +++ b/src/math/numbers.hpp @@ -0,0 +1,122 @@ +/* + * Copyright (C) 2023 Christopher J. Howard + * + * This file is part of Antkeeper source code. + * + * Antkeeper source code is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * Antkeeper source code is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with Antkeeper source code. If not, see . + */ + +#ifndef ANTKEEPER_MATH_NUMBERS_HPP +#define ANTKEEPER_MATH_NUMBERS_HPP + +#include +#include + +namespace math { + +/// Mathematical constants. +namespace numbers { + +/// Positive infinity. +template +inline constexpr T inf = std::numeric_limits::infinity(); + +/// e. +template +inline constexpr T e = std::numbers::e_v; + +/// log2(e). +template +inline constexpr T log2_e = std::numbers::log2e_v; + +/// log10(e). +template +inline constexpr T log10_e = std::numbers::log10e_v; + +/// Pi. +template +inline constexpr T pi = std::numbers::pi_v; + +/// Pi * 2. +template +inline constexpr T two_pi = pi * T{2}; + +/// Pi * 4. +template +inline constexpr T four_pi = pi * T{4}; + +/// Pi / 2. +template +inline constexpr T half_pi = pi / T{2}; + +/// 1 / Pi. +template +inline constexpr T inv_pi = std::numbers::inv_pi_v; + +/// 1 / sqrt(Pi). +template +inline constexpr T inv_sqrt_pi = std::numbers::inv_sqrtpi_v; + +/// ln(2). +template +inline constexpr T ln_2 = std::numbers::ln2_v; + +/// ln(10). +template +inline constexpr T ln_10 = std::numbers::ln10_v; + +/// sqrt(0.5) +template +inline constexpr T sqrt_half = T{0.70710678118654752440084436210485}; + +/// sqrt(2) +template +inline constexpr T sqrt_2 = std::numbers::sqrt2_v; + +/// sqrt(3) +template +inline constexpr T sqrt_3 = std::numbers::sqrt3_v; + +/// 1 / sqrt(3) +template +inline constexpr T inv_sqrt_3 = std::numbers::inv_sqrt3_v; + +/// sqrt(5) +template +inline constexpr T sqrt_5 = T{2.2360679774997896964091736687313}; + +/// Euler–Mascheroni constant. +template +inline constexpr T egamma = std::numbers::egamma_v; + +/// Golden ratio constant. +template +inline constexpr T phi = std::numbers::phi_v; + +/// Degrees-to-radians conversion factor. +template +inline constexpr T deg2rad = pi / T{180}; + +/// Radians-to-degrees conversion factor. +template +inline constexpr T rad2deg = T{180} / pi; + +} // namespace numbers + +// Bring math::numbers into math namespace +using namespace numbers; + +} // namespace math + +#endif // ANTKEEPER_MATH_NUMBERS_HPP diff --git a/src/math/polynomial.hpp b/src/math/polynomial.hpp index aa33fa0..182f703 100644 --- a/src/math/polynomial.hpp +++ b/src/math/polynomial.hpp @@ -20,7 +20,7 @@ #ifndef ANTKEEPER_MATH_POLYNOMIAL_HPP #define ANTKEEPER_MATH_POLYNOMIAL_HPP -#include "math/constants.hpp" +#include "math/numbers.hpp" #include "math/map.hpp" namespace math { @@ -38,7 +38,7 @@ namespace polynomial { * @see https://en.wikipedia.org/wiki/Horner%27s_method */ template -T horner(InputIt first, InputIt last, T x) +[[nodiscard]] constexpr T horner(InputIt first, InputIt last, T x) { T y = *first; for (++first; first != last; ++first) @@ -61,7 +61,7 @@ namespace chebyshev { * @return Evaluated value. */ template - T evaluate(InputIt first, InputIt last, T x) + [[nodiscard]] T evaluate(InputIt first, InputIt last, T x) { T y = *(first++); y += *(first++) * x; @@ -88,7 +88,7 @@ namespace chebyshev { * @return Evaluated value. */ template - T evaluate(InputIt first, InputIt last, T min, T max, T x) + [[nodiscard]] T evaluate(InputIt first, InputIt last, T min, T max, T x) { return evaluate(first, last, math::map(x, min, max, T(-1), T(1))); } diff --git a/src/math/projection.hpp b/src/math/projection.hpp index c136edb..b6d31e8 100644 --- a/src/math/projection.hpp +++ b/src/math/projection.hpp @@ -36,7 +36,7 @@ namespace math { * @see https://en.wikipedia.org/wiki/Field_of_view_in_video_games */ template -T horizontal_fov(T v, T r) +[[nodiscard]] T horizontal_fov(T v, T r) { return T{2} * std::atan(std::tan(v * T{0.5}) * r); } @@ -52,7 +52,7 @@ T horizontal_fov(T v, T r) * @see https://en.wikipedia.org/wiki/Field_of_view_in_video_games */ template -T vertical_fov(T h, T r) +[[nodiscard]] T vertical_fov(T h, T r) { return T{2} * std::atan(std::tan(h * T{0.5}) / r); } @@ -70,7 +70,7 @@ T vertical_fov(T h, T r) * @return Orthographic projection matrix. */ template -constexpr matrix ortho(T left, T right, T bottom, T top, T z_near, T z_far) noexcept +[[nodiscard]] constexpr matrix ortho(T left, T right, T bottom, T top, T z_near, T z_far) noexcept { return {{ @@ -94,7 +94,7 @@ constexpr matrix ortho(T left, T right, T bottom, T top, T z_near, T z_ * @return Orthographic projection matrix. */ template -constexpr matrix ortho_half_z(T left, T right, T bottom, T top, T z_near, T z_far) noexcept +[[nodiscard]] constexpr matrix ortho_half_z(T left, T right, T bottom, T top, T z_near, T z_far) noexcept { return {{ @@ -116,7 +116,7 @@ constexpr matrix ortho_half_z(T left, T right, T bottom, T top, T z_nea * @return Perspective projection matrix. */ template -matrix perspective(T vertical_fov, T aspect_ratio, T z_near, T z_far) +[[nodiscard]] matrix perspective(T vertical_fov, T aspect_ratio, T z_near, T z_far) { T half_fov = vertical_fov * T(0.5); T f = std::cos(half_fov) / std::sin(half_fov); @@ -141,7 +141,7 @@ matrix perspective(T vertical_fov, T aspect_ratio, T z_near, T z_far) * @return Perspective projection matrix. */ template -matrix perspective_half_z(T vertical_fov, T aspect_ratio, T z_near, T z_far) +[[nodiscard]] matrix perspective_half_z(T vertical_fov, T aspect_ratio, T z_near, T z_far) { T half_fov = vertical_fov * T(0.5); T f = std::cos(half_fov) / std::sin(half_fov); diff --git a/src/math/quadrature.hpp b/src/math/quadrature.hpp index 69abc98..34df05e 100644 --- a/src/math/quadrature.hpp +++ b/src/math/quadrature.hpp @@ -33,29 +33,13 @@ namespace quadrature { * * @param f Unary function object to integrate. * @param first,last Range of sample points on `[first, last)`. - * @return Approximated integral of @p f. - * - * @see https://en.wikipedia.org/wiki/Simpson%27s_rule - */ -template -typename std::invoke_result::value_type>::type - simpson(UnaryOp f, InputIt first, InputIt last); - -/** - * Approximates the definite integral of a function using the trapezoidal rule. * - * @param f Unary function object to integrate. - * @param first,last Range of sample points on `[first, last)`. * @return Approximated integral of @p f. * - * @see https://en.wikipedia.org/wiki/Trapezoidal_rule + * @see https://en.wikipedia.org/wiki/Simpson%27s_rule */ template -typename std::invoke_result::value_type>::type - trapezoid(UnaryOp f, InputIt first, InputIt last); - -template -typename std::invoke_result::value_type>::type +[[nodiscard]] typename std::invoke_result::value_type>::type simpson(UnaryOp f, InputIt first, InputIt last) { typedef typename std::iterator_traits::value_type input_type; @@ -93,8 +77,18 @@ typename std::invoke_result::val return sum / difference_type(6); } +/** + * Approximates the definite integral of a function using the trapezoidal rule. + * + * @param f Unary function object to integrate. + * @param first,last Range of sample points on `[first, last)`. + * + * @return Approximated integral of @p f. + * + * @see https://en.wikipedia.org/wiki/Trapezoidal_rule + */ template -typename std::invoke_result::value_type>::type +[[nodiscard]] typename std::invoke_result::value_type>::type trapezoid(UnaryOp f, InputIt first, InputIt last) { typedef typename std::iterator_traits::value_type input_type; diff --git a/src/math/quaternion.hpp b/src/math/quaternion.hpp index e4108f4..a0f4b93 100644 --- a/src/math/quaternion.hpp +++ b/src/math/quaternion.hpp @@ -20,7 +20,7 @@ #ifndef ANTKEEPER_MATH_QUATERNION_HPP #define ANTKEEPER_MATH_QUATERNION_HPP -#include "math/constants.hpp" +#include "math/numbers.hpp" #include "math/matrix.hpp" #include "math/vector.hpp" #include @@ -54,11 +54,11 @@ struct quaternion /// Returns a reference to the quaternion real part. /// @{ - inline constexpr scalar_type& w() noexcept + [[nodiscard]] inline constexpr scalar_type& w() noexcept { return r; } - inline constexpr const scalar_type& w() const noexcept + [[nodiscard]] inline constexpr const scalar_type& w() const noexcept { return r; } @@ -66,11 +66,11 @@ struct quaternion /// Returns a reference to the first element of the quaternion imaginary part. /// @{ - inline constexpr scalar_type& x() noexcept + [[nodiscard]] inline constexpr scalar_type& x() noexcept { return i.x(); } - inline constexpr const scalar_type& x() const noexcept + [[nodiscard]] inline constexpr const scalar_type& x() const noexcept { return i.x(); } @@ -78,11 +78,11 @@ struct quaternion /// Returns a reference to the second element of the quaternion imaginary part. /// @{ - inline constexpr scalar_type& y() noexcept + [[nodiscard]] inline constexpr scalar_type& y() noexcept { return i.y(); } - inline constexpr const scalar_type& y() const noexcept + [[nodiscard]] inline constexpr const scalar_type& y() const noexcept { return i.y(); } @@ -90,11 +90,11 @@ struct quaternion /// Returns a reference to the third element of the quaternion imaginary part. /// @{ - inline constexpr scalar_type& z() noexcept + [[nodiscard]] inline constexpr scalar_type& z() noexcept { return i.z(); } - inline constexpr const scalar_type& z() const noexcept + [[nodiscard]] inline constexpr const scalar_type& z() const noexcept { return i.z(); } @@ -107,7 +107,7 @@ struct quaternion * * @return Quaternion representing an x-axis rotation. */ - static quaternion rotate_x(scalar_type angle) + [[nodiscard]] static quaternion rotate_x(scalar_type angle) { return {std::cos(angle * T(0.5)), std::sin(angle * T(0.5)), T(0), T(0)}; } @@ -119,7 +119,7 @@ struct quaternion * * @return Quaternion representing an y-axis rotation. */ - static quaternion rotate_y(scalar_type angle) + [[nodiscard]] static quaternion rotate_y(scalar_type angle) { return {std::cos(angle * T(0.5)), T(0), std::sin(angle * T(0.5)), T(0)}; } @@ -130,7 +130,7 @@ struct quaternion * @param angle Angle of rotation, in radians. * @return Quaternion representing an z-axis rotation. */ - static quaternion rotate_z(scalar_type angle) + [[nodiscard]] static quaternion rotate_z(scalar_type angle) { return {std::cos(angle * T(0.5)), T(0), T(0), std::sin(angle * T(0.5))}; } @@ -143,7 +143,7 @@ struct quaternion * @return Type-casted quaternion. */ template - inline constexpr explicit operator quaternion() const noexcept + [[nodiscard]] inline constexpr explicit operator quaternion() const noexcept { return {static_cast(r), vector(i)}; } @@ -153,7 +153,7 @@ struct quaternion * * @return Rotation matrix. */ - constexpr explicit operator matrix_type() const noexcept + [[nodiscard]] constexpr explicit operator matrix_type() const noexcept { const T xx = x() * x(); const T xy = x() * y(); @@ -178,19 +178,19 @@ struct quaternion * * @return Vector containing the real and imaginary parts of the quaternion. */ - inline constexpr explicit operator vector() const noexcept + [[nodiscard]] inline constexpr explicit operator vector() const noexcept { return {r, i[0], i[1], i[2]}; } /// Returns a zero quaternion, where every scalar is equal to zero. - static constexpr quaternion zero() noexcept + [[nodiscard]] static constexpr quaternion zero() noexcept { return {}; } /// Returns a rotation identity quaternion. - static constexpr quaternion identity() noexcept + [[nodiscard]] static constexpr quaternion identity() noexcept { return {T{1}, vector_type::zero()}; } @@ -205,7 +205,7 @@ struct quaternion * @return Sum of the two quaternions. */ template -constexpr quaternion add(const quaternion& a, const quaternion& b) noexcept; +[[nodiscard]] constexpr quaternion add(const quaternion& a, const quaternion& b) noexcept; /** * Adds a quaternion and a scalar. @@ -216,7 +216,7 @@ constexpr quaternion add(const quaternion& a, const quaternion& b) noex * @return Sum of the quaternion and scalar. */ template -constexpr quaternion add(const quaternion& a, T b) noexcept; +[[nodiscard]] constexpr quaternion add(const quaternion& a, T b) noexcept; /** * Calculates the conjugate of a quaternion. @@ -226,7 +226,7 @@ constexpr quaternion add(const quaternion& a, T b) noexcept; * @return Conjugate of the quaternion. */ template -constexpr quaternion conjugate(const quaternion& q) noexcept; +[[nodiscard]] constexpr quaternion conjugate(const quaternion& q) noexcept; /** * Calculates the dot product of two quaternions. @@ -237,7 +237,7 @@ constexpr quaternion conjugate(const quaternion& q) noexcept; * @return Dot product of the two quaternions. */ template -constexpr T dot(const quaternion& a, const quaternion& b) noexcept; +[[nodiscard]] constexpr T dot(const quaternion& a, const quaternion& b) noexcept; /** * Divides a quaternion by another quaternion. @@ -248,7 +248,7 @@ constexpr T dot(const quaternion& a, const quaternion& b) noexcept; * @return Result of the division. */ template -constexpr quaternion div(const quaternion& a, const quaternion& b) noexcept; +[[nodiscard]] constexpr quaternion div(const quaternion& a, const quaternion& b) noexcept; /** * Divides a quaternion by a scalar. @@ -259,7 +259,7 @@ constexpr quaternion div(const quaternion& a, const quaternion& b) noex * @return Result of the division. */ template -constexpr quaternion div(const quaternion& a, T b) noexcept; +[[nodiscard]] constexpr quaternion div(const quaternion& a, T b) noexcept; /** * Divides a scalar by a quaternion. @@ -270,7 +270,7 @@ constexpr quaternion div(const quaternion& a, T b) noexcept; * @return Result of the division. */ template -constexpr quaternion div(T a, const quaternion& b) noexcept; +[[nodiscard]] constexpr quaternion div(T a, const quaternion& b) noexcept; /** * Calculates the inverse length of a quaternion. @@ -280,7 +280,7 @@ constexpr quaternion div(T a, const quaternion& b) noexcept; * @return Inverse length of the quaternion. */ template -T inv_length(const quaternion& q); +[[nodiscard]] T inv_length(const quaternion& q); /** * Calculates the length of a quaternion. @@ -290,7 +290,7 @@ T inv_length(const quaternion& q); * @return Length of the quaternion. */ template -T length(const quaternion& q); +[[nodiscard]] T length(const quaternion& q); /** * Performs linear interpolation between two quaternions. @@ -302,7 +302,7 @@ T length(const quaternion& q); * @return Interpolated quaternion. */ template -constexpr quaternion lerp(const quaternion& a, const quaternion& b, T t) noexcept; +[[nodiscard]] constexpr quaternion lerp(const quaternion& a, const quaternion& b, T t) noexcept; /** * Creates a unit quaternion rotation using forward and up vectors. @@ -313,7 +313,7 @@ constexpr quaternion lerp(const quaternion& a, const quaternion& b, T t * @return Unit rotation quaternion. */ template -quaternion look_rotation(const vector& forward, vector up); +[[nodiscard]] quaternion look_rotation(const vector& forward, vector up); /** * Multiplies two quaternions. @@ -324,7 +324,7 @@ quaternion look_rotation(const vector& forward, vector up); * @return Product of the two quaternions. */ template -constexpr quaternion mul(const quaternion& a, const quaternion& b) noexcept; +[[nodiscard]] constexpr quaternion mul(const quaternion& a, const quaternion& b) noexcept; /** * Multiplies a quaternion by a scalar. @@ -335,7 +335,7 @@ constexpr quaternion mul(const quaternion& a, const quaternion& b) noex * @return Product of the quaternion and scalar. */ template -constexpr quaternion mul(const quaternion& a, T b) noexcept; +[[nodiscard]] constexpr quaternion mul(const quaternion& a, T b) noexcept; /** * Calculates the product of a quaternion and a vector. @@ -347,9 +347,9 @@ constexpr quaternion mul(const quaternion& a, T b) noexcept; */ /// @{ template -constexpr vector mul(const quaternion& a, const vector& b) noexcept; +[[nodiscard]] constexpr vector mul(const quaternion& a, const vector& b) noexcept; template -constexpr vector mul(const vector& a, const quaternion& b) noexcept; +[[nodiscard]] constexpr vector mul(const vector& a, const quaternion& b) noexcept; /// @} /** @@ -360,7 +360,7 @@ constexpr vector mul(const vector& a, const quaternion& b) noexce * @return Negated quaternion. */ template -constexpr quaternion negate(const quaternion& q) noexcept; +[[nodiscard]] constexpr quaternion negate(const quaternion& q) noexcept; /** * Performs normalized linear interpolation between two quaternions. @@ -372,7 +372,7 @@ constexpr quaternion negate(const quaternion& q) noexcept; * @return Interpolated quaternion. */ template -quaternion nlerp(const quaternion& a, const quaternion& b, T t); +[[nodiscard]] quaternion nlerp(const quaternion& a, const quaternion& b, T t); /** * Normalizes a quaternion. @@ -382,7 +382,7 @@ quaternion nlerp(const quaternion& a, const quaternion& b, T t); * @return Normalized quaternion. */ template -quaternion normalize(const quaternion& q); +[[nodiscard]] quaternion normalize(const quaternion& q); /** * Creates a rotation from an angle and axis. @@ -393,7 +393,7 @@ quaternion normalize(const quaternion& q); * @return Quaternion representing the rotation. */ template -quaternion angle_axis(T angle, const vector& axis); +[[nodiscard]] quaternion angle_axis(T angle, const vector& axis); /** * Calculates the minimum rotation between two normalized direction vectors. @@ -404,7 +404,7 @@ quaternion angle_axis(T angle, const vector& axis); * @return Quaternion representing the minimum rotation between the source and destination vectors. */ template -quaternion rotation(const vector& source, const vector& destination); +[[nodiscard]] quaternion rotation(const vector& source, const vector& destination); /** * Performs spherical linear interpolation between two quaternions. @@ -416,7 +416,7 @@ quaternion rotation(const vector& source, const vector& destinati * @return Interpolated quaternion. */ template -quaternion slerp(const quaternion& a, const quaternion& b, T t, T error = T{1e-6}); +[[nodiscard]] quaternion slerp(const quaternion& a, const quaternion& b, T t, T error = T{1e-6}); /** * Calculates the square length of a quaternion. The square length can be calculated faster than the length because a call to `std::sqrt` is saved. @@ -426,7 +426,7 @@ quaternion slerp(const quaternion& a, const quaternion& b, T t, T error * @return Square length of the quaternion. */ template -constexpr T sqr_length(const quaternion& q) noexcept; +[[nodiscard]] constexpr T sqr_length(const quaternion& q) noexcept; /** * Subtracts a quaternion from another quaternion. @@ -437,7 +437,7 @@ constexpr T sqr_length(const quaternion& q) noexcept; * @return Difference between the quaternions. */ template -constexpr quaternion sub(const quaternion& a, const quaternion& b) noexcept; +[[nodiscard]] constexpr quaternion sub(const quaternion& a, const quaternion& b) noexcept; /** * Subtracts a quaternion and a scalar. @@ -449,9 +449,9 @@ constexpr quaternion sub(const quaternion& a, const quaternion& b) noex */ /// @{ template -constexpr quaternion sub(const quaternion& a, T b) noexcept; +[[nodiscard]] constexpr quaternion sub(const quaternion& a, T b) noexcept; template -constexpr quaternion sub(T a, const quaternion& b) noexcept; +[[nodiscard]] constexpr quaternion sub(T a, const quaternion& b) noexcept; /// @} /** @@ -476,7 +476,7 @@ void swing_twist(const quaternion& q, const vector& a, quaternion& q * @return Unit quaternion representing the rotation described by @p m. */ template -quaternion quaternion_cast(const matrix& m); +[[nodiscard]] quaternion quaternion_cast(const matrix& m); template inline constexpr quaternion add(const quaternion& a, const quaternion& b) noexcept diff --git a/src/math/random.hpp b/src/math/random.hpp index b89629e..04f0345 100644 --- a/src/math/random.hpp +++ b/src/math/random.hpp @@ -35,10 +35,7 @@ namespace math { * @return Pseudo-random floating point number. */ template -T random(T start, T end); - -template -inline T random(T start, T end) +[[nodiscard]] T random(T start, T end) { static_assert(std::is_floating_point::value); constexpr T rand_max_inverse = T(1) / static_cast(RAND_MAX); diff --git a/src/math/se3.hpp b/src/math/se3.hpp index 807c344..6b76c98 100644 --- a/src/math/se3.hpp +++ b/src/math/se3.hpp @@ -54,10 +54,10 @@ public: quaternion_type r; /// Returns the inverse of this SE(3) transformation. - se3 inverse() const; + [[nodiscard]] se3 inverse() const; /// Returns a matrix representation of the SE(3) transformation. - matrix_type matrix() const; + [[nodiscard]] matrix_type matrix() const; /** * Transforms a vector by this SE(3) transformation. @@ -65,7 +65,7 @@ public: * @param x Untransformed vector. * @return Transformed vector. */ - vector_type transform(const vector_type& x) const; + [[nodiscard]] vector_type transform(const vector_type& x) const; /** * Transforms an SE(3) transformation by this SE(3) transformation. @@ -73,13 +73,13 @@ public: * @param x Other SE(3) transformation. * @return Frame in this se3's space. */ - se3 transform(const se3& x) const; + [[nodiscard]] se3 transform(const se3& x) const; /// @copydoc se3::transform(const vector_type&) const - vector_type operator*(const vector_type& x) const; + [[nodiscard]] vector_type operator*(const vector_type& x) const; /// @copydoc se3::transform(const se3&) const - se3 operator*(const se3& x) const; + [[nodiscard]] se3 operator*(const se3& x) const; }; template diff --git a/src/math/transform-functions.hpp b/src/math/transform-functions.hpp index 046ff87..e1994f3 100644 --- a/src/math/transform-functions.hpp +++ b/src/math/transform-functions.hpp @@ -31,7 +31,7 @@ namespace math { * @param t Transform of which to take the inverse. */ template -transform inverse(const transform& t); +[[nodiscard]] transform inverse(const transform& t); /** * Converts a transform to a transformation matrix. @@ -40,7 +40,7 @@ transform inverse(const transform& t); * @return Matrix representing the transformation described by `t`. */ template -matrix matrix_cast(const transform& t); +[[nodiscard]] matrix matrix_cast(const transform& t); /** * Multiplies two transforms. @@ -50,7 +50,7 @@ matrix matrix_cast(const transform& t); * @return Product of the two transforms. */ template -transform mul(const transform& x, const transform& y); +[[nodiscard]] transform mul(const transform& x, const transform& y); /** * Multiplies a vector by a transform. @@ -60,7 +60,7 @@ transform mul(const transform& x, const transform& y); * @return Product of the transform and vector. */ template -vector mul(const transform& t, const vector& v); +[[nodiscard]] vector mul(const transform& t, const vector& v); template transform inverse(const transform& t) diff --git a/src/math/transform-operators.hpp b/src/math/transform-operators.hpp index d818402..5c8774b 100644 --- a/src/math/transform-operators.hpp +++ b/src/math/transform-operators.hpp @@ -25,11 +25,11 @@ /// @copydoc math::mul(const math::transform&, const math::transform&) template -math::transform operator*(const math::transform& x, const math::transform& y); +[[nodiscard]] math::transform operator*(const math::transform& x, const math::transform& y); /// @copydoc math::mul(const math::transform&, const math::vector&) template -math::vector operator*(const math::transform& t, const math::vector& v); +[[nodiscard]] math::vector operator*(const math::transform& t, const math::vector& v); /** * Multiplies two transforms and stores the result in the first transform. diff --git a/src/math/vector.hpp b/src/math/vector.hpp index 7444672..4c0064e 100644 --- a/src/math/vector.hpp +++ b/src/math/vector.hpp @@ -55,7 +55,7 @@ struct vector /// @private template - inline constexpr vector type_cast(std::index_sequence) const noexcept + [[nodiscard]] inline constexpr vector type_cast(std::index_sequence) const noexcept { return {static_cast(elements[I])...}; } @@ -68,14 +68,14 @@ struct vector * @return Vector containing the type-casted elements. */ template - inline constexpr explicit operator vector() const noexcept + [[nodiscard]] inline constexpr explicit operator vector() const noexcept { return type_cast(std::make_index_sequence{}); } /// @private template - inline constexpr vector size_cast(std::index_sequence) const noexcept + [[nodiscard]] inline constexpr vector size_cast(std::index_sequence) const noexcept { return {(I < N) ? elements[I] : T{0} ...}; } @@ -88,7 +88,7 @@ struct vector * @return *m*-dimensional vector. */ template - inline constexpr explicit operator vector() const noexcept + [[nodiscard]] inline constexpr explicit operator vector() const noexcept { return size_cast(std::make_index_sequence{}); } @@ -106,11 +106,11 @@ struct vector * @return Reference to the element at index @p i. */ /// @{ - inline constexpr element_type& operator[](std::size_t i) noexcept + [[nodiscard]] inline constexpr element_type& operator[](std::size_t i) noexcept { return elements[i]; } - inline constexpr const element_type& operator[](std::size_t i) const noexcept + [[nodiscard]] inline constexpr const element_type& operator[](std::size_t i) const noexcept { return elements[i]; } @@ -120,11 +120,11 @@ struct vector * Returns a reference to the first element. */ /// @{ - inline constexpr element_type& front() noexcept + [[nodiscard]] inline constexpr element_type& front() noexcept { return elements[0]; } - inline constexpr const element_type& front() const noexcept + [[nodiscard]] inline constexpr const element_type& front() const noexcept { return elements[0]; } @@ -134,11 +134,11 @@ struct vector * Returns a reference to the last element. */ /// @{ - inline constexpr element_type& back() noexcept + [[nodiscard]] inline constexpr element_type& back() noexcept { return elements[N - 1]; } - inline constexpr const element_type& back() const noexcept + [[nodiscard]] inline constexpr const element_type& back() const noexcept { return elements[N - 1]; } @@ -148,11 +148,11 @@ struct vector * Returns a pointer to the element array. */ /// @{ - inline constexpr element_type* data() noexcept + [[nodiscard]] inline constexpr element_type* data() noexcept { return elements; }; - inline constexpr const element_type* data() const noexcept + [[nodiscard]] inline constexpr const element_type* data() const noexcept { return elements; }; @@ -160,12 +160,12 @@ struct vector /// Returns a reference to the first element. /// @{ - inline constexpr element_type& x() noexcept + [[nodiscard]] inline constexpr element_type& x() noexcept { static_assert(N > 0, "Vector does not contain an x element."); return elements[0]; } - inline constexpr const element_type& x() const noexcept + [[nodiscard]] inline constexpr const element_type& x() const noexcept { static_assert(N > 0, "Vector does not contain an x element."); return elements[0]; @@ -176,12 +176,12 @@ struct vector * Returns a reference to the second element. */ /// @{ - inline constexpr element_type& y() noexcept + [[nodiscard]] inline constexpr element_type& y() noexcept { static_assert(N > 1, "Vector does not contain a y element."); return elements[1]; } - inline constexpr const element_type& y() const noexcept + [[nodiscard]] inline constexpr const element_type& y() const noexcept { static_assert(N > 1, "Vector does not contain a y element."); return elements[1]; @@ -192,12 +192,12 @@ struct vector * Returns a reference to the third element. */ /// @{ - inline constexpr element_type& z() noexcept + [[nodiscard]] inline constexpr element_type& z() noexcept { static_assert(N > 2, "Vector does not contain a z element."); return elements[2]; } - inline constexpr const element_type& z() const noexcept + [[nodiscard]] inline constexpr const element_type& z() const noexcept { static_assert(N > 2, "Vector does not contain a z element."); return elements[2]; @@ -213,15 +213,15 @@ struct vector * Returns an iterator to the first element. */ /// @{ - inline constexpr element_type* begin() noexcept + [[nodiscard]] inline constexpr element_type* begin() noexcept { return elements; } - inline constexpr const element_type* begin() const noexcept + [[nodiscard]] inline constexpr const element_type* begin() const noexcept { return elements; } - inline constexpr const element_type* cbegin() const noexcept + [[nodiscard]] inline constexpr const element_type* cbegin() const noexcept { return elements; } @@ -231,15 +231,15 @@ struct vector * Returns an iterator to the element following the last element. */ /// @{ - inline constexpr element_type* end() noexcept + [[nodiscard]] inline constexpr element_type* end() noexcept { return elements + N; } - inline constexpr const element_type* end() const noexcept + [[nodiscard]] inline constexpr const element_type* end() const noexcept { return elements + N; } - inline constexpr const element_type* cend() const noexcept + [[nodiscard]] inline constexpr const element_type* cend() const noexcept { return elements + N; } @@ -249,15 +249,15 @@ struct vector * Returns a reverse iterator to the first element of the reversed vector. */ /// @{ - inline constexpr std::reverse_iterator rbegin() noexcept + [[nodiscard]] inline constexpr std::reverse_iterator rbegin() noexcept { return std::reverse_iterator(elements + N); } - inline constexpr std::reverse_iterator rbegin() const noexcept + [[nodiscard]] inline constexpr std::reverse_iterator rbegin() const noexcept { return std::reverse_iterator(elements + N); } - inline constexpr std::reverse_iterator crbegin() const noexcept + [[nodiscard]] inline constexpr std::reverse_iterator crbegin() const noexcept { return std::reverse_iterator(elements + N); } @@ -267,15 +267,15 @@ struct vector * Returns a reverse iterator to the element following the last element of the reversed vector. */ /// @{ - inline constexpr std::reverse_iterator rend() noexcept + [[nodiscard]] inline constexpr std::reverse_iterator rend() noexcept { return std::reverse_iterator(elements); } - inline constexpr std::reverse_iterator rend() const noexcept + [[nodiscard]] inline constexpr std::reverse_iterator rend() const noexcept { return std::reverse_iterator(elements); } - inline constexpr std::reverse_iterator crend() const noexcept + [[nodiscard]] inline constexpr std::reverse_iterator crend() const noexcept { return std::reverse_iterator(elements); } @@ -289,7 +289,7 @@ struct vector /** * Returns the number of elements in the vector. */ - inline constexpr std::size_t size() const noexcept + [[nodiscard]] inline constexpr std::size_t size() const noexcept { return N; }; @@ -302,14 +302,14 @@ struct vector /** * Returns a zero vector, where every element is equal to zero. */ - static constexpr vector zero() noexcept + [[nodiscard]] static constexpr vector zero() noexcept { return {}; } /// @private template - static constexpr vector one(std::index_sequence) noexcept + [[nodiscard]] static constexpr vector one(std::index_sequence) noexcept { //return {T{1}...}; @@ -320,7 +320,7 @@ struct vector /** * Returns a vector of ones, where every element is equal to one. */ - static constexpr vector one() noexcept + [[nodiscard]] static constexpr vector one() noexcept { return one(std::make_index_sequence{}); } @@ -348,7 +348,7 @@ using vector4 = vector; * @return Absolute values of input vector elements. */ template -constexpr vector abs(const vector& x); +[[nodiscard]] constexpr vector abs(const vector& x); /** * Adds two values. @@ -360,9 +360,9 @@ constexpr vector abs(const vector& x); */ /// @{ template -constexpr vector add(const vector& x, const vector& y) noexcept; +[[nodiscard]] constexpr vector add(const vector& x, const vector& y) noexcept; template -constexpr vector add(const vector& x, T y) noexcept; +[[nodiscard]] constexpr vector add(const vector& x, T y) noexcept; /// @} /** @@ -373,7 +373,7 @@ constexpr vector add(const vector& x, T y) noexcept; * @return `true` if all elements are `true`, `false` otherwise. */ template -constexpr bool all(const vector& x) noexcept; +[[nodiscard]] constexpr bool all(const vector& x) noexcept; /** * Checks if any elements of a boolean vector are `true`. @@ -383,7 +383,7 @@ constexpr bool all(const vector& x) noexcept; * @return `true` if any elements are `true`, `false` otherwise. */ template -constexpr bool any(const vector& x) noexcept; +[[nodiscard]] constexpr bool any(const vector& x) noexcept; /** * Performs a element-wise ceil operation. @@ -393,7 +393,7 @@ constexpr bool any(const vector& x) noexcept; * @return Component-wise ceil of input vector. */ template -constexpr vector ceil(const vector& x); +[[nodiscard]] constexpr vector ceil(const vector& x); /** * Clamps the values of a vector's elements. @@ -406,9 +406,9 @@ constexpr vector ceil(const vector& x); */ /// @{ template -constexpr vector clamp(const vector& x, const vector& min, const vector& max); +[[nodiscard]] constexpr vector clamp(const vector& x, const vector& min, const vector& max); template -constexpr vector clamp(const vector& x, T min, T max); +[[nodiscard]] constexpr vector clamp(const vector& x, T min, T max); /// @} /** @@ -420,7 +420,7 @@ constexpr vector clamp(const vector& x, T min, T max); * @return Length-clamped vector. */ template -vector clamp_length(const vector& x, T max_length); +[[nodiscard]] vector clamp_length(const vector& x, T max_length); /** * Calculate the cross product of two vectors. @@ -431,7 +431,7 @@ vector clamp_length(const vector& x, T max_length); * @return Cross product of the two vectors. */ template -constexpr vector cross(const vector& x, const vector& y) noexcept; +[[nodiscard]] constexpr vector cross(const vector& x, const vector& y) noexcept; /** * Calculates the distance between two points. @@ -442,7 +442,7 @@ constexpr vector cross(const vector& x, const vector& y) noexc * @return Distance between the two points. */ template -T distance(const vector& p0, const vector& p1); +[[nodiscard]] T distance(const vector& p0, const vector& p1); /** * Divides a vector by a value. @@ -454,11 +454,11 @@ T distance(const vector& p0, const vector& p1); */ /// @{ template -constexpr vector div(const vector& x, const vector& y) noexcept; +[[nodiscard]] constexpr vector div(const vector& x, const vector& y) noexcept; template -constexpr vector div(const vector& x, T y) noexcept; +[[nodiscard]] constexpr vector div(const vector& x, T y) noexcept; template -constexpr vector div(T x, const vector& y) noexcept; +[[nodiscard]] constexpr vector div(T x, const vector& y) noexcept; /// @} /** @@ -470,7 +470,7 @@ constexpr vector div(T x, const vector& y) noexcept; * @return Dot product of the two vectors. */ template -constexpr T dot(const vector& x, const vector& y) noexcept; +[[nodiscard]] constexpr T dot(const vector& x, const vector& y) noexcept; /** * Compares two vectors for equality @@ -481,7 +481,7 @@ constexpr T dot(const vector& x, const vector& y) noexcept; * @return Boolean vector containing the result of the element comparisons. */ template -constexpr vector equal(const vector& x, const vector& y) noexcept; +[[nodiscard]] constexpr vector equal(const vector& x, const vector& y) noexcept; /** * Performs a element-wise floor operation. @@ -491,7 +491,7 @@ constexpr vector equal(const vector& x, const vector& y) no * @return Component-wise floor of input vector. */ template -constexpr vector floor(const vector& x); +[[nodiscard]] constexpr vector floor(const vector& x); /** * Performs a multiply-add operation. @@ -504,9 +504,9 @@ constexpr vector floor(const vector& x); */ /// @{ template -constexpr vector fma(const vector& x, const vector& y, const vector& z); +[[nodiscard]] constexpr vector fma(const vector& x, const vector& y, const vector& z); template -constexpr vector fma(const vector& x, T y, T z); +[[nodiscard]] constexpr vector fma(const vector& x, T y, T z); /// @} /** @@ -517,7 +517,7 @@ constexpr vector fma(const vector& x, T y, T z); * @return Fractional parts of input vector. */ template -constexpr vector fract(const vector& x); +[[nodiscard]] constexpr vector fract(const vector& x); /** * Extracts the Ith element from a vector. @@ -532,13 +532,13 @@ constexpr vector fract(const vector& x); */ /// @{ template -constexpr T& get(math::vector& v) noexcept; +[[nodiscard]] constexpr T& get(math::vector& v) noexcept; template -constexpr T&& get(math::vector&& v) noexcept; +[[nodiscard]] constexpr T&& get(math::vector&& v) noexcept; template -constexpr const T& get(const math::vector& v) noexcept; +[[nodiscard]] constexpr const T& get(const math::vector& v) noexcept; template -constexpr const T&& get(const math::vector&& v) noexcept; +[[nodiscard]] constexpr const T&& get(const math::vector&& v) noexcept; /// @} /** @@ -550,7 +550,7 @@ constexpr const T&& get(const math::vector&& v) noexcept; * @return Boolean vector containing the result of the element comparisons. */ template -constexpr vector greater_than(const vector& x, const vector& y) noexcept; +[[nodiscard]] constexpr vector greater_than(const vector& x, const vector& y) noexcept; /** * Performs a element-wise greater-than or equal-to comparison of two vectors. @@ -561,7 +561,7 @@ constexpr vector greater_than(const vector& x, const vector * @return Boolean vector containing the result of the element comparisons. */ template -constexpr vector greater_than_equal(const vector& x, const vector& y) noexcept; +[[nodiscard]] constexpr vector greater_than_equal(const vector& x, const vector& y) noexcept; /** * Calculates the inverse length of a vector. @@ -571,7 +571,7 @@ constexpr vector greater_than_equal(const vector& x, const vector * @return Inverse length of the vector. */ template -T inv_length(const vector& x); +[[nodiscard]] T inv_length(const vector& x); /** * Calculates the length of a vector. @@ -581,7 +581,7 @@ T inv_length(const vector& x); * @return Length of the vector. */ template -T length(const vector& x); +[[nodiscard]] T length(const vector& x); /** * Performs a element-wise less-than comparison of two vectors. @@ -592,7 +592,7 @@ T length(const vector& x); * @return Boolean vector containing the result of the element comparisons. */ template -constexpr vector less_than(const vector& x, const vector& y) noexcept; +[[nodiscard]] constexpr vector less_than(const vector& x, const vector& y) noexcept; /** * Performs a element-wise less-than or equal-to comparison of two vectors. @@ -603,7 +603,7 @@ constexpr vector less_than(const vector& x, const vector& y * @return Boolean vector containing the result of the element comparisons. */ template -constexpr vector less_than_equal(const vector& x, const vector& y) noexcept; +[[nodiscard]] constexpr vector less_than_equal(const vector& x, const vector& y) noexcept; /** * Returns a vector containing the maximum elements of two vectors. @@ -614,7 +614,7 @@ constexpr vector less_than_equal(const vector& x, const vector -constexpr vector max(const vector& x, const vector& y); +[[nodiscard]] constexpr vector max(const vector& x, const vector& y); /** * Returns the value of the greatest element in a vector. @@ -624,7 +624,7 @@ constexpr vector max(const vector& x, const vector& y); * @return Value of the greatest element in the input vector. */ template -constexpr T max(const vector& x); +[[nodiscard]] constexpr T max(const vector& x); /** * Returns a vector containing the minimum elements of two vectors. @@ -635,7 +635,7 @@ constexpr T max(const vector& x); * @return Minimum elements of the two vectors. */ template -constexpr vector min(const vector& x, const vector& y); +[[nodiscard]] constexpr vector min(const vector& x, const vector& y); /** * Returns the value of the smallest element in a vector. @@ -645,7 +645,7 @@ constexpr vector min(const vector& x, const vector& y); * @return Value of the smallest element in the input vector. */ template -constexpr T min(const vector& x); +[[nodiscard]] constexpr T min(const vector& x); /** * Calculates the element-wise remainder of the division operation `x / y`. @@ -657,9 +657,9 @@ constexpr T min(const vector& x); */ /// @{ template -constexpr vector mod(const vector& x, const vector& y); +[[nodiscard]] constexpr vector mod(const vector& x, const vector& y); template -constexpr vector mod(const vector& x, T y); +[[nodiscard]] constexpr vector mod(const vector& x, T y); /// @} /** @@ -672,9 +672,9 @@ constexpr vector mod(const vector& x, T y); */ /// @{ template -constexpr vector mul(const vector& x, const vector& y) noexcept; +[[nodiscard]] constexpr vector mul(const vector& x, const vector& y) noexcept; template -constexpr vector mul(const vector& x, T y) noexcept; +[[nodiscard]] constexpr vector mul(const vector& x, T y) noexcept; /// @} /** @@ -685,7 +685,7 @@ constexpr vector mul(const vector& x, T y) noexcept; * @return Negated vector. */ template -constexpr vector negate(const vector& x) noexcept; +[[nodiscard]] constexpr vector negate(const vector& x) noexcept; /** * Calculates the unit vector in the same direction as the original vector. @@ -695,7 +695,7 @@ constexpr vector negate(const vector& x) noexcept; * @return Normalized vector. */ template -vector normalize(const vector& x); +[[nodiscard]] vector normalize(const vector& x); /** * Logically inverts a boolean vector. @@ -705,7 +705,7 @@ vector normalize(const vector& x); * @return Logically inverted vector. */ template -constexpr vector logical_not(const vector& x) noexcept; +[[nodiscard]] constexpr vector logical_not(const vector& x) noexcept; /** * Compares two vectors for inequality @@ -716,7 +716,7 @@ constexpr vector logical_not(const vector& x) noexcept; * @return Boolean vector containing the result of the element comparisons. */ template -constexpr vector not_equal(const vector& x, const vector& y) noexcept; +[[nodiscard]] constexpr vector not_equal(const vector& x, const vector& y) noexcept; /** * Raises each element to a power. @@ -728,9 +728,9 @@ constexpr vector not_equal(const vector& x, const vector& y */ /// @{ template -vector pow(const vector& x, const vector& y); +[[nodiscard]] vector pow(const vector& x, const vector& y); template -vector pow(const vector& x, T y); +[[nodiscard]] vector pow(const vector& x, T y); /// @} /** @@ -741,7 +741,7 @@ vector pow(const vector& x, T y); * @return Component-wise round of input vector. */ template -constexpr vector round(const vector& x); +[[nodiscard]] constexpr vector round(const vector& x); /** * Returns a vector containing the signs of each element. @@ -750,7 +750,7 @@ constexpr vector round(const vector& x); * @return Signs of input vector elements. */ template -constexpr vector sign(const vector& x); +[[nodiscard]] constexpr vector sign(const vector& x); /** * Calculates the square distance between two points. The square distance can be calculated faster than the distance because a call to `std::sqrt` is saved. @@ -761,7 +761,7 @@ constexpr vector sign(const vector& x); * @return Square distance between the two points. */ template -constexpr T sqr_distance(const vector& p0, const vector& p1) noexcept; +[[nodiscard]] constexpr T sqr_distance(const vector& p0, const vector& p1) noexcept; /** * Calculates the square length of a vector. The square length can be calculated faster than the length because a call to `std::sqrt` is saved. @@ -771,7 +771,7 @@ constexpr T sqr_distance(const vector& p0, const vector& p1) noexcep * @return Square length of the vector. */ template -constexpr T sqr_length(const vector& x) noexcept; +[[nodiscard]] constexpr T sqr_length(const vector& x) noexcept; /** * Takes the square root of each element. @@ -781,7 +781,7 @@ constexpr T sqr_length(const vector& x) noexcept; * @return Square roots of the input vector elements. */ template -vector sqrt(const vector& x); +[[nodiscard]] vector sqrt(const vector& x); /** * Subtracts a value by another value. @@ -793,11 +793,11 @@ vector sqrt(const vector& x); */ /// @{ template -constexpr vector sub(const vector& x, const vector& y) noexcept; +[[nodiscard]] constexpr vector sub(const vector& x, const vector& y) noexcept; template -constexpr vector sub(const vector& x, T y) noexcept; +[[nodiscard]] constexpr vector sub(const vector& x, T y) noexcept; template -constexpr vector sub(T x, const vector& y) noexcept; +[[nodiscard]] constexpr vector sub(T x, const vector& y) noexcept; /// @} /** @@ -807,7 +807,7 @@ constexpr vector sub(T x, const vector& y) noexcept; * @return Sum of the vector's elements. */ template -constexpr T sum(const vector& x) noexcept; +[[nodiscard]] constexpr T sum(const vector& x) noexcept; /** * Makes an *m*-dimensional vector by rearranging and/or duplicating elements of an *n*-dimensional vector. @@ -821,7 +821,7 @@ constexpr T sum(const vector& x) noexcept; * @return Vector containing elements from @p x in the order specified by @p Indices. The size of the returned vector is equivalent to the number of indices in @p Indices. */ template -constexpr vector swizzle(const vector& x) noexcept; +[[nodiscard]] constexpr vector swizzle(const vector& x) noexcept; /** * Performs a element-wise trunc operation. @@ -830,7 +830,7 @@ constexpr vector swizzle(const vector& x) noexcept; * @return Component-wise trunc of input vector. */ template -constexpr vector trunc(const vector& x); +[[nodiscard]] constexpr vector trunc(const vector& x); /// @private template diff --git a/src/physics/gas/atmosphere.hpp b/src/physics/gas/atmosphere.hpp index 878aea6..328edf1 100644 --- a/src/physics/gas/atmosphere.hpp +++ b/src/physics/gas/atmosphere.hpp @@ -21,7 +21,7 @@ #define ANTKEEPER_PHYSICS_GAS_ATMOSPHERE_HPP #include "physics/constants.hpp" -#include "math/constants.hpp" +#include "math/numbers.hpp" #include #include diff --git a/src/physics/light/blackbody.hpp b/src/physics/light/blackbody.hpp index afa2162..bc5d80d 100644 --- a/src/physics/light/blackbody.hpp +++ b/src/physics/light/blackbody.hpp @@ -20,7 +20,7 @@ #ifndef ANTKEEPER_PHYSICS_LIGHT_BLACKBODY_HPP #define ANTKEEPER_PHYSICS_LIGHT_BLACKBODY_HPP -#include "math/constants.hpp" +#include "math/numbers.hpp" #include "physics/constants.hpp" namespace physics { diff --git a/src/physics/light/phase.hpp b/src/physics/light/phase.hpp index 917b797..46fae4c 100644 --- a/src/physics/light/phase.hpp +++ b/src/physics/light/phase.hpp @@ -20,7 +20,7 @@ #ifndef ANTKEEPER_PHYSICS_LIGHT_PHASE_HPP #define ANTKEEPER_PHYSICS_LIGHT_PHASE_HPP -#include "math/constants.hpp" +#include "math/numbers.hpp" #include namespace physics { diff --git a/src/physics/light/photometry.hpp b/src/physics/light/photometry.hpp index 258b627..0d845d7 100644 --- a/src/physics/light/photometry.hpp +++ b/src/physics/light/photometry.hpp @@ -20,7 +20,7 @@ #ifndef ANTKEEPER_PHYSICS_LIGHT_PHOTOMETRY_HPP #define ANTKEEPER_PHYSICS_LIGHT_PHOTOMETRY_HPP -#include "math/constants.hpp" +#include "math/numbers.hpp" #include "math/quadrature.hpp" namespace physics { diff --git a/src/physics/orbit/anomaly.hpp b/src/physics/orbit/anomaly.hpp index b00ef9c..dfb14d7 100644 --- a/src/physics/orbit/anomaly.hpp +++ b/src/physics/orbit/anomaly.hpp @@ -20,7 +20,7 @@ #ifndef ANTKEEPER_PHYSICS_ORBIT_ANOMALY_HPP #define ANTKEEPER_PHYSICS_ORBIT_ANOMALY_HPP -#include "math/constants.hpp" +#include "math/numbers.hpp" #include namespace physics { diff --git a/src/physics/orbit/elements.hpp b/src/physics/orbit/elements.hpp index 6f2f976..82f54bd 100644 --- a/src/physics/orbit/elements.hpp +++ b/src/physics/orbit/elements.hpp @@ -21,7 +21,7 @@ #define ANTKEEPER_PHYSICS_ORBIT_ELEMENTS_HPP #include "utility/fundamental-types.hpp" -#include "math/constants.hpp" +#include "math/numbers.hpp" #include namespace physics { diff --git a/src/physics/time/ut1.hpp b/src/physics/time/ut1.hpp index c85e952..59424f9 100644 --- a/src/physics/time/ut1.hpp +++ b/src/physics/time/ut1.hpp @@ -20,7 +20,7 @@ #ifndef ANTKEEPER_PHYSICS_TIME_UT1_HPP #define ANTKEEPER_PHYSICS_TIME_UT1_HPP -#include "math/constants.hpp" +#include "math/numbers.hpp" namespace physics { namespace time { diff --git a/src/physics/time/utc.hpp b/src/physics/time/utc.hpp index 59275e1..8485cef 100644 --- a/src/physics/time/utc.hpp +++ b/src/physics/time/utc.hpp @@ -20,7 +20,7 @@ #ifndef ANTKEEPER_PHYSICS_TIME_UTC_HPP #define ANTKEEPER_PHYSICS_TIME_UTC_HPP -#include "math/constants.hpp" +#include "math/numbers.hpp" namespace physics { namespace time { diff --git a/src/platform/windows/nvidia.cpp b/src/platform/windows/nvidia.cpp index 900b3a5..a35feb8 100644 --- a/src/platform/windows/nvidia.cpp +++ b/src/platform/windows/nvidia.cpp @@ -22,6 +22,6 @@ extern "C" { - // Direct Nvidia Optimus to use high-performance graphics + /// Direct Nvidia Optimus to use high-performance graphics _declspec(dllexport) DWORD NvOptimusEnablement = 0x00000001; } diff --git a/src/render/passes/material-pass.cpp b/src/render/passes/material-pass.cpp index 0721909..dcd6865 100644 --- a/src/render/passes/material-pass.cpp +++ b/src/render/passes/material-pass.cpp @@ -269,7 +269,7 @@ void material_pass::render(const render::context& ctx, render::queue& queue) con // Sort render queue queue.sort(operation_compare); - + for (const render::operation& operation: queue) { // Get operation material @@ -591,6 +591,8 @@ const material_pass::parameter_set* material_pass::load_parameter_set(const gl:: bool operation_compare(const render::operation& a, const render::operation& b) { + /// @TODO: something is wrong with this compare op, assertion fails + if (!a.material) return false; else if (!b.material) @@ -607,7 +609,7 @@ bool operation_compare(const render::operation& a, const render::operation& b) if (xray_b) { // A and B are both xray, render back to front - return (a.depth >= b.depth); + return (a.depth > b.depth); } else { @@ -641,7 +643,7 @@ bool operation_compare(const render::operation& a, const render::operation& b) if (decal_b) { // A and B are both transparent decals, render back to front - return (a.depth >= b.depth); + return (a.depth > b.depth); } else { @@ -659,7 +661,7 @@ bool operation_compare(const render::operation& a, const render::operation& b) else { // A and B are both transparent, but not decals, render back to front - return (a.depth <= b.depth); + return (a.depth < b.depth); } } } diff --git a/src/render/passes/shadow-map-pass.cpp b/src/render/passes/shadow-map-pass.cpp index dfedde3..a0dfdf9 100644 --- a/src/render/passes/shadow-map-pass.cpp +++ b/src/render/passes/shadow-map-pass.cpp @@ -173,7 +173,6 @@ void shadow_map_pass::render_csm(const scene::directional_light& light, const re float4x4 light_projection = math::ortho(-1.0f, 1.0f, -1.0f, 1.0f, -1.0f, 1.0f); float4x4 light_view_projection = light_projection * light_view; - float4x4 crop_matrix; float4x4 cropped_view_projection; float4x4 model_view_projection; diff --git a/src/render/renderer.cpp b/src/render/renderer.cpp index 9e987a0..a11b8db 100644 --- a/src/render/renderer.cpp +++ b/src/render/renderer.cpp @@ -32,7 +32,7 @@ #include "geom/projection.hpp" #include "config.hpp" #include "math/quaternion.hpp" -#include "math/constants.hpp" +#include "math/numbers.hpp" #include #include diff --git a/src/resources/model-loader.cpp b/src/resources/model-loader.cpp index 9600cdb..c9c343b 100644 --- a/src/resources/model-loader.cpp +++ b/src/resources/model-loader.cpp @@ -24,7 +24,7 @@ #include "gl/vertex-attribute.hpp" #include "gl/drawing-mode.hpp" #include "utility/fundamental-types.hpp" -#include "math/constants.hpp" +#include "math/numbers.hpp" #include #include #include diff --git a/src/scene/camera.cpp b/src/scene/camera.cpp index b2e5a2e..f51b717 100644 --- a/src/scene/camera.cpp +++ b/src/scene/camera.cpp @@ -19,7 +19,7 @@ #include "scene/camera.hpp" #include "config.hpp" -#include "math/constants.hpp" +#include "math/numbers.hpp" #include "math/interpolation.hpp" #include "math/quaternion.hpp" #include "math/projection.hpp"