src/geo3d/ds/p/lib.hpp
| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | #pragma once | ||
| 2 | |||
| 3 | #include "../../../util/traits/others/lib.hpp" | ||
| 4 | |||
| 5 | namespace tifa_libs { | ||
| 6 | |||
| 7 | template <class FP> | ||
| 8 | struct point3d { | ||
| 9 | using FP_t = FP; | ||
| 10 | FP x, y, z; | ||
| 11 | CEXP point3d() = default; | ||
| 12 | 8199 | CEXP point3d(FP x, FP y, FP z) NE : x{x}, y{y}, z{z} {} | |
| 13 | |||
| 14 | 450 | friend auto& operator>>(istream_c auto& is, point3d& p) NE { return is >> p.x >> p.y >> p.z; } | |
| 15 | friend auto& operator<<(ostream_c auto& os, point3d CR p) NE { return os << p.x << ' ' << p.y << ' ' << p.z; } | ||
| 16 | // s + (t - s) * r | ||
| 17 | template <std::floating_point T> | ||
| 18 | friend CEXP point3d lerp(point3d CR s, point3d CR t, T r) NE { return s + (t - s) * r; } | ||
| 19 | friend CEXP point3d mid_point(point3d CR s, point3d CR t) NE { return lerp(s, t, .5); } | ||
| 20 | CEXP point3d& operator+=(FP n) NE { | ||
| 21 | this->x += n, this->y += n, this->z += n; | ||
| 22 | return *this; | ||
| 23 | } | ||
| 24 | CEXP point3d& operator-=(FP n) NE { | ||
| 25 | this->x -= n, this->y -= n, this->z -= n; | ||
| 26 | return *this; | ||
| 27 | } | ||
| 28 | CEXP point3d& operator*=(FP n) NE { | ||
| 29 | this->x *= n, this->y *= n, this->z *= n; | ||
| 30 | return *this; | ||
| 31 | } | ||
| 32 | CEXP point3d& operator/=(FP n) NE { | ||
| 33 | this->x /= n, this->y /= n, this->z /= n; | ||
| 34 | return *this; | ||
| 35 | } | ||
| 36 | CEXP point3d operator+(FP n) CNE { return point3d(*this) += n; } | ||
| 37 | CEXP point3d operator-(FP n) CNE { return point3d(*this) -= n; } | ||
| 38 | CEXP point3d operator*(FP n) CNE { return point3d(*this) *= n; } | ||
| 39 | CEXP point3d operator/(FP n) CNE { return point3d(*this) /= n; } | ||
| 40 | CEXP point3d& operator+=(point3d CR p) NE { | ||
| 41 | this->x += p.x, this->y += p.y, this->z += p.z; | ||
| 42 | return *this; | ||
| 43 | } | ||
| 44 | 22610 | CEXP point3d& operator-=(point3d CR p) NE { | |
| 45 | 22610 | this->x -= p.x, this->y -= p.y, this->z -= p.z; | |
| 46 | 22610 | return *this; | |
| 47 | } | ||
| 48 | CEXP point3d operator+(point3d CR p) CNE { return point3d(*this) += p; } | ||
| 49 | 22610 | CEXP point3d operator-(point3d CR p) CNE { return point3d(*this) -= p; } | |
| 50 | CEXP point3d operator-() CNE { return point3d{-x, -y, -z}; } | ||
| 51 | 5699 | CEXP auto operator<=>(point3d CR p) CNE { | |
| 52 |
2/2✓ Branch 1 taken 3835 times.
✓ Branch 2 taken 1864 times.
|
5699 | if (auto c = comp(x, p.x); c) return c; |
| 53 |
2/2✓ Branch 1 taken 329 times.
✓ Branch 2 taken 1535 times.
|
1864 | if (auto c = comp(y, p.y); c) return c; |
| 54 | 1535 | return comp(z, p.z); | |
| 55 | } | ||
| 56 | 5699 | CEXP bool operator==(point3d CR p) CNE { return (*this <=> p) == 0; } | |
| 57 | 5834 | CEXP FP operator*(point3d CR p) CNE { return x * p.x + y * p.y + z * p.z; } | |
| 58 | 8199 | CEXP point3d operator^(point3d CR p) CNE { return point3d{y * p.z - z * p.y, z * p.x - x * p.z, x * p.y - y * p.x}; } | |
| 59 | CEXP auto norm2() CNE { return x * x + y * y + z * z; } | ||
| 60 | 2743 | CEXP auto norm() CNE { | |
| 61 | static_assert(std::floating_point<FP>); | ||
| 62 | 2743 | return std::hypot(x, y, z); | |
| 63 | } | ||
| 64 | CEXP point3d& do_unit() NE { return *this /= norm(); } | ||
| 65 | CEXP point3d& do_rotx(FP theta) NE { | ||
| 66 | const FP y0 = y, z0 = z, ct = std::cos(theta), st = std::sin(theta); | ||
| 67 | y = y0 * ct - z0 * st, z = y0 * st + z0 * ct; | ||
| 68 | return *this; | ||
| 69 | } | ||
| 70 | CEXP point3d& do_roty(FP theta) NE { | ||
| 71 | const FP x0 = x, z0 = z, ct = std::cos(theta), st = std::sin(theta); | ||
| 72 | z = z0 * ct - x0 * st, x = z0 * st + x0 * ct; | ||
| 73 | return *this; | ||
| 74 | } | ||
| 75 | CEXP point3d& do_rotz(FP theta) NE { | ||
| 76 | const FP x0 = x, y0 = y, ct = std::cos(theta), st = std::sin(theta); | ||
| 77 | x = x0 * ct - y0 * st, y = x0 * st + y0 * ct; | ||
| 78 | return *this; | ||
| 79 | } | ||
| 80 | CEXP point3d& do_rot(point3d e, FP theta) NE { | ||
| 81 | e.do_unit(); | ||
| 82 | const FP a = e.x, b = e.y, c = e.z, x0 = x, y0 = y, z0 = z, ct = std::cos(theta), st = std::sin(theta); | ||
| 83 | x = x0 * (ct + a * a * (1 - ct)) + y0 * (a * b * (1 - ct) - c * st) + z0 * (a * c * (1 - ct) + b * st); | ||
| 84 | y = x0 * (a * b * (1 - ct) + c * st) + y0 * (ct + b * b * (1 - ct)) + z0 * (b * c * (1 - ct) - a * st); | ||
| 85 | z = x0 * (a * c * (1 - ct) - b * st) + y0 * (b * c * (1 - ct) + a * st) + z0 * (ct + c * c * (1 - ct)); | ||
| 86 | return *this; | ||
| 87 | } | ||
| 88 | }; | ||
| 89 | |||
| 90 | } // namespace tifa_libs | ||
| 91 |