[Bf-blender-cvs] [3e251da3aa1] temp-experimental-cpp-math-refactor: update float2

Jacques Lucke noreply at git.blender.org
Mon Jan 11 18:40:52 CET 2021


Commit: 3e251da3aa11a153e4e7c8a5e576ff8e5b2c23c8
Author: Jacques Lucke
Date:   Mon Jan 11 13:11:46 2021 +0100
Branches: temp-experimental-cpp-math-refactor
https://developer.blender.org/rB3e251da3aa11a153e4e7c8a5e576ff8e5b2c23c8

update float2

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

M	source/blender/blenlib/BLI_float2.hh

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

diff --git a/source/blender/blenlib/BLI_float2.hh b/source/blender/blenlib/BLI_float2.hh
index d41c4d262d3..40cbc272102 100644
--- a/source/blender/blenlib/BLI_float2.hh
+++ b/source/blender/blenlib/BLI_float2.hh
@@ -18,7 +18,7 @@
 
 #include "BLI_float3.hh"
 
-namespace blender {
+namespace blender::math {
 
 struct float2 {
   float x, y;
@@ -47,39 +47,6 @@ struct float2 {
     return &x;
   }
 
-  float length() const
-  {
-    return len_v2(*this);
-  }
-
-  float2 &operator+=(const float2 &other)
-  {
-    x += other.x;
-    y += other.y;
-    return *this;
-  }
-
-  float2 &operator-=(const float2 &other)
-  {
-    x -= other.x;
-    y -= other.y;
-    return *this;
-  }
-
-  float2 &operator*=(float factor)
-  {
-    x *= factor;
-    y *= factor;
-    return *this;
-  }
-
-  float2 &operator/=(float divisor)
-  {
-    x /= divisor;
-    y /= divisor;
-    return *this;
-  }
-
   uint64_t hash() const
   {
     uint64_t x1 = *reinterpret_cast<const uint32_t *>(&x);
@@ -87,63 +54,6 @@ struct float2 {
     return (x1 * 812519) ^ (x2 * 707951);
   }
 
-  friend float2 operator+(const float2 &a, const float2 &b)
-  {
-    return {a.x + b.x, a.y + b.y};
-  }
-
-  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)
-  {
-    return {a.x * b, a.y * b};
-  }
-
-  friend float2 operator/(const float2 &a, float b)
-  {
-    BLI_assert(b != 0.0f);
-    return {a.x / b, a.y / b};
-  }
-
-  friend float2 operator*(float a, const float2 &b)
-  {
-    return b * a;
-  }
-
-  friend std::ostream &operator<<(std::ostream &stream, const float2 &v)
-  {
-    stream << "(" << v.x << ", " << v.y << ")";
-    return stream;
-  }
-
-  static float dot(const float2 &a, const float2 &b)
-  {
-    return a.x * b.x + a.y * b.y;
-  }
-
-  static float2 interpolate(const float2 &a, const float2 &b, float t)
-  {
-    return a * (1 - t) + b * t;
-  }
-
-  static float2 abs(const float2 &a)
-  {
-    return float2(fabsf(a.x), fabsf(a.y));
-  }
-
-  static float distance(const float2 &a, const float2 &b)
-  {
-    return (a - b).length();
-  }
-
-  static float distance_squared(const float2 &a, const float2 &b)
-  {
-    return float2::dot(a, b);
-  }
-
   struct isect_result {
     enum {
       LINE_LINE_COLINEAR = -1,
@@ -159,16 +69,110 @@ struct float2 {
                                     const float2 &v2,
                                     const float2 &v3,
                                     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
+inline bool operator==(const float2 &a, const float2 &b)
+{
+  return a.x == b.x && a.y == b.y;
+}
+
+inline bool operator!=(const float2 &a, const float2 &b)
+{
+  return !(a == b);
+}
+
+inline float2 &operator+=(float2 &a, const float2 &other)
+{
+  a.x += other.x;
+  a.y += other.y;
+  return a;
+}
+
+inline float2 &operator-=(float2 &a, const float2 &other)
+{
+  a.x -= other.x;
+  a.y -= other.y;
+  return a;
+}
+
+inline float2 &operator*=(float2 &a, float factor)
+{
+  a.x *= factor;
+  a.y *= factor;
+  return a;
+}
+
+inline float2 &operator/=(float2 &a, float divisor)
+{
+  a.x /= divisor;
+  a.y /= divisor;
+  return a;
+}
+
+inline float2 operator+(const float2 &a, const float2 &b)
+{
+  return {a.x + b.x, a.y + b.y};
+}
+
+inline float2 operator-(const float2 &a, const float2 &b)
+{
+  return {a.x - b.x, a.y - b.y};
+}
+
+inline float2 operator*(const float2 &a, float b)
+{
+  return {a.x * b, a.y * b};
+}
+
+inline float2 operator/(const float2 &a, float b)
+{
+  BLI_assert(b != 0.0f);
+  return {a.x / b, a.y / b};
+}
+
+inline float2 operator*(float a, const float2 &b)
+{
+  return b * a;
+}
+
+inline std::ostream &operator<<(std::ostream &stream, const float2 &v)
+{
+  stream << "(" << v.x << ", " << v.y << ")";
+  return stream;
+}
+
+inline float length(const float2 &a)
+{
+  return len_v2(a);
+}
+
+inline float dot(const float2 &a, const float2 &b)
+{
+  return a.x * b.x + a.y * b.y;
+}
+
+inline float2 lerp(const float2 &a, const float2 &b, float t)
+{
+  return a * (1 - t) + b * t;
+}
+
+inline float2 abs(const float2 &a)
+{
+  return float2(fabsf(a.x), fabsf(a.y));
+}
+
+inline float distance(const float2 &a, const float2 &b)
+{
+  return length(a - b);
+}
+
+inline float distance_squared(const float2 &a, const float2 &b)
+{
+  return dot(a, b);
+}
+
+}  // namespace blender::math
+
+namespace blender {
+using math::float2;
+}



More information about the Bf-blender-cvs mailing list