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 <string>
9
10namespace enzo::geo {
11
25class Primitive
26{
27 public:
28 // Point Iterator
30 {
31 PointOffsets(Primitive& primitive) : primitive_(primitive) {}
32 struct Iterator
33 {
34 using iterator_category = std::forward_iterator_tag;
35 using difference_type = std::ptrdiff_t;
36 using value_type = Offset;
37
38 value_type operator*() const { return curOffset_; }
39
40 Iterator(Primitive& primitive, Offset current)
41 : primitive_(primitive), curOffset_(current)
42 {
43 skipInvalid();
44 }
45
46 // Prefix increment
47 Iterator& operator++()
48 {
49 ++curOffset_;
50 skipInvalid();
51 return *this;
52 }
53
54 // Postfix increment
55 Iterator operator++(int)
56 {
57 Iterator tmp = *this;
58 ++(*this);
59 return tmp;
60 }
61
62 friend bool operator==(const Iterator& a, const Iterator& b)
63 {
64 return a.curOffset_ == b.curOffset_;
65 };
66 friend bool operator!=(const Iterator& a, const Iterator& b)
67 {
68 return a.curOffset_ != b.curOffset_;
69 };
70
71 private:
72 enzo::geo::Primitive& primitive_;
73 Offset curOffset_ = 0;
74 void skipInvalid()
75 {
76 const Offset end = primitive_.getNumPoints();
77 while (curOffset_ < end && !primitive_.isValidPoint(curOffset_))
78 ++curOffset_;
79 }
80 };
81 Iterator begin() { return Iterator(primitive_, 0); }
82 Iterator end() { return Iterator(primitive_, primitive_.getNumPoints()); }
83
84 private:
85 enzo::geo::Primitive& primitive_;
86 };
87 Primitive(std::string_view path = "/prim");
88 Primitive(const Primitive& other);
89 Primitive& operator=(const Primitive& rhs);
90 virtual ~Primitive() = default;
91
92 virtual PrimType getType() const = 0;
93 virtual std::shared_ptr<Primitive> clone() const = 0;
94 virtual TransformClass transformType() const = 0;
95 virtual void applyTransform(const Matrix4& mat, TransformClass transformClass) = 0;
96 void applyTransform(const Transform& transform, TransformClass transformClass)
97 {
98 applyTransform(transform.getMatrix(), transformClass);
99 }
100 virtual bool canMerge() const { return false; }
101 virtual void merge(std::shared_ptr<Primitive> other) {}
102 void incrementVersion();
103
104 virtual bool hasPoints() const { return false; }
105 Offset getNumPoints() const { return getElementCount(attr::AttributeOwner::POINT); }
106 virtual PointOffsets getPoints() { return PointOffsets(*this); }
107 virtual bool isValidPoint(Offset offset) const { return true; }
108 virtual void deletePoints(const std::vector<Offset>& pointOffsets) {}
109
114 virtual void defragment() {}
115
117 addIntAttribute(attr::AttributeOwner owner, std::string name, bool intrinsic = false);
118 attr::AttributeHandleBool addBoolAttribute(
120 std::string name,
121 bool intrinsic = false,
122 bool isPrivate = false
123 );
125 addVector3Attribute(attr::AttributeOwner owner, std::string name, bool intrinsic = false);
127 addMatrix4Attribute(attr::AttributeOwner owner, std::string name, bool intrinsic = false);
128
129 std::shared_ptr<attr::Attribute>
130 getAttribByName(attr::AttributeOwner owner, std::string name, bool includeIntrinsics = false);
137 std::shared_ptr<const attr::Attribute> getAttribByName(
139 std::string name,
140 bool includeIntrinsics = false
141 ) const;
142 const size_t getNumAttributes(const attr::AttributeOwner owner) const;
143 std::weak_ptr<const attr::Attribute>
144 getAttributeByIndex(attr::AttributeOwner owner, unsigned int index) const;
145 bool attributeExists(attr::AttributeOwner owner, std::string name);
146
159 void addToGroup(
161 const std::string& name,
162 const std::vector<Offset>& offsets
163 );
168 std::shared_ptr<attr::Attribute>
169 getGroupByName(attr::AttributeOwner owner, const std::string& name) const;
173 size_t getNumGroups(attr::AttributeOwner owner) const;
177 std::weak_ptr<const attr::Attribute>
178 getGroupByIndex(attr::AttributeOwner owner, unsigned int index) const;
179
183 {
184 return createGroup(attr::AttributeOwner::POINT, std::move(name));
185 }
189 {
190 return createGroup(attr::AttributeOwner::PRIMITIVE, std::move(name));
191 }
193 void addToPointGroup(const std::string& name, const std::vector<Offset>& offsets)
194 {
195 addToGroup(attr::AttributeOwner::POINT, name, offsets);
196 }
198 void addToPrimitiveGroup(const std::string& name, const std::vector<Offset>& offsets)
199 {
200 addToGroup(attr::AttributeOwner::PRIMITIVE, name, offsets);
201 }
202
203 String getPath() const { return path_; };
204 void setPath(const String& path) { path_ = path; };
205
206 protected:
207 virtual attr::attribVector& getAttributeStore(const attr::AttributeOwner& owner);
208 virtual const attr::attribVector& getAttributeStore(const attr::AttributeOwner& owner) const;
209 virtual attr::attribVector& getGroupStore(const attr::AttributeOwner& owner);
210 virtual const attr::attribVector& getGroupStore(const attr::AttributeOwner& owner) const;
211 attr::attribVector deepCopyAttributes(attr::attribVector source);
212
217 size_t getElementCount(const attr::AttributeOwner& owner) const;
218
219 std::string path_ = "/prim";
220 attr::attribVector pointAttributes_;
221 attr::attribVector primitiveAttributes_;
222 attr::attribVector pointGroups_;
223 attr::attribVector primitiveGroups_;
224};
225
226using PrimPtr = std::shared_ptr<Primitive>;
227} // 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:100
AttributeOwner
The segment of geometry that owns a particular attribute.
Definition Types.h:22
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 createPrimitiveGroup(std::string name)
Creates a primitive group.
Definition Primitive.h:188
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:206
std::shared_ptr< attr::Attribute > getGroupByName(attr::AttributeOwner owner, const std::string &name) const
Looks up a group by name.
Definition Primitive.cpp:191
size_t getElementCount(const attr::AttributeOwner &owner) const
Returns the number of elements in the given owner's store.
Definition Primitive.cpp:154
attr::AttributeHandleBool createGroup(attr::AttributeOwner owner, std::string name)
Creates a group on the given owner.
Definition Primitive.cpp:166
void addToPointGroup(const std::string &name, const std::vector< Offset > &offsets)
Marks the given offsets as members of the point group.
Definition Primitive.h:193
void addToPrimitiveGroup(const std::string &name, const std::vector< Offset > &offsets)
Marks the given offsets as members of the primitive group.
Definition Primitive.h:198
size_t getNumGroups(attr::AttributeOwner owner) const
Returns how many groups live on the given owner.
Definition Primitive.cpp:200
attr::AttributeHandleBool createPointGroup(std::string name)
Creates a point group.
Definition Primitive.h:182
virtual void defragment()
Compacts storage, removing entries marked invalid so offsets are contiguous again.
Definition Primitive.h:114
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:175
Definition Primitive.h:30