Skip to content

Commit 24ceb5f

Browse files
committed
PR03
1 parent 6c84354 commit 24ceb5f

File tree

5 files changed

+237
-7
lines changed

5 files changed

+237
-7
lines changed

src/geometries/Geometries.java

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package geometries;
2+
3+
import primitives.Point;
4+
import primitives.Ray;
5+
6+
import java.util.LinkedList;
7+
import java.util.List;
8+
9+
public class Geometries implements Intersectable {
10+
private final List<Intersectable> geometries;
11+
12+
public Geometries() {
13+
this.geometries = new LinkedList<>();
14+
}
15+
16+
public Geometries(Intersectable... geometries) {
17+
this.geometries = List.of(geometries);
18+
}
19+
20+
public void add(Intersectable... geometries) {
21+
this.geometries.addAll(List.of(geometries));
22+
}
23+
24+
@Override
25+
public List<Point> findIntersections(Ray ray) {
26+
List<Point> result = null;
27+
28+
for (Intersectable geometry: this.geometries) {
29+
List<Point> points = geometry.findIntersections(ray);
30+
31+
if (points == null)
32+
continue;
33+
34+
if (result == null)
35+
result = new LinkedList<>(points);
36+
else
37+
result.addAll(points);
38+
}
39+
40+
return result;
41+
}
42+
}

src/geometries/Triangle.java

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,18 +23,19 @@ public Triangle(Point p1, Point p2, Point p3) {
2323
super(p1, p2, p3);
2424
}
2525

26-
// TODO: Maybe remove if imp in Polygon
2726
@Override
2827
public List<Point> findIntersections(Ray ray) {
29-
Vector v1 = super.vertices.get(1).subtract(super.vertices.get(0));
30-
Vector v2 = super.vertices.get(2).subtract(super.vertices.get(0));
31-
Vector v3 = super.vertices.get(3).subtract(super.vertices.get(0));
28+
Point P0 = ray.getStartPoint();
29+
Vector v = ray.getDirection();
30+
31+
Vector v1 = super.vertices.get(0).subtract(P0);
32+
Vector v2 = super.vertices.get(1).subtract(P0);
33+
Vector v3 = super.vertices.get(2).subtract(P0);
34+
3235
Vector n1 = v1.crossProduct(v2).normalize();
3336
Vector n2 = v2.crossProduct(v3).normalize();
3437
Vector n3 = v3.crossProduct(v1).normalize();
3538

36-
Vector v = ray.getDirection();
37-
3839
if (!(v.dotProduct(n1) > 0 && v.dotProduct(n2) > 0 && v.dotProduct(n3) > 0 ||
3940
v.dotProduct(n1) < 0 && v.dotProduct(n2) < 0 && v.dotProduct(n3) < 0))
4041
return null;

src/primitives/Vector.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,10 @@ public class Vector extends Point {
1414
* @param z third number value
1515
*/
1616
public Vector(double x, double y, double z) {
17-
this(new Double3(x, y, z)); // May impact performance!!!
17+
super(x,y,z);
18+
19+
if (xyz.equals(Double3.ZERO))
20+
throw new IllegalArgumentException("The Zero Vector is illegal");
1821
}
1922

2023
/**
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
package geometries;
2+
3+
import org.junit.jupiter.api.Test;
4+
import primitives.*;
5+
6+
import java.util.List;
7+
8+
import static org.junit.jupiter.api.Assertions.*;
9+
10+
class GeometriesTest {
11+
@Test
12+
void testFindIntersections() {
13+
Geometries geometries = new Geometries(
14+
new Plane(new Point(1, 1, 1), new Vector(1, 1, 1)),
15+
new Sphere(new Point(1, 1, 0), 2),
16+
new Triangle(new Point(2, 0, 0), new Point(2, 2, 0), new Point(0, 2, 0))
17+
);
18+
Ray ray = new Ray(new Point(1, 1.5, 4), new Vector(0.5, 0, -1));
19+
20+
// ============ Equivalence Partitions Tests ==============
21+
// ray intersect with some geometries
22+
List<Point> result = geometries.findIntersections(ray);
23+
assertEquals(
24+
3,
25+
result.size(),
26+
"EP01: Wrong intersections"
27+
);
28+
29+
// ============= Boundary Values Tests =================
30+
// BVA01: geometries is empty
31+
assertNull(new Geometries().findIntersections(ray), "BVA01: Empty geometries");
32+
33+
// BVA02: ray intersect with no geometries
34+
ray = new Ray(new Point(1, 1.5, 4), new Vector(0, -1, 1));
35+
result = geometries.findIntersections(ray);
36+
assertNull(
37+
result,
38+
"BVA02: Wrong intersections"
39+
);
40+
41+
// BVA03: ray intersect with one geometry only
42+
ray = new Ray(new Point(1, 1.5, 4), new Vector(0, -1, 0));
43+
result = geometries.findIntersections(ray);
44+
assertEquals(
45+
1,
46+
result.size(),
47+
"BVA03: find intersection not working");
48+
49+
50+
// BVA04: ray intersect with all geometries
51+
ray = new Ray(new Point(1, 1.5, 4), new Vector(0, 0, -1));
52+
53+
result = geometries.findIntersections(ray);
54+
assertEquals(
55+
4,
56+
result.size(),
57+
"BVA04: Wrong intersections"
58+
);
59+
}
60+
}
Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
1+
package geometries;
2+
3+
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
4+
import static org.junit.jupiter.api.Assertions.assertEquals;
5+
import static org.junit.jupiter.api.Assertions.assertThrows;
6+
import static org.junit.jupiter.api.Assertions.assertTrue;
7+
import static org.junit.jupiter.api.Assertions.fail;
8+
import static primitives.Util.isZero;
9+
10+
import org.junit.jupiter.api.Test;
11+
12+
import primitives.Point;
13+
import primitives.Vector;
14+
15+
/** Testing Polygons
16+
* @author Dan */
17+
public class PolygonTests {
18+
19+
/** Test method for {@link geometries.Polygon#Polygon(primitives.Point...)}. */
20+
@Test
21+
public void testConstructor() {
22+
// ============ Equivalence Partitions Tests ==============
23+
24+
// TC01: Correct concave quadrangular with vertices in correct order
25+
try {
26+
new Polygon(
27+
new Point(0, 0, 1),
28+
new Point(1, 0, 0),
29+
new Point(0, 1, 0),
30+
new Point(-1, 1, 1)
31+
);
32+
} catch (IllegalArgumentException e) {
33+
fail("Failed constructing a correct polygon");
34+
}
35+
36+
// TC02: Wrong vertices order
37+
assertThrows(IllegalArgumentException.class, //
38+
() -> new Polygon(
39+
new Point(0, 0, 1),
40+
new Point(0, 1, 0),
41+
new Point(1, 0, 0),
42+
new Point(-1, 1, 1)
43+
), //
44+
"Constructed a polygon with wrong order of vertices");
45+
46+
// TC03: Not in the same plane
47+
assertThrows(IllegalArgumentException.class, //
48+
() -> new Polygon(
49+
new Point(0, 0, 1),
50+
new Point(1, 0, 0),
51+
new Point(0, 1, 0),
52+
new Point(0, 2, 2)
53+
), //
54+
"Constructed a polygon with vertices that are not in the same plane");
55+
56+
// TC04: Concave quadrangular
57+
assertThrows(IllegalArgumentException.class, //
58+
() -> new Polygon(
59+
new Point(0, 0, 1),
60+
new Point(1, 0, 0),
61+
new Point(0, 1, 0),
62+
new Point(0.5, 0.25, 0.5)
63+
), //
64+
"Constructed a concave polygon");
65+
66+
// =============== Boundary Values Tests ==================
67+
68+
// TC10: Vertex on a side of a quadrangular
69+
assertThrows(IllegalArgumentException.class, //
70+
() -> new Polygon(
71+
new Point(0, 0, 1),
72+
new Point(1, 0, 0),
73+
new Point(0, 1, 0),
74+
new Point(0, 0.5, 0.5)
75+
),
76+
"Constructed a polygon with vertices on a side");
77+
78+
// TC11: Last point = first point
79+
assertThrows(IllegalArgumentException.class, //
80+
() -> new Polygon(
81+
new Point(0, 0, 1),
82+
new Point(1, 0, 0),
83+
new Point(0, 1, 0),
84+
new Point(0, 0, 1)
85+
),
86+
"Constructed a polygon with vertices on a side");
87+
88+
// TC12: Co-located points
89+
assertThrows(IllegalArgumentException.class, //
90+
() -> new Polygon(
91+
new Point(0, 0, 1),
92+
new Point(1, 0, 0),
93+
new Point(0, 1, 0),
94+
new Point(0, 1, 0)
95+
),
96+
"Constructed a polygon with vertices on a side");
97+
98+
}
99+
100+
/** Test method for {@link geometries.Polygon#getNormal(primitives.Point)}. */
101+
@Test
102+
public void testGetNormal() {
103+
// ============ Equivalence Partitions Tests ==============
104+
// TC01: There is a simple single test here - using a quad
105+
Point[] pts =
106+
{
107+
new Point(0, 0, 1),
108+
new Point(1, 0, 0),
109+
new Point(0, 1, 0),
110+
new Point(-1, 1, 1)
111+
};
112+
Polygon pol = new Polygon(pts);
113+
// ensure there are no exceptions
114+
assertDoesNotThrow(() -> pol.getNormal(new Point(0, 0, 1)), "");
115+
// generate the test result
116+
Vector result = pol.getNormal(new Point(0, 0, 1));
117+
// ensure |result| = 1
118+
assertEquals(1, result.length(), 0.00000001, "Polygon's normal is not a unit vector");
119+
// ensure the result is orthogonal to all the edges
120+
for (int i = 0; i < 3; ++i)
121+
assertTrue(isZero(result.dotProduct(pts[i].subtract(pts[i == 0 ? 3 : i - 1]))),
122+
"Polygon's normal is not orthogonal to one of the edges");
123+
}
124+
}

0 commit comments

Comments
 (0)