Fix #121169: asserts in curve geometry code.

Asserts triggered e.g. by opening Gold production files (like
`pro/shots/220_storm/220_0020/220_0020-anim.blend`). Their root cause
are zero tangent vectors.

The asserts initially came from unormalized normals, but the root issue
is actually using zero vector as axis in calls to
`math::rotate_direction_around_axis`.

While rotating a zero direction vector is possible (though useless),
rotating around a zero axis vector makes no sense?

So this commit adds an assert that the given axis is non-zero in
`rotate_direction_around_axis`. And 'fixes' the found cases triggering
such assert by skipping rotation when the axis (tangent) is null.

Another related issue fixed by this commit is the iterative process in
calls to `calculate_next_normal`, which can accumulate small floating
point errors over time, leading to generating not normalized-enough
normals at some point.

Pull Request: https://projects.blender.org/blender/blender/pulls/122441
This commit is contained in:
Bastien Montagne
2024-05-30 10:24:04 +02:00
committed by Bastien Montagne
parent a8a976e09b
commit e18dd894b8
3 changed files with 20 additions and 6 deletions

View File

@@ -146,9 +146,14 @@ static float3 calculate_next_normal(const float3 &last_normal,
return last_normal;
}
const float angle = angle_normalized_v3v3(last_tangent, current_tangent);
if (angle != 0.0) {
if (angle != 0.0f) {
const float3 axis = math::normalize(math::cross(last_tangent, current_tangent));
return math::rotate_direction_around_axis(last_normal, axis, angle);
if (LIKELY(!math::is_zero(axis))) {
/* The iterative process here (computing the current normal by rotating the previous one) can
* accumulate small floating point errors, leading to 'not enough' normalized results at some
* point (see #121169). */
return math::normalize(math::rotate_direction_around_axis(last_normal, axis, angle));
}
}
return last_normal;
}
@@ -167,7 +172,7 @@ void calculate_normals_minimum(const Span<float3> tangents,
/* Set initial normal. */
const float3 &first_tangent = tangents.first();
if (fabs(first_tangent.x) + fabs(first_tangent.y) < epsilon) {
if (UNLIKELY(fabs(first_tangent.x) + fabs(first_tangent.y) < epsilon)) {
normals.first() = {1.0f, 0.0f, 0.0f};
}
else {
@@ -193,11 +198,15 @@ void calculate_normals_minimum(const Span<float3> tangents,
correction_angle = correction_angle - 2 * M_PI;
}
/* Gradually apply correction by rotating all normals slightly. */
/* Gradually apply correction by rotating all normals slightly around their tangents. */
const float angle_step = correction_angle / normals.size();
for (const int i : normals.index_range()) {
const float3 axis = tangents[i];
if (UNLIKELY(math::is_zero(axis))) {
continue;
}
const float angle = angle_step * i;
normals[i] = math::rotate_direction_around_axis(normals[i], tangents[i], angle);
normals[i] = math::rotate_direction_around_axis(normals[i], axis, angle);
}
}

View File

@@ -795,7 +795,11 @@ static void rotate_directions_around_axes(MutableSpan<float3> directions,
const Span<float> angles)
{
for (const int i : directions.index_range()) {
directions[i] = math::rotate_direction_around_axis(directions[i], axes[i], angles[i]);
const float3 axis = axes[i];
if (UNLIKELY(math::is_zero(axis))) {
continue;
}
directions[i] = math::rotate_direction_around_axis(directions[i], axis, angles[i]);
}
}

View File

@@ -113,6 +113,7 @@ float3 rotate_direction_around_axis(const float3 &direction, const float3 &axis,
{
BLI_ASSERT_UNIT_V3(direction);
BLI_ASSERT_UNIT_V3(axis);
BLI_assert(!math::is_zero(axis));
const float3 axis_scaled = axis * math::dot(direction, axis);
const float3 diff = direction - axis_scaled;