[Bf-blender-cvs] [34115114999] functions: new float2 and rgba_b types

Jacques Lucke noreply at git.blender.org
Fri Aug 2 19:14:45 CEST 2019


Commit: 3411511499927dc063b528b1ab66d4550b929c08
Author: Jacques Lucke
Date:   Fri Aug 2 12:52:40 2019 +0200
Branches: functions
https://developer.blender.org/rB3411511499927dc063b528b1ab66d4550b929c08

new float2 and rgba_b types

===================================================================

M	source/blender/blenlib/BLI_math.hpp

===================================================================

diff --git a/source/blender/blenlib/BLI_math.hpp b/source/blender/blenlib/BLI_math.hpp
index b2e6243cf48..6240fb4df00 100644
--- a/source/blender/blenlib/BLI_math.hpp
+++ b/source/blender/blenlib/BLI_math.hpp
@@ -7,6 +7,73 @@
 
 namespace BLI {
 
+struct float2;
+struct float3;
+struct rgba_f;
+struct rgba_b;
+
+struct float2 {
+  float x, y;
+
+  float2() = default;
+
+  float2(const float *ptr) : x{ptr[0]}, y{ptr[1]}
+  {
+  }
+
+  float2(float x, float y) : x(x), y(y)
+  {
+  }
+
+  float2(float3 other);
+
+  operator float *()
+  {
+    return &x;
+  }
+
+  float2 clamped(float min, float max)
+  {
+    return {std::min(std::max(x, min), max), std::min(std::max(y, min), max)};
+  }
+
+  float2 clamped_01()
+  {
+    return this->clamped(0, 1);
+  }
+
+  friend float2 operator+(float2 a, float2 b)
+  {
+    return {a.x + b.x, a.y + b.y};
+  }
+
+  friend float2 operator-(float2 a, float2 b)
+  {
+    return {a.x - b.x, a.y - b.y};
+  }
+
+  friend float2 operator*(float2 a, float b)
+  {
+    return {a.x * b, a.y * b};
+  }
+
+  friend float2 operator/(float2 a, float b)
+  {
+    return {a.x / b, a.y / b};
+  }
+
+  friend float2 operator*(float a, float2 b)
+  {
+    return b * a;
+  }
+
+  friend std::ostream &operator<<(std::ostream &stream, float2 v)
+  {
+    stream << "(" << v.x << ", " << v.y << ")";
+    return stream;
+  }
+};
+
 struct float3 {
   float x, y, z;
 
@@ -239,6 +306,46 @@ struct rgba_f {
   {
     return {r, g, b, a};
   }
+
+  friend std::ostream &operator<<(std::ostream &stream, rgba_f c)
+  {
+    stream << "(" << c.r << ", " << c.g << ", " << c.b << ", " << c.a << ")";
+    return stream;
+  }
+};
+
+struct rgba_b {
+  uint8_t r, g, b, a;
+
+  rgba_b() = default;
+
+  rgba_b(uint8_t r, uint8_t g, uint8_t b, uint8_t a) : r(r), g(g), b(b), a(a)
+  {
+  }
+
+  rgba_b(rgba_f other)
+  {
+    rgba_float_to_uchar(*this, other);
+  }
+
+  operator rgba_f()
+  {
+    rgba_f result;
+    rgba_uchar_to_float(result, *this);
+    return result;
+  }
+
+  operator uint8_t *()
+  {
+    return &r;
+  }
 };
 
+/* Conversions
+ *****************************************/
+
+inline float2::float2(float3 other) : x(other.x), y(other.y)
+{
+}
+
 }  // namespace BLI



More information about the Bf-blender-cvs mailing list