Instantly share code, notes, and snippets.

@hikiko hikiko/minandmax.pl
Created Jan 24, 2019

Embed
What would you like to do?
reads v x y z values from obj (supposing 1 model) to find the bounding sphere radius
#!/usr/bin/perl
use strict;
use warnings;
use POSIX;
use List::Util qw[min max];
my $fname = $ARGV[0];
if(not defined $fname) {
die "You must set the vectors filename.\n";
}
open(my $fh, $fname) or die "Could not open $fname: $!.\n";
my $min_x = POSIX::FLT_MAX;
my $min_y = POSIX::FLT_MAX;
my $min_z = POSIX::FLT_MAX;
my $max_x = POSIX::FLT_MIN;
my $max_y = POSIX::FLT_MIN;
my $max_z = POSIX::FLT_MIN;
while(!eof($fh)) {
defined(my $line = readline $fh) or die "Failed to read line\n";
my $num = qr/[-+]?[0-9]*\.?[0-9]+./;
if($line =~ m/^v\s($num)\s($num)\s($num)$/) {
my $x = $1;
my $y = $2;
my $z = $3;
$max_x = $max_x < $x ? $x : $max_x;
$max_y = $max_y < $y ? $y : $max_y;
$max_z = $max_z < $z ? $z : $max_z;
$min_x = $min_x > $x ? $x : $min_x;
$min_y = $min_y > $y ? $y : $min_y;
$min_z = $min_z > $z ? $z : $min_z;
}
}
close($fh);
print qq[max x: $max_x, max_y: $max_y, max_z: $max_z]."\n";
print qq[min x: $min_x, min_y: $min_y, min_z: $min_z]."\n";
my $xdist = $max_x - $min_x;
my $ydist = $max_y - $min_y;
my $zdist = $max_z - $min_z;
print qq[distances: $xdist $ydist $zdist\n];
my $max_dist = max($xdist, $ydist, $zdist);
print qq[bounding sphere radius: $max_dist\n];
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment