-
Notifications
You must be signed in to change notification settings - Fork 1
/
lines2curves.pl
executable file
·431 lines (360 loc) · 15.4 KB
/
lines2curves.pl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
#!/usr/bin/perl -w
#-----------------------------------------------------------------------------
#
# lines2curves.pl
#
# This script post-processes Osmarender output to change lines in ways
# into smooth bezier curves.
#
# This is what it does for each 'way' in the svg:
# For each pair of lines that make up the way it will replace the two
# straight lines with an appropriate bezier curve through the point where
# the lines meet. Appropriate means that the curve is more localised the
# sharper the angle, and if the angle is less than 90 degrees it will not
# introduce a bezier curve.
#
# If the point where the lines meet has other ways that meet it (at a 'T'
# junction for example), it leaves those segments untouched (i.e. it
# doesn't introduce curves for those segments).
#
# Call it as follows:
#
# lines2curves.pl YourSVGFile.svg >SVGFileWithCurves.svg
#
#-----------------------------------------------------------------------------
#
# Copyright 2007 by Barry Crabtree
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA
#
#-----------------------------------------------------------------------------
use strict;
use Carp;
use Math::Vec qw(:terse);
my $min_angle = 0.5;
my $min_scale = 0;
#
# transform linear paths to curves
#
# Globals...
my $line_position = 0; # current line position in the svg file
my @svg_lines = (); # the lines from the svg file
my %point_is_in; # lookup for 'what ways is this point in?'
my %to_transform; # ways that need transforming
# first pass, read in the svg lines, build the %point_is_in @svg_lines &
# %to_transform structures.
while (<>) {
my $line = $_;
$svg_lines[$line_position] = $line;
if ( $line =~ m{(<path \s id=\"(?:way|area)_[^\"]+\" \s d=\") # the prefix of the path
([^\"]+) # the core path itself
(.*/>)$ # the rest
}x ) { # found a path
my $path_prefix = $1;
my $path_statement = $2;
my $path_suffix = $3;
my $path_points_ref = path_points($path_statement);
foreach my $point (@$path_points_ref) {
my $point_index = $point->[0].$point->[1];
my $way_list_ref = $point_is_in{$point_index};
push @$way_list_ref, $path_prefix;
$point_is_in{$point_index} = $way_list_ref;
}
$to_transform{$path_prefix}
= [$line_position, $path_statement, $path_suffix, $path_points_ref];
}
$line_position++;
}
# Second pass, create the bezier curve versions of all the paths that have
# been found.
foreach my $path (keys %to_transform) {
# transform path_statment to curves
my $path_info_ref = $to_transform{$path};
my $line_index = $path_info_ref->[0];
my $path_statement = $path_info_ref->[1];
my $path_suffix = $path_info_ref->[2];
my $transformed_path = curvify_path($path_statement, $path);
$svg_lines[$line_index] = $path.$transformed_path.$path_suffix."\n";
}
# print out the transformed svg file.
foreach my $line (@svg_lines) {
print $line;
}
# Get all the points in a path, removing duplicates along the way...
sub path_points {
my $path_string = shift;
my $path_points_ref;
# there may be multiple moves in a path so get each one
my @move_segments = split /M\s*/, $path_string;
foreach my $move_segment (@move_segments) {
$move_segment =~ s/Z//g;
next if !$move_segment; # there is no pre-seg if there is only 1 'M'
# get all the points in the path
# in rare cases there is a space (' ') after the character 'L', e.g. L 1.13686837721616e-13 430.833460032803
# if you change the line below, you may need to change appropriate line in the sub curvify_path()
my $tmp_points_ref = [map [split /(?:\s|,)/, $_], split(/L[ ]*/, $move_segment)];
# stop those occasional divide by zero errors...
push @$path_points_ref, @$tmp_points_ref;
}
my $clean_points_ref = remove_duplicate_points($path_points_ref);
return $clean_points_ref;
}
sub remove_duplicate_points {
my $points_ref = shift;
my $clean_points_ref = [$points_ref->[0]];
shift @$points_ref;
foreach my $point_ref (@$points_ref) {
if ($point_ref->[0].$point_ref->[1] eq
$clean_points_ref->[-1][0].$clean_points_ref->[-1][1]) {
next;
}
push @$clean_points_ref, $point_ref;
}
if (scalar(@$points_ref)+1 != scalar(@$clean_points_ref)) {
}
return $clean_points_ref;
}
sub remove_spur_points {
my $points_ref = shift;
# spur points are when you get a way that goes A->B->A->C. The point 'B' is a
# spur point & we don't like 'em.
my $clean_points_ref = [$points_ref->[0]];
shift @$points_ref;
for(my $i=0; $i < scalar(@$points_ref)-1; $i++) {
if ($clean_points_ref->[-1][0].$clean_points_ref->[-1][1] ne
$points_ref->[$i+1][0].$points_ref->[$i+1][1]
) {
push @$clean_points_ref, $points_ref->[$i];
}
}
push @$clean_points_ref, $points_ref->[-1];
return $clean_points_ref;
}
sub dup_points {
my $points_ref = shift;
foreach my $p (@$points_ref) {
my $count = 0;
foreach my $q (@$points_ref) {
if (join('', @$p) eq join('', @$q)) {
$count++;
}
}
if ($count > 1) {
return 1;
}
}
return;
}
# splits up the path string and calls 'from_lines_to_curves' appropriately to
# build up a modified path string.
sub curvify_path {
my $path_string = shift;
my $way_id = shift;
my $tmp_string = $path_string;
$tmp_string =~ s/[^L]//g;
#
if (length($tmp_string) < 1) { # cant do much with a single line segment
return $path_string;
}
my $bezier_path_string = q{};
# there may be multiple moves in a path so get each one
my @move_segments = split /M\s*/, $path_string;
foreach my $move_segment (@move_segments) {
my $closed = 0;
if ($move_segment =~ m{(.*)Z}) {
$move_segment = $1;
$closed = 1;
}
next if !$move_segment; # there is no pre-seg if there is only 1 'M'
# get all the points in the path
# in rare cases there is a space (' ') after the character 'L', e.g. L 1.13686837721616e-13 430.833460032803
# if you change the line below, you may need to change appropriate line in the sub path_points()
my @path_points = map [split /(?:\s|,)/, $_], split(/L[ ]*/, $move_segment);
if (0 && $way_id =~ /way_/ && dup_points(\@path_points)) { # *Martin*: why paths with dup point(s) are skipped?
$bezier_path_string .= $path_string;
} else {
$bezier_path_string .= 'M'.from_lines_to_curves(\@path_points, $way_id);
}
if ($closed) {
$bezier_path_string .= "Z";
}
}
return $bezier_path_string;
}
# When two ways meet at their ends, we need the second point in the 'other'
# way to get good control points in the bezier curve. This just returns the
# second point.
sub get_second_point {
my ($start_point, $way_id) = @_;
# uncomment the line below if you don't want the last segment in a way to be
# curved when it meets another way.
#
# return undef;
my $ways_ref = $point_is_in{$start_point};
if ($way_id =~ /area_/) { # areas are easier...
my $way_points = $to_transform{$way_id}->[3];
if ($way_points->[0][0].$way_points->[0][1] eq $start_point) {
return $way_points->[-1];
} else {
return $way_points->[0];
}
}
# now do normal ways...
# more than two ways meet - dont curve these.
return undef if @$ways_ref != 2;
my $otherway = ($ways_ref->[0] eq $way_id && $ways_ref->[1] ne $way_id)? $ways_ref->[1]: $ways_ref->[0];
# maybe there wasn't another way.
return undef if !$otherway;
# maybe this way has a loop in it.
#return undef if $otherway eq $way_id;
my $way_points = $to_transform{$otherway}->[3];
if ($start_point eq $way_points->[0][0].$way_points->[0][1]) {
return $way_points->[1];
} elsif ($start_point eq $way_points->[-1][0].$way_points->[-1][1]) {
return $way_points->[-2];
}
return undef;
}
# heavy work here. Takes the set of points in a path and generates a bezier
# version where appropriate.
sub from_lines_to_curves {
my $points_ref = shift;
my $way_id = shift;
my $cp_range = 0.5;
my $incremental_string = q{};
# Add a point at either end of the set of points to make it easy to
# generate the correct control points. If this way is standalone, then the
# ends of the way are extended straight back from the start/end
# points. If it joins another way, then use the second point in the
# other way as the end point. This will make ways that join connect
# smoothly - a point that Jochn Topf noticed.
#
my $start_point = $points_ref->[0][0].$points_ref->[0][1];
my $start_point2 = $points_ref->[1][0].$points_ref->[1][1];
my $end_point = $points_ref->[-1][0].$points_ref->[-1][1];
my $end_point2 = $points_ref->[-2][0].$points_ref->[-2][1];
my $penultimate_point_ref;
my $second_point_ref;
if ($start_point eq $end_point && $way_id =~ /area_/) {
$penultimate_point_ref = $points_ref->[-2];
} else {
$penultimate_point_ref = get_second_point($start_point, $way_id);
}
if ($start_point eq $end_point && $way_id =~ /area_/) {
$second_point_ref = $points_ref->[1];
} else {
$second_point_ref = get_second_point($end_point, $way_id);
}
if ($penultimate_point_ref && $penultimate_point_ref->[0].$penultimate_point_ref->[1] ne $start_point2) {
unshift @$points_ref, $penultimate_point_ref;
} else { # make a dummy point
unshift @$points_ref, [ $points_ref->[0][0]
-$points_ref->[1][0] + $points_ref->[0][0],
$points_ref->[0][1]
-$points_ref->[1][1] + $points_ref->[0][1] ];
}
if ($second_point_ref && $second_point_ref->[0].$second_point_ref->[1] ne $end_point2) {
push @$points_ref, $second_point_ref;
} else { # make a dummy point
push @$points_ref, [ $points_ref->[-1][0]
+$points_ref->[-1][0] - $points_ref->[-2][0],
$points_ref->[-1][1]
+$points_ref->[-1][1] - $points_ref->[-2][1] ];
}
$points_ref = remove_duplicate_points(remove_spur_points($points_ref));
my $points_in_line = scalar(@$points_ref);
my $current_point = 0;
my $path_start_ref = shift @$points_ref;
my $path_mid_ref = $points_ref->[0];
my $path_end_ref = $points_ref->[1];
my ($start_v, $mid_v, $end_v, $mid_start_v, $mid_end_v );
my ($start_mid_nv, $mid_start_nv, $mid_end_nv);
my $control_v;
my $control_scale;
my $pl;
foreach my $p (@$points_ref) {
$pl .= "\n\t".join(':', @$p);
}
# print "$way_id: $pl\n";
# go round each set of 3 points to generate the bezier points
while (@$points_ref >= 3) {
if (!$incremental_string) {
$incremental_string = q{ }.$path_mid_ref->[0].q{ }.$path_mid_ref->[1];
}
# decide to use real control points or not. We only use real control
# points if this node is only referenced in one 'way' or we are at the
# beginning/end of a way that only joins with one other way. This makes ways
# that 'T' sharp on the join.
if ( $way_id =~ /area_/
|| @{$point_is_in{$path_mid_ref->[0].$path_mid_ref->[1]}} == 1
|| ( $current_point == 0
&& @{$point_is_in{$path_mid_ref->[0].$path_mid_ref->[1]}} == 2)
|| ( $current_point == $points_in_line-3
&& @{$point_is_in{$path_mid_ref->[0].$path_mid_ref->[1]}} == 2)
) {
# print "\n\t->".join(':', @$path_start_ref)." ".join(':',
# @$path_mid_ref)." ".join(':',@$path_end_ref)."\n";
$incremental_string .= ' C ';
# work out control point 1 from $path_start, $path_mid & $path_end
$start_v = V($path_start_ref->[0], $path_start_ref->[1]);
$mid_v = V($path_mid_ref->[0], $path_mid_ref->[1]);
$end_v = V($path_end_ref->[0], $path_end_ref->[1]);
$start_mid_nv = U( ($mid_v-$start_v) + ($end_v-$mid_v) );
$mid_start_v = V($start_v->[0]-$mid_v->[0], $start_v->[1]-$mid_v->[1]);
$mid_end_v = V($end_v->[0]-$mid_v->[0], $end_v->[1]-$mid_v->[1]);
$control_scale = normalise_cp($mid_start_v, $mid_end_v);
$control_v = $mid_v + V($start_mid_nv->ScalarMult($control_scale*abs($end_v-$mid_v)*$cp_range));
$incremental_string .= $control_v->[0].','.$control_v->[1].q{ };
# move on a segment
$path_start_ref = $path_mid_ref;
$path_mid_ref = $path_end_ref;
$path_end_ref = $points_ref->[2];
shift @$points_ref;
# work out control point 2 from new $path_start, $path_mid & path_end
$start_v = V($path_start_ref->[0], $path_start_ref->[1]);
$mid_v = V($path_mid_ref->[0], $path_mid_ref->[1]);
$end_v = V($path_end_ref->[0], $path_end_ref->[1]);
$start_mid_nv = U( ($start_v-$mid_v) + ($mid_v-$end_v) );
$mid_start_v = V($start_v->[0]-$mid_v->[0], $start_v->[1]-$mid_v->[1]);
$mid_end_v = V($end_v->[0]-$mid_v->[0], $end_v->[1]-$mid_v->[1]);
$control_scale = normalise_cp($mid_start_v, $mid_end_v);
$control_v = $mid_v + V($start_mid_nv->ScalarMult($control_scale * abs($mid_v-$start_v)*$cp_range));
$incremental_string .= $control_v->[0].','.$control_v->[1].q{ };
$incremental_string .= $path_mid_ref->[0].q{ }.$path_mid_ref->[1].q{ };
} else { # make a straight line segment
$incremental_string .= 'L'.$path_end_ref->[0].','.$path_end_ref->[1].q{};
# move on a segment
$path_start_ref = $path_mid_ref;
$path_mid_ref = $path_end_ref;
$path_end_ref = $points_ref->[2];
shift @$points_ref;
}
}
return $incremental_string;
}
# if the angle of the control point is less than 90 degrees return 0
# between 90 & 180 degrees return a number between 0 & 1.
sub normalise_cp {
my ($start_v, $end_v) = @_;
my $PI = 3.1415926;
my $max_angle = $PI/2; # 90degrees
my $angle = $start_v->InnerAngle($end_v);
if ($angle < $PI*$min_angle) { # too small, so
return 0;
}
# angle is between $PI/4 and $PI/2
$angle = $angle - $PI*$min_angle;
return $angle / ($PI*$min_angle);
}