summaryrefslogtreecommitdiff
path: root/tags/host/0.8.1/src/org/reprap/geometry/polygons/RrInterval.java
blob: 2d4dce12c8da8bcbf884206da5479087f98101d0 (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
/*
 
 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/
 
 =====================================================================
 
 RrInterval: 1D intervals
 
 First version 20 May 2005
 This version: 1 May 2006 (Now in CVS - no more comments here)
 
 */

package org.reprap.geometry.polygons;

/**
 * Real 1D intervals
 */
public class RrInterval
{
	private double low;
	private double high;
	private boolean empty;
	
	public RrInterval()
	{
		empty = true;
	}
	
	/**
	 * Two ends...
	 * @param l
	 * @param h
	 */
	public RrInterval(double l, double h)
	{
		low = l;
		high = h;
		empty = (low > high);
		if(empty)
			System.err.println("RrInterval: low value bigger than high.");
	}
	
	/**
	 * Deep copy
	 * @param i
	 */
	public RrInterval(RrInterval i)
	{
		low = i.low;
		high = i.high;
		empty = i.empty;
	}
	
	/**
	 * @return Return contents
	 */
	public double low() { return low; }
	public double high() { return high; }
	public boolean empty() { return empty; }
	
	/**
	 * The biggest possible
	 * @return biggest possible interval
	 */
	public static RrInterval big_interval()
	{
		return new RrInterval(Double.NEGATIVE_INFINITY, Double.POSITIVE_INFINITY);
	}
	
	/* (non-Javadoc)
	 * @see java.lang.Object#toString()
	 */
	public String toString()
	{
		if(empty)
			return "[empty]";
		return "[" + Double.toString(low) + ", " + Double.toString(high) + "]";
	}
	
	/**
	 * Accomodate v
	 * @param v
	 */
	public void expand(double v)
	{
		if(empty)
		{
			low = v;
			high = v;
		} else
		{
			if(v < low)
				low = v;
			if(v > high)
				high = v;
		}
	}
	
	/**
	 * Accommodate another interval
	 * @param i
	 */
	public void expand(RrInterval i)
	{
		expand(i.low);
		expand(i.high);
	}
	
	/**
	 * Size
	 * @return size
	 */
	public double length()
	{
		return high - low;
	}
	
	
	/**
	 * Interval addition
	 * @param a
	 * @param b
	 * @return new interval based on addition of intervals a and b
	 */
	public static RrInterval add(RrInterval a, RrInterval b)
	{
		if(a.empty || b.empty)
			System.err.println("add(...): adding empty interval(s).");	    
		return new RrInterval(a.low + b.low, a.high + b.high);
	}
	
	/**
	 * @param a
	 * @param b
	 * @return new interval based on addition of interval a and value b
	 */
	public static RrInterval add(RrInterval a, double b)
	{
		if(a.empty)
			System.err.println("add(...): adding an empty interval.");	    
		return new RrInterval(a.low + b, a.high + b);
	}
	
	/**
	 * @param b
	 * @param a
	 * @return new interval based on addition of interval a and value b
	 */
	public static RrInterval add(double b, RrInterval a)
	{	    
		return add(a, b);
	}
	
	
	/**
	 * Interval subtraction
	 * @param a
	 * @param b
	 * @return new interval based on subtraction of interval a and value b
	 */
	public static RrInterval sub(RrInterval a, RrInterval b)
	{
		if(a.empty || b.empty)
			System.err.println("difference(...): subtracting empty interval(s).");
		return new RrInterval(a.low - b.high, a.high - b.low);
	}
	
	/**
	 * @param a
	 * @param b
	 * @return new interval based on subtraction of interval a and value b
	 */
	public static RrInterval sub(RrInterval a, double b)
	{
		if(a.empty)
			System.err.println("difference(...): subtracting an empty interval.");
		return new RrInterval(a.low - b, a.high - b);
	}
	
	/**
	 * @param b
	 * @param a
	 * @return new interval based on subtraction of interval a and value b
	 */
	public static RrInterval sub(double b, RrInterval a)
	{
		if(a.empty)
			System.err.println("difference(...): subtracting an empty interval.");
		return new RrInterval(b - a.high, b - a.low);
	}   
	
	/**
	 * Interval multiplication
	 * @param a
	 * @param b
	 * @return new interval based on interval multiplication of intervals a and b
	 */
	public static RrInterval mul(RrInterval a, RrInterval b)
	{
		if(a.empty || b.empty)
			System.err.println("multiply(...): multiplying empty intervals.");
		double d = a.low*b.low;
		RrInterval r = new RrInterval(d, d);
		r.expand(a.low*b.high);
		r.expand(a.high*b.low);
		r.expand(a.high*b.high);
		return r;
	}
	
	/**
	 * @param a
	 * @param f
	 * @return new interval based on interval multiplication of interval a by factor f
	 */
	public static RrInterval mul(RrInterval a, double f)
	{
		if(a.empty)
			System.err.println("multiply(...): multiplying an empty interval.");
		if(f > 0)
			return new RrInterval(a.low*f, a.high*f);
		else
			return new RrInterval(a.high*f, a.low*f);	    
	}
	
	/**
	 * @param f
	 * @param a
	 * @return new interval based on interval multiplication of interval a by factor f
	 */
	public static RrInterval mul(double f, RrInterval a)
	{
		return mul(a, f);	    
	}
	
	/**
	 * Negative, zero, or positive?
	 * @return true if interval is negative (?)
	 */
	public boolean neg()
	{
		return high < 0;
	}
	
	/**
	 * @return true if intervale is positive (?)
	 */
	public boolean pos()
	{
		return low >= 0;
	}
	
	/**
	 * Does the interval _contain_ zero?
	 * @return true if zero is within the interval
	 */
	public boolean zero()
	{
		return(!neg() && !pos());
	}
	
	/**
	 * In or out
	 * @param v
	 * @return true if v is within the interval
	 */
	public boolean in(double v)
	{
		return v >= low && v <= high;
	}
	
	/**
	 * Identical within tolerance
	 * @param a
	 * @param b
	 * @param tolerance
	 * @return true if intervals a and b are identical within the tolerance
	 */
	public static boolean same(RrInterval a, RrInterval b, double tolerance)
	{
		if(a.empty() && b.empty())   //??? !!!
			return true;
		
		if( Math.abs(a.low - b.low) > tolerance)
			return false;
		if (Math.abs(a.high - b.high) > tolerance)
			return false;
		return true;
	}
	
	/**
	 * Absolute value of an interval
	 * @return absolute value of the interval
	 */
	public RrInterval abs()
	{
		RrInterval result = new RrInterval(this);
		double p;
		
		if (low < 0)
		{
			if (high <= 0)
			{
				result = new RrInterval(-high, -low);
			} else
			{
				result = new RrInterval(0, result.high);
				p = -low;
				if ( p > high ) result = new RrInterval(result.low, p);
			}
		}
		return(result);
	}
	
	/**
	 * Sign of an interval
	 * @param x
	 * @return sign of the interval
	 */
	public static double sign(double x) 
	{ 
		if (x < 0) return -1; 
		else if (x > 0) return 1; 
		else return 0;
	}
	
	/**
	 * @return new interval object based on ?
	 */
	public RrInterval sign()
	{
		return( new RrInterval(sign(low), sign(high)) );
	}
	
	/**
	 * Max
	 * @param a
	 * @param b
	 * @return max value of the interval
	 */
	public static RrInterval max(RrInterval a, RrInterval b)
	{
		RrInterval result = new RrInterval(b);
		if (a.low > b.low) result = new RrInterval(a.low, result.high);
		if (a.high > b.high) result = new RrInterval(result.low, a.high);
		return(result);
	}
	
	/**
	 * Min
	 * @param a
	 * @param b
	 * @return minimal value of the interval
	 */
	public static RrInterval min(RrInterval a, RrInterval b)
	{
		RrInterval result = new RrInterval(b);
		if (a.low < b.low) result = new RrInterval(a.low, result.high);
		if (a.high < b.high) result = new RrInterval(result.low, a.high);
		return(result);
	}
}