Thread Rating:
  • 1 Vote(s) - 5 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Paths Basics
#1
Paths Basics

This is a short introduction to paths (also known as "vectors") as Gimp uses them.


Bézier Curves

In the sixties, Pierre Bézier was looking for a way to generate smooth curves in Computer-Aided Graphics and eventually settled on curves that have since been named after him. There are many types of Bézier curves but Gimp, as most graphics programs, uses the "cubic" ones. These are simple curves defined by:
  • Two points called the "anchors" (A1 and A2, below) that are the starting and ending point of the curve.
  • Two control points called the "handles" (H1 and H2, below), that indicate:
    • Using the line between the anchor and the handle: the general direction of the curve near the corresponding anchor. For the mathematically-oriented this line is the tangent of the curve on the anchor.
    • Using the distance between the anchor and the handle: how much the curve follows the direction of the handle.
   

If we bring H1 closer to A1 and drag H2 further from A2:

   

The closer the handle is to the anchor, the less influence it has on the shape of the whole curve. The limit is when the handle coincides with the anchor: in that case there is no tangent defined, and the general direction of the curve near the anchor is determined by the other handle. When there are no tangents defined at both ends, the curve becomes the straight line between the two anchors. In Gimp's path editor, anchors are usually created without handles, or, more accurately, with handles over the anchors, so a sequence of points creates a polygon, until you start dragging the handles out of the anchors.
We can even have loops and cusps:

   

Some nice properties of Bézier curves

The Bézier curves have a few properties worth remembering:
  • The whole curve is always inside the convex polygon defined by the two anchors and the two handles.
  • They are "compatible" with the simpler geometric transforms (a.k.a. "affine transforms") which are those that are found in Gimp's toolbox: shear, rotation, scale, flip(*). Which means that these two sequence of actions are equivalent:
    • "stroke" the curve to create a bitmap layer
    • use a transform tool on the layer
  • and
    • use a transform tool on the curve (in "Path" mode)
    • "stroke" the curve to create a bitmap layer
  • but of course the curve obtained with the second method will be sharper.
 
(*) The perspective transform isn't an affine transform, and in some cases, the two methods above would yield slightly different results, but in practice this still works most of the time, especially with paths obtained by Layer>Text to path.

Strokes

As elegant as they are, Bézier curves are not powerful enough to describe real-life curves such as the outline of characters. So more arbitrary curves are implemented by stringing together Bézier curves, the ending anchor of a curve also being the starting anchor of the next:

   

Each anchor becomes associated with two handles, one for the curve that ends on it (the "backward" handle) and one for the curve that starts from it (the "forward" handle). If these handles are aligned (A2) the curve remains smooth, but cusps can be obtained with handles on the same side (A3), and loops are obtained by interchanging the handles (or rotating them both by half a turn around the anchor)(A4).

In practice the stroke is defined and handled as a series of triplets of points, each triplet being made of the backward handle, the anchor, and the forward handle:

   

When doing so, the first and last anchor have one extra handle (backward for the first, forward for the last). These handles have a purpose: if you connect two strokes, the line between the two strokes is the Bézier curve controlled by these handles. Likewise, if you cut a stroke by Ctrl-Shift-clicking a segment to delete it, the new final anchors of the two resulting strokes keep the handles that controlled the removed segment.

Open and closed strokes

A similar principle is used to "close" strokes. When the stroke is marked "closed" (and this is just a supplemental closure indicator, Gimp doesn't add any point), an additional Bézier curve is generated from the last anchor to the first, under the control of these extra handles.

   

In Gimp, you mark the stroke as "closed" by connecting the first and last anchors.
Once the stroke is closed it is impossible to distinguish the actual first and last anchors, and in the picture above, the stroke could just as well start on A3 and end on A2. It is however possible to force Gimp to consider two anchors as the first and the last:
  • Given a stroke like this (the red part is the Bézier curve generated by the closure):
   
  • Open the stroke by deleting the segment between the two target anchors (2 and 3). These two anchors becomes the start and end of the open stroke. The handles are preserved:
   
  • Now, reconnect these anchors. The Bézier curve is regenerated, so the stroke shape is the same as initially, but the start and end anchors are the ones delimiting the temporarily deleted segment.
   
  • This technique can be generalized to have the stroke start on any point. Just add an anchor on the path by Control-licking the path where you want the stroke to start. Then use the technique above, removing and adding a segment that ends on that new anchor.
Stroke direction

Sometimes, the direction of the stroke is important. The stroke 1-to-4 above has the very same shape as the stroke obtained from the same triplets, ordered 4-to-1. But there are cases where the direction counts, for instance:
 
  • Using the "Text-along-path" function
  • Rendering the stroke by painting it with a gradient
Well, tough luck, there is nothing in out-of-the-box Gimp that will tell you the stroke direction, or let you change it. It just happens that Gimp doesn't usually change a stroke direction, so the first point of the stroke is usually the one you created first. But you have little control on the direction of strokes generated by Gimp (Layer>Text to path, or Select>To path).

But there are scripts available to help with this.


Paths

Strokes are powerful, but have a restriction: they are continuous lines. So, a single stroke is not powerful enough to describe complex curves such as the one needed to describe text. Enter the paths, that are nothing more than a bunch of strokes taken together. For instance, the word "hobbit" is made of 10 strokes:
  • One singe stroke for the 'h'.
  • Two strokes for the "o", the outer outline and the inner hole.
  • Two strokes for each "b", the outer outline and the inside for the loops.
  • Two strokes for the 'i', one for the body and one for the dot (a.k.a. "tittle").
  • One singe stroke for the 't'.
In uppercase, 'HOBBIT' is 11 strokes, since the 'B's have one more loop but the 'I' loses the dot.

Path handling in Gimp
  • Gimp processes the path as a whole, there is no way to restrict its action to specific strokes.
  • The path is associated to the canvas, not to a specific layer.
  • A path can have points that lie outside of the canvas
  • Coordinates of anchors and handles are not restricted to integer pixels. Sub-pixel paths are possible
  • Selection from paths where strokes are not disjoint follows an 'even-odd' rule. Starting un-selected from a border, the selection state changes each time it crosses a line. This is neither and intersection nor a union, but a rule that creates the expected selection on text (unless characters overlap).
Reply
#2
Quote:For instance, the word "hobbit" is made of 10 strokes:
  • One singe stroke for the 'h'.

  • Two strokes for the "o", the outer outline and the inner hole.

  • Two strokes for each "b", the outer outline and the inside for the loops.

  • Two strokes for the 'i', one for the body and one for the dot (a.k.a. "tittle").

  • One singe stroke for the 'h'.
In uppercase, 'HOBBIT' is 11 strokes, since the 'B's have one more loop but the 'I' loses the dot.

Just pointing the last 'h', where it missing the 't'. 
Although it does not change the 10 strokes at all. Eraser Text Zoom
Reply
#3
Yeah. Fixed Smile
Reply
#4
Thanks so much for the clear explanations, Ofnuts....

Much appreciated by us newbies....
Reply
#5
WOW!!! Brilliant! Agree with FlutteringBy, a great intro to paths for us newbies. Major Kudos!

I am restoring an old pinball machine that has a playfield in poor condition. I scanned it, and have been using Gimp to repair the scans with the goal of creating stencils for at least the black keylines that separate the areas of color. The straight lines are easy, but I'd struggled with curved ones, trying to freehand the fixes. Not a good idea - I'm fairly steady, but the results were poor, and I had to keep trying to fix things. Until I found this, my attempts to use the path tool were unsuccessful since I had no idea how they worked. Once I read this, though, I was able to quickly path and stroke the curved keylines. I'm lucky in that they are pretty simple curves, just the arc of a circle, so I only needed the two endpoints.

Again, thanks Ofnuts for the easy to understand intro!
Reply
#6
Two very nice videos if you want to know a bit more about Bézier curves (some math formulas may be flashing before you eyes, but there are also very nice animations):





Some more (the first 40mn)



Reply
#7
Wow, thank you for this detailed explanation about paths in GIMP Smile For geeks like me, I found this tutorial which approaches the Paths tool like the Pen tool very interesting (similar to your explanation).
Reply
#8
Being new to all this, I'm learning a lot. I'm pretty sure I understand the basic concepts and how it all works together. However, the problem I'm having so far is in the actual manipulation of things.
Every time I try to create a path, I seem to click on the wrong thing. It's like the node and the handles are all piled on top of each other. I think I'm clicking on a node when I'm actually clicking on a handle. So instead of creating a continuation of the line, I'm moving a handle around doing things I didn't want. Which leads to a bunch of CTRL Z's or erasing the whole thing out of frustration and walking away until I calm down enough to try again.
Hopefully, if I spend a few hundred hours practicing I'll get the hang of it. I've watched I don't know how many videos on how it works but I've yet to find one that helps me move my fumble fingers and gnarly hands to create the path I want.
I'm to the point where I just create nodes one after the other all the way around a curve just to get something done.
Reply
#9
(02-24-2024, 02:43 PM)Danbor Wrote: Being new to all this, I'm learning a lot. I'm pretty sure I understand the basic concepts and how it all works together. However, the problem I'm having so far is in the actual manipulation of things.
  Every time I try to create a path, I seem to click on the wrong thing. It's like the node and the handles are all piled on top of each other. I think I'm clicking on a node when I'm actually clicking on a handle. So instead of creating a continuation of the line, I'm moving a handle around doing things I didn't want. Which leads to a bunch of CTRL Z's or erasing the whole thing out of frustration and walking away until I calm down enough to try again.
  Hopefully, if I spend a few hundred hours practicing I'll get the hang of it. I've watched I don't know how many videos on how it works but I've yet to find one that helps me move my fumble fingers and gnarly hands to create the path I want.
  I'm to the point where I just create nodes one after the other all the way around a curve just to get something done.

In these cases, hold down the CTRL key and click on the node. This way you will select the handle and not the node.
                               .....
Samj PortableGimp 2.10.28 - Win-10 /64.
Reply
#10
I just need more practice using the tool. Using the keyboard in combination with the mouse is not something I'm familiar with doing.
So, to use it, you left click and release to place the first node. Next, left click to place the second node, then, while still left clicked, press the ctrl key to adjust the curvature of the connecting line. Release the left click and ctrl key, move pointer to next position, left click again to place it, hold ctrl while still left clicked to adjust the curvature of new connecting line. And so on.
Have I got that right?
Reply


Forum Jump: