1 /**
2 * \file
3 * \brief nearest point routines for D2<SBasis> and Piecewise<D2<SBasis>>
4 *
5 * Authors:
6 *
7 * Marco Cecchetti <mrcekets at gmail.com>
8 *
9 * Copyright 2007-2008 authors
10 *
11 * This library is free software; you can redistribute it and/or
12 * modify it either under the terms of the GNU Lesser General Public
13 * License version 2.1 as published by the Free Software Foundation
14 * (the "LGPL") or, at your option, under the terms of the Mozilla
15 * Public License Version 1.1 (the "MPL"). If you do not alter this
16 * notice, a recipient may use your version of this file under either
17 * the MPL or the LGPL.
18 *
19 * You should have received a copy of the LGPL along with this library
20 * in the file COPYING-LGPL-2.1; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 * You should have received a copy of the MPL along with this library
23 * in the file COPYING-MPL-1.1
24 *
25 * The contents of this file are subject to the Mozilla Public License
26 * Version 1.1 (the "License"); you may not use this file except in
27 * compliance with the License. You may obtain a copy of the License at
28 * http://www.mozilla.org/MPL/
29 *
30 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY
31 * OF ANY KIND, either express or implied. See the LGPL or the MPL for
32 * the specific language governing rights and limitations.
33 */
36 #ifndef _NEAREST_POINT_H_
37 #define _NEAREST_POINT_H_
40 #include <vector>
42 #include <2geom/d2.h>
43 #include <2geom/piecewise.h>
44 #include <2geom/exception.h>
48 namespace Geom
49 {
51 /*
52 * Given a line L specified by a point A and direction vector v,
53 * return the point on L nearest to p. Note that the returned value
54 * is with respect to the _normalized_ direction of v!
55 */
56 inline double nearest_point(Point const &p, Point const &A, Point const &v)
57 {
58 Point d(p - A);
59 return d[0] * v[0] + d[1] * v[1];
60 }
62 ////////////////////////////////////////////////////////////////////////////////
63 // D2<SBasis> versions
65 /*
66 * Return the parameter t of a nearest point on the portion of the curve "c",
67 * related to the interval [from, to], to the point "p".
68 * The needed curve derivative "dc" is passed as parameter.
69 * The function return the first nearest point to "p" that is found.
70 */
71 double nearest_point( Point const& p,
72 D2<SBasis> const& c, D2<SBasis> const& dc,
73 double from = 0, double to = 1 );
75 inline
76 double nearest_point( Point const& p,
77 D2<SBasis> const& c,
78 double from = 0, double to = 1 )
79 {
80 return nearest_point(p, c, Geom::derivative(c), from, to);
81 }
83 /*
84 * Return the parameters t of all the nearest points on the portion of
85 * the curve "c", related to the interval [from, to], to the point "p".
86 * The needed curve derivative "dc" is passed as parameter.
87 */
88 std::vector<double>
89 all_nearest_points( Point const& p,
90 D2<SBasis> const& c, D2<SBasis> const& dc,
91 double from = 0, double to = 1 );
93 inline
94 std::vector<double>
95 all_nearest_points( Point const& p,
96 D2<SBasis> const& c,
97 double from = 0, double to = 1 )
98 {
99 return all_nearest_points(p, c, Geom::derivative(c), from, to);
100 }
103 ////////////////////////////////////////////////////////////////////////////////
104 // Piecewise< D2<SBasis> > versions
106 double nearest_point( Point const& p,
107 Piecewise< D2<SBasis> > const& c,
108 double from, double to );
110 inline
111 double nearest_point( Point const& p, Piecewise< D2<SBasis> > const& c )
112 {
113 return nearest_point(p, c, c.cuts[0], c.cuts[c.size()]);
114 }
117 std::vector<double>
118 all_nearest_points( Point const& p,
119 Piecewise< D2<SBasis> > const& c,
120 double from, double to );
122 inline
123 std::vector<double>
124 all_nearest_points( Point const& p, Piecewise< D2<SBasis> > const& c )
125 {
126 return all_nearest_points(p, c, c.cuts[0], c.cuts[c.size()]);
127 }
129 } // end namespace Geom
133 #endif /*_NEAREST_POINT_H_*/