Enzo
Loading...
Searching...
No Matches
Types.h
Go to the documentation of this file.
1
6#pragma once
7#include <Eigen/Dense>
8#include <array>
9#include <cstdint>
10#include <optional>
11#include <string>
12#include <type_traits>
13#include <vector>
14
15namespace enzo {
16
17namespace attr {
27{
28 POINT,
29 VERTEX,
30 FACE,
31 PRIMITIVE
32};
36enum class AttributeType
37{
38 intT,
39 floatT,
40 listT,
41 vectorT,
42 boolT,
43 matrixT,
44};
45using AttrType = AttributeType;
46using AttrOwner = AttributeOwner;
47
49inline std::vector<AttributeOwner> getAllOwners()
50{
51 return {AttributeOwner::POINT,
52 AttributeOwner::VERTEX,
53 AttributeOwner::FACE,
54 AttributeOwner::PRIMITIVE};
55}
56
58inline std::vector<AttributeType> getAllTypes()
59{
60 return {AttributeType::intT,
61 AttributeType::floatT,
62 AttributeType::listT,
63 AttributeType::vectorT,
64 AttributeType::boolT,
65 AttributeType::matrixT};
66}
67
69inline std::string getOwnerName(AttributeOwner owner)
70{
71 switch (owner)
72 {
73 case AttributeOwner::POINT:
74 return "point";
75 case AttributeOwner::VERTEX:
76 return "vertex";
77 case AttributeOwner::FACE:
78 return "face";
79 case AttributeOwner::PRIMITIVE:
80 return "primitive";
81 }
82 return "";
83}
84
86inline std::string getTypeName(AttributeType type)
87{
88 switch (type)
89 {
90 case AttributeType::intT:
91 return "int";
92 case AttributeType::floatT:
93 return "float";
94 case AttributeType::listT:
95 return "list";
96 case AttributeType::vectorT:
97 return "vector";
98 case AttributeType::boolT:
99 return "bool";
100 case AttributeType::matrixT:
101 return "matrix";
102 }
103 return "";
104}
105
108inline std::optional<AttributeOwner> getOwner(const std::string& name)
109{
110 for (AttributeOwner owner : getAllOwners())
111 if (getOwnerName(owner) == name) return owner;
112 return std::nullopt;
113}
114
117inline std::optional<AttributeType> getType(const std::string& name)
118{
119 for (AttributeType type : getAllTypes())
120 if (getTypeName(type) == name) return type;
121 return std::nullopt;
122}
123} // namespace attr
124namespace geo {
125enum class PrimType
126{
127 MESH,
128 CAMERA,
129 POINT_LIGHT
130};
131}
132
133enum class TransformClass : uint8_t
134{
135 NONE = 0,
136 POINT = 1,
137 PRIMITIVE = 2,
138 POINT_PRIORITY = 3 // POINT | PRIMITIVE
139};
140
141// Enum class doesn't support bitwise ops natively. These allow bit-testing
142// e.g. (transformClass & TransformClass::POINT) != TransformClass::NONE
143inline TransformClass operator|(TransformClass a, TransformClass b)
144{
145 return static_cast<TransformClass>(static_cast<uint8_t>(a) | static_cast<uint8_t>(b));
146}
147inline TransformClass operator&(TransformClass a, TransformClass b)
148{
149 return static_cast<TransformClass>(static_cast<uint8_t>(a) & static_cast<uint8_t>(b));
150}
151// e.g. hasFlag(prim.transformType(), TransformClass::POINT)
152inline bool hasFlag(TransformClass value, TransformClass flag)
153{
154 return (value & flag) != TransformClass::NONE;
155}
156
157// Basic types
158using floatT = float;
159using intT = int64_t;
160using boolT = bool;
161using Vector2 = Eigen::Vector2f;
162using Vector3 = Eigen::Vector3f;
163using Vector4 = Eigen::Vector4f;
164using Matrix3 = Eigen::Matrix3f;
165using Matrix4 = Eigen::Matrix4f;
166using String = std::string;
173using Index = size_t;
182using Offset = size_t;
183
184namespace attr {
186template <typename T> constexpr AttributeType getAttributeType()
187{
188 if constexpr (std::is_same_v<T, intT>) return AttributeType::intT;
189 else if constexpr (std::is_same_v<T, floatT>) return AttributeType::floatT;
190 else if constexpr (std::is_same_v<T, boolT>) return AttributeType::boolT;
191 else if constexpr (std::is_same_v<T, Vector3>) return AttributeType::vectorT;
192 else if constexpr (std::is_same_v<T, Matrix4>) return AttributeType::matrixT;
193 else static_assert(sizeof(T) == 0, "No attribute type stores this C++ type");
194}
195
203template <typename Action> auto visitType(AttributeType type, Action&& action)
204{
205 using Result = decltype(action.template operator()<intT>());
206 switch (type)
207 {
208 case AttributeType::intT:
209 return action.template operator()<intT>();
210 case AttributeType::floatT:
211 return action.template operator()<floatT>();
212 case AttributeType::vectorT:
213 return action.template operator()<Vector3>();
214 case AttributeType::boolT:
215 return action.template operator()<boolT>();
216 case AttributeType::matrixT:
217 return action.template operator()<Matrix4>();
218 case AttributeType::listT:
219 break;
220 }
221 return Result();
222}
223} // namespace attr
224
225namespace prm {
226enum class Type
227{
228 STRING,
229 FLOAT,
230 BOOL,
231 INT,
232 TOGGLE,
233 GROUP,
234 DROPDOWN,
235 RAMP,
236 DIVIDER
237};
238enum class Direction
239{
240 HORIZONTAL,
241 VERTICAL
242};
249enum class ValueType
250{
251 Float,
252 Int,
253 String
254};
255
257inline std::string toString(Type type)
258{
259 // Using a switch to catch missing cases at compile time.
260 switch (type)
261 {
262 case Type::STRING:
263 return "string";
264 case Type::FLOAT:
265 return "float";
266 case Type::BOOL:
267 return "bool";
268 case Type::INT:
269 return "int";
270 case Type::TOGGLE:
271 return "toggle";
272 case Type::GROUP:
273 return "group";
274 case Type::DROPDOWN:
275 return "dropdown";
276 case Type::RAMP:
277 return "ramp";
278 case Type::DIVIDER:
279 return "divider";
280 }
281 return "";
282}
283
285inline constexpr std::array kAllTypes = {
286 Type::STRING,
287 Type::FLOAT,
288 Type::BOOL,
289 Type::INT,
290 Type::TOGGLE,
291 Type::GROUP,
292 Type::DROPDOWN,
293 Type::RAMP,
294 Type::DIVIDER
295};
296
299inline std::optional<Type> toType(const std::string& name)
300{
301 for (Type type : kAllTypes)
302 if (toString(type) == name) return type;
303 return std::nullopt;
304}
305
307inline ValueType toValueType(Type type)
308{
309 switch (type)
310 {
311 case Type::FLOAT:
312 return ValueType::Float;
313 case Type::INT:
314 case Type::BOOL:
315 case Type::TOGGLE:
316 // Multiparm parameters (like ramp) use integers to represent their instance
317 // count and store the actual data in their instances.
318 case Type::RAMP:
319 // Dividers are purely visual and store an int nobody reads.
320 case Type::DIVIDER:
321 return ValueType::Int;
322 case Type::STRING:
323 case Type::DROPDOWN:
324 return ValueType::String;
325 case Type::GROUP:
326 return ValueType::Float;
327 }
328 return ValueType::Float;
329}
330} // namespace prm
331namespace nt {
335using NodeId = uint64_t;
336
338constexpr NodeId nullNode = 0;
339
340enum class SocketIOType
341{
342 Input,
343 Output
344};
345} // namespace nt
346} // namespace enzo
size_t Offset
enzo::Offset is the internal discontinuous index of an element in a given AttributeOwner.
Definition Types.h:182
std::string getOwnerName(AttributeOwner owner)
Returns the name of a part of the geometry, e.g. "point".
Definition Types.h:69
ValueType toValueType(Type type)
Returns the kind of value a parameter of this type stores.
Definition Types.h:307
AttributeType
Data types available to store attribute values in.
Definition Types.h:37
constexpr AttributeType getAttributeType()
Returns the attribute type that stores values of the given C++ type.
Definition Types.h:186
std::vector< AttributeType > getAllTypes()
Returns every type an attribute can store.
Definition Types.h:58
std::vector< AttributeOwner > getAllOwners()
Returns every part of the geometry that can own an attribute.
Definition Types.h:49
constexpr NodeId nullNode
The id that names no node, since real ids start at 1.
Definition Types.h:338
std::string toString(Type type)
The lowercase canonical name of a parameter type, e.g. "dropdown".
Definition Types.h:257
uint64_t NodeId
The unique ID assigned to each node in the network.
Definition Types.h:335
constexpr std::array kAllTypes
Every parameter type, in declaration order.
Definition Types.h:285
AttributeOwner
The segment of geometry that owns a particular attribute.
Definition Types.h:27
size_t Index
enzo::Index is the continuous index of an element in a given AttributeOwner.
Definition Types.h:173
std::optional< AttributeType > getType(const std::string &name)
Returns the type a name stands for, e.g. "vector".
Definition Types.h:117
ValueType
Which kind of value a parameter stores.
Definition Types.h:250
auto visitType(AttributeType type, Action &&action)
Calls an action with the C++ type that stores an attribute type.
Definition Types.h:203
std::optional< Type > toType(const std::string &name)
Returns the parameter type a canonical name stands for, e.g. "dropdown".
Definition Types.h:299
std::string getTypeName(AttributeType type)
Returns the name of a type an attribute can store, e.g. "vector".
Definition Types.h:86
std::optional< AttributeOwner > getOwner(const std::string &name)
Returns the part of the geometry a name stands for, e.g. "point".
Definition Types.h:108