diff --git a/src/pencil-context.cpp b/src/pencil-context.cpp
index f173371838c0c680d1cfa5310f0bf35eae0cd839..d6050ba04e9da0e8f495a334991af59887bffdfa 100644 (file)
--- a/src/pencil-context.cpp
+++ b/src/pencil-context.cpp
#include "message-context.h"
#include "modifier-fns.h"
#include "sp-path.h"
-#include "prefs-utils.h"
+#include "preferences.h"
#include "snap.h"
#include "pixmaps/cursor-pencil.xpm"
-#include "display/bezier-utils.h"
+#include <2geom/sbasis-to-bezier.h>
+#include <2geom/bezier-utils.h>
#include "display/canvas-bpath.h"
#include <glibmm/i18n.h>
#include "libnr/in-svg-plane.h"
-#include "libnr/n-art-bpath.h"
#include "context-fns.h"
#include "sp-namedview.h"
#include "xml/repr.h"
#include "document.h"
#include "desktop-style.h"
#include "macros.h"
+#include "display/curve.h"
+#include "livarot/Path.h"
static void sp_pencil_context_class_init(SPPencilContextClass *klass);
static void sp_pencil_context_init(SPPencilContext *pc);
@@ -52,15 +54,19 @@ static gint pencil_handle_button_press(SPPencilContext *const pc, GdkEventButton
static gint pencil_handle_motion_notify(SPPencilContext *const pc, GdkEventMotion const &mevent);
static gint pencil_handle_button_release(SPPencilContext *const pc, GdkEventButton const &revent);
static gint pencil_handle_key_press(SPPencilContext *const pc, guint const keyval, guint const state);
+static gint pencil_handle_key_release(SPPencilContext *const pc, guint const keyval, guint const state);
-static void spdc_set_startpoint(SPPencilContext *pc, NR::Point const p);
-static void spdc_set_endpoint(SPPencilContext *pc, NR::Point const p);
+static void spdc_set_startpoint(SPPencilContext *pc, Geom::Point const p);
+static void spdc_set_endpoint(SPPencilContext *pc, Geom::Point const p);
static void spdc_finish_endpoint(SPPencilContext *pc);
-static void spdc_add_freehand_point(SPPencilContext *pc, NR::Point p, guint state);
+static void spdc_add_freehand_point(SPPencilContext *pc, Geom::Point p, guint state);
static void fit_and_split(SPPencilContext *pc);
-
+static void interpolate(SPPencilContext *pc);
+static void sketch_interpolate(SPPencilContext *pc);
static SPDrawContextClass *pencil_parent_class;
+static Geom::Point pencil_drag_origin_w(0, 0);
+static bool pencil_within_tolerance = false;
/**
* Register SPPencilContext class with Gdk and return its type number.
pc->npoints = 0;
pc->state = SP_PENCIL_CONTEXT_IDLE;
- pc->req_tangent = NR::Point(0, 0);
+ pc->req_tangent = Geom::Point(0, 0);
+
+ // since SPPencilContext is not properly constructed...
+ pc->sketch_interpolation = Geom::Piecewise<Geom::D2<Geom::SBasis> >();
+ pc->sketch_n = 0;
}
/**
static void
sp_pencil_context_setup(SPEventContext *ec)
{
- if (prefs_get_int_attribute("tools.freehand.pencil", "selcue", 0) != 0) {
+ Inkscape::Preferences *prefs = Inkscape::Preferences::get();
+ if (prefs->getBool("/tools/freehand/pencil/selcue")) {
ec->enableSelectionCue();
}
/** Snaps new node relative to the previous node. */
static void
-spdc_endpoint_snap(SPPencilContext const *pc, NR::Point &p, guint const state)
+spdc_endpoint_snap(SPPencilContext const *pc, Geom::Point &p, guint const state)
{
- spdc_endpoint_snap_rotation(pc, p, pc->p[0], state);
- spdc_endpoint_snap_free(pc, p, state);
+ if ((state & GDK_CONTROL_MASK)) { //CTRL enables constrained snapping
+ spdc_endpoint_snap_rotation(pc, p, pc->p[0], state);
+ } else {
+ if (!(state & GDK_SHIFT_MASK)) { //SHIFT disables all snapping, except the angular snapping above
+ //After all, the user explicitely asked for angular snapping by
+ //pressing CTRL
+ spdc_endpoint_snap_free(pc, p, state);
+ }
+ }
}
/**
ret = pencil_handle_key_press(pc, get_group0_keyval (&event->key), event->key.state);
break;
+ case GDK_KEY_RELEASE:
+ ret = pencil_handle_key_release(pc, get_group0_keyval (&event->key), event->key.state);
+ break;
+
default:
break;
}
@@ -213,14 +235,17 @@ pencil_handle_button_press(SPPencilContext *const pc, GdkEventButton const &beve
return TRUE;
}
- NR::Point const button_w(bevent.x, bevent.y);
+ Geom::Point const button_w(bevent.x, bevent.y);
/* Find desktop coordinates */
- NR::Point p = pc->desktop->w2d(button_w);
+ Geom::Point p = pc->desktop->w2d(button_w);
/* Test whether we hit any anchor. */
SPDrawAnchor *anchor = spdc_test_inside(pc, button_w);
+ pencil_drag_origin_w = Geom::Point(bevent.x,bevent.y);
+ pencil_within_tolerance = true;
+
switch (pc->state) {
case SP_PENCIL_CONTEXT_ADDLINE:
/* Current segment will be finished with release */
@@ -228,29 +253,16 @@ pencil_handle_button_press(SPPencilContext *const pc, GdkEventButton const &beve
break;
default:
/* Set first point of sequence */
+ SnapManager &m = desktop->namedview->snap_manager;
+ m.setup(desktop);
+
if (bevent.state & GDK_CONTROL_MASK) {
- /* Create object */
- Inkscape::XML::Document *xml_doc = sp_document_repr_doc(desktop->doc());
- Inkscape::XML::Node *repr = xml_doc->createElement("svg:path");
- repr->setAttribute("sodipodi:type", "arc");
- SPItem *item = SP_ITEM(desktop->currentLayer()->appendChildRepr(repr));
- Inkscape::GC::release(repr);
- NR::Matrix const i2d (sp_item_i2d_affine (item));
- NR::Point pp = p * i2d;
- sp_repr_set_svg_double (repr, "sodipodi:cx", pp[NR::X]);
- sp_repr_set_svg_double (repr, "sodipodi:cy", pp[NR::Y]);
- sp_repr_set_int (repr, "sodipodi:rx", 10);
- sp_repr_set_int (repr, "sodipodi:ry", 10);
-
- /* Set style */
- sp_desktop_apply_style_tool(desktop, repr, "tools.shapes.arc", false);
-
- item->updateRepr();
- desktop->messageStack()->flash(Inkscape::NORMAL_MESSAGE, _("Creating single point"));
- sp_document_done(sp_desktop_document(desktop), SP_VERB_CONTEXT_PENCIL, _("Create single point"));
+ if (!(bevent.state & GDK_SHIFT_MASK)) {
+ m.freeSnapReturnByRef(Inkscape::SnapPreferences::SNAPPOINT_NODE, p, Inkscape::SNAPSOURCE_HANDLE);
+ }
+ spdc_create_single_dot(event_context, p, "/tools/freehand/pencil", bevent.state);
ret = true;
break;
-
}
if (anchor) {
p = anchor->dp;
@@ -258,16 +270,15 @@ pencil_handle_button_press(SPPencilContext *const pc, GdkEventButton const &beve
} else {
if (!(bevent.state & GDK_SHIFT_MASK)) {
-
- // This is the first click of a new curve; deselect item so that
+ // This is the first click of a new curve; deselect item so that
// this curve is not combined with it (unless it is drawn from its
// anchor, which is handled by the sibling branch above)
selection->clear();
desktop->messageStack()->flash(Inkscape::NORMAL_MESSAGE, _("Creating new path"));
- SnapManager const &m = desktop->namedview->snap_manager;
- p = m.freeSnap(Inkscape::Snapper::SNAPPOINT_NODE, p, NULL).getPoint();
+ m.freeSnapReturnByRef(Inkscape::SnapPreferences::SNAPPOINT_NODE, p, Inkscape::SNAPSOURCE_HANDLE);
} else if (selection->singleItem() && SP_IS_PATH(selection->singleItem())) {
desktop->messageStack()->flash(Inkscape::NORMAL_MESSAGE, _("Appending to selected path"));
+ m.freeSnapReturnByRef(Inkscape::SnapPreferences::SNAPPOINT_NODE, p, Inkscape::SNAPSOURCE_HANDLE);
}
}
pc->sa = anchor;
@@ -284,13 +295,15 @@ pencil_handle_button_press(SPPencilContext *const pc, GdkEventButton const &beve
static gint
pencil_handle_motion_notify(SPPencilContext *const pc, GdkEventMotion const &mevent)
{
- if (mevent.state & GDK_CONTROL_MASK) {
+ SPDesktop *const dt = pc->desktop;
+
+ if ((mevent.state & GDK_CONTROL_MASK) && (mevent.state & GDK_BUTTON1_MASK)) {
// mouse was accidentally moved during Ctrl+click;
// ignore the motion and create a single point
+ pc->is_drawing = false;
return TRUE;
}
gint ret = FALSE;
- SPDesktop *const dt = pc->desktop;
SPEventContext *event_context = SP_EVENT_CONTEXT(pc);
if (event_context->space_panning || mevent.state & GDK_BUTTON2_MASK || mevent.state & GDK_BUTTON3_MASK) {
@@ -308,10 +321,23 @@ pencil_handle_motion_notify(SPPencilContext *const pc, GdkEventMotion const &mev
}
/* Find desktop coordinates */
- NR::Point p = dt->w2d(NR::Point(mevent.x, mevent.y));
+ Geom::Point p = dt->w2d(Geom::Point(mevent.x, mevent.y));
/* Test whether we hit any anchor. */
- SPDrawAnchor *anchor = spdc_test_inside(pc, NR::Point(mevent.x, mevent.y));
+ SPDrawAnchor *anchor = spdc_test_inside(pc, Geom::Point(mevent.x, mevent.y));
+
+ if (pencil_within_tolerance) {
+ Inkscape::Preferences *prefs = Inkscape::Preferences::get();
+ gint const tolerance = prefs->getIntLimited("/options/dragtolerance/value", 0, 0, 100);
+ if ( Geom::LInfty( Geom::Point(mevent.x,mevent.y) - pencil_drag_origin_w ) < tolerance ) {
+ return FALSE; // Do not drag if we're within tolerance from origin.
+ }
+ }
+
+ // Once the user has moved farther than tolerance from the original location
+ // (indicating they intend to move the object, not click), then always process the
+ // motion notify coordinates as given (no snapping back to origin)
+ pencil_within_tolerance = false;
switch (pc->state) {
case SP_PENCIL_CONTEXT_ADDLINE:
@@ -319,7 +345,9 @@ pencil_handle_motion_notify(SPPencilContext *const pc, GdkEventMotion const &mev
if (anchor) {
p = anchor->dp;
} else {
- spdc_endpoint_snap(pc, p, mevent.state);
+ Geom::Point ptnr(p);
+ spdc_endpoint_snap(pc, ptnr, mevent.state);
+ p = ptnr;
}
spdc_set_endpoint(pc, p);
ret = TRUE;
@@ -327,7 +355,11 @@ pencil_handle_motion_notify(SPPencilContext *const pc, GdkEventMotion const &mev
default:
/* We may be idle or already freehand */
if ( mevent.state & GDK_BUTTON1_MASK && pc->is_drawing ) {
- pc->state = SP_PENCIL_CONTEXT_FREEHAND;
+ if (pc->state == SP_PENCIL_CONTEXT_IDLE) {
+ sp_event_context_discard_delayed_snap_event(event_context);
+ }
+ pc->state = SP_PENCIL_CONTEXT_FREEHAND;
+
if ( !pc->sa && !pc->green_anchor ) {
/* Create green anchor */
pc->green_anchor = sp_draw_anchor_new(pc, pc->green_curve, TRUE, pc->p[0]);
@@ -338,13 +370,11 @@ pencil_handle_motion_notify(SPPencilContext *const pc, GdkEventMotion const &mev
*/
if (anchor) {
p = anchor->dp;
- } else if ((mevent.state & GDK_SHIFT_MASK) == 0) {
- SnapManager const &m = dt->namedview->snap_manager;
- p = m.freeSnap(Inkscape::Snapper::SNAPPOINT_NODE, p, NULL).getPoint();
}
- if ( pc->npoints != 0 ) { // buttonpress may have happened before we entered draw context!
- spdc_add_freehand_point(pc, p, mevent.state);
- ret = TRUE;
+
+ if ( pc->npoints != 0) { // buttonpress may have happened before we entered draw context!
+ spdc_add_freehand_point(pc, p, mevent.state);
+ ret = TRUE;
}
if (anchor && !pc->anchor_statusbar) {
@@ -383,10 +413,10 @@ pencil_handle_button_release(SPPencilContext *const pc, GdkEventButton const &re
pc->is_drawing = false;
/* Find desktop coordinates */
- NR::Point p = dt->w2d(NR::Point(revent.x, revent.y));
+ Geom::Point p = dt->w2d(Geom::Point(revent.x, revent.y));
/* Test whether we hit any anchor. */
- SPDrawAnchor *anchor = spdc_test_inside(pc, NR::Point(revent.x,
+ SPDrawAnchor *anchor = spdc_test_inside(pc, Geom::Point(revent.x,
revent.y));
switch (pc->state) {
@@ -410,28 +440,45 @@ pencil_handle_button_release(SPPencilContext *const pc, GdkEventButton const &re
spdc_set_endpoint(pc, p);
spdc_finish_endpoint(pc);
pc->state = SP_PENCIL_CONTEXT_IDLE;
+ sp_event_context_discard_delayed_snap_event(event_context);
ret = TRUE;
break;
case SP_PENCIL_CONTEXT_FREEHAND:
- /* Finish segment now */
- /// \todo fixme: Clean up what follows (Lauris)
- if (anchor) {
- p = anchor->dp;
- }
- pc->ea = anchor;
- /* Write curves to object */
+ if (revent.state & GDK_MOD1_MASK) {
+ /* sketch mode: interpolate the sketched path and improve the current output path with the new interpolation. don't finish sketch */
- dt->messageStack()->flash(Inkscape::NORMAL_MESSAGE, _("Finishing freehand"));
+ sketch_interpolate(pc);
- spdc_concat_colors_and_flush(pc, FALSE);
- pc->sa = NULL;
- pc->ea = NULL;
- if (pc->green_anchor) {
- pc->green_anchor = sp_draw_anchor_destroy(pc->green_anchor);
+ if (pc->green_anchor) {
+ pc->green_anchor = sp_draw_anchor_destroy(pc->green_anchor);
+ }
+
+ pc->state = SP_PENCIL_CONTEXT_SKETCH;
+ } else {
+ /* Finish segment now */
+ /// \todo fixme: Clean up what follows (Lauris)
+ if (anchor) {
+ p = anchor->dp;
+ }
+ pc->ea = anchor;
+ /* Write curves to object */
+
+ dt->messageStack()->flash(Inkscape::NORMAL_MESSAGE, _("Finishing freehand"));
+
+ interpolate(pc);
+ spdc_concat_colors_and_flush(pc, FALSE);
+ pc->sa = NULL;
+ pc->ea = NULL;
+ if (pc->green_anchor) {
+ pc->green_anchor = sp_draw_anchor_destroy(pc->green_anchor);
+ }
+ pc->state = SP_PENCIL_CONTEXT_IDLE;
+ // reset sketch mode too
+ pc->sketch_n = 0;
}
- pc->state = SP_PENCIL_CONTEXT_IDLE;
ret = TRUE;
break;
+ case SP_PENCIL_CONTEXT_SKETCH:
default:
break;
}
@@ -448,7 +495,7 @@ pencil_handle_button_release(SPPencilContext *const pc, GdkEventButton const &re
}
static void
-pencil_cancel (SPPencilContext *const pc)
+pencil_cancel (SPPencilContext *const pc)
{
if (pc->grab) {
/* Release grab now */
}
pc->is_drawing = false;
-
pc->state = SP_PENCIL_CONTEXT_IDLE;
+ sp_event_context_discard_delayed_snap_event(SP_EVENT_CONTEXT(pc));
- sp_curve_reset(pc->red_curve);
+ pc->red_curve->reset();
sp_canvas_bpath_set_bpath(SP_CANVAS_BPATH(pc->red_bpath), NULL);
while (pc->green_bpaths) {
gtk_object_destroy(GTK_OBJECT(pc->green_bpaths->data));
pc->green_bpaths = g_slist_remove(pc->green_bpaths, pc->green_bpaths->data);
}
- sp_curve_reset(pc->green_curve);
+ pc->green_curve->reset();
if (pc->green_anchor) {
pc->green_anchor = sp_draw_anchor_destroy(pc->green_anchor);
}
sp_canvas_end_forced_full_redraws(pc->desktop->canvas);
}
-
static gint
pencil_handle_key_press(SPPencilContext *const pc, guint const keyval, guint const state)
{
@@ -495,7 +541,7 @@ pencil_handle_key_press(SPPencilContext *const pc, guint const keyval, guint con
case GDK_Escape:
if (pc->npoints != 0) {
// if drawing, cancel, otherwise pass it up for deselecting
- if (pc->is_drawing) {
+ if (pc->state != SP_PENCIL_CONTEXT_IDLE) {
pencil_cancel (pc);
ret = TRUE;
}
@@ -505,7 +551,7 @@ pencil_handle_key_press(SPPencilContext *const pc, guint const keyval, guint con
case GDK_Z:
if (mod_ctrl_only(state) && pc->npoints != 0) {
// if drawing, cancel, otherwise pass it up for undo
- if (pc->is_drawing) {
+ if (pc->state != SP_PENCIL_CONTEXT_IDLE) {
pencil_cancel (pc);
ret = TRUE;
}
@@ -514,10 +560,47 @@ pencil_handle_key_press(SPPencilContext *const pc, guint const keyval, guint con
case GDK_g:
case GDK_G:
if (mod_shift_only(state)) {
- sp_selection_to_guides();
+ sp_selection_to_guides(SP_EVENT_CONTEXT(pc)->desktop);
ret = true;
}
break;
+ case GDK_Alt_L:
+ case GDK_Alt_R:
+ case GDK_Meta_L:
+ case GDK_Meta_R:
+ if (pc->state == SP_PENCIL_CONTEXT_IDLE) {
+ pc->desktop->messageStack()->flash(Inkscape::NORMAL_MESSAGE, _("<b>Sketch mode</b>: holding <b>Alt</b> interpolates between sketched paths. Release <b>Alt</b> to finalize."));
+ }
+ break;
+ default:
+ break;
+ }
+ return ret;
+}
+
+static gint
+pencil_handle_key_release(SPPencilContext *const pc, guint const keyval, guint const /*state*/)
+{
+ gint ret = FALSE;
+ switch (keyval) {
+ case GDK_Alt_L:
+ case GDK_Alt_R:
+ case GDK_Meta_L:
+ case GDK_Meta_R:
+ if (pc->state == SP_PENCIL_CONTEXT_SKETCH) {
+ spdc_concat_colors_and_flush(pc, FALSE);
+ pc->sketch_n = 0;
+ pc->sa = NULL;
+ pc->ea = NULL;
+ if (pc->green_anchor) {
+ pc->green_anchor = sp_draw_anchor_destroy(pc->green_anchor);
+ }
+ pc->state = SP_PENCIL_CONTEXT_IDLE;
+ sp_event_context_discard_delayed_snap_event(SP_EVENT_CONTEXT(pc));
+ pc->desktop->messageStack()->flash(Inkscape::NORMAL_MESSAGE, _("Finishing freehand sketch"));
+ ret = TRUE;
+ }
+ break;
default:
break;
}
@@ -528,7 +611,7 @@ pencil_handle_key_press(SPPencilContext *const pc, guint const keyval, guint con
* Reset points and set new starting point.
*/
static void
-spdc_set_startpoint(SPPencilContext *const pc, NR::Point const p)
+spdc_set_startpoint(SPPencilContext *const pc, Geom::Point const p)
{
pc->npoints = 0;
pc->red_curve_is_valid = false;
* We change RED curve.
*/
static void
-spdc_set_endpoint(SPPencilContext *const pc, NR::Point const p)
+spdc_set_endpoint(SPPencilContext *const pc, Geom::Point const p)
{
if (pc->npoints == 0) {
return;
}
g_return_if_fail( pc->npoints > 0 );
- sp_curve_reset(pc->red_curve);
+ pc->red_curve->reset();
if ( ( p == pc->p[0] )
|| !in_svg_plane(p) )
{
pc->p[1] = p;
pc->npoints = 2;
- sp_curve_moveto(pc->red_curve, pc->p[0]);
- sp_curve_lineto(pc->red_curve, pc->p[1]);
+ pc->red_curve->moveto(pc->p[0]);
+ pc->red_curve->lineto(pc->p[1]);
pc->red_curve_is_valid = true;
sp_canvas_bpath_set_bpath(SP_CANVAS_BPATH(pc->red_bpath), pc->red_curve);
static void
spdc_finish_endpoint(SPPencilContext *const pc)
{
- if ( ( SP_CURVE_LENGTH(pc->red_curve) != 2 )
- || ( SP_CURVE_SEGMENT(pc->red_curve, 0)->c(3) ==
- SP_CURVE_SEGMENT(pc->red_curve, 1)->c(3) ) )
+ if ( ( pc->red_curve->is_empty() )
+ || ( *(pc->red_curve->first_point()) == *(pc->red_curve->second_point()) ) )
{
- sp_curve_reset(pc->red_curve);
+ pc->red_curve->reset();
sp_canvas_bpath_set_bpath(SP_CANVAS_BPATH(pc->red_bpath), NULL);
} else {
/* Write curves to object. */
}
}
+
static void
-spdc_add_freehand_point(SPPencilContext *pc, NR::Point p, guint /*state*/)
+spdc_add_freehand_point(SPPencilContext *pc, Geom::Point p, guint /*state*/)
{
g_assert( pc->npoints > 0 );
g_return_if_fail(unsigned(pc->npoints) < G_N_ELEMENTS(pc->p));
if ( ( p != pc->p[ pc->npoints - 1 ] )
&& in_svg_plane(p) )
{
+ pc->ps.push_back(p);
pc->p[pc->npoints++] = p;
fit_and_split(pc);
}
return x * x;
}
+static void
+interpolate(SPPencilContext *pc)
+{
+ if ( pc->ps.size() <= 1 ) {
+ return;
+ }
+
+ Inkscape::Preferences *prefs = Inkscape::Preferences::get();
+ double const tol = prefs->getDoubleLimited("/tools/freehand/pencil/tolerance", 10.0, 1.0, 100.0) * 0.4;
+ double const tolerance_sq = 0.02 * square( pc->desktop->w2d().descrim() *
+ tol) * exp(0.2*tol - 2);
+
+ g_assert(is_zero(pc->req_tangent)
+ || is_unit_vector(pc->req_tangent));
+ Geom::Point const tHatEnd(0, 0);
+
+ guint n_points = pc->ps.size();
+ pc->green_curve->reset();
+ pc->red_curve->reset();
+ pc->red_curve_is_valid = false;
+
+ Geom::Point * b = g_new(Geom::Point, 4*n_points);
+ Geom::Point * points = g_new(Geom::Point, 4*n_points);
+ for (unsigned int i = 0; i < pc->ps.size(); i++) {
+ points[i] = pc->ps[i];
+ }
+
+ // worst case gives us a segment per point
+ int max_segs = 4*n_points;
+
+ int const n_segs = Geom::bezier_fit_cubic_r(b, points, n_points,
+ tolerance_sq, max_segs);
+
+ if ( n_segs > 0)
+ {
+ /* Fit and draw and reset state */
+ pc->green_curve->moveto(b[0]);
+ for (int c = 0; c < n_segs; c++) {
+ pc->green_curve->curveto(b[4*c+1], b[4*c+2], b[4*c+3]);
+ }
+ sp_canvas_bpath_set_bpath(SP_CANVAS_BPATH(pc->red_bpath), pc->green_curve);
+
+ /* Fit and draw and copy last point */
+ g_assert(!pc->green_curve->is_empty());
+
+ /* Set up direction of next curve. */
+ {
+ Geom::CubicBezier const * last_seg = dynamic_cast<Geom::CubicBezier const *>(pc->green_curve->last_segment());
+ g_assert( last_seg ); // Relevance: validity of (*last_seg)[2]
+ pc->p[0] = last_seg->finalPoint();
+ pc->npoints = 1;
+ Geom::Point const req_vec( pc->p[0] - (*last_seg)[2] );
+ pc->req_tangent = ( ( Geom::is_zero(req_vec) || !in_svg_plane(req_vec) )
+ ? Geom::Point(0, 0)
+ : Geom::unit_vector(req_vec) );
+ }
+ }
+ g_free(b);
+ g_free(points);
+ pc->ps.clear();
+}
+
+
+/* interpolates the sketched curve and tweaks the current sketch interpolation*/
+static void
+sketch_interpolate(SPPencilContext *pc)
+{
+ if ( pc->ps.size() <= 1 ) {
+ return;
+ }
+
+ Inkscape::Preferences *prefs = Inkscape::Preferences::get();
+ double const tol = prefs->getDoubleLimited("/tools/freehand/pencil/tolerance", 10.0, 1.0, 100.0) * 0.4;
+ double const tolerance_sq = 0.02 * square( pc->desktop->w2d().descrim() *
+ tol) * exp(0.2*tol - 2);
+
+ bool average_all_sketches = prefs->getBool("/tools/freehand/pencil/average_all_sketches", true);
+
+ g_assert(is_zero(pc->req_tangent)
+ || is_unit_vector(pc->req_tangent));
+ Geom::Point const tHatEnd(0, 0);
+
+ guint n_points = pc->ps.size();
+ pc->red_curve->reset();
+ pc->red_curve_is_valid = false;
+
+ Geom::Point * b = g_new(Geom::Point, 4*n_points);
+ Geom::Point * points = g_new(Geom::Point, 4*n_points);
+ for (unsigned i = 0; i < pc->ps.size(); i++) {
+ points[i] = pc->ps[i];
+ }
+
+ // worst case gives us a segment per point
+ int max_segs = 4*n_points;
+
+ int const n_segs = Geom::bezier_fit_cubic_r(b, points, n_points,
+ tolerance_sq, max_segs);
+
+ if ( n_segs > 0)
+ {
+ Geom::Path fit(b[0]);
+ for (int c = 0; c < n_segs; c++) {
+ fit.appendNew<Geom::CubicBezier>(b[4*c+1], b[4*c+2], b[4*c+3]);
+ }
+ Geom::Piecewise<Geom::D2<Geom::SBasis> > fit_pwd2 = fit.toPwSb();
+
+ double t =0.;
+ if ( pc->sketch_n > 0 ) {
+ if (average_all_sketches) {
+ // Average = (sum of all) / n
+ // = (sum of all + new one) / n+1
+ // = ((old average)*n + new one) / n+1
+ t = pc->sketch_n / (pc->sketch_n + 1.);
+ } else {
+ t = 0.5;
+ }
+ pc->sketch_interpolation = Geom::lerp(t, fit_pwd2, pc->sketch_interpolation);
+ // simplify path, to eliminate small segments
+ Path *path = new Path;
+ path->LoadPathVector(Geom::path_from_piecewise(pc->sketch_interpolation, 0.01));
+ path->Simplify(0.5);
+ Geom::PathVector *pathv = path->MakePathVector();
+ pc->sketch_interpolation = (*pathv)[0].toPwSb();
+ delete path;
+ delete pathv;
+ } else {
+ pc->sketch_interpolation = fit_pwd2;
+ }
+ pc->sketch_n++;
+
+ pc->green_curve->reset();
+ pc->green_curve->set_pathvector(Geom::path_from_piecewise(pc->sketch_interpolation, 0.01));
+ sp_canvas_bpath_set_bpath(SP_CANVAS_BPATH(pc->red_bpath), pc->green_curve);
+
+ /* Fit and draw and copy last point */
+ g_assert(!pc->green_curve->is_empty());
+
+ /* Set up direction of next curve. */
+ {
+ Geom::CubicBezier const * last_seg = dynamic_cast<Geom::CubicBezier const *>(pc->green_curve->last_segment());
+ g_assert( last_seg ); // Relevance: validity of (*last_seg)[2]
+ pc->p[0] = last_seg->finalPoint();
+ pc->npoints = 1;
+ Geom::Point const req_vec( pc->p[0] - (*last_seg)[2] );
+ pc->req_tangent = ( ( Geom::is_zero(req_vec) || !in_svg_plane(req_vec) )
+ ? Geom::Point(0, 0)
+ : Geom::unit_vector(req_vec) );
+ }
+ }
+ g_free(b);
+ g_free(points);
+ pc->ps.clear();
+}
+
static void
fit_and_split(SPPencilContext *pc)
{
g_assert( pc->npoints > 1 );
- double const tolerance_sq = square( NR::expansion(pc->desktop->w2d())
- * prefs_get_double_attribute_limited("tools.freehand.pencil",
- "tolerance", 10.0, 1.0, 100.0) );
+ double const tolerance_sq = 0;
- NR::Point b[4];
+ Geom::Point b[4];
g_assert(is_zero(pc->req_tangent)
|| is_unit_vector(pc->req_tangent));
- NR::Point const tHatEnd(0, 0);
- int const n_segs = sp_bezier_fit_cubic_full(b, NULL, pc->p, pc->npoints,
- pc->req_tangent, tHatEnd, tolerance_sq, 1);
+ Geom::Point const tHatEnd(0, 0);
+ int const n_segs = Geom::bezier_fit_cubic_full(b, NULL, pc->p, pc->npoints,
+ pc->req_tangent, tHatEnd,
+ tolerance_sq, 1);
if ( n_segs > 0
&& unsigned(pc->npoints) < G_N_ELEMENTS(pc->p) )
{
/* Fit and draw and reset state */
- sp_curve_reset(pc->red_curve);
- sp_curve_moveto(pc->red_curve, b[0]);
- sp_curve_curveto(pc->red_curve, b[1], b[2], b[3]);
+ pc->red_curve->reset();
+ pc->red_curve->moveto(b[0]);
+ pc->red_curve->curveto(b[1], b[2], b[3]);
sp_canvas_bpath_set_bpath(SP_CANVAS_BPATH(pc->red_bpath), pc->red_curve);
pc->red_curve_is_valid = true;
} else {
/* Fit and draw and copy last point */
- g_assert(!sp_curve_empty(pc->red_curve));
+ g_assert(!pc->red_curve->is_empty());
/* Set up direction of next curve. */
{
- NArtBpath const &last_seg = *sp_curve_last_bpath(pc->red_curve);
- pc->p[0] = last_seg.c(3);
+ Geom::CubicBezier const * last_seg = dynamic_cast<Geom::CubicBezier const *>(pc->red_curve->last_segment());
+ g_assert( last_seg ); // Relevance: validity of (*last_seg)[2]
+ pc->p[0] = last_seg->finalPoint();
pc->npoints = 1;
- g_assert( last_seg.code == NR_CURVETO );
- /* Relevance: validity of last_seg.c(2). */
- NR::Point const req_vec( pc->p[0] - last_seg.c(2) );
- pc->req_tangent = ( ( NR::is_zero(req_vec) || !in_svg_plane(req_vec) )
- ? NR::Point(0, 0)
- : NR::unit_vector(req_vec) );
+ Geom::Point const req_vec( pc->p[0] - (*last_seg)[2] );
+ pc->req_tangent = ( ( Geom::is_zero(req_vec) || !in_svg_plane(req_vec) )
+ ? Geom::Point(0, 0)
+ : Geom::unit_vector(req_vec) );
}
- sp_curve_append_continuous(pc->green_curve, pc->red_curve, 0.0625);
- SPCurve *curve = sp_curve_copy(pc->red_curve);
+ pc->green_curve->append_continuous(pc->red_curve, 0.0625);
+ SPCurve *curve = pc->red_curve->copy();
/// \todo fixme:
SPCanvasItem *cshape = sp_canvas_bpath_new(sp_desktop_sketch(pc->desktop), curve);
- sp_curve_unref(curve);
+ curve->unref();
sp_canvas_bpath_set_stroke(SP_CANVAS_BPATH(cshape), pc->green_color, 1.0, SP_STROKE_LINEJOIN_MITER, SP_STROKE_LINECAP_BUTT);
pc->green_bpaths = g_slist_prepend(pc->green_bpaths, cshape);