From 7d83acaaadf2df11448379cfba85f6270a089374 Mon Sep 17 00:00:00 2001 From: Johan Engelen Date: Thu, 29 Jul 2010 00:13:19 +0200 Subject: [PATCH] powerstroke: code only, added bad interpolator --- src/live_effects/lpe-powerstroke.cpp | 49 +++++++++++++++++++++++++--- 1 file changed, 45 insertions(+), 4 deletions(-) diff --git a/src/live_effects/lpe-powerstroke.cpp b/src/live_effects/lpe-powerstroke.cpp index 447b85664..af5739b32 100644 --- a/src/live_effects/lpe-powerstroke.cpp +++ b/src/live_effects/lpe-powerstroke.cpp @@ -19,6 +19,7 @@ #include <2geom/piecewise.h> #include <2geom/sbasis-geometric.h> #include <2geom/transforms.h> +#include <2geom/bezier-utils.h> /// @TODO Move this to 2geom @@ -44,12 +45,12 @@ public: virtual ~Linear() {}; virtual Path interpolateToPath(std::vector points) { - Path strokepath; - strokepath.start( points.at(0) ); + Path path; + path.start( points.at(0) ); for (unsigned int i = 1 ; i < points.size(); ++i) { - strokepath.appendNew(points.at(i)); + path.appendNew(points.at(i)); } - return strokepath; + return path; }; private: @@ -57,6 +58,46 @@ private: Linear& operator=(const Linear&); }; +// this class is terrible +class CubicBezierFit : public Interpolator { +public: + CubicBezierFit() {}; + virtual ~CubicBezierFit() {}; + + virtual Path interpolateToPath(std::vector points) { + unsigned int n_points = points.size(); + // worst case gives us 2 segment per point + int max_segs = 8*n_points; + Geom::Point * b = g_new(Geom::Point, max_segs); + Geom::Point * points_array = g_new(Geom::Point, 4*n_points); + for (unsigned i = 0; i < n_points; ++i) { + points_array[i] = points.at(i); + } + + double tolerance_sq = 0; // this value is just a random guess + + int const n_segs = Geom::bezier_fit_cubic_r(b, points_array, n_points, + tolerance_sq, max_segs); + + Geom::Path fit; + if ( n_segs > 0) + { + fit.start(b[0]); + for (int c = 0; c < n_segs; c++) { + fit.appendNew(b[4*c+1], b[4*c+2], b[4*c+3]); + } + } + g_free(b); + g_free(points_array); + return fit; + }; + +private: + CubicBezierFit(const CubicBezierFit&); + CubicBezierFit& operator=(const CubicBezierFit&); +}; + + } //namespace Interpolate } //namespace Geom -- 2.30.2