[Bf-blender-cvs] [875c2ea5b5e] master: Using relative threshold for floats in mesh comparison

Himanshi Kalra noreply at git.blender.org
Mon Aug 23 21:21:38 CEST 2021


Commit: 875c2ea5b5ee8425c3bc969dbaba3fab3ecbaab4
Author: Himanshi Kalra
Date:   Tue Aug 24 00:49:36 2021 +0530
Branches: master
https://developer.blender.org/rB875c2ea5b5ee8425c3bc969dbaba3fab3ecbaab4

Using relative threshold for floats in mesh comparison

Changes the threshold comparison from absolute to relative.
Removes threshold for MLoopCol comparison.

Adds a compare relative threshold function.

Reviewed By: JacquesLucke

Differential Revision: https://developer.blender.org/D12273

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

M	source/blender/blenkernel/intern/mesh.c
M	source/blender/blenlib/BLI_math_base.h
M	source/blender/blenlib/intern/math_base_inline.c

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

diff --git a/source/blender/blenkernel/intern/mesh.c b/source/blender/blenkernel/intern/mesh.c
index eb8e6aad736..e3650e03c8a 100644
--- a/source/blender/blenkernel/intern/mesh.c
+++ b/source/blender/blenkernel/intern/mesh.c
@@ -466,8 +466,10 @@ static int customdata_compare(
           int vtot = m1->totvert;
 
           for (j = 0; j < vtot; j++, v1++, v2++) {
-            if (len_squared_v3v3(v1->co, v2->co) > thresh_sq) {
-              return MESHCMP_VERTCOMISMATCH;
+            for (int k = 0; k < 3; k++) {
+              if (compare_threshold_relative(v1->co[k], v2->co[k], thresh)) {
+                return MESHCMP_VERTCOMISMATCH;
+              }
             }
             /* I don't care about normals, let's just do coordinates. */
           }
@@ -547,8 +549,7 @@ static int customdata_compare(
           int ltot = m1->totloop;
 
           for (j = 0; j < ltot; j++, lp1++, lp2++) {
-            if (abs(lp1->r - lp2->r) > thresh || abs(lp1->g - lp2->g) > thresh ||
-                abs(lp1->b - lp2->b) > thresh || abs(lp1->a - lp2->a) > thresh) {
+            if (lp1->r != lp2->r || lp1->g != lp2->g || lp1->b != lp2->b || lp1->a != lp2->a) {
               return MESHCMP_LOOPCOLMISMATCH;
             }
           }
@@ -583,7 +584,7 @@ static int customdata_compare(
           const float *l2_data = l2->data;
 
           for (int i = 0; i < total_length; i++) {
-            if (fabsf(l1_data[i] - l2_data[i]) > thresh) {
+            if (compare_threshold_relative(l1_data[i], l2_data[i], thresh)) {
               return MESHCMP_ATTRIBUTE_VALUE_MISMATCH;
             }
           }
@@ -594,7 +595,10 @@ static int customdata_compare(
           const float(*l2_data)[2] = l2->data;
 
           for (int i = 0; i < total_length; i++) {
-            if (len_squared_v2v2(l1_data[i], l2_data[i]) > thresh_sq) {
+            if (compare_threshold_relative(l1_data[i][0], l2_data[i][0], thresh)) {
+              return MESHCMP_ATTRIBUTE_VALUE_MISMATCH;
+            }
+            if (compare_threshold_relative(l1_data[i][1], l2_data[i][1], thresh)) {
               return MESHCMP_ATTRIBUTE_VALUE_MISMATCH;
             }
           }
@@ -605,7 +609,13 @@ static int customdata_compare(
           const float(*l2_data)[3] = l2->data;
 
           for (int i = 0; i < total_length; i++) {
-            if (len_squared_v3v3(l1_data[i], l2_data[i]) > thresh_sq) {
+            if (compare_threshold_relative(l1_data[i][0], l2_data[i][0], thresh)) {
+              return MESHCMP_ATTRIBUTE_VALUE_MISMATCH;
+            }
+            if (compare_threshold_relative(l1_data[i][1], l2_data[i][1], thresh)) {
+              return MESHCMP_ATTRIBUTE_VALUE_MISMATCH;
+            }
+            if (compare_threshold_relative(l1_data[i][2], l2_data[i][2], thresh)) {
               return MESHCMP_ATTRIBUTE_VALUE_MISMATCH;
             }
           }
@@ -639,7 +649,7 @@ static int customdata_compare(
 
           for (int i = 0; i < total_length; i++) {
             for (j = 0; j < 4; j++) {
-              if (fabsf(l1_data[i].color[j] - l2_data[i].color[j]) > thresh) {
+              if (compare_threshold_relative(l1_data[i].color[j], l2_data[i].color[j], thresh)) {
                 return MESHCMP_ATTRIBUTE_VALUE_MISMATCH;
               }
             }
diff --git a/source/blender/blenlib/BLI_math_base.h b/source/blender/blenlib/BLI_math_base.h
index dbdd28766a5..9b54f780296 100644
--- a/source/blender/blenlib/BLI_math_base.h
+++ b/source/blender/blenlib/BLI_math_base.h
@@ -172,6 +172,9 @@ MINLINE size_t clamp_z(size_t value, size_t min, size_t max);
 
 MINLINE int compare_ff(float a, float b, const float max_diff);
 MINLINE int compare_ff_relative(float a, float b, const float max_diff, const int max_ulps);
+MINLINE bool compare_threshold_relative(const float value1,
+                                        const float value2,
+                                        const float thresh);
 
 MINLINE float signf(float f);
 MINLINE int signum_i_ex(float a, float eps);
diff --git a/source/blender/blenlib/intern/math_base_inline.c b/source/blender/blenlib/intern/math_base_inline.c
index 983fd3b6543..49f9faf1704 100644
--- a/source/blender/blenlib/intern/math_base_inline.c
+++ b/source/blender/blenlib/intern/math_base_inline.c
@@ -656,6 +656,18 @@ MINLINE int compare_ff_relative(float a, float b, const float max_diff, const in
   return ((ua.i < 0) != (ub.i < 0)) ? 0 : (abs(ua.i - ub.i) <= max_ulps) ? 1 : 0;
 }
 
+MINLINE bool compare_threshold_relative(const float value1, const float value2, const float thresh)
+{
+  const float abs_diff = fabsf(value1 - value2);
+  /* Avoid letting the threshold get too small just because the values happen to be close to zero.
+   */
+  if (fabsf(value2) < 1) {
+    return abs_diff > thresh;
+  }
+  /* Using relative threshold in general. */
+  return abs_diff > thresh * fabsf(value2);
+}
+
 MINLINE float signf(float f)
 {
   return (f < 0.0f) ? -1.0f : 1.0f;



More information about the Bf-blender-cvs mailing list