public void CanFailToIntersectWhenRayParallelToPlane() { // Arrange var plane = new Plane() { Point = new Vector(0, 0, 0), Norm = new Vector(0, 1, 0) }; var ray = new Ray() { Start = new Vector(0, 1, 0), Direction = new Vector(1, 0, 0) }; // Act var isect = plane.Intersect(ray); // Assert Assert.IsNull(isect); }
public void CanFailToInsersectWithRayFacingAwayFromSphere() { // Arrange var sphere = new Sphere() { Center = new Vector(0, 0, 0), Radius = 1 }; var ray = new Ray() { Start = new Vector(0, 0, -2), Direction = new Vector(0, 0, -1) }; // Act var isect = sphere.Intersect(ray); // Assert Assert.IsNull(isect); }
public void CanFailToInsersectParallelLine() { // Arrange var line = new Line() { Point = new Vector(0, 1, 0), Direction = new Vector(1, 0, 0) }; var ray = new Ray() { Start = new Vector(0, 0, 0), Direction = new Vector(1, 0, 0) }; // Act var isect = line.Intersect(ray); // Assert Assert.IsNull(isect); }
public void CanInsersectPlane() { // Arrange var plane = new Plane() { Point = new Vector(0, 0, 0), Norm = new Vector(0, 1, 0) }; var ray = new Ray() { Start = new Vector(0, 1, 0), Direction = new Vector(0, -1, 0) }; // Act var isect = plane.Intersect(ray); // Assert Assert.NotNull(isect); Assert.AreEqual(1, isect.Distance); Assert.AreEqual(plane, isect.Element); }
public void CanIntersectEdgeOfSphere() { // Arrange var sphere = new Sphere() { Center = new Vector(0, 0, 0), Radius = 1 }; var ray = new Ray() { Start = new Vector(1, 0, -2), Direction = new Vector(0, 0, 1) }; // Act var isect = sphere.Intersect(ray); // Assert Assert.NotNull(isect); Assert.AreEqual(2, isect.Distance); Assert.AreEqual(sphere, isect.Element); }
public void CanInsersectLine() { // Arrange var line = new Line() { Point = new Vector(0, 1, 0), Direction = new Vector(1, 0, 0) }; var ray = new Ray() { Start = new Vector(0, 0, 0), Direction = new Vector(1, 1, 0) }; // Act var isect = line.Intersect(ray); // Assert Assert.NotNull(isect); Assert.AreEqual(Math.Sqrt(2), isect.Distance); Assert.AreEqual(line, isect.Element); }
/// <summary> /// Trace a ray through the scene /// </summary> /// <param name="ray">The ray to test</param> /// <param name="scene">The scene description</param> /// <param name="depth">The current depth of recursion</param> /// <returns>The color of the pixel</returns> private Color TraceRay(Ray ray, Scene scene, int depth) { var intersection = scene.ClosestIntersection(ray); if (intersection == null) { return Color.Background; } return this.Shade(intersection, scene, depth); }
public void CanInsersectPolygon_Square_NearTopRight() { // Arrange var points = new List<Vector>() { new Vector(0, 0, 0), new Vector(1, 0, 0), new Vector(1, 1, 0), new Vector(0, 1, 0) }; var poly = new Polygon(points, new Vector(0, 0, 1)); var ray = new Ray() { Start = new Vector(0.9, 0.9, 0.5), Direction = new Vector(0, 0, -1) }; // Act var isect = poly.Intersect(ray); // Assert Assert.NotNull(isect); Assert.AreEqual(0.5, isect.Distance); Assert.AreEqual(poly, isect.Element); }
public void CanFindPointOfIntersection() { // Arrange var points = new List<Vector>() { new Vector(0, 0, 0), new Vector(1, 0, 0), new Vector(1, 1, 0), new Vector(0, 1, 0) }; var poly = new Polygon(points, new Vector(0, 0, 1)); var ray = new Ray() { Start = new Vector(0.5, 0.5, 0.5), Direction = new Vector(0, 0, -1) }; // Act var isect = poly.GetPointOfIntersectionWithPlane(ray); // Assert Assert.NotNull(isect); Assert.AreEqual(0.5, isect.X); Assert.AreEqual(0.5, isect.Y); }
public void CanFailToIntersectPlaneWithParallelRay() { // Arrange var points = new List<Vector>() { new Vector(0, 0, 0), new Vector(1, 0, 0), new Vector(1, 1, 0), new Vector(0, 1, 0) }; var poly = new Polygon(points, new Vector(0, 0, 1)); var ray = new Ray() { Start = new Vector(1, 0, 0.5), Direction = new Vector(1, 0, 0) }; // Act var isect = poly.GetPointOfIntersectionWithPlane(ray); // Assert Assert.IsNull(isect); }
public void CanFailToInsersectTheBackFaceOfAPolygon() { // Arrange var points = new List<Vector>() { new Vector(0, 0, 0), new Vector(1, 0, 0), new Vector(1, 1, 0), new Vector(0, 1, 0) }; var poly = new Polygon(points, new Vector(0, 0, 1)); var ray = new Ray() { Start = new Vector(0.5, 0.5, -0.5), Direction = new Vector(0, 0, 1) }; // Act var isect = poly.Intersect(ray); // Assert Assert.IsNull(isect); }
public void CanFailToInsersectPolygon_Square_NegativeMiss() { // Arrange var points = new List<Vector>() { new Vector(0, 0, 0), new Vector(1, 0, 0), new Vector(1, 1, 0), new Vector(0, 1, 0) }; var poly = new Polygon(points, new Vector(0, 0, 1)); var ray = new Ray() { Start = new Vector(-0.5, 0.5, 0.5), Direction = new Vector(0, 0, -1) }; // Act var isect = poly.Intersect(ray); // Assert Assert.IsNull(isect); }