[Bf-blender-cvs] [581d46112ab] soc-2021-adaptive-cloth: bli: float2x2: initial implementation

ishbosamiya noreply at git.blender.org
Mon Jul 19 17:35:43 CEST 2021


Commit: 581d46112abdfc238e06d13119d532be53739252
Author: ishbosamiya
Date:   Fri Jul 16 14:58:50 2021 +0530
Branches: soc-2021-adaptive-cloth
https://developer.blender.org/rB581d46112abdfc238e06d13119d532be53739252

bli: float2x2: initial implementation

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

M	source/blender/blenlib/BLI_float2.hh
A	source/blender/blenlib/BLI_float2x2.hh
M	source/blender/blenlib/BLI_math_matrix.h
M	source/blender/blenlib/CMakeLists.txt
M	source/blender/blenlib/intern/math_matrix.c

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

diff --git a/source/blender/blenlib/BLI_float2.hh b/source/blender/blenlib/BLI_float2.hh
index cf6e00ba938..c459c937fc7 100644
--- a/source/blender/blenlib/BLI_float2.hh
+++ b/source/blender/blenlib/BLI_float2.hh
@@ -29,6 +29,10 @@ struct float2 {
   {
   }
 
+  float2(const float (*ptr)[2]) : float2(static_cast<const float *>(ptr[0]))
+  {
+  }
+
   explicit float2(float value) : x(value), y(value)
   {
   }
diff --git a/source/blender/blenlib/BLI_float2x2.hh b/source/blender/blenlib/BLI_float2x2.hh
new file mode 100644
index 00000000000..087d9323e80
--- /dev/null
+++ b/source/blender/blenlib/BLI_float2x2.hh
@@ -0,0 +1,96 @@
+/*
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License
+ * as published by the Free Software Foundation; either version 2
+ * of the License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ */
+
+#pragma once
+
+#include "BLI_float2.hh"
+#include "BLI_math_matrix.h"
+
+namespace blender {
+
+struct float2x2 {
+  float values[2][2];
+
+  float2x2() = default;
+
+  float2x2(const float *matrix)
+  {
+    memcpy(values, matrix, sizeof(float) * 4);
+  }
+
+  float2x2(const float matrix[2][2]) : float2x2(static_cast<const float *>(matrix[0]))
+  {
+  }
+
+  static float2x2 identity()
+  {
+    float2x2 mat;
+    unit_m2(mat.values);
+    return mat;
+  }
+
+  operator float *()
+  {
+    return &values[0][0];
+  }
+
+  operator const float *() const
+  {
+    return &values[0][0];
+  }
+
+  using c_style_float2x2 = float[2][2];
+  c_style_float2x2 &ptr()
+  {
+    return values;
+  }
+
+  const c_style_float2x2 &ptr() const
+  {
+    return values;
+  }
+
+  friend float2x2 operator*(const float2x2 &a, const float2x2 &b)
+  {
+    float2x2 result;
+    mul_m2_m2m2(result.values, a.values, b.values);
+    return result;
+  }
+
+  friend float2 operator*(const float2x2 &m, const float2 &v)
+  {
+    float2 result;
+    mul_v2_m2v2(result, m.values, v);
+    return result;
+  }
+
+  friend float2 operator*(const float2x2 &m, const float (*v)[2])
+  {
+    return m * float2(v);
+  }
+
+  uint64_t hash() const
+  {
+    uint64_t h = 435109;
+    for (int i = 0; i < 4; i++) {
+      float value = (static_cast<const float *>(values[0]))[i];
+      h = h * 33 + *reinterpret_cast<const uint32_t *>(&value);
+    }
+    return h;
+  }
+};
+
+} /* namespace blender */
diff --git a/source/blender/blenlib/BLI_math_matrix.h b/source/blender/blenlib/BLI_math_matrix.h
index 54df88ca541..85ae63711c3 100644
--- a/source/blender/blenlib/BLI_math_matrix.h
+++ b/source/blender/blenlib/BLI_math_matrix.h
@@ -76,6 +76,7 @@ void madd_m4_m4m4fl(float R[4][4], const float A[4][4], const float B[4][4], con
 void sub_m3_m3m3(float R[3][3], const float A[3][3], const float B[3][3]);
 void sub_m4_m4m4(float R[4][4], const float A[4][4], const float B[4][4]);
 
+void mul_m2_m2m2(float R[2][2], const float A[2][2], const float B[2][2]);
 void mul_m3_m3m3(float R[3][3], const float A[3][3], const float B[3][3]);
 void mul_m4_m3m4(float R[4][4], const float A[3][3], const float B[4][4]);
 void mul_m4_m4m3(float R[4][4], const float A[4][4], const float B[3][3]);
@@ -89,6 +90,10 @@ void mul_m3_m4m4(float R[3][3], const float A[4][4], const float B[4][4]);
  * pre:  R <-- AR
  * post: R <-- RB
  */
+
+void mul_m2_m2m2_uniq(float R[2][2], const float A[2][2], const float B[2][2]);
+void mul_m2_m2_pre(float R[2][2], const float A[2][2]);
+void mul_m2_m2_post(float R[2][2], const float B[2][2]);
 void mul_m3_m3m3_uniq(float R[3][3], const float A[3][3], const float B[3][3]);
 void mul_m3_m3_pre(float R[3][3], const float A[3][3]);
 void mul_m3_m3_post(float R[3][3], const float B[3][3]);
diff --git a/source/blender/blenlib/CMakeLists.txt b/source/blender/blenlib/CMakeLists.txt
index 0e35ccaccf9..85c2925eabf 100644
--- a/source/blender/blenlib/CMakeLists.txt
+++ b/source/blender/blenlib/CMakeLists.txt
@@ -191,6 +191,7 @@ set(SRC
   BLI_fileops.h
   BLI_fileops_types.h
   BLI_float2.hh
+  BLI_float2x2.hh
   BLI_float3.hh
   BLI_float4x4.hh
   BLI_fnmatch.h
diff --git a/source/blender/blenlib/intern/math_matrix.c b/source/blender/blenlib/intern/math_matrix.c
index d447da4de64..af24b47dabb 100644
--- a/source/blender/blenlib/intern/math_matrix.c
+++ b/source/blender/blenlib/intern/math_matrix.c
@@ -25,6 +25,7 @@
 
 #include "BLI_math.h"
 
+#include "BLI_math_matrix.h"
 #include "BLI_strict_flags.h"
 
 #ifndef MATH_STANDALONE
@@ -259,6 +260,46 @@ void shuffle_m4(float R[4][4], const int index[4])
 
 /******************************** Arithmetic *********************************/
 
+void mul_m2_m2m2(float R[2][2], const float A[2][2], const float B[2][2])
+{
+  if (A == R) {
+    mul_m2_m2_post(R, B);
+  }
+  else if (B == R) {
+    mul_m2_m2_pre(R, A);
+  }
+  else {
+    mul_m2_m2m2_uniq(R, A, B);
+  }
+}
+
+void mul_m2_m2_pre(float R[2][2], const float A[2][2])
+{
+  BLI_assert(A != R);
+  float B[2][2];
+  copy_m2_m2(B, R);
+  mul_m2_m2m2_uniq(R, A, B);
+}
+
+void mul_m2_m2_post(float R[2][2], const float B[2][2])
+{
+  BLI_assert(B != R);
+  float A[2][2];
+  copy_m2_m2(A, R);
+  mul_m2_m2m2_uniq(R, A, B);
+}
+
+void mul_m2_m2m2_uniq(float R[2][2], const float A[2][2], const float B[2][2])
+{
+  BLI_assert(!ELEM(R, A, B));
+
+  R[0][0] = B[0][0] * A[0][0] + B[0][1] * A[1][0];
+  R[0][1] = B[0][0] * A[0][1] + B[0][1] * A[1][1];
+
+  R[1][0] = B[1][0] * A[0][0] + B[1][1] * A[1][0];
+  R[1][1] = B[1][0] * A[0][1] + B[1][1] * A[1][1];
+}
+
 void mul_m4_m4m4(float R[4][4], const float A[4][4], const float B[4][4])
 {
   if (A == R) {



More information about the Bf-blender-cvs mailing list