Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[mesh] Replacing custom code for mesh::saveToObj by using assimp library #1094

Merged
merged 1 commit into from
Aug 12, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
87 changes: 52 additions & 35 deletions src/aliceVision/mesh/Mesh.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
#include <boost/filesystem.hpp>

#include <assimp/Importer.hpp>
#include <assimp/Exporter.hpp>
#include <assimp/postprocess.h>
#include <assimp/scene.h>
#include <Eigen/Dense>
Expand All @@ -37,41 +38,57 @@ Mesh::~Mesh()

void Mesh::saveToObj(const std::string& filename)
{
ALICEVISION_LOG_INFO("Save mesh to obj: " << filename);
ALICEVISION_LOG_INFO("Nb points: " << pts.size());
ALICEVISION_LOG_INFO("Nb triangles: " << tris.size());

FILE* f = fopen(filename.c_str(), "w");

fprintf(f, "# \n");
fprintf(f, "# Wavefront OBJ file\n");
fprintf(f, "# Created with AliceVision\n");
fprintf(f, "# \n");
fprintf(f, "g Mesh\n");

if(_colors.size() == pts.size())
{
std::size_t i = 0;
for(const auto& point : pts)
{
const rgb& col = _colors[i];
fprintf(f, "v %f %f %f %f %f %f\n", point.x, point.y, point.z, col.r/255.0f, col.g/255.0f, col.b/255.0f);
++i;
}
}
else
{
for(const auto& point : pts)
fprintf(f, "v %f %f %f\n", point.x, point.y, point.z);
}

for(int i = 0; i < tris.size(); ++i)
{
Mesh::triangle& t = tris[i];
fprintf(f, "f %i %i %i\n", t.v[0] + 1, t.v[1] + 1, t.v[2] + 1);
}
fclose(f);
ALICEVISION_LOG_INFO("Save mesh to obj done.");
ALICEVISION_LOG_INFO("Writing obj and mtl file.");

aiScene scene;

scene.mRootNode = new aiNode;

scene.mMeshes = new aiMesh*[1];
scene.mNumMeshes = 1;
scene.mRootNode->mMeshes = new unsigned int[1];
scene.mRootNode->mNumMeshes = 1;

scene.mMaterials = new aiMaterial*[1];
scene.mNumMaterials = 1;
scene.mMaterials[0] = new aiMaterial;

scene.mRootNode->mMeshes[0] = 0;
scene.mMeshes[0] = new aiMesh;
aiMesh * aimesh = scene.mMeshes[0];
aimesh->mMaterialIndex = 0;

aimesh->mNumVertices = pts.size();
aimesh->mVertices = new aiVector3D[pts.size()];

int index = 0;
for (const auto & p : pts)
{
aimesh->mVertices[index].x = p.x;
aimesh->mVertices[index].y = p.y;
aimesh->mVertices[index].z = p.z;

++index;
}

aimesh->mNumFaces = tris.size();
aimesh->mFaces = new aiFace[tris.size()];

for(int i = 0; i < tris.size(); ++i)
{
aimesh->mFaces[i].mNumIndices = 3;
aimesh->mFaces[i].mIndices = new unsigned int[3];

for (int k = 0; k < 3; ++k)
{
aimesh->mFaces[i].mIndices[k] = tris[i].v[k];
}
}

Assimp::Exporter exporter;
exporter.Export(&scene, "objnomtl", filename);

ALICEVISION_LOG_INFO("Save mesh to obj done.");
}

bool Mesh::loadFromBin(const std::string& binFileName)
Expand Down