mirror of
https://github.com/Armored-Dragon/overte.git
synced 2025-03-11 16:13:16 +01:00
vhacd tool
this tool is as per joblist #20305. Its provides an executable to test VHACD computation on simple fbx models.
This commit is contained in:
parent
ddd137ed60
commit
ce04b1accb
3 changed files with 287 additions and 0 deletions
130
tools/vhacd/src/VHACDUtil.cpp
Normal file
130
tools/vhacd/src/VHACDUtil.cpp
Normal file
|
@ -0,0 +1,130 @@
|
|||
//
|
||||
// VHACDUtil.cpp
|
||||
// tools/vhacd/src
|
||||
//
|
||||
// Created by Virendra Singh on 2/20/15.
|
||||
// Copyright 2015 High Fidelity, Inc.
|
||||
//
|
||||
// Distributed under the Apache License, Version 2.0.
|
||||
// See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html
|
||||
//
|
||||
|
||||
#include <Qvector>
|
||||
#include "VHACDUtil.h"
|
||||
|
||||
|
||||
//Read all the meshes from provided FBX file
|
||||
bool vhacd::VHACDUtil::loadFBX(const QString filename, vhacd::LoadFBXResults *results)
|
||||
{
|
||||
|
||||
// open the fbx file
|
||||
QFile fbx(filename);
|
||||
if (!fbx.open(QIODevice::ReadOnly)) {
|
||||
return false;
|
||||
}
|
||||
std::cout << "Reading FBX.....\n";
|
||||
|
||||
QByteArray fbxContents = fbx.readAll();
|
||||
FBXGeometry geometry = readFBX(fbxContents, QVariantHash());
|
||||
//results->meshCount = geometry.meshes.count();
|
||||
|
||||
int count = 0;
|
||||
foreach(FBXMesh mesh, geometry.meshes)
|
||||
{
|
||||
//get vertices for each mesh
|
||||
QVector<glm::vec3> vertices = mesh.vertices;
|
||||
|
||||
//get the triangle indices for each mesh
|
||||
QVector<int> triangles;
|
||||
foreach(FBXMeshPart part, mesh.parts)
|
||||
{
|
||||
QVector<int> indices = part.triangleIndices;
|
||||
triangles += indices;
|
||||
}
|
||||
|
||||
//only read meshes with triangles
|
||||
if (triangles.count() <= 0)
|
||||
continue;
|
||||
results->perMeshVertices.append(vertices);
|
||||
results->perMeshTriangleIndices.append(triangles);
|
||||
count++;
|
||||
}
|
||||
|
||||
results->meshCount = count;
|
||||
return true;
|
||||
}
|
||||
|
||||
bool vhacd::VHACDUtil::computeVHACD(vhacd::LoadFBXResults *meshes, VHACD::IVHACD::Parameters params, vhacd::ComputeResults *results)const
|
||||
{
|
||||
VHACD::IVHACD * interfaceVHACD = VHACD::CreateVHACD();
|
||||
int meshCount = meshes->meshCount;
|
||||
int count = 0;
|
||||
std::cout << "Performing V-HACD computation on " << meshCount <<" meshes ..... " << std::endl;
|
||||
|
||||
for (int i = 0; i < meshCount; i++)
|
||||
{
|
||||
|
||||
std::vector<glm::vec3> vertices = meshes->perMeshVertices.at(i).toStdVector();
|
||||
std::vector<int> triangles = meshes->perMeshTriangleIndices.at(i).toStdVector();
|
||||
int nPoints = (unsigned int)vertices.size();
|
||||
int nTriangles = (unsigned int)triangles.size() / 3;
|
||||
std::cout << "Mesh " << i + 1 << " : ";
|
||||
// compute approximate convex decomposition
|
||||
bool res = interfaceVHACD->Compute(&vertices[0].x, 3, nPoints, &triangles[0], 3, nTriangles, params);
|
||||
if (!res)
|
||||
{
|
||||
std::cout << "V-HACD computation failed for Mesh : " << i + 1 << std::endl;
|
||||
continue;
|
||||
}
|
||||
count++; //For counting number of successfull computations
|
||||
|
||||
//Number of hulls for the mesh
|
||||
unsigned int nConvexHulls = interfaceVHACD->GetNConvexHulls();
|
||||
results->convexHullsCountList.append(nConvexHulls);
|
||||
|
||||
//get all the convex hulls for this mesh
|
||||
QVector<VHACD::IVHACD::ConvexHull> convexHulls;
|
||||
for (unsigned int j = 0; j < nConvexHulls; j++)
|
||||
{
|
||||
VHACD::IVHACD::ConvexHull hull;
|
||||
interfaceVHACD->GetConvexHull(j, hull);
|
||||
convexHulls.append(hull);
|
||||
}
|
||||
results->convexHullList.append(convexHulls);
|
||||
} //end of for loop
|
||||
|
||||
results->meshCount = count;
|
||||
|
||||
//release memory
|
||||
interfaceVHACD->Clean();
|
||||
interfaceVHACD->Release();
|
||||
|
||||
if (count > 0)
|
||||
return true;
|
||||
else
|
||||
return false;
|
||||
}
|
||||
|
||||
vhacd::VHACDUtil:: ~VHACDUtil()
|
||||
{
|
||||
//nothing to be cleaned
|
||||
}
|
||||
|
||||
//ProgressClaback implementation
|
||||
void vhacd::ProgressCallback::Update(const double overallProgress, const double stageProgress, const double operationProgress, const char * const stage, const char * const operation)
|
||||
{
|
||||
int progress = (int)(overallProgress + 0.5);
|
||||
|
||||
if (progress < 10)
|
||||
std::cout << "\b\b";
|
||||
else
|
||||
std::cout << "\b\b\b";
|
||||
std::cout << progress << "%";
|
||||
|
||||
if (progress >= 100)
|
||||
std::cout << std::endl;
|
||||
|
||||
}
|
||||
|
||||
vhacd::ProgressCallback::ProgressCallback(void){}
|
||||
vhacd::ProgressCallback::~ProgressCallback(){}
|
55
tools/vhacd/src/VHACDUtil.h
Normal file
55
tools/vhacd/src/VHACDUtil.h
Normal file
|
@ -0,0 +1,55 @@
|
|||
//
|
||||
// VHACDUtil.h
|
||||
// tools/vhacd/src
|
||||
//
|
||||
// Created by Virendra Singh on 2/20/15.
|
||||
// Copyright 2015 High Fidelity, Inc.
|
||||
//
|
||||
// Distributed under the Apache License, Version 2.0.
|
||||
// See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html
|
||||
//
|
||||
|
||||
#ifndef hifi_VHACDUtil_h
|
||||
#define hifi_VHACDUtil_h
|
||||
|
||||
#include <iostream>
|
||||
#include <iomanip>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
#include <QFile>
|
||||
#include <FBXReader.h>
|
||||
#include <VHACD.h>
|
||||
|
||||
namespace vhacd{
|
||||
|
||||
typedef struct{
|
||||
int meshCount;
|
||||
QVector<int> convexHullsCountList;
|
||||
QVector<QVector<VHACD::IVHACD::ConvexHull>> convexHullList;
|
||||
}ComputeResults;
|
||||
|
||||
typedef struct{
|
||||
int meshCount;
|
||||
QVector<QVector<glm::vec3>> perMeshVertices;
|
||||
QVector<QVector<int>> perMeshTriangleIndices;
|
||||
}LoadFBXResults;
|
||||
|
||||
class VHACDUtil{
|
||||
public:
|
||||
bool loadFBX(const QString filename, vhacd::LoadFBXResults *results);
|
||||
bool computeVHACD(vhacd::LoadFBXResults *meshes, VHACD::IVHACD::Parameters params, vhacd::ComputeResults *results)const;
|
||||
~VHACDUtil();
|
||||
};
|
||||
|
||||
class ProgressCallback : public VHACD::IVHACD::IUserCallback
|
||||
{
|
||||
public:
|
||||
ProgressCallback(void);
|
||||
~ProgressCallback();
|
||||
|
||||
//Couldn't follow coding guideline here due to virtual function declared in IUserCallback
|
||||
void Update(const double overallProgress, const double stageProgress, const double operationProgress, const char * const stage, const char * const operation);
|
||||
};
|
||||
}
|
||||
#endif //hifi_VHACDUtil_h
|
102
tools/vhacd/src/main.cpp
Normal file
102
tools/vhacd/src/main.cpp
Normal file
|
@ -0,0 +1,102 @@
|
|||
//
|
||||
// main.cpp
|
||||
// tools/vhacd/src
|
||||
//
|
||||
// Created by Virendra Singh on 2/20/15.
|
||||
// Copyright 2015 High Fidelity, Inc.
|
||||
//
|
||||
// Distributed under the Apache License, Version 2.0.
|
||||
// See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html
|
||||
|
||||
#include <stdio.h>
|
||||
#include <iostream>
|
||||
#include <iomanip>
|
||||
#include <VHACD.h>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
#include "VHACDUtil.h"
|
||||
|
||||
using namespace std;
|
||||
using namespace VHACD;
|
||||
|
||||
int main(int argc, char * argv[])
|
||||
{
|
||||
vector<int> triangles; // array of indexes
|
||||
vector<float> points; // array of coordinates
|
||||
vhacd::VHACDUtil vUtil;
|
||||
vhacd::LoadFBXResults fbx; //mesh data from loaded fbx file
|
||||
vhacd::ComputeResults results; // results after computing vhacd
|
||||
VHACD::IVHACD::Parameters params;
|
||||
vhacd::ProgressCallback pCallBack;
|
||||
|
||||
|
||||
QString fname = "F:/models/ship/Sample_Ship.fbx";
|
||||
|
||||
//set parameters for V-HACD
|
||||
params.m_callback = &pCallBack; //progress callback
|
||||
params.m_resolution = 50000;
|
||||
params.m_depth = 10;
|
||||
params.m_concavity = 0.003;
|
||||
params.m_alpha = 0.05; // controls the bias toward clipping along symmetry planes
|
||||
params.m_pca = 1; // enable/disable normalizing the mesh before applying the convex decomposition
|
||||
params.m_mode = 1; // 0: voxel - based approximate convex decomposition, 1 : tetrahedron - based approximate convex decomposition
|
||||
params.m_maxNumVerticesPerCH = 128;
|
||||
params.m_minVolumePerCH = 0.0001; // controls the adaptive sampling of the generated convex - hulls
|
||||
|
||||
// load the mesh
|
||||
if (!vUtil.loadFBX(fname, &fbx))
|
||||
{
|
||||
cout << "Error in opening FBX file....";
|
||||
return 1;
|
||||
}
|
||||
|
||||
if (!vUtil.computeVHACD(&fbx, params, &results))
|
||||
{
|
||||
cout << "Compute Failed...";
|
||||
return 1;
|
||||
}
|
||||
|
||||
int totalVertices = 0;
|
||||
for (int i = 0; i < fbx.meshCount; i++)
|
||||
{
|
||||
totalVertices += fbx.perMeshVertices.at(i).count();
|
||||
}
|
||||
|
||||
int totalTriangles = 0;
|
||||
for (int i = 0; i < fbx.meshCount; i++)
|
||||
{
|
||||
totalTriangles += fbx.perMeshTriangleIndices.at(i).count();
|
||||
}
|
||||
|
||||
int totalHulls = 0;
|
||||
QVector<int> hullCounts = results.convexHullsCountList;
|
||||
for (int i = 0; i < results.meshCount; i++)
|
||||
{
|
||||
totalHulls += hullCounts.at(i);
|
||||
}
|
||||
cout << endl << "Summary of V-HACD Computation..................." << endl;
|
||||
cout << "File Path : " << fname.toStdString() << endl;
|
||||
cout << "Number Of Meshes : " << fbx.meshCount << endl;
|
||||
cout << "Processed Meshes : " << results.meshCount << endl;
|
||||
cout << "Total vertices : " << totalVertices << endl;
|
||||
cout << "Total Triangles : " << totalTriangles << endl;
|
||||
cout << "Total Convex Hulls : " << totalHulls << endl;
|
||||
cout << endl << "Summary per convex hull ........................" << endl <<endl;
|
||||
for (int i = 0; i < results.meshCount; i++)
|
||||
{
|
||||
cout << "Mesh : " << i + 1 << endl;
|
||||
QVector<VHACD::IVHACD::ConvexHull> chList = results.convexHullList.at(i);
|
||||
cout << "\t" << "Number Of Hulls : " << chList.count() << endl;
|
||||
|
||||
for (int j = 0; j < results.convexHullList.at(i).count(); j++)
|
||||
{
|
||||
|
||||
cout << "\tHUll : " << j + 1 << endl;
|
||||
cout << "\t\tNumber Of Points : " << chList.at(j).m_nPoints << endl;
|
||||
cout << "\t\tNumber Of Triangles : " << chList.at(j).m_nTriangles << endl;
|
||||
}
|
||||
}
|
||||
|
||||
getchar();
|
||||
return 0;
|
||||
}
|
Loading…
Reference in a new issue