[Bf-blender-cvs] [f9ebd17b4b1] master: Gizmo: warn when 2D projection fails with non-invertable matrices

Campbell Barton noreply at git.blender.org
Thu Sep 9 05:23:52 CEST 2021


Commit: f9ebd17b4b124d93e4ad94c2c1a2ce020d8d334b
Author: Campbell Barton
Date:   Thu Sep 9 13:02:16 2021 +1000
Branches: master
https://developer.blender.org/rBf9ebd17b4b124d93e4ad94c2c1a2ce020d8d334b

Gizmo: warn when 2D projection fails with non-invertable matrices

Add a warning to quickly pinpoint the problem.

This would have simplified tracking down this problem in D12105.

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

M	source/blender/editors/gizmo_library/CMakeLists.txt
M	source/blender/editors/gizmo_library/gizmo_library_utils.c

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

diff --git a/source/blender/editors/gizmo_library/CMakeLists.txt b/source/blender/editors/gizmo_library/CMakeLists.txt
index eeb1e60166b..bfe8334b390 100644
--- a/source/blender/editors/gizmo_library/CMakeLists.txt
+++ b/source/blender/editors/gizmo_library/CMakeLists.txt
@@ -27,6 +27,7 @@ set(INC
   ../../makesdna
   ../../makesrna
   ../../windowmanager
+  ../../../../intern/clog
   ../../../../intern/eigen
   ../../../../intern/glew-mx
   ../../../../intern/guardedalloc
diff --git a/source/blender/editors/gizmo_library/gizmo_library_utils.c b/source/blender/editors/gizmo_library/gizmo_library_utils.c
index 7d0ae5afb9b..a0db2a8e627 100644
--- a/source/blender/editors/gizmo_library/gizmo_library_utils.c
+++ b/source/blender/editors/gizmo_library/gizmo_library_utils.c
@@ -39,9 +39,13 @@
 
 #include "ED_view3d.h"
 
+#include "CLG_log.h"
+
 /* own includes */
 #include "gizmo_library_intern.h"
 
+static CLG_LogRef LOG = {"ed.gizmo.library_utils"};
+
 /* factor for precision tweaking */
 #define GIZMO_PRECISION_FAC 0.05f
 
@@ -182,7 +186,7 @@ bool gizmo_window_project_2d(bContext *C,
                              bool use_offset,
                              float r_co[2])
 {
-  float mat[4][4];
+  float mat[4][4], imat[4][4];
   {
     float mat_identity[4][4];
     struct WM_GizmoMatrixParams params = {NULL};
@@ -193,6 +197,14 @@ bool gizmo_window_project_2d(bContext *C,
     WM_gizmo_calc_matrix_final_params(gz, &params, mat);
   }
 
+  if (!invert_m4_m4(imat, mat)) {
+    CLOG_WARN(&LOG,
+              "Gizmo \"%s\" of group \"%s\" has matrix that could not be inverted "
+              "(projection will fail)",
+              gz->type->idname,
+              gz->parent_gzgroup->type->idname);
+  }
+
   /* rotate mouse in relation to the center and relocate it */
   if (gz->parent_gzgroup->type->flag & WM_GIZMOGROUPTYPE_3D) {
     /* For 3d views, transform 2D mouse pos onto plane. */
@@ -202,8 +214,6 @@ bool gizmo_window_project_2d(bContext *C,
     plane_from_point_normal_v3(plane, mat[3], mat[2]);
     bool clip_ray = ((RegionView3D *)region->regiondata)->is_persp;
     if (ED_view3d_win_to_3d_on_plane(region, plane, mval, clip_ray, co)) {
-      float imat[4][4];
-      invert_m4_m4(imat, mat);
       mul_m4_v3(imat, co);
       r_co[0] = co[(axis + 1) % 3];
       r_co[1] = co[(axis + 2) % 3];
@@ -213,8 +223,6 @@ bool gizmo_window_project_2d(bContext *C,
   }
 
   float co[3] = {mval[0], mval[1], 0.0f};
-  float imat[4][4];
-  invert_m4_m4(imat, mat);
   mul_m4_v3(imat, co);
   copy_v2_v2(r_co, co);
   return true;
@@ -223,7 +231,7 @@ bool gizmo_window_project_2d(bContext *C,
 bool gizmo_window_project_3d(
     bContext *C, const struct wmGizmo *gz, const float mval[2], bool use_offset, float r_co[3])
 {
-  float mat[4][4];
+  float mat[4][4], imat[4][4];
   {
     float mat_identity[4][4];
     struct WM_GizmoMatrixParams params = {NULL};
@@ -234,20 +242,25 @@ bool gizmo_window_project_3d(
     WM_gizmo_calc_matrix_final_params(gz, &params, mat);
   }
 
+  if (!invert_m4_m4(imat, mat)) {
+    CLOG_WARN(&LOG,
+              "Gizmo \"%s\" of group \"%s\" has matrix that could not be inverted "
+              "(projection will fail)",
+              gz->type->idname,
+              gz->parent_gzgroup->type->idname);
+  }
+
   if (gz->parent_gzgroup->type->flag & WM_GIZMOGROUPTYPE_3D) {
     View3D *v3d = CTX_wm_view3d(C);
     ARegion *region = CTX_wm_region(C);
     /* NOTE: we might want a custom reference point passed in,
      * instead of the gizmo center. */
     ED_view3d_win_to_3d(v3d, region, mat[3], mval, r_co);
-    invert_m4(mat);
-    mul_m4_v3(mat, r_co);
+    mul_m4_v3(imat, r_co);
     return true;
   }
 
   float co[3] = {mval[0], mval[1], 0.0f};
-  float imat[4][4];
-  invert_m4_m4(imat, mat);
   mul_m4_v3(imat, co);
   copy_v2_v2(r_co, co);
   return true;



More information about the Bf-blender-cvs mailing list