[Bf-blender-cvs] [bd0a9f19430] soc-2021-adaptive-cloth: adaptive_cloth: Mesh: msgpack: store the type of Mesh on first line

ishbosamiya noreply at git.blender.org
Sun Aug 22 17:23:39 CEST 2021


Commit: bd0a9f194301392f02eb553133f0167d6b6caac2
Author: ishbosamiya
Date:   Thu Aug 12 13:25:37 2021 +0530
Branches: soc-2021-adaptive-cloth
https://developer.blender.org/rBbd0a9f194301392f02eb553133f0167d6b6caac2

adaptive_cloth: Mesh: msgpack: store the type of Mesh on first line

This allows the code that deserializes it to decide which type of Mesh
structure to deserialize to. Since msgpack is compact, it doesn't
store information about the size (explicitly or implicitly) of the
element, this means that deserialization can lead to weird outputs if
wrong data structures are used.

===================================================================

M	source/blender/blenkernel/BKE_cloth_remesh.hh
M	source/blender/blenkernel/intern/cloth_remesh.cc

===================================================================

diff --git a/source/blender/blenkernel/BKE_cloth_remesh.hh b/source/blender/blenkernel/BKE_cloth_remesh.hh
index e66dfd7655c..6e7dd9e8db3 100644
--- a/source/blender/blenkernel/BKE_cloth_remesh.hh
+++ b/source/blender/blenkernel/BKE_cloth_remesh.hh
@@ -2458,6 +2458,8 @@ template<typename END, typename EVD, typename EED, typename EFD> class Mesh {
   std::string serialize() const
   {
     std::stringstream ss;
+    ss << "GenericMesh" << std::endl;
+
     msgpack::pack(ss, *this);
 
     return ss.str();
@@ -3338,79 +3340,86 @@ MSGPACK_API_VERSION_NAMESPACE(MSGPACK_DEFAULT_API_NS)
 {
   namespace adaptor {
 
-  template<typename END, typename EVD, typename EED, typename EFD>
-  struct pack<blender::bke::internal::Mesh<END, EVD, EED, EFD>> {
+  template<typename END, typename EVD, typename EED, typename EFD, typename Stream>
+  void pack_mesh(msgpack::packer<Stream> &o,
+                 const blender::bke::internal::Mesh<END, EVD, EED, EFD> &mesh)
+  {
+    blender::Map<blender::bke::internal::NodeIndex, uint64_t> node_pos_index_map;
+    auto node_pos = 0;
+    for (const auto &node : mesh.get_nodes()) {
+      auto self_index = node.get_self_index();
+      node_pos_index_map.add_new(self_index, node_pos);
+      node_pos++;
+    }
+
+    blender::Map<blender::bke::internal::VertIndex, uint64_t> vert_pos_index_map;
+    auto vert_pos = 0;
+    for (const auto &vert : mesh.get_verts()) {
+      auto self_index = vert.get_self_index();
+      vert_pos_index_map.add_new(self_index, vert_pos);
+      vert_pos++;
+    }
+
+    blender::Map<blender::bke::internal::EdgeIndex, uint64_t> edge_pos_index_map;
+    auto edge_pos = 0;
+    for (const auto &edge : mesh.get_edges()) {
+      auto self_index = edge.get_self_index();
+      edge_pos_index_map.add_new(self_index, edge_pos);
+      edge_pos++;
+    }
+
+    blender::Map<blender::bke::internal::FaceIndex, uint64_t> face_pos_index_map;
+    auto face_pos = 0;
+    for (const auto &face : mesh.get_faces()) {
+      auto self_index = face.get_self_index();
+      face_pos_index_map.add_new(self_index, face_pos);
+      face_pos++;
+    }
+
+    /* Need to store the arenas and the corresponding mappings
+     * between the arena index and positional index of that element */
+    o.pack_array(16);
+
+    o.pack(std::string("nodes"));
+    o.pack(mesh.get_nodes());
+    o.pack(std::string("verts"));
+    o.pack(mesh.get_verts());
+    o.pack(std::string("edges"));
+    o.pack(mesh.get_edges());
+    o.pack(std::string("faces"));
+    o.pack(mesh.get_faces());
+
+    o.pack(std::string("node_map"));
+    o.pack(node_pos_index_map);
+    o.pack(std::string("vert_map"));
+    o.pack(vert_pos_index_map);
+    o.pack(std::string("edge_map"));
+    o.pack(edge_pos_index_map);
+    o.pack(std::string("face_map"));
+    o.pack(face_pos_index_map);
+  }
+
+  template<> struct pack<blender::bke::internal::EmptyExtraData> {
     template<typename Stream>
-    msgpack::packer<Stream> &operator()(
-        msgpack::packer<Stream> &o,
-        const blender::bke::internal::Mesh<END, EVD, EED, EFD> &mesh) const
+    msgpack::packer<Stream> &operator()(msgpack::packer<Stream> &o,
+                                        blender::bke::internal::EmptyExtraData /*unused*/) const
     {
-      blender::Map<blender::bke::internal::NodeIndex, uint64_t> node_pos_index_map;
-      auto node_pos = 0;
-      for (const auto &node : mesh.get_nodes()) {
-        auto self_index = node.get_self_index();
-        node_pos_index_map.add_new(self_index, node_pos);
-        node_pos++;
-      }
-
-      blender::Map<blender::bke::internal::VertIndex, uint64_t> vert_pos_index_map;
-      auto vert_pos = 0;
-      for (const auto &vert : mesh.get_verts()) {
-        auto self_index = vert.get_self_index();
-        vert_pos_index_map.add_new(self_index, vert_pos);
-        vert_pos++;
-      }
-
-      blender::Map<blender::bke::internal::EdgeIndex, uint64_t> edge_pos_index_map;
-      auto edge_pos = 0;
-      for (const auto &edge : mesh.get_edges()) {
-        auto self_index = edge.get_self_index();
-        edge_pos_index_map.add_new(self_index, edge_pos);
-        edge_pos++;
-      }
-
-      blender::Map<blender::bke::internal::FaceIndex, uint64_t> face_pos_index_map;
-      auto face_pos = 0;
-      for (const auto &face : mesh.get_faces()) {
-        auto self_index = face.get_self_index();
-        face_pos_index_map.add_new(self_index, face_pos);
-        face_pos++;
-      }
+      o.pack_array(1);
 
-      /* Need to store the arenas and the corresponding mappings
-       * between the arena index and positional index of that element */
-      o.pack_array(16);
-
-      o.pack(std::string("nodes"));
-      o.pack(mesh.get_nodes());
-      o.pack(std::string("verts"));
-      o.pack(mesh.get_verts());
-      o.pack(std::string("edges"));
-      o.pack(mesh.get_edges());
-      o.pack(std::string("faces"));
-      o.pack(mesh.get_faces());
-
-      o.pack(std::string("node_map"));
-      o.pack(node_pos_index_map);
-      o.pack(std::string("vert_map"));
-      o.pack(vert_pos_index_map);
-      o.pack(std::string("edge_map"));
-      o.pack(edge_pos_index_map);
-      o.pack(std::string("face_map"));
-      o.pack(face_pos_index_map);
+      o.pack("EmptyExtraData");
 
       return o;
     }
   };
 
-  template<> struct pack<blender::bke::internal::EmptyExtraData> {
+  template<typename END, typename EVD, typename EED, typename EFD>
+  struct pack<blender::bke::internal::Mesh<END, EVD, EED, EFD>> {
     template<typename Stream>
-    msgpack::packer<Stream> &operator()(msgpack::packer<Stream> &o,
-                                        blender::bke::internal::EmptyExtraData /*unused*/) const
+    msgpack::packer<Stream> &operator()(
+        msgpack::packer<Stream> &o,
+        const blender::bke::internal::Mesh<END, EVD, EED, EFD> &mesh) const
     {
-      o.pack_array(1);
-
-      o.pack("EmptyExtraData");
+      pack_mesh(o, mesh);
 
       return o;
     }
diff --git a/source/blender/blenkernel/intern/cloth_remesh.cc b/source/blender/blenkernel/intern/cloth_remesh.cc
index 2189237a21e..74d6b47a23d 100644
--- a/source/blender/blenkernel/intern/cloth_remesh.cc
+++ b/source/blender/blenkernel/intern/cloth_remesh.cc
@@ -316,6 +316,16 @@ using AdaptiveMeshDiff = MeshDiff<NodeData<T>, VertData, EdgeData, internal::Emp
 template<typename END>
 class AdaptiveMesh : public Mesh<NodeData<END>, VertData, EdgeData, internal::EmptyExtraData> {
  public:
+  std::string serialize() const
+  {
+    std::stringstream ss;
+    ss << "GenericAdaptiveMesh" << std::endl;
+
+    msgpack::pack(ss, *this);
+
+    return ss.str();
+  }
+
   float compute_edge_size(const AdaptiveVert &v1, const AdaptiveVert &v2) const
   {
     const auto &v1_uv = v1.get_uv();
@@ -969,6 +979,33 @@ class AdaptiveMesh : public Mesh<NodeData<END>, VertData, EdgeData, internal::Em
   }
 };
 
+template<typename END> using AdaptiveNode = Node<NodeData<END>>;
+using AdaptiveVert = Vert<VertData>;
+using AdaptiveEdge = Edge<EdgeData>;
+using AdaptiveFace = Face<EmptyExtraData>;
+using EmptyAdaptiveMesh = AdaptiveMesh<EmptyExtraData>;
+using ClothAdaptiveMesh = AdaptiveMesh<ClothNodeData>;
+
+template<> std::string EmptyAdaptiveMesh::serialize() const
+{
+  std::stringstream ss;
+  ss << "EmptyAdaptiveMesh" << std::endl;
+
+  msgpack::pack(ss, *this);
+
+  return ss.str();
+}
+
+template<> std::string ClothAdaptiveMesh::serialize() const
+{
+  std::stringstream ss;
+  ss << "ClothAdaptiveMesh" << std::endl;
+
+  msgpack::pack(ss, *this);
+
+  return ss.str();
+}
+
 }  // namespace blender::bke::internal
 
 namespace msgpack {
@@ -1116,6 +1153,28 @@ MSGPACK_API_VERSION_NAMESPACE(MSGPACK_DEFAULT_API_NS)
     }
   };
 
+  template<> struct pack<blender::bke::internal::EmptyAdaptiveMesh> {
+    template<typename Stream>
+    msgpack::packer<Stream> &operator()(
+        msgpack::packer<Stream> &o, const blender::bke::internal::EmptyAdaptiveMesh &mesh) const
+    {
+      pack_mesh(o, mesh);
+
+      return o;
+    }
+  };
+
+  template<> struct pack<blender::bke::internal::ClothAdaptiveMesh> {
+    template<typename Stream>
+    msgpack::packer<Stream> &operator()(
+        msgpack::packer<Stream> &o, const blender::bke::internal::ClothAdaptiveMesh &mesh) const
+    {
+      pack_mesh(o, mesh);
+
+      return o;
+    }
+  };
+
   }  // namespace adaptor
 }  // MSGPACK_API_VERSION_NAMESPACE(MSGPACK_DEFAULT_API_NS)
 }  // namespace msgpack



More information about the Bf-blender-cvs mailing list