forked from MarkWheadon/velocity-painting
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathvelPaint
executable file
·219 lines (173 loc) · 6.47 KB
/
velPaint
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
#!/opt/local/bin/perl -w
# Velocity Painting by [Mark Wheadon](https://github.com/MarkWheadon) is licensed under a [Creative Commons Attribution 4.0
# International License](http://creativecommons.org/licenses/by/4.0/).
# Based on a work at https://github.com/MarkWheadon/velocity-painting.
our $VERSION = 0.1;
use Math::Trig;
use Image::Magick;
unless ((@ARGV > 0) &&
(($ARGV[0] eq '-projectX' && @ARGV >= 10) ||
($ARGV[0] eq '-cylinderZ' && @ARGV >= 9))) {
print STDERR <<END;
Usage:
$0 -projectX printCentreX printCentreY imageWidth imageHeight zOffset targetSpeed lowSpeed highSpeed imageFile [sourceGcodeFile] > paintedGcodeFile
$0 -cylinderZ printCentreX printCentreY imageHeight zOffset targetSpeed lowSpeed highSpeed imageFile [sourceGcodeFile] > paintedGcodeFile
printCentre{X,Y} is the centre of the print in the printer's coordinate space, in mm.
When the print in centred on the bed then printCentre{X,Y} is 0,0 for most (all?) detas. 125,125 for the Prusa i3 MK2.
imageWidth and imageHeight are also in mm.
Specifying an imageWith of '-' sets it to the correct width for the specified height.
Specifying an imageHeight of '-' sets it to the correct height for the specified width.
targetSpeed is the speed of the vectors you wish to manipulate
(so slice the model with all speeds set to this value).
lowSpeed is the required speed for the slow parts of the print
highSpeed is the required speed for the quick parts of the print
imageFile is the image to be mapped onto the print.
All speeds above are in mm/min.
END
exit 1;
}
my $projectionMode = shift @ARGV;
my $projectedImageWidth;
my $printCentreX = shift @ARGV;
my $printCentreY = shift @ARGV;
if ($projectionMode eq '-projectX') {
$projectedImageWidth = shift @ARGV;
}
my $projectedImageHeight = shift @ARGV;
my $zOffset = shift @ARGV;
my $targetSpeed = shift @ARGV;
my $lowSpeed = shift @ARGV;
my $highSpeed = shift @ARGV;
my $imageFile = shift @ARGV;
my $image = Image::Magick->new;
my $res = $image->Read($imageFile);
die "$res" if "$res";
my $imageWidth = $image->Get('columns');;
my $imageHeight = $image->Get('rows');
if ((($projectionMode eq '-cylinderZ') || $projectedImageWidth eq '-') && $projectedImageHeight eq '-') {
print STDERR <<END;
$0: you must set either the image width or its height, or both.
END
exit 1;
}
if (defined $projectedImageWidth && $projectedImageWidth eq '-') {
$projectedImageWidth = $projectedImageHeight * $imageWidth / $imageHeight;
}
if ($projectedImageHeight eq '-') {
$projectedImageHeight = $projectedImageWidth * $imageHeight / $imageWidth;
}
my $maxVecLength = .1; # Longest vector in mm. Splits longer vectors. Very small -> long processing times.
my ($oldX, $oldY, $oldZ);
my $currentZ;
my $lastZOutput = -1;
while (my $line = <>) {
my ($x, $y, $z, $e, $f);
chomp $line;
$line =~ s/\r//g;
($x, $y, $z, $e, $f) =
$line =~ /G1 X([^\s]+) Y([^\s]+) Z([^\s]+) E([^\s]+) F($targetSpeed)$/;
($x, $y, $e, $f) =
$line =~ /G1 X([^\s]+) Y([^\s]+) E([^\s]+) F($targetSpeed)$/ unless (defined $x);
($x, $y, $z, $e) =
$line =~ /G1 X([^\s]+) Y([^\s]+) Z([^\s]+) E([^\s]+)$/ unless (defined $x);
($x, $y, $e) =
$line =~ /G1 X([^\s]+) Y([^\s]+) E([^\s]+)$/ unless (defined $x);
if (defined $z) { $currentZ = $z } else { $z = $currentZ; }
if (defined $x) {
if (!defined $oldZ) {
&outMove($x, $y, $z, $e, 0);
} else {
my $xd = $x - $oldX;
my $yd = $y - $oldY;
my $zd = $z - $oldZ;
my $ed = $e - $oldE;
my $length = sqrt($xd * $xd + $yd * $yd + $zd * $zd);
if ($length <= $maxVecLength) {
&outMove($x, $y, $z, $e, 0);
} else {
my $lastSegOut = -1;
my $oSlow = &surfaceSpeed($oldX, $oldY, $oldZ);
my $nSegs = int($length / $maxVecLength + 0.5);
my $xDelta = $xd / $nSegs;
my $yDelta = $yd / $nSegs;
my $zDelta = $zd / $nSegs;
my $eDelta = $ed / $nSegs;
for (my $i = 1; $i <= $nSegs; $i++) {
my $nx = $oldX + $xDelta * $i;
my $ny = $oldY + $yDelta * $i;
my $nz = $oldZ + $zDelta * $i;
my $slow = &surfaceSpeed($nx, $ny, $nz);
if (($slow != $oSlow) && ($i > 1)) {
# pattern has changed. Time to output the vector so far
&outMove($oldX + $xDelta * ($i - 1),
$oldY + $yDelta * ($i - 1),
$oldZ + $zDelta * ($i - 1),
$oldE + $eDelta * ($i - 1), 1);
$oSlow = $slow;
$lastSegOut = $i;
}
}
if ($lastSegOut != $nSegs) {
&outMove($x, $y, $z, $e);
}
}
}
$oldX = $x; $oldY = $y; $oldZ = $z; $oldE = $e;
} else {
if ($line =~ /G1 X([^\s]+) Y([^\s]+) Z([^\s]+)/) {
$oldX = $1; $oldY=$2; $oldZ = $3;
} elsif ($line =~ /G1 X([^\s]+) Y([^\s]+)/) {
$oldX = $1; $oldY=$2;
}
if ($line =~ /Z([\d\.]+)/) {
$currentZ = $1;
$oldZ = $1;
}
if ($line =~ /E([\d\.]+)/) {
$oldE = $1;
}
print "$line\n";
}
}
sub surfaceSpeed() {
if ($projectionMode eq '-cylinderZ') {
return surfaceSpeedCylinderZ(@_);
} elsif ($projectionMode eq '-projectX') {
return surfaceSpeedProjectX(@_);
}
}
sub surfaceSpeedCylinderZ() {
my ($x, $y, $z) = @_;
my $zNormalized = ($z - $zOffset) / $projectedImageHeight;
my $theta = atan2($y - $printCentreY, $x - $printCentreX) + pi; # 0 to 2pi
my $xNormalized = $theta / (2 * pi);
my $imageX = $xNormalized * $imageWidth;
my $imageY = $imageHeight - $zNormalized * $imageHeight;
if ($imageX < 0 || $imageX >= $imageWidth || $imageY < 0 || $imageY >= $imageHeight) {
return $highSpeed;
# return $lowSpeed;
}
# return $highSpeed - $image->GetPixel(x=>$imageX, y=>$imageY) * ($highSpeed - $lowSpeed);
return $lowSpeed + $image->GetPixel(x=>$imageX, y=>$imageY) * ($highSpeed - $lowSpeed);
}
sub surfaceSpeedProjectX() {
my ($x, $y, $z) = @_;
my $xNormalized = ($x - $printCentreX + $projectedImageWidth / 2) / $projectedImageWidth;
my $zNormalized = ($z - $zOffset) / $projectedImageHeight;
my $imageX = $xNormalized * $imageWidth;
my $imageY = $imageHeight - $zNormalized * $imageHeight;
if ($imageX < 0 || $imageX >= $imageWidth || $imageY < 0 || $imageY >= $imageHeight) {
# return $highSpeed;
return $lowSpeed;
}
# return $highSpeed - $image->GetPixel(x=>$imageX, y=>$imageY) * ($highSpeed - $lowSpeed);
return $lowSpeed + $image->GetPixel(x=>$imageX, y=>$imageY) * ($highSpeed - $lowSpeed);
}
sub outMove() {
my ($x, $y, $z, $e, $extra) = @_;
my $zCommand = "";
if ($z != $lastZOutput) { $zCommand = sprintf(" Z%.3f", $z); }
my $added = ""; if ($extra) { $added = " ; added"; }
printf("G1 X%.3f Y%.3f$zCommand E%.3f F%.3f$added\n", $x, $y, $e, &surfaceSpeed($x, $y, $z));
$lastZOutput = $z;
}