Vital
Loading...
Searching...
No Matches
LineGenerator Class Reference

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_floatgetBuffer () const
 Gets a pointer to the internal buffer used for interpolation.
 
force_inline vital::mono_floatgetCubicInterpolationBuffer () 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.
 

Detailed Description

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.

Constructor & Destructor Documentation

◆ LineGenerator()

LineGenerator::LineGenerator ( int resolution = kDefaultResolution)

Constructs a LineGenerator with a given resolution.

Parameters
resolutionThe number of samples in the generated buffer. Defaults to kDefaultResolution.

◆ ~LineGenerator()

virtual LineGenerator::~LineGenerator ( )
inlinevirtual

Virtual destructor.

Member Function Documentation

◆ addMiddlePoint()

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.

Parameters
indexThe index after which the middle point will be inserted.

◆ addPoint()

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.

Parameters
indexThe position to insert the new point.
positionThe (x, y) coordinates of the new point.

◆ checkLineIsLinear()

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.

◆ flipHorizontal()

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.

◆ flipVertical()

void LineGenerator::flipVertical ( )

Flips the line vertically around y=0.5.

This inverts the y-values of the points, mirroring the shape vertically.

◆ getBuffer()

force_inline vital::mono_float * LineGenerator::getBuffer ( ) const
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.

Returns
A pointer to the buffer.

◆ getCubicInterpolationBuffer()

force_inline vital::mono_float * LineGenerator::getCubicInterpolationBuffer ( ) const
inline

Gets a pointer to the buffer used for cubic interpolation.

This returns the buffer from index 0, allowing for additional interpolation techniques.

Returns
A pointer to the buffer.

◆ getLastBrowsedFile()

std::string LineGenerator::getLastBrowsedFile ( ) const
inline

Gets the last browsed file path associated with this line.

Returns
The file path as a string.

◆ getName()

std::string LineGenerator::getName ( ) const
inline

Gets the current name of the line.

Returns
The line name.

◆ getNumPoints()

force_inline int LineGenerator::getNumPoints ( ) const
inline

Returns the current number of points defining the line.

Returns
The number of points.

◆ getPoint()

force_inline std::pair< float, float > LineGenerator::getPoint ( int index) const
inline

Returns a point at the given index.

Parameters
indexThe point index (0-based).
Returns
A pair (x, y) representing the point's coordinates.

◆ getPower()

force_inline float LineGenerator::getPower ( int index) const
inline

Returns the power at the given index.

Parameters
indexThe point index.
Returns
The power value for that point.

◆ getRenderCount()

int LineGenerator::getRenderCount ( ) const
inline

Gets the number of times the line has been rendered since initialization.

Returns
The render count.

◆ getValueAtPhase()

float LineGenerator::getValueAtPhase ( float phase)

Returns the value of the line at a given phase by searching through the points.

Parameters
phaseA value between 0.0 and 1.0.
Returns
The interpolated line value at the given phase.

◆ getValueBetweenPoints()

float LineGenerator::getValueBetweenPoints ( float x,
int index_from,
int index_to )

Interpolates a value between two points at a given x position.

Parameters
xThe x-coordinate between the two points.
index_fromThe index of the starting point.
index_toThe index of the ending point.
Returns
The interpolated y-value at x.

◆ initLinear()

void LineGenerator::initLinear ( )

Initializes the line to a simple linear shape (from 1.0 at x=0 to 0.0 at x=1).

◆ initSawDown()

void LineGenerator::initSawDown ( )

Initializes the line as a "saw down" shape.

◆ initSawUp()

void LineGenerator::initSawUp ( )

Initializes the line as a "saw up" shape.

◆ initSin()

void LineGenerator::initSin ( )

Initializes the line as a sine-like shape.

◆ initSquare()

void LineGenerator::initSquare ( )

Initializes the line as a square shape.

◆ initTriangle()

void LineGenerator::initTriangle ( )

Initializes the line as a triangle shape.

◆ isValidJson()

bool LineGenerator::isValidJson ( json data)
static

Checks if a given JSON object contains valid line data.

Parameters
dataThe JSON object to check.
Returns
True if valid, false otherwise.

◆ jsonToState()

void LineGenerator::jsonToState ( json data)

Restores the line state from a given JSON object.

Parameters
dataA JSON object previously generated by stateToJson().

◆ lastPoint()

std::pair< float, float > LineGenerator::lastPoint ( ) const
inline

Returns the last point in the line.

Returns
A pair representing the (x, y) coordinates of the last point.

◆ lastPower()

float LineGenerator::lastPower ( ) const
inline

Returns the power (interpolation shape factor) of the last point.

Returns
A float representing the power of the last point.

◆ linear()

force_inline bool LineGenerator::linear ( ) const
inline

Indicates whether the line is currently a simple linear shape.

Returns
True if linear, false otherwise.

◆ removePoint()

void LineGenerator::removePoint ( int index)

Removes the point at a specified index.

Parameters
indexThe position of the point to remove.

◆ render()

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.

◆ resolution()

force_inline int LineGenerator::resolution ( ) const
inline

Gets the resolution of the line's internal buffer.

Returns
The resolution, typically kDefaultResolution.

◆ setLastBrowsedFile()

void LineGenerator::setLastBrowsedFile ( const std::string & path)
inline

Stores the last browsed file path associated with this line.

Parameters
pathThe file path.

◆ setLoop()

void LineGenerator::setLoop ( bool loop)
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.

Parameters
loopTrue to enable looping, false otherwise.

◆ setName()

void LineGenerator::setName ( const std::string & name)
inline

Sets a name identifier for the line.

Parameters
nameA string representing the line's name.

◆ setNumPoints()

force_inline void LineGenerator::setNumPoints ( int num_points)
inline

Sets the number of points currently in use.

Parameters
num_pointsThe new number of points.

◆ setPoint()

force_inline void LineGenerator::setPoint ( int index,
std::pair< float, float > point )
inline

Sets the position of a specific point.

Parameters
indexThe point index to modify.
pointThe new (x, y) coordinates for that point.

◆ setPower()

force_inline void LineGenerator::setPower ( int index,
float power )
inline

Sets the power for a specific point.

Parameters
indexThe point index to modify.
powerThe new power value.

◆ setSmooth()

void LineGenerator::setSmooth ( bool smooth)
inline

Enables or disables smoothing behavior.

Smoothing affects how values between points are interpolated. When smoothing is enabled, the transitions between points are softened.

Parameters
smoothTrue to enable smoothing, false to disable.

◆ smooth()

force_inline bool LineGenerator::smooth ( ) const
inline

Indicates whether smoothing is enabled.

Returns
True if smoothing is enabled, false otherwise.

◆ smoothTransition()

static force_inline float LineGenerator::smoothTransition ( float t)
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.

Parameters
tA normalized value between 0.0 and 1.0.
Returns
The smoothed value.

◆ stateToJson()

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.

Returns
A JSON object representing the current line state.

◆ valueAtPhase()

float LineGenerator::valueAtPhase ( float phase)

Gets the line value at a given normalized phase.

Parameters
phaseA value between 0.0 and 1.0 representing a position along the line.
Returns
The interpolated line value at the given phase.

Member Data Documentation

◆ buffer_

std::unique_ptr<vital::mono_float[]> LineGenerator::buffer_
protected

Internal buffer holding the rendered line values.

◆ kDefaultResolution

int LineGenerator::kDefaultResolution = 2048
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.

◆ kExtraValues

int LineGenerator::kExtraValues = 3
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.

◆ kMaxPoints

int LineGenerator::kMaxPoints = 100
staticconstexpr

Maximum number of points that can define the line.

◆ last_browsed_file_

std::string LineGenerator::last_browsed_file_
protected

The last browsed file path for saving/loading this line.

◆ linear_

bool LineGenerator::linear_
protected

Whether the line is the simple linear shape.

◆ loop_

bool LineGenerator::loop_
protected

Whether the line loops at the end.

◆ name_

std::string LineGenerator::name_
protected

The name of the line shape.

◆ num_points_

int LineGenerator::num_points_
protected

Current number of points.

◆ points_

std::pair<float, float> LineGenerator::points_[kMaxPoints]
protected

Array of points defining the line shape.

◆ powers_

float LineGenerator::powers_[kMaxPoints]
protected

Powers controlling interpolation shape between points.

◆ render_count_

int LineGenerator::render_count_
protected

Count of how many times render() was called.

◆ resolution_

int LineGenerator::resolution_
protected

Resolution of the internal buffer.

◆ smooth_

bool LineGenerator::smooth_
protected

Whether to apply smoothing between points.


The documentation for this class was generated from the following files: