#include <iostream>

#include "catch.hpp"
#include "sconvcol.h"

using namespace std;

TEST_CASE("Plane Distance", "[sconvcol]") {
	sch_plane plane = {simd4f_create (0.f, 0.f, 0.f, 0.f), simd4f_create(0.f, 1.f, 0.f, 0.f)};

	simd4f point = simd4f_create (0.f, 0.23f, 0.f, 0.f);

	float dist = sch_plane_distance (&plane, &point);

	REQUIRE (dist == 0.23f);
}

TEST_CASE("Edge Get Direction", "[sconvcol]") {
	simd4f vertices[4] = {
		simd4f_create (-1.f, -1.f, 0.f, 0.f),
		simd4f_create ( 1.f, -1.f, 0.f, 0.f),
		simd4f_create ( 1.f,  1.f, 0.f, 0.f),
		simd4f_create (-1.f,  1.f, 0.f, 0.f)
	};

	sch_face face = {0};
	sch_create_face (4, vertices, &face);

	simd4f dir;
	sch_edge_get_dir (face.edge, &dir);

	simd4f dir_ref = simd4f_create (1.f, 0.f, 0.f, 0.f);
	REQUIRE (1.f == simd4f_dot3_scalar (dir, dir_ref));
}

TEST_CASE("Hull Get Plane", "[sconvcol]") {
	simd4f vertices_pos_z[4] = {
		simd4f_create (-1.f, -1.f, 1.f, 0.f),
		simd4f_create ( 1.f, -1.f, 1.f, 0.f),
		simd4f_create ( 1.f,  1.f, 1.f, 0.f),
		simd4f_create (-1.f,  1.f, 1.f, 0.f)
	};

	sch_face face_pos_z = {0};
	sch_create_face (4, vertices_pos_z, &face_pos_z);

	simd4f vertices_pos_x[4] = {
		simd4f_create (1.f, -1.f,  1.f, 0.f),
		simd4f_create (1.f, -1.f, -1.f, 0.f),
		simd4f_create (1.f,  1.f, -1.f, 0.f),
		simd4f_create (1.f,  1.f,  1.f, 0.f)
	};

	sch_face face_pos_x = {0};
	sch_create_face (4, vertices_pos_x, &face_pos_x);

	sch_hull hull = {0};
	sch_face faces[2] = { face_pos_z, face_pos_x };
	sch_create_hull (2, faces, &hull);

	sch_plane plane_pos_z;
	sch_hull_get_plane (&hull, 0, &plane_pos_z);

	simd4f point = simd4f_create (0.f, 0.f, 1.23f, 0.f);

	float dist = sch_plane_distance (&plane_pos_z, &point);

	REQUIRE_THAT(dist, Catch::Matchers::WithinRel(0.23, 1.0e-3));
}

