-
Notifications
You must be signed in to change notification settings - Fork 62
/
drawing.pde
970 lines (830 loc) · 28.2 KB
/
drawing.pde
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
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
/**
Polargraph controller
Copyright Sandy Noble 2015.
This file is part of Polargraph Controller.
Polargraph Controller is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
Polargraph Controller is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with Polargraph Controller. If not, see <http://www.gnu.org/licenses/>.
Requires the excellent ControlP5 GUI library available from http://www.sojamo.de/libraries/controlP5/.
Requires the excellent Geomerative library available from http://www.ricardmarxer.com/geomerative/.
This is an application for controlling a polargraph machine, communicating using ASCII command language over a serial link.
sandy.noble@gmail.com
http://www.polargraph.co.uk/
https://github.com/euphy/polargraphcontroller
*/
static final String CMD_CHANGELENGTH = "C01,";
static final String CMD_SETPENWIDTH = "C02,";
//static final String CMD_CHANGEMOTORSPEED = "C03,";
//static final String CMD_CHANGEMOTORACCEL = "C04,";
static final String CMD_DRAWPIXEL = "C05,";
static final String CMD_DRAWSCRIBBLEPIXEL = "C06,";
static final String CMD_DRAWRECT = "C07,";
static final String CMD_CHANGEDRAWINGDIRECTION = "C08,";
static final String CMD_SETPOSITION = "C09,";
static final String CMD_TESTPATTERN = "C10,";
static final String CMD_TESTPENWIDTHSQUARE = "C11,";
static final String CMD_TESTPENWIDTHSCRIBBLE = "C12,";
static final String CMD_PENDOWN = "C13,";
static final String CMD_PENUP = "C14,";
static final String CMD_DRAWSAWPIXEL = "C15,";
static final String CMD_DRAWROUNDPIXEL = "C16,";
static final String CMD_CHANGELENGTHDIRECT = "C17,";
static final String CMD_TXIMAGEBLOCK = "C18,";
static final String CMD_STARTROVE = "C19,";
static final String CMD_STOPROVE = "C20,";
static final String CMD_SET_ROVE_AREA = "C21,";
static final String CMD_LOADMAGEFILE = "C23,";
static final String CMD_CHANGEMACHINESIZE = "C24,";
static final String CMD_CHANGEMACHINENAME = "C25,";
static final String CMD_REQUESTMACHINESIZE = "C26,";
static final String CMD_RESETMACHINE = "C27,";
static final String CMD_DRAWDIRECTIONTEST = "C28,";
static final String CMD_CHANGEMACHINEMMPERREV = "C29,";
static final String CMD_CHANGEMACHINESTEPSPERREV = "C30,";
static final String CMD_SETMOTORSPEED = "C31,";
static final String CMD_SETMOTORACCEL = "C32,";
static final String CMD_MACHINE_MODE_STORE_COMMANDS = "C33,";
static final String CMD_MACHINE_MODE_EXEC_FROM_STORE = "C34,";
static final String CMD_MACHINE_MODE_LIVE = "C35,";
static final String CMD_RANDOM_DRAW = "C36,";
static final String CMD_SETMACHINESTEPMULTIPLIER = "C37,";
static final String CMD_START_TEXT = "C38,";
static final String CMD_DRAW_SPRITE = "C39,";
static final String CMD_CHANGELENGTH_RELATIVE = "C40,";
static final String CMD_SWIRLING = "C41,";
static final String CMD_DRAW_RANDOM_SPRITE = "C42,";
static final String CMD_DRAW_NORWEGIAN = "C43,";
static final String CMD_DRAW_NORWEGIAN_OUTLINE = "C44,";
static final String CMD_SETPENLIFTRANGE = "C45,";
static final String CMD_SELECT_ROVE_SOURCE_IMAGE = "C46";
static final String CMD_RENDER_ROVE = "C47";
static final String CMD_ACTIVATE_MACHINE_BUTTON = "C49";
static final String CMD_DEACTIVATE_MACHINE_BUTTON = "C50";
static final int PATH_SORT_NONE = 0;
static final int PATH_SORT_MOST_POINTS_FIRST = 1;
static final int PATH_SORT_GREATEST_AREA_FIRST = 2;
static final int PATH_SORT_CENTRE_FIRST = 3;
private PVector mouseVector = new PVector(0, 0);
Comparator xAscending = new Comparator()
{
public int compare(Object p1, Object p2)
{
PVector a = (PVector) p1;
PVector b = (PVector) p2;
int xValue = new Float(a.x).compareTo(b.x);
return xValue;
}
};
Comparator yAscending = new Comparator()
{
public int compare(Object p1, Object p2)
{
PVector a = (PVector) p1;
PVector b = (PVector) p2;
int yValue = new Float(a.y).compareTo(b.y);
return yValue;
}
};
void sendResetMachine()
{
String command = CMD_RESETMACHINE + "END";
addToCommandQueue(command);
}
void sendRequestMachineSize()
{
String command = CMD_REQUESTMACHINESIZE + "END";
addToCommandQueue(command);
}
void sendMachineSpec()
{
// ask for input to get the new machine size
String command = CMD_CHANGEMACHINESIZE+getDisplayMachine().inMM(getDisplayMachine().getWidth())+","+getDisplayMachine().inMM(getDisplayMachine().getHeight())+",END";
addToCommandQueue(command);
command = CMD_CHANGEMACHINEMMPERREV+int(getDisplayMachine().getMMPerRev())+",END";
addToCommandQueue(command);
command = CMD_CHANGEMACHINESTEPSPERREV+int(getDisplayMachine().getStepsPerRev())+",END";
addToCommandQueue(command);
command = CMD_SETMACHINESTEPMULTIPLIER+machineStepMultiplier+",END";
addToCommandQueue(command);
command = CMD_SETPENLIFTRANGE+penLiftDownPosition+","+penLiftUpPosition+",1,END";
addToCommandQueue(command);
// speeds
NumberFormat nf = NumberFormat.getNumberInstance(Locale.UK);
DecimalFormat df = (DecimalFormat)nf;
df.applyPattern("###.##");
addToCommandQueue(CMD_SETMOTORSPEED+df.format(currentMachineMaxSpeed)+",1,END");
addToCommandQueue(CMD_SETMOTORACCEL+df.format(currentMachineAccel)+",1,END");
}
public PVector getMouseVector()
{
if (mouseVector == null)
{
mouseVector = new PVector(0, 0);
}
mouseVector.x = mouseX;
mouseVector.y = mouseY;
return mouseVector;
}
// Uses the mouse position unless one is specified
void sendMoveToPosition(boolean direct)
{
sendMoveToPosition(direct, getMouseVector());
}
void sendMoveToPosition(boolean direct, PVector position)
{
String command = null;
PVector p = getDisplayMachine().scaleToDisplayMachine(position);
p = getDisplayMachine().inSteps(p);
p = getDisplayMachine().asNativeCoords(p);
sendMoveToNativePosition(direct, p);
}
void sendMoveToNativePosition(boolean direct, PVector p)
{
String command = null;
if (direct)
command = CMD_CHANGELENGTHDIRECT+int(p.x+0.5)+","+int(p.y+0.5)+","+getMaxSegmentLength()+",END";
else
command = CMD_CHANGELENGTH+(int)p.x+","+(int)p.y+",END";
addToCommandQueue(command);
}
int getMaxSegmentLength()
{
return this.maxSegmentLength;
}
void sendTestPattern()
{
String command = CMD_DRAWDIRECTIONTEST+int(gridSize)+",6,END";
addToCommandQueue(command);
}
void sendTestPenWidth()
{
NumberFormat nf = NumberFormat.getNumberInstance(Locale.UK);
DecimalFormat df = (DecimalFormat)nf;
df.applyPattern("##0.##");
StringBuilder sb = new StringBuilder();
sb.append(testPenWidthCommand)
.append(int(gridSize))
.append(",")
.append(df.format(testPenWidthStartSize))
.append(",")
.append(df.format(testPenWidthEndSize))
.append(",")
.append(df.format(testPenWidthIncrementSize))
.append(",END");
addToCommandQueue(sb.toString());
}
void sendSetPosition()
{
PVector p = getDisplayMachine().scaleToDisplayMachine(getMouseVector());
p = getDisplayMachine().convertToNative(p);
p = getDisplayMachine().inSteps(p);
String command = CMD_SETPOSITION+int(p.x+0.5)+","+int(p.y+0.5)+",END";
addToCommandQueue(command);
}
void sendStartTextAtPoint()
{
PVector p = getDisplayMachine().scaleToDisplayMachine(getMouseVector());
p = getDisplayMachine().convertToNative(p);
p = getDisplayMachine().inSteps(p);
String command = CMD_START_TEXT+(int)p.x+","+(int)p.y+","+gridSize+",2,END";
addToCommandQueue(command);
}
void sendSetHomePosition()
{
PVector pgCoords = getDisplayMachine().asNativeCoords(getHomePoint());
String command = CMD_SETPOSITION+int(pgCoords.x+0.5)+","+int(pgCoords.y+0.5)+",END";
addToCommandQueue(command);
}
int scaleDensity(int inDens, int inMax, int outMax)
{
float reducedDens = (float(inDens) / float(inMax)) * float(outMax);
reducedDens = outMax-reducedDens;
// println("inDens:"+inDens+", inMax:"+inMax+", outMax:"+outMax+", reduced:"+reducedDens);
// round up if bigger than .5
int result = int(reducedDens);
if (reducedDens - (result) > 0.5)
result ++;
//result = outMax - result;
return result;
}
SortedMap<Float, List<PVector>> divideIntoRows(Set<PVector> pixels, int direction)
{
SortedMap<Float, List<PVector>> inRows = new TreeMap<Float, List<PVector>>();
for (PVector p : pixels)
{
Float row = p.x;
if (direction == DRAW_DIR_SE || direction == DRAW_DIR_NW)
row = p.y;
if (!inRows.containsKey(row))
{
inRows.put(row, new ArrayList<PVector>());
}
inRows.get(row).add(p);
}
return inRows;
}
PVector sortPixelsInRowsAlternating(SortedMap<Float, List<PVector>> inRows, int initialDirection, float maxPixelSize)
{
PVector startPoint = null;
Comparator comp = null;
boolean rowIsAlongXAxis = true;
if (initialDirection == DRAW_DIR_SE || initialDirection == DRAW_DIR_NW)
{
rowIsAlongXAxis = true;
comp = xAscending;
}
else
{
rowIsAlongXAxis = false;
comp = yAscending;
}
// now sort each row, reversing the direction after each row
boolean reverse = false;
for (Float rowCoord : inRows.keySet())
{
println("row: " + rowCoord);
List<PVector> row = inRows.get(rowCoord);
if (reverse)
{
// reverse it (descending)
Collections.sort(row, comp);
Collections.reverse(row);
// if (startPoint == null)
// {
// if (rowIsAlongXAxis)
// startPoint = new PVector(row.get(0).x+(maxPixelSize/2.0), row.get(0).y);
// else
// startPoint = new PVector(row.get(0).x, row.get(0).y-(maxPixelSize/2.0));
// }
reverse = false;
}
else
{
// sort row ascending
Collections.sort(row, comp);
// if (startPoint == null)
// {
// if (rowIsAlongXAxis)
// startPoint = new PVector(row.get(0).x-(maxPixelSize/2.0), row.get(0).y);
// else
// startPoint = new PVector(row.get(0).x, row.get(0).y+(maxPixelSize/2.0));
// }
reverse = true;
}
}
return startPoint;
}
void sortPixelsInRows(SortedMap<Float, List<PVector>> inRows, int initialDirection)
{
PVector startPoint = null;
Comparator comp = null;
boolean rowIsAlongXAxis = true;
if (initialDirection == DRAW_DIR_SE || initialDirection == DRAW_DIR_NW)
{
rowIsAlongXAxis = true;
comp = xAscending;
}
else
{
rowIsAlongXAxis = false;
comp = yAscending;
}
// now sort each row, reversing the direction after each row
for (Float rowCoord : inRows.keySet())
{
println("row: " + rowCoord);
List<PVector> row = inRows.get(rowCoord);
// sort row ascending
Collections.sort(row, comp);
if (initialDirection == DRAW_DIR_NW || initialDirection == DRAW_DIR_NE)
Collections.reverse(row);
}
}
void sendPixels(Set<PVector> pixels, String pixelCommand, int initialDirection, int startCorner, float maxPixelSize, boolean scaleSizeToDensity)
{
// sort it into a map of rows, keyed by y coordinate value
SortedMap<Float, List<PVector>> inRows = divideIntoRows(pixels, initialDirection);
sortPixelsInRowsAlternating(inRows, initialDirection, maxPixelSize);
// that was easy.
// load the queue
// add a preamble
// set the first direction
int drawDirection = initialDirection;
String changeDir = CMD_CHANGEDRAWINGDIRECTION+getPixelDirectionMode()+"," + drawDirection +",END";
addToCommandQueue(changeDir);
// reverse the row sequence if the draw is starting from the bottom
// and reverse the pixel sequence if it needs to be done (odd number of rows)
boolean reversePixelSequence = false;
List<Float> rowKeys = new ArrayList<Float>();
rowKeys.addAll(inRows.keySet());
Collections.sort(rowKeys);
if (startCorner == DRAW_DIR_SE || startCorner == DRAW_DIR_SW)
{
Collections.reverse(rowKeys);
if (rowKeys.size() % 2 == 0)
reversePixelSequence = true;
}
// and move the pen to just next to the first pixel
List<PVector> firstRow = inRows.get(rowKeys.get(0));
PVector startPoint = firstRow.get(0);
int startPointX = int(startPoint.x);
int startPointY = int(startPoint.y);
int halfSize = int(maxPixelSize/2.0);
print("Dir:");
if (initialDirection == DRAW_DIR_SE)
{
startPointX-=halfSize;
println("SE");
}
else if (initialDirection == DRAW_DIR_SW)
{
startPointY-=halfSize;
println("SW");
}
else if (initialDirection == DRAW_DIR_NW)
{
startPointX-=halfSize;
println("NW");
}
else if (initialDirection == DRAW_DIR_NE)
{
startPointY-=halfSize;
println("NE");
}
if (startPoint != null)
{
String touchdown = CMD_CHANGELENGTH+int(startPointX)+","+int(startPointY)+",END";
addToCommandQueue(touchdown);
addToCommandQueue(CMD_PENDOWN+"END");
}
boolean penLifted = false;
// so for each row
for (Float key : rowKeys)
{
List<PVector> row = inRows.get(key);
if (reversePixelSequence)
Collections.reverse(row);
for (PVector v : row)
{
if (isHiddenPixel(v)) // check for masked pixels,
{
//println("It's outside the bright/dark threshold.");
if (liftPenOnMaskedPixels)
{
if (!penLifted) // if the pen isn't already up
{
String raisePen = CMD_PENUP + "END";
addToCommandQueue(raisePen);
penLifted = true;
}
else
{
// println("Pen is already lifted.");
}
// now convert to ints
int inX = int(v.x);
int inY = int(v.y);
int pixelSize = int(maxPixelSize);
// render a fully bright (255) pixel.
String command = pixelCommand+inX+","+inY+","+int(pixelSize+0.5)+",255,END";
addToCommandQueue(command);
}
else
{
//println("liftPenOnMaskedPixels is not selected.");
}
// so this pixel doesn't get added to the queue.
}
else // pixel wasn't masked - render it up
{
// now convert to ints
int inX = int(v.x);
int inY = int(v.y);
Integer density = int(v.z);
int pixelSize = int(maxPixelSize);
if (scaleSizeToDensity)
{
pixelSize = scaleDensity(density, 255, int(maxPixelSize));
density = 0;
}
int scaledPixelSize = int((pixelSize*getPixelScalingOverGridSize())+0.5);
String command = pixelCommand+inX+","+inY+","+scaledPixelSize+","+density+",END";
// put the pen down if lifting over masked pixels is on
if (liftPenOnMaskedPixels && penLifted)
{
// println("Pen down.");
String lowerPen = CMD_PENDOWN + "END";
addToCommandQueue(lowerPen);
penLifted = false;
}
addToCommandQueue(command);
}
}
drawDirection = flipDrawDirection(drawDirection);
String command = CMD_CHANGEDRAWINGDIRECTION+getPixelDirectionMode()+"," + drawDirection +",END";
addToCommandQueue(command);
}
addToCommandQueue(CMD_PENUP+"END");
numberOfPixelsTotal = commandQueue.size();
startPixelTimer();
}
int flipDrawDirection(int curr)
{
if (curr == DRAW_DIR_SE)
return DRAW_DIR_NW;
else if (curr == DRAW_DIR_NW)
return DRAW_DIR_SE;
else if (curr == DRAW_DIR_NE)
return DRAW_DIR_SW;
else if (curr == DRAW_DIR_SW)
return DRAW_DIR_NE;
else return DRAW_DIR_SE;
}
int getPixelDirectionMode()
{
return pixelDirectionMode;
}
void sendSawtoothPixels(Set<PVector> pixels)
{
sendPixels(pixels, CMD_DRAWSAWPIXEL, renderStartDirection, renderStartPosition, getGridSize(), false);
}
void sendCircularPixels(Set<PVector> pixels)
{
sendPixels(pixels, CMD_DRAWROUNDPIXEL, renderStartDirection, renderStartPosition, getGridSize(), false);
}
void sendScaledSquarePixels(Set<PVector> pixels)
{
sendPixels(pixels, CMD_DRAWPIXEL, renderStartDirection, renderStartPosition, getGridSize(), true);
}
void sendSolidSquarePixels(Set<PVector> pixels)
{
for (PVector p : pixels)
{
if (p.z != MASKED_PIXEL_BRIGHTNESS)
p.z = 0.0;
}
sendPixels(pixels, CMD_DRAWPIXEL, renderStartDirection, renderStartPosition, getGridSize(), false);
}
void sendSquarePixels(Set<PVector> pixels)
{
sendPixels(pixels, CMD_DRAWPIXEL, renderStartDirection, renderStartPosition, getGridSize(), false);
}
void sendScribblePixels(Set<PVector> pixels)
{
sendPixels(pixels, CMD_DRAWSCRIBBLEPIXEL, renderStartDirection, renderStartPosition, getGridSize(), false);
}
void sendOutlineOfPixels(Set<PVector> pixels)
{
// sort it into a map of rows, keyed by y coordinate value
SortedMap<Float, List<PVector>> inRows = divideIntoRows(pixels, DRAW_DIR_SE);
sortPixelsInRowsAlternating(inRows, DRAW_DIR_SE, getGridSize());
float halfGrid = getGridSize() / 2.0;
for (Float key : inRows.keySet())
{
for (PVector p : inRows.get(key))
{
PVector startPoint = new PVector(p.x-halfGrid, p.y-halfGrid);
PVector endPoint = new PVector(p.x+halfGrid, p.y+halfGrid);
String command = CMD_DRAWRECT + int(startPoint.x)+","+int(startPoint.y)+","+int(endPoint.x)+","+int(endPoint.y)+",END";
addToCommandQueue(command);
}
}
}
void sendOutlineOfRows(Set<PVector> pixels, int drawDirection)
{
// sort it into a map of rows, keyed by y coordinate value
SortedMap<Float, List<PVector>> inRows = divideIntoRows(pixels, drawDirection);
sortPixelsInRows(inRows, drawDirection);
PVector offset = new PVector(getGridSize() / 2.0, getGridSize() / 2.0);
for (Float key : inRows.keySet())
{
PVector startPoint = inRows.get(key).get(0);
PVector endPoint = inRows.get(key).get(inRows.get(key).size()-1);
if (drawDirection == DRAW_DIR_SE)
{
startPoint.sub(offset);
endPoint.add(offset);
}
else if (drawDirection == DRAW_DIR_NW)
{
startPoint.add(offset);
endPoint.sub(offset);
}
else if (drawDirection == DRAW_DIR_SW)
{
startPoint.add(offset);
endPoint.sub(offset);
}
else if (drawDirection == DRAW_DIR_NW)
{
startPoint.add(offset);
endPoint.sub(offset);
}
String command = CMD_DRAWRECT + int(startPoint.x)+","+int(startPoint.y)+","+int(endPoint.x)+","+int(endPoint.y)+",END";
addToCommandQueue(command);
}
}
void sendGridOfBox(Set<PVector> pixels)
{
sendOutlineOfRows(pixels, DRAW_DIR_SE);
sendOutlineOfRows(pixels, DRAW_DIR_SW);
}
void sendOutlineOfBox()
{
// convert cartesian to native format
PVector tl = getDisplayMachine().inSteps(getBoxVector1());
PVector br = getDisplayMachine().inSteps(getBoxVector2());
PVector tr = new PVector(br.x, tl.y);
PVector bl = new PVector(tl.x, br.y);
tl = getDisplayMachine().asNativeCoords(tl);
tr = getDisplayMachine().asNativeCoords(tr);
bl = getDisplayMachine().asNativeCoords(bl);
br = getDisplayMachine().asNativeCoords(br);
String cmd = (true) ? CMD_CHANGELENGTHDIRECT : CMD_CHANGELENGTH;
String command = cmd+(int)tl.x+","+(int)tl.y+","+getMaxSegmentLength()+",END";
addToCommandQueue(command);
command = cmd+(int)tr.x+","+(int)tr.y+","+getMaxSegmentLength()+",END";
addToCommandQueue(command);
command = cmd+(int)br.x+","+(int)br.y+","+getMaxSegmentLength()+",END";
addToCommandQueue(command);
command = cmd+(int)bl.x+","+(int)bl.y+","+getMaxSegmentLength()+",END";
addToCommandQueue(command);
command = cmd+(int)tl.x+","+(int)tl.y+","+getMaxSegmentLength()+",END";
addToCommandQueue(command);
}
void sendVectorShapes()
{
sendVectorShapes(getVectorShape(), vectorScaling/100, getVectorPosition(), PATH_SORT_NONE);
}
void sendVectorShapes(RShape vec, float scaling, PVector position, int pathSortingAlgorithm)
{
println("Send vector shapes.");
RPoint[][] pointPaths = vec.getPointsInPaths();
// sort the paths to optimise the draw sequence
switch (pathSortingAlgorithm) {
case PATH_SORT_MOST_POINTS_FIRST: pointPaths = sortPathsLongestFirst(pointPaths, pathLengthHighPassCutoff); break;
case PATH_SORT_GREATEST_AREA_FIRST: pointPaths = sortPathsGreatestAreaFirst(vec, pathLengthHighPassCutoff); break;
case PATH_SORT_CENTRE_FIRST: pointPaths = sortPathsCentreFirst(vec, pathLengthHighPassCutoff); break;
}
String command = "";
PVector lastPoint = new PVector();
boolean liftToGetToNewPoint = true;
// go through and get each path
for (int i = 0; i<pointPaths.length; i++)
{
if (pointPaths[i] != null)
{
boolean firstPointFound = false;
if (pointPaths[i].length > pathLengthHighPassCutoff)
{
List<PVector> filteredPoints = filterPoints(pointPaths[i], VECTOR_FILTER_LOW_PASS, minimumVectorLineLength, scaling, position);
if (!filteredPoints.isEmpty())
{
// draw the first one with a pen up and down to get to it
PVector p = filteredPoints.get(0);
if ( p.x == lastPoint.x && p.y == lastPoint.y )
liftToGetToNewPoint = false;
else
liftToGetToNewPoint = true;
// pen UP! (IF THE NEW POINT IS DIFFERENT FROM THE LAST ONE!)
if (liftToGetToNewPoint)
addToCommandQueue(CMD_PENUP+"END");
// move to this point and put the pen down
command = CMD_CHANGELENGTHDIRECT+Math.round(p.x)+","+Math.round(p.y)+","+getMaxSegmentLength()+",END";
addToCommandQueue(command);
if (liftToGetToNewPoint)
addToCommandQueue(CMD_PENDOWN+"END");
// then just iterate through the rest
for (int j=1; j<filteredPoints.size(); j++)
{
p = filteredPoints.get(j);
command = CMD_CHANGELENGTHDIRECT+Math.round(p.x)+","+Math.round(p.y)+","+getMaxSegmentLength()+",END";
addToCommandQueue(command);
}
lastPoint = new PVector(p.x, p.y);
}
}
}
}
addToCommandQueue(CMD_PENUP+"END");
println("finished.");
}
public RPoint[][] sortPathsLongestFirst(RPoint[][] pointPaths, int highPassCutoff)
{
// put the paths into a list
List<RPoint[]> pathsList = new ArrayList<RPoint[]>(pointPaths.length);
for (int i = 0; i<pointPaths.length; i++)
{
if (pointPaths[i] != null)
{
pathsList.add(pointPaths[i]);
}
}
// sort the list
Collections.sort(pathsList, new Comparator<RPoint[]>() {
public int compare(RPoint[] o1, RPoint[] o2) {
if (o1.length > o2.length) {
return -1;
}
else if (o1.length < o2.length) {
return 1;
}
else {
return 0;
}
}
}
);
// filter out some short paths
pathsList = removeShortPaths(pathsList, highPassCutoff);
// and put them into a new array
for (int i=0; i<pathsList.size(); i++)
{
pointPaths[i] = pathsList.get(i);
}
return pointPaths;
}
public RPoint[][] sortPathsGreatestAreaFirst(RShape vec, int highPassCutoff)
{
// put the paths into a list
SortedMap<Float, RPoint[]> pathsList = new TreeMap<Float, RPoint[]>();
int noOfChildren = vec.countChildren();
for (int i=0; i < noOfChildren; i++)
{
float area = vec.children[i].getArea();
RPoint[] path = vec.children[i].getPointsInPaths()[0];
pathsList.put(area, path);
}
RPoint[][] pointPaths = vec.getPointsInPaths();
List<RPoint[]> filtered = new ArrayList<RPoint[]>();
// and put them into a new array
int i = 0;
for (Float k : pathsList.keySet())
{
if (k >= highPassCutoff)
{
filtered.add(pathsList.get(k));
println("Filtered kept path of area " + k);
}
else
println("Filtered discarded path of area " + k);
}
pointPaths = new RPoint[filtered.size()][];
for (i = 0; i < filtered.size(); i++)
{
pointPaths[i] = filtered.get(i);
}
return pointPaths;
}
public RPoint[][] sortPathsCentreFirst(RShape vec, int highPassCutoff)
{
// put the paths into a list
int noOfChildren = vec.countChildren();
List<RShape> pathsList = new ArrayList<RShape>(noOfChildren);
for (int i=0; i < noOfChildren; i++)
pathsList.add(vec.children[i]);
List<RShape> orderedPathsList = new ArrayList<RShape>(noOfChildren);
// make a tiny area in the centre of the shape,
// plan to increment the size of the area until it covers vec entirely
// (radius of area min = 0, max = distance from shape centre to any corner.)
float aspectRatio = vec.getHeight() / vec.getWidth();
int n = 0;
float w = 1.0;
float h = w * aspectRatio;
RPoint topLeft = vec.getTopLeft();
RPoint botRight = vec.getBottomRight();
PVector centre = new PVector(vec.getWidth()/2, vec.getHeight()/2);
float vecWidth = vec.getWidth();
while (w <= vecWidth)
{
w+=6.0;
h = w * aspectRatio;
//println(n++ + ". Rect w " + w + ", h " + h);
RShape field = RShape.createRectangle(centre.x-(w/2.0), centre.y-(h/2.0), w, h);
// add all the shapes that are entirely inside the circle to orderedPathsList
ListIterator<RShape> it = pathsList.listIterator();
int shapesAdded = 0;
while (it.hasNext())
{
RShape sh = it.next();
if (field.contains(sh.getCenter()))
{
orderedPathsList.add(sh);
// remove the shapes from pathsList (so it isn't found again)
shapesAdded++;
it.remove();
}
}
// increase the size of the circle and try again
}
RPoint[][] pointPaths = new RPoint[orderedPathsList.size()][];// vec.getPointsInPaths();
for (int i = 0; i < orderedPathsList.size(); i++)
{
pointPaths[i] = orderedPathsList.get(i).getPointsInPaths()[0];
}
return pointPaths;
}
List<RPoint[]> removeShortPaths(List<RPoint[]> list, int cutoff)
{
if (cutoff > 0)
{
int numberOfPaths = list.size();
ListIterator<RPoint[]> it = list.listIterator();
while (it.hasNext ())
{
RPoint[] paths = it.next();
if (paths == null || cutoff >= paths.length)
{
it.remove();
}
}
}
return list;
}
List<PVector> filterPoints(RPoint[] points, int filterToUse, long filterParam, float scaling, PVector position)
{
return filterPointsLowPass(points, filterParam, scaling, position);
}
List<PVector> filterPointsLowPass(RPoint[] points, long filterParam, float scaling, PVector position)
{
List<PVector> result = new ArrayList<PVector>();
// scale and convert all the points first
List<PVector> scaled = new ArrayList<PVector>(points.length);
println("a filterPointsLowPass: Scaled length: " + points.length);
for (int j = 0; j<points.length; j++)
{
RPoint firstPoint = points[j];
PVector p = new PVector(firstPoint.x, firstPoint.y);
p = PVector.mult(p, scaling);
p = PVector.add(p, position);
p = getDisplayMachine().inSteps(p);
if (getDisplayMachine().getPictureFrame().surrounds(p))
{
p = getDisplayMachine().asNativeCoords(p);
scaled.add(p);
}
}
println("b filterPointsLowPass: Scaled length: " + scaled.size());
if (scaled.size() > 1.0)
{
PVector p = scaled.get(0);
result.add(p);
for (int j = 1; j<scaled.size(); j++)
{
p = scaled.get(j);
// and even then, only bother drawing if it's a move of over "x" steps
int diffx = abs(int(p.x) - int(result.get(result.size()-1).x));
int diffy = abs(int(p.y) - int(result.get(result.size()-1).y));
if (diffx > filterParam || diffy > filterParam)
{
println(j + ". Adding point " + p + " because diffx (" + diffx + ") or diffy (" + diffy + ") is > " + filterParam + ", last: " + result.get(result.size()-1));
result.add(p);
}
}
}
println("c filterPointsLowPass: Scaled length: " + result.size());
if (result.size() < 2)
result.clear();
//println("finished filter.");
return result;
}
void sendMachineStoreMode()
{
String overwrite = ",R";
if (!getOverwriteExistingStoreFile())
overwrite = ",A";
addToRealtimeCommandQueue(CMD_MACHINE_MODE_STORE_COMMANDS + getStoreFilename()+overwrite+",END");
}
void sendMachineLiveMode()
{
addToCommandQueue(CMD_MACHINE_MODE_LIVE+"END");
}
void sendMachineExecMode()
{
sendMachineLiveMode();
if (storeFilename != null && !"".equals(storeFilename))
addToCommandQueue(CMD_MACHINE_MODE_EXEC_FROM_STORE + getStoreFilename() + ",END");
}
void sendRandomDraw()
{
addToCommandQueue(CMD_RANDOM_DRAW+"END");
}
void sendStartSwirling()
{
addToCommandQueue(CMD_SWIRLING+"1,END");
}
void sendStopSwirling()
{
addToCommandQueue(CMD_SWIRLING+"0,END");
}
void sendDrawRandomSprite(String spriteFilename)
{
addToCommandQueue(CMD_DRAW_RANDOM_SPRITE+","+spriteFilename+",100,500,END");
}