[Bf-blender-cvs] [9ea4a50fee8] soc-2020-io-performance: Simplify vertex group calculation.

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


Commit: 9ea4a50fee8a3956ec4803bd3797d732d360eca2
Author: Ankit Meel
Date:   Wed Sep 16 15:13:53 2020 +0530
Branches: soc-2020-io-performance
https://developer.blender.org/rB9ea4a50fee8a3956ec4803bd3797d732d360eca2

Simplify vertex group calculation.

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

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

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

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 bec8c81ade5..8de62fa5e22 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
@@ -259,25 +259,28 @@ void OBJWriter::write_poly_material(const OBJMesh &obj_mesh_data,
 }
 
 /**
- * Write the name of the deform group of a face. If no vertex group is found in the face, "off" is
- * written.
+ * Write the name of the deform group of a polygon. If no vertex group is found in
+ * the polygon, "off" is written.
  */
 void OBJWriter::write_vertex_group(const OBJMesh &obj_mesh_data,
                                    const uint poly_index,
-                                   short &last_face_vertex_group) const
+                                   short &r_last_poly_vertex_group) const
 {
   if (!export_params_.export_vertex_groups) {
     return;
   }
+  const short current_group = obj_mesh_data.get_poly_deform_group_index(poly_index);
 
-  const char *def_group_name = obj_mesh_data.get_poly_deform_group_name(poly_index,
-                                                                        last_face_vertex_group);
-  if (!def_group_name) {
-    /* Don't write the name of the group again. If set once, the group name changes only when a new
-     * one is encountered. */
+  if (current_group == r_last_poly_vertex_group) {
+    /* No vertex group found in this face, just like in the last iteration. */
     return;
   }
-  fprintf(outfile_, "g %s\n", def_group_name);
+  r_last_poly_vertex_group = current_group;
+  if (current_group == NOT_FOUND) {
+    fprintf(outfile_, "g off\n");
+    return;
+  }
+  fprintf(outfile_, "g %s\n", obj_mesh_data.get_poly_deform_group_name(current_group));
 }
 
 /**
@@ -286,9 +289,6 @@ void OBJWriter::write_vertex_group(const OBJMesh &obj_mesh_data,
 OBJWriter::func_vert_uv_normal_indices OBJWriter::get_poly_element_writer(
     const OBJMesh &obj_mesh_data)
 {
-  /* -1 is used to denote face having no vertex group. It can be any _other_ negative
-   * number. */
-  short last_face_vertex_group = -2;
   /* -1 has no significant value, it can be any negative number. */
   short last_face_mat_nr = -1;
 
@@ -316,6 +316,7 @@ OBJWriter::func_vert_uv_normal_indices OBJWriter::get_poly_element_writer(
 void OBJWriter::write_poly_elements(const OBJMesh &obj_mesh_data)
 {
   int last_face_smooth_group = NEGATIVE_INIT;
+  short last_face_vertex_group = NEGATIVE_INIT;
 
   Vector<uint> vertex_indices;
     const int totloop = obj_mesh_data.ith_poly_totloop(i);
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 2aa0609b976..69475dcfb32 100644
--- a/source/blender/io/wavefront_obj/intern/obj_export_mesh.cc
+++ b/source/blender/io/wavefront_obj/intern/obj_export_mesh.cc
@@ -396,7 +396,6 @@ void OBJMesh::calc_loop_normals(const uint poly_index, Vector<float3> &r_loop_no
 }
 
 /**
- * Find the name of the vertex group with the maximum number of vertices in a poly.
  * Calculate a polygon's face/loop normal indices.
  * \param Number of normals of this Object written so far.
  * \return Number of distinct normal indices.
@@ -426,18 +425,15 @@ int OBJMesh::calc_poly_normal_indices(const uint poly_index,
     return 1;
   }
 }
+
+/**
+ * Find the index of the vertex group with the maximum number of vertices in a poly.
+ * The index indices into the `Object.defbase`.
  *
- * If no vertex belongs to any group, returned name is "off".
  * If two or more groups have the same number of vertices (maximum), group name depends on the
  * implementation of std::max_element.
- * If the group corresponding to r_last_vertex_group shows up on current polygon, return nullptr so
- * that caller can skip that group.
- *
- * \param r_last_vertex_group stores the index of the vertex group found in last iteration,
- * indexing into Object->defbase.
  */
-const char *OBJMesh::get_poly_deform_group_name(const uint poly_index,
-                                                short &r_last_vertex_group) const
+short OBJMesh::get_poly_deform_group_index(const uint poly_index) const
 {
   BLI_assert(poly_index < export_mesh_eval_->totpoly);
   const MPoly &mpoly = export_mesh_eval_->mpoly[poly_index];
@@ -445,14 +441,14 @@ const char *OBJMesh::get_poly_deform_group_name(const uint poly_index,
   const uint tot_deform_groups = BLI_listbase_count(&export_object_eval_->defbase);
   /* Indices of the vector index into deform groups of an object; values are the number of vertex
    * members in one deform group. */
-  Vector<int> deform_group_members(tot_deform_groups, 0);
+  Vector<short> deform_group_members(tot_deform_groups, 0);
   /* Whether at least one vertex in the polygon belongs to any group. */
   bool found_group = false;
 
   const MDeformVert *dvert_orig = static_cast<MDeformVert *>(
       CustomData_get_layer(&export_mesh_eval_->vdata, CD_MDEFORMVERT));
   if (!dvert_orig) {
-    return nullptr;
+    return NOT_FOUND;
   }
 
   const MDeformWeight *curr_weight = nullptr;
@@ -471,28 +467,22 @@ const char *OBJMesh::get_poly_deform_group_name(const uint poly_index,
   }
 
   if (!found_group) {
-    if (r_last_vertex_group == -1) {
-      /* No vertex group found in this face, just like in the last iteration. */
-      return nullptr;
-    }
-    /* -1 indicates deform group having no vertices in it. */
-    r_last_vertex_group = -1;
-    return "off";
+    return NOT_FOUND;
   }
-
   /* Index of the group with maximum vertices. */
-  short max_idx = (short)(std::max_element(deform_group_members.begin(),
-                                           deform_group_members.end()) -
-                          deform_group_members.begin());
-  if (max_idx == r_last_vertex_group) {
-    /* No need to update the name, this is the same as the group name in the last iteration. */
-    return nullptr;
-  }
+  short max_idx = std::max_element(deform_group_members.begin(), deform_group_members.end()) -
+                  deform_group_members.begin();
+  return max_idx;
+}
 
-  r_last_vertex_group = max_idx;
+/**
+ * Find the name of the vertex deform group at the given index.
+ * The index indices into the `Object.defbase`.
+ */
+const char *OBJMesh::get_poly_deform_group_name(const short def_group_index) const
+{
   const bDeformGroup &vertex_group = *(
-      static_cast<bDeformGroup *>(BLI_findlink(&export_object_eval_->defbase, max_idx)));
-
+      static_cast<bDeformGroup *>(BLI_findlink(&export_object_eval_->defbase, def_group_index)));
   return vertex_group.name;
 }
 
diff --git a/source/blender/io/wavefront_obj/intern/obj_export_mesh.hh b/source/blender/io/wavefront_obj/intern/obj_export_mesh.hh
index 7a217d82761..a4da543965b 100644
--- a/source/blender/io/wavefront_obj/intern/obj_export_mesh.hh
+++ b/source/blender/io/wavefront_obj/intern/obj_export_mesh.hh
@@ -117,11 +117,12 @@ class OBJMesh : NonMovable, NonCopyable {
   void calc_poly_vertex_indices(const uint poly_index, Vector<uint> &r_poly_vertex_indices) const;
   void store_uv_coords_and_indices(Vector<std::array<float, 2>> &r_uv_coords);
   float3 calc_poly_normal(const uint poly_index) const;
-  const char *get_poly_deform_group_name(const uint poly_index, short &r_last_vertex_group) const;
   int calc_poly_normal_indices(const uint poly_index,
                                const int object_tot_prev_normals,
                                Vector<uint> &r_face_normal_indices) const;
   void calc_loop_normals(const uint poly_index, Vector<float3> &r_loop_normals) const;
+  short get_poly_deform_group_index(const uint poly_index) const;
+  const char *get_poly_deform_group_name(const short def_group_index) const;
 
   std::optional<std::array<int, 2>> calc_loose_edge_vert_indices(const uint edge_index) const;



More information about the Bf-blender-cvs mailing list