summaryrefslogtreecommitdiff
path: root/trunk/users/adrian/host/src/org/reprap/geometry/polygons/STLSlice.java
blob: ab5a1f746205a8ecc2a4e016aeba98b31229b2e0 (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
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
///*
// 
// RepRap
// ------
// 
// The Replicating Rapid Prototyper Project
// 
// 
// Copyright (C) 2005
// Adrian Bowyer & The University of Bath
// 
// http://reprap.org
// 
// Principal author:
// 
// Adrian Bowyer
// Department of Mechanical Engineering
// Faculty of Engineering and Design
// University of Bath
// Bath BA2 7AY
// U.K.
// 
// e-mail: A.Bowyer@bath.ac.uk
// 
// RepRap is free; you can redistribute it and/or
// modify it under the terms of the GNU Library General Public
// Licence as published by the Free Software Foundation; either
// version 2 of the Licence, or (at your option) any later version.
// 
// RepRap 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
// Library General Public Licence for more details.
// 
// For this purpose the words "software" and "library" in the GNU Library
// General Public Licence are taken to mean any and all computer programs
// computer files data results documents and other copyright information
// available from the RepRap project.
// 
// You should have received a copy of the GNU Library General Public
// Licence along with RepRap; if not, write to the Free
// Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA,
// or see
// 
// http://www.gnu.org/
// 
// =====================================================================
// 
// 
// STLSlice: deals with the slices through an STL object
// 
// */
//
//package org.reprap.geometry.polygons;
//
//import java.util.ArrayList;
//import java.util.Collections;
//import java.util.List;
//import java.util.Random;
//
//import javax.media.j3d.Appearance;
//import javax.media.j3d.BranchGroup;
//import javax.media.j3d.GeometryArray;
//import javax.media.j3d.Group;
//import javax.media.j3d.SceneGraphObject;
//import javax.media.j3d.Shape3D;
//import javax.media.j3d.Transform3D;
//import javax.vecmath.Point3d;
//import javax.vecmath.Tuple3d;
//
//import com.sun.j3d.utils.geometry.GeometryInfo;
//import com.sun.j3d.utils.geometry.NormalGenerator;
//import org.reprap.gui.STLObject;
//import org.reprap.utilities.Debug;
//import org.reprap.utilities.RrGraphics;
//import org.reprap.Attributes;
//import org.reprap.Extruder;
//import org.reprap.Preferences;
//import org.reprap.devices.GenericExtruder;
//
//// Small class to hold line segments and the quads in which their ends lie
//
//class LineSegment
//{	
//	/**
//	 * The ends of the line segment
//	 */
//	public Rr2Point a = null, b = null;
//	
//	/**
//	 * The attribute (i.e. RepRap material) of the segment.
//	 */
//	public Attributes att = null;
//	
//	/**
//	 * 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;
//		if(a != null)
//			a.destroy();
//		a = null;
//		if(b != null)
//			b.destroy();
//		b = null;
////		if(qa != null)
////			qa.destroyLayer();
////		qa = null;
////		if(qb != null)
////			qb.destroyLayer();
////		qb = null;
//		
//		// Don't destroy the attribute - it may be needed
//		
//		//if(att != null)
//		//	att.destroy();
//		att = null;
//		
//	}
//	
//	/**
//	 * Destroy just me
//	 */
//	protected void finalize() throws Throwable
//	{
//		a = null;
//		b = null;
////		qa = null;
////		qb = null;
//		att = null;
//		super.finalize();
//	}
//	
//	/**
//	 * Constructor takes two intersection points with an STL triangle edge.
//	 * @param p
//	 * @param q
//	 */
//	public LineSegment(Rr2Point p, Rr2Point q, Attributes at)
//	{
//		if(at == null)
//			System.err.println("LineSegment(): null attributes!");
//		a = p;
//		b = q;
//		att = at;
////		qa = null;
////		qb = null;
//	}
//	
//	public String toString()
//	{
//		return "edge ends: (" + a.toString() + ") to (" + b.toString() + ")"; 
//	}
//
////	/**
////	 * A quad contains (we hope...) the ends of two segments - record that
////	 * @param q
////	 */
////	public static void setQuad(STLSlice q)
////	{
////		if(q.edges().size() != 2)
////			Debug.d("LineSegment.setQuad(): dud edge count: " + q.edges().size());
////		
////		int count = 0;
////		
////		for(int i = 0; i < q.edges().size(); i++)
////		{
////			if(q.box().pointRelative(q.segmentA(i)) == 0)
////			{
////				q.segment(i).qa = q;
////				count++;
////			}
////			if(q.box().pointRelative(q.segmentB(i)) == 0)
////			{
////				q.segment(i).qb = q;
////				count++;
////			}
////		}
////		
////		//if(count != 2)
////			//System.err.println("LineSegment.setQuad(): dud end count = " + count);
////	}
//}
//
/////**
//// * Small holder for quads and points as they get visited
//// * @author ensab
//// *
//// */
////class trackPolygon
////{
////	public STLSlice nextQ = null;
////	public LineSegment nextE = null;
////	public Rr2Point here = null;
////	
////	/**
////	 * 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;
////		if(nextQ != null)
////			nextQ.destroyLayer();
////		nextQ = null;
////		if(nextE != null)
////			nextE.destroy();
////		nextE = null;
////		if(here != null)
////			here.destroy();
////		here = null;
////		
////	}
////	
////	/**
////	 * Destroy just me
////	 */
////	protected void finalize() throws Throwable
////	{
////		nextQ = null;
////		nextE = null;
////		here = null;
////		super.finalize();
////	}
////	
////	public trackPolygon()
////	{
////		nextQ = null;
////		nextE = null;
////		here = null;
////	}
////}
//
///**
// * Very small class to hold attributes (i.e. material made from) and transforms for
// * the objects made from them.
// * @author ensab
// *
// */
//class AandT
//{
//	public Attributes att = null;
//	public Transform3D trans = null;
//	
//	/**
//	 * 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;
//		
//		// Both the attribute and transform may be needed again
//		
//		if(att != null)
//		//	att.destroy();
//		att = null;
//		//if(trans != null)
//		//	trans.destroy();
//		trans = null;
//	}
//	
//	/**
//	 * Destroy just me
//	 */
//	protected void finalize() throws Throwable
//	{
//		att = null;
//		trans = null;
//		super.finalize();
//	}
//	
//	public AandT(Attributes a, Transform3D t)
//	{
//		att = a;
//		trans = t;
//	}
//}
//
///**
// * list of materials and transforms of the objects made from them.
// * @author ensab
// *
// */
//class MaterialLists
//{
//	private ArrayList<AandT> ats[] = null;
//	int extruderCount;
//	
//	/**
//	 * 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;
//		if(ats != null)
//		{
//			for(int i = 0; i < ats.length; i++)
//			{
//				for(int j = 0; j < ats[i].size(); j++)
//				{
//					ats[i].get(j).destroy();
//					ats[i].set(j, null);
//				}
//				ats[i] = null;
//			}
//			
//		}
//		ats = null;
//	}
//		
//	/**
//	 * Destroy just me
//	 */
//	protected void finalize() throws Throwable
//	{
//		ats = null;
//		super.finalize();
//	}	
//	
//	@SuppressWarnings("unchecked")
//	public MaterialLists()
//	{
//		extruderCount = 0;
//		
//		try
//		{
//			extruderCount = Preferences.loadGlobalInt("NumberOfExtruders");
//		} catch (Exception ex)
//		{
//			System.err.println("MaterialLists(): " + ex.toString());
//		}
//		
//		ats = new ArrayList[extruderCount]; // Javanonsense: Why can't this be ats = new ArrayList<AandT>[extruderCount]; then?
//		for(int i = 0; i < extruderCount; i++)
//			ats[i] = new ArrayList<AandT>();
//	}
//	
//	public void add(Attributes a, Transform3D t)
//	{
//		int i = GenericExtruder.getNumberFromMaterial(a.getMaterial());
//		if(i < 0 || i >= extruderCount)
//			System.err.println("MaterialLists.add() - dud material: " + a.getMaterial());
//		else
//			ats[i].add(new AandT(a, t));
//	}
//	
//	public ArrayList<AandT> getAandTs(int i)
//	{
//		return ats[i];
//	}
//	
//	public int getExtruderCount() { return extruderCount; }
//}
//
///**
// * Class to hold all the STL objects in the scene and to compute slices through them.
// * The slices are computed by dividing a rectangle (box) in a quad tree down to the point
// * where each quad contains two ends of different line segments.  These ends are then assumed
// * to join together.
// */
//public class STLSlice 
//{
//	/**
//	 * For debugging
//	 */
//	private static RrGraphics picture = null;
//	
//	/**
//	 * Used to make unbiased but arbitrary decisions
//	 */
//	private static Random rangen = new Random(739127);
//	
//	/**
//	 * The STL objects in 3D
//	 */
//	private List<STLObject> shapeList = null;
//	
//	/**
//	 * List of the edges with points in this quad
//	 */
//	private List<LineSegment> edges = null;
//	
//	/**
//	 * Its enclosing box
//	 */
//	private RrRectangle box = null;
//	
//	/**
//	 * Quad tree division - NW, NE, SE, SW
//	 */
//	//private STLSlice q1 = null, q2 = null, q3 = null, q4 = null;
//	
//	/**
//	 * Lists of the x coordinates of the segment endpoints in this quad
//	 */
//	private ArrayList<Double> xCoords = null;
//	
//	/**
//	 * Lists of the y coordinates of the segment endpoints in this quad
//	 */	
//	private ArrayList<Double> yCoords = null;
//	
//	/**
//	 * Squared diagonal of the smallest box to go to 
//	 */
//	private static double resolution_2 = Preferences.tiny();
//	
//	/**
//	 * Swell factor for division
//	 */
//	private static double sFactor = 1;
//	
//	/**
//	 * All the STL triangles and part-triangles below slice-height, Z 
//	 */
//	private List<Point3d> triangles = null;
//	
//	/**
//	 * Made from the below-Z triangles 
//	 */
//	private BranchGroup below = null;
//	
//	/**
//	 * The lists of parts sorted by material
//	 */
//	private MaterialLists materialList = null;
//	/**
//	 * For debugging
//	 */
//	RrGraphics qp = null;
//	
//	/**
//	 * Do we need the lower shell for the simulation window?
//	 */
//	private boolean generateLowerTriangles = true;
//	
//	/**
//	 * Flag to prevent cyclic graphs going round forever
//	 */
//	private boolean beingDestroyed = false;
//	
//	/**
//	 * Destroy me and (almost) all that I point to,
//	 * setting things up for the next layer
//	 */
//	public void destroyLayer() 
//	{
//		if(beingDestroyed) // Prevent infinite loop
//			return;
//		beingDestroyed = true;
//		
//		// Don't destroy the shapeList - we need it for the next layer
//		
////		if(shapeList != null)
////		{
////			for(int i = 0; i < shapeList.size(); i++)
////			{
////				shapeList.get(i).destroy();
////				shapeList.set(i, null);
////			}
////		}
////		shapeList = null;
//		
//		if(edges != null)
//		{
//			for(int i = 0; i < edges.size(); i++)
//			{
//				edges.get(i).destroy();
//				edges.set(i, null);
//			}
//		}
//		edges = null;
//		if(box != null)
//			box.destroy();
//		box = null;
////		if(q1 != null)
////			q1.destroyLayer();
////		q1 = null;
////		if(q2 != null)
////			q2.destroyLayer();
////		q2 = null;
////		if(q3 != null)
////			q3.destroyLayer();
////		q3 = null;
////		if(q4 != null)
////			q4.destroyLayer();
////		q4 = null;
//		xCoords = null;
//		yCoords = null;
//		triangles = null;
//		
//		// We need below
//		//below = null;
//		
//		// We need the material lists next time
//		
//		//if(materialList != null)
//		//	materialList.destroy();
//		//materialList = null;
//		
//		qp = null;		
//		
//		edges = new ArrayList<LineSegment>();
//		xCoords = new ArrayList<Double>();
//		yCoords = new ArrayList<Double>();
//		box = new RrRectangle();
//		triangles = new ArrayList<Point3d>();
//		beingDestroyed = false;
//	}
//	
//	/**
//	 * Destroy just me
//	 */
//	protected void finalize() throws Throwable
//	{
//		shapeList = null;
//		edges = null;
//		box = null;
////		q1 = null;
////		q2 = null;
////		q3 = null;
////		q4 = null;
//		xCoords = null;
//		yCoords = null;
//		triangles = null;
//		below = null;
//		materialList = null;
//		qp = null;				
//		super.finalize();
//	}	
//	
//	/**
//	 * Initialises a few things
//	 */
//	private void setUp()
//	{
//		edges = new ArrayList<LineSegment>();
////		q1 = null;
////		q2 = null;
////		q3 = null;
////		q4 = null;
//		xCoords = new ArrayList<Double>();
//		yCoords = new ArrayList<Double>();
//		box = new RrRectangle();
//		triangles = new ArrayList<Point3d>();
//	}
//	
//	/**
//	 * This constructor records the list of STL objects. 
//	 * @param s
//	 */
//	public STLSlice(List<STLObject> s)
//	{
//		setUp();
//		shapeList = s;
//		materialList = new MaterialLists();
//		try
//		{
//			generateLowerTriangles = false; //Preferences.loadGlobalBool("DisplaySimulation");
//		} catch (Exception e)
//		{}
//		
//		// For each object, record its material and transform
//		
//		for(int i = 0; i < shapeList.size(); i++)
//		{
//			STLObject stl = shapeList.get(i);
//			Transform3D trans = stl.getTransform();
//			//for(int c = 0; c < stl.numChildren(); c++)
//			//{
//				BranchGroup bg = stl.getSTL();
//				java.util.Enumeration<?> enumKids = bg.getAllChildren();
//
//				while(enumKids.hasMoreElements())
//				{
//					Object ob = enumKids.nextElement();
//
//					if(ob instanceof BranchGroup)
//					{
//						BranchGroup bg1 = (BranchGroup)ob;
//						Attributes att = (Attributes)(bg1.getUserData());
//						materialList.add(att, trans);
//					}
//				}
//			//}
//		}
//	}
//	
////	/**
////	 * This quad is a leaf if it has no children; just check the first.
////	 * @return
////	 */
////	public boolean leaf()
////	{
////		return q1 == null;
////	}
//	
//	/**
//	 * Add a new line segment to the list.  Also add its end coordinates to
//	 * the lists of those.
//	 * @param p
//	 * @param q
//	 */
//	public void add(Rr2Point p, Rr2Point q, Attributes att)
//	{
//		xCoords.add(new Double(p.x()));
//		xCoords.add(new Double(q.x()));
//		yCoords.add(new Double(p.y()));
//		yCoords.add(new Double(q.y()));
//		
//		edges.add(new LineSegment(p, q, att));
//	}
//	
//	/**
//	 * Return the box
//	 * @return 
//	 */
//	public RrRectangle box()
//	{
//		return box;
//	}
//	
//
////	/**
////	 * Run through a Shape3D and find its enclosing XY box
////	 * @param shape
////	 * @param trans
////	 * @param z
////	 */
////	private RrRectangle BBoxPoints(Shape3D shape, Transform3D trans)
////    {
////		RrRectangle r = null;
////        GeometryArray g = (GeometryArray)shape.getGeometry();
////        Point3d p1 = new Point3d();
////        Point3d q1 = new Point3d();
////        
////        if(g != null)
////        {
////            for(int i = 0; i < g.getVertexCount(); i++) 
////            {
////                g.getCoordinate(i, p1);
////                trans.transform(p1, q1);
////                if(r == null)
////                	r = new RrRectangle(new RrInterval(q1.x, q1.x), new RrInterval(q1.y, q1.y));
////                else
////                	r.expand(new Rr2Point(q1.x, q1.y));
////            }
////        }
////        return r;
////    }
////	
////	/**
////	 * Unpack the Shape3D(s) from value and find their exclosing XY box
////	 * @param value
////	 * @param trans
////	 * @param z
////	 */
////	private RrRectangle BBox(Object value, Transform3D trans) 
////    {
////		RrRectangle r = null;
////		RrRectangle s;
////		
////        if(value instanceof SceneGraphObject) 
////        {
////            SceneGraphObject sg = (SceneGraphObject)value;
////            if(sg instanceof Group) 
////            {
////                Group g = (Group)sg;
////                java.util.Enumeration<?> enumKids = g.getAllChildren( );
////                while(enumKids.hasMoreElements())
////                {
////                	if(r == null)
////                		r = BBox(enumKids.nextElement(), trans);
////                	else
////                	{
////                		s = BBox(enumKids.nextElement(), trans);
////                		if(s != null)
////                			r = RrRectangle.union(r, s);
////                	}
////                }
////            } else if (sg instanceof Shape3D) 
////            {
////                r = BBoxPoints((Shape3D)sg, trans);
////            }
////        }
////        
////        return r;
////    }
////	
////	/**
////	 * Find the minimum XY enclosing box round the corners of the object
////	 * @return
////	 */
////	public RrRectangle ObjectPlanRectangle()
////	{
////		RrRectangle r = null;
////		RrRectangle s;
////		
////		for(int mat = 0; mat < materialList.getExtruderCount(); mat++)
////		{
////			ArrayList<AandT> aats = materialList.getAandTs(mat);
////
////			if(aats.size() > 0)
////			{
////				for(int obj = 0; obj < aats.size(); obj++)
////				{
////					AandT aat = aats.get(obj);
////					Transform3D trans = aat.trans;
////					Attributes attr = aat.att;
////					if(r == null)
////						r = BBox(attr.getPart(), trans);
////					else
////					{
////						s = BBox(attr.getPart(), trans);
////						if(s != null)
////						r = RrRectangle.union(r, s);
////					}
////				}
////			}
////		}
////		
////		return r;
////	}
////		//BoundingBox r = null;
////		
////		for(int mat = 0; mat < materialList.getExtruderCount(); mat++)
////		{
////			ArrayList<AandT> aats = materialList.getAandTs(mat);
////
////			if(aats.size() > 0)
////			{
////				for(int obj = 0; obj < aats.size(); obj++)
////				{
////					AandT aat = aats.get(obj);
////					Transform3D trans = aat.trans;
////					Attributes attr = aat.att;
////					BranchGroup bg = attr.getPart();
////					Bounds bd = bg.getBounds();
////					bd.transform(trans);
////					BoundingBox bx = new BoundingBox(bd);
////					if(r == null)
////						r = bx;
////					else
////						r.combine(bx);
////				}
////			}
////		}
////		Point3d p1 = new Point3d();
////		r.getLower(p1); 
////		Point3d p2 = new Point3d();
////		r.getUpper(p2);
////		return new RrRectangle(new Rr2Point(p1.x, p1.y), new Rr2Point(p2.x, p2.y));
////	}
//
//	
//	/**
//	 * @return the edges of the STL slice
//	 */
//	public List<LineSegment> edges()
//	{
//		return edges;
//	}
//	
////	/**
////	 * get the quad children
////	 * @return
////	 */
////	public STLSlice c_1()
////	{
////		return q1;
////	}
////	public STLSlice c_2()
////	{
////		return q2;
////	}
////	public STLSlice c_3()
////	{
////		return q3;
////	}
////	public STLSlice c_4()
////	{
////		return q4;
////	}
//	
//	/**
//	 * @param i index of line segement
//	 * @return Linesegment object of the STL slice at index i
//	 */
//	protected LineSegment segment(int i)
//	{
//		return edges.get(i);
//	}
//	
//	private RrPolygon getNextPolygon()
//	{
//		if(edges.size() <= 0)
//			return null;
//		LineSegment next = segment(0);
//		edges.remove(0);
//		RrPolygon result = new RrPolygon(next.att, true);
//		result.add(next.a);
//		result.add(next.b);
//		Rr2Point start = next.a;
//		Rr2Point end = next.b;
//		
//		boolean first = true;
//		while(edges.size() > 0)
//		{
//			double d2 = Rr2Point.dSquared(start, end);
//			if(first)
//				d2 = Math.max(d2, 1);
//			first = false;
//			boolean aEnd = false;
//			int index = -1;
//			for(int i = 0; i < edges.size(); i++)
//			{
//				double dd = Rr2Point.dSquared(segment(i).a, end);
//				if(dd < d2)
//				{
//					d2 = dd;
//					aEnd = true;
//					index = i;
//				}
//				dd = Rr2Point.dSquared(segment(i).b, end);
//				if(dd < d2)
//				{
//					d2 = dd;
//					aEnd = false;
//					index = i;
//				}
//			}
//
//			if(index >= 0)
//			{
//				next = edges.get(index);
//				edges.remove(index);
//				int ipt = result.size() - 1;
//				if(aEnd)
//				{
//					result.set(ipt, Rr2Point.mul(Rr2Point.add(next.a, result.point(ipt)), 0.5));
//					result.add(next.b);
//					end = next.b;
//				} else
//				{
//					result.set(ipt, Rr2Point.mul(Rr2Point.add(next.b, result.point(ipt)), 0.5));
//					result.add(next.a);
//					end = next.a;				
//				}
//			} else
//				return result;
//		}
//		
//		Debug.d("STLSlice.getNextPolygon(): exhausted edge list!");
//		
//		return result;
//	}
//	
//	private RrPolygonList simpleCull()
//	{
//		RrPolygonList result = new RrPolygonList();
//		RrPolygon next = getNextPolygon();
//		while(next != null)
//		{
//			if(next.size() >= 3)
//				result.add(next);
//			next = getNextPolygon();
//		}
//		
//		return result;
//	}
//	
//	public Rr2Point segmentA(int i)
//	{
//		return edges.get(i).a;
//	}
//	
//	public Rr2Point segmentB(int i)
//	{
//		return edges.get(i).b;
//	}
//	
////	/**
////	 * Get the triangulation below the current slice level.
////	 * @return
////	 */
////	public BranchGroup getBelow()
////	{
////		if(generateLowerTriangles)
////			return below;
////		else
////			return null;
////	}
//	
//	/**
//	 * FIXME: Not sure about this - at the moment it clicks all points
//	 * onto an 0.01 mm grid.
//	 * @param x
//	 * @return grid value nearest x
//	 */
////	private double toGrid(double x)
////	{
////		return x;
////		//return (double)((int)(x*Preferences.grid() + 0.5))*Preferences.gridRes();
////	}
//	
//	/**
//	 * Add the edge where the plane z cuts the triangle (p, q, r) (if it does).
//	 * Also update the triangulation of the object below the current slice used
//	 * for the simulation window.
//	 * @param p
//	 * @param q
//	 * @param r
//	 * @param z
//	 */
//	private void addEdge(Point3d p, Point3d q, Point3d r, double z, Attributes att)
//	{
//		Point3d odd = null, even1 = null, even2 = null;
//		int pat = 0;
//		boolean twoBelow = false;
//		
//		if(p.z < z)
//			pat = pat | 1;
//		if(q.z < z)
//			pat = pat | 2;
//		if(r.z < z)
//			pat = pat | 4;
//		
//		switch(pat)
//		{
//		// All above
//		case 0:
//			return;
//			
//		// All below
//		case 7:
//			if(generateLowerTriangles)
//			{
//				triangles.add(new Point3d(p));
//				triangles.add(new Point3d(q));
//				triangles.add(new Point3d(r));
//			}
//			return;
//			
//		// q, r below, p above	
//		case 6:
//			twoBelow = true;
//		// p below, q, r above
//		case 1:
//			odd = p;
//			even1 = q;
//			even2 = r;
//			break;
//			
//		// p, r below, q above	
//		case 5:
//			twoBelow = true;
//		// q below, p, r above	
//		case 2:
//			odd = q;
//			even1 = r;
//			even2 = p;
//			break;
//
//		// p, q below, r above	
//		case 3:
//			twoBelow = true;
//		// r below, p, q above	
//		case 4:
//			odd = r;
//			even1 = p;
//			even2 = q;
//			break;
//			
//		default:
//			System.err.println("addEdge(): the | function doesn't seem to work...");
//		}
//		
//		// Work out the intersection line segment (e1 -> e2) between the z plane and the triangle
//		
//		even1.sub((Tuple3d)odd);
//		even2.sub((Tuple3d)odd);
//		double t = (z - odd.z)/even1.z;	
//		Rr2Point e1 = new Rr2Point(odd.x + t*even1.x, odd.y + t*even1.y);	
//		Point3d e3_1 = new Point3d(e1.x(), e1.y(), z);
//		//e1 = new Rr2Point(toGrid(e1.x()), toGrid(e1.y()));
//		e1 = new Rr2Point(e1.x(), e1.y());
//		t = (z - odd.z)/even2.z;
//		Rr2Point e2 = new Rr2Point(odd.x + t*even2.x, odd.y + t*even2.y);
//		Point3d e3_2 = new Point3d(e2.x(), e2.y(), z);
//		//e2 = new Rr2Point(toGrid(e2.x()), toGrid(e2.y()));
//		e2 = new Rr2Point(e2.x(), e2.y());
//		
//		// Too short?
//		if(!Rr2Point.same(e1, e2, Preferences.lessGridSquare()))
//		{
//			add(e1, e2, att);
//			box.expand(e1);
//			box.expand(e2);
//		}
//		
//		// Sort out the bits of triangle to add to the shape under the z plane
//		
//		if(twoBelow)
//		{
//			even1.add((Tuple3d)odd);
//			even2.add((Tuple3d)odd);
//			if(generateLowerTriangles)
//			{
//				triangles.add(new Point3d(even1));
//				triangles.add(new Point3d(even2));
//				triangles.add(new Point3d(e3_1));
//				triangles.add(new Point3d(e3_2));
//				triangles.add(new Point3d(e3_1));
//				triangles.add(new Point3d(even2));
//			}
//		} else if(generateLowerTriangles)
//		{
//			triangles.add(new Point3d(odd));
//			triangles.add(new Point3d(e3_1));
//			triangles.add(new Point3d(e3_2));
//		}
//	}
//	
//
//	
//	/**
//	 * Run through a Shape3D and set edges from it at plane z
//	 * Apply the transform first
//	 * @param shape
//	 * @param trans
//	 * @param z
//	 */
//	private void addAllEdges(Shape3D shape, Transform3D trans, double z, Attributes att)
//    {
//        GeometryArray g = (GeometryArray)shape.getGeometry();
//        Point3d p1 = new Point3d();
//        Point3d p2 = new Point3d();
//        Point3d p3 = new Point3d();
//        Point3d q1 = new Point3d();
//        Point3d q2 = new Point3d();
//        Point3d q3 = new Point3d();
//        
//        if(g.getVertexCount()%3 != 0)
//        {
//        	System.err.println("addAllEdges(): shape3D with vertices not a multiple of 3!");
//        }
//        if(g != null)
//        {
//            for(int i = 0; i < g.getVertexCount(); i+=3) 
//            {
//                g.getCoordinate(i, p1);
//                g.getCoordinate(i+1, p2);
//                g.getCoordinate(i+2, p3);
//                trans.transform(p1, q1);
//                trans.transform(p2, q2);
//                trans.transform(p3, q3);
//                addEdge(q1, q2, q3, z, att);
//            }
//        }
//    }
//	
//	/**
//	 * Unpack the Shape3D(s) from value and set edges from them
//	 * @param value
//	 * @param trans
//	 * @param z
//	 */
//	private void recursiveSetEdges(Object value, Transform3D trans, double z, Attributes att) 
//    {
//        if(value instanceof SceneGraphObject) 
//        {
//            SceneGraphObject sg = (SceneGraphObject)value;
//            if(sg instanceof Group) 
//            {
//                Group g = (Group)sg;
//                java.util.Enumeration<?> enumKids = g.getAllChildren( );
//                while(enumKids.hasMoreElements())
//                    recursiveSetEdges(enumKids.nextElement(), trans, z, att);
//            } else if (sg instanceof Shape3D) 
//            {
//                addAllEdges((Shape3D)sg, trans, z, att);
//            }
//        }
//    }
//	
////	/**
////	 * Constructor for building a branch quad in the division
////	 * @param pgl
////	 * @param b
////	 * @param res
////	 * @param fac
////	 */
////	private STLSlice(List<LineSegment> pgl, RrRectangle b)
////	{
////		edges = pgl;
////		box = b;
////		q1 = null;
////		q2 = null;
////		q3 = null;
////		q4 = null;
////		xCoords = new ArrayList<Double>();
////		yCoords = new ArrayList<Double>();
////	}
//	
////	/**
////	 * Prune the edge list to the box so that only segments
////	 * with endpoints in the box are retained.  
////	 */
////	private void prune()
////	{
////		List<LineSegment> result = new ArrayList<LineSegment>();
////		
////		for(int i = 0; i < edges.size(); i++)
////		{
////			Rr2Point aa = segment(i).a;
////			Rr2Point bb = segment(i).b;
////			
////			boolean aIn = (box.pointRelative(aa) == 0);
////			boolean bIn = (box.pointRelative(bb) == 0);
////			
////			if(aIn || bIn)
////				result.add(segment(i));
////
////			if(aIn)
////			{
////				xCoords.add(new Double(aa.x()));
////				yCoords.add(new Double(aa.y()));
////			}
////			
////			if(bIn)
////			{
////				xCoords.add(new Double(bb.x()));
////				yCoords.add(new Double(bb.y()));
////			}
////		}
////				
////		edges = result;
////	}
////	
////	/**
////	 * Start near the middle of a sorted list of coordinates, working both
////	 * up and down and find the first decent-sized gap
////	 * @param coords
////	 * @param i
////	 * @return
////	 */
////	private static double findGap(ArrayList<Double> coords, RrInterval i)
////	{
////		double g, v;
////		
////		Collections.sort(coords);
////		
////		int middle = coords.size()/2;
////		double vpOld = coords.get(middle).doubleValue();
////		double vmOld = vpOld;
////		int p = middle + 1;
////		int m = middle - 1;
////		RrInterval biggest = new RrInterval(0, 0);
////		
////		while(p < coords.size() || m  >= 0)
////		{
////			if(p < coords.size())
////			{
////				v = coords.get(p).doubleValue();
////				g = v - vpOld;
////				if(g > Preferences.gridRes()*0.1)
////				{
////					return 0.5*(vpOld + v);
////				}
////				if(g > biggest.length())
////					biggest = new RrInterval(vpOld, v);
////				vpOld = v;
////				p++;
////			}
////			
////			if(m >= 0)
////			{
////				v = coords.get(m).doubleValue();
////				g = vmOld - v;
////				if(g > Preferences.gridRes()*0.1)
////				{
////					return 0.5*(v + vmOld);
////				}
////				if(g > biggest.length())
////					biggest = new RrInterval(v, vmOld);
////				vmOld = v;
////				m--;
////			}
////		}
////		
////		if(biggest.length() > Preferences.gridRes()*0.1)
////			return biggest.low() + 0.5*biggest.length();
////		else
////		{
////			do
////				v = coords.get(0) + (rangen.nextDouble() - 0.5)*i.length();
////			while(!i.in(v));
////			return v;
////		}
////	}
//	
////	/**
////	 * Find the place to put the quad division.
////	 * @return
////	 */
////	private Rr2Point biggestGap()
////	{
////		Rr2Point result = new Rr2Point(findGap(xCoords, box.x()), findGap(yCoords, box.y()));
////		//System.out.println("Box: " + box.toString() + "   centre: " + result.toString());
////		
////		// Not needed any more
////		
////		//xCoords = new ArrayList<Double>();
////		//yCoords = new ArrayList<Double>();
////		
////		// Sanity check
////		
////		if(box.pointRelative(result) != 0)
////			Debug.d("STLSlice.biggestGap(): point outside box! point: " + 
////					result.toString() + ", box: " + box.toString());
////		
////		return result;		
////	}
//	
////	/**
////	 * Quad tree division - make the 4 sub quads.
////	 */
////	private void makeQuads()
////	{
//////		 Set up the quad-tree division
////		
////		Rr2Point sw = box.sw();
////		Rr2Point nw = box.nw();
////		Rr2Point ne = box.ne();
////		Rr2Point se = box.se();
////		
////		Rr2Point cen = biggestGap();
////		double w = cen.x() - (cen.x() - sw.x())*(sFactor - 1);
////		double n = cen.y() + (nw.y() - cen.y())*(sFactor - 1);
////		double e = cen.x() + (se.x() - cen.x())*(sFactor - 1);
////		double s = cen.y() - (cen.y() - sw.y())*(sFactor - 1);
////		
//////		 Put the results in the children
////		
////		RrRectangle b = new RrRectangle(nw, new Rr2Point(e, s));
////		q1 = new STLSlice(edges, b);
////		q1.prune();
////		
////		b = new RrRectangle(ne, new Rr2Point(w, s));
////		q2 = new STLSlice(edges, b);
////		q2.prune();
////		
////		b = new RrRectangle(se, new Rr2Point(w, n));
////		q3 = new STLSlice(edges, b);
////		q3.prune();
////		
////		b = new RrRectangle(sw, new Rr2Point(e, n));
////		q4 = new STLSlice(edges, b);
////		q4.prune();
////	}
////	
////
////	/**
////	 * Quad tree division to end up with two (or no) ends in each box.
////	 */
////	public void divide()
////	{
////		if(edges.size() <= 0)
////			return;
////		
////		if(box.dSquared() < resolution_2)
////		{
////			Debug.d("STLSlice.divide(): hit resolution limit! Edge end count: " + edges.size());
////			for(int i = 0; i < edges.size(); i++)
////				Debug.d(edges.get(i).toString());
////			LineSegment.setQuad(this);
////			return;
////		}
////		
////		if(edges.size() > 2)
////		{
////			makeQuads();
////			q1.divide();
////			q2.divide();
////			q3.divide();
////			q4.divide();
////			return;
////		}
////
////		for(int i = 0; i < edges.size(); i++)
////		{
////			if(box.pointRelative(segment(i).a) == 0 &&  
////					box.pointRelative(segment(i).b) == 0)
////			{
////				makeQuads();
////				q1.divide();
////				q2.divide();
////				q3.divide();
////				q4.divide();
////				return;
////			}		
////		}
////
////		if(edges.size() == 1)
////		{
////			Debug.d("STLSlice.divide(): only one end in box: " + edges.get(0).toString() + " box: " + box.toString());
////			edges.remove(0);
////		} else
////			LineSegment.setQuad(this);
////
////	}
//	
////	 /**
////	 * Recursively walk the tree to find an unvisited corner in a quad
////     */
////    private STLSlice findCorner()
////    {	
////    	STLSlice result = null;
////    	
////    	if(!leaf())
////    	{
////    		result = q1.findCorner();
////    		if(result != null)
////    			return result;
////    		result = q2.findCorner();
////    		if(result != null)
////    			return result;
////    		result = q3.findCorner();
////    		if(result != null)
////    			return result;
////    		result = q4.findCorner();
////    		return result;
////    	}
////
////    	if(edges.size() > 0)
////    		return this;
////    	else
////    		return null;
////    }
////
//////    /**
//////     * Useful for debugging - plot a bit of the quad tree.
//////     */
//////    private static void quickPlot(STLSlice s)
//////    {
//////    	s.qp = new RrGraphics(s);
//////    }
////    
////    public void recursiveReport()
////    {
////    	if(leaf())
////    	{
////    		System.out.println("Leaf box: " + box.toString() + " contains -");
////        	for(int i = 0; i < edges.size(); i++)
////        		System.out.println("   " + segment(i).toString());
////    	} else
////    	{
////    		q1.recursiveReport();
////    		q2.recursiveReport();
////    		q3.recursiveReport();
////    		q4.recursiveReport();
////    	}
////    }
//    
////    /**
////     * Useful for debugging - print statistics
////     * TODO: rewrite so log4j can be used
////     */
////    public void reportStats()
////    {
////    	int single = 0;
////    	int twin = 0;
////    	for(int i = 0; i < edges.size(); i++)
////    	{
////    		LineSegment s = segment(i);
////    		if(s.qa == null)
////    			single++;
////    		if(s.qb == null)
////    			single++;
////    		if(s.qa != null && s.qb != null)
////    			twin++;
////    	}
////    	System.out.println("STLSlice.reportStats() - lines: " + edges.size()
////    			+ " double-ended quads: " + twin +
////    			" single ends: " + single);
////    }
//    
//
////    /**
////     * We are working our way round a polygon.  This function takes the
////     * edge we're running along from the quad we're in and finds the 
////     * next quad and next edge at its other end.
////     */
////    private trackPolygon processThisQuad(LineSegment edge)
////    {
////    	trackPolygon result = new trackPolygon();
////    	
////    	if(edges.size() <= 0)
////    		return result;
////    	
////    	boolean aEnd;
////    	if(edge.qa == this)
////    		aEnd = true;
////    	else
////    		aEnd = false;
////    	
////    	boolean dud = true;
////    	for(int i = 0; i < edges.size(); i++)
////    	{
////    		if(edge == segment(i))
////    		{
////    			edges.remove(i);
////    			dud = false;
////    			break;
////    		}
////    	}
////    	
////    	if(dud)
////    		Debug.d("processThisQuad(): edge not found!");
////    	
////    	if(edges.size() <= 0)
////    	{
////    		Debug.d("processThisQuad(): entered quad with no exit!");
////    		return result;
////    	}
////    	
////    	result.nextE = segment(0);
////    	edges.remove(0);
////		
////		Rr2Point p0, p1;
////		
////		if(aEnd)
////			p0 = edge.a;
////		else
////			p0 = edge.b;
////		
////    	if(result.nextE.qa == this)
////    		aEnd = true;
////    	else
////    		aEnd = false;
////    	
////		if(aEnd)
////			p1 = result.nextE.a;
////		else
////			p1 = result.nextE.b;
////		
////		result.here = Rr2Point.mul(0.5, Rr2Point.add(p0, p1));
////		
////		if(aEnd)
////			result.nextQ = result.nextE.qb;
////		else
////			result.nextQ = result.nextE.qa;
////    	
////		return result;
////    }
//	
////	/**
////	 * Stitch up the line segment ends in the quad tree.
////	 * @param fg
////	 * @param fs
////	 * @return a list of all the resulting polygons.
////	 */
////	private static RrPolygonList conquer(STLSlice root) //, int fg, int fs)
////	{
////		RrPolygonList pgl = new RrPolygonList();
////		
////		STLSlice corner, startCorner;
////		LineSegment edge;
////		startCorner = root.findCorner();
////		
////		while(startCorner != null)
////		{
////			corner = startCorner;
////			edge = corner.segment(0);
////			RrPolygon pg = new RrPolygon(edge.att, true);
////			do
////			{
////				trackPolygon tp = corner.processThisQuad(edge);
////				if(tp.here != null)
////					pg.add(tp.here);
////				corner = tp.nextQ;
////				edge = tp.nextE;
////			} while (corner != startCorner && corner != null);
////			
////			if(pg.size() > 2)  // Throw away "noise"...
////			{
////				//pg.flag(pg.size() - 1, fs);
////				pgl.add(pg);
////			}
////
////			startCorner = root.findCorner();
////		}
////		return pgl;
////	}
//    
//
////	/**
////	 * Find the maximum height of the object(s) to be built
////	 * @return that height
////	 */
////	public double maxZ()
////	{
////		STLObject stl;
////		double result = Double.NEGATIVE_INFINITY;
////		
////		for(int i = 0; i < shapeList.size(); i++)
////		{
////			stl = shapeList.get(i);
////			if(stl.size().z > result)
////				result = stl.size().z;
////		}
////		return result;
////	}
//	
//	
//	/**
//	 * build a 2D polygon list of all edges in the plane z
//	 * from all the objects in shapeList then turn it in CSG form.
//	 * @param z
//	 * @return a CSG representation of all the polygons in the slice
//	 */
//	public BooleanGridList slice(double z, Extruder es[])
//	{
//		BooleanGridList rl = new BooleanGridList();
//		RrCSG csgp = null;
//		RrPolygonList pgl = new RrPolygonList();
//		
//		if(generateLowerTriangles)
//			below = new BranchGroup();
//		else
//			below = null;
//
//		for(int mat = 0; mat < materialList.getExtruderCount(); mat++)
//		{
//			//destroyLayer();
//			ArrayList<AandT> aats = materialList.getAandTs(mat);
//
//			if(aats.size() > 0)
//			{
//				//Appearance ap = aats.get(0).att.getAppearance();
//				
//				for(int obj = 0; obj < aats.size(); obj++)
//				{
//					destroyLayer();
//					AandT aat = aats.get(obj);
//					Transform3D trans = aat.trans;
//					Attributes attr = aat.att;
//					recursiveSetEdges(attr.getPart(), trans, z, attr);
//
//
//					// Make sure nothing falls down the cracks.
//
//					sFactor = Preferences.swell();
//					box = box.scale(sFactor);
//					resolution_2 = box.dSquared()*Preferences.tiny();
//
//					// Recursively generate the quad tree.  The aim is to have each
//					// leaf quad containing either 0 or 2 ends of different line
//					// segments.  Then we just run round joining up all the pairs of
//					// ends.
//
//					//divide();
//
//					// Run round joining up all the pairs of ends...
//
//					//RrPolygonList pgl = conquer(this); 
//					
//					pgl = simpleCull();
//					
//					// Remove wrinkles
//
//					pgl = pgl.simplify(Preferences.gridRes()*1.5);
//
//					// Fix small radii
//
//					pgl = pgl.arcCompensate();
//
//					// Check for a silly result.
//
//					if(pgl.size() > 0)
//					{
//						csgp = pgl.toCSG(Preferences.tiny());
//						rl.add(new BooleanGrid(csgp, box, attr));
////						if(picture == null)
////						{
////						  picture = new RrGraphics("STL Slice");
////						  picture.init(ObjectPlanRectangle(), false);
////						}
////
////						picture.cleanPolygons();
////						csgp.divide(0.0001, 1.0);
////						//picture.add(pgl);
////						picture.add(csgp);
//					}
//				}
//			}
//		}
////		if(picture == null)
////		{
////		  picture = new RrGraphics("STL Slice");
////		  picture.init(ObjectPlanRectangle(), false);
////		}
////
////		picture.cleanPolygons();
////		picture.add(pgl);
//
//		return rl;
//	}
//}