1 #define __CURVE_C__
3 /** \file
4 * Routines for SPCurve and for its Geom::PathVector
5 */
7 /*
8 * Authors:
9 * Lauris Kaplinski <lauris@kaplinski.com>
10 * Johan Engelen
11 *
12 * Copyright (C) 2000 Lauris Kaplinski
13 * Copyright (C) 2000-2001 Ximian, Inc.
14 * Copyright (C) 2002 Lauris Kaplinski
15 * Copyright (C) 2008 Johan Engelen
16 *
17 * Released under GNU GPL
18 */
20 #include "display/curve.h"
22 #include <glib/gmessages.h>
23 #include <2geom/pathvector.h>
24 #include <2geom/sbasis-geometric.h>
25 #include <2geom/sbasis-to-bezier.h>
26 #include <2geom/point.h>
28 /* Constructors */
30 /**
31 * The returned curve's state is as if SPCurve::reset has just been called on it.
32 */
33 SPCurve::SPCurve()
34 : _refcount(1),
35 _pathv()
36 {
37 _pathv.clear();
38 }
40 SPCurve::SPCurve(Geom::PathVector const& pathv)
41 : _refcount(1),
42 _pathv(pathv)
43 {
44 }
46 SPCurve *
47 SPCurve::new_from_rect(Geom::Rect const &rect)
48 {
49 SPCurve *c = new SPCurve();
51 Geom::Point p = rect.corner(0);
52 c->moveto(p);
54 for (int i=3; i>=1; i--) {
55 c->lineto(rect.corner(i));
56 }
57 c->closepath();
59 return c;
60 }
62 SPCurve::~SPCurve()
63 {
64 }
66 /* Methods */
68 void
69 SPCurve::set_pathvector(Geom::PathVector const & new_pathv)
70 {
71 _pathv = new_pathv;
72 }
74 Geom::PathVector const &
75 SPCurve::get_pathvector() const
76 {
77 return _pathv;
78 }
80 /*
81 * Returns the number of segments of all paths summed
82 * This count includes the closing line segment of a closed path.
83 */
84 guint
85 SPCurve::get_segment_count() const
86 {
87 guint nr = 0;
88 for(Geom::PathVector::const_iterator it = _pathv.begin(); it != _pathv.end(); ++it) {
89 nr += (*it).size();
91 if (it->closed()) nr += 1;
92 }
93 return nr;
94 }
96 /**
97 * Increase _refcount of curve.
98 *
99 * \todo should this be shared with other refcounting code?
100 */
101 SPCurve *
102 SPCurve::ref()
103 {
104 _refcount += 1;
106 return this;
107 }
109 /**
110 * Decrease refcount of curve, with possible destruction.
111 *
112 * \todo should this be shared with other refcounting code?
113 */
114 SPCurve *
115 SPCurve::unref()
116 {
117 _refcount -= 1;
119 if (_refcount < 1) {
120 delete this;
121 }
123 return NULL;
124 }
126 /**
127 * Create new curve from this curve's pathvector array.
128 */
129 SPCurve *
130 SPCurve::copy() const
131 {
132 return new SPCurve(_pathv);
133 }
135 /**
136 * Return new curve that is the concatenation of all curves in list.
137 */
138 SPCurve *
139 SPCurve::concat(GSList const *list)
140 {
141 SPCurve *new_curve = new SPCurve();
143 for (GSList const *l = list; l != NULL; l = l->next) {
144 SPCurve *c = (SPCurve *) l->data;
145 new_curve->_pathv.insert( new_curve->_pathv.end(), c->get_pathvector().begin(), c->get_pathvector().end() );
146 }
148 return new_curve;
149 }
151 /**
152 * Returns a list of new curves corresponding to the subpaths in \a curve.
153 * 2geomified
154 */
155 GSList *
156 SPCurve::split() const
157 {
158 GSList *l = NULL;
160 for (Geom::PathVector::const_iterator path_it = _pathv.begin(); path_it != _pathv.end(); ++path_it) {
161 Geom::PathVector newpathv;
162 newpathv.push_back(*path_it);
163 SPCurve * newcurve = new SPCurve(newpathv);
164 l = g_slist_prepend(l, newcurve);
165 }
167 return l;
168 }
170 /**
171 * Transform all paths in curve using matrix.
172 */
173 void
174 SPCurve::transform(Geom::Matrix const &m)
175 {
176 _pathv *= m;
177 }
179 /**
180 * Set curve to empty curve.
181 * In more detail: this clears the internal pathvector from all its paths.
182 */
183 void
184 SPCurve::reset()
185 {
186 _pathv.clear();
187 }
189 /** Several consecutive movetos are ALLOWED
190 * Ref: http://www.w3.org/TR/SVG11/implnote.html#PathElementImplementationNotes
191 * (first subitem of the item about zero-length path segments) */
193 /**
194 * Calls SPCurve::moveto() with point made of given coordinates.
195 */
196 void
197 SPCurve::moveto(gdouble x, gdouble y)
198 {
199 moveto(Geom::Point(x, y));
200 }
201 /**
202 * Perform a moveto to a point, thus starting a new subpath.
203 * Point p must be finite.
204 */
205 void
206 SPCurve::moveto(Geom::Point const &p)
207 {
208 _pathv.push_back( Geom::Path() ); // for some reason Geom::Path(p) does not work...
209 _pathv.back().start(p);
210 }
212 /**
213 * Adds a line to the current subpath.
214 * Point p must be finite.
215 */
216 void
217 SPCurve::lineto(Geom::Point const &p)
218 {
219 if (_pathv.empty()) g_message("SPCurve::lineto - path is empty!");
220 else _pathv.back().appendNew<Geom::LineSegment>( p );
221 }
222 /**
223 * Calls SPCurve::lineto( Geom::Point(x,y) )
224 */
225 void
226 SPCurve::lineto(gdouble x, gdouble y)
227 {
228 lineto(Geom::Point(x,y));
229 }
231 /**
232 * Adds a quadratic bezier segment to the current subpath.
233 * All points must be finite.
234 */
235 void
236 SPCurve::quadto(Geom::Point const &p1, Geom::Point const &p2)
237 {
238 if (_pathv.empty()) g_message("SPCurve::quadto - path is empty!");
239 else _pathv.back().appendNew<Geom::QuadraticBezier>( p1, p2);
240 }
241 /**
242 * Calls SPCurve::quadto( Geom::Point(x1,y1), Geom::Point(x2,y2) )
243 * All coordinates must be finite.
244 */
245 void
246 SPCurve::quadto(gdouble x1, gdouble y1, gdouble x2, gdouble y2)
247 {
248 quadto( Geom::Point(x1,y1), Geom::Point(x2,y2) );
249 }
251 /**
252 * Adds a bezier segment to the current subpath.
253 * All points must be finite.
254 */
255 void
256 SPCurve::curveto(Geom::Point const &p0, Geom::Point const &p1, Geom::Point const &p2)
257 {
258 if (_pathv.empty()) g_message("SPCurve::curveto - path is empty!");
259 else _pathv.back().appendNew<Geom::CubicBezier>( p0, p1, p2 );
260 }
261 /**
262 * Calls SPCurve::curveto( Geom::Point(x0,y0), Geom::Point(x1,y1), Geom::Point(x2,y2) )
263 * All coordinates must be finite.
264 */
265 void
266 SPCurve::curveto(gdouble x0, gdouble y0, gdouble x1, gdouble y1, gdouble x2, gdouble y2)
267 {
268 curveto( Geom::Point(x0,y0), Geom::Point(x1,y1), Geom::Point(x2,y2) );
269 }
271 /**
272 * Close current subpath by possibly adding a line between start and end.
273 */
274 void
275 SPCurve::closepath()
276 {
277 _pathv.back().close(true);
278 }
280 /** Like SPCurve::closepath() but sets the end point of the last subpath
281 to the subpath start point instead of adding a new lineto.
283 Used for freehand drawing when the user draws back to the start point.
284 **/
285 void
286 SPCurve::closepath_current()
287 {
288 if (_pathv.back().size() > 0 && dynamic_cast<Geom::LineSegment const *>(&_pathv.back().back_open())) {
289 _pathv.back().erase_last();
290 } else {
291 _pathv.back().setFinal(_pathv.back().initialPoint());
292 }
293 _pathv.back().close(true);
294 }
296 /**
297 * True if no paths are in curve. If it only contains a path with only a moveto, the path is considered NON-empty
298 */
299 bool
300 SPCurve::is_empty() const
301 {
302 return _pathv.empty();
303 }
305 /**
306 * True iff all subpaths are closed.
307 * Returns false if the curve is empty.
308 */
309 bool
310 SPCurve::is_closed() const
311 {
312 if (is_empty()) {
313 return false;
314 } else {
315 bool closed = true;
316 for (Geom::PathVector::const_iterator it = _pathv.begin(); it != _pathv.end(); it++) {
317 if ( ! it->closed() ) {
318 closed = false;
319 break;
320 }
321 }
322 return closed;
323 }
324 }
326 /**
327 * Return last pathsegment (possibly the closing path segment) of the last path in PathVector or NULL.
328 * If the last path is empty (contains only a moveto), the function returns NULL
329 */
330 Geom::Curve const *
331 SPCurve::last_segment() const
332 {
333 if (is_empty()) {
334 return NULL;
335 }
336 if (_pathv.back().empty()) {
337 return NULL;
338 }
340 return &_pathv.back().back_default();
341 }
343 /**
344 * Return last path in PathVector or NULL.
345 */
346 Geom::Path const *
347 SPCurve::last_path() const
348 {
349 if (is_empty()) {
350 return NULL;
351 }
353 return &_pathv.back();
354 }
356 /**
357 * Return first pathsegment in PathVector or NULL.
358 * equal in functionality to SPCurve::first_bpath()
359 */
360 Geom::Curve const *
361 SPCurve::first_segment() const
362 {
363 if (is_empty()) {
364 return NULL;
365 }
366 if (_pathv.front().empty()) {
367 return NULL;
368 }
370 return &_pathv.front().front();
371 }
373 /**
374 * Return first path in PathVector or NULL.
375 */
376 Geom::Path const *
377 SPCurve::first_path() const
378 {
379 if (is_empty()) {
380 return NULL;
381 }
383 return &_pathv.front();
384 }
386 /**
387 * Return first point of first subpath or nothing when the path is empty.
388 */
389 boost::optional<Geom::Point>
390 SPCurve::first_point() const
391 {
392 boost::optional<Geom::Point> retval;
394 if (!is_empty()) {
395 retval = _pathv.front().initialPoint();
396 }
398 return retval;
399 }
401 /**
402 * Return the second point of first subpath or _movePos if curve too short.
403 * If the pathvector is empty, this returns nothing. If the first path is only a moveto, this method
404 * returns the first point of the second path, if it exists. If there is no 2nd path, it returns the
405 * first point of the first path.
406 */
407 boost::optional<Geom::Point>
408 SPCurve::second_point() const
409 {
410 boost::optional<Geom::Point> retval;
411 if (!is_empty()) {
412 if (_pathv.front().empty()) {
413 // first path is only a moveto
414 // check if there is second path
415 if (_pathv.size() > 1) {
416 retval = _pathv[1].initialPoint();
417 } else {
418 retval = _pathv[0].initialPoint();
419 }
420 } else {
421 retval = _pathv.front()[0].finalPoint();
422 }
423 }
425 return retval;
426 }
428 /**
429 * Return the second-last point of last subpath or first point when that last subpath has only a moveto.
430 */
431 boost::optional<Geom::Point>
432 SPCurve::penultimate_point() const
433 {
434 boost::optional<Geom::Point> retval;
435 if (!is_empty()) {
436 Geom::Path const &lastpath = _pathv.back();
437 if (!lastpath.empty()) {
438 Geom::Curve const &back = lastpath.back_default();
439 retval = back.initialPoint();
440 } else {
441 retval = lastpath.initialPoint();
442 }
443 }
445 return retval;
446 }
448 /**
449 * Return last point of last subpath or nothing when the curve is empty.
450 * If the last path is only a moveto, then return that point.
451 */
452 boost::optional<Geom::Point>
453 SPCurve::last_point() const
454 {
455 boost::optional<Geom::Point> retval;
457 if (!is_empty()) {
458 retval = _pathv.back().finalPoint();
459 }
461 return retval;
462 }
464 /**
465 * Returns a *new* \a curve but drawn in the opposite direction.
466 * Should result in the same shape, but
467 * with all its markers drawn facing the other direction.
468 * Reverses the order of subpaths as well
469 **/
470 SPCurve *
471 SPCurve::create_reverse() const
472 {
473 SPCurve *new_curve = new SPCurve(Geom::reverse_paths_and_order(_pathv));
475 return new_curve;
476 }
478 /**
479 * Append \a curve2 to \a this.
480 * If \a use_lineto is false, simply add all paths in \a curve2 to \a this;
481 * 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.
482 */
483 void
484 SPCurve::append(SPCurve const *curve2,
485 bool use_lineto)
486 {
487 if (curve2->is_empty())
488 return;
490 if (use_lineto) {
491 Geom::PathVector::const_iterator it = curve2->_pathv.begin();
492 if ( ! _pathv.empty() ) {
493 Geom::Path & lastpath = _pathv.back();
494 lastpath.appendNew<Geom::LineSegment>( (*it).initialPoint() );
495 lastpath.append( (*it) );
496 } else {
497 _pathv.push_back( (*it) );
498 }
500 for (it++; it != curve2->_pathv.end(); it++) {
501 _pathv.push_back( (*it) );
502 }
503 } else {
504 for (Geom::PathVector::const_iterator it = curve2->_pathv.begin(); it != curve2->_pathv.end(); it++) {
505 _pathv.push_back( (*it) );
506 }
507 }
508 }
510 /**
511 * Append \a c1 to \a this with possible fusing of close endpoints. If the end of this curve and the start of c1 are within tolerance distance,
512 * then the startpoint of c1 is moved to the end of this curve and the first subpath of c1 is appended to the last subpath of this curve.
513 * When one of the curves (this curve or the argument curve) is closed, the returned value is NULL; otherwise the returned value is this curve.
514 * When one of the curves is empty, this curves path becomes the non-empty path.
515 */
516 SPCurve *
517 SPCurve::append_continuous(SPCurve const *c1, gdouble tolerance)
518 {
519 using Geom::X;
520 using Geom::Y;
522 g_return_val_if_fail(c1 != NULL, NULL);
523 if ( this->is_closed() || c1->is_closed() ) {
524 return NULL;
525 }
527 if (c1->is_empty()) {
528 return this;
529 }
531 if (this->is_empty()) {
532 _pathv = c1->_pathv;
533 return this;
534 }
536 if ( (fabs((*this->last_point())[X] - (*c1->first_point())[X]) <= tolerance)
537 && (fabs((*this->last_point())[Y] - (*c1->first_point())[Y]) <= tolerance) )
538 {
539 // c1's first subpath can be appended to this curve's last subpath
540 Geom::PathVector::const_iterator path_it = c1->_pathv.begin();
541 Geom::Path & lastpath = _pathv.back();
543 Geom::Path newfirstpath(*path_it);
544 newfirstpath.setInitial(lastpath.finalPoint());
545 lastpath.append( newfirstpath );
547 for (path_it++; path_it != c1->_pathv.end(); path_it++) {
548 _pathv.push_back( (*path_it) );
549 }
551 } else {
552 append(c1, true);
553 }
555 return this;
556 }
558 /**
559 * Remove last segment of curve.
560 * (Only used once in /src/pen-context.cpp)
561 */
562 void
563 SPCurve::backspace()
564 {
565 if ( is_empty() )
566 return;
568 if ( !_pathv.back().empty() ) {
569 _pathv.back().erase_last();
570 _pathv.back().close(false);
571 }
572 }
574 /**
575 * TODO: add comments about what this method does and what assumptions are made and requirements are put on SPCurve
576 (2:08:18 AM) Johan: basically, i convert the path to pw<d2>
577 (2:08:27 AM) Johan: then i calculate an offset path
578 (2:08:29 AM) Johan: to move the knots
579 (2:08:36 AM) Johan: then i add it
580 (2:08:40 AM) Johan: then convert back to path
581 If I remember correctly, this moves the firstpoint to new_p0, and the lastpoint to new_p1, and moves all nodes in between according to their arclength (interpolates the movement amount)
582 */
583 void
584 SPCurve::stretch_endpoints(Geom::Point const &new_p0, Geom::Point const &new_p1)
585 {
586 if (is_empty()) {
587 return;
588 }
590 Geom::Point const offset0( new_p0 - *first_point() );
591 Geom::Point const offset1( new_p1 - *last_point() );
593 Geom::Piecewise<Geom::D2<Geom::SBasis> > pwd2 = _pathv.front().toPwSb();
594 Geom::Piecewise<Geom::SBasis> arclength = Geom::arcLengthSb(pwd2);
595 if ( arclength.lastValue() <= 0 ) {
596 g_error("SPCurve::stretch_endpoints - arclength <= 0");
597 throw;
598 }
599 arclength *= 1./arclength.lastValue();
600 Geom::Point const A( offset0 );
601 Geom::Point const B( offset1 );
602 Geom::Piecewise<Geom::SBasis> offsetx = (arclength*-1.+1)*A[0] + arclength*B[0];
603 Geom::Piecewise<Geom::SBasis> offsety = (arclength*-1.+1)*A[1] + arclength*B[1];
604 Geom::Piecewise<Geom::D2<Geom::SBasis> > offsetpath = Geom::sectionize( Geom::D2<Geom::Piecewise<Geom::SBasis> >(offsetx, offsety) );
605 pwd2 += offsetpath;
606 _pathv = Geom::path_from_piecewise( pwd2, 0.001 );
607 }
609 /**
610 * sets start of first path to new_p0, and end of first path to new_p1
611 */
612 void
613 SPCurve::move_endpoints(Geom::Point const &new_p0, Geom::Point const &new_p1)
614 {
615 if (is_empty()) {
616 return;
617 }
618 _pathv.front().setInitial(new_p0);
619 _pathv.front().setFinal(new_p1);
620 }
622 /**
623 * returns the number of nodes in a path, used for statusbar text when selecting an spcurve.
624 * Sum of nodes in all the paths. When a path is closed, and its closing line segment is of zero-length,
625 * this function will not count the closing knot double (so basically ignores the closing line segment when it has zero length)
626 */
627 guint
628 SPCurve::nodes_in_path() const
629 {
630 guint nr = 0;
631 for(Geom::PathVector::const_iterator it = _pathv.begin(); it != _pathv.end(); ++it) {
632 nr += (*it).size();
634 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
636 // do not count closing knot double for zero-length closing line segments
637 // however, if the path is only a moveto, and is closed, do not subtract 1 (otherwise the result will be zero nodes)
638 if ( it->closed()
639 && ((*it).size() != 0) )
640 {
641 Geom::Curve const &c = it->back_closed();
642 if (are_near(c.initialPoint(), c.finalPoint())) {
643 nr--;
644 }
645 }
646 }
648 return nr;
649 }
651 /**
652 * Adds p to the last point (and last handle if present) of the last path
653 */
654 void
655 SPCurve::last_point_additive_move(Geom::Point const & p)
656 {
657 if (is_empty()) {
658 return;
659 }
661 _pathv.back().setFinal( _pathv.back().finalPoint() + p );
663 // Move handle as well when the last segment is a cubic bezier segment:
664 // TODO: what to do for quadratic beziers?
665 if ( Geom::CubicBezier const *lastcube = dynamic_cast<Geom::CubicBezier const *>(&_pathv.back().back()) ) {
666 Geom::CubicBezier newcube( *lastcube );
667 newcube.setPoint(2, newcube[2] + p);
668 _pathv.back().replace( --_pathv.back().end(), newcube );
669 }
670 }
672 /*
673 Local Variables:
674 mode:c++
675 c-file-style:"stroustrup"
676 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
677 indent-tabs-mode:nil
678 fill-column:99
679 End:
680 */
681 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :