[Bf-blender-cvs] [f4c70d7c9fe] soc-2020-io-performance: Review update: use float3, std::optional, c_str()

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


Commit: f4c70d7c9fee1cc7e8f42487e81d85bbdf290053
Author: Ankit Meel
Date:   Tue Sep 1 01:41:34 2020 +0530
Branches: soc-2020-io-performance
https://developer.blender.org/rBf4c70d7c9fee1cc7e8f42487e81d85bbdf290053

Review update: use float3, std::optional, c_str()

split nurbs function into two.

Add material name append if material groups are specified.

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

M	source/blender/editors/io/io_obj.c
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
M	source/blender/io/wavefront_obj/intern/obj_export_mtl.cc
M	source/blender/io/wavefront_obj/intern/obj_export_nurbs.cc
M	source/blender/io/wavefront_obj/intern/obj_export_nurbs.hh

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

diff --git a/source/blender/editors/io/io_obj.c b/source/blender/editors/io/io_obj.c
index 693746ca6c6..e7b020c6f6c 100644
--- a/source/blender/editors/io/io_obj.c
+++ b/source/blender/editors/io/io_obj.c
@@ -323,7 +323,7 @@ void WM_OT_obj_export(struct wmOperatorType *ot)
                   true,
                   "Export Materials",
                   "Export MTL library. There must be a Principled-BSDF node for image textures to "
-                  "be exported to the MTL file.");
+                  "be exported to the MTL file");
   RNA_def_boolean(ot->srna,
                   "export_triangulated_mesh",
                   false,
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 795034edcd6..23c00fc6cc2 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
@@ -143,15 +143,19 @@ void OBJWriter::write_mtllib(const char *obj_filepath) const
 }
 
 /**
- * Write object name as it appears in the outliner.
+ * Write object name conditionally with mesh and material name.
  */
 void OBJWriter::write_object_name(const OBJMesh &obj_mesh_data) const
 {
   const char *object_name = obj_mesh_data.get_object_name();
 
   if (export_params_.export_object_groups) {
-    const char *object_data_name = obj_mesh_data.get_object_data_name();
-    fprintf(outfile_, "g %s_%s\n", object_name, object_data_name);
+    const char *object_mesh_name = obj_mesh_data.get_object_mesh_name();
+    if (export_params_.export_materials && export_params_.export_material_groups) {
+      const char *object_material_name = obj_mesh_data.get_object_material_name(0);
+      fprintf(outfile_, "g %s_%s_%s\n", object_name, object_mesh_name, object_material_name);
+    }
+    fprintf(outfile_, "g %s_%s\n", object_name, object_mesh_name);
   }
   else {
     fprintf(outfile_, "o %s\n", object_name);
@@ -163,9 +167,8 @@ void OBJWriter::write_object_name(const OBJMesh &obj_mesh_data) const
  */
 void OBJWriter::write_vertex_coords(const OBJMesh &obj_mesh_data) const
 {
-  float vertex[3];
   for (uint i = 0; i < obj_mesh_data.tot_vertices(); i++) {
-    obj_mesh_data.calc_vertex_coords(i, vertex);
+    float3 vertex = obj_mesh_data.calc_vertex_coords(i);
     fprintf(outfile_, "v %f %f %f\n", vertex[0], vertex[1], vertex[2]);
   }
 }
@@ -256,7 +259,7 @@ void OBJWriter::write_poly_material(const OBJMesh &obj_mesh_data,
     const char *mat_name = obj_mesh_data.get_object_material_name(mat_nr + 1);
     if (export_params_.export_material_groups) {
       const char *object_name = obj_mesh_data.get_object_name();
-      const char *object_data_name = obj_mesh_data.get_object_data_name();
+      const char *object_data_name = obj_mesh_data.get_object_mesh_name();
       fprintf(outfile_, "g %s_%s_%s\n", object_name, object_data_name, mat_name);
     }
     fprintf(outfile_, "usemtl %s\n", mat_name);
@@ -348,15 +351,15 @@ void OBJWriter::write_poly_elements(const OBJMesh &obj_mesh_data,
  */
 void OBJWriter::write_loose_edges(const OBJMesh &obj_mesh_data) const
 {
-  Array<int, 2> vertex_indices;
   obj_mesh_data.ensure_mesh_edges();
   for (uint edge_index = 0; edge_index < obj_mesh_data.tot_edges(); edge_index++) {
-    vertex_indices = obj_mesh_data.calc_edge_vert_indices(edge_index);
-    if (vertex_indices.size() == 2) {
+    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]);
+              (*vertex_indices)[0] + index_offset_[VERTEX_OFF],
+              (*vertex_indices)[1] + index_offset_[VERTEX_OFF]);
     }
   }
 }
@@ -370,17 +373,15 @@ void OBJWriter::write_nurbs_curve(const OBJNurbs &obj_nurbs_data) const
   LISTBASE_FOREACH (const Nurb *, nurb, nurbs) {
     /* Total control points in a nurbs. */
     int tot_points = nurb->pntsv * nurb->pntsu;
-    float point_coord[3];
     for (int point_idx = 0; point_idx < tot_points; point_idx++) {
-      obj_nurbs_data.calc_point_coords(nurb, point_idx, point_coord);
+      float3 point_coord = obj_nurbs_data.calc_point_coords(nurb, point_idx);
       fprintf(outfile_, "v %f %f %f\n", point_coord[0], point_coord[1], point_coord[2]);
     }
 
     const char *nurbs_name = obj_nurbs_data.get_curve_name();
-    int nurbs_degree = 0;
+    int nurbs_degree = obj_nurbs_data.get_curve_degree(nurb);
     /* Number of vertices in the curve + degree of the curve if it is cyclic. */
-    int curv_num = 0;
-    obj_nurbs_data.get_curve_info(nurb, nurbs_degree, curv_num);
+    int curv_num = obj_nurbs_data.get_curve_num(nurb);
 
     fprintf(outfile_, "g %s\ncstype bspline\ndeg %d\n", nurbs_name, nurbs_degree);
     /**
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 82dea0a4efb..ae97612810a 100644
--- a/source/blender/io/wavefront_obj/intern/obj_export_mesh.cc
+++ b/source/blender/io/wavefront_obj/intern/obj_export_mesh.cc
@@ -245,7 +245,7 @@ const char *OBJMesh::get_object_name() const
 /**
  * Get object's mesh name.
  */
-const char *OBJMesh::get_object_data_name() const
+const char *OBJMesh::get_object_mesh_name() const
 {
   return export_mesh_eval_->id.name + 2;
 }
@@ -256,17 +256,22 @@ const char *OBJMesh::get_object_data_name() const
 const char *OBJMesh::get_object_material_name(short mat_nr) const
 {
   const Material *mat = BKE_object_material_get(export_object_eval_, mat_nr);
-  return mat->id.name + 2;
+#ifdef DEBUG
+  std::cerr << "Material not found for mat_nr" << mat_nr << std::endl;
+#endif
+  return mat ? mat->id.name + 2 : nullptr;
 }
 
 /**
  * Calculate coordinates of a vertex at the given index.
  */
-void OBJMesh::calc_vertex_coords(const uint vert_index, float r_coords[3]) const
+float3 OBJMesh::calc_vertex_coords(const uint vert_index) const
 {
+  float3 r_coords;
   copy_v3_v3(r_coords, export_mesh_eval_->mvert[vert_index].co);
   mul_m4_v3(world_and_axes_transform_, r_coords);
   mul_v3_fl(r_coords, export_params_.scaling_factor);
+  return r_coords;
 }
 
 /**
@@ -398,11 +403,10 @@ const char *OBJMesh::get_poly_deform_group_name(const MPoly &mpoly,
                                                 short &r_last_vertex_group) const
 {
   const MLoop *mloop = &export_mesh_eval_->mloop[mpoly.loopstart];
+  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{};
-  const uint tot_deform_groups = BLI_listbase_count(&export_object_eval_->defbase);
-  deform_group_members.resize(tot_deform_groups, 0);
+  Vector<int> deform_group_members(tot_deform_groups, 0);
   /* Whether at least one vertex in the polygon belongs to any group. */
   bool found_group = false;
 
@@ -456,11 +460,11 @@ const char *OBJMesh::get_poly_deform_group_name(const MPoly &mpoly,
 /**
  * Only for curve converted to meshes and primitive circle: calculate vertex indices of one edge.
  */
-Array<int, 2> OBJMesh::calc_edge_vert_indices(const uint edge_index) const
+std::optional<std::array<int, 2>> OBJMesh::calc_edge_vert_indices(const uint edge_index) const
 {
   const MEdge &edge = export_mesh_eval_->medge[edge_index];
   if (edge.flag & ME_LOOSEEDGE) {
-    return {edge.v1 + 1, edge.v2 + 1};
+    return std::array<int, 2>{static_cast<int>(edge.v1 + 1), static_cast<int>(edge.v2 + 1)};
   }
   return {};
 }
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 18b901a729c..6568f8b596a 100644
--- a/source/blender/io/wavefront_obj/intern/obj_export_mesh.hh
+++ b/source/blender/io/wavefront_obj/intern/obj_export_mesh.hh
@@ -23,7 +23,10 @@
 
 #pragma once
 
+#include <optional>
+
 #include "BLI_array.hh"
+#include "BLI_float3.hh"
 #include "BLI_utility_mixins.hh"
 #include "BLI_vector.hh"
 
@@ -84,10 +87,10 @@ class OBJMesh : NonMovable, NonCopyable {
   const MPoly &get_ith_poly(const uint i) const;
 
   const char *get_object_name() const;
-  const char *get_object_data_name() const;
+  const char *get_object_mesh_name() const;
   const char *get_object_material_name(const short mat_nr) const;
 
-  void calc_vertex_coords(const uint vert_index, float r_coords[3]) const;
+  float3 calc_vertex_coords(const uint vert_index) const;
   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,
                                    Vector<Vector<uint>> &r_uv_indices);
@@ -95,7 +98,7 @@ class OBJMesh : NonMovable, NonCopyable {
   float3 calc_vertex_normal(const uint vert_index) const;
   void calc_poly_normal_indices(const uint poly_index, Vector<uint> &r_normal_indices) const;
   const char *get_poly_deform_group_name(const MPoly &mpoly, short &r_last_vertex_group) const;
-  Array<int, 2> calc_edge_vert_indices(const uint edge_index) const;
+  std::optional<std::array<int, 2>> calc_edge_vert_indices(const uint edge_index) const;
 
  private:
   void triangulate_mesh_eval();
diff --git a/source/blender/io/wavefront_obj/intern/obj_export_mtl.cc b/source/blender/io/wavefront_obj/intern/obj_export_mtl.cc
index b31bd040a0d..f18d1b5297f 100644
--- a/source/blender/io/wavefront_obj/intern/obj_export_mtl.cc
+++ b/source/blender/io/wavefront_obj/intern/obj_export_mtl.cc
@@ -99,7 +99,7 @@ static void linked_sockets_to_dest_id(Vector<const nodes::OutputSocketRef *> &r_
   Span<const nodes::InputSocketRef *> dest_inputs = object_dest_nodes.first()->inputs();
   const nodes::InputSocketRef *dest_socket = nullptr;
   for (const nodes::InputSocketRef *curr_socket : dest_inputs) {
-    if (STREQ(curr_socket->bsocket()->identifier, dest_socket_id.data())) {
+    if (STREQ(curr_socket->bsocket()->identifier, dest_socket_id.c_str())) {
       dest_socket = 

@@ Diff output truncated at 10240 characters. @@



More information about the Bf-blender-cvs mailing list