forked from MarkWheadon/velocity-painting
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvelPaint
executable file
·309 lines (243 loc) · 9.38 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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
#!/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.
use strict;
use warnings;
use 5.10.1;
our $VERSION = 0.4;
use Math::Trig;
use Imager;
unless ((@ARGV > 0) &&
((($ARGV[0] =~ /^-project[XYZ]$/ || $ARGV[0] eq '-spherical') && @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 -projectY printCentreX printCentreY imageWidth imageHeight zOffset targetSpeed lowSpeed highSpeed imageFile [sourceGcodeFile] > paintedGcodeFile
$0 -projectZ printCentreX printCentreY imageWidth imageHeight zOffset targetSpeed lowSpeed highSpeed imageFile [sourceGcodeFile] > paintedGcodeFile
$0 -cylinderZ printCentreX printCentreY imageHeight zOffset targetSpeed lowSpeed highSpeed imageFile [sourceGcodeFile] > paintedGcodeFile
$0 -spherical printCentreX printCentreY xOffset yOffset zOffset targetSpeed lowSpeed highSpeed imageFile [sourceGcodeFile] > paintedGcodeFile
printCentre{X,Y} is the centre of the print in the printer's coordinate space, in mm.
-projectX projects the image onto the X axis as if the X axis were a cinema screen.
-projectY and -projectZ are similar, so use -projectZ to project onto the top/bottom of the model.
-cylinderZ wraps the image around the Z axis: great for vases.
-spherical projects outwards from the origin, offset by {x,y,z}Offset
When the print in centred on the bed then printCentre{X,Y} is 0,0 for most (all?) detas. 125,105 for the Prusa i3 MK2.
imageWidth and imageHeight and the offsets are all 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 =~ /^-project[XYZ]$/) {
$projectedImageWidth = shift @ARGV;
}
my $projectedImageHeight;
my $xOffset;
my $yOffset;
if ($projectionMode eq '-spherical') {
$xOffset = shift @ARGV;
$yOffset = shift @ARGV;
} else {
$projectedImageHeight = shift @ARGV;
}
my $zOffset = shift @ARGV;
my $targetSpeed = shift @ARGV;
my $lowSpeed = shift @ARGV;
my $highSpeed = shift @ARGV;
my $imageFile = shift @ARGV;
my $colourImage = Imager->new(file=>$imageFile) or die Imager->errstr();
my $image = $colourImage->convert(preset=>'grey');
my $imageWidth = $image->getwidth();
my $imageHeight = $image->getheight();
my $speedRange = $highSpeed - $lowSpeed;
if (($projectionMode ne '-spherical') && ((($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 (defined $projectedImageHeight && $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, $oldE);
my $currentZ;
my $lastZOutput = -1;
my $extrudeAbsolute = 1;
while (my $line = <>) {
my ($x, $y, $z, $e, $f);
chomp $line;
$line =~ s/\r//g;
if ($line =~ /G1 X(?<x>\S+) Y(?<y>\S+)(?: Z(?<z>\S+))? E(?<e>\S+)(?: F(?<f>$targetSpeed))?$/ ||
$line =~ /G1 Z(?<z>\S+) X(?<x>\S+) Y(?<y>\S+) E(?<e>\S+)(?: F(?<f>$targetSpeed))?$/)
{
($x, $y, $z, $e, $f) = @+{qw/x y z e f/};
}
if ($line =~ /M82/) { $extrudeAbsolute = 1; }
if ($line =~ /M83/) { $extrudeAbsolute = 0; }
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 = $extrudeAbsolute ? ($e - $oldE) : $e;
my $length = sqrt($xd * $xd + $yd * $yd + $zd * $zd);
if ($length <= $maxVecLength) {
outMove($x, $y, $z, $e, 0);
} else {
my $lastSegOut = 0;
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),
$extrudeAbsolute ? ($oldE + $eDelta * ($i - 1)) : ($eDelta * ($i - $lastSegOut)), 1);
$oSlow = $slow;
$lastSegOut = $i;
}
}
if ($lastSegOut != $nSegs) {
outMove($x, $y, $z, $extrudeAbsolute ? $e : ($eDelta * ($nSegs - $lastSegOut)), $lastSegOut != 0);
}
}
}
($oldX, $oldY, $oldZ, $oldE) = ($x, $y, $z, $e);
} else {
if ($line =~ /G1 X(\S+) Y(\S+) Z(\S+)/) {
($oldX, $oldY, $oldZ) = ($1, $2, $3);
} elsif ($line =~ /G1 X(\S+) Y(\S+)/) {
($oldX, $oldY) = ($1, $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(@_);
} elsif ($projectionMode eq '-projectY') {
return surfaceSpeedProjectY(@_);
} elsif ($projectionMode eq '-projectZ') {
return surfaceSpeedProjectZ(@_);
} elsif ($projectionMode eq '-spherical') {
return surfaceSpeedSpherical(@_);
}
}
sub outsideImage {
my ($imageX, $imageY) = @_;
return $imageX < 0 || $imageX >= $imageWidth || $imageY < 0 || $imageY >= $imageHeight;
}
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 (outsideImage( $imageX, $imageY )) {
return $highSpeed;
# return $lowSpeed;
}
return $lowSpeed + greyAt($imageX, $imageY) * $speedRange;
}
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 (outsideImage( $imageX, $imageY )) {
# return $highSpeed;
return $lowSpeed;
}
return $lowSpeed + greyAt($imageX, $imageY) * $speedRange;
}
sub surfaceSpeedProjectY {
my ($x, $y, $z) = @_;
my $xNormalized = ($y - $printCentreY + $projectedImageWidth / 2) / $projectedImageWidth;
my $zNormalized = ($z - $zOffset) / $projectedImageHeight;
my $imageX = $xNormalized * $imageWidth;
my $imageY = $imageHeight - $zNormalized * $imageHeight;
if (outsideImage( $imageX, $imageY )) {
# return $highSpeed;
return $lowSpeed;
}
return $lowSpeed + greyAt($imageX, $imageY) * $speedRange;
}
sub surfaceSpeedProjectZ {
my ($x, $y, $z) = @_;
my $xNormalized = ($x - $printCentreX + $projectedImageWidth / 2) / $projectedImageWidth;
my $yNormalized = ($y - $printCentreY + $projectedImageHeight / 2) / $projectedImageHeight;
my $imageX = $xNormalized * $imageWidth;
my $imageY = $yNormalized * $imageHeight;
if (outsideImage( $imageX, $imageY )) {
# return $highSpeed;
return $lowSpeed;
}
return $lowSpeed + greyAt($imageX, $imageY) * $speedRange;
}
sub surfaceSpeedSpherical() {
my ($x, $y, $z) = @_;
my $theta = atan2($y - $yOffset - $printCentreY, $x - $xOffset - $printCentreX) + pi; # 0 to 2pi
my $xNormalized = $theta / (2 * pi);
$theta = atan2($z - $zOffset, $x - $xOffset - $printCentreX) + pi; # 0 to 2pi
my $zNormalized = $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 $lowSpeed + greyAt($imageX, $imageY) * $speedRange;
}
sub greyAt {
my ($x, $y) = @_;
my $colours = $image->getpixel(x=>$x, y=>$y, type=>'float');
my ($r, $g, $b, $a) = $colours->rgba();
return $r;
}
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%.4f F%.3f$added\n", $x, $y, $e, surfaceSpeed($x, $y, $z));
$lastZOutput = $z;
}