From 91e6d3abeb2b35861538c31e0b0927a42d4f830f Mon Sep 17 00:00:00 2001 From: Cory Fabre Date: Thu, 21 Nov 2019 23:46:14 -0600 Subject: [PATCH 1/2] Fix glTF Accessor min and max values --- code/glTF/glTFAsset.h | 4 +- code/glTF/glTFAssetWriter.inl | 26 ++++++++-- code/glTF/glTFExporter.cpp | 84 ++++++++++++++++++++++----------- code/glTF2/glTF2Asset.h | 4 +- code/glTF2/glTF2AssetWriter.inl | 31 +++++++++--- code/glTF2/glTF2Exporter.cpp | 84 ++++++++++++++++++++++----------- 6 files changed, 163 insertions(+), 70 deletions(-) diff --git a/code/glTF/glTFAsset.h b/code/glTF/glTFAsset.h index ddc7f086e..e7dd9c438 100644 --- a/code/glTF/glTFAsset.h +++ b/code/glTF/glTFAsset.h @@ -362,8 +362,8 @@ namespace glTF ComponentType componentType; //!< The datatype of components in the attribute. (required) unsigned int count; //!< The number of attributes referenced by this accessor. (required) AttribType::Value type; //!< Specifies if the attribute is a scalar, vector, or matrix. (required) - std::vector max; //!< Maximum value of each component in this attribute. - std::vector min; //!< Minimum value of each component in this attribute. + std::vector max; //!< Maximum value of each component in this attribute. + std::vector min; //!< Minimum value of each component in this attribute. unsigned int GetNumComponents(); unsigned int GetBytesPerComponent(); diff --git a/code/glTF/glTFAssetWriter.inl b/code/glTF/glTFAssetWriter.inl index 1bbb8fd8c..a3c99a9a8 100644 --- a/code/glTF/glTFAssetWriter.inl +++ b/code/glTF/glTFAssetWriter.inl @@ -54,9 +54,9 @@ namespace glTF { namespace { - template + template inline - Value& MakeValue(Value& val, float(&r)[N], MemoryPoolAllocator<>& al) { + Value& MakeValue(Value& val, T(&r)[N], MemoryPoolAllocator<>& al) { val.SetArray(); val.Reserve(N, al); for (decltype(N) i = 0; i < N; ++i) { @@ -65,8 +65,9 @@ namespace glTF { return val; } + template inline - Value& MakeValue(Value& val, const std::vector & r, MemoryPoolAllocator<>& al) { + Value& MakeValue(Value& val, const std::vector & r, MemoryPoolAllocator<>& al) { val.SetArray(); val.Reserve(static_cast(r.size()), al); for (unsigned int i = 0; i < r.size(); ++i) { @@ -75,6 +76,16 @@ namespace glTF { return val; } + template + inline Value& MakeValueCast(Value& val, const std::vector & r, MemoryPoolAllocator<>& al) { + val.SetArray(); + val.Reserve(static_cast(r.size()), al); + for (unsigned int i = 0; i < r.size(); ++i) { + val.PushBack(static_cast(r[i]), al); + } + return val; + } + template inline void AddRefsVector(Value& obj, const char* fieldId, std::vector< Ref >& v, MemoryPoolAllocator<>& al) { if (v.empty()) return; @@ -100,8 +111,13 @@ namespace glTF { obj.AddMember("type", StringRef(AttribType::ToString(a.type)), w.mAl); Value vTmpMax, vTmpMin; - obj.AddMember("max", MakeValue(vTmpMax, a.max, w.mAl), w.mAl); - obj.AddMember("min", MakeValue(vTmpMin, a.min, w.mAl), w.mAl); + if (a.componentType == ComponentType_FLOAT) { + obj.AddMember("max", MakeValue(vTmpMax, a.max, w.mAl), w.mAl); + obj.AddMember("min", MakeValue(vTmpMin, a.min, w.mAl), w.mAl); + } else { + obj.AddMember("max", MakeValueCast(vTmpMax, a.max, w.mAl), w.mAl); + obj.AddMember("min", MakeValueCast(vTmpMin, a.min, w.mAl), w.mAl); + } } inline void Write(Value& obj, Animation& a, AssetWriter& w) diff --git a/code/glTF/glTFExporter.cpp b/code/glTF/glTFExporter.cpp index 034f91f3b..676f8a83b 100644 --- a/code/glTF/glTFExporter.cpp +++ b/code/glTF/glTFExporter.cpp @@ -173,6 +173,62 @@ static void IdentityMatrix4(glTF::mat4& o) o[12] = 0; o[13] = 0; o[14] = 0; o[15] = 1; } +template +void SetAccessorRange(Ref acc, void* data, unsigned int count, + unsigned int numCompsIn, unsigned int numCompsOut) +{ + ai_assert(numCompsOut <= numCompsIn); + + // Allocate and initialize with large values. + for (unsigned int i = 0 ; i < numCompsOut ; i++) { + acc->min.push_back(DBL_MAX); + acc->max.push_back(DBL_MIN); + } + + size_t totalComps = count * numCompsIn; + T* buffer_ptr = static_cast(data); + T* buffer_end = buffer_ptr + totalComps; + + // Search and set extreme values. + for (; buffer_ptr < buffer_end ; buffer_ptr += numCompsIn) { + for (unsigned int j = 0 ; j < numCompsOut ; j++) { + double valueTmp = buffer_ptr[j]; + + if (valueTmp < acc->min[j]) { + acc->min[j] = valueTmp; + } + if (valueTmp > acc->max[j]) { + acc->max[j] = valueTmp; + } + } + } +} + +inline void SetAccessorRange(ComponentType compType, Ref acc, void* data, + unsigned int count, unsigned int numCompsIn, unsigned int numCompsOut) +{ + switch (compType) { + case ComponentType_SHORT: + SetAccessorRange(acc, data, count, numCompsIn, numCompsOut); + return; + case ComponentType_UNSIGNED_SHORT: + SetAccessorRange(acc, data, count, numCompsIn, numCompsOut); + return; + case ComponentType_UNSIGNED_INT: + SetAccessorRange(acc, data, count, numCompsIn, numCompsOut); + return; + case ComponentType_FLOAT: + SetAccessorRange(acc, data, count, numCompsIn, numCompsOut); + return; + case ComponentType_BYTE: + SetAccessorRange(acc, data, count, numCompsIn, numCompsOut); + return; + case ComponentType_UNSIGNED_BYTE: + SetAccessorRange(acc, data, count, numCompsIn, numCompsOut); + return; + } +} + inline Ref ExportData(Asset& a, std::string& meshName, Ref& buffer, unsigned int count, void* data, AttribType::Value typeIn, AttribType::Value typeOut, ComponentType compType, bool isIndices = false) { @@ -206,33 +262,7 @@ inline Ref ExportData(Asset& a, std::string& meshName, Ref& bu acc->type = typeOut; // calculate min and max values - { - // Allocate and initialize with large values. - float float_MAX = 10000000000000.0f; - for (unsigned int i = 0 ; i < numCompsOut ; i++) { - acc->min.push_back( float_MAX); - acc->max.push_back(-float_MAX); - } - - // Search and set extreme values. - float valueTmp; - for (unsigned int i = 0 ; i < count ; i++) { - for (unsigned int j = 0 ; j < numCompsOut ; j++) { - if (numCompsOut == 1) { - valueTmp = static_cast(data)[i]; - } else { - valueTmp = static_cast(data)[i][j]; - } - - if (valueTmp < acc->min[j]) { - acc->min[j] = valueTmp; - } - if (valueTmp > acc->max[j]) { - acc->max[j] = valueTmp; - } - } - } - } + SetAccessorRange(compType, acc, data, count, numCompsIn, numCompsOut); // copy the data acc->WriteData(count, data, numCompsIn*bytesPerComp); diff --git a/code/glTF2/glTF2Asset.h b/code/glTF2/glTF2Asset.h index 15c4c44fa..201d87d6b 100644 --- a/code/glTF2/glTF2Asset.h +++ b/code/glTF2/glTF2Asset.h @@ -387,8 +387,8 @@ namespace glTF2 ComponentType componentType; //!< The datatype of components in the attribute. (required) size_t count; //!< The number of attributes referenced by this accessor. (required) AttribType::Value type; //!< Specifies if the attribute is a scalar, vector, or matrix. (required) - std::vector max; //!< Maximum value of each component in this attribute. - std::vector min; //!< Minimum value of each component in this attribute. + std::vector max; //!< Maximum value of each component in this attribute. + std::vector min; //!< Minimum value of each component in this attribute. unsigned int GetNumComponents(); unsigned int GetBytesPerComponent(); diff --git a/code/glTF2/glTF2AssetWriter.inl b/code/glTF2/glTF2AssetWriter.inl index 5d1b22064..4f545ebc7 100644 --- a/code/glTF2/glTF2AssetWriter.inl +++ b/code/glTF2/glTF2AssetWriter.inl @@ -54,8 +54,8 @@ namespace glTF2 { namespace { - template - inline Value& MakeValue(Value& val, float(&r)[N], MemoryPoolAllocator<>& al) { + template + inline Value& MakeValue(Value& val, T(&r)[N], MemoryPoolAllocator<>& al) { val.SetArray(); val.Reserve(N, al); for (decltype(N) i = 0; i < N; ++i) { @@ -64,7 +64,8 @@ namespace glTF2 { return val; } - inline Value& MakeValue(Value& val, const std::vector & r, MemoryPoolAllocator<>& al) { + template + inline Value& MakeValue(Value& val, const std::vector & r, MemoryPoolAllocator<>& al) { val.SetArray(); val.Reserve(static_cast(r.size()), al); for (unsigned int i = 0; i < r.size(); ++i) { @@ -73,8 +74,19 @@ namespace glTF2 { return val; } - inline Value& MakeValue(Value& val, float r, MemoryPoolAllocator<>& /*al*/) { - val.SetDouble(r); + template + inline Value& MakeValueCast(Value& val, const std::vector & r, MemoryPoolAllocator<>& al) { + val.SetArray(); + val.Reserve(static_cast(r.size()), al); + for (unsigned int i = 0; i < r.size(); ++i) { + val.PushBack(static_cast(r[i]), al); + } + return val; + } + + template + inline Value& MakeValue(Value& val, T r, MemoryPoolAllocator<>& /*al*/) { + val.Set(r); return val; } @@ -104,8 +116,13 @@ namespace glTF2 { obj.AddMember("type", StringRef(AttribType::ToString(a.type)), w.mAl); Value vTmpMax, vTmpMin; - obj.AddMember("max", MakeValue(vTmpMax, a.max, w.mAl), w.mAl); - obj.AddMember("min", MakeValue(vTmpMin, a.min, w.mAl), w.mAl); + if (a.componentType == ComponentType_FLOAT) { + obj.AddMember("max", MakeValue(vTmpMax, a.max, w.mAl), w.mAl); + obj.AddMember("min", MakeValue(vTmpMin, a.min, w.mAl), w.mAl); + } else { + obj.AddMember("max", MakeValueCast(vTmpMax, a.max, w.mAl), w.mAl); + obj.AddMember("min", MakeValueCast(vTmpMin, a.min, w.mAl), w.mAl); + } } inline void Write(Value& obj, Animation& a, AssetWriter& w) diff --git a/code/glTF2/glTF2Exporter.cpp b/code/glTF2/glTF2Exporter.cpp index 4724c2ef4..cab02710f 100644 --- a/code/glTF2/glTF2Exporter.cpp +++ b/code/glTF2/glTF2Exporter.cpp @@ -152,6 +152,62 @@ static void IdentityMatrix4(mat4& o) { o[12] = 0; o[13] = 0; o[14] = 0; o[15] = 1; } +template +void SetAccessorRange(Ref acc, void* data, size_t count, + unsigned int numCompsIn, unsigned int numCompsOut) +{ + ai_assert(numCompsOut <= numCompsIn); + + // Allocate and initialize with large values. + for (unsigned int i = 0 ; i < numCompsOut ; i++) { + acc->min.push_back(DBL_MAX); + acc->max.push_back(DBL_MIN); + } + + size_t totalComps = count * numCompsIn; + T* buffer_ptr = static_cast(data); + T* buffer_end = buffer_ptr + totalComps; + + // Search and set extreme values. + for (; buffer_ptr < buffer_end ; buffer_ptr += numCompsIn) { + for (unsigned int j = 0 ; j < numCompsOut ; j++) { + double valueTmp = buffer_ptr[j]; + + if (valueTmp < acc->min[j]) { + acc->min[j] = valueTmp; + } + if (valueTmp > acc->max[j]) { + acc->max[j] = valueTmp; + } + } + } +} + +inline void SetAccessorRange(ComponentType compType, Ref acc, void* data, + size_t count, unsigned int numCompsIn, unsigned int numCompsOut) +{ + switch (compType) { + case ComponentType_SHORT: + SetAccessorRange(acc, data, count, numCompsIn, numCompsOut); + return; + case ComponentType_UNSIGNED_SHORT: + SetAccessorRange(acc, data, count, numCompsIn, numCompsOut); + return; + case ComponentType_UNSIGNED_INT: + SetAccessorRange(acc, data, count, numCompsIn, numCompsOut); + return; + case ComponentType_FLOAT: + SetAccessorRange(acc, data, count, numCompsIn, numCompsOut); + return; + case ComponentType_BYTE: + SetAccessorRange(acc, data, count, numCompsIn, numCompsOut); + return; + case ComponentType_UNSIGNED_BYTE: + SetAccessorRange(acc, data, count, numCompsIn, numCompsOut); + return; + } +} + inline Ref ExportData(Asset& a, std::string& meshName, Ref& buffer, size_t count, void* data, AttribType::Value typeIn, AttribType::Value typeOut, ComponentType compType, bool isIndices = false) { @@ -187,33 +243,7 @@ inline Ref ExportData(Asset& a, std::string& meshName, Ref& bu acc->type = typeOut; // calculate min and max values - { - // Allocate and initialize with large values. - float float_MAX = 10000000000000.0f; - for (unsigned int i = 0 ; i < numCompsOut ; i++) { - acc->min.push_back( float_MAX); - acc->max.push_back(-float_MAX); - } - - // Search and set extreme values. - float valueTmp; - for (unsigned int i = 0 ; i < count ; i++) { - for (unsigned int j = 0 ; j < numCompsOut ; j++) { - if (numCompsOut == 1) { - valueTmp = static_cast(data)[i]; - } else { - valueTmp = static_cast(data)[i][j]; - } - - if (valueTmp < acc->min[j]) { - acc->min[j] = valueTmp; - } - if (valueTmp > acc->max[j]) { - acc->max[j] = valueTmp; - } - } - } - } + SetAccessorRange(compType, acc, data, count, numCompsIn, numCompsOut); // copy the data acc->WriteData(count, data, numCompsIn*bytesPerComp); From 136996e1f674f964bfa55dd092ed3df5a2ad5f6c Mon Sep 17 00:00:00 2001 From: Cory Fabre Date: Fri, 22 Nov 2019 10:56:31 -0600 Subject: [PATCH 2/2] Use std::numeric_limits for double max --- code/glTF/glTFExporter.cpp | 5 +++-- code/glTF2/glTF2Exporter.cpp | 5 +++-- 2 files changed, 6 insertions(+), 4 deletions(-) diff --git a/code/glTF/glTFExporter.cpp b/code/glTF/glTFExporter.cpp index 676f8a83b..3faa1a84b 100644 --- a/code/glTF/glTFExporter.cpp +++ b/code/glTF/glTFExporter.cpp @@ -58,6 +58,7 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. // Header files, standard library. #include +#include #include #ifdef ASSIMP_IMPORTER_GLTF_USE_OPEN3DGC @@ -181,8 +182,8 @@ void SetAccessorRange(Ref acc, void* data, unsigned int count, // Allocate and initialize with large values. for (unsigned int i = 0 ; i < numCompsOut ; i++) { - acc->min.push_back(DBL_MAX); - acc->max.push_back(DBL_MIN); + acc->min.push_back( std::numeric_limits::max()); + acc->max.push_back(-std::numeric_limits::max()); } size_t totalComps = count * numCompsIn; diff --git a/code/glTF2/glTF2Exporter.cpp b/code/glTF2/glTF2Exporter.cpp index 01ec1ba5a..f85803b52 100644 --- a/code/glTF2/glTF2Exporter.cpp +++ b/code/glTF2/glTF2Exporter.cpp @@ -58,6 +58,7 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. // Header files, standard library. #include +#include #include using namespace rapidjson; @@ -160,8 +161,8 @@ void SetAccessorRange(Ref acc, void* data, size_t count, // Allocate and initialize with large values. for (unsigned int i = 0 ; i < numCompsOut ; i++) { - acc->min.push_back(DBL_MAX); - acc->max.push_back(DBL_MIN); + acc->min.push_back( std::numeric_limits::max()); + acc->max.push_back(-std::numeric_limits::max()); } size_t totalComps = count * numCompsIn;