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