import/importer.h
| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | // SPDX-License-Identifier: BSD-3-Clause | ||
| 2 | #pragma once | ||
| 3 | |||
| 4 | #include <cstdint> | ||
| 5 | #include <filesystem> | ||
| 6 | #include <functional> | ||
| 7 | #include <map> | ||
| 8 | #include <memory> | ||
| 9 | #include <optional> | ||
| 10 | #include <string> | ||
| 11 | #include <string_view> | ||
| 12 | #include <utility> | ||
| 13 | #include <variant> | ||
| 14 | #include <vector> | ||
| 15 | |||
| 16 | #include "chrono_batch_render/export.h" | ||
| 17 | #include "chrono_batch_render/scene/prim.h" | ||
| 18 | #include "chrono_batch_render/scene/scene.h" | ||
| 19 | #include "chrono_batch_render/util/error.h" | ||
| 20 | |||
| 21 | namespace chrono::batch_render { | ||
| 22 | |||
| 23 | 2 | class CHRONO_BATCH_RENDER_API CancelledError : public Error { | |
| 24 | public: | ||
| 25 | 4 | using Error::Error; | |
| 26 | ~CancelledError() override; | ||
| 27 | }; | ||
| 28 | |||
| 29 | 134 | struct CHRONO_BATCH_RENDER_API ImportProgress { | |
| 30 | std::string stage; | ||
| 31 | uint64_t done = 0; | ||
| 32 | uint64_t total = 0; | ||
| 33 | }; | ||
| 34 | |||
| 35 | using ImportProgressCallback = std::function<void(const ImportProgress& progress)>; | ||
| 36 | |||
| 37 | class ImportState; | ||
| 38 | |||
| 39 | enum class UpAxis : uint8_t { Y, Z }; | ||
| 40 | |||
| 41 | using ImportValue = std::variant<bool, int64_t, double, std::string>; | ||
| 42 | using ImportValues = std::map<std::string, ImportValue, std::less<>>; | ||
| 43 | |||
| 44 | struct CHRONO_BATCH_RENDER_API ImportOption { | ||
| 45 | std::string name; | ||
| 46 | std::string label; | ||
| 47 | std::string tip; | ||
| 48 | ImportValue value = false; | ||
| 49 | std::vector<std::string> choices; | ||
| 50 | }; | ||
| 51 | |||
| 52 | 46 | class CHRONO_BATCH_RENDER_API ImportContext { | |
| 53 | public: | ||
| 54 | ImportContext(); | ||
| 55 | explicit ImportContext(std::shared_ptr<ImportState> state); | ||
| 56 | |||
| 57 | void Report(std::string_view stage, uint64_t done, uint64_t total); | ||
| 58 | bool Cancelled() const; | ||
| 59 | std::optional<UpAxis> Up() const; | ||
| 60 | std::optional<float> MetersPerUnit() const; | ||
| 61 | std::optional<ImportValue> Option(std::string_view name) const; | ||
| 62 | |||
| 63 | private: | ||
| 64 | std::shared_ptr<ImportState> m_state; | ||
| 65 | }; | ||
| 66 | |||
| 67 | struct CHRONO_BATCH_RENDER_API ImportSource { | ||
| 68 | std::filesystem::path file; | ||
| 69 | std::map<std::string, FileBytes, std::less<>> files; | ||
| 70 | |||
| 71 | std::filesystem::path Locate(std::string_view name) const; | ||
| 72 | FileBytes Find(std::string_view name) const; | ||
| 73 | FileBytes Read(std::string_view name) const; | ||
| 74 | FileBytes ReadEntry() const; | ||
| 75 | }; | ||
| 76 | |||
| 77 | 46 | class CHRONO_BATCH_RENDER_API ImportPlan { | |
| 78 | public: | ||
| 79 | using DefineStep = std::function<void(Scene& scene, std::string_view path)>; | ||
| 80 | |||
| 81 | 35 | template <class T> void Define(std::string path, typename T::Desc desc = {}) { | |
| 82 | 74 | Add(std::move(path), [desc = std::move(desc)](Scene& scene, std::string_view at) { | |
| 83 | 2 | scene.Define<T>(at, desc); | |
| 84 | }); | ||
| 85 | 35 | } | |
| 86 | |||
| 87 | void Add(std::string path, DefineStep define); | ||
| 88 | std::string AddFile(std::string_view name, FileBytes bytes); | ||
| 89 | uint32_t PrimCount() const; | ||
| 90 | Prim Apply(Scene& scene, std::string_view root) const; | ||
| 91 | |||
| 92 | private: | ||
| 93 | 159 | struct Step { | |
| 94 | std::string path; | ||
| 95 | DefineStep define; | ||
| 96 | }; | ||
| 97 | |||
| 98 | std::vector<Step> m_steps; | ||
| 99 | std::vector<std::pair<std::string, FileBytes>> m_files; | ||
| 100 | uint64_t m_id = 0; | ||
| 101 | }; | ||
| 102 | |||
| 103 | 14 | class CHRONO_BATCH_RENDER_API Importer { | |
| 104 | public: | ||
| 105 | virtual ~Importer(); | ||
| 106 | |||
| 107 | virtual std::string Name() const = 0; | ||
| 108 | virtual std::vector<std::string> Extensions() const = 0; | ||
| 109 | virtual std::vector<ImportOption> Options() const; | ||
| 110 | virtual ImportPlan Read(std::string_view path, const ImportSource& source, | ||
| 111 | ImportContext& context) = 0; | ||
| 112 | }; | ||
| 113 | |||
| 114 | 17 | struct CHRONO_BATCH_RENDER_API ImportReader { | |
| 115 | std::string name; | ||
| 116 | std::function<ImportPlan(ImportContext& context)> read; | ||
| 117 | }; | ||
| 118 | |||
| 119 | struct CHRONO_BATCH_RENDER_API ImportDesc { | ||
| 120 | std::shared_ptr<Importer> importer; | ||
| 121 | ImportProgressCallback progress; | ||
| 122 | bool synchronous = false; | ||
| 123 | std::optional<UpAxis> up; | ||
| 124 | std::optional<float> metersPerUnit; | ||
| 125 | ImportValues options; | ||
| 126 | }; | ||
| 127 | |||
| 128 | 6 | class CHRONO_BATCH_RENDER_API ImportJob { | |
| 129 | public: | ||
| 130 | ImportJob() = default; | ||
| 131 | |||
| 132 | bool Ready() const; | ||
| 133 | ImportProgress Progress() const; | ||
| 134 | void Cancel(); | ||
| 135 | Prim Wait(); | ||
| 136 | |||
| 137 | private: | ||
| 138 | friend class Scene; | ||
| 139 | |||
| 140 | 15 | ImportJob(Ref<Scene> scene, std::shared_ptr<ImportState> state) | |
| 141 | 15 | : m_scene(std::move(scene)), m_state(std::move(state)) {} | |
| 142 | |||
| 143 | Ref<Scene> m_scene; | ||
| 144 | std::shared_ptr<ImportState> m_state; | ||
| 145 | }; | ||
| 146 | |||
| 147 | CHRONO_BATCH_RENDER_API void RegisterImporter(std::shared_ptr<Importer> importer); | ||
| 148 | CHRONO_BATCH_RENDER_API void UnregisterImporter(std::string_view extension, | ||
| 149 | std::string_view name = {}); | ||
| 150 | CHRONO_BATCH_RENDER_API std::shared_ptr<Importer> FindImporter(std::string_view extension); | ||
| 151 | CHRONO_BATCH_RENDER_API std::vector<std::shared_ptr<Importer>> | ||
| 152 | FindImporters(std::string_view extension); | ||
| 153 | CHRONO_BATCH_RENDER_API std::vector<std::string> ImporterExtensions(); | ||
| 154 | |||
| 155 | } | ||
| 156 |