From c883d7627a479c8c5b6a9f77b9841fa5631572ad Mon Sep 17 00:00:00 2001 From: Krzysztof Kosi??ski Date: Mon, 27 Apr 2015 19:39:29 -0400 Subject: 2Geom sync - initial commit (bzr r14059.2.1) --- src/2geom/pathvector.h | 318 ++++++++++++++++++++++++++++++++++++------------- 1 file changed, 235 insertions(+), 83 deletions(-) (limited to 'src/2geom/pathvector.h') diff --git a/src/2geom/pathvector.h b/src/2geom/pathvector.h index e875e915f..6765d6bc0 100644 --- a/src/2geom/pathvector.h +++ b/src/2geom/pathvector.h @@ -1,13 +1,11 @@ -/** - * \file - * \brief PathVector - std::vector containing Geom::Path. - * This file provides a set of operations that can be performed on PathVector, - * e.g. an affine transform. +/** @file + * @brief PathVector - a sequence of subpaths *//* * Authors: - * Johan Engelen + * Johan Engelen + * Krzysztof KosiƄski * - * Copyright 2008 authors + * Copyright 2008-2014 authors * * This library is free software; you can redistribute it and/or * modify it either under the terms of the GNU Lesser General Public @@ -36,93 +34,247 @@ #ifndef LIB2GEOM_SEEN_PATHVECTOR_H #define LIB2GEOM_SEEN_PATHVECTOR_H +#include +#include +#include #include <2geom/forward.h> #include <2geom/path.h> #include <2geom/transforms.h> namespace Geom { -typedef std::vector PathVector; - -/* general path transformation: */ -inline -void operator*= (PathVector & path_in, Affine const &m) { - for(PathVector::iterator it = path_in.begin(); it != path_in.end(); ++it) { - (*it) *= m; - } -} -inline -PathVector operator*(PathVector const & path_in, Affine const &m) { - PathVector ret(path_in); - ret *= m; - return ret; -} - -/* specific path transformations: Translation: - * This makes it possible to make optimized implementations for Translate transforms */ -inline -void operator*= (PathVector & path_in, Translate const &m) { - for(PathVector::iterator it = path_in.begin(); it != path_in.end(); ++it) { - (*it) *= m; - } -} -inline -PathVector operator*(PathVector const & path_in, Translate const &m) { - PathVector ret(path_in); - ret *= m; - return ret; -} - -/* user friendly approach to Translate transforms: just add an offset Point to the whole path */ -inline -void operator+=(PathVector &path_in, Point const &p) { - for(PathVector::iterator it = path_in.begin(); it != path_in.end(); ++it) { - (*it) *= Translate(p); - } -} -inline -PathVector operator+(PathVector const &path_in, Point const &p) { - PathVector ret(path_in); - ret *= Translate(p); - return ret; -} - -inline -Geom::Point initialPoint(PathVector const &path_in) +/** @brief Position (generalized time value) in the path vector. + * + * This class exists because mapping the range of multiple curves onto the same interval + * as the curve index, we lose some precision. For instance, a path with 16 curves will + * have 4 bits less precision than a path with 1 curve. If you need high precision results + * in long paths, use this class and related methods instead of the standard methods + * pointAt(), nearestTime() and so on. + * + * @ingroup Paths */ +struct PathVectorPosition + : public PathPosition + , boost::totally_ordered { - return path_in.front().initialPoint(); -} + size_type path_index; ///< Index of the path in the vector -inline -Geom::Point finalPoint(PathVector const &path_in) -{ - return path_in.back().finalPoint(); -} - -PathVector reverse_paths_and_order (PathVector const & path_in); - -OptRect bounds_fast( PathVector const & pv ); -OptRect bounds_exact( PathVector const & pv ); - -struct PathVectorPosition { - // pathvector[path_nr].pointAt(t) is the position - unsigned int path_nr; - double t; - PathVectorPosition() : - path_nr(0), - t(0) - {} - PathVectorPosition(unsigned int path_nr, - double t) : path_nr(path_nr), t(t) {} + PathVectorPosition() : PathPosition(0, 0), path_index(0) {} + PathVectorPosition(size_type _i, size_type _c, Coord _t) + : PathPosition(_c, _t), path_index(_i) {} + PathVectorPosition(size_type _i, PathPosition const &pos) + : PathPosition(pos), path_index(_i) {} + + bool operator<(PathVectorPosition const &other) const { + if (path_index < other.path_index) return true; + if (path_index == other.path_index) { + return static_cast(*this) < static_cast(other); + } + return false; + } + bool operator==(PathVectorPosition const &other) const { + return path_index == other.path_index + && static_cast(*this) == static_cast(other); + } + + PathPosition const &asPathPosition() const { + return *static_cast(this); + } +}; + +typedef Intersection PathVectorIntersection; +typedef PathVectorIntersection PVIntersection; ///< Alias to save typing + +template <> +struct ShapeTraits { + typedef PathVectorPosition TimeType; + //typedef PathVectorInterval IntervalType; + typedef PathVector AffineClosureType; + typedef PathVectorIntersection IntersectionType; }; -boost::optional nearestPoint(PathVector const & path_in, Point const& _point, double *distance_squared = NULL); -std::vector allNearestPoints(PathVector const & path_in, Point const& _point, double *distance_squared = NULL); +/** @brief Sequence of subpaths. + * + * This class corresponds to the SVG notion of a path: + * a sequence of any number of open or closed contiguous subpaths. + * Unlike Path, this class is closed under boolean operations. + * + * If you want to represent an arbitrary shape, this is the best class to use. + * Shapes with a boundary that is composed of only a single contiguous + * component can be represented with Path instead. + * + * @ingroup Paths + */ +class PathVector + : MultipliableNoncommutative< PathVector, Affine + , MultipliableNoncommutative< PathVector, Translate + , MultipliableNoncommutative< PathVector, Scale + , MultipliableNoncommutative< PathVector, Rotate + , MultipliableNoncommutative< PathVector, HShear + , MultipliableNoncommutative< PathVector, VShear + , MultipliableNoncommutative< PathVector, Zoom + , boost::addable< PathVector + , boost::equality_comparable< PathVector + > > > > > > > > > +{ + typedef std::vector Sequence; +public: + typedef PathVectorPosition Position; + typedef Sequence::iterator iterator; + typedef Sequence::const_iterator const_iterator; + typedef Sequence::size_type size_type; + typedef Path value_type; + typedef Path &reference; + typedef Path const &const_reference; + typedef Path *pointer; + typedef std::ptrdiff_t difference_type; + + PathVector() {} + PathVector(Path const &p) + : _data(1, p) + {} + template + PathVector(InputIter first, InputIter last) + : _data(first, last) + {} + + /// Check whether the vector contains any paths. + bool empty() const { return _data.empty(); } + /// Get the number of paths in the vector. + size_type size() const { return _data.size(); } + /// Get the total number of curves in the vector. + size_type curveCount() const; + + iterator begin() { return _data.begin(); } + iterator end() { return _data.end(); } + const_iterator begin() const { return _data.begin(); } + const_iterator end() const { return _data.end(); } + Path &operator[](size_type index) { + return _data[index]; + } + Path const &operator[](size_type index) const { + return _data[index]; + } + Path &at(size_type index) { + return _data.at(index); + } + Path const &at(size_type index) const { + return _data.at(index); + } + Path &front() { return _data.front(); } + Path const &front() const { return _data.front(); } + Path &back() { return _data.back(); } + Path const &back() const { return _data.back(); } + /// Append a path at the end. + void push_back(Path const &path) { + _data.push_back(path); + } + /// Remove the last path. + void pop_back() { + _data.pop_back(); + } + iterator insert(iterator pos, Path const &p) { + return _data.insert(pos, p); + } + template + void insert(iterator out, InputIter first, InputIter last) { + _data.insert(out, first, last); + } + /// Remove a path from the vector. + iterator erase(iterator i) { + return _data.erase(i); + } + /// Remove a range of paths from the vector. + iterator erase(iterator first, iterator last) { + return _data.erase(first, last); + } + /// Remove all paths from the vector. + void clear() { _data.clear(); } + /** @brief Change the number of paths. + * If the vector size increases, it is passed with paths that contain only + * a degenerate closing segment at (0,0). */ + void resize(size_type n) { _data.resize(n); } + /** @brief Reverse the direction of paths in the vector. + * @param reverse_paths If this is true, the order of paths is reversed as well; + * otherwise each path is reversed, but their order in the + * PathVector stays the same */ + void reverse(bool reverse_paths = true); + /** @brief Get a new vector with reversed direction of paths. + * @param reverse_paths If this is true, the order of paths is reversed as well; + * otherwise each path is reversed, but their order in the + * PathVector stays the same */ + PathVector reversed(bool reverse_paths = true) const; + + /// Get the range of allowed time values. + Interval timeRange() const { + Interval ret(0, curveCount()); return ret; + } + /** @brief Get the first point in the first path of the vector. + * This method will throw an exception if the vector doesn't contain any paths. */ + Point initialPoint() const { + return _data.front().initialPoint(); + } + /** @brief Get the last point in the last path of the vector. + * This method will throw an exception if the vector doesn't contain any paths. */ + Point finalPoint() const { + return _data.back().finalPoint(); + } + Path &pathAt(Coord t, Coord *rest = NULL); + Path const &pathAt(Coord t, Coord *rest = NULL) const; + Curve const &curveAt(Coord t, Coord *rest = NULL) const; + Coord valueAt(Coord t, Dim2 d) const; + Point pointAt(Coord t) const; + + Path &pathAt(Position const &pos) { + return const_cast(static_cast(this)->pathAt(pos)); + } + Path const &pathAt(Position const &pos) const { + return at(pos.path_index); + } + Curve const &curveAt(Position const &pos) const { + return at(pos.path_index).at(pos.curve_index); + } + Point pointAt(Position const &pos) const { + return at(pos.path_index).at(pos.curve_index).pointAt(pos.t); + } + Coord valueAt(Position const &pos, Dim2 d) const { + return at(pos.path_index).at(pos.curve_index).valueAt(pos.t, d); + } + + OptRect boundsFast() const; + OptRect boundsExact() const; + + template + BOOST_CONCEPT_REQUIRES(((TransformConcept)), (PathVector &)) + operator*=(T const &t) { + if (empty()) return *this; + for (iterator i = begin(); i != end(); ++i) { + *i *= t; + } + return *this; + } + + bool operator==(PathVector const &other) const { + return boost::range::equal(_data, other._data); + } + + std::vector intersect(PathVector const &other, Coord precision = EPSILON) const; + + /** @brief Determine the winding number at the specified point. + * This is simply the sum of winding numbers for constituent paths. */ + int winding(Point const &p) const; + + Coord nearestTime(Point const &p) const; + boost::optional nearestPosition(Point const &p, Coord *dist = NULL) const; + std::vector allNearestPositions(Point const &p, Coord *dist = NULL) const; + +private: + Position _getPosition(Coord t) const; + + Sequence _data; +}; -inline -Point pointAt(PathVector const & path_in, PathVectorPosition const &pvp) { - return path_in[pvp.path_nr].pointAt(pvp.t); -} +inline OptRect bounds_fast(PathVector const &pv) { return pv.boundsFast(); } +inline OptRect bounds_exact(PathVector const &pv) { return pv.boundsExact(); } } // end namespace Geom -- cgit v1.2.3 From cfa7054c950050095e596edd18fedad53e7ed636 Mon Sep 17 00:00:00 2001 From: Krzysztof Kosi??ski Date: Tue, 28 Apr 2015 19:02:19 -0400 Subject: Fix calls to Geom::cross() - sign change. (bzr r14059.2.2) --- src/2geom/pathvector.h | 2 ++ 1 file changed, 2 insertions(+) (limited to 'src/2geom/pathvector.h') diff --git a/src/2geom/pathvector.h b/src/2geom/pathvector.h index 6765d6bc0..9140e3872 100644 --- a/src/2geom/pathvector.h +++ b/src/2geom/pathvector.h @@ -257,6 +257,8 @@ public: return boost::range::equal(_data, other._data); } + void snapEnds(Coord precision = EPSILON); + std::vector intersect(PathVector const &other, Coord precision = EPSILON) const; /** @brief Determine the winding number at the specified point. -- cgit v1.2.3 From 6a9762c7603a32c7ec5cc0aaed8048d84daee6e8 Mon Sep 17 00:00:00 2001 From: Krzysztof Kosi??ski Date: Thu, 30 Apr 2015 11:17:07 +0200 Subject: Update 2Geom to r2347 (bzr r14059.2.3) --- src/2geom/pathvector.h | 53 +++++++++++++++++++++++++------------------------- 1 file changed, 26 insertions(+), 27 deletions(-) (limited to 'src/2geom/pathvector.h') diff --git a/src/2geom/pathvector.h b/src/2geom/pathvector.h index 9140e3872..375c4f0a0 100644 --- a/src/2geom/pathvector.h +++ b/src/2geom/pathvector.h @@ -43,7 +43,7 @@ namespace Geom { -/** @brief Position (generalized time value) in the path vector. +/** @brief Generalized time value in the path vector. * * This class exists because mapping the range of multiple curves onto the same interval * as the curve index, we lose some precision. For instance, a path with 16 curves will @@ -52,41 +52,41 @@ namespace Geom { * pointAt(), nearestTime() and so on. * * @ingroup Paths */ -struct PathVectorPosition - : public PathPosition - , boost::totally_ordered +struct PathVectorTime + : public PathTime + , boost::totally_ordered { size_type path_index; ///< Index of the path in the vector - PathVectorPosition() : PathPosition(0, 0), path_index(0) {} - PathVectorPosition(size_type _i, size_type _c, Coord _t) - : PathPosition(_c, _t), path_index(_i) {} - PathVectorPosition(size_type _i, PathPosition const &pos) - : PathPosition(pos), path_index(_i) {} + PathVectorTime() : PathTime(0, 0), path_index(0) {} + PathVectorTime(size_type _i, size_type _c, Coord _t) + : PathTime(_c, _t), path_index(_i) {} + PathVectorTime(size_type _i, PathTime const &pos) + : PathTime(pos), path_index(_i) {} - bool operator<(PathVectorPosition const &other) const { + bool operator<(PathVectorTime const &other) const { if (path_index < other.path_index) return true; if (path_index == other.path_index) { - return static_cast(*this) < static_cast(other); + return static_cast(*this) < static_cast(other); } return false; } - bool operator==(PathVectorPosition const &other) const { + bool operator==(PathVectorTime const &other) const { return path_index == other.path_index - && static_cast(*this) == static_cast(other); + && static_cast(*this) == static_cast(other); } - PathPosition const &asPathPosition() const { - return *static_cast(this); + PathTime const &asPathTime() const { + return *static_cast(this); } }; -typedef Intersection PathVectorIntersection; +typedef Intersection PathVectorIntersection; typedef PathVectorIntersection PVIntersection; ///< Alias to save typing template <> struct ShapeTraits { - typedef PathVectorPosition TimeType; + typedef PathVectorTime TimeType; //typedef PathVectorInterval IntervalType; typedef PathVector AffineClosureType; typedef PathVectorIntersection IntersectionType; @@ -118,7 +118,7 @@ class PathVector { typedef std::vector Sequence; public: - typedef PathVectorPosition Position; + typedef PathVectorTime Position; typedef Sequence::iterator iterator; typedef Sequence::const_iterator const_iterator; typedef Sequence::size_type size_type; @@ -224,19 +224,19 @@ public: Coord valueAt(Coord t, Dim2 d) const; Point pointAt(Coord t) const; - Path &pathAt(Position const &pos) { + Path &pathAt(PathVectorTime const &pos) { return const_cast(static_cast(this)->pathAt(pos)); } - Path const &pathAt(Position const &pos) const { + Path const &pathAt(PathVectorTime const &pos) const { return at(pos.path_index); } - Curve const &curveAt(Position const &pos) const { + Curve const &curveAt(PathVectorTime const &pos) const { return at(pos.path_index).at(pos.curve_index); } - Point pointAt(Position const &pos) const { + Point pointAt(PathVectorTime const &pos) const { return at(pos.path_index).at(pos.curve_index).pointAt(pos.t); } - Coord valueAt(Position const &pos, Dim2 d) const { + Coord valueAt(PathVectorTime const &pos, Dim2 d) const { return at(pos.path_index).at(pos.curve_index).valueAt(pos.t, d); } @@ -265,12 +265,11 @@ public: * This is simply the sum of winding numbers for constituent paths. */ int winding(Point const &p) const; - Coord nearestTime(Point const &p) const; - boost::optional nearestPosition(Point const &p, Coord *dist = NULL) const; - std::vector allNearestPositions(Point const &p, Coord *dist = NULL) const; + boost::optional nearestTime(Point const &p, Coord *dist = NULL) const; + std::vector allNearestTimes(Point const &p, Coord *dist = NULL) const; private: - Position _getPosition(Coord t) const; + PathVectorTime _factorTime(Coord t) const; Sequence _data; }; -- cgit v1.2.3 From e110371c4d69ea0a407d7500b156cf373109787b Mon Sep 17 00:00:00 2001 From: Krzysztof Kosi??ski Date: Fri, 8 May 2015 08:29:58 +0200 Subject: Fix node editing problems (bzr r14059.2.4) --- src/2geom/pathvector.h | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) (limited to 'src/2geom/pathvector.h') diff --git a/src/2geom/pathvector.h b/src/2geom/pathvector.h index 375c4f0a0..955cd1d37 100644 --- a/src/2geom/pathvector.h +++ b/src/2geom/pathvector.h @@ -112,9 +112,8 @@ class PathVector , MultipliableNoncommutative< PathVector, HShear , MultipliableNoncommutative< PathVector, VShear , MultipliableNoncommutative< PathVector, Zoom - , boost::addable< PathVector , boost::equality_comparable< PathVector - > > > > > > > > > + > > > > > > > > { typedef std::vector Sequence; public: -- cgit v1.2.3 From 25fa09178b7d0d0befa708e93ea5316ef381caa0 Mon Sep 17 00:00:00 2001 From: Krzysztof Kosi??ski Date: Fri, 22 May 2015 10:23:27 +0200 Subject: Update to 2Geom revision 2396 (bzr r14059.2.16) --- src/2geom/pathvector.h | 2 ++ 1 file changed, 2 insertions(+) (limited to 'src/2geom/pathvector.h') diff --git a/src/2geom/pathvector.h b/src/2geom/pathvector.h index 955cd1d37..6636cbf2e 100644 --- a/src/2geom/pathvector.h +++ b/src/2geom/pathvector.h @@ -276,6 +276,8 @@ private: inline OptRect bounds_fast(PathVector const &pv) { return pv.boundsFast(); } inline OptRect bounds_exact(PathVector const &pv) { return pv.boundsExact(); } +std::ostream &operator<<(std::ostream &out, PathVector const &pv); + } // end namespace Geom #endif // LIB2GEOM_SEEN_PATHVECTOR_H -- cgit v1.2.3 From 60437ac397d41678daba5daece227240e8ddd364 Mon Sep 17 00:00:00 2001 From: Krzysztof Kosi??ski Date: Sat, 4 Jul 2015 17:25:59 +0200 Subject: Upgrade to 2Geom r2413 (bzr r14059.2.18) --- src/2geom/pathvector.h | 5 +++++ 1 file changed, 5 insertions(+) (limited to 'src/2geom/pathvector.h') diff --git a/src/2geom/pathvector.h b/src/2geom/pathvector.h index 6636cbf2e..108f2aa05 100644 --- a/src/2geom/pathvector.h +++ b/src/2geom/pathvector.h @@ -81,6 +81,11 @@ struct PathVectorTime } }; +inline std::ostream &operator<<(std::ostream &os, PathVectorTime const &pvt) { + os << pvt.path_index << ": " << pvt.asPathTime(); + return os; +} + typedef Intersection PathVectorIntersection; typedef PathVectorIntersection PVIntersection; ///< Alias to save typing -- cgit v1.2.3