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