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_circle.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_circle.cc')
-rw-r--r-- | source/blender/nodes/geometry/nodes/node_geo_mesh_primitive_circle.cc | 10 |
1 files changed, 5 insertions, 5 deletions
diff --git a/source/blender/nodes/geometry/nodes/node_geo_mesh_primitive_circle.cc b/source/blender/nodes/geometry/nodes/node_geo_mesh_primitive_circle.cc index 96c6f073ab3..131f9548b40 100644 --- a/source/blender/nodes/geometry/nodes/node_geo_mesh_primitive_circle.cc +++ b/source/blender/nodes/geometry/nodes/node_geo_mesh_primitive_circle.cc @@ -126,11 +126,11 @@ static Mesh *create_circle_mesh(const float radius, MutableSpan<MEdge> edges{mesh->medge, mesh->totedge}; MutableSpan<MPoly> polys{mesh->mpoly, mesh->totpoly}; - float angle = 0.0f; - const float angle_delta = 2.0f * M_PI / static_cast<float>(verts_num); - for (MVert &vert : verts) { - copy_v3_v3(vert.co, float3(std::cos(angle) * radius, std::sin(angle) * radius, 0.0f)); - angle += angle_delta; + /* Assign vertex coordinates. */ + const float angle_delta = 2.0f * (M_PI / static_cast<float>(verts_num)); + for (const int i : IndexRange(verts_num)) { + const float angle = i * angle_delta; + copy_v3_v3(verts[i].co, float3(std::cos(angle) * radius, std::sin(angle) * radius, 0.0f)); } if (fill_type == GEO_NODE_MESH_CIRCLE_FILL_TRIANGLE_FAN) { copy_v3_v3(verts.last().co, float3(0)); |