-
Notifications
You must be signed in to change notification settings - Fork 4
Develop vector2、constants and scalar. #43
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
6900975
Develop vector2、constants and scalar.
YuanSang0512 7642a89
Use the standard limits lib instead of hardcoding for scalar types; F…
YuanSang0512 54718a4
Remove unnecessary math namespace;
YuanSang0512 282d15a
Remove unnecessary math namespace
YuanSang0512 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,81 @@ | ||
| #pragma once | ||
|
|
||
| #include <numbers> | ||
|
|
||
| namespace gkit::math { | ||
| // Mathematical constants (IEEE 754 standard) | ||
| // Pi multiples | ||
| constexpr float PI_32 = std::numbers::pi_v<float>; | ||
| constexpr float TWO_PI_32 = PI_32 * 2.0f; | ||
| constexpr float HALF_PI_32 = PI_32 * 0.5f; | ||
| constexpr float INV_PI_32 = std::numbers::inv_pi_v<float>; | ||
|
|
||
| constexpr float PI_64 = std::numbers::pi; | ||
| constexpr float TWO_PI_64 = PI_64 * 2.0; | ||
| constexpr float HALF_PI_64 = PI_64 * 0.5; | ||
| constexpr float INV_PI_64 = std::numbers::inv_pi; | ||
|
|
||
| // Natural constants | ||
| constexpr float E_32 = std::numbers::e_v<float>; | ||
| constexpr float E_64 = std::numbers::e; | ||
|
|
||
| // Golden ratio | ||
| constexpr float PHI_32 = std::numbers::phi_v<float>; | ||
| constexpr float PHI_64 = std::numbers::phi; | ||
|
|
||
| // Powers and roots | ||
| constexpr float SQRT_2_32 = std::numbers::sqrt2_v<float>; | ||
| constexpr float SQRT_3_32 = std::numbers::sqrt3_v<float>; | ||
| constexpr float SQRT_5_32 = 2.2360679774997897f; // sqrt(5) | ||
| constexpr float LN_2_32 = std::numbers::ln2_v<float>; | ||
| constexpr float LN_10_32 = std::numbers::ln10_v<float>; | ||
|
|
||
| constexpr float SQRT_2_64 = std::numbers::sqrt2; | ||
| constexpr float SQRT_3_64 = std::numbers::sqrt3; | ||
| constexpr float SQRT_5_64 = 2.2360679774997896964091736687313; // sqrt(5) | ||
| constexpr float LN_2_64 = std::numbers::ln2; | ||
| constexpr float LN_10_64 = std::numbers::ln10; | ||
|
|
||
| // Angle conversion constants | ||
| constexpr float DEG_TO_RAD_32 = gkit::math::PI_32 / 180.0f; | ||
| constexpr float RAD_TO_DEG_32 = 180.0f / gkit::math::PI_32; | ||
|
|
||
| constexpr float DEG_TO_RAD_64 = gkit::math::PI_64 / 180.0; | ||
| constexpr float RAD_TO_DEG_64 = 180.0 / gkit::math::PI_64; | ||
|
|
||
| // Common numeric constants | ||
| constexpr float ZERO_32 = 0.0f; | ||
| constexpr float ONE_32 = 1.0f; | ||
| constexpr float NEG_ONE_32 = -1.0f; | ||
| constexpr float TWO_32 = 2.0f; | ||
| constexpr float THREE_32 = 3.0f; | ||
| constexpr float FOUR_32 = 4.0f; | ||
| constexpr float FIVE_32 = 5.0f; | ||
| constexpr float TEN_32 = 10.0f; | ||
| constexpr float HUNDRED_32 = 100.0f; | ||
| constexpr float THOUSAND_32 = 1000.0f; | ||
| constexpr float HALF_32 = 0.5f; | ||
| constexpr float QUARTER_32 = 0.25f; | ||
| constexpr float THIRD_32 = 0.3333333333333333333333333333333f; | ||
|
|
||
| constexpr float ZERO_64 = 0.0; | ||
| constexpr float ONE_64 = 1.0; | ||
| constexpr float NEG_ONE_64 = -1.0; | ||
| constexpr float TWO_64 = 2.0; | ||
| constexpr float THREE_64 = 3.0; | ||
| constexpr float FOUR_64 = 4.0; | ||
| constexpr float FIVE_64 = 5.0; | ||
| constexpr float TEN_64 = 10.0; | ||
| constexpr float HUNDRED_64 = 100.0; | ||
| constexpr float THOUSAND_64 = 1000.0; | ||
| constexpr float HALF_64 = 0.5; | ||
| constexpr float QUARTER_64 = 0.25; | ||
| constexpr float THIRD_64 = 0.3333333333333333333333333333333; | ||
|
|
||
| // Integers | ||
| constexpr int ZERO_I32 = 0; | ||
| constexpr int ONE_I32 = 1; | ||
| constexpr int NEG_ONE_I32 = -1; | ||
| constexpr int TWO_I32 = 2; | ||
| constexpr int THREE_I32 = 3; | ||
| } // namespace gkit::math |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,41 @@ | ||
| #pragma once | ||
|
|
||
| #include <cstdint> | ||
| #include <limits> | ||
|
|
||
| namespace gkit::math { | ||
| // Common limits for scalar types | ||
| template<typename T> | ||
| struct ScalarLimits { | ||
| static constexpr T min_v = std::numeric_limits<T>::min(); | ||
| static constexpr T max_v = std::numeric_limits<T>::max(); | ||
| static constexpr T lowest_v = std::numeric_limits<T>::lowest(); | ||
| static constexpr T epsilon_v = std::numeric_limits<T>::epsilon(); | ||
| }; | ||
|
|
||
| // Floating-point special values using numeric_limits | ||
| constexpr float EPSILON32 = std::numeric_limits<float>::epsilon(); | ||
| constexpr float MIN32 = std::numeric_limits<float>::min(); | ||
| constexpr float MAX32 = std::numeric_limits<float>::max(); | ||
|
|
||
| constexpr float EPSILON64 = std::numeric_limits<float>::epsilon(); | ||
| constexpr float MIN64 = std::numeric_limits<float>::min(); | ||
| constexpr float MAX64 = std::numeric_limits<float>::max(); | ||
|
|
||
| // Integer special values using numeric_limits | ||
| constexpr int8_t I8_MIN = std::numeric_limits<int8_t>::min(); | ||
| constexpr int8_t I8_MAX = std::numeric_limits<int8_t>::max(); | ||
| constexpr uint8_t U8_MAX = std::numeric_limits<uint8_t>::max(); | ||
|
|
||
| constexpr int16_t I16_MIN = std::numeric_limits<int16_t>::min(); | ||
| constexpr int16_t I16_MAX = std::numeric_limits<int16_t>::max(); | ||
| constexpr uint16_t U16_MAX = std::numeric_limits<uint16_t>::max(); | ||
|
|
||
| constexpr int32_t I32_MIN = std::numeric_limits<int32_t>::min(); | ||
| constexpr int32_t I32_MAX = std::numeric_limits<int32_t>::max(); | ||
| constexpr uint32_t U32_MAX = std::numeric_limits<uint32_t>::max(); | ||
|
|
||
| constexpr int64_t I64_MIN = std::numeric_limits<int64_t>::min(); | ||
| constexpr int64_t I64_MAX = std::numeric_limits<int64_t>::max(); | ||
| constexpr uint64_t U64_MAX = std::numeric_limits<uint64_t>::max(); | ||
| } // namespace gkit::math |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,44 +1,71 @@ | ||
| #pragma once | ||
|
|
||
| #include <cmath> | ||
| #include <cstdint> | ||
| #include <tuple> | ||
|
|
||
|
|
||
| namespace gkit::math { | ||
| class Vector2 final { | ||
| public: | ||
| float x = 0.0f; | ||
| float y = 0.0f; | ||
|
|
||
| Vector2() noexcept = default; | ||
| explicit Vector2(float v) noexcept; | ||
| Vector2(float x, float y) noexcept; | ||
| Vector2(const Vector2& other) noexcept; | ||
| Vector2(const Vector2&& other) noexcept; | ||
| ~Vector2() noexcept = default; | ||
|
|
||
| public: // Arithmetic operators | ||
| inline auto operator= (const Vector2& other) noexcept -> Vector2& { this->x = other.x; this->y = other.y; return *this; } | ||
| inline auto operator= (const Vector2&&other) noexcept -> Vector2& { this->x = other.x; this->y = other.y; return *this; } | ||
| public: // Arithmetic operators | ||
| inline auto operator=(const Vector2& other) noexcept -> Vector2& { this->x = other.x; this->y = other.y; return *this; } | ||
| inline auto operator=(const Vector2&& other) noexcept -> Vector2& { this->x = other.x; this->y = other.y; return *this; } | ||
| inline auto operator==(const Vector2& other) noexcept -> bool { return this->x == other.x && this->y == other.y; } | ||
| inline auto operator!=(const Vector2& other) noexcept -> bool { return this->x != other.x || this->y != other.y; } | ||
| inline auto operator+ (const Vector2& other) noexcept -> Vector2 { return Vector2(this->x + other.x, this->y + other.y); } | ||
| inline auto operator- (const Vector2& other) noexcept -> Vector2 { return Vector2(this->x - other.x, this->y - other.y); } | ||
| inline auto operator+(const Vector2& other) noexcept -> Vector2 { return Vector2(this->x + other.x, this->y + other.y); } | ||
| inline auto operator-(const Vector2& other) noexcept -> Vector2 { return Vector2(this->x - other.x, this->y - other.y); } | ||
| inline auto operator+=(const Vector2& other) noexcept -> const Vector2& { this->x += other.x; this->y += other.y; return *this; } | ||
| inline auto operator-=(const Vector2& other) noexcept -> const Vector2& { this->x -= other.x; this->y -= other.y; return *this; } | ||
| inline auto operator* (const int32_t n) noexcept -> Vector2 { return Vector2(this->x * n, this->y * n); } | ||
| inline auto operator/ (const int32_t n) noexcept -> Vector2 { return Vector2(this->x / n, this->y / n); } | ||
| inline auto operator*=(const int32_t n) noexcept -> const Vector2& { this->x *= n; this->y *= n; return *this; } | ||
| inline auto operator/=(const int32_t n) noexcept -> const Vector2& { this->x /= n; this->y /= n; return *this; } | ||
| inline auto operator*(float s) noexcept -> Vector2 { return {this->x * s, this->y * s}; } | ||
| inline auto operator/(float s) noexcept -> Vector2 { return {this->x / s, this->y / s}; } | ||
| inline auto operator*=(float s) noexcept -> const Vector2& { this->x *= s; this->y *= s; return *this; } | ||
| inline auto operator/=(float s) noexcept -> const Vector2& { this->x /= s; this->y /= s; return *this; } | ||
| inline auto operator-() noexcept -> Vector2 { return {-this->x, -this->y}; } | ||
|
|
||
| public: | ||
| static auto zero() -> const Vector2&; | ||
| auto normalization() -> void; | ||
| #ifdef _MSC_VER | ||
| inline auto length() const -> float { return std::sqrt(x * x + y * y); } | ||
| #else | ||
| inline constexpr auto length() const -> float { return std::sqrt(x * x + y * y); } | ||
| #endif | ||
| inline auto properties() -> auto { return std::tie(x, y);} | ||
|
|
||
| private: | ||
| float x = 0.f, | ||
| y = 0.f; | ||
| public: // Properties | ||
| [[nodiscard]] inline auto length() const -> float { return std::sqrt(x * x + y * y); } | ||
| [[nodiscard]] inline constexpr auto length_sq() const -> float { return x * x + y * y; } | ||
| inline auto properties() -> auto { return std::tie(x, y); } | ||
|
|
||
| public: // Operations | ||
| inline static auto zero() noexcept -> Vector2 { return {0.0f, 0.0f}; } | ||
| inline static auto one() noexcept -> Vector2 { return {1.0f, 1.0f}; } | ||
|
|
||
|
|
||
| static inline auto dot(const Vector2& a, const Vector2& b) noexcept -> float { return a.x * b.x + a.y * b.y; } | ||
| static inline auto cross(const Vector2& a, const Vector2& b) noexcept -> float { return a.x * b.y - a.y * b.x; } | ||
| static inline auto normalize(const Vector2& v) noexcept -> Vector2 { | ||
| float len = v.length(); | ||
| return (len > 0.0f) ? Vector2{v.x / len, v.y / len} : Vector2{0.0f, 0.0f}; | ||
| } | ||
| static inline auto lerp(const Vector2& a, const Vector2& b, float t) noexcept -> Vector2 { | ||
| return {a.x + t * (b.x - a.x), a.y + t * (b.y - a.y)}; | ||
| } | ||
| static inline auto min(const Vector2& a, const Vector2& b) noexcept -> Vector2 { | ||
| return {(a.x < b.x) ? a.x : b.x, (a.y < b.y) ? a.y : b.y}; | ||
| } | ||
| static inline auto max(const Vector2& a, const Vector2& b) noexcept -> Vector2 { | ||
| return {(a.x > b.x) ? a.x : b.x, (a.y > b.y) ? a.y : b.y}; | ||
| } | ||
| static inline auto perp(const Vector2& v) noexcept -> Vector2 { return {-v.y, v.x}; } | ||
| static inline auto reflect(const Vector2& v, const Vector2& n) noexcept -> Vector2 { | ||
| float d = 2.0f * dot(v, n); | ||
| return {v.x - d * n.x, v.y - d * n.y}; | ||
| } | ||
| static inline auto distance(const Vector2& a, const Vector2& b) noexcept -> float { | ||
| float dx = b.x - a.x; | ||
| float dy = b.y - a.y; | ||
| return std::sqrt(dx * dx + dy * dy); | ||
| } | ||
| }; // class Vector2 | ||
| } // namespace math | ||
| } // namespace gkit::math | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,18 +1,6 @@ | ||
| #include <gkit/math/vector2.hpp> | ||
|
|
||
| #include "gkit/math/vector2.hpp" | ||
|
|
||
| gkit::math::Vector2::Vector2(float v) noexcept : x(v), y(v) { } | ||
| gkit::math::Vector2::Vector2(float x, float y) noexcept : x(x), y(y) { } | ||
| gkit::math::Vector2::Vector2(const gkit::math::Vector2& other) noexcept : x(other.x), y(other.y) { } | ||
| gkit::math::Vector2::Vector2(const gkit::math::Vector2&& other) noexcept : x(other.x), y(other.y) { } | ||
|
|
||
|
|
||
| auto gkit::math::Vector2::normalization() -> void { | ||
| auto len = this->length(); | ||
| this->x /= len; this->y /= len; | ||
| } | ||
|
|
||
|
|
||
| auto gkit::math::Vector2::zero() -> const Vector2& { | ||
| static Vector2 zero = Vector2(0.0f, 0.0f); | ||
| return zero; | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,76 @@ | ||
| #include <cstdio> | ||
| #include <cassert> | ||
| #include "gkit/math/constants.hpp" | ||
| #include "gkit/math/scalar.hpp" | ||
|
|
||
| using namespace gkit::math; | ||
|
|
||
| int main() { | ||
| // ====== test scalar.hpp ====== | ||
|
|
||
| // ScalarLimits tests | ||
| assert(ScalarLimits<int32_t>::min_v == -2147483647 - 1); | ||
| assert(ScalarLimits<int32_t>::max_v == 2147483647); | ||
| assert(ScalarLimits<float>::epsilon_v > 0); | ||
|
|
||
| // fp constants tests | ||
| assert(EPSILON32 > 0); | ||
| assert(MIN32 > 0); | ||
| assert(MAX32 > MIN32); | ||
|
|
||
| // integer constants tests | ||
| assert(I8_MIN == -128); | ||
| assert(I8_MAX == 127); | ||
| assert(U8_MAX == 255); | ||
| assert(I16_MIN == -32768); | ||
| assert(I16_MAX == 32767); | ||
| assert(U16_MAX == 65535); | ||
|
|
||
| // ====== Test constants.hpp ====== | ||
|
|
||
| // Math constants tests | ||
| assert(gkit::math::PI_32 > 3.14f && gkit::math::PI_32 < 3.15f); | ||
| assert(gkit::math::TWO_PI_32 > 6.28f && gkit::math::TWO_PI_32 < 6.29f); | ||
| assert(gkit::math::HALF_PI_32 > 1.57f && gkit::math::HALF_PI_32 < 1.58f); | ||
| assert(gkit::math::INV_PI_32 > 0.31f && gkit::math::INV_PI_32 < 0.32f); | ||
| assert(gkit::math::E_32 > 2.71f && gkit::math::E_32 < 2.72f); | ||
| assert(gkit::math::PHI_32 > 1.61f && gkit::math::PHI_32 < 1.62f); | ||
| assert(gkit::math::SQRT_2_32 > 1.41f && gkit::math::SQRT_2_32 < 1.42f); | ||
| assert(gkit::math::LN_2_32 > 0.69f && gkit::math::LN_2_32 < 0.70f); | ||
|
|
||
| // Angle conversion tests | ||
| assert(gkit::math::DEG_TO_RAD_32 > 0.017f && gkit::math::DEG_TO_RAD_32 < 0.018f); | ||
| assert(gkit::math::RAD_TO_DEG_32 > 57.2f && gkit::math::RAD_TO_DEG_32 < 57.3f); | ||
|
|
||
| // Numeric constants tests | ||
| assert(gkit::math::ZERO_32 == 0.0f); | ||
| assert(gkit::math::ONE_32 == 1.0f); | ||
| assert(gkit::math::NEG_ONE_32 == -1.0f); | ||
| assert(gkit::math::TWO_32 == 2.0f); | ||
| assert(gkit::math::HALF_32 == 0.5f); | ||
| assert(gkit::math::QUARTER_32 == 0.25f); | ||
|
|
||
| assert(gkit::math::ZERO_I32 == 0); | ||
| assert(gkit::math::ONE_I32 == 1); | ||
| assert(gkit::math::NEG_ONE_I32 == -1); | ||
| assert(gkit::math::TWO_I32 == 2); | ||
|
|
||
| // Print test output | ||
| printf("=== scalar.hpp tests ===\n"); | ||
| printf("int32 min: %d, max: %d\n", | ||
| gkit::math::ScalarLimits<int32_t>::min_v, | ||
| gkit::math::ScalarLimits<int32_t>::max_v); | ||
| printf("float32 epsilon: %.10f\n", gkit::math::ScalarLimits<float>::epsilon_v); | ||
|
|
||
| printf("\n=== constants.hpp tests ===\n"); | ||
| printf("PI_32: %.10f\n", gkit::math::PI_32); | ||
| printf("TWO_PI_32: %.10f\n", gkit::math::TWO_PI_32); | ||
| printf("E_32: %.10f\n", gkit::math::E_32); | ||
| printf("PHI_32: %.10f\n", gkit::math::PHI_32); | ||
| printf("SQRT_2_32: %.10f\n", gkit::math::SQRT_2_32); | ||
| printf("DEG_TO_RAD_32: %.10f\n", gkit::math::DEG_TO_RAD_32); | ||
| printf("RAD_TO_DEG_32: %.10f\n", gkit::math::RAD_TO_DEG_32); | ||
|
|
||
| printf("\nAll tests passed!\n"); | ||
| return 0; | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.