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 void incrementVersion();
104
105 virtual bool hasPoints() const { return false; }
106 Offset getNumPoints() const { return getElementCount(attr::AttributeOwner::POINT); }
107 virtual PointOffsets getPoints() { return PointOffsets(*this); }
108 virtual bool isValidPoint(Offset offset) const { return true; }
109 virtual void deletePoints(const std::vector<Offset>& pointOffsets) {}
110
115 virtual void defragment() {}
116
126 std::shared_ptr<attr::Attribute> addAttribute(
128 std::string name,
130 bool intrinsic = false,
131 bool isInternal = false
132 );
133
143 std::shared_ptr<attr::Attribute> tryAddAttribute(
145 std::string name,
147 bool intrinsic = false,
148 bool isInternal = false
149 );
150
160 template <typename T>
163 std::string name,
164 bool intrinsic = false,
165 bool isInternal = false
166 )
167 {
169 owner, std::move(name), attr::getAttributeType<T>(), intrinsic, isInternal
170 ));
171 }
172
182 template <typename T>
183 std::optional<attr::AttributeHandle<T>> tryAddAttribute(
185 std::string name,
186 bool intrinsic = false,
187 bool isInternal = false
188 )
189 {
190 const std::shared_ptr<attr::Attribute> attribute = tryAddAttribute(
191 owner,
192 std::move(name),
193 attr::getAttributeType<T>(),
194 intrinsic,
195 isInternal
196 );
197 if (!attribute) return std::nullopt;
198 return attr::AttributeHandle<T>(attribute);
199 }
200
201 std::shared_ptr<attr::Attribute>
202 getAttribByName(attr::AttributeOwner owner, std::string name, bool includeIntrinsics = false);
209 std::shared_ptr<const attr::Attribute> getAttribByName(
211 std::string name,
212 bool includeIntrinsics = false
213 ) const;
214 const size_t getNumAttributes(const attr::AttributeOwner owner) const;
215 std::weak_ptr<const attr::Attribute>
216 getAttributeByIndex(attr::AttributeOwner owner, unsigned int index) const;
222 std::vector<std::shared_ptr<const attr::Attribute>>
223 getAttributes(attr::AttributeOwner owner, bool includeIntrinsics = false) const;
224 bool attributeExists(attr::AttributeOwner owner, std::string name);
225
237 void addToGroup(
239 const std::string& name,
240 const std::vector<Offset>& offsets
241 );
246 std::shared_ptr<attr::Attribute>
247 getGroupByName(attr::AttributeOwner owner, const std::string& name) const;
251 size_t getNumGroups(attr::AttributeOwner owner) const;
255 std::weak_ptr<const attr::Attribute>
256 getGroupByIndex(attr::AttributeOwner owner, unsigned int index) const;
258 std::vector<std::shared_ptr<const attr::Attribute>> getGroups(attr::AttributeOwner owner) const;
259
266 void addAttributesFrom(const Primitive& source, attr::AttributeOwner owner);
267
270 {
271 return addGroup(attr::AttributeOwner::POINT, std::move(name));
272 }
275 {
276 return addGroup(attr::AttributeOwner::PRIMITIVE, std::move(name));
277 }
279 void addToPointGroup(const std::string& name, const std::vector<Offset>& offsets)
280 {
281 addToGroup(attr::AttributeOwner::POINT, name, offsets);
282 }
284 void addToPrimitiveGroup(const std::string& name, const std::vector<Offset>& offsets)
285 {
286 addToGroup(attr::AttributeOwner::PRIMITIVE, name, offsets);
287 }
288
289 String getPath() const { return path_; };
290 void setPath(const String& path) { path_ = path; };
291
292 protected:
293 virtual attr::attribVector& getAttributeStore(const attr::AttributeOwner& owner);
294 virtual const attr::attribVector& getAttributeStore(const attr::AttributeOwner& owner) const;
295 virtual attr::attribVector& getGroupStore(const attr::AttributeOwner& owner);
296 virtual const attr::attribVector& getGroupStore(const attr::AttributeOwner& owner) const;
297 attr::attribVector deepCopyAttributes(attr::attribVector source);
298
303 size_t getElementCount(const attr::AttributeOwner& owner) const;
304
305 std::string path_ = "/prim";
306 attr::attribVector pointAttributes_;
307 attr::attribVector primitiveAttributes_;
308 attr::attribVector pointGroups_;
309 attr::attribVector primitiveGroups_;
310};
311
312using PrimPtr = std::shared_ptr<Primitive>;
313} // 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:181
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:183
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:161
void addToPointGroup(const std::string &name, const std::vector< Offset > &offsets)
Marks the given offsets as members of the point group.
Definition Primitive.h:279
void addToPrimitiveGroup(const std::string &name, const std::vector< Offset > &offsets)
Marks the given offsets as members of the primitive group.
Definition Primitive.h:284
attr::AttributeHandleBool addPointGroup(std::string name)
Returns the point group of this name, adding one when the name is free.
Definition Primitive.h:269
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:274
virtual void defragment()
Compacts storage, removing entries marked invalid so offsets are contiguous again.
Definition Primitive.h:115
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