summaryrefslogtreecommitdiff
path: root/tags/host/0.8.1/src/org/reprap/geometry/polygons/RrCSG.java
blob: c935442e2c106b2b9e917f7a15a49f3679e68682 (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
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
/*
 
 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/
 
 =====================================================================
 
 */

package org.reprap.geometry.polygons;

import javax.media.j3d.Link;

/**
 * RepRap Constructive Solid Geometry class
 * 
 * RrCSG: 2D polygons as boolean combinations of half-planes
 * First version 14 November 2005 
 */
public class RrCSG
{
	
	/**
	 * Universal set 
	 */
	private static final RrCSG u = new RrCSG(true);  
	
	/**
	 * Null set  
	 */
	private static final RrCSG n = new RrCSG(false); 
	
	/**
	 * Leaf half plane 
	 */
	private RrHalfPlane hp;
	
	/**
	 * Type of set
	 */
	private RrCSGOp op;

	//private int op;			   ///< Will go at Java 1.5
	
	/**
	 * Non-leaf child operands 
	 */
	private RrCSG c1, c2; 
	
	/**
	 * The complement (if there is one) 
	 */
	private RrCSG comp;        
	
	/**
	 * How much is in here (leaf count)?
	 */
	private int complexity;
	
	/**
	 * Make a leaf from a single half-plane
	 * @param h
	 */
	public RrCSG(RrHalfPlane h)
	{
		hp = new RrHalfPlane(h);
		op = RrCSGOp.LEAF;
		c1 = null;
		c2 = null;
		comp = null;
		complexity = 1;
	}
	
	/**
	 * One off constructor for the universal and null sets
	 * @param b
	 */
	private RrCSG(boolean b)
	{
		hp = null;
		if(b)
			op = RrCSGOp.UNIVERSE;
		else
			op = RrCSGOp.NULL;
		c1 = null;
		c2 = null;
		comp = null;   // Resist temptation to be clever here
		complexity = 0;
	}
	
	/**
	 * Universal or null set
	 * @return universal or null set
	 */
	public static RrCSG universe()
	{
		return u;
	}
	
	/**
	 * @return nothing/null set
	 */
	public static RrCSG nothing()
	{
		return n;
	}
	
	/**
	 * Deep copy
	 * @param c
	 */
	public RrCSG(RrCSG c)
	{
		if(c == u || c == n)
			System.err.println("RrCSG deep copy: copying null or universal set.");
		
		if(c.hp != null)
			hp = new RrHalfPlane(c.hp);
		else
			hp = null;
		
		if(c.c1 != null)
			c1 = new RrCSG(c.c1);
		else
			c1 = null;
		
		if(c.c2 != null)
			c2 = new RrCSG(c.c2);
		else
			c2 = null;
		
		comp = null;  // This'll be built if it's needed
		
		op = c.op;
		complexity = c.complexity;
	}
	
	/**
	 * Get children, operator etc
	 * @return children
	 */
	public RrCSG c_1() { return c1; }
	public RrCSG c_2() { return c2; }
	public RrCSGOp operator() { return op; }
	//public int operator() { return op; }
	public RrHalfPlane plane() { return hp; }
	public int complexity() { return complexity; }
	
	/**
	 * Convert to a string
	 * @param result
	 * @param white
	 * @return string representation
	 */
	private String toString_r(String result, String white)
	{
		switch(op)
		{
		case LEAF:
			result = result + white + hp.toString() + "\n";
			break;
			
		case NULL:
			result = result + white + "0\n";
			break;
			
		case UNIVERSE:
			result = result + white + "U\n";
			break;
			
		case UNION:
			result = result + white + "+\n";
			white = white + " ";
			result = c1.toString_r(result, white);
			result = c2.toString_r(result, white);
			break;
			
		case INTERSECTION:
			result = result + white + "&\n";
			white = white + " ";
			result = c1.toString_r(result, white);
			result = c2.toString_r(result, white);
			break;
			
		default:
			System.err.println("toString_r(): invalid operator.");
		}
		return result;
	}
	
	/* (non-Javadoc)
	 * @see java.lang.Object#toString()
	 */
	public String toString()
	{
		String result = "RrCSG: complexity = " + 
			Integer.toString(complexity) + "\n";
		result = toString_r(result, " ");
		return result;
	}
	
	/**
	 * Private constructor for common work setting up booleans
	 * @param a
	 * @param b
	 */
	private RrCSG(RrCSG a, RrCSG b)
	{
		hp = null;
		comp = null;
		if(a.complexity <= b.complexity) // So we know the 1st child is the simplest
		{
			c1 = a;
			c2 = b;
		} else
		{
			c1 = b;
			c2 = a;
		}
		complexity = c1.complexity + c2.complexity;
	}
	
	/**
	 * Boolean operations, with de Morgan simplifications
	 * @param a
	 * @param b
	 * @return union of passed CSG objects a and b
	 */
	public static RrCSG union(RrCSG a, RrCSG b)
	{
		if(a == b)
			return a;
		if(a.op == RrCSGOp.NULL)
			return b;
		if(b.op == RrCSGOp.NULL)
			return a;
		if((a.op == RrCSGOp.UNIVERSE) || (b.op == RrCSGOp.UNIVERSE))
			return universe();
		
		if(a.comp != null && b.comp != null)
			if(a.comp == b)
				return universe();
		
		RrCSG r = new RrCSG(a, b);
		r.op = RrCSGOp.UNION;
		return r;
	}
	
	/**
	 * Boolean operation to perform an intersection
	 * @param a
	 * @param b
	 * @return intersection of passed CSG objects a and b
	 */
	public static RrCSG intersection(RrCSG a, RrCSG b)
	{
		if(a == b)
			return a;
		if(a.op == RrCSGOp.UNIVERSE)
			return b;
		if(b.op == RrCSGOp.UNIVERSE)
			return a;
		if((a.op == RrCSGOp.NULL) || (b.op == RrCSGOp.NULL))
			return nothing();
		
		if(a.comp != null && b.comp != null)
			if(a.comp == b)
				return nothing();
		
		RrCSG r = new RrCSG(a, b);
		r.op = RrCSGOp.INTERSECTION;
		return r;
	}
	
	/**
	 * Lazy evaluation for complement.
	 * @return completent ?
	 */
	public RrCSG complement()
	{		
		if(comp != null)
			return comp;
		
		RrCSG result;
		
		switch(op)
		{
		case LEAF:
			result = new RrCSG(hp.complement());
			break;
			
		case NULL:
			return universe();
			
		case UNIVERSE:
			return nothing();
			
		case UNION:
			result = intersection(c1.complement(), c2.complement());
			break;
			
		case INTERSECTION:
			result = union(c1.complement(), c2.complement());
			break;
			
		default:
			System.err.println("complement(): invalid operator.");
			return nothing();
		}
		
		// Remember, so we don't have to do it again.
		// (I do hope that the Java garbage collector is up to 
		// spotting this deadly embrace, or we - I mean it - has
		// a memory leak.)
		
		comp = result;
		result.comp = this;
		
		return comp;
	}
	
	/**
	 * Set difference is intersection with complement
	 * @param a
	 * @param b
	 * @return set difference as CSG object base don input CSG objects a and b
	 */		
	public static RrCSG difference(RrCSG a, RrCSG b)
	{
		return intersection(a, b.complement());
	}
	
	
	/**
	 * Regularise a set with a contents of 3
	 * This assumes simplify has been run over this set
	 * @return regularised CSG object
	 */	
	private RrCSG reg_3()
	{
		RrCSG result = this;
		
		if(complexity != 3)
			return result;
		
		boolean different = true;
		boolean acomp = false;
		RrCSG temp;
		if(c1 == c2.c1)
			different = false;
		if(c1 == c2.c2)
		{
			different = false;
			temp = c2.c2;
			c2.c2 = c2.c1;
			c2.c1 = temp;
		}
		
		if(c1.comp != null)
		{
			if(c1.comp == c2.c1)
			{
				different = false;
				acomp = true;
			}
			if(c1.comp == c2.c2) 
			{
				different = false;
				acomp = true;
				temp = c2.c2;
				c2.c2 = c2.c1;
				c2.c1 = temp;
			}
		}
		
		if(different)
			return result;
		
		if(acomp)
		{
			if(op == RrCSGOp.UNION) 
			{
				if(c2.op == RrCSGOp.UNION)
					result = universe();
				else
					result = union(c1, c2.c2);
			}else 
			{
				if(c2.op == RrCSGOp.UNION)
					result = intersection(c1, c2.c2);
				else
					result = nothing();
			}            
		} else
		{
			if(op == RrCSGOp.UNION) 
			{
				if(c2.op == RrCSGOp.UNION)
					result = c2;
				else
					result = c1;
			}else 
			{
				if(c2.op == RrCSGOp.UNION)
					result = c1;
				else
					result = c2;
			}
		}
		return result;
	}
	
	
	/**
	 * FIXME: there's a bug in reg_4 somewhere.  So it's not called at present.
	 * Regularise a set with a contents of 4
	 * This assumes simplify has been run over the set
	 * @return regularised CSG object
	 */	
	private RrCSG reg_4()
	{            
		RrCSG result = this;
		
		if(complexity != 4)
			return result;
		
		RrCSG temp;	
		
		if(c1.complexity == 1)
		{
			temp = c2.reg_3();
			if(temp.complexity <= 2)
			{
				if(op == RrCSGOp.UNION)
					result = union(c1, temp).reg_3();
				else
					result = intersection(c1, temp).reg_3();
			}else
			{
				// c1 can only equal at most one leaf of c2 as all three c2 leaves
				// must be distinct because reg_3() didn't simplify c2.
				
				if(c1 == c2.c1)
				{
					result = c1;
				}
				else if(c1 == c2.c2.c1 || c1 == c2.c2.c2)
				{
					if(c1 == c2.c2.c2)
						c2.c2.c2 = c2.c2.c1;
					int ops = 0;
					if(op == RrCSGOp.UNION)
						ops++;
					if(c2.op == RrCSGOp.UNION)
						ops += 2;
					if(c2.c2.op == RrCSGOp.UNION)
						ops += 4;
					switch(ops)
					{
					case 0:
						result = c2;
						break;
					case 1:
					case 6:
						result = c1;
						break;
					case 2:
					case 5:
					case 7:
						result.c2.c2 = c2.c2.c2;
						break;                            
					case 3:
					case 4:
						result.c2 = c2.c1;
						break;
					default:
						System.err.println("reg_4() 1: addition doesn't work...");
					}  
				}
			}
		} else
		{
			int type = 0;
			if(c1.c1 == c2.c1)
				type++;
			else if(c1.c1 == c2.c2)
			{
				type++;
				temp = c2.c2;
				c2.c2 = c2.c1;
				c2.c1 = temp;
			}
			if(c1.c2 == c2.c2)
				type++;
			else if(c1.c2 == c2.c1)
			{
				type++;
				temp = c1.c2;
				c1.c2 = c1.c1;
				c1.c1 = temp;
			}
			
			int ops = 0;
			if(op == RrCSGOp.UNION)
				ops += 4;
			if(c1.op == RrCSGOp.UNION)
				ops++;
			if(c2.op == RrCSGOp.UNION)
				ops += 2;
			
			switch(type)
			{
			case 0:
				break;
				
			case 1:
				switch(ops)
				{
				case 0:
					result = intersection(c1, c2.c2);
					break;
				case 1:
					result = intersection(c1.c1, c2.c2);
					break;
				case 2:
				case 5:
					result = c1;
				case 3:
					result = union(c1.c1, intersection(c1.c2, c2.c2));
					break;
				case 4:
					result = intersection(c1.c1, union(c1.c2, c2.c2));
					break;
				case 6:
					result = union(c1.c1, c2.c2);
					break;
				case 7:
					result = union(c1, c2.c2);
					break;
				default:
					System.err.println("reg_4() 2: addition doesn't work...");
				}
				break;
				
			case 2:		// Pick the child that's an intersection (if there is one)		
				if(c1.op == RrCSGOp.UNION)
					result = c2;
				else
					result = c1;
				break;
				
			default:
				System.err.println("reg_4() 4: addition doesn't work...");
			}
		}
		
		return result;
	}
	
	/**
	 * Regularise a set with simple contents ( < 4 )
	 * This assumes simplify has been run over the set
	 * @return regularised CSG object
	 */	
	public RrCSG regularise()
	{
		RrCSG result = this;

		switch(complexity)
		{
		case 0:
		case 1:
		case 2:
			break;
		case 3:
			result = reg_3();
//			if(result.complexity < complexity)
//				System.out.println("regularise: \n" + toString() + " > " + 
//						result.toString());
			break;
			
		case 4:
			//result = reg_4();
//			if(result.complexity < complexity)
//				System.out.println("regularise: \n" + toString() + " > " + 
//						result.toString());
			break;
			
		default:
			System.err.println("regularise(): set too complicated.");
		}
		
		return result;
	}
	
	/**
	 * Replace duplicate of leaf with leaf itself
	 * TODO: this should also use known complements
	 * @param leaf
	 * @param tolerance
	 */		
	private void replace_all_same_leaves(RrCSG leaf, double tolerance)
	{	
		switch(op)
		{
		case LEAF:
		case NULL:   
		case UNIVERSE:
			//System.out.println("replace_all_same_leaves(): at a leaf!");
			break;
			
		case UNION:
		case INTERSECTION:    
			RrHalfPlane hp = leaf.hp;
			if(c1.op == RrCSGOp.LEAF)
			{
				if(RrHalfPlane.same(hp, c1.hp, tolerance))
					c1 = leaf;
			} else
				c1.replace_all_same_leaves(leaf, tolerance);
			
			if(c2.op == RrCSGOp.LEAF)
			{
				if(RrHalfPlane.same(hp, c2.hp, tolerance))
					c2 = leaf;                        
			} else
				c2.replace_all_same_leaves(leaf, tolerance);
			break;
			
		default:
			System.err.println("replace_all_same(): invalid operator.");		
		}
	}
	
	/**
	 * Replace duplicate of all leaves with the last instance of each
	 * @param root
	 * @param tolerance
	 * @return simplified CSG object
	 */		
	private void simplify_r(RrCSG root, double tolerance)
	{
		switch(op)
		{
		case LEAF:
		case NULL:   
		case UNIVERSE:
			//System.out.println("simplify_r(): at a leaf!");
			break;
			
		case UNION:
		case INTERSECTION:    
			if(c1.op == RrCSGOp.LEAF)
				root.replace_all_same_leaves(c1, tolerance);
			else
				c1.simplify_r(root, tolerance);
			
			if(c2.op == RrCSGOp.LEAF)
				root.replace_all_same_leaves(c2, tolerance);
			else
				c2.simplify_r(root, tolerance);
			
			break;
			
		default:
			System.err.println("simplify_r(): invalid operator.");
		
		}
	}
	
	/**
	 * Replace duplicate of all leaves with the last instance of each
	 * @param tolerance
	 * @return simplified CSG object
	 */		
	public RrCSG simplify(double tolerance)
	{
		RrCSG root = new RrCSG(this);
		simplify_r(root, tolerance);
		return root;
	}
	
	/**
	 * For each half plane remove any existing crossing list.
     */
    public void clearCrossings()
    {
    	if(complexity() > 1)
    	{
    		c_1().clearCrossings();
    		c_2().clearCrossings();
    	} else
    	{
    		if(operator() == RrCSGOp.LEAF)
    			plane().removeCrossings();
    	}
    }
    
    
    /**
	 * For each half plane sort the crossing list.
     */
    public void sortCrossings(boolean up, RrCSGPolygon q)
    {
    	if(complexity() > 1)
    	{
    		c_1().sortCrossings(up, q);
    		c_2().sortCrossings(up, q);
    	} else
    	{
    		if(operator() == RrCSGOp.LEAF)
    			plane().sort(up, q);
    	}	
    }
	
	/**
	 * Offset by a distance (+ve or -ve)
	 * TODO: this should keep track of complements
	 * @param d
	 * @return offset CSG object by distance d
	 */
	public RrCSG offset(double d)
	{
		RrCSG result;
		
		switch(op)
		{
		case LEAF:
			result = new RrCSG(hp.offset(d));
			break;
			
		case NULL:
		case UNIVERSE:
			result = this;
			break;
			
		case UNION:
			result = union(c1.offset(d), c2.offset(d));
			break;
			
		case INTERSECTION:
			result = intersection(c1.offset(d), c2.offset(d));
			break;
			
		default:
			System.err.println("offset(): invalid operator.");
			result = nothing();
		}
		return result;
	}
	
	
	/**
	 * leaf find the half-plane that generates the value for a point
	 * @param p
	 * @return leaf?
	 */
	public RrCSG leaf(Rr2Point p)
	{
		RrCSG result, r1, r2;
		
		switch(op)
		{
		case LEAF:
			result = this;
			break;
			
		case NULL:
			result = this;
			break;
			
		case UNIVERSE:
			result = this;
			break;
			
		case UNION:
			r1 = c1.leaf(p);
			r2 = c2.leaf(p);
			if(r1.value(p) < r2.value(p))
				return r1;
			else
				return r2;
			
		case INTERSECTION:
			r1 = c1.leaf(p);
			r2 = c2.leaf(p);
			if(r1.value(p) > r2.value(p))
				return r1;
			else
				return r2;
			
		default:
			System.err.println("leaf(Rr2Point): invalid operator.");
			result = nothing();
		}
		return result;
	}
	
	/**
	 * "Potential" value of a point; i.e. a membership test
	 * -ve means inside; 0 means on the surface; +ve means outside
	 * TODO - this should work independently of a call to leaf(); that's more efficient
	 * @param p
	 * @return value of a point
	 */
	public double value(Rr2Point p)
	{
		double result = 1;
		RrCSG c = leaf(p);
		switch(c.op)
		{
		case LEAF:
			result = c.hp.value(p);
			break;
			
		case NULL:
			result = 1;
			break;
			
		case UNIVERSE:
			result = -1;
			break;
			
		case UNION:
		case INTERSECTION:
			
		default:
			System.err.println("value(Rr2Point): non-leaf operator.");
		}
		return result;
	}
	
	/**
	 * The interval value of a box (analagous to point)
	 * @param b
	 * @return value of a box
	 */
	public RrInterval value(RrBox b)
	{
		RrInterval result;
		
		switch(op)
		{
		case LEAF:
			result = hp.value(b);
			break;
			
		case NULL:
			result = new RrInterval(1, 1.01);  // Is this clever?  Or dumb?
			break;
			
		case UNIVERSE:
			result = new RrInterval(-1.01, -1);  // Ditto.
			break;
			
		case UNION:
			result = RrInterval.min(c1.value(b), c2.value(b));
			break;
			
		case INTERSECTION:
			result = RrInterval.max(c1.value(b), c2.value(b));
			break;
			
		default:
			System.err.println("value(RrBox): invalid operator.");
			result = new RrInterval();
		}
		
		return result;
	}
	
	/**
	 * Prune the set to a box
	 * @param b
	 * @return pruned box as new CSG object
	 */
	public RrCSG prune(RrBox b)
	{
		RrCSG result = this;
		
		switch(op)
		{
		case LEAF:            
			RrInterval i = hp.value(b);
			if (i.empty())
				System.err.println("RrCSG.prune(RrBox): empty interval!");
			else if(i.neg())
				result = universe();
			else if (i.pos())
				result = nothing();
			break;
			
		case NULL:
		case UNIVERSE:
			break;
			
		case UNION:
			result =  union(c1.prune(b), c2.prune(b));
			break;
			
		case INTERSECTION:
			result = intersection(c1.prune(b), c2.prune(b));
			break;
			
		default:
			System.err.println("RrCSG.prune(RrBox): dud op value!");
		}
		
		return result;
	}
}