chrono_batch_render/
Coverage:
low: ≥ 0%
medium: ≥ 75.0%
high: ≥ 90.0%
Lines:
39 / 39
100.0%
Branches:
20 / 40
50.0%
Functions:
6 / 6, 3 excl
100.0%

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
1/2
✗ Branch 1 not taken.
✓ Branch 5 taken 55 times.
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
4/19
<unknown function 23>:
✓ Branch 0 taken 13 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 5 times.
✗ Branch 3 not taken.
<unknown function 29>:
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✓ Branch 4 taken 5 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 5 times.
✗ Branch 7 not taken.
✗ Branch 8 not taken.
✗ Branch 9 not taken.
✗ Branch 11 not taken.
✗ Branch 13 not taken.
✗ Branch 14 not taken.
✗ Branch 15 not taken.
✗ Branch 16 not taken.
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
1/2
✓ Branch 4 taken 13 times.
✗ Branch 5 not taken.
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
12/13
chrono::batch_render::ImportPlan::Define<chrono::batch_render::Xform>(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, chrono::batch_render::Xform::Desc)::{lambda(chrono::batch_render::Scene&, std::basic_string_view<char, std::char_traits<char> >)#1}::operator()(chrono::batch_render::Scene&, std::basic_string_view<char, std::char_traits<char> >) const:
✗ Branch 1 not taken.
✓ Branch 8 taken 2 times.
✓ Branch 12 taken 2 times.
void chrono::batch_render::ImportPlan::Define<chrono::batch_render::Cube>(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, chrono::batch_render::Cube::Desc):
✓ Branch 2 taken 3 times.
✓ Branch 6 taken 3 times.
void chrono::batch_render::ImportPlan::Define<chrono::batch_render::GeomSubset>(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, chrono::batch_render::GeomSubset::Desc):
✓ Branch 2 taken 2 times.
✓ Branch 6 taken 2 times.
void chrono::batch_render::ImportPlan::Define<chrono::batch_render::Mesh>(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, chrono::batch_render::Mesh::Desc):
✓ Branch 2 taken 13 times.
✓ Branch 6 taken 13 times.
void chrono::batch_render::ImportPlan::Define<chrono::batch_render::Scope>(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, chrono::batch_render::Scope::Desc):
✓ Branch 2 taken 5 times.
✓ Branch 6 taken 5 times.
void chrono::batch_render::ImportPlan::Define<chrono::batch_render::Xform>(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, chrono::batch_render::Xform::Desc):
✓ Branch 2 taken 10 times.
✓ Branch 6 taken 10 times.
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
2/4
✗ Branch 0 not taken.
✓ Branch 1 taken 13 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 2 times.
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