Added all remaining gists.

This commit is contained in:
Miguel Astor
2023-06-21 21:40:51 -04:00
parent 22ff5bfa25
commit b0ca706a25
26 changed files with 892 additions and 0 deletions

1
read_off.pl/README.md Normal file
View File

@@ -0,0 +1 @@
A little project to learn the basics of Perl. A Perl script that reads a 3D model in the OFF file format and spits it's data to standard output.

84
read_off.pl/read_off.pl Normal file
View File

@@ -0,0 +1,84 @@
#! /usr/bin/env perl
use 5.010;
use strict;
use warnings;
my $status = 0;
foreach my $arg (0 .. $#ARGV){
say "---------------------------------------------------------------------";
say "FILE: $ARGV[$arg]";
say "---------------------------------------------------------------------";
$status = &read_off_file($ARGV[$arg]);
}
exit $status;
##########################
# SUBROUTINE DEFINITIONS #
##########################
sub read_off_file {
return unless defined wantarray;
# Local variable definitions.
my $v_ind = 0;
my $f_ind = 0;
my $status = 0;
my $meta_read = 0;
my $arg = shift @_;
open (my $file, '<', $arg) or die "$arg: $!";
my $row = <$file>;
chomp $row;
# Check if the first line in the file matches the "OFF" string. If it
# doesn't then this file is not a valid OFF file.
if(not $row =~ /OFF/){
say "$arg is not an OFF file.";
$status = 1;
last;
}else{
while($row = <$file>){
chomp $row;
# Remove leading and trailing whitespace.
$row =~ s/^\s+|\s+$//g;
if(not $meta_read and $row =~ /^(((\d+)(\s+)){2})(\d+)$/){
# If the line is exactly three integers separated by arbitrary
# whitespace then it is the number of vertices, faces and edges.
my ($num_v, $num_f, $num_e) = split(/\s+/, $row);
say "Number of vertices: $num_v";
say "Number of faces: $num_f";
say "Number of edges: $num_e";
$meta_read = 1;
}elsif($row =~ /^(((\-?)(\d+)(\.(\d+))?(\s+)){2})((\-?)(\d+)(\.(\d+))?)$/){
# If the line has three numbers (integer or floating point)
# surrounded by arbitrary whitespace then it is a vertex.
my ($x, $y, $z) = split(/\s+/, $row);
say "Vertex #$v_ind: ($x, $y, $z)";
$v_ind = $v_ind + 1;
}elsif($row =~ /(\d+)(((\s+)(\d+)){3,})/){
# If the line has one integer followed by at least three or more
# integers, all of them separated by arbitrary whitespace, then
# it is a polygon.
my @face = split(/\s+/, $row);
print "Face $f_ind: Num vertices = $face[0] - ";
print "v::$face[$_] " foreach 1 .. $#face;
print "\n";
$f_ind = $f_ind + 1;
}elsif($row =~ /^(\s*)#(.*)/ or $row =~ /^(s*)$/){
say "Skipping comment or empty line.";
}else{
say "Unrecognized line.";
}
}
}
close $file;
return $status;
}