New math function to add small bias to zero axis

In some situations the zero axis can produce problems and need to add a small bias.

This function adds a small bias using the orthogonal result of the others valid axis.

Reviewed By: fclem

Differential Revision: https://developer.blender.org/D16158
6d
This commit is contained in:
Antonio Vazquez 2022-10-05 22:10:53 +02:00 committed by Philipp Oeser
parent cb2fb570f1
commit 7e5db850dc
Notes: blender-bot 2023-07-10 10:12:37 +02:00
Referenced by issue #100749, Blender LTS: Maintenance Task 3.3
2 changed files with 30 additions and 0 deletions

View File

@ -367,6 +367,8 @@ void pseudoinverse_m4_m4(float Ainv[4][4], const float A[4][4], float epsilon);
void pseudoinverse_m3_m3(float Ainv[3][3], const float A[3][3], float epsilon);
bool has_zero_axis_m4(const float matrix[4][4]);
/** Fix any zero scale axis adding a small bias orthogonal to the other valid axis. */
void zero_axis_bias_m4(float mat[4][4]);
void invert_m4_m4_safe(float Ainv[4][4], const float A[4][4]);

View File

@ -3122,6 +3122,34 @@ bool has_zero_axis_m4(const float matrix[4][4])
len_squared_v3(matrix[2]) < FLT_EPSILON;
}
void zero_axis_bias_m4(float mat[4][4])
{
const bool axis_x_degenerate = len_squared_v3(mat[0]) < FLT_EPSILON;
const bool axis_y_degenerate = len_squared_v3(mat[1]) < FLT_EPSILON;
const bool axis_z_degenerate = len_squared_v3(mat[2]) < FLT_EPSILON;
/* X Axis. */
if (axis_x_degenerate && !axis_y_degenerate && !axis_z_degenerate) {
cross_v3_v3v3(mat[0], mat[1], mat[2]);
mul_v3_fl(mat[0], FLT_EPSILON);
return;
}
/* Y Axis. */
if (!axis_x_degenerate && axis_y_degenerate && !axis_z_degenerate) {
cross_v3_v3v3(mat[1], mat[2], mat[0]);
mul_v3_fl(mat[1], FLT_EPSILON);
return;
}
/* Z Axis. */
if (!axis_x_degenerate && !axis_y_degenerate && axis_z_degenerate) {
cross_v3_v3v3(mat[2], mat[0], mat[1]);
mul_v3_fl(mat[2], FLT_EPSILON);
return;
}
}
void invert_m4_m4_safe(float Ainv[4][4], const float A[4][4])
{
if (!invert_m4_m4(Ainv, A)) {