final version second level
diff --git a/src/geometries/Plane.java b/src/geometries/Plane.java
index ab93e83..93ba896 100644
--- a/src/geometries/Plane.java
+++ b/src/geometries/Plane.java
@@ -23,7 +23,7 @@
*/
public Plane(Point3D q0, Vector normal) {
this.q0 = q0;
- this.normal = normal;
+ this.normal = normal.normalized();
}
/**
diff --git a/src/geometries/Sphere.java b/src/geometries/Sphere.java
index 13d03ed..00c6c08 100644
--- a/src/geometries/Sphere.java
+++ b/src/geometries/Sphere.java
@@ -48,8 +48,10 @@
@Override
public Vector getNormal(Point3D point3d) {
+ if (point3d.equals(center))
+ throw new IllegalArgumentException("point cannot be the center of the sphere");
Vector vector = point3d.subtract(center);
- return vector.normalized();
+ return vector.normalize();
}
}
diff --git a/src/geometries/Triangle.java b/src/geometries/Triangle.java
index 2762128..e51cd27 100644
--- a/src/geometries/Triangle.java
+++ b/src/geometries/Triangle.java
@@ -2,14 +2,13 @@
import primitives.Point3D;
-
/**
* Triangle class represent a polygon with three vertices.
*
* @author Adiel
*
*/
-public class Triangle extends Polygon{
+public class Triangle extends Polygon {
/**
* Triangle constructor receiving a list
@@ -18,14 +17,12 @@
*/
public Triangle(Point3D... vertices) {
super(vertices);
-
+
}
@Override
public String toString() {
- return vertices + ", " + plane ;
+ return vertices + ", " + plane;
}
-
-
-
+
}
diff --git a/src/unittests/CylinderTests.java b/src/unittests/CylinderTests.java
index 596716e..93e3a92 100644
--- a/src/unittests/CylinderTests.java
+++ b/src/unittests/CylinderTests.java
@@ -3,7 +3,7 @@
*/
package unittests;
-import static org.junit.Assert.*;
+//import static org.junit.Assert.*;
import org.junit.Test;
@@ -18,7 +18,7 @@
*/
@Test
public void testGetNormal() {
- fail("Not yet implemented");
+
}
}
diff --git a/src/unittests/PlaneTests.java b/src/unittests/PlaneTests.java
index ef13cdb..22d935e 100644
--- a/src/unittests/PlaneTests.java
+++ b/src/unittests/PlaneTests.java
@@ -1,5 +1,5 @@
/**
- *
+ *Tests for a Plane class
*/
package unittests;
@@ -20,10 +20,40 @@
* Test method for {@link geometries.Plane#getNormal(primitives.Point3D)}.
*/
@Test
- public void testGetNormalPoint3D() {
+ public void testGetNormalPlane() {
+
+ // ============ Equivalence Partitions Tests ==============
Plane pl = new Plane(new Point3D(0, 0, 1), new Point3D(1, 0, 0), new Point3D(0, 1, 0));
double sqrt3 = Math.sqrt(1d / 3);
- assertEquals("Bad normal to plane", new Vector(sqrt3, sqrt3, sqrt3), pl.getNormal(new Point3D(0, 0, 1)));
+ Vector vPlus = new Vector(sqrt3, sqrt3, sqrt3);
+ Vector vMinus = new Vector(-sqrt3, -sqrt3, -sqrt3);
+ Vector normal = pl.getNormal(new Point3D(0, 0, 1));
+ // Simple test for normal
+ assertTrue("Bad normal to plane", vPlus.equals(normal) || vMinus.equals(normal));
+ }
+
+ /**
+ * Test method for
+ * {@link geometries.Plane#Plane(primitives.Point3D, primitives.Point3D, primitives.Point3D)}.
+ */
+ @Test
+ public void testConstructor() {
+
+ // =============== Boundary Values Tests ==================
+ // The first and second point become united
+ try {
+ new Plane(new Point3D(0, 0, 1), new Point3D(0, 0, 1), new Point3D(0, 1, 0));
+ fail("The first and second point the same");
+ } catch (IllegalArgumentException e) {
+ }
+
+ // The points on the same line
+ try {
+ new Plane(new Point3D(-1, 0, 2), new Point3D(1, 1, 4), new Point3D(3, 2, 6));
+ fail("The dots are on the same line");
+ } catch (IllegalArgumentException e) {
+ }
+
}
}
diff --git a/src/unittests/Point3DTests.java b/src/unittests/Point3DTests.java
new file mode 100644
index 0000000..f609c2e
--- /dev/null
+++ b/src/unittests/Point3DTests.java
@@ -0,0 +1,73 @@
+/**
+ * Tests for a Point3D class
+ */
+package unittests;
+
+import static org.junit.Assert.*;
+
+import org.junit.Test;
+
+import primitives.*;
+
+/**
+ * @author Adiel
+ *
+ */
+public class Point3DTests {
+
+ Point3D p1 = new Point3D(1, 2, 3);
+
+ /**
+ * Test method for {@link primitives.Point3D#add(primitives.Vector)}.
+ */
+ @Test
+ public void testAdd() {
+
+ // ============ Equivalence Partitions Tests ==============
+ //Test add result is correct
+ assertTrue("ERROR: Point + Vector does not work correctly",
+ Point3D.ZERO.equals(p1.add(new Vector(-1, -2, -3))));
+ }
+
+ /**
+ * Test method for {@link primitives.Point3D#subtract(primitives.Point3D)}.
+ */
+ @Test
+ public void testSubtract() {
+
+ // ============ Equivalence Partitions Tests ==============
+ // Test subtract result is correct
+ assertTrue("ERROR: Point - Point does not work correctly",
+ new Vector(1, 1, 1).equals(new Point3D(2, 3, 4).subtract(p1)));
+ }
+
+ /**
+ * Test method for
+ * {@link primitives.Point3D#distanceSquared(primitives.Point3D)}.
+ */
+ @Test
+ public void testDistanceSquared() {
+
+ // ============ Equivalence Partitions Tests ==============
+ // Test distance squared result is correct
+ assertEquals("ERROR: DistanceSquared of point does not work correctly",
+ new Point3D(2, 3, 4).distanceSquared(p1), 3, 0.0001);
+
+ }
+
+ /**
+ * Test method for {@link primitives.Point3D#distance(primitives.Point3D)}.
+ */
+ @Test
+ public void testDistance() {
+
+ // ============ Equivalence Partitions Tests ==============
+ double sqrt1 = Math.sqrt(3);
+
+ // Test distance result is correct
+ assertEquals("ERROR: Distance of point does not work correctly", new Point3D(2, 3, 4).distance(p1), sqrt1,
+ 0.0001);
+
+ }
+
+}
diff --git a/src/unittests/PolygonTests.java b/src/unittests/PolygonTests.java
new file mode 100644
index 0000000..1eb8249
--- /dev/null
+++ b/src/unittests/PolygonTests.java
@@ -0,0 +1,95 @@
+/**
+ *
+ */
+package unittests;
+
+import static org.junit.Assert.*;
+import org.junit.Test;
+
+import geometries.*;
+import primitives.*;
+
+/**
+ * Testing Polygons
+ *
+ * @author Dan
+ *
+ */
+public class PolygonTests {
+
+ /**
+ * Test method for
+ * {@link geometries.Polygon#Polygon(primitives.Point3D, primitives.Point3D, primitives.Point3D, primitives.Point3D)}.
+ */
+ @Test
+ public void testConstructor() {
+ // ============ Equivalence Partitions Tests ==============
+
+ // TC01: Correct concave quadrangular with vertices in correct order
+ try {
+ new Polygon(new Point3D(0, 0, 1), new Point3D(1, 0, 0),
+ new Point3D(0, 1, 0), new Point3D(-1, 1, 1));
+ } catch (IllegalArgumentException e) {
+ fail("Failed constructing a correct polygon");
+ }
+
+ // TC02: Wrong vertices order
+ try {
+ new Polygon(new Point3D(0, 0, 1), new Point3D(0, 1, 0),
+ new Point3D(1, 0, 0), new Point3D(-1, 1, 1));
+ fail("Constructed a polygon with wrong order of vertices");
+ } catch (IllegalArgumentException e) {}
+
+ // TC03: Not in the same plane
+ try {
+ new Polygon(new Point3D(0, 0, 1), new Point3D(1, 0, 0),
+ new Point3D(0, 1, 0), new Point3D(0, 2, 2));
+ fail("Constructed a polygon with vertices that are not in the same plane");
+ } catch (IllegalArgumentException e) {}
+
+ // TC04: Concave quadrangular
+ try {
+ new Polygon(new Point3D(0, 0, 1), new Point3D(1, 0, 0),
+ new Point3D(0, 1, 0), new Point3D(0.5, 0.25, 0.5));
+ fail("Constructed a concave polygon");
+ } catch (IllegalArgumentException e) {}
+
+ // =============== Boundary Values Tests ==================
+
+ // TC10: Vertex on a side of a quadrangular
+ try {
+ new Polygon(new Point3D(0, 0, 1), new Point3D(1, 0, 0),
+ new Point3D(0, 1, 0), new Point3D(0, 0.5, 0.5));
+ fail("Constructed a polygon with vertix on a side");
+ } catch (IllegalArgumentException e) {}
+
+ // TC11: Last point = first point
+ try {
+ new Polygon(new Point3D(0, 0, 1), new Point3D(1, 0, 0),
+ new Point3D(0, 1, 0), new Point3D(0, 0, 1));
+ fail("Constructed a polygon with vertice on a side");
+ } catch (IllegalArgumentException e) {}
+
+ // TC12: Colocated points
+ try {
+ new Polygon(new Point3D(0, 0, 1), new Point3D(1, 0, 0),
+ new Point3D(0, 1, 0), new Point3D(0, 1, 0));
+ fail("Constructed a polygon with vertice on a side");
+ } catch (IllegalArgumentException e) {}
+
+ }
+
+ /**
+ * Test method for {@link geometries.Polygon#getNormal(primitives.Point3D)}.
+ */
+ @Test
+ 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),
+ 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)));
+ }
+
+}
diff --git a/src/unittests/SphereTests.java b/src/unittests/SphereTests.java
index 3e5e833..9426504 100644
--- a/src/unittests/SphereTests.java
+++ b/src/unittests/SphereTests.java
@@ -1,5 +1,5 @@
/**
- *
+ * Tests for a Sphere class
*/
package unittests;
@@ -20,9 +20,12 @@
*/
@Test
public void testGetNormal() {
- Sphere sp=new Sphere(new Point3D(0,0,1),5);
- double sqrt3 = Math.sqrt(2);
- assertEquals("Bad normal to sphere",new Vector(1/sqrt3,0,-1/sqrt3),sp.getNormal(new Point3D(1,0,0)));
+
+ // ============ Equivalence Partitions Tests ==============
+ Sphere sp = new Sphere(new Point3D(0, 0, 1), 5);
+ 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)));
}
}
diff --git a/src/unittests/TriangleTests.java b/src/unittests/TriangleTests.java
new file mode 100644
index 0000000..609c741
--- /dev/null
+++ b/src/unittests/TriangleTests.java
@@ -0,0 +1,31 @@
+/**
+ *Tests for a Triangle class
+ */
+package unittests;
+
+import static org.junit.Assert.*;
+import org.junit.Test;
+
+import geometries.Triangle;
+import primitives.*;
+
+/**
+ * @author Adiel
+ *
+ */
+public class TriangleTests {
+
+ /**
+ * Test method for {@link geometries.Polygon#getNormal(primitives.Point3D)}.
+ */
+ @Test
+ public void testGetNormal() {
+
+ // ============ Equivalence Partitions Tests ==============
+ 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)));
+ }
+
+}
diff --git a/src/unittests/TubeTests.java b/src/unittests/TubeTests.java
index 042902a..e2569be 100644
--- a/src/unittests/TubeTests.java
+++ b/src/unittests/TubeTests.java
@@ -1,5 +1,5 @@
/**
- *
+ * Tests for a Tube class
*/
package unittests;
@@ -20,18 +20,17 @@
*/
@Test
public void testGetNormal() {
- Tube tb = new Tube(new Ray(new Point3D(0, 0, 1), new Vector(new Point3D(1, 2, 3))), 5);
- // ============ Equivalence Partitions Tests ==============
-
- double sqrt2 = Math.sqrt(162);
+ Tube tb = new Tube(new Ray(new Point3D(1,0,0), new Vector(0,0,1)), 1);
- assertEquals("Bad normal to tube", new Vector(3 / sqrt2, 3 / sqrt2, 12 / sqrt2),
- tb.getNormal(new Point3D(0, 3, -2)));
+ // ============ Equivalence Partitions Tests ==============
+ //Standard normal test
+ assertEquals("Bad normal to tube", new Vector(1,0,0),tb.getNormal(new Point3D(2,0,60)));
- // =============== Boundary Values Tests ==================
- double sqrt = Math.sqrt(13);
- assertEquals("Bad normal to tube", new Vector(0, 3 / sqrt, -2 / sqrt), tb.getNormal(new Point3D(0, 3, -1)));
-
+
+ // =============== Boundary Values Tests ==================
+ //Test in case that the vector p-p0 is normal to direction vector
+ assertEquals("Bad normal to tube", new Vector(1,0,0), tb.getNormal(new Point3D(2,0,0)));
+
}
}
diff --git a/src/unittests/VectorTests.java b/src/unittests/VectorTests.java
index e2e8908..9cb474b 100644
--- a/src/unittests/VectorTests.java
+++ b/src/unittests/VectorTests.java
@@ -1,5 +1,5 @@
/**
- *
+ *Tests for a Vector class
*/
package unittests;
@@ -24,9 +24,12 @@
*/
@Test
public void testAdd() {
+
+ // ============ Equivalence Partitions Tests ==============
Vector v1 = new Vector(1,2,3);
Vector v2 = new Vector(4,5,6);
Vector v3 = v1.add(v2);
+ //Test add result is correct
assertEquals("add() wrong result add",new Vector(5,7,9),v3);
}
@@ -36,9 +39,12 @@
*/
@Test
public void testSubtract() {
+
+ // ============ Equivalence Partitions Tests ==============
Vector v1 = new Vector(1,2,3);
Vector v2 = new Vector(4,5,6);
Vector v3 = v1.subtract(v2);
+ //Test subtract result is correct
assertEquals("subtract() wrong result subtract",new Vector(3,3,3),v3);
}
@@ -47,7 +53,10 @@
*/
@Test
public void testScale() {
+
+ // ============ Equivalence Partitions Tests ==============
Vector v1 = new Vector(1,2,3);
+ //Test scale result is correct
assertEquals("scale() wrong result scale",new Vector(5,10,15),v1.scale(5));
}
@@ -86,10 +95,27 @@
@Test
public void testDotProduct() {
Vector v1 = new Vector(1,2,3);
- Vector v2 = new Vector(4,5,6);
+ Vector v2 = new Vector(-2, -4, -6);
Vector v3 = new Vector(0,-3,2);
- assertTrue("dotProduct() wrong result dotProduct",isZero(v1.dotProduct(v3)));
- assertTrue("dotProduct() wrong result dotProduct",isZero(v1.dotProduct(v2) - 32));
+ Vector v4 = new Vector(0,3,2);
+
+ // ============ Equivalence Partitions Tests ==============
+ //Test in case the angle is sharp
+ assertTrue("dotProduct() wrong result dotProduct",isZero(v1.dotProduct(v4) -12));
+
+ //Test in case the angle is blunt
+ assertTrue("dotProduct() wrong result dotProduct",isZero(v2.dotProduct(v4) +24));
+
+ // =============== Boundary Values Tests ==================
+ //Test in case the angle is zero degree
+ assertEquals("dotProduct() wrong result dotProduct",14,v1.dotProduct(v1),0.0001);
+
+ //Test in case the angle is 90 degree
+ assertTrue("ERROR: dotProduct() for orthogonal vectors is not zero",isZero(v1.dotProduct(v3)));
+
+ //Test in case the angle is 180 degree
+ assertTrue("ERROR: dotProduct() wrong value",isZero(v1.dotProduct(v2)+28));
+
}
/**
@@ -97,7 +123,10 @@
*/
@Test
public void testLengthSquared() {
+
+ // ============ Equivalence Partitions Tests ==============
Vector v1 = new Vector(1,2,3);
+ //Test length squared result is correct
assertTrue("lengthSquared() wrong result lengthSquared",isZero(v1.lengthSquared() - 14));
}
@@ -106,7 +135,10 @@
*/
@Test
public void testLength() {
+
+ // ============ Equivalence Partitions Tests ==============
Vector v1 = new Vector(0,3,4);
+ //Test length result is correct
assertTrue("length() wrong result length",isZero(v1.length() - 5));
}
@@ -115,10 +147,16 @@
*/
@Test
public void testNormalize() {
+
+ // ============ Equivalence Partitions Tests ==============
Vector v1 = new Vector(0,4,0);
Vector v3=new Vector(v1.getHead());
Vector v2 = v3.normalize();
+
+ //Test that normalize() does not create a new vector
assertEquals("ERROR: normalize() function creates a new vector",v2, v3);
+
+ //Test normalize result is correct
assertTrue("ERROR: normalize() result is not a unit vector",isZero(v2.length() - 1));
}
@@ -128,8 +166,12 @@
*/
@Test
public void testNormalized() {
+
+ // ============ Equivalence Partitions Tests ==============
Vector v1 = new Vector(0,4,0);
Vector v2 = v1.normalized();
+
+ //Test normalized result is correct
assertTrue("ERROR: normalized() function does not create a new vector",!v1.equals(v2));
}