From ce04b1accb2690edcf43cfa1676c34e8fd519e80 Mon Sep 17 00:00:00 2001 From: Virendra Singh Date: Sat, 21 Feb 2015 15:40:39 +0530 Subject: [PATCH] vhacd tool this tool is as per joblist #20305. Its provides an executable to test VHACD computation on simple fbx models. --- tools/vhacd/src/VHACDUtil.cpp | 130 ++++++++++++++++++++++++++++++++++ tools/vhacd/src/VHACDUtil.h | 55 ++++++++++++++ tools/vhacd/src/main.cpp | 102 ++++++++++++++++++++++++++ 3 files changed, 287 insertions(+) create mode 100644 tools/vhacd/src/VHACDUtil.cpp create mode 100644 tools/vhacd/src/VHACDUtil.h create mode 100644 tools/vhacd/src/main.cpp diff --git a/tools/vhacd/src/VHACDUtil.cpp b/tools/vhacd/src/VHACDUtil.cpp new file mode 100644 index 0000000000..bfcdb74634 --- /dev/null +++ b/tools/vhacd/src/VHACDUtil.cpp @@ -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 +#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 vertices = mesh.vertices; + + //get the triangle indices for each mesh + QVector triangles; + foreach(FBXMeshPart part, mesh.parts) + { + QVector 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 vertices = meshes->perMeshVertices.at(i).toStdVector(); + std::vector 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 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(){} \ No newline at end of file diff --git a/tools/vhacd/src/VHACDUtil.h b/tools/vhacd/src/VHACDUtil.h new file mode 100644 index 0000000000..a9df5d7f2a --- /dev/null +++ b/tools/vhacd/src/VHACDUtil.h @@ -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 +#include +#include +#include + +#include +#include +#include + +namespace vhacd{ + + typedef struct{ + int meshCount; + QVector convexHullsCountList; + QVector> convexHullList; + }ComputeResults; + + typedef struct{ + int meshCount; + QVector> perMeshVertices; + QVector> 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 \ No newline at end of file diff --git a/tools/vhacd/src/main.cpp b/tools/vhacd/src/main.cpp new file mode 100644 index 0000000000..55a152c179 --- /dev/null +++ b/tools/vhacd/src/main.cpp @@ -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 +#include +#include +#include +#include +#include +#include "VHACDUtil.h" + +using namespace std; +using namespace VHACD; + +int main(int argc, char * argv[]) +{ + vector triangles; // array of indexes + vector 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 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 < 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; +} \ No newline at end of file