Merge "Add Filter in InputFilter" into devel/master
[platform/core/uifw/dali-toolkit.git] / dali-scene3d / public-api / loader / mesh-definition.cpp
index b07d0e2..4b8f21d 100644 (file)
@@ -82,18 +82,22 @@ bool ReadBlob(const MeshDefinition::Blob& descriptor, std::istream& source, uint
   }
   else
   {
-    DALI_ASSERT_DEBUG(descriptor.mStride > descriptor.mElementSizeHint);
-    const uint32_t diff     = descriptor.mStride - descriptor.mElementSizeHint;
-    uint32_t       readSize = 0;
-    while(readSize < descriptor.mLength &&
-          source.read(reinterpret_cast<char*>(target), descriptor.mElementSizeHint) &&
-          source.seekg(diff, std::istream::cur))
-    {
-      readSize += descriptor.mStride;
-      target += descriptor.mElementSizeHint;
+    if(descriptor.mStride > descriptor.mElementSizeHint)
+    {
+      const uint32_t diff      = descriptor.mStride - descriptor.mElementSizeHint;
+      uint32_t       readSize  = 0;
+      uint32_t       totalSize = (descriptor.mLength / descriptor.mElementSizeHint) * descriptor.mStride;
+      while(readSize < totalSize &&
+            source.read(reinterpret_cast<char*>(target), descriptor.mElementSizeHint) &&
+            source.seekg(diff, std::istream::cur))
+      {
+        readSize += descriptor.mStride;
+        target += descriptor.mElementSizeHint;
+      }
+      return readSize == totalSize;
     }
-    return readSize == descriptor.mLength;
   }
+  return false;
 }
 
 template<typename T>
@@ -177,7 +181,7 @@ void GenerateNormals(MeshDefinition::RawData& raw)
   DALI_ASSERT_DEBUG(attribs.size() > 0); // positions
   IndexProvider getIndex(raw.mIndices.data());
 
-  const uint32_t numIndices = raw.mIndices.empty() ? attribs[0].mNumElements : raw.mIndices.size();
+  const uint32_t numIndices = raw.mIndices.empty() ? attribs[0].mNumElements : static_cast<uint32_t>(raw.mIndices.size());
 
   auto* positions = reinterpret_cast<const Vector3*>(attribs[0].mData.data());
 
@@ -214,7 +218,7 @@ void GenerateTangentsWithUvs(MeshDefinition::RawData& raw)
   DALI_ASSERT_DEBUG(attribs.size() > 2); // positions, normals, uvs
   IndexProvider getIndex(raw.mIndices.data());
 
-  const uint32_t numIndices = raw.mIndices.empty() ? attribs[0].mNumElements : raw.mIndices.size();
+  const uint32_t numIndices = raw.mIndices.empty() ? attribs[0].mNumElements : static_cast<uint32_t>(raw.mIndices.size());
 
   auto* positions = reinterpret_cast<const Vector3*>(attribs[0].mData.data());
   auto* uvs       = reinterpret_cast<const Vector2*>(attribs[2].mData.data());
@@ -328,7 +332,7 @@ void CalculateGltf2BlendShapes(uint8_t* geometryBuffer, std::ifstream& binFile,
       std::vector<uint8_t> buffer(bufferSize);
       if(ReadAccessor(blendShape.deltas, binFile, buffer.data()))
       {
-        blendShape.deltas.mBlob.ApplyMinMax(bufferSize / sizeof(Vector3), reinterpret_cast<float*>(buffer.data()));
+        blendShape.deltas.mBlob.ApplyMinMax(static_cast<uint32_t>(bufferSize / sizeof(Vector3)), reinterpret_cast<float*>(buffer.data()));
         // Calculate the difference with the original mesh.
         // Find the max distance to normalize the deltas.
         const Vector3* const deltasBuffer = reinterpret_cast<const Vector3* const>(buffer.data());
@@ -353,7 +357,7 @@ void CalculateGltf2BlendShapes(uint8_t* geometryBuffer, std::ifstream& binFile,
       std::vector<uint8_t> buffer(bufferSize);
       if(ReadAccessor(blendShape.normals, binFile, buffer.data()))
       {
-        blendShape.normals.mBlob.ApplyMinMax(bufferSize / sizeof(Vector3), reinterpret_cast<float*>(buffer.data()));
+        blendShape.normals.mBlob.ApplyMinMax(static_cast<uint32_t>(bufferSize / sizeof(Vector3)), reinterpret_cast<float*>(buffer.data()));
 
         // Calculate the difference with the original mesh, and translate to make all values positive.
         const Vector3* const deltasBuffer = reinterpret_cast<const Vector3* const>(buffer.data());
@@ -384,7 +388,7 @@ void CalculateGltf2BlendShapes(uint8_t* geometryBuffer, std::ifstream& binFile,
       std::vector<uint8_t> buffer(bufferSize);
       if(ReadAccessor(blendShape.tangents, binFile, buffer.data()))
       {
-        blendShape.tangents.mBlob.ApplyMinMax(bufferSize / sizeof(Vector3), reinterpret_cast<float*>(buffer.data()));
+        blendShape.tangents.mBlob.ApplyMinMax(static_cast<uint32_t>(bufferSize / sizeof(Vector3)), reinterpret_cast<float*>(buffer.data()));
 
         // Calculate the difference with the original mesh, and translate to make all values positive.
         const Vector3* const deltasBuffer = reinterpret_cast<const Vector3* const>(buffer.data());
@@ -518,7 +522,7 @@ MeshDefinition::Blob::Blob(uint32_t offset, uint32_t length, uint16_t stride, ui
 
 uint32_t MeshDefinition::Blob::GetBufferSize() const
 {
-  return IsConsecutive() ? mLength : (mLength * mElementSizeHint / mStride);
+  return mLength;
 }
 
 void MeshDefinition::Blob::ComputeMinMax(uint32_t numComponents, uint32_t count, float* values)
@@ -634,7 +638,7 @@ MeshDefinition::LoadRaw(const std::string& modelsPath)
       ExceptionFlinger(ASSERT_LOCATION) << "Failed to read positions from '" << meshPath << "'.";
     }
 
-    uint32_t numVector3 = bufferSize / sizeof(Vector3);
+    uint32_t numVector3 = static_cast<uint32_t>(bufferSize / sizeof(Vector3));
     if(mPositions.mBlob.mMin.size() != 3u || mPositions.mBlob.mMax.size() != 3u)
     {
       mPositions.mBlob.ComputeMinMax(3u, numVector3, reinterpret_cast<float*>(buffer.data()));
@@ -667,7 +671,7 @@ MeshDefinition::LoadRaw(const std::string& modelsPath)
       ExceptionFlinger(ASSERT_LOCATION) << "Failed to read normals from '" << meshPath << "'.";
     }
 
-    mNormals.mBlob.ApplyMinMax(bufferSize / sizeof(Vector3), reinterpret_cast<float*>(buffer.data()));
+    mNormals.mBlob.ApplyMinMax(static_cast<uint32_t>(bufferSize / sizeof(Vector3)), reinterpret_cast<float*>(buffer.data()));
 
     raw.mAttribs.push_back({"aNormal", Property::VECTOR3, static_cast<uint32_t>(bufferSize / sizeof(Vector3)), std::move(buffer)});
   }
@@ -703,14 +707,14 @@ MeshDefinition::LoadRaw(const std::string& modelsPath)
       }
     }
 
-    mTexCoords.mBlob.ApplyMinMax(bufferSize / sizeof(Vector2), reinterpret_cast<float*>(buffer.data()));
+    mTexCoords.mBlob.ApplyMinMax(static_cast<uint32_t>(bufferSize / sizeof(Vector2)), reinterpret_cast<float*>(buffer.data()));
 
     raw.mAttribs.push_back({"aTexCoord", Property::VECTOR2, static_cast<uint32_t>(uvCount), std::move(buffer)});
   }
 
   if(mTangents.IsDefined())
   {
-    uint32_t propertySize = (mTangentType == Property::VECTOR4) ? sizeof(Vector4) : sizeof(Vector3);
+    uint32_t propertySize = static_cast<uint32_t>((mTangentType == Property::VECTOR4) ? sizeof(Vector4) : sizeof(Vector3));
     DALI_ASSERT_ALWAYS(((mTangents.mBlob.mLength % propertySize == 0) ||
                         mTangents.mBlob.mStride >= propertySize) &&
                        "Tangents buffer length not a multiple of element size");
@@ -825,7 +829,7 @@ MeshDefinition::LoadRaw(const std::string& modelsPath)
 
   if(HasBlendShapes())
   {
-    const uint32_t numberOfVertices = mPositions.mBlob.mLength / sizeof(Vector3);
+    const uint32_t numberOfVertices = static_cast<uint32_t>(mPositions.mBlob.mLength / sizeof(Vector3));
 
     // Calculate the size of one buffer inside the texture.
     raw.mBlendShapeBufferOffset = numberOfVertices;
@@ -836,7 +840,7 @@ MeshDefinition::LoadRaw(const std::string& modelsPath)
 
     if(!mBlendShapeHeader.IsDefined())
     {
-      CalculateTextureSize(blendShapesBlob.mLength / sizeof(Vector3), textureWidth, textureHeight);
+      CalculateTextureSize(static_cast<uint32_t>(blendShapesBlob.mLength / sizeof(Vector3)), textureWidth, textureHeight);
       calculateGltf2BlendShapes = true;
     }
     else
@@ -860,7 +864,7 @@ MeshDefinition::LoadRaw(const std::string& modelsPath)
     else
     {
       Blob unnormalizeFactorBlob;
-      unnormalizeFactorBlob.mLength = sizeof(float) * ((BlendShapes::Version::VERSION_2_0 == mBlendShapeVersion) ? 1u : numberOfBlendShapes);
+      unnormalizeFactorBlob.mLength = static_cast<uint32_t>(sizeof(float) * ((BlendShapes::Version::VERSION_2_0 == mBlendShapeVersion) ? 1u : numberOfBlendShapes));
 
       if(blendShapesBlob.IsDefined())
       {