summaryrefslogtreecommitdiff
path: root/branches/sm-unittesting/src/org/reprap/geometry/polygons/RrGraphics.java
blob: 30d7a58b32a1d3f198fbbf422202b760eb620e61 (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
/*
 
 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/
 
 =====================================================================
 
 
 RrGraphics: Simple 2D graphics
 
 First version 20 May 2005
 This version: 1 May 2006 (Now in CVS - no more comments here)
 
 */

package org.reprap.geometry.polygons;

import java.awt.*;
import javax.swing.*;

public class RrGraphics 
{
	private final int frameWidth = 600; // Pixels
	private int frameHeight;
	private RrPolygonList p_list;
	private RrCSGPolygon csg_p;
	private double scale;
	private Rr2Point p_0;
	private Rr2Point pos;
	private Graphics2D g2d;
	private boolean plot_box;
	
	// Constructor for point-list polygon
	
	public RrGraphics(RrPolygonList pl, boolean pb) 
	{
		if(pl.size() <= 0)
		{
			System.err.println("Attempt to plot a null polygon list!");
			return;
		}
		
		p_list = pl;
		csg_p = null;
		plot_box = pb;
		
		RrBox big = pl.box.scale(1.1);
		
		double width = big.x().length();
		double height = big.y().length();
		frameHeight = (int)(0.5 + (frameWidth*height)/width);
		double xs = (double)frameWidth/width;
		double ys = (double)frameHeight/height;
		
		if (xs < ys)
			scale = xs;
		else
			scale = ys;
		p_0 = new Rr2Point((frameWidth - (width + 2*big.x().low())*scale)*0.5,
				(frameHeight - (height + 2*big.y().low())*scale)*0.5);
		
		pos = new Rr2Point(width*0.5, height*0.5);
		
		// Display the frame
		
		JFrame frame = new JFrame();
		frame.setSize(frameWidth, frameHeight);
		frame.getContentPane().add(new MyComponent());
		frame.setVisible(true);
	}
	
	// Constructor for CSG polygon
	
	public RrGraphics(RrCSGPolygon cp, boolean pb) 
	{
		p_list = null;
		csg_p = cp;
		plot_box = pb;
		
		RrBox big = csg_p.box().scale(1.1);
		
		double width = big.x().length();
		double height = big.y().length();
		frameHeight = (int)(0.5 + (frameWidth*height)/width);
		double xs = (double)frameWidth/width;
		double ys = (double)frameHeight/height;
		
		if (xs < ys)
			scale = xs;
		else
			scale = ys;
		p_0 = new Rr2Point((frameWidth - (width + 2*big.x().low())*scale)*0.5,
				(frameHeight - (height + 2*big.y().low())*scale)*0.5);
		
		pos = new Rr2Point(width*0.5, height*0.5);
		
		// Display the frame
		
		JFrame frame = new JFrame();
		frame.setSize(frameWidth, frameHeight);
		frame.getContentPane().add(new MyComponent());
		frame.setVisible(true);
	}
	
	// Real-world coordinates to pixels
	
	private Rr2Point transform(Rr2Point p)
	{
		return new Rr2Point(p_0.x() + scale*p.x(), (double)frameHeight - 
				(p_0.y() + scale*p.y()));
	}
	
	// Move invisibly to a point
	
	private void move(Rr2Point p)
	{
		pos = transform(p);
	}
	
	// Draw a straight line to a point
	
	private void plot(Rr2Point p)
	{
		Rr2Point a = transform(p);
		g2d.drawLine((int)(pos.x() + 0.5), (int)(pos.y() + 0.5), 
				(int)(a.x() + 0.5), (int)(a.y() + 0.5));
		pos = a;
	}
	
	// Set the plotting colour
	
	private void colour(int c)
	{
		switch(c)
		{
		case 0:
			g2d.setColor(Color.white);
			break;
			
		case 1:
			g2d.setColor(Color.black);
			break;
			
		case 2:
			g2d.setColor(Color.red);
			break;
			
		case 3:
			g2d.setColor(Color.green);
			break;
			
		case 4:
			g2d.setColor(Color.blue);
			break;
			
		default:
			g2d.setColor(Color.orange);
		break;
		
		}
	}
	
	// Plot a box
	
	private void plot(RrBox b)
	{
		colour(4);
		move(b.sw());
		plot(b.nw());
		plot(b.ne());
		plot(b.se());
		plot(b.sw());
	}
	
	// Plot a polygon
	
	private void plot(RrPolygon p)
	{
		if(plot_box)
			plot(p.box);
		
		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)
			{
				colour(f);
				plot(p.point(i));
			} else
				move(p.point(i)); 
		}
	}
	
	// Plot a section of parametric line
	
	private void plot(RrLine a, RrInterval i)
	{
		if(i.empty()) return;
		move(a.point(i.low()));
		plot(a.point(i.high()));
	}
	
	// Plot a set in a box
	
	private void plot(RrCSG c, RrBox b)
	{
		if(plot_box)
			plot(b);
		
		colour(1);
		
		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;
			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);                    
			}
			
			plot(ln1, range1);
			plot(ln2, range2);
			break;
			
		default:
			System.err.println("plot(RrCSG, RrBox): complexity > 2.");
		}
	}
	
	// Plot a divided CSG polygon recursively
	
	private void plot(RrCSGPolygon p)
	{
		if(p.c_1() == null)
		{
			plot(p.csg(), p.box());
		} else
		{
			plot(p.c_1());
			plot(p.c_2());
			plot(p.c_3());
			plot(p.c_4());
		}
	}
	
	
	// Master plot function - draw everything
	
	private void plot()
	{
		if(p_list == null)
			plot(csg_p);
		else
		{
			int leng = p_list.size();
			for(int i = 0; i < leng; i++)
				plot(p_list.polygon(i));
		}
	}
	
	class MyComponent extends JComponent 
	{
		// This method is called whenever the contents needs to be painted
		public void paint(Graphics g) 
		{
			// Retrieve the graphics context; this object is used to paint shapes
			g2d = (Graphics2D)g;
			// Draw everything
			plot();
		}
	}
}