summaryrefslogtreecommitdiff
path: root/branches/sm-unittesting/src/org/reprap/geometry/LayerProducer.java
blob: 625c34c5264e4a125f595b224246e948595cf20b (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
/*
 * Created on May 1, 2006
 *
 * TODO To change the template for this generated file go to
 * Window - Preferences - Java - Code Style - Code Templates
 */
package org.reprap.geometry;

import java.io.IOException;

import org.reprap.Printer;
import org.reprap.ReprapException;
import org.reprap.geometry.polygons.Rr2Point;
import org.reprap.geometry.polygons.RrBox;
import org.reprap.geometry.polygons.RrCSG;
import org.reprap.geometry.polygons.RrCSGOp;
import org.reprap.geometry.polygons.RrCSGPolygon;
import org.reprap.geometry.polygons.RrInterval;
import org.reprap.geometry.polygons.RrLine;
import org.reprap.geometry.polygons.RrPolygon;
import org.reprap.geometry.polygons.RrPolygonList;

public class LayerProducer {
	private static final double extrusionWidth = 0.3;  ///< Extrusion thickness in millimeters
	private static int gapMaterial = 0;
	private static int solidMaterial = 1;
	

	private Printer printer;
	private RrPolygonList hatchedPolygons;
	private RrPolygonList borderPolygons;
	
	private RrCSGPolygon csg_p;
	
	/**
	 * @param reprap
	 * @param list
	 * @param hatchDirection
	 */
	public LayerProducer(Printer printer, RrPolygonList list, RrLine hatchDirection) {
		this.printer = printer;

		borderPolygons = list;
		
		RrPolygon hatched = list.hatch(hatchDirection, extrusionWidth,
				gapMaterial, solidMaterial);

		hatchedPolygons = new RrPolygonList();
		hatchedPolygons.append(hatched);
		
		//new RrGraphics(p_list, false);
		
		csg_p = null;
		
		//RrBox big = hatchedPolygons.box.scale(1.1);
		
		//double width = big.x().length();
		//double height = big.y().length();
	}
	
	private void plot(Rr2Point a) throws ReprapException, IOException
	{
		if (printer.isCancelled()) return;
		printer.printTo(a.x(), a.y(), printer.getZ());
	}

	private void move(Rr2Point a) throws ReprapException, IOException
	{
		if (printer.isCancelled()) return;
		printer.moveTo(a.x(), a.y(), printer.getZ());
	}


	/**
	 * Plot a polygon
	 * @throws IOException
	 * @throws ReprapException
	 */
	private void plot(RrPolygon p) throws ReprapException, IOException
	{
		int leng = p.size();
		for(int j = 0; j <= leng; j++)
		{
			int i = j%leng;
			int f = p.flag(i);
			if(f != 0 && j != 0)
			{
				if (printer.isCancelled()) return;
				plot(p.point(i));
			} else
				if (printer.isCancelled()) return;
				move(p.point(i)); 
		}
	}
	
	/**
	 * Plot a section of parametric line
	 * @throws IOException
	 * @throws ReprapException
	 */
	private void plot(RrLine a, RrInterval i) throws ReprapException, IOException
	{
		if(i.empty()) return;
		if (printer.isCancelled()) return;
		move(a.point(i.low()));
		if (printer.isCancelled()) return;
		plot(a.point(i.high()));
	}
	
	/**
	 * Plot a set in a box
	 * @throws IOException
	 * @throws ReprapException
	 */
	private void plot(RrCSG c, RrBox b) throws ReprapException, IOException
	{
		switch(c.complexity())
		{
		case 0:
			return;
			
			// One half-plane in the box
			
		case 1:
			if(c.plane() == null)
				System.err.println("plot(RrCSG, RrBox): hp not set.");
			RrLine ln = new RrLine(c.plane());
			RrInterval range = RrInterval.big_interval();
			range = b.wipe(ln, range);
			if(range.empty()) return;
			if (printer.isCancelled()) return;
			plot(ln, range);
			break;
			
			// Two - maybe a corner, or they may not intersect
			
		case 2:
			RrLine ln1 = new RrLine(c.c_1().plane());
			RrInterval range1 = RrInterval.big_interval();
			range1 = b.wipe(ln1, range1);
			RrLine ln2 = new RrLine(c.c_2().plane());
			RrInterval range2 = RrInterval.big_interval();
			range2 = b.wipe(ln2, range2);              
			if(c.operator() == RrCSGOp.INTERSECTION)
			{
				range2 = c.c_1().plane().wipe(ln2, range2);
				range1 = c.c_2().plane().wipe(ln1, range1);
			} else
			{
				range2 = c.c_1().plane().complement().wipe(ln2, range2);
				range1 = c.c_2().plane().complement().wipe(ln1, range1);                    
			}
			
			if (printer.isCancelled()) return;
			plot(ln1, range1);
			if (printer.isCancelled()) return;
			plot(ln2, range2);
			break;
			
		default:
			System.err.println("plot(RrCSG, RrBox): complexity > 2.");
		}
	}
	
	/**
	 * Plot a divided CSG polygon recursively
	 * @throws IOException
	 * @throws ReprapException
	 */
	private void plot(RrCSGPolygon p) throws ReprapException, IOException
	{
		if(p.c_1() == null)
		{
			if (printer.isCancelled()) return;
			plot(p.csg(), p.box());
		} else
		{
			if (printer.isCancelled()) return;
			plot(p.c_1());
			if (printer.isCancelled()) return;
			plot(p.c_2());
			if (printer.isCancelled()) return;
			plot(p.c_3());
			if (printer.isCancelled()) return;
			plot(p.c_4());
		}
	}
	
	/**
	 * Master plot function - draw everything
	 * @throws IOException
	 * @throws ReprapException
	 */
	public void plot() throws ReprapException, IOException
	{
		if (hatchedPolygons == null)
			plot(csg_p);
		else {
			int leng = borderPolygons.size();
			for(int i = 0; i < leng; i++) {
				plot(borderPolygons.polygon(i));
			}			
			leng = hatchedPolygons.size();
			for(int i = 0; i < leng; i++) {
				if (printer.isCancelled())
					break;
				plot(hatchedPolygons.polygon(i));
			}
		}
	}
	
}