Gimp-Forum.net
Rounding corners of a straight-edge path - Printable Version

+- Gimp-Forum.net (https://www.gimp-forum.net)
+-- Forum: GIMP (https://www.gimp-forum.net/Forum-GIMP)
+--- Forum: Extending the GIMP (https://www.gimp-forum.net/Forum-Extending-the-GIMP)
+--- Thread: Rounding corners of a straight-edge path (/Thread-Rounding-corners-of-a-straight-edge-path)

Pages: 1 2


Rounding corners of a straight-edge path - Ottia Tuota - 05-27-2021

I made a plugin to round path corners. (Again I can't help wondering if I am just re-inventing the wheel. But at least it is a new wheel.) The plugin assumes that the path consists of straight line segments. Example: With default input values the plugin did this:

[attachment=6076]

To get the plugin, go to

http://kmarkku.arkku.net/Path_modify_files/Gimp_master.html

scroll to the bottom, and click the right download button. You get a zip file. Unzip it and place the one file it contains (round_path_corners.py) in your user's plug-ins folder. Then (re)start Gimp. To use the plugin: in the Paths tab, right-click a path and follow the links Tools > Modify path > Round corners.

I explain a little about the inputs.

The first input is "Where to set arc ends". It offers two choices: the default is "Use fixed distance from vertices", and the other one I skip now entirely. The value for 'distance' is input in the next field. The picture shows the meaning of "distance":

[attachment=6077]

(One fact may cause surprises: In the code it is prohibited that the arc ends could go past the midpoints of the corner legs. The purpose is to avoid that two neighbouring arcs might get mixed up with each other.)

The next input allows the user to choose the shape of the arc. The list of the built-in shapes is:
  • circular
  • straight
  • parabola
  • close to vertex
  • through vertex
  • cusp

In addition, there is a customizable shape about which I talk in a subsequent post; it takes one parameter which is the next input, and I say nothing about that now. The built-in shapes are shown in pictures:

[attachment=6078]

Finally, the last input is "How to use the selection?" which allows the user to make roundings only in some part of the path, keeping the rest untouched (just about). About this I give one example. Suppose you have a path as on the left below. You want to round the corners but want to preserve the long arc. Running the plugin as such would lose the arc (the plugin assumes that the path consists of straight edges and ignores all curvatures). You can protect the long arc as follows: (1) Create a new anchor; (2) make a selection; (3) call the plugin with "Round the corners only inside the selection." The result may be close to what you want, hopefully. (Or maybe not. Some manual tweaking may be needed.)

[attachment=6079]

Writing the plugin had complications, as always, and I am not sure that I got everything right. Please tell me if you find the plugin doing anything unruly.

I explain now a little about how the plugin makes the rounding arcs. (I hesitate posting this since hardly anybody wants to read such details, but I think that maybe, just maybe, somebody somewhere will find it useful. And it is easy just to skip it.)

The plugin uses two different algorithms:

  1. An algorithm to make circular arcs.
  2. An algorithm customizable with one parameter (this handles all other cases except the circular arcs).
Some words about each. For simplicity, I assume that we are using the option "Use fixed distance...".

Algorithm 1

The algorithm to draw circular arcs is based on the well-known idea. But it is here in a disguise: Instead of a center and central angle, it takes as input a tangent triangle.

Algorithm 2

The algorithm customizable with one parameter takes as input one parameter (in addition to the corner and the distance from the vertex). Look at the picture below. It shows how the control points are chosen by the algorithm. First, |AC| = |BC| = distance (the input value). The arc should be drawn from A to B. It is done by control points A,X,Y,B where X and Y are chosen from the legs so that |AX|/|AC| = |BY|/|BC| = parameter. So that is the definition the algorithm uses.

[attachment=6080]


In the GUI of the plugin you may wonder what the numbers in the list of built-in shapes mean:

straight (0)
parabola (2/3)
close to vertex (1)
through vertex (4/3)
cusp (2)

They are the preset parameter values; they are hard-coded in the built-in cases, and with those parameter values the algorithm draws a straight line, a parabola, and so on.

But in addition to these preset values, you may use any custom value: choose the option "one-parametric custom shape" and input some value for the parameter (default is 0.5). For instance, you may want to try arcs flatter than circular but not quite straight; try then parameter=0.3, for example. Any float number can be used, even negative (but not in fractional form unfortunately).

Another example:

[attachment=6081]

Here the plugin was applied to the hexagon with "Use midpoints of edges" and custom shape with parameter = 6.

Some random thoughts: I am thinking that it might be nice to have more flexibility in determining the shape of the arcs. But on the other hand I think that it would not be worth the trouble. It would mean more parameters and would therefore be difficult to grasp and difficult to use. But if a good scenario for such function could be designed (flexible, versatile, simple, easy to grasp and control - is it possible?) then I think it would be easily added into the plugin. I already had a two-parametric custom shape there. But I dropped it. Too difficult, too little gain.


RE: Rounding corners of a straight-edge path - rich2005 - 05-27-2021

Nice plugin. Still to explore all the options. There is an old (compiled) plugin "smooth-path" been around a long time, bit of a square wheel, needs reinventing.

Just for information:

[attachment=6083]


RE: Rounding corners of a straight-edge path - Ottia Tuota - 05-27-2021

Thanks. The effect of the old "smooth-path" seems to be quite different. From the picture you posted it is clear that it makes the smoothed path to run through the anchors of the original path. My plugin does not do that (except for the option "through vertex" and even there the result is a quite different curve).


RE: Rounding corners of a straight-edge path - Ofnuts - 05-27-2021

Quote:The algorithm to draw circular arcs is based on the well-known idea.

Is that the one that uses kappa=(4/3)*(sqrt(2)-1) (from memory)? The shallowness of the proof of that one has always intrigued me. But it does work with all angles...


RE: Rounding corners of a straight-edge path - Ottia Tuota - 05-27-2021

(05-27-2021, 05:24 PM)Ofnuts Wrote:
Quote:The algorithm to draw circular arcs is based on the well-known idea.

Is that the one that uses kappa=(4/3)*(sqrt(2)-1) (from memory)? The shallowness of the proof of that one has always intrigued me. But it does work with all angles...

Yes that is the one. I took that very formula and with some trigonometric manipulation changed it to suit my current purpose. Namely, I was drawing circular arcs without knowing the centers. But I had tangent triangles available. The code became pleasantly simple. You can find it in the plugin. Search for "def circlular_arc_in_tangent_triangle" (yes I notice now that there is a typo!). Note that I use the plane as the complex number plane. So, instead of [x,y] all points are complex numbers x+iy.

Edit: No, I said wrong. I did not take quite that formula but a more general one that works for circular arcs with central angle other than 90 degrees. The formula is: (4/3)*tan(theta/4) where theta is the central angle.


RE: Rounding corners of a straight-edge path - Ofnuts - 05-28-2021

(05-27-2021, 05:37 PM)Ottia Tuota Wrote:
(05-27-2021, 05:24 PM)Ofnuts Wrote:
Quote:The algorithm to draw circular arcs is based on the well-known idea.

Is that the one that uses kappa=(4/3)*(sqrt(2)-1) (from memory)? The shallowness of the proof of that one has always intrigued me. But it does work with all angles...

Yes that is the one. I took that very formula and with some trigonometric manipulation changed it to suit my current purpose. Namely, I was drawing circular arcs without knowing the centers. But I had tangent triangles available. The code became pleasantly simple. You can find it in the plugin. Search for "def circlular_arc_in_tangent_triangle" (yes I notice now that there is a typo!). Note that I use the plane as the complex number plane. So, instead of [x,y] all points are complex numbers x+iy.

Edit: No, I said wrong. I did not take quite that formula but a more general one that works for circular arcs with central angle other than 90 degrees. The formula is: (4/3)*tan(theta/4) where theta is the central angle.

That's still the one I think; the value I mentioned is for theta=π/2. It is based on the assumption that the best Bezier approxmation is when the intersection of the Bezier curve and the bisector is on the true circle, but there are possibly better criteria, such as minimizing the area between the true circle and the curve. The main merit of that approximation is that it is easy to compute for all angles.


RE: Rounding corners of a straight-edge path - Ottia Tuota - 05-29-2021

(05-28-2021, 03:32 PM)Ofnuts Wrote:
(05-27-2021, 05:37 PM)Ottia Tuota Wrote: ...


Edit: No, I said wrong. I did not take quite that formula but a more general one that works for circular arcs with central angle other than 90 degrees. The formula is: (4/3)*tan(theta/4) where theta is the central angle.

That's still the one I think; the value I mentioned is for theta=π/2. It is based on the assumption that the best Bezier approxmation is when the intersection of the Bezier curve and the bisector is on the true circle, but there are possibly better criteria, such as minimizing the area between the true circle and the curve. The main merit of that approximation is that it is easy to compute for all angles.

And for central angles <= 90 degrees the error in that formula will be smaller than line thickness in a drawing. That is good enough.

But it would be nice to have a formula that works reasonably well up to 180 degrees. The usual remedy is to split arcs so that a 180 degrees arc is drawn as two 90 degrees arcs. That means more control points.

In this plugin this formula is used for all circular arcs, without any splitting, even up to close 180 degrees. You see, in the particular situations where this plugin is applied, the circular arcs with such large central angles will be so small that nobody will notice anything... Cheating? No, I think it is just good enough. But perhaps I will have a better solution some day. I have a little idea now... we shall see.


RE: Rounding corners of a straight-edge path - Ottia Tuota - 06-27-2021

A bug fix. New version 0.3 is at the old place

http://kmarkku.arkku.net/Path_modify_files/Gimp_master.html

For an explanation of the bug and a workaround of a difficulty, see

https://www.gimp-forum.net/Thread-Simplifying-a-path?pid=24258#pid24258


RE: Rounding corners of a straight-edge path - Ottia Tuota - 06-30-2021

I updated the three plugins under Tools > Modify path: Round corners, Simplify, and Simple smooth.

No functional changes. The only change is better wording in the GUIs, following a suggestion by PixLab.


RE: Rounding corners of a straight-edge path - Ottia Tuota - 11-16-2021

I updated the corner-rounding plugin to version 0.6. To get it go to

http://kmarkku.arkku.net/Path_modify_files/Gimp_master.html

and push the Download button and unzip the file you get and place it in your Gimp's plug-ins folder (replacing the older version) and restart Gimp. Then you find the plugin in Gimp's menu (in the paths dock) under

Tools > Modify path > Round path corners

I deleted some functionalities that I thought not essential. If you disagree, raise your voice. There are some new options. The GUI looks now as follows:

[attachment=7070]

At first the GUI asks if you want symmetric or asymmetric arcs. To show the difference I made a rough rectangle. First I called  the plugin with otherwise the default values except that I chose 'parabola' for the shape of the arcs.

[attachment=7071]

The result is on the left: at each corner we got a parabolic arc. And each arc is symmetric. On the right I chose to use asymmetric arcs. Again we have parabolic arcs but they are not symmetric. The arcs run between the midpoints of edges (that is the default action).

In the symmetric and asymmetric cases the plugin needs different inputs. When you are making symmetric arcs, the relevant input is "Give the length...". The meaning is shown on the left below.

[attachment=7072]

When you are making asymmetric arcs you fill the input "Give the proportion ...". If you change the input from the default 0 to 50, say, the plugin leaves a straight part in the middle of each edge, 50% of the edge length. See the picture on the right where I used the value 20 (percents), getting 20% straight parts on each edge.

The available arc shapes are:
  • circular (this choice available only for symmetric arcs)
  • logarithmic spiral arc (approx)
  • parabola
  • osculating
  • straight
  • custom shape -- set the custom parameter value below
I say now a little of each.

Circular is clear. Not available for asymmetric arcs.

Logarithmic spiral arc makes what the name says, approximately. The approximation is rather good except for narrow corners. As I see it, circular arcs are always symmetric, and logarithmic spiral arcs are their counterparts in the asymmetric case. After all, the question is of direct generalization: a circle is a special logarithmic spiral.

Parabola makes a parabola arc, exactly.

Osculating is what was in the previous version called "Close to vertex". I changed there now the proper name. Osculating means that the arcs will have curvature 0 where they meet the corner legs; the arcs meet the legs very very closely. The osculating arc is the closest we get to the corner vertex with a single Bézier arc when we don't want to cross the legs.

Straight makes straight line segments.

Custom shape gives you a one-parameter method to adjust the arc. If you choose "Custom shape" then you supply the parameter value in the next input. Best is to experiment. Parameter=0 gives straight lines and Parameter=1 gives osculating arcs. In the previous version there were choices "Through vertex" and "Cusp". I removed those choices but you still get them as custom arcs by setting Parameter=1.3333333 and Parameter=2, respectively. Any value is possibly, even negative.

And as before, there is the option to restrict the effect by means of a selection.

Any suggestions are welcome.