Advertisement
Guest User

binvox2ldr

a guest
Apr 7th, 2014
513
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Perl 2.86 KB | None | 0 0
  1. #!/usr/bin/perl
  2.  
  3. use strict;
  4. use warnings;
  5.  
  6. my ($filename) = @ARGV;
  7.  
  8. if ( ! $filename ) {
  9.     print "Usage: perl $0 \$filename\n";
  10.     exit(2);
  11. }
  12.  
  13. open(my $fh, "<", $filename) or die "Failed to open file: $filename\n$!";
  14.  
  15. my $rotate_data = "1 0 0 0 1 0 0 0 1";
  16. my $side_length = 10000000;
  17.  
  18.  
  19. while (<$fh>){
  20.     if ( /^dim/ ) {
  21.         $side_length = (split('\s+', $_))[1];
  22.     }
  23.     if ( /^data/ ) {
  24.         last;
  25.     }
  26. }
  27.  
  28. my @blocks = ( { "x"    => 2,
  29.                  "y"    => 10,
  30.                  "num"  => "3006.DAT",
  31.                 },
  32.                 { "x"    => 2,
  33.                  "y"    => 8,
  34.                  "num"  => "3007.DAT",
  35.                 },
  36.                 { "x"    => 2,
  37.                  "y"    => 6,
  38.                  "num"  => "2456.DAT",
  39.                 },
  40.                 { "x"    => 2,
  41.                  "y"    => 4,
  42.                  "num"  => "3001.DAT",
  43.                 },
  44.                 { "x"    => 2,
  45.                  "y"    => 3,
  46.                  "num"  => "3002.DAT",
  47.                 },
  48.                 { "x"    => 2,
  49.                  "y"    => 2,
  50.                  "num"  => "3003.DAT",
  51.                 },
  52.                 { "x"    => 1,
  53.                  "y"    => 8,
  54.                  "num"  => "3008.DAT",
  55.                 },
  56.                 { "x"    => 1,
  57.                  "y"    => 6,
  58.                  "num"  => "3009.DAT",
  59.                 },
  60.                 { "x"    => 1,
  61.                  "y"    => 4,
  62.                  "num"  => "3010.DAT",
  63.                 },
  64.                 { "x"    => 1,
  65.                  "y"    => 3,
  66.                  "num"  => "3622.DAT",
  67.                 },
  68.                 { "x"    => 1,
  69.                  "y"    => 2,
  70.                  "num"  => "3004.DAT",
  71.                 },
  72.                 { "x"    => 1,
  73.                  "y"    => 1,
  74.                  "num"  => "3005.DAT",
  75.                 }
  76.             );
  77. my ($x, $y, $z) = (0, 0, 0);
  78. while(1) {
  79.     my @rows;
  80.     for ( my $index = 0; $index < $side_length; $index++ ) {
  81.         my $line = <$fh>;
  82.         if ( ! $line ) {
  83.             exit(0);
  84.         }
  85.         my @voxels = split('\s+', $line);
  86.         push @rows, \@voxels;
  87.     }
  88.    
  89.    
  90.     for my $block_ref (@blocks) {
  91.         for my $r (0..$#rows) {
  92.             my @voxels = @{$rows[$r]};
  93.             for my $index (0..$#voxels) {
  94.                 $y = $index;
  95.                 if ( $voxels[$index] == 1 ) {
  96.                    
  97.                     my $block_end = $index + $block_ref->{'x'} ;
  98.                     my $row_end = $r + $block_ref->{'y'};
  99.                    
  100.                     if ( $block_end <= $#voxels and $row_end <= $#rows ) {
  101.                    
  102.                         my $matching_blocks = 0;
  103.                         my $expected_match = $block_ref->{'x'} * $block_ref->{'y'};
  104.                         for ( my $block_index = $index; $block_index < $block_end; $block_index++ ) {
  105.                             for ( my $row_index = $r; $row_index < $row_end; $row_index++ ) {
  106.                                 if ( $rows[$row_index][$block_index] == 1 ) {
  107.                                     $matching_blocks++;
  108.                                 }
  109.                             }
  110.                         }
  111.                         if ( $matching_blocks == $expected_match ) {
  112.                             my $x_ = $x + ($block_ref->{'y'} - 1) / 2;
  113.                             my $y_ = $y + ($block_ref->{'x'} - 1) / 2;
  114.                             my $z_ = $z;
  115.                             print "1 0 " . $x_ * 20 . " " . $z_ * 24 . " " . $y_ * 20 . " " . $rotate_data . " " . $block_ref->{'num'} . "\n";
  116.                             for ( my $block_index = $index; $block_index < $block_end; $block_index++ ) {
  117.                                 for ( my $row_index = $r; $row_index < $row_end; $row_index++ ) {
  118.                                     $rows[$row_index][$block_index] = 2;
  119.                                 }
  120.                             }
  121.                         }
  122.                     }
  123.                 }
  124.             }
  125.             $x += 1;
  126.         }
  127.         $x = 0;
  128.     }
  129.    
  130.     $z += 1;
  131. }
  132.  
  133. close $fh;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement