Code

remove many unnecessary to_2geom and from_2geom calls
[inkscape.git] / src / dyna-draw-context.cpp
index c6140d17d08e30ce0ae78699df481b84b05b9788..c56ba7a4c197092d932ae979f8108b648530d833 100644 (file)
 #include "message-context.h"
 #include "prefs-utils.h"
 #include "pixmaps/cursor-calligraphy.xpm"
-#include "libnr/n-art-bpath.h"
-#include "libnr/nr-path.h"
 #include "libnr/nr-matrix-ops.h"
 #include "libnr/nr-scale-translate-ops.h"
+#include "libnr/nr-convert2geom.h"
 #include "xml/repr.h"
 #include "context-fns.h"
 #include "sp-item.h"
@@ -65,7 +64,8 @@
 #include "display/canvas-bpath.h"
 #include "display/canvas-arena.h"
 #include "livarot/Shape.h"
-#include "isnan.h"
+#include <2geom/isnan.h>
+#include <2geom/pathvector.h>
 
 #include "dyna-draw-context.h"
 
 
 #define DYNA_MIN_WIDTH 1.0e-6
 
-
-// FIXME: move it to some shared file to be reused by both calligraphy and dropper
-#define C1 0.552
-static NArtBpath const hatch_area_circle[] = {
-    { NR_MOVETO, 0, 0, 0, 0, -1, 0 },
-    { NR_CURVETO, -1, C1, -C1, 1, 0, 1 },
-    { NR_CURVETO, C1, 1, 1, C1, 1, 0 },
-    { NR_CURVETO, 1, -C1, C1, -1, 0, -1 },
-    { NR_CURVETO, -C1, -1, -1, -C1, -1, 0 },
-    { NR_END, 0, 0, 0, 0, 0, 0 }
-};
-#undef C1
-
-
 static void sp_dyna_draw_context_class_init(SPDynaDrawContextClass *klass);
 static void sp_dyna_draw_context_init(SPDynaDrawContext *ddc);
 static void sp_dyna_draw_context_dispose(GObject *object);
@@ -103,8 +89,8 @@ static gint sp_dyna_draw_context_root_handler(SPEventContext *ec, GdkEvent *even
 
 static void clear_current(SPDynaDrawContext *dc);
 static void set_to_accumulated(SPDynaDrawContext *dc, bool unionize);
-static void add_cap(SPCurve *curve, NR::Point const &pre, NR::Point const &from, NR::Point const &to, NR::Point const &post, double rounding);
-static void accumulate_calligraphic(SPDynaDrawContext *dc);
+static void add_cap(SPCurve *curve, NR::Point const &from, NR::Point const &to, double rounding);
+static bool accumulate_calligraphic(SPDynaDrawContext *dc);
 
 static void fit_and_split(SPDynaDrawContext *ddc, gboolean release);
 
@@ -206,11 +192,11 @@ sp_dyna_draw_context_setup(SPEventContext *ec)
     if (((SPEventContextClass *) dd_parent_class)->setup)
         ((SPEventContextClass *) dd_parent_class)->setup(ec);
 
-    ddc->accumulated = new SPCurve(32);
-    ddc->currentcurve = new SPCurve(4);
+    ddc->accumulated = new SPCurve();
+    ddc->currentcurve = new SPCurve();
 
-    ddc->cal1 = new SPCurve(32);
-    ddc->cal2 = new SPCurve(32);
+    ddc->cal1 = new SPCurve();
+    ddc->cal2 = new SPCurve();
 
     ddc->currentshape = sp_canvas_item_new(sp_desktop_sketch(ec->desktop), SP_TYPE_CANVAS_BPATH, NULL);
     sp_canvas_bpath_set_fill(SP_CANVAS_BPATH(ddc->currentshape), DDC_RED_RGBA, SP_WIND_RULE_EVENODD);
@@ -219,7 +205,15 @@ sp_dyna_draw_context_setup(SPEventContext *ec)
     g_signal_connect(G_OBJECT(ddc->currentshape), "event", G_CALLBACK(sp_desktop_root_handler), ec->desktop);
 
     {
-        SPCurve *c = SPCurve::new_from_foreign_bpath(hatch_area_circle);
+        /* TODO: this can be done either with an arcto, and should maybe also be put in a general file (other tools use this as well) */
+        SPCurve *c = new SPCurve();
+        const double C1 = 0.552;
+        c->moveto(-1,0);
+        c->curveto(-1, C1, -C1, 1, 0, 1 );
+        c->curveto(C1, 1, 1, C1, 1, 0 );
+        c->curveto(1, -C1, C1, -1, 0, -1 );
+        c->curveto(-C1, -1, -1, -C1, -1, 0 );
+        c->closepath();
         ddc->hatch_area = sp_canvas_bpath_new(sp_desktop_controls(ec->desktop), c);
         c->unref();
         sp_canvas_bpath_set_fill(SP_CANVAS_BPATH(ddc->hatch_area), 0x00000000,(SPWindRule)0);
@@ -613,7 +607,7 @@ sp_dyna_draw_context_root_handler(SPEventContext *event_context,
                     pointer = motion_dt * motion_to_curve;
 
                     // calculate the nearest point on the guide path
-                    NR::Maybe<Path::cut_position> position = get_nearest_position_on_Path(dc->hatch_livarot_path, pointer);
+                    boost::optional<Path::cut_position> position = get_nearest_position_on_Path(dc->hatch_livarot_path, pointer);
                     nearest = get_point_on_Path(dc->hatch_livarot_path, position->piece, position->t);
 
 
@@ -811,8 +805,10 @@ sp_dyna_draw_context_root_handler(SPEventContext *event_context,
 
             /* Create object */
             fit_and_split(dc, TRUE);
-            accumulate_calligraphic(dc);
-            set_to_accumulated(dc, event->button.state & GDK_SHIFT_MASK); // performs document_done
+            if (accumulate_calligraphic(dc))
+                set_to_accumulated(dc, event->button.state & GDK_SHIFT_MASK); // performs document_done
+            else
+                g_warning ("Failed to create path: invalid data in dc->cal1 or dc->cal2");
 
             /* reset accumulated curve */
             dc->accumulated->reset();
@@ -971,9 +967,6 @@ set_to_accumulated(SPDynaDrawContext *dc, bool unionize)
     SPDesktop *desktop = SP_EVENT_CONTEXT(dc)->desktop;
 
     if (!dc->accumulated->is_empty()) {
-        NArtBpath *abp;
-        gchar *str;
-
         if (!dc->repr) {
             /* Create object */
             Inkscape::XML::Document *xml_doc = sp_document_repr_doc(desktop->doc());
@@ -989,10 +982,9 @@ set_to_accumulated(SPDynaDrawContext *dc, bool unionize)
             item->transform = SP_ITEM(desktop->currentRoot())->getRelativeTransform(desktop->currentLayer());
             item->updateRepr();
         }
-        abp = nr_artpath_affine(dc->accumulated->get_bpath(), sp_desktop_dt2root_affine(desktop));
-        str = sp_svg_write_path(abp);
+        Geom::PathVector pathv = dc->accumulated->get_pathvector() * sp_desktop_dt2root_affine(desktop);
+        gchar *str = sp_svg_write_path(pathv);
         g_assert( str != NULL );
-        g_free(abp);
         dc->repr->setAttribute("d", str);
         g_free(str);
 
@@ -1018,57 +1010,70 @@ set_to_accumulated(SPDynaDrawContext *dc, bool unionize)
 
 static void
 add_cap(SPCurve *curve,
-        NR::Point const &pre, NR::Point const &from,
-        NR::Point const &to, NR::Point const &post,
+        NR::Point const &from,
+        NR::Point const &to, 
         double rounding)
 {
-    NR::Point vel = rounding * NR::rot90( to - from ) / sqrt(2.0);
-    double mag = NR::L2(vel);
-
-    NR::Point v_in = from - pre;
-    double mag_in = NR::L2(v_in);
-    if ( mag_in > DYNA_EPSILON ) {
-        v_in = mag * v_in / mag_in;
-    } else {
-        v_in = NR::Point(0, 0);
-    }
-
-    NR::Point v_out = to - post;
-    double mag_out = NR::L2(v_out);
-    if ( mag_out > DYNA_EPSILON ) {
-        v_out = mag * v_out / mag_out;
-    } else {
-        v_out = NR::Point(0, 0);
-    }
+    if (NR::L2( to - from ) > DYNA_EPSILON) {
+        NR::Point vel = rounding * NR::rot90( to - from ) / sqrt(2.0);
+        double mag = NR::L2(vel);
 
-    if ( NR::L2(v_in) > DYNA_EPSILON || NR::L2(v_out) > DYNA_EPSILON ) {
-        curve->curveto(from + v_in, to + v_out, to);
+        NR::Point v = mag * NR::rot90( to - from ) / NR::L2( to - from );
+        curve->curveto(from + v, to + v, to);
     }
 }
 
-static void
+static bool
 accumulate_calligraphic(SPDynaDrawContext *dc)
 {
-    if ( !dc->cal1->is_empty() && !dc->cal2->is_empty() ) {
-        dc->accumulated->reset(); /*  Is this required ?? */
+        if (
+            dc->cal1->is_empty() ||
+            dc->cal2->is_empty() ||
+            (dc->cal1->get_segment_count() <= 0) ||
+            dc->cal1->first_path()->closed() 
+            ) {
+            dc->cal1->reset();
+            dc->cal2->reset();
+            return false; // failure
+        }
+
         SPCurve *rev_cal2 = dc->cal2->create_reverse();
+        if (
+            (rev_cal2->get_segment_count() <= 0) ||
+            rev_cal2->first_path()->closed() 
+            ) {
+            rev_cal2->unref();
+            dc->cal1->reset();
+            dc->cal2->reset();
+            return false; // failure
+        }
+
+        Geom::CubicBezier const * dc_cal1_firstseg  = dynamic_cast<Geom::CubicBezier const *>( dc->cal1->first_segment() );
+        Geom::CubicBezier const * rev_cal2_firstseg = dynamic_cast<Geom::CubicBezier const *>( rev_cal2->first_segment() );
+        Geom::CubicBezier const * dc_cal1_lastseg   = dynamic_cast<Geom::CubicBezier const *>( dc->cal1->last_segment() );
+        Geom::CubicBezier const * rev_cal2_lastseg  = dynamic_cast<Geom::CubicBezier const *>( rev_cal2->last_segment() );
+
+        if (
+            !dc_cal1_firstseg ||
+            !rev_cal2_firstseg ||
+            !dc_cal1_lastseg ||
+            !rev_cal2_lastseg 
+            ) {
+            rev_cal2->unref();
+            dc->cal1->reset();
+            dc->cal2->reset();
+            return false; // failure
+        }
 
-        g_assert(dc->cal1->get_length() > 1);
-        g_assert(rev_cal2->get_length() > 1);
-        g_assert(SP_CURVE_SEGMENT(dc->cal1, 0)->code == NR_MOVETO_OPEN);
-        g_assert(SP_CURVE_SEGMENT(rev_cal2, 0)->code == NR_MOVETO_OPEN);
-        g_assert(SP_CURVE_SEGMENT(dc->cal1, 1)->code == NR_CURVETO);
-        g_assert(SP_CURVE_SEGMENT(rev_cal2, 1)->code == NR_CURVETO);
-        g_assert(SP_CURVE_SEGMENT(dc->cal1, dc->cal1->get_length()-1)->code == NR_CURVETO);
-        g_assert(SP_CURVE_SEGMENT(rev_cal2, rev_cal2->get_length()-1)->code == NR_CURVETO);
+        dc->accumulated->reset(); /*  Is this required ?? */
 
-        dc->accumulated->append(dc->cal1, FALSE);
+        dc->accumulated->append(dc->cal1, false);
 
-        add_cap(dc->accumulated, SP_CURVE_SEGMENT(dc->cal1, dc->cal1->get_length()-1)->c(2), SP_CURVE_SEGMENT(dc->cal1, dc->cal1->get_length()-1)->c(3), SP_CURVE_SEGMENT(rev_cal2, 0)->c(3), SP_CURVE_SEGMENT(rev_cal2, 1)->c(1), dc->cap_rounding);
+        add_cap(dc->accumulated, (*dc_cal1_lastseg)[3], (*rev_cal2_firstseg)[0], dc->cap_rounding);
 
-        dc->accumulated->append(rev_cal2, TRUE);
+        dc->accumulated->append(rev_cal2, true);
 
-        add_cap(dc->accumulated, SP_CURVE_SEGMENT(rev_cal2, rev_cal2->get_length()-1)->c(2), SP_CURVE_SEGMENT(rev_cal2, rev_cal2->get_length()-1)->c(3), SP_CURVE_SEGMENT(dc->cal1, 0)->c(3), SP_CURVE_SEGMENT(dc->cal1, 1)->c(1), dc->cap_rounding);
+        add_cap(dc->accumulated, (*rev_cal2_lastseg)[3], (*dc_cal1_firstseg)[0], dc->cap_rounding);
 
         dc->accumulated->closepath();
 
@@ -1076,7 +1081,8 @@ accumulate_calligraphic(SPDynaDrawContext *dc)
 
         dc->cal1->reset();
         dc->cal2->reset();
-    }
+
+        return true; // success
 }
 
 static double square(double const x)
@@ -1107,7 +1113,7 @@ fit_and_split(SPDynaDrawContext *dc, gboolean release)
 #endif
 
         /* Current calligraphic */
-        if ( dc->cal1->get_length() == 0 || dc->cal2->get_length() == 0 ) {
+        if ( dc->cal1->is_empty() || dc->cal2->is_empty() ) {
             /* dc->npoints > 0 */
             /* g_print("calligraphics(1|2) reset\n"); */
             dc->cal1->reset();
@@ -1137,8 +1143,7 @@ fit_and_split(SPDynaDrawContext *dc, gboolean release)
                 dc->currentcurve->reset();
                 dc->currentcurve->moveto(b1[0]);
                 for (NR::Point *bp1 = b1; bp1 < b1 + BEZIER_SIZE * nb1; bp1 += BEZIER_SIZE) {
-                    dc->currentcurve->curveto(bp1[1],
-                                     bp1[2], bp1[3]);
+                    dc->currentcurve->curveto(bp1[1], bp1[2], bp1[3]);
                 }
                 dc->currentcurve->lineto(b2[BEZIER_SIZE*(nb2-1) + 3]);
                 for (NR::Point *bp2 = b2 + BEZIER_SIZE * ( nb2 - 1 ); bp2 >= b2; bp2 -= BEZIER_SIZE) {
@@ -1146,7 +1151,7 @@ fit_and_split(SPDynaDrawContext *dc, gboolean release)
                 }
                 // FIXME: dc->segments is always NULL at this point??
                 if (!dc->segments) { // first segment
-                    add_cap(dc->currentcurve, b2[1], b2[0], b1[0], b1[1], dc->cap_rounding);
+                    add_cap(dc->currentcurve, b2[0], b1[0], dc->cap_rounding);
                 }
                 dc->currentcurve->closepath();
                 sp_canvas_bpath_set_bpath(SP_CANVAS_BPATH(dc->currentshape), dc->currentcurve);
@@ -1216,15 +1221,16 @@ draw_temporary_box(SPDynaDrawContext *dc)
 {
     dc->currentcurve->reset();
 
-    dc->currentcurve->moveto(dc->point1[dc->npoints-1]);
+    dc->currentcurve->moveto(dc->point2[dc->npoints-1]);
     for (gint i = dc->npoints-2; i >= 0; i--) {
-        dc->currentcurve->lineto(dc->point1[i]);
+        dc->currentcurve->lineto(dc->point2[i]);
     }
     for (gint i = 0; i < dc->npoints; i++) {
-        dc->currentcurve->lineto(dc->point2[i]);
+        dc->currentcurve->lineto(dc->point1[i]);
     }
+
     if (dc->npoints >= 2) {
-        add_cap(dc->currentcurve, dc->point2[dc->npoints-2], dc->point2[dc->npoints-1], dc->point1[dc->npoints-1], dc->point1[dc->npoints-2], dc->cap_rounding);
+        add_cap(dc->currentcurve, dc->point1[dc->npoints-1], dc->point2[dc->npoints-1], dc->cap_rounding);
     }
 
     dc->currentcurve->closepath();