Changeset View
Changeset View
Standalone View
Standalone View
source/blender/blenlib/BLI_float2.hh
| Show All 10 Lines | |||||
| * | * | ||||
| * You should have received a copy of the GNU General Public License | * You should have received a copy of the GNU General Public License | ||||
| * along with this program; if not, write to the Free Software Foundation, | * along with this program; if not, write to the Free Software Foundation, | ||||
| * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. | * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. | ||||
| */ | */ | ||||
| #pragma once | #pragma once | ||||
| #include "BLI_float3.hh" | #include <iostream> | ||||
| #include "BLI_math_vector.h" | |||||
| namespace blender { | namespace blender { | ||||
| struct float2 { | struct float2 { | ||||
| float x, y; | float x, y; | ||||
| float2() = default; | float2() = default; | ||||
| float2(const float *ptr) : x{ptr[0]}, y{ptr[1]} | float2(const float *ptr) : x{ptr[0]}, y{ptr[1]} | ||||
| { | { | ||||
| } | } | ||||
| float2(const float (*ptr)[2]) : float2(static_cast<const float *>(ptr[0])) | |||||
| { | |||||
| } | |||||
| explicit float2(float value) : x(value), y(value) | explicit float2(float value) : x(value), y(value) | ||||
| { | { | ||||
| } | } | ||||
| explicit float2(int value) : x(value), y(value) | explicit float2(int value) : x(value), y(value) | ||||
| { | { | ||||
| } | } | ||||
| float2(float x, float y) : x(x), y(y) | float2(float x, float y) : x(x), y(y) | ||||
| { | { | ||||
| } | } | ||||
| float2(const float3 &other) : x(other.x), y(other.y) | /** Conversions. */ | ||||
| operator const float *() const | |||||
| { | { | ||||
| return &x; | |||||
| } | } | ||||
| operator float *() | operator float *() | ||||
| { | { | ||||
| return &x; | return &x; | ||||
| } | } | ||||
| operator const float *() const | /** Array access. */ | ||||
| const float &operator[](int64_t index) const | |||||
| { | { | ||||
| return &x; | BLI_assert(index < 3); | ||||
| return (&x)[index]; | |||||
| } | } | ||||
| float length() const | float &operator[](int64_t index) | ||||
| { | { | ||||
| return len_v2(*this); | BLI_assert(index < 3); | ||||
| return (&x)[index]; | |||||
| } | } | ||||
| float length_squared() const | /** Arithmetic. */ | ||||
| friend float2 operator+(const float2 &a, const float2 &b) | |||||
| { | { | ||||
| return len_squared_v2(*this); | return {a.x + b.x, a.y + b.y}; | ||||
| } | } | ||||
| bool is_zero() const | friend float2 operator+(const float2 &a, const float &b) | ||||
| { | { | ||||
| return this->x == 0.0f && this->y == 0.0f; | return {a.x + b, a.y + b}; | ||||
| } | } | ||||
| float2 &operator+=(const float2 &other) | friend float2 operator+(const float &a, const float2 &b) | ||||
| { | { | ||||
| x += other.x; | return b + a; | ||||
| y += other.y; | |||||
| return *this; | |||||
| } | } | ||||
| float2 &operator-=(const float2 &other) | float2 &operator+=(const float2 &b) | ||||
| { | { | ||||
| x -= other.x; | x += b.x; | ||||
| y -= other.y; | y += b.y; | ||||
| return *this; | return *this; | ||||
| } | } | ||||
| float2 &operator*=(float factor) | float2 &operator+=(const float &b) | ||||
| { | { | ||||
| x *= factor; | x += b; | ||||
| y *= factor; | y += b; | ||||
| return *this; | return *this; | ||||
| } | } | ||||
| float2 &operator/=(float divisor) | friend float2 operator-(const float2 &a) | ||||
| { | { | ||||
| x /= divisor; | return {-a.x, -a.y}; | ||||
| y /= divisor; | |||||
| return *this; | |||||
| } | } | ||||
| uint64_t hash() const | friend float2 operator-(const float2 &a, const float2 &b) | ||||
| { | { | ||||
| uint64_t x1 = *reinterpret_cast<const uint32_t *>(&x); | return {a.x - b.x, a.y - b.y}; | ||||
| uint64_t x2 = *reinterpret_cast<const uint32_t *>(&y); | |||||
| return (x1 * 812519) ^ (x2 * 707951); | |||||
| } | } | ||||
| friend float2 operator+(const float2 &a, const float2 &b) | friend float2 operator-(const float2 &a, const float &b) | ||||
| { | { | ||||
| return {a.x + b.x, a.y + b.y}; | return {a.x - b, a.y - b}; | ||||
| } | } | ||||
| friend float2 operator-(const float2 &a, const float2 &b) | friend float2 operator-(const float &a, const float2 &b) | ||||
| { | { | ||||
| return {a.x - b.x, a.y - b.y}; | return {a - b.x, a - b.y}; | ||||
| } | } | ||||
| friend float2 operator-(const float2 &a, const float &b) | float2 &operator-=(const float2 &b) | ||||
| { | { | ||||
| return {a.x - b, a.y - b}; | x -= b.x; | ||||
| y -= b.y; | |||||
| return *this; | |||||
| } | |||||
| float2 &operator-=(const float &b) | |||||
| { | |||||
| x -= b; | |||||
| y -= b; | |||||
| return *this; | |||||
| } | |||||
| friend float2 operator*(const float2 &a, const float2 &b) | |||||
| { | |||||
| return {a.x * b.x, a.y * b.y}; | |||||
| } | } | ||||
| friend float2 operator*(const float2 &a, float b) | friend float2 operator*(const float2 &a, float b) | ||||
| { | { | ||||
| return {a.x * b, a.y * b}; | return {a.x * b, a.y * b}; | ||||
| } | } | ||||
| friend float2 operator*(float a, const float2 &b) | |||||
| { | |||||
| return b * a; | |||||
| } | |||||
| float2 &operator*=(float b) | |||||
| { | |||||
| x *= b; | |||||
| y *= b; | |||||
| return *this; | |||||
| } | |||||
| float2 &operator*=(const float2 &b) | |||||
| { | |||||
| x *= b.x; | |||||
| y *= b.y; | |||||
| return *this; | |||||
| } | |||||
| friend float2 operator/(const float2 &a, const float2 &b) | |||||
| { | |||||
| BLI_assert(b.x != 0.0f && b.y != 0.0f); | |||||
| return {a.x / b.x, a.y / b.y}; | |||||
| } | |||||
| friend float2 operator/(const float2 &a, float b) | friend float2 operator/(const float2 &a, float b) | ||||
| { | { | ||||
| BLI_assert(b != 0.0f); | BLI_assert(b != 0.0f); | ||||
| return {a.x / b, a.y / b}; | return {a.x / b, a.y / b}; | ||||
| } | } | ||||
| friend float2 operator*(float a, const float2 &b) | friend float2 operator/(float a, const float2 &b) | ||||
| { | { | ||||
| return b * a; | BLI_assert(b.x != 0.0f && b.y != 0.0f); | ||||
| return {a / b.x, a / b.y}; | |||||
| } | } | ||||
| float2 &operator/=(float b) | |||||
| { | |||||
| BLI_assert(b != 0.0f); | |||||
| x /= b; | |||||
| y /= b; | |||||
| return *this; | |||||
| } | |||||
| float2 &operator/=(const float2 &b) | |||||
| { | |||||
| BLI_assert(b.x != 0.0f && b.y != 0.0f); | |||||
| x /= b.x; | |||||
| y /= b.y; | |||||
| return *this; | |||||
| } | |||||
| /** Compare. */ | |||||
| friend bool operator==(const float2 &a, const float2 &b) | |||||
| { | |||||
| return a.x == b.x && a.y == b.y; | |||||
| } | |||||
| friend bool operator!=(const float2 &a, const float2 &b) | |||||
| { | |||||
| return !(a == b); | |||||
| } | |||||
| /** Print. */ | |||||
| friend std::ostream &operator<<(std::ostream &stream, const float2 &v) | friend std::ostream &operator<<(std::ostream &stream, const float2 &v) | ||||
| { | { | ||||
| stream << "(" << v.x << ", " << v.y << ")"; | stream << "(" << v.x << ", " << v.y << ")"; | ||||
| return stream; | return stream; | ||||
| } | } | ||||
| uint64_t hash() const | |||||
| { | |||||
| uint64_t x1 = *reinterpret_cast<const uint32_t *>(&x); | |||||
| uint64_t x2 = *reinterpret_cast<const uint32_t *>(&y); | |||||
| return (x1 * 812519) ^ (x2 * 707951); | |||||
| } | |||||
| static float2 safe_divide(const float2 &a, const float b) | static float2 safe_divide(const float2 &a, const float b) | ||||
| { | { | ||||
| return (b != 0.0f) ? a / b : float2(0.0f); | return (b != 0.0f) ? a / b : float2(0.0f); | ||||
| } | } | ||||
| static float2 floor(const float2 &a) | static float2 floor(const float2 &a) | ||||
| { | { | ||||
| return float2(floorf(a.x), floorf(a.y)); | return float2(floorf(a.x), floorf(a.y)); | ||||
| } | } | ||||
| float length() const | |||||
| { | |||||
| return len_v2(*this); | |||||
| } | |||||
| float length_squared() const | |||||
| { | |||||
| return len_squared_v2(*this); | |||||
| } | |||||
| bool is_zero() const | |||||
| { | |||||
| return this->x == 0.0f && this->y == 0.0f; | |||||
| } | |||||
| /** | /** | ||||
| * Returns a normalized vector. The original vector is not changed. | * Returns a normalized vector. The original vector is not changed. | ||||
| */ | */ | ||||
| float2 normalized() const | float2 normalized() const | ||||
| { | { | ||||
| float2 result; | float2 result; | ||||
| normalize_v2_v2(result, *this); | normalize_v2_v2(result, *this); | ||||
| return result; | return result; | ||||
| Show All 35 Lines | struct isect_result { | ||||
| float lambda; | float lambda; | ||||
| float mu; | float mu; | ||||
| }; | }; | ||||
| static isect_result isect_seg_seg(const float2 &v1, | static isect_result isect_seg_seg(const float2 &v1, | ||||
| const float2 &v2, | const float2 &v2, | ||||
| const float2 &v3, | const float2 &v3, | ||||
| const float2 &v4); | const float2 &v4); | ||||
| friend bool operator==(const float2 &a, const float2 &b) | |||||
| { | |||||
| return a.x == b.x && a.y == b.y; | |||||
| } | |||||
| friend bool operator!=(const float2 &a, const float2 &b) | |||||
| { | |||||
| return !(a == b); | |||||
| } | |||||
| }; | }; | ||||
| } // namespace blender | } // namespace blender | ||||