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};
130}
131
132enum class TransformClass : uint8_t
133{
134 NONE = 0,
135 POINT = 1,
136 PRIMITIVE = 2,
137 POINT_PRIORITY = 3 // POINT | PRIMITIVE
138};
139
140// Enum class doesn't support bitwise ops natively. These allow bit-testing
141// e.g. (transformClass & TransformClass::POINT) != TransformClass::NONE
142inline TransformClass operator|(TransformClass a, TransformClass b)
143{
144 return static_cast<TransformClass>(static_cast<uint8_t>(a) | static_cast<uint8_t>(b));
145}
146inline TransformClass operator&(TransformClass a, TransformClass b)
147{
148 return static_cast<TransformClass>(static_cast<uint8_t>(a) & static_cast<uint8_t>(b));
149}
150// e.g. hasFlag(prim.transformType(), TransformClass::POINT)
151inline bool hasFlag(TransformClass value, TransformClass flag)
152{
153 return (value & flag) != TransformClass::NONE;
154}
155
156// Basic types
157using floatT = float;
158using intT = int64_t;
159using boolT = bool;
160using Vector2 = Eigen::Vector2f;
161using Vector3 = Eigen::Vector3f;
162using Vector4 = Eigen::Vector4f;
163using Matrix3 = Eigen::Matrix3f;
164using Matrix4 = Eigen::Matrix4f;
165using String = std::string;
172using Index = size_t;
181using Offset = size_t;
182
183namespace attr {
185template <typename T> constexpr AttributeType getAttributeType()
186{
187 if constexpr (std::is_same_v<T, intT>) return AttributeType::intT;
188 else if constexpr (std::is_same_v<T, floatT>) return AttributeType::floatT;
189 else if constexpr (std::is_same_v<T, boolT>) return AttributeType::boolT;
190 else if constexpr (std::is_same_v<T, Vector3>) return AttributeType::vectorT;
191 else if constexpr (std::is_same_v<T, Matrix4>) return AttributeType::matrixT;
192 else static_assert(sizeof(T) == 0, "No attribute type stores this C++ type");
193}
194} // namespace attr
195
196namespace prm {
197enum class Type
198{
199 STRING,
200 FLOAT,
201 BOOL,
202 INT,
203 TOGGLE,
204 GROUP,
205 DROPDOWN,
206 RAMP,
207 DIVIDER
208};
209enum class Direction
210{
211 HORIZONTAL,
212 VERTICAL
213};
220enum class ValueType
221{
222 Float,
223 Int,
224 String
225};
226
228inline std::string toString(Type type)
229{
230 // Using a switch to catch missing cases at compile time.
231 switch (type)
232 {
233 case Type::STRING:
234 return "string";
235 case Type::FLOAT:
236 return "float";
237 case Type::BOOL:
238 return "bool";
239 case Type::INT:
240 return "int";
241 case Type::TOGGLE:
242 return "toggle";
243 case Type::GROUP:
244 return "group";
245 case Type::DROPDOWN:
246 return "dropdown";
247 case Type::RAMP:
248 return "ramp";
249 case Type::DIVIDER:
250 return "divider";
251 }
252 return "";
253}
254
256inline constexpr std::array kAllTypes = {
257 Type::STRING,
258 Type::FLOAT,
259 Type::BOOL,
260 Type::INT,
261 Type::TOGGLE,
262 Type::GROUP,
263 Type::DROPDOWN,
264 Type::RAMP,
265 Type::DIVIDER
266};
267
270inline std::optional<Type> toType(const std::string& name)
271{
272 for (Type type : kAllTypes)
273 if (toString(type) == name) return type;
274 return std::nullopt;
275}
276
278inline ValueType toValueType(Type type)
279{
280 switch (type)
281 {
282 case Type::FLOAT:
283 return ValueType::Float;
284 case Type::INT:
285 case Type::BOOL:
286 case Type::TOGGLE:
287 // Multiparm parameters (like ramp) use integers to represent their instance
288 // count and store the actual data in their instances.
289 case Type::RAMP:
290 // Dividers are purely visual and store an int nobody reads.
291 case Type::DIVIDER:
292 return ValueType::Int;
293 case Type::STRING:
294 case Type::DROPDOWN:
295 return ValueType::String;
296 case Type::GROUP:
297 return ValueType::Float;
298 }
299 return ValueType::Float;
300}
301} // namespace prm
302namespace nt {
306using NodeId = uint64_t;
307
309constexpr NodeId nullNode = 0;
310
311enum class SocketIOType
312{
313 Input,
314 Output
315};
316} // namespace nt
317} // namespace enzo
size_t Offset
enzo::Offset is the internal discontinuous index of an element in a given AttributeOwner.
Definition Types.h:181
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:278
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:185
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:309
std::string toString(Type type)
The lowercase canonical name of a parameter type, e.g. "dropdown".
Definition Types.h:228
uint64_t NodeId
The unique ID assigned to each node in the network.
Definition Types.h:306
constexpr std::array kAllTypes
Every parameter type, in declaration order.
Definition Types.h:256
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:172
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:221
std::optional< Type > toType(const std::string &name)
Returns the parameter type a canonical name stands for, e.g. "dropdown".
Definition Types.h:270
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