diff options
author | Mattias Fredriksson <Osares> | 2021-08-06 02:34:32 +0300 |
---|---|---|
committer | Hans Goudey <h.goudey@me.com> | 2021-08-06 02:34:32 +0300 |
commit | bc0d55e724a27fba61a93cc95f2cc48e205e1cd8 (patch) | |
tree | 1dcd4c1db63c7590c53da8dfe752ede0d07b27dd /source/blender/nodes/geometry/nodes/node_geo_mesh_primitive_uv_sphere.cc | |
parent | 263fa406cd2bc1aefe410fe735c22967ee052e22 (diff) |
Fix: Avoid floating point error in some mesh primitive nodes
Some mesh primitives created using geometry nodes use loops to create
vertices and accumulates positions/angles in FP variables. This allows
rounding errors to accumulate and can introduce significant errors.
To minimize changes from original implementation, variables allowing
errors to accumulate are replaced by: delta * index. Affected Mesh
Primitives nodes are Line, Grid, Cylinder, Circle, Cone, and UV-Sphere.
Differential Revision: https://developer.blender.org/D12136
Diffstat (limited to 'source/blender/nodes/geometry/nodes/node_geo_mesh_primitive_uv_sphere.cc')
-rw-r--r-- | source/blender/nodes/geometry/nodes/node_geo_mesh_primitive_uv_sphere.cc | 14 |
1 files changed, 6 insertions, 8 deletions
diff --git a/source/blender/nodes/geometry/nodes/node_geo_mesh_primitive_uv_sphere.cc b/source/blender/nodes/geometry/nodes/node_geo_mesh_primitive_uv_sphere.cc index fe456dc4564..affba602234 100644 --- a/source/blender/nodes/geometry/nodes/node_geo_mesh_primitive_uv_sphere.cc +++ b/source/blender/nodes/geometry/nodes/node_geo_mesh_primitive_uv_sphere.cc @@ -69,26 +69,24 @@ static void calculate_sphere_vertex_data(MutableSpan<MVert> verts, const int rings) { const float delta_theta = M_PI / rings; - const float delta_phi = (2 * M_PI) / segments; + const float delta_phi = (2.0f * M_PI) / segments; copy_v3_v3(verts[0].co, float3(0.0f, 0.0f, radius)); normal_float_to_short_v3(verts[0].no, float3(0.0f, 0.0f, 1.0f)); int vert_index = 1; - float theta = delta_theta; - for (const int UNUSED(ring) : IndexRange(rings - 1)) { - float phi = 0.0f; - const float z = cosf(theta); - for (const int UNUSED(segment) : IndexRange(segments)) { + for (const int ring : IndexRange(1, rings - 1)) { + const float theta = ring * delta_theta; + const float z = std::cos(theta); + for (const int segment : IndexRange(1, segments)) { + const float phi = segment * delta_phi; const float sin_theta = std::sin(theta); const float x = sin_theta * std::cos(phi); const float y = sin_theta * std::sin(phi); copy_v3_v3(verts[vert_index].co, float3(x, y, z) * radius); normal_float_to_short_v3(verts[vert_index].no, float3(x, y, z)); - phi += delta_phi; vert_index++; } - theta += delta_theta; } copy_v3_v3(verts.last().co, float3(0.0f, 0.0f, -radius)); |