Tesselation tolerance of 0.0f (or negative) disables curve drawing for num_segments = 0 #5713

Closed
opened 2022-09-27 00:34:10 +00:00 by Andersama · 9 comments
Andersama commented 2022-09-27 00:34:10 +00:00 (Migrated from github.com)

Version/Branch of Dear ImGui:

Version: v1.88
Branch: master

Back-end/Renderer/Compiler/OS

Back-ends: any
Compiler: any
Operating System: windows

My Issue/Question:

Currently trying to build out some utility functions to render svgs. What I found is that when using:

svg_draw_list.PathBezierCubicCurveTo(
	ImVec2{ x1, y1 },
	ImVec2{ x2, y2 },
	ImVec2{ current_x + cmd->x, current_y + cmd->y });

There's a defaulted num_segment value of 0 which eventually uses the functionality from https://github.com/ocornut/imgui/issues/3127

static void PathBezierCubicCurveToCasteljau(ImVector<ImVec2>* path, float x1, float y1, float x2, float y2, float x3, float y3, float x4, float y4, float tess_tol, int level)
{
    float dx = x4 - x1;
    float dy = y4 - y1;
    float d2 = (x2 - x4) * dy - (y2 - y4) * dx;
    float d3 = (x3 - x4) * dy - (y3 - y4) * dx;
    d2 = (d2 >= 0) ? d2 : -d2;
    d3 = (d3 >= 0) ? d3 : -d3;
    if ((d2 + d3) * (d2 + d3) < tess_tol * (dx * dx + dy * dy))
    {
        path->push_back(ImVec2(x4, y4));
    }
    else if (level < 10)
    {
        float x12 = (x1 + x2) * 0.5f, y12 = (y1 + y2) * 0.5f;
        float x23 = (x2 + x3) * 0.5f, y23 = (y2 + y3) * 0.5f;
        float x34 = (x3 + x4) * 0.5f, y34 = (y3 + y4) * 0.5f;
        float x123 = (x12 + x23) * 0.5f, y123 = (y12 + y23) * 0.5f;
        float x234 = (x23 + x34) * 0.5f, y234 = (y23 + y34) * 0.5f;
        float x1234 = (x123 + x234) * 0.5f, y1234 = (y123 + y234) * 0.5f;
        PathBezierCubicCurveToCasteljau(path, x1, y1, x12, y12, x123, y123, x1234, y1234, tess_tol, level + 1);
        PathBezierCubicCurveToCasteljau(path, x1234, y1234, x234, y234, x34, y34, x4, y4, tess_tol, level + 1);
    }
}

static void PathBezierQuadraticCurveToCasteljau(ImVector<ImVec2>* path, float x1, float y1, float x2, float y2, float x3, float y3, float tess_tol, int level)
{
    float dx = x3 - x1, dy = y3 - y1;
    float det = (x2 - x3) * dy - (y2 - y3) * dx;
    if (det * det * 4.0f < tess_tol * (dx * dx + dy * dy))
    {
        path->push_back(ImVec2(x3, y3));
    }
    else if (level < 10)
    {
        float x12 = (x1 + x2) * 0.5f, y12 = (y1 + y2) * 0.5f;
        float x23 = (x2 + x3) * 0.5f, y23 = (y2 + y3) * 0.5f;
        float x123 = (x12 + x23) * 0.5f, y123 = (y12 + y23) * 0.5f;
        PathBezierQuadraticCurveToCasteljau(path, x1, y1, x12, y12, x123, y123, tess_tol, level + 1);
        PathBezierQuadraticCurveToCasteljau(path, x123, y123, x23, y23, x3, y3, tess_tol, level + 1);
    }
}

Quick eyeballing of the code and you'll notice that with a tessellation tolerance <= 0.0f no points will ever be added, this includes the start and end point of the curve (presumably the start of the curve has already been added). I'm not sure if this is intended, I'd assume a failover of at least a line drawn to the end point? I ran into this by accident because I didn't specifically set the tessellation value and apparently it was zeroed out. In my case it clearly broke drawings with a start point followed by a curve because the resulting path only contained the start point, which of course doesn't end up rendering much of anything to notice visually.

Screenshots/Video
For reference...my svg renderer is not complete and is still buggy, but here's what I was dealing with:
image

Compared to:
image

The original svg was a chess piece, in case that isn't clear.
Standalone, minimal, complete and verifiable example: (see https://github.com/ocornut/imgui/issues/2261)

	ImGui::Begin("Example Bug");
		ImDrawList* current_list = ImGui::GetWindowDrawList();
		ImGuiWindow* current_window = ImGui::GetCurrentWindow();
		
		ImDrawListSharedData* shared_data = ImGui::GetDrawListSharedData();
		shared_data->CurveTessellationTol = 0.0f; //if this isn't modified it could easily be 0'd out memory which does this
		ImDrawList svg_draw_list = ImDrawList(shared_data);
		float x1 = 0.0f + current_window->DC.CursorPos.x;
		float x2 = 100.f + current_window->DC.CursorPos.x;
		float x3 = 200.0f + current_window->DC.CursorPos.x;
		float y1 = 0.0f + current_window->DC.CursorPos.y;
		float y2 = 100.0f + current_window->DC.CursorPos.y;
		float y3 = 200.0f + current_window->DC.CursorPos.y;
		svg_draw_list.PathLineTo(ImVec2{ x1, y1 });
		svg_draw_list.PathBezierCubicCurveTo(
			ImVec2{ x2, y1 },
			ImVec2{ x3, y2 },
			ImVec2{ x3, y3 }); // should now how an arc like path
		//svg_draw_list.AddPolyline(svg_draw_list._Path.Data, svg_draw_list._Path.Size, ImU32{0xffffffff}, 0, 1.0f);
		//svg_draw_list.AddConvexPolyFilled(svg_draw_list.Path.Data, svg_draw_list._Path.Size, ImU32{0xffffffff});
		current_list->AddPolyline(svg_draw_list._Path.Data, svg_draw_list._Path.Size, ImU32{ 0xffffffff }, 0, 10.0f); // as a white line
		//svg_draw_list.PathClear();

		// should see this:
		shared_data->CurveTessellationTol = 0.1f;
		ImDrawList nsvg_draw_list = ImDrawList(shared_data);
		x1 += 200.0f;
		x2 += 200.0f;
		x3 += 200.0f;

		nsvg_draw_list.PathLineTo(ImVec2{ x1, y1 });
		nsvg_draw_list.PathBezierCubicCurveTo(
			ImVec2{ x2, y1 },
			ImVec2{ x3, y2 },
			ImVec2{ x3, y3 });

		current_list->AddPolyline(nsvg_draw_list._Path.Data, nsvg_draw_list._Path.Size, ImU32{ 0xff0000ff }, 0, 10.0f); // as a red line
		//nsvg_draw_list.PathClear();
		
		current_list->AddRect(ImVec2{ 1000.0f, 0.0f }, ImVec2{ 1100.0f, 100.0f }, ImU32{0xffffffff}, 0.0f);

		//note: svg_draw_list and nsvg_draw_list have different _Path data!
	ImGui::End();
**Version/Branch of Dear ImGui:** Version: [v1.88](https://github.com/ocornut/imgui/releases/tag/v1.88) Branch: master **Back-end/Renderer/Compiler/OS** Back-ends: any Compiler: any Operating System: windows **My Issue/Question:** Currently trying to build out some utility functions to render svgs. What I found is that when using: ```c svg_draw_list.PathBezierCubicCurveTo( ImVec2{ x1, y1 }, ImVec2{ x2, y2 }, ImVec2{ current_x + cmd->x, current_y + cmd->y }); ``` There's a defaulted num_segment value of 0 which eventually uses the functionality from https://github.com/ocornut/imgui/issues/3127 ```c static void PathBezierCubicCurveToCasteljau(ImVector<ImVec2>* path, float x1, float y1, float x2, float y2, float x3, float y3, float x4, float y4, float tess_tol, int level) { float dx = x4 - x1; float dy = y4 - y1; float d2 = (x2 - x4) * dy - (y2 - y4) * dx; float d3 = (x3 - x4) * dy - (y3 - y4) * dx; d2 = (d2 >= 0) ? d2 : -d2; d3 = (d3 >= 0) ? d3 : -d3; if ((d2 + d3) * (d2 + d3) < tess_tol * (dx * dx + dy * dy)) { path->push_back(ImVec2(x4, y4)); } else if (level < 10) { float x12 = (x1 + x2) * 0.5f, y12 = (y1 + y2) * 0.5f; float x23 = (x2 + x3) * 0.5f, y23 = (y2 + y3) * 0.5f; float x34 = (x3 + x4) * 0.5f, y34 = (y3 + y4) * 0.5f; float x123 = (x12 + x23) * 0.5f, y123 = (y12 + y23) * 0.5f; float x234 = (x23 + x34) * 0.5f, y234 = (y23 + y34) * 0.5f; float x1234 = (x123 + x234) * 0.5f, y1234 = (y123 + y234) * 0.5f; PathBezierCubicCurveToCasteljau(path, x1, y1, x12, y12, x123, y123, x1234, y1234, tess_tol, level + 1); PathBezierCubicCurveToCasteljau(path, x1234, y1234, x234, y234, x34, y34, x4, y4, tess_tol, level + 1); } } static void PathBezierQuadraticCurveToCasteljau(ImVector<ImVec2>* path, float x1, float y1, float x2, float y2, float x3, float y3, float tess_tol, int level) { float dx = x3 - x1, dy = y3 - y1; float det = (x2 - x3) * dy - (y2 - y3) * dx; if (det * det * 4.0f < tess_tol * (dx * dx + dy * dy)) { path->push_back(ImVec2(x3, y3)); } else if (level < 10) { float x12 = (x1 + x2) * 0.5f, y12 = (y1 + y2) * 0.5f; float x23 = (x2 + x3) * 0.5f, y23 = (y2 + y3) * 0.5f; float x123 = (x12 + x23) * 0.5f, y123 = (y12 + y23) * 0.5f; PathBezierQuadraticCurveToCasteljau(path, x1, y1, x12, y12, x123, y123, tess_tol, level + 1); PathBezierQuadraticCurveToCasteljau(path, x123, y123, x23, y23, x3, y3, tess_tol, level + 1); } } ``` Quick eyeballing of the code and you'll notice that with a tessellation tolerance <= 0.0f no points will ever be added, this includes the start and end point of the curve (presumably the start of the curve has already been added). I'm not sure if this is intended, I'd assume a failover of at least a line drawn to the end point? I ran into this by accident because I didn't specifically set the tessellation value and apparently it was zeroed out. In my case it clearly broke drawings with a start point followed by a curve because the resulting path only contained the start point, which of course doesn't end up rendering much of anything to notice visually. **Screenshots/Video** For reference...my svg renderer is not complete and is still buggy, but here's what I was dealing with: ![image](https://user-images.githubusercontent.com/25020235/192398784-ddbfb858-b18d-48b3-ad2f-7172492f80e9.png) Compared to: ![image](https://user-images.githubusercontent.com/25020235/192398998-9bdb9831-1c2d-4c4b-b605-3d2875472a8d.png) The original svg was a chess piece, in case that isn't clear. **Standalone, minimal, complete and verifiable example:** _(see https://github.com/ocornut/imgui/issues/2261)_ ```c ImGui::Begin("Example Bug"); ImDrawList* current_list = ImGui::GetWindowDrawList(); ImGuiWindow* current_window = ImGui::GetCurrentWindow(); ImDrawListSharedData* shared_data = ImGui::GetDrawListSharedData(); shared_data->CurveTessellationTol = 0.0f; //if this isn't modified it could easily be 0'd out memory which does this ImDrawList svg_draw_list = ImDrawList(shared_data); float x1 = 0.0f + current_window->DC.CursorPos.x; float x2 = 100.f + current_window->DC.CursorPos.x; float x3 = 200.0f + current_window->DC.CursorPos.x; float y1 = 0.0f + current_window->DC.CursorPos.y; float y2 = 100.0f + current_window->DC.CursorPos.y; float y3 = 200.0f + current_window->DC.CursorPos.y; svg_draw_list.PathLineTo(ImVec2{ x1, y1 }); svg_draw_list.PathBezierCubicCurveTo( ImVec2{ x2, y1 }, ImVec2{ x3, y2 }, ImVec2{ x3, y3 }); // should now how an arc like path //svg_draw_list.AddPolyline(svg_draw_list._Path.Data, svg_draw_list._Path.Size, ImU32{0xffffffff}, 0, 1.0f); //svg_draw_list.AddConvexPolyFilled(svg_draw_list.Path.Data, svg_draw_list._Path.Size, ImU32{0xffffffff}); current_list->AddPolyline(svg_draw_list._Path.Data, svg_draw_list._Path.Size, ImU32{ 0xffffffff }, 0, 10.0f); // as a white line //svg_draw_list.PathClear(); // should see this: shared_data->CurveTessellationTol = 0.1f; ImDrawList nsvg_draw_list = ImDrawList(shared_data); x1 += 200.0f; x2 += 200.0f; x3 += 200.0f; nsvg_draw_list.PathLineTo(ImVec2{ x1, y1 }); nsvg_draw_list.PathBezierCubicCurveTo( ImVec2{ x2, y1 }, ImVec2{ x3, y2 }, ImVec2{ x3, y3 }); current_list->AddPolyline(nsvg_draw_list._Path.Data, nsvg_draw_list._Path.Size, ImU32{ 0xff0000ff }, 0, 10.0f); // as a red line //nsvg_draw_list.PathClear(); current_list->AddRect(ImVec2{ 1000.0f, 0.0f }, ImVec2{ 1100.0f, 100.0f }, ImU32{0xffffffff}, 0.0f); //note: svg_draw_list and nsvg_draw_list have different _Path data! ImGui::End(); ```
ocornut commented 2022-09-27 12:25:28 +00:00 (Migrated from github.com)

Hello,
Thanks for reporting.
A zero of negative tessellation tolerance is invalid, for the global setting we are asserting in NewFrame():

IM_ASSERT(g.Style.CurveTessellationTol > 0.0f                       && "Invalid style setting!");

And this is where the value is pulled from.

if this isn't modified it could easily be 0'd out memory which does this

I don't expect us to magically positively react to people randomly zeroing memory. I am guessing it is slightly more likely to happen if the underlying ImDrawListSharedData backing is swapped or modified. I am not sure what you are trying to solve here. We could indeed add more assert in entry point function, which is what I did now (56c3eae) but I don't think there's more than that to do.

Hello, Thanks for reporting. A zero of negative tessellation tolerance is invalid, for the global setting we are asserting in `NewFrame()`: ```cpp IM_ASSERT(g.Style.CurveTessellationTol > 0.0f && "Invalid style setting!"); ``` And this is where the value is pulled from. > if this isn't modified it could easily be 0'd out memory which does this I don't expect us to magically positively react to people randomly zeroing memory. I am guessing it is slightly more likely to happen if the underlying `ImDrawListSharedData` backing is swapped or modified. I am not sure what you are trying to solve here. We could indeed add more assert in entry point function, which is what I did now (56c3eae) but I don't think there's more than that to do.
Andersama commented 2022-09-27 16:18:59 +00:00 (Migrated from github.com)

Ah ok, so my suspicion that it was intended was correct. Do you have a general sense of the minimum/maximum value should be? EG: if before the call I instead the tolerance was clamped?

I don't expect us to magically positively react to people randomly zeroing memory.

No I wouldn't imagine you would. I ran into this by accident.

Ah ok, so my suspicion that it was intended was correct. Do you have a general sense of the minimum/maximum value should be? EG: if before the call I instead the tolerance was clamped? > I don't expect us to magically positively react to people randomly zeroing memory. No I wouldn't imagine you would. I ran into this by accident.
ocornut commented 2022-09-27 16:34:43 +00:00 (Migrated from github.com)

You can leave the default value which is 1.25 and tweak the value in style editor. I don’t know off hand of there’s a meaningful “min” and “max” there, its a tradeoff.

You can leave the default value which is 1.25 and tweak the value in style editor. I don’t know off hand of there’s a meaningful “min” and “max” there, its a tradeoff.
Andersama commented 2022-09-27 16:53:43 +00:00 (Migrated from github.com)

It looked like the math behind it was doing some form of quick check against distances between points, not exactly studied it completely so I'm guessing it's some estimation along the bezier compared against the actual bezier curve? And then presumably when the estimated point fails distance wise it subdivides between that section of the curve (generating more points tighter to the curve). I was wondering if the tolerance value (or starting level) is something that could be dynamically adjusted by the start and end points.

It looked like the math behind it was doing some form of quick check against distances between points, not exactly studied it completely so I'm guessing it's some estimation along the bezier compared against the actual bezier curve? And then presumably when the estimated point fails distance wise it subdivides between that section of the curve (generating more points tighter to the curve). I was wondering if the tolerance value (or starting level) is something that could be dynamically adjusted by the start and end points.
ocornut commented 2022-09-27 16:56:07 +00:00 (Migrated from github.com)

What are you trying to solve?

What are you trying to solve?
Andersama commented 2022-09-27 17:08:37 +00:00 (Migrated from github.com)

Well I'm trying to work out in my head how I might set some minimum value so that that particular function always at least draws a line, I'm somewhat trying to figure out how I can maintain some minimum level of detail. The issue I have is that svgs aren't consistent in the slightest, I can trust the points are relative to where the should be to other points, but that's about it. The defaulted num_segments = 0 drawing a dynamic number of points seems handy to me, but now i know I can't rely on it directly, because I just had it not draw anything.

Well I'm trying to work out in my head how I might set some minimum value so that that particular function always at least draws a line, I'm somewhat trying to figure out how I can maintain some minimum level of detail. The issue I have is that svgs aren't consistent in the slightest, I can trust the points are relative to where the should be to other points, but that's about it. The defaulted num_segments = 0 drawing a dynamic number of points seems handy to me, but now i know I can't rely on it directly, because I just had it not draw anything.
ocornut commented 2022-09-27 17:15:53 +00:00 (Migrated from github.com)

Just leave it to the default value. It should always draw something given g.Style.CurveTessellationTol is left on default. You should investigate why you were clearing that data. Anyhow I don't imagine you can draw SVG with only available ImDrawList function you'll probably need to get lower level anyhow.

Just leave it to the default value. It should always draw something given g.Style.CurveTessellationTol is left on default. You should investigate why you were clearing that data. Anyhow I don't imagine you can draw SVG with only available ImDrawList function you'll probably need to get lower level anyhow.
Andersama commented 2022-09-27 17:21:37 +00:00 (Migrated from github.com)
    if ((d2 + d3) * (d2 + d3) < tess_tol * (dx * dx + dy * dy))
    {
        path->push_back(ImVec2(x4, y4));
    }
    else if (level < 10)
    {
        float x12 = (x1 + x2) * 0.5f, y12 = (y1 + y2) * 0.5f;
        float x23 = (x2 + x3) * 0.5f, y23 = (y2 + y3) * 0.5f;
        float x34 = (x3 + x4) * 0.5f, y34 = (y3 + y4) * 0.5f;
        float x123 = (x12 + x23) * 0.5f, y123 = (y12 + y23) * 0.5f;
        float x234 = (x23 + x34) * 0.5f, y234 = (y23 + y34) * 0.5f;
        float x1234 = (x123 + x234) * 0.5f, y1234 = (y123 + y234) * 0.5f;
        PathBezierCubicCurveToCasteljau(path, x1, y1, x12, y12, x123, y123, x1234, y1234, tess_tol, level + 1);
        PathBezierCubicCurveToCasteljau(path, x1234, y1234, x234, y234, x34, y34, x4, y4, tess_tol, level + 1);
    } else {
        path->push_back(ImVec2(x4, y4);
    }

Suppose I write a custom function which does this instead? That would force drawing points along the curve if it goes to the maximum level but fails to find anything...that should be how this works right?

```c if ((d2 + d3) * (d2 + d3) < tess_tol * (dx * dx + dy * dy)) { path->push_back(ImVec2(x4, y4)); } else if (level < 10) { float x12 = (x1 + x2) * 0.5f, y12 = (y1 + y2) * 0.5f; float x23 = (x2 + x3) * 0.5f, y23 = (y2 + y3) * 0.5f; float x34 = (x3 + x4) * 0.5f, y34 = (y3 + y4) * 0.5f; float x123 = (x12 + x23) * 0.5f, y123 = (y12 + y23) * 0.5f; float x234 = (x23 + x34) * 0.5f, y234 = (y23 + y34) * 0.5f; float x1234 = (x123 + x234) * 0.5f, y1234 = (y123 + y234) * 0.5f; PathBezierCubicCurveToCasteljau(path, x1, y1, x12, y12, x123, y123, x1234, y1234, tess_tol, level + 1); PathBezierCubicCurveToCasteljau(path, x1234, y1234, x234, y234, x34, y34, x4, y4, tess_tol, level + 1); } else { path->push_back(ImVec2(x4, y4); } ``` Suppose I write a custom function which does this instead? That would force drawing points along the curve if it goes to the maximum level but fails to find anything...that should be how this works right?
Andersama commented 2022-09-27 17:23:36 +00:00 (Migrated from github.com)

What was the original curve drawing function supposed to do with num_segments=0? Would that have not drawn anything eaither? Because then the recursive function could be split into two:

if (tess <= 0.0f) {
//original
} else {
//the above
}
What was the original curve drawing function supposed to do with num_segments=0? Would that have not drawn anything eaither? Because then the recursive function could be split into two: ```c if (tess <= 0.0f) { //original } else { //the above } ```
Sign in to join this conversation.
No milestone
No assignees
1 participant
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
ocornut/imgui#5713
No description provided.