[Bf-blender-cvs] [b7689c00838] soc-2020-io-performance: Create IndexOffsets struct instead of array[3].

Ankit Meel noreply at git.blender.org
Wed Sep 16 13:05:53 CEST 2020


Commit: b7689c00838b34a80cd47106bb59268141ede4cb
Author: Ankit Meel
Date:   Wed Sep 16 11:46:29 2020 +0530
Branches: soc-2020-io-performance
https://developer.blender.org/rBb7689c00838b34a80cd47106bb59268141ede4cb

Create IndexOffsets struct instead of array[3].

Make OBJ indices one-based in Writer, not in Object processor.

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

M	source/blender/io/wavefront_obj/intern/obj_export_file_writer.cc
M	source/blender/io/wavefront_obj/intern/obj_export_file_writer.hh
M	source/blender/io/wavefront_obj/intern/obj_export_mesh.cc

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

diff --git a/source/blender/io/wavefront_obj/intern/obj_export_file_writer.cc b/source/blender/io/wavefront_obj/intern/obj_export_file_writer.cc
index 6bbe5e5422c..c3afebd07d0 100644
--- a/source/blender/io/wavefront_obj/intern/obj_export_file_writer.cc
+++ b/source/blender/io/wavefront_obj/intern/obj_export_file_writer.cc
@@ -44,9 +44,9 @@ void OBJWriter::write_vert_uv_normal_indices(Span<uint> vert_indices,
   for (uint j = 0; j < tot_loop; j++) {
     fprintf(outfile_,
             " %u/%u/%u",
-            vert_indices[j] + index_offset_[VERTEX_OFF],
-            uv_indices[j] + index_offset_[UV_VERTEX_OFF],
-            normal_indices[j] + index_offset_[NORMAL_OFF]);
+            vert_indices[j] + index_offsets_.vertex_offset + 1,
+            uv_indices[j] + index_offsets_.uv_vertex_offset + 1,
+            normal_indices[j] + index_offsets_.normal_offset + 1);
   }
   fprintf(outfile_, "\n");
 }
@@ -63,8 +63,8 @@ void OBJWriter::write_vert_normal_indices(Span<uint> vert_indices,
   for (uint j = 0; j < tot_loop; j++) {
     fprintf(outfile_,
             " %u//%u",
-            vert_indices[j] + index_offset_[VERTEX_OFF],
-            normal_indices[j] + index_offset_[NORMAL_OFF]);
+            vert_indices[j] + index_offsets_.vertex_offset + 1,
+            normal_indices[j] + index_offsets_.normal_offset + 1);
   }
   fprintf(outfile_, "\n");
 }
@@ -81,8 +81,8 @@ void OBJWriter::write_vert_uv_indices(Span<uint> vert_indices,
   for (uint j = 0; j < tot_loop; j++) {
     fprintf(outfile_,
             " %u/%u",
-            vert_indices[j] + index_offset_[VERTEX_OFF],
-            uv_indices[j] + index_offset_[UV_VERTEX_OFF]);
+            vert_indices[j] + index_offsets_.vertex_offset + 1,
+            uv_indices[j] + index_offsets_.uv_vertex_offset + 1);
   }
   fprintf(outfile_, "\n");
 }
@@ -97,7 +97,7 @@ void OBJWriter::write_vert_indices(Span<uint> vert_indices,
 {
   fprintf(outfile_, "f");
   for (uint j = 0; j < tot_loop; j++) {
-    fprintf(outfile_, " %u", vert_indices[j] + index_offset_[VERTEX_OFF]);
+    fprintf(outfile_, " %u", vert_indices[j] + index_offsets_.vertex_offset + 1);
   }
   fprintf(outfile_, "\n");
 }
@@ -371,12 +371,13 @@ void OBJWriter::write_loose_edges(const OBJMesh &obj_mesh_data) const
   for (uint edge_index = 0; edge_index < obj_mesh_data.tot_edges(); edge_index++) {
     std::optional<std::array<int, 2>> vertex_indices = obj_mesh_data.calc_edge_vert_indices(
         edge_index);
-    if (vertex_indices) {
-      fprintf(outfile_,
-              "l %u %u\n",
-              (*vertex_indices)[0] + index_offset_[VERTEX_OFF],
-              (*vertex_indices)[1] + index_offset_[VERTEX_OFF]);
+    if (!vertex_indices) {
+      continue;
     }
+    fprintf(outfile_,
+            "l %u %u\n",
+            (*vertex_indices)[0] + index_offsets_.vertex_offset + 1,
+            (*vertex_indices)[1] + index_offsets_.vertex_offset + 1);
   }
 }
 
@@ -426,15 +427,16 @@ void OBJWriter::write_nurbs_curve(const OBJCurve &obj_nurbs_data) const
 }
 
 /**
- *  When there are multiple objects in a frame, the indices of previous objects' coordinates or
+ * When there are multiple objects in a frame, the indices of previous objects' coordinates or
  * normals add up.
+ * Make sure to call this after an Object is exported.
  */
 void OBJWriter::update_index_offsets(const OBJMesh &obj_mesh_data)
 {
-  index_offset_[VERTEX_OFF] += obj_mesh_data.tot_vertices();
-  index_offset_[UV_VERTEX_OFF] += obj_mesh_data.tot_uv_vertices();
-  index_offset_[NORMAL_OFF] = tot_normals_;
-  tot_normals_ = 0;
+  index_offsets_.vertex_offset += obj_mesh_data.tot_vertices();
+  index_offsets_.uv_vertex_offset += obj_mesh_data.tot_uv_vertices();
+  index_offsets_.normal_offset += per_object_tot_normals_;
+  per_object_tot_normals_ = 0;
 }
 
 /* -------------------------------------------------------------------- */
diff --git a/source/blender/io/wavefront_obj/intern/obj_export_file_writer.hh b/source/blender/io/wavefront_obj/intern/obj_export_file_writer.hh
index 4cb1a48ad73..18c366fdc0d 100644
--- a/source/blender/io/wavefront_obj/intern/obj_export_file_writer.hh
+++ b/source/blender/io/wavefront_obj/intern/obj_export_file_writer.hh
@@ -35,11 +35,14 @@ namespace blender::io::obj {
 class OBJMesh;
 class OBJCurve;
 
-/* Types of index offsets. */
-enum eIndexOffsets {
-  VERTEX_OFF = 0,
-  UV_VERTEX_OFF = 1,
-  NORMAL_OFF = 2,
+/**
+ * For an Object, total vertices/ UV vertices/ Normals written by previous objects
+ * are added to its indices.
+ */
+struct IndexOffsets {
+  uint vertex_offset;
+  uint uv_vertex_offset;
+  uint normal_offset;
 };
 
 class OBJWriter {
@@ -49,15 +52,15 @@ class OBJWriter {
    */
   FILE *outfile_;
   const OBJExportParams &export_params_;
+
+  IndexOffsets index_offsets_{0, 0, 0};
   /**
-   * Vertex offset, UV vertex offset, face/loop normal offset respetively.
-   */
-  uint index_offset_[3] = {0, 0, 0};
-  /**
-   * Keeps track of number of normals which depend of smooth shaded faces.
-   * Update index offset for normals using this.
+   * Total normals of an Object. It is not that same as `Mesh.tot_poly` due
+   * to unknown smooth groups which add loop normals for smooth faces.
+   *
+   * Used for updating normal offset.
    */
-  int tot_normals_ = 0;
+  int per_object_tot_normals_ = 0;
 
  public:
   OBJWriter(const OBJExportParams &export_params) : export_params_(export_params)
diff --git a/source/blender/io/wavefront_obj/intern/obj_export_mesh.cc b/source/blender/io/wavefront_obj/intern/obj_export_mesh.cc
index d5a66afa21e..79b10ce1124 100644
--- a/source/blender/io/wavefront_obj/intern/obj_export_mesh.cc
+++ b/source/blender/io/wavefront_obj/intern/obj_export_mesh.cc
@@ -308,7 +308,7 @@ void OBJMesh::calc_poly_vertex_indices(const uint poly_index,
   const MPoly &mpoly = export_mesh_eval_->mpoly[poly_index];
   const MLoop *mloop = &export_mesh_eval_->mloop[mpoly.loopstart];
   for (uint loop_index = 0; loop_index < mpoly.totloop; loop_index++) {
-    r_poly_vertex_indices[loop_index] = mloop[loop_index].v + 1;
+    r_poly_vertex_indices[loop_index] = mloop[loop_index].v;
   }
 }
 
@@ -358,7 +358,8 @@ void OBJMesh::store_uv_coords_and_indices(Vector<std::array<float, 2>> &r_uv_coo
       r_uv_coords[tot_uv_vertices_ - 1][1] = vert_uv_coords[1];
 
       uv_indices_[uv_vert->poly_index].resize(vertices_in_poly);
-      uv_indices_[uv_vert->poly_index][uv_vert->loop_of_poly_index] = tot_uv_vertices_;
+      /* Keep indices zero-based and let the writer handle the + 1. */
+      uv_indices_[uv_vert->poly_index][uv_vert->loop_of_poly_index] = tot_uv_vertices_ - 1;
     }
   }
   BKE_mesh_uv_vert_map_free(uv_vert_map);
@@ -476,7 +477,7 @@ std::optional<std::array<int, 2>> OBJMesh::calc_edge_vert_indices(const uint edg
 {
   const MEdge &edge = export_mesh_eval_->medge[edge_index];
   if (edge.flag & ME_LOOSEEDGE) {
-    return std::array<int, 2>{static_cast<int>(edge.v1 + 1), static_cast<int>(edge.v2 + 1)};
+    return std::array<int, 2>{static_cast<int>(edge.v1), static_cast<int>(edge.v2)};
   }
   return {};
 }



More information about the Bf-blender-cvs mailing list