Fix remesh memory leak and add one more check for combinable mesh [skip ci]

master
Jeremy Hu 2020-04-11 20:22:58 +09:30
parent fd17185720
commit 3be82de836
4 changed files with 37 additions and 1 deletions

View File

@ -27,9 +27,18 @@ MeshCombiner::Mesh::Mesh(const std::vector<QVector3D> &vertices, const std::vect
qDebug() << "Mesh does_self_intersect"; qDebug() << "Mesh does_self_intersect";
delete cgalMesh; delete cgalMesh;
cgalMesh = nullptr; cgalMesh = nullptr;
} else {
std::vector<QVector3D> fetchedVertices;
std::vector<std::vector<size_t>> fetchedFaces;
fetchFromCgalMesh<CgalKernel>(cgalMesh, fetchedVertices, fetchedFaces);
if (!isManifold(fetchedFaces)) {
qDebug() << "Mesh does not self intersect but is not manifold";
delete cgalMesh;
cgalMesh = nullptr;
} else { } else {
m_isCombinable = true; m_isCombinable = true;
} }
}
} else { } else {
qDebug() << "Mesh triangulate failed"; qDebug() << "Mesh triangulate failed";
delete cgalMesh; delete cgalMesh;

View File

@ -1,5 +1,6 @@
#include <cmath> #include <cmath>
#include <QtMath> #include <QtMath>
#include <QFile>
#include <unordered_set> #include <unordered_set>
#include <unordered_map> #include <unordered_map>
#include "util.h" #include "util.h"
@ -488,3 +489,24 @@ QVector3D choosenBaseAxis(const QVector3D &layoutDirection)
return first.first < second.first; return first.first < second.first;
})->second]; })->second];
} }
void saveAsObj(const char *filename, const std::vector<QVector3D> &vertices,
const std::vector<std::vector<size_t>> &faces)
{
QFile file(filename);
if (!file.open(QIODevice::WriteOnly))
return;
QTextStream stream(&file);
for (std::vector<QVector3D>::const_iterator it = vertices.begin() ; it != vertices.end(); ++it) {
stream << "v " << (*it).x() << " " << (*it).y() << " " << (*it).z() << endl;
}
for (std::vector<std::vector<size_t>>::const_iterator it = faces.begin() ; it != faces.end(); ++it) {
stream << "f";
for (std::vector<size_t>::const_iterator subIt = (*it).begin() ; subIt != (*it).end(); ++subIt) {
stream << " " << (1 + *subIt);
}
stream << endl;
}
}

View File

@ -45,5 +45,7 @@ void trim(std::vector<QVector3D> *vertices, bool normalize=false);
void chamferFace2D(std::vector<QVector2D> *face); void chamferFace2D(std::vector<QVector2D> *face);
void subdivideFace2D(std::vector<QVector2D> *face); void subdivideFace2D(std::vector<QVector2D> *face);
QVector3D choosenBaseAxis(const QVector3D &layoutDirection); QVector3D choosenBaseAxis(const QVector3D &layoutDirection);
void saveAsObj(const char *filename, const std::vector<QVector3D> &vertices,
const std::vector<std::vector<size_t>> &faces);
#endif #endif

View File

@ -312,6 +312,9 @@ void DUST3D_INSTANT_MESHES_FUNCTION_CONVENTION Dust3D_instantMeshesRemesh(const
}; };
outputMesh(O_extr, F_extr); outputMesh(O_extr, F_extr);
if (bvh)
delete bvh;
*nResultVertices = g_resultVertices.size(); *nResultVertices = g_resultVertices.size();
*resultVertices = g_resultVertices.data(); *resultVertices = g_resultVertices.data();
*nResultTriangles = g_resultTriangles.size(); *nResultTriangles = g_resultTriangles.size();