Loading...
Searching...
No Matches
MathGenerator.hpp
1#pragma once
2#include <Fx/MathHelpers.hpp>
3#include <Fx/MathMapping_generic.hpp>
4#include <Fx/Types.hpp>
5
6#include <ossia/dataflow/value_port.hpp>
7
8#include <halp/layout.hpp>
9
10namespace Nodes
11{
12namespace MathGenerator
13{
14struct Node
15{
16 halp_meta(name, "Expression Value Generator")
17 halp_meta(c_name, "MathGenerator")
18 halp_meta(category, "Control/Generators")
19 halp_meta(manual_url, "https://ossia.io/score-docs/processes/exprtk.html#exprtk-support")
20 halp_meta(author, "ossia score, ExprTK (Arash Partow)")
21 static const constexpr auto description
22 = "Generate a signal from a math expression.\n"
23 "Available variables: a,b,c, t (samples), dt (delta), pos (position "
24 "in parent)\n"
25 "See the documentation at http://www.partow.net/programming/exprtk";
26 halp_meta(uuid, "d757bd0d-c0a1-4aec-bf72-945b722ab85b")
27
28 struct ins
29 {
30 halp::lineedit<"Expression (ExprTK)", "cos(t) + log(pos * (1+abs(x)) / dt)"> expr;
31 halp::hslider_f32<"Param (a)", halp::range{0., 1., 0.5}> a;
32 halp::hslider_f32<"Param (b)", halp::range{0., 1., 0.5}> b;
33 halp::hslider_f32<"Param (c)", halp::range{0., 1., 0.5}> c;
34 } inputs;
35 struct
36 {
37 value_out port{};
38 } outputs;
39
40 struct State
41 {
42 State()
43 {
44 pov.resize(1024);
45 expr.add_vector("pov", pov);
46 expr.add_variable("po", po);
47
48 expr.add_variable("t", cur_time);
49 expr.add_variable("dt", cur_deltatime);
50 expr.add_variable("pos", cur_pos);
51
52 expr.add_variable("a", a);
53 expr.add_variable("b", b);
54 expr.add_variable("c", c);
55 expr.add_variable("pa", pa);
56 expr.add_variable("pb", pb);
57 expr.add_variable("pc", pc);
58
59 expr.add_variable("m1", m1);
60 expr.add_variable("m2", m2);
61 expr.add_variable("m3", m3);
62 expr.add_constants();
63 expr.register_symbol_table();
64 }
65 std::vector<double> pov;
66 double po{};
67
68 double cur_time{};
69 double cur_deltatime{};
70 double cur_pos{};
71
72 double a{}, b{}, c{};
73 double pa{}, pb{}, pc{};
74
75 double m1{}, m2{}, m3{};
76 ossia::math_expression expr;
77 bool ok = false;
78 } state;
79
80 using tick = halp::tick_flicks;
81
82 void operator()(const tick& tk)
83 {
84 auto& self = this->state;
85 if(!self.expr.set_expression(this->inputs.expr))
86 return;
87
88 setMathExpressionTiming(self, tk);
89 self.a = this->inputs.a;
90 self.b = this->inputs.b;
91 self.c = this->inputs.c;
92
93 auto res = self.expr.result();
94 outputs.port(res);
95
97
98 self.pa = self.a;
99 self.pb = self.b;
100 self.pc = self.c;
101 }
102
103 struct ui
104 {
105 halp_meta(layout, halp::layouts::vbox)
106 struct
107 {
108 halp_meta(layout, halp::layouts::hbox)
109 halp::control<&ins::a> a;
110 halp::control<&ins::b> b;
111 halp::control<&ins::c> c;
112 } controls;
113
114 struct : halp::control<&ins::expr>
115 {
116 halp_flag(dynamic_size);
117 } expr;
118 };
119};
120}
121}
Utilities for OSSIA data structures.
Definition DeviceInterface.hpp:33
Definition MathMapping_generic.hpp:12
Definition MathGenerator.hpp:104
Definition MathGenerator.hpp:15