decimate.pl 868 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. #!/usr/bin/perl -i~
  2. use strict;
  3. use warnings;
  4. my %lastpos = (X => 10000, Y => 10000, Z => 10000, E => 10000, F => 10000);
  5. my %pos = (X => 0, Y => 0, Z => 0, E => 0, F => 0);
  6. my $mindist = 0.33;
  7. my $mindistz = 0.005;
  8. my $mindistsq = $mindist * $mindist;
  9. sub dist {
  10. my $sq = 0;
  11. for (qw/X Y Z E/) {
  12. $sq += ($pos{$_} - $lastpos{$_}) ** 2;
  13. }
  14. return $sq;
  15. }
  16. while (<>) {
  17. if (m#\bG[01]\b#) {
  18. while (m#([XYZEF])(\d+(\.\d+)?)#gi) {
  19. $pos{uc $1} = $2;
  20. }
  21. if (
  22. (
  23. /X/ &&
  24. /Y/ &&
  25. (dist() >= $mindistsq)
  26. ) ||
  27. (abs($pos{Z} - $lastpos{Z}) > $mindistz) ||
  28. (!/X/ || !/Y/)
  29. ) {
  30. print;
  31. %lastpos = %pos;
  32. }
  33. elsif (($pos{F} - $lastpos{F}) != 0) {
  34. printf "G1 F%s\n", $pos{F};
  35. $lastpos{F} = $pos{F};
  36. }
  37. }
  38. else {
  39. if (m#\bG92\b#) {
  40. while (m#([XYZEF])(\d+(\.\d+)?)#gi) {
  41. $lastpos{uc $1} = $2;
  42. }
  43. }
  44. print;
  45. }
  46. }