GPv3: Fix floating-point error in the hard eraser tool. #110801

Merged
Amélie Fondevilla merged 9 commits from amelief/blender:gpv3-eraser-fix-floating-point-issue into main 2023-08-07 15:11:41 +02:00

The hard eraser tool was not working properly when trying to erase closely to existing points, leading either to the insertion of various close points in the stroke, or to deleting a whole stroke segment.

This was due to the differenciation between the computation of the intersections between the eraser and the stroke, and the computation of whether a point lie inside the eraser, which lead to inconsistencies in the interpretation of the result.

This patch solves this issue by :

  • computing if the point is inside the eraser based on the result of the intersection, to avoid unconsistencies,
  • computation with integers and not float (we are in screen space anyways),
  • the stroke points can now be cuts, and not only the inner intersections.
The hard eraser tool was not working properly when trying to erase closely to existing points, leading either to the insertion of various close points in the stroke, or to deleting a whole stroke segment. This was due to the differenciation between the computation of the intersections between the eraser and the stroke, and the computation of whether a point lie inside the eraser, which lead to inconsistencies in the interpretation of the result. This patch solves this issue by : * computing if the point is inside the eraser based on the result of the intersection, to avoid unconsistencies, * computation with integers and not float (we are in screen space anyways), * the stroke points can now be cuts, and not only the inner intersections.
Amélie Fondevilla added this to the Grease Pencil project 2023-08-04 17:21:07 +02:00
Amélie Fondevilla requested review from Falk David 2023-08-07 09:14:29 +02:00
Amélie Fondevilla force-pushed gpv3-eraser-fix-floating-point-issue from b8b3982cdd to 0267abd05f 2023-08-07 09:37:26 +02:00 Compare
Falk David requested changes 2023-08-07 14:03:37 +02:00
Falk David left a comment
Member

First pass. In general, we use the more explicit types of int, e.g. int64_t. Here it even makes sense to use int8_t in some cases.

First pass. In general, we use the more explicit types of `int`, e.g. `int64_t`. Here it even makes sense to use `int8_t` in some cases.
@ -71,0 +94,4 @@
int &r_mu1)
{
const int64_t d_s0_center = math::distance_squared(s0, center);
Member

No need for the space between each line here.

No need for the space between each line here.
amelief marked this conversation as resolved
@ -71,0 +118,4 @@
}
/* Two intersections. */
const float i_sqrt = sqrtf(i);
Member

sqrtf(float(i))

`sqrtf(float(i))`
amelief marked this conversation as resolved
@ -74,3 +150,2 @@
*/
int intersections_with_segment(const float2 &point,
const float2 &point_after,
int intersections_with_segment(const int2 &point,
Member

Return value should be int8_t.

Return value should be `int8_t`.
amelief marked this conversation as resolved
@ -98,1 +160,3 @@
std::swap(r_mu0, r_mu1);
/* Compute the integer values of the intersection. */
const int segment_length = math::distance(point, point_after);
Member

I think we should use int64_t here. Same for mu0 and mu1.

I think we should use `int64_t` here. Same for `mu0` and `mu1`.
amelief marked this conversation as resolved
@ -103,0 +186,4 @@
* 0 : in-between the endpoints,
* 1 : after the last endpoint.
*/
const int side_mu0 = (mu0 <= 0) ? (-1) : ((mu0 >= segment_length) ? 1 : 0);
Member

Should be int8_t.

Should be `int8_t`.
amelief marked this conversation as resolved
@ -103,0 +227,4 @@
/* Only one intersection lies within the segment. Only one point should be erased, depending on
* the side of the other intersection. */
const int side_outside_intersection = is_mu0_inside ? side_mu1 : side_mu0;
Member

Should be int8_t.

Should be `int8_t`.
amelief marked this conversation as resolved
@ -117,4 +263,2 @@
* Note that for the two output arrays the last element may contain intersections if the
* corresponding curve is cyclic.
*/
int intersections_with_curves(const bke::CurvesGeometry &src,
Member

Return value should be int64_t.

Return value should be `int64_t`.
amelief marked this conversation as resolved
@ -143,0 +288,4 @@
/* One-point stroke : just check if the point is inside the eraser. */
const int src_point = src_curve_points.first();
const float2 pos = screen_space_positions[src_point];
r_is_point_inside[src_point] = contains_point(pos);
Member

Since the contains_point function is only used here, I would just copy the condition here and remove the inline function.

Since the `contains_point` function is only used here, I would just copy the condition here and remove the inline function.
amelief marked this conversation as resolved
@ -236,0 +369,4 @@
*/
int total_points_cut = 0;
for (const int src_point : src.points_range()) {
total_points_cut += int(is_point_cut[src_point]);
Member

I would prefer total_points_cut += is_point_cut[src_point] ? 1 : 0;

I would prefer `total_points_cut += is_point_cut[src_point] ? 1 : 0;`
amelief marked this conversation as resolved
@ -515,1 +654,4 @@
this->mouse_position_pixels = int2(round_fl_to_int(mouse_position[0]),
round_fl_to_int(mouse_position[1]));
const int eraser_radius_pixels = round_fl_to_int(eraser_radius);
Member

Should be int64_t.

Should be `int64_t`.
amelief marked this conversation as resolved
Amélie Fondevilla added 3 commits 2023-08-07 14:21:20 +02:00
Falk David requested changes 2023-08-07 14:28:08 +02:00
Falk David left a comment
Member

Found a couple more ints that should be explicitly typed :)

Found a couple more `int`s that should be explicitly typed :)
@ -123,1 +250,3 @@
MutableSpan<float2> r_intersections_factors) const
int64_t intersections_with_curves(const bke::CurvesGeometry &src,
const Span<float2> screen_space_positions,
MutableSpan<int> r_nb_intersections,
Member

MutableSpan<int64_t>

`MutableSpan<int64_t>`
amelief marked this conversation as resolved
@ -128,1 +260,3 @@
threading::parallel_for(src.curves_range(), 256, [&](const IndexRange src_curves) {
Array<int2> screen_space_positions_pixel(src.points_num());
threading::parallel_for(src.points_range(), 1024, [&](const IndexRange src_points) {
for (const int src_point : src_points) {
Member

const int64_t src_point

`const int64_t src_point`
amelief marked this conversation as resolved
@ -142,1 +273,3 @@
mu1);
if (src_curve_points.size() == 1) {
/* One-point stroke : just check if the point is inside the eraser. */
const int src_point = src_curve_points.first();
Member

const int64_t src_point

`const int64_t src_point`
amelief marked this conversation as resolved
@ -144,3 +282,1 @@
r_intersections_factors[src_point] = float2(mu0, mu1);
}
});
for (const int src_point : src_curve_points.drop_back(1)) {
Member

const int64_t src_point

`const int64_t src_point`
amelief marked this conversation as resolved
@ -148,3 +294,4 @@
if (src_cyclic[src_curve]) {
/* If the curve is cyclic, we need to check for the closing segment. */
const int src_last_point = src_curve_points.last();
const int src_first_point = src_curve_points.first();
Member

const int64_t src_first_point

`const int64_t src_first_point`
amelief marked this conversation as resolved
@ -165,3 +313,3 @@
/* Compute total number of intersections. */
int total_intersections = 0;
int64_t total_intersections = 0;
for (const int src_point : src.points_range()) {
Member

const int64_t src_point

`const int64_t src_point`
amelief marked this conversation as resolved
@ -226,2 +336,4 @@
/* Compute intersections between the eraser and the curves in the source domain. */
Array<bool> is_point_inside(src_points_num, false);
Array<bool> is_point_cut(src_points_num, false);
Array<int> nb_intersections(src_points_num, 0);
Member

Array<int64_t> nb_intersections

`Array<int64_t> nb_intersections`
amelief marked this conversation as resolved
@ -236,0 +355,4 @@
* These points will be kept in the destination geometry, but change the topology of the curve.
*/
int64_t total_points_cut = 0;
for (const int src_point : src.points_range()) {
Member

const int64_t src_point

`const int64_t src_point`
amelief marked this conversation as resolved
@ -257,2 +380,4 @@
* numbers for which the integer is the index of the corresponding segment in the source
* curves, and the float part is the (0,1) factor representing its position in the segment.
*/
Array<std::pair<int, float>> dst_points_parameters(dst_points_num);
Member

Array<std::pair<int64_t, float>> dst_points_parameters

`Array<std::pair<int64_t, float>> dst_points_parameters`
amelief marked this conversation as resolved
@ -464,2 +589,3 @@
* stroke. */
/* If any segment of the stroke is closer to the eraser than its radius, then remove
* the stroke. */
for (const int src_point : src_curve_points.drop_back(1)) {
Member

const int64_t src_point

`const int64_t src_point`
amelief marked this conversation as resolved
Amélie Fondevilla added 1 commit 2023-08-07 14:49:37 +02:00
Amélie Fondevilla added 1 commit 2023-08-07 14:52:00 +02:00
Falk David approved these changes 2023-08-07 15:05:39 +02:00
Falk David left a comment
Member

Looks good!

Looks good!
Amélie Fondevilla merged commit 242e94acec into main 2023-08-07 15:11:41 +02:00
Amélie Fondevilla deleted branch gpv3-eraser-fix-floating-point-issue 2023-08-07 15:11:42 +02:00
Sign in to join this conversation.
No reviewers
No Label
Interest
Alembic
Interest
Animation & Rigging
Interest
Asset Browser
Interest
Asset Browser Project Overview
Interest
Audio
Interest
Automated Testing
Interest
Blender Asset Bundle
Interest
BlendFile
Interest
Collada
Interest
Compatibility
Interest
Compositing
Interest
Core
Interest
Cycles
Interest
Dependency Graph
Interest
Development Management
Interest
EEVEE
Interest
EEVEE & Viewport
Interest
Freestyle
Interest
Geometry Nodes
Interest
Grease Pencil
Interest
ID Management
Interest
Images & Movies
Interest
Import Export
Interest
Line Art
Interest
Masking
Interest
Metal
Interest
Modeling
Interest
Modifiers
Interest
Motion Tracking
Interest
Nodes & Physics
Interest
OpenGL
Interest
Overlay
Interest
Overrides
Interest
Performance
Interest
Physics
Interest
Pipeline, Assets & IO
Interest
Platforms, Builds & Tests
Interest
Python API
Interest
Render & Cycles
Interest
Render Pipeline
Interest
Sculpt, Paint & Texture
Interest
Text Editor
Interest
Translations
Interest
Triaging
Interest
Undo
Interest
USD
Interest
User Interface
Interest
UV Editing
Interest
VFX & Video
Interest
Video Sequencer
Interest
Virtual Reality
Interest
Vulkan
Interest
Wayland
Interest
Workbench
Interest: X11
Legacy
Blender 2.8 Project
Legacy
Milestone 1: Basic, Local Asset Browser
Legacy
OpenGL Error
Meta
Good First Issue
Meta
Papercut
Meta
Retrospective
Meta
Security
Module
Animation & Rigging
Module
Core
Module
Development Management
Module
EEVEE & Viewport
Module
Grease Pencil
Module
Modeling
Module
Nodes & Physics
Module
Pipeline, Assets & IO
Module
Platforms, Builds & Tests
Module
Python API
Module
Render & Cycles
Module
Sculpt, Paint & Texture
Module
Triaging
Module
User Interface
Module
VFX & Video
Platform
FreeBSD
Platform
Linux
Platform
macOS
Platform
Windows
Priority
High
Priority
Low
Priority
Normal
Priority
Unbreak Now!
Status
Archived
Status
Confirmed
Status
Duplicate
Status
Needs Info from Developers
Status
Needs Information from User
Status
Needs Triage
Status
Resolved
Type
Bug
Type
Design
Type
Known Issue
Type
Patch
Type
Report
Type
To Do
No Milestone
No project
No Assignees
2 Participants
Notifications
Due Date
The due date is invalid or out of range. Please use the format 'yyyy-mm-dd'.

No due date set.

Dependencies

No dependencies set.

Reference: blender/blender#110801
No description provided.