tinypbrt 1.1.1
pbrt-v4 scene file parser for C11 with a header-only C++20 wrapper
Loading...
Searching...
No Matches
math.hpp
Go to the documentation of this file.
1#pragma once
2#ifndef _TINYPBRT_MATH_HPP_
3#define _TINYPBRT_MATH_HPP_
4
6
8
9#include <algorithm>
10#include <optional>
11
13
14namespace tpbrt {
24 struct opt_float : detail::wrapper<opt_float, tpbrt_opt_float_t> {
26 using optional_type = std::optional<float_t>;
27
29 using wrapper::wrapper;
30
32 [[nodiscard]] constexpr float_t value() const noexcept { return _native->value; }
33
35 [[nodiscard]] constexpr bool_t has_value() const noexcept { return _native->has_value == True; }
36
38 [[nodiscard]] constexpr optional_type to_optional() const noexcept {
39 if (!has_value()) { return std::nullopt; }
40 return value();
41 }
42
44 [[nodiscard]] constexpr auto reflect() const noexcept { return std::make_tuple(value(), has_value()); }
45 };
46
51 struct bool_array : array<tpbrt_bool_array_t, bool_t> {
53 using array::array;
54 };
55
60 struct int_array : array<tpbrt_int_array_t, int_t> {
62 using array::array;
63 };
64
69 struct uint_array : array<tpbrt_uint_array_t, uint_t> {
71 using array::array;
72 };
73
78 struct float_array : array<tpbrt_float_array_t, float_t> {
80 using array::array;
81 };
82
87 template<class NativeVec>
88 struct vec_base : array<NativeVec, float_t, &NativeVec::v> {
91
93 using array<NativeVec, float_t, &NativeVec::v>::array;
94
95 protected:
97 using array<NativeVec, float_t, &NativeVec::v>::_native;
98
99 public:
101 [[nodiscard]] static constexpr size_t length() noexcept { return Length; }
102 };
103
108 struct vec2 : vec_base<tpbrt_vec2_t> {
110 using vec_base::vec_base;
111
113 [[nodiscard]] constexpr float_t x() const noexcept { return _native->x; }
114
116 [[nodiscard]] constexpr float_t r() const noexcept { return _native->r; }
117
119 [[nodiscard]] constexpr float_t y() const noexcept { return _native->y; }
120
122 [[nodiscard]] constexpr float_t g() const noexcept { return _native->g; }
123
125 [[nodiscard]] constexpr auto reflect() const noexcept { return std::make_tuple(x(), y()); }
126 };
127
132 struct vec3 : vec_base<tpbrt_vec3_t> {
134 using vec_base::vec_base;
135
137 [[nodiscard]] constexpr float_t x() const noexcept { return _native->x; }
138
140 [[nodiscard]] constexpr float_t r() const noexcept { return _native->r; }
141
143 [[nodiscard]] constexpr float_t y() const noexcept { return _native->y; }
144
146 [[nodiscard]] constexpr float_t g() const noexcept { return _native->g; }
147
149 [[nodiscard]] constexpr float_t z() const noexcept { return _native->z; }
150
152 [[nodiscard]] constexpr float_t b() const noexcept { return _native->b; }
153
155 [[nodiscard]] constexpr auto reflect() const noexcept { return std::make_tuple(x(), y(), z()); }
156 };
157
162 struct vec4 : vec_base<tpbrt_vec4_t> {
164 using vec_base::vec_base;
165
167 [[nodiscard]] constexpr float_t x() const noexcept { return _native->x; }
168
170 [[nodiscard]] constexpr float_t r() const noexcept { return _native->r; }
171
173 [[nodiscard]] constexpr float_t y() const noexcept { return _native->y; }
174
176 [[nodiscard]] constexpr float_t g() const noexcept { return _native->g; }
177
179 [[nodiscard]] constexpr float_t z() const noexcept { return _native->z; }
180
182 [[nodiscard]] constexpr float_t b() const noexcept { return _native->b; }
183
185 [[nodiscard]] constexpr float_t w() const noexcept { return _native->w; }
186
188 [[nodiscard]] constexpr float_t a() const noexcept { return _native->a; }
189
191 [[nodiscard]] constexpr auto reflect() const noexcept { return std::make_tuple(x(), y(), z(), w()); }
192 };
193
198 struct vec2_array : array<tpbrt_vec2_array_t, vec2> {
200 using array::array;
201 };
202
207 struct vec3_array : array<tpbrt_vec3_array_t, vec3> {
209 using array::array;
210 };
211
216 struct mat4 : array<tpbrt_mat4_t, vec4, &tpbrt_mat4_t::rows> {
218 static constexpr size_t RowLength = 4u;
219
221 static constexpr size_t RowsCount = Extent;
222
224 static constexpr size_t ValuesCount = RowsCount * RowLength;
225
227 using array::array;
228
230 [[nodiscard]] constexpr size_t length() const noexcept { return size(); }
231
233 [[nodiscard]] constexpr std::span<const float_t, ValuesCount> to_values_span() const noexcept {
234 return std::span(_native->m);
235 }
236
238 [[nodiscard]] constexpr float_t value_at(const size_t index) const noexcept { return _native->m[index]; }
239
241 [[nodiscard]] constexpr operator const float_t*() const noexcept { return _native->m; } // NOLINT
242 };
243
248 struct mat4_animated : detail::wrapper<mat4_animated, tpbrt_mat4_animated_t> {
250 using wrapper::wrapper;
251
253 [[nodiscard]] constexpr mat4 start() const noexcept { return _native->start; }
254
256 [[nodiscard]] constexpr mat4 end() const noexcept { return _native->end; }
257
259 [[nodiscard]] constexpr auto reflect() const noexcept { return std::make_tuple(start(), end()); }
260 };
261} // namespace tpbrt
262#endif // _TINYPBRT_MATH_HPP_
bool bool_t
Bool type.
Definition fwd.hpp:82
tpbrt_float_t float_t
Float type.
Definition fwd.hpp:97
constexpr auto True
True value.
Definition fwd.hpp:20
Definition accelerator.hpp:14
Generic C++ array view with type conversion (a non owning view on memory owned by the C library).
Definition common.hpp:448
constexpr iterator end() const noexcept
Returns end iterator.
Definition common.hpp:507
constexpr size_t size() const noexcept
Returns size of this array.
Definition common.hpp:489
static constexpr size_t Extent
Array static size.
Definition common.hpp:468
C++ wrapper for tpbrt_bool_array_t.
Definition math.hpp:51
C++ wrapper for tpbrt_float_array_t.
Definition math.hpp:78
C++ wrapper for tpbrt_int_array_t.
Definition math.hpp:60
C++ wrapper for tpbrt_mat4_animated_t.
Definition math.hpp:248
constexpr mat4 end() const noexcept
Returns end value.
Definition math.hpp:256
constexpr mat4 start() const noexcept
Returns start value.
Definition math.hpp:253
constexpr auto reflect() const noexcept
Conversion to tuple.
Definition math.hpp:259
C++ wrapper for tpbrt_mat4_t.
Definition math.hpp:216
static constexpr size_t RowsCount
Rows count.
Definition math.hpp:221
constexpr std::span< const float_t, ValuesCount > to_values_span() const noexcept
Conversion to values span.
Definition math.hpp:233
constexpr size_t length() const noexcept
Returns values count.
Definition math.hpp:230
constexpr float_t value_at(const size_t index) const noexcept
Returns element at given index.
Definition math.hpp:238
static constexpr size_t RowLength
Row Vec length.
Definition math.hpp:218
static constexpr size_t ValuesCount
Values count.
Definition math.hpp:224
C++ wrapper for tpbrt_opt_float_t.
Definition math.hpp:24
constexpr optional_type to_optional() const noexcept
Converts to std::optional.
Definition math.hpp:38
constexpr auto reflect() const noexcept
Conversion to tuple.
Definition math.hpp:44
std::optional< float_t > optional_type
std::optional type.
Definition math.hpp:26
constexpr float_t value() const noexcept
Returns value value.
Definition math.hpp:32
constexpr bool_t has_value() const noexcept
Returns has_value value.
Definition math.hpp:35
C++ wrapper for tpbrt_uint_array_t.
Definition math.hpp:69
C++ wrapper for tpbrt_vec2_array_t.
Definition math.hpp:198
C++ wrapper for tpbrt_vec2_t.
Definition math.hpp:108
constexpr float_t r() const noexcept
Returns r value.
Definition math.hpp:116
constexpr float_t x() const noexcept
Returns x value.
Definition math.hpp:113
constexpr float_t y() const noexcept
Returns y value.
Definition math.hpp:119
constexpr auto reflect() const noexcept
Conversion operator to tuple.
Definition math.hpp:125
constexpr float_t g() const noexcept
Returns g value.
Definition math.hpp:122
C++ wrapper for tpbrt_vec3_array_t.
Definition math.hpp:207
C++ wrapper for tpbrt_vec3_t.
Definition math.hpp:132
constexpr float_t g() const noexcept
Returns g value.
Definition math.hpp:146
constexpr float_t z() const noexcept
Returns z value.
Definition math.hpp:149
constexpr float_t x() const noexcept
Returns x value.
Definition math.hpp:137
constexpr float_t y() const noexcept
Returns y value.
Definition math.hpp:143
constexpr float_t b() const noexcept
Returns b value.
Definition math.hpp:152
constexpr auto reflect() const noexcept
Conversion to tuple.
Definition math.hpp:155
constexpr float_t r() const noexcept
Returns r value.
Definition math.hpp:140
C++ wrapper for tpbrt_vec4_t.
Definition math.hpp:162
constexpr float_t x() const noexcept
Returns x value.
Definition math.hpp:167
constexpr auto reflect() const noexcept
Conversion to tuple.
Definition math.hpp:191
constexpr float_t z() const noexcept
Returns z value.
Definition math.hpp:179
constexpr float_t r() const noexcept
Returns r value.
Definition math.hpp:170
constexpr float_t y() const noexcept
Returns y value.
Definition math.hpp:173
constexpr float_t b() const noexcept
Returns b value.
Definition math.hpp:182
constexpr float_t w() const noexcept
Returns w value.
Definition math.hpp:185
constexpr float_t g() const noexcept
Returns g value.
Definition math.hpp:176
constexpr float_t a() const noexcept
Returns a value.
Definition math.hpp:188
Generic C++ wrapper for all tpbrt_vec*_t types.
Definition math.hpp:88
static constexpr size_t length() noexcept
Returns vec length.
Definition math.hpp:101
static constexpr size_t Length
Vec length.
Definition math.hpp:90