3#include <ossia/detail/config.hpp>
5#include <ossia/detail/flat_map.hpp>
6#include <ossia/detail/optional.hpp>
11#include <ossia/network/base/node.hpp>
12#include <ossia/network/base/parameter.hpp>
14#include <ossia/network/value/value.hpp>
30static constexpr auto curve_segment_type_map()
32 if constexpr(std::is_same_v<T, int>)
33 return ossia::curve_segment_type::INT;
34 else if constexpr(std::is_same_v<T, int64_t>)
35 return ossia::curve_segment_type::INT64;
36 else if constexpr(std::is_same_v<T, float>)
37 return ossia::curve_segment_type::FLOAT;
38 else if constexpr(std::is_same_v<T, double>)
39 return ossia::curve_segment_type::DOUBLE;
40 else if constexpr(std::is_same_v<T, bool>)
41 return ossia::curve_segment_type::BOOL;
42 else if constexpr(std::is_same_v<T, ossia::value>)
43 return ossia::curve_segment_type::ANY;
48template <
typename K,
typename V>
49using curve_map = ossia::flat_map<K, V>;
51template <
typename X,
typename Y>
61 using abscissa_type = X;
62 using ordinate_type = Y;
63 using curve_type = curve<X, Y>;
64 using map_type = curve_map<X, std::pair<Y, ossia::curve_segment<Y>>>;
67 curve(
const curve& other)
71 m_y0_destination = other.m_y0_destination;
73 m_points = other.m_points;
75 m_y0_cacheUsed =
false;
78 curve(curve&& other)
noexcept
82 m_y0_destination = std::move(other.m_y0_destination);
84 m_points = std::move(other.m_points);
86 m_y0_cacheUsed =
false;
89 curve& operator=(
const curve& other)
93 m_y0_destination = other.m_y0_destination;
95 m_points = other.m_points;
97 m_y0_cacheUsed =
false;
100 curve& operator=(curve&& other)
noexcept
104 m_y0_destination = std::move(other.m_y0_destination);
106 m_points = std::move(other.m_points);
108 m_y0_cacheUsed =
false;
171 static Y convert_to_template_type_value(
174 void reserve(std::size_t count) { m_points.reserve(count); }
179 mutable std::optional<ossia::destination> m_y0_destination;
181 mutable map_type m_points;
183 mutable Y m_y0_cache;
185 mutable bool m_y0_cacheUsed =
false;
188template <
typename X,
typename Y>
191 m_y0_cacheUsed =
false;
194template <
typename X,
typename Y>
199 m_points.emplace_hint(
200 m_points.end(), abscissa, std::make_pair(
value, std::move(segment)));
205template <
typename X,
typename Y>
208 return m_points.erase(abscissa) > 0;
211template <
typename X,
typename Y>
222 auto it = m_points.lower_bound(abscissa);
223 if(it == m_points.end())
224 return m_points.empty() ? y0 : std::prev(it)->second.first;
228 if(it != m_points.begin())
230 auto prev = std::prev(it);
231 if(prev->first >= x0)
233 lastAbscissa = prev->first;
234 lastValue = prev->second.first;
238 return it->second.second(
239 ((
double)abscissa - (
double)lastAbscissa)
240 / ((
double)it->first - (
double)lastAbscissa),
241 lastValue, it->second.first);
244template <
typename X,
typename Y>
247 static constexpr auto type
248 = std::make_pair(curve_segment_type_map<X>(), curve_segment_type_map<Y>());
252template <
typename X,
typename Y>
258template <
typename X,
typename Y>
261 if(!m_y0_destination)
268 const destination& dest = *m_y0_destination;
269 m_y0_cacheUsed =
true;
270 m_y0_cache = convert_to_template_type_value(
277template <
typename X,
typename Y>
283template <
typename X,
typename Y>
289template <
typename X,
typename Y>
292 return m_y0_destination;
295template <
typename X,
typename Y>
298 m_y0_destination = destination;
301template <
typename X,
typename Y>
307template <
typename X,
typename Y>
308inline Y curve<X, Y>::convert_to_template_type_value(
309 const value& v, ossia::destination_index::const_iterator idx)
311 using namespace ossia;
315 destination_index::const_iterator index;
316 Y operator()(int32_t i)
const {
return i; }
317 Y operator()(
float f)
const {
return f; }
318 Y operator()(
bool b)
const {
return b; }
319 Y operator()(
char c)
const {
return c; }
320 Y operator()(vec2f vec)
const {
return vec[*index]; }
321 Y operator()(vec3f vec)
const {
return vec[*index]; }
322 Y operator()(vec4f vec)
const {
return vec[*index]; }
323 Y operator()(
const std::vector<ossia::value>& t)
const
325 auto& val = t[*index];
326 return convert_to_template_type_value(val, index + 1);
328 Y operator()(
const value_map_type& t)
const
330#if defined(__cpp_exceptions)
331 throw invalid_value_type_error(
332 "curve_impl::convertToTemplateTypeValue: "
333 "Cannot convert Map to a numeric type");
338 Y operator()(impulse)
const
340#if defined(__cpp_exceptions)
341 throw invalid_value_type_error(
342 "curve_impl::convertToTemplateTypeValue: "
343 "Cannot convert Impulse to a numeric type");
347 Y operator()(
const std::string& str)
const
349#if defined(__cpp_exceptions)
350 throw invalid_value_type_error(
351 "curve_impl::convertToTemplateTypeValue: "
352 "Cannot convert String to a numeric type");
358#if defined(__cpp_exceptions)
359 throw invalid_value_type_error(
360 "curve_impl::convertToTemplateTypeValue: "
385 : mValue{std::move(v)}
388 ~constant_curve()
override;
389 constant_curve() =
default;
390 constant_curve(
const constant_curve&) =
delete;
391 constant_curve(constant_curve&&) =
delete;
392 constant_curve& operator=(
const constant_curve&) =
delete;
393 constant_curve& operator=(constant_curve&&) =
delete;
395 [[nodiscard]]
ossia::value value()
const {
return mValue; }
399 return std::make_pair(
400 ossia::curve_segment_type::DOUBLE, ossia::curve_segment_type::ANY);
void reset() override
Clear internal data structures of the curve.
Definition curve.hpp:403
curve_type get_type() const override
get the type of both coordinates
Definition curve.hpp:397
The curve_abstract class.
Definition curve_abstract.hpp:42
X get_x0() const
get initial point abscissa
Definition curve.hpp:253
void reset() override
Clear internal data structures of the curve.
Definition curve.hpp:189
bool remove_point(X abscissa)
remove a point from the curve
Definition curve.hpp:206
void set_x0(X value)
set initial point abscissa
Definition curve.hpp:278
bool add_point(ossia::curve_segment< Y > &&segment, X abscissa, Y value)
add a segment to reach a target point to the curve
Definition curve.hpp:196
std::optional< destination > get_y0_destination() const
get initial point ordinate destination
Definition curve.hpp:290
map_type get_points() const
get points of the curve
Definition curve.hpp:302
Y value_at(X abscissa) const
get value at an abscissa
Definition curve.hpp:212
void set_y0(Y value)
set initial point ordinate
Definition curve.hpp:284
ossia::curve_type get_type() const override
get the type of both coordinates
Definition curve.hpp:245
virtual ~curve()=default
destructor
void set_y0_destination(const ossia::destination &destination)
set initial curve ordinate using a Destination
Definition curve.hpp:296
Y get_y0() const
get initial point ordinate
Definition curve.hpp:259
ossia::value fetch_value()
Pulls and clone the value.
Definition ossia/network/base/parameter.cpp:61
The value class.
Definition value.hpp:173