third level
diff --git a/src/geometries/Geometries.java b/src/geometries/Geometries.java
new file mode 100644
index 0000000..0e7444c
--- /dev/null
+++ b/src/geometries/Geometries.java
@@ -0,0 +1,64 @@
+/**
+ *
+ */
+package geometries;
+
+import primitives.Point3D;
+import primitives.Ray;
+
+import java.util.Arrays;
+import java.util.LinkedList;
+import java.util.List;
+
+/**
+ * @author Adiel
+ *
+ * Composite class for all Intersectable objects
+ */
+public class Geometries implements Intersectable {
+ private List<Intersectable> _intersectableList;
+
+ /**
+ * Default constructor that initializes the field with an empty list
+ */
+ public Geometries() {
+ _intersectableList = new LinkedList<>();
+ }
+
+ /**
+ * Constructor for the geometries
+ * @param geometries for the intersections
+ */
+ public Geometries(Intersectable... geometries) {
+ _intersectableList = new LinkedList<>();
+ add(geometries);
+
+ }
+
+ /**
+ * Adding to list
+ * @param geometries
+ */
+ public void add(Intersectable... geometries) {
+ _intersectableList.addAll(Arrays.asList(geometries));
+
+ }
+
+ @Override
+ public List<Point3D> findIntersections(Ray ray) {
+ List<Point3D> result = null;
+
+ // A loop that adds to the list all the bodies that have
+ // intersections with the ray
+ for (Intersectable item : _intersectableList) {
+ List<Point3D> elementList = item.findIntersections(ray);
+ if (elementList != null) {
+ if (result == null) {
+ result = new LinkedList<>();
+ }
+ result.addAll(elementList);
+ }
+ }
+ return result;
+ }
+}
\ No newline at end of file
diff --git a/src/geometries/Geometry.java b/src/geometries/Geometry.java
index e87ca32..dcd47be 100644
--- a/src/geometries/Geometry.java
+++ b/src/geometries/Geometry.java
@@ -1,5 +1,6 @@
package geometries;
+
import primitives.Point3D;
import primitives.Vector;
@@ -9,11 +10,13 @@
* @author Adiel
*
*/
-public interface Geometry {
+public interface Geometry extends Intersectable{
/**
*
* @param point3D of the vector
* @return normalized vector
*/
Vector getNormal(Point3D point3D);
+
+
}
diff --git a/src/geometries/Intersectable.java b/src/geometries/Intersectable.java
new file mode 100644
index 0000000..c3e8508
--- /dev/null
+++ b/src/geometries/Intersectable.java
@@ -0,0 +1,23 @@
+/**
+ *
+ */
+package geometries;
+
+import primitives.*;
+
+import java.util.List;
+
+/**
+ * @author Adiel
+ *
+ * Interface for intersection points
+ */
+public interface Intersectable {
+ /**
+ * Find all intersection points from the ray
+ *
+ * @param ray the famous Ray pointing to
+ * @return intersection points
+ */
+ List<Point3D> findIntersections(Ray ray);
+}
diff --git a/src/geometries/Plane.java b/src/geometries/Plane.java
index 93ba896..26ae874 100644
--- a/src/geometries/Plane.java
+++ b/src/geometries/Plane.java
@@ -1,7 +1,9 @@
package geometries;
-import primitives.Point3D;
-import primitives.Vector;
+import java.util.List;
+import static primitives.Util.*;
+
+import primitives.*;
/**
* Plane class represents a plane using a point in space and a vertical vector
@@ -18,7 +20,7 @@
/**
* Plane constructor receive the point and vertical
*
- * @param q0 the point
+ * @param q0 the point
* @param normal the vertical
*/
public Plane(Point3D q0, Vector normal) {
@@ -34,11 +36,11 @@
* @param p3 for the third point
*/
public Plane(Point3D p1, Point3D p2, Point3D p3) {
- this.q0=p1;
- Vector v1=p2.subtract(p1);
- Vector v2=p3.subtract(p1);
- Vector n =v1.crossProduct(v2);
- this.normal=n.normalize();
+ this.q0 = p1;
+ Vector v1 = p2.subtract(p1);
+ Vector v2 = p3.subtract(p1);
+ Vector n = v1.crossProduct(v2);
+ this.normal = n.normalize();
}
@@ -69,4 +71,32 @@
return normal;
}
+
+ @Override
+ public List<Point3D> findIntersections(Ray ray) {
+ List<Point3D> result = null;
+
+ //Check if q0=p0
+ if (q0.equals(ray.getP0())) {
+ return null;
+ }
+
+ double numerator = alignZero(normal.dotProduct(q0.subtract(ray.getP0())));
+ double denominator = alignZero(normal.dotProduct(ray.getDir()));
+
+ //Check if numerator or denominator equal zero
+ if (isZero(numerator) || isZero(denominator)) {
+ return null;
+ }
+
+ double t = numerator / denominator;
+ if (t < 0) {
+ return result;//result=null
+ }
+
+
+ return List.of(ray.getPoint(t));
+
+ }
+
}
diff --git a/src/geometries/Polygon.java b/src/geometries/Polygon.java
index ab073eb..ec98b6b 100644
--- a/src/geometries/Polygon.java
+++ b/src/geometries/Polygon.java
@@ -85,4 +85,45 @@
public Vector getNormal(Point3D point) {
return plane.getNormal();
}
+
+ @Override
+ public List<Point3D> findIntersections(Ray ray) {
+ List<Point3D> result = plane.findIntersections(ray);
+
+ //Check if intersect ray with the plane.
+ if (result == null) {
+ return null;
+ }
+
+ //Check if the intersection point with its plane is inside the polygon
+ Vector v = ray.getDir();
+ Point3D P0 = ray.getP0();
+ Vector v1 = vertices.get(0).subtract(P0);
+ Vector v2 = vertices.get(1).subtract(P0);
+
+ double t = alignZero(v.dotProduct(v1.crossProduct(v2).normalize()));
+ if (isZero(t)) {
+ return null;
+ }
+
+ boolean sign = t > 0;
+ int size = vertices.size();
+ Vector vn = vertices.get(size - 1).subtract(P0);
+ t = alignZero(v.dotProduct(vn.crossProduct(v1).normalize()));
+ if (isZero(t) || sign ^ (t > 0)) {
+ return null;
+ }
+
+ //Check if all vertices have the same sign
+ for (int i = 2; i < size; i++) {
+ v1 = v2;
+ v2 = vertices.get(i).subtract(P0);
+ t = alignZero(v.dotProduct(v1.crossProduct(v2).normalize()));
+ if (isZero(t) || sign ^ (t > 0)) {
+ return null;
+ }
+
+ }
+ return result;
+ }
}
diff --git a/src/geometries/Sphere.java b/src/geometries/Sphere.java
index 00c6c08..ba98378 100644
--- a/src/geometries/Sphere.java
+++ b/src/geometries/Sphere.java
@@ -1,7 +1,11 @@
package geometries;
+import java.util.List;
+
import primitives.Point3D;
+import primitives.Ray;
import primitives.Vector;
+import static primitives.Util.*;
/**
* Sphere class represent sphere by center point radius.
@@ -54,4 +58,42 @@
return vector.normalize();
}
+ @Override
+ public List<Point3D> findIntersections(Ray ray) {
+ List<Point3D> result = null;
+ Point3D P0 = ray.getP0();
+ Vector v = ray.getDir();
+
+ if (center.equals(P0)) {
+
+ return List.of(ray.getPoint(radius));
+
+ }
+
+ Vector u = center.subtract(P0);
+
+ double tm = alignZero(u.dotProduct(v));
+ double d = alignZero(Math.sqrt(u.lengthSquared() - tm * tm));
+
+ if (d > radius) {
+ return null;
+ }
+ double th = alignZero(Math.sqrt(radius * radius - d * d));
+
+ double t1 = alignZero(tm - th);
+ double t2 = alignZero(tm + th);
+
+ if (t1 > 0 && t2 > 0) {
+ return List.of(ray.getPoint(t1), ray.getPoint(t2));
+
+ } else if (t1 > 0) {
+ return List.of(ray.getPoint(t1));
+
+ } else if (t2 > 0) {
+ return List.of(ray.getPoint(t2));
+ }
+
+ return result;
+ }
+
}
diff --git a/src/geometries/Triangle.java b/src/geometries/Triangle.java
index e51cd27..9efcba7 100644
--- a/src/geometries/Triangle.java
+++ b/src/geometries/Triangle.java
@@ -1,7 +1,9 @@
package geometries;
+
import primitives.Point3D;
+
/**
* Triangle class represent a polygon with three vertices.
*
diff --git a/src/geometries/Tube.java b/src/geometries/Tube.java
index b01814c..c7df809 100644
--- a/src/geometries/Tube.java
+++ b/src/geometries/Tube.java
@@ -5,6 +5,8 @@
import primitives.Vector;
import static primitives.Util.*;
+import java.util.List;
+
/**
* Tube class represent tube by radius and ray.
*
@@ -68,4 +70,10 @@
Vector vector = point3d.subtract(O);
return vector.normalize();
}
+
+ @Override
+ public List<Point3D> findIntersections(Ray ray) {
+ // TODO Auto-generated method stub
+ return null;
+ }
}
diff --git a/src/primitives/Point3D.java b/src/primitives/Point3D.java
index 49ea698..280a703 100644
--- a/src/primitives/Point3D.java
+++ b/src/primitives/Point3D.java
@@ -62,7 +62,10 @@
*/
public Vector subtract(Point3D point3D) {
- return new Vector(_x.coord - point3D._x.coord, _y.coord - point3D._y.coord, _z.coord - point3D._z.coord);
+ return new Vector(
+ _x.coord - point3D._x.coord,
+ _y.coord - point3D._y.coord,
+ _z.coord - point3D._z.coord);
}
/**
@@ -110,4 +113,9 @@
return "(" + _x + ", " + _y + ", " + _z + ")";
}
+ public double getX() {
+
+ return _x.coord;
+ }
+
}
diff --git a/src/primitives/Ray.java b/src/primitives/Ray.java
index 9faaec7..ec012b3 100644
--- a/src/primitives/Ray.java
+++ b/src/primitives/Ray.java
@@ -21,7 +21,7 @@
public Ray(Point3D p0, Vector dir) {
this.p0 = p0;
- this.dir = dir;
+ this.dir = dir.normalized();
}
/**
@@ -55,5 +55,8 @@
public String toString() {
return p0 + ", " + dir;
}
-
+
+ public Point3D getPoint(double t){
+ return p0.add(dir.scale(t));
+ }
}
diff --git a/src/unittests/GeometriesTests.java b/src/unittests/GeometriesTests.java
new file mode 100644
index 0000000..4d8bcaa
--- /dev/null
+++ b/src/unittests/GeometriesTests.java
@@ -0,0 +1,75 @@
+/**
+ *
+ */
+package unittests;
+
+import static org.junit.Assert.*;
+
+import java.util.List;
+
+import primitives.*;
+
+import org.junit.Test;
+
+import geometries.*;
+
+/**
+ * @author Adiel
+ *
+ */
+public class GeometriesTests {
+
+ /**
+ * Test method for {@link geometries.Geometries#findIntersections(primitives.Ray)}.
+ */
+ @Test
+ public void testFindIntersections() {
+ Plane plane = new Plane(
+ new Point3D(1, 0, 0),
+ new Point3D(0, 1, 0),
+ new Point3D(0, 0, 1));
+
+ Sphere sphere = new Sphere(new Point3D(0, 6, 0), 2);
+ Triangle triangle = new Triangle(
+ new Point3D(1, 0, 0),
+ new Point3D(3, 2, 0),
+ new Point3D(5, 0, 0));
+ Geometries geometries = new Geometries(plane, sphere, triangle);
+
+ // ============ Equivalence Partitions Tests ==============
+
+ //TC01: Some (but not all) shapes are intersected
+ Ray ray = new Ray(new Point3D(2, 0.5, 3), new Vector(0, 0, -1));
+ List<Point3D> intersections = geometries.findIntersections(ray);
+ assertEquals("Some shapes are intersected",2, intersections.size());
+
+ // =============== Boundary Values Tests ==================
+
+ //TC02: No shape is intersected
+ assertNull( "No shape is intersected",geometries.findIntersections(new Ray(
+ new Point3D(8, 8, 8),
+ new Vector(0, 0, 1))));
+
+ //TC03: Only one shape is intersected
+ intersections = geometries.findIntersections(new Ray(
+ new Point3D(0, 6, 0),
+ new Vector(0, 1, 0)));
+ assertEquals("Only one shape is intersected",1, intersections.size());
+
+
+ //TC04: All shapes are intersected
+ sphere = new Sphere(new Point3D(3, 0, -3), 2);
+ geometries.add(sphere);
+ intersections = geometries.findIntersections(ray);
+ assertEquals("Some shapes are intersected",4, intersections.size());
+
+ //TC05: Collection of bodies is empty
+ geometries = new Geometries();
+ assertNull("No intersection points",geometries.findIntersections(new Ray(
+ new Point3D(0, 0, 0),
+ new Vector(0, 0, 1))));
+
+
+ }
+
+}
diff --git a/src/unittests/PlaneTests.java b/src/unittests/PlaneTests.java
index 22d935e..e50ee3e 100644
--- a/src/unittests/PlaneTests.java
+++ b/src/unittests/PlaneTests.java
@@ -5,6 +5,8 @@
import static org.junit.Assert.*;
+import java.util.List;
+
import org.junit.Test;
import geometries.Plane;
@@ -56,4 +58,67 @@
}
+ /**
+ * Test method for {@link geometries.Plane#findIntersections(primitives.Ray)}.
+ */
+ @Test
+ public void testFindIntersections() {
+ Plane plane = new Plane(new Point3D(1, 0, 0),
+ new Point3D(0, 1, 0),
+ new Point3D(0, 0, 1));
+
+ // ============ Equivalence Partitions Tests ==============
+
+ // TC01: Ray intersects the plane (1 point)
+ Point3D p1 = new Point3D(1, 0, 0);
+ List<Point3D> result = plane.findIntersections(new Ray(
+ new Point3D(-1, 0, 0),
+ new Vector(1, 0, 0)));
+ assertEquals("Result is wrong", 1, result.size());
+ assertEquals("Ray intersects the plane", List.of(p1), result);
+
+ // TC02: Ray does not intersect the plane (0 point)
+ assertNull("Ray not intersects the plane",
+ plane.findIntersections(new Ray(
+ new Point3D(-1, 0, 0),
+ new Vector(-1, -1, 0))));
+
+ // =============== Boundary Values Tests ==================
+
+ // **** Group: Ray is parallel to the plane
+ // TC03: The ray included in the plane
+ assertNull("Ray not intersects the plane",plane.findIntersections(new Ray(
+ new Point3D(-1, 0, 0),
+ new Vector(1, -1, 0))));
+
+ // TC04: The ray not included in the plane
+ assertNull("Ray not intersects the plane",plane.findIntersections(new Ray(
+ new Point3D(-1, 0, 0),
+ new Vector(-2, 0, 0))));
+
+ // **** Group: Ray is orthogonal to the plane
+ // TC05: The point before the plane
+ assertNull("Ray not intersects the plane",plane.findIntersections(new Ray(
+ new Point3D(-1, 0, 0),
+ new Vector(0, -2, 0))));
+ // TC06: The point in the plane
+ assertNull("Ray not intersects the plane",plane.findIntersections(new Ray(
+ new Point3D(0, 0, 1),
+ new Vector(1, 1, 1))));
+ // TC07: The point after the plane
+ assertNull("Ray not intersects the plane",plane.findIntersections(new Ray(
+ new Point3D(3, 2, 2),
+ new Vector(1, 1, 1))));
+
+ // **** Group: Ray is neither orthogonal nor parallel to the plane
+ // TC08: The ray begins at the plane (p0 is in the plane, but not the ray)
+ assertNull("Ray not intersects the plane",plane.findIntersections(new Ray(
+ new Point3D(1.5, 1.5, 0),
+ new Vector(2, 2, -3))));
+ // TC09: The ray begins in the same point which appears as reference point in
+ // the plane (Q)
+ assertNull("Ray not intersects the plane",plane.findIntersections(new Ray(
+ new Point3D(1, 0, 0),
+ new Vector(1, 1, -2))));
+ }
}
diff --git a/src/unittests/PolygonTests.java b/src/unittests/PolygonTests.java
index 1eb8249..677c937 100644
--- a/src/unittests/PolygonTests.java
+++ b/src/unittests/PolygonTests.java
@@ -4,6 +4,9 @@
package unittests;
import static org.junit.Assert.*;
+
+import java.util.List;
+
import org.junit.Test;
import geometries.*;
@@ -86,10 +89,60 @@
public void testGetNormal() {
// ============ Equivalence Partitions Tests ==============
// TC01: There is a simple single test here
- Polygon pl = new Polygon(new Point3D(0, 0, 1), new Point3D(1, 0, 0), new Point3D(0, 1, 0),
+ Polygon pl = new Polygon(new Point3D(0, 0, 1),
+ new Point3D(1, 0, 0),
+ new Point3D(0, 1, 0),
new Point3D(-1, 1, 1));
double sqrt3 = Math.sqrt(1d / 3);
- assertEquals("Bad normal to trinagle", new Vector(sqrt3, sqrt3, sqrt3), pl.getNormal(new Point3D(0, 0, 1)));
+ assertEquals("Bad normal to trinagle",
+ new Vector(sqrt3, sqrt3, sqrt3),
+ pl.getNormal(new Point3D(0, 0, 1)));
+ }
+
+ /**
+ * Test method for {@link geometries.Polygon#findIntersections(primitives.Point3D)}.
+ */
+ @Test
+ public void findIntersections() {
+ Polygon pl = new Polygon(
+ new Point3D(2,0,0),
+ new Point3D(1, 1, 0),
+ new Point3D(3,2, 0),
+ new Point3D(5, 1, 0));
+
+ // ============ Equivalence Partitions Tests ==============
+ //TC01: The point inside the polygon (1 point)
+ Point3D p1=new Point3D(2,1,0);
+ List<Point3D> result=pl.findIntersections(new Ray(
+ new Point3D(2,1,1),new Vector(0,0,-1)));
+ assertEquals( "Wrong number of points",1, result.size());
+ assertEquals("Ray crosses polygon",List.of(p1), result);
+ //TC02: The point outside the polygon, against edge (0 points)
+ assertNull( "Ray not intersects the polygon",pl.findIntersections(new Ray(
+ new Point3D(2, 2, 0),
+ new Vector(0, 0, -1))));
+
+ //TC03: The point outside the polygon, against vertex (0 points)
+ assertNull("Ray not intersects the polygon",pl.findIntersections(new Ray(
+ new Point3D(0.5, 1.0, 0),
+ new Vector(0, 0, -1))));
+
+ // =============== Boundary Values Tests ==================
+ //TC04: The point on edge (0 points)
+ assertNull("Ray not intersects the polygon",pl.findIntersections(new Ray(
+ new Point3D(1.5, 1.25, 1),
+ new Vector(0, 0, -1))));
+ //TC05: The point in vertex (0 points)
+ assertNull("Ray not intersects the polygon",pl.findIntersections(new Ray(
+ new Point3D(1, 1.0, 0),
+ new Vector(0, 0, -1))));
+ //TC06: The point on edge's continuation (0 points)
+ assertNull("Ray not intersects the polygon",pl.findIntersections(new Ray(
+ new Point3D(4, 3.0, 1),
+ new Vector(0, 0, -1))));
+
+
+
}
}
diff --git a/src/unittests/SphereTests.java b/src/unittests/SphereTests.java
index 9426504..277ffcf 100644
--- a/src/unittests/SphereTests.java
+++ b/src/unittests/SphereTests.java
@@ -4,10 +4,14 @@
package unittests;
import static org.junit.Assert.*;
+
+import java.util.List;
+
import org.junit.Test;
import geometries.Sphere;
import primitives.*;
+import static primitives.Point3D.ZERO;
/**
* @author Adiel
@@ -22,10 +26,127 @@
public void testGetNormal() {
// ============ Equivalence Partitions Tests ==============
- Sphere sp = new Sphere(new Point3D(0, 0, 1), 5);
+ Sphere sp = new Sphere(new Point3D(0, 0, 1), 1d);
double sqrt2 = Math.sqrt(2);
// Standard normal test
- assertEquals("Bad normal to sphere", new Vector(1 / sqrt2, 0, -1 / sqrt2), sp.getNormal(new Point3D(1, 0, 0)));
+ assertEquals("Bad normal to sphere", new Vector(1 / sqrt2, 0, -1 / sqrt2),
+ sp.getNormal(new Point3D(1, 0, 0)));
+ }
+
+ /**
+ * Test method for
+ * {@link geometries.Sphere#findIntersections(primitives.Point3D)}.
+ */
+ @Test
+ public void testFindIntersections() {
+ Sphere sphere = new Sphere(new Point3D(1, 0, 0), 1d);
+
+ // ============ Equivalence Partitions Tests ==============
+
+ // TC01: Ray's line is outside the sphere (0 points)
+ assertNull("Ray's line out of sphere",
+ sphere.findIntersections(new Ray(new Point3D(-1, 0, 0),
+ new Vector(1, 1, 0))));
+
+ // TC02: Ray starts before and crosses the sphere (2 points)
+ Point3D p1 = new Point3D(0.0651530771650466, 0.355051025721682, 0);
+ Point3D p2 = new Point3D(1.53484692283495, 0.844948974278318, 0);
+ List<Point3D> result = sphere.findIntersections(new Ray(new Point3D(-1, 0, 0),
+ new Vector(3, 1, 0)));
+ assertEquals("Wrong number of points", 2, result.size());
+ if (result.get(0).getX() > result.get(1).getX())
+ result = List.of(result.get(1), result.get(0));
+ assertEquals("Ray crosses sphere", List.of(p1, p2), result);
+
+ // TC03: Ray starts inside the sphere (1 point)
+ Point3D p3 = new Point3D(1.6851646544245034, 0.7283882181415011, 0);
+ List<Point3D> result1 = sphere.findIntersections(new Ray(new Point3D(1, 0.5, 0),
+ new Vector(3, 1, 0)));
+ assertEquals("Wrong number of points", 1, result1.size());
+ assertEquals("Ray coming out of a sphere", List.of(p3), result1);
+
+ // TC04: Ray starts after the sphere (0 points)
+ assertNull("Ray's line after the sphere",
+ sphere.findIntersections(new Ray(new Point3D(-1, 0, 0),
+ new Vector(-2, -1, 0))));
+
+ // =============== Boundary Values Tests ==================
+
+ // **** Group: Ray's line crosses the sphere (but not the center)
+ // TC11: Ray starts at sphere and goes inside (1 points)
+ Point3D p4 = new Point3D(1.6851646544245034, 0.7283882181415011, 0);
+ List<Point3D> result2 = sphere.findIntersections(new Ray(
+ new Point3D(0.4, 0.5, 0), new Vector(3, 1, 0)));
+ assertEquals("Wrong number of points", 1, result2.size());
+ assertEquals("Ray coming out of a sphere", List.of(p4), result1);
+
+ // TC12: Ray starts at sphere and goes outside (0 points)
+ assertNull("Ray's line out of sphere",
+ sphere.findIntersections(new Ray(new Point3D(0, 0, 1),
+ new Vector(-1, 0, 0))));
+
+ // **** Group: Ray's line goes through the center
+ // TC13: Ray starts before the sphere (2 points)
+ Point3D p6=new Point3D(2, 0, 0);
+
+ List<Point3D> result3 = sphere.findIntersections(new Ray(new Point3D(2.5, 0, 0),
+ new Vector(-1, 0, 0)));
+ assertEquals("Wrong number of points", 2, result3.size());
+ if (result3.get(0).getX() > result3.get(1).getX())
+ result3 = List.of(result3.get(1), result3.get(0));
+ assertEquals("Ray crosses sphere", List.of(ZERO, p6), result3);
+
+ // TC14: Ray starts at sphere and goes outside (1 points)
+ List<Point3D> result4 = sphere.findIntersections(new Ray(new Point3D(0.4, 0, 0),
+ new Vector(-1, 0, 0)));
+ assertEquals("Wrong number of points", 1, result4.size());
+ assertEquals("Ray coming out of a sphere", List.of(ZERO), result4);
+
+ // TC15: Ray starts inside (1 points)
+ List<Point3D> result5 = sphere.findIntersections(new Ray(new Point3D(2, 0, 0),
+ new Vector(-1, 0, 0)));
+ assertEquals("Wrong number of points", 1, result5.size());
+ assertEquals("Ray coming out of a sphere", List.of(ZERO), result5);
+
+ // TC16: Ray starts at the center (1 points)
+ List<Point3D> result6 = sphere.findIntersections(new Ray(new Point3D(1, 0, 0),
+ new Vector(-1, 0, 0)));
+ assertEquals("Wrong number of points", 1, result6.size());
+ assertEquals("Ray coming out of a sphere", List.of(ZERO), result6);
+
+ // TC17: Ray starts at sphere and goes outside (0 points)
+ assertNull("Ray's line out of sphere",
+ sphere.findIntersections(new Ray(
+ new Point3D(0, 0, 1), new Vector(-1, 0, 1))));
+
+ // TC18: Ray starts after sphere (0 points)
+ assertNull("Ray's line out of sphere",
+ sphere.findIntersections(new Ray(
+ new Point3D(-1, 0, 0), new Vector(-2, 0, 0))));
+
+ // **** Group: Ray's line is tangent to the sphere (all tests 0 points)
+ // TC19: Ray starts before the tangent point (0 points)
+ assertNull("Ray's line out of sphere", sphere.findIntersections(new Ray( new
+ Point3D(0, 0, -1), new Vector(0, 1, 0))));
+
+
+
+ // TC20: Ray starts at the tangent point (0 points)
+ assertNull("Ray's line out of sphere",
+ sphere.findIntersections(new Ray(
+ new Point3D(1, 1, 0), new Vector(3, 2, 1))));
+
+ // TC21: Ray starts after the tangent point (0 points)
+ assertNull("Ray's line out of sphere",
+ sphere.findIntersections(new Ray(
+ new Point3D(1, 1, 0), new Vector(1, 0, 0))));
+
+ // **** Group: Special cases
+ // TC19: Ray's line is outside, ray is orthogonal to ray start to sphere's
+ // center line (0 points)
+ assertNull("Ray's line out of sphere",
+ sphere.findIntersections(
+ new Ray(new Point3D(-1, 0, 0), new Vector(-1, 1, 0))));
}
}
diff --git a/src/unittests/TriangleTests.java b/src/unittests/TriangleTests.java
index 609c741..d1ed1ad 100644
--- a/src/unittests/TriangleTests.java
+++ b/src/unittests/TriangleTests.java
@@ -4,6 +4,9 @@
package unittests;
import static org.junit.Assert.*;
+
+import java.util.List;
+
import org.junit.Test;
import geometries.Triangle;
@@ -25,7 +28,52 @@
Triangle tr = new Triangle(new Point3D(0, 0, 1), new Point3D(1, 0, 0), new Point3D(0, 1, 0));
double sqrt3 = Math.sqrt(1d / 3);
//Standard normal test
- assertEquals("Bad normal to triangle", new Vector(sqrt3, sqrt3, sqrt3), tr.getNormal(new Point3D(0, 0, 1)));
+ assertEquals("Bad normal to triangle",
+ new Vector(sqrt3, sqrt3, sqrt3),
+ tr.getNormal(new Point3D(0, 0, 1)));
}
+
+ /**
+ * Test method for {@link geometries.Polygon#getNormal(primitives.Point3D)}.
+ */
+ @Test
+ public void findIntersections() {
+ Triangle tr=new Triangle(
+ new Point3D(1,0,0),
+ new Point3D(3,2,0),
+ new Point3D(5,0, 0));
+
+ // ============ Equivalence Partitions Tests ==============
+ //TC01: The point inside the triangle (1 point)
+ Point3D p1=new Point3D(2,0.5,0);
+ List<Point3D> result=tr.findIntersections(new Ray(
+ new Point3D(2,0.5,1),new Vector(0,0,-1)));
+ assertEquals("Wrong number of points",1, result.size());
+ assertEquals( "Ray crosses triangle",List.of(p1), result);
+ //TC02: The point outside the triangle, against edge
+ assertNull("Ray not intersects the triangle",tr.findIntersections(new Ray(
+ new Point3D(1.5, 1.0, 0),
+ new Vector(0, 0, -1))));
+ //TC03: The point outside the triangle, against vertex (0 point)
+ assertNull("Ray not intersects the triangle",tr.findIntersections(new Ray(
+ new Point3D(0.5, 0, 0),
+ new Vector(0, 0, -1))));
+
+ // =============== Boundary Values Tests ==================
+ //TC04: The point on edge (0 points)
+ assertNull("Ray not intersects the triangle",tr.findIntersections(new Ray(
+ new Point3D(2, 0, 0),
+ new Vector(0, 0, -1))));
+
+ //TC05: The point in vertex (0 points)
+ assertNull( "Ray not intersects the triangle",tr.findIntersections(new Ray(
+ new Point3D(3, -2, 0),
+ new Vector(0, 0, -1))));
+
+ //TC06: The point on edge's continuation (0 points)
+ assertNull("Ray not intersects the triangle",tr.findIntersections(new Ray(
+ new Point3D(6, 0, 0),
+ new Vector(0, 0, -1))));
+ }
}