diff --git a/cameraproject/src/geometries/Plane.java b/cameraproject/src/geometries/Plane.java index 2199290..3732b06 100644 --- a/cameraproject/src/geometries/Plane.java +++ b/cameraproject/src/geometries/Plane.java @@ -1,4 +1,5 @@ package geometries; + import static primitives.Util.*; import java.util.List; @@ -71,17 +72,17 @@ public String toString() { @Override public List findIntersections(Ray ray) { - - if(q0.equals(ray.getP0())) + + if (q0.equals(ray.getP0())) return null; - - double mone = normal.dotProduct(q0.subtract(ray.getP0())) ,mechane =(normal.dotProduct(ray.getDir())); - if( isZero(mone)|| isZero(mechane)) + + double mone = normal.dotProduct(q0.subtract(ray.getP0())), mechane = (normal.dotProduct(ray.getDir())); + if (isZero(mone) || isZero(mechane)) return null; - double t = mone/mechane; - if(t<0) + double t = mone / mechane; + if (t < 0) return null; return List.of(ray.getPoint(t)); - + } } diff --git a/cameraproject/src/unittests/geometries/PlaneTests.java b/cameraproject/src/unittests/geometries/PlaneTests.java index 4da0e7d..ea8a7fe 100644 --- a/cameraproject/src/unittests/geometries/PlaneTests.java +++ b/cameraproject/src/unittests/geometries/PlaneTests.java @@ -26,47 +26,50 @@ public void testGetNormalPoint3D() { assertEquals("getNormal() Mistake found with GetNormal", p.getNormal(new Point3D(7, -2, 1)), new Vector(1, 2, 3).normalize()); } - + + /** + * Test method for {@link geometries.Plane#findIntersections(primitives.Ray)}. + */ @Test public void findIntersections() { Plane p = new Plane(new Point3D(0, 0, 0), new Vector(0, 0, 1)); List expected; - - //EP01:Ray intersects with plane + + // EP01:Ray intersects with plane expected = p.findIntersections(new Ray(new Point3D(-1, -1, -1), new Vector(1, -1, 1))); assertEquals("Wrong number of points", 1, expected.size()); - assertEquals("Wrong points expected",List.of(new Point3D(0,-2,0)),expected); - - //EP02: Ray does not intersect with the plane + assertEquals("Wrong points expected", List.of(new Point3D(0, -2, 0)), expected); + + // EP02: Ray does not intersect with the plane expected = p.findIntersections(new Ray(new Point3D(0, 0, 1), new Vector(1, 0, 0))); assertNull("Wrong number of points", expected); - - // BVA01: Ray is parallel to the plane - included in plane + + // BVA01: Ray is parallel to the plane - included in plane expected = p.findIntersections(new Ray(new Point3D(0, 1, 0), new Vector(0, -1, 0))); assertNull("Wrong number of points", expected); - - // BVA02: Ray is parallel to the plane - not included in plane + + // BVA02: Ray is parallel to the plane - not included in plane expected = p.findIntersections(new Ray(new Point3D(0, 0, 2), new Vector(0, 0, 1))); assertNull("Wrong number of points", expected); - - // BVA03: Ray is orthogonal to the plane - before it + + // BVA03: Ray is orthogonal to the plane - before it expected = p.findIntersections(new Ray(new Point3D(-1, -1, -1), new Vector(0, 0, 1))); assertEquals("Wrong number of points", 1, expected.size()); - assertEquals("Wrong points expected",List.of(new Point3D(-1,-1,0)),expected); - - // BVA04: Ray is orthogonal to the plane - after it + assertEquals("Wrong points expected", List.of(new Point3D(-1, -1, 0)), expected); + + // BVA04: Ray is orthogonal to the plane - after it expected = p.findIntersections(new Ray(new Point3D(1, 1, 1), new Vector(0, 0, 1))); assertNull("Wrong number of points", expected); - - // BVA05: Ray is orthogonal to the plane - starts in it + + // BVA05: Ray is orthogonal to the plane - starts in it expected = p.findIntersections(new Ray(new Point3D(0, 0, 0), new Vector(0, 0, 1))); assertNull("Wrong number of points", expected); - - // BVA06: Ray starts at the plane, if it isn't included in the other BVAs + + // BVA06: Ray starts at the plane, if it isn't included in the other BVAs expected = p.findIntersections(new Ray(new Point3D(0, 0, 0), new Vector(1, -1, 0))); assertNull("Wrong number of points", expected); - - // BVA07: Ray begins at the point of the plane (not its' direction) + + // BVA07: Ray begins at the point of the plane (not its' direction) expected = p.findIntersections(new Ray(new Point3D(0, 0, 0), new Vector(1, -1, 0))); assertNull("Wrong number of points", expected); } diff --git a/cameraproject/src/unittests/geometries/TriangleTest.java b/cameraproject/src/unittests/geometries/TriangleTest.java index ec8c0b0..b6a6d63 100644 --- a/cameraproject/src/unittests/geometries/TriangleTest.java +++ b/cameraproject/src/unittests/geometries/TriangleTest.java @@ -27,38 +27,40 @@ public void testGetNormal() { assertEquals("getNormal() Bad normal to Triangle", new Vector(new Point3D(1 / x, 2 / x, 3 / x)), newTriangle.getNormal(new Point3D(4, 1d / 4, 1d / 2))); } - - @Test - public void testFindIntersections() { - - Triangle t=new Triangle(new Point3D(2,1,0), new Point3D(0, 0, 0), new Point3D(0, 2, 0)); - List result; - // EP01: ray goes inside the triangle - result=t.findIntersections(new Ray(new Point3D(-1, -1, -1), new Vector(2, 2, 1))); - assertEquals("Wrong point recieved",List.of(new Point3D(1,1,0)), result); - // EP02: Outside against edge - result=t.findIntersections(new Ray(new Point3D(-1, -2, -1), new Vector(3, 4, 1))); - assertNull("Ray doesnt work when it touches the edge",result); + /** + * Test method for + * {@link geometries.Triangle#findIntersections(primitives.Ray)}. + */ + @Test + public void testFindIntersections() { - //EP03: Outside against vertex - result=t.findIntersections(new Ray(new Point3D(-2, -2, -1), new Vector(5, 4, 1))); - assertNull("Ray doesnt work when it touches the vertex",result); + Triangle t = new Triangle(new Point3D(2, 1, 0), new Point3D(0, 0, 0), new Point3D(0, 2, 0)); + List result; + // EP01: ray goes inside the triangle + result = t.findIntersections(new Ray(new Point3D(-1, -1, -1), new Vector(2, 2, 1))); + assertEquals("Wrong point recieved", List.of(new Point3D(1, 1, 0)), result); + // EP02: ray goes Outside against edge + result = t.findIntersections(new Ray(new Point3D(-1, -2, -1), new Vector(3, 4, 1))); + assertNull("Ray doesnt work when it touches the edge", result); - // BVA01: Ray touches the corner of the triangle - result=t.findIntersections(new Ray(new Point3D(-1, -1, -1), new Vector(1, 2, 1))); - assertNull("Ray is outside against edge",result); + // EP03: ray goes Outside against vertex + result = t.findIntersections(new Ray(new Point3D(-2, -2, -1), new Vector(5, 4, 1))); + assertNull("Ray doesnt work when it touches the vertex", result); - // BVA02: Ray intersects with the vertex - result=t.findIntersections(new Ray(new Point3D(-1, -2, -1), new Vector(1, 4, 1))); - assertNull("Ray is outside against edge",result); + // BVA01: Ray touches the corner of the triangle + result = t.findIntersections(new Ray(new Point3D(-1, -1, -1), new Vector(1, 2, 1))); + assertNull("Ray is outside against edge", result); - //BVA03: Ray intersects with the continuation of the vertex - result=t.findIntersections(new Ray(new Point3D(-3, -3, -1), new Vector(1, 6, 1))); - assertNull("Ray is outside against edge",result); + // BVA02: Ray intersects with the vertex + result = t.findIntersections(new Ray(new Point3D(-1, -2, -1), new Vector(1, 4, 1))); + assertNull("Ray is outside against edge", result); - } + // BVA03: Ray intersects with the continuation of the vertex + result = t.findIntersections(new Ray(new Point3D(-3, -3, -1), new Vector(1, 6, 1))); + assertNull("Ray is outside against edge", result); + } }