Skip to content

Commit

Permalink
Removed more references from modelio (#1072)
Browse files Browse the repository at this point in the history
  • Loading branch information
xelatihy authored Sep 4, 2020
1 parent 82b4756 commit 64593eb
Show file tree
Hide file tree
Showing 5 changed files with 76 additions and 54 deletions.
4 changes: 2 additions & 2 deletions libs/yocto/yocto_mesh.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2582,7 +2582,7 @@ bool load_mesh(const string& filename, vector<vec3i>& triangles,
return shape_error();

// decide what to do and get properties
auto materials = vector<obj_material*>{};
auto materials = vector<string>{};
auto ematerials = vector<int>{};
if (!shape->faces.empty()) {
get_triangles(shape, triangles, positions, normals, texcoords, materials,
Expand Down Expand Up @@ -2693,7 +2693,7 @@ bool load_lines(const string& filename, vector<vec2i>& lines,
return shape_error();

// decide what to do and get properties
auto materials = vector<obj_material*>{};
auto materials = vector<string>{};
auto ematerials = vector<int>{};
if (!shape->faces.empty()) {
get_lines(shape, lines, positions, normals, texcoords, materials,
Expand Down
62 changes: 39 additions & 23 deletions libs/yocto/yocto_modelio.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -98,11 +98,24 @@ inline void skip_whitespace(string_view& str) {
while (!str.empty() && is_space(str.front())) str.remove_prefix(1);
}

inline void remove_comment(string_view& str, char comment_char = '#') {
while (!str.empty() && is_newline(str.back())) str.remove_suffix(1);
auto cpy = str;
while (!cpy.empty() && cpy.front() != comment_char) cpy.remove_prefix(1);
str.remove_suffix(cpy.size());
inline void remove_comment(
string_view& str, char comment_char = '#', bool handle_quotes = false) {
if (!handle_quotes) {
while (!str.empty() && is_newline(str.back())) str.remove_suffix(1);
auto cpy = str;
while (!cpy.empty() && cpy.front() != comment_char) cpy.remove_prefix(1);
str.remove_suffix(cpy.size());
} else {
while (!str.empty() && is_newline(str.back())) str.remove_suffix(1);
auto cpy = str;
auto in_string = false;
while (!cpy.empty()) {
if (cpy.front() == '"') in_string = !in_string;
if (cpy.front() == comment_char && !in_string) break;
cpy.remove_prefix(1);
}
str.remove_suffix(cpy.size());
}
}

// Parse values from a string
Expand Down Expand Up @@ -1620,7 +1633,7 @@ bool load_obj(const string& filename, obj_scene* obj, string& error,
!geom_only && split_materials && !shape->materials.empty()) {
if (shape->materials.size() > 1)
throw std::runtime_error("should not have happened");
if (shape->materials.back()->name != mname) {
if (shape->materials.back() != mname) {
add_shape(obj);
obj->shapes.back()->name = oname + gname;
}
Expand All @@ -1639,9 +1652,9 @@ bool load_obj(const string& filename, obj_scene* obj, string& error,
}
auto mat_idx = -1;
for (auto midx = 0; midx < shape->materials.size(); midx++)
if (shape->materials[midx]->name == mname) mat_idx = midx;
if (shape->materials[midx] == mname) mat_idx = midx;
if (mat_idx < 0) {
shape->materials.push_back(material_map.at(mname));
shape->materials.push_back(mname);
mat_idx = (int)shape->materials.size() - 1;
}
element.material = (uint8_t)mat_idx;
Expand Down Expand Up @@ -2060,7 +2073,7 @@ bool save_obj(const string& filename, obj_scene* obj, string& error) {
for (auto& element : elements) {
if (!shape->materials.empty() && cur_material != element.material) {
if (!format_values(
fs, "usemtl {}\n", shape->materials[element.material]->name))
fs, "usemtl {}\n", shape->materials[element.material]))
return write_error();
cur_material = element.material;
}
Expand Down Expand Up @@ -2135,7 +2148,7 @@ inline vector<vec2f> flip_obj_texcoord(const vector<vec2f>& texcoord) {
// Get obj shape
void get_triangles(const obj_shape* shape, vector<vec3i>& triangles,
vector<vec3f>& positions, vector<vec3f>& normals, vector<vec2f>& texcoords,
vector<obj_material*>& materials, vector<int>& ematerials, bool flipv) {
vector<string>& materials, vector<int>& ematerials, bool flipv) {
if (shape->faces.empty()) return;
auto vindex = vector<int>{};
get_vertices(shape, positions, normals, texcoords, vindex, flipv);
Expand All @@ -2154,7 +2167,7 @@ void get_triangles(const obj_shape* shape, vector<vec3i>& triangles,
}
void get_quads(const obj_shape* shape, vector<vec4i>& quads,
vector<vec3f>& positions, vector<vec3f>& normals, vector<vec2f>& texcoords,
vector<obj_material*>& materials, vector<int>& ematerials, bool flipv) {
vector<string>& materials, vector<int>& ematerials, bool flipv) {
if (shape->faces.empty()) return;
auto vindex = vector<int>{};
get_vertices(shape, positions, normals, texcoords, vindex, flipv);
Expand All @@ -2179,7 +2192,7 @@ void get_quads(const obj_shape* shape, vector<vec4i>& quads,
}
void get_lines(const obj_shape* shape, vector<vec2i>& lines,
vector<vec3f>& positions, vector<vec3f>& normals, vector<vec2f>& texcoords,
vector<obj_material*>& materials, vector<int>& ematerials, bool flipv) {
vector<string>& materials, vector<int>& ematerials, bool flipv) {
if (shape->lines.empty()) return;
auto vindex = vector<int>{};
get_vertices(shape, positions, normals, texcoords, vindex, flipv);
Expand All @@ -2197,7 +2210,7 @@ void get_lines(const obj_shape* shape, vector<vec2i>& lines,
}
void get_points(const obj_shape* shape, vector<int>& points,
vector<vec3f>& positions, vector<vec3f>& normals, vector<vec2f>& texcoords,
vector<obj_material*>& materials, vector<int>& ematerials, bool flipv) {
vector<string>& materials, vector<int>& ematerials, bool flipv) {
if (shape->points.empty()) return;
auto vindex = vector<int>{};
get_vertices(shape, positions, normals, texcoords, vindex, flipv);
Expand All @@ -2216,7 +2229,7 @@ void get_points(const obj_shape* shape, vector<int>& points,
void get_fvquads(const obj_shape* shape, vector<vec4i>& quadspos,
vector<vec4i>& quadsnorm, vector<vec4i>& quadstexcoord,
vector<vec3f>& positions, vector<vec3f>& normals, vector<vec2f>& texcoords,
vector<obj_material*>& materials, vector<int>& ematerials, bool flipv) {
vector<string>& materials, vector<int>& ematerials, bool flipv) {
if (shape->faces.empty()) return;
positions = shape->positions;
normals = shape->normals;
Expand Down Expand Up @@ -2506,7 +2519,7 @@ void set_fvquads(obj_shape* shape, const vector<vec4i>& quadspos,
ematerials.empty() ? (uint8_t)0 : (uint8_t)ematerials[idx]});
}
}
void set_materials(obj_shape* shape, const vector<obj_material*>& materials) {
void set_materials(obj_shape* shape, const vector<string>& materials) {
shape->materials = materials;
}
void set_instances(obj_shape* shape, const vector<frame3f>& instances) {
Expand Down Expand Up @@ -2808,7 +2821,7 @@ inline bool read_pbrt_cmdline(file_stream& fs, string& cmd) {
while (read_line(fs, buffer)) {
// line
auto line = string_view{buffer.data()};
remove_comment(line);
remove_comment(line, '#', true);
skip_whitespace(line);
if (line.empty()) continue;

Expand Down Expand Up @@ -4218,6 +4231,7 @@ inline bool load_pbrt(const string& filename, pbrt_scene* pbrt, string& error,
} else if (cmd == "Texture") {
auto command = pbrt_command{};
auto comptype = ""s;
auto str_ = string{str};
if (!parse_param(str, command.name)) return parse_error();
if (!parse_param(str, comptype)) return parse_error();
if (!parse_param(str, command.type)) return parse_error();
Expand Down Expand Up @@ -4274,7 +4288,7 @@ inline bool load_pbrt(const string& filename, pbrt_scene* pbrt, string& error,
material->alpha_tex = alphamap;
material_map[matkey] = material;
}
shape->material = material_map.at(matkey);
shape->material = material_map.at(matkey)->name;
if (!ctx.cur_object.empty()) {
ctx.objects[ctx.cur_object].push_back(shape);
}
Expand Down Expand Up @@ -4369,12 +4383,13 @@ bool load_pbrt(const string& filename, pbrt_scene* pbrt, string& error) {
return false;

// remove unused materials
auto used_materials = unordered_set<pbrt_material*>{};
auto used_materials = unordered_set<string>{};
for (auto shape : pbrt->shapes) used_materials.insert(shape->material);
pbrt->materials.erase(
std::remove_if(pbrt->materials.begin(), pbrt->materials.end(),
[&used_materials](auto material) {
auto found = used_materials.find(material) != used_materials.end();
auto found = used_materials.find(material->name) !=
used_materials.end();
if (!found) delete material;
return !found;
}),
Expand Down Expand Up @@ -4555,6 +4570,7 @@ bool save_pbrt(
return (1 + sqrt(reflectivity)) / (1 - sqrt(reflectivity));
};

auto material_map = unordered_map<string, pbrt_material*>{};
for (auto material : pbrt->materials) {
auto command = pbrt_command{};
if (material->specular != 0 && material->transmission != 0 &&
Expand Down Expand Up @@ -4610,6 +4626,7 @@ bool save_pbrt(

auto object_id = 0;
for (auto shape : pbrt->shapes) {
auto material = material_map.at(shape->material);
auto command = pbrt_command{};
command.frame = shape->frame;
if (ply_meshes) {
Expand Down Expand Up @@ -4644,16 +4661,15 @@ bool save_pbrt(
if (!format_values(fs, "AttributeBegin\n")) return write_error();
if (!format_values(fs, "Transform {}\n", frame_to_mat(shape->frame)))
return write_error();
if (shape->material->emission != zero3f) {
if (material->emission != zero3f) {
auto acommand = pbrt_command{};
acommand.type = "diffuse";
acommand.values.push_back(
make_pbrt_value("L", shape->material->emission));
acommand.values.push_back(make_pbrt_value("L", material->emission));
if (!format_values(fs, "AreaLightSource \"{}\" {}\n", acommand.type,
acommand.values))
return write_error();
}
if (!format_values(fs, "NamedMaterial \"{}\"\n", shape->material->name))
if (!format_values(fs, "NamedMaterial \"{}\"\n", material->name))
return write_error();
if (!format_values(fs, "Shape \"{}\" {}\n", command.type, command.values))
return write_error();
Expand Down
34 changes: 17 additions & 17 deletions libs/yocto/yocto_modelio.h
Original file line number Diff line number Diff line change
Expand Up @@ -298,16 +298,16 @@ struct obj_material {

// Obj shape
struct obj_shape {
string name = "";
vector<vec3f> positions = {};
vector<vec3f> normals = {};
vector<vec2f> texcoords = {};
vector<obj_material*> materials = {};
vector<obj_vertex> vertices = {};
vector<obj_element> faces = {};
vector<obj_element> lines = {};
vector<obj_element> points = {};
vector<frame3f> instances = {};
string name = "";
vector<vec3f> positions = {};
vector<vec3f> normals = {};
vector<vec2f> texcoords = {};
vector<string> materials = {};
vector<obj_vertex> vertices = {};
vector<obj_element> faces = {};
vector<obj_element> lines = {};
vector<obj_element> points = {};
vector<frame3f> instances = {};
};

// Obj camera
Expand Down Expand Up @@ -352,24 +352,24 @@ bool save_obj(const string& filename, obj_scene* obj, string& error);
// to position only if duplication occurs.
void get_triangles(const obj_shape* shape, vector<vec3i>& triangles,
vector<vec3f>& positions, vector<vec3f>& normals, vector<vec2f>& texcoords,
vector<obj_material*>& materials, vector<int>& ematerials,
vector<string>& materials, vector<int>& ematerials,
bool flip_texcoord = false);
void get_quads(const obj_shape* shape, vector<vec4i>& quads,
vector<vec3f>& positions, vector<vec3f>& normals, vector<vec2f>& texcoords,
vector<obj_material*>& materials, vector<int>& ematerials,
vector<string>& materials, vector<int>& ematerials,
bool flip_texcoord = false);
void get_lines(const obj_shape* shape, vector<vec2i>& lines,
vector<vec3f>& positions, vector<vec3f>& normals, vector<vec2f>& texcoords,
vector<obj_material*>& materials, vector<int>& ematerials,
vector<string>& materials, vector<int>& ematerials,
bool flip_texcoord = false);
void get_points(const obj_shape* shape, vector<int>& points,
vector<vec3f>& positions, vector<vec3f>& normals, vector<vec2f>& texcoords,
vector<obj_material*>& materials, vector<int>& ematerials,
vector<string>& materials, vector<int>& ematerials,
bool flip_texcoord = false);
void get_fvquads(const obj_shape* shape, vector<vec4i>& quadspos,
vector<vec4i>& quadsnorm, vector<vec4i>& quadstexcoord,
vector<vec3f>& positions, vector<vec3f>& normals, vector<vec2f>& texcoords,
vector<obj_material*>& materials, vector<int>& ematerials,
vector<string>& materials, vector<int>& ematerials,
bool flip_texcoord = false);
bool has_quads(obj_shape* shape);

Expand Down Expand Up @@ -415,7 +415,7 @@ void set_fvquads(obj_shape* shape, const vector<vec4i>& quadspos,
const vector<vec3f>& positions, const vector<vec3f>& normals,
const vector<vec2f>& texcoords, const vector<int>& ematerials = {},
bool flip_texcoord = false);
void set_materials(obj_shape* shape, const vector<obj_material*>& materials);
void set_materials(obj_shape* shape, const vector<string>& materials);
void set_instances(obj_shape* shape, const vector<frame3f>& instances);

} // namespace yocto
Expand Down Expand Up @@ -492,7 +492,7 @@ struct pbrt_shape {
vector<vec2f> texcoords = {};
vector<vec3i> triangles = {};
// material
pbrt_material* material = nullptr;
string material = "";
};

// Pbrt lights
Expand Down
26 changes: 16 additions & 10 deletions libs/yocto/yocto_sceneio.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1894,6 +1894,10 @@ static bool load_obj_scene(const string& filename, sceneio_scene* scene,
error = filename + ": empty shape";
return false;
};
auto material_error = [filename, &error](const string& name) {
error = filename + ": missing material " + name;
return false;
};
auto dependent_error = [filename, &error]() {
error = filename + ": error in " + error;
return false;
Expand Down Expand Up @@ -1951,7 +1955,7 @@ static bool load_obj_scene(const string& filename, sceneio_scene* scene,
};

// handler for materials
auto material_map = unordered_map<obj_material*, sceneio_material*>{};
auto material_map = unordered_map<string, sceneio_material*>{};
for (auto omat : obj->materials) {
auto material = add_material(scene);
// material->name = make_safe_name("material", omat->name);
Expand Down Expand Up @@ -1980,7 +1984,7 @@ static bool load_obj_scene(const string& filename, sceneio_scene* scene,
material->opacity_tex = get_stexture(omat->pbr_opacity_tex);
material->normal_tex = get_ctexture(omat->normal_tex);
material->scattering_tex = get_ctexture(omat->pbr_volscattering_tex);
material_map[omat] = material;
material_map[omat->name] = material;
}

// convert shapes
Expand All @@ -1990,7 +1994,9 @@ static bool load_obj_scene(const string& filename, sceneio_scene* scene,
if (materials.empty()) materials.push_back(nullptr);
for (auto material_idx = 0; material_idx < materials.size();
material_idx++) {
auto shape = add_shape(scene);
auto shape = add_shape(scene);
if (material_map.find(materials[material_idx]) == material_map.end())
return material_error(materials[material_idx]);
auto material = material_map.at(materials[material_idx]);
auto has_quads_ = has_quads(oshape);
if (!oshape->faces.empty() && !has_quads_) {
Expand Down Expand Up @@ -2106,7 +2112,7 @@ static bool save_obj_scene(const string& filename, const sceneio_scene* scene,
};

// convert materials and textures
auto material_map = unordered_map<sceneio_material*, obj_material*>{
auto material_map = unordered_map<sceneio_material*, string>{
{nullptr, nullptr}};
for (auto material : scene->materials) {
auto omaterial = add_material(obj);
Expand All @@ -2132,7 +2138,7 @@ static bool save_obj_scene(const string& filename, const sceneio_scene* scene,
omaterial->pbr_coat_tex = get_texture(material->coat_tex);
omaterial->pbr_opacity_tex = get_texture(material->opacity_tex);
omaterial->pbr_normal_tex = get_texture(material->normal_tex);
material_map[material] = omaterial;
material_map[material] = omaterial->name;
}

// convert objects
Expand Down Expand Up @@ -2833,7 +2839,7 @@ static bool load_pbrt_scene(const string& filename, sceneio_scene* scene,
};

// convert material
auto material_map = unordered_map<pbrt_material*, sceneio_material*>{};
auto material_map = unordered_map<string, sceneio_material*>{};
for (auto pmaterial : pbrt->materials) {
auto material = add_material(scene);
material->emission = pmaterial->emission;
Expand All @@ -2849,11 +2855,11 @@ static bool load_pbrt_scene(const string& filename, sceneio_scene* scene,
material->opacity_tex = get_stexture(pmaterial->opacity_tex);
if (material->opacity_tex == nullptr)
material->opacity_tex = get_atexture(pmaterial->alpha_tex);
material_map[pmaterial] = material;
material_map[pmaterial->name] = material;
}

// hack for pbrt empty material
material_map[nullptr] = add_material(scene);
material_map[""] = add_material(scene);

// convert shapes
for (auto pshape : pbrt->shapes) {
Expand Down Expand Up @@ -2980,7 +2986,7 @@ static bool save_pbrt_scene(const string& filename, const sceneio_scene* scene,
};

// convert materials
auto material_map = unordered_map<sceneio_material*, pbrt_material*>{};
auto material_map = unordered_map<sceneio_material*, string>{};
for (auto material : scene->materials) {
auto pmaterial = add_material(pbrt);
pmaterial->name = path_basename(material->name);
Expand All @@ -2994,7 +3000,7 @@ static bool save_pbrt_scene(const string& filename, const sceneio_scene* scene,
pmaterial->opacity = material->opacity;
pmaterial->color_tex = get_texture(material->color_tex);
pmaterial->opacity_tex = get_texture(material->opacity_tex);
material_map[material] = pmaterial;
material_map[material] = pmaterial->name;
}

// convert instances
Expand Down
Loading

0 comments on commit 64593eb

Please sign in to comment.