X-Git-Url: https://git.tokkee.org/?a=blobdiff_plain;ds=sidebyside;f=src%2Fdisplay%2Fcurve.cpp;h=7d7dbc987664c417bb894503eccc40695d6c45a6;hb=d91c7044b45dde766203822b19e4affead5ce10f;hp=af26fdda459fd7d4983c637ccdb62ce542bebb92;hpb=81519968ff53204d2d065b4ba61fa581474cb8a5;p=inkscape.git diff --git a/src/display/curve.cpp b/src/display/curve.cpp index af26fdda4..7d7dbc987 100644 --- a/src/display/curve.cpp +++ b/src/display/curve.cpp @@ -1,12 +1,13 @@ #define __CURVE_C__ /** \file - * Routines for SPCurve and for NArtBpath arrays / Geom::PathVector in general. + * Routines for SPCurve and for its Geom::PathVector */ /* - * Author: + * Authors: * Lauris Kaplinski + * Johan Engelen * * Copyright (C) 2000 Lauris Kaplinski * Copyright (C) 2000-2001 Ximian, Inc. @@ -18,209 +19,48 @@ #include "display/curve.h" -#include -#include -#include "libnr/nr-point.h" -#include "libnr/nr-rect.h" -#include -#include -#include -#include -#include -#include -#include +#include #include <2geom/pathvector.h> #include <2geom/sbasis-geometric.h> #include <2geom/sbasis-to-bezier.h> -#include "svg/svg.h" - -static unsigned sp_bpath_length(NArtBpath const bpath[]); -static bool sp_bpath_closed(NArtBpath const bpath[]); - -#define NO_CHECKS // define this to disable the checking for unequal paths in SPCurve, improves performance by a lot! - - -#ifndef NO_CHECKS -static void debug_out( char const * text, Geom::PathVector const & pathv) { - char * str = sp_svg_write_path(pathv); - g_message("%s : %s", text, str); - g_free(str); -} -#endif - -#ifndef NO_CHECKS -static void debug_out( char const * text, NArtBpath const * bpath) { - char * str = sp_svg_write_path(bpath); - g_message("%s : %s", text, str); - g_free(str); -} -#endif - -#ifndef NO_CHECKS -void SPCurve::debug_check( char const * text, SPCurve const * curve) { - char * pathv_str = sp_svg_write_path(curve->_pathv); - char * bpath_str = sp_svg_write_path(curve->_bpath); - if ( strcmp(pathv_str, bpath_str) ) { - g_message("%s : unequal paths", text); - g_message("bpath : %s", bpath_str); - g_message("pathv : %s", pathv_str); - } - g_free(pathv_str); - g_free(bpath_str); -#else -void SPCurve::debug_check( char const *, SPCurve const *) { -#endif -} - -#ifndef NO_CHECKS -void SPCurve::debug_check( char const * text, bool a) { - if ( !a ) { - g_message("%s : bool fail", text); - } -#else -void SPCurve::debug_check( char const *, bool) { -#endif -} +#include <2geom/point.h> /* Constructors */ /** * The returned curve's state is as if SPCurve::reset has just been called on it. - * \param length Initial number of NArtBpath elements allocated for bpath (including NR_END - * element). - * 2GEOMproof */ -SPCurve::SPCurve(guint length) +SPCurve::SPCurve() : _refcount(1), - _bpath(NULL), - _pathv(), - _end(0), - _length(length), - _substart(0), - _hascpt(false), - _posSet(false), - _moving(false), - _closed(false) + _pathv() { - if (length <= 0) { - g_error("SPCurve::SPCurve called with invalid length parameter"); - throw; - } - - _bpath = g_new(NArtBpath, length); - _bpath->code = NR_END; - _pathv.clear(); - - debug_check("SPCurve::SPCurve(guint length)", this); } SPCurve::SPCurve(Geom::PathVector const& pathv) : _refcount(1), - _bpath(NULL), - _pathv(pathv), - _end(0), - _length(0), - _substart(0), - _hascpt(false), - _posSet(false), - _moving(false), - _closed(false) + _pathv(pathv) { - // temporary code to convert to _bpath as well: - _bpath = BPath_from_2GeomPath(_pathv); - unsigned const len = sp_bpath_length(_bpath); - _length = len; - _end = _length - 1; - gint i = _end; - for (; i > 0; i--) - if ((_bpath[i].code == NR_MOVETO) || - (_bpath[i].code == NR_MOVETO_OPEN)) - break; - _substart = i; - _closed = sp_bpath_closed(_bpath); - - debug_check("SPCurve::SPCurve(Geom::PathVector const& pathv)", this); } -// * 2GEOMproof SPCurve * -SPCurve::new_from_foreign_bpath(NArtBpath const *bpath) +SPCurve::new_from_rect(Geom::Rect const &rect) { - g_return_val_if_fail(bpath != NULL, NULL); - - NArtBpath *new_bpath; - unsigned const len = sp_bpath_length(bpath); - new_bpath = g_new(NArtBpath, len); - memcpy(new_bpath, bpath, len * sizeof(NArtBpath)); - - SPCurve *curve = new SPCurve(); - - curve->_bpath = new_bpath; - curve->_length = len; - curve->_end = curve->_length - 1; - gint i = curve->_end; - for (; i > 0; i--) - if ((curve->_bpath[i].code == NR_MOVETO) || - (curve->_bpath[i].code == NR_MOVETO_OPEN)) - break; - curve->_substart = i; - curve->_closed = sp_bpath_closed(new_bpath); - - curve->_pathv = BPath_to_2GeomPath(curve->_bpath); - - debug_check("SPCurve::new_from_foreign_bpath", curve); - - return curve; -} - -/** - * Convert NArtBpath object to SPCurve object. - * - * \return new SPCurve, or NULL if the curve was not created for some reason. - * 2GEOMproof - */ -SPCurve * -SPCurve::new_from_bpath(NArtBpath *bpath) -{ - g_return_val_if_fail(bpath != NULL, NULL); - - SPCurve *curve = SPCurve::new_from_foreign_bpath(bpath); - g_free(bpath); - - debug_check("SPCurve::new_from_bpath", curve); - - return curve; -} - -// * 2GEOMproof -SPCurve * -SPCurve::new_from_rect(NR::Maybe const &rect) -{ - g_return_val_if_fail(rect, NULL); - SPCurve *c = new SPCurve(); - NR::Point p = rect->corner(0); + Geom::Point p = rect.corner(0); c->moveto(p); for (int i=3; i>=0; i--) { - c->lineto(rect->corner(i)); + c->lineto(rect.corner(i)); } c->closepath_current(); - debug_check("SPCurve::new_from_rect", c); - return c; } -// * 2GEOMproof SPCurve::~SPCurve() { - if (_bpath) { - g_free(_bpath); - _bpath = NULL; - } } /* Methods */ @@ -229,71 +69,38 @@ void SPCurve::set_pathvector(Geom::PathVector const & new_pathv) { _pathv = new_pathv; - - _hascpt = false; - _posSet = false; - _moving = false; - - // temporary code to convert to _bpath as well: - if (_bpath) { - g_free(_bpath); - _bpath = NULL; - } - _bpath = BPath_from_2GeomPath(_pathv); - unsigned const len = sp_bpath_length(_bpath); - _length = len; - _end = _length - 1; - gint i = _end; - for (; i > 0; i--) - if ((_bpath[i].code == NR_MOVETO) || - (_bpath[i].code == NR_MOVETO_OPEN)) - break; - _substart = i; - _closed = sp_bpath_closed(_bpath); - - debug_check("SPCurve::set_pathvector", this); } -/** - * Get pointer to bpath data. Don't keep this reference too long, because the path might change by another function. - */ -NArtBpath const * -SPCurve::get_bpath() const -{ - debug_check("SPCurve::get_bpath", this); - return _bpath; -}; - Geom::PathVector const & SPCurve::get_pathvector() const { - debug_check("SPCurve::get_pathvector", this); return _pathv; } -/** - *Returns index in bpath[] of NR_END element. - * remove for 2geom +/* + * Returns the number of segments of all paths summed + * This count includes the closing line segment of a closed path. */ guint -SPCurve::get_length() const +SPCurve::get_segment_count() const { -// g_message("SPCurve::get_length must be removed"); + guint nr = 0; + for(Geom::PathVector::const_iterator it = _pathv.begin(); it != _pathv.end(); ++it) { + nr += (*it).size(); - return _end; + if (it->closed()) nr += 1; + } + return nr; } /** * Increase _refcount of curve. * * \todo should this be shared with other refcounting code? - * 2GEOMproof */ SPCurve * SPCurve::ref() { - g_return_val_if_fail(this != NULL, NULL); - _refcount += 1; return this; @@ -303,13 +110,10 @@ SPCurve::ref() * Decrease refcount of curve, with possible destruction. * * \todo should this be shared with other refcounting code? - * 2GEOMproof */ SPCurve * SPCurve::unref() { - g_return_val_if_fail(this != NULL, NULL); - _refcount -= 1; if (_refcount < 1) { @@ -320,84 +124,27 @@ SPCurve::unref() } /** - * Add space for more paths in curve. - * This function has no meaning for 2geom representation, other than maybe for optimization issues (enlargening the vector for what is to come) - * 2GEOMproof - */ -void -SPCurve::ensure_space(guint space) -{ - g_return_if_fail(this != NULL); - g_return_if_fail(space > 0); - - if (_end + space < _length) - return; - - if (space < SP_CURVE_LENSTEP) - space = SP_CURVE_LENSTEP; - - _bpath = g_renew(NArtBpath, _bpath, _length + space); - - _length += space; -} - -/** - * Create new curve from its own bpath array. - * 2GEOMproof + * Create new curve from this curve's pathvector array. */ SPCurve * SPCurve::copy() const { - g_return_val_if_fail(this != NULL, NULL); - - return SPCurve::new_from_foreign_bpath(_bpath); + return new SPCurve(_pathv); } /** * Return new curve that is the concatenation of all curves in list. - * 2GEOMified */ SPCurve * SPCurve::concat(GSList const *list) { - g_return_val_if_fail(list != NULL, NULL); - - gint length = 0; - - for (GSList const *l = list; l != NULL; l = l->next) { - SPCurve *c = (SPCurve *) l->data; - length += c->_end; - } - - SPCurve *new_curve = new SPCurve(length + 1); - - NArtBpath *bp = new_curve->_bpath; - - for (GSList const *l = list; l != NULL; l = l->next) { - SPCurve *c = (SPCurve *) l->data; - memcpy(bp, c->_bpath, c->_end * sizeof(NArtBpath)); - bp += c->_end; - } - - bp->code = NR_END; - - new_curve->_end = length; - gint i; - for (i = new_curve->_end; i > 0; i--) { - if ((new_curve->_bpath[i].code == NR_MOVETO) || - (new_curve->_bpath[i].code == NR_MOVETO_OPEN) ) - break; - } - - new_curve->_substart = i; + SPCurve *new_curve = new SPCurve(); for (GSList const *l = list; l != NULL; l = l->next) { SPCurve *c = (SPCurve *) l->data; new_curve->_pathv.insert( new_curve->_pathv.end(), c->get_pathvector().begin(), c->get_pathvector().end() ); } - debug_check("SPCurve::concat", new_curve); - return new_curve; } @@ -408,127 +155,40 @@ SPCurve::concat(GSList const *list) GSList * SPCurve::split() const { - g_return_val_if_fail(this != NULL, NULL); - - guint p = 0; GSList *l = NULL; - gint pathnr = 0; - while (p < _end) { - gint i = 1; - while ((_bpath[p + i].code == NR_LINETO) || - (_bpath[p + i].code == NR_CURVETO)) - i++; - SPCurve *new_curve = new SPCurve(i + 1); - memcpy(new_curve->_bpath, _bpath + p, i * sizeof(NArtBpath)); - new_curve->_end = i; - new_curve->_bpath[i].code = NR_END; - new_curve->_substart = 0; - new_curve->_closed = (new_curve->_bpath->code == NR_MOVETO); - new_curve->_hascpt = (new_curve->_bpath->code == NR_MOVETO_OPEN); - new_curve->_pathv = Geom::PathVector(1, _pathv[pathnr]); - l = g_slist_prepend(l, new_curve); - p += i; - pathnr++; + for (Geom::PathVector::const_iterator path_it = _pathv.begin(); path_it != _pathv.end(); ++path_it) { + Geom::PathVector newpathv; + newpathv.push_back(*path_it); + SPCurve * newcurve = new SPCurve(newpathv); + l = g_slist_prepend(l, newcurve); } return l; } -/** - * Transform all paths in curve, template helper. - */ -template -static void -tmpl_curve_transform(SPCurve * curve, M const &m) -{ - g_return_if_fail(curve != NULL); - - for (guint i = 0; i < curve->_end; i++) { - NArtBpath *p = curve->_bpath + i; - switch (p->code) { - case NR_MOVETO: - case NR_MOVETO_OPEN: - case NR_LINETO: { - p->setC(3, p->c(3) * m); - break; - } - case NR_CURVETO: - for (unsigned i = 1; i <= 3; ++i) { - p->setC(i, p->c(i) * m); - } - break; - default: - g_warning("Illegal pathcode %d", p->code); - break; - } - } -} - -/** - * Transform all paths in curve using matrix. - * 2GEOMified, can be deleted when completely 2geom - */ -void -SPCurve::transform(NR::Matrix const &m) -{ - tmpl_curve_transform(this, m); - - _pathv = _pathv * to_2geom(m); - - debug_check("SPCurve::transform(NR::Matrix const &m)", this); -} - /** * Transform all paths in curve using matrix. */ void SPCurve::transform(Geom::Matrix const &m) { - tmpl_curve_transform(this, from_2geom(m)); - - _pathv = _pathv * m; - - debug_check("SPCurve::transform(Geom::Matrix const &m)", this); -} - -/** - * Transform all paths in curve using NR::translate. - * 2GEOMified, can be deleted when completely 2geom - */ -void -SPCurve::transform(NR::translate const &m) -{ - tmpl_curve_transform(this, m); - - _pathv = _pathv * to_2geom(m); - - debug_check("SPCurve::transform(NR::translate const &m)", this); + _pathv *= m; } /** * Set curve to empty curve. - * 2GEOMified + * In more detail: this clears the internal pathvector from all its paths. */ void SPCurve::reset() { - g_return_if_fail(this != NULL); - - _bpath->code = NR_END; - _end = 0; - _substart = 0; - _hascpt = false; - _posSet = false; - _moving = false; - _closed = false; - _pathv.clear(); - - debug_check("SPCurve::reset", this); } -/* Several consecutive movetos are ALLOWED */ +/** Several consecutive movetos are ALLOWED + * Ref: http://www.w3.org/TR/SVG11/implnote.html#PathElementImplementationNotes + * (first subitem of the item about zero-length path segments) */ /** * Calls SPCurve::moveto() with point made of given coordinates. @@ -536,367 +196,132 @@ SPCurve::reset() void SPCurve::moveto(gdouble x, gdouble y) { - moveto(NR::Point(x, y)); -} -/** - * Calls SPCurve::moveto() with point made of given coordinates. - */ -void -SPCurve::moveto(Geom::Point const &p) -{ - moveto(from_2geom(p)); + moveto(Geom::Point(x, y)); } /** * Perform a moveto to a point, thus starting a new subpath. - * 2GEOMified + * Point p must be finite. */ void -SPCurve::moveto(NR::Point const &p) +SPCurve::moveto(Geom::Point const &p) { - g_return_if_fail(this != NULL); - g_return_if_fail(!_moving); - - _substart = _end; - _hascpt = true; - _posSet = true; - _movePos = p; _pathv.push_back( Geom::Path() ); // for some reason Geom::Path(p) does not work... - _pathv.back().start(to_2geom(p)); - - // the output is not the same. This is because SPCurve *incorrectly* coaslesces multiple moveto's into one for NArtBpath. -// debug_check("SPCurve::moveto", this); + _pathv.back().start(p); } /** - * Calls SPCurve::lineto() with a point's coordinates. + * Adds a line to the current subpath. + * Point p must be finite. */ void SPCurve::lineto(Geom::Point const &p) { - lineto(p[Geom::X], p[Geom::Y]); + if (_pathv.empty()) g_message("SPCurve::lineto - path is empty!"); + else _pathv.back().appendNew( p ); } /** - * Calls SPCurve::lineto() with a point's coordinates. + * Calls SPCurve::lineto( Geom::Point(x,y) ) */ void -SPCurve::lineto(NR::Point const &p) +SPCurve::lineto(gdouble x, gdouble y) { - lineto(p[NR::X], p[NR::Y]); + lineto(Geom::Point(x,y)); } + /** - * Adds a line to the current subpath. - * 2GEOMified + * Adds a quadratic bezier segment to the current subpath. + * All points must be finite. */ void -SPCurve::lineto(gdouble x, gdouble y) +SPCurve::quadto(Geom::Point const &p1, Geom::Point const &p2) { - g_return_if_fail(this != NULL); - g_return_if_fail(_hascpt); - - if (_moving) { - /* fix endpoint */ - g_return_if_fail(!_posSet); - g_return_if_fail(_end > 1); - NArtBpath *bp = _bpath + _end - 1; - g_return_if_fail(bp->code == NR_LINETO); - bp->x3 = x; - bp->y3 = y; - _moving = false; - - Geom::Path::iterator it = _pathv.back().end(); - if ( Geom::LineSegment const *last_line_segment = dynamic_cast( &(*it) )) { - Geom::LineSegment new_seg( *last_line_segment ); - new_seg.setFinal( Geom::Point(x,y) ); - _pathv.back().replace(it, new_seg); - } - } else if (_posSet) { - /* start a new segment */ - ensure_space(2); - NArtBpath *bp = _bpath + _end; - bp->code = NR_MOVETO_OPEN; - bp->setC(3, _movePos); - bp++; - bp->code = NR_LINETO; - bp->x3 = x; - bp->y3 = y; - bp++; - bp->code = NR_END; - _end += 2; - _posSet = false; - _closed = false; - - _pathv.back().appendNew( Geom::Point(x,y) ); - return; - } else { - /* add line */ - - g_return_if_fail(_end > 1); - ensure_space(1); - NArtBpath *bp = _bpath + _end; - bp->code = NR_LINETO; - bp->x3 = x; - bp->y3 = y; - bp++; - bp->code = NR_END; - _end++; - _pathv.back().appendNew( Geom::Point(x,y) ); - } - - debug_check("SPCurve::lineto", this); + if (_pathv.empty()) g_message("SPCurve::quadto - path is empty!"); + else _pathv.back().appendNew( p1, p2); } - /** - * Calls SPCurve::curveto() with coordinates of three points. + * Calls SPCurve::quadto( Geom::Point(x1,y1), Geom::Point(x2,y2) ) + * All coordinates must be finite. */ void -SPCurve::curveto(Geom::Point const &p0, Geom::Point const &p1, Geom::Point const &p2) +SPCurve::quadto(gdouble x1, gdouble y1, gdouble x2, gdouble y2) { - using Geom::X; - using Geom::Y; - curveto( p0[X], p0[Y], - p1[X], p1[Y], - p2[X], p2[Y] ); + quadto( Geom::Point(x1,y1), Geom::Point(x2,y2) ); } + /** - * Calls SPCurve::curveto() with coordinates of three points. + * Adds a bezier segment to the current subpath. + * All points must be finite. */ void -SPCurve::curveto(NR::Point const &p0, NR::Point const &p1, NR::Point const &p2) +SPCurve::curveto(Geom::Point const &p0, Geom::Point const &p1, Geom::Point const &p2) { - using NR::X; - using NR::Y; - curveto( p0[X], p0[Y], - p1[X], p1[Y], - p2[X], p2[Y] ); + if (_pathv.empty()) g_message("SPCurve::curveto - path is empty!"); + else _pathv.back().appendNew( p0, p1, p2 ); } /** - * Adds a bezier segment to the current subpath. - * 2GEOMified + * Calls SPCurve::curveto( Geom::Point(x0,y0), Geom::Point(x1,y1), Geom::Point(x2,y2) ) + * All coordinates must be finite. */ void SPCurve::curveto(gdouble x0, gdouble y0, gdouble x1, gdouble y1, gdouble x2, gdouble y2) { - g_return_if_fail(this != NULL); - g_return_if_fail(_hascpt); - g_return_if_fail(!_moving); - - if (_posSet) { - /* start a new segment */ - ensure_space(2); - NArtBpath *bp = _bpath + _end; - bp->code = NR_MOVETO_OPEN; - bp->setC(3, _movePos); - bp++; - bp->code = NR_CURVETO; - bp->x1 = x0; - bp->y1 = y0; - bp->x2 = x1; - bp->y2 = y1; - bp->x3 = x2; - bp->y3 = y2; - bp++; - bp->code = NR_END; - _end += 2; - _posSet = false; - _closed = false; - _pathv.back().appendNew( Geom::Point(x0,y0), Geom::Point(x1,y1), Geom::Point(x2,y2) ); - } else { - /* add curve */ - - g_return_if_fail(_end > 1); - ensure_space(1); - NArtBpath *bp = _bpath + _end; - bp->code = NR_CURVETO; - bp->x1 = x0; - bp->y1 = y0; - bp->x2 = x1; - bp->y2 = y1; - bp->x3 = x2; - bp->y3 = y2; - bp++; - bp->code = NR_END; - _end++; - if (_pathv.empty()) g_message("leeg"); - else _pathv.back().appendNew( Geom::Point(x0,y0), Geom::Point(x1,y1), Geom::Point(x2,y2) ); - } - - debug_check("SPCurve::curveto", this); + curveto( Geom::Point(x0,y0), Geom::Point(x1,y1), Geom::Point(x2,y2) ); } /** * Close current subpath by possibly adding a line between start and end. - * 2GEOMified */ void SPCurve::closepath() { - g_return_if_fail(this != NULL); - g_return_if_fail(_hascpt); - g_return_if_fail(!_posSet); - g_return_if_fail(!_moving); - g_return_if_fail(!_closed); - /* We need at least moveto, curveto, end. */ - g_return_if_fail(_end - _substart > 1); - - { - NArtBpath *bs = _bpath + _substart; - NArtBpath *be = _bpath + _end - 1; - - if (bs->c(3) != be->c(3)) { - lineto(bs->c(3)); - bs = _bpath + _substart; - } - - bs->code = NR_MOVETO; - } - // Inkscape always manually adds the closing line segment to SPCurve with a lineto. - // This lineto is removed in the writing function for NArtBpath, - // so when path is closed and the last segment is a lineto, the closing line segment must really be removed first! - // TODO: fix behavior in Inkscape! - if ( /*Geom::LineSegment const *line_segment = */ dynamic_cast(&_pathv.back().back())) { - _pathv.back().erase_last(); - } _pathv.back().close(true); - _closed = true; - - for (Geom::PathVector::const_iterator it = _pathv.begin(); it != _pathv.end(); it++) { - if ( ! it->closed() ) { - _closed = false; - break; - } - } - - for (NArtBpath const *bp = _bpath; bp->code != NR_END; bp++) { - /** \todo - * effic: Maintain a count of NR_MOVETO_OPEN's (e.g. instead of - * the closed boolean). - */ - if (bp->code == NR_MOVETO_OPEN) { - _closed = false; - break; - } - } - - _hascpt = false; - - debug_check("SPCurve::closepath", this); } -/** Like SPCurve::closepath() but sets the end point of the current - command to the subpath start point instead of adding a new lineto. +/** Like SPCurve::closepath() but sets the end point of the last subpath + to the subpath start point instead of adding a new lineto. Used for freehand drawing when the user draws back to the start point. - - 2GEOMified **/ void SPCurve::closepath_current() { - g_return_if_fail(this != NULL); - g_return_if_fail(_hascpt); - g_return_if_fail(!_posSet); - g_return_if_fail(!_closed); - /* We need at least moveto, curveto, end. */ - g_return_if_fail(_end - _substart > 1); - - { - NArtBpath *bs = _bpath + _substart; - NArtBpath *be = _bpath + _end - 1; - - be->x3 = bs->x3; - be->y3 = bs->y3; - - bs->code = NR_MOVETO; - } - // Inkscape always manually adds the closing line segment to SPCurve with a lineto. - // This lineto is removed in the writing function for NArtBpath, - // so when path is closed and the last segment is a lineto, the closing line segment must really be removed first! - // TODO: fix behavior in Inkscape! - if ( /*Geom::LineSegment const *line_segment = */ dynamic_cast(&_pathv.back().back())) { - _pathv.back().erase_last(); - } + _pathv.back().setFinal(_pathv.back().initialPoint()); _pathv.back().close(true); - _closed = true; - - for (Geom::PathVector::const_iterator it = _pathv.begin(); it != _pathv.end(); it++) { - if ( ! it->closed() ) { - _closed = false; - break; - } - } - - for (NArtBpath const *bp = _bpath; bp->code != NR_END; bp++) { - /** \todo - * effic: Maintain a count of NR_MOVETO_OPEN's (e.g. instead of - * the closed boolean). - */ - if (bp->code == NR_MOVETO_OPEN) { - _closed = false; - break; - } - } - - _hascpt = false; - _moving = false; - - debug_check("SPCurve::closepath_current", this); } /** - * True if no paths are in curve. - * 2GEOMproof + * True if no paths are in curve. If it only contains a path with only a moveto, the path is considered NON-empty */ bool SPCurve::is_empty() const { - g_return_val_if_fail(this != NULL, TRUE); - - if (!_bpath) - return true; - - bool empty = _pathv.empty() || _pathv.front().empty(); - debug_check("SPCurve::is_empty", (_bpath->code == NR_END) == empty ); - - return empty; + return _pathv.empty(); } /** * True iff all subpaths are closed. - * 2GEOMproof + * Returns false if the curve is empty. */ bool SPCurve::is_closed() const { - bool closed = true; - for (Geom::PathVector::const_iterator it = _pathv.begin(); it != _pathv.end(); it++) { - if ( ! it->closed() ) { - closed = false; - break; + if (is_empty()) { + return false; + } else { + bool closed = true; + for (Geom::PathVector::const_iterator it = _pathv.begin(); it != _pathv.end(); it++) { + if ( ! it->closed() ) { + closed = false; + break; + } } + return closed; } - debug_check("SPCurve::is_closed", (closed) == (_closed) ); - - return closed; } /** - * Return last subpath or NULL. - */ -NArtBpath const * -SPCurve::last_bpath() const -{ - g_return_val_if_fail(this != NULL, NULL); - - if (_end == 0) { - return NULL; - } - - return _bpath + _end - 1; -} - -/** - * Return last pathsegment (possibly the closing path segment) in PathVector or NULL. - * equal in functionality to SPCurve::last_bpath() + * Return last pathsegment (possibly the closing path segment) of the last path in PathVector or NULL. + * If the last path is empty (contains only a moveto), the function returns NULL */ Geom::Curve const * SPCurve::last_segment() const @@ -917,8 +342,6 @@ SPCurve::last_segment() const Geom::Path const * SPCurve::last_path() const { - g_return_val_if_fail(this != NULL, NULL); - if (is_empty()) { return NULL; } @@ -949,8 +372,6 @@ SPCurve::first_segment() const Geom::Path const * SPCurve::first_path() const { - g_return_val_if_fail(this != NULL, NULL); - if (is_empty()) { return NULL; } @@ -959,94 +380,81 @@ SPCurve::first_path() const } /** - * 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) ? + * Return first point of first subpath or nothing when the path is empty. */ -NR::Point +boost::optional SPCurve::first_point() const { - NArtBpath const * bpath = get_bpath(); - g_return_val_if_fail(bpath != NULL, NR::Point(0, 0)); - if (is_empty()) - return NR::Point(0, 0); + boost::optional retval; - debug_check("SPCurve::first_point", bpath->c(3) == _pathv.front().initialPoint() ); + if (!is_empty()) { + retval = _pathv.front().initialPoint(); + } - //return bpath->c(3); - return from_2geom( _pathv.front().initialPoint() ); + return retval; } /** * Return the second point of first subpath or _movePos if curve too short. + * If the pathvector is empty, this returns nothing. If the first path is only a moveto, this method + * returns the first point of the second path, if it exists. If there is no 2nd path, it returns the + * first point of the first path. */ -NR::Point +boost::optional SPCurve::second_point() const { - g_return_val_if_fail(this != NULL, NR::Point(0, 0)); - - if (_end < 1) { - return _movePos; - } - - NArtBpath *bpath = NULL; - if (_end < 2) { - bpath = _bpath; - } else { - bpath = _bpath + 1; + boost::optional retval; + if (!is_empty()) { + if (_pathv.front().empty()) { + // first path is only a moveto + // check if there is second path + if (_pathv.size() > 1) { + retval = _pathv[1].initialPoint(); + } else { + retval = _pathv[0].initialPoint(); + } + } else { + retval = _pathv.front()[0].finalPoint(); + } } - g_return_val_if_fail(bpath != NULL, NR::Point(0, 0)); - - debug_check("SPCurve::second_point", bpath->c(3) == _pathv.front()[0].finalPoint() ); - return bpath->c(3); + return retval; } /** - * Return the second-last point of last subpath or _movePos if curve too short. + * Return the second-last point of last subpath or first point when that last subpath has only a moveto. */ -NR::Point +boost::optional SPCurve::penultimate_point() const { - g_return_val_if_fail(this != NULL, NR::Point(0, 0)); - - if (_end < 2) { - return _movePos; - } - - NArtBpath *const bpath = _bpath + _end - 2; - g_return_val_if_fail(bpath != NULL, NR::Point(0, 0)); - - Geom::Point p(NR_HUGE, NR_HUGE); - Geom::Curve const& back = _pathv.back().back(); - if (_pathv.back().closed()) { - p = back.finalPoint(); - } else { - p = back.initialPoint(); + boost::optional retval; + if (!is_empty()) { + Geom::Path const &lastpath = _pathv.back(); + if (!lastpath.empty()) { + Geom::Curve const &back = lastpath.back_default(); + retval = back.initialPoint(); + } else { + retval = lastpath.initialPoint(); + } } - debug_check("SPCurve::penultimate_point", bpath->c(3) == p ); - return bpath->c(3); + return retval; } /** - * 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) ? + * Return last point of last subpath or nothing when the curve is empty. + * If the last path is only a moveto, then return that point. */ -NR::Point +boost::optional SPCurve::last_point() const { - NArtBpath const * bpath = last_bpath(); - g_return_val_if_fail(bpath != NULL, NR::Point(0, 0)); - if (is_empty()) - return NR::Point(0, 0); - - debug_check("SPCurve::last_point", bpath->c(3) == _pathv.back().finalPoint() ); - //return bpath->c(3); - return from_2geom( _pathv.back().finalPoint() ); -} + boost::optional retval; -inline static bool -is_moveto(NRPathcode const c) -{ - return c == NR_MOVETO || c == NR_MOVETO_OPEN; + if (!is_empty()) { + retval = _pathv.back().finalPoint(); + } + + return retval; } /** @@ -1054,120 +462,27 @@ is_moveto(NRPathcode const c) * Should result in the same shape, but * with all its markers drawn facing the other direction. * Reverses the order of subpaths as well - * 2GEOMified **/ SPCurve * SPCurve::create_reverse() const { - /* We need at least moveto, curveto, end. */ - g_return_val_if_fail(_end - _substart > 1, NULL); - - NArtBpath const *be = _bpath + _end - 1; - - g_assert(is_moveto(_bpath[_substart].code)); - g_assert(is_moveto(_bpath[0].code)); - g_assert((be+1)->code == NR_END); - - SPCurve *new_curve = new SPCurve(_length); - new_curve->moveto(be->c(3)); - - for (NArtBpath const *bp = be; ; --bp) { - switch (bp->code) { - case NR_MOVETO: - g_assert(new_curve->_bpath[new_curve->_substart].code == NR_MOVETO_OPEN); - new_curve->_bpath[new_curve->_substart].code = NR_MOVETO; - /* FALL-THROUGH */ - case NR_MOVETO_OPEN: - if (bp == _bpath) { - return new_curve; - } - new_curve->moveto((bp-1)->c(3)); - break; + SPCurve *new_curve = new SPCurve(Geom::reverse_paths_and_order(_pathv)); - case NR_LINETO: - new_curve->lineto((bp-1)->c(3)); - break; - - case NR_CURVETO: - new_curve->curveto(bp->c(2), bp->c(1), (bp-1)->c(3)); - break; - - default: - g_assert_not_reached(); - } - } - - new_curve->_pathv = Geom::reverse_paths_and_order(_pathv); - - debug_check("SPCurve::create_reverse", new_curve); + return new_curve; } /** * Append \a curve2 to \a this. * If \a use_lineto is false, simply add all paths in \a curve2 to \a this; * 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. - * 2GEOMified */ void SPCurve::append(SPCurve const *curve2, bool use_lineto) { - g_return_if_fail(this != NULL); - g_return_if_fail(curve2 != NULL); - if (curve2->is_empty()) return; - if (curve2->_end < 1) - return; - - NArtBpath const *bs = curve2->_bpath; - - bool closed = this->_closed; - - for (NArtBpath const *bp = bs; bp->code != NR_END; bp++) { - switch (bp->code) { - case NR_MOVETO_OPEN: - if (use_lineto && _hascpt) { - lineto(bp->x3, bp->y3); - use_lineto = false; - } else { - if (closed && _hascpt) closepath(); - moveto(bp->x3, bp->y3); - } - closed = false; - break; - - case NR_MOVETO: - if (use_lineto && _hascpt) { - lineto(bp->x3, bp->y3); - use_lineto = FALSE; - } else { - if (closed && _hascpt) closepath(); - moveto(bp->x3, bp->y3); - } - closed = true; - break; - case NR_LINETO: - lineto(bp->x3, bp->y3); - break; - - case NR_CURVETO: - curveto(bp->x1, bp->y1, bp->x2, bp->y2, bp->x3, bp->y3); - break; - - case NR_END: - g_assert_not_reached(); - } - } - - if (closed) { - closepath(); - } - - debug_check("SPCurve::append", this); - - /* 2GEOM code when code above is removed: if (use_lineto) { Geom::PathVector::const_iterator it = curve2->_pathv.begin(); if ( ! _pathv.empty() ) { @@ -1186,292 +501,90 @@ SPCurve::append(SPCurve const *curve2, _pathv.push_back( (*it) ); } } - */ } /** - * Append \a c1 to \a this with possible fusing of close endpoints. - * 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 + * 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, + * 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. + * 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. + * When one of the curves is empty, this curves path becomes the non-empty path. */ SPCurve * SPCurve::append_continuous(SPCurve const *c1, gdouble tolerance) { - g_return_val_if_fail(this != NULL, NULL); + using Geom::X; + using Geom::Y; + g_return_val_if_fail(c1 != NULL, NULL); - g_return_val_if_fail(!_closed, NULL); - g_return_val_if_fail(!c1->_closed, NULL); + if ( this->is_closed() || c1->is_closed() ) { + return NULL; + } - if (c1->_end < 1) { + if (c1->is_empty()) { return this; } - debug_check("SPCurve::append_continuous 11", this); + if (this->is_empty()) { + _pathv = c1->_pathv; + return this; + } - NArtBpath const *be = last_bpath(); - if (be) { - NArtBpath const *bs = c1->get_bpath(); - if ( bs - && ( fabs( bs->x3 - be->x3 ) <= tolerance ) - && ( fabs( bs->y3 - be->y3 ) <= tolerance ) ) - { - /** \todo - * fixme: Strictly we mess in case of multisegment mixed - * open/close curves - */ - bool closed = false; - for (bs = bs + 1; bs->code != NR_END; bs++) { - switch (bs->code) { - case NR_MOVETO_OPEN: - if (closed) closepath(); - moveto(bs->x3, bs->y3); - closed = false; - break; - case NR_MOVETO: - if (closed) closepath(); - moveto(bs->x3, bs->y3); - closed = true; - break; - case NR_LINETO: - lineto(bs->x3, bs->y3); - break; - case NR_CURVETO: - curveto(bs->x1, bs->y1, bs->x2, bs->y2, bs->x3, bs->y3); - break; - case NR_END: - g_assert_not_reached(); - } - } - } else { - append(c1, TRUE); + if ( (fabs((*this->last_point())[X] - (*c1->first_point())[X]) <= tolerance) + && (fabs((*this->last_point())[Y] - (*c1->first_point())[Y]) <= tolerance) ) + { + // c1's first subpath can be appended to this curve's last subpath + Geom::PathVector::const_iterator path_it = c1->_pathv.begin(); + Geom::Path & lastpath = _pathv.back(); + + Geom::Path newfirstpath(*path_it); + newfirstpath.setInitial(lastpath.finalPoint()); + lastpath.append( newfirstpath ); + + for (path_it++; path_it != c1->_pathv.end(); path_it++) { + _pathv.push_back( (*path_it) ); } + } else { - append(c1, TRUE); + append(c1, true); } - debug_check("SPCurve::append_continuous", this); - return this; } /** * Remove last segment of curve. * (Only used once in /src/pen-context.cpp) - * 2GEOMified */ void SPCurve::backspace() { - g_return_if_fail(this != NULL); - if ( is_empty() ) return; - if (_end > 0) { - _end -= 1; - if (_end > 0) { - NArtBpath *bp = _bpath + _end - 1; - if ((bp->code == NR_MOVETO) || - (bp->code == NR_MOVETO_OPEN) ) - { - _hascpt = true; - _posSet = true; - _closed = false; - _movePos = bp->c(3); - _end -= 1; - } - } - _bpath[_end].code = NR_END; - } - if ( !_pathv.back().empty() ) { _pathv.back().erase_last(); _pathv.back().close(false); } - - debug_check("SPCurve::backspace", this); -} - -/* Private methods */ - -/** - * Returns index of first NR_END bpath in array. - */ -static unsigned sp_bpath_length(NArtBpath const bpath[]) -{ - g_return_val_if_fail(bpath != NULL, FALSE); - - unsigned ret = 0; - while ( bpath[ret].code != NR_END ) { - ++ret; - } - ++ret; - - return ret; -} - -/** - * \brief - * - * \todo - * fixme: this is bogus -- it doesn't check for nr_moveto, which will indicate - * a closing of the subpath it's nonsense to talk about a path as a whole - * being closed, although maybe someone would want that for some other reason? - * Oh, also, if the bpath just ends, then it's *open*. I hope nobody is using - * this code for anything. - */ -static bool sp_bpath_closed(NArtBpath const bpath[]) -{ - g_return_val_if_fail(bpath != NULL, FALSE); - - for (NArtBpath const *bp = bpath; bp->code != NR_END; bp++) { - if (bp->code == NR_MOVETO_OPEN) { - return false; - } - } - - return true; -} - -/** - * Returns length of bezier segment. - */ -static double -bezier_len(NR::Point const &c0, - NR::Point const &c1, - NR::Point const &c2, - NR::Point const &c3, - double const threshold) -{ - /** \todo - * The SVG spec claims that a closed form exists, but for the moment I'll - * use a stupid algorithm. - */ - double const lbound = L2( c3 - c0 ); - double const ubound = L2( c1 - c0 ) + L2( c2 - c1 ) + L2( c3 - c2 ); - double ret; - if ( ubound - lbound <= threshold ) { - ret = .5 * ( lbound + ubound ); - } else { - NR::Point const a1( .5 * ( c0 + c1 ) ); - NR::Point const b2( .5 * ( c2 + c3 ) ); - NR::Point const c12( .5 * ( c1 + c2 ) ); - NR::Point const a2( .5 * ( a1 + c12 ) ); - NR::Point const b1( .5 * ( c12 + b2 ) ); - NR::Point const midpoint( .5 * ( a2 + b1 ) ); - double const rec_threshold = .625 * threshold; - ret = bezier_len(c0, a1, a2, midpoint, rec_threshold) + bezier_len(midpoint, b1, b2, c3, rec_threshold); - if (!(lbound - 1e-2 <= ret && ret <= ubound + 1e-2)) { - using NR::X; using NR::Y; - g_warning("ret=%f outside of expected bounds [%f, %f] for {(%.0f %.0f) (%.0f %.0f) (%.0f %.0f) (%.0f %.0f)}", - ret, lbound, ubound, c0[X], c0[Y], c1[X], c1[Y], c2[X], c2[Y], c3[X], c3[Y]); - } - } - return ret; -} - -/** - * Returns total length of curve, excluding length of closepath segments. - */ -double -sp_curve_distance_including_space(SPCurve const *const curve, double seg2len[]) -{ - g_return_val_if_fail(curve != NULL, 0.); - - double ret = 0.0; - - if ( curve->_bpath->code == NR_END ) { - return ret; - } - - NR::Point prev(curve->_bpath->c(3)); - for (guint i = 1; i < curve->_end; ++i) { - NArtBpath &p = curve->_bpath[i]; - double seg_len = 0; - switch (p.code) { - case NR_MOVETO_OPEN: - case NR_MOVETO: - case NR_LINETO: - seg_len = L2(p.c(3) - prev); - break; - - case NR_CURVETO: - seg_len = bezier_len(prev, p.c(1), p.c(2), p.c(3), 1.); - break; - - case NR_END: - return ret; - } - seg2len[i - 1] = seg_len; - ret += seg_len; - prev = p.c(3); - } - g_assert(!(ret < 0)); - return ret; -} - -/** - * Like sp_curve_distance_including_space(), but ensures that the - * result >= 1e-18: uses 1 per segment if necessary. - */ -double -sp_curve_nonzero_distance_including_space(SPCurve const *const curve, double seg2len[]) -{ - double const real_dist(sp_curve_distance_including_space(curve, seg2len)); - if (real_dist >= 1e-18) { - return real_dist; - } else { - unsigned const nSegs = SP_CURVE_LENGTH(curve) - 1; - for (unsigned i = 0; i < nSegs; ++i) { - seg2len[i] = 1.; - } - return (double) nSegs; - } } /** - * 2GEOMified + * TODO: add comments about what this method does and what assumptions are made and requirements are put on SPCurve + (2:08:18 AM) Johan: basically, i convert the path to pw +(2:08:27 AM) Johan: then i calculate an offset path +(2:08:29 AM) Johan: to move the knots +(2:08:36 AM) Johan: then i add it +(2:08:40 AM) Johan: then convert back to path +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) */ void -SPCurve::stretch_endpoints(NR::Point const &new_p0, NR::Point const &new_p1) +SPCurve::stretch_endpoints(Geom::Point const &new_p0, Geom::Point const &new_p1) { if (is_empty()) { return; } - g_assert(unsigned(SP_CURVE_LENGTH(this)) + 1 == sp_bpath_length(_bpath)); - unsigned const nSegs = SP_CURVE_LENGTH(this) - 1; - g_assert(nSegs != 0); - double *const seg2len = new double[nSegs]; - double const tot_len = sp_curve_nonzero_distance_including_space(this, seg2len); - NR::Point const offset0( new_p0 - first_point() ); - NR::Point const offset1( new_p1 - last_point() ); - _bpath->setC(3, new_p0); - double begin_dist = 0.; - for (unsigned si = 0; si < nSegs; ++si) { - double const end_dist = begin_dist + seg2len[si]; - NArtBpath &p = _bpath[1 + si]; - switch (p.code) { - case NR_LINETO: - case NR_MOVETO: - case NR_MOVETO_OPEN: - p.setC(3, p.c(3) + NR::Lerp(end_dist / tot_len, offset0, offset1)); - break; - - case NR_CURVETO: - for (unsigned ci = 1; ci <= 3; ++ci) { - p.setC(ci, p.c(ci) + Lerp((begin_dist + ci * seg2len[si] / 3.) / tot_len, offset0, offset1)); - } - break; - default: - g_assert_not_reached(); - } - - begin_dist = end_dist; - } - g_assert(L1(_bpath[nSegs].c(3) - new_p1) < 1.); - /* Explicit set for better numerical properties. */ - _bpath[nSegs].setC(3, new_p1); - delete [] seg2len; + Geom::Point const offset0( new_p0 - *first_point() ); + Geom::Point const offset1( new_p1 - *last_point() ); Geom::Piecewise > pwd2 = _pathv.front().toPwSb(); Geom::Piecewise arclength = Geom::arcLengthSb(pwd2); @@ -1480,68 +593,59 @@ SPCurve::stretch_endpoints(NR::Point const &new_p0, NR::Point const &new_p1) throw; } arclength *= 1./arclength.lastValue(); - Geom::Point const A( to_2geom(offset0) ); - Geom::Point const B( to_2geom(offset1) ); + Geom::Point const A( offset0 ); + Geom::Point const B( offset1 ); Geom::Piecewise offsetx = (arclength*-1.+1)*A[0] + arclength*B[0]; Geom::Piecewise offsety = (arclength*-1.+1)*A[1] + arclength*B[1]; Geom::Piecewise > offsetpath = Geom::sectionize( Geom::D2 >(offsetx, offsety) ); pwd2 += offsetpath; _pathv = Geom::path_from_piecewise( pwd2, 0.001 ); - - debug_check("SPCurve::stretch_endpoints", this); } /** * sets start of first path to new_p0, and end of first path to new_p1 - * 2GEOMified */ void -SPCurve::move_endpoints(NR::Point const &new_p0, NR::Point const &new_p1) +SPCurve::move_endpoints(Geom::Point const &new_p0, Geom::Point const &new_p1) { if (is_empty()) { return; } - unsigned const nSegs = SP_CURVE_LENGTH(this) - 1; - g_assert(nSegs != 0); - - _bpath->setC(3, new_p0); - _bpath[nSegs].setC(3, new_p1); - - _pathv.front().setInitial(to_2geom(new_p0)); - _pathv.front().setFinal(to_2geom(new_p1)); - - debug_check("SPCurve::move_endpoints", this); + _pathv.front().setInitial(new_p0); + _pathv.front().setFinal(new_p1); } /** * returns the number of nodes in a path, used for statusbar text when selecting an spcurve. - * 2GEOMified + * Sum of nodes in all the paths. When a path is closed, and its closing line segment is of zero-length, + * this function will not count the closing knot double (so basically ignores the closing line segment when it has zero length) */ guint SPCurve::nodes_in_path() const { - gint r = _end; - gint i = _length - 1; - if (i > r) i = r; // sometimes after switching from node editor length is wrong, e.g. f6 - draw - f2 - tab - f1, this fixes it - for (; i >= 0; i --) - if (_bpath[i].code == NR_MOVETO) - r --; - guint nr = 0; for(Geom::PathVector::const_iterator it = _pathv.begin(); it != _pathv.end(); ++it) { nr += (*it).size(); 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 - } - debug_check("SPCurve::nodes_in_path", r == (gint)nr); + // do not count closing knot double for zero-length closing line segments + // however, if the path is only a moveto, and is closed, do not subtract 1 (otherwise the result will be zero nodes) + if ( it->closed() + && ((*it).size() != 0) ) + { + Geom::Curve const &c = it->back_closed(); + if (are_near(c.initialPoint(), c.finalPoint())) { + nr--; + } + } + } - return r; + return nr; } /** * Adds p to the last point (and last handle if present) of the last path - * 2GEOMified */ void SPCurve::last_point_additive_move(Geom::Point const & p) @@ -1549,17 +653,6 @@ SPCurve::last_point_additive_move(Geom::Point const & p) if (is_empty()) { return; } - if (_end == 0) { - return; - } - NArtBpath * path = _bpath + _end - 1; - - if (path->code == NR_CURVETO) { - path->x2 += p[Geom::X]; - path->y2 += p[Geom::Y]; - } - path->x3 += p[Geom::X]; - path->y3 += p[Geom::Y]; _pathv.back().setFinal( _pathv.back().finalPoint() + p ); @@ -1570,8 +663,6 @@ SPCurve::last_point_additive_move(Geom::Point const & p) newcube.setPoint(2, newcube[2] + p); _pathv.back().replace( --_pathv.back().end(), newcube ); } - - debug_check("SPCurve::last_point_additive_move", this); } /*