in reply to How to smooth values of an x,y,z array using Math::Spline
When I tried running
use warnings; use strict; use feature ':5.10'; my @accel = ([-0.7437,0.1118,-0.5367], [-0.5471,0.0062,-0.6338], [-0.7437,0.1216,-0.5255], [-0.4437,0.3216,-0.3255], ); # note: I changed from an array whose only element is an arrayref +of arrayrefs to an array who has n arrayrefs, to be a simple 2D array use Math::Spline; my (@x,@y,@z); my ($xmin, $xmax) = (9.99e99,-9.99e99); # for tracking min and max x v +alues # generate x, y, and z arrays for my $xyz (@accel) { push @x, $xyz->[0]; push @y, $xyz->[1]; push @z, $xyz->[2]; if($x[-1] < $xmin) { $xmin = $x[-1]; } if($x[-1] > $xmax) { $xmax = $x[-1]; } } # create spline calculators for x&y and x&z my $spline_xy = eval { Math::Spline::->new(\@x, \@y) } or do { die "xy +: $@" }; my $spline_xz = eval { Math::Spline::->new(\@x, \@z) } or do { die "yz +: $@" }; my @interp_accel = (); my $NSTEPS = 200; my $dx = ($xmax-$xmin)/$NSTEPS; # $NSTEPS+1 values from xmin to xmax, +inclusive for my $i (0..$NSTEPS) { my $x = $xmin + $dx * $i; my $y = defined($spline_xy) ? $spline_xy->evaluate($x) : undef; my $z = defined($spline_xz) ? $spline_xz->evaluate($x) : undef; push @interp_accel, [$x,$y,$z]; # store for later printf "interpolate # %d => [%s,%s,%s]\n", $i, map {$_//'<undef>'} + $x, $y, $z; # debug print }
... I found that $spline_xy=Math::Spline::->new(...) and $spline_xz both die when they are created; if I change the third row to having an x value of -0.6437 instead of -0.7437, it fixes it, so I am assuming that it's because those x-values are the same.
Were you intending the middle two of the four points to be points on the curve (so the interpolation would hit them exactly); or are they "control points", where the interpolation doesn't hit them directly, but instead only touches on the outer two, and the inner two just define directions (like this image from Bézier_curve)? Because, from what I can tell, Math::Spline makes sure that it hits the points from the instantiation-list rather than treats them as controls.
If what you really wanted was more along the lines of a Bézier curve, let me know, because that's pretty simple to code up without a module.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: How to smooth values of an x,y,z array using Math::Spline
by cormanaz (Deacon) on May 10, 2023 at 17:34 UTC | |
by pryrt (Abbot) on May 10, 2023 at 18:35 UTC |