Skip to content

Commit 9bbfe3b

Browse files
committed
refactor(tree): add tests for boundary
1 parent 457c93f commit 9bbfe3b

11 files changed

Lines changed: 216 additions & 7 deletions

File tree

src/main/java/labs/introtoprogramming/lab5/object/OptimizedObject.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,12 @@
55
import labs.introtoprogramming.lab5.scene.Transform;
66
import labs.introtoprogramming.lab5.tree.KDTree;
77

8-
import java.util.ArrayList;
8+
import java.util.List;
99

1010
public class OptimizedObject extends SceneObject {
1111
private KDTree tree;
1212

13-
public OptimizedObject(Transform transform, ArrayList<SceneObject> objects) {
13+
public OptimizedObject(Transform transform, List<SceneObject> objects) {
1414
super(transform);
1515
tree = new KDTree(objects);
1616
}

src/main/java/labs/introtoprogramming/lab5/object/Sphere.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,6 @@ public boolean intersect(Ray ray) {
5353
public Box getBoundary() {
5454
Vector3 pos = transform.position();
5555
Vector3 size = Vector3.ONE.multiply(radius);
56-
return new Box(new Transform(), pos.add(size), pos.add(size.multiply(-1)));
56+
return new Box(new Transform(), pos.subtract(size), pos.add(size));
5757
}
5858
}

src/main/java/labs/introtoprogramming/lab5/tree/KDTree.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
import java.util.List;
1111

1212
public class KDTree {
13-
private static double MIN_NUMBER_OF_OBJECTS = 3;
13+
private static final double MIN_NUMBER_OF_OBJECTS = 3;
1414
private KDNode root;
1515
private SceneObject intersection;
1616

@@ -41,6 +41,12 @@ private Box getBoundary(List<SceneObject> objects) {
4141
Math.max(upperBound.z, boundary.upperBounds.z)
4242
);
4343
}
44+
if (lowerBound.equals(Vector3.ONE.multiply(Double.MAX_VALUE))) {
45+
lowerBound = Vector3.ONE.multiply(Double.MIN_VALUE);
46+
}
47+
if (upperBound.equals(Vector3.ONE.multiply(Double.MIN_VALUE))) {
48+
upperBound = Vector3.ONE.multiply(Double.MAX_VALUE);
49+
}
4450
return new Box(new Transform(), lowerBound, upperBound);
4551
}
4652

src/test/java/labs/introtoprogramming/lab5/object/BoxTests.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import static org.junit.Assert.assertEquals;
44
import static org.junit.Assert.assertFalse;
5+
import static org.junit.Assert.assertNull;
56
import static org.junit.Assert.assertTrue;
67

78
import labs.introtoprogramming.lab5.geometry.Ray;
@@ -81,4 +82,12 @@ public void testIntersectionMinusZeroDirection() {
8182
Box box = new Box(new Transform(Vector3.RIGHT.multiply(2)));
8283
assertFalse(box.intersect(ray));
8384
}
85+
86+
@Test
87+
public void testBoundary() {
88+
Box box = new Box(new Transform(Vector3.RIGHT.multiply(2)));
89+
Box boundary = box.getBoundary();
90+
assertEquals(box.upperBounds, boundary.upperBounds);
91+
assertEquals(box.lowerBounds, boundary.lowerBounds);
92+
}
8493
}

src/test/java/labs/introtoprogramming/lab5/object/DiskTests.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import static org.junit.Assert.assertEquals;
44
import static org.junit.Assert.assertFalse;
5+
import static org.junit.Assert.assertNull;
56
import static org.junit.Assert.assertTrue;
67

78
import labs.introtoprogramming.lab5.geometry.Ray;
@@ -43,4 +44,10 @@ public void testGreaterThanRadius() {
4344
assertFalse(disk.intersect(ray));
4445
assertEquals(1, ray.getScale(), DELTA);
4546
}
47+
48+
@Test
49+
public void testBoundary() {
50+
Disk disk = new Disk(new Transform());
51+
assertNull(disk.getBoundary());
52+
}
4653
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package labs.introtoprogramming.lab5.object;
2+
3+
import static org.junit.Assert.assertEquals;
4+
import static org.junit.Assert.assertTrue;
5+
6+
import java.util.Arrays;
7+
import java.util.List;
8+
9+
import labs.introtoprogramming.lab5.geometry.Ray;
10+
import labs.introtoprogramming.lab5.geometry.Vector3;
11+
import labs.introtoprogramming.lab5.scene.SceneObject;
12+
import labs.introtoprogramming.lab5.scene.Transform;
13+
import org.junit.Test;
14+
15+
public class OptimizedObjectTests {
16+
private static final double DELTA = 1e-10;
17+
18+
private static List<SceneObject> DUMMY_OBJECTS = Arrays.asList(
19+
new Box(new Transform(), Vector3.ONE.multiply(-5), Vector3.ONE.multiply(5)),
20+
new Triangle(new Transform(), Vector3.RIGHT, Vector3.RIGHT.multiply(-1), Vector3.UP),
21+
new Sphere(new Transform(), 1)
22+
);
23+
24+
@Test
25+
public void testInstantiation() {
26+
OptimizedObject obj = new OptimizedObject(new Transform(), DUMMY_OBJECTS);
27+
Box boundary = obj.getBoundary();
28+
Box expected = DUMMY_OBJECTS.get(0).getBoundary();
29+
assertEquals(expected.lowerBounds, boundary.lowerBounds);
30+
assertEquals(expected.upperBounds, boundary.upperBounds);
31+
}
32+
33+
@Test
34+
public void testIntersection() {
35+
OptimizedObject obj = new OptimizedObject(new Transform(), DUMMY_OBJECTS);
36+
Ray ray = new Ray(Vector3.FORWARD.multiply(2), Vector3.FORWARD.multiply(-1));
37+
assertTrue(obj.intersect(ray));
38+
assertEquals(1, ray.getScale(), DELTA);
39+
}
40+
}

src/test/java/labs/introtoprogramming/lab5/object/PlaneTests.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import static org.junit.Assert.assertEquals;
44
import static org.junit.Assert.assertFalse;
5+
import static org.junit.Assert.assertNull;
56
import static org.junit.Assert.assertTrue;
67

78
import labs.introtoprogramming.lab5.geometry.Ray;
@@ -43,4 +44,10 @@ public void testBelowRayOrigin() {
4344
assertFalse(plane.intersect(ray));
4445
assertEquals(1, ray.getScale(), DELTA);
4546
}
47+
48+
@Test
49+
public void testBoundary() {
50+
Plane plane = new Plane(new Transform());
51+
assertNull(plane.getBoundary());
52+
}
4653
}

src/test/java/labs/introtoprogramming/lab5/object/SphereTests.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,4 +43,12 @@ public void testIntersectionOppositeDirection() {
4343
assertFalse(sphere.intersect(ray));
4444
assertEquals(1, ray.getScale(), DELTA);
4545
}
46+
47+
@Test
48+
public void testBoundary() {
49+
Sphere sphere = new Sphere(new Transform(), 5);
50+
Box boundary = sphere.getBoundary();
51+
assertEquals(Vector3.ONE.multiply(5), boundary.upperBounds);
52+
assertEquals(Vector3.ONE.multiply(-5), boundary.lowerBounds);
53+
}
4654
}

src/test/java/labs/introtoprogramming/lab5/object/TriangleTests.java

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,4 +65,21 @@ public void testNoIntersectionOutOfBounds() {
6565
assertFalse(triangle.intersect(ray));
6666
assertEquals(1, ray.getScale(), DELTA);
6767
}
68+
69+
@Test
70+
public void testNoIntersectionOppositeDirection() {
71+
Ray ray = new Ray(Vector3.ZERO, Vector3.FORWARD);
72+
Triangle triangle = new Triangle(new Transform(Vector3.FORWARD.multiply(-9)),
73+
Vector3.RIGHT, Vector3.UP, Vector3.RIGHT.multiply(-1));
74+
assertFalse(triangle.intersect(ray));
75+
assertEquals(1, ray.getScale(), DELTA);
76+
}
77+
78+
@Test
79+
public void testBoundary() {
80+
Triangle triangle = new Triangle(new Transform(), Vector3.RIGHT, Vector3.FORWARD, Vector3.UP);
81+
Box boundary = triangle.getBoundary();
82+
assertEquals(Vector3.ONE, boundary.upperBounds);
83+
assertEquals(Vector3.ZERO, boundary.lowerBounds);
84+
}
6885
}

src/test/java/labs/introtoprogramming/lab5/tree/KDNodeTests.java

Lines changed: 48 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919

2020
public class KDNodeTests {
2121
private static List<SceneObject> DUMMY_OBJECTS = Arrays.asList(
22-
new Box(new Transform(), Vector3.ONE, Vector3.ONE.multiply(-1)),
22+
new Box(new Transform(), Vector3.ONE.multiply(-5), Vector3.ONE.multiply(5)),
2323
new Triangle(new Transform(), Vector3.RIGHT, Vector3.RIGHT.multiply(-1), Vector3.UP),
2424
new Sphere(new Transform(), 1)
2525
);
@@ -67,4 +67,51 @@ public void testIntersectionOwnObjectIntersection() {
6767
new KDNode[0]);
6868
assertEquals(DUMMY_OBJECTS.get(2), node.intersect(new Ray(Vector3.FORWARD, Vector3.UP)));
6969
}
70+
71+
@Test
72+
public void testIntersectionEmptyChild() {
73+
KDNode child = new KDNode(Collections.emptyList(),
74+
new Box(new Transform(), Vector3.ONE.multiply(-5), Vector3.ONE.multiply(5)),
75+
new KDNode[0]);
76+
KDNode node = new KDNode(Collections.emptyList(),
77+
new Box(new Transform(), Vector3.ONE.multiply(-5), Vector3.ONE.multiply(5)),
78+
new KDNode[] { child });
79+
assertNull(node.intersect(new Ray(Vector3.FORWARD, Vector3.UP)));
80+
}
81+
82+
@Test
83+
public void testIntersectionNullChild() {
84+
KDNode node = new KDNode(Collections.emptyList(),
85+
new Box(new Transform(), Vector3.ONE.multiply(-5), Vector3.ONE.multiply(5)),
86+
new KDNode[2]);
87+
assertNull(node.intersect(new Ray(Vector3.FORWARD, Vector3.UP)));
88+
}
89+
90+
@Test
91+
public void testIntersectionNoChildIntersection() {
92+
KDNode child = new KDNode(DUMMY_OBJECTS,
93+
new Box(new Transform(), Vector3.ONE.multiply(-5), Vector3.ONE.multiply(5)),
94+
new KDNode[0]);
95+
KDNode node = new KDNode(Collections.emptyList(),
96+
new Box(new Transform(), Vector3.ONE.multiply(-5), Vector3.ONE.multiply(5)),
97+
new KDNode[] { child });
98+
assertNull(node.intersect(new Ray(Vector3.FORWARD.multiply(100), Vector3.FORWARD)));
99+
}
100+
101+
@Test
102+
public void testIntersectionMultyChildIntersection() {
103+
KDNode first = new KDNode(Collections.singletonList(DUMMY_OBJECTS.get(0)),
104+
new Box(new Transform(), Vector3.ONE.multiply(-5), Vector3.ONE.multiply(5)),
105+
new KDNode[0]);
106+
KDNode second = new KDNode(Collections.singletonList(DUMMY_OBJECTS.get(1)),
107+
new Box(new Transform(), Vector3.ONE.multiply(-5), Vector3.ONE.multiply(5)),
108+
new KDNode[0]);
109+
KDNode third = new KDNode(Collections.singletonList(DUMMY_OBJECTS.get(2)),
110+
new Box(new Transform(), Vector3.ONE.multiply(-5), Vector3.ONE.multiply(5)),
111+
new KDNode[0]);
112+
KDNode node = new KDNode(Collections.emptyList(),
113+
new Box(new Transform(), Vector3.ONE.multiply(-5), Vector3.ONE.multiply(5)),
114+
new KDNode[] { first, second, third });
115+
assertEquals(DUMMY_OBJECTS.get(2), node.intersect(new Ray(Vector3.FORWARD, Vector3.UP)));
116+
}
70117
}

0 commit comments

Comments
 (0)