Huge speed up by changing how Intersect are shared.

This commit is contained in:
Godzil
2020-03-12 00:11:26 +00:00
parent 0aa949c60b
commit b00bb75189
38 changed files with 116 additions and 153 deletions

View File

@@ -32,7 +32,8 @@ int main()
double worldX = -(wallSize / 2) + pixelSize * x;
Point position = Point(worldX, worldY, wallDistance);
Ray r = Ray(cameraOrigin, (position - cameraOrigin).normalise());
Intersect xs = s.intersect(r);
Intersect xs;
s.intersect(r, xs);
if (!xs.hit().nothing())
{

View File

@@ -36,7 +36,8 @@ int main()
double worldX = -(wallSize / 2) + pixelSize * x;
Point position = Point(worldX, worldY, wallDistance);
Ray r = Ray(cameraOrigin, (position - cameraOrigin).normalise());
Intersect xs = s.intersect(r);
Intersect xs;
s.intersect(r, xs);
Intersection hit = xs.hit();

View File

@@ -46,7 +46,7 @@ TEST(ConeTest, Intersecting_a_cone_with_a_ray)
Tuple direction = Directions[i].normalise();
Ray r = Ray(Origins[i], direction);
Intersect xs = cone.intersect(r);
Intersect xs; cone.intersect(r, xs);
/* Temporary lower the precision */
set_equal_precision(0.00001);
@@ -64,7 +64,7 @@ TEST(ConeTest, Intersecting_a_cone_with_a_ray_parall_to_one_of_its_halves)
Cone cone = Cone();
Tuple direction = Vector(0, 1, 1).normalise();
Ray r = Ray(Point(0, 0, -1), direction);
Intersect xs = cone.intersect(r);
Intersect xs; cone.intersect(r, xs);
ASSERT_EQ(xs.count(), 1);
/* Temporary lower the precision */
@@ -102,7 +102,8 @@ TEST(ConeTest, Intersecting_a_cone_end_cap)
Tuple direction = Directions[i].normalise();
Ray r = Ray(Origins[i], direction);
Intersect xs = cone.intersect(r);
Intersect xs;
cone.intersect(r, xs);
ASSERT_EQ(xs.count(), Counts[i]);
}

View File

@@ -18,8 +18,8 @@
class CSGTest : public CSG
{
public:
Intersect doLocalIntersect(Ray r) {
return this->localIntersect(r);
void doLocalIntersect(Ray r, Intersect &xs) {
return this->localIntersect(r, xs);
};
Tuple doLocalNormalAt(Tuple point, Intersection *hit = nullptr) {
return this->localNormalAt(point, hit);
@@ -202,7 +202,7 @@ TEST(CSGTest, A_ray_misses_a_csg_object)
CSGTest c = CSGTest(CSG::UNION, &s1, &s2);
Ray r = Ray(Point(0, 2, -5), Vector(0, 0, 1));
Intersect xs = c.doLocalIntersect(r);
Intersect xs; c.doLocalIntersect(r, xs);
ASSERT_EQ(xs.count(), 0);
}
@@ -217,7 +217,7 @@ TEST(CSGTest, A_ray_hits_a_csg_object)
CSGTest c = CSGTest(CSG::UNION, &s1, &s2);
Ray r = Ray(Point(0, 0, -5), Vector(0, 0, 1));
Intersect xs = c.doLocalIntersect(r);
Intersect xs; c.doLocalIntersect(r, xs);
ASSERT_EQ(xs.count(), 2);
ASSERT_TRUE(double_equal(xs[0].t, 4));

View File

@@ -43,7 +43,7 @@ TEST(CubeTest, A_ray_intersects_a_cube)
for(i = 0; i < 7; i++)
{
Ray r = Ray(Origins[i], Directions[i]);
Intersect xs = c.intersect(r);
Intersect xs; c.intersect(r, xs);
ASSERT_EQ(xs.count(), 2);
EXPECT_EQ(xs[0].t, t1[i]);
@@ -77,7 +77,7 @@ TEST(CubeTest, A_ray_miss_a_cube)
for(i = 0; i < 6; i++)
{
Ray r = Ray(Origins[i], Directions[i]);
Intersect xs = c.intersect(r);
Intersect xs; c.intersect(r, xs);
ASSERT_EQ(xs.count(), 0);
}

View File

@@ -34,7 +34,7 @@ TEST(CylinderTest, A_ray_miss_a_cylinder)
Tuple direction = Directions[i].normalise();
Ray r = Ray(Origins[i], direction);
Intersect xs = cyl.intersect(r);
Intersect xs; cyl.intersect(r, xs);
ASSERT_EQ(xs.count(), 0);
}
@@ -65,7 +65,7 @@ TEST(CylinderTest, A_ray_hit_a_cylinder)
Tuple direction = Directions[i].normalise();
Ray r = Ray(Origins[i], direction);
Intersect xs = cyl.intersect(r);
Intersect xs; cyl.intersect(r, xs);
/* Temporary lower the precision */
set_equal_precision(0.00001);
@@ -142,7 +142,7 @@ TEST(CylinderTest, Intersecting_a_constrained_cylinder)
Tuple direction = Directions[i].normalise();
Ray r = Ray(Origins[i], direction);
Intersect xs = cyl.intersect(r);
Intersect xs; cyl.intersect(r, xs);
ASSERT_EQ(xs.count(), Counts[i]);
}
@@ -184,7 +184,7 @@ TEST(CylinderTest, Intersecting_the_caps_of_a_close_cylinder)
Tuple direction = Directions[i].normalise();
Ray r = Ray(Origins[i], direction);
Intersect xs = cyl.intersect(r);
Intersect xs; cyl.intersect(r, xs);
ASSERT_EQ(xs.count(), Counts[i]);
}
}

View File

@@ -39,7 +39,7 @@ TEST(GroupTest, Intersecting_a_ray_with_an_empty_group)
{
Group g = Group();
Ray r = Ray(Point(0, 0, 0), Vector(0, 0, 1));
Intersect xs = g.intersect(r);
Intersect xs; g.intersect(r, xs);
ASSERT_EQ(xs.count(), 0);
}
@@ -58,7 +58,7 @@ TEST(GroupTest, Intersecting_a_ray_with_an_nonempty_group)
g.addObject(&s3);
Ray r = Ray(Point(0, 0, -5), Vector(0, 0, 1));
Intersect xs = g.intersect(r);
Intersect xs; g.intersect(r, xs);
ASSERT_EQ(xs.count(), 4);
EXPECT_EQ(xs[0].object, &s2);
EXPECT_EQ(xs[1].object, &s2);
@@ -77,7 +77,7 @@ TEST(GroupTest, Intersecting_a_transformed_group)
g.addObject(&s);
Ray r = Ray(Point(10, 0, -50), Vector(0, 0, 1));
Intersect xs = g.intersect(r);
Intersect xs; g.intersect(r, xs);
ASSERT_EQ(xs.count(), 2);
}

View File

@@ -57,7 +57,7 @@ TEST(IntersectTest, Intersect_sets_the_object_on_the_intersection)
{
Ray r = Ray(Point(0, 0, -5), Vector(0, 0, 1));
Sphere s = Sphere();
Intersect xs = s.intersect(r);
Intersect xs; s.intersect(r, xs);
ASSERT_EQ(xs.count(), 2);
ASSERT_EQ(xs[0].object, (Shape *)&s);

View File

@@ -31,7 +31,7 @@ TEST(PlaneTest, Intersect_with_a_ray_parallel_to_the_plane)
Plane p = Plane();
Ray r = Ray(Point(0, 10, 0), Vector(0, 0, 1));
Intersect xs = p.intersect(r);
Intersect xs; p.intersect(r, xs);
ASSERT_EQ(xs.count(), 0);
}
@@ -41,7 +41,7 @@ TEST(PlaneTest, Intersect_with_a_coplanar_ray)
Plane p = Plane();
Ray r = Ray(Point(0, 0, 0), Vector(0, 0, 1));
Intersect xs = p.intersect(r);
Intersect xs; p.intersect(r, xs);
ASSERT_EQ(xs.count(), 0);
}
@@ -51,7 +51,7 @@ TEST(PlaneTest, A_ray_intersecting_a_plane_from_above)
Plane p = Plane();
Ray r = Ray(Point(0, 1, 0), Vector(0, -1, 0));
Intersect xs = p.intersect(r);
Intersect xs; p.intersect(r, xs);
ASSERT_EQ(xs.count(), 1);
ASSERT_EQ(xs[0].t, 1);
@@ -63,7 +63,7 @@ TEST(PlaneTest, A_ray_intersecting_a_plane_from_below)
Plane p = Plane();
Ray r = Ray(Point(0, -1, 0), Vector(0, 1, 0));
Intersect xs = p.intersect(r);
Intersect xs; p.intersect(r, xs);
ASSERT_EQ(xs.count(), 1);
ASSERT_EQ(xs[0].t, 1);

View File

@@ -53,7 +53,7 @@ TEST(ShapeTest, Intersecting_a_scaled_shape_with_a_ray)
TestShape s = TestShape();
s.setTransform(scaling(2, 2, 2));
Intersect xs = s.intersect(r);
Intersect xs; s.intersect(r, xs);
ASSERT_EQ(s.localRay.origin, Point(0, 0, -2.5));
ASSERT_EQ(s.localRay.direction, Vector(0, 0, 0.5));
@@ -65,7 +65,7 @@ TEST(ShapeTest, Intersecting_a_translated_shape_with_a_ray)
TestShape s = TestShape();
s.setTransform(translation(5, 0, 0));
Intersect xs = s.intersect(r);
Intersect xs; s.intersect(r, xs);
ASSERT_EQ(s.localRay.origin, Point(-5, 0, -5));
ASSERT_EQ(s.localRay.direction, Vector(0, 0, 1));

View File

@@ -14,9 +14,9 @@ class SmoothTriTest : public SmoothTriangle
{
public:
SmoothTriTest(Point p1, Point p2, Point p3, Vector n1, Vector n2, Vector n3) : SmoothTriangle(p1, p2, p3, n1, n2, n3) {};
Intersect doLocalIntersect(Ray ray)
void doLocalIntersect(Ray ray, Intersect &xs)
{
return this->localIntersect(ray);
this->localIntersect(ray, xs);
};
Tuple doLocalNormalAt(Tuple point, Intersection *hit)
@@ -48,7 +48,7 @@ TEST(SmoothTriangleTest, An_intersection_with_a_smooth_triangle_stores_u_v)
{
Ray r = Ray(Point(-0.2, 0.3, -2), Vector(0, 0, 1));
Intersect xs = tri.doLocalIntersect(r);
Intersect xs; tri.doLocalIntersect(r, xs);
ASSERT_TRUE(double_equal(xs[0].u, 0.45));
ASSERT_TRUE(double_equal(xs[0].v, 0.25));

View File

@@ -17,7 +17,7 @@ TEST(SphereTest, A_ray_intersect_a_sphere_at_two_points)
{
Ray r = Ray(Point(0, 0, -5), Vector(0, 0, 1));
Sphere s = Sphere();
Intersect xs = s.intersect(r);
Intersect xs; s.intersect(r, xs);
ASSERT_EQ(xs.count(), 2);
ASSERT_EQ(xs[0].t, 4.0);
@@ -28,7 +28,7 @@ TEST(SphereTest, A_ray_intersect_a_sphere_at_a_tangent)
{
Ray r = Ray(Point(0, 1, -5), Vector(0, 0, 1));
Sphere s = Sphere();
Intersect xs = s.intersect(r);
Intersect xs; s.intersect(r, xs);
ASSERT_EQ(xs.count(), 2);
ASSERT_EQ(xs[0].t, 5.0);
@@ -39,7 +39,7 @@ TEST(SphereTest, A_ray_miss_a_sphere)
{
Ray r = Ray(Point(0, 2, -5), Vector(0, 0, 1));
Sphere s = Sphere();
Intersect xs = s.intersect(r);
Intersect xs; s.intersect(r, xs);
ASSERT_EQ(xs.count(), 0);
}
@@ -48,7 +48,7 @@ TEST(SphereTest, A_ray_originate_inside_a_sphere)
{
Ray r = Ray(Point(0, 0, 0), Vector(0, 0, 1));
Sphere s = Sphere();
Intersect xs = s.intersect(r);
Intersect xs; s.intersect(r, xs);
ASSERT_EQ(xs.count(), 2);
ASSERT_EQ(xs[0].t, -1.0);
@@ -59,7 +59,7 @@ TEST(SphereTest, A_sphere_is_behind_a_ray)
{
Ray r = Ray(Point(0, 0, 5), Vector(0, 0, 1));
Sphere s = Sphere();
Intersect xs = s.intersect(r);
Intersect xs; s.intersect(r, xs);
ASSERT_EQ(xs.count(), 2);
ASSERT_EQ(xs[0].t, -6.0);
@@ -89,7 +89,7 @@ TEST(SphereTest, Intersecting_a_scaled_sphere_with_a_ray)
s.setTransform(scaling(2, 2, 2));
Intersect xs = s.intersect(r);
Intersect xs; s.intersect(r, xs);
ASSERT_EQ(xs.count(), 2);
ASSERT_EQ(xs[0].t, 3.0);
@@ -103,7 +103,7 @@ TEST(SphereTest, Intersecting_a_translated_sphere_with_a_ray)
s.setTransform(translation(5, 0, 0));
Intersect xs = s.intersect(r);
Intersect xs; s.intersect(r, xs);
ASSERT_EQ(xs.count(), 0);
}

View File

@@ -44,7 +44,7 @@ TEST(TriangleTest, Intersecting_a_ray_parallel_to_the_triangle)
Triangle t = Triangle(Point(0, 1, 0), Point(-1, 0, 0), Point(1, 0, 0));
Ray r = Ray(Point(0, -1, -2), Vector(0, 1, 0));
Intersect xs = t.intersect(r);
Intersect xs; t.intersect(r, xs);
ASSERT_EQ(xs.count(), 0);
}
@@ -54,7 +54,7 @@ TEST(TriangleTest, A_ray_miss_the_p1_p3_edge)
Triangle t = Triangle(Point(0, 1, 0), Point(-1, 0, 0), Point(1, 0, 0));
Ray r = Ray(Point(1, 1, -2), Vector(0, 0, 1));
Intersect xs = t.intersect(r);
Intersect xs; t.intersect(r, xs);
ASSERT_EQ(xs.count(), 0);
}
@@ -64,7 +64,7 @@ TEST(TriangleTest, A_ray_miss_the_p1_p2_edge)
Triangle t = Triangle(Point(0, 1, 0), Point(-1, 0, 0), Point(1, 0, 0));
Ray r = Ray(Point(-1, 1, -2), Vector(0, 0, 1));
Intersect xs = t.intersect(r);
Intersect xs; t.intersect(r, xs);
ASSERT_EQ(xs.count(), 0);
}
@@ -74,7 +74,7 @@ TEST(TriangleTest, A_ray_miss_the_p2_p3_edge)
Triangle t = Triangle(Point(0, 1, 0), Point(-1, 0, 0), Point(1, 0, 0));
Ray r = Ray(Point(0, -1, -2), Vector(0, 0, 1));
Intersect xs = t.intersect(r);
Intersect xs; t.intersect(r, xs);
ASSERT_EQ(xs.count(), 0);
}
@@ -84,7 +84,7 @@ TEST(TriangleTest, A_ray_strikes_a_triangle)
Triangle t = Triangle(Point(0, 1, 0), Point(-1, 0, 0), Point(1, 0, 0));
Ray r = Ray(Point(0, .5, -2), Vector(0, 0, 1));
Intersect xs = t.intersect(r);
Intersect xs; t.intersect(r, xs);
ASSERT_EQ(xs.count(), 1);
EXPECT_EQ(xs[0].t, 2);

View File

@@ -54,7 +54,8 @@ TEST(WorldTest, Intersect_a_world_with_a_ray)
World w = DefaultWorld();
Ray r = Ray(Point(0, 0, -5), Vector(0, 0, 1));
Intersect xs = w.intersect(r);
Intersect xs;
w.intersect(r, xs);
ASSERT_EQ(xs.count(), 4);
ASSERT_EQ(xs[0].t, 4);