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 | |
| parent | more spiro cleanup (diff) | |
| download | inkscape-2ad4d90803d4c83b28f146130db83c5e569b8ec7.tar.gz inkscape-2ad4d90803d4c83b28f146130db83c5e569b8ec7.zip | |
cleanup spiro code
(bzr r11123)
Diffstat (limited to 'src')
| -rw-r--r-- | src/live_effects/CMakeLists.txt | 6 | ||||
| -rw-r--r-- | src/live_effects/Makefile_insert | 3 | ||||
| -rw-r--r-- | src/live_effects/bezctx.h | 15 | ||||
| -rw-r--r-- | src/live_effects/lpe-powerstroke-interpolators.h | 82 | ||||
| -rw-r--r-- | src/live_effects/lpe-spiro.cpp | 7 | ||||
| -rw-r--r-- | src/live_effects/spiro-converters.h | 69 | ||||
| -rw-r--r-- | src/live_effects/spiro.cpp | 111 | ||||
| -rw-r--r-- | src/live_effects/spiro.h | 25 |
8 files changed, 124 insertions, 194 deletions
diff --git a/src/live_effects/CMakeLists.txt b/src/live_effects/CMakeLists.txt index f2e10c771..a5f50a69d 100644 --- a/src/live_effects/CMakeLists.txt +++ b/src/live_effects/CMakeLists.txt @@ -40,6 +40,7 @@ set(live_effects_SRC lpeobject-reference.cpp lpeobject.cpp spiro.cpp + spiro-converters.cpp parameter/array.cpp parameter/bool.cpp @@ -57,7 +58,6 @@ set(live_effects_SRC # ------- # Headers - bezctx.h effect-enum.h effect.h lpe-angle_bisector.h @@ -99,6 +99,8 @@ set(live_effects_SRC lpegroupbbox.h lpeobject-reference.h lpeobject.h + spiro.h + spiro-converters.h parameter/array.h parameter/bool.h @@ -113,7 +115,7 @@ set(live_effects_SRC parameter/text.h parameter/unit.h parameter/vector.h - spiro.h + ) # add_inkscape_lib(live_effects_LIB "${live_effects_SRC}") diff --git a/src/live_effects/Makefile_insert b/src/live_effects/Makefile_insert index 65a934c9c..9c3c171f2 100644 --- a/src/live_effects/Makefile_insert +++ b/src/live_effects/Makefile_insert @@ -50,7 +50,8 @@ ink_common_sources += \ live_effects/lpe-perp_bisector.h \ live_effects/spiro.h \ live_effects/spiro.cpp \ - live_effects/bezctx.h \ + live_effects/spiro-converters.h \ + live_effects/spiro-converters.cpp \ live_effects/lpe-circle_with_radius.cpp \ live_effects/lpe-circle_with_radius.h \ live_effects/lpe-perspective_path.cpp \ diff --git a/src/live_effects/bezctx.h b/src/live_effects/bezctx.h deleted file mode 100644 index d8eb38e8c..000000000 --- a/src/live_effects/bezctx.h +++ /dev/null @@ -1,15 +0,0 @@ -#ifndef INKSCAPE_SPIRO_bezctx_H -#define INKSCAPE_SPIRO_bezctx_H - -#include "bezctx_intf.h" - -class _bezctx { -public: - void (*moveto)(bezctx *bc, double x, double y, int is_open); - void (*lineto)(bezctx *bc, double x, double y); - void (*quadto)(bezctx *bc, double x1, double y1, double x2, double y2); - void (*curveto)(bezctx *bc, double x1, double y1, double x2, double y2, - double x3, double y3); -}; - -#endif diff --git a/src/live_effects/lpe-powerstroke-interpolators.h b/src/live_effects/lpe-powerstroke-interpolators.h index 224c56e89..6f5b75af8 100644 --- a/src/live_effects/lpe-powerstroke-interpolators.h +++ b/src/live_effects/lpe-powerstroke-interpolators.h @@ -16,8 +16,6 @@ #include <2geom/bezier-utils.h> #include <2geom/sbasis-to-bezier.h> -#include "live_effects/bezctx.h" -#include "live_effects/bezctx_intf.h" #include "live_effects/spiro.h" @@ -136,7 +134,6 @@ private: }; -#define SPIRO_SHOW_INFINITE_COORDINATE_CALLS class SpiroInterpolator : public Interpolator { public: SpiroInterpolator() {}; @@ -148,8 +145,7 @@ public: Coord scale_y = 100.; guint len = points.size(); - bezctx *bc = new_bezctx_ink(&fit); - spiro_cp *controlpoints = g_new (spiro_cp, len); + Spiro::spiro_cp *controlpoints = g_new (Spiro::spiro_cp, len); for (unsigned int i = 0; i < len; ++i) { controlpoints[i].x = points[i][X]; controlpoints[i].y = points[i][Y] / scale_y; @@ -160,87 +156,13 @@ public: controlpoints[len-2].ty = 'v'; controlpoints[len-1].ty = '}'; - spiro_seg *s = run_spiro(controlpoints, len); - spiro_to_bpath(s, len, bc); - free(s); - free(bc); + Spiro::spiro_run(controlpoints, len, fit); fit *= Scale(1,scale_y); return fit; }; private: - typedef struct { - bezctx base; - Path *path; - int is_open; - } bezctx_ink; - - static 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->path->start(Point(x, y)); - } - #ifdef SPIRO_SHOW_INFINITE_COORDINATE_CALLS - else { - g_message("spiro moveto not finite"); - } - #endif - } - - static void bezctx_ink_lineto(bezctx *bc, double x, double y) - { - bezctx_ink *bi = (bezctx_ink *) bc; - if ( IS_FINITE(x) && IS_FINITE(y) ) { - bi->path->appendNew<LineSegment>( Point(x, y) ); - } - #ifdef SPIRO_SHOW_INFINITE_COORDINATE_CALLS - else { - g_message("spiro lineto not finite"); - } - #endif - } - - static 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->path->appendNew<QuadraticBezier>(Point(xm, ym), Point(x3, y3)); - } - #ifdef SPIRO_SHOW_INFINITE_COORDINATE_CALLS - else { - g_message("spiro quadto not finite"); - } - #endif - } - - static 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->path->appendNew<CubicBezier>(Point(x1, y1), Point(x2, y2), Point(x3, y3)); - } - #ifdef SPIRO_SHOW_INFINITE_COORDINATE_CALLS - else { - g_message("spiro curveto not finite"); - } - #endif - } - - bezctx * - new_bezctx_ink(Geom::Path *path) const { - 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->path = path; - return &result->base; - } - SpiroInterpolator(const SpiroInterpolator&); SpiroInterpolator& operator=(const SpiroInterpolator&); }; diff --git a/src/live_effects/lpe-spiro.cpp b/src/live_effects/lpe-spiro.cpp index 7f700bddd..8b4274ab2 100644 --- a/src/live_effects/lpe-spiro.cpp +++ b/src/live_effects/lpe-spiro.cpp @@ -46,8 +46,7 @@ LPESpiro::doEffect(SPCurve * curve) guint len = curve->get_segment_count() + 2; curve->reset(); - bezctx *bc = new_bezctx_ink(curve); - spiro_cp *path = g_new (spiro_cp, len); + Spiro::spiro_cp *path = g_new (Spiro::spiro_cp, len); int ip = 0; for(Geom::PathVector::const_iterator path_it = original_pathv.begin(); path_it != original_pathv.end(); ++path_it) { @@ -140,9 +139,7 @@ LPESpiro::doEffect(SPCurve * curve) // run subpath through spiro int sp_len = ip; - spiro_seg *s = run_spiro(path, sp_len); - spiro_to_bpath(s, sp_len, bc); - free(s); + Spiro::spiro_run(path, sp_len, *curve); ip = 0; } diff --git a/src/live_effects/spiro-converters.h b/src/live_effects/spiro-converters.h new file mode 100644 index 000000000..dbc10efda --- /dev/null +++ b/src/live_effects/spiro-converters.h @@ -0,0 +1,69 @@ +#ifndef INKSCAPE_SPIRO_CONVERTERS_H +#define INKSCAPE_SPIRO_CONVERTERS_H + +#include <2geom/forward.h> +class SPCurve; + +namespace Spiro { + +class ConverterBase { +public: + ConverterBase() {}; + virtual ~ConverterBase() {}; + + virtual void moveto(double x, double y, bool is_open) = 0; + virtual void lineto(double x, double y) = 0; + virtual void quadto(double x1, double y1, double x2, double y2) = 0; + virtual void curveto(double x1, double y1, double x2, double y2, double x3, double y3) = 0; +}; + + +/** + * Converts Spiro to Inkscape's SPCurve + */ +class ConverterSPCurve : public ConverterBase { +public: + ConverterSPCurve(SPCurve &curve) + : _curve(curve), _is_open(false) + {} ; + + virtual void moveto(double x, double y, bool is_open); + virtual void lineto(double x, double y); + virtual void quadto(double x1, double y1, double x2, double y2); + virtual void curveto(double x1, double y1, double x2, double y2, double x3, double y3); + + SPCurve &_curve; + bool _is_open; + +private: + ConverterSPCurve(const ConverterSPCurve&); + ConverterSPCurve& operator=(const ConverterSPCurve&); +}; + + +/** + * Converts Spiro to 2Geom's Path + */ +class ConverterPath : public ConverterBase { +public: + ConverterPath(Geom::Path &path) + : _path(path), _is_open(false) + {} ; + + virtual void moveto(double x, double y, bool is_open); + virtual void lineto(double x, double y); + virtual void quadto(double x1, double y1, double x2, double y2); + virtual void curveto(double x1, double y1, double x2, double y2, double x3, double y3); + + Geom::Path &_path; + bool _is_open; + +private: + ConverterPath(const ConverterPath&); + ConverterPath& operator=(const ConverterPath&); +}; + + +} // namespace Spiro + +#endif 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) { diff --git a/src/live_effects/spiro.h b/src/live_effects/spiro.h index f39fbded6..0d85da74b 100644 --- a/src/live_effects/spiro.h +++ b/src/live_effects/spiro.h @@ -24,10 +24,14 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA #ifndef INKSCAPE_SPIRO_H #define INKSCAPE_SPIRO_H -#include "live_effects/bezctx.h" -#include "live_effects/bezctx_intf.h" +#include "live_effects/spiro-converters.h" class SPCurve; +namespace Geom { + class Path; +} + +namespace Spiro { typedef struct { double x; @@ -35,21 +39,18 @@ typedef struct { char ty; } spiro_cp; -typedef struct spiro_seg_s spiro_seg; +void spiro_run(const spiro_cp *src, int src_len, SPCurve &curve); +void spiro_run(const spiro_cp *src, int src_len, Geom::Path &path); + +/* the following methods are only for expert use: */ +typedef struct spiro_seg_s spiro_seg; spiro_seg * run_spiro(const spiro_cp *src, int n); void free_spiro(spiro_seg *s); -void spiro_to_bpath(const spiro_seg *s, int n, bezctx *bc); +void spiro_to_otherpath(const spiro_seg *s, int n, ConverterBase &bc); double get_knot_th(const spiro_seg *s, int i); -typedef struct { - bezctx base; - SPCurve *curve; - int is_open; -} bezctx_ink; - -bezctx * new_bezctx_ink(SPCurve *curve); - +} // namespace Spiro #endif // INKSCAPE_SPIRO_H
\ No newline at end of file |
