2021-11-03 22:57:48 +03:00
|
|
|
//
|
|
|
|
// Created by Иван Ильин on 04.11.2021.
|
|
|
|
//
|
|
|
|
|
2021-11-04 04:30:58 +03:00
|
|
|
#include <algorithm>
|
|
|
|
#include <execution>
|
|
|
|
|
2021-11-03 22:57:48 +03:00
|
|
|
#include "HitBox.h"
|
|
|
|
#include "../Consts.h"
|
|
|
|
|
|
|
|
HitBox::HitBox(const Mesh &mesh) {
|
2021-11-04 03:44:53 +03:00
|
|
|
_hitBox.reserve(mesh.triangles().size() * 3);
|
2021-11-03 22:57:48 +03:00
|
|
|
for(const auto& t : mesh.triangles()) {
|
|
|
|
for(int i = 0; i < 3; i++) {
|
|
|
|
// we dont need to add the same points in hit box
|
|
|
|
_addIfUnique(Vec3D(t[i]));
|
|
|
|
}
|
|
|
|
}
|
2021-11-04 03:44:53 +03:00
|
|
|
_hitBox.shrink_to_fit();
|
2021-11-03 22:57:48 +03:00
|
|
|
}
|
|
|
|
|
2021-11-04 04:30:58 +03:00
|
|
|
void HitBox::_addIfUnique(Vec3D &&point) {
|
2021-11-03 22:57:48 +03:00
|
|
|
bool addPoint = true;
|
2021-11-04 04:30:58 +03:00
|
|
|
auto check = [&point](const auto& p) { return (p - point).sqrAbs() < Consts::EPS; };
|
|
|
|
|
|
|
|
if (std::find_if(std::execution::par, _hitBox.rbegin(), _hitBox.rend(), check) == _hitBox.rend())
|
2021-11-03 22:57:48 +03:00
|
|
|
_hitBox.push_back(point);
|
|
|
|
}
|
|
|
|
|
|
|
|
HitBox HitBox::Box(const Mesh &mesh) {
|
|
|
|
HitBox result;
|
|
|
|
|
|
|
|
double maxX = -std::numeric_limits<double>::max();
|
|
|
|
double maxY = -std::numeric_limits<double>::max();
|
|
|
|
double maxZ = -std::numeric_limits<double>::max();
|
|
|
|
|
|
|
|
double minX = std::numeric_limits<double>::max();
|
|
|
|
double minY = std::numeric_limits<double>::max();
|
|
|
|
double minZ = std::numeric_limits<double>::max();
|
|
|
|
|
|
|
|
for(const auto& t : mesh.triangles()) {
|
|
|
|
for(int i = 0; i < 3; i++) {
|
|
|
|
Vec3D point = Vec3D(t[i]);
|
|
|
|
if(point.x() > maxX) {
|
|
|
|
maxX = point.x();
|
|
|
|
}
|
|
|
|
if(point.y() > maxY) {
|
|
|
|
maxY = point.y();
|
|
|
|
}
|
|
|
|
if(point.z() > maxZ) {
|
|
|
|
maxZ = point.z();
|
|
|
|
}
|
|
|
|
|
|
|
|
if(point.x() < minX) {
|
|
|
|
minX = point.x();
|
|
|
|
}
|
|
|
|
if(point.y() < minY) {
|
|
|
|
minY = point.y();
|
|
|
|
}
|
|
|
|
if(point.z() < minZ) {
|
|
|
|
minZ = point.z();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
result._hitBox.emplace_back(minX, minY, minZ);
|
|
|
|
result._hitBox.emplace_back(minX, maxY, minZ);
|
|
|
|
result._hitBox.emplace_back(maxX, minY, minZ);
|
|
|
|
result._hitBox.emplace_back(maxX, maxY, minZ);
|
|
|
|
|
|
|
|
result._hitBox.emplace_back(minX, minY, maxZ);
|
|
|
|
result._hitBox.emplace_back(minX, maxY, maxZ);
|
|
|
|
result._hitBox.emplace_back(maxX, minY, maxZ);
|
|
|
|
result._hitBox.emplace_back(maxX, maxY, maxZ);
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|