Changeset View
Changeset View
Standalone View
Standalone View
source/blender/blenlib/BLI_float3.hh
| Show All 12 Lines | |||||
| * 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 <iostream> | #include <iostream> | ||||
| #include "BLI_float2.hh" | |||||
| #include "BLI_math_vector.h" | #include "BLI_math_vector.h" | ||||
| namespace blender { | namespace blender { | ||||
| struct float3 { | struct float3 { | ||||
| float x, y, z; | float x, y, z; | ||||
| float3() = default; | float3() = default; | ||||
| Show All 13 Lines | struct float3 { | ||||
| explicit float3(int value) : x(value), y(value), z(value) | explicit float3(int value) : x(value), y(value), z(value) | ||||
| { | { | ||||
| } | } | ||||
| float3(float x, float y, float z) : x{x}, y{y}, z{z} | float3(float x, float y, float z) : x{x}, y{y}, z{z} | ||||
| { | { | ||||
| } | } | ||||
| float3(float2 xy, float z) : x(xy.x), y(xy.y), z(z) | |||||
| { | |||||
| } | |||||
| float3(float x, float2 yz) : x(x), y(yz.x), z(yz.y) | |||||
| { | |||||
| } | |||||
| /** Conversions. */ | |||||
| explicit operator float2() const | |||||
| { | |||||
| return float2(x, y); | |||||
| } | |||||
| operator const float *() const | operator const float *() const | ||||
| { | { | ||||
| return &x; | return &x; | ||||
| } | } | ||||
| operator float *() | operator float *() | ||||
| { | { | ||||
| return &x; | return &x; | ||||
| } | } | ||||
| /** Array access. */ | |||||
| const float &operator[](int64_t index) const | |||||
| { | |||||
| BLI_assert(index < 3); | |||||
| return (&x)[index]; | |||||
| } | |||||
| float &operator[](int64_t index) | |||||
| { | |||||
| BLI_assert(index < 3); | |||||
| return (&x)[index]; | |||||
| } | |||||
| /** Arithmetic. */ | |||||
| friend float3 operator+(const float3 &a, const float3 &b) | friend float3 operator+(const float3 &a, const float3 &b) | ||||
| { | { | ||||
| return {a.x + b.x, a.y + b.y, a.z + b.z}; | return {a.x + b.x, a.y + b.y, a.z + b.z}; | ||||
| } | } | ||||
| friend float3 operator+(const float3 &a, const float &b) | friend float3 operator+(const float3 &a, const float &b) | ||||
| { | { | ||||
| return {a.x + b, a.y + b, a.z + b}; | return {a.x + b, a.y + b, a.z + b}; | ||||
| } | } | ||||
| friend float3 operator+(const float &a, const float3 &b) | |||||
| { | |||||
| return b + a; | |||||
| } | |||||
| float3 &operator+=(const float3 &b) | float3 &operator+=(const float3 &b) | ||||
| { | { | ||||
| this->x += b.x; | x += b.x; | ||||
| this->y += b.y; | y += b.y; | ||||
| this->z += b.z; | z += b.z; | ||||
| return *this; | return *this; | ||||
| } | } | ||||
| friend float3 operator-(const float3 &a, const float3 &b) | float3 &operator+=(const float &b) | ||||
| { | { | ||||
| return {a.x - b.x, a.y - b.y, a.z - b.z}; | x += b; | ||||
| y += b; | |||||
| z += b; | |||||
| return *this; | |||||
| } | } | ||||
| friend float3 operator-(const float3 &a) | friend float3 operator-(const float3 &a) | ||||
| { | { | ||||
| return {-a.x, -a.y, -a.z}; | return {-a.x, -a.y, -a.z}; | ||||
| } | } | ||||
| friend float3 operator-(const float3 &a, const float3 &b) | |||||
| { | |||||
| return {a.x - b.x, a.y - b.y, a.z - b.z}; | |||||
| } | |||||
| friend float3 operator-(const float3 &a, const float &b) | friend float3 operator-(const float3 &a, const float &b) | ||||
| { | { | ||||
| return {a.x - b, a.y - b, a.z - b}; | return {a.x - b, a.y - b, a.z - b}; | ||||
| } | } | ||||
| float3 &operator-=(const float3 &b) | friend float3 operator-(const float &a, const float3 &b) | ||||
| { | { | ||||
| this->x -= b.x; | return {a - b.x, a - b.y, a - b.z}; | ||||
| this->y -= b.y; | |||||
| this->z -= b.z; | |||||
| return *this; | |||||
| } | } | ||||
| float3 &operator*=(float scalar) | float3 &operator-=(const float3 &b) | ||||
| { | { | ||||
| this->x *= scalar; | x -= b.x; | ||||
| this->y *= scalar; | y -= b.y; | ||||
| this->z *= scalar; | z -= b.z; | ||||
| return *this; | return *this; | ||||
| } | } | ||||
| float3 &operator*=(const float3 &other) | float3 &operator-=(const float &b) | ||||
| { | { | ||||
| this->x *= other.x; | x -= b; | ||||
| this->y *= other.y; | y -= b; | ||||
| this->z *= other.z; | z -= b; | ||||
| return *this; | return *this; | ||||
| } | } | ||||
| friend float3 operator*(const float3 &a, const float3 &b) | friend float3 operator*(const float3 &a, const float3 &b) | ||||
| { | { | ||||
| return {a.x * b.x, a.y * b.y, a.z * b.z}; | return {a.x * b.x, a.y * b.y, a.z * b.z}; | ||||
| } | } | ||||
| friend float3 operator*(const float3 &a, float b) | friend float3 operator*(const float3 &a, float b) | ||||
| { | { | ||||
| return {a.x * b, a.y * b, a.z * b}; | return {a.x * b, a.y * b, a.z * b}; | ||||
| } | } | ||||
| friend float3 operator*(float a, const float3 &b) | friend float3 operator*(float a, const float3 &b) | ||||
| { | { | ||||
| return b * a; | return b * a; | ||||
| } | } | ||||
| float3 &operator*=(float b) | |||||
| { | |||||
| x *= b; | |||||
| y *= b; | |||||
| z *= b; | |||||
| return *this; | |||||
| } | |||||
| float3 &operator*=(const float3 &b) | |||||
| { | |||||
| x *= b.x; | |||||
| y *= b.y; | |||||
| z *= b.z; | |||||
| return *this; | |||||
| } | |||||
| friend float3 operator/(const float3 &a, const float3 &b) | |||||
| { | |||||
| BLI_assert(b.x != 0.0f && b.y != 0.0f && b.z != 0.0f); | |||||
| return {a.x / b.x, a.y / b.y, a.z / b.z}; | |||||
| } | |||||
| friend float3 operator/(const float3 &a, float b) | friend float3 operator/(const float3 &a, float b) | ||||
| { | { | ||||
| BLI_assert(b != 0.0f); | BLI_assert(b != 0.0f); | ||||
| return {a.x / b, a.y / b, a.z / b}; | return {a.x / b, a.y / b, a.z / b}; | ||||
| } | } | ||||
| friend std::ostream &operator<<(std::ostream &stream, const float3 &v) | friend float3 operator/(float a, const float3 &b) | ||||
| { | { | ||||
| stream << "(" << v.x << ", " << v.y << ", " << v.z << ")"; | BLI_assert(b.x != 0.0f && b.y != 0.0f && b.z != 0.0f); | ||||
| return stream; | return {a / b.x, a / b.y, a / b.z}; | ||||
| } | } | ||||
| float3 &operator/=(float b) | |||||
| { | |||||
| BLI_assert(b != 0.0f); | |||||
| x /= b; | |||||
| y /= b; | |||||
| z /= b; | |||||
| return *this; | |||||
| } | |||||
| float3 &operator/=(const float3 &b) | |||||
| { | |||||
| BLI_assert(b.x != 0.0f && b.y != 0.0f && b.z != 0.0f); | |||||
| x /= b.x; | |||||
| y /= b.y; | |||||
| z /= b.z; | |||||
| return *this; | |||||
| } | |||||
| /** Compare. */ | |||||
| friend bool operator==(const float3 &a, const float3 &b) | friend bool operator==(const float3 &a, const float3 &b) | ||||
| { | { | ||||
| return a.x == b.x && a.y == b.y && a.z == b.z; | return a.x == b.x && a.y == b.y && a.z == b.z; | ||||
| } | } | ||||
| friend bool operator!=(const float3 &a, const float3 &b) | friend bool operator!=(const float3 &a, const float3 &b) | ||||
| { | { | ||||
| return !(a == b); | return !(a == b); | ||||
| } | } | ||||
| /** Print. */ | |||||
| friend std::ostream &operator<<(std::ostream &stream, const float3 &v) | |||||
| { | |||||
| stream << "(" << v.x << ", " << v.y << ", " << v.z << ")"; | |||||
| return stream; | |||||
| } | |||||
| float normalize_and_get_length() | float normalize_and_get_length() | ||||
| { | { | ||||
| return normalize_v3(*this); | return normalize_v3(*this); | ||||
| } | } | ||||
| /** | /** | ||||
| * Normalizes the vector in place. | * Normalizes the vector in place. | ||||
| */ | */ | ||||
| ▲ Show 20 Lines • Show All 159 Lines • Show Last 20 Lines | |||||