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)
104 {
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);
116 }
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)
129 {
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);
144 }
146 // * 2GEOMproof
147 SPCurve *
148 SPCurve::new_from_foreign_bpath(NArtBpath const *bpath)
149 {
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;
175 }
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)
185 {
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;
194 }
196 // * 2GEOMproof
197 SPCurve *
198 SPCurve::new_from_rect(NR::Maybe<NR::Rect> const &rect)
199 {
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;
215 }
217 // * 2GEOMproof
218 SPCurve::~SPCurve()
219 {
220 if (_bpath) {
221 g_free(_bpath);
222 _bpath = NULL;
223 }
224 }
226 /* Methods */
228 void
229 SPCurve::set_pathvector(Geom::PathVector const & new_pathv)
230 {
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);
255 }
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
262 {
263 debug_check("SPCurve::get_bpath", this);
264 return _bpath;
265 };
267 Geom::PathVector const &
268 SPCurve::get_pathvector() const
269 {
270 debug_check("SPCurve::get_pathvector", this);
271 return _pathv;
272 }
274 /**
275 *Returns index in bpath[] of NR_END element.
276 * remove for 2geom
277 */
278 guint
279 SPCurve::get_length() const
280 {
281 // g_message("SPCurve::get_length must be removed");
283 return _end;
284 }
286 /*
287 * Returns the number of segments of all paths summed
288 */
289 guint
290 SPCurve::get_segment_count() const
291 {
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;
299 }
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()
310 {
311 g_return_val_if_fail(this != NULL, NULL);
313 _refcount += 1;
315 return this;
316 }
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()
326 {
327 g_return_val_if_fail(this != NULL, NULL);
329 _refcount -= 1;
331 if (_refcount < 1) {
332 delete this;
333 }
335 return NULL;
336 }
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)
345 {
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;
358 }
360 /**
361 * Create new curve from its own bpath array.
362 * 2GEOMproof
363 */
364 SPCurve *
365 SPCurve::copy() const
366 {
367 g_return_val_if_fail(this != NULL, NULL);
369 return SPCurve::new_from_foreign_bpath(_bpath);
370 }
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)
378 {
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;
418 }
420 /**
421 * Returns a list of new curves corresponding to the subpaths in \a curve.
422 * 2geomified
423 */
424 GSList *
425 SPCurve::split() const
426 {
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;
452 }
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)
460 {
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 }
482 }
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)
490 {
491 tmpl_curve_transform<NR::Matrix>(this, m);
493 _pathv = _pathv * to_2geom(m);
495 debug_check("SPCurve::transform(NR::Matrix const &m)", this);
496 }
498 /**
499 * Transform all paths in curve using matrix.
500 */
501 void
502 SPCurve::transform(Geom::Matrix const &m)
503 {
504 tmpl_curve_transform<NR::Matrix>(this, from_2geom(m));
506 _pathv = _pathv * m;
508 debug_check("SPCurve::transform(Geom::Matrix const &m)", this);
509 }
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)
517 {
518 tmpl_curve_transform<NR::translate>(this, m);
520 _pathv = _pathv * to_2geom(m);
522 debug_check("SPCurve::transform(NR::translate const &m)", this);
523 }
525 /**
526 * Set curve to empty curve.
527 * 2GEOMified
528 */
529 void
530 SPCurve::reset()
531 {
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);
545 }
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)
554 {
555 moveto(NR::Point(x, y));
556 }
557 /**
558 * Calls SPCurve::moveto() with point made of given coordinates.
559 */
560 void
561 SPCurve::moveto(Geom::Point const &p)
562 {
563 moveto(from_2geom(p));
564 }
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)
571 {
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);
584 }
586 /**
587 * Calls SPCurve::lineto() with a point's coordinates.
588 */
589 void
590 SPCurve::lineto(Geom::Point const &p)
591 {
592 lineto(p[Geom::X], p[Geom::Y]);
593 }
594 /**
595 * Calls SPCurve::lineto() with a point's coordinates.
596 */
597 void
598 SPCurve::lineto(NR::Point const &p)
599 {
600 lineto(p[NR::X], p[NR::Y]);
601 }
602 /**
603 * Adds a line to the current subpath.
604 * 2GEOMified
605 */
606 void
607 SPCurve::lineto(gdouble x, gdouble y)
608 {
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);
662 }
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)
669 {
670 using Geom::X;
671 using Geom::Y;
672 curveto( p0[X], p0[Y],
673 p1[X], p1[Y],
674 p2[X], p2[Y] );
675 }
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)
681 {
682 using NR::X;
683 using NR::Y;
684 curveto( p0[X], p0[Y],
685 p1[X], p1[Y],
686 p2[X], p2[Y] );
687 }
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)
694 {
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);
740 }
742 /**
743 * Close current subpath by possibly adding a line between start and end.
744 * 2GEOMified
745 */
746 void
747 SPCurve::closepath()
748 {
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);
799 }
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.
806 2GEOMified
807 **/
808 void
809 SPCurve::closepath_current()
810 {
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);
859 }
861 /**
862 * True if no paths are in curve. If it only contains a path with only a moveto, the path is considered NON-empty
863 */
864 bool
865 SPCurve::is_empty() const
866 {
867 bool empty = _pathv.empty();
869 return empty;
870 }
872 /**
873 * True iff all subpaths are closed.
874 */
875 bool
876 SPCurve::is_closed() const
877 {
878 bool closed = true;
879 for (Geom::PathVector::const_iterator it = _pathv.begin(); it != _pathv.end(); it++) {
880 if ( ! it->closed() ) {
881 closed = false;
882 break;
883 }
884 }
886 return closed;
887 }
889 /**
890 * Return last subpath or NULL.
891 */
892 NArtBpath const *
893 SPCurve::last_bpath() const
894 {
895 g_return_val_if_fail(this != NULL, NULL);
897 if (_end == 0) {
898 return NULL;
899 }
901 return _bpath + _end - 1;
902 }
904 /**
905 * Return last pathsegment (possibly the closing path segment) in PathVector or NULL.
906 * equal in functionality to SPCurve::last_bpath()
907 */
908 Geom::Curve const *
909 SPCurve::last_segment() const
910 {
911 if (is_empty()) {
912 return NULL;
913 }
914 if (_pathv.back().empty()) {
915 return NULL;
916 }
918 return &_pathv.back().back_default();
919 }
921 /**
922 * Return last path in PathVector or NULL.
923 */
924 Geom::Path const *
925 SPCurve::last_path() const
926 {
927 if (is_empty()) {
928 return NULL;
929 }
931 return &_pathv.back();
932 }
934 /**
935 * Return first pathsegment in PathVector or NULL.
936 * equal in functionality to SPCurve::first_bpath()
937 */
938 Geom::Curve const *
939 SPCurve::first_segment() const
940 {
941 if (is_empty()) {
942 return NULL;
943 }
944 if (_pathv.front().empty()) {
945 return NULL;
946 }
948 return &_pathv.front().front();
949 }
951 /**
952 * Return first path in PathVector or NULL.
953 */
954 Geom::Path const *
955 SPCurve::first_path() const
956 {
957 if (is_empty()) {
958 return NULL;
959 }
961 return &_pathv.front();
962 }
964 /**
965 * 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) ?
966 */
967 NR::Point
968 SPCurve::first_point() const
969 {
970 if (is_empty())
971 return NR::Point(0, 0);
973 return from_2geom( _pathv.front().initialPoint() );
974 }
976 /**
977 * Return the second point of first subpath or _movePos if curve too short.
978 * If the pathvector is empty, this returns (0,0). If the first path is only a moveto, this method
979 * returns the first point of the second path, if it exists. If there is no 2nd path, it returns the
980 * first point of the first path.
981 *
982 * FIXME: for empty paths shouldn't this return (NR_HUGE,NR_HUGE)
983 */
984 NR::Point
985 SPCurve::second_point() const
986 {
987 if (is_empty()) {
988 return NR::Point(0,0);
989 }
990 else if (_pathv.front().empty()) {
991 // first path is only a moveto
992 // check if there is second path
993 if (_pathv.size() > 1) {
994 return _pathv[1].initialPoint();
995 } else {
996 return _pathv[0].initialPoint();
997 }
998 }
999 else
1000 return _pathv.front()[0].finalPoint();
1001 }
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
1008 {
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));
1019 Geom::Curve const& back = _pathv.back().back_default();
1020 Geom::Point p = back.initialPoint();
1022 debug_check("SPCurve::penultimate_point", bpath->c(3) == from_2geom(p) );
1023 return from_2geom(p);
1024 }
1026 /**
1027 * 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) ?
1028 * If the last path is only a moveto, then return that point.
1029 */
1030 NR::Point
1031 SPCurve::last_point() const
1032 {
1033 if (is_empty())
1034 return NR::Point(0, 0);
1036 return from_2geom( _pathv.back().finalPoint() );
1037 }
1039 inline static bool
1040 is_moveto(NRPathcode const c)
1041 {
1042 return c == NR_MOVETO || c == NR_MOVETO_OPEN;
1043 }
1045 /**
1046 * Returns a *new* \a curve but drawn in the opposite direction.
1047 * Should result in the same shape, but
1048 * with all its markers drawn facing the other direction.
1049 * Reverses the order of subpaths as well
1050 * 2GEOMified
1051 **/
1052 SPCurve *
1053 SPCurve::create_reverse() const
1054 {
1055 /* We need at least moveto, curveto, end. */
1056 g_return_val_if_fail(_end - _substart > 1, NULL);
1058 NArtBpath const *be = _bpath + _end - 1;
1060 g_assert(is_moveto(_bpath[_substart].code));
1061 g_assert(is_moveto(_bpath[0].code));
1062 g_assert((be+1)->code == NR_END);
1064 SPCurve *new_curve = new SPCurve(_length);
1065 new_curve->moveto(be->c(3));
1067 for (NArtBpath const *bp = be; ; --bp) {
1068 switch (bp->code) {
1069 case NR_MOVETO:
1070 g_assert(new_curve->_bpath[new_curve->_substart].code == NR_MOVETO_OPEN);
1071 new_curve->_bpath[new_curve->_substart].code = NR_MOVETO;
1072 /* FALL-THROUGH */
1073 case NR_MOVETO_OPEN:
1074 if (bp == _bpath) {
1075 return new_curve;
1076 }
1077 new_curve->moveto((bp-1)->c(3));
1078 break;
1080 case NR_LINETO:
1081 new_curve->lineto((bp-1)->c(3));
1082 break;
1084 case NR_CURVETO:
1085 new_curve->curveto(bp->c(2), bp->c(1), (bp-1)->c(3));
1086 break;
1088 default:
1089 g_assert_not_reached();
1090 }
1091 }
1093 new_curve->_pathv = Geom::reverse_paths_and_order(_pathv);
1095 debug_check("SPCurve::create_reverse", new_curve);
1096 }
1098 /**
1099 * Append \a curve2 to \a this.
1100 * If \a use_lineto is false, simply add all paths in \a curve2 to \a this;
1101 * 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.
1102 * 2GEOMified
1103 */
1104 void
1105 SPCurve::append(SPCurve const *curve2,
1106 bool use_lineto)
1107 {
1108 g_return_if_fail(this != NULL);
1109 g_return_if_fail(curve2 != NULL);
1111 if (curve2->is_empty())
1112 return;
1113 if (curve2->_end < 1)
1114 return;
1116 NArtBpath const *bs = curve2->_bpath;
1118 bool closed = this->_closed;
1120 for (NArtBpath const *bp = bs; bp->code != NR_END; bp++) {
1121 switch (bp->code) {
1122 case NR_MOVETO_OPEN:
1123 if (use_lineto && _hascpt) {
1124 lineto(bp->x3, bp->y3);
1125 use_lineto = false;
1126 } else {
1127 if (closed && _hascpt) closepath();
1128 moveto(bp->x3, bp->y3);
1129 }
1130 closed = false;
1131 break;
1133 case NR_MOVETO:
1134 if (use_lineto && _hascpt) {
1135 lineto(bp->x3, bp->y3);
1136 use_lineto = FALSE;
1137 } else {
1138 if (closed && _hascpt) closepath();
1139 moveto(bp->x3, bp->y3);
1140 }
1141 closed = true;
1142 break;
1144 case NR_LINETO:
1145 lineto(bp->x3, bp->y3);
1146 break;
1148 case NR_CURVETO:
1149 curveto(bp->x1, bp->y1, bp->x2, bp->y2, bp->x3, bp->y3);
1150 break;
1152 case NR_END:
1153 g_assert_not_reached();
1154 }
1155 }
1157 if (closed) {
1158 closepath();
1159 }
1161 debug_check("SPCurve::append", this);
1163 /* 2GEOM code when code above is removed:
1164 if (use_lineto) {
1165 Geom::PathVector::const_iterator it = curve2->_pathv.begin();
1166 if ( ! _pathv.empty() ) {
1167 Geom::Path & lastpath = _pathv.back();
1168 lastpath.appendNew<Geom::LineSegment>( (*it).initialPoint() );
1169 lastpath.append( (*it) );
1170 } else {
1171 _pathv.push_back( (*it) );
1172 }
1174 for (it++; it != curve2->_pathv.end(); it++) {
1175 _pathv.push_back( (*it) );
1176 }
1177 } else {
1178 for (Geom::PathVector::const_iterator it = curve2->_pathv.begin(); it != curve2->_pathv.end(); it++) {
1179 _pathv.push_back( (*it) );
1180 }
1181 }
1182 */
1183 }
1185 /**
1186 * Append \a c1 to \a this with possible fusing of close endpoints.
1187 * 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
1188 */
1189 SPCurve *
1190 SPCurve::append_continuous(SPCurve const *c1, gdouble tolerance)
1191 {
1192 g_return_val_if_fail(this != NULL, NULL);
1193 g_return_val_if_fail(c1 != NULL, NULL);
1194 g_return_val_if_fail(!_closed, NULL);
1195 g_return_val_if_fail(!c1->_closed, NULL);
1197 if (c1->_end < 1) {
1198 return this;
1199 }
1201 debug_check("SPCurve::append_continuous 11", this);
1203 NArtBpath const *be = last_bpath();
1204 if (be) {
1205 NArtBpath const *bs = c1->get_bpath();
1206 if ( bs
1207 && ( fabs( bs->x3 - be->x3 ) <= tolerance )
1208 && ( fabs( bs->y3 - be->y3 ) <= tolerance ) )
1209 {
1210 /** \todo
1211 * fixme: Strictly we mess in case of multisegment mixed
1212 * open/close curves
1213 */
1214 bool closed = false;
1215 for (bs = bs + 1; bs->code != NR_END; bs++) {
1216 switch (bs->code) {
1217 case NR_MOVETO_OPEN:
1218 if (closed) closepath();
1219 moveto(bs->x3, bs->y3);
1220 closed = false;
1221 break;
1222 case NR_MOVETO:
1223 if (closed) closepath();
1224 moveto(bs->x3, bs->y3);
1225 closed = true;
1226 break;
1227 case NR_LINETO:
1228 lineto(bs->x3, bs->y3);
1229 break;
1230 case NR_CURVETO:
1231 curveto(bs->x1, bs->y1, bs->x2, bs->y2, bs->x3, bs->y3);
1232 break;
1233 case NR_END:
1234 g_assert_not_reached();
1235 }
1236 }
1237 } else {
1238 append(c1, TRUE);
1239 }
1240 } else {
1241 append(c1, TRUE);
1242 }
1244 debug_check("SPCurve::append_continuous", this);
1246 return this;
1247 }
1249 /**
1250 * Remove last segment of curve.
1251 * (Only used once in /src/pen-context.cpp)
1252 * 2GEOMified
1253 */
1254 void
1255 SPCurve::backspace()
1256 {
1257 g_return_if_fail(this != NULL);
1259 if ( is_empty() )
1260 return;
1262 if (_end > 0) {
1263 _end -= 1;
1264 if (_end > 0) {
1265 NArtBpath *bp = _bpath + _end - 1;
1266 if ((bp->code == NR_MOVETO) ||
1267 (bp->code == NR_MOVETO_OPEN) )
1268 {
1269 _hascpt = true;
1270 _posSet = true;
1271 _closed = false;
1272 _movePos = bp->c(3);
1273 _end -= 1;
1274 }
1275 }
1276 _bpath[_end].code = NR_END;
1277 }
1279 if ( !_pathv.back().empty() ) {
1280 _pathv.back().erase_last();
1281 _pathv.back().close(false);
1282 }
1284 debug_check("SPCurve::backspace", this);
1285 }
1287 /* Private methods */
1289 /**
1290 * Returns index of first NR_END bpath in array.
1291 */
1292 static unsigned sp_bpath_length(NArtBpath const bpath[])
1293 {
1294 g_return_val_if_fail(bpath != NULL, FALSE);
1296 unsigned ret = 0;
1297 while ( bpath[ret].code != NR_END ) {
1298 ++ret;
1299 }
1300 ++ret;
1302 return ret;
1303 }
1305 /**
1306 * \brief
1307 *
1308 * \todo
1309 * fixme: this is bogus -- it doesn't check for nr_moveto, which will indicate
1310 * a closing of the subpath it's nonsense to talk about a path as a whole
1311 * being closed, although maybe someone would want that for some other reason?
1312 * Oh, also, if the bpath just ends, then it's *open*. I hope nobody is using
1313 * this code for anything.
1314 */
1315 static bool sp_bpath_closed(NArtBpath const bpath[])
1316 {
1317 g_return_val_if_fail(bpath != NULL, FALSE);
1319 for (NArtBpath const *bp = bpath; bp->code != NR_END; bp++) {
1320 if (bp->code == NR_MOVETO_OPEN) {
1321 return false;
1322 }
1323 }
1325 return true;
1326 }
1328 /**
1329 * Returns length of bezier segment.
1330 */
1331 static double
1332 bezier_len(NR::Point const &c0,
1333 NR::Point const &c1,
1334 NR::Point const &c2,
1335 NR::Point const &c3,
1336 double const threshold)
1337 {
1338 /** \todo
1339 * The SVG spec claims that a closed form exists, but for the moment I'll
1340 * use a stupid algorithm.
1341 */
1342 double const lbound = L2( c3 - c0 );
1343 double const ubound = L2( c1 - c0 ) + L2( c2 - c1 ) + L2( c3 - c2 );
1344 double ret;
1345 if ( ubound - lbound <= threshold ) {
1346 ret = .5 * ( lbound + ubound );
1347 } else {
1348 NR::Point const a1( .5 * ( c0 + c1 ) );
1349 NR::Point const b2( .5 * ( c2 + c3 ) );
1350 NR::Point const c12( .5 * ( c1 + c2 ) );
1351 NR::Point const a2( .5 * ( a1 + c12 ) );
1352 NR::Point const b1( .5 * ( c12 + b2 ) );
1353 NR::Point const midpoint( .5 * ( a2 + b1 ) );
1354 double const rec_threshold = .625 * threshold;
1355 ret = bezier_len(c0, a1, a2, midpoint, rec_threshold) + bezier_len(midpoint, b1, b2, c3, rec_threshold);
1356 if (!(lbound - 1e-2 <= ret && ret <= ubound + 1e-2)) {
1357 using NR::X; using NR::Y;
1358 g_warning("ret=%f outside of expected bounds [%f, %f] for {(%.0f %.0f) (%.0f %.0f) (%.0f %.0f) (%.0f %.0f)}",
1359 ret, lbound, ubound, c0[X], c0[Y], c1[X], c1[Y], c2[X], c2[Y], c3[X], c3[Y]);
1360 }
1361 }
1362 return ret;
1363 }
1365 /**
1366 * Returns total length of curve, excluding length of closepath segments.
1367 */
1368 double
1369 sp_curve_distance_including_space(SPCurve const *const curve, double seg2len[])
1370 {
1371 g_return_val_if_fail(curve != NULL, 0.);
1373 double ret = 0.0;
1375 if ( curve->_bpath->code == NR_END ) {
1376 return ret;
1377 }
1379 NR::Point prev(curve->_bpath->c(3));
1380 for (guint i = 1; i < curve->_end; ++i) {
1381 NArtBpath &p = curve->_bpath[i];
1382 double seg_len = 0;
1383 switch (p.code) {
1384 case NR_MOVETO_OPEN:
1385 case NR_MOVETO:
1386 case NR_LINETO:
1387 seg_len = L2(p.c(3) - prev);
1388 break;
1390 case NR_CURVETO:
1391 seg_len = bezier_len(prev, p.c(1), p.c(2), p.c(3), 1.);
1392 break;
1394 case NR_END:
1395 return ret;
1396 }
1397 seg2len[i - 1] = seg_len;
1398 ret += seg_len;
1399 prev = p.c(3);
1400 }
1401 g_assert(!(ret < 0));
1402 return ret;
1403 }
1405 /**
1406 * Like sp_curve_distance_including_space(), but ensures that the
1407 * result >= 1e-18: uses 1 per segment if necessary.
1408 */
1409 double
1410 sp_curve_nonzero_distance_including_space(SPCurve const *const curve, double seg2len[])
1411 {
1412 double const real_dist(sp_curve_distance_including_space(curve, seg2len));
1413 if (real_dist >= 1e-18) {
1414 return real_dist;
1415 } else {
1416 unsigned const nSegs = SP_CURVE_LENGTH(curve) - 1;
1417 for (unsigned i = 0; i < nSegs; ++i) {
1418 seg2len[i] = 1.;
1419 }
1420 return (double) nSegs;
1421 }
1422 }
1424 /**
1425 * 2GEOMified
1426 */
1427 void
1428 SPCurve::stretch_endpoints(NR::Point const &new_p0, NR::Point const &new_p1)
1429 {
1430 if (is_empty()) {
1431 return;
1432 }
1433 g_assert(unsigned(SP_CURVE_LENGTH(this)) + 1 == sp_bpath_length(_bpath));
1434 unsigned const nSegs = SP_CURVE_LENGTH(this) - 1;
1435 g_assert(nSegs != 0);
1436 double *const seg2len = new double[nSegs];
1437 double const tot_len = sp_curve_nonzero_distance_including_space(this, seg2len);
1438 NR::Point const offset0( new_p0 - first_point() );
1439 NR::Point const offset1( new_p1 - last_point() );
1440 _bpath->setC(3, new_p0);
1441 double begin_dist = 0.;
1442 for (unsigned si = 0; si < nSegs; ++si) {
1443 double const end_dist = begin_dist + seg2len[si];
1444 NArtBpath &p = _bpath[1 + si];
1445 switch (p.code) {
1446 case NR_LINETO:
1447 case NR_MOVETO:
1448 case NR_MOVETO_OPEN:
1449 p.setC(3, p.c(3) + NR::Lerp(end_dist / tot_len, offset0, offset1));
1450 break;
1452 case NR_CURVETO:
1453 for (unsigned ci = 1; ci <= 3; ++ci) {
1454 p.setC(ci, p.c(ci) + Lerp((begin_dist + ci * seg2len[si] / 3.) / tot_len, offset0, offset1));
1455 }
1456 break;
1458 default:
1459 g_assert_not_reached();
1460 }
1462 begin_dist = end_dist;
1463 }
1464 g_assert(L1(_bpath[nSegs].c(3) - new_p1) < 1.);
1465 /* Explicit set for better numerical properties. */
1466 _bpath[nSegs].setC(3, new_p1);
1467 delete [] seg2len;
1469 Geom::Piecewise<Geom::D2<Geom::SBasis> > pwd2 = _pathv.front().toPwSb();
1470 Geom::Piecewise<Geom::SBasis> arclength = Geom::arcLengthSb(pwd2);
1471 if ( arclength.lastValue() <= 0 ) {
1472 g_error("SPCurve::stretch_endpoints - arclength <= 0");
1473 throw;
1474 }
1475 arclength *= 1./arclength.lastValue();
1476 Geom::Point const A( to_2geom(offset0) );
1477 Geom::Point const B( to_2geom(offset1) );
1478 Geom::Piecewise<Geom::SBasis> offsetx = (arclength*-1.+1)*A[0] + arclength*B[0];
1479 Geom::Piecewise<Geom::SBasis> offsety = (arclength*-1.+1)*A[1] + arclength*B[1];
1480 Geom::Piecewise<Geom::D2<Geom::SBasis> > offsetpath = Geom::sectionize( Geom::D2<Geom::Piecewise<Geom::SBasis> >(offsetx, offsety) );
1481 pwd2 += offsetpath;
1482 _pathv = Geom::path_from_piecewise( pwd2, 0.001 );
1484 debug_check("SPCurve::stretch_endpoints", this);
1485 }
1487 /**
1488 * sets start of first path to new_p0, and end of first path to new_p1
1489 * 2GEOMified
1490 */
1491 void
1492 SPCurve::move_endpoints(NR::Point const &new_p0, NR::Point const &new_p1)
1493 {
1494 if (is_empty()) {
1495 return;
1496 }
1497 unsigned const nSegs = SP_CURVE_LENGTH(this) - 1;
1498 g_assert(nSegs != 0);
1500 _bpath->setC(3, new_p0);
1501 _bpath[nSegs].setC(3, new_p1);
1503 _pathv.front().setInitial(to_2geom(new_p0));
1504 _pathv.front().setFinal(to_2geom(new_p1));
1506 debug_check("SPCurve::move_endpoints", this);
1507 }
1509 /**
1510 * returns the number of nodes in a path, used for statusbar text when selecting an spcurve.
1511 * 2GEOMified
1512 */
1513 guint
1514 SPCurve::nodes_in_path() const
1515 {
1516 gint r = _end;
1517 gint i = _length - 1;
1518 if (i > r) i = r; // sometimes after switching from node editor length is wrong, e.g. f6 - draw - f2 - tab - f1, this fixes it
1519 for (; i >= 0; i --)
1520 if (_bpath[i].code == NR_MOVETO)
1521 r --;
1523 guint nr = 0;
1524 for(Geom::PathVector::const_iterator it = _pathv.begin(); it != _pathv.end(); ++it) {
1525 nr += (*it).size();
1527 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
1528 }
1530 debug_check("SPCurve::nodes_in_path", r == (gint)nr);
1532 return r;
1533 }
1535 /**
1536 * Adds p to the last point (and last handle if present) of the last path
1537 * 2GEOMified
1538 */
1539 void
1540 SPCurve::last_point_additive_move(Geom::Point const & p)
1541 {
1542 if (is_empty()) {
1543 return;
1544 }
1545 if (_end == 0) {
1546 return;
1547 }
1548 NArtBpath * path = _bpath + _end - 1;
1550 if (path->code == NR_CURVETO) {
1551 path->x2 += p[Geom::X];
1552 path->y2 += p[Geom::Y];
1553 }
1554 path->x3 += p[Geom::X];
1555 path->y3 += p[Geom::Y];
1557 _pathv.back().setFinal( _pathv.back().finalPoint() + p );
1559 // Move handle as well when the last segment is a cubic bezier segment:
1560 // TODO: what to do for quadratic beziers?
1561 if ( Geom::CubicBezier const *lastcube = dynamic_cast<Geom::CubicBezier const *>(&_pathv.back().back()) ) {
1562 Geom::CubicBezier newcube( *lastcube );
1563 newcube.setPoint(2, newcube[2] + p);
1564 _pathv.back().replace( --_pathv.back().end(), newcube );
1565 }
1567 debug_check("SPCurve::last_point_additive_move", this);
1568 }
1570 /*
1571 Local Variables:
1572 mode:c++
1573 c-file-style:"stroustrup"
1574 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
1575 indent-tabs-mode:nil
1576 fill-column:99
1577 End:
1578 */
1579 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :