From da0b64f1f4b168a3307c067aede8bc145219aa2e Mon Sep 17 00:00:00 2001 From: Syoyo Fujita Date: Sat, 10 Oct 2020 18:08:38 +0900 Subject: [PATCH 1/2] Remove lfpAlloc dependency from public API and public struct definition. Suppress clang warnings. --- experimental/tinyobj_loader_opt.h | 68 +++++++++++++++---------------- experimental/viewer.cc | 4 +- 2 files changed, 35 insertions(+), 37 deletions(-) diff --git a/experimental/tinyobj_loader_opt.h b/experimental/tinyobj_loader_opt.h index d2b007db..45c82d9e 100644 --- a/experimental/tinyobj_loader_opt.h +++ b/experimental/tinyobj_loader_opt.h @@ -6,7 +6,7 @@ /* The MIT License (MIT) -Copyright (c) 2012-2017 Syoyo Fujita and many contributors. +Copyright (c) 2012-2020 Syoyo Fujita and many contributors. Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal @@ -54,8 +54,6 @@ THE SOFTWARE. #include // C++11 #include // C++11 -#include "lfpAlloc/Allocator.hpp" - namespace tinyobj_opt { // ---------------------------------------------------------------------------- @@ -330,19 +328,19 @@ struct index_t { }; typedef struct { - std::vector > vertices; - std::vector > normals; - std::vector > texcoords; - std::vector > indices; + std::vector vertices; + std::vector normals; + std::vector texcoords; + std::vector indices; // # of vertices for each face. // 3 for triangle, 4 for qual, ... // If triangulation is enabled and the original face are quad, // face_num_verts will be 6(3 + 3) - std::vector > face_num_verts; + std::vector face_num_verts; // Per-face material IDs. - std::vector > material_ids; + std::vector material_ids; } attrib_t; typedef StackVector ShortString; @@ -980,6 +978,33 @@ static void LoadMtl(std::map *material_map, materials->push_back(material); } + +class LoadOption { + public: + LoadOption() : req_num_threads(-1), triangulate(true), verbose(false) {} + + int req_num_threads; + bool triangulate; + bool verbose; +}; + +/// Parse wavefront .obj(.obj string data is expanded to linear char array +/// `buf') +/// -1 to req_num_threads use the number of HW threads in the running system. +bool parseObj(attrib_t *attrib, std::vector *shapes, + std::vector *materials, const char *buf, size_t len, + const LoadOption &option); + +} // namespace tinyobj_opt + +#endif // TINOBJ_LOADER_OPT_H_ + +#ifdef TINYOBJ_LOADER_OPT_IMPLEMENTATION + +#include "lfpAlloc/Allocator.hpp" + +namespace tinyobj_opt { + typedef enum { COMMAND_EMPTY, COMMAND_V, @@ -1000,7 +1025,6 @@ typedef struct { // for f std::vector > f; - // std::vector f; std::vector > f_num_verts; const char *group_name; @@ -1031,30 +1055,6 @@ struct CommandCount { } }; -class LoadOption { - public: - LoadOption() : req_num_threads(-1), triangulate(true), verbose(false) {} - - int req_num_threads; - bool triangulate; - bool verbose; -}; - -/// Parse wavefront .obj(.obj string data is expanded to linear char array -/// `buf') -/// -1 to req_num_threads use the number of HW threads in the running system. -bool parseObj(attrib_t *attrib, std::vector *shapes, - std::vector *materials, const char *buf, size_t len, - const LoadOption &option); - -} // namespace tinyobj_opt - -#endif // TINOBJ_LOADER_OPT_H_ - -#ifdef TINYOBJ_LOADER_OPT_IMPLEMENTATION - -namespace tinyobj_opt { - static bool parseLine(Command *command, const char *p, size_t p_len, bool triangulate = true) { // @todo { operate directly on pointer `p'. to do that, add range check for diff --git a/experimental/viewer.cc b/experimental/viewer.cc index 4886b784..957b8bd5 100644 --- a/experimental/viewer.cc +++ b/experimental/viewer.cc @@ -650,13 +650,11 @@ int main(int argc, char **argv) if (data == nullptr) { printf("failed to load file\n"); exit(-1); - return false; } if (data_len < 4) { printf("Empty file\n"); exit(-1); - return false; } printf("filesize: %d\n", (int)data_len); tinyobj_opt::LoadOption option; @@ -665,7 +663,7 @@ int main(int argc, char **argv) bool ret = parseObj(&attrib, &shapes, &materials, data, data_len, option); - return ret; + return ret ? 0 : -1; } Init(); From 1369a85b5ac6da5158ff6058b8b10ab50f446782 Mon Sep 17 00:00:00 2001 From: Syoyo Fujita Date: Tue, 5 Jan 2021 02:20:30 +0900 Subject: [PATCH 2/2] Introduce TINYOBJ_LOADER_OPT_USE_LFPALLOC to enable/disable lfpAlloc. --- experimental/tinyobj_loader_opt.h | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) diff --git a/experimental/tinyobj_loader_opt.h b/experimental/tinyobj_loader_opt.h index 45c82d9e..4958c7df 100644 --- a/experimental/tinyobj_loader_opt.h +++ b/experimental/tinyobj_loader_opt.h @@ -1001,7 +1001,18 @@ bool parseObj(attrib_t *attrib, std::vector *shapes, #ifdef TINYOBJ_LOADER_OPT_IMPLEMENTATION +// uncoment to use STL default allocator +#define TINYOBJ_LOADER_OPT_USE_LFPALLOC + +#ifdef TINYOBJ_LOADER_OPT_USE_LFPALLOC #include "lfpAlloc/Allocator.hpp" +template +using allocator = lfpAlloc::lfpAllocator; +#else +template +using allocator = std::allocator; +#endif + namespace tinyobj_opt { @@ -1024,8 +1035,8 @@ typedef struct { float tx, ty; // for f - std::vector > f; - std::vector > f_num_verts; + std::vector > f; + std::vector > f_num_verts; const char *group_name; unsigned int group_name_len; @@ -1279,7 +1290,7 @@ bool parseObj(attrib_t *attrib, std::vector *shapes, auto t1 = std::chrono::high_resolution_clock::now(); - std::vector > + std::vector > line_infos[kMaxThreads]; for (size_t t = 0; t < static_cast(num_threads); t++) { // Pre allocate enough memory. len / 128 / num_threads is just a heuristic