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

@ -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<unsigned char *>(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<ScalarType, float>::value && std::is_same<C, unsigned char>::value)
norm = 1.0f / 255.0f;
else if (std::is_same<ScalarType, unsigned char>::value && std::is_same<C, float>::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<ScalarType, float>::value && prop.type == NNP_UINT8)
norm = 1.0f / 255.0f;
else if (std::is_same<ScalarType, unsigned char>::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<ScalarType, float>::value && std::is_same<C, unsigned char>::value)
norm = 255.0f;
else if (std::is_same<ScalarType, unsigned char>::value && std::is_same<C, float>::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<ScalarType, float>::value && prop.type == NNP_UINT8)
norm = 255.0f;
else if (std::is_same<ScalarType, unsigned char>::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;

View File

@ -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<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.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, 1>(std::string("material"), std::string("rho"), nanoply::NNP_FLOAT32, &mesh.material()[0].rho);
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("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);
}
//Load the ply file
unsigned int mask = 0;
@ -102,7 +105,7 @@ bool Load(const char* filename, MyMesh& mesh)
mask |= nanoply::NanoPlyWrapper<MyMesh>::IO_FACENORMAL;
mask |= nanoply::NanoPlyWrapper<MyMesh>::IO_FACEATTRIB;
mask |= nanoply::NanoPlyWrapper<MyMesh>::IO_MESHATTRIB;
return (nanoply::NanoPlyWrapper<MyMesh>::LoadModel(filename, mesh, mask, customAttrib) != 0);
return (nanoply::NanoPlyWrapper<MyMesh>::LoadModel(filename, mesh, mask, customAttrib) != 0);
}
@ -113,10 +116,13 @@ bool Save(const char* filename, MyMesh& mesh, bool binary)
nanoply::NanoPlyWrapper<MyMesh>::CustomAttributeDescriptor customAttrib;
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.AddMeshAttrib(std::string("material"), 2);
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, 1>(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<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, 1>(std::string("material"), std::string("rho"), nanoply::NNP_FLOAT32, &mesh.material()[0].rho);
}
//Save the ply file
unsigned int mask = 0;