diff options
| author | Johan B. C. Engelen <jbc.engelen@swissonline.ch> | 2012-03-23 21:02:03 +0000 |
|---|---|---|
| committer | Johan B. C. Engelen <j.b.c.engelen@alumnus.utwente.nl> | 2012-03-23 21:02:03 +0000 |
| commit | 2ad4d90803d4c83b28f146130db83c5e569b8ec7 (patch) | |
| tree | 0d0dfb0e628438cab148b28012790d38f4d3e636 /src/live_effects/spiro.cpp | |
| parent | more spiro cleanup (diff) | |
| download | inkscape-2ad4d90803d4c83b28f146130db83c5e569b8ec7.tar.gz inkscape-2ad4d90803d4c83b28f146130db83c5e569b8ec7.zip | |
cleanup spiro code
(bzr r11123)
Diffstat (limited to 'src/live_effects/spiro.cpp')
| -rw-r--r-- | src/live_effects/spiro.cpp | 111 |
1 files changed, 32 insertions, 79 deletions
diff --git a/src/live_effects/spiro.cpp b/src/live_effects/spiro.cpp index e7824c297..e4b72793c 100644 --- a/src/live_effects/spiro.cpp +++ b/src/live_effects/spiro.cpp @@ -33,6 +33,24 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA #define SPIRO_SHOW_INFINITE_COORDINATE_CALLS +namespace Spiro { + +void spiro_run(const spiro_cp *src, int src_len, SPCurve &curve) +{ + spiro_seg *s = Spiro::run_spiro(src, src_len); + Spiro::ConverterSPCurve bc(curve); + Spiro::spiro_to_otherpath(s, src_len, bc); + free(s); +} + +void spiro_run(const spiro_cp *src, int src_len, Geom::Path &path) +{ + spiro_seg *s = Spiro::run_spiro(src, src_len); + Spiro::ConverterPath bc(path); + Spiro::spiro_to_otherpath(s, src_len, bc); + free(s); +} + /************************************ * Spiro math @@ -825,15 +843,15 @@ solve_spiro(spiro_seg *s, int nseg) } static void -spiro_seg_to_bpath(const double ks[4], +spiro_seg_to_otherpath(const double ks[4], double x0, double y0, double x1, double y1, - bezctx *bc, int depth) + ConverterBase &bc, int depth) { double bend = fabs(ks[0]) + fabs(.5 * ks[1]) + fabs(.125 * ks[2]) + fabs((1./48) * ks[3]); if (!bend > 1e-8) { - bc->lineto(bc, x1, y1); + bc.lineto(x1, y1); } else { double seg_ch = hypot(x1 - x0, y1 - y0); double seg_th = atan2(y1 - y0, x1 - x0); @@ -856,7 +874,7 @@ spiro_seg_to_bpath(const double ks[4], vl = (scale * (1./3)) * sin(th_even - th_odd); ur = (scale * (1./3)) * cos(th_even + th_odd); vr = (scale * (1./3)) * sin(th_even + th_odd); - bc->curveto(bc, x0 + ul, y0 + vl, x1 - ur, y1 - vr, x1, y1); + bc.curveto(x0 + ul, y0 + vl, x1 - ur, y1 - vr, x1, y1); } else { /* subdivide */ double ksub[4]; @@ -875,11 +893,11 @@ spiro_seg_to_bpath(const double ks[4], integrate_spiro(ksub, xysub); xmid = x0 + cth * xysub[0] - sth * xysub[1]; ymid = y0 + cth * xysub[1] + sth * xysub[0]; - spiro_seg_to_bpath(ksub, x0, y0, xmid, ymid, bc, depth + 1); + spiro_seg_to_otherpath(ksub, x0, y0, xmid, ymid, bc, depth + 1); ksub[0] += .25 * ks[1] + (1./384) * ks[3]; ksub[1] += .125 * ks[2]; ksub[2] += (1./16) * ks[3]; - spiro_seg_to_bpath(ksub, xmid, ymid, x1, y1, bc, depth + 1); + spiro_seg_to_otherpath(ksub, xmid, ymid, x1, y1, bc, depth + 1); } } } @@ -901,7 +919,7 @@ free_spiro(spiro_seg *s) } void -spiro_to_bpath(const spiro_seg *s, int n, bezctx *bc) +spiro_to_otherpath(const spiro_seg *s, int n, ConverterBase &bc) { int i; int nsegs = s[n - 1].ty == '}' ? n - 1 : n; @@ -912,9 +930,10 @@ spiro_to_bpath(const spiro_seg *s, int n, bezctx *bc) double x1 = s[i + 1].x; double y1 = s[i + 1].y; - if (i == 0) - bc->moveto(bc, x0, y0, s[0].ty == '{'); - spiro_seg_to_bpath(s[i].ks, x0, y0, x1, y1, bc, 0); + if (i == 0) { + bc.moveto(x0, y0, s[0].ty == '{'); + } + spiro_seg_to_otherpath(s[i].ks, x0, y0, x1, y1, bc, 0); } } @@ -933,75 +952,7 @@ get_knot_th(const spiro_seg *s, int i) } -/************************************ - * Conversion to Inkscape's curve - */ - -void bezctx_ink_moveto(bezctx *bc, double x, double y, int /*is_open*/) -{ - bezctx_ink *bi = (bezctx_ink *) bc; - if ( IS_FINITE(x) && IS_FINITE(y) ) { - bi->curve->moveto(x, y); - } -#ifdef SPIRO_SHOW_INFINITE_COORDINATE_CALLS - else { - g_message("Spiro: moveto not finite"); - } -#endif -} - -void bezctx_ink_lineto(bezctx *bc, double x, double y) -{ - bezctx_ink *bi = (bezctx_ink *) bc; - if ( IS_FINITE(x) && IS_FINITE(y) ) { - bi->curve->lineto(x, y); - } -#ifdef SPIRO_SHOW_INFINITE_COORDINATE_CALLS - else { - g_message("Spiro: lineto not finite"); - } -#endif -} - -void bezctx_ink_quadto(bezctx *bc, double xm, double ym, double x3, double y3) -{ - bezctx_ink *bi = (bezctx_ink *) bc; - - if ( IS_FINITE(xm) && IS_FINITE(ym) && IS_FINITE(x3) && IS_FINITE(y3) ) { - bi->curve->quadto(xm, ym, x3, y3); - } -#ifdef SPIRO_SHOW_INFINITE_COORDINATE_CALLS - else { - g_message("Spiro: quadto not finite"); - } -#endif -} - -void bezctx_ink_curveto(bezctx *bc, double x1, double y1, double x2, double y2, - double x3, double y3) -{ - bezctx_ink *bi = (bezctx_ink *) bc; - if ( IS_FINITE(x1) && IS_FINITE(y1) && IS_FINITE(x2) && IS_FINITE(y2) ) { - bi->curve->curveto(x1, y1, x2, y2, x3, y3); - } -#ifdef SPIRO_SHOW_INFINITE_COORDINATE_CALLS - else { - g_message("Spiro: curveto not finite"); - } -#endif -} - -bezctx * new_bezctx_ink(SPCurve *curve) -{ - bezctx_ink *result = g_new(bezctx_ink, 1); - result->base.moveto = bezctx_ink_moveto; - result->base.lineto = bezctx_ink_lineto; - result->base.quadto = bezctx_ink_quadto; - result->base.curveto = bezctx_ink_curveto; - result->curve = curve; - return &result->base; -} - +} // namespace Spiro /************************************ * Unit_test code @@ -1012,6 +963,8 @@ bezctx * new_bezctx_ink(SPCurve *curve) #include <stdio.h> #include <sys/time.h> /* for gettimeofday */ +using namespace Spiro; + static double get_time (void) { |
