std::vector resize & move optimisations

master
Neirokan 2021-11-04 03:44:53 +03:00
parent 7fd9814e33
commit 3ce6345d21
5 changed files with 18 additions and 4 deletions

View File

@ -11,10 +11,11 @@ using namespace std;
Mesh &Mesh::operator*=(const Matrix4x4 &matrix4X4) {
std::vector<Triangle> newTriangles;
newTriangles.reserve(_tris.size());
for (auto &t : _tris) {
newTriangles.emplace_back(t * matrix4X4);
}
setTriangles(newTriangles);
setTriangles(std::move(newTriangles));
return *this;
}
@ -46,10 +47,11 @@ void Mesh::setColor(const sf::Color &c) {
// change color of all mesh triangles:
std::vector<Triangle> newTriangles;
newTriangles.reserve(_tris.size());
for (auto &t : _tris) {
newTriangles.emplace_back(Triangle(t[0], t[1], t[2], c));
}
setTriangles(newTriangles);
setTriangles(std::move(newTriangles));
}
Mesh
@ -99,6 +101,10 @@ void Mesh::setTriangles(const vector<Triangle> &t) {
}
}
void Mesh::setTriangles(vector<Triangle>&& t) {
_tris = std::move(t);
}
Mesh::~Mesh() {
_tris.clear();
}

View File

@ -38,6 +38,8 @@ public:
void setTriangles(const std::vector<Triangle> &t);
void setTriangles(std::vector<Triangle>&& t);
[[nodiscard]] size_t size() const { return _tris.size() * 3; }
[[nodiscard]] sf::Color color() const { return _color; }

View File

@ -21,12 +21,14 @@ private:
return;
}
auto object = _object.lock();
std::vector<Triangle> newTriangles;
for (auto &t : _object->triangles()) {
newTriangles.reserve(object->triangles().size());
for (auto &t : object->triangles()) {
newTriangles.emplace_back(
t * Matrix4x4::Scale(Vec3D{1, 1, 1} + (_scalingValue - Vec3D{1, 1, 1}) * progress()));
}
_object.lock()->setTriangles(newTriangles);
object.lock()->setTriangles(std::move(newTriangles));
}
public:

View File

@ -6,12 +6,14 @@
#include "../Consts.h"
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
_addIfUnique(Vec3D(t[i]));
}
}
_hitBox.shrink_to_fit();
}
void HitBox::_addIfUnique(const Vec3D &point) {

View File

@ -253,6 +253,7 @@ CollisionPoint RigidBody::EPA(const Simplex &simplex, std::shared_ptr<RigidBody>
}
std::vector<size_t> newFaces;
newFaces.reserve(uniqueEdges.size() * 3);
for (auto[edgeIndex1, edgeIndex2] : uniqueEdges) {
newFaces.push_back(edgeIndex1);
newFaces.push_back(edgeIndex2);
@ -280,6 +281,7 @@ CollisionPoint RigidBody::EPA(const Simplex &simplex, std::shared_ptr<RigidBody>
std::pair<std::vector<FaceNormal>, size_t>
RigidBody::_getFaceNormals(const std::vector<Vec3D> &polytope, const std::vector<size_t> &faces) {
std::vector<FaceNormal> normals;
normals.reserve(faces.size() / 3);
size_t nearestFaceIndex = 0;
double minDistance = std::numeric_limits<double>::max();