[Bf-blender-cvs] [ad40a7c3d6a] soc-2020-io-performance: Exporter: create new API for writer.

Ankit Meel noreply at git.blender.org
Fri Dec 11 22:08:14 CET 2020


Commit: ad40a7c3d6a40a9207b90079c7bf0f0b7357361d
Author: Ankit Meel
Date:   Sat Dec 12 02:14:53 2020 +0530
Branches: soc-2020-io-performance
https://developer.blender.org/rBad40a7c3d6a40a9207b90079c7bf0f0b7357361d

Exporter: create new API for writer.

Isolate formatting options.
Add exceptions instead of three layers of booleans.
Separate header writing from constructor.

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

M	source/blender/io/wavefront_obj/CMakeLists.txt
M	source/blender/io/wavefront_obj/exporter/obj_export_file_writer.cc
M	source/blender/io/wavefront_obj/exporter/obj_export_file_writer.hh
A	source/blender/io/wavefront_obj/exporter/obj_export_io.hh
M	source/blender/io/wavefront_obj/exporter/obj_export_mtl.cc
M	source/blender/io/wavefront_obj/exporter/obj_export_mtl.hh
M	source/blender/io/wavefront_obj/exporter/obj_exporter.cc

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

diff --git a/source/blender/io/wavefront_obj/CMakeLists.txt b/source/blender/io/wavefront_obj/CMakeLists.txt
index 57b3c672487..8e49f4fe2b6 100644
--- a/source/blender/io/wavefront_obj/CMakeLists.txt
+++ b/source/blender/io/wavefront_obj/CMakeLists.txt
@@ -58,6 +58,7 @@ set(SRC
   IO_wavefront_obj.h
   exporter/obj_exporter.hh
   exporter/obj_export_file_writer.hh
+  exporter/obj_export_io.hh
   exporter/obj_export_mesh.hh
   exporter/obj_export_mtl.hh
   exporter/obj_export_nurbs.hh
diff --git a/source/blender/io/wavefront_obj/exporter/obj_export_file_writer.cc b/source/blender/io/wavefront_obj/exporter/obj_export_file_writer.cc
index 3720a41080e..5f55bcf241d 100644
--- a/source/blender/io/wavefront_obj/exporter/obj_export_file_writer.cc
+++ b/source/blender/io/wavefront_obj/exporter/obj_export_file_writer.cc
@@ -61,15 +61,14 @@ void OBJWriter::write_vert_uv_normal_indices(Span<int> vert_indices,
 {
   BLI_assert(vert_indices.size() == uv_indices.size() &&
              vert_indices.size() == normal_indices.size());
-  fputs("f", outfile_);
+  file_handler_->write<eOBJSyntaxElement::poly_element_begin>();
   for (int j = 0; j < vert_indices.size(); j++) {
-    fprintf(outfile_,
-            " %u/%u/%u",
-            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);
+    file_handler_->write<eOBJSyntaxElement::vertex_uv_normal_indices>(
+        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);
   }
-  fputs("\n", outfile_);
+  file_handler_->write<eOBJSyntaxElement::poly_element_end>();
 }
 
 /**
@@ -80,14 +79,13 @@ void OBJWriter::write_vert_normal_indices(Span<int> vert_indices,
                                           Span<int> normal_indices) const
 {
   BLI_assert(vert_indices.size() == normal_indices.size());
-  fputs("f", outfile_);
+  file_handler_->write<eOBJSyntaxElement::poly_element_begin>();
   for (int j = 0; j < vert_indices.size(); j++) {
-    fprintf(outfile_,
-            " %u//%u",
-            vert_indices[j] + index_offsets_.vertex_offset + 1,
-            normal_indices[j] + index_offsets_.normal_offset + 1);
+    file_handler_->write<eOBJSyntaxElement::vertex_normal_indices>(
+        vert_indices[j] + index_offsets_.vertex_offset + 1,
+        normal_indices[j] + index_offsets_.normal_offset + 1);
   }
-  fputs("\n", outfile_);
+  file_handler_->write<eOBJSyntaxElement::poly_element_end>();
 }
 
 /**
@@ -98,14 +96,13 @@ void OBJWriter::write_vert_uv_indices(Span<int> vert_indices,
                                       Span<int> /*normal_indices*/) const
 {
   BLI_assert(vert_indices.size() == uv_indices.size());
-  fputs("f", outfile_);
+  file_handler_->write<eOBJSyntaxElement::poly_element_begin>();
   for (int j = 0; j < vert_indices.size(); j++) {
-    fprintf(outfile_,
-            " %u/%u",
-            vert_indices[j] + index_offsets_.vertex_offset + 1,
-            uv_indices[j] + index_offsets_.uv_vertex_offset + 1);
+    file_handler_->write<eOBJSyntaxElement::vertex_uv_indices>(
+        vert_indices[j] + index_offsets_.vertex_offset + 1,
+        uv_indices[j] + index_offsets_.uv_vertex_offset + 1);
   }
-  fputs("\n", outfile_);
+  file_handler_->write<eOBJSyntaxElement::poly_element_end>();
 }
 
 /**
@@ -115,46 +112,32 @@ void OBJWriter::write_vert_indices(Span<int> vert_indices,
                                    Span<int> /*uv_indices*/,
                                    Span<int> /*normal_indices*/) const
 {
-  fputs("f", outfile_);
+  file_handler_->write<eOBJSyntaxElement::poly_element_begin>();
   for (const int vert_index : vert_indices) {
-    fprintf(outfile_, " %u", vert_index + index_offsets_.vertex_offset + 1);
+    file_handler_->write<eOBJSyntaxElement::vertex_indices>(vert_index +
+                                                            index_offsets_.vertex_offset + 1);
   }
-  fputs("\n", outfile_);
+  file_handler_->write<eOBJSyntaxElement::poly_element_end>();
 }
 
-OBJWriter::~OBJWriter()
+void OBJWriter::writer_header() const
 {
-  if (outfile_ && fclose(outfile_)) {
-    std::cerr << "Error: could not close the OBJ file properly, file may be corrupted."
-              << std::endl;
-  }
-}
-
-/**
- * Try to open the .OBJ file and write file header.
- * \return Whether the destination file is writable.
- */
-bool OBJWriter::init_writer(const char *filepath)
-{
-  outfile_ = fopen(filepath, "w");
-  if (!outfile_) {
-    std::perror(std::string("Error in creating the file at: ").append(filepath).c_str());
-    return false;
-  }
-  fprintf(outfile_, "# Blender %s\n# www.blender.org\n", BKE_blender_version_string());
-  return true;
+  using namespace std::string_literals;
+  file_handler_->write<eOBJSyntaxElement::string>("# Blender "s + BKE_blender_version_string() +
+                                                  "\n");
+  file_handler_->write<eOBJSyntaxElement::string>("# www.blender.org\n");
 }
 
 /**
  * Write file name of Material Library in .OBJ file.
  */
-void OBJWriter::write_mtllib_name(const char *mtl_filepath) const
+void OBJWriter::write_mtllib_name(const StringRefNull mtl_filepath) const
 {
   /* Split .MTL file path into parent directory and filename. */
   char mtl_file_name[FILE_MAXFILE];
   char mtl_dir_name[FILE_MAXDIR];
-  BLI_split_dirfile(mtl_filepath, mtl_dir_name, mtl_file_name, FILE_MAXDIR, FILE_MAXFILE);
-  fprintf(outfile_, "mtllib %s\n", mtl_file_name);
+  BLI_split_dirfile(mtl_filepath.data(), mtl_dir_name, mtl_file_name, FILE_MAXDIR, FILE_MAXFILE);
+  file_handler_->write<eOBJSyntaxElement::mtllib>(mtl_file_name);
 }
 
 /**
@@ -168,15 +151,16 @@ void OBJWriter::write_object_group(const OBJMesh &obj_mesh_data) const
   if (!export_params_.export_object_groups) {
     return;
   }
-  const char *object_name = obj_mesh_data.get_object_name();
+  const std::string object_name = obj_mesh_data.get_object_name();
   const char *object_mesh_name = obj_mesh_data.get_object_mesh_name();
   const char *object_material_name = obj_mesh_data.get_object_material_name(0);
   if (export_params_.export_materials && export_params_.export_material_groups &&
       object_material_name) {
-    fprintf(outfile_, "g %s_%s_%s\n", object_name, object_mesh_name, object_material_name);
+    file_handler_->write<eOBJSyntaxElement::object_group>(object_name + "_" + object_mesh_name +
+                                                          "_" + object_material_name);
     return;
   }
-  fprintf(outfile_, "g %s_%s\n", object_name, object_mesh_name);
+  file_handler_->write<eOBJSyntaxElement::object_group>(object_name + "_" + object_mesh_name);
 }
 
 /**
@@ -189,7 +173,7 @@ void OBJWriter::write_object_name(const OBJMesh &obj_mesh_data) const
     write_object_group(obj_mesh_data);
     return;
   }
-  fprintf(outfile_, "o %s\n", object_name);
+  file_handler_->write<eOBJSyntaxElement::object_name>(object_name);
 }
 
 /**
@@ -200,7 +184,7 @@ void OBJWriter::write_vertex_coords(const OBJMesh &obj_mesh_data) const
   const int tot_vertices = obj_mesh_data.tot_vertices();
   for (int i = 0; i < tot_vertices; i++) {
     float3 vertex = obj_mesh_data.calc_vertex_coords(i, export_params_.scaling_factor);
-    fprintf(outfile_, "v %f %f %f\n", vertex[0], vertex[1], vertex[2]);
+    file_handler_->write<eOBJSyntaxElement::vertex_coords>(vertex[0], vertex[1], vertex[2]);
   }
 }
 
@@ -215,7 +199,7 @@ void OBJWriter::write_uv_coords(OBJMesh &r_obj_mesh_data) const
   r_obj_mesh_data.store_uv_coords_and_indices(uv_coords);
 
   for (const std::array<float, 2> &uv_vertex : uv_coords) {
-    fprintf(outfile_, "vt %f %f\n", uv_vertex[0], uv_vertex[1]);
+    file_handler_->write<eOBJSyntaxElement::uv_vertex_coords>(uv_vertex[0], uv_vertex[1]);
   }
 }
 
@@ -231,12 +215,13 @@ void OBJWriter::write_poly_normals(const OBJMesh &obj_mesh_data) const
     if (obj_mesh_data.is_ith_poly_smooth(i)) {
       obj_mesh_data.calc_loop_normals(i, lnormals);
       for (const float3 &lnormal : lnormals) {
-        fprintf(outfile_, "vn %f %f %f\n", lnormal[0], lnormal[1], lnormal[2]);
+        file_handler_->write<eOBJSyntaxElement::normal>(lnormal[0], lnormal[1], lnormal[2]);
       }
     }
     else {
       float3 poly_normal = obj_mesh_data.calc_poly_normal(i);
-      fprintf(outfile_, "vn %f %f %f\n", poly_normal[0], poly_normal[1], poly_normal[2]);
+      file_handler_->write<eOBJSyntaxElement::normal>(
+          poly_normal[0], poly_normal[1], poly_normal[2]);
     }
   }
 }
@@ -262,7 +247,7 @@ int OBJWriter::write_smooth_group(const OBJMesh &obj_mesh_data,
     /* Group has already been written, even if it is "s 0". */
     return current_group;
   }
-  fprintf(outfile_, "s %d\n", current_group);
+  file_handler_->write<eOBJSyntaxElement::smooth_group>(current_group);
   return current_group;
 }
 
@@ -285,14 +270,14 @@ int16_t OBJWriter::write_poly_material(const OBJMesh &obj_mesh_data,
     return current_mat_nr;
   }
   if (current_mat_nr == NOT_FOUND) {
-    fprintf(outfile_, "usemtl %s\n", MATERIAL_GROUP_DISABLED);
+    file_handler_->write<eOBJSyntaxElement::poly_usemtl>(MATERIAL_GROUP_DISABLED);
     return current_mat_nr;
   }
   const char *mat_name = obj_mesh_data.get_object_material_name(current_mat_nr);
   if (export_params_.export_object_groups) {
     write_object_group(obj_mesh_data);
   }
-  fprintf(outfile_, "usemtl %s\n", mat_name);
+  file_handler_->write<eOBJSyntaxElement::poly_usemtl>(mat_name);
   return current_mat_nr;
 }
 
@@ -313,10 +298,11 @@ int16_t OBJWriter::write_vertex_group(const OBJMesh &obj_mesh_data,
     return current_group;
   }
   if (current_group == NOT_FOUND) {
-    fprintf(outfile_, "g %s\n", DEFORM_GROUP_DISABLED);
+    file_handler_->write<eOBJSyntaxElement::object_group>(DEFORM_GROUP_DISABLED);
     return current_group;
   }
-  fprintf(outfile_, "g %s\n", obj_mesh_data.get_poly_deform_group_name(current_group));
+  file_handler_->write<eOBJSyntaxElement::object_group>(
+      obj_mesh_data.get_poly_deform_group_name(current_group));
   return current_group;
 }
 
@@ -391,10 +377,9 @@ v

@@ Diff output truncated at 10240 characters. @@



More information about the Bf-blender-cvs mailing list