Vital
|
A class for generating and storing a line shape, defined by a series of points and associated powers. More...
#include <line_generator.h>
Public Member Functions | |
LineGenerator (int resolution=kDefaultResolution) | |
Constructs a LineGenerator with a given resolution. | |
virtual | ~LineGenerator () |
Virtual destructor. | |
void | setLoop (bool loop) |
Sets whether the line should loop at the end. | |
void | setName (const std::string &name) |
Sets a name identifier for the line. | |
void | setLastBrowsedFile (const std::string &path) |
Stores the last browsed file path associated with this line. | |
void | setSmooth (bool smooth) |
Enables or disables smoothing behavior. | |
void | initLinear () |
Initializes the line to a simple linear shape (from 1.0 at x=0 to 0.0 at x=1). | |
void | initTriangle () |
Initializes the line as a triangle shape. | |
void | initSquare () |
Initializes the line as a square shape. | |
void | initSin () |
Initializes the line as a sine-like shape. | |
void | initSawUp () |
Initializes the line as a "saw up" shape. | |
void | initSawDown () |
Initializes the line as a "saw down" shape. | |
void | render () |
Renders the line into the internal buffer based on the current points and settings. | |
json | stateToJson () |
Converts the current state of the line into a JSON object. | |
void | jsonToState (json data) |
Restores the line state from a given JSON object. | |
float | valueAtPhase (float phase) |
Gets the line value at a given normalized phase. | |
void | checkLineIsLinear () |
Checks if the line is the default linear shape. | |
float | getValueBetweenPoints (float x, int index_from, int index_to) |
Interpolates a value between two points at a given x position. | |
float | getValueAtPhase (float phase) |
Returns the value of the line at a given phase by searching through the points. | |
std::string | getName () const |
Gets the current name of the line. | |
std::string | getLastBrowsedFile () const |
Gets the last browsed file path associated with this line. | |
void | addPoint (int index, std::pair< float, float > position) |
Inserts a new point into the line at a specified index. | |
void | addMiddlePoint (int index) |
Inserts a point exactly between two existing points. | |
void | removePoint (int index) |
Removes the point at a specified index. | |
void | flipHorizontal () |
Flips the line horizontally around the x=0.5 vertical axis. | |
void | flipVertical () |
Flips the line vertically around y=0.5. | |
std::pair< float, float > | lastPoint () const |
Returns the last point in the line. | |
float | lastPower () const |
Returns the power (interpolation shape factor) of the last point. | |
force_inline int | resolution () const |
Gets the resolution of the line's internal buffer. | |
force_inline bool | linear () const |
Indicates whether the line is currently a simple linear shape. | |
force_inline bool | smooth () const |
Indicates whether smoothing is enabled. | |
force_inline vital::mono_float * | getBuffer () const |
Gets a pointer to the internal buffer used for interpolation. | |
force_inline vital::mono_float * | getCubicInterpolationBuffer () const |
Gets a pointer to the buffer used for cubic interpolation. | |
force_inline std::pair< float, float > | getPoint (int index) const |
Returns a point at the given index. | |
force_inline float | getPower (int index) const |
Returns the power at the given index. | |
force_inline int | getNumPoints () const |
Returns the current number of points defining the line. | |
force_inline void | setPoint (int index, std::pair< float, float > point) |
Sets the position of a specific point. | |
force_inline void | setPower (int index, float power) |
Sets the power for a specific point. | |
force_inline void | setNumPoints (int num_points) |
Sets the number of points currently in use. | |
int | getRenderCount () const |
Gets the number of times the line has been rendered since initialization. | |
Static Public Member Functions | |
static force_inline float | smoothTransition (float t) |
Smooth transition function for smoothing between points. | |
static bool | isValidJson (json data) |
Checks if a given JSON object contains valid line data. | |
Static Public Attributes | |
static constexpr int | kMaxPoints = 100 |
Maximum number of points that can define the line. | |
static constexpr int | kDefaultResolution = 2048 |
Default resolution of the rendered line buffer. | |
static constexpr int | kExtraValues = 3 |
Extra buffer values used for interpolation outside the main range. | |
Protected Attributes | |
std::string | name_ |
The name of the line shape. | |
std::string | last_browsed_file_ |
The last browsed file path for saving/loading this line. | |
std::pair< float, float > | points_ [kMaxPoints] |
Array of points defining the line shape. | |
float | powers_ [kMaxPoints] |
Powers controlling interpolation shape between points. | |
int | num_points_ |
Current number of points. | |
int | resolution_ |
Resolution of the internal buffer. | |
std::unique_ptr< vital::mono_float[]> | buffer_ |
Internal buffer holding the rendered line values. | |
bool | loop_ |
Whether the line loops at the end. | |
bool | smooth_ |
Whether to apply smoothing between points. | |
bool | linear_ |
Whether the line is the simple linear shape. | |
int | render_count_ |
Count of how many times render() was called. | |
A class for generating and storing a line shape, defined by a series of points and associated powers.
The LineGenerator manages a set of control points and their associated interpolation powers to generate a line shape. This line can represent a waveform, an envelope, or any parameter that changes smoothly (or linearly) over a normalized 0.0 to 1.0 range.
The class supports various presets (linear, triangle, square, sine, sawtooth), optional looping behavior, and configurable smoothing. It also provides JSON serialization and deserialization to save and load states, as well as utility methods to flip the line horizontally or vertically.
LineGenerator::LineGenerator | ( | int | resolution = kDefaultResolution | ) |
Constructs a LineGenerator with a given resolution.
resolution | The number of samples in the generated buffer. Defaults to kDefaultResolution. |
|
inlinevirtual |
Virtual destructor.
void LineGenerator::addMiddlePoint | ( | int | index | ) |
Inserts a point exactly between two existing points.
This finds the midpoint between points[index-1] and points[index], and adds a new point there.
index | The index after which the middle point will be inserted. |
void LineGenerator::addPoint | ( | int | index, |
std::pair< float, float > | position ) |
Inserts a new point into the line at a specified index.
The number of points increases by one. Existing points from index onward are shifted.
index | The position to insert the new point. |
position | The (x, y) coordinates of the new point. |
void LineGenerator::checkLineIsLinear | ( | ) |
Checks if the line is the default linear shape.
This updates the linear_
member based on the current points and smoothness setting.
void LineGenerator::flipHorizontal | ( | ) |
Flips the line horizontally around the x=0.5 vertical axis.
This reverses the order of points and their x-values to mirror the shape horizontally.
void LineGenerator::flipVertical | ( | ) |
Flips the line vertically around y=0.5.
This inverts the y-values of the points, mirroring the shape vertically.
|
inline |
Gets a pointer to the internal buffer used for interpolation.
This returns the buffer starting from index 1, which is typically how the lookup is performed.
|
inline |
Gets a pointer to the buffer used for cubic interpolation.
This returns the buffer from index 0, allowing for additional interpolation techniques.
|
inline |
Gets the last browsed file path associated with this line.
|
inline |
Gets the current name of the line.
|
inline |
Returns the current number of points defining the line.
|
inline |
Returns a point at the given index.
index | The point index (0-based). |
|
inline |
Returns the power at the given index.
index | The point index. |
|
inline |
Gets the number of times the line has been rendered since initialization.
float LineGenerator::getValueAtPhase | ( | float | phase | ) |
Returns the value of the line at a given phase by searching through the points.
phase | A value between 0.0 and 1.0. |
float LineGenerator::getValueBetweenPoints | ( | float | x, |
int | index_from, | ||
int | index_to ) |
Interpolates a value between two points at a given x position.
x | The x-coordinate between the two points. |
index_from | The index of the starting point. |
index_to | The index of the ending point. |
void LineGenerator::initLinear | ( | ) |
Initializes the line to a simple linear shape (from 1.0 at x=0 to 0.0 at x=1).
void LineGenerator::initSawDown | ( | ) |
Initializes the line as a "saw down" shape.
void LineGenerator::initSawUp | ( | ) |
Initializes the line as a "saw up" shape.
void LineGenerator::initSin | ( | ) |
Initializes the line as a sine-like shape.
void LineGenerator::initSquare | ( | ) |
Initializes the line as a square shape.
void LineGenerator::initTriangle | ( | ) |
Initializes the line as a triangle shape.
|
static |
Checks if a given JSON object contains valid line data.
data | The JSON object to check. |
void LineGenerator::jsonToState | ( | json | data | ) |
Restores the line state from a given JSON object.
data | A JSON object previously generated by stateToJson(). |
|
inline |
Returns the last point in the line.
|
inline |
Returns the power (interpolation shape factor) of the last point.
|
inline |
Indicates whether the line is currently a simple linear shape.
void LineGenerator::removePoint | ( | int | index | ) |
Removes the point at a specified index.
index | The position of the point to remove. |
void LineGenerator::render | ( | ) |
Renders the line into the internal buffer based on the current points and settings.
This method should be called after modifying points, powers, or other parameters. It updates the internal buffer used for fast lookups.
|
inline |
Gets the resolution of the line's internal buffer.
|
inline |
Stores the last browsed file path associated with this line.
path | The file path. |
|
inline |
Sets whether the line should loop at the end.
If loop is true, the line wraps around from the end back to the start for continuous interpolation.
loop | True to enable looping, false otherwise. |
|
inline |
Sets a name identifier for the line.
name | A string representing the line's name. |
|
inline |
Sets the number of points currently in use.
num_points | The new number of points. |
|
inline |
Sets the position of a specific point.
index | The point index to modify. |
point | The new (x, y) coordinates for that point. |
|
inline |
Sets the power for a specific point.
index | The point index to modify. |
power | The new power value. |
|
inline |
Enables or disables smoothing behavior.
Smoothing affects how values between points are interpolated. When smoothing is enabled, the transitions between points are softened.
smooth | True to enable smoothing, false to disable. |
|
inline |
Indicates whether smoothing is enabled.
|
inlinestatic |
Smooth transition function for smoothing between points.
This function applies a sine-based smoothing to a normalized parameter t, producing a smooth transition curve.
t | A normalized value between 0.0 and 1.0. |
json LineGenerator::stateToJson | ( | ) |
Converts the current state of the line into a JSON object.
The JSON includes the number of points, their positions, powers, name, and smooth setting.
float LineGenerator::valueAtPhase | ( | float | phase | ) |
Gets the line value at a given normalized phase.
phase | A value between 0.0 and 1.0 representing a position along the line. |
|
protected |
Internal buffer holding the rendered line values.
|
staticconstexpr |
Default resolution of the rendered line buffer.
This is the number of samples (points) in the generated line buffer. Higher resolution results in finer granularity.
|
staticconstexpr |
Extra buffer values used for interpolation outside the main range.
A small number of extra samples are stored to facilitate safe interpolation at buffer edges.
|
staticconstexpr |
Maximum number of points that can define the line.
|
protected |
The last browsed file path for saving/loading this line.
|
protected |
Whether the line is the simple linear shape.
|
protected |
Whether the line loops at the end.
|
protected |
The name of the line shape.
|
protected |
Current number of points.
|
protected |
Array of points defining the line shape.
|
protected |
Powers controlling interpolation shape between points.
|
protected |
Count of how many times render() was called.
|
protected |
Resolution of the internal buffer.
|
protected |
Whether to apply smoothing between points.