summaryrefslogtreecommitdiff
path: root/trunk/users/adrian/host/src/org/reprap/geometry/LayerProducer.java
blob: 9553cc5e9cfa358bd41d43d6b5707b2758835a2c (plain)
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
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
/*
 * Created on May 1, 2006
 *
 * Changed by Vik to reject polts of less than 0.05mm
 */
package org.reprap.geometry;

import java.io.IOException;

import javax.media.j3d.BranchGroup;

import org.reprap.Printer;
import org.reprap.Attributes;
import org.reprap.Preferences;
import org.reprap.ReprapException;
import org.reprap.Extruder;
import org.reprap.devices.pseudo.LinePrinter;
import org.reprap.geometry.polygons.Rr2Point;
//import org.reprap.geometry.polygons.RrCSGPolygonList;
import org.reprap.geometry.polygons.BooleanGridList;
import org.reprap.geometry.polygons.RrPolygon;
import org.reprap.geometry.polygons.RrPolygonList;
import org.reprap.geometry.polygons.RrRectangle;
import org.reprap.geometry.polygons.BooleanGrid;
import org.reprap.utilities.Debug;
import org.reprap.utilities.RrGraphics;

/**
 *
 */
class segmentSpeeds
{
	/**
	 * 
	 */
	public Rr2Point p1, p2, p3;
	
	/**
	 * 
	 */
	public double ca;
	
	/**
	 * 
	 */
	public boolean plotMiddle;
	
	/**
	 * 
	 */
	public boolean abandon;
	
	/**
	 * @param before
	 * @param now
	 * @param after
	 * @param fastLength
	 */
	public segmentSpeeds(Rr2Point before, Rr2Point now, Rr2Point after, double fastLength)
	{
		Rr2Point a = Rr2Point.sub(now, before);
		double amod = a.mod();
		abandon = amod == 0;
		if(abandon)
			return;
		Rr2Point b = Rr2Point.sub(after, now);
		if(b.mod() == 0)
			ca = 0;
		else
			ca = Rr2Point.mul(a.norm(), b.norm());
		plotMiddle = true;
		if(amod <= 2*fastLength)
		{
			fastLength = amod*0.5;
			plotMiddle = false;
		}
		a = a.norm();
		p1 = Rr2Point.add(before, Rr2Point.mul(a, fastLength));
		p2 = Rr2Point.add(p1, Rr2Point.mul(a, amod - 2*fastLength));
		p3 = Rr2Point.add(p2, Rr2Point.mul(a, fastLength));
	}
	
	int speed(int currentSpeed, double angFac)
	{
		double fac = (1 - 0.5*(1 + ca)*angFac);
		return LinePrinter.speedFix(currentSpeed, fac);
	}
}

/**
 *
 */
public class LayerProducer {
	
	private RrGraphics simulationPlot = null;
	
	/**
	 * 
	 */
	private LayerRules layerConditions = null;
	
	/**
	 * 
	 */
	private boolean paused = false;

	/**
	 * The shape of the object built so far under the current layer
	 */
	//private BranchGroup lowerShell = null;

	
	/**
	 * The polygons to infill
	 */
	//private RrPolygonList hatchedPolygons = null;
	
	private RrPolygonList allPolygons[];
	
	/**
	 * The polygons to outline
	 */
	//private RrPolygonList borderPolygons = null;
	
	/**
	 * Boolean Grid representation of the polygons as input
	 */
	//private BooleanGridList boolGrdSlice = null;
	
	/**
	 * CSG representation of the polygons offset by the width of
	 * the extruders
	 */
	//RrCSGPolygonList offBorder = null;
	
	/**
	 * CSG representation of the polygons offset by the factor of the
	 * width of the extruders needed to lay down internal cross-hatching
	 */	
	//BooleanGridList offHatch = null;
	
	/**
	 * Counters use so that each material is plotted completely before
	 * moving on to the next.
	 */
	//private int commonBorder, commonHatch;
	
	/**
	 * The clue is in the name...
	 */
	private double currentFeedrate;
	
	/**
	 * Record the end of each polygon as a clue where to start next
	 */
	private Rr2Point startNearHere = null;
	
	//private boolean shellSet = false;
	
	/**
	 * Flag to prevent cyclic graphs going round forever
	 */
	private boolean beingDestroyed = false;
	
	/**
	 * Destroy me and all that I point to
	 */
	public void destroy() 
	{
		if(beingDestroyed) // Prevent infinite loop
			return;
		beingDestroyed = true;
		
		// Keep the lower shell - the graphics system is using it
		
		//lowerShell = null;

		// Keep the printer; that's needed for the next layer

		//printer = null;
		
//		if(hatchedPolygons != null)
//			hatchedPolygons.destroy();
//		hatchedPolygons = null;
//		
//		if(borderPolygons != null)
//			borderPolygons.destroy();
//		borderPolygons = null;
//		
//		//if(boolGrdSlice != null)
//		//	boolGrdSlice.destroy();
//		boolGrdSlice = null;
//		
//		//if(offHatch != null)
//			//offHatch.destroy();
//		offHatch = null;
		
		if(startNearHere != null)
			startNearHere.destroy();
		startNearHere = null;
		allPolygons = null;
		beingDestroyed = false;
	}
	
	/**
	 * Destroy just me
	 */
	protected void finalize() throws Throwable
	{
		// Keep the lower shell - the graphics system is using it
		
		//lowerShell = null;

		// Keep the printer; that's needed for the next layer

		//printer = null;
		
//		hatchedPolygons = null;
//		borderPolygons = null;
//		boolGrdSlice = null;
//		offHatch = null;
		allPolygons = null;
		startNearHere = null;
		super.finalize();
	}
	
	/**
	 * Set up a layer consisting of a single material in a single pre-computed list.
	 * Used for creating foundations.
	 * @param pols
	 * @param lc
	 * @param simPlot
	 * @throws Exception
	 */
//	public LayerProducer(RrPolygonList pols, LayerRules lc, RrGraphics simPlot) throws Exception 
//	{
//		layerConditions = lc;
//		startNearHere = null; //new Rr2Point(0, 0);
//		//lowerShell = null;
//		//shellSet = false;
//		simulationPlot = simPlot;
//		
//		boolGrdSlice = null;
//		
//		borderPolygons = null;
//		
//		hatchedPolygons = pols;
//		
//		
//		if(simulationPlot != null)
//		{
//			if(!simulationPlot.isInitialised())
//				simulationPlot.init(lc.getBox(), false);
//			else
//				simulationPlot.cleanPolygons();
//		}
//
//	}
//	
	
	/**
	 * Set up a normal layer
	 * @param boolGrdSliceols
	 * @param ls
	 * @param lc
	 * @param simPlot
	 * @throws Exception
	 */
	//public LayerProducer(BooleanGridList bgPols, LayerRules lc, RrGraphics simPlot) throws Exception 
	public LayerProducer(RrPolygonList ap[], LayerRules lc, RrGraphics simPlot) throws Exception 
	{
		layerConditions = lc;
		startNearHere = null;
		//lowerShell = ls;
		//shellSet = false;
		simulationPlot = simPlot;
		
		allPolygons = ap;
		
		//boolGrdSlice = bgPols;
		
		// The next two are experimental for the moment...
		
		//inFillCalculations();
		//supportCalculations();
		
		//offHatch = boolGrdSlice.offset(layerConditions, false);
		
//		if(layerConditions.getLayingSupport())
//		{
//			borderPolygons = null;
//			offHatch = offHatch.union(lc.getPrinter().getExtruders());
//		} else
//		{
//			BooleanGridList offBorder = boolGrdSlice.offset(layerConditions, true);
//			borderPolygons = offBorder.borders();
//		}
//			
//		hatchedPolygons = offHatch.hatch(layerConditions);
//
//
//		if(borderPolygons != null && borderPolygons.size() > 0)
//		{
//			borderPolygons.middleStarts(hatchedPolygons, layerConditions);
//			if(Preferences.loadGlobalBool("Shield"))
//			{
//				RrRectangle rr = lc.getBox();
//				Rr2Point corner = Rr2Point.add(rr.sw(), new Rr2Point(-3, -3));
//				RrPolygon ell = new RrPolygon(borderPolygons.polygon(0).getAttributes(), false);
//				ell.add(corner);
//				ell.add(Rr2Point.add(corner, new Rr2Point(-2, 10)));
//				ell.add(Rr2Point.add(corner, new Rr2Point(-2, -2)));
//				ell.add(Rr2Point.add(corner, new Rr2Point(20, -2)));
//				borderPolygons.add(0, ell);
//			}
//		}
		
		if(simulationPlot != null)
		{
			if(!simulationPlot.isInitialised())
			{
				RrRectangle rec = lc.getBox();
				if(Preferences.loadGlobalBool("Shield"))
					rec.expand(Rr2Point.add(rec.sw(), new Rr2Point(-7, -7))); // TODO: Yuk - this should be a parameter
				simulationPlot.init(rec, false);
			} else
				simulationPlot.cleanPolygons();
		}
	}
	
//	/**
//	 * look at the layer above where we are, and support everything
//	 * in it that has nothing under it in this layer.
//	 *
//	 */
//	private void supportCalculations()
//	{
//		// We can only work out support if we're going top down
//		
//		if(!layerConditions.getTopDown())
//			return;
//		
//		// Get the layer immediately above
//		
//		RrCSGPolygonList above = layerConditions.getLayerAbove(1);
//		
//		// If there was no layer immediately above, record this layer to 
//		// be the one above for the next layer down and return.
//		
//		if(above == null)
//		{
//			layerConditions.recordThisLayer(boolGrdSlice);
//			return;
//		}
//		
//		// Pick up our materials
//		
//		Extruder [] es = layerConditions.getPrinter().getExtruders();
//		
//		// A list for the supports for the materials in the layer above
//		
//		RrCSGPolygonList supports = new RrCSGPolygonList();
//		
//		// Each material in this layer unioned with the same material in the layer above
//		// This may be what needs support on the next layer down.
//		
//		RrCSGPolygonList thisForTheRecord = new RrCSGPolygonList();
//		
//		// Compute the union of everything on this layer and grow it by the
//		// extrusion height (i.e. grow to a 45 degree overhang).  Nothing in that region
//		// will need any support.
//		
//		RrCSGPolygon allThisLayer = new RrCSGPolygon();
//		for(int i = 0; i < boolGrdSlice.size(); i++)
//		{
//			allThisLayer = RrCSGPolygon.union(boolGrdSlice.get(i), allThisLayer);
//			if(i > 0)
//				allThisLayer = allThisLayer.reEvaluate();
//		}
//		RrCSGPolygon allThisLayerGrown = allThisLayer.offset(layerConditions.getZStep());
//	
//		// This material's shape in the above layer, its attributes,
//		// the extruder used for it, and the name of its support material.
//		
//		RrCSGPolygon pgAboveLevel;
//		Attributes aAboveLevel;
//		Extruder eAboveLevel;
//		String supportName;
//		
//		// The polygons at this level
//		
//		RrCSGPolygon thisLevel;
//		
//		// For each material in the layer above...
//		
//		for(int i = 0; i < above.size(); i++)
//		{
//			// Get this material's shape in the above layer, its attributes,
//			// the extruder used for it, and the name of its support material.
//			
//			pgAboveLevel = above.get(i);
//			aAboveLevel = pgAboveLevel.getAttributes();
//			eAboveLevel = aAboveLevel.getExtruder(es);
//			supportName = eAboveLevel.getSupportMaterial();
//			
//			// If this stuff's support is not called "null"...
//			
//			if(!supportName.contentEquals("null"))
//			{
//				// Pick up the support extruder
//				
//				Extruder supportExtruder = es[GenericExtruder.getNumberFromMaterial(supportName)];
//				
//				// Find the same material at this level
//				
//				thisLevel = boolGrdSlice.find(aAboveLevel);
//				
//				// If the material is in this level and the one above...
//				
//				if(thisLevel != null)
//				{
//					// The union of them both is the shape that may need support at the next level down
//					
//					RrCSGPolygon toRemember = RrCSGPolygon.union(pgAboveLevel, thisLevel);
//					toRemember = toRemember.reEvaluate();
//					thisForTheRecord.add(toRemember);
//					
//					// The bit left over of the level above after we subtract all this layer
//					// is what needs support
//					
//					RrCSGPolygon sup = RrCSGPolygon.difference(pgAboveLevel, allThisLayerGrown);
//					sup = sup.reEvaluate();
//					sup.setAttributes(new Attributes(supportName, null, null, 
//							supportExtruder.getAppearance()));
//					supports.add(sup);
//				} else
//					
//					// If the material wasn't in this layer, carry the need to support it on down
//					
//					thisForTheRecord.add(pgAboveLevel);
//			}
//		}
//		
//		// Add every material in this layer that has no equivalent in the layer
//		// above as it may need support in the next layer down.
//		
//		for(int i = 0; i < boolGrdSlice.size(); i++)
//		{
//			thisLevel = boolGrdSlice.get(i);
//			if(above.find(thisLevel.getAttributes()) == null)
//				thisForTheRecord.add(thisLevel);	
//		}
//		
//		// Record everything in this layer as potentially needing support
//		// in the next layer down.
//		
//		layerConditions.recordThisLayer(thisForTheRecord);
//		
//		// Add all the supports needed to this layer
//		
//		boolGrdSlice.add(supports);
//		
//	}
//	
//	/**
//	 * look at the layer above where we are, and calculate the infill depending
//	 * on whether something is an open surface or not.
//	 *
//	 */
//	private void inFillCalculations()
//	{
//		// We can only work out infill if we're going top down
//		
//		if(!layerConditions.getTopDown())
//			return;
//		
//		// Get the layer immediately above
//		
//		RrCSGPolygonList above = layerConditions.getLayerAbove(2);
//		if(above == null)
//		{
//			layerConditions.recordThisLayer(boolGrdSlice);
//			return;
//		}
//		
//		// Pick up our materials
//		
//		Extruder [] es = layerConditions.getPrinter().getExtruders();
//		
//		//Horrid hack...
//		
//		if(layerConditions.getModelLayer() < es[0].getLowerFineLayers())
//			return;
//		
//		// Compute the union of everything on the layer above
//		// Nothing in that region will have a free surface in this layer.
//
//		RrCSGPolygon allAboveLayer = new RrCSGPolygon();
//		for(int i = 0; i < above.size(); i++)
//		{
//			allAboveLayer = RrCSGPolygon.union(above.get(i), allAboveLayer);
//			if(i > 0)
//				allAboveLayer = allAboveLayer.reEvaluate();
//		}
//		
//
//		
//		// A list for the infills
//		
//		RrCSGPolygonList inFills = new RrCSGPolygonList();
//		
//		// A list for the free surface bits
//		
//		RrCSGPolygonList surfaces = new RrCSGPolygonList();	
//
//		// This material's shape, its attributes,
//		// the extruder used for it, and the name of its infill material.
//		
//		RrCSGPolygon pgThisLevel;
//		Attributes aThisLevel;
//		Extruder eThisLevel;
//		String inFillName;
//		
//		// For each material in this layer
//		
//		for(int i = 0; i < boolGrdSlice.size(); i++)
//		{
//			// Get this material's shape in the above layer, its attributes,
//			// the extruder used for it, and the name of its support material.
//			
//			pgThisLevel = boolGrdSlice.get(i);
//			aThisLevel = pgThisLevel.getAttributes();
//			eThisLevel = aThisLevel.getExtruder(es);
//			inFillName = eThisLevel.getBroadInfillMaterial();
//			
//			// If this stuff's infill is not called "null"...
//
//			if(!inFillName.contentEquals("null"))
//			{
//				// Pick up the infill extruder
//
//				Extruder inFillExtruder = es[GenericExtruder.getNumberFromMaterial(inFillName)];
//
//				// The exposed region is whatever's in this layer that isn't above
//				
//				RrCSGPolygon exposed = RrCSGPolygon.difference(pgThisLevel, allAboveLayer);
//				exposed = exposed.reEvaluate();
//				
//				// Make the exposed layer bigger...
//				
//				exposed = exposed.offset(layerConditions.getZStep()*eThisLevel.getLowerFineLayers());
//				
//				// ...Then intersect it with what we have already.  That will cause the exposed
//				// region to penetrate the solid a bit.
//				
//				exposed = RrCSGPolygon.intersection(exposed, pgThisLevel);
//				exposed = exposed.reEvaluate();
//				surfaces.add(exposed);
//				
//				// What's left of this layer after the exposed bit is subtracted is the infill part
//				
//				pgThisLevel = RrCSGPolygon.difference(pgThisLevel, exposed);
//				pgThisLevel = pgThisLevel.reEvaluate();
//				pgThisLevel.setAttributes(new Attributes(inFillName, null, null, 
//						inFillExtruder.getAppearance()));
//				inFills.add(pgThisLevel);
//			}
//		}
//		
//		// We now have two new collections of polygons representing this layer
//		
//		boolGrdSlice = inFills;
//		boolGrdSlice.add(surfaces);
//		layerConditions.recordThisLayer(boolGrdSlice);
//	}
	
	/**
	 * Stop printing
	 *
	 */
	public void pause()
	{
		paused = true;
	}
	
	/**
	 * Start printing
	 *
	 */
	public void resume()
	{
		paused = false;
	}
	
	/**
	 * @return current X and Y position of the printer
	 */
	private Rr2Point posNow()
	{
		return new Rr2Point(layerConditions.getPrinter().getX(), layerConditions.getPrinter().getY());
	}
	
	/**
	 * speed up for short lines
	 * @param p
	 * @return
	 * @throws ReprapException
	 * @throws IOException
	 */
	private boolean shortLine(Rr2Point p, boolean stopExtruder, boolean closeValve) throws ReprapException, IOException
	{
		Printer printer = layerConditions.getPrinter();
		double shortLen = printer.getExtruder().getShortLength();
		if(shortLen < 0)
			return false;
		Rr2Point a = Rr2Point.sub(posNow(), p);
		double amod = a.mod();
		if(amod > shortLen) {
//			Debug.d("Long segment.  Current feedrate is: " + currentFeedrate);
			return false;
		}

		//printer.setFeedrate(printer.getExtruder().getShortLineFeedrate());
// TODO: FIX THIS
//		printer.setSpeed(LinePrinter.speedFix(printer.getExtruder().getXYSpeed(), 
//				printer.getExtruder().getShortSpeed()));
		printer.printTo(p.x(), p.y(), layerConditions.getMachineZ(), printer.getExtruder().getShortLineFeedrate(), stopExtruder, closeValve);
		//printer.setFeedrate(currentFeedrate);
		return true;	
	}
	
	/**
	 * @param first First point, the end of the line segment to be plotted to from the current position.
	 * @param second Second point, the end of the next line segment; used for angle calculations
	 * @param turnOff True if the extruder should be turned off at the end of this segment.
	 * @throws ReprapException
	 * @throws IOException
	 */
	private void plot(Rr2Point first, Rr2Point second, boolean stopExtruder, boolean closeValve) throws ReprapException, IOException
	{
		Printer printer = layerConditions.getPrinter();
		if (printer.isCancelled()) return;
		
		// Don't call delay; this isn't controlling the printer
		while(paused)
		{
			try
			{
				Thread.sleep(200);
			} catch (Exception ex) {}
		}
		
		if(shortLine(first, stopExtruder, closeValve))
			return;
		
		double z = layerConditions.getMachineZ();
		
		double speedUpLength = printer.getExtruder().getAngleSpeedUpLength();
		if(speedUpLength > 0)
		{
			segmentSpeeds ss = new segmentSpeeds(posNow(), first, second, 
					speedUpLength);
			if(ss.abandon)
				return;

			printer.printTo(ss.p1.x(), ss.p1.y(), z, currentFeedrate, false, false);

			if(ss.plotMiddle)
			{
//TODO: FIX THIS.
//				int straightSpeed = LinePrinter.speedFix(currentSpeed, (1 - 
//						printer.getExtruder().getAngleSpeedFactor()));
				//printer.setFeedrate(printer.getExtruder().getAngleFeedrate());
				printer.printTo(ss.p2.x(), ss.p2.y(), z, printer.getExtruder().getAngleFeedrate(), false, false);
			}

			//printer.setSpeed(ss.speed(currentSpeed, printer.getExtruder().getAngleSpeedFactor()));
			
			//printer.setFeedrate(printer.getExtruder().getAngleFeedrate());
			printer.printTo(ss.p3.x(), ss.p3.y(), z, printer.getExtruder().getAngleFeedrate(), stopExtruder, closeValve);
			//pos = ss.p3;
		// Leave speed set for the start of the next line.
		} else
			printer.printTo(first.x(), first.y(), z, currentFeedrate, stopExtruder, closeValve);
	}
	
	private void singleMove(Rr2Point p)
	{
		Printer pt = layerConditions.getPrinter();
		pt.singleMove(p.x(), p.y(), pt.getZ(), pt.getFastXYFeedrate());
	}
	
	/**
	 * @param first
	 * @param second
	 * @param startUp
	 * @param endUp
	 * @throws ReprapException
	 * @throws IOException
	 */
	private void move(Rr2Point first, Rr2Point second, boolean startUp, boolean endUp, boolean fast) 
		throws ReprapException, IOException
	{
		Printer printer = layerConditions.getPrinter();
		
		if (printer.isCancelled()) return;
		
//		 Don't call delay; this isn't controlling the printer
		while(paused)
		{
			try
			{
				Thread.sleep(200);
			} catch (Exception ex) {}
		}
		
		double z = layerConditions.getMachineZ();
		
		//if(startUp)
		if(fast)
		{
			//printer.setFeedrate(printer.getFastFeedrateXY());
			printer.moveTo(first.x(), first.y(), z, printer.getExtruder().getFastXYFeedrate(), startUp, endUp);
			return;
		}
		
		double speedUpLength = printer.getExtruder().getAngleSpeedUpLength();
		if(speedUpLength > 0)
		{
			segmentSpeeds ss = new segmentSpeeds(posNow(), first, second, 
					speedUpLength);
			if(ss.abandon)
				return;

			printer.moveTo(ss.p1.x(), ss.p1.y(), z, printer.getCurrentFeedrate(), startUp, startUp);

			if(ss.plotMiddle)
			{
				//printer.setFeedrate(currentFeedrate);
				printer.moveTo(ss.p2.x(), ss.p2.y(), z, currentFeedrate, startUp, startUp);
			}

			//TODO: FIX ME!
			//printer.setSpeed(ss.speed(currentSpeed, printer.getExtruder().getAngleSpeedFactor()));
			
			//printer.setFeedrate(printer.getExtruder().getAngleFeedrate());
			printer.moveTo(ss.p3.x(), ss.p3.y(), z, printer.getExtruder().getAngleFeedrate(), startUp, endUp);
			//pos = ss.p3;
			// Leave speed set for the start of the next movement.
		} else
			printer.moveTo(first.x(), first.y(), z, currentFeedrate, startUp, endUp);
	}


	/**
	 * Plot a polygon
	 * @throws IOException
	 * @throws ReprapException
	 * @return
	 */
	private void plot(RrPolygon p, boolean firstOneInLayer) throws ReprapException, IOException
	{
		Attributes att = p.getAttributes();
		Printer printer = layerConditions.getPrinter();
		double outlineFeedrate = att.getExtruder().getOutlineFeedrate();
		double infillFeedrate = att.getExtruder().getInfillFeedrate();
		
		boolean acc = att.getExtruder().getMaxAcceleration() > 0; //Preferences.loadGlobalBool("Accelerating");
			
		//int leng = p.size();
	
		if(p.size() <= 1)
		{
			startNearHere = null;
			return;
		}

		// If the length of the plot is <0.05mm, don't bother with it.
		// This will not spot an attempt to plot 10,000 points in 1mm.
		double plotDist=0;
		Rr2Point lastPoint=p.point(0);
		for (int i=1; i<p.size(); i++)
		{
			Rr2Point n=p.point(i);
			plotDist+=Rr2Point.d(lastPoint, n);
			lastPoint=n;
		}
		if (plotDist<Preferences.machineResolution()*0.5) {
			Debug.d("Rejected line with "+p.size()+" points, length: "+plotDist);
			startNearHere = null;
			return;
		}	
		
		printer.selectExtruder(att);
		
// Don't do these with mid-point starting
		
//		if(p.isClosed() && att.getExtruder().incrementedStart())
//			p = p.incrementedStart(layerConditions);
//		else if(p.isClosed() && att.getExtruder().randomStart())
//			p = p.randomStart();
		
		int stopExtruding = p.size() + 10;
		int stopValve = stopExtruding;
		double extrudeBackLength = att.getExtruder().getExtrusionOverRun();
		double valveBackLength = att.getExtruder().getValveOverRun();
		if(extrudeBackLength >= valveBackLength)
		{
			if(extrudeBackLength > 0)
				stopExtruding = p.backStep(extrudeBackLength);
			if(valveBackLength > 0)
				stopValve = p.backStep(valveBackLength);
		} else
		{
			if(valveBackLength > 0)
				stopValve = p.backStep(valveBackLength);
			if(extrudeBackLength > 0)
				stopExtruding = p.backStep(extrudeBackLength);			
		}
		
		if (printer.isCancelled()) return;
		
		// If getMinLiftedZ() is negative, never lift the head
		
		Boolean lift = att.getExtruder().getMinLiftedZ() >= 0;
		
		if(acc)
			p.setSpeeds(att.getExtruder().getSlowXYFeedrate(), p.isClosed()?outlineFeedrate:infillFeedrate, 
					att.getExtruder().getMaxAcceleration());
		
		
		if(extrudeBackLength <= 0)
			stopExtruding = Integer.MAX_VALUE;
		else if(acc)
			stopExtruding = p.findBackPoint(extrudeBackLength);
		
		if(valveBackLength <= 0)
			stopValve = Integer.MAX_VALUE;
		else if(acc)
			stopValve = p.findBackPoint(valveBackLength);

		currentFeedrate = att.getExtruder().getFastXYFeedrate();
		singleMove(p.point(0));
		
		if(acc)
			currentFeedrate = p.speed(0);
		else
		{
			if(p.isClosed())
			{
				currentFeedrate = outlineFeedrate;			
			} else
			{
				currentFeedrate = infillFeedrate;			
			}
		}
		
		plot(p.point(0), p.point(1), false, false);
		
		// Print any lead-in.
		printer.printStartDelay(firstOneInLayer);
		
		boolean extrudeOff = false;
		boolean valveOff = false;
		boolean oldexoff;
		
//		if(p.isClosed())
//		{
//			for(int j = 1; j <= p.size(); j++)
//			{
//				int i = j%p.size();
//				Rr2Point next = p.point((j+1)%p.size());
//				
//				if (printer.isCancelled())
//				{
//					printer.stopMotor();
//					singleMove(posNow());
//					move(posNow(), posNow(), lift, true, true);
//					return;
//				}
//				if(acc)
//					currentFeedrate = p.speed(i);
//				
//				oldexoff = extrudeOff;
//				extrudeOff = j > stopExtruding || j == p.size();
//				valveOff = j > stopValve || j == p.size();
//				
//				plot(p.point(i), next, extrudeOff, valveOff);
//				if(oldexoff ^ extrudeOff)
//					printer.printEndReverse();
//			}
//		} else
		
//		{
			for(int i = 1; i < p.size(); i++)
			{
				Rr2Point next = p.point((i+1)%p.size());
				
				if (printer.isCancelled())
				{
					printer.stopMotor();
					singleMove(posNow());
					move(posNow(), posNow(), lift, lift, true);
					return;
				}
				
				if(acc)
					currentFeedrate = p.speed(i);
				
				oldexoff = extrudeOff;
				extrudeOff = i > stopExtruding || i == p.size()-1;
				valveOff = i > stopValve || i == p.size()-1;
//				if(oldexoff ^ extrudeOff)
//					printer.printEndReverse();				
				plot(p.point(i), next, extrudeOff, valveOff);
				if(oldexoff ^ extrudeOff)
					printer.printEndReverse();
			}
//		}
		
		if(p.isClosed())
			move(p.point(0), p.point(0), false, false, true);
			
		move(posNow(), posNow(), lift, lift, true);
		
		// The last point is near where we want to start next
		if(p.isClosed())
			startNearHere = p.point(0);	
		else
			startNearHere = p.point(p.size() - 1);
		
		if(simulationPlot != null)
		{
			RrPolygonList pgl = new RrPolygonList();
			pgl.add(p);
			simulationPlot.add(pgl);
		}
		
	}
	


	private int plotOneMaterial(RrPolygonList polygons, int i, boolean firstOneInLayer)
		throws ReprapException, IOException
	{
		String material = polygons.polygon(i).getAttributes().getMaterial();
		
		while(i < polygons.size() && polygons.polygon(i).getAttributes().getMaterial().equals(material))
		{
			if (layerConditions.getPrinter().isCancelled())
				return i;
			plot(polygons.polygon(i), firstOneInLayer);
			firstOneInLayer = false;
			i++;
		}
		return i;
	}
	
//	private boolean nextCommon(int ib, int ih)
//	{
//		if(borderPolygons == null || hatchedPolygons == null)
//			return false;
//		
//		commonBorder = ib;
//		commonHatch = ih;
//		
//		if(borderPolygons.size() <= 0)
//		{
//			commonHatch = hatchedPolygons.size();
//			return false;
//		}
//
//
//		if(hatchedPolygons.size() <= 0)
//		{
//			commonBorder = borderPolygons.size();
//			return false;
//		}
//
//		for(int jb = ib; jb < borderPolygons.size(); jb++)
//		{
//			for(int jh = ih; jh < hatchedPolygons.size(); jh++)
//			{
//				if(borderPolygons.polygon(ib).getAttributes().getMaterial().equals(
//						hatchedPolygons.polygon(ih).getAttributes().getMaterial()))
//				{
//					commonBorder = jb;
//					commonHatch = jh;
//					return true;
//				}
//			}
//		}
//		return false;
//	}
		
	/**
	 * Master plot function - draw everything.  Supress border and/or hatch by
	 * setting borderPolygons and/or hatchedPolygons null
	 * @throws IOException
	 * @throws ReprapException
	 */
	public void plot() throws ReprapException, IOException
	{
		int ib, jb, ih, jh;

		boolean firstOneInLayer = true;
		
		for(int i = 0; i < allPolygons.length; i++)
		{
			firstOneInLayer = true;
			RrPolygonList pl = allPolygons[i];
			for(int j = 0; j < pl.size(); j++)
			{
				plot(pl.polygon(j), firstOneInLayer);
				firstOneInLayer = false;
			}
		}

//		//borderPolygons = borderPolygons.filterShorts(Preferences.machineResolution()*2);
//		//hatchedPolygons = hatchedPolygons.filterShorts(Preferences.machineResolution()*2);
//
//		ib = 0;
//		ih = 0;
//		
//		Printer printer = layerConditions.getPrinter();	
//		
//		while(nextCommon(ib, ih)) 
//		{	
//			for(jb = ib; jb < commonBorder; jb++)
//			{
//				if (printer.isCancelled())
//					break;
//				plot(borderPolygons.polygon(jb), firstOneInLayer);
//				firstOneInLayer = false;
//			}
//			ib = commonBorder;
//
//			for(jh = ih; jh < commonHatch; jh++)
//			{
//				if (printer.isCancelled())
//					break;
//				plot(hatchedPolygons.polygon(jh), firstOneInLayer);
//				firstOneInLayer = false;
//			}
//			ih = commonHatch;
//
//			firstOneInLayer = true;
//			borderPolygons = borderPolygons.nearEnds(startNearHere);
//			
//			ib = plotOneMaterial(borderPolygons, ib, firstOneInLayer);
//			firstOneInLayer = false;
//			hatchedPolygons = hatchedPolygons.nearEnds(startNearHere);
//			ih = plotOneMaterial(hatchedPolygons, ih, firstOneInLayer);	
//		}
//
//		firstOneInLayer = true; // Not sure about this - AB
//
//		if(borderPolygons != null)
//		{
//			for(jb = ib; jb < borderPolygons.size(); jb++)
//			{
//				if (printer.isCancelled())
//					break;
//				plot(borderPolygons.polygon(jb), firstOneInLayer);
//				firstOneInLayer = false;
//			}
//		}
//
//		if(hatchedPolygons != null)
//		{
//			for(jh = ih; jh < hatchedPolygons.size(); jh++)
//			{
//				if (printer.isCancelled())
//					break;
//				plot(hatchedPolygons.polygon(jh), firstOneInLayer);
//				firstOneInLayer = false;
//			}
//		}
////		if(!shellSet)
////		{
////			printer.setLowerShell(lowerShell);
////			shellSet = true;
////		}
	}		
	
}