Enzo
Loading...
Searching...
No Matches
Ramp.h
1#pragma once
2
3#include "Engine/Core/Types.h"
4#include <algorithm>
5#include <vector>
6
7namespace enzo::prm {
8
9class Parameter;
10
12// The full set also includes catmull rom, monotone cubic, bezier, and hermite.
13// Only constant, linear, and b spline are sampled today.
14enum class Interpolation
15{
16 CONSTANT,
17 LINEAR,
18 BSPLINE
19};
20
24class Ramp
25{
26 public:
28 struct Key
29 {
30 floatT position;
31 floatT value;
32 Interpolation interp;
33 };
34
35 Ramp() = default;
36 explicit Ramp(std::vector<Key> keys);
37
39 explicit Ramp(const Parameter& rampParameter);
40
43 floatT sample(floatT position) const
44 {
45 if (lookupTable_.empty()) return 0;
46
47 // Map the position onto the table as a fractional step index.
48 const floatT tableEndPosition = tableStartPosition_ + tablePositionSpan_;
49 const floatT clampedPosition = std::clamp(position, tableStartPosition_, tableEndPosition);
50 const floatT stepIndex = tablePositionSpan_ > 0 ? (clampedPosition - tableStartPosition_) /
51 tablePositionSpan_ * lookupTableSteps
52 : 0;
53
54 // Blend the two table samples bracketing that step.
55 int lowerStep = static_cast<int>(stepIndex);
56 if (lowerStep >= lookupTableSteps) lowerStep = lookupTableSteps - 1;
57 const floatT blend = stepIndex - static_cast<floatT>(lowerStep);
58 return lookupTable_[lowerStep] +
59 (lookupTable_[lowerStep + 1] - lookupTable_[lowerStep]) * blend;
60 }
61
62 bool empty() const;
63 size_t size() const;
64 const Key& key(size_t index) const;
65
66 private:
68 void bake();
69
71 static constexpr int lookupTableSteps = 4096;
72
73 std::vector<Key> keys_;
74 std::vector<floatT> lookupTable_;
75 floatT tableStartPosition_ = 0;
76 floatT tablePositionSpan_ = 0;
77};
78
79} // namespace enzo::prm
Basic attribute, parameter, and node types for Enzo.
Definition Parameter.h:19
A sampled snapshot of a ramp parameter read during cook.
Definition Ramp.h:25
floatT sample(floatT position) const
Samples the ramp at position.
Definition Ramp.h:43
A single ramp control point.
Definition Ramp.h:29