From 893176a0b18a2281abe09def716ccc3db5583c3f Mon Sep 17 00:00:00 2001 From: Julian T Date: Sun, 26 Jul 2020 12:56:27 +0200 Subject: Implemented object intersection and startet work on render gui --- test/main.cpp | 2 ++ test/object.cpp | 36 ++++++++++++++++++++++++++++++++++++ test/vector.cpp | 31 +++++++++++++++++++++++++++++++ 3 files changed, 69 insertions(+) create mode 100644 test/main.cpp create mode 100644 test/object.cpp create mode 100644 test/vector.cpp (limited to 'test') diff --git a/test/main.cpp b/test/main.cpp new file mode 100644 index 0000000..4ed06df --- /dev/null +++ b/test/main.cpp @@ -0,0 +1,2 @@ +#define CATCH_CONFIG_MAIN +#include diff --git a/test/object.cpp b/test/object.cpp new file mode 100644 index 0000000..bbce055 --- /dev/null +++ b/test/object.cpp @@ -0,0 +1,36 @@ +#include +#include +#include +#include + +#include +#include + +TEST_CASE("Sphere normal at", "[sphere]") { + auto sph = Sphere(Vec3d(2, 3, 4), 2); + + auto norm = sph.norm_at(Vec3d(2, 3, 2), Vec3d()); + REQUIRE(norm.m_x == 0); + REQUIRE(norm.m_y == 0); + REQUIRE(norm.m_z == -1); +} + +TEST_CASE("Sphere intersect", "[sphere]") { + auto sph = Sphere(Vec3d(2, 3, 4), 2); + auto ray = Ray(Vec3d(1, 0, 0), Vec3d(0, 1, 1.5), true); + + auto dist = sph.intersect(ray, false); + REQUIRE(abs(dist - 3.28) < 0.01); +} + +TEST_CASE("Plane intersect", "[plane]") { + auto pln = Plane(Vec3d(3, 4, 2), Vec3d(-6, -3, -2)); + auto ray = Ray(Vec3d(0, 0, 0), Vec3d(-2, -1, 5)); + + auto dist = pln.intersect(ray, false); + REQUIRE(dist == -1); + + ray = Ray(Vec3d(-2, -2, 0), Vec3d(-2, -1, 5)); + dist = pln.intersect(ray, false); + REQUIRE(abs(dist - 20.4) < 0.1); +} diff --git a/test/vector.cpp b/test/vector.cpp new file mode 100644 index 0000000..61648c6 --- /dev/null +++ b/test/vector.cpp @@ -0,0 +1,31 @@ +#include +#include +#include + +TEST_CASE( "Vector length", "[vector]" ) { + auto vec = Vec3d(2, 4, 4); + REQUIRE(vec.length() == 6); + vec.set(0, 0, 0); + REQUIRE(vec.length() == 0); + vec.set(0, 3.5, 0); + REQUIRE(vec.length() == 3.5); +} + +TEST_CASE("Vector_normal", "[vector]") { + auto vec = Vec3d(4, 5, 4545); + REQUIRE(vec.length() != 1.0); + vec.normalize(); + REQUIRE(vec.length() - 1.0 < ZERO_APPROX); + vec.set(0, 0, 0); + REQUIRE_THROWS(vec.normalize()); +} + +TEST_CASE("Vector dot", "[vector]") { + auto a = Vec3d(4, 5, 6); + auto b = Vec3d(1, 2, 3); + REQUIRE(a.dot(b) == 32); + a.set(0, 0, 0); + REQUIRE(a.dot(b) == 0); + a.set(0, 5, 0); + REQUIRE(a.dot(b) == 10); +} -- cgit v1.2.3