#include <utils/maths.h>

Vec3 g_zeroVector = Vec3(0.0f, 0.0f, 0.0f);
Vec3 g_identityVector = Vec3(1.0f, 1.0f, 1.0f);
Matrix g_identityMatrix = { 1.0f, 0.0f, 0.0f, 0.0f,
							0.0f, 1.0f, 0.0f, 0.0f,
							0.0f, 0.0f, 1.0f, 0.0f,
							0.0f, 0.0f, 0.0f, 1.0f };

BoundingBox g_identityBoundingBox = { { -0.1f, -0.1f, -0.1f }, { 0.1f, 0.1f, 0.1f } };

void TransformBoundingBox(BoundingBox& bbox, const glm::mat4& model)
{
#if 1
	// transform to center/extents box representation
	glm::vec3 center = (bbox.max + bbox.min) * glm::vec3(0.5);
	glm::vec3 extents = bbox.max - center;

	// transform center
	glm::vec3 t_center = glm::vec3(model * glm::vec4(center, 1.0));

	// transform extents (take maximum)
	glm::mat3 abs_mat = glm::mat3(abs(glm::vec3(model[0])), abs(glm::vec3(model[1])), abs(glm::vec3(model[2])));
	glm::vec3 t_extents = abs_mat * extents;

	// transform to min/max box representation
	glm::vec3 tmin = t_center - t_extents;
	glm::vec3 tmax = t_center + t_extents;

	bbox.min = tmin;
	bbox.max = tmax;
#else

	glm::mat3 M(model);

	// Split the transform into a translation vector (T) and a 3x3 rotation (M).
	BoundingBox B = {}; // zero-volume AABB at T
	for (int i = 0, j = 0; i < 3, j < 3; i++, j++)
	{
		float a = M[i][j] * bbox.min[j];
		float b = M[i][j] * bbox.max[j];
		B.min[i] += a < b ? a : b;
		B.max[i] += a < b ? b : a;
	}

	bbox.min = B.min;
	bbox.max = B.max;
#endif
}

void TransformOBB(OBB& obb, const BoundingBox& bbox, const glm::mat4& model)
{
	// transform to center/extents box representation
	glm::vec3 center = (bbox.max + bbox.min) * glm::vec3(0.5);
	glm::vec3 extents = bbox.max - center;

	// transform center
	glm::vec3 t_center = glm::vec3(model * glm::vec4(center, 1.0));

	
}
