6 loop_(false), smooth_(false), linear_(true), render_count_(0) {
101 std::pair<float, float> point =
points_[i];
102 point_data.push_back(point.first);
103 point_data.push_back(point.second);
104 power_data.push_back(
powers_[i]);
109 data[
"points"] = point_data;
110 data[
"powers"] = power_data;
111 data[
"name"] =
name_;
117 if (!data.count(
"num_points") || !data.count(
"points") || !data.count(
"powers"))
120 json point_data = data[
"points"];
121 json power_data = data[
"powers"];
122 return point_data.is_array() && power_data.is_array();
127 json point_data = data[
"points"];
128 json power_data = data[
"powers"];
130 if (data.count(
"name"))
131 name_ = data[
"name"].get<std::string>();
134 if (data.count(
"smooth"))
138 num_read = std::min(num_read,
static_cast<int>(power_data.size()));
140 for (
int i = 0; i < num_read; ++i) {
141 points_[i] = std::pair<float, float>(point_data[2 * i], point_data[2 * i + 1]);
153 std::pair<float, float> last_point =
points_[point_index];
154 float current_power = 0.0f;
155 std::pair<float, float> current_point =
points_[point_index];
158 last_point.first -= 1.0f;
164 if (current_point.first > last_point.first)
165 t = (x - last_point.first) / (current_point.first - last_point.first);
172 float y = last_point.second + t * (current_point.second - last_point.second);
175 while (x > current_point.first && point_index <
num_points_) {
178 last_point = current_point;
181 current_point.first += 1.0f;
201 int index = scaled_phase;
207 points_[0] == std::pair<float, float>(0.0f, 1.0f) &&
208 points_[1] == std::pair<float, float>(1.0f, 0.0f);
214 std::pair<float, float> first =
points_[index_from];
215 std::pair<float, float> second =
points_[index_to];
216 float power =
powers_[index_from];
218 float width = second.first - first.first;
220 return second.second;
222 float t = (x - first.first) / width;
227 return t * (second.second - first.second) + first.second;
254 float x = (
points_[index - 1].first +
points_[index].first) * 0.5f;
270 float tmp_x = 1.0f -
points_[i].first;
271 float tmp_y =
points_[i].second;
std::string name_
The name of the line shape.
Definition line_generator.h:374
int render_count_
Count of how many times render() was called.
Definition line_generator.h:385
std::pair< float, float > lastPoint() const
Returns the last point in the line.
Definition line_generator.h:252
void initSin()
Initializes the line as a sine-like shape.
Definition line_generator.cpp:54
float valueAtPhase(float phase)
Gets the line value at a given normalized phase.
Definition line_generator.cpp:199
bool loop_
Whether the line loops at the end.
Definition line_generator.h:382
void render()
Renders the line into the internal buffer based on the current points and settings.
Definition line_generator.cpp:149
void checkLineIsLinear()
Checks if the line is the default linear shape.
Definition line_generator.cpp:205
LineGenerator(int resolution=kDefaultResolution)
Constructs a LineGenerator with a given resolution.
Definition line_generator.cpp:5
float powers_[kMaxPoints]
Powers controlling interpolation shape between points.
Definition line_generator.h:377
void addMiddlePoint(int index)
Inserts a point exactly between two existing points.
Definition line_generator.cpp:251
float getValueBetweenPoints(float x, int index_from, int index_to)
Interpolates a value between two points at a given x position.
Definition line_generator.cpp:211
int num_points_
Current number of points.
Definition line_generator.h:378
static bool isValidJson(json data)
Checks if a given JSON object contains valid line data.
Definition line_generator.cpp:116
void removePoint(int index)
Removes the point at a specified index.
Definition line_generator.cpp:259
void flipHorizontal()
Flips the line horizontally around the x=0.5 vertical axis.
Definition line_generator.cpp:268
void initTriangle()
Initializes the line as a triangle shape.
Definition line_generator.cpp:23
force_inline int resolution() const
Gets the resolution of the line's internal buffer.
Definition line_generator.h:266
std::pair< float, float > points_[kMaxPoints]
Array of points defining the line shape.
Definition line_generator.h:376
std::unique_ptr< vital::mono_float[]> buffer_
Internal buffer holding the rendered line values.
Definition line_generator.h:381
static force_inline float smoothTransition(float t)
Smooth transition function for smoothing between points.
Definition line_generator.h:49
float getValueAtPhase(float phase)
Returns the value of the line at a given phase by searching through the points.
Definition line_generator.cpp:230
void jsonToState(json data)
Restores the line state from a given JSON object.
Definition line_generator.cpp:125
void initSawDown()
Initializes the line as a "saw down" shape.
Definition line_generator.cpp:82
static constexpr int kMaxPoints
Maximum number of points that can define the line.
Definition line_generator.h:25
void initSquare()
Initializes the line as a square shape.
Definition line_generator.cpp:37
json stateToJson()
Converts the current state of the line into a JSON object.
Definition line_generator.cpp:96
void initLinear()
Initializes the line to a simple linear shape (from 1.0 at x=0 to 0.0 at x=1).
Definition line_generator.cpp:11
bool linear_
Whether the line is the simple linear shape.
Definition line_generator.h:384
bool smooth_
Whether to apply smoothing between points.
Definition line_generator.h:383
static constexpr int kExtraValues
Extra buffer values used for interpolation outside the main range.
Definition line_generator.h:39
void addPoint(int index, std::pair< float, float > position)
Inserts a new point into the line at a specified index.
Definition line_generator.cpp:239
void flipVertical()
Flips the line vertically around y=0.5.
Definition line_generator.cpp:286
void initSawUp()
Initializes the line as a "saw up" shape.
Definition line_generator.cpp:68
int resolution_
Resolution of the internal buffer.
Definition line_generator.h:379
#define VITAL_ASSERT(x)
Definition common.h:11
Contains faster but less accurate versions of utility math functions, such as exponential,...
nlohmann::json json
Definition line_generator.h:7
force_inline mono_float powerScale(mono_float value, mono_float power)
A power-scaling function to map a linear range to a curved response.
Definition futils.h:455
force_inline poly_float clamp(poly_float value, mono_float min, mono_float max)
Clamps each lane of a vector to [min, max].
Definition poly_utils.h:306
force_inline poly_float interpolate(poly_float from, poly_float to, mono_float t)
Performs a linear interpolation between two poly_floats using a scalar t in [0..1].
Definition poly_utils.h:182