1 /*
2 * Nearest Points Toy
3 *
4 * Authors:
5 * Nathan Hurst <njh at njhurst.com>
6 * Marco Cecchetti <mrcekets at gmail.com>
7 *
8 * Copyright 2008 authors
9 *
10 * This library is free software; you can redistribute it and/or
11 * modify it either under the terms of the GNU Lesser General Public
12 * License version 2.1 as published by the Free Software Foundation
13 * (the "LGPL") or, at your option, under the terms of the Mozilla
14 * Public License Version 1.1 (the "MPL"). If you do not alter this
15 * notice, a recipient may use your version of this file under either
16 * the MPL or the LGPL.
17 *
18 * You should have received a copy of the LGPL along with this library
19 * in the file COPYING-LGPL-2.1; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 * You should have received a copy of the MPL along with this library
22 * in the file COPYING-MPL-1.1
23 *
24 * The contents of this file are subject to the Mozilla Public License
25 * Version 1.1 (the "License"); you may not use this file except in
26 * compliance with the License. You may obtain a copy of the License at
27 * http://www.mozilla.org/MPL/
28 *
29 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY
30 * OF ANY KIND, either express or implied. See the LGPL or the MPL for
31 * the specific language governing rights and limitations.
32 */
33
34 #include <2geom/d2.h>
35 #include <2geom/sbasis.h>
36 #include <2geom/path.h>
37 #include <2geom/bezier-to-sbasis.h>
38
39 #include <toys/path-cairo.h>
40 #include <toys/toy-framework.h>
41
42
43 using namespace Geom;
44
45
46 class np_finder
47 {
48 public:
np_finder(cairo_t * _cr,D2<SBasis> const & _c1,D2<SBasis> const & _c2)49 np_finder(cairo_t* _cr, D2<SBasis> const& _c1, D2<SBasis> const& _c2)
50 : cr(_cr), c1(_c1), c2(_c2)
51 {
52 dc1 = derivative(c1);
53 dc2 = derivative(c2);
54 cd1 = dot(c1,dc1);
55 cd2 = dot(c2,dc2);
56 dsq = 10e30;
57 }
58
operator ()()59 void operator() ()
60 {
61 nearest_times_impl(0.5, 0, 1);
62 d = sqrt(dsq);
63 }
64
firstPoint() const65 Point firstPoint() const
66 {
67 return p1;
68 }
69
secondPoint() const70 Point secondPoint() const
71 {
72 return p2;
73 }
74
firstValue() const75 double firstValue() const
76 {
77 return t1;
78 }
79
secondValue() const80 double secondValue() const
81 {
82 return t2;
83 }
84
distance() const85 double distance() const
86 {
87 return d;
88 }
89
90 private:
nearest_times_impl(double t,double from=0,double to=1)91 void nearest_times_impl( double t, double from = 0, double to = 1 )
92 {
93 //std::cerr << "[" << from << "," << to << "] t: " << t << std::endl;
94
95 double st = t, et = t;
96 std::pair<double, double> npc = loc_nearest_times(t, from, to);
97 //std::cerr << "(" << npc.first << "," << npc.second << ")" << std::endl;
98 if ( npc.second != -1 && dsq > L2sq(c1(npc.first) - c2(npc.second)) )
99 {
100 t1 = npc.first;
101 t2 = npc.second;
102 p1 = c1(t1);
103 p2 = c2(t2);
104 dsq = L2sq(p1 - p2);
105 }
106 if (npc.first < t)
107 st = npc.first;
108 else
109 et = npc.first;
110 //std::cerr << "[" << st << "," << et << "]" << std::endl;
111 double d1 = std::fabs(st - from);
112 double d2 = std::fabs(to - et);
113 if ( d1 > EPSILON )
114 nearest_times_impl(from + d1/2, from, st);
115 if ( d2 > EPSILON )
116 nearest_times_impl(et + d2/2, et, to);
117 }
118
119 std::pair<double, double>
loc_nearest_times(double t,double from=0,double to=1)120 loc_nearest_times( double t, double from = 0, double to = 1 )
121 {
122 unsigned int i = 0;
123 std::pair<double, double> np(-1,-1);
124 std::pair<double, double> npf(from, -1);
125 std::pair<double, double> npt(to, -1);
126 double ct = t;
127 double pt = -1;
128 double s;
129 //cairo_set_source_rgba(cr, 1/(t+1), t*t, t, 1.0);
130 //cairo_move_to(cr, c1(t));
131 while( !are_near(ct, pt) )
132 {
133 ++i;
134 pt = ct;
135 s = nearest_time(c1(ct), c2, dc2, cd2);
136 //std::cerr << "s: " << s << std::endl;
137 //cairo_line_to(cr, c2(s));
138 ct = nearest_time(c2(s), c1, dc1, cd1, from, to);
139 //std::cerr << "t: " << t1 << std::endl;
140 //cairo_line_to(cr, c1(ct));
141 if ( ct < from ) return npf;
142 if ( ct > to ) return npt;
143 }
144 //std::cerr << "\n \n";
145 //std::cerr << "iterations: " << i << std::endl;
146 cairo_stroke(cr);
147 np.first = ct;
148 np.second = s;
149 return np;
150 }
151
nearest_time(Point const & p,D2<SBasis> const & c,D2<SBasis> const & dc,SBasis const & cd,double from=0,double to=1)152 double nearest_time( Point const& p, D2<SBasis> const&c, D2<SBasis> const& dc, SBasis const& cd, double from = 0, double to = 1 )
153 {
154 D2<SBasis> sbc = c - p;
155 SBasis dd = cd - dotp(p, dc);
156 std::vector<double> zeros = roots(dd);
157 double closest = from;
158 double distsq = L2sq(sbc(from));
159 for ( unsigned int i = 0; i < zeros.size(); ++i )
160 {
161 if ( distsq > L2sq(sbc(zeros[i])) )
162 {
163 closest = zeros[i];
164 distsq = L2sq(sbc(closest));
165 }
166 }
167 if ( distsq > L2sq(sbc(to)) )
168 closest = to;
169 return closest;
170 }
171
dotp(Point const & p,D2<SBasis> const & c)172 SBasis dotp(Point const& p, D2<SBasis> const& c)
173 {
174 SBasis d;
175 d.resize(c[X].size());
176 for ( unsigned int i = 0; i < c[0].size(); ++i )
177 {
178 for( unsigned int j = 0; j < 2; ++j )
179 d[i][j] = p[X] * c[X][i][j] + p[Y] * c[Y][i][j];
180 }
181 return d;
182 }
183
184 private:
185 static const Coord EPSILON = 10e-3;
186 cairo_t* cr;
187 D2<SBasis> const& c1, c2;
188 D2<SBasis> dc1, dc2;
189 SBasis cd1, cd2;
190 double t1, t2, d, dsq;
191 Point p1, p2;
192 };
193
194
195
196
197 class NearestPoints : public Toy
198 {
199 private:
draw(cairo_t * cr,std::ostringstream * notify,int width,int height,bool save,std::ostringstream * timer_stream)200 void draw( cairo_t *cr, std::ostringstream *notify,
201 int width, int height, bool save, std::ostringstream *timer_stream)
202 {
203 cairo_set_line_width (cr, 0.2);
204 D2<SBasis> A = handles_to_sbasis(handles.begin(), A_bez_ord-1);
205 cairo_d2_sb(cr, A);
206 D2<SBasis> B = handles_to_sbasis(handles.begin()+A_bez_ord, B_bez_ord-1);
207 cairo_d2_sb(cr, B);
208
209
210 np_finder np(cr, A, B);
211 np();
212 cairo_move_to(cr, np.firstPoint());
213 cairo_line_to(cr, np.secondPoint());
214 cairo_stroke(cr);
215 //std::cerr << "np: (" << np.firstValue() << "," << np.secondValue() << ")" << std::endl;
216
217 Toy::draw(cr, notify, width, height, save,timer_stream);
218 }
219
220 public:
NearestPoints(unsigned int _A_bez_ord,unsigned int _B_bez_ord)221 NearestPoints(unsigned int _A_bez_ord, unsigned int _B_bez_ord)
222 : A_bez_ord(_A_bez_ord), B_bez_ord(_B_bez_ord)
223 {
224 unsigned int total_handles = A_bez_ord + B_bez_ord;
225 for ( unsigned int i = 0; i < total_handles; ++i )
226 handles.push_back(Geom::Point(uniform()*400, uniform()*400));
227 }
228
229 private:
230 unsigned int A_bez_ord;
231 unsigned int B_bez_ord;
232 };
233
234
main(int argc,char ** argv)235 int main(int argc, char **argv)
236 {
237 unsigned int A_bez_ord=8;
238 unsigned int B_bez_ord=5;
239 if(argc > 2)
240 sscanf(argv[2], "%d", &B_bez_ord);
241 if(argc > 1)
242 sscanf(argv[1], "%d", &A_bez_ord);
243
244 init( argc, argv, new NearestPoints(A_bez_ord, B_bez_ord));
245 return 0;
246 }
247
248
249 /*
250 Local Variables:
251 mode:c++
252 c-file-style:"stroustrup"
253 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
254 indent-tabs-mode:nil
255 fill-column:99
256 End:
257 */
258 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:fileencoding=utf-8:textwidth=99 :
259