-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTwoOLSystem.cpp
229 lines (208 loc) · 7.98 KB
/
TwoOLSystem.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
#pragma once
#include <DirectXMath.h>
#include "TwoOLSystem.h" // Include your L-system library header
#include <sstream>
#include <fstream>
#include <iostream>
#include <vector>
std::string LSystemNode::serialize() const {
std::ostringstream oss;
oss << static_cast<int>(type) << " " << nodeid << " " << parentid << " "
<< stage << " " << length << " " << radius << " " << angle << " "
<< static_cast<float>(position.x) << " " << static_cast<float>(position.y) << " " << static_cast<float>(position.z) << " "
<< static_cast<float>(rotation.x) << " " << static_cast<float>(rotation.y) << " " << static_cast<float>(rotation.z) << " " << static_cast<float>(rotation.w);
return oss.str();
}
LSystemNode LSystemNode::deserialize(const std::string& data) {
LSystemNode node{};
std::istringstream iss(data);
int typeInt;
// Output the input data for debugging
wi::backlog::post("Deserializing data: [" + data + "]", wi::backlog::LogLevel::Default);
// Check for leading and trailing whitespaces
if (data.empty()) {
wi::backlog::post("Input data is empty", wi::backlog::LogLevel::Error);
return node;
}
if (!(iss >> typeInt)) {
wi::backlog::post("Failed to read type. Input data: [" + data + "]", wi::backlog::LogLevel::Error);
return node;
}
//wi::backlog::post("Read type: " + std::to_string(typeInt), wi::backlog::LogLevel::Default);
if (!(iss >> node.nodeid)) {
wi::backlog::post("Failed to read nodeid", wi::backlog::LogLevel::Error);
return node;
}
//wi::backlog::post("Read nodeid: " + std::to_string(node.nodeid), wi::backlog::LogLevel::Default);
if (!(iss >> node.parentid)) {
wi::backlog::post("Failed to read parentid", wi::backlog::LogLevel::Error);
return node;
}
//wi::backlog::post("Read parentid: " + std::to_string(node.parentid), wi::backlog::LogLevel::Default);
if (!(iss >> node.stage)) {
wi::backlog::post("Failed to read stage", wi::backlog::LogLevel::Error);
return node;
}
//wi::backlog::post("Read stage: " + std::to_string(node.stage), wi::backlog::LogLevel::Default);
if (!(iss >> node.length)) {
wi::backlog::post("Failed to read length", wi::backlog::LogLevel::Error);
return node;
}
//wi::backlog::post("Read length: " + std::to_string(node.length), wi::backlog::LogLevel::Default);
if (!(iss >> node.radius)) {
wi::backlog::post("Failed to read radius", wi::backlog::LogLevel::Error);
return node;
}
//wi::backlog::post("Read radius: " + std::to_string(node.radius), wi::backlog::LogLevel::Default);
if (!(iss >> node.angle)) {
wi::backlog::post("Failed to read angle", wi::backlog::LogLevel::Error);
return node;
}
//wi::backlog::post("Read angle: " + std::to_string(node.angle), wi::backlog::LogLevel::Default);
if (!(iss >> node.position.x >> node.position.y >> node.position.z)) {
wi::backlog::post("Failed to read position", wi::backlog::LogLevel::Error);
return node;
}
//wi::backlog::post("Read position: " + std::to_string(node.position.x) + ", " + std::to_string(node.position.y) + ", " + std::to_string(node.position.z), wi::backlog::LogLevel::Default);
if (!(iss >> node.rotation.x >> node.rotation.y >> node.rotation.z >> node.rotation.w)) {
wi::backlog::post("Failed to read rotation", wi::backlog::LogLevel::Error);
return node;
}
//wi::backlog::post("Read rotation: " + std::to_string(node.rotation.x) + ", " + std::to_string(node.rotation.y) + ", " + std::to_string(node.rotation.z) + ", " + std::to_string(node.rotation.w), wi::backlog::LogLevel::Default);
node.type = static_cast<NodeType>(typeInt);
// Output the nodeid after deserialization for debugging
wi::backlog::post("Deserialized node ID: " + std::to_string(node.nodeid), wi::backlog::LogLevel::Default);
return node;
}
void saveGenerationsToFile(const std::vector<LSystemGeneration>& generations, const std::string& filename) {
std::ofstream file(filename);
if (file.is_open()) {
for (const auto& generation : generations) {
for (const auto& node : generation) {
file << node.serialize() << "\n";
}
file << "---\n"; // Separate generations with a marker
}
file.close();
}
else {
std::cerr << "Unable to open file for saving: " << filename << "\n";
}
}
std::vector<LSystemGeneration> loadGenerationsFromFile(const std::string& filename) {
std::vector<LSystemGeneration> generations;
std::ifstream file(filename);
if (file.is_open()) {
LSystemGeneration currentGeneration;
std::string line;
while (std::getline(file, line)) {
if (line == "---") {
generations.push_back(std::move(currentGeneration));
currentGeneration.clear();
}
else if (!line.empty()) {
currentGeneration.push_back(LSystemNode::deserialize(line));
}
}
if (!currentGeneration.empty()) {
generations.push_back(std::move(currentGeneration));
}
file.close();
}
else {
std::cerr << "Unable to open file for loading: " << filename << "\n";
}
return generations;
}
void simulateGrowth(std::vector<LSystemGeneration>& generations, double elapsedTime) {
float timeScale = static_cast<float>(elapsedTime) / 1000000.0f; // Convert to seconds
for (auto& gen : generations) {
for (auto& node : gen) {
switch (node.type) {
case NodeType::Forward:
// Example growth simulation: increase length and radius over time
if (elapsedTime > node.stage) {
node.length += timeScale * 0.100f; // Adjust growth rate as needed
node.radius += timeScale * 0.100f;
}
break;
case NodeType::Branch:
// Example growth simulation: increase length and radius over time
if (elapsedTime > node.stage) {
node.length += timeScale * 0.050f; // Adjust growth rate as needed
node.radius += timeScale * 0.050f;
}
break;
case NodeType::Twig:
// Example growth simulation: increase length and radius over time
if (elapsedTime > node.stage) {
node.length += timeScale * 0.010f; // Adjust growth rate as needed
node.radius += timeScale * 0.010f;
}
break;
case NodeType::Leaf:
// Example growth simulation: increase length and radius over time
if (elapsedTime > node.stage) {
node.length += timeScale * 0.005f; // Adjust growth rate as needed
node.radius += timeScale * 0.005f;
}
break;
case NodeType::Decal:
// Example growth simulation: increase length and radius over time
if (elapsedTime > node.stage) {
node.length += timeScale * 0.001f; // Adjust growth rate as needed
node.radius += timeScale * 0.001f;
}
break;
default:
break;
}
}
}
}
void simulateNegativeGrowth(std::vector<LSystemGeneration>& generations, double elapsedTime) {
float timeScale = static_cast<float>(elapsedTime) / 1000000.0f; // Convert to microseconds
for (auto& gen : generations) {
for (auto& node : gen) {
switch (node.type) {
case NodeType::Forward:
// Example negative growth simulation: decrease length and radius over time
if (elapsedTime > node.stage) {
node.length -= timeScale * 0.100f; // Adjust growth rate as needed
node.radius -= timeScale * 0.100f;
}
break;
case NodeType::Branch:
// Example negative growth simulation: decrease length and radius over time
if (elapsedTime > node.stage) {
node.length -= timeScale * 0.050f; // Adjust growth rate as needed
node.radius -= timeScale * 0.050f;
}
break;
case NodeType::Twig:
// Example negative growth simulation: decrease length and radius over time
if (elapsedTime > node.stage) {
node.length -= timeScale * 0.010f; // Adjust growth rate as needed
node.radius -= timeScale * 0.010f;
}
break;
case NodeType::Leaf:
// Example negative growth simulation: decrease length and radius over time
if (elapsedTime > node.stage) {
node.length -= timeScale * 0.005f; // Adjust growth rate as needed
node.radius -= timeScale * 0.005f;
}
break;
case NodeType::Decal:
// Example negative growth simulation: decrease length and radius over time
if (elapsedTime > node.stage) {
node.length -= timeScale * 0.001f; // Adjust growth rate as needed
node.radius -= timeScale * 0.001f;
}
break;
default:
break;
}
}
}
}