[Bf-blender-cvs] [0f383a3d7c9] soc-2020-io-performance: Review update: move more things to private access

Ankit Meel noreply at git.blender.org
Thu Jun 25 14:25:51 CEST 2020


Commit: 0f383a3d7c9ab2b40dd593efd8ef8a942dd02cf6
Author: Ankit Meel
Date:   Wed Jun 24 00:28:26 2020 +0530
Branches: soc-2020-io-performance
https://developer.blender.org/rB0f383a3d7c9ab2b40dd593efd8ef8a942dd02cf6

Review update: move more things to private access

Review as per D8089:

Add constructors and destructors for both OBJWriter and OBJMesh classes

Move more variables to private and access the required via getters.

Remove unnecessary variables.

Rename ob_mesh to export_mesh_data.

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

M	source/blender/io/wavefront_obj/intern/wavefront_obj.hh
M	source/blender/io/wavefront_obj/intern/wavefront_obj_exporter.cc
M	source/blender/io/wavefront_obj/intern/wavefront_obj_file_handler.cc
M	source/blender/io/wavefront_obj/intern/wavefront_obj_file_handler.hh

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

diff --git a/source/blender/io/wavefront_obj/intern/wavefront_obj.hh b/source/blender/io/wavefront_obj/intern/wavefront_obj.hh
index d494ffb7108..25a6afd9a0a 100644
--- a/source/blender/io/wavefront_obj/intern/wavefront_obj.hh
+++ b/source/blender/io/wavefront_obj/intern/wavefront_obj.hh
@@ -22,10 +22,10 @@
 #define __WAVEFRONT_OBJ_HH__
 
 #include "BKE_context.h"
+#include "BKE_lib_id.h"
 #include "BKE_mesh.h"
 #include "BKE_object.h"
 
-#include "BLI_array.hh"
 #include "BLI_vector.hh"
 
 #include "DNA_mesh_types.h"
@@ -44,37 +44,51 @@ namespace obj {
 
 class OBJMesh {
  public:
-  const OBJExportParams *export_params;
-  bContext *C;
+  OBJMesh(bContext *C, const OBJExportParams *export_params, Object *export_object)
+      : _C(C), _export_params(export_params), _export_object_eval(export_object)
+  {
+    init_export_mesh(export_object);
+  }
 
-  Object *object;
-  Mesh *me_eval;
-  /** For curves and triangulated meshes, a new mesh is allocated which needs to be freed later. */
-  bool me_eval_needs_free = false;
+  /** Free new meshes we allocate for triangulated meshes, and curves converted to meshes. */
+  void destruct()
+  {
+    if (_me_eval_needs_free) {
+      BKE_id_free(NULL, _export_mesh_eval);
+    }
+  }
 
-  /** Total vertices in a mesh. */
-  uint tot_vertices;
-  /** Total polygons (and thus normals) in a mesh. */
-  uint tot_poly_normals;
-  /** Total UV vertices in a mesh's texture map. */
-  uint tot_uv_vertices;
-  /** Only for curve-like meshes: total edges in a mesh. */
-  uint tot_edges;
-  /** Final transform of an object obtained from export settings (up_axis, forward_axis) and world
-   * transform matrix.
-   */
-  float world_and_axes_transform[4][4];
+  uint tot_vertices()
+  {
+    return _tot_vertices;
+  }
 
-  /**
-   * Store evaluated object and mesh pointers depending on object type.
-   * New meshes are created for curves and triangulated meshes.
-   */
-  void get_mesh_eval();
-  /**
-   * Store the product of export axes settings and an object's world transform matrix in
-   * world_and_axes_transform[4][4].
-   */
-  void store_world_axes_transform();
+  uint tot_poly_normals()
+  {
+    return _tot_poly_normals;
+  }
+
+  uint tot_uv_vertices()
+  {
+    return _tot_uv_vertices;
+  }
+
+  uint tot_edges()
+  {
+    return _tot_edges;
+  }
+
+  Mesh *export_mesh_eval()
+  {
+    return _export_mesh_eval;
+  }
+
+  const MPoly &get_ith_poly(uint i)
+  {
+    return _export_mesh_eval->mpoly[i];
+  }
+
+  void get_object_name(const char **object_name);
   /**
    * Calculate coordinates of the vertex at given index.
    */
@@ -97,9 +111,50 @@ class OBJMesh {
    */
   void calc_poly_normal_indices(blender::Vector<uint> &normal_indices, uint poly_indices);
   /**
-   * Only for curve-like meshes: calculate vertex indices of one edge.
+   * Only for curve converted to meshes: calculate vertex indices of one edge.
    */
-  void calc_edge_vert_indices(blender::Array<uint, 2> &vert_indices, uint edge_index);
+  void calc_edge_vert_indices(uint vert_indices[2], uint edge_index);
+
+ private:
+  bContext *_C;
+  const OBJExportParams *_export_params;
+
+  Object *_export_object_eval;
+  Mesh *_export_mesh_eval;
+  /**
+   * Store evaluated object and mesh pointers depending on object type.
+   * New meshes are created for curves converted to meshes and triangulated meshes.
+   */
+  void init_export_mesh(Object *export_object);
+  /**
+   * Triangulate given mesh and update _export_mesh_eval.
+   * \note The new mesh created here needs to be freed.
+   */
+  void triangulate_mesh(Mesh *me_eval);
+  /**
+   * For curves which are converted to mesh, and triangulated meshes, a new mesh is allocated
+   * which needs to be freed later.
+   */
+  bool _me_eval_needs_free = false;
+
+  /**
+   * Store the product of export axes settings and an object's world transform matrix in
+   * world_and_axes_transform[4][4].
+   */
+  void store_world_axes_transform();
+  /** Final transform of an object obtained from export settings (up_axis, forward_axis) and world
+   * transform matrix.
+   */
+  float _world_and_axes_transform[4][4];
+
+  /** Total vertices in a mesh. */
+  uint _tot_vertices;
+  /** Total polygons (and thus normals) in a mesh. */
+  uint _tot_poly_normals;
+  /** Total UV vertices in a mesh's texture map. */
+  uint _tot_uv_vertices;
+  /** Only for curve converted to meshes: total edges in a mesh. */
+  uint _tot_edges;
 };
 
 class OBJNurbs {
diff --git a/source/blender/io/wavefront_obj/intern/wavefront_obj_exporter.cc b/source/blender/io/wavefront_obj/intern/wavefront_obj_exporter.cc
index 1987d51a515..e781ccba642 100644
--- a/source/blender/io/wavefront_obj/intern/wavefront_obj_exporter.cc
+++ b/source/blender/io/wavefront_obj/intern/wavefront_obj_exporter.cc
@@ -26,6 +26,7 @@
 #include <stdio.h>
 
 #include "BKE_curve.h"
+#include "BKE_customdata.h"
 #include "BKE_lib_id.h"
 #include "BKE_mesh.h"
 #include "BKE_mesh_mapping.h"
@@ -58,6 +59,65 @@
 namespace io {
 namespace obj {
 
+/**
+ * Store evaluated object and mesh pointers depending on object type.
+ * New meshes are created for curves converted to meshes and triangulated meshes.
+ */
+void OBJMesh::init_export_mesh(Object *export_object)
+{
+  Depsgraph *depsgraph = CTX_data_ensure_evaluated_depsgraph(_C);
+  _export_object_eval = DEG_get_evaluated_object(depsgraph, export_object);
+  _export_mesh_eval = BKE_object_get_evaluated_mesh(_export_object_eval);
+  _me_eval_needs_free = false;
+
+  if (_export_mesh_eval && _export_mesh_eval->totpoly > 0) {
+    if (_export_params->export_triangulated_mesh) {
+      triangulate_mesh(_export_mesh_eval);
+      _me_eval_needs_free = true;
+    }
+    _tot_vertices = _export_mesh_eval->totvert;
+    _tot_poly_normals = _export_mesh_eval->totpoly;
+    store_world_axes_transform();
+  }
+
+  /* Curves need a new mesh when exported in the form of vertices and edges.
+   * For primitive circle, new mesh is redundant, but it behaves more like curves, so kept it here.
+   */
+  else if (_export_object_eval->type == OB_CURVE && !_export_params->export_curves_as_nurbs) {
+    _export_mesh_eval = BKE_mesh_new_from_object(depsgraph, _export_object_eval, true);
+    _me_eval_needs_free = true;
+    _tot_vertices = _export_mesh_eval->totvert;
+    _tot_edges = _export_mesh_eval->totedge;
+    store_world_axes_transform();
+  }
+}
+
+/**
+ * Triangulate given mesh and update _export_mesh_eval.
+ * \note The new mesh created here needs to be freed.
+ */
+void OBJMesh::triangulate_mesh(Mesh *me_eval)
+{
+  struct BMeshCreateParams bm_create_params = {false};
+  /* If calc_face_normal is false, it triggers BLI_assert(BM_face_is_normal_valid(f)). */
+  struct BMeshFromMeshParams bm_convert_params = {true, 0, 0, 0};
+  /* Lower threshold where triangulation of a face starts, i.e. a quadrilateral will be
+   * triangulated here. */
+  int triangulate_min_verts = 4;
+
+  BMesh *bmesh = BKE_mesh_to_bmesh_ex(me_eval, &bm_create_params, &bm_convert_params);
+  BM_mesh_triangulate(bmesh,
+                      MOD_TRIANGULATE_NGON_BEAUTY,
+                      MOD_TRIANGULATE_QUAD_SHORTEDGE,
+                      triangulate_min_verts,
+                      false,
+                      NULL,
+                      NULL,
+                      NULL);
+  _export_mesh_eval = BKE_mesh_from_bmesh_for_eval_nomain(bmesh, NULL, me_eval);
+  BM_mesh_free(bmesh);
+}
+
 /**
  * Store the product of export axes settings and an object's world transform matrix in
  * world_and_axes_transform[4][4].
@@ -68,200 +128,132 @@ void OBJMesh::store_world_axes_transform()
   unit_m3(axes_transform);
   mat3_from_axis_conversion(DEFAULT_AXIS_FORWARD,
                             DEFAULT_AXIS_UP,
-                            export_params->forward_axis,
-                            export_params->up_axis,
+                            _export_params->forward_axis,
+                            _export_params->up_axis,
                             axes_transform);
-  mul_m4_m3m4(world_and_axes_transform, axes_transform, object->obmat);
+  mul_m4_m3m4(_world_and_axes_transform, axes_transform, _export_object_eval->obmat);
   /* mul_m4_m3m4 does not copy last row of obmat, i.e. location data. */
-  copy_v4_v4(world_and_axes_transform[3], object->obmat[3]);
+  copy_v4_v4(_world_and_axes_transform[3], _export_object_eval->obmat[3]);
+}
+
+void OBJMesh::get_object_name(const char **r_object_name)
+{
+  *r_object_name = _export_object_eval->id.name + 2;
 }
 
 /**
  * Calculate coordinates of the vertex at given index.
  */
-void OBJMesh::calc_vertex_coords(float coords[3], uint vert_index)
+void OBJMesh::calc_vertex_coords(float r_coords[3], uint point_index)
 {
-  copy_v3_v3(coords, me_eval->mvert[vert_index].co);
-  mul_m4_v3(world_and_axes_transform, coords);
-  mul_v3_fl(coords, export_params->scaling_factor);
+  copy_v3_v3(r_coords, _export_mesh_eval->mvert[point_index].co);
+  mul_m4_v3(_world_and_axes_transform, r_coords);
+  mul_v3_fl(r_coords, _export_params->scaling_factor);
 }
 
 /**
  * Calculate vertex indices of all vertices of a polygon.
  */
-void OBJMesh::calc_poly_vertex_indices(blender::Vector<uint> &poly_vertex_indices, uint poly_index)
+void OBJMesh::calc_poly_vertex_indices(blender::Vector<uint> &r_poly_vertex_indices,
+                                       uint poly_index)
 {
-  const MPoly &mpoly = me_eval->mpoly[poly_index];
-  const MLoop *mloop = &me_eval->mloop[mpoly.loopstart];
-  poly_vertex_indices.resize(mpoly.totloop);
+  const MPoly &mpoly = _export_mesh_eval->mpoly[poly_index];
+  const MLoop *mloop = &_export_mesh_eval->mloop[mpoly.loopstart];
+  r_poly_vertex_indices.resize(mpoly.totloop);
   for (uint loop_index = 0; loop_index < mpoly.totloop; loop_index++) {
-    poly_vertex_indices[loop_index] = (mloop + loop_index)->v + 1;
+    r_poly_vertex_indices[loop_index] = (mloop + loop_index)->v + 1;
   }
 }
 
 /**
  * Store UV vertex coordinates as well as their indices.
  */
-void OBJMesh::store_uv_coords_and_indices(blender::Vector<std::array<float, 2>> &uv_coords,
-                                          blender::Vector<blender::Vector<uint>> &uv_indices)
+void OBJMesh::store_uv_coords_and_indices(blender::Vecto

@@ Diff output truncated at 10240 characters. @@



More information about the Bf-blender-cvs mailing list