Bug fixed for an implicit cast to float

This commit is contained in:
Gianpaolo Palma 2016-10-03 10:09:27 +02:00
parent 684bb9ecfb
commit 9b9e4f6681
2 changed files with 57 additions and 30 deletions

View File

@ -2075,18 +2075,23 @@ namespace nanoply
if (typeSize > 1 && fixEndian) if (typeSize > 1 && fixEndian)
adjustEndianess(reinterpret_cast<unsigned char *>(buffer), typeSize, count); adjustEndianess(reinterpret_cast<unsigned char *>(buffer), typeSize, count);
unsigned char* baseProp = (unsigned char*)base + this->curPos*sizeof(ContainerType);
C* temp = (C*)buffer; C* temp = (C*)buffer;
float norm = 1.0f;
if ((prop.elem == NNP_CRGB || prop.elem == NNP_CRGBA)) if ((prop.elem == NNP_CRGB || prop.elem == NNP_CRGBA))
{ {
float norm = 1.0f;
if (std::is_same<ScalarType, float>::value && std::is_same<C, unsigned char>::value) if (std::is_same<ScalarType, float>::value && std::is_same<C, unsigned char>::value)
norm = 1.0f / 255.0f; norm = 1.0f / 255.0f;
else if (std::is_same<ScalarType, unsigned char>::value && std::is_same<C, float>::value) else if (std::is_same<ScalarType, unsigned char>::value && std::is_same<C, float>::value)
norm = 255.0f; norm = 255.0f;
}
unsigned char* baseProp = (unsigned char*)base + this->curPos*sizeof(ContainerType);
for (int i = 0; i < std::min(VectorSize, count); i++) for (int i = 0; i < std::min(VectorSize, count); i++)
*((ScalarType *)(baseProp + i*sizeof(ScalarType))) = ScalarType(temp[i] * norm); *((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); ++(this->curPos);
} }
@ -2140,17 +2145,22 @@ namespace nanoply
for (int i = 0; i < count; i++) for (int i = 0; i < count; i++)
file.ReadAsciiData(temp[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)) if ((prop.elem == NNP_CRGB || prop.elem == NNP_CRGBA))
{ {
float norm = 1.0f;
if (std::is_same<ScalarType, float>::value && prop.type == NNP_UINT8) if (std::is_same<ScalarType, float>::value && prop.type == NNP_UINT8)
norm = 1.0f / 255.0f; norm = 1.0f / 255.0f;
else if (std::is_same<ScalarType, unsigned char>::value && prop.type == NNP_FLOAT32) else if (std::is_same<ScalarType, unsigned char>::value && prop.type == NNP_FLOAT32)
norm = 255.0f; norm = 255.0f;
}
unsigned char* baseProp = (unsigned char*)base + this->curPos*sizeof(ContainerType);
for (int i = 0; i < std::min(VectorSize, count); i++) for (int i = 0; i < std::min(VectorSize, count); i++)
*((ScalarType *)(baseProp + i*sizeof(ScalarType))) = ScalarType(temp[i] * norm); *((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; delete[] temp;
++(this->curPos); ++(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)) if ((prop.elem == NNP_CRGB || prop.elem == NNP_CRGBA))
{ {
float norm = 1.0f;
if (std::is_same<ScalarType, float>::value && std::is_same<C, unsigned char>::value) if (std::is_same<ScalarType, float>::value && std::is_same<C, unsigned char>::value)
norm = 255.0f; norm = 255.0f;
else if (std::is_same<ScalarType, unsigned char>::value && std::is_same<C, float>::value) else if (std::is_same<ScalarType, unsigned char>::value && std::is_same<C, float>::value)
norm = 1.0f / 255.0f; norm = 1.0f / 255.0f;
}
C temp = 0;
unsigned char* baseProp = (unsigned char*)base + this->curPos*sizeof(ContainerType);
for (int i = 0; i < std::min(VectorSize, count); i++) for (int i = 0; i < std::min(VectorSize, count); i++)
data[i] = (C)((*(ScalarType*)(baseProp + i*sizeof(ScalarType))) * norm); 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))));
}
if (sizeof(C) > 1 && fixEndian) if (sizeof(C) > 1 && fixEndian)
adjustEndianess((unsigned char*)data, sizeof(C), std::min(VectorSize, count)); adjustEndianess((unsigned char*)data, sizeof(C), std::min(VectorSize, count));
@ -2298,19 +2313,25 @@ namespace nanoply
file.WriteAsciiData(std::string(" ")); 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)) if ((prop.elem == NNP_CRGB || prop.elem == NNP_CRGBA))
{ {
float norm = 1.0;
if (std::is_same<ScalarType, float>::value && prop.type == NNP_UINT8) if (std::is_same<ScalarType, float>::value && prop.type == NNP_UINT8)
norm = 255.0f; norm = 255.0f;
else if (std::is_same<ScalarType, unsigned char>::value && prop.type == NNP_FLOAT32) else if (std::is_same<ScalarType, unsigned char>::value && prop.type == NNP_FLOAT32)
norm = 1.0f / 255.0f; norm = 1.0f / 255.0f;
}
C data[VectorSize];
unsigned char* baseProp = (unsigned char*)base + this->curPos*sizeof(ContainerType);
for (int i = 0; i < std::min(VectorSize, count); i++) for (int i = 0; i < std::min(VectorSize, count); i++)
data[i] = (C)((*(ScalarType*)(baseProp + i*sizeof(ScalarType))) * norm); 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++) for (int i = 0; i < (count - VectorSize); i++)
data[i] = 0; data[i] = 0;

View File

@ -71,8 +71,8 @@ public:
faceBarycenter[i] = vcg::Barycenter(face[i]); faceBarycenter[i] = vcg::Barycenter(face[i]);
material().resize(2); material().resize(2);
material()[0] = { vcg::Point3f(0.1, 0.2, 0.3), vcg::Point3f(0.3, 0.3, 0.3), 5.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.1, 0.1, 0.1), vcg::Point3f(0.5, 0.3, 0.4), 50.0 }; 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); mesh.material().resize(count);
customAttrib.AddVertexAttribDescriptor<int, int, 1>(std::string("materialId"), nanoply::NNP_INT32, NULL); customAttrib.AddVertexAttribDescriptor<int, int, 1>(std::string("materialId"), nanoply::NNP_INT32, NULL);
customAttrib.AddFaceAttribDescriptor<vcg::Point3f, float, 3>(std::string("barycenter"), nanoply::NNP_LIST_UINT8_FLOAT32, NULL); customAttrib.AddFaceAttribDescriptor<vcg::Point3f, float, 3>(std::string("barycenter"), nanoply::NNP_LIST_UINT8_FLOAT32, NULL);
if (count > 0)
{
customAttrib.AddMeshAttribDescriptor<Material, float, 3>(std::string("material"), std::string("kd"), nanoply::NNP_FLOAT32, mesh.material()[0].kd.V()); customAttrib.AddMeshAttribDescriptor<Material, float, 3>(std::string("material"), std::string("kd"), nanoply::NNP_FLOAT32, mesh.material()[0].kd.V());
customAttrib.AddMeshAttribDescriptor<Material, float, 3>(std::string("material"), std::string("ks"), nanoply::NNP_FLOAT32, mesh.material()[0].ks.V()); customAttrib.AddMeshAttribDescriptor<Material, float, 3>(std::string("material"), std::string("ks"), nanoply::NNP_FLOAT32, mesh.material()[0].ks.V());
customAttrib.AddMeshAttribDescriptor<Material, float, 1>(std::string("material"), std::string("rho"), nanoply::NNP_FLOAT32, &mesh.material()[0].rho); customAttrib.AddMeshAttribDescriptor<Material, float, 1>(std::string("material"), std::string("rho"), nanoply::NNP_FLOAT32, &mesh.material()[0].rho);
}
//Load the ply file //Load the ply file
unsigned int mask = 0; unsigned int mask = 0;
@ -113,10 +116,13 @@ bool Save(const char* filename, MyMesh& mesh, bool binary)
nanoply::NanoPlyWrapper<MyMesh>::CustomAttributeDescriptor customAttrib; nanoply::NanoPlyWrapper<MyMesh>::CustomAttributeDescriptor customAttrib;
customAttrib.AddVertexAttribDescriptor<int, int, 1>(std::string("materialId"), nanoply::NNP_INT32, &mesh.vertexMaterial[0]); customAttrib.AddVertexAttribDescriptor<int, int, 1>(std::string("materialId"), nanoply::NNP_INT32, &mesh.vertexMaterial[0]);
customAttrib.AddFaceAttribDescriptor<vcg::Point3f, float, 3>(std::string("barycenter"), nanoply::NNP_LIST_UINT8_FLOAT32, mesh.faceBarycenter[0].V()); customAttrib.AddFaceAttribDescriptor<vcg::Point3f, float, 3>(std::string("barycenter"), nanoply::NNP_LIST_UINT8_FLOAT32, mesh.faceBarycenter[0].V());
customAttrib.AddMeshAttrib(std::string("material"), 2); if (mesh.material().size() > 0)
{
customAttrib.AddMeshAttrib(std::string("material"), mesh.material().size());
customAttrib.AddMeshAttribDescriptor<Material, float, 3>(std::string("material"), std::string("kd"), nanoply::NNP_LIST_UINT8_FLOAT32, mesh.material()[0].kd.V()); customAttrib.AddMeshAttribDescriptor<Material, float, 3>(std::string("material"), std::string("kd"), nanoply::NNP_LIST_UINT8_FLOAT32, mesh.material()[0].kd.V());
customAttrib.AddMeshAttribDescriptor<Material, float, 3>(std::string("material"), std::string("ks"), nanoply::NNP_LIST_UINT8_FLOAT32, mesh.material()[0].ks.V()); customAttrib.AddMeshAttribDescriptor<Material, float, 3>(std::string("material"), std::string("ks"), nanoply::NNP_LIST_UINT8_FLOAT32, mesh.material()[0].ks.V());
customAttrib.AddMeshAttribDescriptor<Material, float, 1>(std::string("material"), std::string("rho"), nanoply::NNP_FLOAT32, &mesh.material()[0].rho); customAttrib.AddMeshAttribDescriptor<Material, float, 1>(std::string("material"), std::string("rho"), nanoply::NNP_FLOAT32, &mesh.material()[0].rho);
}
//Save the ply file //Save the ply file
unsigned int mask = 0; unsigned int mask = 0;