<commit_before>#pragma once

#include <vector>
#include "Component.h"

struct BoundingBox {
	Point3 position;
	Point3 size;
	std::vector<BoundingBox> children;

	BoundingBox() {}
	BoundingBox(const Point3 &position, const Point3 &size) : position(position), size(size) {}

	inline bool AABBCheck(const BoundingBox &b, const Point3 &ownPos, const Point3 &bPos) const {
		Point3 aBegin = position + ownPos;
		Point3 bBegin = b.position + bPos;
		auto aEnd = aBegin + size;
		auto bEnd = bBegin + b.size;

		return !(
			aEnd.x < bBegin.x ||
			aEnd.y < bBegin.y ||
			aEnd.z < bBegin.z ||
			aBegin.x > bEnd.x ||
			aBegin.y > bEnd.y ||
			aBegin.z > bEnd.z);
	}

	std::pair<float, Point3> AABBSwept(const BoundingBox &b, const std::tuple<Point3, Point3, Point3> &own, const Point3 &bPos) const {
		std::pair<float, Point3> result = std::make_pair(1.0f, Point3(0.0f));

		auto &ownPos = std::get<0>(own);
		auto &ownPos2 = std::get<1>(own);
		auto &ownVel = std::get<2>(own);

		BoundingBox broadphase(
			Point3(
				ownVel.x > 0.0f ? ownPos.x : ownPos2.x,
				ownVel.y > 0.0f ? ownPos.y : ownPos2.y,
				ownVel.z > 0.0f ? ownPos.z : ownPos2.z
			),
			Point3(
				ownVel.x > 0.0f ? size.x + ownVel.x : size.x - ownVel.x,<commit_msg>Write ray-AABB intersection function<commit_after>#pragma once

#include <vector>
#include "Component.h"

struct BoundingBox {
	Point3 position;
	Point3 size;
	std::vector<BoundingBox> children;

	BoundingBox() {}
	BoundingBox(const Point3 &position, const Point3 &size) : position(position), size(size) {}

	inline Point3 Center() const { return position + size / 2.0f; }

	inline std::pair<bool, float> TestRay(const Point3 origin, const Point3 direction, float tMax, const Point3 &ownPos) const {
		auto tMin = 0.0f;
		auto p = ownPos + Center() - origin;
		auto h = size / 2.0f;

		for(glm::length_t i = 0; i < 3; ++i) {
			if(glm::abs(direction[i]) > 0.00000000000000000001f) {
				float invDir = 1.0f / direction[i];
				float tEntry = (p[i] - h[i]) * invDir;
				float tExit = (p[i] + h[i]) * invDir;
				if(tEntry > tExit) { std::swap(tEntry, tExit); }
				if(tEntry > tMin) { tMin = tEntry; }
				if(tExit < tMax) { tMax = tExit; }
				if(tMin > tMax) { return std::make_pair(false, 0.0f); }
				if(tMax < 0.0f) { return std::make_pair(false, 0.0f); }
			} else if(-p[i] - h[i] > 0.0f || -p[i] + h[i] < 0.0f) { return std::make_pair(false, 0.0f); }
		}

		return std::make_pair(true, tMin > 0.0f ? tMin : tMax);
	}

	inline bool AABBCheck(const BoundingBox &b, const Point3 &ownPos, const Point3 &bPos) const {
		Point3 aBegin = position + ownPos;
		Point3 bBegin = b.position + bPos;
		auto aEnd = aBegin + size;
		auto bEnd = bBegin + b.size;

		return !(
			aEnd.x < bBegin.x ||
			aEnd.y < bBegin.y ||
			aEnd.z < bBegin.z ||
			aBegin.x > bEnd.x ||
			aBegin.y > bEnd.y ||
			aBegin.z > bEnd.z);
	}

	std::pair<float, Point3> AABBSwept(const BoundingBox &b, const std::tuple<Point3, Point3, Point3> &own, const Point3 &bPos) const {
		std::pair<float, Point3> result = std::make_pair(1.0f, Point3(0.0f));

		auto &ownPos = std::get<0>(own);
		auto &ownPos2 = std::get<1>(own);
		auto &ownVel = std::get<2>(own);

		BoundingBox broadphase(
			Point3(
				ownVel.x > 0.0f ? ownPos.x : ownPos2.x,
				ownVel.y > 0.0f ? ownPos.y : ownPos2.y,
				ownVel.z > 0.0f ? ownPos.z : ownPos2.z
			),
			Point3(
				ownVel.x > 0.0f ? size.x + ownVel.x : size.x - ownVel.x,
{'fraction_non_alphanumeric': 0.1645123384253819, 'fraction_numerical': 0.03554641598119859, 'mean_word_length': 3.3709884467265727, 'pattern_counts': {'":': 0, '<': 30, '<?xml version=': 0, '>': 40, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 1, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4609639', 'n_tokens_mistral': 1571, 'n_tokens_neox': 1397, 'n_words': 325}