Code

even with zero opacity, paths must be selectable in outline mode
[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  * Increase _refcount of curve.
288  *
289  * \todo should this be shared with other refcounting code?
290  * 2GEOMproof
291  */
292 SPCurve *
293 SPCurve::ref()
295     g_return_val_if_fail(this != NULL, NULL);
297     _refcount += 1;
299     return this;
302 /**
303  * Decrease refcount of curve, with possible destruction.
304  *
305  * \todo should this be shared with other refcounting code?
306  * 2GEOMproof
307  */
308 SPCurve *
309 SPCurve::unref()
311     g_return_val_if_fail(this != NULL, NULL);
313     _refcount -= 1;
315     if (_refcount < 1) {
316         delete this;
317     }
319     return NULL;
322 /**
323  * Add space for more paths in curve.
324  * This function has no meaning for 2geom representation, other than maybe for optimization issues (enlargening the vector for what is to come)
325  * 2GEOMproof
326  */
327 void
328 SPCurve::ensure_space(guint space)
330     g_return_if_fail(this != NULL);
331     g_return_if_fail(space > 0);
333     if (_end + space < _length)
334         return;
336     if (space < SP_CURVE_LENSTEP)
337         space = SP_CURVE_LENSTEP;
339     _bpath = g_renew(NArtBpath, _bpath, _length + space);
341     _length += space;
344 /**
345  * Create new curve from its own bpath array.
346  * 2GEOMproof
347  */
348 SPCurve *
349 SPCurve::copy() const
351     g_return_val_if_fail(this != NULL, NULL);
353     return SPCurve::new_from_foreign_bpath(_bpath);
356 /**
357  * Return new curve that is the concatenation of all curves in list.
358  * 2GEOMified
359  */
360 SPCurve *
361 SPCurve::concat(GSList const *list)
363     g_return_val_if_fail(list != NULL, NULL);
365     gint length = 0;
367     for (GSList const *l = list; l != NULL; l = l->next) {
368         SPCurve *c = (SPCurve *) l->data;
369         length += c->_end;
370     }
372     SPCurve *new_curve = new SPCurve(length + 1);
374     NArtBpath *bp = new_curve->_bpath;
376     for (GSList const *l = list; l != NULL; l = l->next) {
377         SPCurve *c = (SPCurve *) l->data;
378         memcpy(bp, c->_bpath, c->_end * sizeof(NArtBpath));
379         bp += c->_end;
380     }
382     bp->code = NR_END;
384     new_curve->_end = length;
385     gint i;
386     for (i = new_curve->_end; i > 0; i--) {
387         if ((new_curve->_bpath[i].code == NR_MOVETO)     ||
388             (new_curve->_bpath[i].code == NR_MOVETO_OPEN)  )
389             break;
390     }
392     new_curve->_substart = i;
394     for (GSList const *l = list; l != NULL; l = l->next) {
395         SPCurve *c = (SPCurve *) l->data;
396         new_curve->_pathv.insert( new_curve->_pathv.end(), c->get_pathvector().begin(), c->get_pathvector().end() );
397     }
399     debug_check("SPCurve::concat", new_curve);
401     return new_curve;
404 /**
405  * Returns a list of new curves corresponding to the subpaths in \a curve.
406  * 2geomified
407  */
408 GSList *
409 SPCurve::split() const
411     g_return_val_if_fail(this != NULL, NULL);
413     guint p = 0;
414     GSList *l = NULL;
416     gint pathnr = 0;
417     while (p < _end) {
418         gint i = 1;
419         while ((_bpath[p + i].code == NR_LINETO) ||
420                (_bpath[p + i].code == NR_CURVETO))
421             i++;
422         SPCurve *new_curve = new SPCurve(i + 1);
423         memcpy(new_curve->_bpath, _bpath + p, i * sizeof(NArtBpath));
424         new_curve->_end = i;
425         new_curve->_bpath[i].code = NR_END;
426         new_curve->_substart = 0;
427         new_curve->_closed = (new_curve->_bpath->code == NR_MOVETO);
428         new_curve->_hascpt = (new_curve->_bpath->code == NR_MOVETO_OPEN);
429         new_curve->_pathv = Geom::PathVector(1, _pathv[pathnr]);
430         l = g_slist_prepend(l, new_curve);
431         p += i;
432         pathnr++;
433     }
435     return l;
438 /**
439  * Transform all paths in curve, template helper.
440  */
441 template<class M>
442 static void
443 tmpl_curve_transform(SPCurve * curve, M const &m)
445     g_return_if_fail(curve != NULL);
447     for (guint i = 0; i < curve->_end; i++) {
448         NArtBpath *p = curve->_bpath + i;
449         switch (p->code) {
450             case NR_MOVETO:
451             case NR_MOVETO_OPEN:
452             case NR_LINETO: {
453                 p->setC(3, p->c(3) * m);
454                 break;
455             }
456             case NR_CURVETO:
457                 for (unsigned i = 1; i <= 3; ++i) {
458                     p->setC(i, p->c(i) * m);
459                 }
460                 break;
461             default:
462                 g_warning("Illegal pathcode %d", p->code);
463                 break;
464         }
465     }
468 /**
469  * Transform all paths in curve using matrix.
470  * 2GEOMified, can be deleted when completely 2geom
471  */
472 void
473 SPCurve::transform(NR::Matrix const &m)
475     tmpl_curve_transform<NR::Matrix>(this, m);
477     _pathv = _pathv * to_2geom(m);
479     debug_check("SPCurve::transform(NR::Matrix const &m)", this);
482 /**
483  * Transform all paths in curve using matrix.
484  */
485 void
486 SPCurve::transform(Geom::Matrix const &m)
488     tmpl_curve_transform<NR::Matrix>(this, from_2geom(m));
490     _pathv = _pathv * m;
492     debug_check("SPCurve::transform(Geom::Matrix const &m)", this);
495 /**
496  * Transform all paths in curve using NR::translate.
497  * 2GEOMified, can be deleted when completely 2geom
498  */
499 void
500 SPCurve::transform(NR::translate const &m)
502     tmpl_curve_transform<NR::translate>(this, m);
504     _pathv = _pathv * to_2geom(m);
506     debug_check("SPCurve::transform(NR::translate const &m)", this);
509 /**
510  * Set curve to empty curve.
511  * 2GEOMified
512  */
513 void
514 SPCurve::reset()
516     g_return_if_fail(this != NULL);
518     _bpath->code = NR_END;
519     _end = 0;
520     _substart = 0;
521     _hascpt = false;
522     _posSet = false;
523     _moving = false;
524     _closed = false;
526     _pathv.clear();
528     debug_check("SPCurve::reset", this);
531 /* Several consecutive movetos are ALLOWED */
533 /**
534  * Calls SPCurve::moveto() with point made of given coordinates.
535  */
536 void
537 SPCurve::moveto(gdouble x, gdouble y)
539     moveto(NR::Point(x, y));
541 /**
542  * Calls SPCurve::moveto() with point made of given coordinates.
543  */
544 void
545 SPCurve::moveto(Geom::Point const &p)
547     moveto(from_2geom(p));
549 /**
550  * Perform a moveto to a point, thus starting a new subpath.
551  * 2GEOMified
552  */
553 void
554 SPCurve::moveto(NR::Point const &p)
556     g_return_if_fail(this != NULL);
557     g_return_if_fail(!_moving);
559     _substart = _end;
560     _hascpt = true;
561     _posSet = true;
562     _movePos = p;
563     _pathv.push_back( Geom::Path() );  // for some reason Geom::Path(p) does not work...
564     _pathv.back().start(to_2geom(p));
566     // the output is not the same. This is because SPCurve *incorrectly* coaslesces multiple moveto's into one for NArtBpath.
567 //    debug_check("SPCurve::moveto", this);
570 /**
571  * Calls SPCurve::lineto() with a point's coordinates.
572  */
573 void
574 SPCurve::lineto(Geom::Point const &p)
576     lineto(p[Geom::X], p[Geom::Y]);
578 /**
579  * Calls SPCurve::lineto() with a point's coordinates.
580  */
581 void
582 SPCurve::lineto(NR::Point const &p)
584     lineto(p[NR::X], p[NR::Y]);
586 /**
587  * Adds a line to the current subpath.
588  * 2GEOMified
589  */
590 void
591 SPCurve::lineto(gdouble x, gdouble y)
593     g_return_if_fail(this != NULL);
594     g_return_if_fail(_hascpt);
596     if (_moving) {
597         /* fix endpoint */
598         g_return_if_fail(!_posSet);
599         g_return_if_fail(_end > 1);
600         NArtBpath *bp = _bpath + _end - 1;
601         g_return_if_fail(bp->code == NR_LINETO);
602         bp->x3 = x;
603         bp->y3 = y;
604         _moving = false;
606         Geom::Path::iterator it = _pathv.back().end();
607         if ( Geom::LineSegment const *last_line_segment = dynamic_cast<Geom::LineSegment const *>( &(*it) )) {
608             Geom::LineSegment new_seg( *last_line_segment );
609             new_seg.setFinal( Geom::Point(x,y) );
610             _pathv.back().replace(it, new_seg);
611         }
612     } else if (_posSet) {
613         /* start a new segment */
614         ensure_space(2);
615         NArtBpath *bp = _bpath + _end;
616         bp->code = NR_MOVETO_OPEN;
617         bp->setC(3, _movePos);
618         bp++;
619         bp->code = NR_LINETO;
620         bp->x3 = x;
621         bp->y3 = y;
622         bp++;
623         bp->code = NR_END;
624         _end += 2;
625         _posSet = false;
626         _closed = false;
628         _pathv.back().appendNew<Geom::LineSegment>( Geom::Point(x,y) );
629         return;
630     } else {
631         /* add line */
633         g_return_if_fail(_end > 1);
634         ensure_space(1);
635         NArtBpath *bp = _bpath + _end;
636         bp->code = NR_LINETO;
637         bp->x3 = x;
638         bp->y3 = y;
639         bp++;
640         bp->code = NR_END;
641         _end++;
642         _pathv.back().appendNew<Geom::LineSegment>( Geom::Point(x,y) );
643     }
645     debug_check("SPCurve::lineto", this);
648 /**
649  * Calls SPCurve::curveto() with coordinates of three points.
650  */
651 void
652 SPCurve::curveto(Geom::Point const &p0, Geom::Point const &p1, Geom::Point const &p2)
654     using Geom::X;
655     using Geom::Y;
656     curveto( p0[X], p0[Y],
657              p1[X], p1[Y],
658              p2[X], p2[Y] );
660 /**
661  * Calls SPCurve::curveto() with coordinates of three points.
662  */
663 void
664 SPCurve::curveto(NR::Point const &p0, NR::Point const &p1, NR::Point const &p2)
666     using NR::X;
667     using NR::Y;
668     curveto( p0[X], p0[Y],
669              p1[X], p1[Y],
670              p2[X], p2[Y] );
672 /**
673  * Adds a bezier segment to the current subpath.
674  * 2GEOMified
675  */
676 void
677 SPCurve::curveto(gdouble x0, gdouble y0, gdouble x1, gdouble y1, gdouble x2, gdouble y2)
679     g_return_if_fail(this != NULL);
680     g_return_if_fail(_hascpt);
681     g_return_if_fail(!_moving);
683     if (_posSet) {
684         /* start a new segment */
685         ensure_space(2);
686         NArtBpath *bp = _bpath + _end;
687         bp->code = NR_MOVETO_OPEN;
688         bp->setC(3, _movePos);
689         bp++;
690         bp->code = NR_CURVETO;
691         bp->x1 = x0;
692         bp->y1 = y0;
693         bp->x2 = x1;
694         bp->y2 = y1;
695         bp->x3 = x2;
696         bp->y3 = y2;
697         bp++;
698         bp->code = NR_END;
699         _end += 2;
700         _posSet = false;
701         _closed = false;
702         _pathv.back().appendNew<Geom::CubicBezier>( Geom::Point(x0,y0), Geom::Point(x1,y1), Geom::Point(x2,y2) );
703     } else {
704         /* add curve */
706         g_return_if_fail(_end > 1);
707         ensure_space(1);
708         NArtBpath *bp = _bpath + _end;
709         bp->code = NR_CURVETO;
710         bp->x1 = x0;
711         bp->y1 = y0;
712         bp->x2 = x1;
713         bp->y2 = y1;
714         bp->x3 = x2;
715         bp->y3 = y2;
716         bp++;
717         bp->code = NR_END;
718         _end++;
719         if (_pathv.empty())  g_message("leeg");
720         else _pathv.back().appendNew<Geom::CubicBezier>( Geom::Point(x0,y0), Geom::Point(x1,y1), Geom::Point(x2,y2) );
721     }
723     debug_check("SPCurve::curveto", this);
726 /**
727  * Close current subpath by possibly adding a line between start and end.
728   * 2GEOMified
729  */
730 void
731 SPCurve::closepath()
733     g_return_if_fail(this != NULL);
734     g_return_if_fail(_hascpt);
735     g_return_if_fail(!_posSet);
736     g_return_if_fail(!_moving);
737     g_return_if_fail(!_closed);
738     /* We need at least moveto, curveto, end. */
739     g_return_if_fail(_end - _substart > 1);
741     {
742         NArtBpath *bs = _bpath + _substart;
743         NArtBpath *be = _bpath + _end - 1;
745         if (bs->c(3) != be->c(3)) {
746             lineto(bs->c(3));
747             bs = _bpath + _substart;
748         }
750         bs->code = NR_MOVETO;
751     }
752     // Inkscape always manually adds the closing line segment to SPCurve with a lineto.
753     // This lineto is removed in the writing function for NArtBpath, 
754     // so when path is closed and the last segment is a lineto, the closing line segment must really be removed first!
755     // TODO: fix behavior in Inkscape!
756     if ( /*Geom::LineSegment const *line_segment = */ dynamic_cast<Geom::LineSegment const  *>(&_pathv.back().back())) {
757         _pathv.back().erase_last();
758     }
759     _pathv.back().close(true);
760     _closed = true;
762     for (Geom::PathVector::const_iterator it = _pathv.begin(); it != _pathv.end(); it++) {
763          if ( ! it->closed() ) {
764             _closed = false;
765             break;
766         }
767     }
769     for (NArtBpath const *bp = _bpath; bp->code != NR_END; bp++) {
770         /** \todo
771          * effic: Maintain a count of NR_MOVETO_OPEN's (e.g. instead of
772          * the closed boolean).
773          */
774         if (bp->code == NR_MOVETO_OPEN) {
775             _closed = false;
776             break;
777         }
778     }
780     _hascpt = false;
782     debug_check("SPCurve::closepath", this);
785 /** Like SPCurve::closepath() but sets the end point of the current
786     command to the subpath start point instead of adding a new lineto.
788     Used for freehand drawing when the user draws back to the start point.
789   
790   2GEOMified
791 **/
792 void
793 SPCurve::closepath_current()
795     g_return_if_fail(this != NULL);
796     g_return_if_fail(_hascpt);
797     g_return_if_fail(!_posSet);
798     g_return_if_fail(!_closed);
799     /* We need at least moveto, curveto, end. */
800     g_return_if_fail(_end - _substart > 1);
802     {
803         NArtBpath *bs = _bpath + _substart;
804         NArtBpath *be = _bpath + _end - 1;
806         be->x3 = bs->x3;
807         be->y3 = bs->y3;
809         bs->code = NR_MOVETO;
810     }
811     // Inkscape always manually adds the closing line segment to SPCurve with a lineto.
812     // This lineto is removed in the writing function for NArtBpath, 
813     // so when path is closed and the last segment is a lineto, the closing line segment must really be removed first!
814     // TODO: fix behavior in Inkscape!
815     if ( /*Geom::LineSegment const *line_segment = */ dynamic_cast<Geom::LineSegment const  *>(&_pathv.back().back())) {
816         _pathv.back().erase_last();
817     }
818     _pathv.back().close(true);
819     _closed = true;
821     for (Geom::PathVector::const_iterator it = _pathv.begin(); it != _pathv.end(); it++) {
822          if ( ! it->closed() ) {
823             _closed = false;
824             break;
825         }
826     }
828     for (NArtBpath const *bp = _bpath; bp->code != NR_END; bp++) {
829         /** \todo
830          * effic: Maintain a count of NR_MOVETO_OPEN's (e.g. instead of
831          * the closed boolean).
832          */
833         if (bp->code == NR_MOVETO_OPEN) {
834             _closed = false;
835             break;
836         }
837     }
839     _hascpt = false;
840     _moving = false;
842     debug_check("SPCurve::closepath_current", this);
845 /**
846  * True if no paths are in curve.
847  * 2GEOMproof
848  */
849 bool
850 SPCurve::is_empty() const
852     g_return_val_if_fail(this != NULL, TRUE);
854     if (!_bpath)
855         return true;
857     bool empty = _pathv.empty() || _pathv.front().empty();
858     debug_check("SPCurve::is_empty", (_bpath->code == NR_END)  ==  empty );
860     return empty;
863 /**
864  * True iff all subpaths are closed.
865  * 2GEOMproof
866  */
867 bool
868 SPCurve::is_closed() const
870     bool closed = true;
871     for (Geom::PathVector::const_iterator it = _pathv.begin(); it != _pathv.end(); it++) {
872          if ( ! it->closed() ) {
873             closed = false;
874             break;
875         }
876     }
877     debug_check("SPCurve::is_closed", (closed)  ==  (_closed) );
879     return closed;
882 /**
883  * Return last subpath or NULL.
884  */
885 NArtBpath const *
886 SPCurve::last_bpath() const
888     g_return_val_if_fail(this != NULL, NULL);
890     if (_end == 0) {
891         return NULL;
892     }
894     return _bpath + _end - 1;
897 /**
898  * Return last pathsegment (possibly the closing path segment) in PathVector or NULL.
899  * equal in functionality to SPCurve::last_bpath()
900  */
901 Geom::Curve const *
902 SPCurve::last_segment() const
904     if (is_empty()) {
905         return NULL;
906     }
907     if (_pathv.back().empty()) {
908         return NULL;
909     }
911     return &_pathv.back().back_default();
914 /**
915  * Return last path in PathVector or NULL.
916  */
917 Geom::Path const *
918 SPCurve::last_path() const
920     g_return_val_if_fail(this != NULL, NULL);
922     if (is_empty()) {
923         return NULL;
924     }
926     return &_pathv.back();
929 /**
930  * Return first pathsegment in PathVector or NULL.
931  * equal in functionality to SPCurve::first_bpath()
932  */
933 Geom::Curve const *
934 SPCurve::first_segment() const
936     if (is_empty()) {
937         return NULL;
938     }
939     if (_pathv.front().empty()) {
940         return NULL;
941     }
943     return &_pathv.front().front();
946 /**
947  * Return first path in PathVector or NULL.
948  */
949 Geom::Path const *
950 SPCurve::first_path() const
952     g_return_val_if_fail(this != NULL, NULL);
954     if (is_empty()) {
955         return NULL;
956     }
958     return &_pathv.front();
961 /**
962  * 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) ?
963  */
964 NR::Point
965 SPCurve::first_point() const
967     NArtBpath const * bpath = get_bpath();
968     g_return_val_if_fail(bpath != NULL, NR::Point(0, 0));
969     if (is_empty())
970         return NR::Point(0, 0);
972     debug_check("SPCurve::first_point", bpath->c(3) == _pathv.front().initialPoint() );
974     //return bpath->c(3);
975     return from_2geom( _pathv.front().initialPoint() );
978 /**
979  * Return the second point of first subpath or _movePos if curve too short.
980  */
981 NR::Point
982 SPCurve::second_point() const
984     g_return_val_if_fail(this != NULL, NR::Point(0, 0));
986     if (_end < 1) {
987         return _movePos;
988     }
990     NArtBpath *bpath = NULL;
991     if (_end < 2) {
992         bpath = _bpath;
993     } else {
994         bpath = _bpath + 1;
995     }
996     g_return_val_if_fail(bpath != NULL, NR::Point(0, 0));
998     debug_check("SPCurve::second_point", bpath->c(3) == _pathv.front()[0].finalPoint() );
1000     return bpath->c(3);
1003 /**
1004  * Return the second-last point of last subpath or _movePos if curve too short.
1005  */
1006 NR::Point
1007 SPCurve::penultimate_point() const
1009     g_return_val_if_fail(this != NULL, NR::Point(0, 0));
1011     if (_end < 2) {
1012         return _movePos;
1013     }
1015     NArtBpath *const bpath = _bpath + _end - 2;
1016     g_return_val_if_fail(bpath != NULL, NR::Point(0, 0));
1017     
1018     Geom::Point p(NR_HUGE, NR_HUGE);
1019     Geom::Curve const& back = _pathv.back().back();
1020     if (_pathv.back().closed()) {
1021         p = back.finalPoint();
1022     } else {
1023         p = back.initialPoint();
1024     }
1026     debug_check("SPCurve::penultimate_point", bpath->c(3) == p );
1027     return bpath->c(3);
1030 /**
1031  * 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) ?
1032  */
1033 NR::Point
1034 SPCurve::last_point() const
1036     NArtBpath const * bpath = last_bpath();
1037     g_return_val_if_fail(bpath != NULL, NR::Point(0, 0));
1038     if (is_empty())
1039         return NR::Point(0, 0);
1041     debug_check("SPCurve::last_point", bpath->c(3) == _pathv.back().finalPoint() );
1042     //return bpath->c(3);
1043     return from_2geom( _pathv.back().finalPoint() );
1046 inline static bool
1047 is_moveto(NRPathcode const c)
1049     return c == NR_MOVETO || c == NR_MOVETO_OPEN;
1052 /**
1053  * Returns a *new* \a curve but drawn in the opposite direction.
1054  * Should result in the same shape, but
1055  * with all its markers drawn facing the other direction.
1056  * Reverses the order of subpaths as well
1057  * 2GEOMified
1058  **/
1059 SPCurve *
1060 SPCurve::create_reverse() const
1062     /* We need at least moveto, curveto, end. */
1063     g_return_val_if_fail(_end - _substart > 1, NULL);
1065     NArtBpath const *be = _bpath + _end - 1;
1067     g_assert(is_moveto(_bpath[_substart].code));
1068     g_assert(is_moveto(_bpath[0].code));
1069     g_assert((be+1)->code == NR_END);
1071     SPCurve  *new_curve = new SPCurve(_length);
1072     new_curve->moveto(be->c(3));
1074     for (NArtBpath const *bp = be; ; --bp) {
1075         switch (bp->code) {
1076             case NR_MOVETO:
1077                 g_assert(new_curve->_bpath[new_curve->_substart].code == NR_MOVETO_OPEN);
1078                 new_curve->_bpath[new_curve->_substart].code = NR_MOVETO;
1079                 /* FALL-THROUGH */
1080             case NR_MOVETO_OPEN:
1081                 if (bp == _bpath) {
1082                     return new_curve;
1083                 }
1084                 new_curve->moveto((bp-1)->c(3));
1085                 break;
1087             case NR_LINETO:
1088                 new_curve->lineto((bp-1)->c(3));
1089                 break;
1091             case NR_CURVETO:
1092                 new_curve->curveto(bp->c(2), bp->c(1), (bp-1)->c(3));
1093                 break;
1095             default:
1096                 g_assert_not_reached();
1097         }
1098     }
1100     new_curve->_pathv = Geom::reverse_paths_and_order(_pathv);
1102     debug_check("SPCurve::create_reverse", new_curve);
1105 /**
1106  * Append \a curve2 to \a this.
1107  * If \a use_lineto is false, simply add all paths in \a curve2 to \a this;
1108  * 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.
1109  * 2GEOMified
1110  */
1111 void
1112 SPCurve::append(SPCurve const *curve2,
1113                 bool use_lineto)
1115     g_return_if_fail(this != NULL);
1116     g_return_if_fail(curve2 != NULL);
1118     if (curve2->is_empty())
1119         return;
1120     if (curve2->_end < 1)
1121         return;
1123     NArtBpath const *bs = curve2->_bpath;
1125     bool closed = this->_closed;
1127     for (NArtBpath const *bp = bs; bp->code != NR_END; bp++) {
1128         switch (bp->code) {
1129             case NR_MOVETO_OPEN:
1130                 if (use_lineto && _hascpt) {
1131                     lineto(bp->x3, bp->y3);
1132                     use_lineto = false;
1133                 } else {
1134                     if (closed && _hascpt) closepath();
1135                     moveto(bp->x3, bp->y3);
1136                 }
1137                 closed = false;
1138                 break;
1140             case NR_MOVETO:
1141                 if (use_lineto && _hascpt) {
1142                     lineto(bp->x3, bp->y3);
1143                     use_lineto = FALSE;
1144                 } else {
1145                     if (closed && _hascpt) closepath();
1146                     moveto(bp->x3, bp->y3);
1147                 }
1148                 closed = true;
1149                 break;
1151             case NR_LINETO:
1152                 lineto(bp->x3, bp->y3);
1153                 break;
1155             case NR_CURVETO:
1156                 curveto(bp->x1, bp->y1, bp->x2, bp->y2, bp->x3, bp->y3);
1157                 break;
1159             case NR_END:
1160                 g_assert_not_reached();
1161         }
1162     }
1164     if (closed) {
1165         closepath();
1166     }
1168     debug_check("SPCurve::append", this);
1170     /* 2GEOM code when code above is removed:
1171     if (use_lineto) {
1172         Geom::PathVector::const_iterator it = curve2->_pathv.begin();
1173         if ( ! _pathv.empty() ) {
1174             Geom::Path & lastpath = _pathv.back();
1175             lastpath.appendNew<Geom::LineSegment>( (*it).initialPoint() );
1176             lastpath.append( (*it) );
1177         } else {
1178             _pathv.push_back( (*it) );
1179         }
1181         for (it++; it != curve2->_pathv.end(); it++) {
1182             _pathv.push_back( (*it) );
1183         }
1184     } else {
1185         for (Geom::PathVector::const_iterator it = curve2->_pathv.begin(); it != curve2->_pathv.end(); it++) {
1186             _pathv.push_back( (*it) );
1187         }
1188     }
1189     */
1192 /**
1193  * Append \a c1 to \a this with possible fusing of close endpoints.
1194  * 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
1195  */
1196 SPCurve *
1197 SPCurve::append_continuous(SPCurve const *c1, gdouble tolerance)
1199     g_return_val_if_fail(this != NULL, NULL);
1200     g_return_val_if_fail(c1 != NULL, NULL);
1201     g_return_val_if_fail(!_closed, NULL);
1202     g_return_val_if_fail(!c1->_closed, NULL);
1204     if (c1->_end < 1) {
1205         return this;
1206     }
1208     debug_check("SPCurve::append_continuous 11", this);
1210     NArtBpath const *be = last_bpath();
1211     if (be) {
1212         NArtBpath const *bs = c1->get_bpath();
1213         if ( bs
1214              && ( fabs( bs->x3 - be->x3 ) <= tolerance )
1215              && ( fabs( bs->y3 - be->y3 ) <= tolerance ) )
1216         {
1217             /** \todo
1218              * fixme: Strictly we mess in case of multisegment mixed
1219              * open/close curves
1220              */
1221             bool closed = false;
1222             for (bs = bs + 1; bs->code != NR_END; bs++) {
1223                 switch (bs->code) {
1224                     case NR_MOVETO_OPEN:
1225                         if (closed) closepath();
1226                         moveto(bs->x3, bs->y3);
1227                         closed = false;
1228                         break;
1229                     case NR_MOVETO:
1230                         if (closed) closepath();
1231                         moveto(bs->x3, bs->y3);
1232                         closed = true;
1233                         break;
1234                     case NR_LINETO:
1235                         lineto(bs->x3, bs->y3);
1236                         break;
1237                     case NR_CURVETO:
1238                         curveto(bs->x1, bs->y1, bs->x2, bs->y2, bs->x3, bs->y3);
1239                         break;
1240                     case NR_END:
1241                         g_assert_not_reached();
1242                 }
1243             }
1244         } else {
1245             append(c1, TRUE);
1246         }
1247     } else {
1248         append(c1, TRUE);
1249     }
1251     debug_check("SPCurve::append_continuous", this);
1253     return this;
1256 /**
1257  * Remove last segment of curve.
1258  * (Only used once in /src/pen-context.cpp)
1259  * 2GEOMified
1260  */
1261 void
1262 SPCurve::backspace()
1264     g_return_if_fail(this != NULL);
1266     if ( is_empty() )
1267         return;
1269     if (_end > 0) {
1270         _end -= 1;
1271         if (_end > 0) {
1272             NArtBpath *bp = _bpath + _end - 1;
1273             if ((bp->code == NR_MOVETO)     ||
1274                 (bp->code == NR_MOVETO_OPEN)  )
1275             {
1276                 _hascpt = true;
1277                 _posSet = true;
1278                 _closed = false;
1279                 _movePos = bp->c(3);
1280                 _end -= 1;
1281             }
1282         }
1283         _bpath[_end].code = NR_END;
1284     }
1286     if ( !_pathv.back().empty() ) {
1287         _pathv.back().erase_last();
1288         _pathv.back().close(false);
1289     }
1291     debug_check("SPCurve::backspace", this);
1294 /* Private methods */
1296 /**
1297  * Returns index of first NR_END bpath in array.
1298  */
1299 static unsigned sp_bpath_length(NArtBpath const bpath[])
1301     g_return_val_if_fail(bpath != NULL, FALSE);
1303     unsigned ret = 0;
1304     while ( bpath[ret].code != NR_END ) {
1305         ++ret;
1306     }
1307     ++ret;
1309     return ret;
1312 /**
1313  * \brief
1314  *
1315  * \todo
1316  * fixme: this is bogus -- it doesn't check for nr_moveto, which will indicate
1317  * a closing of the subpath it's nonsense to talk about a path as a whole
1318  * being closed, although maybe someone would want that for some other reason?
1319  * Oh, also, if the bpath just ends, then it's *open*.  I hope nobody is using
1320  * this code for anything.
1321  */
1322 static bool sp_bpath_closed(NArtBpath const bpath[])
1324     g_return_val_if_fail(bpath != NULL, FALSE);
1326     for (NArtBpath const *bp = bpath; bp->code != NR_END; bp++) {
1327         if (bp->code == NR_MOVETO_OPEN) {
1328             return false;
1329         }
1330     }
1332     return true;
1335 /**
1336  * Returns length of bezier segment.
1337  */
1338 static double
1339 bezier_len(NR::Point const &c0,
1340            NR::Point const &c1,
1341            NR::Point const &c2,
1342            NR::Point const &c3,
1343            double const threshold)
1345     /** \todo
1346      * The SVG spec claims that a closed form exists, but for the moment I'll
1347      * use a stupid algorithm.
1348      */
1349     double const lbound = L2( c3 - c0 );
1350     double const ubound = L2( c1 - c0 ) + L2( c2 - c1 ) + L2( c3 - c2 );
1351     double ret;
1352     if ( ubound - lbound <= threshold ) {
1353         ret = .5 * ( lbound + ubound );
1354     } else {
1355         NR::Point const a1( .5 * ( c0 + c1 ) );
1356         NR::Point const b2( .5 * ( c2 + c3 ) );
1357         NR::Point const c12( .5 * ( c1 + c2 ) );
1358         NR::Point const a2( .5 * ( a1 + c12 ) );
1359         NR::Point const b1( .5 * ( c12 + b2 ) );
1360         NR::Point const midpoint( .5 * ( a2 + b1 ) );
1361         double const rec_threshold = .625 * threshold;
1362         ret = bezier_len(c0, a1, a2, midpoint, rec_threshold) + bezier_len(midpoint, b1, b2, c3, rec_threshold);
1363         if (!(lbound - 1e-2 <= ret && ret <= ubound + 1e-2)) {
1364             using NR::X; using NR::Y;
1365             g_warning("ret=%f outside of expected bounds [%f, %f] for {(%.0f %.0f) (%.0f %.0f) (%.0f %.0f) (%.0f %.0f)}",
1366                       ret, lbound, ubound, c0[X], c0[Y], c1[X], c1[Y], c2[X], c2[Y], c3[X], c3[Y]);
1367         }
1368     }
1369     return ret;
1372 /**
1373  * Returns total length of curve, excluding length of closepath segments.
1374  */
1375 double
1376 sp_curve_distance_including_space(SPCurve const *const curve, double seg2len[])
1378     g_return_val_if_fail(curve != NULL, 0.);
1380     double ret = 0.0;
1382     if ( curve->_bpath->code == NR_END ) {
1383         return ret;
1384     }
1386     NR::Point prev(curve->_bpath->c(3));
1387     for (guint i = 1; i < curve->_end; ++i) {
1388         NArtBpath &p = curve->_bpath[i];
1389         double seg_len = 0;
1390         switch (p.code) {
1391             case NR_MOVETO_OPEN:
1392             case NR_MOVETO:
1393             case NR_LINETO:
1394                 seg_len = L2(p.c(3) - prev);
1395                 break;
1397             case NR_CURVETO:
1398                 seg_len = bezier_len(prev, p.c(1), p.c(2), p.c(3), 1.);
1399                 break;
1401             case NR_END:
1402                 return ret;
1403         }
1404         seg2len[i - 1] = seg_len;
1405         ret += seg_len;
1406         prev = p.c(3);
1407     }
1408     g_assert(!(ret < 0));
1409     return ret;
1412 /**
1413  * Like sp_curve_distance_including_space(), but ensures that the
1414  * result >= 1e-18:  uses 1 per segment if necessary.
1415  */
1416 double
1417 sp_curve_nonzero_distance_including_space(SPCurve const *const curve, double seg2len[])
1419     double const real_dist(sp_curve_distance_including_space(curve, seg2len));
1420     if (real_dist >= 1e-18) {
1421         return real_dist;
1422     } else {
1423         unsigned const nSegs = SP_CURVE_LENGTH(curve) - 1;
1424         for (unsigned i = 0; i < nSegs; ++i) {
1425             seg2len[i] = 1.;
1426         }
1427         return (double) nSegs;
1428     }
1431 /**
1432  * 2GEOMified
1433  */
1434 void
1435 SPCurve::stretch_endpoints(NR::Point const &new_p0, NR::Point const &new_p1)
1437     if (is_empty()) {
1438         return;
1439     }
1440     g_assert(unsigned(SP_CURVE_LENGTH(this)) + 1 == sp_bpath_length(_bpath));
1441     unsigned const nSegs = SP_CURVE_LENGTH(this) - 1;
1442     g_assert(nSegs != 0);
1443     double *const seg2len = new double[nSegs];
1444     double const tot_len = sp_curve_nonzero_distance_including_space(this, seg2len);
1445     NR::Point const offset0( new_p0 - first_point() );
1446     NR::Point const offset1( new_p1 - last_point() );
1447     _bpath->setC(3, new_p0);
1448     double begin_dist = 0.;
1449     for (unsigned si = 0; si < nSegs; ++si) {
1450         double const end_dist = begin_dist + seg2len[si];
1451         NArtBpath &p = _bpath[1 + si];
1452         switch (p.code) {
1453             case NR_LINETO:
1454             case NR_MOVETO:
1455             case NR_MOVETO_OPEN:
1456                 p.setC(3, p.c(3) + NR::Lerp(end_dist / tot_len, offset0, offset1));
1457                 break;
1459             case NR_CURVETO:
1460                 for (unsigned ci = 1; ci <= 3; ++ci) {
1461                     p.setC(ci, p.c(ci) + Lerp((begin_dist + ci * seg2len[si] / 3.) / tot_len, offset0, offset1));
1462                 }
1463                 break;
1465             default:
1466                 g_assert_not_reached();
1467         }
1469         begin_dist = end_dist;
1470     }
1471     g_assert(L1(_bpath[nSegs].c(3) - new_p1) < 1.);
1472     /* Explicit set for better numerical properties. */
1473     _bpath[nSegs].setC(3, new_p1);
1474     delete [] seg2len;
1476     Geom::Piecewise<Geom::D2<Geom::SBasis> > pwd2 = _pathv.front().toPwSb();
1477     Geom::Piecewise<Geom::SBasis> arclength = Geom::arcLengthSb(pwd2);
1478     if ( arclength.lastValue() <= 0 ) {
1479         g_error("SPCurve::stretch_endpoints - arclength <= 0");
1480         throw;
1481     }
1482     arclength *= 1./arclength.lastValue();
1483     Geom::Point const A( to_2geom(offset0) );
1484     Geom::Point const B( to_2geom(offset1) );
1485     Geom::Piecewise<Geom::SBasis> offsetx = (arclength*-1.+1)*A[0] + arclength*B[0];
1486     Geom::Piecewise<Geom::SBasis> offsety = (arclength*-1.+1)*A[1] + arclength*B[1];
1487     Geom::Piecewise<Geom::D2<Geom::SBasis> > offsetpath = Geom::sectionize( Geom::D2<Geom::Piecewise<Geom::SBasis> >(offsetx, offsety) );
1488     pwd2 += offsetpath;
1489     _pathv = Geom::path_from_piecewise( pwd2, 0.001 );
1491     debug_check("SPCurve::stretch_endpoints", this);
1494 /**
1495  *  sets start of first path to new_p0, and end of first path to  new_p1
1496  * 2GEOMified
1497  */
1498 void
1499 SPCurve::move_endpoints(NR::Point const &new_p0, NR::Point const &new_p1)
1501     if (is_empty()) {
1502         return;
1503     }
1504     unsigned const nSegs = SP_CURVE_LENGTH(this) - 1;
1505     g_assert(nSegs != 0);
1507     _bpath->setC(3, new_p0);
1508     _bpath[nSegs].setC(3, new_p1);
1510     _pathv.front().setInitial(to_2geom(new_p0));
1511     _pathv.front().setFinal(to_2geom(new_p1));
1513     debug_check("SPCurve::move_endpoints", this);
1516 /**
1517  * returns the number of nodes in a path, used for statusbar text when selecting an spcurve.
1518  * 2GEOMified
1519  */
1520 guint
1521 SPCurve::nodes_in_path() const
1523     gint r = _end;
1524     gint i = _length - 1;
1525     if (i > r) i = r; // sometimes after switching from node editor length is wrong, e.g. f6 - draw - f2 - tab - f1, this fixes it
1526     for (; i >= 0; i --)
1527         if (_bpath[i].code == NR_MOVETO)
1528             r --;
1530     guint nr = 0;
1531     for(Geom::PathVector::const_iterator it = _pathv.begin(); it != _pathv.end(); ++it) {
1532         nr += (*it).size();
1534         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
1535     }
1537     debug_check("SPCurve::nodes_in_path", r == (gint)nr);
1539     return r;
1542 /**
1543  *  Adds p to the last point (and last handle if present) of the last path
1544  * 2GEOMified
1545  */
1546 void
1547 SPCurve::last_point_additive_move(Geom::Point const & p)
1549     if (is_empty()) {
1550         return;
1551     }
1552     if (_end == 0) {
1553         return;
1554     }
1555     NArtBpath * path = _bpath + _end - 1;
1557     if (path->code == NR_CURVETO) {
1558         path->x2 += p[Geom::X];
1559         path->y2 += p[Geom::Y];
1560     }
1561     path->x3 += p[Geom::X];
1562     path->y3 += p[Geom::Y];
1564     _pathv.back().setFinal( _pathv.back().finalPoint() + p );
1566     // Move handle as well when the last segment is a cubic bezier segment:
1567     // TODO: what to do for quadratic beziers?
1568     if ( Geom::CubicBezier const *lastcube = dynamic_cast<Geom::CubicBezier const *>(&_pathv.back().back()) ) {
1569         Geom::CubicBezier newcube( *lastcube );
1570         newcube.setPoint(2, newcube[2] + p);
1571         _pathv.back().replace( --_pathv.back().end(), newcube );
1572     }
1574     debug_check("SPCurve::last_point_additive_move", this);
1577 /*
1578   Local Variables:
1579   mode:c++
1580   c-file-style:"stroustrup"
1581   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
1582   indent-tabs-mode:nil
1583   fill-column:99
1584   End:
1585 */
1586 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :