Browse Source

And now we have cylinders!

Godzil 4 years ago
parent
commit
d87bbb184e

+ 5 - 1
README.md

@@ -48,4 +48,8 @@ From Chapter 11 - Reflections, Transparency & Refractions
 
 From Chapter 12 - Cubes:
 
-![Chapter 12 rendering test](output/ch12_test.png)
+![Chapter 12 rendering test](output/ch12_test.png)
+
+From Chapter 13 - Cylinders:
+
+![Chapter 13 rendering test](output/ch13_test.png)

BIN
output/ch13_test.png


+ 33 - 0
source/include/cylinder.h

@@ -0,0 +1,33 @@
+/*
+ *  DoRayMe - a quick and dirty Raytracer
+ *  Cylinder header
+ *
+ *  Created by Manoël Trapier
+ *  Copyright (c) 2020 986-Studio.
+ *
+ */
+#ifndef DORAYME_CYLINDER_H
+#define DORAYME_CYLINDER_H
+
+#include <shape.h>
+#include <ray.h>
+#include <intersect.h>
+
+class Cylinder : public Shape {
+private:
+    Intersect localIntersect(Ray r);
+
+    Tuple localNormalAt(Tuple point);
+
+    bool checkCap(Ray r, double t);
+    void intersectCaps(Ray r, Intersect &xs);
+
+public:
+    bool isClosed;
+    double minCap;
+    double maxCap;
+
+    Cylinder() : minCap(-INFINITY), maxCap(INFINITY), isClosed(false), Shape(SHAPE_CYLINDER) {};
+};
+
+#endif //DORAYME_CYLINDER_H

+ 1 - 0
source/include/shape.h

@@ -23,6 +23,7 @@ enum ShapeType
     SHAPE_SPHERE,
     SHAPE_PLANE,
     SHAPE_CUBE,
+    SHAPE_CYLINDER,
     SHAPE_CONE,
 };
 

+ 109 - 0
source/shapes/cylinder.cpp

@@ -0,0 +1,109 @@
+/*
+ *  DoRayMe - a quick and dirty Raytracer
+ *  Cylinder implementation
+ *
+ *  Created by Manoël Trapier
+ *  Copyright (c) 2020 986-Studio.
+ *
+ */
+#include <tuple.h>
+#include <ray.h>
+#include <shape.h>
+#include <cylinder.h>
+#include <math_helper.h>
+
+bool Cylinder::checkCap(Ray r, double t)
+{
+    /* Helping function to reduce duplication.
+     * Checks to see if the intersection ot t is within a radius
+     * of 1 (the radius of our cylinder from the y axis
+     */
+    double x = r.origin.x + t * r.direction.x;
+    double z = r.origin.z + t * r.direction.z;
+    return (x * x + z * z) <= 1;
+}
+
+void Cylinder::intersectCaps(Ray r, Intersect &xs)
+{
+    /* Caps only mattter is the cylinder is closed, and might possibly be
+     * intersected by the ray
+     */
+    if ((this->isClosed) && (fabs(r.direction.y) > getEpsilon()))
+    {
+        double t;
+        /* Check for an intersection with the lower end cap by intersecting
+         * the ray with the plan at y = this->minCap
+         */
+        t = (this->minCap - r.origin.y) / r.direction.y;
+        if (this->checkCap(r, t))
+        {
+            xs.add(Intersection(t, this));
+        }
+
+        /* Check for an intersection with the upper end cap by intersecting
+         * the ray with the plan at y = this->maxCap
+         */
+        t = (this->maxCap - r.origin.y) / r.direction.y;
+        if (this->checkCap(r, t))
+        {
+            xs.add(Intersection(t, this));
+        }
+    }
+}
+
+Intersect Cylinder::localIntersect(Ray r)
+{
+    Intersect ret;
+
+    double A = r.direction.x * r.direction.x + r.direction.z * r.direction.z;
+
+    /* Ray is parallel to the Y axis */
+    if (A >= getEpsilon())
+    {
+        double B = 2 * r.origin.x * r.direction.x +
+                   2 * r.origin.z * r.direction.z;
+        double C = r.origin.x * r.origin.x + r.origin.z * r.origin.z - 1;
+
+        double disc = B * B - 4 * A * C;
+
+        if (disc >= 0)
+        {
+            double t0 = (-B - sqrt(disc)) / (2 * A);
+            double t1 = (-B + sqrt(disc)) / (2 * A);
+
+            double y0 = r.origin.y + t0 * r.direction.y;
+            if ((this->minCap < y0) && (y0 < this->maxCap))
+            {
+                ret.add(Intersection(t0, this));
+            }
+
+            double y1 = r.origin.y + t1 * r.direction.y;
+            if ((this->minCap < y1) && (y1 < this->maxCap))
+            {
+                ret.add(Intersection(t1, this));
+            }
+        }
+    }
+
+    this->intersectCaps(r, ret);
+
+    return ret;
+}
+
+Tuple Cylinder::localNormalAt(Tuple point)
+{
+    /* Compute the square of the distance from the Y axis */
+    double dist = point.x * point.x + point.z * point.z;
+
+    if ((dist < 1) && (point.y >= (this->maxCap - getEpsilon())))
+    {
+        return Vector(0, 1, 0);
+    }
+
+    if ((dist < 1) && (point.y <= this->minCap + getEpsilon()))
+    {
+        return Vector(0, -1, 0);
+    }
+
+    return Vector(point.x, 0, point.z);
+}

+ 7 - 1
tests/CMakeLists.txt

@@ -5,7 +5,7 @@ find_package(Threads REQUIRED)
 
 set(TESTS_SRC math_test.cpp tuple_test.cpp colour_test.cpp canvas_test.cpp matrix_test.cpp transformation_test.cpp
         ray_test.cpp intersect_test.cpp sphere_test.cpp light_test.cpp material_test.cpp world_test.cpp camera_test.cpp
-        shape_test.cpp plane_test.cpp pattern_test.cpp cube_test.cpp)
+        shape_test.cpp plane_test.cpp pattern_test.cpp cube_test.cpp cylinder_test.cpp)
 
 add_executable(testMyRays)
 target_include_directories(testMyRays PUBLIC ${gtest_SOURCE_DIR}/include ${gtest_SOURCE_DIR})
@@ -69,6 +69,11 @@ target_include_directories(ch12_test PUBLIC ../source/include)
 target_sources(ch12_test PRIVATE ch12_test.cpp)
 target_link_libraries(ch12_test rayonnement)
 
+add_executable(ch13_test)
+target_include_directories(ch13_test PUBLIC ../source/include)
+target_sources(ch13_test PRIVATE ch13_test.cpp)
+target_link_libraries(ch13_test rayonnement)
+
 add_test(NAME Chapter05_Test COMMAND $<TARGET_FILE:ch5_test>)
 add_test(NAME Chapter06_Test COMMAND $<TARGET_FILE:ch6_test>)
 add_test(NAME Chapter07_Test COMMAND $<TARGET_FILE:ch7_test>)
@@ -78,4 +83,5 @@ add_test(NAME Chapter11_Reflection COMMAND $<TARGET_FILE:ch11_reflection>)
 add_test(NAME Chapter11_Refraction COMMAND $<TARGET_FILE:ch11_refraction>)
 add_test(NAME Chapter11_Test COMMAND $<TARGET_FILE:ch11_test>)
 add_test(NAME Chapter12_Test COMMAND $<TARGET_FILE:ch12_test>)
+add_test(NAME Chapter13_Test COMMAND $<TARGET_FILE:ch13_test>)
 add_test(NAME Hw3Render COMMAND $<TARGET_FILE:hw3render> ${CMAKE_CURRENT_SOURCE_DIR}/test.hw3scene)

+ 2 - 2
tests/ch12_test.cpp

@@ -120,7 +120,7 @@ int main()
 
     /* Little cube 1 */
     Cube lilCube1 = Cube();
-    lilCube1.setTransform(translation(1, 3.25, -0.9) *
+    lilCube1.setTransform(translation(1, 3.35, -0.9) *
                            rotationY(-0.4) *
                            scaling(0.15, 0.15, 0.15));
     lilCube1.material.colour = Colour(1, 0.5, 0.5);
@@ -199,7 +199,7 @@ int main()
     /* ----------------------------- */
 
     /* Set the camera */
-    Camera camera = Camera(400, 200, 0.785);
+    Camera camera = Camera(1000, 500, 0.785);
     camera.setTransform(viewTransform(Point(8, 6, -8),
                                       Point(0, 3, 0),
                                       Vector(0, 1, 0)));

+ 191 - 0
tests/ch13_test.cpp

@@ -0,0 +1,191 @@
+/*
+ *  DoRayMe - a quick and dirty Raytracer
+ *  Render test for reflection in chapter 13.
+ *
+ *  Created by Manoël Trapier
+ *  Copyright (c) 2020 986-Studio.
+ *
+ */
+#include <world.h>
+#include <light.h>
+#include <sphere.h>
+#include <plane.h>
+#include <cube.h>
+#include <cylinder.h>
+#include <material.h>
+#include <colour.h>
+#include <canvas.h>
+#include <camera.h>
+
+#include <pattern.h>
+#include <strippattern.h>
+#include <gradientpattern.h>
+#include <checkerspattern.h>
+#include <ringpattern.h>
+
+#include <transformation.h>
+
+int main()
+{
+    World w = World();
+
+    /* Add light */
+    Light light = Light(POINT_LIGHT, Point(1, 6.9, -4.9), Colour(1, 1, 1));
+    w.addLight(&light);
+
+    /* ----------------------------- */
+
+    /* The floor / ceiling */
+    Plane floor = Plane();
+    floor.material.pattern = new CheckersPattern(Colour(0.5, 0.5, 0.5), Colour(0.75, 0.75, 0.75));
+    floor.material.pattern->setTransform(rotationY(0.3) * scaling(0.25, 0.25, 0.25));
+    w.addObject(&floor);
+
+    /* ----------------------------- */
+
+    Cylinder cyl1 = Cylinder();
+    cyl1.minCap = 0;
+    cyl1.maxCap = 0.75;
+    cyl1.isClosed = true;
+    cyl1.setTransform(translation(-1, 0, 1) * scaling(0.5, 1, 0.5));
+    cyl1.material.colour = Colour(0, 0, 0.6);
+    cyl1.material.diffuse = 0.1;
+    cyl1.material.specular = 0.9;
+    cyl1.material.shininess = 300;
+    cyl1.material.reflective = 0.9;
+    w.addObject(&cyl1);
+
+    /* ----------------------------- */
+
+    /* Concentrics */
+    Cylinder cons1 = Cylinder();
+    cons1.minCap = 0;
+    cons1.maxCap = 0.2;
+    cons1.isClosed = false;
+    cons1.setTransform(translation(1, 0, 0) * scaling(0.8, 1, 0.8));
+    cons1.material.colour = Colour(1, 1, 0.3);
+    cons1.material.ambient = 0.1;
+    cons1.material.diffuse = 0.8;
+    cons1.material.specular = 0.9;
+    cons1.material.shininess = 300;
+    w.addObject(&cons1);
+
+    Cylinder cons2 = Cylinder();
+    cons2.minCap = 0;
+    cons2.maxCap = 0.3;
+    cons2.isClosed = false;
+    cons2.setTransform(translation(1, 0, 0) * scaling(0.6, 1, 0.6));
+    cons2.material.colour = Colour(1, 0.9, 0.4);
+    cons2.material.ambient = 0.1;
+    cons2.material.diffuse = 0.8;
+    cons2.material.specular = 0.9;
+    cons2.material.shininess = 300;
+    w.addObject(&cons2);
+
+    Cylinder cons3 = Cylinder();
+    cons3.minCap = 0;
+    cons3.maxCap = 0.4;
+    cons3.isClosed = false;
+    cons3.setTransform(translation(1, 0, 0) * scaling(0.4, 1, 0.4));
+    cons3.material.colour = Colour(1, 0.8, 0.5);
+    cons3.material.ambient = 0.1;
+    cons3.material.diffuse = 0.8;
+    cons3.material.specular = 0.9;
+    cons3.material.shininess = 300;
+    w.addObject(&cons3);
+
+    Cylinder cons4 = Cylinder();
+    cons4.minCap = 0;
+    cons4.maxCap = 0.5;
+    cons4.isClosed = true;
+    cons4.setTransform(translation(1, 0, 0) * scaling(0.2, 1, 0.2));
+    cons4.material.colour = Colour(1, 0.7, 0.6);
+    cons4.material.ambient = 0.1;
+    cons4.material.diffuse = 0.8;
+    cons4.material.specular = 0.9;
+    cons4.material.shininess = 300;
+    w.addObject(&cons4);
+
+    /* ----------------------------- */
+
+    /* decoratives cylinders */
+    Cylinder deco1 = Cylinder();
+    deco1.minCap = 0;
+    deco1.maxCap = 0.3;
+    deco1.isClosed = true;
+    deco1.setTransform(translation(0, 0, -0.75) * scaling(0.05, 1, 0.05));
+    deco1.material.colour = Colour(1, 0, 0);
+    deco1.material.ambient = 0.1;
+    deco1.material.diffuse = 0.9;
+    deco1.material.specular = 0.9;
+    deco1.material.shininess = 300;
+    w.addObject(&deco1);
+
+    Cylinder deco2 = Cylinder();
+    deco2.minCap = 0;
+    deco2.maxCap = 0.3;
+    deco2.isClosed = true;
+    deco2.setTransform(translation(0, 0, -2.25) * rotationY(-0.15) * translation(0, 0, 1.5) * scaling(0.05, 1, 0.05));
+    deco2.material.colour = Colour(1, 1, 0);
+    deco2.material.ambient = 0.1;
+    deco2.material.diffuse = 0.9;
+    deco2.material.specular = 0.9;
+    deco2.material.shininess = 300;
+    w.addObject(&deco2);
+
+    Cylinder deco3 = Cylinder();
+    deco3.minCap = 0;
+    deco3.maxCap = 0.3;
+    deco3.isClosed = true;
+    deco3.setTransform(translation(0, 0, -2.25) * rotationY(-0.3) * translation(0, 0, 1.5) * scaling(0.05, 1, 0.05));
+    deco3.material.colour = Colour(0, 1, 0);
+    deco3.material.ambient = 0.1;
+    deco3.material.diffuse = 0.9;
+    deco3.material.specular = 0.9;
+    deco3.material.shininess = 300;
+    w.addObject(&deco3);
+
+    Cylinder deco4 = Cylinder();
+    deco4.minCap = 0;
+    deco4.maxCap = 0.3;
+    deco4.isClosed = true;
+    deco4.setTransform(translation(0, 0, -2.25) * rotationY(-0.45) * translation(0, 0, 1.5) * scaling(0.05, 1, 0.05));
+    deco4.material.colour = Colour(0, 1, 1);
+    deco4.material.ambient = 0.1;
+    deco4.material.diffuse = 0.9;
+    deco4.material.specular = 0.9;
+    deco4.material.shininess = 300;
+    w.addObject(&deco4);
+
+    /* ----------------------------- */
+
+    /* glass cylinder */
+    Cylinder glassCylinder = Cylinder();
+    glassCylinder.minCap = 0.0001;
+    glassCylinder.maxCap = 0.5;
+    glassCylinder.isClosed = true;
+    glassCylinder.setTransform(translation(0, 0, -1.5) * scaling(0.33, 1, 0.33));
+    glassCylinder.material.colour = Colour(0.25, 0, 0);
+    glassCylinder.material.diffuse = 0.1;
+    glassCylinder.material.specular = 0.9;
+    glassCylinder.material.shininess = 300;
+    glassCylinder.material.reflective = 0.9;
+    glassCylinder.material.transparency = 0.9;
+    glassCylinder.material.refractiveIndex = 1.5;
+    w.addObject(&glassCylinder);
+
+    /* ----------------------------- */
+
+    /* Set the camera */
+    Camera camera = Camera(400, 200, 0.314);
+    camera.setTransform(viewTransform(Point(8, 3.5, -9),
+                                      Point(0, 0.3, 0),
+                                      Vector(0, 1, 0)));
+
+    /* Now render it */
+    Canvas image = camera.render(w, 20);
+
+    image.SaveAsPNG("ch13_test.png");
+
+    return 0;
+}

+ 223 - 0
tests/cylinder_test.cpp

@@ -0,0 +1,223 @@
+/*
+ *  DoRayMe - a quick and dirty Raytracer
+ *  Cylinder unit tests
+ *
+ *  Created by Manoël Trapier
+ *  Copyright (c) 2020 986-Studio.
+ *
+ */
+#include <intersect.h>
+#include <intersection.h>
+#include <cylinder.h>
+#include <transformation.h>
+#include <gtest/gtest.h>
+
+TEST(CylinderTest, A_ray_miss_a_cylinder)
+{
+    Cylinder cyl = Cylinder();
+
+    Point Origins[] = {
+            Point(1, 0, 0),
+            Point(0, 0, 0),
+            Point(0, 0, -5),
+    };
+
+    Vector Directions[] = {
+            Vector(0, 1, 0),
+            Vector(0, 1, 0),
+            Vector(1, 1, 1),
+    };
+
+    int i;
+    for(i = 0; i < 3; i++)
+    {
+        Tuple direction = Directions[i].normalise();
+        Ray r = Ray(Origins[i], direction);
+
+        Intersect xs = cyl.intersect(r);
+
+        ASSERT_EQ(xs.count(), 0);
+    }
+}
+
+TEST(CylinderTest, A_ray_hit_a_cylinder)
+{
+    Cylinder cyl = Cylinder();
+
+    Point Origins[] = {
+            Point(1, 0, -5),
+            Point(0, 0, -5),
+            Point(0.5, 0, -5),
+    };
+
+    Vector Directions[] = {
+            Vector(0, 0, 1),
+            Vector(0, 0, 1),
+            Vector(0.1, 1, 1),
+    };
+
+    double t0s[] = { 5, 4, 6.80798 };
+    double t1s[] = { 5, 6, 7.08872 };
+
+    int i, j;
+    for(i = 0; i < 3; i++)
+    {
+        Tuple direction = Directions[i].normalise();
+        Ray r = Ray(Origins[i], direction);
+
+        Intersect xs = cyl.intersect(r);
+
+        /* Temporary lower the precision */
+        set_equal_precision(0.00001);
+
+        ASSERT_EQ(xs.count(), 2);
+        EXPECT_TRUE(double_equal(xs[0].t, t0s[i]));
+        EXPECT_TRUE(double_equal(xs[1].t, t1s[i]));
+
+        set_equal_precision(FLT_EPSILON);
+    }
+}
+
+TEST(CylinderTest, Normal_vector_on_a_cylinder)
+{
+    Cylinder cyl = Cylinder();
+
+    Point HitPointss[] = {
+            Point(1, 0, 0),
+            Point(0, 5, -1),
+            Point(0, -2, 1),
+            Point(-1, 1, 0),
+    };
+
+    Vector Normals[] = {
+            Vector(1, 0, 0),
+            Vector(0, 0, -1),
+            Vector(0, 0, 1),
+            Vector(-1, 0, 0),
+    };
+
+    int i;
+    for(i = 0; i < 4; i++)
+    {
+        ASSERT_EQ(cyl.normalAt(HitPointss[i]), Normals[i]);
+    }
+}
+
+TEST(CylinderTest, The_default_minimum_and_maximum_for_a_cylinder)
+{
+    Cylinder cyl = Cylinder();
+    ASSERT_EQ(cyl.minCap, -INFINITY);
+    ASSERT_EQ(cyl.maxCap, INFINITY);
+}
+
+TEST(CylinderTest, Intersecting_a_constrained_cylinder)
+{
+    Point Origins[] = {
+            Point(0, 1.5, 0),
+            Point(0, 3, -5),
+            Point(0, 0, -5),
+            Point(0, 2, -5),
+            Point(0, 1, -5),
+            Point(0, 1.5, -2),
+    };
+
+    Vector Directions[] = {
+            Vector(0.1, 1, 0),
+            Vector(0, 0, 1),
+            Vector(0, 0, 1),
+            Vector(0, 0, 1),
+            Vector(0, 0, 1),
+            Vector(0., 0, 1),
+    };
+
+    uint32_t Counts[] = { 0, 0, 0, 0, 0, 2 };
+
+    Cylinder cyl = Cylinder();
+    cyl.minCap = 1;
+    cyl.maxCap = 2;
+
+    int i;
+    for(i = 0; i < 6; i++)
+    {
+        Tuple direction = Directions[i].normalise();
+        Ray r = Ray(Origins[i], direction);
+
+        Intersect xs = cyl.intersect(r);
+
+        ASSERT_EQ(xs.count(), Counts[i]);
+    }
+}
+
+TEST(CylinderTest, The_default_closed_value_for_a_cylinder)
+{
+    Cylinder cyl = Cylinder();
+    ASSERT_EQ(cyl.isClosed, false);
+}
+TEST(CylinderTest, Intersecting_the_caps_of_a_close_cylinder)
+{
+    Point Origins[] = {
+            Point(0, 3, 0),
+            Point(0, 3, -2),
+            Point(0, 4, -2),  /* Edge case */
+            Point(0, 0, -5),
+            Point(0, -1, -2), /* Edge case */
+    };
+
+    Vector Directions[] = {
+            Vector(0, -1, 0),
+            Vector(0, -1, 2),
+            Vector(0, -1, 1),
+            Vector(0, 1, 2),
+            Vector(0, 1, 1),
+     };
+
+    uint32_t Counts[] = { 2, 2, 2, 2, 2 };
+
+    Cylinder cyl = Cylinder();
+    cyl.minCap = 1;
+    cyl.maxCap = 2;
+    cyl.isClosed = true;
+
+    int i;
+    for(i = 0; i < 5; i++)
+    {
+        Tuple direction = Directions[i].normalise();
+        Ray r = Ray(Origins[i], direction);
+
+        Intersect xs = cyl.intersect(r);
+        ASSERT_EQ(xs.count(), Counts[i]);
+    }
+}
+
+TEST(CylinderTest, The_normal_on_a_cylinder_end_cap)
+{
+    Cylinder cyl = Cylinder();
+
+    Point HitPointss[] = {
+            Point(0, 1, 0),
+            Point(0.5, 1, 0),
+            Point(0, 1, 0.5),
+            Point(0, 2, 0),
+            Point(0.5, 2, 0),
+            Point(0, 2, 0.5),
+    };
+
+    Vector Normals[] = {
+            Vector(0, -1, 0),
+            Vector(0, -1, 0),
+            Vector(0, -1, 0),
+            Vector(0, 1, 0),
+            Vector(0, 1, 0),
+            Vector(0, 1, 0),
+    };
+
+    cyl.minCap = 1;
+    cyl.maxCap = 2;
+    cyl.isClosed = true;
+
+    int idx;
+    for(idx = 0; idx < 6; idx++)
+    {
+        ASSERT_EQ(cyl.normalAt(HitPointss[idx]), Normals[idx]);
+    }
+}