Code

simplify SPCurve::penultimate_point and return 2geom type
[inkscape.git] / src / display / curve.cpp
1 #define __CURVE_C__
3 /** \file
4  * Routines for SPCurve and for NArtBpath arrays / Geom::PathVector in general.
5  */
7 /*
8  * Author:
9  *   Lauris Kaplinski <lauris@kaplinski.com>
10  *
11  * Copyright (C) 2000 Lauris Kaplinski
12  * Copyright (C) 2000-2001 Ximian, Inc.
13  * Copyright (C) 2002 Lauris Kaplinski
14  * Copyright (C) 2008 Johan Engelen
15  *
16  * Released under GNU GPL
17  */
19 #include "display/curve.h"
21 #include <string.h>
22 #include <glib/gmem.h>
23 #include "libnr/nr-point.h"
24 #include "libnr/nr-rect.h"
25 #include <libnr/n-art-bpath.h>
26 #include <libnr/nr-point-matrix-ops.h>
27 #include <libnr/nr-translate-ops.h>
28 #include <libnr/n-art-bpath-2geom.h>
29 #include <libnr/nr-convert2geom.h>
30 #include <cstring>
31 #include <string>
32 #include <2geom/pathvector.h>
33 #include <2geom/sbasis-geometric.h>
34 #include <2geom/sbasis-to-bezier.h>
35 #include "svg/svg.h"
37 static unsigned sp_bpath_length(NArtBpath const bpath[]);
38 static bool sp_bpath_closed(NArtBpath const bpath[]);
40 #define NO_CHECKS   // define this to disable the checking for unequal paths in SPCurve, improves performance by a lot!
43 #ifndef NO_CHECKS
44 static void debug_out( char const * text, Geom::PathVector const & pathv) {
45     char * str = sp_svg_write_path(pathv);
46     g_message("%s : %s", text, str);
47     g_free(str);
48 }
49 #endif
51 #ifndef NO_CHECKS
52 static void debug_out( char const * text, NArtBpath const * bpath) {
53     char * str = sp_svg_write_path(bpath);
54     g_message("%s : %s", text, str);
55     g_free(str);
56 }
57 #endif
59 #ifndef NO_CHECKS
60 void SPCurve::debug_check( char const * text, SPCurve const * curve) {
61     char * pathv_str = sp_svg_write_path(curve->_pathv);
62     char * bpath_str = sp_svg_write_path(curve->_bpath);
63     if ( strcmp(pathv_str, bpath_str) ) {
64         g_message("%s : unequal paths", text);
65         g_message("bpath : %s", bpath_str);
66         g_message("pathv : %s", pathv_str);
67     }
68     g_free(pathv_str);
69     g_free(bpath_str);
70 #else
71 void SPCurve::debug_check( char const *, SPCurve const *) {
72 #endif
73 }
75 #ifndef NO_CHECKS
76 void SPCurve::debug_check( char const * text, bool a) {
77     if ( !a ) {
78         g_message("%s : bool fail", text);
79     }
80 #else
81 void SPCurve::debug_check( char const *, bool) {
82 #endif
83 }
85 /* Constructors */
87 /**
88  * The returned curve's state is as if SPCurve::reset has just been called on it.
89  * \param length Initial number of NArtBpath elements allocated for bpath (including NR_END
90  *    element).
91  * 2GEOMproof
92  */
93 SPCurve::SPCurve(guint length)
94   : _refcount(1),
95     _bpath(NULL),
96     _pathv(),
97     _end(0),
98     _length(length),
99     _substart(0),
100     _hascpt(false),
101     _posSet(false),
102     _moving(false),
103     _closed(false)
105     if (length <= 0) {
106         g_error("SPCurve::SPCurve called with invalid length parameter");
107         throw;
108     }
110     _bpath = g_new(NArtBpath, length);
111     _bpath->code = NR_END;
113     _pathv.clear();
115     debug_check("SPCurve::SPCurve(guint length)", this);
118 SPCurve::SPCurve(Geom::PathVector const& pathv)
119   : _refcount(1),
120     _bpath(NULL),
121     _pathv(pathv),
122     _end(0),
123     _length(0),
124     _substart(0),
125     _hascpt(false),
126     _posSet(false),
127     _moving(false),
128     _closed(false)
130     // temporary code to convert to _bpath as well:
131     _bpath = BPath_from_2GeomPath(_pathv);
132     unsigned const len = sp_bpath_length(_bpath);
133     _length = len;
134     _end = _length - 1;
135     gint i = _end;
136     for (; i > 0; i--)
137         if ((_bpath[i].code == NR_MOVETO) ||
138             (_bpath[i].code == NR_MOVETO_OPEN))
139             break;
140     _substart = i;
141     _closed = sp_bpath_closed(_bpath);
143     debug_check("SPCurve::SPCurve(Geom::PathVector const& pathv)", this);
146 // * 2GEOMproof
147 SPCurve *
148 SPCurve::new_from_foreign_bpath(NArtBpath const *bpath)
150     g_return_val_if_fail(bpath != NULL, NULL);
152     NArtBpath *new_bpath;
153     unsigned const len = sp_bpath_length(bpath);
154     new_bpath = g_new(NArtBpath, len);
155     memcpy(new_bpath, bpath, len * sizeof(NArtBpath));
157     SPCurve *curve = new SPCurve();
159     curve->_bpath = new_bpath;
160     curve->_length = len;
161     curve->_end = curve->_length - 1;
162     gint i = curve->_end;
163     for (; i > 0; i--)
164         if ((curve->_bpath[i].code == NR_MOVETO) ||
165             (curve->_bpath[i].code == NR_MOVETO_OPEN))
166             break;
167     curve->_substart = i;
168     curve->_closed = sp_bpath_closed(new_bpath);
170     curve->_pathv = BPath_to_2GeomPath(curve->_bpath);
172     debug_check("SPCurve::new_from_foreign_bpath", curve);
174     return curve;
177 /**
178  * Convert NArtBpath object to SPCurve object.
179  *
180  * \return new SPCurve, or NULL if the curve was not created for some reason.
181  * 2GEOMproof
182  */
183 SPCurve *
184 SPCurve::new_from_bpath(NArtBpath *bpath)
186     g_return_val_if_fail(bpath != NULL, NULL);
188     SPCurve *curve = SPCurve::new_from_foreign_bpath(bpath);
189     g_free(bpath);
191     debug_check("SPCurve::new_from_bpath", curve);
193     return curve;
196 // * 2GEOMproof
197 SPCurve *
198 SPCurve::new_from_rect(NR::Maybe<NR::Rect> const &rect)
200     g_return_val_if_fail(rect, NULL);
202     SPCurve *c =  new SPCurve();
204     NR::Point p = rect->corner(0);
205     c->moveto(p);
207     for (int i=3; i>=0; i--) {
208         c->lineto(rect->corner(i));
209     }
210     c->closepath_current();
212     debug_check("SPCurve::new_from_rect", c);
214     return c;
217 // * 2GEOMproof
218 SPCurve::~SPCurve()
220     if (_bpath) {
221         g_free(_bpath);
222         _bpath = NULL;
223     }
226 /* Methods */
228 void
229 SPCurve::set_pathvector(Geom::PathVector const & new_pathv)
231     _pathv = new_pathv;
233     _hascpt = false;
234     _posSet = false;
235     _moving = false;
237     // temporary code to convert to _bpath as well:
238     if (_bpath) {
239         g_free(_bpath);
240         _bpath = NULL;
241     }
242     _bpath = BPath_from_2GeomPath(_pathv);
243     unsigned const len = sp_bpath_length(_bpath);
244     _length = len;
245     _end = _length - 1;
246     gint i = _end;
247     for (; i > 0; i--)
248         if ((_bpath[i].code == NR_MOVETO) ||
249             (_bpath[i].code == NR_MOVETO_OPEN))
250             break;
251     _substart = i;
252     _closed = sp_bpath_closed(_bpath);
254     debug_check("SPCurve::set_pathvector", this);
257 /**
258  * Get pointer to bpath data. Don't keep this reference too long, because the path might change by another function.
259  */
260 NArtBpath const *
261 SPCurve::get_bpath() const
263     debug_check("SPCurve::get_bpath", this);
264     return _bpath;
265 };
267 Geom::PathVector const &
268 SPCurve::get_pathvector() const
270     debug_check("SPCurve::get_pathvector", this);
271     return _pathv;
274 /**
275  *Returns index in bpath[] of NR_END element.
276  * remove for 2geom
277  */
278 guint
279 SPCurve::get_length() const
281 //    g_message("SPCurve::get_length must be removed");
283     return _end;
286 /*
287  * Returns the number of segments of all paths summed
288  */
289 guint
290 SPCurve::get_segment_count() const
292     guint nr = 0;
293     for(Geom::PathVector::const_iterator it = _pathv.begin(); it != _pathv.end(); ++it) {
294         nr += (*it).size();
296         if (it->closed())   nr += 1;
297     }
298     return nr;
302 /**
303  * Increase _refcount of curve.
304  *
305  * \todo should this be shared with other refcounting code?
306  * 2GEOMproof
307  */
308 SPCurve *
309 SPCurve::ref()
311     g_return_val_if_fail(this != NULL, NULL);
313     _refcount += 1;
315     return this;
318 /**
319  * Decrease refcount of curve, with possible destruction.
320  *
321  * \todo should this be shared with other refcounting code?
322  * 2GEOMproof
323  */
324 SPCurve *
325 SPCurve::unref()
327     g_return_val_if_fail(this != NULL, NULL);
329     _refcount -= 1;
331     if (_refcount < 1) {
332         delete this;
333     }
335     return NULL;
338 /**
339  * Add space for more paths in curve.
340  * This function has no meaning for 2geom representation, other than maybe for optimization issues (enlargening the vector for what is to come)
341  * 2GEOMproof
342  */
343 void
344 SPCurve::ensure_space(guint space)
346     g_return_if_fail(this != NULL);
347     g_return_if_fail(space > 0);
349     if (_end + space < _length)
350         return;
352     if (space < SP_CURVE_LENSTEP)
353         space = SP_CURVE_LENSTEP;
355     _bpath = g_renew(NArtBpath, _bpath, _length + space);
357     _length += space;
360 /**
361  * Create new curve from its own bpath array.
362  * 2GEOMproof
363  */
364 SPCurve *
365 SPCurve::copy() const
367     g_return_val_if_fail(this != NULL, NULL);
369     return SPCurve::new_from_foreign_bpath(_bpath);
372 /**
373  * Return new curve that is the concatenation of all curves in list.
374  * 2GEOMified
375  */
376 SPCurve *
377 SPCurve::concat(GSList const *list)
379     g_return_val_if_fail(list != NULL, NULL);
381     gint length = 0;
383     for (GSList const *l = list; l != NULL; l = l->next) {
384         SPCurve *c = (SPCurve *) l->data;
385         length += c->_end;
386     }
388     SPCurve *new_curve = new SPCurve(length + 1);
390     NArtBpath *bp = new_curve->_bpath;
392     for (GSList const *l = list; l != NULL; l = l->next) {
393         SPCurve *c = (SPCurve *) l->data;
394         memcpy(bp, c->_bpath, c->_end * sizeof(NArtBpath));
395         bp += c->_end;
396     }
398     bp->code = NR_END;
400     new_curve->_end = length;
401     gint i;
402     for (i = new_curve->_end; i > 0; i--) {
403         if ((new_curve->_bpath[i].code == NR_MOVETO)     ||
404             (new_curve->_bpath[i].code == NR_MOVETO_OPEN)  )
405             break;
406     }
408     new_curve->_substart = i;
410     for (GSList const *l = list; l != NULL; l = l->next) {
411         SPCurve *c = (SPCurve *) l->data;
412         new_curve->_pathv.insert( new_curve->_pathv.end(), c->get_pathvector().begin(), c->get_pathvector().end() );
413     }
415     debug_check("SPCurve::concat", new_curve);
417     return new_curve;
420 /**
421  * Returns a list of new curves corresponding to the subpaths in \a curve.
422  * 2geomified
423  */
424 GSList *
425 SPCurve::split() const
427     g_return_val_if_fail(this != NULL, NULL);
429     guint p = 0;
430     GSList *l = NULL;
432     gint pathnr = 0;
433     while (p < _end) {
434         gint i = 1;
435         while ((_bpath[p + i].code == NR_LINETO) ||
436                (_bpath[p + i].code == NR_CURVETO))
437             i++;
438         SPCurve *new_curve = new SPCurve(i + 1);
439         memcpy(new_curve->_bpath, _bpath + p, i * sizeof(NArtBpath));
440         new_curve->_end = i;
441         new_curve->_bpath[i].code = NR_END;
442         new_curve->_substart = 0;
443         new_curve->_closed = (new_curve->_bpath->code == NR_MOVETO);
444         new_curve->_hascpt = (new_curve->_bpath->code == NR_MOVETO_OPEN);
445         new_curve->_pathv = Geom::PathVector(1, _pathv[pathnr]);
446         l = g_slist_prepend(l, new_curve);
447         p += i;
448         pathnr++;
449     }
451     return l;
454 /**
455  * Transform all paths in curve, template helper.
456  */
457 template<class M>
458 static void
459 tmpl_curve_transform(SPCurve * curve, M const &m)
461     g_return_if_fail(curve != NULL);
463     for (guint i = 0; i < curve->_end; i++) {
464         NArtBpath *p = curve->_bpath + i;
465         switch (p->code) {
466             case NR_MOVETO:
467             case NR_MOVETO_OPEN:
468             case NR_LINETO: {
469                 p->setC(3, p->c(3) * m);
470                 break;
471             }
472             case NR_CURVETO:
473                 for (unsigned i = 1; i <= 3; ++i) {
474                     p->setC(i, p->c(i) * m);
475                 }
476                 break;
477             default:
478                 g_warning("Illegal pathcode %d", p->code);
479                 break;
480         }
481     }
484 /**
485  * Transform all paths in curve using matrix.
486  * 2GEOMified, can be deleted when completely 2geom
487  */
488 void
489 SPCurve::transform(NR::Matrix const &m)
491     tmpl_curve_transform<NR::Matrix>(this, m);
493     _pathv = _pathv * to_2geom(m);
495     debug_check("SPCurve::transform(NR::Matrix const &m)", this);
498 /**
499  * Transform all paths in curve using matrix.
500  */
501 void
502 SPCurve::transform(Geom::Matrix const &m)
504     tmpl_curve_transform<NR::Matrix>(this, from_2geom(m));
506     _pathv = _pathv * m;
508     debug_check("SPCurve::transform(Geom::Matrix const &m)", this);
511 /**
512  * Transform all paths in curve using NR::translate.
513  * 2GEOMified, can be deleted when completely 2geom
514  */
515 void
516 SPCurve::transform(NR::translate const &m)
518     tmpl_curve_transform<NR::translate>(this, m);
520     _pathv = _pathv * to_2geom(m);
522     debug_check("SPCurve::transform(NR::translate const &m)", this);
525 /**
526  * Set curve to empty curve.
527  * 2GEOMified
528  */
529 void
530 SPCurve::reset()
532     g_return_if_fail(this != NULL);
534     _bpath->code = NR_END;
535     _end = 0;
536     _substart = 0;
537     _hascpt = false;
538     _posSet = false;
539     _moving = false;
540     _closed = false;
542     _pathv.clear();
544     debug_check("SPCurve::reset", this);
547 /* Several consecutive movetos are ALLOWED */
549 /**
550  * Calls SPCurve::moveto() with point made of given coordinates.
551  */
552 void
553 SPCurve::moveto(gdouble x, gdouble y)
555     moveto(NR::Point(x, y));
557 /**
558  * Calls SPCurve::moveto() with point made of given coordinates.
559  */
560 void
561 SPCurve::moveto(Geom::Point const &p)
563     moveto(from_2geom(p));
565 /**
566  * Perform a moveto to a point, thus starting a new subpath.
567  * 2GEOMified
568  */
569 void
570 SPCurve::moveto(NR::Point const &p)
572     g_return_if_fail(this != NULL);
573     g_return_if_fail(!_moving);
575     _substart = _end;
576     _hascpt = true;
577     _posSet = true;
578     _movePos = p;
579     _pathv.push_back( Geom::Path() );  // for some reason Geom::Path(p) does not work...
580     _pathv.back().start(to_2geom(p));
582     // the output is not the same. This is because SPCurve *incorrectly* coaslesces multiple moveto's into one for NArtBpath.
583 //    debug_check("SPCurve::moveto", this);
586 /**
587  * Calls SPCurve::lineto() with a point's coordinates.
588  */
589 void
590 SPCurve::lineto(Geom::Point const &p)
592     lineto(p[Geom::X], p[Geom::Y]);
594 /**
595  * Calls SPCurve::lineto() with a point's coordinates.
596  */
597 void
598 SPCurve::lineto(NR::Point const &p)
600     lineto(p[NR::X], p[NR::Y]);
602 /**
603  * Adds a line to the current subpath.
604  * 2GEOMified
605  */
606 void
607 SPCurve::lineto(gdouble x, gdouble y)
609     g_return_if_fail(this != NULL);
610     g_return_if_fail(_hascpt);
612     if (_moving) {
613         /* fix endpoint */
614         g_return_if_fail(!_posSet);
615         g_return_if_fail(_end > 1);
616         NArtBpath *bp = _bpath + _end - 1;
617         g_return_if_fail(bp->code == NR_LINETO);
618         bp->x3 = x;
619         bp->y3 = y;
620         _moving = false;
622         Geom::Path::iterator it = _pathv.back().end();
623         if ( Geom::LineSegment const *last_line_segment = dynamic_cast<Geom::LineSegment const *>( &(*it) )) {
624             Geom::LineSegment new_seg( *last_line_segment );
625             new_seg.setFinal( Geom::Point(x,y) );
626             _pathv.back().replace(it, new_seg);
627         }
628     } else if (_posSet) {
629         /* start a new segment */
630         ensure_space(2);
631         NArtBpath *bp = _bpath + _end;
632         bp->code = NR_MOVETO_OPEN;
633         bp->setC(3, _movePos);
634         bp++;
635         bp->code = NR_LINETO;
636         bp->x3 = x;
637         bp->y3 = y;
638         bp++;
639         bp->code = NR_END;
640         _end += 2;
641         _posSet = false;
642         _closed = false;
644         _pathv.back().appendNew<Geom::LineSegment>( Geom::Point(x,y) );
645         return;
646     } else {
647         /* add line */
649         g_return_if_fail(_end > 1);
650         ensure_space(1);
651         NArtBpath *bp = _bpath + _end;
652         bp->code = NR_LINETO;
653         bp->x3 = x;
654         bp->y3 = y;
655         bp++;
656         bp->code = NR_END;
657         _end++;
658         _pathv.back().appendNew<Geom::LineSegment>( Geom::Point(x,y) );
659     }
661     debug_check("SPCurve::lineto", this);
664 /**
665  * Calls SPCurve::curveto() with coordinates of three points.
666  */
667 void
668 SPCurve::curveto(Geom::Point const &p0, Geom::Point const &p1, Geom::Point const &p2)
670     using Geom::X;
671     using Geom::Y;
672     curveto( p0[X], p0[Y],
673              p1[X], p1[Y],
674              p2[X], p2[Y] );
676 /**
677  * Calls SPCurve::curveto() with coordinates of three points.
678  */
679 void
680 SPCurve::curveto(NR::Point const &p0, NR::Point const &p1, NR::Point const &p2)
682     using NR::X;
683     using NR::Y;
684     curveto( p0[X], p0[Y],
685              p1[X], p1[Y],
686              p2[X], p2[Y] );
688 /**
689  * Adds a bezier segment to the current subpath.
690  * 2GEOMified
691  */
692 void
693 SPCurve::curveto(gdouble x0, gdouble y0, gdouble x1, gdouble y1, gdouble x2, gdouble y2)
695     g_return_if_fail(this != NULL);
696     g_return_if_fail(_hascpt);
697     g_return_if_fail(!_moving);
699     if (_posSet) {
700         /* start a new segment */
701         ensure_space(2);
702         NArtBpath *bp = _bpath + _end;
703         bp->code = NR_MOVETO_OPEN;
704         bp->setC(3, _movePos);
705         bp++;
706         bp->code = NR_CURVETO;
707         bp->x1 = x0;
708         bp->y1 = y0;
709         bp->x2 = x1;
710         bp->y2 = y1;
711         bp->x3 = x2;
712         bp->y3 = y2;
713         bp++;
714         bp->code = NR_END;
715         _end += 2;
716         _posSet = false;
717         _closed = false;
718         _pathv.back().appendNew<Geom::CubicBezier>( Geom::Point(x0,y0), Geom::Point(x1,y1), Geom::Point(x2,y2) );
719     } else {
720         /* add curve */
722         g_return_if_fail(_end > 1);
723         ensure_space(1);
724         NArtBpath *bp = _bpath + _end;
725         bp->code = NR_CURVETO;
726         bp->x1 = x0;
727         bp->y1 = y0;
728         bp->x2 = x1;
729         bp->y2 = y1;
730         bp->x3 = x2;
731         bp->y3 = y2;
732         bp++;
733         bp->code = NR_END;
734         _end++;
735         if (_pathv.empty())  g_message("leeg");
736         else _pathv.back().appendNew<Geom::CubicBezier>( Geom::Point(x0,y0), Geom::Point(x1,y1), Geom::Point(x2,y2) );
737     }
739     debug_check("SPCurve::curveto", this);
742 /**
743  * Close current subpath by possibly adding a line between start and end.
744   * 2GEOMified
745  */
746 void
747 SPCurve::closepath()
749     g_return_if_fail(this != NULL);
750     g_return_if_fail(_hascpt);
751     g_return_if_fail(!_posSet);
752     g_return_if_fail(!_moving);
753     g_return_if_fail(!_closed);
754     /* We need at least moveto, curveto, end. */
755     g_return_if_fail(_end - _substart > 1);
757     {
758         NArtBpath *bs = _bpath + _substart;
759         NArtBpath *be = _bpath + _end - 1;
761         if (bs->c(3) != be->c(3)) {
762             lineto(bs->c(3));
763             bs = _bpath + _substart;
764         }
766         bs->code = NR_MOVETO;
767     }
768     // Inkscape always manually adds the closing line segment to SPCurve with a lineto.
769     // This lineto is removed in the writing function for NArtBpath, 
770     // so when path is closed and the last segment is a lineto, the closing line segment must really be removed first!
771     // TODO: fix behavior in Inkscape!
772     if ( /*Geom::LineSegment const *line_segment = */ dynamic_cast<Geom::LineSegment const  *>(&_pathv.back().back())) {
773         _pathv.back().erase_last();
774     }
775     _pathv.back().close(true);
776     _closed = true;
778     for (Geom::PathVector::const_iterator it = _pathv.begin(); it != _pathv.end(); it++) {
779          if ( ! it->closed() ) {
780             _closed = false;
781             break;
782         }
783     }
785     for (NArtBpath const *bp = _bpath; bp->code != NR_END; bp++) {
786         /** \todo
787          * effic: Maintain a count of NR_MOVETO_OPEN's (e.g. instead of
788          * the closed boolean).
789          */
790         if (bp->code == NR_MOVETO_OPEN) {
791             _closed = false;
792             break;
793         }
794     }
796     _hascpt = false;
798     debug_check("SPCurve::closepath", this);
801 /** Like SPCurve::closepath() but sets the end point of the current
802     command to the subpath start point instead of adding a new lineto.
804     Used for freehand drawing when the user draws back to the start point.
805   
806   2GEOMified
807 **/
808 void
809 SPCurve::closepath_current()
811     g_return_if_fail(this != NULL);
812     g_return_if_fail(_hascpt);
813     g_return_if_fail(!_posSet);
814     g_return_if_fail(!_closed);
815     /* We need at least moveto, curveto, end. */
816     g_return_if_fail(_end - _substart > 1);
818     {
819         NArtBpath *bs = _bpath + _substart;
820         NArtBpath *be = _bpath + _end - 1;
822         be->x3 = bs->x3;
823         be->y3 = bs->y3;
825         bs->code = NR_MOVETO;
826     }
827     // Inkscape always manually adds the closing line segment to SPCurve with a lineto.
828     // This lineto is removed in the writing function for NArtBpath, 
829     // so when path is closed and the last segment is a lineto, the closing line segment must really be removed first!
830     // TODO: fix behavior in Inkscape!
831     if ( /*Geom::LineSegment const *line_segment = */ dynamic_cast<Geom::LineSegment const  *>(&_pathv.back().back())) {
832         _pathv.back().erase_last();
833     }
834     _pathv.back().close(true);
835     _closed = true;
837     for (Geom::PathVector::const_iterator it = _pathv.begin(); it != _pathv.end(); it++) {
838          if ( ! it->closed() ) {
839             _closed = false;
840             break;
841         }
842     }
844     for (NArtBpath const *bp = _bpath; bp->code != NR_END; bp++) {
845         /** \todo
846          * effic: Maintain a count of NR_MOVETO_OPEN's (e.g. instead of
847          * the closed boolean).
848          */
849         if (bp->code == NR_MOVETO_OPEN) {
850             _closed = false;
851             break;
852         }
853     }
855     _hascpt = false;
856     _moving = false;
858     debug_check("SPCurve::closepath_current", this);
861 /**
862  * True if no paths are in curve.
863  * 2GEOMproof
864  */
865 bool
866 SPCurve::is_empty() const
868     g_return_val_if_fail(this != NULL, TRUE);
870     if (!_bpath)
871         return true;
873     bool empty = _pathv.empty() || _pathv.front().empty();
874     debug_check("SPCurve::is_empty", (_bpath->code == NR_END)  ==  empty );
876     return empty;
879 /**
880  * True iff all subpaths are closed.
881  * 2GEOMproof
882  */
883 bool
884 SPCurve::is_closed() const
886     bool closed = true;
887     for (Geom::PathVector::const_iterator it = _pathv.begin(); it != _pathv.end(); it++) {
888          if ( ! it->closed() ) {
889             closed = false;
890             break;
891         }
892     }
893     debug_check("SPCurve::is_closed", (closed)  ==  (_closed) );
895     return closed;
898 /**
899  * Return last subpath or NULL.
900  */
901 NArtBpath const *
902 SPCurve::last_bpath() const
904     g_return_val_if_fail(this != NULL, NULL);
906     if (_end == 0) {
907         return NULL;
908     }
910     return _bpath + _end - 1;
913 /**
914  * Return last pathsegment (possibly the closing path segment) in PathVector or NULL.
915  * equal in functionality to SPCurve::last_bpath()
916  */
917 Geom::Curve const *
918 SPCurve::last_segment() const
920     if (is_empty()) {
921         return NULL;
922     }
923     if (_pathv.back().empty()) {
924         return NULL;
925     }
927     return &_pathv.back().back_default();
930 /**
931  * Return last path in PathVector or NULL.
932  */
933 Geom::Path const *
934 SPCurve::last_path() const
936     g_return_val_if_fail(this != NULL, NULL);
938     if (is_empty()) {
939         return NULL;
940     }
942     return &_pathv.back();
945 /**
946  * Return first pathsegment in PathVector or NULL.
947  * equal in functionality to SPCurve::first_bpath()
948  */
949 Geom::Curve const *
950 SPCurve::first_segment() const
952     if (is_empty()) {
953         return NULL;
954     }
955     if (_pathv.front().empty()) {
956         return NULL;
957     }
959     return &_pathv.front().front();
962 /**
963  * Return first path in PathVector or NULL.
964  */
965 Geom::Path const *
966 SPCurve::first_path() const
968     g_return_val_if_fail(this != NULL, NULL);
970     if (is_empty()) {
971         return NULL;
972     }
974     return &_pathv.front();
977 /**
978  * Return first point of first subpath or (0,0).  TODO: shouldn't this be (NR_HUGE, NR_HUGE) to be able to tell it apart from normal (0,0) ?
979  */
980 NR::Point
981 SPCurve::first_point() const
983     NArtBpath const * bpath = get_bpath();
984     g_return_val_if_fail(bpath != NULL, NR::Point(0, 0));
985     if (is_empty())
986         return NR::Point(0, 0);
988     debug_check("SPCurve::first_point", bpath->c(3) == _pathv.front().initialPoint() );
990     //return bpath->c(3);
991     return from_2geom( _pathv.front().initialPoint() );
994 /**
995  * Return the second point of first subpath or _movePos if curve too short.
996  */
997 NR::Point
998 SPCurve::second_point() const
1000     g_return_val_if_fail(this != NULL, NR::Point(0, 0));
1002     if (_end < 1) {
1003         return _movePos;
1004     }
1006     NArtBpath *bpath = NULL;
1007     if (_end < 2) {
1008         bpath = _bpath;
1009     } else {
1010         bpath = _bpath + 1;
1011     }
1012     g_return_val_if_fail(bpath != NULL, NR::Point(0, 0));
1014     debug_check("SPCurve::second_point", bpath->c(3) == _pathv.front()[0].finalPoint() );
1016     return bpath->c(3);
1019 /**
1020  * Return the second-last point of last subpath or _movePos if curve too short.
1021  */
1022 NR::Point
1023 SPCurve::penultimate_point() const
1025     g_return_val_if_fail(this != NULL, NR::Point(0, 0));
1027     if (_end < 2) {
1028         return _movePos;
1029     }
1031     NArtBpath *const bpath = _bpath + _end - 2;
1032     g_return_val_if_fail(bpath != NULL, NR::Point(0, 0));
1035     Geom::Curve const& back = _pathv.back().back_default();
1036     Geom::Point p = back.initialPoint();
1038     debug_check("SPCurve::penultimate_point", bpath->c(3) == from_2geom(p) );
1039     return from_2geom(p);
1042 /**
1043  * Return last point of last subpath or (0,0).  TODO: shouldn't this be (NR_HUGE, NR_HUGE) to be able to tell it apart from normal (0,0) ?
1044  */
1045 NR::Point
1046 SPCurve::last_point() const
1048     NArtBpath const * bpath = last_bpath();
1049     g_return_val_if_fail(bpath != NULL, NR::Point(0, 0));
1050     if (is_empty())
1051         return NR::Point(0, 0);
1053     debug_check("SPCurve::last_point", bpath->c(3) == _pathv.back().finalPoint() );
1054     //return bpath->c(3);
1055     return from_2geom( _pathv.back().finalPoint() );
1058 inline static bool
1059 is_moveto(NRPathcode const c)
1061     return c == NR_MOVETO || c == NR_MOVETO_OPEN;
1064 /**
1065  * Returns a *new* \a curve but drawn in the opposite direction.
1066  * Should result in the same shape, but
1067  * with all its markers drawn facing the other direction.
1068  * Reverses the order of subpaths as well
1069  * 2GEOMified
1070  **/
1071 SPCurve *
1072 SPCurve::create_reverse() const
1074     /* We need at least moveto, curveto, end. */
1075     g_return_val_if_fail(_end - _substart > 1, NULL);
1077     NArtBpath const *be = _bpath + _end - 1;
1079     g_assert(is_moveto(_bpath[_substart].code));
1080     g_assert(is_moveto(_bpath[0].code));
1081     g_assert((be+1)->code == NR_END);
1083     SPCurve  *new_curve = new SPCurve(_length);
1084     new_curve->moveto(be->c(3));
1086     for (NArtBpath const *bp = be; ; --bp) {
1087         switch (bp->code) {
1088             case NR_MOVETO:
1089                 g_assert(new_curve->_bpath[new_curve->_substart].code == NR_MOVETO_OPEN);
1090                 new_curve->_bpath[new_curve->_substart].code = NR_MOVETO;
1091                 /* FALL-THROUGH */
1092             case NR_MOVETO_OPEN:
1093                 if (bp == _bpath) {
1094                     return new_curve;
1095                 }
1096                 new_curve->moveto((bp-1)->c(3));
1097                 break;
1099             case NR_LINETO:
1100                 new_curve->lineto((bp-1)->c(3));
1101                 break;
1103             case NR_CURVETO:
1104                 new_curve->curveto(bp->c(2), bp->c(1), (bp-1)->c(3));
1105                 break;
1107             default:
1108                 g_assert_not_reached();
1109         }
1110     }
1112     new_curve->_pathv = Geom::reverse_paths_and_order(_pathv);
1114     debug_check("SPCurve::create_reverse", new_curve);
1117 /**
1118  * Append \a curve2 to \a this.
1119  * If \a use_lineto is false, simply add all paths in \a curve2 to \a this;
1120  * if \a use_lineto is true, combine \a this's last path and \a curve2's first path and add the rest of the paths in \a curve2 to \a this.
1121  * 2GEOMified
1122  */
1123 void
1124 SPCurve::append(SPCurve const *curve2,
1125                 bool use_lineto)
1127     g_return_if_fail(this != NULL);
1128     g_return_if_fail(curve2 != NULL);
1130     if (curve2->is_empty())
1131         return;
1132     if (curve2->_end < 1)
1133         return;
1135     NArtBpath const *bs = curve2->_bpath;
1137     bool closed = this->_closed;
1139     for (NArtBpath const *bp = bs; bp->code != NR_END; bp++) {
1140         switch (bp->code) {
1141             case NR_MOVETO_OPEN:
1142                 if (use_lineto && _hascpt) {
1143                     lineto(bp->x3, bp->y3);
1144                     use_lineto = false;
1145                 } else {
1146                     if (closed && _hascpt) closepath();
1147                     moveto(bp->x3, bp->y3);
1148                 }
1149                 closed = false;
1150                 break;
1152             case NR_MOVETO:
1153                 if (use_lineto && _hascpt) {
1154                     lineto(bp->x3, bp->y3);
1155                     use_lineto = FALSE;
1156                 } else {
1157                     if (closed && _hascpt) closepath();
1158                     moveto(bp->x3, bp->y3);
1159                 }
1160                 closed = true;
1161                 break;
1163             case NR_LINETO:
1164                 lineto(bp->x3, bp->y3);
1165                 break;
1167             case NR_CURVETO:
1168                 curveto(bp->x1, bp->y1, bp->x2, bp->y2, bp->x3, bp->y3);
1169                 break;
1171             case NR_END:
1172                 g_assert_not_reached();
1173         }
1174     }
1176     if (closed) {
1177         closepath();
1178     }
1180     debug_check("SPCurve::append", this);
1182     /* 2GEOM code when code above is removed:
1183     if (use_lineto) {
1184         Geom::PathVector::const_iterator it = curve2->_pathv.begin();
1185         if ( ! _pathv.empty() ) {
1186             Geom::Path & lastpath = _pathv.back();
1187             lastpath.appendNew<Geom::LineSegment>( (*it).initialPoint() );
1188             lastpath.append( (*it) );
1189         } else {
1190             _pathv.push_back( (*it) );
1191         }
1193         for (it++; it != curve2->_pathv.end(); it++) {
1194             _pathv.push_back( (*it) );
1195         }
1196     } else {
1197         for (Geom::PathVector::const_iterator it = curve2->_pathv.begin(); it != curve2->_pathv.end(); it++) {
1198             _pathv.push_back( (*it) );
1199         }
1200     }
1201     */
1204 /**
1205  * Append \a c1 to \a this with possible fusing of close endpoints.
1206  * 2GEOMproof. Needs to be recoded when NArtBpath is no longer there. Right now, it applies the same changes to bpath and pathv depending on bpath
1207  */
1208 SPCurve *
1209 SPCurve::append_continuous(SPCurve const *c1, gdouble tolerance)
1211     g_return_val_if_fail(this != NULL, NULL);
1212     g_return_val_if_fail(c1 != NULL, NULL);
1213     g_return_val_if_fail(!_closed, NULL);
1214     g_return_val_if_fail(!c1->_closed, NULL);
1216     if (c1->_end < 1) {
1217         return this;
1218     }
1220     debug_check("SPCurve::append_continuous 11", this);
1222     NArtBpath const *be = last_bpath();
1223     if (be) {
1224         NArtBpath const *bs = c1->get_bpath();
1225         if ( bs
1226              && ( fabs( bs->x3 - be->x3 ) <= tolerance )
1227              && ( fabs( bs->y3 - be->y3 ) <= tolerance ) )
1228         {
1229             /** \todo
1230              * fixme: Strictly we mess in case of multisegment mixed
1231              * open/close curves
1232              */
1233             bool closed = false;
1234             for (bs = bs + 1; bs->code != NR_END; bs++) {
1235                 switch (bs->code) {
1236                     case NR_MOVETO_OPEN:
1237                         if (closed) closepath();
1238                         moveto(bs->x3, bs->y3);
1239                         closed = false;
1240                         break;
1241                     case NR_MOVETO:
1242                         if (closed) closepath();
1243                         moveto(bs->x3, bs->y3);
1244                         closed = true;
1245                         break;
1246                     case NR_LINETO:
1247                         lineto(bs->x3, bs->y3);
1248                         break;
1249                     case NR_CURVETO:
1250                         curveto(bs->x1, bs->y1, bs->x2, bs->y2, bs->x3, bs->y3);
1251                         break;
1252                     case NR_END:
1253                         g_assert_not_reached();
1254                 }
1255             }
1256         } else {
1257             append(c1, TRUE);
1258         }
1259     } else {
1260         append(c1, TRUE);
1261     }
1263     debug_check("SPCurve::append_continuous", this);
1265     return this;
1268 /**
1269  * Remove last segment of curve.
1270  * (Only used once in /src/pen-context.cpp)
1271  * 2GEOMified
1272  */
1273 void
1274 SPCurve::backspace()
1276     g_return_if_fail(this != NULL);
1278     if ( is_empty() )
1279         return;
1281     if (_end > 0) {
1282         _end -= 1;
1283         if (_end > 0) {
1284             NArtBpath *bp = _bpath + _end - 1;
1285             if ((bp->code == NR_MOVETO)     ||
1286                 (bp->code == NR_MOVETO_OPEN)  )
1287             {
1288                 _hascpt = true;
1289                 _posSet = true;
1290                 _closed = false;
1291                 _movePos = bp->c(3);
1292                 _end -= 1;
1293             }
1294         }
1295         _bpath[_end].code = NR_END;
1296     }
1298     if ( !_pathv.back().empty() ) {
1299         _pathv.back().erase_last();
1300         _pathv.back().close(false);
1301     }
1303     debug_check("SPCurve::backspace", this);
1306 /* Private methods */
1308 /**
1309  * Returns index of first NR_END bpath in array.
1310  */
1311 static unsigned sp_bpath_length(NArtBpath const bpath[])
1313     g_return_val_if_fail(bpath != NULL, FALSE);
1315     unsigned ret = 0;
1316     while ( bpath[ret].code != NR_END ) {
1317         ++ret;
1318     }
1319     ++ret;
1321     return ret;
1324 /**
1325  * \brief
1326  *
1327  * \todo
1328  * fixme: this is bogus -- it doesn't check for nr_moveto, which will indicate
1329  * a closing of the subpath it's nonsense to talk about a path as a whole
1330  * being closed, although maybe someone would want that for some other reason?
1331  * Oh, also, if the bpath just ends, then it's *open*.  I hope nobody is using
1332  * this code for anything.
1333  */
1334 static bool sp_bpath_closed(NArtBpath const bpath[])
1336     g_return_val_if_fail(bpath != NULL, FALSE);
1338     for (NArtBpath const *bp = bpath; bp->code != NR_END; bp++) {
1339         if (bp->code == NR_MOVETO_OPEN) {
1340             return false;
1341         }
1342     }
1344     return true;
1347 /**
1348  * Returns length of bezier segment.
1349  */
1350 static double
1351 bezier_len(NR::Point const &c0,
1352            NR::Point const &c1,
1353            NR::Point const &c2,
1354            NR::Point const &c3,
1355            double const threshold)
1357     /** \todo
1358      * The SVG spec claims that a closed form exists, but for the moment I'll
1359      * use a stupid algorithm.
1360      */
1361     double const lbound = L2( c3 - c0 );
1362     double const ubound = L2( c1 - c0 ) + L2( c2 - c1 ) + L2( c3 - c2 );
1363     double ret;
1364     if ( ubound - lbound <= threshold ) {
1365         ret = .5 * ( lbound + ubound );
1366     } else {
1367         NR::Point const a1( .5 * ( c0 + c1 ) );
1368         NR::Point const b2( .5 * ( c2 + c3 ) );
1369         NR::Point const c12( .5 * ( c1 + c2 ) );
1370         NR::Point const a2( .5 * ( a1 + c12 ) );
1371         NR::Point const b1( .5 * ( c12 + b2 ) );
1372         NR::Point const midpoint( .5 * ( a2 + b1 ) );
1373         double const rec_threshold = .625 * threshold;
1374         ret = bezier_len(c0, a1, a2, midpoint, rec_threshold) + bezier_len(midpoint, b1, b2, c3, rec_threshold);
1375         if (!(lbound - 1e-2 <= ret && ret <= ubound + 1e-2)) {
1376             using NR::X; using NR::Y;
1377             g_warning("ret=%f outside of expected bounds [%f, %f] for {(%.0f %.0f) (%.0f %.0f) (%.0f %.0f) (%.0f %.0f)}",
1378                       ret, lbound, ubound, c0[X], c0[Y], c1[X], c1[Y], c2[X], c2[Y], c3[X], c3[Y]);
1379         }
1380     }
1381     return ret;
1384 /**
1385  * Returns total length of curve, excluding length of closepath segments.
1386  */
1387 double
1388 sp_curve_distance_including_space(SPCurve const *const curve, double seg2len[])
1390     g_return_val_if_fail(curve != NULL, 0.);
1392     double ret = 0.0;
1394     if ( curve->_bpath->code == NR_END ) {
1395         return ret;
1396     }
1398     NR::Point prev(curve->_bpath->c(3));
1399     for (guint i = 1; i < curve->_end; ++i) {
1400         NArtBpath &p = curve->_bpath[i];
1401         double seg_len = 0;
1402         switch (p.code) {
1403             case NR_MOVETO_OPEN:
1404             case NR_MOVETO:
1405             case NR_LINETO:
1406                 seg_len = L2(p.c(3) - prev);
1407                 break;
1409             case NR_CURVETO:
1410                 seg_len = bezier_len(prev, p.c(1), p.c(2), p.c(3), 1.);
1411                 break;
1413             case NR_END:
1414                 return ret;
1415         }
1416         seg2len[i - 1] = seg_len;
1417         ret += seg_len;
1418         prev = p.c(3);
1419     }
1420     g_assert(!(ret < 0));
1421     return ret;
1424 /**
1425  * Like sp_curve_distance_including_space(), but ensures that the
1426  * result >= 1e-18:  uses 1 per segment if necessary.
1427  */
1428 double
1429 sp_curve_nonzero_distance_including_space(SPCurve const *const curve, double seg2len[])
1431     double const real_dist(sp_curve_distance_including_space(curve, seg2len));
1432     if (real_dist >= 1e-18) {
1433         return real_dist;
1434     } else {
1435         unsigned const nSegs = SP_CURVE_LENGTH(curve) - 1;
1436         for (unsigned i = 0; i < nSegs; ++i) {
1437             seg2len[i] = 1.;
1438         }
1439         return (double) nSegs;
1440     }
1443 /**
1444  * 2GEOMified
1445  */
1446 void
1447 SPCurve::stretch_endpoints(NR::Point const &new_p0, NR::Point const &new_p1)
1449     if (is_empty()) {
1450         return;
1451     }
1452     g_assert(unsigned(SP_CURVE_LENGTH(this)) + 1 == sp_bpath_length(_bpath));
1453     unsigned const nSegs = SP_CURVE_LENGTH(this) - 1;
1454     g_assert(nSegs != 0);
1455     double *const seg2len = new double[nSegs];
1456     double const tot_len = sp_curve_nonzero_distance_including_space(this, seg2len);
1457     NR::Point const offset0( new_p0 - first_point() );
1458     NR::Point const offset1( new_p1 - last_point() );
1459     _bpath->setC(3, new_p0);
1460     double begin_dist = 0.;
1461     for (unsigned si = 0; si < nSegs; ++si) {
1462         double const end_dist = begin_dist + seg2len[si];
1463         NArtBpath &p = _bpath[1 + si];
1464         switch (p.code) {
1465             case NR_LINETO:
1466             case NR_MOVETO:
1467             case NR_MOVETO_OPEN:
1468                 p.setC(3, p.c(3) + NR::Lerp(end_dist / tot_len, offset0, offset1));
1469                 break;
1471             case NR_CURVETO:
1472                 for (unsigned ci = 1; ci <= 3; ++ci) {
1473                     p.setC(ci, p.c(ci) + Lerp((begin_dist + ci * seg2len[si] / 3.) / tot_len, offset0, offset1));
1474                 }
1475                 break;
1477             default:
1478                 g_assert_not_reached();
1479         }
1481         begin_dist = end_dist;
1482     }
1483     g_assert(L1(_bpath[nSegs].c(3) - new_p1) < 1.);
1484     /* Explicit set for better numerical properties. */
1485     _bpath[nSegs].setC(3, new_p1);
1486     delete [] seg2len;
1488     Geom::Piecewise<Geom::D2<Geom::SBasis> > pwd2 = _pathv.front().toPwSb();
1489     Geom::Piecewise<Geom::SBasis> arclength = Geom::arcLengthSb(pwd2);
1490     if ( arclength.lastValue() <= 0 ) {
1491         g_error("SPCurve::stretch_endpoints - arclength <= 0");
1492         throw;
1493     }
1494     arclength *= 1./arclength.lastValue();
1495     Geom::Point const A( to_2geom(offset0) );
1496     Geom::Point const B( to_2geom(offset1) );
1497     Geom::Piecewise<Geom::SBasis> offsetx = (arclength*-1.+1)*A[0] + arclength*B[0];
1498     Geom::Piecewise<Geom::SBasis> offsety = (arclength*-1.+1)*A[1] + arclength*B[1];
1499     Geom::Piecewise<Geom::D2<Geom::SBasis> > offsetpath = Geom::sectionize( Geom::D2<Geom::Piecewise<Geom::SBasis> >(offsetx, offsety) );
1500     pwd2 += offsetpath;
1501     _pathv = Geom::path_from_piecewise( pwd2, 0.001 );
1503     debug_check("SPCurve::stretch_endpoints", this);
1506 /**
1507  *  sets start of first path to new_p0, and end of first path to  new_p1
1508  * 2GEOMified
1509  */
1510 void
1511 SPCurve::move_endpoints(NR::Point const &new_p0, NR::Point const &new_p1)
1513     if (is_empty()) {
1514         return;
1515     }
1516     unsigned const nSegs = SP_CURVE_LENGTH(this) - 1;
1517     g_assert(nSegs != 0);
1519     _bpath->setC(3, new_p0);
1520     _bpath[nSegs].setC(3, new_p1);
1522     _pathv.front().setInitial(to_2geom(new_p0));
1523     _pathv.front().setFinal(to_2geom(new_p1));
1525     debug_check("SPCurve::move_endpoints", this);
1528 /**
1529  * returns the number of nodes in a path, used for statusbar text when selecting an spcurve.
1530  * 2GEOMified
1531  */
1532 guint
1533 SPCurve::nodes_in_path() const
1535     gint r = _end;
1536     gint i = _length - 1;
1537     if (i > r) i = r; // sometimes after switching from node editor length is wrong, e.g. f6 - draw - f2 - tab - f1, this fixes it
1538     for (; i >= 0; i --)
1539         if (_bpath[i].code == NR_MOVETO)
1540             r --;
1542     guint nr = 0;
1543     for(Geom::PathVector::const_iterator it = _pathv.begin(); it != _pathv.end(); ++it) {
1544         nr += (*it).size();
1546         nr++; // count last node (this works also for closed paths because although they don't have a 'last node', they do have an extra segment
1547     }
1549     debug_check("SPCurve::nodes_in_path", r == (gint)nr);
1551     return r;
1554 /**
1555  *  Adds p to the last point (and last handle if present) of the last path
1556  * 2GEOMified
1557  */
1558 void
1559 SPCurve::last_point_additive_move(Geom::Point const & p)
1561     if (is_empty()) {
1562         return;
1563     }
1564     if (_end == 0) {
1565         return;
1566     }
1567     NArtBpath * path = _bpath + _end - 1;
1569     if (path->code == NR_CURVETO) {
1570         path->x2 += p[Geom::X];
1571         path->y2 += p[Geom::Y];
1572     }
1573     path->x3 += p[Geom::X];
1574     path->y3 += p[Geom::Y];
1576     _pathv.back().setFinal( _pathv.back().finalPoint() + p );
1578     // Move handle as well when the last segment is a cubic bezier segment:
1579     // TODO: what to do for quadratic beziers?
1580     if ( Geom::CubicBezier const *lastcube = dynamic_cast<Geom::CubicBezier const *>(&_pathv.back().back()) ) {
1581         Geom::CubicBezier newcube( *lastcube );
1582         newcube.setPoint(2, newcube[2] + p);
1583         _pathv.back().replace( --_pathv.back().end(), newcube );
1584     }
1586     debug_check("SPCurve::last_point_additive_move", this);
1589 /*
1590   Local Variables:
1591   mode:c++
1592   c-file-style:"stroustrup"
1593   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
1594   indent-tabs-mode:nil
1595   fill-column:99
1596   End:
1597 */
1598 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :