Tk Source Code

View Ticket
Login
2026-10-23
10:55 • Closed ticket [974600ff]: Gaps in Line sections plus 6 other changes artifact: 1d05c40d user: serhiy.storchaka
2021-12-05
08:18 • Closed ticket [5fb81459]: Canvas redraw region too small when inserting points to a smoothed line plus 6 other changes artifact: dfdea72d user: fvogel
2021-12-04
19:26
Fix [5fb8145997]: Canvas redraw region too small when inserting points to a smoothed line check-in: 1fe355fb user: jan.nijtmans tags: core-8-6-branch
16:42 • Ticket [5fb81459] Canvas redraw region too small when inserting points to a smoothed line status still Open with 3 other changes artifact: 691878fa user: fvogel
2021-11-05
21:55 • Ticket [5fb81459]: 3 changes artifact: fe5272ad user: fvogel
21:26 • Ticket [5fb81459]: 3 changes artifact: f170214d user: fvogel
2021-10-17
21:04 • Ticket [5fb81459]: 3 changes artifact: 89a72b43 user: fvogel
20:59
Add (theoretically) missing line. See ticket [5fb8145997] for explanations about why this line is not needed in fact, but nevertheless included. check-in: 6f0bf10e user: fvogel tags: bug-5fb8145997
2021-10-15
20:39 • Ticket [5fb81459] Canvas redraw region too small when inserting points to a smoothed line status still Open with 3 other changes artifact: 4a7d13be user: fvogel
08:50 • Ticket [5fb81459]: 4 changes artifact: 4d5f828d user: helium
2021-10-10
19:38 • Ticket [5fb81459]: 3 changes artifact: 58c7e4de user: fvogel
18:35 • Ticket [5fb81459]: 3 changes artifact: e691f60f user: fvogel
17:59 • Ticket [5fb81459]: 4 changes artifact: 7309acb6 user: helium
15:42
Let comments in the optimization code point at ticket [5fb8145997] since there is such a good explanation there about how optimization has to be done. check-in: c37ad166 user: fvogel tags: bug-5fb8145997
15:06
Let comments in the optimization code point at ticket [5fb8145997] since there is such a good explanation there about how optimization has to be done. check-in: b3ef2dcd user: fvogel tags: bug-5fb8145997
14:49 • Ticket [5fb81459] Canvas redraw region too small when inserting points to a smoothed line status still Open with 3 other changes artifact: 2281199d user: fvogel
08:56 • Ticket [5fb81459]: 4 changes artifact: ae9b2f7c user: helium
08:50 • Ticket [5fb81459]: 4 changes artifact: 32f9e5c6 user: helium
08:47 • Add attachment canvas-line-testbed-arrowbug.png to ticket [5fb81459] artifact: 4c32cda0 user: helium
08:42 • Add attachment canvas-line-testbed.tcl to ticket [5fb81459] artifact: 34b34123 user: helium
2021-10-09
18:39 • Ticket [5fb81459] Canvas redraw region too small when inserting points to a smoothed line status still Open with 4 other changes artifact: f64bc260 user: helium
17:10 • Add attachment analysis-cubic-spline.png to ticket [5fb81459] artifact: 7d67ba8c user: helium
17:09 • Add attachment analysis-cubic-spline.tcl to ticket [5fb81459] artifact: 402f312f user: helium
14:11 • Ticket [5fb81459] Canvas redraw region too small when inserting points to a smoothed line status still Open with 3 other changes artifact: ac209ebc user: fvogel
2021-10-02
17:41 • Ticket [5fb81459]: 4 changes artifact: 6a44940e user: helium
17:26 • Add attachment canvas-line-testbed.tcl to ticket [5fb81459] artifact: 3313bd2e user: helium
2021-09-29
03:53 • Ticket [5fb81459] Canvas redraw region too small when inserting points to a smoothed line status still Open with 3 other changes artifact: 9d7d1a14 user: fvogel
2021-09-28
09:10 • Ticket [5fb81459]: 3 changes artifact: 02e62d3e user: fvogel
2021-09-27
19:47 • Ticket [5fb81459]: 3 changes artifact: 3cf62b4d user: fvogel
19:43
Rework optimization code when inserting or deleting points of a canvas line. This now works for both -smooth no and -smooth true (bezier), as demonstrated by the test script 'canvas-line-testbed.tcl' posted in [5fb8145997]. Case -smooth raw still needs to be fixed. check-in: 2f80202c user: fvogel tags: bug-5fb8145997
18:12 • Ticket [5fb81459] Canvas redraw region too small when inserting points to a smoothed line status still Open with 3 other changes artifact: e5bc6b9c user: fvogel
2021-09-26
10:26 • Ticket [5fb81459]: 4 changes artifact: 15cd8fd6 user: helium
10:21 • Add attachment canvas-line-testbed.png to ticket [5fb81459] artifact: 331e44f2 user: helium
10:20 • Add attachment canvas-line-testbed.tcl to ticket [5fb81459] artifact: bf5fbf39 user: helium
2021-09-25
19:08 • Ticket [5fb81459] Canvas redraw region too small when inserting points to a smoothed line status still Open with 4 other changes artifact: e8c3bb11 user: helium
18:57 • Add attachment analysis-quadratic-spline.png to ticket [5fb81459] artifact: 747fbf54 user: helium
18:56 • Add attachment analysis-quadratic-spline.tcl to ticket [5fb81459] artifact: 8e4c8d2c user: helium
11:21 • Ticket [5fb81459] Canvas redraw region too small when inserting points to a smoothed line status still Open with 3 other changes artifact: d79dc56e user: fvogel
09:55 • Ticket [5fb81459]: 4 changes artifact: c4337e24 user: helium
2021-09-20
06:30 • Ticket [5fb81459]: 3 changes artifact: a233ee88 user: fvogel
2021-09-19
20:21 • Ticket [5fb81459]: 3 changes artifact: b369a150 user: fvogel
19:57 • Ticket [5fb81459]: 4 changes artifact: 6234e973 user: fvogel
19:32
Fix [5fb8145997]: Canvas redraw region too small when inserting points to a smoothed line check-in: 5da2de43 user: fvogel tags: bug-5fb8145997
2021-09-16
12:49 • Ticket [5fb81459] Canvas redraw region too small when inserting points to a smoothed line status still Open with 4 other changes artifact: c1507d4e user: helium
2021-09-15
19:40 • Ticket [5fb81459]: 3 changes artifact: e32dd9b2 user: fvogel
07:45 • Ticket [5fb81459]: 5 changes artifact: 87d014cb user: helium
2021-09-14
19:21 • Ticket [5fb81459]: 3 changes artifact: be149de0 user: helium
19:19 • Add attachment bugreport-splines-explained-example.png to ticket [5fb81459] artifact: d628cdef user: helium
19:17 • New ticket [5fb81459] Canvas redraw region too small when inserting points to a smoothed line. artifact: be5d695d user: helium

Ticket UUID: 5fb81459976c4f14c09fc18268ab4fa7a2b17316
Title: Canvas redraw region too small when inserting points to a smoothed line
Type: Bug Version: 8.6.10
Submitter: helium Created on: 2021-09-14 19:17:49
Subsystem: 05. Canvas Items Assigned To: fvogel
Priority: 5 Medium Severity: Minor
Status: Closed Last Modified: 2021-12-05 08:18:29
Resolution: Fixed Closed By: fvogel
    Closed on: 2021-12-05 08:18:29
Description:

While developing a vector drawing program, I discovered that the Tk canvas does not correctly redraw a smoothed line item when coordinate pairs are added at the end of its coordinate list.

Steps to reproduce:

pack [canvas .canvas -width 500 -height 250]
.canvas create line 40 200 160 40 280 200 -smooth true -tag myLine
.canvas insert myLine end {400 40}
.canvas move myLine 0 0

The last step refreshes the entire line object, removing the discontinuity observed after the prior step.

The following script (screenshot attached from Linux/X11) shows these steps in an animation:

#!/usr/bin/wish
# Bug observed with Tk versions 8.5.19 and 8.6.8

wm resizable . 0 0
pack [canvas .c -width 500 -height 250]
pack [label .l]

proc mark_point {x y} {
	.c create oval [expr {$x-2}] [expr {$y-2}] [expr {$x+2}] [expr {$y+2}]
}

proc exec_and_show {args} {
	{*}$args
	.l configure -text $args
}

mark_point 40 200
mark_point 160 40
mark_point 280 200
# Both kinds of smoothing are affected, here we use "true" since it
# needs less points than "raw" for a demonstration.
exec_and_show .c create line 40 200 160 40 280 200 -smooth true -tag myLine

update
after 5000
mark_point 400 40
exec_and_show .c insert myLine end {400 40}

update
after 2000
exec_and_show .c move myLine 0 0

Note that if I use a text on the canvas itself for displaying the commands at each step, the redraw region gets bigger and the bug does not appear:

#!/usr/bin/wish

wm resizable . 0 0
pack [canvas .c -width 500 -height 250]

proc mark_point {x y} {
	.c create oval [expr {$x-2}] [expr {$y-2}] [expr {$x+2}] [expr {$y+2}]
}

.c create text 250 240 -anchor center -tag text

proc exec_and_show {args} {
	{*}$args
	.c itemconfigure text -text $args
}

mark_point 40 200
mark_point 160 40
mark_point 280 200

exec_and_show .c create line 40 200 160 40 280 200 -smooth true -tag myLine

update
after 5000
mark_point 400 40
exec_and_show .c insert myLine end {400 40}

update
after 2000
exec_and_show .c move myLine 0 0

User Comments: fvogel added on 2021-12-05 08:18:29:
Jan did the merge for me, many thanks to him.

Closing this ticket now, with great thanks to the OP for his detailed analysis and help.

fvogel added on 2021-12-04 16:42:13:
I have tried merging the bugfix branch into core-8-6-branch and main. When merging into main there are conflicts due to the change int-->TkSizeT for some variables. I have asked Jan's help regarding this merge.

fvogel added on 2021-11-05 21:55:14:

[cc743eb9db] implements a better fix for this last issue. This deals with the real problem that in the last bug case, we insert at position 0, therefore when dealing with -smooth raw beforeThis is negative (value is -2) and the % (reminder) operator returns a negative result for (beforeThis % 6), which means beforeThis becomes 0 while we want beforeThis to become -4. Adding abs() solves this issue.


fvogel added on 2021-11-05 21:26:48:

With [cadf5106] I believe I have fixed the last known issue with the optimization code. That was when inserting points at position zero in a polygon.

The problem could for instance be seen from your applicatiopn testbed with:

original 145 132 124 116 129 85 160 68 205 58 283 111
additional 226 136 205 146 176 140
index 0; splinetype raw; operation insdel; type polygon

or, equivalently, directly in Tcl/Tk with:

pack req Tk
pack [canvas .c -width 500 -height 315]
.c create polygon 145 132 124 116 129 85 160 68 205 58 283 111 -tag myLine -width 4 -smooth raw -fill {} -outline black
# wait for display update then paste next line
.c insert myLine 0 {226 136 205 146 176 140}

I can't trigger any issue any longer, with either line items or polygon items, for any smoothing method.

If you can review this latest state I would be happy to read your comments. Thanks!


fvogel added on 2021-10-17 21:04:48:

I have understood why this change (the added "objc += 2;" line) is not needed.

The case happens when inserting at the last position of the line. Initially, beforeThis then equals length and objc is at least 2.

Then, one adds one point at each side of the insert position, and (since -smooth is true) another point at each side, i.e. beforeThis becomes length-4 and objc becomes at least 10.

Now, since we're inserting at the last position, we add a third point at the left and beforeThis becomes length-6.

Later, at lines 1094-1096, objc is adjusted so that beforeThis+objc does not exceed length. At this point the condition beforeThis+objc equals length+4 at least, which, obviously, is larger than length. objc is therefore always reduced: objc = length - beforeThis;.

To conclude, the added "objc += 2;" line is indeed correct in principle, but objc will always be reduced to length - beforeThis;, meaning the added "objc += 2;" line is useless in the computation.

That now said, I have added this extra line even if useless, because it is indeed correct in principle and it helps understanding the code. This is [6f0bf10ed3].


fvogel added on 2021-10-15 20:39:46:
This should be right indeed. I'm puzzled by the fact we cannot evidence this bug by running your test tool. That's rather strange and inconsistent.

helium added on 2021-10-15 08:50:00:

In the meantime I managed to compile [0733eac0] and tested it. From reading the code, I noticed there is still a mistake, although I could not expose it in the testbed:

1060  if (beforeThis + 4 == length - (objc - 8)) {
1061      beforeThis -= 2;
1061+     objc += 2;
1062  }

I think that objc += 2; should be added after line 1061 in tkCanvLine.c, since we added another point to be considered for redraw.


fvogel added on 2021-10-10 19:38:33:

With [57d9378418] I have now implemented all your suggestions for the "raw" smoothing method (well, at least I think!). Moreover, optimization aborts when the number of points in the line or polygon is not 3n+1 or 3n respectively, or when the number of inserted/deleted points is not a multiple of 3. Finally, no optimization is attempted for custom smoothing methods.

This code seems to work correctly for line items, both when inserting and deleting points.

For polygon items at least, this needs debug. Indeed the following is still a wrong redraw case for instance:

original 1239 403 1155 143 1276 505 1184 486 65 63 511 15
additional 1158 544 889 478 896 370
index 0; splinetype raw; operation insdel; type polygon

Finally, please note that points deletion for polygons has no optimization implemented in PolygonDeleteCoords() - and never had, no idea why - for any smoothing method.


fvogel added on 2021-10-10 18:35:28:

Yes, I see. I have committed the changes you propose, see [ef0458d4fd]. Your test script with both arrows still refreshes fine with this change. Thanks!


helium added on 2021-10-10 17:59:46:

Regarding Check-in [586fde88]:

1076 -  if ((linePtr->lastArrowPtr != NULL) && (beforeThis+objc >= length)) {
1076 +  if ((linePtr->lastArrowPtr != NULL) && (beforeThis+objc >= length - 2)) {

I think this is not necessary, as beforeThis+objc points one position behind the range to be updated. If the last point is to be included, beforeThis+objc should equal length indeed. Have you tested the code without this change?

In consequence, the very similar line 1118 could also be changed to: if ((linePtr->lastArrowPtr != NULL) && (beforeThis+objc >= length)) {. This is different to my earlier suggestion (sorry...). If we keep the length-2, we just risk including the arrows in cases where we don't have to. This goes undetected and the program operates correctly, but performance is reduced a bit.

Note that the expressions in LineDeleteCoords (lines 1274, 1321) look correct to me, since last1 points to the last x-coordinate inside the range to be updated.

Finally, to help testing the above-mentioned points, I found an example that showcases the arrow redrawing bug in Tk 8.6.8 with non-smoothed lines for both arrows at the same time:

original 53 54 445 267
additional 53 188 445 152
index 1; splinetype no; operation insdel; type line; arrows both


fvogel added on 2021-10-10 14:49:10:

Thank you for your detailed review!

> Line 1086, in LineInsert:
> coordPtr = linePtr->coordPtr + beforeThis + 2;
> I don't understand the "+ 2" here, which appears to always exclude the
> first inserted point.

This is code I didn't touch. I think it was here because the arrow is supposed to always include the first point of the line in it's bounding box: the point starting the line is located "inside" the arrow. This adjustment is made in ConfigureArrows(), see lines 2228-2234 and 2271-2272 in tkCanvLine.c But this code would only be correct if beforeThis was zero. I have removed this +2 (and adjusted the loop that follows), I think you are right.

> Line 1108, in LineInsert:
> if ((linePtr->firstArrowPtr != NULL) && (beforeThis > 2)) {
> I would say it should read "< 2", so the new first arrow is included
> when the first point does belong to the "set of interest".
> See "if ((linePtr->firstArrowPtr != NULL) && (first1 < 4)) {" in
> LineDelete (line 1311), but why is there a 4 instead of a 2? I would say
> that first1 has already been decremented?
> 
> Line 1118, in LineInsert:
> if ((linePtr->lastArrowPtr != NULL) && (beforeThis+objc < length-2)) {
> Same here, should be a ">= length-2".
> See "if ((linePtr->lastArrowPtr != NULL) && (last1 > length-4)) {" in
> LineDelete (line 1321), same story about the 4 instead of 2.

This is also code I didn't touch. What you say all seems correct to me, therefore I have made these changes.

> Furthermore, it appears from experiments that beforeThis is always an
> even number in the LineInsert procedure. But where is the code
> responsible for that?

It is made even in GetLineIndex(), see line 1835 in tkCanvLine.c Having beforeThis odd does not make sense because inserted values would lie between x and y of an existing point. We have the same kind of code in LineDeleteCoords() lines 1187 and 1188, so that deletion does not delete "part of a point".

> There is also a comment glitch: Line 1218, in LineDeleteCoords

Correct (fixed).

> For the arrow handling, I came up with a particular test case that
> confirms there is a bug even for non-smoothed lines.

With the above changes, i.e. at [586fde882a], this bug does no longer trigger. Great!

Now I will look at the "raw" and "custom" cases, based on your detailed and ultra-clear analysis (thanks!).


helium added on 2021-10-10 08:56:26:

Just a small remark: There is also the possibility of custom (plug-in?) smoothing methods other than "0", "true", and "raw". We should always redraw the whole line/polygon item in case we encounter such a smoothing method. (In the present code, it executes the "optimizations" for -smooth true whenever the linePtr->smooth pointer evaluates true.)


helium added on 2021-10-10 08:50:13:

I'm afraid there are still some inconsistencies in tkCanvLine.c:

Line 1086, in LineInsert:
coordPtr = linePtr->coordPtr + beforeThis + 2;
I don't understand the "+ 2" here, which appears to always exclude the first inserted point.

Line 1108, in LineInsert:
if ((linePtr->firstArrowPtr != NULL) && (beforeThis > 2)) {
I would say it should read "< 2", so the new first arrow is included when the first point does belong to the "set of interest".
See "if ((linePtr->firstArrowPtr != NULL) && (first1 < 4)) {" in LineDelete (line 1311), but why is there a 4 instead of a 2? I would say that first1 has already been decremented?

Line 1118, in LineInsert:
if ((linePtr->lastArrowPtr != NULL) && (beforeThis+objc < length-2)) {
Same here, should be a ">= length-2".
See "if ((linePtr->lastArrowPtr != NULL) && (last1 > length-4)) {" in LineDelete (line 1321), same story about the 4 instead of 2.

Furthermore, it appears from experiments that beforeThis is always an even number in the LineInsert procedure. But where is the code responsible for that?

There is also a comment glitch: Line 1218, in LineDeleteCoords:
"Include one point at left of the left delete position, and one point at right of the right insert position."
The last words should read "the right delete position." (copy-paste error).

For the arrow handling, I came up with a particular test case that confirms there is a bug even for non-smoothed lines. (See the attachment canvas-line-testbed-arrowbug.png) Enter this in the "Test Data" field of canvas-line-testbed.tcl:

original 383 78 445 267 179 177
additional 181 224
index 2; splinetype no; operation insdel; type line; arrows both

Alternatively, you can trigger the bug by entering the following minimal code line-for-line into a wish shell:

pack [canvas .c -width 500 -height 339]
.c create line 383 78 445 267 179 177 -tag myLine -width 4 -smooth no -arrow both -arrowshape {20 30 10}
.c insert myLine 4 {181 224}

While generating this code with canvas-line-testbed.tcl, I found that the braces around the numbers for -arrowshape were missing. I just uploaded a corrected version.


helium added on 2021-10-09 18:39:54:

I just uploaded my own analysis of the -smooth raw case. It agrees in the statement that only insertion/deletion of multiples of three points should be optimized, since otherwise the operation would start from and/or lead to a "garbage" curve where one or two points at the end are repeated from the beginning.

I already found some "ready-to-use" formulas for calculating the points affected by such an insert/delete operation. They are not as difficult as you imagined...

How I found the formulas: I defined an abstract notation for segments, since comparing actual curves was not needed anymore. Each segment is written in braces containing its point numbers, e.g.: {0 1 2 3} {3 4 5 6}. Points that are inserted/deleted are written in bold. Segments that need redrawing are underlined.

The variable "first" denotes the first point being inserted/deleted, and the variable "last" denotes the last point, "n" and "m" are arbitrary integers.

Case first divisible by 3, last = 3n+2:
Example 1: {0 1 2 3} {3 4 5 6}
Example 2: {0 1 2 3} {3 4 5 6} {6 7 8 9}
Need to redraw from first-3 to last+1.
Case first = 3n+1, last divisible by 3:
Example: {0 1 2 3} {3 4 5 6} {6 7 8 9} {9 10 11 12}
Need to redraw from first-1 to last+3.
Case first = 3n+2, last = 3m+1:
Example: {0 1 2 3} {3 4 5 6} {6 7 8 9} {9 10 11 12} {12 13 14 15}
Need to redraw from first-2 to last+2.

Summarizing the expressions into tables:

CaseABC
first%3012
Redraw fromfirst-3first-1first-2
Expression(first-1) - (first-1)%3

CaseBCA
last%3012
Redraw up tolast+3last+2last+1
Expressionlast+3 - last%3

Now in the real code we have coordinate arrays, such that point 0 occupies indices 0 and 1, point 1 occupies indices 2 and 3, etc. Additionally, the insertion and deletion procedures have a slightly different formulation of variables:

LineInsert uses the variables beforeThis (corresponding to 2*first) and objc (corresponding to 2*(last-first+1)).

For the first point to be included for redraw, we get the formula:
(beforeThis-2) - (beforeThis-2)%6
with the following value table:

beforeThis | 0 (1) | 2 (3) 4 (5) 6 (7) | 8 (9) 10 (11) 12 (13) | 14 (15) 16 (17) 18 (19) | 20 ...
       out | 0 (0) | 0 (0) 0 (0) 0 (0) | 6 (6)  6  (6)  6  (6) | 12 (12) 12 (12) 12 (12) | 18 ...

For the number of points to be included for redraw, we see in the example cases that always four additional points are underlined besides those in bold. Therefore the formula is as simple as objc + 8.

LineDeleteCoords uses the variables first (two times our previous first) and last (even number, two times our previous last). For the first point to be included, we have the formula from above:
first1 = (first-2) - (first-2) % 6

For the last point to be included for redraw, we get:
last1 = last + 6 - last % 6
with the following value table:

 last | 0 (1) 2 (3) 4 (5) |  6  (7)  8  (9) 10 (11) | 12 (13) 14 (15) 16 (17) | 18 ...
last1 | 6 (6) 6 (6) 6 (6) | 12 (12) 12 (12) 12 (12) | 18 (18) 18 (18) 18 (18) | 24 ...

If the corrections for plain line/polygon items with -smooth 0 are already taken, the formulas may be rewritten:

For LineInsert we have the corrections beforeThis -= 2; objc += 4; such that the formulas (for the range of points to include for redraw) become:
beforeThis = beforeThis - beforeThis % 6; objc += 4;

For LineDeleteCoords we have first1 = first; last1 = last; and the corrections first1 -= 2; last1 += 2; such that the formulas become:
first1 = first1 - first1 % 6; last1 = last + 6 - last % 6;
The second formula was kept unchanged since the last variable is still available and the formula would be longer when formulated in the changed last1.

I would recommend you to integrate these rather easy formulas into the code, but I would also say that we should do a full redraw in any case involving a "degenerate" line (not 3n+1 points) or polygon (not 3n points). There would still be some optimization potential, such as not including the points that precede the insertion/deletion location when something else than a multiple of 3 points is inserted/deleted. But I doubt that these "degenerate" cases are useful for anyone.

I don't know if really nobody has detected the bug before me, since it is so easy to work around it with a $canvas move $object 0 0 (forced full redraw). This workaround is also present in my vector drawing program, and I'll have to keep it depending on the Tk version, such that buggy installations will also be supported.


fvogel added on 2021-10-09 14:11:58:

Thanks a lot for this test application, it is very very helpful!

With [b541b73a] I have fixed the last (I hope) bug for -smooth true. At least I can't trigger a display issue with your test application any longer. This is true for both lines and polygons (there was no bug for polygons, I didn't make any change in the polygons code).

Also, I have taken your remark into account regarding comments.

Now the -smooth raw case would need to be addressed. Since points there go by packets of three (semantically speaking), my first thought is we should redraw the whole line if the number of inserted points is not a multiple of 3. If it is a multiple of 3 (N=3n), we need to figure out how many points we need to include in the redraw bounding box. This number could depend on where the new points need to be inserted in the coordinates list, since in a packet of three the points do not all play the same role. This is starting to become a bit complicated, and I'm wondering if figuring all this out is worth the effort. Indeed, this wrong optimization code was there forever (ca. 20 years at least), and nobody complained so far to my knowledge. Is someone really using -smooth raw with coords insertion/deletion? Doubtful, otherwise we would have had bug reports already. If you agree, we could simply redraw the whole canvas line in case smoothing is "raw". We could always add optimization in that case later, if there is any need. What do you think?


helium added on 2021-10-02 17:41:05:

I just uploaded a version of the test script which allows switching between lines and polygons and has a Modify mode (in addition to the previous Insert/Delete) for testing the rchars operation.

Additionally I went through your code, i.e. the whole LineInsert and LineDeleteCoords procedures. Your newest version is all understandable to me, and I would say it should work for the -smooth no and -smooth true cases. Just a tiny remark: Lines 980-987 and 1191-1198 were quite hard to understand for me as an outsider: Why would we overwrite the start and/or end point from some arrow structures? I found the answer only after reading the ConfigureArrows procedure which offsets the start and end coordinates "such that a thick line does not stick out". A comment in each of these code locations would not hurt...


fvogel added on 2021-09-29 03:53:37:

I have modified the test script to deal with polygons instead of lines (easy: only the threee [canvas create] lines need to be changed). Results: with the current state of he bugfix branch I couldn't trigger an issue with either -smooth no or -smooth true. I do see issues however with -smooth raw for polygons. Do you confirm this?


fvogel added on 2021-09-28 09:10:43:
It just occurred to me that we probably have the same problem with polygons, not just lines. The optimization code is there as well. Can the test tool easily be upgraded to deal with both polygons or lines?

fvogel added on 2021-09-27 19:47:23:

I have reworked my fix, see [2f80202c43]. This now works for both -smooth no and -smooth true (aka bezier), as demonstrated by your test script canvas-line-testbed.tcl. At least I couldn't trigger an issue anymore. Do you confirm this result?

Case -smooth raw still needs to be fixed.


fvogel added on 2021-09-27 18:12:44:
This test program is very useful, it exceeds any expectations (read: dreams!) I had. Many thanks, it will be very useful! I had only a cursive look so far at what it allows, but I will definitely spend more time on it.

Did I say "thank you" already? Thank you !!

helium added on 2021-09-26 10:26:17:
Now I attached a demo testbed application that exposes the point insertion/deletion bug through an animation (insert - redraw - delete - redraw), while all control points can be moved by using the mouse.
The pre-configured case, also seen in the screenshot, is particularly nasty, as here the second point of a curve is affected (not the first or last as discussed before).
I hope that this program is useful, as it took me some 3:30 hours to develop :)

helium added on 2021-09-25 19:08:12:
I just finished translating my pen-and-paper analysis of the redraw situation for "-smooth true" quadratic bézier splines into a Tk script that draws the analysis on a static canvas. A screenshot rendering is also attached.

From a high-level perspective (I still have not studied the code in-depth, maybe it does already what I describe here), the routines for inserting, deleting, and moving points should perform the following:

Insertion of point i
--------------------
* Insert point at index i into the points list
* Take points i-2, i-1, i, i+1, i+2 into the bounding box
* If i==0: Take point i+3 (=3) into the bounding box
* If i==length-1: Take point i-3 into the bounding box

Deletion of point i
-------------------
* BBox &= i-2, i-1, i, i+1, i+2
* If i==0: BBox &= i+3
* If i==N-1: BBox &= i-3
* Delete point at index i from the points list

Movement of point i
-------------------
* BBox &= i-2, i-1, i, i+1, i+2
* Edit point at index i in the points list
* BBox &= i  (new point)

These procedures operate on a single point but should be easily generalized for operating on a list of adjacent points.

fvogel added on 2021-09-25 11:21:03:
Thanks for your feedback. 

Your analysis matches mine regarding the -smooth true case. If you look at my fix you will see this.

Regarding the -smooth raw case, I didn't analyze it so far but you're certainly right. The optimization code currently does not distinguish between beziers (true) and raw modes, which probably means we have problems with -smooth raw. I didn't try this case so far, if you could narrow down what the issues are with raw mode that would certainly help.

helium added on 2021-09-25 09:55:03:

This weekend I'll take some time to compile and test your new code and maybe produce a kind of test-bed application. I found that my vector drawing demo (work in progress, temporary link) is not suitable for extensive testing as its points-manipulation tool draws little markers at the control points of selected lines, so the canvas always redraws the full curve together with the markers.

In the meantime, I did my own analysis of what parts of a -smooth true line change when points are inserted, deleted, or moved. The result is that two neighboring points in each direction need to be taken into the bounding box for redrawing, except when a point is inserted/deleted at the very beginning or end of the line; then three neighboring points need to be considered (that's where the previous code failed). I'll create and upload some nice image as soon as possible.

Anyway, when considering the -smooth raw case (cubic bezier with explicit control points), I discovered that in the worst case the whole curve needs to be redrawn. The reason is that in such curves, points take different roles: Knot points lie on the actual curve, and other control points only give the direction and amount of deflection. Their sequence is e.g. KDDKDDKDDK. When a single point is inserted, the roles of the subsequent points are changed such that other points than before become the knot points. See the following example:

package require Tk
pack [canvas .canvas -width 500 -height 350]

set coords [list 63 248  96 166  218 329  307 299  402 295  410 80  335 147  275 192  220 101  293 37]
.canvas create line $coords -smooth raw -width 3 -fill #0f0
.canvas create line $coords -smooth raw -tag myLine
update; after 2000
.canvas insert myLine 4 {157 247.5}
update; after 2000
.canvas move myLine 0 0

How should we proceed in this case? A lazy solution would be to always redraw the whole line if -smooth is raw. Otherwise we need to check if a multiple of three points is inserted or deleted, because then the roles of the remaining points are not changed.


fvogel added on 2021-09-20 06:30:00:

We have the same problem when deleting points from a canvas line, as demonstrated by:

package require Tk
pack [canvas .canvas -width 500 -height 250]
.canvas create line 40 200 160 40 280 200 400 40 -smooth true -tag myLine
.canvas dchars myLine 7

See the fix [71ccd957ba] in branch bug-5fb8145997. Needs testing as well.


fvogel added on 2021-09-19 20:21:37:
Oh, one last thing: only *smoothed* lines need to be tested.

fvogel added on 2021-09-19 19:57:59:

Analysis of the issue: When inserting a new point in a canvas line, the optimization code I have pointed out earlier selects what part of the canvas (what bounding box) needs to be redrawn. This is based on the fact smoothed canvas lines are implemented as a set of splines, each of them using three points of the line. The optimization code avoids redrawing of the whole canvas when inserting a line point by computing a minimal redraw box. This box is the smallest rectangle that includes two points before the inserted point and two points following the inserted point. This should be enough because only the splines neighbouring the inserted points need to be redrawn. And this is the case except when the inserted point is too close from one of the line ends. Indeed, TkMakeBezierCurve() computes different spline control points at the lines ends than for other areas of the line. Therefore, when the inserted point is close to one of the line ends, the first (or last) spline must be redrawn in addition to what the optimization code already was including in it's redraw box.

I have implemented this change in [5da2de43e8] and it fixes the problem for me.

And now here is what you can do to help: please test this extensively. Your vector drawing program should be a good way to confirm it works. Please try many many cases, both simple (3 points in the line, insert a 4th point at each possible position) and more complex (many points in the line, insert at different positions both close to and far from each line end). Look for any regressions as well. And report your findings here. Thanks!


helium added on 2021-09-16 12:49:32:

Many thanks for looking at this issue! I also read through tkCanvLine.c and its "optimization code" - I still have not taken enough time to fully understand it.

Anyway, I reproduced the bug on Windows 10 (Tk 8.6.9 installed via Git for Windows 2.20.1), which is no wonder since it comes from platform-independent code.


fvogel added on 2021-09-15 19:40:28:

The culprit is the optimization code here. The bounding box of what needs to be redrawn upon item insertion does not seem to be correct.


helium added on 2021-09-15 07:45:56:
I also confirmed the bug on Tk 8.6.10 (Linux/X11).

Attachments: