Skip to content

Commit 64274f4

Browse files
committed
Fixed point curve_bezier_cubic - first attempt
1 parent 4882951 commit 64274f4

File tree

3 files changed

+104
-14
lines changed

3 files changed

+104
-14
lines changed

include/framebuf/curve.h

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ point_t curve_bezier_point_on_quartic_r(point_t p0,
5353
point_t p4,
5454
fix16_t t);
5555

56-
// written in terms of cubics (and in turn of quads)
56+
// written in terms of quartics (and in turn of cubics, etc.)
5757
point_t curve_bezier_point_on_quintic_r(point_t p0,
5858
point_t p1,
5959
point_t p2,
@@ -70,4 +70,11 @@ void curve_bezier_cubic_f(point_t p0,
7070
int nsteps,
7171
point_t *points);
7272

73+
void curve_bezier_cubic(point_t p0,
74+
point_t p1,
75+
point_t p2,
76+
point_t p3,
77+
int nsteps,
78+
point_t *points);
79+
7380
#endif /* DPTLIB_CURVE_H */

libraries/framebuf/curve/curve.c

Lines changed: 95 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,26 @@
11
/* bezier.c */
22

3-
// - Eliminate structure passing, or at least analyse some code with it inlined to see if it helps
4-
// - Does these need to round to nearest?
5-
// - Consider overflow too
3+
// TODO
4+
//
5+
// - Eliminate structure passing, or at least analyse some code with it inlined
6+
// to see if it hinders or helps
7+
// - Consider rounding.
8+
// - Consider overflow too.
9+
10+
#include <assert.h>
611

712
#include "base/utils.h"
813

914
#include "geom/point.h"
1015

1116
#include "framebuf/curve.h"
1217

13-
point_t curve_point_on_line(point_t p0, point_t p1, fix16_t t)
14-
{
15-
point_t c;
16-
17-
c.x = p0.x + (p1.x - p0.x) * t / 65536;
18-
c.y = p0.y + (p1.y - p0.y) * t / 65536;
18+
/* ----------------------------------------------------------------------- */
1919

20-
return c;
21-
}
20+
#define LOG2SCALE (4)
21+
#define SCALE (1 << LOG2SCALE)
2222

23-
#define SCALE (16)
23+
/* ----------------------------------------------------------------------- */
2424

2525
static point_t scale_up(point_t p)
2626
{
@@ -40,6 +40,20 @@ static point_t scale_down(point_t p)
4040
return q;
4141
}
4242

43+
/* ----------------------------------------------------------------------- */
44+
45+
point_t curve_point_on_line(point_t p0, point_t p1, fix16_t t)
46+
{
47+
point_t c;
48+
49+
c.x = p0.x + (p1.x - p0.x) * t / FIX16_ONE;
50+
c.y = p0.y + (p1.y - p0.y) * t / FIX16_ONE;
51+
52+
return c;
53+
}
54+
55+
/* ----------------------------------------------------------------------- */
56+
4357
point_t curve_bezier_point_on_quad(point_t p0,
4458
point_t p1,
4559
point_t p2,
@@ -291,3 +305,72 @@ void curve_bezier_cubic_f(point_t p0,
291305
d2y += d3y;
292306
}
293307
}
308+
309+
// attempt at an integer version
310+
void curve_bezier_cubic(point_t p0,
311+
point_t p1,
312+
point_t p2,
313+
point_t p3,
314+
int nsteps,
315+
point_t *points)
316+
{
317+
fix16_t cx, cy;
318+
fix16_t bx, by;
319+
fix16_t ax, ay;
320+
fix16_t h, hh, hhh;
321+
fix16_t d1x, d1y;
322+
fix16_t d2x, d2y;
323+
fix16_t d3x, d3y;
324+
fix16_t curx, cury;
325+
int i;
326+
point_t *point;
327+
328+
cx = 3 * (p1.x - p0.x) * FIX16_ONE;
329+
cy = 3 * (p1.y - p0.y) * FIX16_ONE;
330+
331+
bx = 3 * (p2.x - p1.x) * FIX16_ONE - cx;
332+
by = 3 * (p2.y - p1.y) * FIX16_ONE - cy;
333+
334+
ax = (p3.x - p0.x) * FIX16_ONE - cx - bx;
335+
ay = (p3.y - p0.y) * FIX16_ONE - cy - by;
336+
337+
h = FIX16_ONE / nsteps;
338+
hh = h * h / FIX16_ONE;
339+
hhh = hh * h / FIX16_ONE;
340+
341+
/* first difference */
342+
d1x = (ax * (long long) hhh + bx * (long long) hh + cx * (long long) h) / FIX16_ONE;
343+
d1y = (ay * (long long) hhh + by * (long long) hh + cy * (long long) h) / FIX16_ONE;
344+
345+
/* second difference */
346+
d2x = (6 * ax * (long long) hhh + 2 * bx * (long long) hh) / FIX16_ONE;
347+
d2y = (6 * ay * (long long) hhh + 2 * by * (long long) hh) / FIX16_ONE;
348+
349+
/* third difference */
350+
d3x = (6 * ax * (long long) hhh) / FIX16_ONE;
351+
d3y = (6 * ay * (long long) hhh) / FIX16_ONE;
352+
353+
point = &points[0];
354+
355+
curx = p0.x * FIX16_ONE;
356+
cury = p0.y * FIX16_ONE;
357+
358+
for (i = 0; ; i++)
359+
{
360+
point->x = (int) (curx >> FIX16_SHIFT);
361+
point->y = (int) (cury >> FIX16_SHIFT);
362+
point++;
363+
364+
if (i == nsteps)
365+
break;
366+
367+
curx += d1x;
368+
cury += d1y;
369+
370+
d1x += d2x;
371+
d1y += d2y;
372+
373+
d2x += d3x;
374+
d2y += d3y;
375+
}
376+
}

libraries/framebuf/curve/test/curve-test.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -411,7 +411,7 @@ static void calc_all_curves(curveteststate_t *state)
411411

412412
if (curves[set].kind == FwdDiffCubic)
413413
{
414-
curve_bezier_cubic_f(state->jitterfn(state->control_points[o + 0]),
414+
curve_bezier_cubic(state->jitterfn(state->control_points[o + 0]),
415415
state->jitterfn(state->control_points[o + 1]),
416416
state->jitterfn(state->control_points[o + 2]),
417417
state->jitterfn(state->control_points[o + 3]),

0 commit comments

Comments
 (0)