[Bf-blender-cvs] [5e54c8a0d56] soc-2020-io-performance: Add method to free mesh; disambiguate triangulate method.

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


Commit: 5e54c8a0d564bc1fee2952ff7c65c5241ecc2d0a
Author: Ankit Meel
Date:   Wed Sep 16 14:24:07 2020 +0530
Branches: soc-2020-io-performance
https://developer.blender.org/rB5e54c8a0d564bc1fee2952ff7c65c5241ecc2d0a

Add method to free mesh; disambiguate triangulate method.

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

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_mesh.cc b/source/blender/io/wavefront_obj/intern/obj_export_mesh.cc
index 6ab7f165ef8..3dfbaa76688 100644
--- a/source/blender/io/wavefront_obj/intern/obj_export_mesh.cc
+++ b/source/blender/io/wavefront_obj/intern/obj_export_mesh.cc
@@ -31,9 +31,6 @@
 #include "BLI_listbase.h"
 #include "BLI_math.h"
 
-#include "bmesh.h"
-#include "bmesh_tools.h"
-
 #include "DEG_depsgraph_query.h"
 
 #include "DNA_material_types.h"
@@ -57,26 +54,16 @@ OBJMesh::OBJMesh(Depsgraph *depsgraph, const OBJExportParams &export_params, Obj
   mesh_eval_needs_free_ = false;
 
   if (!export_mesh_eval_) {
-    /* Curves and NURBS surfaces need a new mesh when they're exported in the form of vertices and
-     * edges.
+    /* Curves and NURBS surfaces need a new mesh when they're
+     * exported in the form of vertices and edges.
      */
     export_mesh_eval_ = BKE_mesh_new_from_object(depsgraph_, export_object_eval_, true);
     /* Since a new mesh been allocated, it needs to be freed in the destructor. */
     mesh_eval_needs_free_ = true;
   }
-
-  switch (export_object_eval_->type) {
-    case OB_SURF:
-      ATTR_FALLTHROUGH;
-    case OB_MESH: {
-      if (export_params.export_triangulated_mesh) {
-        triangulate_mesh_eval();
-      }
-      break;
-    }
-    default: {
-      break;
-    }
+  if (export_params.export_triangulated_mesh &&
+      ELEM(export_object_eval_->type, OB_MESH, OB_SURF)) {
+    std::tie(export_mesh_eval_, mesh_eval_needs_free_) = triangulate_mesh_eval();
   }
   set_world_axes_transform(export_params.forward_axis, export_params.up_axis);
 }
@@ -86,23 +73,30 @@ OBJMesh::OBJMesh(Depsgraph *depsgraph, const OBJExportParams &export_params, Obj
  */
 OBJMesh::~OBJMesh()
 {
-  if (mesh_eval_needs_free_) {
-    BKE_id_free(NULL, export_mesh_eval_);
-  }
+  free_mesh_if_needed();
   if (poly_smooth_groups_) {
     MEM_freeN(poly_smooth_groups_);
   }
+  BLI_assert(!export_mesh_eval_);
+}
+
+void OBJMesh::free_mesh_if_needed()
+{
+  if (mesh_eval_needs_free_ && export_mesh_eval_) {
+    BKE_id_free(NULL, export_mesh_eval_);
+  }
 }
 
 /**
- * Triangulate and update OBJMesh evaluated mesh.
- * \note The new mesh created here needs to be freed.
+ * Allocate a new Mesh with triangulate polygons.
+ *
+ * The returned mesh can be the same as the old one.
+ * \return Owning pointer to the new Mesh, and whether a new Mesh was created.
  */
-void OBJMesh::triangulate_mesh_eval()
+std::pair<Mesh *, bool> OBJMesh::triangulate_mesh_eval()
 {
   if (export_mesh_eval_->totpoly <= 0) {
-    mesh_eval_needs_free_ = false;
-    return;
+    return {export_mesh_eval_, false};
   }
   const struct BMeshCreateParams bm_create_params = {false};
   /* If `BMeshFromMeshParams.calc_face_normal` is false, it triggers
@@ -112,8 +106,9 @@ void OBJMesh::triangulate_mesh_eval()
    * triangulated here. */
   const int triangulate_min_verts = 4;
 
-  BMesh *bmesh = BKE_mesh_to_bmesh_ex(export_mesh_eval_, &bm_create_params, &bm_convert_params);
-  BM_mesh_triangulate(bmesh,
+  unique_bmesh_ptr bmesh(
+      BKE_mesh_to_bmesh_ex(export_mesh_eval_, &bm_create_params, &bm_convert_params));
+  BM_mesh_triangulate(bmesh.get(),
                       MOD_TRIANGULATE_NGON_BEAUTY,
                       MOD_TRIANGULATE_QUAD_SHORTEDGE,
                       triangulate_min_verts,
@@ -121,9 +116,10 @@ void OBJMesh::triangulate_mesh_eval()
                       NULL,
                       NULL,
                       NULL);
-  export_mesh_eval_ = BKE_mesh_from_bmesh_for_eval_nomain(bmesh, NULL, export_mesh_eval_);
-  mesh_eval_needs_free_ = true;
-  BM_mesh_free(bmesh);
+
+  Mesh *triangulated = BKE_mesh_from_bmesh_for_eval_nomain(bmesh.get(), NULL, export_mesh_eval_);
+  free_mesh_if_needed();
+  return {triangulated, true};
 }
 
 /**
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 a264ce069a3..cbe37178739 100644
--- a/source/blender/io/wavefront_obj/intern/obj_export_mesh.hh
+++ b/source/blender/io/wavefront_obj/intern/obj_export_mesh.hh
@@ -29,6 +29,9 @@
 #include "BLI_utility_mixins.hh"
 #include "BLI_vector.hh"
 
+#include "bmesh.h"
+#include "bmesh_tools.h"
+
 #include "DNA_material_types.h"
 #include "DNA_mesh_types.h"
 #include "DNA_meshdata_types.h"
@@ -36,6 +39,17 @@
 #include "IO_wavefront_obj.h"
 
 namespace blender::io::obj {
+
+struct CustomBMeshDeleter {
+  void operator()(BMesh *bmesh)
+  {
+    if (bmesh) {
+      BM_mesh_free(bmesh);
+    }
+  }
+};
+
+using unique_bmesh_ptr = std::unique_ptr<BMesh, CustomBMeshDeleter>;
 class OBJMesh : NonMovable, NonCopyable {
  private:
   Depsgraph *depsgraph_;
@@ -106,7 +120,8 @@ class OBJMesh : NonMovable, NonCopyable {
   std::optional<std::array<int, 2>> calc_loose_edge_vert_indices(const uint edge_index) const;
 
  private:
-  void triangulate_mesh_eval();
+  void free_mesh_if_needed();
+  std::pair<Mesh *, bool> triangulate_mesh_eval();
   void set_world_axes_transform(const eTransformAxisForward forward, const eTransformAxisUp up);
 };
 }  // namespace blender::io::obj



More information about the Bf-blender-cvs mailing list