summaryrefslogtreecommitdiff
path: root/tags/host/0.8.1/src/org/reprap/geometry/polygons/RrHalfPlane.java
blob: bfbf5f30e5cf1e6f840e2873ce149784eac09e4f (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
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
/*
 
 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/
 
 =====================================================================
 
 RrHalfPlane: 2D planar half-spaces
 
 First version 20 May 2005
 This version: 9 March 2006
 
 */

package org.reprap.geometry.polygons;

import java.util.ArrayList;
import java.util.List;

/**
 * Small class to hold parameter/quad pairs
 * @author Adrian
 *
 */
class lineIntersection
{
	/**
	 * The line's parameter 
	 */
	private double t;
	
	/**
	 * Quad containing hit plane 
	 */
	private RrCSGPolygon quad;
	
	/**
	 * @param v
	 * @param q
	 */
	public lineIntersection(double v, RrCSGPolygon q)
	{
		t = v;
		quad = q;
	}
	
	/**
	 * @return
	 */
	public double parameter() { return t; }
	
	/**
	 * @return
	 */
	public RrCSGPolygon quad() { return quad; }
}


/**
 * Class to hold and manipulate linear half-planes
 */
public class RrHalfPlane
{
	
	/**
	 * The half-plane is normal*(x, y) + offset <= 0 
	 */
	private Rr2Point normal; 
	private double offset;
	
	/**
	 * Keep the parametric equivalent to save computing it
	 */
	private RrLine p;
	
	/**
	 * List of intersections with others
	 */
	private List crossings;
	
	/**
	 * Convert a parametric line
	 * @param l
	 */
	public RrHalfPlane(RrLine l)
	{
		p = new RrLine(l);
		p.norm();
		normal = new Rr2Point(-p.direction().y(), p.direction().x());
		offset = -Rr2Point.mul(l.origin(), normal());
		crossings = new ArrayList();
	}
	
	/**
	 * Make one from two points on its edge
	 * @param a
	 * @param b
	 */
	public RrHalfPlane(Rr2Point a, Rr2Point b)
	{
		this(new RrLine(a, b));
	}   
	
	/**
	 * Deep copy
	 * @param a
	 */
	public RrHalfPlane(RrHalfPlane a)
	{
		normal = new Rr2Point(a.normal);
		offset = a.offset;
		p = new RrLine(a.p);
		crossings = new ArrayList(); // No point in deep copy -
		                             // No pointers would match
	}
	
	/**
	 * Get the parametric equivalent
	 * @return parametric equivalent of a line
	 */
	public RrLine pLine()
	{
		return p;
	}
	
	/**
	 * The number of crossings
	 * @return number of crossings
	 */
	public int size()
	{
		return crossings.size();
	}
	
	/**
	 * Get the i-th crossing parameter
	 * @param i
	 * @return i-th crossing parameter
	 */
	public double getParameter(int i)
	{
		return ((lineIntersection)crossings.get(i)).parameter();
	}
	
	/**
	 * i-th point from the crossing list
	 * @param i
	 * @return i-th point
	 */
	public Rr2Point getPoint(int i)
	{
		return pLine().point(getParameter(i));
	}
	
	/**
	 * Get the i-th quad
	 * @param i
	 * @return i-th quad
	 */
	public RrCSGPolygon getQuad(int i)
	{
		return ((lineIntersection)crossings.get(i)).quad();
	}
	
	/**
	 * Get the i-th CSG for the plane
	 * @param i
	 * @return i-th CSG
	 */
	public RrCSG getCSG(int i)
	{
		RrCSGPolygon q = getQuad(i);
		if(q.csg().complexity() == 1)
			return q.csg();
		else if(q.csg().complexity() == 2)
		{
			if(q.csg().c_1().plane() == this)
				return q.csg().c_2();
			if(q.csg().c_2().plane() == this)			
				return q.csg().c_1();
			
			double t = getParameter(i);
			double v = Math.abs(q.csg().c_1().plane().value(pLine().point(t)));
			if(Math.abs(q.csg().c_2().plane().value(pLine().point(t))) < v)
				return q.csg().c_2();
			else
				return q.csg().c_1();
		}
		
		System.err.println("RrHalfPlane.getCSG(): complexity: " + q.csg().complexity());
		return RrCSG.nothing();
	}
	
	/**
	 * Get the i-th plane.
	 * @param i
	 * @return i-th plane
	 */
	public RrHalfPlane getPlane(int i)
	{
		return getCSG(i).plane();
	}
	
	/**
	 * Take the sorted list of parameter values and a shape, and
	 * make sure they alternate solid/void/solid etc.  Insert
	 * duplicate parameter values if need be to ensure this,
	 * or - if two are very close - delete one. 
	 * @param p

	 */
	public void solidSet(RrCSGPolygon p)
	{
		double v;
		boolean odd = true;
		int i = 0;
		while(i < size() - 1)
		{
			double pi = getParameter(i);
			double pi1 = getParameter(i+1);
			v = 0.5*(pi + pi1);
			boolean tiny = Math.abs(pi1 - pi) < 2*Math.sqrt(p.box().d_2()); // Is this too coarse a limit?
			v = p.value(pLine().point(v));
			if(odd)
			{
				if(v > 0)
				{
					if(tiny)
						crossings.remove(i);
					else
						crossings.add(i, crossings.get(i));
				}
			} else
			{
				if(v <= 0)
				{
					if(tiny)
						crossings.remove(i);
					else
						crossings.add(i, crossings.get(i));
				}	
			}
			odd = !odd;
			i++;
		}
		if (size()%2 != 0)    // Nasty hack that seems to work...
		{
			System.err.println("RrHalfPlane.solidSet(): odd number of crossings: " +
					crossings.size());
			crossings.remove(size() - 1);
		}
	}

	
	/**
	 * Add ??? if it contains ??? with a parameter within bounds.
	 * @param p
	 * @param q
	 * @param range
	 * @param me
	 * @return true if ??? may be added, otherwise false
	 */
	private boolean maybeAdd(RrHalfPlane p, RrCSGPolygon q, RrInterval range, boolean me)
	{	
		// Ensure no duplicates
		
		for(int i = 0; i < crossings.size(); i++)
		{
			if(getPlane(i) == p)
				return false;     // Because we've already got it
		}
		
		RrInterval newRange = q.box().wipe(pLine(), range);
		if(!newRange.empty())
			try
		{
				double v = p.cross_t(pLine());
				if(v >= newRange.low() && v < newRange.high())
				{
					if(me)
					{
						crossings.add(new lineIntersection(v, q));
						return true;						
					} else
					{
						Rr2Point x = pLine().point(v);
						double r = Math.sqrt(q.resolution2());
						double pot = q.csg().value(x);
						if(pot > -r && pot < r)
						{
							crossings.add(new lineIntersection(v, q));
							return true;
						}
					}
				}
		} catch (RrParallelLineException ple)
		{}
		return false;
	}
	
	/**
	 * Add quad q if it contains a half-plane with an 
	 * intersection with a parameter within bounds.
	 * @param q
	 * @param range
	 * @return true if quad q may be added, otherwise false
	 */
	public boolean maybeAdd(RrCSGPolygon q, RrInterval range)
	{		
		switch(q.csg().operator())
		{
		case NULL:
		case UNIVERSE:
			return false;
		
		case LEAF:
			return maybeAdd(q.csg().plane(), q, range, false);
			
		case INTERSECTION:
		case UNION:	
			if(q.csg().complexity() != 2)
			{
				System.err.println("RrHalfPlane.maybeAdd(): too complex: " + q.csg().complexity());
				return false;
			}
			RrHalfPlane p1 = q.csg().c_1().plane();
			RrHalfPlane p2 = q.csg().c_2().plane();
			if(p1 == this)
				return maybeAdd(p2, q, range, true);
			if(p2 == this)
				return maybeAdd(p1, q, range, true);
			
			boolean b = maybeAdd(p1, q, range, false); 
			b = b | maybeAdd(p2, q, range, false);
			return b;
			
		default:
			System.err.println("RrHalfPlane.maybeAdd(): invalid CSG operator!");
		}
		
		return false;
	}
	
	/**
	 * Add a crossing
	 * @param qc
	 */
	public static boolean cross(RrCSGPolygon qc)
	{		
		if(qc.corner())
		{
			RrInterval range = RrInterval.big_interval();
			boolean b = qc.csg().c_1().plane().maybeAdd(qc, range);
			range = RrInterval.big_interval();
			b = b & qc.csg().c_2().plane().maybeAdd(qc, range);
			return (b);
		}
		System.err.println("RrHalfPlane.cross(): called for non-corner!");
		return false;
	}
	
	/**
	 * Find a crossing
	 * @param q
	 * @return the index of the quad
	 */
	public int find(RrCSGPolygon q)
	{	
		for(int i = 0; i < crossings.size(); i++)
		{
			if(getQuad(i) == q)
				return i;
		}
		System.err.println("RrHalfPlane.find(): quad not found!");
		return -1;
	}
	
	/**
	 * Find the index of a crossing plane
	 * @param h
	 * @return index of the plane
	 */
	public int find(RrHalfPlane h)
	{	
		for(int i = 0; i < crossings.size(); i++)
		{
			if(getPlane(i) == h)
				return i;
		}
		System.err.println("RrHalfPlane.find(): plane not found!");
		return -1;
	}
	
	/**
	 * Remove all crossings
	 */
	public void removeCrossings()
	{
		crossings = new ArrayList();
	}
		
	/**
	 * Remove a crossing from the list
	 * @param i identifier of the crossing to be removed from the list 
	 */
	public void remove(int i)
	{
		crossings.remove(i);
	}
	
	/**
	 * Sort on ascending parameter value.
	 * @param up use an ascending sort when true, descending when false
	 */
	public void sort(boolean up, RrCSGPolygon q)
	{
		if(up)
		{
			java.util.Collections.sort(crossings, 
					new java.util.Comparator() 
					{
				public int compare(Object a, Object b)
				{
					if(((lineIntersection)a).parameter() < 
							((lineIntersection)b).parameter())
						return -1;
					else if (((lineIntersection)a).parameter() > 
					((lineIntersection)b).parameter())
						return 1;
					return 0;
				}
					}
			);
		} else
		{
			java.util.Collections.sort(crossings, 
					new java.util.Comparator() 
					{
				public int compare(Object a, Object b)
				{
					if(((lineIntersection)a).parameter() > 
							((lineIntersection)b).parameter())
						return -1;
					else if (((lineIntersection)a).parameter() < 
					((lineIntersection)b).parameter())
						return 1;
					return 0;
				}
					}
			);
		}		
		if(crossings.size()%2 != 0)
		{
			//System.err.println("RrHalfPlane.sort(): odd number of crossings: " +
					//crossings.size());
			solidSet(q);
		}
	}
	
	
	
	/**
	 * Return the plane as a string
	 * @return string representation
	 */
	public String toString()
	{
		return "|" + normal.toString() + ", " + Double.toString(offset) + "|";
	} 
	
	
	/**
	 * Get the components
	 * @return components?
	 */
	public Rr2Point normal() { return normal; }
	public double offset() { return offset; }
	
	/**
	 * TODO: make this spot complements too.
	 * Is another line the same within a tolerance?
	 * @param a
	 * @param b
	 * @param tolerance
	 * @return true if the distance between halfplane a and b is less then the tolerance, otherwise false
	 */
	public static boolean same(RrHalfPlane a, RrHalfPlane b, double tolerance)
	{
		if(Math.abs(a.normal.x() - b.normal.x()) > tolerance)
			return false;
		if(Math.abs(a.normal.y() - b.normal.y()) > tolerance)
			return false;
		double rms = Math.sqrt((a.offset*a.offset + b.offset*b.offset)*0.5);
		if(Math.abs(a.offset - b.offset) > tolerance*rms)
			return false;
		
		return true;
	}
	
	
	/**
	 * Change the sense
	 * @return complent of half plane
	 */
	public RrHalfPlane complement()
	{
		RrHalfPlane r = new RrHalfPlane(this);
		r.normal = r.normal.neg();
		r.offset = -r.offset;
		r.p = r.p.neg();
		return r;
	}
	
	/**
	 * Move
	 * @param d
	 * @return offset halfplane
	 */
	public RrHalfPlane offset(double d)
	{
		RrHalfPlane r = new RrHalfPlane(this);
		r.offset = r.offset - d;
		r.p = p.offset(d);
		return r;
	}
	
	
	/**
	 * Find the potential value of a point
	 * @param p
	 * @return potential value of point p
	 */
	public double value(Rr2Point p)
	{
		return offset + Rr2Point.mul(normal, p);
	}
	
	
	/**
	 * Find the potential interval of a box
	 * @param b
	 * @return potential interval of box b
	 */
	public RrInterval value(RrBox b)
	{
		return RrInterval.add(RrInterval.add((RrInterval.mul(b.x(), normal.x())), 
				(RrInterval.mul(b.y(), normal.y()))), offset);
	}
	
	/**
	 * The point where another line crosses
	 * @param a
	 * @return cross point
	 * @throws RrParallelLineException
	 */
	public Rr2Point cross_point(RrHalfPlane a) throws RrParallelLineException
	{
		double det = Rr2Point.op(normal, a.normal);
		if(det == 0)
			throw new RrParallelLineException("cross_point: parallel lines.");
		det = 1/det;
		double x = normal.y()*a.offset - a.normal.y()*offset;
		double y = a.normal.x()*offset - normal.x()*a.offset;
		return new Rr2Point(x*det, y*det);
	}
	
	/**
	 * Parameter value where a line crosses
	 * @param a
	 * @return parameter value
	 * @throws RrParallelLineException
	 */
	public double cross_t(RrLine a) throws RrParallelLineException 
	{
		double det = Rr2Point.mul(a.direction(), normal);
		if (det == 0)
			throw new RrParallelLineException("cross_t: parallel lines.");  
		return -value(a.origin())/det;
	}
	
	/**
	 * Point where a parametric line crosses
	 * @param a
	 * @return cross point
	 * @throws RrParallelLineException
	 */
	public Rr2Point cross_point(RrLine a) throws RrParallelLineException
	{
		return a.point(cross_t(a));
	}
	
	/**
	 * Take a range of parameter values and a line, and find
	 * the intersection of that range with the part of the line
	 * (if any) on the solid side of the half-plane.
	 * @param a
	 * @param range
	 * @return intersection interval
	 */
	public RrInterval wipe(RrLine a, RrInterval range)
	{
		if(range.empty()) return range;
		
		// Which way is the line pointing relative to our normal?
		
		boolean wipe_down = (Rr2Point.mul(a.direction(), normal) >= 0);
		
		double t;
		
		try
		{
			t = cross_t(a);
			if (t >= range.high())
			{
				if(wipe_down)
					return range;
				else
					return new RrInterval();
			} else if (t <= range.low())
			{
				if(wipe_down)
					return new RrInterval();
				else
					return range;                
			} else
			{
				if(wipe_down)
					return new RrInterval(range.low(), t);
				else
					return new RrInterval(t, range.high());                 
			}
		} catch (RrParallelLineException ple)
		{
			t = value(a.origin());
			if(t <= 0)
				return range;
			else
				return new RrInterval();  
		}
	}
}