[Bf-blender-cvs] [b54aa8a77c7] soc-2020-io-performance: Importer: make a class OBJStorer, and smaller functions.

Ankit Meel noreply at git.blender.org
Fri Dec 4 19:57:27 CET 2020


Commit: b54aa8a77c733d33f30695513a7b77fa2efc2c33
Author: Ankit Meel
Date:   Tue Dec 1 16:49:36 2020 +0530
Branches: soc-2020-io-performance
https://developer.blender.org/rBb54aa8a77c733d33f30695513a7b77fa2efc2c33

Importer: make a class OBJStorer, and smaller functions.

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

M	source/blender/io/wavefront_obj/importer/obj_import_file_reader.cc
M	source/blender/io/wavefront_obj/importer/obj_import_file_reader.hh
M	source/blender/io/wavefront_obj/importer/obj_import_objects.hh
M	source/blender/io/wavefront_obj/importer/obj_importer.cc

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

diff --git a/source/blender/io/wavefront_obj/importer/obj_import_file_reader.cc b/source/blender/io/wavefront_obj/importer/obj_import_file_reader.cc
index 5cbd06bad67..2af5c1a612b 100644
--- a/source/blender/io/wavefront_obj/importer/obj_import_file_reader.cc
+++ b/source/blender/io/wavefront_obj/importer/obj_import_file_reader.cc
@@ -87,6 +87,199 @@ static Geometry *create_geometry(Geometry *const prev_geometry,
   return new_geometry();
 }
 
+void OBJStorer::add_vertex(const StringRef rest_line, GlobalVertices &r_global_vertices)
+{
+  float3 curr_vert;
+  Vector<StringRef> str_vert_split;
+  split_by_char(rest_line, ' ', str_vert_split);
+  copy_string_to_float(str_vert_split, FLT_MAX, {curr_vert, 3});
+  r_global_vertices.vertices.append(curr_vert);
+  r_geom_.vertex_indices_.append(r_global_vertices.vertices.size() - 1);
+}
+
+void OBJStorer::add_vertex_normal(const StringRef rest_line, GlobalVertices &r_global_vertices)
+{
+  float3 curr_vert_normal;
+  Vector<StringRef> str_vert_normal_split;
+  split_by_char(rest_line, ' ', str_vert_normal_split);
+  copy_string_to_float(str_vert_normal_split, FLT_MAX, {curr_vert_normal, 2});
+  r_global_vertices.vertex_normals.append(curr_vert_normal);
+  r_geom_.vertex_normal_indices_.append(r_global_vertices.vertex_normals.size() - 1);
+}
+
+void OBJStorer::add_uv_vertex(const StringRef rest_line, GlobalVertices &r_global_vertices)
+{
+  float2 curr_uv_vert;
+  Vector<StringRef> str_uv_vert_split;
+  split_by_char(rest_line, ' ', str_uv_vert_split);
+  copy_string_to_float(str_uv_vert_split, FLT_MAX, {curr_uv_vert, 2});
+  r_global_vertices.uv_vertices.append(curr_uv_vert);
+}
+
+void OBJStorer::add_edge(const StringRef rest_line,
+                         const VertexIndexOffset &offsets,
+                         GlobalVertices &r_global_vertices)
+{
+  int edge_v1 = -1, edge_v2 = -1;
+  Vector<StringRef> str_edge_split;
+  split_by_char(rest_line, ' ', str_edge_split);
+  copy_string_to_int(str_edge_split[0], -1, edge_v1);
+  copy_string_to_int(str_edge_split[1], -1, edge_v2);
+  /* Always keep stored indices non-negative and zero-based. */
+  edge_v1 += edge_v1 < 0 ? r_global_vertices.vertices.size() : -offsets.get_index_offset() - 1;
+  edge_v2 += edge_v2 < 0 ? r_global_vertices.vertices.size() : -offsets.get_index_offset() - 1;
+  BLI_assert(edge_v1 >= 0 && edge_v2 >= 0);
+  r_geom_.edges_.append({static_cast<uint>(edge_v1), static_cast<uint>(edge_v2)});
+}
+
+void OBJStorer::add_polygon(const StringRef rest_line,
+                            const VertexIndexOffset &offsets,
+                            const GlobalVertices &global_vertices,
+                            const StringRef state_material_name,
+                            const StringRef state_object_group,
+                            const bool state_shaded_smooth)
+{
+  PolyElem curr_face;
+  curr_face.shaded_smooth = state_shaded_smooth;
+  if (!state_material_name.is_empty()) {
+    curr_face.material_name = state_material_name;
+  }
+  if (!state_object_group.is_empty()) {
+    curr_face.vertex_group = state_object_group;
+    /* Yes it repeats several times, but another if-check will not reduce steps either. */
+    r_geom_.use_vertex_groups_ = true;
+  }
+
+  Vector<StringRef> str_corners_split;
+  split_by_char(rest_line, ' ', str_corners_split);
+  for (StringRef str_corner : str_corners_split) {
+    PolyCorner corner;
+    const size_t n_slash = std::count(str_corner.begin(), str_corner.end(), '/');
+    if (n_slash == 0) {
+      /* Case: "f v1 v2 v3". */
+      copy_string_to_int(str_corner, INT32_MAX, corner.vert_index);
+    }
+    else if (n_slash == 1) {
+      /* Case: "f v1/vt1 v2/vt2 v3/vt3". */
+      Vector<StringRef> vert_uv_split;
+      split_by_char(str_corner, '/', vert_uv_split);
+      copy_string_to_int(vert_uv_split[0], INT32_MAX, corner.vert_index);
+      if (vert_uv_split.size() == 2) {
+        copy_string_to_int(vert_uv_split[1], INT32_MAX, corner.uv_vert_index);
+      }
+    }
+    else if (n_slash == 2) {
+      /* Case: "f v1//vn1 v2//vn2 v3//vn3". */
+      /* Case: "f v1/vt1/vn1 v2/vt2/vn2 v3/vt3/vn3". */
+      Vector<StringRef> vert_uv_normal_split;
+      split_by_char(str_corner, '/', vert_uv_normal_split);
+      copy_string_to_int(vert_uv_normal_split[0], INT32_MAX, corner.vert_index);
+      copy_string_to_int(vert_uv_normal_split[1], INT32_MAX, corner.uv_vert_index);
+      if (vert_uv_normal_split.size() == 3) {
+        copy_string_to_int(vert_uv_normal_split[2], INT32_MAX, corner.vertex_normal_index);
+      }
+    }
+    /* Always keep stored indices non-negative and zero-based. */
+    corner.vert_index += corner.vert_index < 0 ? global_vertices.vertices.size() :
+                                                 -offsets.get_index_offset() - 1;
+    corner.uv_vert_index += corner.uv_vert_index < 0 ? global_vertices.uv_vertices.size() : -1;
+    corner.vertex_normal_index += corner.vertex_normal_index < 0 ?
+                                      global_vertices.vertex_normals.size() :
+                                      -1;
+    curr_face.face_corners.append(corner);
+  }
+
+  r_geom_.face_elements_.append(curr_face);
+  r_geom_.total_loops_ += curr_face.face_corners.size();
+}
+
+void OBJStorer::set_curve_type(const StringRef rest_line,
+                               const GlobalVertices &global_vertices,
+                               const StringRef state_object_group,
+                               VertexIndexOffset &r_offsets,
+                               Vector<std::unique_ptr<Geometry>> &r_all_geometries)
+{
+  if (rest_line.find("bspline") != StringRef::not_found) {
+    r_geom_ = *create_geometry(
+        &r_geom_, GEOM_CURVE, state_object_group, global_vertices, r_all_geometries, r_offsets);
+    r_geom_.nurbs_element_.group_ = state_object_group;
+  }
+  else {
+    std::cerr << "Curve type not supported:'" << rest_line << "'" << std::endl;
+  }
+}
+
+void OBJStorer::set_curve_degree(const StringRef rest_line)
+{
+  copy_string_to_int(rest_line, 3, r_geom_.nurbs_element_.degree);
+}
+
+void OBJStorer::add_curve_vertex_indices(const StringRef rest_line,
+                                         const GlobalVertices &global_vertices)
+{
+  Vector<StringRef> str_curv_split;
+  split_by_char(rest_line, ' ', str_curv_split);
+  /* Remove "0.0" and "1.0" from the strings. They are hardcoded. */
+  str_curv_split.remove(0);
+  str_curv_split.remove(0);
+  r_geom_.nurbs_element_.curv_indices.resize(str_curv_split.size());
+  copy_string_to_int(str_curv_split, INT32_MAX, r_geom_.nurbs_element_.curv_indices);
+  for (int &curv_index : r_geom_.nurbs_element_.curv_indices) {
+    /* Always keep stored indices non-negative and zero-based. */
+    curv_index += curv_index < 0 ? global_vertices.vertices.size() : -1;
+  }
+}
+
+void OBJStorer::add_curve_parameters(const StringRef rest_line)
+{
+  Vector<StringRef> str_parm_split;
+  split_by_char(rest_line, ' ', str_parm_split);
+  if (str_parm_split[0] == "u" || str_parm_split[0] == "v") {
+    str_parm_split.remove(0);
+    r_geom_.nurbs_element_.parm.resize(str_parm_split.size());
+    copy_string_to_float(str_parm_split, FLT_MAX, r_geom_.nurbs_element_.parm);
+  }
+  else {
+    std::cerr << "Surfaces are not supported:'" << str_parm_split[0] << "'" << std::endl;
+  }
+}
+
+void OBJStorer::update_object_group(const StringRef rest_line,
+                                    std::string &r_state_object_group) const
+{
+
+  if (rest_line.find("off") != string::npos || rest_line.find("null") != string::npos ||
+      rest_line.find("default") != string::npos) {
+    /* Set group for future elements like faces or curves to empty. */
+    r_state_object_group = "";
+    return;
+  }
+  r_state_object_group = rest_line;
+}
+
+void OBJStorer::update_polygon_material(const StringRef rest_line,
+                                        std::string &r_state_material_name) const
+{
+  /* Materials may repeat if faces are written without sorting. */
+  r_geom_.material_names_.add(string(rest_line));
+  r_state_material_name = rest_line;
+}
+
+void OBJStorer::update_smooth_group(const StringRef rest_line, bool &r_state_shaded_smooth) const
+{
+  /* Some implementations use "0" and "null" too, in addition to "off". */
+  if (rest_line != "0" && rest_line.find("off") == StringRef::not_found &&
+      rest_line.find("null") == StringRef::not_found) {
+    int smooth = 0;
+    copy_string_to_int(rest_line, 0, smooth);
+    r_state_shaded_smooth = smooth != 0;
+  }
+  else {
+    /* The OBJ file explicitly set shading to off. */
+    r_state_shaded_smooth = false;
+  }
+}
+
 /**
  * Open OBJ file at the path given in import parameters.
  */
@@ -104,8 +297,8 @@ OBJParser::OBJParser(const OBJImportParams &import_params) : import_params_(impo
  * Read the OBJ file line by line and create OBJ Geometry instances. Also store all the vertex
  * and UV vertex coordinates in a struct accessible by all objects.
  */
-void OBJParser::parse_and_store(Vector<std::unique_ptr<Geometry>> &r_all_geometries,
-                                GlobalVertices &r_global_vertices)
+void OBJParser::parse(Vector<std::unique_ptr<Geometry>> &r_all_geometries,
+                      GlobalVertices &r_global_vertices)
 {
   if (!obj_file_.good()) {
     return;
@@ -135,180 +328,69 @@ void OBJParser::parse_and_store(Vector<std::unique_ptr<Geometry>> &r_all_geometr
       continue;
     }
 
-    if (line_key == "mtllib") {
-      mtl_libraries_.append(string(rest_line));
-    }
-    else if (line_key == "o") {
-      state_shaded_smooth = false;
-      state_object_group = "";
-      state_material_name = "";
-      current_geometry = create_geometry(
-          current_geometry, GEOM_MESH, rest_line, r_global_vertices, r_all_geometries, offsets);
-    }
-    else if (line_key == "v") {
-      BLI_assert(current_geometry);
-      float3 curr_vert;
-      Vector<StringRef> str_vert_split;
-      split_by_char(rest_line, ' ', str_vert_split);
-      copy_string_to_float(str_vert_split, FLT_MAX, {curr_vert, 3});
-      r_global_vertices.vertices.append(curr_vert);
-      current_geometry->vertex_indices_.append(r_global_vertices.vertices.size() - 1);
+    if (line_key == "

@@ Diff output truncated at 10240 characters. @@



More information about the Bf-blender-cvs mailing list