summaryrefslogtreecommitdiff
path: root/trunk/users/adrian/host/src/org/reprap/geometry/LayerRules.java
blob: 9049026b26c5ece86aca1705f181fc71913d23d7 (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

package org.reprap.geometry;

import org.reprap.Printer;
import org.reprap.Extruder;
import org.reprap.devices.GenericExtruder;
import org.reprap.geometry.polygons.RrHalfPlane;
import org.reprap.geometry.polygons.RrRectangle;
import org.reprap.geometry.polygons.Rr2Point;
//import org.reprap.geometry.polygons.RrCSGPolygonList;
import org.reprap.Preferences;

/**
 * This stores a set of facts about the layer currently being made, and the
 * rules for such things as infill patterns, support patterns etc.
 */
public class LayerRules 
{	
	/**
	 * The machine
	 */
	private Printer printer;
	
	/**
	 * How far up the model we are in mm
	 */
	private double modelZ;
	
	/**
	 * How far we are up from machine Z=0
	 */
	private double machineZ;
	
	/**
	 * The count of layers up the model
	 */
	private int modelLayer;
	
	/**
	 * The number of layers the machine has done
	 */
	private int machineLayer;
	
	/**
	 * The top of the model in model coordinates
	 */
	private double modelZMax;
	
	/**
	 * The highest the machine should go this build
	 */
	private double machineZMax;
	
	/**
	 * The number of the last model layer (first = 0)
	 */
	private int modelLayerMax;
	
	/**
	 * The number of the last machine layer (first = 0)
	 */
	private int machineLayerMax;
	
	/**
	 * Putting down foundations?
	 */
	private boolean layingSupport;
	
	/**
	 * The step height of all the extruders
	 */
	private double zStep;
	
	/**
	 * If we take a short step, remember it and add it on next time
	 */
	private double addToStep = 0;
	
	/**
	 * Are we going top to bottom or ground up?
	 */
	private boolean topDown = false;
	
	/**
	 * This is true until it is first read, when it becomes false
	 */
	private boolean notStartedYet;
	
	/**
	 * layers above and below where we are for infill and support calculations
	 */
	//private RrCSGPolygonList [] layerRecord;
	
	/**
	 * The machineLayer value for each entry in layerRecord
	 */
	private int [] recordNumber;
	
	/**
	 * index into layerRecord
	 */
	private int layerPointer;
	
	/**
	 * The XY rectangle that bounds the build
	 */
	private RrRectangle bBox;
	
	/**
	 * 
	 * @param p
	 * @param modZMax
	 * @param macZMax
	 * @param modLMax
	 * @param macLMax
	 * @param found
	 */
	public LayerRules(Printer p, double modZMax, double macZMax,
			int modLMax, int macLMax, boolean found, RrRectangle bb)
	{
		printer = p;
		
		bBox = bb;
		
		notStartedYet = true;

		topDown = printer.getTopDown();

		modelZMax = modZMax;
		machineZMax = macZMax;
		modelLayerMax = modLMax;
		machineLayerMax = macLMax;
		if(topDown)
		{
			modelZ = modelZMax;
			machineZ = machineZMax;
			modelLayer = modelLayerMax;
			machineLayer = machineLayerMax;			
		} else
		{
			modelZ = 0;
			machineZ = 0;
			modelLayer = -1;
			machineLayer = 0;			
		}
		addToStep = 0;

		layingSupport = found;
		Extruder[] es = printer.getExtruders();
		zStep = es[0].getExtrusionHeight();
		int fineLayers = es[0].getLowerFineLayers();
		if(es.length > 1)
		{
			for(int i = 1; i < es.length; i++)
			{
				if(es[i].getLowerFineLayers() > fineLayers)
					fineLayers = es[i].getLowerFineLayers();
				if(Math.abs(es[i].getExtrusionHeight() - zStep) > Preferences.tiny())
					System.err.println("Not all extruders extrude the same height of filament: " + 
							zStep + " and " + es[i].getExtrusionHeight());
			}
		}
		
		//layerRecord = new RrCSGPolygonList[fineLayers+1];
		recordNumber = new int[fineLayers+1];
		layerPointer = 0;
//		for(int i = 0; i < layerRecord.length; i++)
//		{
//			layerRecord[i] = null;
//			recordNumber[i] = -1;
//		}
	}
	
	public RrRectangle getBox()
	{
		return bBox;
	}
	
//	public void recordThisLayer(RrCSGPolygonList pl)
//	{
//		//if(layerRecord[layerPointer] != null)
//		//	layerRecord[layerPointer].destroy();
//		layerRecord[layerPointer] = pl;
//		recordNumber[layerPointer] = machineLayer;
//		layerPointer++;
//		if(layerPointer >= layerRecord.length)
//			layerPointer = 0;
//	}
	
	/**
	 * Return the layer i above where we are 
	 * @param i
	 * @return
	 */
//	public RrCSGPolygonList getLayerAbove(int i)
//	{
//		int lp = layerPointer - i;
//		if(lp < 0)
//			return layerRecord[layerRecord.length + lp];
//		else
//			return layerRecord[lp];
//	}
	
	public boolean getTopDown() { return topDown; }
	
	public void setPrinter(Printer p) { printer = p; }
	public Printer getPrinter() { return printer; }
	
	//public void setModelZ(double mz) { modelZ = mz; }
	public double getModelZ() { return modelZ; }
	
	public double getMachineZ() { return machineZ; }
	
	//private void setModelLayer(int ml) { modelLayer = ml; }
	public int getModelLayer() { return modelLayer; }
	
	public int getModelLayerMax() { return modelLayerMax; }
	
	public int getMachineLayerMax() { return machineLayerMax; }
	
	public int getMachineLayer() { return machineLayer; }
	
	public int getFoundationLayers() { return machineLayerMax - modelLayerMax; }
	
	public double getModelZMAx() { return modelZMax; }
	
	public double getMachineZMAx() { return machineZMax; }
	
	public double getZStep() { return zStep; }
	
	public boolean notStartedYet()
	{
		if(notStartedYet)
		{
			notStartedYet = false;
			return true;
		}
		return false;
	}
	
	
	public void setLayingSupport(boolean lf) { layingSupport = lf; }
	public boolean getLayingSupport() { return layingSupport; }
	
	/**
	 * Does the layer about to be produced need to be recomputed?
	 * @return
	 */
	public boolean recomputeLayer()
	{
		return getFoundationLayers() - getMachineLayer() <= 2;
	}
	
	/**
	 * The hatch pattern is:
	 * 
	 *  Foundation:
	 *   X and Y rectangle
	 *   
	 *  Model:
	 *   Alternate even then odd (which can be set to the same angle if wanted).
	 *   
	 * @return
	 */
	public RrHalfPlane getHatchDirection(Extruder e) 
	{
		double angle;
		
		if(getMachineLayer() < getFoundationLayers())
		{
			if(getMachineLayer() == getFoundationLayers() - 2)
				angle = e.getEvenHatchDirection();
			else
				angle = e.getOddHatchDirection();
		} else
		{
			if(getModelLayer()%2 == 0)
				angle = e.getEvenHatchDirection();
			else
				angle = e.getOddHatchDirection();
		}
		angle = angle*Math.PI/180;
		return new RrHalfPlane(new Rr2Point(0.0, 0.0), new Rr2Point(Math.sin(angle), Math.cos(angle)));
	}
	
	/**
	 * The gap in the layer zig-zag is:
	 * 
	 *  Foundation:
	 *   The foundation width for all but...
	 *   ...the penultimate foundation layer, which is half that and..
	 *   ...the last foundation layer, which is the model fill width
	 *   
	 *  Model:
	 *   The model fill width
	 *   
	 * @param e
	 * @return
	 */
	public double getHatchWidth(Extruder e)
	{
		if(getMachineLayer() < getFoundationLayers())
		{
			//if(getMachineLayer() == getFoundationLayers() - 2)
				//return e.getExtrusionFoundationWidth()*0.5;

			
			return e.getExtrusionFoundationWidth();
		}
		
		
//		String inFillName = e.getBroadInfillMaterial();
//		
//		// If this stuff's infill is not called "null"...
//		
//		if(!inFillName.contentEquals("null"))
//		{
//			if(modelLayer+1 > e.getLowerFineLayers() && modelLayer+1 <= modelLayerMax - e.getUpperFineLayers())
//			{
//				Extruder inFillExtruder = printer.getExtruders()[GenericExtruder.getNumberFromMaterial(inFillName)];
//				return inFillExtruder.getExtrusionInfillWidth();
//			}
//		}
		
		return e.getExtrusionInfillWidth();
	}
	
	/**
	 * Move the machine up/down, but leave the model's layer where it is.
	 *
	 * @param e
	 */
	public void stepMachine(Extruder e)
	{
		double sZ = e.getExtrusionHeight();
		int ld;
		
		if(topDown)
		{
			machineZ -= (sZ + addToStep);
			machineLayer--;
			ld = getFoundationLayers() - getMachineLayer();
			if(ld == 2)
				addToStep = sZ*(1 - e.getSeparationFraction());
			else if(ld == 1)
				addToStep = -sZ*(1 - e.getSeparationFraction());
			else
				addToStep = 0;
		} else
		{
			machineZ += (sZ + addToStep);
			machineLayer++;
			ld = getFoundationLayers() - getMachineLayer();
			if(ld == 2)
				addToStep = -sZ*(1 - e.getSeparationFraction());
			else if(ld == 1)
				addToStep = sZ*(1 - e.getSeparationFraction());
			else
				addToStep = 0;
		}
	}
	
	public void moveZAtStartOfLayer()
	{
		double z = getMachineZ();

		if(topDown)
		{
			printer.setZ(z - (zStep + addToStep));
		}
		printer.singleMove(printer.getX(), printer.getY(), z, printer.getFastFeedrateZ());
	}
	
	/**
	 * Move both the model and the machine up/down a layer
	 * @param e
	 */
	public void step(Extruder e)
	{		
		double sZ = e.getExtrusionHeight();
		if(topDown)
		{
			modelZ -= (sZ + addToStep);
			modelLayer--;			
		} else
		{
			modelZ += (sZ + addToStep);
			modelLayer++;
		}
		addToStep = 0;
		stepMachine(e);
		
//		// modelLayer < 0 when we're building foundations
//		if(modelLayer < 0)
//		{
//			modelLayer = 0;
//			modelZ = 0;
//		}
	}
	
	public void setFractionDone()
	{
//		double f = machineZ/machineZMax;
//		if(topDown)
//			f = 1 - f;
		
		// Set -ve to force the system to query the layer rules
		
		org.reprap.gui.botConsole.BotConsoleFrame.getBotConsoleFrame().setFractionDone(-1);
	}
	
}