Code

Fix LPEs and break mask transform undo
[inkscape.git] / src / live_effects / lpe-spiro.cpp
1 #define INKSCAPE_LPE_SPIRO_C
3 /*
4  * Released under GNU GPL, read the file 'COPYING' for more information
5  */
7 #include "live_effects/lpe-spiro.h"
9 #include "display/curve.h"
10 #include "nodepath.h"
11 #include <typeinfo>
12 #include <2geom/pathvector.h>
13 #include <2geom/matrix.h>
14 #include <2geom/bezier-curve.h>
15 #include <2geom/hvlinesegment.h>
16 #include <2geom/isnan.h>
17 #include "helper/geom-nodetype.h"
18 #include "helper/geom-curves.h"
20 #include "live_effects/bezctx.h"
21 #include "live_effects/bezctx_intf.h"
22 #include "live_effects/spiro.h"
24 // For handling un-continuous paths:
25 #include "message-stack.h"
26 #include "inkscape.h"
27 #include "desktop.h"
29 #define SPIRO_SHOW_INFINITE_COORDINATE_CALLS
31 typedef struct {
32     bezctx base;
33     SPCurve *curve;
34     int is_open;
35 } bezctx_ink;
37 void bezctx_ink_moveto(bezctx *bc, double x, double y, int /*is_open*/)
38 {
39     bezctx_ink *bi = (bezctx_ink *) bc;
40     if ( IS_FINITE(x) && IS_FINITE(y) ) {
41         bi->curve->moveto(x, y);
42     }
43 #ifdef SPIRO_SHOW_INFINITE_COORDINATE_CALLS
44     else {
45         g_message("lpe moveto not finite");
46     }
47 #endif
48 }
50 void bezctx_ink_lineto(bezctx *bc, double x, double y)
51 {
52     bezctx_ink *bi = (bezctx_ink *) bc;
53     if ( IS_FINITE(x) && IS_FINITE(y) ) {
54         bi->curve->lineto(x, y);
55     }
56 #ifdef SPIRO_SHOW_INFINITE_COORDINATE_CALLS
57     else {
58         g_message("lpe lineto not finite");
59     }
60 #endif
61 }
63 void bezctx_ink_quadto(bezctx *bc, double xm, double ym, double x3, double y3)
64 {
65     bezctx_ink *bi = (bezctx_ink *) bc;
67     if ( IS_FINITE(xm) && IS_FINITE(ym) && IS_FINITE(x3) && IS_FINITE(y3) ) {
68         bi->curve->quadto(xm, ym, x3, y3);
69     }
70 #ifdef SPIRO_SHOW_INFINITE_COORDINATE_CALLS
71     else {
72         g_message("lpe quadto not finite");
73     }
74 #endif
75 }
77 void bezctx_ink_curveto(bezctx *bc, double x1, double y1, double x2, double y2,
78                     double x3, double y3)
79 {
80     bezctx_ink *bi = (bezctx_ink *) bc;
81     if ( IS_FINITE(x1) && IS_FINITE(y1) && IS_FINITE(x2) && IS_FINITE(y2) ) {
82         bi->curve->curveto(x1, y1, x2, y2, x3, y3);
83     }
84 #ifdef SPIRO_SHOW_INFINITE_COORDINATE_CALLS
85     else {
86         g_message("lpe curveto not finite");
87     }
88 #endif
89 }
91 bezctx *
92 new_bezctx_ink(SPCurve *curve) {
93     bezctx_ink *result = g_new(bezctx_ink, 1);
94     result->base.moveto = bezctx_ink_moveto;
95     result->base.lineto = bezctx_ink_lineto;
96     result->base.quadto = bezctx_ink_quadto;
97     result->base.curveto = bezctx_ink_curveto;
98     result->base.mark_knot = NULL;
99     result->curve = curve;
100     return &result->base;
106 namespace Inkscape {
107 namespace LivePathEffect {
109 LPESpiro::LPESpiro(LivePathEffectObject *lpeobject) :
110     Effect(lpeobject)
114 LPESpiro::~LPESpiro()
118 void
119 LPESpiro::doEffect(SPCurve * curve)
121     using Geom::X;
122     using Geom::Y;
124     // Make copy of old path as it is changed during processing
125     Geom::PathVector const original_pathv = curve->get_pathvector();
126     guint len = curve->get_segment_count() + 2;
128     curve->reset();
129     bezctx *bc = new_bezctx_ink(curve);
130     spiro_cp *path = g_new (spiro_cp, len);
131     int ip = 0;
133     for(Geom::PathVector::const_iterator path_it = original_pathv.begin(); path_it != original_pathv.end(); ++path_it) {
134         if (path_it->empty())
135             continue;
137         // start of path
138         {
139             Geom::Point p = path_it->front().pointAt(0);
140             path[ip].x = p[X];
141             path[ip].y = p[Y];
142             path[ip].ty = '{' ;  // for closed paths, this is overwritten
143             ip++;
144         }
146         // midpoints
147         Geom::Path::const_iterator curve_it1 = path_it->begin();      // incoming curve
148         Geom::Path::const_iterator curve_it2 = ++(path_it->begin());         // outgoing curve
150         Geom::Path::const_iterator curve_endit = path_it->end_default(); // this determines when the loop has to stop
151         if (path_it->closed()) {
152             // if the path is closed, maybe we have to stop a bit earlier because the closing line segment has zerolength.
153             const Geom::Curve &closingline = path_it->back_closed(); // the closing line segment is always of type Geom::LineSegment.
154             if (are_near(closingline.initialPoint(), closingline.finalPoint())) {
155                 // closingline.isDegenerate() did not work, because it only checks for *exact* zero length, which goes wrong for relative coordinates and rounding errors...
156                 // the closing line segment has zero-length. So stop before that one!
157                 curve_endit = path_it->end_open();
158             }
159         }
161         while ( curve_it2 != curve_endit )
162         {
163             /* This deals with the node between curve_it1 and curve_it2.
164              * Loop to end_default (so without last segment), loop ends when curve_it2 hits the end
165              * and then curve_it1 points to end or closing segment */
166             Geom::Point p = curve_it1->finalPoint();
167             path[ip].x = p[X];
168             path[ip].y = p[Y];
170             // Determine type of spiro node this is, determined by the tangents (angles) of the curves
171             // TODO: see if this can be simplified by using /helpers/geom-nodetype.cpp:get_nodetype
172             bool this_is_line = is_straight_curve(*curve_it1);
173             bool next_is_line = is_straight_curve(*curve_it2);
175             Geom::NodeType nodetype = Geom::get_nodetype(*curve_it1, *curve_it2);
177             if ( nodetype == Geom::NODE_SMOOTH || nodetype == Geom::NODE_SYMM )
178             {
179                 if (this_is_line && !next_is_line) {
180                     path[ip].ty = ']';
181                 } else if (next_is_line && !this_is_line) {
182                     path[ip].ty = '[';
183                 } else {
184                     path[ip].ty = 'c';
185                 }
186             } else {
187                 path[ip].ty = 'v';
188             }
190             ++curve_it1;
191             ++curve_it2;
192             ip++;
193         }
195         // add last point to the spiropath
196         Geom::Point p = curve_it1->finalPoint();
197         path[ip].x = p[X];
198         path[ip].y = p[Y];
199         if (path_it->closed()) {
200             // curve_it1 points to the (visually) closing segment. determine the match between first and this last segment (the closing node)
201             Geom::NodeType nodetype = Geom::get_nodetype(*curve_it1, path_it->front());
202             switch (nodetype) {
203                 case Geom::NODE_NONE: // can't happen! but if it does, it means the path isn't closed :-)
204                     path[ip].ty = '}';
205                     ip++;
206                     break;
207                 case Geom::NODE_CUSP:
208                     path[0].ty = path[ip].ty = 'v';
209                     break;
210                 case Geom::NODE_SMOOTH:
211                 case Geom::NODE_SYMM:
212                     path[0].ty = path[ip].ty = 'c';
213                     break;
214             }
215         } else {
216             // set type to path closer
217             path[ip].ty = '}';
218             ip++;
219         }
221         // run subpath through spiro
222         int sp_len = ip;
223         spiro_seg *s = run_spiro(path, sp_len);
224         spiro_to_bpath(s, sp_len, bc);
225         free(s);
226         ip = 0;
227     }
229     g_free (path);
232 }; //namespace LivePathEffect
233 }; /* namespace Inkscape */
235 /*
236   Local Variables:
237   mode:c++
238   c-file-style:"stroustrup"
239   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
240   indent-tabs-mode:nil
241   fill-column:99
242   End:
243 */
244 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :