/usr/share/perl5/Math/PlanePath/File.pm is in libmath-planepath-perl 117-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 | # Copyright 2011, 2012, 2013, 2014 Kevin Ryde
# This file is part of Math-PlanePath.
#
# Math-PlanePath 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 3, or (at your option) any later
# version.
#
# Math-PlanePath 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 Math-PlanePath. If not, see <http://www.gnu.org/licenses/>.
# maybe file type for sequence of turns instead of x,y
package Math::PlanePath::File;
use 5.004;
use strict;
use Carp 'croak';
use vars '$VERSION', '@ISA';
$VERSION = 117;
use Math::PlanePath;
@ISA = ('Math::PlanePath');
use Math::PlanePath::Base::Generic
'round_nearest',
'is_infinite';
# uncomment this to run the ### lines
#use Devel::Comments;
sub n_start {
my ($self) = @_;
if (ref $self) {
_read($self);
}
return $self->SUPER::n_start;
}
sub x_negative { return _read($_[0])->{'x_negative'} }
sub y_negative { return _read($_[0])->{'y_negative'} }
sub figure { return _read($_[0])->{'figure'} }
use constant parameter_info_array =>
[ { name => 'filename',
display => 'Filename',
type => 'filename',
width => 40,
default => '',
description => 'File name to read.',
} ];
sub n_to_xy {
my ($self, $n) = @_;
if ($n < $self->{'n_start'}) {
return;
}
if (is_infinite($n)) {
return;
}
if (defined (my $x = _read($self)->{'x_array'}->[$n])) {
return ($x, $self->{'y_array'}->[$n]);
}
return;
}
sub xy_to_n {
my ($self, $x, $y) = @_;
# lazy xy_hash creation
if (! defined $self->{'xy_hash'}) {
my %xy_hash;
_read($self)->{'xy_hash'} = \%xy_hash;
my $x_array = $self->{'x_array'};
my $y_array = $self->{'y_array'};
for (my $n = 0; $n <= $#$x_array; $n++) {
if (defined (my $nx = $x_array->[$n])) {
$xy_hash{"$nx,$y_array->[$n]"} = $n;
# && $nx == int($nx)
# if ($ny == int($ny)) {
# }
}
}
}
{
my $key = ($self->{'figure'} eq 'square'
? round_nearest($x).','.round_nearest($y)
: "$x,$y");
if (defined (my $n = _read($self)->{'xy_hash'}->{$key})) {
return $n;
}
}
my $x_array = $self->{'x_array'};
my $y_array = $self->{'y_array'};
for (my $n = 0; $n <= $#$x_array; $n++) {
defined (my $nx = $x_array->[$n]) or next;
my $ny = $y_array->[$n];
if (($x-$nx)**2 + ($y-$ny)**2 <= .25) {
return $n;
}
}
return undef;
}
# exact
sub rect_to_n_range {
my ($self) = @_;
_read($self);
return ($self->{'n_start'}, $self->{'n_last'});
}
my $num = "-?(?:\\.[0-9]+|[0-9]+(?:\\.[0-9]*)?)(?:[eE]-?[0-9]+)?";
sub _read {
my ($self) = @_;
if (defined $self->{'n_start'}) {
return $self;
}
my $n = 1;
$self->{'n_start'} = $n;
$self->{'n_last'} = $n-1; # default no range
$self->{'x_negative'} = 0;
$self->{'y_negative'} = 0;
$self->{'figure'} = 'square';
my $filename = $self->{'filename'};
if (! defined $filename || $filename =~ /^\s*$/) {
return $self;
}
my $fh;
($] >= 5.006
? open $fh, '<', $filename
: open $fh, "< $filename")
or croak "Cannot open ",$filename,": ",$!;
my $n_start;
my @x_array;
my @y_array;
my $x_negative = 0;
my $y_negative = 0;
my $any_frac = 0;
while (my $line = <$fh>) {
$line =~ /^\s*-?\.?[0-9]/
or next;
$line =~ /^\s*($num)[ \t,]+($num)([ \t,]+($num))?/o
or do {
warn $filename,':',$.,": File unrecognised line: ",$line;
next;
};
my ($x,$y);
if (defined $4) {
$n = $1;
$x = $2;
$y = $4;
} else {
$x = $1;
$y = $2;
}
$x_array[$n] = $x;
$y_array[$n] = $y;
$x_negative ||= ($x < 0);
$y_negative ||= ($y < 0);
$any_frac ||= ($x != int($x) || $y != int($y));
if (! defined $n_start || $n < $n_start) { $n_start = $n; }
### $x
### $y
$n++;
}
close $fh
or croak "Error closing ",$filename,": ",$!;
$self->{'x_array'} = \@x_array;
$self->{'y_array'} = \@y_array;
$self->{'x_negative'} = $x_negative;
$self->{'y_negative'} = $y_negative;
$self->{'n_start'} = $n_start;
$self->{'n_last'} = $#x_array; # last n index
if ($any_frac) { $self->{'figure'} = 'circle' }
return $self;
}
1;
__END__
=for stopwords Ryde Math-PlanePath PlanePath
=head1 NAME
Math::PlanePath::File -- points from a file
=head1 SYNOPSIS
use Math::PlanePath::File;
my $path = Math::PlanePath::File->new (filename => 'foo.txt');
my ($x, $y) = $path->n_to_xy (123);
=head1 DESCRIPTION
This path reads X,Y points from a file to present in PlanePath style. It's
slightly preliminary yet but is handy to get numbers from elsewhere into a
PlanePath program.
The intention is to be flexible about the file format and to auto-detect as
far as possible. Currently the only format is plain text, with an X,Y pair,
or N,X,Y triplet on each line
5,6 # X,Y
123 5 6 # N,X,Y
Numbers can be separated by a comma or just spaces and tabs. Lines not
starting with a number are ignored as comments (or blanks). N values must
be integers, but the X,Y values can be fractions like 1.5 too, including
exponential floating point 1500.5e-1 etc.
=head1 FUNCTIONS
See L<Math::PlanePath/FUNCTIONS> for behaviour common to all path classes.
=over 4
=item C<$path = Math::PlanePath::File-E<gt>new (filename =E<gt> "/my/file/name.txt")>
Create and return a new path object.
=item C<($x,$y) = $path-E<gt>n_to_xy ($n)>
Return the X,Y coordinates of point number C<$n> on the path.
=item C<$n = $path-E<gt>xy_to_n ($x,$y)>
Return the point number for coordinates C<$x,$y>.
In the current code an C<$x,$y> within a unit circle or square of a point
from the file gives that point. But perhaps in the future some attention
could be paid to apparent spacing of points closer than that.
=item C<$bool = $path-E<gt>x_negative()>
=item C<$bool = $path-E<gt>y_negative()>
Return true if there are any negative X or negative Y coordinates in the
file.
=item C<$n = $path-E<gt>n_start()>
Return the first N in the path. For files of just X,Y points the start is
N=1, for N,X,Y data it's the first N.
=item C<$str = $path-E<gt>figure()>
Return a string name of the figure (shape) intended to be drawn at each
C<$n> position. In the current code if all X,Y are integers then this is
"square", otherwise it's "circle". But perhaps that will change.
=back
=head1 SEE ALSO
L<Math::PlanePath>
=head1 HOME PAGE
L<http://user42.tuxfamily.org/math-planepath/index.html>
=head1 LICENSE
Copyright 2011, 2012, 2013, 2014 Kevin Ryde
This file is part of Math-PlanePath.
Math-PlanePath 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 3, or (at your option) any later
version.
Math-PlanePath 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
Math-PlanePath. If not, see <http://www.gnu.org/licenses/>.
=cut
|