From a7894b090d389af9c05c59bc25e97c0f8286c942 Mon Sep 17 00:00:00 2001 From: Chris Russ Date: Sat, 16 Jul 2016 18:22:01 +1000 Subject: [PATCH] Upgrading PLY exporter to allow element definition as double --- code/PlyExporter.cpp | 60 ++++++++++++++++++++++++++------------------ code/PlyParser.cpp | 13 ++++++---- 2 files changed, 44 insertions(+), 29 deletions(-) diff --git a/code/PlyExporter.cpp b/code/PlyExporter.cpp index 87f5bbee8..af4d0d372 100644 --- a/code/PlyExporter.cpp +++ b/code/PlyExporter.cpp @@ -56,6 +56,12 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. //using namespace Assimp; namespace Assimp { +// make sure typeof returns consistent output across different platforms +// also consider using: typeid(VAR).name() +template const char* typeof(T&) { return "unknown"; } +template<> const char* typeof(float&) { return "float"; } +template<> const char* typeof(double&) { return "double"; } + // ------------------------------------------------------------------------------------------------ // Worker function for exporting a scene to PLY. Prototyped and registered in Exporter.cpp void ExportScenePly(const char* pFile,IOSystem* pIOSystem, const aiScene* pScene, const ExportProperties* pProperties) @@ -136,15 +142,21 @@ PlyExporter::PlyExporter(const char* _filename, const aiScene* pScene, bool bina << aiGetVersionMajor() << '.' << aiGetVersionMinor() << '.' << aiGetVersionRevision() << ")" << endl; + // TODO: probably want to check here rather than just assume something + // definitely not good to always write float even if we might have double precision + + ai_real tmp = 0.0; + const char * typeName = typeof(tmp); + mOutput << "element vertex " << vertices << endl; - mOutput << "property float x" << endl; - mOutput << "property float y" << endl; - mOutput << "property float z" << endl; + mOutput << "property " << typeName << " x" << endl; + mOutput << "property " << typeName << " y" << endl; + mOutput << "property " << typeName << " z" << endl; if(components & PLY_EXPORT_HAS_NORMALS) { - mOutput << "property float nx" << endl; - mOutput << "property float ny" << endl; - mOutput << "property float nz" << endl; + mOutput << "property " << typeName << " nx" << endl; + mOutput << "property " << typeName << " ny" << endl; + mOutput << "property " << typeName << " nz" << endl; } // write texcoords first, just in case an importer does not support tangents @@ -154,37 +166,37 @@ PlyExporter::PlyExporter(const char* _filename, const aiScene* pScene, bool bina // and texture coordinates). for (unsigned int n = PLY_EXPORT_HAS_TEXCOORDS, c = 0; (components & n) && c != AI_MAX_NUMBER_OF_TEXTURECOORDS; n <<= 1, ++c) { if (!c) { - mOutput << "property float s" << endl; - mOutput << "property float t" << endl; + mOutput << "property " << typeName << " s" << endl; + mOutput << "property " << typeName << " t" << endl; } else { - mOutput << "property float s" << c << endl; - mOutput << "property float t" << c << endl; + mOutput << "property " << typeName << " s" << c << endl; + mOutput << "property " << typeName << " t" << c << endl; } } for (unsigned int n = PLY_EXPORT_HAS_COLORS, c = 0; (components & n) && c != AI_MAX_NUMBER_OF_COLOR_SETS; n <<= 1, ++c) { if (!c) { - mOutput << "property float r" << endl; - mOutput << "property float g" << endl; - mOutput << "property float b" << endl; - mOutput << "property float a" << endl; + mOutput << "property " << typeName << " r" << endl; + mOutput << "property " << typeName << " g" << endl; + mOutput << "property " << typeName << " b" << endl; + mOutput << "property " << typeName << " a" << endl; } else { - mOutput << "property float r" << c << endl; - mOutput << "property float g" << c << endl; - mOutput << "property float b" << c << endl; - mOutput << "property float a" << c << endl; + mOutput << "property " << typeName << " r" << c << endl; + mOutput << "property " << typeName << " g" << c << endl; + mOutput << "property " << typeName << " b" << c << endl; + mOutput << "property " << typeName << " a" << c << endl; } } if(components & PLY_EXPORT_HAS_TANGENTS_BITANGENTS) { - mOutput << "property float tx" << endl; - mOutput << "property float ty" << endl; - mOutput << "property float tz" << endl; - mOutput << "property float bx" << endl; - mOutput << "property float by" << endl; - mOutput << "property float bz" << endl; + mOutput << "property " << typeName << " tx" << endl; + mOutput << "property " << typeName << " ty" << endl; + mOutput << "property " << typeName << " tz" << endl; + mOutput << "property " << typeName << " bx" << endl; + mOutput << "property " << typeName << " by" << endl; + mOutput << "property " << typeName << " bz" << endl; } mOutput << "element face " << faces << endl; diff --git a/code/PlyParser.cpp b/code/PlyParser.cpp index bffc5be03..9168170e8 100644 --- a/code/PlyParser.cpp +++ b/code/PlyParser.cpp @@ -819,15 +819,18 @@ bool PLY::PropertyInstance::ParseValue( break; case EDT_Float: - - pCur = fast_atoreal_move(pCur,out->fFloat); + // technically this should cast to float, but people tend to use float descriptors for double data + // this is the best way to not risk loosing precision on import and it doesn't hurt to do this + ai_real f; + pCur = fast_atoreal_move(pCur,f); + out->fFloat = (ai_real)f; break; case EDT_Double: - double f; - pCur = fast_atoreal_move(pCur,f); - out->fDouble = (double)f; + double d; + pCur = fast_atoreal_move(pCur,d); + out->fDouble = (double)d; break; default: