Enzo
Loading...
Searching...
No Matches
PointScript.h
1#pragma once
2#include "Engine/Attribute/AttributeHandle.h"
3#include "Engine/Core/Types.h"
4#include "Engine/Expression/DasRuntime.h"
5#include <deque>
6#include <memory>
7#include <optional>
8#include <vector>
9
10namespace enzo::geo {
11class Primitive;
12}
13
14namespace enzo::expr {
15
16class ExpressionContext;
17
20{
21 String name;
23 bool written = false;
24};
25
40{
41 public:
44 static std::shared_ptr<PointScript> compile(const String& code, String& error);
45
47 const std::vector<AttributeBinding>& getBindings() const { return bindings_; }
48
51 std::shared_ptr<PointScript> clone() const;
52
55 bool addWrittenAttributes(geo::Primitive& output, String& error) const;
56
65 bool run(
66 const geo::Primitive& input,
67 geo::Primitive& output,
68 Offset begin,
69 Offset end,
70 const ExpressionContext* context,
71 String& error
72 );
73
74 private:
75 // Values the script works on for one point, one list per attribute type.
76 template <typename Value> struct BindingValues
77 {
78 std::deque<Value> values;
79 std::vector<std::optional<attr::AttributeHandleRO<Value>>> inputs;
80 std::vector<std::optional<attr::AttributeHandle<Value>>> outputs;
81
82 // Reads a point's values from the input, leaving missing attributes at the default value.
83 void loadFrom(Offset point)
84 {
85 for (size_t valueIndex = 0; valueIndex < values.size(); ++valueIndex)
86 {
87 const auto& input = inputs[valueIndex];
88 values[valueIndex] = input ? input->getValue(point) : Value{};
89 }
90 }
91
92 // Writes a point's values back to the output attributes.
93 void storeTo(Offset point)
94 {
95 for (size_t valueIndex = 0; valueIndex < values.size(); ++valueIndex)
96 {
97 auto& output = outputs[valueIndex];
98 if (output) output->setValue(point, values[valueIndex]);
99 }
100 }
101 };
102
103 PointScript(std::shared_ptr<CompiledScript> compiled, std::vector<AttributeBinding> bindings);
104
105 template <typename Action> void visitBindingValues(attr::AttributeType type, Action&& action);
106 template <typename Action> void visitAllBindingValues(Action&& action);
107 bool resolveAttributes(const geo::Primitive& input, geo::Primitive& output, String& error);
108
109 std::shared_ptr<CompiledScript> compiled_;
110 std::vector<AttributeBinding> bindings_;
111
112 BindingValues<floatT> floats_;
113 BindingValues<intT> ints_;
114 BindingValues<Vector3> vectors_;
115 BindingValues<boolT> bools_;
116
117 // The point offset followed by a pointer to each binding's value.
118 std::vector<ScriptArgument> arguments_;
119};
120
121} // namespace enzo::expr
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
The world a single expression evaluation reads and writes to.
Definition ExpressionContext.h:28
A compiled script that runs once per point and edits the point's attributes.
Definition PointScript.h:40
bool addWrittenAttributes(geo::Primitive &output, String &error) const
Adds every attribute the script writes to the output, filled with the default value.
Definition PointScript.cpp:433
static std::shared_ptr< PointScript > compile(const String &code, String &error)
Returns the compiled script for the user's code.
Definition PointScript.cpp:395
std::shared_ptr< PointScript > clone() const
Returns a copy that runs in its own context.
Definition PointScript.cpp:426
bool run(const geo::Primitive &input, geo::Primitive &output, Offset begin, Offset end, const ExpressionContext *context, String &error)
Runs the script for each valid point in a range.
Definition PointScript.cpp:489
const std::vector< AttributeBinding > & getBindings() const
Returns every attribute the code binds, in the order they first appear.
Definition PointScript.h:47
Base class for all primitive types in the engine.
A point attribute the script reads or writes as @name.
Definition PointScript.h:20