Skip to content

Commit 345fa57

Browse files
committed
Update README.
Seach .mtl file from same directory of .obj file by default. Fixes #208
1 parent e871e97 commit 345fa57

File tree

3 files changed

+55
-22
lines changed

3 files changed

+55
-22
lines changed

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ Old version is available as `v0.9.x` branch https://github.com/syoyo/tinyobjload
2626

2727
## What's new
2828

29+
* 18 May, 2019 : Python binding!(See `python` folder. Also see https://pypi.org/project/tinyobjloader/)
2930
* 14 Apr, 2019 : Bump version v2.0.0 rc0. New C++ API and python bindings!(1.x API still exists for backward compatibility)
3031
* 20 Aug, 2016 : Bump version v1.0.0. New data structure and API!
3132

@@ -100,6 +101,8 @@ TinyObjLoader is successfully used in ...
100101
* Callback API for custom loading.
101102
* Double precision support(for HPC application).
102103
* Smoothing group
104+
* Python binding : See `python` folder.
105+
* Precompiled binary(manylinux1-x86_64 only) is hosted at pypi https://pypi.org/project/tinyobjloader/)
103106

104107
### Primitives
105108

tests/tester.cc

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1109,6 +1109,18 @@ TEST_CASE("leading-decimal-dots", "[Issue201]") {
11091109
REQUIRE(.940448 == Approx(attrib.vertices[6]));
11101110
}
11111111

1112+
TEST_CASE("mtl-default-search-path-v2-API", "[Issue208]") {
1113+
1114+
tinyobj::ObjReader reader;
1115+
1116+
bool ret = reader.ParseFromFile("../models/cornell_box.obj");
1117+
1118+
std::cout << "WARN: " << reader.Warning() << "\n";
1119+
1120+
REQUIRE(ret == true);
1121+
REQUIRE(reader.Warning().empty());
1122+
}
1123+
11121124
// Fuzzer test.
11131125
// Just check if it does not crash.
11141126
// Disable by default since Windows filesystem can't create filename of afl

tiny_obj_loader.h

Lines changed: 40 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -298,12 +298,13 @@ typedef struct {
298298
}
299299

300300
std::string GetCustomParameter(const std::string &key) {
301-
std::map<std::string, std::string>::const_iterator it = unknown_parameter.find(key);
301+
std::map<std::string, std::string>::const_iterator it =
302+
unknown_parameter.find(key);
302303

303-
if (it != unknown_parameter.end()) {
304-
return it->second;
305-
}
306-
return std::string();
304+
if (it != unknown_parameter.end()) {
305+
return it->second;
306+
}
307+
return std::string();
307308
}
308309

309310
#endif
@@ -344,7 +345,7 @@ typedef struct {
344345

345346
typedef struct {
346347
// Linear flattened indices.
347-
std::vector<index_t> indices; // indices for vertices(poly lines)
348+
std::vector<index_t> indices; // indices for vertices(poly lines)
348349
std::vector<int> num_line_vertices; // The number of vertices per line.
349350
} lines_t;
350351

@@ -475,13 +476,12 @@ struct ObjReaderConfig {
475476

476477
///
477478
/// Search path to .mtl file.
478-
/// Default = search from same directory of .obj file.
479+
/// Default = "" = search from the same directory of .obj file.
479480
/// Valid only when loading .obj from a file.
480481
///
481482
std::string mtl_search_path;
482483

483-
ObjReaderConfig()
484-
: triangulate(true), vertex_color(true), mtl_search_path("./") {}
484+
ObjReaderConfig() : triangulate(true), vertex_color(true) {}
485485
};
486486

487487
///
@@ -498,7 +498,8 @@ class ObjReader {
498498
/// @param[in] filename wavefront .obj filename
499499
/// @param[in] config Reader configuration
500500
///
501-
bool ParseFromFile(const std::string &filename, const ObjReaderConfig &config = ObjReaderConfig());
501+
bool ParseFromFile(const std::string &filename,
502+
const ObjReaderConfig &config = ObjReaderConfig());
502503

503504
///
504505
/// Parse .obj from a text string.
@@ -509,7 +510,8 @@ class ObjReader {
509510
/// @param[in] mtl_text wavefront .mtl filename
510511
/// @param[in] config Reader configuration
511512
///
512-
bool ParseFromString(const std::string &obj_text, const std::string &mtl_text, const ObjReaderConfig &config = ObjReaderConfig());
513+
bool ParseFromString(const std::string &obj_text, const std::string &mtl_text,
514+
const ObjReaderConfig &config = ObjReaderConfig());
513515

514516
///
515517
/// .obj was loaded or parsed correctly.
@@ -1583,8 +1585,8 @@ static bool exportGroupsToShape(shape_t *shape, const PrimGroup &prim_group,
15831585
if (!prim_group.lineGroup.empty()) {
15841586
// Flatten indices
15851587
for (size_t i = 0; i < prim_group.lineGroup.size(); i++) {
1586-
for (size_t j = 0; j < prim_group.lineGroup[i].vertex_indices.size(); j++) {
1587-
1588+
for (size_t j = 0; j < prim_group.lineGroup[i].vertex_indices.size();
1589+
j++) {
15881590
const vertex_index_t &vi = prim_group.lineGroup[i].vertex_indices[j];
15891591

15901592
index_t idx;
@@ -1595,16 +1597,17 @@ static bool exportGroupsToShape(shape_t *shape, const PrimGroup &prim_group,
15951597
shape->lines.indices.push_back(idx);
15961598
}
15971599

1598-
shape->lines.num_line_vertices.push_back(int(prim_group.lineGroup[i].vertex_indices.size()));
1600+
shape->lines.num_line_vertices.push_back(
1601+
int(prim_group.lineGroup[i].vertex_indices.size()));
15991602
}
16001603
}
16011604

16021605
// points
16031606
if (!prim_group.pointsGroup.empty()) {
16041607
// Flatten & convert indices
16051608
for (size_t i = 0; i < prim_group.pointsGroup.size(); i++) {
1606-
for (size_t j = 0; j < prim_group.pointsGroup[i].vertex_indices.size(); j++) {
1607-
1609+
for (size_t j = 0; j < prim_group.pointsGroup[i].vertex_indices.size();
1610+
j++) {
16081611
const vertex_index_t &vi = prim_group.pointsGroup[i].vertex_indices[j];
16091612

16101613
index_t idx;
@@ -2209,7 +2212,8 @@ bool LoadObj(attrib_t *attrib, std::vector<shape_t> *shapes,
22092212
static_cast<int>(vt.size() / 2), &vi)) {
22102213
if (err) {
22112214
std::stringstream ss;
2212-
ss << "Failed parse `l' line(e.g. zero value for vertex index. line "
2215+
ss << "Failed parse `l' line(e.g. zero value for vertex index. "
2216+
"line "
22132217
<< line_num << ".)\n";
22142218
(*err) += ss.str();
22152219
}
@@ -2240,7 +2244,8 @@ bool LoadObj(attrib_t *attrib, std::vector<shape_t> *shapes,
22402244
static_cast<int>(vt.size() / 2), &vi)) {
22412245
if (err) {
22422246
std::stringstream ss;
2243-
ss << "Failed parse `p' line(e.g. zero value for vertex index. line "
2247+
ss << "Failed parse `p' line(e.g. zero value for vertex index. "
2248+
"line "
22442249
<< line_num << ".)\n";
22452250
(*err) += ss.str();
22462251
}
@@ -2876,16 +2881,29 @@ bool LoadObjWithCallback(std::istream &inStream, const callback_t &callback,
28762881
}
28772882

28782883
bool ObjReader::ParseFromFile(const std::string &filename,
2879-
const ObjReaderConfig &config) {
2884+
const ObjReaderConfig &config) {
2885+
std::string mtl_search_path;
2886+
2887+
if (config.mtl_search_path.empty()) {
2888+
//
2889+
// split at last '\'(for unixish system) or '\\'(for windows) to get
2890+
// the base directory of .obj file
2891+
//
2892+
if (filename.find_last_of("/\\") != std::string::npos) {
2893+
mtl_search_path = filename.substr(0, filename.find_last_of("/\\"));
2894+
}
2895+
}
2896+
28802897
valid_ = LoadObj(&attrib_, &shapes_, &materials_, &warning_, &error_,
2881-
filename.c_str(), config.mtl_search_path.c_str(),
2898+
filename.c_str(), mtl_search_path.c_str(),
28822899
config.triangulate, config.vertex_color);
28832900

28842901
return valid_;
28852902
}
28862903

2887-
bool ObjReader::ParseFromString(const std::string &obj_text, const std::string &mtl_text,
2888-
const ObjReaderConfig &config) {
2904+
bool ObjReader::ParseFromString(const std::string &obj_text,
2905+
const std::string &mtl_text,
2906+
const ObjReaderConfig &config) {
28892907
std::stringbuf obj_buf(obj_text);
28902908
std::stringbuf mtl_buf(mtl_text);
28912909

0 commit comments

Comments
 (0)