[Bf-blender-cvs] [c5bd1631f60] soc-2020-io-performance: Add fallback value in string to number conversions
Ankit Meel
noreply at git.blender.org
Thu Jul 30 15:02:01 CEST 2020
Commit: c5bd1631f60c7dc4d569e988e12e1ac7cb33cc1f
Author: Ankit Meel
Date: Thu Jul 30 18:07:13 2020 +0530
Branches: soc-2020-io-performance
https://developer.blender.org/rBc5bd1631f60c7dc4d569e988e12e1ac7cb33cc1f
Add fallback value in string to number conversions
Remove remnant `stringstream s_line` which was used with `>>`
operator for storing floats and ints. That has been replaced
with `stof`/ `stoi`.
Limit the scope of MTL exporter's tex_node pointer.
===================================================================
M source/blender/io/wavefront_obj/intern/wavefront_obj_ex_mtl.cc
M source/blender/io/wavefront_obj/intern/wavefront_obj_im_file_reader.cc
M source/blender/io/wavefront_obj/intern/wavefront_obj_im_file_reader.hh
===================================================================
diff --git a/source/blender/io/wavefront_obj/intern/wavefront_obj_ex_mtl.cc b/source/blender/io/wavefront_obj/intern/wavefront_obj_ex_mtl.cc
index 5cff990a766..0bc4193d65f 100644
--- a/source/blender/io/wavefront_obj/intern/wavefront_obj_ex_mtl.cc
+++ b/source/blender/io/wavefront_obj/intern/wavefront_obj_ex_mtl.cc
@@ -227,7 +227,6 @@ void MTLWriter::write_curr_material(const char *object_name)
texture_map_types.add("map_Ke", "Emission");
const char *tex_image_filepath = nullptr;
- const bNode *tex_node = nullptr;
/* Need to create a NodeTreeRef for a faster way to find linked sockets, as opposed to
* looping over all the links in a node tree to match two sockets of our interest. */
@@ -238,11 +237,12 @@ void MTLWriter::write_curr_material(const char *object_name)
/* Find sockets linked to the destination socket of interest, in p-bsdf node. */
linked_sockets_to_dest_id(linked_sockets, bsdf_node_, node_tree, map_type_id.value.c_str());
/* Among the linked sockets, find Image Texture shader node. */
- tex_node = get_node_of_type(linked_sockets, SH_NODE_TEX_IMAGE);
+ const bNode *tex_node{get_node_of_type(linked_sockets, SH_NODE_TEX_IMAGE)};
/* Find "Mapping" node if connected to texture node. */
linked_sockets_to_dest_id(linked_sockets, tex_node, node_tree, "Vector");
const bNode *mapping = get_node_of_type(linked_sockets, SH_NODE_MAPPING);
+
/* Texture transform options. Only translation (origin offset, "-o") and scale
* ("-o") are supported. */
float map_translation[3] = {0.0f, 0.0f, 0.0f};
@@ -278,11 +278,12 @@ void MTLWriter::write_curr_material(const char *object_name)
/* Find sockets linked to "Color" socket in normal map node. */
linked_sockets_to_dest_id(linked_sockets, normal_map_node, node_tree, "Color");
/* Among the linked sockets, find Image Texture shader node. */
- tex_node = get_node_of_type(linked_sockets, SH_NODE_TEX_IMAGE);
+ const bNode *tex_node{get_node_of_type(linked_sockets, SH_NODE_TEX_IMAGE)};
/* Find "Mapping" node if connected to the texture node. */
linked_sockets_to_dest_id(linked_sockets, tex_node, node_tree, "Vector");
const bNode *mapping = get_node_of_type(linked_sockets, SH_NODE_MAPPING);
+
float map_translation[3] = {0.0f, 0.0f, 0.0f};
float map_scale[3] = {1.0f, 1.0f, 1.0f};
float normal_map_strength = 1.0f;
diff --git a/source/blender/io/wavefront_obj/intern/wavefront_obj_im_file_reader.cc b/source/blender/io/wavefront_obj/intern/wavefront_obj_im_file_reader.cc
index 0e086d7f0c8..f2dfdc919a1 100644
--- a/source/blender/io/wavefront_obj/intern/wavefront_obj_im_file_reader.cc
+++ b/source/blender/io/wavefront_obj/intern/wavefront_obj_im_file_reader.cc
@@ -63,25 +63,39 @@ static string first_word_of_string(const string &in_string)
return pos == string::npos ? in_string.substr(0, 1) : in_string.substr(0, pos);
}
+/**
+ * Convert the given string to float and assign it to the destination value.
+ *
+ * Catches exception if the string cannot be converted to a float. The destination value
+ * is set to the given fallback value in that case.
+ */
+
+void copy_string_to_float(StringRef src, const float fallback_value, float &r_dst)
+{
+ try {
+ r_dst = std::stof(src.data());
+ }
+ catch (const std::invalid_argument &inv_arg) {
+ fprintf(stderr, "Bad conversion to float:%s:%s\n", inv_arg.what(), src.data());
+ r_dst = fallback_value;
+ }
+}
+
/**
* Convert all members of the Span of strings to floats and assign them to the float
* array members. Usually used for values like coordinates.
*
- * Catches exception if the string cannot be converted to a float. The float array members
- * are set to <TODO ankitm: values can be -1.0 too!> in that case.
+ * Catches exception if any string cannot be converted to a float. The destination
+ * float is set to the given fallback value in that case.
*/
-BLI_INLINE void copy_string_to_float(Span<string> src, MutableSpan<float> r_dst)
+BLI_INLINE void copy_string_to_float(Span<string> src,
+ const float fallback_value,
+ MutableSpan<float> r_dst)
{
BLI_assert(src.size() == r_dst.size());
for (int i = 0; i < r_dst.size(); ++i) {
- try {
- r_dst[i] = std::stof(src[i]);
- }
- catch (const std::invalid_argument &inv_arg) {
- fprintf(stderr, "Bad conversion to float:%s:%s\n", inv_arg.what(), src[i].c_str());
- r_dst[i] = -1.0f;
- }
+ copy_string_to_float(src[i], fallback_value, r_dst[i]);
}
}
@@ -89,30 +103,32 @@ BLI_INLINE void copy_string_to_float(Span<string> src, MutableSpan<float> r_dst)
* Convert the given string to int and assign it to the destination value.
*
* Catches exception if the string cannot be converted to an integer. The destination
- * int is set to <TODO ankitm: indices can be -1 too!> in that case.
+ * int is set to the given fallback value in that case.
*/
-BLI_INLINE void copy_string_to_int(const string &src, int &r_dst)
+BLI_INLINE void copy_string_to_int(StringRef src, const int fallback_value, int &r_dst)
{
try {
- r_dst = std::stoi(src);
+ r_dst = std::stoi(src.data());
}
catch (const std::invalid_argument &inv_arg) {
- fprintf(stderr, "Bad conversion to int:%s:%s\n", inv_arg.what(), src.c_str());
- r_dst = -1;
+ fprintf(stderr, "Bad conversion to int:%s:%s\n", inv_arg.what(), src.data());
+ r_dst = fallback_value;
}
}
/**
* Convert the given strings to ints and fill the destination int buffer.
*
- * Catches exception if a string cannot be converted to an integer. The destination
- * int is set to <TODO ankitm: indices can be -1 too!> in that case.
+ * Catches exception if any string cannot be converted to an integer. The destination
+ * int is set to the given fallback value in that case.
*/
-BLI_INLINE void copy_string_to_int(Span<string> src, MutableSpan<int> r_dst)
+BLI_INLINE void copy_string_to_int(Span<string> src,
+ const int fallback_value,
+ MutableSpan<int> r_dst)
{
BLI_assert(src.size() == r_dst.size());
for (int i = 0; i < r_dst.size(); ++i) {
- copy_string_to_int(src[i], r_dst[i]);
+ copy_string_to_int(src[i], fallback_value, r_dst[i]);
}
}
@@ -142,7 +158,7 @@ static bool create_raw_curve(std::unique_ptr<OBJRawObject> *raw_object)
OBJParser::OBJParser(const OBJImportParams &import_params) : import_params_(import_params)
{
- infile_.open(import_params_.filepath);
+ obj_file_.open(import_params_.filepath);
}
/**
@@ -169,6 +185,10 @@ void OBJParser::update_index_offsets(std::unique_ptr<OBJRawObject> *curr_ob)
void OBJParser::parse_and_store(Vector<std::unique_ptr<OBJRawObject>> &list_of_objects,
GlobalVertices &global_vertices)
{
+ if (!obj_file_.good()) {
+ fprintf(stderr, "Cannot read from file:%s.\n", import_params_.filepath);
+ return;
+ }
string line;
/* Non owning raw pointer to the unique_ptr to a raw object.
* Needed to update object data in the same while loop. */
@@ -178,24 +198,24 @@ void OBJParser::parse_and_store(Vector<std::unique_ptr<OBJRawObject>> &list_of_o
bool shaded_smooth = false;
string object_group{};
- while (std::getline(infile_, line)) {
- string line_key = first_word_of_string(line);
- std::stringstream s_line(line.substr(line_key.size()));
+ while (std::getline(obj_file_, line)) {
+ string line_key{first_word_of_string(line)};
+ string rest_line{line.substr(line_key.size())};
if (line_key == "o") {
/* Update index offsets to keep track of objects which have claimed their vertices. */
update_index_offsets(curr_ob);
shaded_smooth = false;
object_group = {};
- list_of_objects.append(std::make_unique<OBJRawObject>(s_line.str()));
+ list_of_objects.append(std::make_unique<OBJRawObject>(rest_line));
curr_ob = &list_of_objects.last();
(*curr_ob)->object_type_ = OB_MESH;
}
else if (line_key == "v") {
float3 curr_vert{};
Vector<string> str_vert_split;
- split_by_char(s_line.str(), ' ', str_vert_split);
- copy_string_to_float(str_vert_split, {curr_vert, 3});
+ split_by_char(rest_line, ' ', str_vert_split);
+ copy_string_to_float(str_vert_split, FLT_MAX, {curr_vert, 3});
global_vertices.vertices.append(curr_vert);
if (curr_ob) {
/* Always keep indices zero-based. */
@@ -208,8 +228,8 @@ void OBJParser::parse_and_store(Vector<std::unique_ptr<OBJRawObject>> &list_of_o
else if (line_key == "vt") {
float2 curr_uv_vert{};
Vector<string> str_uv_vert_split;
- split_by_char(s_line.str(), ' ', str_uv_vert_split);
- copy_string_to_float(str_uv_vert_split, {curr_uv_vert, 2});
+ split_by_char(rest_line, ' ', str_uv_vert_split);
+ copy_string_to_float(str_uv_vert_split, FLT_MAX, {curr_uv_vert, 2});
global_vertices.uv_vertices.append(curr_uv_vert);
if (curr_ob) {
(*curr_ob)->uv_vertex_indices_.append(global_vertices.uv_vertices.size() - 1);
@@ -218,9 +238,9 @@ void OBJParser::parse_and_store(Vector<std::unique_ptr<OBJRawObject>> &list_of_o
else if (line_key == "l") {
int edge_v1 = -1, edge_v2 = -1;
Vector<string> str_edge_split;
- split_by_char(s_line.str(), ' ', str_edge_split);
- copy_string_to_int(str_edge_split[0], edge_v1);
- copy_string_to_int(str_edge_split[1], edge_v2);
+ 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);
/* Remove the indices of vertices "claimed" by other raw objects. Subtract 1 to make the OBJ
* indices (one-based) C++'s zero-based. In the other case, make relative index positive and
* absolute, starting with zero. */
@@ -230,28 +250,26 @@ void OBJParser::parse_and_store(Vector<std::unique_ptr<OBJRawObject>> &list_of_o
(*curr_ob)->edges_.append({static_cast<uint>(edge_v1), static_cast<uint>(edge_v2)});
}
else if (line_key == "g") {
- object_group = s_line.str();
+ object_group
@@ Diff output truncated at 10240 characters. @@
More information about the Bf-blender-cvs
mailing list