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+a) / (1+abs(dt)))">
31 expr;
32 halp::hslider_f32<"Param (a)", halp::range{0., 1., 0.5}> a;
33 halp::hslider_f32<"Param (b)", halp::range{0., 1., 0.5}> b;
34 halp::hslider_f32<"Param (c)", halp::range{0., 1., 0.5}> c;
35 } inputs;
36 struct
37 {
38 value_out port{};
39 } outputs;
40
41 struct State
42 {
43 State()
44 {
45 pov.resize(1024);
46 expr.add_vector("pov", pov);
47 expr.add_variable("po", po);
48
49 expr.add_variable("t", cur_time);
50 expr.add_variable("dt", cur_deltatime);
51 expr.add_variable("pos", cur_pos);
52
53 expr.add_variable("a", a);
54 expr.add_variable("b", b);
55 expr.add_variable("c", c);
56 expr.add_variable("pa", pa);
57 expr.add_variable("pb", pb);
58 expr.add_variable("pc", pc);
59
60 expr.add_variable("m1", m1);
61 expr.add_variable("m2", m2);
62 expr.add_variable("m3", m3);
63 expr.add_constants();
64 expr.register_symbol_table();
65 }
66 std::vector<double> pov;
67 double po{};
68
69 double cur_time{};
70 double cur_deltatime{};
71 double cur_pos{};
72
73 double a{}, b{}, c{};
74 double pa{}, pb{}, pc{};
75
76 double m1{}, m2{}, m3{};
77 ossia::math_expression expr;
78 bool ok = false;
79 } state;
80
81 using tick = halp::tick_flicks;
82
83 void operator()(const tick& tk)
84 {
85 auto& self = this->state;
86 if(!self.expr.set_expression(this->inputs.expr))
87 return;
88
89 setMathExpressionTiming(self, tk);
90 self.a = this->inputs.a;
91 self.b = this->inputs.b;
92 self.c = this->inputs.c;
93
94 auto res = self.expr.result();
95 outputs.port(res);
96
98
99 self.pa = self.a;
100 self.pb = self.b;
101 self.pc = self.c;
102 }
103
104 struct ui
105 {
106 halp_meta(layout, halp::layouts::vbox)
107 struct
108 {
109 halp_meta(layout, halp::layouts::hbox)
110 halp::control<&ins::a> a;
111 halp::control<&ins::b> b;
112 halp::control<&ins::c> c;
113 } controls;
114
115 struct : halp::control<&ins::expr>
116 {
117 halp_flag(dynamic_size);
118 } expr;
119 };
120};
121}
122}
Utilities for OSSIA data structures.
Definition DeviceInterface.hpp:33
Definition MathMapping_generic.hpp:12
Definition MathGenerator.hpp:105
Definition MathGenerator.hpp:15