Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Making G2-continuous paths
#1
I made a plugin which, given a path as input, creates a smooth path through its anchors. You may remember that I already made one such simple plugin:

https://www.gimp-forum.net/Thread-Smooth...ple-plugin

but now I have a better algorithm. Instead of that simple plugin, this new plugin (named "G2-continuity") should be compared to the old smooth-path.exe:

https://www.gimp-forum.net/Thread-Rich-I...0#pid28230

That old plugin makes C2-continuous paths. The new plugin does that too, but it makes also more general G2-continuous paths.

To get the plugin, go to

http://kmarkku.arkku.net/Path_modify_fil...aster.html

scroll to the bottom, and click the right download button ("G2-continuity"). You get a zip file. Unzip it and place the one file it contains (G2_continuity.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 > G2-continuity.

A picture using the default values:

   

The blue path is the input path, and the red path is the path made by the plugin. So, this is another method to draw a smooth path through some given points.

We had some discussion of C2-continuity with Ofnuts in:

https://www.gimp-forum.net/Thread-Rich-I...6#pid28246

C2-continuity is about derivatives of the parametric representation of the curve. So, it is very mathematical. We use now another concept, G2-continuity (the 'G' coming from "geometric"), which is more directly connected with the visual appearance of the curve and independent of the parametric representation. It means that the curve is smooth and has continuous curvature. For paths G2-continuity means:
  1. At every anchor the two tangents are parallel (the same). But the handles need not be symmetric, that is, of equal length.
  2. At every anchor the two curvatures are equal. This means that the two circles of curvature at the anchor are the same.
C2-continuity implies G2-continuity but not conversely. You may think that C2-continuity is better. After all, it is a stronger condition, so it should make more regular curves, right? But it has one feature I don't quite like. Consider a straight-edge path with very different edge lengths:

   

C2-continuity makes at the short edge an unnecessarily prominent arc. I would like better a smaller arc, see the picture. The reason why C2-continuity gives such a strong arc is the continuity of the first derivative: it forces the handles at an anchor to be of equal length (symmetric). But I think it would be better if the handle on the longer edge would be longer and the handle on the shorter edge would be shorter, like this:

   

So I think that the edge lengths should be taken into account when making the handles. But then C2-continuity is no longer possible, not even C1-continuity. But G2-continuity is possible. And I think that it is this condition which really counts visually. Continuous curvature means that the curve is very smooth.

So I took the theory in

https://www.stkent.com/2015/07/03/buildi...urves.html

and modified it suitably to take into account edge lengths, producing G2-continuous paths (and since that can be done in infinitely many ways I had to do some ad hoc choices). I also did the case of closed strokes and added some embellishments. No need to go into further details. It was rather pleasing work to do.

Be kind enough to tell me if you find cases where the plugin works wrong.

There are two tuning parameters. I try to explain what they do though you may do wisest if you just go experimenting.

The first tuning parameter: Let us consider a straight-edge path. The tuning parameter adjusts how strongly the different lengths of the edges are taken into account; see the above pictures. Let us call the parameter K. If K=0 the lengths are not taken into account at all, just like in C2-continuity. I put K=1 as the default since I think it gives nice results.

The second tuning parameter determines how tightly the curve will bend at anchors. C2-continuity allows no such adjustings, but with G2-continuity this can be done.

The way I implemented the two tuning parameters is very ad hoc and could be done in infinitely many different ways, still getting G2-continuity. I could easily make the plugin to have 10 tuning parameters, say, but who would want it? I think 2 parameters is optimal.

There is also an option on how the end parts of open strokes are treated. This is useful only if the original path has some curvature and you want to preserve the tangent directions at open stroke ends:

   

This option arose from a mathematical reason. If you look at the text by Stuart Kent you see that if an open stroke has n+1 anchors (knots) then the requirement of C2-continuity gives us a big group of equations with 2n unknowns and 2n-2 equations. So, to solve the group we need two additional equations. They can be chosen freely. For those two, Kent takes what he calls "natural boundary conditions": f''=0 at each end of the stroke. It occurred to me that we could use some other boundary conditions. I chose to preserve tangent directions at the ends; this seemed neat, easy to grasp, and sometimes useful.

There are two options about using a selection. They should be clear enough, so I say nothing about those here.
Reply
#2
Pretty cool. But any way to prevent loops?


.xcf   G2Loop.xcf (Size: 4.22 KB / Downloads: 219)
Reply
#3
(03-18-2022, 06:20 PM)Ofnuts Wrote: Pretty cool. But any way to prevent loops?

No, except by changing the values for the parameters. Of course we could develop the theory further. I modified it to take into account edge lengths, and we could of course use some other criteria. The question is, what criteria, and who would do it ...
Reply
#4
(03-18-2022, 06:42 PM)Ottia Tuota Wrote:
(03-18-2022, 06:20 PM)Ofnuts Wrote: Pretty cool. But any way to prevent loops?

No, except by changing the values for the parameters. Of course we could develop the theory further. I modified it to take into account edge lengths, and we could of course use some other criteria. The question is, what criteria, and who would do it ...

Ok, I stumbled on the problem by making  a quick test: take a "natural" shape, make a path (with only anchors), and apply the script to see how close the resulting path is to the real thing. This wants to represent the rather frequent use case of having a shape with long curves and not wanting to adjusts all the tangents.

Since it was a plane, there were sharp angles and protruding antennas. The script does a fairly good job on smooth curves, even on relatively tight bends:

   
(blue: anchors-only manual path, red: G2-continuity, click for bigger size)

But of course it softens angles even where it is not wanted, but then on such angles a user would put tangents... So a possible criterion is to stop the smoothing on anchors where one or more tangents are defined, and of course, keep the tangents as boundary conditions.
Reply
#5
(03-18-2022, 08:54 PM)Ofnuts Wrote: ...

But of course it softens angles even where it is not wanted, but then on such angles a user would put tangents... So a possible criterion is to stop the smoothing on anchors where one or more tangents are defined, and of course, keep the tangents as boundary conditions.

Currently one can use a selection to exclude some anchors. But it could be automated just as you say. That is an idea. i must think about it.

(03-18-2022, 06:42 PM)Ottia Tuota Wrote:
(03-18-2022, 06:20 PM)Ofnuts Wrote: Pretty cool. But any way to prevent loops?

No, except by changing the values for the parameters. Of course we could develop the theory further. I modified it to take into account edge lengths, and we could of course use some other criteria. The question is, what criteria, and who would do it ...

On the second thought, perhaps loops are unavoidable. Mathematically it is now building a big system of linear equations and then solving it. It comes down to inverting a big matrix, and that is easy. It seems to me that avoiding loops would mean that, in addition to equations, we should put there some inequalities to rule out unacceptable solutions. Might even lead to non-linear equations. I don't know how to do it, and anyway it would be quite a different problem.

One can of course do manual corrections to the resulting path, but that destroys G2-continuity locally, though the path can visually still be quite smooth. So perhaps we should just say that occasional loops are essential in the G2 job.

Changing parameters may help. But in that difficult example of yours, if we use the default values except that we put the second tuning parameter to 10, say, then there is no loop but all segments become almost straight and the sharp point is rounded only marginally. That is hardly what one would want. And the plugin "Simple smooth" makes a double loop. I leave this as an open question.
Reply
#6
Quote:On the second thought, perhaps loops are unavoidable.

Still is a nice useful plugin and much appreciated, especially the selection include/exclude option. Easy to use quick-mask to 'dot-in' selections around control points.
Reply
#7
Ottia Tuota, nice plugin to make a quick selection : set a few points, launch 2G (is working at the same speed) and correct te paths. A five minute job. A rough example on a new background :
     
Thanks
Reply
#8
Thanks, Rich and denzjos. Nice to hear that it works ok and may be useful. I am now thinking about the suggestion of Ofnuts. Simple idea, until one starts pondering how to add it to the current plugin, not to mention implementation details...
Reply
#9
Now the plugin has a new version. I removed some functionalities. Instead, I implemented what Ofnuts suggested, the option to preserve non-zero handles (post #4, though perhaps what I made is not quite what he had in mind).

I talk now about that feature only.

Suppose you have a path where there is a corner that should not be smoothed, rather its shape should be preserved. Here is a picture:

   

On the left is the input path. All handles are zero except that the upmost corner has two non-zero handles which determine the shape of the corner. Call the plugin with proper inputs, and it smooths the path elsewhere but preserves those two handles, on the right.

This feature is governed by two inputs in the GUI. The first is a boolean switch; default is 'No', and the feature becomes activated if you click it to 'Yes'. The second input asks for a threshold for "long" handles. Namely, the feature affects only handles which are longer than the threshold. Default is 5.0 pixels.

   

That threshold serves also as a safety guard. It happens very easily (to me at least) when creating a path that where I think a handle is zero it actually is not quite zero. The threshold ensures that such very short handles are ignored by the plugin.

The next picture shows a case where there is some anchor where one handle is "long" (longer than the threshold) and the other is of zero length. On the right is the result: that "long" handle is preserved and everything else is smoothed.

   

The following picture shows a case where there is one segment with a "long" handle at each end. Those handles are preserved, hence the whole segment is preserved:

   

The plugin makes no wonders. Manual corrections may be necessary afterwards. The following rules hold:
  1. If an anchor has two "long" handles they are preserved. The shape of the corner is preserved in the immediate vicinity of the anchor but the adjacent segments are usually slightly deformed since one handle is not enough to define the shape of a segment.
  2. If an anchor has only one "long" handle it is preserved and the other handle is made such that the path is smooth there (this is G1-continuity only).
  3. If an anchor has no "long" handles the path will be made G2-continuous there (that means that the path is smooth and even has continuous curvature there).
Note: If you want to preserve a corner you have to put there two "long" handles (that is, handles exceeding the threshold). If only one of the two handles is "long" the path will be smoothed there; see the pictures.

As before, you can use a selection to restrict the effect. And you can experiment with the two tuning parameters.

The new version is 0.8 at the old place:

http://kmarkku.arkku.net/Path_modify_fil...aster.html

Scroll to the bottom, and click the right download button ("G2-continuity"). You get a zip file. Unzip it and place the one file it contains (G2_continuity.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 > G2-continuity.

Please report any erronous workings. It is getting rather complicated and I am not at all confident that it works all right in all cases. But we can hope for the best.
Reply


Forum Jump: