Enzo
Loading...
Searching...
No Matches
Primitive.h
1#pragma once
2#include "Engine/Attribute/Attribute.h"
3#include "Engine/Attribute/AttributeHandle.h"
4#include "Engine/Attribute/Point.h"
5#include "Engine/Attribute/Transform.h"
6#include "Engine/Core/Types.h"
7#include <memory>
8#include <optional>
9#include <string>
10
11namespace enzo::geo {
12
26class Primitive
27{
28 public:
29 // Point Iterator
31 {
32 PointOffsets(Primitive& primitive) : primitive_(primitive) {}
33 struct Iterator
34 {
35 using iterator_category = std::forward_iterator_tag;
36 using difference_type = std::ptrdiff_t;
37 using value_type = Offset;
38
39 value_type operator*() const { return curOffset_; }
40
41 Iterator(Primitive& primitive, Offset current)
42 : primitive_(primitive), curOffset_(current)
43 {
44 skipInvalid();
45 }
46
47 // Prefix increment
48 Iterator& operator++()
49 {
50 ++curOffset_;
51 skipInvalid();
52 return *this;
53 }
54
55 // Postfix increment
56 Iterator operator++(int)
57 {
58 Iterator tmp = *this;
59 ++(*this);
60 return tmp;
61 }
62
63 friend bool operator==(const Iterator& a, const Iterator& b)
64 {
65 return a.curOffset_ == b.curOffset_;
66 };
67 friend bool operator!=(const Iterator& a, const Iterator& b)
68 {
69 return a.curOffset_ != b.curOffset_;
70 };
71
72 private:
73 enzo::geo::Primitive& primitive_;
74 Offset curOffset_ = 0;
75 void skipInvalid()
76 {
77 const Offset end = primitive_.getNumPoints();
78 while (curOffset_ < end && !primitive_.isValidPoint(curOffset_))
79 ++curOffset_;
80 }
81 };
82 Iterator begin() { return Iterator(primitive_, 0); }
83 Iterator end() { return Iterator(primitive_, primitive_.getNumPoints()); }
84
85 private:
86 enzo::geo::Primitive& primitive_;
87 };
88 Primitive(std::string_view path = "/prim");
89 Primitive(const Primitive& other);
90 Primitive& operator=(const Primitive& rhs);
91 virtual ~Primitive() = default;
92
93 virtual PrimType getType() const = 0;
94 virtual std::shared_ptr<Primitive> clone() const = 0;
95 virtual TransformClass transformType() const = 0;
96 virtual void applyTransform(const Matrix4& mat, TransformClass transformClass) = 0;
97 void applyTransform(const Transform& transform, TransformClass transformClass)
98 {
99 applyTransform(transform.getMatrix(), transformClass);
100 }
101 virtual bool canMerge() const { return false; }
102 virtual void merge(std::shared_ptr<Primitive> other) {}
103
105 virtual bool hasOwner(attr::AttributeOwner owner) const
106 {
107 return owner == attr::AttributeOwner::PRIMITIVE;
108 }
109
111 size_t getElementCount(const attr::AttributeOwner& owner) const;
112
113 Offset getNumPoints() const { return getElementCount(attr::AttributeOwner::POINT); }
114 virtual PointOffsets getPoints() { return PointOffsets(*this); }
115 virtual bool isValidPoint(Offset offset) const { return true; }
116 virtual void deletePoints(const std::vector<Offset>& pointOffsets) {}
117
122 virtual void defragment() {}
123
133 std::shared_ptr<attr::Attribute> addAttribute(
135 std::string name,
137 bool intrinsic = false,
138 bool isInternal = false
139 );
140
150 std::shared_ptr<attr::Attribute> tryAddAttribute(
152 std::string name,
154 bool intrinsic = false,
155 bool isInternal = false
156 );
157
167 template <typename T>
170 std::string name,
171 bool intrinsic = false,
172 bool isInternal = false
173 )
174 {
176 owner, std::move(name), attr::getAttributeType<T>(), intrinsic, isInternal
177 ));
178 }
179
189 template <typename T>
190 std::optional<attr::AttributeHandle<T>> tryAddAttribute(
192 std::string name,
193 bool intrinsic = false,
194 bool isInternal = false
195 )
196 {
197 const std::shared_ptr<attr::Attribute> attribute = tryAddAttribute(
198 owner,
199 std::move(name),
200 attr::getAttributeType<T>(),
201 intrinsic,
202 isInternal
203 );
204 if (!attribute) return std::nullopt;
205 return attr::AttributeHandle<T>(attribute);
206 }
207
208 std::shared_ptr<attr::Attribute>
209 getAttribByName(attr::AttributeOwner owner, std::string_view name, bool includeIntrinsics = false);
216 std::shared_ptr<const attr::Attribute> getAttribByName(
218 std::string_view name,
219 bool includeIntrinsics = false
220 ) const;
221 const size_t getNumAttributes(const attr::AttributeOwner owner) const;
222 std::weak_ptr<const attr::Attribute>
223 getAttributeByIndex(attr::AttributeOwner owner, unsigned int index) const;
229 std::vector<std::shared_ptr<const attr::Attribute>>
230 getAttributes(attr::AttributeOwner owner, bool includeIntrinsics = false) const;
231 bool attributeExists(attr::AttributeOwner owner, std::string_view name);
232
244 void addToGroup(
246 const std::string& name,
247 const std::vector<Offset>& offsets
248 );
253 std::shared_ptr<attr::Attribute>
254 getGroupByName(attr::AttributeOwner owner, const std::string& name) const;
258 size_t getNumGroups(attr::AttributeOwner owner) const;
262 std::weak_ptr<const attr::Attribute>
263 getGroupByIndex(attr::AttributeOwner owner, unsigned int index) const;
265 std::vector<std::shared_ptr<const attr::Attribute>> getGroups(attr::AttributeOwner owner) const;
266
273 void addAttributesFrom(const Primitive& source, attr::AttributeOwner owner);
274
277 {
278 return addGroup(attr::AttributeOwner::POINT, std::move(name));
279 }
282 {
283 return addGroup(attr::AttributeOwner::PRIMITIVE, std::move(name));
284 }
286 void addToPointGroup(const std::string& name, const std::vector<Offset>& offsets)
287 {
288 addToGroup(attr::AttributeOwner::POINT, name, offsets);
289 }
291 void addToPrimitiveGroup(const std::string& name, const std::vector<Offset>& offsets)
292 {
293 addToGroup(attr::AttributeOwner::PRIMITIVE, name, offsets);
294 }
295
296 String getPath() const { return path_; };
297 void setPath(const String& path) { path_ = path; };
298
299 protected:
300 virtual attr::attribVector& getAttributeStore(const attr::AttributeOwner& owner);
301 virtual const attr::attribVector& getAttributeStore(const attr::AttributeOwner& owner) const;
302 virtual attr::attribVector& getGroupStore(const attr::AttributeOwner& owner);
303 virtual const attr::attribVector& getGroupStore(const attr::AttributeOwner& owner) const;
304 attr::attribVector deepCopyAttributes(attr::attribVector source);
305
306 std::string path_ = "/prim";
307 attr::attribVector pointAttributes_;
308 attr::attribVector primitiveAttributes_;
309 attr::attribVector pointGroups_;
310 attr::attribVector primitiveGroups_;
311};
312
313using PrimPtr = std::shared_ptr<Primitive>;
314} // namespace enzo::geo
Basic attribute, parameter, and node types for Enzo.
size_t Offset
enzo::Offset is the internal discontinuous index of an element in a given AttributeOwner.
Definition Types.h:182
AttributeType
Data types available to store attribute values in.
Definition Types.h:37
AttributeOwner
The segment of geometry that owns a particular attribute.
Definition Types.h:27
An affine transform that composes translation, rotation, and scale.
Read write accessor for enzo::attr::Attribute.
Base class for all primitive types in the engine.
attr::AttributeHandleBool addGroup(attr::AttributeOwner owner, std::string name)
Returns the group of this name on the given owner, adding one when the name is free.
Definition Primitive.cpp:216
std::vector< std::shared_ptr< const attr::Attribute > > getGroups(attr::AttributeOwner owner) const
Returns every group on the owner.
Definition Primitive.cpp:268
std::shared_ptr< attr::Attribute > tryAddAttribute(attr::AttributeOwner owner, std::string name, attr::AttributeType type, bool intrinsic=false, bool isInternal=false)
Returns the attribute of this name and type, adding one when the name is free.
Definition Primitive.cpp:139
std::shared_ptr< attr::Attribute > addAttribute(attr::AttributeOwner owner, std::string name, attr::AttributeType type, bool intrinsic=false, bool isInternal=false)
Returns the attribute of this name and type, adding one when the name is free.
Definition Primitive.cpp:124
void addAttributesFrom(const Primitive &source, attr::AttributeOwner owner)
Adds each attribute and group of the source to this primitive, keeping its name and type.
Definition Primitive.cpp:274
std::vector< std::shared_ptr< const attr::Attribute > > getAttributes(attr::AttributeOwner owner, bool includeIntrinsics=false) const
Returns every attribute on the owner.
Definition Primitive.cpp:65
std::weak_ptr< const attr::Attribute > getGroupByIndex(attr::AttributeOwner owner, unsigned int index) const
Returns the group at the given index in the owner's group store.
Definition Primitive.cpp:254
std::shared_ptr< attr::Attribute > getGroupByName(attr::AttributeOwner owner, const std::string &name) const
Looks up a group by name.
Definition Primitive.cpp:239
size_t getElementCount(const attr::AttributeOwner &owner) const
Returns the number of elements in the given owner's store.
Definition Primitive.cpp:204
std::optional< attr::AttributeHandle< T > > tryAddAttribute(attr::AttributeOwner owner, std::string name, bool intrinsic=false, bool isInternal=false)
Returns a handle to the attribute of this type stored under this name, adding one when the name is fr...
Definition Primitive.h:190
virtual bool hasOwner(attr::AttributeOwner owner) const
Returns whether this primitive type stores elements of the owner.
Definition Primitive.h:105
attr::AttributeHandle< T > addAttribute(attr::AttributeOwner owner, std::string name, bool intrinsic=false, bool isInternal=false)
Returns a handle to the attribute of this type stored under this name, adding one when the name is fr...
Definition Primitive.h:168
void addToPointGroup(const std::string &name, const std::vector< Offset > &offsets)
Marks the given offsets as members of the point group.
Definition Primitive.h:286
void addToPrimitiveGroup(const std::string &name, const std::vector< Offset > &offsets)
Marks the given offsets as members of the primitive group.
Definition Primitive.h:291
attr::AttributeHandleBool addPointGroup(std::string name)
Returns the point group of this name, adding one when the name is free.
Definition Primitive.h:276
size_t getNumGroups(attr::AttributeOwner owner) const
Returns how many groups live on the given owner.
Definition Primitive.cpp:248
attr::AttributeHandleBool addPrimitiveGroup(std::string name)
Returns the primitive group of this name, adding one when the name is free.
Definition Primitive.h:281
virtual void defragment()
Compacts storage, removing entries marked invalid so offsets are contiguous again.
Definition Primitive.h:122
void addToGroup(attr::AttributeOwner owner, const std::string &name, const std::vector< Offset > &offsets)
Marks the given offsets as members of the group.
Definition Primitive.cpp:223
Definition Primitive.h:31