52ffe4f5b9a4b73acc2dc034ec260e5f179e5c66
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>=0; i--) {
55 c->lineto(rect.corner(i));
56 }
57 c->closepath_current();
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 */
204 void
205 SPCurve::moveto(Geom::Point const &p)
206 {
207 _pathv.push_back( Geom::Path() ); // for some reason Geom::Path(p) does not work...
208 _pathv.back().start(p);
209 }
211 /**
212 * Calls SPCurve::lineto() with a point's coordinates.
213 */
214 void
215 SPCurve::lineto(Geom::Point const &p)
216 {
217 if (_pathv.empty()) g_message("SPCurve::lineto path is empty!");
218 else _pathv.back().appendNew<Geom::LineSegment>( p );
219 }
220 /**
221 * Adds a line to the current subpath.
222 */
223 void
224 SPCurve::lineto(gdouble x, gdouble y)
225 {
226 lineto(Geom::Point(x,y));
227 }
229 /**
230 * Calls SPCurve::curveto() with coordinates of three points.
231 */
232 void
233 SPCurve::curveto(Geom::Point const &p0, Geom::Point const &p1, Geom::Point const &p2)
234 {
235 if (_pathv.empty()) g_message("SPCurve::lineto path is empty!");
236 else _pathv.back().appendNew<Geom::CubicBezier>( p0, p1, p2 );
237 }
238 /**
239 * Adds a bezier segment to the current subpath.
240 */
241 void
242 SPCurve::curveto(gdouble x0, gdouble y0, gdouble x1, gdouble y1, gdouble x2, gdouble y2)
243 {
244 curveto( Geom::Point(x0,y0), Geom::Point(x1,y1), Geom::Point(x2,y2) );
245 }
247 /**
248 * Close current subpath by possibly adding a line between start and end.
249 */
250 void
251 SPCurve::closepath()
252 {
253 _pathv.back().close(true);
254 }
256 /** Like SPCurve::closepath() but sets the end point of the last subpath
257 to the subpath start point instead of adding a new lineto.
259 Used for freehand drawing when the user draws back to the start point.
260 **/
261 void
262 SPCurve::closepath_current()
263 {
264 _pathv.back().setFinal(_pathv.back().initialPoint());
265 _pathv.back().close(true);
266 }
268 /**
269 * True if no paths are in curve. If it only contains a path with only a moveto, the path is considered NON-empty
270 */
271 bool
272 SPCurve::is_empty() const
273 {
274 return _pathv.empty();
275 }
277 /**
278 * True iff all subpaths are closed.
279 * Returns false if the curve is empty.
280 */
281 bool
282 SPCurve::is_closed() const
283 {
284 if (is_empty()) {
285 return false;
286 } else {
287 bool closed = true;
288 for (Geom::PathVector::const_iterator it = _pathv.begin(); it != _pathv.end(); it++) {
289 if ( ! it->closed() ) {
290 closed = false;
291 break;
292 }
293 }
294 return closed;
295 }
296 }
298 /**
299 * Return last pathsegment (possibly the closing path segment) of the last path in PathVector or NULL.
300 * If the last path is empty (contains only a moveto), the function returns NULL
301 */
302 Geom::Curve const *
303 SPCurve::last_segment() const
304 {
305 if (is_empty()) {
306 return NULL;
307 }
308 if (_pathv.back().empty()) {
309 return NULL;
310 }
312 return &_pathv.back().back_default();
313 }
315 /**
316 * Return last path in PathVector or NULL.
317 */
318 Geom::Path const *
319 SPCurve::last_path() const
320 {
321 if (is_empty()) {
322 return NULL;
323 }
325 return &_pathv.back();
326 }
328 /**
329 * Return first pathsegment in PathVector or NULL.
330 * equal in functionality to SPCurve::first_bpath()
331 */
332 Geom::Curve const *
333 SPCurve::first_segment() const
334 {
335 if (is_empty()) {
336 return NULL;
337 }
338 if (_pathv.front().empty()) {
339 return NULL;
340 }
342 return &_pathv.front().front();
343 }
345 /**
346 * Return first path in PathVector or NULL.
347 */
348 Geom::Path const *
349 SPCurve::first_path() const
350 {
351 if (is_empty()) {
352 return NULL;
353 }
355 return &_pathv.front();
356 }
358 /**
359 * Return first point of first subpath or nothing when the path is empty.
360 */
361 boost::optional<Geom::Point>
362 SPCurve::first_point() const
363 {
364 boost::optional<Geom::Point> retval;
366 if (!is_empty()) {
367 retval = _pathv.front().initialPoint();
368 }
370 return retval;
371 }
373 /**
374 * Return the second point of first subpath or _movePos if curve too short.
375 * If the pathvector is empty, this returns nothing. If the first path is only a moveto, this method
376 * returns the first point of the second path, if it exists. If there is no 2nd path, it returns the
377 * first point of the first path.
378 */
379 boost::optional<Geom::Point>
380 SPCurve::second_point() const
381 {
382 boost::optional<Geom::Point> retval;
383 if (!is_empty()) {
384 if (_pathv.front().empty()) {
385 // first path is only a moveto
386 // check if there is second path
387 if (_pathv.size() > 1) {
388 retval = _pathv[1].initialPoint();
389 } else {
390 retval = _pathv[0].initialPoint();
391 }
392 } else {
393 retval = _pathv.front()[0].finalPoint();
394 }
395 }
397 return retval;
398 }
400 /**
401 * Return the second-last point of last subpath or first point when that last subpath has only a moveto.
402 */
403 boost::optional<Geom::Point>
404 SPCurve::penultimate_point() const
405 {
406 boost::optional<Geom::Point> retval;
407 if (!is_empty()) {
408 Geom::Path const &lastpath = _pathv.back();
409 if (!lastpath.empty()) {
410 Geom::Curve const &back = lastpath.back_default();
411 retval = back.initialPoint();
412 } else {
413 retval = lastpath.initialPoint();
414 }
415 }
417 return retval;
418 }
420 /**
421 * Return last point of last subpath or nothing when the curve is empty.
422 * If the last path is only a moveto, then return that point.
423 */
424 boost::optional<Geom::Point>
425 SPCurve::last_point() const
426 {
427 boost::optional<Geom::Point> retval;
429 if (!is_empty()) {
430 retval = _pathv.back().finalPoint();
431 }
433 return retval;
434 }
436 /**
437 * Returns a *new* \a curve but drawn in the opposite direction.
438 * Should result in the same shape, but
439 * with all its markers drawn facing the other direction.
440 * Reverses the order of subpaths as well
441 **/
442 SPCurve *
443 SPCurve::create_reverse() const
444 {
445 SPCurve *new_curve = new SPCurve(Geom::reverse_paths_and_order(_pathv));
447 return new_curve;
448 }
450 /**
451 * Append \a curve2 to \a this.
452 * If \a use_lineto is false, simply add all paths in \a curve2 to \a this;
453 * 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.
454 */
455 void
456 SPCurve::append(SPCurve const *curve2,
457 bool use_lineto)
458 {
459 if (curve2->is_empty())
460 return;
462 if (use_lineto) {
463 Geom::PathVector::const_iterator it = curve2->_pathv.begin();
464 if ( ! _pathv.empty() ) {
465 Geom::Path & lastpath = _pathv.back();
466 lastpath.appendNew<Geom::LineSegment>( (*it).initialPoint() );
467 lastpath.append( (*it) );
468 } else {
469 _pathv.push_back( (*it) );
470 }
472 for (it++; it != curve2->_pathv.end(); it++) {
473 _pathv.push_back( (*it) );
474 }
475 } else {
476 for (Geom::PathVector::const_iterator it = curve2->_pathv.begin(); it != curve2->_pathv.end(); it++) {
477 _pathv.push_back( (*it) );
478 }
479 }
480 }
482 /**
483 * 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,
484 * 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.
485 * 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.
486 * When one of the curves is empty, this curves path becomes the non-empty path.
487 */
488 SPCurve *
489 SPCurve::append_continuous(SPCurve const *c1, gdouble tolerance)
490 {
491 using Geom::X;
492 using Geom::Y;
494 g_return_val_if_fail(c1 != NULL, NULL);
495 if ( this->is_closed() || c1->is_closed() ) {
496 return NULL;
497 }
499 if (c1->is_empty()) {
500 return this;
501 }
503 if (this->is_empty()) {
504 _pathv = c1->_pathv;
505 return this;
506 }
508 if ( (fabs((*this->last_point())[X] - (*c1->first_point())[X]) <= tolerance)
509 && (fabs((*this->last_point())[Y] - (*c1->first_point())[Y]) <= tolerance) )
510 {
511 // c1's first subpath can be appended to this curve's last subpath
512 Geom::PathVector::const_iterator path_it = c1->_pathv.begin();
513 Geom::Path & lastpath = _pathv.back();
515 Geom::Path newfirstpath(*path_it);
516 newfirstpath.setInitial(lastpath.finalPoint());
517 lastpath.append( newfirstpath );
519 for (path_it++; path_it != c1->_pathv.end(); path_it++) {
520 _pathv.push_back( (*path_it) );
521 }
523 } else {
524 append(c1, true);
525 }
527 return this;
528 }
530 /**
531 * Remove last segment of curve.
532 * (Only used once in /src/pen-context.cpp)
533 */
534 void
535 SPCurve::backspace()
536 {
537 if ( is_empty() )
538 return;
540 if ( !_pathv.back().empty() ) {
541 _pathv.back().erase_last();
542 _pathv.back().close(false);
543 }
544 }
546 /**
547 * TODO: add comments about what this method does and what assumptions are made and requirements are put on SPCurve
548 (2:08:18 AM) Johan: basically, i convert the path to pw<d2>
549 (2:08:27 AM) Johan: then i calculate an offset path
550 (2:08:29 AM) Johan: to move the knots
551 (2:08:36 AM) Johan: then i add it
552 (2:08:40 AM) Johan: then convert back to path
553 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)
554 */
555 void
556 SPCurve::stretch_endpoints(Geom::Point const &new_p0, Geom::Point const &new_p1)
557 {
558 if (is_empty()) {
559 return;
560 }
562 Geom::Point const offset0( new_p0 - *first_point() );
563 Geom::Point const offset1( new_p1 - *last_point() );
565 Geom::Piecewise<Geom::D2<Geom::SBasis> > pwd2 = _pathv.front().toPwSb();
566 Geom::Piecewise<Geom::SBasis> arclength = Geom::arcLengthSb(pwd2);
567 if ( arclength.lastValue() <= 0 ) {
568 g_error("SPCurve::stretch_endpoints - arclength <= 0");
569 throw;
570 }
571 arclength *= 1./arclength.lastValue();
572 Geom::Point const A( offset0 );
573 Geom::Point const B( offset1 );
574 Geom::Piecewise<Geom::SBasis> offsetx = (arclength*-1.+1)*A[0] + arclength*B[0];
575 Geom::Piecewise<Geom::SBasis> offsety = (arclength*-1.+1)*A[1] + arclength*B[1];
576 Geom::Piecewise<Geom::D2<Geom::SBasis> > offsetpath = Geom::sectionize( Geom::D2<Geom::Piecewise<Geom::SBasis> >(offsetx, offsety) );
577 pwd2 += offsetpath;
578 _pathv = Geom::path_from_piecewise( pwd2, 0.001 );
579 }
581 /**
582 * sets start of first path to new_p0, and end of first path to new_p1
583 */
584 void
585 SPCurve::move_endpoints(Geom::Point const &new_p0, Geom::Point const &new_p1)
586 {
587 if (is_empty()) {
588 return;
589 }
590 _pathv.front().setInitial(new_p0);
591 _pathv.front().setFinal(new_p1);
592 }
594 /**
595 * returns the number of nodes in a path, used for statusbar text when selecting an spcurve.
596 * Sum of nodes in all the paths. When a path is closed, and its closing line segment is of zero-length,
597 * this function will not count the closing knot double (so basically ignores the closing line segment when it has zero length)
598 */
599 guint
600 SPCurve::nodes_in_path() const
601 {
602 guint nr = 0;
603 for(Geom::PathVector::const_iterator it = _pathv.begin(); it != _pathv.end(); ++it) {
604 nr += (*it).size();
606 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
608 if (it->closed()) {
609 Geom::Curve const &c = it->back_closed();
610 if (are_near(c.initialPoint(), c.finalPoint())) {
611 nr--; // do not count closing knot double for zero-length closing line segments
612 }
613 }
614 }
616 return nr;
617 }
619 /**
620 * Adds p to the last point (and last handle if present) of the last path
621 */
622 void
623 SPCurve::last_point_additive_move(Geom::Point const & p)
624 {
625 if (is_empty()) {
626 return;
627 }
629 _pathv.back().setFinal( _pathv.back().finalPoint() + p );
631 // Move handle as well when the last segment is a cubic bezier segment:
632 // TODO: what to do for quadratic beziers?
633 if ( Geom::CubicBezier const *lastcube = dynamic_cast<Geom::CubicBezier const *>(&_pathv.back().back()) ) {
634 Geom::CubicBezier newcube( *lastcube );
635 newcube.setPoint(2, newcube[2] + p);
636 _pathv.back().replace( --_pathv.back().end(), newcube );
637 }
638 }
640 /*
641 Local Variables:
642 mode:c++
643 c-file-style:"stroustrup"
644 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
645 indent-tabs-mode:nil
646 fill-column:99
647 End:
648 */
649 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :