Skip to content

Commit fd38f64

Browse files
authored
fix/crash deleting source during process path generator (#25)
* fixed output tab size declaration on update
1 parent 33c9871 commit fd38f64

File tree

3 files changed

+53
-43
lines changed

3 files changed

+53
-43
lines changed

GBAP/GBAPModel.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -230,4 +230,4 @@ void GBAP::updateInterv(){
230230
: 0;
231231
}
232232

233-
}
233+
}

PathGenerator/PathGeneratorModel.cpp

+52-32
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,23 @@
44

55
namespace Example {
66

7+
// Constants for angle calculations
78
constexpr float TWO_PI = 2.0f * std::numbers::pi;
89
constexpr float FOUR_PI = 4.0f * std::numbers::pi;
910

1011
void PathGenerator::operator()(const halp::tick_flicks& t) {
12+
float relativePos = std::fmod(t.relative_position * inputs.speed, 1.0f);
13+
bool reverse = static_cast<int>(t.relative_position * inputs.speed) % 2 != 0;
1114

12-
float relativePos = fmod(t.relative_position * inputs.speed, 1.0f);
13-
bool reverse = (int(t.relative_position * inputs.speed) % 2) != 0;
15+
outputs.OutTab.value.clear();
1416

17+
// Initialize output positions with first point from each input
18+
for (const auto& val : inputs.pos.value) {
19+
const auto& vec = val.get<std::vector<ossia::value>>();
20+
outputs.OutTab.value.push_back(vec[0].get<ossia::vec2f>());
21+
}
22+
23+
// Dispatch to path function
1524
switch (inputs.Path) {
1625
case 0: linear_path(t, relativePos, reverse); break;
1726
case 1: circle_path(t, relativePos, reverse); break;
@@ -20,52 +29,63 @@ void PathGenerator::operator()(const halp::tick_flicks& t) {
2029
}
2130
}
2231

23-
void PathGenerator::linear_path(const halp::tick_flicks& t, float relativePos, bool reverse) {
24-
25-
for (size_t v = 0; v < outputs.OutTab.value.size(); v++) {
26-
const auto start = inputs.pos.value[v].get<std::vector<ossia::value>>()[0].get<ossia::vec2f>();
27-
const auto end = inputs.pos.value[v].get<std::vector<ossia::value>>()[1].get<ossia::vec2f>();
28-
auto& out = outputs.OutTab.value[v].get<ossia::vec2f>();
32+
// Linear interpolation between two points
33+
void PathGenerator::linear_path(const halp::tick_flicks&, float relativePos, bool reverse) {
34+
for (size_t i = 0; i < outputs.OutTab.value.size(); ++i) {
35+
const auto& vec = inputs.pos.value[i].get<std::vector<ossia::value>>();
36+
const auto& start = vec[0].get<ossia::vec2f>();
37+
const auto& end = vec[1].get<ossia::vec2f>();
38+
auto& out = outputs.OutTab.value[i].get<ossia::vec2f>();
2939

3040
if (inputs.loop && reverse) {
31-
out[0] = end[0] - (end[0] - start[0]) * relativePos;
32-
out[1] = end[1] - (end[1] - start[1]) * relativePos;
41+
out = {
42+
end[0] - (end[0] - start[0]) * relativePos,
43+
end[1] - (end[1] - start[1]) * relativePos
44+
};
3345
} else {
34-
out[0] = start[0] + (end[0] - start[0]) * relativePos;
35-
out[1] = start[1] + (end[1] - start[1]) * relativePos;
46+
out = {
47+
start[0] + (end[0] - start[0]) * relativePos,
48+
start[1] + (end[1] - start[1]) * relativePos
49+
};
3650
}
3751
}
3852
}
3953

40-
void PathGenerator::circle_path(const halp::tick_flicks& t, const float relativePos, const bool reverse) {
54+
// Generate circular motion around a point
55+
void PathGenerator::circle_path(const halp::tick_flicks&, float relativePos, bool reverse) {
56+
float angle = TWO_PI * (inputs.loop && reverse ? relativePos : 1.0f - relativePos);
4157

42-
const float angle = TWO_PI * (inputs.loop && reverse ? 1. - relativePos : relativePos);
58+
for (size_t i = 0; i < outputs.OutTab.value.size(); ++i) {
59+
const auto& center = inputs.pos.value[i].get<std::vector<ossia::value>>()[0].get<ossia::vec2f>();
60+
auto& out = outputs.OutTab.value[i].get<ossia::vec2f>();
4361

44-
for (size_t v = 0; v < outputs.OutTab.value.size(); v++) {
45-
const auto point = inputs.pos.value[v].get<std::vector<ossia::value>>()[0].get<ossia::vec2f>();
46-
auto& out = outputs.OutTab.value[v].get<ossia::vec2f>();
47-
48-
out[0] = point[0] + inputs.radius.value.x * std::cos(angle);
49-
out[1] = point[1] + inputs.radius.value.y * std::sin(angle);
62+
out = {
63+
center[0] + inputs.radius.value.x * std::cos(angle),
64+
center[1] + inputs.radius.value.y * std::sin(angle)
65+
};
5066
}
5167
}
5268

53-
void PathGenerator::spiral_path(const halp::tick_flicks& t,const float relativePos,const bool reverse) {
54-
55-
const float angle = relativePos * FOUR_PI;
56-
const float radX = inputs.radius.value.x * relativePos;
57-
const float radY = inputs.radius.value.y * relativePos;
69+
// Generate expanding spiral motion around a point
70+
void PathGenerator::spiral_path(const halp::tick_flicks&, float relativePos, bool reverse) {
71+
float angle = FOUR_PI * (inputs.loop && reverse ? 1.0f - relativePos : relativePos);
72+
float radX = inputs.radius.value.x * relativePos;
73+
float radY = inputs.radius.value.y * relativePos;
5874

59-
for (size_t v = 0; v < outputs.OutTab.value.size(); v++) {
60-
auto point = inputs.pos.value[v].get<std::vector<ossia::value>>()[0].get<ossia::vec2f>();
61-
auto& out = outputs.OutTab.value[v].get<ossia::vec2f>();
75+
for (size_t i = 0; i < outputs.OutTab.value.size(); ++i) {
76+
const auto& center = inputs.pos.value[i].get<std::vector<ossia::value>>()[0].get<ossia::vec2f>();
77+
auto& out = outputs.OutTab.value[i].get<ossia::vec2f>();
6278

6379
if (inputs.loop && reverse) {
64-
out[0] = point[0] + (inputs.radius.value.x - radX) * std::cos((1 - relativePos) * FOUR_PI);
65-
out[1] = point[1] + (inputs.radius.value.y - radY) * std::sin((1 - relativePos) * FOUR_PI);
80+
out = {
81+
center[0] + (inputs.radius.value.x - radX) * std::cos(angle),
82+
center[1] + (inputs.radius.value.y - radY) * std::sin(angle)
83+
};
6684
} else {
67-
out[0] = point[0] + radX * std::cos(angle);
68-
out[1] = point[1] + radY * std::sin(angle);
85+
out = {
86+
center[0] + radX * std::cos(angle),
87+
center[1] + radY * std::sin(angle)
88+
};
6989
}
7090
}
7191
}

PathGenerator/PathGeneratorModel.hpp

-10
Original file line numberDiff line numberDiff line change
@@ -41,16 +41,6 @@ class PathGenerator {
4141
halp::val_port<"Output", std::vector<ossia::value>> OutTab;
4242
} outputs;
4343

44-
using setup = halp::setup;
45-
46-
void prepare(halp::setup info) {
47-
for (size_t i = 0; i < inputs.pos.value.size(); i++) {
48-
outputs.OutTab.value.push_back(
49-
inputs.pos.value[i].get<std::vector<ossia::value>>()[0].get<ossia::vec2f>()
50-
);
51-
}
52-
}
53-
5444
using tick = halp::tick_flicks;
5545

5646
void operator()(const halp::tick_flicks& t);

0 commit comments

Comments
 (0)