tinypbrt 1.1.1
pbrt-v4 scene file parser for C11 with a header-only C++20 wrapper
Loading...
Searching...
No Matches
scene.hpp
Go to the documentation of this file.
1#pragma once
2#ifndef _TINYPBRT_SCENE_HPP_
3#define _TINYPBRT_SCENE_HPP_
4
6
8
9#include <memory>
10#include <string>
11#include <tuple>
12#include <utility>
13
31
32namespace tpbrt {
44 struct scene_view : detail::wrapper<scene_view, tpbrt_scene_t> {
46 using wrapper::wrapper;
47
49 [[nodiscard]] constexpr tpbrt::options options() const noexcept { return _native->options; }
50
52 [[nodiscard]] constexpr tpbrt::camera camera() const noexcept { return _native->camera; }
53
55 [[nodiscard]] constexpr tpbrt::film film() const noexcept { return _native->film; }
56
58 [[nodiscard]] constexpr tpbrt::integrator integrator() const noexcept { return _native->integrator; }
59
61 [[nodiscard]] constexpr tpbrt::accelerator accelerator() const noexcept { return _native->accelerator; }
62
64 [[nodiscard]] constexpr tpbrt::sampler sampler() const noexcept { return _native->sampler; }
65
67 [[nodiscard]] constexpr float_t start_time() const noexcept { return _native->start_time; }
68
70 [[nodiscard]] constexpr tpbrt::filter filter() const noexcept { return _native->filter; }
71
73 [[nodiscard]] constexpr float_t end_time() const noexcept { return _native->end_time; }
74
76 [[nodiscard]] constexpr tpbrt::textures_list textures() const noexcept { return _native->textures; }
77
79 [[nodiscard]] constexpr tpbrt::materials_list materials() const noexcept { return _native->materials; }
80
82 [[nodiscard]] constexpr tpbrt::medias_list medias() const noexcept { return _native->medias; }
83
85 [[nodiscard]] constexpr tpbrt::lights_list lights() const noexcept { return _native->lights; }
86
88 [[nodiscard]] constexpr tpbrt::shapes_list shapes() const noexcept { return _native->shapes; }
89
91 [[nodiscard]] constexpr tpbrt::objects_list objects() const noexcept { return _native->objects; }
92
94 [[nodiscard]] constexpr tpbrt::objects_instances_list instances() const noexcept { return _native->instances; }
95
97 [[nodiscard]] constexpr tpbrt::transforms_list transforms() const noexcept { return _native->transforms; }
98
100 [[nodiscard]] constexpr auto reflect() const noexcept {
101 return std::make_tuple(options(), camera(), film(), integrator(), accelerator(), sampler(), start_time(), filter(),
103 }
104 };
105
112 class scene : public scene_view {
114 struct deleter {
116 void operator()(tpbrt_scene_t* const c_scene) const noexcept {
117 tpbrt_scene_free(c_scene);
118 delete c_scene; // NOLINT(cppcoreguidelines-owning-memory)
119 }
120 };
121
123 std::unique_ptr<tpbrt_scene_t, deleter> _owned;
124
126 explicit scene(std::unique_ptr<tpbrt_scene_t, deleter> owned) noexcept : scene_view(*owned), _owned(std::move(owned)) {}
127
129 template<class Loader>
130 [[nodiscard]] static result<scene> _load(const Loader& loader) {
131 auto owned = std::unique_ptr<tpbrt_scene_t, deleter>(new tpbrt_scene_t {}); // NOLINT
132 // on error the C loader already freed everything it allocated, only the struct itself is left
133 if (const auto err = static_cast<error>(loader(owned.get())); err != error::NONE) {
134 delete owned.release(); // NOLINT(cppcoreguidelines-owning-memory)
135 return err;
136 }
137 return scene(std::move(owned));
138 }
139
140 public:
142 scene(scene&& other) noexcept = default;
143
145 scene& operator=(scene&& other) noexcept = default;
146
148 scene(const scene&) = delete;
149
151 scene& operator=(const scene&) = delete;
152
154 ~scene() = default;
155
161 [[nodiscard]] static result<scene> load_from_file(const std::string& filepath) {
162 return _load([&](tpbrt_scene_t* const out) { return tpbrt_scene_load_from_file(filepath.c_str(), out); });
163 }
164
171 [[nodiscard]] static result<scene> load_from_memory(const std::string& data, const std::string& working_directory = ".") {
172 return _load([&](tpbrt_scene_t* const out) {
173 return tpbrt_scene_load_from_memory(data.c_str(), working_directory.c_str(), out);
174 });
175 }
176
183 [[nodiscard]] static result<scene> load_from_memory(const char_t* const data,
184 const std::string& working_directory = ".") {
185 if (data == nullptr) { return error::INVALID_POINTER; }
186 return _load([&](tpbrt_scene_t* const out) {
187 return tpbrt_scene_load_from_memory(data, working_directory.c_str(), out);
188 });
189 }
190 };
191
198 [[nodiscard]] inline result<scene> load_scene_from_file(const std::string& filepath) {
199 return scene::load_from_file(filepath);
200 }
201
209 [[nodiscard]] inline result<scene> load_scene_from_memory(const std::string& data,
210 const std::string& working_directory = ".") {
211 return scene::load_from_memory(data, working_directory);
212 }
213
221 [[nodiscard]] inline result<scene> load_scene_from_memory(const char_t* const data,
222 const std::string& working_directory = ".") {
223 return scene::load_from_memory(data, working_directory);
224 }
225} // namespace tpbrt
226#endif // _TINYPBRT_SCENE_HPP_
Scene loaded from a pbrt-v4 file or string, owning all its memory (move only, freed in the destructor...
Definition scene.hpp:112
scene & operator=(scene &&other) noexcept=default
Moves scene (the scene held before is freed).
scene(scene &&other) noexcept=default
Moves scene (views created from other stay valid, they point to the same heap memory).
~scene()=default
Frees the scene.
scene & operator=(const scene &)=delete
Scene owns C memory, so it cannot be copied.
static result< scene > load_from_file(const std::string &filepath)
Loads scene from given file.
Definition scene.hpp:161
static result< scene > load_from_memory(const char_t *const data, const std::string &working_directory=".")
Loads scene from memory (provided null-terminated string).
Definition scene.hpp:183
scene(const scene &)=delete
Scene owns C memory, so it cannot be copied.
static result< scene > load_from_memory(const std::string &data, const std::string &working_directory=".")
Loads scene from memory (provided string).
Definition scene.hpp:171
tpbrt_char_t char_t
Char type.
Definition fwd.hpp:29
error
Error type (used as result type where success = TPBRT_ERROR_NONE = 0).
Definition error.hpp:25
@ INVALID_POINTER
Provided NULL where it is not allowed.
@ NONE
No error -> Success.
tpbrt_float_t float_t
Float type.
Definition fwd.hpp:97
result< scene > load_scene_from_memory(const std::string &data, const std::string &working_directory=".")
Loads scene from memory (provided string).
Definition scene.hpp:209
result< scene > load_scene_from_file(const std::string &filepath)
Loads scene from given file.
Definition scene.hpp:198
tpbrt_error_t tpbrt_scene_load_from_memory(const tpbrt_char_t *data, const tpbrt_char_t *working_directory, tpbrt_scene_t *out_scene)
Loads scene from memory (provided string).
void tpbrt_scene_free(tpbrt_scene_t *scene)
Free all scene resources.
tpbrt_error_t tpbrt_scene_load_from_file(const tpbrt_char_t *filepath, tpbrt_scene_t *out_scene)
Loads scene from given file.
Definition accelerator.hpp:14
C++ wrapper for tpbrt_accelerator_t.
Definition accelerator.hpp:138
C++ wrapper for tpbrt_camera_t.
Definition camera.hpp:313
C++ wrapper for tpbrt_film_t.
Definition film.hpp:170
C++ wrapper for tpbrt_filter_t.
Definition filter.hpp:108
C++ wrapper for tpbrt_integrator_t.
Definition integrator.hpp:336
C++ wrapper for tpbrt_lights_list_t.
Definition light.hpp:460
C++ wrapper for tpbrt_materials_list_t.
Definition material.hpp:907
C++ wrapper for tpbrt_medias_list_t.
Definition media.hpp:475
C++ wrapper for tpbrt_objects_instances_list_t.
Definition object_instance.hpp:40
C++ wrapper for tpbrt_objects_list_t.
Definition object.hpp:65
C++ wrapper for tpbrt_options_t.
Definition options.hpp:22
Result of an operation: either a value or an error code (similar to C++23 std::expected).
Definition error.hpp:165
C++ wrapper for tpbrt_sampler_t.
Definition sampler.hpp:215
C++ wrapper for tpbrt_scene_t (a non owning view, e.g.
Definition scene.hpp:44
constexpr tpbrt::options options() const noexcept
Returns options value.
Definition scene.hpp:49
constexpr tpbrt::lights_list lights() const noexcept
Returns lights value.
Definition scene.hpp:85
constexpr tpbrt::film film() const noexcept
Returns film value.
Definition scene.hpp:55
constexpr float_t end_time() const noexcept
Returns end_time value.
Definition scene.hpp:73
constexpr tpbrt::materials_list materials() const noexcept
Returns materials value.
Definition scene.hpp:79
constexpr tpbrt::transforms_list transforms() const noexcept
Returns transforms value.
Definition scene.hpp:97
constexpr tpbrt::medias_list medias() const noexcept
Returns medias value.
Definition scene.hpp:82
constexpr tpbrt::objects_instances_list instances() const noexcept
Returns instances value.
Definition scene.hpp:94
constexpr tpbrt::sampler sampler() const noexcept
Returns sampler value.
Definition scene.hpp:64
constexpr tpbrt::camera camera() const noexcept
Returns camera value.
Definition scene.hpp:52
constexpr auto reflect() const noexcept
Conversion to tuple.
Definition scene.hpp:100
constexpr float_t start_time() const noexcept
Returns start_time value.
Definition scene.hpp:67
constexpr tpbrt::filter filter() const noexcept
Returns filter value.
Definition scene.hpp:70
constexpr tpbrt::integrator integrator() const noexcept
Returns integrator value.
Definition scene.hpp:58
constexpr tpbrt::accelerator accelerator() const noexcept
Returns accelerator value.
Definition scene.hpp:61
constexpr tpbrt::shapes_list shapes() const noexcept
Returns shapes value.
Definition scene.hpp:88
constexpr tpbrt::objects_list objects() const noexcept
Returns objects value.
Definition scene.hpp:91
constexpr tpbrt::textures_list textures() const noexcept
Returns textures value.
Definition scene.hpp:76
C++ wrapper for tpbrt_shapes_list_t.
Definition shape.hpp:511
C++ wrapper for tpbrt_textures_list_t.
Definition texture.hpp:933
C++ wrapper for tpbrt_transforms_list_t.
Definition transform.hpp:59
PBRT scene representation.
Definition scene.h:38