From 9b9e4f668198141018fda2d8b6aee915c143cbc5 Mon Sep 17 00:00:00 2001 From: gianpaolopalma Date: Mon, 3 Oct 2016 10:09:27 +0200 Subject: [PATCH] Bug fixed for an implicit cast to float --- wrap/nanoply/include/nanoply.hpp | 61 +++++++++++++++++++++---------- wrap/nanoply/nanoply_vcg/main.cpp | 26 ++++++++----- 2 files changed, 57 insertions(+), 30 deletions(-) diff --git a/wrap/nanoply/include/nanoply.hpp b/wrap/nanoply/include/nanoply.hpp index 810d6fd5..071f5365 100644 --- a/wrap/nanoply/include/nanoply.hpp +++ b/wrap/nanoply/include/nanoply.hpp @@ -511,7 +511,7 @@ namespace nanoply bufferOffset = 0; } //memcpy(dest, &buffer[bufferOffset], nByte); - dest = buffer + bufferOffset; + dest = buffer + bufferOffset; bufferOffset += nByte; return true; } @@ -2075,18 +2075,23 @@ namespace nanoply if (typeSize > 1 && fixEndian) adjustEndianess(reinterpret_cast(buffer), typeSize, count); + unsigned char* baseProp = (unsigned char*)base + this->curPos*sizeof(ContainerType); C* temp = (C*)buffer; - float norm = 1.0f; if ((prop.elem == NNP_CRGB || prop.elem == NNP_CRGBA)) { + float norm = 1.0f; if (std::is_same::value && std::is_same::value) norm = 1.0f / 255.0f; else if (std::is_same::value && std::is_same::value) norm = 255.0f; + for (int i = 0; i < std::min(VectorSize, count); i++) + *((ScalarType *)(baseProp + i*sizeof(ScalarType))) = ScalarType(temp[i] * norm); } - unsigned char* baseProp = (unsigned char*)base + this->curPos*sizeof(ContainerType); - for (int i = 0; i < std::min(VectorSize, count); i++) - *((ScalarType *)(baseProp + i*sizeof(ScalarType))) = ScalarType(temp[i] * norm); + else + { + for (int i = 0; i < std::min(VectorSize, count); i++) + *((ScalarType *)(baseProp + i*sizeof(ScalarType))) = ScalarType(temp[i]); + } ++(this->curPos); } @@ -2140,17 +2145,22 @@ namespace nanoply for (int i = 0; i < count; i++) file.ReadAsciiData(temp[i]); - float norm = 1.0f; + unsigned char* baseProp = (unsigned char*)base + this->curPos*sizeof(ContainerType); if ((prop.elem == NNP_CRGB || prop.elem == NNP_CRGBA)) { + float norm = 1.0f; if (std::is_same::value && prop.type == NNP_UINT8) norm = 1.0f / 255.0f; else if (std::is_same::value && prop.type == NNP_FLOAT32) norm = 255.0f; + for (int i = 0; i < std::min(VectorSize, count); i++) + *((ScalarType *)(baseProp + i*sizeof(ScalarType))) = ScalarType(temp[i] * norm); } - unsigned char* baseProp = (unsigned char*)base + this->curPos*sizeof(ContainerType); - for (int i = 0; i < std::min(VectorSize, count); i++) - *((ScalarType *)(baseProp + i*sizeof(ScalarType))) = ScalarType(temp[i] * norm); + else + { + for (int i = 0; i < std::min(VectorSize, count); i++) + *((ScalarType *)(baseProp + i*sizeof(ScalarType))) = ScalarType(temp[i]); + } delete[] temp; ++(this->curPos); } @@ -2217,19 +2227,24 @@ namespace nanoply } } - float norm = 1.0f; + C temp = 0; + unsigned char* baseProp = (unsigned char*)base + this->curPos*sizeof(ContainerType); if ((prop.elem == NNP_CRGB || prop.elem == NNP_CRGBA)) { + float norm = 1.0f; if (std::is_same::value && std::is_same::value) norm = 255.0f; else if (std::is_same::value && std::is_same::value) norm = 1.0f / 255.0f; + for (int i = 0; i < std::min(VectorSize, count); i++) + data[i] = (C)((*(ScalarType*)(baseProp + i*sizeof(ScalarType))) * norm); } + else + { + for (int i = 0; i < std::min(VectorSize, count); i++) + data[i] = (C)((*(ScalarType*)(baseProp + i*sizeof(ScalarType)))); + } - C temp = 0; - unsigned char* baseProp = (unsigned char*)base + this->curPos*sizeof(ContainerType); - for (int i = 0; i < std::min(VectorSize, count); i++) - data[i] = (C)((*(ScalarType*)(baseProp + i*sizeof(ScalarType))) * norm); if (sizeof(C) > 1 && fixEndian) adjustEndianess((unsigned char*)data, sizeof(C), std::min(VectorSize, count)); @@ -2298,19 +2313,25 @@ namespace nanoply file.WriteAsciiData(std::string(" ")); } - float norm = 1.0; + C data[VectorSize]; + unsigned char* baseProp = (unsigned char*)base + this->curPos*sizeof(ContainerType); if ((prop.elem == NNP_CRGB || prop.elem == NNP_CRGBA)) { + float norm = 1.0; if (std::is_same::value && prop.type == NNP_UINT8) norm = 255.0f; else if (std::is_same::value && prop.type == NNP_FLOAT32) norm = 1.0f / 255.0f; + for (int i = 0; i < std::min(VectorSize, count); i++) + data[i] = (C)((*(ScalarType*)(baseProp + i*sizeof(ScalarType))) * norm); } - - C data[VectorSize]; - unsigned char* baseProp = (unsigned char*)base + this->curPos*sizeof(ContainerType); - for (int i = 0; i < std::min(VectorSize, count); i++) - data[i] = (C)((*(ScalarType*)(baseProp + i*sizeof(ScalarType))) * norm); + else + { + for (int i = 0; i < std::min(VectorSize, count); i++) + data[i] = (C)((*(ScalarType*)(baseProp + i*sizeof(ScalarType)))); + } + + for (int i = 0; i < (count - VectorSize); i++) data[i] = 0; diff --git a/wrap/nanoply/nanoply_vcg/main.cpp b/wrap/nanoply/nanoply_vcg/main.cpp index 14236dd3..62a95b41 100644 --- a/wrap/nanoply/nanoply_vcg/main.cpp +++ b/wrap/nanoply/nanoply_vcg/main.cpp @@ -71,8 +71,8 @@ public: faceBarycenter[i] = vcg::Barycenter(face[i]); material().resize(2); - material()[0] = { vcg::Point3f(0.1, 0.2, 0.3), vcg::Point3f(0.3, 0.3, 0.3), 5.0 }; - material()[1] = { vcg::Point3f(0.1, 0.1, 0.1), vcg::Point3f(0.5, 0.3, 0.4), 50.0 }; + material()[0] = { vcg::Point3f(0.1f, 0.2f, 0.3f), vcg::Point3f(0.3f, 0.3f, 0.3f), 5.0f }; + material()[1] = { vcg::Point3f(0.1f, 0.1f, 0.1f), vcg::Point3f(0.5f, 0.3f, 0.4f), 50.0f }; } }; @@ -87,9 +87,12 @@ bool Load(const char* filename, MyMesh& mesh) mesh.material().resize(count); customAttrib.AddVertexAttribDescriptor(std::string("materialId"), nanoply::NNP_INT32, NULL); customAttrib.AddFaceAttribDescriptor(std::string("barycenter"), nanoply::NNP_LIST_UINT8_FLOAT32, NULL); - customAttrib.AddMeshAttribDescriptor(std::string("material"), std::string("kd"), nanoply::NNP_FLOAT32, mesh.material()[0].kd.V()); - customAttrib.AddMeshAttribDescriptor(std::string("material"), std::string("ks"), nanoply::NNP_FLOAT32, mesh.material()[0].ks.V()); - customAttrib.AddMeshAttribDescriptor(std::string("material"), std::string("rho"), nanoply::NNP_FLOAT32, &mesh.material()[0].rho); + if (count > 0) + { + customAttrib.AddMeshAttribDescriptor(std::string("material"), std::string("kd"), nanoply::NNP_FLOAT32, mesh.material()[0].kd.V()); + customAttrib.AddMeshAttribDescriptor(std::string("material"), std::string("ks"), nanoply::NNP_FLOAT32, mesh.material()[0].ks.V()); + customAttrib.AddMeshAttribDescriptor(std::string("material"), std::string("rho"), nanoply::NNP_FLOAT32, &mesh.material()[0].rho); + } //Load the ply file unsigned int mask = 0; @@ -102,7 +105,7 @@ bool Load(const char* filename, MyMesh& mesh) mask |= nanoply::NanoPlyWrapper::IO_FACENORMAL; mask |= nanoply::NanoPlyWrapper::IO_FACEATTRIB; mask |= nanoply::NanoPlyWrapper::IO_MESHATTRIB; - return (nanoply::NanoPlyWrapper::LoadModel(filename, mesh, mask, customAttrib) != 0); + return (nanoply::NanoPlyWrapper::LoadModel(filename, mesh, mask, customAttrib) != 0); } @@ -113,10 +116,13 @@ bool Save(const char* filename, MyMesh& mesh, bool binary) nanoply::NanoPlyWrapper::CustomAttributeDescriptor customAttrib; customAttrib.AddVertexAttribDescriptor(std::string("materialId"), nanoply::NNP_INT32, &mesh.vertexMaterial[0]); customAttrib.AddFaceAttribDescriptor(std::string("barycenter"), nanoply::NNP_LIST_UINT8_FLOAT32, mesh.faceBarycenter[0].V()); - customAttrib.AddMeshAttrib(std::string("material"), 2); - customAttrib.AddMeshAttribDescriptor(std::string("material"), std::string("kd"), nanoply::NNP_LIST_UINT8_FLOAT32, mesh.material()[0].kd.V()); - customAttrib.AddMeshAttribDescriptor(std::string("material"), std::string("ks"), nanoply::NNP_LIST_UINT8_FLOAT32, mesh.material()[0].ks.V()); - customAttrib.AddMeshAttribDescriptor(std::string("material"), std::string("rho"), nanoply::NNP_FLOAT32, &mesh.material()[0].rho); + if (mesh.material().size() > 0) + { + customAttrib.AddMeshAttrib(std::string("material"), mesh.material().size()); + customAttrib.AddMeshAttribDescriptor(std::string("material"), std::string("kd"), nanoply::NNP_LIST_UINT8_FLOAT32, mesh.material()[0].kd.V()); + customAttrib.AddMeshAttribDescriptor(std::string("material"), std::string("ks"), nanoply::NNP_LIST_UINT8_FLOAT32, mesh.material()[0].ks.V()); + customAttrib.AddMeshAttribDescriptor(std::string("material"), std::string("rho"), nanoply::NNP_FLOAT32, &mesh.material()[0].rho); + } //Save the ply file unsigned int mask = 0;