[Bf-blender-cvs] [0126abfd6e9] soc-2021-adaptive-cloth: adaptive_cloth: MeshIO: write obj

ishbosamiya noreply at git.blender.org
Mon Jun 28 08:28:24 CEST 2021


Commit: 0126abfd6e9f0c33c9451f7543b3dfc37aff0be6
Author: ishbosamiya
Date:   Thu Jun 24 18:14:41 2021 +0530
Branches: soc-2021-adaptive-cloth
https://developer.blender.org/rB0126abfd6e9f0c33c9451f7543b3dfc37aff0be6

adaptive_cloth: MeshIO: write obj

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

M	source/blender/blenkernel/BKE_cloth_remesh.hh
M	source/blender/blenkernel/tests/BKE_cloth_remesh_test.cc

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

diff --git a/source/blender/blenkernel/BKE_cloth_remesh.hh b/source/blender/blenkernel/BKE_cloth_remesh.hh
index bc3dba35997..044ec8725f5 100644
--- a/source/blender/blenkernel/BKE_cloth_remesh.hh
+++ b/source/blender/blenkernel/BKE_cloth_remesh.hh
@@ -389,6 +389,38 @@ class MeshIO {
     return true;
   }
 
+  bool write(const fs::path &filepath, FileType type)
+  {
+    if (type != FILETYPE_OBJ) {
+      return false;
+    }
+
+    if (!fs::exists(filepath)) {
+      return false;
+    }
+
+    std::fstream fout;
+    fout.open(filepath, std::ios::out);
+
+    if (!fout.is_open()) {
+      return false;
+    }
+
+    write(fout, type);
+
+    return true;
+  }
+
+  void write(std::ostream &out, FileType type)
+  {
+    if (type == FILETYPE_OBJ) {
+      this->write_obj(out);
+    }
+    else {
+      BLI_assert_unreachable();
+    }
+  }
+
   const auto &get_positions() const
   {
     return this->positions;
@@ -414,6 +446,11 @@ class MeshIO {
     return this->line_indices;
   }
 
+  static constexpr inline auto invalid_index()
+  {
+    return std::numeric_limits<usize>::max();
+  }
+
  private:
   blender::Vector<std::string> tokenize(std::string const &str, const char delim)
   {
@@ -487,9 +524,8 @@ class MeshIO {
             std::istringstream isi(indices_str[0]);
             usize pos_index;
             isi >> pos_index;
-            face.append(std::make_tuple(pos_index - 1,
-                                        std::numeric_limits<usize>::max(),
-                                        std::numeric_limits<usize>::max()));
+            face.append(
+                std::make_tuple(pos_index - 1, MeshIO::invalid_index(), MeshIO::invalid_index()));
           }
           else if (indices_str.size() == 2) {
             std::istringstream isi_pos(indices_str[0]);
@@ -498,8 +534,7 @@ class MeshIO {
             usize uv_index;
             isi_pos >> pos_index;
             isi_uv >> uv_index;
-            face.append(
-                std::make_tuple(pos_index - 1, uv_index - 1, std::numeric_limits<usize>::max()));
+            face.append(std::make_tuple(pos_index - 1, uv_index - 1, MeshIO::invalid_index()));
           }
           else if (indices_str.size() == 3) {
             std::istringstream isi_pos(indices_str[0]);
@@ -544,6 +579,41 @@ class MeshIO {
 
     return true;
   }
+
+  void write_obj(std::ostream &out)
+  {
+    for (const auto &pos : this->positions) {
+      out << "v " << pos.x << " " << pos.y << " " << pos.z << std::endl;
+    }
+
+    for (const auto &uv : this->uvs) {
+      out << "vt " << uv.x << " " << uv.y << std::endl;
+    }
+
+    for (const auto &normal : this->normals) {
+      out << "v " << normal.x << " " << normal.y << " " << normal.z << std::endl;
+    }
+
+    for (const auto &face : this->face_indices) {
+      out << "f ";
+      for (const auto &[pos_index, uv_index, normal_index] : face) {
+        if (normal_index == MeshIO::invalid_index()) {
+          if (uv_index == MeshIO::invalid_index()) {
+            out << pos_index + 1 << " ";
+          }
+          else {
+            out << pos_index + 1 << "/" << uv_index + 1 << " ";
+          }
+        }
+        else {
+          out << pos_index + 1 << "/" << uv_index + 1 << "/" << normal_index + 1 << " ";
+        }
+      }
+      out << std::endl;
+    }
+
+    /* TODO(ish): add line support */
+  }
 };
 
 template<typename END, typename EVD, typename EED, typename EFD> class Mesh {
diff --git a/source/blender/blenkernel/tests/BKE_cloth_remesh_test.cc b/source/blender/blenkernel/tests/BKE_cloth_remesh_test.cc
index a2e3b4a0760..43b6ef5d4d4 100644
--- a/source/blender/blenkernel/tests/BKE_cloth_remesh_test.cc
+++ b/source/blender/blenkernel/tests/BKE_cloth_remesh_test.cc
@@ -71,6 +71,55 @@ TEST(cloth_remesh, MeshIO_ReadObj)
   EXPECT_EQ(line_indices.size(), 0);
 }
 
+TEST(cloth_remesh, MeshIO_WriteObj)
+{
+  MeshIO reader;
+  std::istringstream stream_in(cube_pos_uv_normal);
+  auto res = reader.read(std::move(stream_in), MeshIO::FILETYPE_OBJ);
+  EXPECT_TRUE(res);
+
+  std::ostringstream stream_out;
+  reader.write(stream_out, MeshIO::FILETYPE_OBJ);
+
+  std::string expected =
+      "v 1 1 -1\n"
+      "v 1 -1 -1\n"
+      "v 1 1 1\n"
+      "v 1 -1 1\n"
+      "v -1 1 -1\n"
+      "v -1 -1 -1\n"
+      "v -1 1 1\n"
+      "v -1 -1 1\n"
+      "vt 0.625 0.5\n"
+      "vt 0.875 0.5\n"
+      "vt 0.875 0.75\n"
+      "vt 0.625 0.75\n"
+      "vt 0.375 0.75\n"
+      "vt 0.625 1\n"
+      "vt 0.375 1\n"
+      "vt 0.375 0\n"
+      "vt 0.625 0\n"
+      "vt 0.625 0.25\n"
+      "vt 0.375 0.25\n"
+      "vt 0.125 0.5\n"
+      "vt 0.375 0.5\n"
+      "vt 0.125 0.75\n"
+      "v 0 1 0\n"
+      "v 0 0 1\n"
+      "v -1 0 0\n"
+      "v 0 -1 0\n"
+      "v 1 0 0\n"
+      "v 0 0 -1\n"
+      "f 1/1/1 5/2/1 7/3/1 3/4/1 \n"
+      "f 4/5/2 3/4/2 7/6/2 8/7/2 \n"
+      "f 8/8/3 7/9/3 5/10/3 6/11/3 \n"
+      "f 6/12/4 2/13/4 4/5/4 8/14/4 \n"
+      "f 2/13/5 1/1/5 3/4/5 4/5/5 \n"
+      "f 6/11/6 5/10/6 1/1/6 2/13/6 \n";
+
+  EXPECT_EQ(stream_out.str(), expected);
+}
+
 TEST(cloth_remesh, Mesh_Read)
 {
   MeshIO reader;



More information about the Bf-blender-cvs mailing list