Changeset View
Changeset View
Standalone View
Standalone View
source/blender/blenlib/BLI_float4.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 <iostream> | |||||
| #include "BLI_float2.hh" | |||||
| #include "BLI_float3.hh" | |||||
| #include "BLI_math_vector.h" | |||||
| namespace blender { | namespace blender { | ||||
| struct float4 { | struct float4 { | ||||
| float x, y, z, w; | float x, y, z, w; | ||||
| float4() = default; | float4() = default; | ||||
| float4(const float *ptr) : x{ptr[0]}, y{ptr[1]}, z{ptr[2]}, w{ptr[3]} | float4(const float *ptr) : x{ptr[0]}, y{ptr[1]}, z{ptr[2]}, w{ptr[3]} | ||||
| { | { | ||||
| } | } | ||||
| float4(const float (*ptr)[4]) : float4(static_cast<const float *>(ptr[0])) | |||||
| { | |||||
| } | |||||
| explicit float4(float value) : x(value), y(value), z(value), w(value) | explicit float4(float value) : x(value), y(value), z(value), w(value) | ||||
| { | { | ||||
| } | } | ||||
| explicit float4(int value) : x(value), y(value), z(value), w(value) | explicit float4(int value) : x(value), y(value), z(value), w(value) | ||||
| { | { | ||||
| } | } | ||||
| float4(float x, float y, float z, float w) : x(x), y(y), z(z), w(w) | explicit float4(float x, float y, float z, float w) : x(x), y(y), z(z), w(w) | ||||
| { | |||||
| } | |||||
| explicit float4(float3 xyz, float w) : x(xyz.x), y(xyz.y), z(xyz.z), w(w) | |||||
| { | |||||
| } | |||||
| explicit float4(float x, float3 yzw) : x(x), y(yzw.x), z(yzw.y), w(yzw.z) | |||||
| { | |||||
| } | |||||
| explicit float4(float2 xy, float2 zw) : x(xy.x), y(xy.y), z(zw.x), w(zw.y) | |||||
| { | |||||
| } | |||||
| explicit float4(float2 xy, float z, float w) : x(xy.x), y(xy.y), z(z), w(w) | |||||
| { | |||||
| } | |||||
| explicit float4(float x, float2 yz, float w) : x(x), y(yz.x), z(yz.y), w(w) | |||||
| { | |||||
| } | |||||
| explicit float4(float x, float y, float2 zw) : x(x), y(y), z(zw.x), w(zw.y) | |||||
| { | { | ||||
| } | } | ||||
| /** Conversions. */ | |||||
| explicit operator float2() const | |||||
| { | |||||
| return float2(x, y); | |||||
| } | |||||
| explicit operator float3() const | |||||
| { | |||||
| return float3(x, y, z); | |||||
| } | |||||
| operator const float *() const | |||||
| { | |||||
| return &x; | |||||
| } | |||||
| operator float *() | operator float *() | ||||
| { | { | ||||
| return &x; | return &x; | ||||
| } | } | ||||
| /** Arithmetic. */ | |||||
| friend float4 operator+(const float4 &a, const float4 &b) | |||||
| { | |||||
| return {a.x + b.x, a.y + b.y, a.z + b.z, a.w + b.w}; | |||||
| } | |||||
| friend float4 operator+(const float4 &a, const float &b) | friend float4 operator+(const float4 &a, const float &b) | ||||
| { | { | ||||
| return {a.x + b, a.y + b, a.z + b, a.w + b}; | return {a.x + b, a.y + b, a.z + b, a.w + b}; | ||||
| } | } | ||||
| operator const float *() const | friend float4 operator+(const float &a, const float4 &b) | ||||
| { | { | ||||
| return &x; | return b + a; | ||||
| } | |||||
| float4 &operator+=(const float4 &b) | |||||
| { | |||||
| x += b.x, y += b.y, z += b.z, w += b.w; | |||||
| return *this; | |||||
| } | } | ||||
| float4 &operator+=(const float4 &other) | float4 &operator+=(const float &b) | ||||
| { | { | ||||
| x += other.x; | x += b, y += b, z += b, w += b; | ||||
| y += other.y; | |||||
| z += other.z; | |||||
| w += other.w; | |||||
| return *this; | return *this; | ||||
| } | } | ||||
| friend float4 operator-(const float4 &a) | |||||
| { | |||||
| return {-a.x, -a.y, -a.z, -a.w}; | |||||
| } | |||||
| friend float4 operator-(const float4 &a, const float4 &b) | friend float4 operator-(const float4 &a, const float4 &b) | ||||
| { | { | ||||
| return {a.x - b.x, a.y - b.y, a.z - b.z, a.w - b.w}; | return {a.x - b.x, a.y - b.y, a.z - b.z, a.w - b.w}; | ||||
| } | } | ||||
| friend float4 operator-(const float4 &a, const float &b) | friend float4 operator-(const float4 &a, const float &b) | ||||
| { | { | ||||
| return {a.x - b, a.y - b, a.z - b, a.w - b}; | return {a.x - b, a.y - b, a.z - b, a.w - b}; | ||||
| } | } | ||||
| friend float4 operator+(const float4 &a, const float4 &b) | friend float4 operator-(const float &a, const float4 &b) | ||||
| { | { | ||||
| return {a.x + b.x, a.y + b.y, a.z + b.z, a.w + b.w}; | return {a - b.x, a - b.y, a - b.z, a - b.w}; | ||||
| } | } | ||||
| friend float4 operator/(const float4 &a, float f) | float4 &operator-=(const float4 &b) | ||||
| { | { | ||||
| BLI_assert(f != 0.0f); | x -= b.x, y -= b.y, z -= b.z, w -= b.w; | ||||
| return a * (1.0f / f); | return *this; | ||||
| } | } | ||||
| float4 &operator*=(float factor) | float4 &operator-=(const float &b) | ||||
| { | { | ||||
| x *= factor; | x -= b, y -= b, z -= b, w -= b; | ||||
| y *= factor; | |||||
| z *= factor; | |||||
| w *= factor; | |||||
| return *this; | return *this; | ||||
| } | } | ||||
| friend float4 operator*(const float4 &a, const float4 &b) | |||||
| { | |||||
| return {a.x * b.x, a.y * b.y, a.z * b.z, a.w * b.w}; | |||||
| } | |||||
| friend float4 operator*(const float4 &a, float b) | friend float4 operator*(const float4 &a, float b) | ||||
| { | { | ||||
| return {a.x * b, a.y * b, a.z * b, a.w * b}; | return {a.x * b, a.y * b, a.z * b, a.w * b}; | ||||
| } | } | ||||
| friend float4 operator*(float a, const float4 &b) | friend float4 operator*(float a, const float4 &b) | ||||
| { | { | ||||
| return b * a; | return b * a; | ||||
| } | } | ||||
| float4 &operator*=(float b) | |||||
| { | |||||
| x *= b, y *= b, z *= b, w *= b; | |||||
| return *this; | |||||
| } | |||||
| float4 &operator*=(const float4 &b) | |||||
| { | |||||
| x *= b.x, y *= b.y, z *= b.z, w *= b.w; | |||||
| return *this; | |||||
| } | |||||
| friend float4 operator/(const float4 &a, const float4 &b) | |||||
| { | |||||
| BLI_assert(b.x != 0.0f && b.y != 0.0f && b.z != 0.0f && b.w != 0.0f); | |||||
| return {a.x / b.x, a.y / b.y, a.z / b.z, a.w / b.w}; | |||||
| } | |||||
| friend float4 operator/(const float4 &a, float b) | |||||
| { | |||||
| BLI_assert(b != 0.0f); | |||||
| return {a.x / b, a.y / b, a.z / b, a.w / b}; | |||||
| } | |||||
| friend float4 operator/(float a, const float4 &b) | |||||
| { | |||||
| BLI_assert(b.x != 0.0f && b.y != 0.0f && b.z != 0.0f && b.w != 0.0f); | |||||
| return {a / b.x, a / b.y, a / b.z, a / b.w}; | |||||
| } | |||||
| float4 &operator/=(float b) | |||||
| { | |||||
| BLI_assert(b != 0.0f); | |||||
| x /= b, y /= b, z /= b, w /= b; | |||||
| return *this; | |||||
| } | |||||
| float4 &operator/=(const float4 &b) | |||||
| { | |||||
| BLI_assert(b.x != 0.0f && b.y != 0.0f && b.z != 0.0f && b.w != 0.0f); | |||||
| x /= b.x, y /= b.y, z /= b.z, w /= b.w; | |||||
| return *this; | |||||
| } | |||||
| /** Compare. */ | |||||
| friend bool operator==(const float4 &a, const float4 &b) | |||||
| { | |||||
| return a.x == b.x && a.y == b.y && a.z == b.z && a.w == b.w; | |||||
| } | |||||
| friend bool operator!=(const float4 &a, const float4 &b) | |||||
| { | |||||
| return !(a == b); | |||||
| } | |||||
| /** Print. */ | |||||
| friend std::ostream &operator<<(std::ostream &stream, const float4 &v) | |||||
| { | |||||
| stream << "(" << v.x << ", " << v.y << ", " << v.z << ", " << v.w << ")"; | |||||
| return stream; | |||||
| } | |||||
| float length() const | float length() const | ||||
| { | { | ||||
| return len_v4(*this); | return len_v4(*this); | ||||
| } | } | ||||
| static float distance(const float4 &a, const float4 &b) | static float distance(const float4 &a, const float4 &b) | ||||
| { | { | ||||
| return (a - b).length(); | return (a - b).length(); | ||||
| Show All 25 Lines | |||||