Code

update 2geom
authorjohanengelen <johanengelen@users.sourceforge.net>
Sun, 3 Aug 2008 18:45:24 +0000 (18:45 +0000)
committerjohanengelen <johanengelen@users.sourceforge.net>
Sun, 3 Aug 2008 18:45:24 +0000 (18:45 +0000)
src/2geom/matrix.h
src/2geom/nearest-point.cpp
src/2geom/nearest-point.h
src/2geom/path.cpp

index dcf765c50ae9dde84cc1ab1911049c0be81bd8bc..c81efab68a452576dffab8b7eb1fc8582ff09f84 100644 (file)
@@ -73,13 +73,15 @@ class Matrix {
 
     Point xAxis() const;
     Point yAxis() const;
-    Point translation() const;
     void setXAxis(Point const &vec);
     void setYAxis(Point const &vec);
+
+    Point translation() const;
     void setTranslation(Point const &loc);
 
     double expansionX() const;
     double expansionY() const;
+    inline Point expansion() const { return Point(expansionX(), expansionY()); }
     void setExpansionX(double val);
     void setExpansionY(double val);
 
index 7fef8c12002fd216b51952ab6b9d7a472a39b82f..705767f13d58bddec830726981f7b78a441f1abc 100644 (file)
@@ -2,9 +2,9 @@
  * nearest point routines for D2<SBasis> and Piecewise<D2<SBasis>>
  *
  * Authors:
- *             
+ *
  *             Marco Cecchetti <mrcekets at gmail.com>
- * 
+ *
  * Copyright 2007-2008  authors
  *
  * This library is free software; you can redistribute it and/or
@@ -33,6 +33,8 @@
 
 
 #include <2geom/nearest-point.h>
+#include <algorithm>
+
 
 namespace Geom
 {
@@ -41,15 +43,15 @@ namespace Geom
 // D2<SBasis> versions
 
 /*
- * Return the parameter t of a nearest point on the portion of the curve "c", 
+ * Return the parameter t of a nearest point on the portion of the curve "c",
  * related to the interval [from, to], to the point "p".
  * The needed curve derivative "dc" is passed as parameter.
  * The function return the first nearest point to "p" that is found.
  */
 
-double nearest_point( Point const& p, 
-                                         D2<SBasis> const& c, 
-                                         D2<SBasis> const& dc, 
+double nearest_point( Point const& p,
+                                         D2<SBasis> const& c,
+                                         D2<SBasis> const& dc,
                              double from, double to )
 {
        if ( from > to ) std::swap(from, to);
@@ -57,10 +59,10 @@ double nearest_point( Point const& p,
        {
                THROW_RANGEERROR("[from,to] interval out of bounds");
        }
-
-       SBasis dd = dot(c - p, dc);     
+       if (c.isConstant()) return from;
+       SBasis dd = dot(c - p, dc);
        std::vector<double> zeros = Geom::roots(dd);
-       
+
        double closest = from;
        double min_dist_sq = L2sq(c(from) - p);
        double distsq;
@@ -80,15 +82,15 @@ double nearest_point( Point const& p,
 }
 
 /*
- * Return the parameters t of all the nearest points on the portion of 
+ * Return the parameters t of all the nearest points on the portion of
  * the curve "c", related to the interval [from, to], to the point "p".
  * The needed curve derivative "dc" is passed as parameter.
  */
 
-std::vector<double> 
-all_nearest_points( Point const& p, 
-                   D2<SBasis> const& c, 
-                   D2<SBasis> const& /*dc*/, 
+std::vector<double>
+all_nearest_points( Point const& p,
+                   D2<SBasis> const& c,
+                   D2<SBasis> const& dc,
                    double from, double to )
 {
        std::swap(from, to);
@@ -99,8 +101,13 @@ all_nearest_points( Point const& p,
        }
 
        std::vector<double> result;
-       SBasis dd = dot(c - p, Geom::derivative(c));
-       
+       if (c.isConstant())
+       {
+           result.push_back(from);
+           return result;
+       }
+       SBasis dd = dot(c - p, dc);
+
        std::vector<double> zeros = Geom::roots(dd);
        std::vector<double> candidates;
        candidates.push_back(from);
@@ -137,7 +144,7 @@ all_nearest_points( Point const& p,
 // Piecewise< D2<SBasis> > versions
 
 
-double nearest_point( Point const& p,  
+double nearest_point( Point const& p,
                                          Piecewise< D2<SBasis> > const& c,
                              double from, double to )
 {
@@ -146,7 +153,7 @@ double nearest_point( Point const& p,
        {
                THROW_RANGEERROR("[from,to] interval out of bounds");
        }
-       
+
        unsigned int si = c.segN(from);
        unsigned int ei = c.segN(to);
        if ( si == ei )
@@ -190,9 +197,9 @@ double nearest_point( Point const& p,
        return c.mapToDomain(nearest, ni);
 }
 
-std::vector<double> 
-all_nearest_points( Point const& p, 
-                    Piecewise< D2<SBasis> > const& c, 
+std::vector<double>
+all_nearest_points( Point const& p,
+                    Piecewise< D2<SBasis> > const& c,
                            double from, double to )
 {
        if ( from > to ) std::swap(from, to);
@@ -200,12 +207,12 @@ all_nearest_points( Point const& p,
        {
                THROW_RANGEERROR("[from,to] interval out of bounds");
        }
-       
+
        unsigned int si = c.segN(from);
        unsigned int ei = c.segN(to);
        if ( si == ei )
        {
-               std::vector<double>     all_nearest = 
+               std::vector<double>     all_nearest =
                        all_nearest_points(p, c[si], c.segT(from, si), c.segT(to, si));
                for ( unsigned int i = 0; i < all_nearest.size(); ++i )
                {
@@ -270,6 +277,8 @@ all_nearest_points( Point const& p,
                        all_nearest.push_back( c.mapToDomain(all_np[i][j], ni[i]) );
                }
        }
+       all_nearest.erase(std::unique(all_nearest.begin(), all_nearest.end()),
+                         all_nearest.end());
        return all_nearest;
 }
 
index c8588214bfc3d1f35e7e5c00c77d01374c858229..0ca28c4f218fcfdb8d024cac2da94e23d182b43a 100644 (file)
@@ -3,9 +3,9 @@
  *
  *
  * Authors:
- *             
+ *
  *             Marco Cecchetti <mrcekets at gmail.com>
- * 
+ *
  * Copyright 2007-2008  authors
  *
  * This library is free software; you can redistribute it and/or
@@ -63,37 +63,37 @@ inline double nearest_point(Point const &p, Point const &A, Point const &v)
 // D2<SBasis> versions
 
 /*
- * Return the parameter t of a nearest point on the portion of the curve "c", 
+ * Return the parameter t of a nearest point on the portion of the curve "c",
  * related to the interval [from, to], to the point "p".
  * The needed curve derivative "dc" is passed as parameter.
  * The function return the first nearest point to "p" that is found.
  */
 double nearest_point( Point const& p,
-                             D2<SBasis> const& c, D2<SBasis> const& dc, 
+                             D2<SBasis> const& c, D2<SBasis> const& dc,
                              double from = 0, double to = 1 );
 
 inline
-double nearest_point( Point const& p, 
-                             D2<SBasis> const& c, 
+double nearest_point( Point const& p,
+                             D2<SBasis> const& c,
                              double from = 0, double to = 1 )
 {
        return nearest_point(p, c, Geom::derivative(c), from, to);
 }
 
 /*
- * Return the parameters t of all the nearest points on the portion of 
+ * Return the parameters t of all the nearest points on the portion of
  * the curve "c", related to the interval [from, to], to the point "p".
  * The needed curve derivative "dc" is passed as parameter.
  */
-std::vector<double> 
-all_nearest_points( Point const& p, 
-                           D2<SBasis> const& c, D2<SBasis> const& dc, 
+std::vector<double>
+all_nearest_points( Point const& p,
+                           D2<SBasis> const& c, D2<SBasis> const& dc,
                            double from = 0, double to = 1 );
 
 inline
-std::vector<double> 
-all_nearest_points( Point const& p, 
-                           D2<SBasis> const& c, 
+std::vector<double>
+all_nearest_points( Point const& p,
+                           D2<SBasis> const& c,
                            double from = 0, double to = 1 )
 {
        return all_nearest_points(p, c,  Geom::derivative(c), from, to);
@@ -103,25 +103,25 @@ all_nearest_points( Point const& p,
 ////////////////////////////////////////////////////////////////////////////////
 // Piecewise< D2<SBasis> > versions
 
-double nearest_point( Point const& p, 
-                             Piecewise< D2<SBasis> > const& c, 
+double nearest_point( Point const& p,
+                             Piecewise< D2<SBasis> > const& c,
                              double from, double to );
 
 inline
-double nearest_point( Point const& p, Piecewise< D2<SBasis> > const& c ) 
+double nearest_point( Point const& p, Piecewise< D2<SBasis> > const& c )
 {
        return nearest_point(p, c, c.cuts[0], c.cuts[c.size()]);
 }
 
 
-std::vector<double> 
-all_nearest_points( Point const& p, 
-                                       Piecewise< D2<SBasis> > const& c, 
+std::vector<double>
+all_nearest_points( Point const& p,
+                                       Piecewise< D2<SBasis> > const& c,
                            double from, double to );
 
 inline
-std::vector<double> 
-all_nearest_points( Point const& p, Piecewise< D2<SBasis> > const& c ) 
+std::vector<double>
+all_nearest_points( Point const& p, Piecewise< D2<SBasis> > const& c )
 {
        return all_nearest_points(p, c, c.cuts[0], c.cuts[c.size()]);
 }
index 5f321d5175be09153fc29c132393de6056800fe2..2943d6a9279142aec5dfe8ba73ae8678a51ddf30 100644 (file)
@@ -4,7 +4,7 @@
  * Authors:
  *             MenTaLguY <mental@rydia.net>
  *             Marco Cecchetti <mrcekets at gmail.com>
- * 
+ *
  * Copyright 2007-2008  authors
  *
  * This library is free software; you can redistribute it and/or
 
 
 #include <2geom/path.h>
+#include <algorithm>
+
 
 using namespace Geom::PathInternal;
 
-namespace Geom 
+namespace Geom
 {
 
 Rect Path::boundsFast() const {
@@ -98,7 +100,7 @@ Path &Path::operator*=(Matrix const &m) {
   return *this;
 }
 
-std::vector<double> 
+std::vector<double>
 Path::allNearestPoints(Point const& _point, double from, double to) const
 {
        if ( from > to ) std::swap(from, to);
@@ -125,7 +127,7 @@ Path::allNearestPoints(Point const& _point, double from, double to) const
        }
        if ( si == ei )
        {
-               std::vector<double>     all_nearest = 
+               std::vector<double>     all_nearest =
                        _path[si].allNearestPoints(_point, st, et);
                for ( unsigned int i = 0; i < all_nearest.size(); ++i )
                {
@@ -139,7 +141,7 @@ Path::allNearestPoints(Point const& _point, double from, double to) const
        std::vector<unsigned int> ni;
        ni.push_back(si);
        double dsq;
-       double mindistsq 
+       double mindistsq
                = distanceSq( _point, _path[si].pointAt( all_np.front().front() ) );
        Rect bb;
        for ( unsigned int i = si + 1; i < ei; ++i )
@@ -191,7 +193,9 @@ Path::allNearestPoints(Point const& _point, double from, double to) const
                        all_nearest.push_back( ni[i] + all_np[i][j] );
                }
        }
-       return all_nearest;     
+       all_nearest.erase(std::unique(all_nearest.begin(), all_nearest.end()),
+                         all_nearest.end());
+       return all_nearest;
 }
 
 double Path::nearestPoint(Point const &_point, double from, double to, double *distance_squared) const
@@ -221,7 +225,8 @@ double Path::nearestPoint(Point const &_point, double from, double to, double *d
        if ( si == ei )
        {
                double nearest = _path[si].nearestPoint(_point, st, et);
-        *distance_squared = distanceSq(_point, _path[si].pointAt(nearest));
+               if (distance_squared != NULL)
+                   *distance_squared = distanceSq(_point, _path[si].pointAt(nearest));
                return si + nearest;
        }
 
@@ -259,9 +264,9 @@ double Path::nearestPoint(Point const &_point, double from, double to, double *d
                }
        }
 
-    if (distance_squared) {
+    if (distance_squared != NULL)
         *distance_squared = mindistsq;
-    }
+
        return ni + nearest;
 }