Hitbox creation through set

master
Neirokan 2021-11-06 00:28:55 +03:00
parent 556ca037ec
commit f07f9a6638
2 changed files with 25 additions and 17 deletions

View File

@ -2,30 +2,36 @@
// Created by Иван Ильин on 04.11.2021. // Created by Иван Ильин on 04.11.2021.
// //
#include <algorithm> #include <set>
#include "HitBox.h" #include "HitBox.h"
#include "../Consts.h"
bool HitBox::Vec3DLess::operator()(const Vec3D& lhs, const Vec3D& rhs) const noexcept {
if (fabs(lhs.x() - rhs.x()) >= Consts::EPS)
return lhs.x() < rhs.x();
else if (fabs(lhs.y() - rhs.y()) >= Consts::EPS)
return lhs.y() < rhs.y();
else if (fabs(lhs.z() - rhs.z()) >= Consts::EPS)
return lhs.z() < rhs.z();
else
return false;
}
HitBox::HitBox(const Mesh& mesh) { HitBox::HitBox(const Mesh& mesh) {
_hitBox.reserve(mesh.triangles().size() * 3);
for(const auto& t : mesh.triangles()) {
for(int i = 0; i < 3; i++) {
// we dont need to add the same points in hit box // we dont need to add the same points in hit box
_addIfUnique(Vec3D(t[i])); std::set<Vec3D, HitBox::Vec3DLess> points;
}
} for (const auto& t : mesh.triangles())
for (int i = 0; i < 3; i++)
points.insert(Vec3D(t[i]));
_hitBox.reserve(points.size());
for (const auto& it : points)
_hitBox.push_back(it);
_hitBox.shrink_to_fit(); _hitBox.shrink_to_fit();
} }
void HitBox::_addIfUnique(Vec3D &&point) {
auto check = [&point](const auto& p) { return p == point; };
if (std::find_if(_hitBox.rbegin(), _hitBox.rend(), check) == _hitBox.rend()) {
_hitBox.push_back(point);
}
}
HitBox HitBox::Box(const Mesh &mesh) { HitBox HitBox::Box(const Mesh &mesh) {
HitBox result; HitBox result;

View File

@ -9,9 +9,11 @@
class HitBox final { class HitBox final {
private: private:
std::vector<Vec3D> _hitBox; struct Vec3DLess {
bool operator()(const Vec3D& lhs, const Vec3D& rhs) const noexcept;
};
void _addIfUnique(Vec3D &&point); std::vector<Vec3D> _hitBox;
public: public:
HitBox() = default; HitBox() = default;