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 lineto(p[Geom::X], p[Geom::Y]);
218 }
219 /**
220 * Adds a line to the current subpath.
221 */
222 void
223 SPCurve::lineto(gdouble x, gdouble y)
224 {
225 if (_pathv.empty()) g_message("leeg");
226 else _pathv.back().appendNew<Geom::LineSegment>( 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 using Geom::X;
236 using Geom::Y;
237 curveto( p0[X], p0[Y],
238 p1[X], p1[Y],
239 p2[X], p2[Y] );
240 }
241 /**
242 * Adds a bezier segment to the current subpath.
243 */
244 void
245 SPCurve::curveto(gdouble x0, gdouble y0, gdouble x1, gdouble y1, gdouble x2, gdouble y2)
246 {
247 if (_pathv.empty()) g_message("leeg");
248 else _pathv.back().appendNew<Geom::CubicBezier>( Geom::Point(x0,y0), Geom::Point(x1,y1), Geom::Point(x2,y2) );
249 }
251 /**
252 * Close current subpath by possibly adding a line between start and end.
253 */
254 void
255 SPCurve::closepath()
256 {
257 _pathv.back().close(true);
258 }
260 /** Like SPCurve::closepath() but sets the end point of the last subpath
261 to the subpath start point instead of adding a new lineto.
263 Used for freehand drawing when the user draws back to the start point.
264 **/
265 void
266 SPCurve::closepath_current()
267 {
268 _pathv.back().setFinal(_pathv.back().initialPoint());
269 _pathv.back().close(true);
270 }
272 /**
273 * True if no paths are in curve. If it only contains a path with only a moveto, the path is considered NON-empty
274 */
275 bool
276 SPCurve::is_empty() const
277 {
278 return _pathv.empty();
279 }
281 /**
282 * True iff all subpaths are closed.
283 * Returns false if the curve is empty.
284 */
285 bool
286 SPCurve::is_closed() const
287 {
288 if (is_empty()) {
289 return false;
290 } else {
291 bool closed = true;
292 for (Geom::PathVector::const_iterator it = _pathv.begin(); it != _pathv.end(); it++) {
293 if ( ! it->closed() ) {
294 closed = false;
295 break;
296 }
297 }
298 return closed;
299 }
300 }
302 /**
303 * Return last pathsegment (possibly the closing path segment) of the last path in PathVector or NULL.
304 * If the last path is empty (contains only a moveto), the function returns NULL
305 */
306 Geom::Curve const *
307 SPCurve::last_segment() const
308 {
309 if (is_empty()) {
310 return NULL;
311 }
312 if (_pathv.back().empty()) {
313 return NULL;
314 }
316 return &_pathv.back().back_default();
317 }
319 /**
320 * Return last path in PathVector or NULL.
321 */
322 Geom::Path const *
323 SPCurve::last_path() const
324 {
325 if (is_empty()) {
326 return NULL;
327 }
329 return &_pathv.back();
330 }
332 /**
333 * Return first pathsegment in PathVector or NULL.
334 * equal in functionality to SPCurve::first_bpath()
335 */
336 Geom::Curve const *
337 SPCurve::first_segment() const
338 {
339 if (is_empty()) {
340 return NULL;
341 }
342 if (_pathv.front().empty()) {
343 return NULL;
344 }
346 return &_pathv.front().front();
347 }
349 /**
350 * Return first path in PathVector or NULL.
351 */
352 Geom::Path const *
353 SPCurve::first_path() const
354 {
355 if (is_empty()) {
356 return NULL;
357 }
359 return &_pathv.front();
360 }
362 /**
363 * 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) ?
364 */
365 Geom::Point
366 SPCurve::first_point() const
367 {
368 if (is_empty())
369 return Geom::Point(0, 0);
371 return _pathv.front().initialPoint();
372 }
374 /**
375 * Return the second point of first subpath or _movePos if curve too short.
376 * If the pathvector is empty, this returns (0,0). If the first path is only a moveto, this method
377 * returns the first point of the second path, if it exists. If there is no 2nd path, it returns the
378 * first point of the first path.
379 *
380 * FIXME: for empty paths shouldn't this return (NR_HUGE,NR_HUGE)
381 */
382 Geom::Point
383 SPCurve::second_point() const
384 {
385 if (is_empty()) {
386 return Geom::Point(0,0);
387 }
388 else if (_pathv.front().empty()) {
389 // first path is only a moveto
390 // check if there is second path
391 if (_pathv.size() > 1) {
392 return _pathv[1].initialPoint();
393 } else {
394 return _pathv[0].initialPoint();
395 }
396 }
397 else
398 return _pathv.front()[0].finalPoint();
399 }
401 /**
402 * TODO: fix comment: Return the second-last point of last subpath or _movePos if curve too short.
403 */
404 Geom::Point
405 SPCurve::penultimate_point() const
406 {
407 Geom::Curve const& back = _pathv.back().back_default();
408 return back.initialPoint();
409 }
411 /**
412 * 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) ?
413 * If the last path is only a moveto, then return that point.
414 */
415 Geom::Point
416 SPCurve::last_point() const
417 {
418 if (is_empty())
419 return Geom::Point(0, 0);
421 return _pathv.back().finalPoint();
422 }
424 /**
425 * Returns a *new* \a curve but drawn in the opposite direction.
426 * Should result in the same shape, but
427 * with all its markers drawn facing the other direction.
428 * Reverses the order of subpaths as well
429 **/
430 SPCurve *
431 SPCurve::create_reverse() const
432 {
433 SPCurve *new_curve = new SPCurve(Geom::reverse_paths_and_order(_pathv));
435 return new_curve;
436 }
438 /**
439 * Append \a curve2 to \a this.
440 * If \a use_lineto is false, simply add all paths in \a curve2 to \a this;
441 * 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.
442 */
443 void
444 SPCurve::append(SPCurve const *curve2,
445 bool use_lineto)
446 {
447 if (curve2->is_empty())
448 return;
450 if (use_lineto) {
451 Geom::PathVector::const_iterator it = curve2->_pathv.begin();
452 if ( ! _pathv.empty() ) {
453 Geom::Path & lastpath = _pathv.back();
454 lastpath.appendNew<Geom::LineSegment>( (*it).initialPoint() );
455 lastpath.append( (*it) );
456 } else {
457 _pathv.push_back( (*it) );
458 }
460 for (it++; it != curve2->_pathv.end(); it++) {
461 _pathv.push_back( (*it) );
462 }
463 } else {
464 for (Geom::PathVector::const_iterator it = curve2->_pathv.begin(); it != curve2->_pathv.end(); it++) {
465 _pathv.push_back( (*it) );
466 }
467 }
468 }
470 /**
471 * 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,
472 * 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.
473 * 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.
474 * When one of the curves is empty, this curves path becomes the non-empty path.
475 */
476 SPCurve *
477 SPCurve::append_continuous(SPCurve const *c1, gdouble tolerance)
478 {
479 using Geom::X;
480 using Geom::Y;
482 g_return_val_if_fail(c1 != NULL, NULL);
483 if ( this->is_closed() || c1->is_closed() ) {
484 return NULL;
485 }
487 if (c1->is_empty()) {
488 return this;
489 }
491 if (this->is_empty()) {
492 _pathv = c1->_pathv;
493 return this;
494 }
496 if ( (fabs(this->last_point()[X] - c1->first_point()[X]) <= tolerance)
497 && (fabs(this->last_point()[Y] - c1->first_point()[Y]) <= tolerance) )
498 {
499 // c1's first subpath can be appended to this curve's last subpath
500 Geom::PathVector::const_iterator path_it = c1->_pathv.begin();
501 Geom::Path & lastpath = _pathv.back();
503 Geom::Path newfirstpath(*path_it);
504 newfirstpath.setInitial(lastpath.finalPoint());
505 lastpath.append( newfirstpath );
507 for (path_it++; path_it != c1->_pathv.end(); path_it++) {
508 _pathv.push_back( (*path_it) );
509 }
511 } else {
512 append(c1, false);
513 }
515 return this;
516 }
518 /**
519 * Remove last segment of curve.
520 * (Only used once in /src/pen-context.cpp)
521 */
522 void
523 SPCurve::backspace()
524 {
525 if ( is_empty() )
526 return;
528 if ( !_pathv.back().empty() ) {
529 _pathv.back().erase_last();
530 _pathv.back().close(false);
531 }
532 }
534 /**
535 * TODO: add comments about what this method does and what assumptions are made and requirements are put on SPCurve
536 */
537 void
538 SPCurve::stretch_endpoints(Geom::Point const &new_p0, Geom::Point const &new_p1)
539 {
540 if (is_empty()) {
541 return;
542 }
544 Geom::Point const offset0( new_p0 - first_point() );
545 Geom::Point const offset1( new_p1 - last_point() );
547 Geom::Piecewise<Geom::D2<Geom::SBasis> > pwd2 = _pathv.front().toPwSb();
548 Geom::Piecewise<Geom::SBasis> arclength = Geom::arcLengthSb(pwd2);
549 if ( arclength.lastValue() <= 0 ) {
550 g_error("SPCurve::stretch_endpoints - arclength <= 0");
551 throw;
552 }
553 arclength *= 1./arclength.lastValue();
554 Geom::Point const A( offset0 );
555 Geom::Point const B( offset1 );
556 Geom::Piecewise<Geom::SBasis> offsetx = (arclength*-1.+1)*A[0] + arclength*B[0];
557 Geom::Piecewise<Geom::SBasis> offsety = (arclength*-1.+1)*A[1] + arclength*B[1];
558 Geom::Piecewise<Geom::D2<Geom::SBasis> > offsetpath = Geom::sectionize( Geom::D2<Geom::Piecewise<Geom::SBasis> >(offsetx, offsety) );
559 pwd2 += offsetpath;
560 _pathv = Geom::path_from_piecewise( pwd2, 0.001 );
561 }
563 /**
564 * sets start of first path to new_p0, and end of first path to new_p1
565 */
566 void
567 SPCurve::move_endpoints(Geom::Point const &new_p0, Geom::Point const &new_p1)
568 {
569 if (is_empty()) {
570 return;
571 }
572 _pathv.front().setInitial(new_p0);
573 _pathv.front().setFinal(new_p1);
574 }
576 /**
577 * returns the number of nodes in a path, used for statusbar text when selecting an spcurve.
578 */
579 guint
580 SPCurve::nodes_in_path() const
581 {
582 guint nr = 0;
583 for(Geom::PathVector::const_iterator it = _pathv.begin(); it != _pathv.end(); ++it) {
584 nr += (*it).size();
586 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
587 }
589 return nr;
590 }
592 /**
593 * Adds p to the last point (and last handle if present) of the last path
594 */
595 void
596 SPCurve::last_point_additive_move(Geom::Point const & p)
597 {
598 if (is_empty()) {
599 return;
600 }
602 _pathv.back().setFinal( _pathv.back().finalPoint() + p );
604 // Move handle as well when the last segment is a cubic bezier segment:
605 // TODO: what to do for quadratic beziers?
606 if ( Geom::CubicBezier const *lastcube = dynamic_cast<Geom::CubicBezier const *>(&_pathv.back().back()) ) {
607 Geom::CubicBezier newcube( *lastcube );
608 newcube.setPoint(2, newcube[2] + p);
609 _pathv.back().replace( --_pathv.back().end(), newcube );
610 }
611 }
613 /*
614 Local Variables:
615 mode:c++
616 c-file-style:"stroustrup"
617 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
618 indent-tabs-mode:nil
619 fill-column:99
620 End:
621 */
622 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :