summaryrefslogtreecommitdiff
path: root/trunk/users/adrian/host/src/org/reprap/geometry/polygons/RrRectangle.java
blob: 416ef20ac9f2071f9a55c1e24353b5d3b3c3ae63 (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
/*
 
 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/
 
 =====================================================================
 
 RrBox: 2D rectangles
 
 First version 20 May 2005
 This version: 1 May 2006 (Now in CVS - no more comments here)
 
 */

package org.reprap.geometry.polygons;

/**
 * A 2D box is an X and a Y interval
 */
public class RrRectangle
{
	/**
	 * Compass directions
	 */
	public static final byte rr_N = 0x01;
	public static final byte rr_E = 0x02;
	public static final byte rr_S = 0x04;
	public static final byte rr_W = 0x08;
	
	/**
	 * X interval
	 */
	private RrInterval x = null;
	
	/**
	 * Y interval
	 */
	private RrInterval y = null;
	
	/**
	 * Anyone home?
	 */
	private boolean empty;
	
	/**
	 * 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(x != null)
			x.destroy();
		x = null;
		if(y != null)
			y.destroy();		
		y = null;
		beingDestroyed = false;
	}
	
	/**
	 * Destroy just me
	 */
	protected void finalize() throws Throwable
	{
		x = null;
		y = null;
		super.finalize();
	}
	
	/**
	 * Default is empty
	 */ 
	public RrRectangle()
	{
		empty = true;
	}
	
//	/**
//	 * For when we need one that has just something in
//	 * @param a
//	 */
//	public RrBox(double a)
//	{
//		x = new RrInterval(0, a);
//		y = new RrInterval(0, a);
//		empty = false;
//	}
	
	/**
	 * Copy constructor
	 */
	public RrRectangle(RrRectangle b)
	{
		x = new RrInterval(b.x);
		y = new RrInterval(b.y);
		empty = b.empty;
	}
	
	/**
	 * Make from any diagonal corners
	 * @param sw
	 * @param ne
	 */
	public RrRectangle(Rr2Point a, Rr2Point b)
	{
		x = new RrInterval(Math.min(a.x(), b.x()), Math.max(a.x(), b.x()));
		y = new RrInterval(Math.min(a.y(), b.y()), Math.max(a.y(), b.y()));
		empty = x.empty() || y.empty();
	}
	
	/**
	 * Make from X and Y intervals
	 * @param sw
	 * @param ne
	 */
	public RrRectangle(RrInterval xi, RrInterval yi)
	{
		x = new RrInterval(xi);
		y = new RrInterval(yi);
		empty = x.empty() || y.empty();
	}
	
	
	/**
	 * @return Return the x interval
	 */
	public RrInterval x() { return x; }
	
	
	/**
	 * @return Return the y interval
	 * 
	 */
	public RrInterval y() { return y; }
	
	/**
	 * 
	 * @return
	 */
	public boolean empty() { return empty; }
	
	
	/**
	 * Expand the box to incorporate another box or a point
	 * @param a
	 */
	public void expand(RrRectangle a)
	{
		if(a.empty)
			return;
		if(empty)
		{
			empty = false;
			x = new RrInterval(a.x);
			y = new RrInterval(a.y);
		} else
		{
			x.expand(a.x);
			y.expand(a.y);
		}
	}
	
	/**
	 * Shrink or grow by a given distance
	 * @param dist
	 * @return
	 */
	public RrRectangle offset(double dist)
	{
		return new RrRectangle(new RrInterval(x.low() - dist, x.high() + dist), 
				new RrInterval(y.low() - dist, y.high() + dist));
	}
	
	/**
	 * @param a
	 */
	public void expand(Rr2Point a)
	{
		if(empty)
		{
			empty = false;
			x = new RrInterval(a.x(), a.x());
			y = new RrInterval(a.y(), a.y());
		} else
		{
			x.expand(a.x());
			y.expand(a.y());
		}
	}
	
	/**
	 * Corner points and center
	 * @return NE cornerpoint
	 */
	public Rr2Point ne()
	{
		return new Rr2Point(x.high(), y.high());
	}
	
	/**
	 * @return SW cornerpoint
	 */
	public Rr2Point sw()
	{
		return new Rr2Point(x.low(), y.low());
	}
	
	/**
	 * @return SE cornerpoint
	 */
	public Rr2Point se()
	{
		return new Rr2Point(x.high(), y.low());
	}
	
	/**
	 * @return NW cornerpoint
	 */
	public Rr2Point nw()
	{
		return new Rr2Point(x.low(), y.high());
	}   
	
	/**
	 * @return Centre point
	 */
	public Rr2Point centre()
	{
		return new Rr2Point(x.cen(), y.cen());
	}	
	/**
	 * Scale the box by a factor about its center
	 * @param f
	 * @return scaled box object
	 */
	public RrRectangle scale(double f)
	{
		RrRectangle r = new RrRectangle();
		if(empty)
			return r;
		f = 0.5*f;
		Rr2Point p = new Rr2Point(x.length()*f, y.length()*f);
		Rr2Point c = centre();
		r.expand(Rr2Point.add(c, p));
		r.expand(Rr2Point.sub(c, p));
		return r;
	}
	
	/**
	 * Convert to a string
	 */
	public String toString()
	{
		if(empty)
			return "<empty>";
		return "<BOX x:" + x.toString() + ", y: " + y.toString() + ">";
	}
	
	/**
	 * Squared diagonal
	 * @return squared diagonal of the box
	 */
	public double dSquared()
	{
		if(empty)
			return 0;
		return Rr2Point.dSquared(sw(), ne());
	}

	/**
	 * Squared distance to a point
	 * @return minimal squared distance to a point from one of the corners of the box
	 */
	public double dSquared(Rr2Point p)
	{
		if(empty)
			return Double.POSITIVE_INFINITY;
		byte b = pointRelative(p);
		double d1 = Double.POSITIVE_INFINITY, d2 = Double.POSITIVE_INFINITY;
		switch(b)
		{
		case 0:
			return 0;
			
		case 1:
			d1 = Rr2Point.dSquared(p, nw());
			d2 = Rr2Point.dSquared(p, ne());
			break;
			
		case 2:
			d1 = Rr2Point.dSquared(p, ne());
			d2 = Rr2Point.dSquared(p, se());
			break;
			
		case 3:
			return Rr2Point.dSquared(p, ne());
			
		case 4:
			d1 = Rr2Point.dSquared(p, sw());
			d2 = Rr2Point.dSquared(p, se());
			break;
			
		case 6:
			return Rr2Point.dSquared(p, se());
			
		case 8:
			d1 = Rr2Point.dSquared(p, sw());
			d2 = Rr2Point.dSquared(p, nw());
			break;
			
		case 9:
			return Rr2Point.dSquared(p, nw());
			
		case 12:
			return Rr2Point.dSquared(p, sw());
			
		default:
			System.err.println("RrBox.dSquared(): dud value from point_relative()!");	
		}
		
		return Math.min(d1, d2);

	}
	

	/**
	 * Take a range of parameter values and a line, and find
     * the intersection of that range with the part of the line
     * (if any) in the box.
	 * @param a
	 * @param oldRange
	 * @return intersection interval
	 */
	public RrInterval wipe(RrLine a, RrInterval oldRange)
	{
		if(oldRange.empty()) return oldRange;
		
		RrInterval range = new RrInterval(oldRange);
		
		RrHalfPlane hp = new RrHalfPlane(sw(), nw());
		range = hp.wipe(a, range);
		if(range.empty()) return range;
		
		hp = new RrHalfPlane(nw(), ne());
		range = hp.wipe(a, range);
		if(range.empty()) return range;
		
		hp = new RrHalfPlane(ne(), se());
		range = hp.wipe(a, range);
		if(range.empty()) return range;
		
		hp = new RrHalfPlane(se(), sw());
		range = hp.wipe(a, range);
		return range;
	}
	
	/**
	 * Where is a point relative to a box?
	 * @param p 
	 * @return relative position of a point p (E, W, N or S)
	 */
	public byte pointRelative(Rr2Point p)
	{
		byte result = 0;
		if(p.x() >= x.high())
			result |= rr_E;
		if(p.x() < x.low())
			result |= rr_W;
		if(p.y() >= y.high())
			result |= rr_N;
		if(p.y() < y.low())
			result |= rr_S;        
		return result;
	}
	
	/**
	 * Intersection
	 * @param a
	 * @param b
	 * @return
	 */
	public static RrRectangle intersection(RrRectangle a, RrRectangle b)
	{
		return new RrRectangle(RrInterval.intersection(a.x, b.x), RrInterval.intersection(a.y, b.y));	
	}
	
	/**
	 * Union
	 * @param a
	 * @param b
	 * @return
	 */
	public static RrRectangle union(RrRectangle a, RrRectangle b)
	{
		return new RrRectangle(RrInterval.union(a.x, b.x), RrInterval.union(a.y, b.y));	
	}
}