Cog/Gdcode.pm untested

filter: Statementstmt 0.0% 0 / 263 Branchbran 0.0% 0 / 62 Conditioncond 0.0% 0 / 28 MC/DCmcdc 0.0% 0 / 18 Subroutinesub 0.0% 0 / 34 Podpod 0.0% 0 / 29 totaltotal 0.0% 0 / 434 CC 32 SCAR 33.1
CC
32
Cov
0%
zcol
90.0
zcol2
72.0
grapid
30.0
CRAP 27.3
1 # Written by Mark Winder, mark.winder4@btinternet.com
2 # This package is used for storing scaling information when plotting using GD. It is not used when generating gcode.
3 # Its simple and fairly versatile, but still results in inverted y axis which are not taken account
4 use strict;
5 use vars qw($VERSION);
6 $VERSION=0.061;
7
8 package Scale;
9 my $pi=4.0 * atan2(1, 1);
10 my $debug=0;
11 my $yinvert=-1; # 1 for not inverted, -1 for inverted to correct way up.
12 # This is a debug setting, should be at -1, for correct orientation of y axis: +ve is up.
13 sub new
14 {
15 my $s={};
16
17 $s->{ox}=$s->{oy}=0;
18 $s->{s}=1.0;
19 $s->{cuttersize}=0;
20 return bless $s;
21 }
22
23 sub setorigin
24 {
25 my ($s,$x,$y)=@_;
26
27 $s->{ox}=$x;
28 $s->{oy}=$y;
29
30 }
31 sub setpixelorigin
32 {
33 my ($s,$x,$y)=@_;
34
35 $s->{pox}=$x;
36 $s->{poy}=$y;
37
38 }
39
40 sub setscale
41 {
42 my ($s,$scale,$xsize,$ysize)=@_;
43
44 $s->{s}=$xsize/$scale;
45 $s->{xsize}=$xsize;
46 $s->{ysize}=$ysize;
47 $s->{pox}=abs($xsize/2);
48 $s->{poy}=abs($ysize/2);
49 }
50
51 sub scalexy
52 {
53 my $s=shift(@_);
54 my (@xyus)=@_; # unscaled xy pairs
55 my @xys; # scaled xy pairs
56 # print "scalexy b4 is @xyus\n";
57 while (@xyus)
58 {
59 my $x=$xyus[0];
60 my $y=$xyus[1];
61 push(@xys, (int(0.5+($x-$s->{ox})*$s->{s})+$s->{pox}),int(0.5+(($y-$s->{oy})*$s->{s}*$yinvert)+$s->{poy}));
62 shift(@xyus); shift(@xyus);
63 }
64 # print "scalexy after is @xys\n";
65 return (@xys);
66 }
67 sub scaled # scale a distance only, no origin offset, use for things like diameters
68 {
69 my $s=shift(@_);
70 return map { int(0.5+$_*$s->{s}) } @_;
71 }
72
73 # graphical routine. Has same interface as gcode pretty much.
74 package Gdcode;
75 use vars qw(@ISA);
76 @ISA=qw(CNC::Cog::Gdcode);
77 # I define another package Gdcode, this enables you to say new Gcode(...
78 # instead of new CNC::Cog::Gdcode(...;
79
80
81 package CNC::Cog::Gdcode;
82 use GD;
83 use vars qw(@ISA);
84 @ISA=qw(Exporter);
85 #@EXPORT=qw( x y z d f r );
86 my $f="%9f ";
87 my $ff="%2.1f";
88 #sub x {'x'}
89 #sub y {'y'}
90 #sub z {'z'}
91 #sub f {'f'}
92 #sub r {'r'}
93 #sub d {'d'}
94 sub new
95 {
96 my ($class,$file,$scale,$xsize,$ysize)=@_;
97 $class=ref($class) || $class;
98 my $g={};
99
100 my $i = new GD::Image(abs($xsize),abs($ysize));
101 my $s=new Scale;
102 $s->setscale($scale,$xsize,$ysize);
103 $s->setorigin(0,0);
104 $g->{i}=$i;
105 $g->{x}=0;
106 $g->{y}=0;
107 $g->{z}=0;
108 $g->{s}=$s;
109 $g->{file}=$file;
110
111
112 # allocate some colors
113 $g->{col}->{white} = $i->colorAllocate(255,255,255);
114 $g->{col}->{black} = $i->colorAllocate(0,0,0);
115 $g->{col}->{blue} = $i->colorAllocate(0,0,255);
116 $g->{col}->{dred} = $i->colorAllocate(128,0,0);
117 $g->{col}->{bred} = $i->colorAllocate(255,0,0);
118 $g->{col}->{green} = $i->colorAllocate(0,255,0);
119
120
121 #$red = $i->colorAllocate(255,0,0);
122 #$blue = $i->colorAllocate(0,0,255);
123 #$green=$i->colorAllocate(0,255,0);
124 #$turk=$i->colorAllocate(0,255,255);
125 #$beig=$i->colorAllocate(196,128,0);
126 #$purp=$i->colorAllocate(128,0,128);
127
128 $g->{feed}=0; # compatability.
129
130 return bless $g,$class;
131 }
132
133 # internal function. used for all circular arcs, emulates the
134 # gcode arc command.
135 sub range
136 {
137 my ($v,$i,$x)=@_;
138 my ($r)=$x-$i;
139
140 while ($v>=$x) { $v-=$r; }
141 while ($v<$i) { $v+=$r; }
142 return $v;
143 }
144 sub arcpath
145 {
146 my ($i,$s,$x1,$y1,$x2,$y2,$r,$col)=@_;
147
148 # ($yinvert<0) and ($x1,$y1,$x2,$y2)=($x2,$y2,$x1,$y1);
149
150 my $l=sqrt((($x2-$x1)**2)+(($y2-$y1)**2)); # dist between supplied points
151 # printf "%f %f\n",$r,$l/2;
152 my $l2=sqrt(abs($r**2-($l/2)**2)); # dist from center of normal to arc center
153 my $a=2*atan2($l/2,$l2); # vertex angle
154 my $ra=0.5*($pi-$a); # radial line line 1/2 angle
155 my $cata=atan2($y2-$y1,$x2-$x1);
156
157 my $a2=-($pi-$cata-$ra);
158 my ($cx,$cy)=($x1-$r*cos($a2),$y1-$r*sin($a2));
159
160 # $i->arc($s->scalexy($cx,$cy),$s->scaled($r*2,$r*2),($a2)*180/$pi+360,($a2+$a)*180/$pi+360,$col);
161
162 $a=$a*$yinvert;
163 $a2*=$yinvert;
164
165 $a2+=0.003; # rounding error ? Problem in GD.
166 $a=$a2+$a;
167 $a2=range($a2,0,2*$pi);
168 $a=range($a,0,2*$pi);
169
170 # if ($a>$a2)
171 if ($yinvert<0)
172 {
173 ($a,$a2)=($a2,$a);
174 }
175
176 $i->arc($s->scalexy($cx,$cy),$s->scaled($r*2,$r*2),($a2)*180/$pi,($a)*180/$pi,$col);
177 }
178
179 sub setcuttersize
180 {
181 my ($g,$s)=@_; # set cutter diameter default to inches.
182 # can add pt for point, mm for millimetres, cm for centimetres
183 # can add i for inches (default)
184 # can add t for thous of an inch
185 $s=~s/i//;
186 $s=~s/pt// and $s/=72;
187 $s=~s/mm// and $s/=25.4;
188 $s=~s/cm// and $s/=2.54;
189 $s=~s/t// and $s/=1000.0;
190
191 $s=~/[a-zA-Z]/ and die "Invalid unit specification $s";
192
193 $g->{cuttersize}=$s; # Thickness in inches
194
195 ($s)=$g->{s}->scaled($s);
196 $s=1 if ($s<1);
197 $g->{i}->setThickness($s);
198
199
200 }
201
202 sub getcuttersize
203 {
204 my ($g)=@_;
205
206 return $g->{cuttersize};
207 }
208
209 sub ginit
210 {
211 my ($g)=@_;
212
213 }
214 sub gcomment
215 {
216 shift if (!ref($_[0]) eq '');
217 my ($c)=@_;
218
219 $c=~s/\n$//;
220 print "*** $c \n" if ($c);
221 return;
222 }
223 sub gmove
224 {
225 my ($g)=shift;
226 my (@xy)=($g->{x},$g->{y});
227 while (@_)
228 {
229 $g->{$_[0]}=$_[1] if ($_[0] =~/^[xyz]$/i);
230 shift; shift;
231 }
232 push(@xy,$g->{x},$g->{y});
233
234 my $col=$g->{z}>=0?$g->{col}->{green}:$g->{col}->{blue};
235 $col=zcol($g);
236 $g->{i}->line($g->{s}->scalexy(@xy),$col);
237 }
238 sub gdebug
239 {
240 shift();
241 ($debug)=@_;
242 }
243 # debug only.
244 sub gline
245 {
246 # used for debug draws a line between 2 points
247
248 my ($g)=shift;
249
250 return if (!$debug);
251
252 my ($x1,$y1,$x2,$y2)=@_;
253
254 $g->gmove('z',0.1);
255 $g->gmove('x',$x1,'y',$y1);
256 $g->gmove('z',-0.5);
257 $g->gmove('x',$x2,'y',$y2);
258 $g->gmove('z',0.1);
259
260 }
261 # debug only. draws a cross.
262 sub gmark
263 {
264 my ($g,$x,$y)=@_;
265
266 my $d=0.025;
267
268 # return if (!$debug);
269
270 $g->gmove('z',0.1);
271 $g->gmove('x',$x,'y',$y);
272 $g->gmove('x',$x-$d,'y',$y-$d);
273 $g->gmove('z',-0.15);
274 $g->gmove('x',$x+$d,'y',$y+$d);
275 $g->gmove('x',$x,'y',$y);
276 $g->gmove('x',$x-$d,'y',$y+$d);
277 $g->gmove('x',$x+$d,'y',$y-$d);
278 $g->gmove('x',$x,'y',$y);
279 }
280 sub grapid
281 {
282 my ($g)=shift;
283 my (@xy)=($g->{x},$g->{y});
284 my ($z)=$g->{z};
285 my ($col);
286
287 while (@_)
288 {
289 $g->{$_[0]}=$_[1] if ($_[0] =~/^[xyz]$/i);
290 shift; shift;
291 }
292 push(@xy,$g->{x},$g->{y});
293
294 $col=$g->{col}->{green};
295 $col=$g->{col}->{bred} if ((defined $z and $z<0) or (defined $g->{z} and $g->{z}<0));
296 $g->{i}->setThickness(1);
297 $g->{i}->line($g->{s}->scalexy(@xy),$col);
298
299 if ($g->{cuttersize})
300 {
301 my ($s)=$g->{s}->scaled($g->{cuttersize});
302 $s=1 if ($s<1);
303 $g->{i}->setThickness($s);
304 }
305 }
306 sub rednext
307 {
308 my ($g)=@_;
309 $g->{rednext}=1;
310 print "**** red ****\n";
311 }
312 sub zcol
313 {
314 my ($g)=@_;
315 my $z=$g->{z};
316 my $col;
317
318 if ($g->{rednext}==1)
319 {
320 $g->{rednext}=0;
321 $col = $g->{col}->{bred};
322 return $col;
323 }
324
325
326 if ($z>0)
327 {
328 $col=$g->{col}->{green};
329 }
330 else
331 {
332 $z=abs($z);
333
334 my ($r,$gr,$b);
335
336 my $l=127*$z/0.3;
337 my $ll=127*($z/0.1-0.5);
338 $r=$l+$ll;
339 $b=$l-$ll;
340 $gr=$l;
341
342
343 $r=0 if ($r<0);
344 $gr=0 if ($gr<0);
345 $b=0 if ($b<0);
346 $r=255 if ($r>255);
347 $gr=255 if ($gr>255);
348 $b=255 if ($b>255);
349
350 $col = $g->{i}->colorAllocate($r,$gr,$b);
351 }
352 return $col;
353 }
354 sub zcol2
355 {
356 my ($g)=@_;
357 my $z=$g->{z};
358 my $col;
359
360 if ($z>0)
361 {
362 $col=$g->{col}->{green};
363 }
364 else
365 {
366 $z=abs($z);
367
368 my ($r,$gr,$b);
369 my $maxz=0.3;
370
371 my $lum=$z/$maxz;
372
373 my $col=2*3.14159*$z/$maxz;
374
375 $r=127+127*cos($col);
376 $gr=127+127*cos($col+3.14159*2/3);
377 $b=127+127*cos($col+3.14159*4/3);
378
379 print "r=$r g=$gr b=$b\n";
380 $r=0 if ($r<0);
381 $gr=0 if ($gr<0);
382 $b=0 if ($b<0);
383 $r=255 if ($r>255);
384 $gr=255 if ($gr>255);
385 $b=255 if ($b>255);
386
387 ($r,$gr,$b)=map { $_=$_/4 } ($r,$gr,$b);
388
389 # print "r=$r g=$gr b=$b\n";
390 $col = $g->{i}->colorAllocate(int($r),int($gr),int($b));
391 }
392 return $col;
393 }
394 sub garcccw
395 {
396 my ($g)=shift;
397 my (@xy)=($g->{x},$g->{y});
398 my ($r);
399 while (@_)
400 {
401 $g->{$_[0]}=$_[1] if ($_[0] =~/^[xyz]$/i); # i,j not implemented
402 $r=$_[1] if ($_[0] =~/^[r]$/i);
403 shift; shift;
404 }
405
406
407 @xy=(@xy,$g->{x},$g->{y});
408 my $col;
409
410 $col=zcol($g);
411
412
413 arcpath($g->{i},$g->{s},@xy,$r,$col);
414 }
415 sub garccw
416 {
417 my ($g)=shift;
418 my (@xy)=($g->{x},$g->{y});
419 my ($r);
420 while (@_)
421 {
422 $g->{$_[0]}=$_[1] if ($_[0] =~/^[xyz]$/i); # i,j not implemented
423 $r=$_[1] if ($_[0] =~/^[r]$/i);
424 shift; shift;
425 }
426 @xy=($g->{x},$g->{y},@xy);
427 my $col;
428 $col=zcol($g);
429 arcpath($g->{i},$g->{s},@xy,$r,$col);
430 }
431 # although cutter compensation changes the path plotted by, roughly speaking
432 # the radius of the tool, we dont show this here, all we show
433 # is the actual path plotted. These functions therefore do nothing
434 sub gcompr
435 {
436
437 }
438 sub gcompl
439 {
440
441 }
442 sub gcomp0
443 {
444
445 }
446 # generate the output
447 sub gend
448 {
449 my ($g)=@_;
450 open(F,">".$g->{file}) or die "Cannot open file ".$g->{file};
451 binmode F;
452 print F $g->{i}->png;
453 close F;
454 }
455 sub gruler
456 {
457 my ($g,$x,$y,$ndiv,$div)=@_;
458 my $col=$g->{col}->{blue};
459 my $n=0;
460 my $ys=0.01;
461 my $yl=$ys*3;
462 $g->{i}->line($g->{s}->scalexy($x,$y,$x,$y-$yl),$col);
463 $ndiv*=10;
464 $div/=10;
465 while ($n++<$ndiv)
466 {
467 $g->{i}->line($g->{s}->scalexy($x+$n*$div-$div,$y,$x+$n*$div,$y),$col);
468
469 my ($dy)=$ys;
470 $dy=$yl if ($n%10==0);
471 $g->{i}->line($g->{s}->scalexy($x+$n*$div,$y,$x+$n*$div,$y-$dy),$col);
472 }
473 }
474 # dwell, do nothing!
475 sub gdwell
476 {
477 }
478
479 1;