Code

fix bbox calculation for groups that contain groups with nothing in them (zero bbox...
[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_pathv(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_pathv", 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 (_bpath->code == NR_END);
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 path in PathVector or NULL.
899  */
900 Geom::Path const *
901 SPCurve::last_path() const
903     g_return_val_if_fail(this != NULL, NULL);
905     if (is_empty()) {
906         return NULL;
907     }
909     return &_pathv.back();
912 /**
913  * Return first subpath or NULL.
914  */
915 NArtBpath const *
916 SPCurve::first_bpath() const
918     g_return_val_if_fail(this != NULL, NULL);
920     if (_end == 0) {
921         return NULL;
922     }
924     return _bpath;
927 /**
928  * Return first path in PathVector or NULL.
929  */
930 Geom::Path const *
931 SPCurve::first_path() const
933     g_return_val_if_fail(this != NULL, NULL);
935     if (is_empty()) {
936         return NULL;
937     }
939     return &_pathv.front();
942 /**
943  * 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) ?
944  */
945 NR::Point
946 SPCurve::first_point() const
948     NArtBpath const * bpath = first_bpath();
949     g_return_val_if_fail(bpath != NULL, NR::Point(0, 0));
950     if (is_empty())
951         return NR::Point(0, 0);
953     debug_check("SPCurve::first_point", bpath->c(3) == _pathv.front().initialPoint() );
955     return bpath->c(3);
956     // return from_2geom( _pathv.front().initialPoint() );
959 /**
960  * Return the second point of first subpath or _movePos if curve too short.
961  */
962 NR::Point
963 SPCurve::second_point() const
965     g_return_val_if_fail(this != NULL, NR::Point(0, 0));
967     if (_end < 1) {
968         return _movePos;
969     }
971     NArtBpath *bpath = NULL;
972     if (_end < 2) {
973         bpath = _bpath;
974     } else {
975         bpath = _bpath + 1;
976     }
977     g_return_val_if_fail(bpath != NULL, NR::Point(0, 0));
979     debug_check("SPCurve::second_point", bpath->c(3) == _pathv.front()[0].finalPoint() );
981     return bpath->c(3);
984 /**
985  * Return the second-last point of last subpath or _movePos if curve too short.
986  */
987 NR::Point
988 SPCurve::penultimate_point() const
990     g_return_val_if_fail(this != NULL, NR::Point(0, 0));
992     if (_end < 2) {
993         return _movePos;
994     }
996     NArtBpath *const bpath = _bpath + _end - 2;
997     g_return_val_if_fail(bpath != NULL, NR::Point(0, 0));
998     
999     Geom::Point p(NR_HUGE, NR_HUGE);
1000     Geom::Curve const& back = _pathv.back().back();
1001     if (_pathv.back().closed()) {
1002         p = back.finalPoint();
1003     } else {
1004         p = back.initialPoint();
1005     }
1007     debug_check("SPCurve::penultimate_point", bpath->c(3) == p );
1008     return bpath->c(3);
1011 /**
1012  * 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) ?
1013  */
1014 NR::Point
1015 SPCurve::last_point() const
1017     NArtBpath const * bpath = last_bpath();
1018     g_return_val_if_fail(bpath != NULL, NR::Point(0, 0));
1019     if (is_empty())
1020         return NR::Point(0, 0);
1022     debug_check("SPCurve::last_point", bpath->c(3) == _pathv.back().finalPoint() );
1023     return bpath->c(3);
1024     // return from_2geom( _pathv.back().finalPoint() );
1027 inline static bool
1028 is_moveto(NRPathcode const c)
1030     return c == NR_MOVETO || c == NR_MOVETO_OPEN;
1033 /**
1034  * Returns a *new* \a curve but drawn in the opposite direction.
1035  * Should result in the same shape, but
1036  * with all its markers drawn facing the other direction.
1037  * Reverses the order of subpaths as well
1038  * 2GEOMified
1039  **/
1040 SPCurve *
1041 SPCurve::create_reverse() const
1043     /* We need at least moveto, curveto, end. */
1044     g_return_val_if_fail(_end - _substart > 1, NULL);
1046     NArtBpath const *be = _bpath + _end - 1;
1048     g_assert(is_moveto(_bpath[_substart].code));
1049     g_assert(is_moveto(_bpath[0].code));
1050     g_assert((be+1)->code == NR_END);
1052     SPCurve  *new_curve = new SPCurve(_length);
1053     new_curve->moveto(be->c(3));
1055     for (NArtBpath const *bp = be; ; --bp) {
1056         switch (bp->code) {
1057             case NR_MOVETO:
1058                 g_assert(new_curve->_bpath[new_curve->_substart].code == NR_MOVETO_OPEN);
1059                 new_curve->_bpath[new_curve->_substart].code = NR_MOVETO;
1060                 /* FALL-THROUGH */
1061             case NR_MOVETO_OPEN:
1062                 if (bp == _bpath) {
1063                     return new_curve;
1064                 }
1065                 new_curve->moveto((bp-1)->c(3));
1066                 break;
1068             case NR_LINETO:
1069                 new_curve->lineto((bp-1)->c(3));
1070                 break;
1072             case NR_CURVETO:
1073                 new_curve->curveto(bp->c(2), bp->c(1), (bp-1)->c(3));
1074                 break;
1076             default:
1077                 g_assert_not_reached();
1078         }
1079     }
1081     new_curve->_pathv = Geom::reverse_paths_and_order(_pathv);
1083     debug_check("SPCurve::create_reverse", new_curve);
1086 /**
1087  * Append \a curve2 to \a this.
1088  * If \a use_lineto is false, simply add all paths in \a curve2 to \a this;
1089  * 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.
1090  * 2GEOMified
1091  */
1092 void
1093 SPCurve::append(SPCurve const *curve2,
1094                 bool use_lineto)
1096     g_return_if_fail(this != NULL);
1097     g_return_if_fail(curve2 != NULL);
1099     if (curve2->is_empty())
1100         return;
1101     if (curve2->_end < 1)
1102         return;
1104     NArtBpath const *bs = curve2->_bpath;
1106     bool closed = this->_closed;
1108     for (NArtBpath const *bp = bs; bp->code != NR_END; bp++) {
1109         switch (bp->code) {
1110             case NR_MOVETO_OPEN:
1111                 if (use_lineto && _hascpt) {
1112                     lineto(bp->x3, bp->y3);
1113                     use_lineto = false;
1114                 } else {
1115                     if (closed && _hascpt) closepath();
1116                     moveto(bp->x3, bp->y3);
1117                 }
1118                 closed = false;
1119                 break;
1121             case NR_MOVETO:
1122                 if (use_lineto && _hascpt) {
1123                     lineto(bp->x3, bp->y3);
1124                     use_lineto = FALSE;
1125                 } else {
1126                     if (closed && _hascpt) closepath();
1127                     moveto(bp->x3, bp->y3);
1128                 }
1129                 closed = true;
1130                 break;
1132             case NR_LINETO:
1133                 lineto(bp->x3, bp->y3);
1134                 break;
1136             case NR_CURVETO:
1137                 curveto(bp->x1, bp->y1, bp->x2, bp->y2, bp->x3, bp->y3);
1138                 break;
1140             case NR_END:
1141                 g_assert_not_reached();
1142         }
1143     }
1145     if (closed) {
1146         closepath();
1147     }
1149     debug_check("SPCurve::append", this);
1151     /* 2GEOM code when code above is removed:
1152     if (use_lineto) {
1153         Geom::PathVector::const_iterator it = curve2->_pathv.begin();
1154         if ( ! _pathv.empty() ) {
1155             Geom::Path & lastpath = _pathv.back();
1156             lastpath.appendNew<Geom::LineSegment>( (*it).initialPoint() );
1157             lastpath.append( (*it) );
1158         } else {
1159             _pathv.push_back( (*it) );
1160         }
1162         for (it++; it != curve2->_pathv.end(); it++) {
1163             _pathv.push_back( (*it) );
1164         }
1165     } else {
1166         for (Geom::PathVector::const_iterator it = curve2->_pathv.begin(); it != curve2->_pathv.end(); it++) {
1167             _pathv.push_back( (*it) );
1168         }
1169     }
1170     */
1173 /**
1174  * Append \a c1 to \a this with possible fusing of close endpoints.
1175  * 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
1176  */
1177 SPCurve *
1178 SPCurve::append_continuous(SPCurve const *c1, gdouble tolerance)
1180     g_return_val_if_fail(this != NULL, NULL);
1181     g_return_val_if_fail(c1 != NULL, NULL);
1182     g_return_val_if_fail(!_closed, NULL);
1183     g_return_val_if_fail(!c1->_closed, NULL);
1185     if (c1->_end < 1) {
1186         return this;
1187     }
1189     debug_check("SPCurve::append_continuous 11", this);
1191     NArtBpath const *be = last_bpath();
1192     if (be) {
1193         NArtBpath const *bs = c1->first_bpath();
1194         if ( bs
1195              && ( fabs( bs->x3 - be->x3 ) <= tolerance )
1196              && ( fabs( bs->y3 - be->y3 ) <= tolerance ) )
1197         {
1198             /** \todo
1199              * fixme: Strictly we mess in case of multisegment mixed
1200              * open/close curves
1201              */
1202             bool closed = false;
1203             for (bs = bs + 1; bs->code != NR_END; bs++) {
1204                 switch (bs->code) {
1205                     case NR_MOVETO_OPEN:
1206                         if (closed) closepath();
1207                         moveto(bs->x3, bs->y3);
1208                         closed = false;
1209                         break;
1210                     case NR_MOVETO:
1211                         if (closed) closepath();
1212                         moveto(bs->x3, bs->y3);
1213                         closed = true;
1214                         break;
1215                     case NR_LINETO:
1216                         lineto(bs->x3, bs->y3);
1217                         break;
1218                     case NR_CURVETO:
1219                         curveto(bs->x1, bs->y1, bs->x2, bs->y2, bs->x3, bs->y3);
1220                         break;
1221                     case NR_END:
1222                         g_assert_not_reached();
1223                 }
1224             }
1225         } else {
1226             append(c1, TRUE);
1227         }
1228     } else {
1229         append(c1, TRUE);
1230     }
1232     debug_check("SPCurve::append_continuous", this);
1234     return this;
1237 /**
1238  * Remove last segment of curve.
1239  * (Only used once in /src/pen-context.cpp)
1240  * 2GEOMified
1241  */
1242 void
1243 SPCurve::backspace()
1245     g_return_if_fail(this != NULL);
1247     if ( is_empty() )
1248         return;
1250     if (_end > 0) {
1251         _end -= 1;
1252         if (_end > 0) {
1253             NArtBpath *bp = _bpath + _end - 1;
1254             if ((bp->code == NR_MOVETO)     ||
1255                 (bp->code == NR_MOVETO_OPEN)  )
1256             {
1257                 _hascpt = true;
1258                 _posSet = true;
1259                 _closed = false;
1260                 _movePos = bp->c(3);
1261                 _end -= 1;
1262             }
1263         }
1264         _bpath[_end].code = NR_END;
1265     }
1267     if ( !_pathv.back().empty() ) {
1268         _pathv.back().erase_last();
1269         _pathv.back().close(false);
1270     }
1272     debug_check("SPCurve::backspace", this);
1275 /* Private methods */
1277 /**
1278  * Returns index of first NR_END bpath in array.
1279  */
1280 static unsigned sp_bpath_length(NArtBpath const bpath[])
1282     g_return_val_if_fail(bpath != NULL, FALSE);
1284     unsigned ret = 0;
1285     while ( bpath[ret].code != NR_END ) {
1286         ++ret;
1287     }
1288     ++ret;
1290     return ret;
1293 /**
1294  * \brief
1295  *
1296  * \todo
1297  * fixme: this is bogus -- it doesn't check for nr_moveto, which will indicate
1298  * a closing of the subpath it's nonsense to talk about a path as a whole
1299  * being closed, although maybe someone would want that for some other reason?
1300  * Oh, also, if the bpath just ends, then it's *open*.  I hope nobody is using
1301  * this code for anything.
1302  */
1303 static bool sp_bpath_closed(NArtBpath const bpath[])
1305     g_return_val_if_fail(bpath != NULL, FALSE);
1307     for (NArtBpath const *bp = bpath; bp->code != NR_END; bp++) {
1308         if (bp->code == NR_MOVETO_OPEN) {
1309             return false;
1310         }
1311     }
1313     return true;
1316 /**
1317  * Returns length of bezier segment.
1318  */
1319 static double
1320 bezier_len(NR::Point const &c0,
1321            NR::Point const &c1,
1322            NR::Point const &c2,
1323            NR::Point const &c3,
1324            double const threshold)
1326     /** \todo
1327      * The SVG spec claims that a closed form exists, but for the moment I'll
1328      * use a stupid algorithm.
1329      */
1330     double const lbound = L2( c3 - c0 );
1331     double const ubound = L2( c1 - c0 ) + L2( c2 - c1 ) + L2( c3 - c2 );
1332     double ret;
1333     if ( ubound - lbound <= threshold ) {
1334         ret = .5 * ( lbound + ubound );
1335     } else {
1336         NR::Point const a1( .5 * ( c0 + c1 ) );
1337         NR::Point const b2( .5 * ( c2 + c3 ) );
1338         NR::Point const c12( .5 * ( c1 + c2 ) );
1339         NR::Point const a2( .5 * ( a1 + c12 ) );
1340         NR::Point const b1( .5 * ( c12 + b2 ) );
1341         NR::Point const midpoint( .5 * ( a2 + b1 ) );
1342         double const rec_threshold = .625 * threshold;
1343         ret = bezier_len(c0, a1, a2, midpoint, rec_threshold) + bezier_len(midpoint, b1, b2, c3, rec_threshold);
1344         if (!(lbound - 1e-2 <= ret && ret <= ubound + 1e-2)) {
1345             using NR::X; using NR::Y;
1346             g_warning("ret=%f outside of expected bounds [%f, %f] for {(%.0f %.0f) (%.0f %.0f) (%.0f %.0f) (%.0f %.0f)}",
1347                       ret, lbound, ubound, c0[X], c0[Y], c1[X], c1[Y], c2[X], c2[Y], c3[X], c3[Y]);
1348         }
1349     }
1350     return ret;
1353 /**
1354  * Returns total length of curve, excluding length of closepath segments.
1355  */
1356 double
1357 sp_curve_distance_including_space(SPCurve const *const curve, double seg2len[])
1359     g_return_val_if_fail(curve != NULL, 0.);
1361     double ret = 0.0;
1363     if ( curve->_bpath->code == NR_END ) {
1364         return ret;
1365     }
1367     NR::Point prev(curve->_bpath->c(3));
1368     for (guint i = 1; i < curve->_end; ++i) {
1369         NArtBpath &p = curve->_bpath[i];
1370         double seg_len = 0;
1371         switch (p.code) {
1372             case NR_MOVETO_OPEN:
1373             case NR_MOVETO:
1374             case NR_LINETO:
1375                 seg_len = L2(p.c(3) - prev);
1376                 break;
1378             case NR_CURVETO:
1379                 seg_len = bezier_len(prev, p.c(1), p.c(2), p.c(3), 1.);
1380                 break;
1382             case NR_END:
1383                 return ret;
1384         }
1385         seg2len[i - 1] = seg_len;
1386         ret += seg_len;
1387         prev = p.c(3);
1388     }
1389     g_assert(!(ret < 0));
1390     return ret;
1393 /**
1394  * Like sp_curve_distance_including_space(), but ensures that the
1395  * result >= 1e-18:  uses 1 per segment if necessary.
1396  */
1397 double
1398 sp_curve_nonzero_distance_including_space(SPCurve const *const curve, double seg2len[])
1400     double const real_dist(sp_curve_distance_including_space(curve, seg2len));
1401     if (real_dist >= 1e-18) {
1402         return real_dist;
1403     } else {
1404         unsigned const nSegs = SP_CURVE_LENGTH(curve) - 1;
1405         for (unsigned i = 0; i < nSegs; ++i) {
1406             seg2len[i] = 1.;
1407         }
1408         return (double) nSegs;
1409     }
1412 /**
1413  * 2GEOMified
1414  */
1415 void
1416 SPCurve::stretch_endpoints(NR::Point const &new_p0, NR::Point const &new_p1)
1418     if (is_empty()) {
1419         return;
1420     }
1421     g_assert(unsigned(SP_CURVE_LENGTH(this)) + 1 == sp_bpath_length(_bpath));
1422     unsigned const nSegs = SP_CURVE_LENGTH(this) - 1;
1423     g_assert(nSegs != 0);
1424     double *const seg2len = new double[nSegs];
1425     double const tot_len = sp_curve_nonzero_distance_including_space(this, seg2len);
1426     NR::Point const offset0( new_p0 - first_point() );
1427     NR::Point const offset1( new_p1 - last_point() );
1428     _bpath->setC(3, new_p0);
1429     double begin_dist = 0.;
1430     for (unsigned si = 0; si < nSegs; ++si) {
1431         double const end_dist = begin_dist + seg2len[si];
1432         NArtBpath &p = _bpath[1 + si];
1433         switch (p.code) {
1434             case NR_LINETO:
1435             case NR_MOVETO:
1436             case NR_MOVETO_OPEN:
1437                 p.setC(3, p.c(3) + NR::Lerp(end_dist / tot_len, offset0, offset1));
1438                 break;
1440             case NR_CURVETO:
1441                 for (unsigned ci = 1; ci <= 3; ++ci) {
1442                     p.setC(ci, p.c(ci) + Lerp((begin_dist + ci * seg2len[si] / 3.) / tot_len, offset0, offset1));
1443                 }
1444                 break;
1446             default:
1447                 g_assert_not_reached();
1448         }
1450         begin_dist = end_dist;
1451     }
1452     g_assert(L1(_bpath[nSegs].c(3) - new_p1) < 1.);
1453     /* Explicit set for better numerical properties. */
1454     _bpath[nSegs].setC(3, new_p1);
1455     delete [] seg2len;
1457     Geom::Piecewise<Geom::D2<Geom::SBasis> > pwd2 = _pathv.front().toPwSb();
1458     Geom::Piecewise<Geom::SBasis> arclength = Geom::arcLengthSb(pwd2);
1459     if ( arclength.lastValue() <= 0 ) {
1460         g_error("SPCurve::stretch_endpoints - arclength <= 0");
1461         throw;
1462     }
1463     arclength *= 1./arclength.lastValue();
1464     Geom::Point const A( to_2geom(offset0) );
1465     Geom::Point const B( to_2geom(offset1) );
1466     Geom::Piecewise<Geom::SBasis> offsetx = (arclength*-1.+1)*A[0] + arclength*B[0];
1467     Geom::Piecewise<Geom::SBasis> offsety = (arclength*-1.+1)*A[1] + arclength*B[1];
1468     Geom::Piecewise<Geom::D2<Geom::SBasis> > offsetpath = Geom::sectionize( Geom::D2<Geom::Piecewise<Geom::SBasis> >(offsetx, offsety) );
1469     pwd2 += offsetpath;
1470     _pathv = Geom::path_from_piecewise( pwd2, 0.001 );
1472     debug_check("SPCurve::stretch_endpoints", this);
1475 /**
1476  *  sets start of first path to new_p0, and end of first path to  new_p1
1477  * 2GEOMified
1478  */
1479 void
1480 SPCurve::move_endpoints(NR::Point const &new_p0, NR::Point const &new_p1)
1482     if (is_empty()) {
1483         return;
1484     }
1485     unsigned const nSegs = SP_CURVE_LENGTH(this) - 1;
1486     g_assert(nSegs != 0);
1488     _bpath->setC(3, new_p0);
1489     _bpath[nSegs].setC(3, new_p1);
1491     _pathv.front().setInitial(to_2geom(new_p0));
1492     _pathv.front().setFinal(to_2geom(new_p1));
1494     debug_check("SPCurve::move_endpoints", this);
1497 /**
1498  * returns the number of nodes in a path, used for statusbar text when selecting an spcurve.
1499  * 2GEOMified
1500  */
1501 guint
1502 SPCurve::nodes_in_path() const
1504     gint r = _end;
1505     gint i = _length - 1;
1506     if (i > r) i = r; // sometimes after switching from node editor length is wrong, e.g. f6 - draw - f2 - tab - f1, this fixes it
1507     for (; i >= 0; i --)
1508         if (_bpath[i].code == NR_MOVETO)
1509             r --;
1511     guint nr = 0;
1512     for(Geom::PathVector::const_iterator it = _pathv.begin(); it != _pathv.end(); ++it) {
1513         nr += (*it).size();
1515         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
1516     }
1518     debug_check("SPCurve::nodes_in_path", r == (gint)nr);
1520     return r;
1523 /**
1524  *  Adds p to the last point (and last handle if present) of the last path
1525  * 2GEOMified
1526  */
1527 void
1528 SPCurve::last_point_additive_move(Geom::Point const & p)
1530     if (is_empty()) {
1531         return;
1532     }
1533     if (_end == 0) {
1534         return;
1535     }
1536     NArtBpath * path = _bpath + _end - 1;
1538     if (path->code == NR_CURVETO) {
1539         path->x2 += p[Geom::X];
1540         path->y2 += p[Geom::Y];
1541     }
1542     path->x3 += p[Geom::X];
1543     path->y3 += p[Geom::Y];
1545     _pathv.back().setFinal( _pathv.back().finalPoint() + p );
1547     // Move handle as well when the last segment is a cubic bezier segment:
1548     // TODO: what to do for quadratic beziers?
1549     if ( Geom::CubicBezier const *lastcube = dynamic_cast<Geom::CubicBezier const *>(&_pathv.back().back()) ) {
1550         Geom::CubicBezier newcube( *lastcube );
1551         newcube.setPoint(2, newcube[2] + p);
1552         _pathv.back().replace( --_pathv.back().end(), newcube );
1553     }
1555     debug_check("SPCurve::last_point_additive_move", this);
1558 /*
1559   Local Variables:
1560   mode:c++
1561   c-file-style:"stroustrup"
1562   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
1563   indent-tabs-mode:nil
1564   fill-column:99
1565   End:
1566 */
1567 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :