-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
342 lines (296 loc) · 11.6 KB
/
Copy pathmain.cpp
File metadata and controls
342 lines (296 loc) · 11.6 KB
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
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
#include "headers/explosionaxis/explosion_axis_strategy.h"
#include "headers/planes/selecting_planes.h"
#include "headers/planes/cutting_planes.h"
#include "headers/planes/exploded_view.h"
#include "headers/marching_cubes.h"
#include "headers/visual.h"
#include "post_processor.h"
#include "headers/data.h"
#include <iostream>
#include <vector>
#include <cmath>
#include <filesystem>
#include <algorithm> // for std::max
#include "imgui.h"
#include "imgui_impl_glfw.h"
#include "imgui_impl_opengl3.h"
// GLAD & GLFW
#include <glad/glad.h>
#include <GLFW/glfw3.h>
// GLM for matrix transformations
#include <glm/glm.hpp>
#include <glm/gtc/matrix_transform.hpp>
#include <glm/gtc/type_ptr.hpp>
// isoLevel change
void handleIsoLevelChange(
MC::VolumeData &volumeData,
float newIsoLevel,
MC::Mesh &mesh,
unsigned int VAO,
unsigned int VBO,
unsigned int EBO,
MC::Vec3 &explosionAxis,
MC::Vertex &axisStart,
MC::Vertex &axisEnd,
unsigned int axisVBO,
std::vector<MC::CuttingPlane> &cuttingPlanes,
MC::ExplodedView &explodedView)
{
// regenerate Mesh
MC::generateMesh(volumeData, newIsoLevel, mesh);
MC::updateMesh(mesh, VAO, VBO, EBO);
// update axis vertex
float halfLength = mesh.max_dimension * 2.0f;
axisStart.x = mesh.center.x - explosionAxis.x * halfLength;
axisStart.y = mesh.center.y - explosionAxis.y * halfLength;
axisStart.z = mesh.center.z - explosionAxis.z * halfLength;
axisEnd.x = mesh.center.x + explosionAxis.x * halfLength;
axisEnd.y = mesh.center.y + explosionAxis.y * halfLength;
axisEnd.z = mesh.center.z + explosionAxis.z * halfLength;
// update axis VBO
MC::Vertex axisLine[2] = {axisStart, axisEnd};
glBindBuffer(GL_ARRAY_BUFFER, axisVBO);
glBufferData(GL_ARRAY_BUFFER, sizeof(axisLine), axisLine, GL_STATIC_DRAW);
// regenerate cutting planes
cuttingPlanes = MC::generateAdaptiveCuttingPlanes(mesh, explosionAxis, 3);
// recalculate exploded view
MC::cleanupExplodedView(explodedView);
explodedView = computeExplodedView(mesh, cuttingPlanes, explosionAxis, MC::g_explosionDistance);
// set up VAO/VBO/EBO for each clip in the exploded view
for (auto &segment : explodedView.segments)
{
MC::setupSegmentMesh(segment);
}
}
// explosion axis change
void recalculateExplosionAxisAndView(
MC::Mesh &mesh,
MC::Vec3 &explosionAxis,
std::string ¤tExplosionStrategy,
std::string &lastStrategy,
MC::Vertex &axisStart,
MC::Vertex &axisEnd,
unsigned int axisVBO,
std::vector<MC::CuttingPlane> &cuttingPlanes,
MC::ExplodedView &explodedView,
float &lastExplosionDistance)
{
if (lastStrategy != currentExplosionStrategy)
{
std::cout << "Strategy changed from " << lastStrategy
<< " to " << currentExplosionStrategy << std::endl;
lastStrategy = currentExplosionStrategy;
}
float currentExplosionDistance = explodedView.explosionDistance;
bool wasEnabled = explodedView.enabled;
MC::cleanupExplodedView(explodedView);
explosionAxis = MC::computeExplosionAxis(mesh.vertices);
std::cout << "Updated explosion axis: ("
<< explosionAxis.x << ", " << explosionAxis.y << ", " << explosionAxis.z << ")\n";
float length = std::sqrt(
explosionAxis.x * explosionAxis.x +
explosionAxis.y * explosionAxis.y +
explosionAxis.z * explosionAxis.z);
if (length > 0.0001f) {
explosionAxis.x /= length;
explosionAxis.y /= length;
explosionAxis.z /= length;
}
float halfLength = mesh.max_dimension * 2.0f;
axisStart.x = mesh.center.x - explosionAxis.x * halfLength;
axisStart.y = mesh.center.y - explosionAxis.y * halfLength;
axisStart.z = mesh.center.z - explosionAxis.z * halfLength;
axisEnd.x = mesh.center.x + explosionAxis.x * halfLength;
axisEnd.y = mesh.center.y + explosionAxis.y * halfLength;
axisEnd.z = mesh.center.z + explosionAxis.z * halfLength;
MC::Vertex axisLine[2] = {axisStart, axisEnd};
glBindBuffer(GL_ARRAY_BUFFER, axisVBO);
glBufferData(GL_ARRAY_BUFFER, sizeof(axisLine), axisLine, GL_STATIC_DRAW);
cuttingPlanes = MC::generateAdaptiveCuttingPlanes(mesh, explosionAxis, 3); // 3个切割平面
explodedView = computeExplodedView(mesh, cuttingPlanes, explosionAxis, MC::g_explosionDistance);
explodedView.enabled = wasEnabled;
explodedView.explosionDistance = currentExplosionDistance;
MC::updateExplodedViewDisplacements(explodedView, explosionAxis, currentExplosionDistance);
for (auto &segment : explodedView.segments)
{
MC::setupSegmentMesh(segment);
}
lastExplosionDistance = currentExplosionDistance;
}
int main()
{
using namespace MC;
// initialize OpenGL
GLFWwindow *window = initOpenGL();
if (!window){ return -1;}
// initialize ImGui
setupImGui(window);
bool imguiInitialized = true;
auto cleanupAndExit = [&](int exitCode) {
if (imguiInitialized)
{
cleanupImGui();
imguiInitialized = false;
}
glfwDestroyWindow(window);
glfwTerminate();
return exitCode;
};
// symbol for explosion axis recalculate
bool recalculateExplosionAxis = false;
ImGui::GetIO().UserData = &recalculateExplosionAxis;
// create post-process for rendering
PostProcess postProcessor;
int width, height;
glfwGetFramebufferSize(window, &width, &height);
if (!postProcessor.init(width, height))
{
std::cerr << "Failed to initialize post processor\n";
postProcessor.cleanup();
return cleanupAndExit(-1);
}
int lastWidth = width, lastHeight = height;
// open NIfTI file
std::string filePath = openNiftiFileDialog();
if (filePath.empty())
{
std::cerr << "No file selected.\n";
postProcessor.cleanup();
return cleanupAndExit(-1);
}
// load volume data
VolumeData volumeData;
if (!loadNiiFile(filePath, volumeData))
{
std::cerr << "Failed to load file\n";
postProcessor.cleanup();
return cleanupAndExit(-1);
}
// initialize mesh and marching cubes
Mesh mesh;
float isoLevel = volumeData.minValue + (MC::g_isoLevelPercent / 100.0f) * (volumeData.maxValue - volumeData.minValue);
float tempIsoLevel = isoLevel;
generateMesh(volumeData, isoLevel, mesh);
// compute symmetry/explosion axis
std::string currentExplosionStrategy = MC::ExplosionAxisConfig::convertToUIName(MC::getCurrentExplosionStrategyName());
Vec3 explosionAxis = MC::computeExplosionAxis(mesh.vertices);
std::cout << "Computed explosion axis: (" << explosionAxis.x << ", " << explosionAxis.y << ", " << explosionAxis.z << ")\n";
Vertex axisStart, axisEnd;
float halfLength = mesh.max_dimension * 2.0f;
axisStart.x = mesh.center.x - explosionAxis.x * halfLength;
axisStart.y = mesh.center.y - explosionAxis.y * halfLength;
axisStart.z = mesh.center.z - explosionAxis.z * halfLength;
axisEnd.x = mesh.center.x + explosionAxis.x * halfLength;
axisEnd.y = mesh.center.y + explosionAxis.y * halfLength;
axisEnd.z = mesh.center.z + explosionAxis.z * halfLength;
// create VAO/VBO for the symmetry axis line
unsigned int axisVAO, axisVBO;
glGenVertexArrays(1, &axisVAO);
glGenBuffers(1, &axisVBO);
Vertex axisLine[2] = {axisStart, axisEnd};
glBindVertexArray(axisVAO);
glBindBuffer(GL_ARRAY_BUFFER, axisVBO);
glBufferData(GL_ARRAY_BUFFER, sizeof(axisLine), axisLine, GL_STATIC_DRAW);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), (void *)0);
glEnableVertexAttribArray(0);
glBindVertexArray(0);
// create shader program
unsigned int shaderProgram = createShaderProgram();
unsigned int lineShaderProgram = createLineShaderProgram();
// create VAO/VBO/EBO for the mesh
unsigned int VAO, VBO, EBO;
setupMesh(mesh, VAO, VBO, EBO);
// cutting plane & exploded view
std::vector<CuttingPlane> cuttingPlanes = generateAdaptiveCuttingPlanes(mesh, explosionAxis, 3);
ExplodedView explodedView = computeExplodedView(mesh, cuttingPlanes, explosionAxis);
for (auto &segment : explodedView.segments)
{
setupSegmentMesh(segment);
}
// record current status
static std::string lastStrategy = currentExplosionStrategy;
static float lastIsoLevel = isoLevel;
static float lastExplosionDistance = explodedView.explosionDistance;
// main loop
while (!glfwWindowShouldClose(window))
{
glfwPollEvents();
if (!updateViewport(window, &width, &height))
{
glfwWaitEvents();
continue;
}
if (width != lastWidth || height != lastHeight)
{
postProcessor.resize(width, height);
lastWidth = width;
lastHeight = height;
}
// update exploded view
explodedView.enabled = showIntersections;
if (g_explosionDistance != explodedView.explosionDistance)
{
explodedView.explosionDistance = g_explosionDistance;
updateExplodedViewDisplacements(explodedView, explosionAxis, explodedView.explosionDistance);
lastExplosionDistance = explodedView.explosionDistance;
}
// render frame
if (!explodedView.enabled)
{
renderFrame(window, shaderProgram, VAO, mesh, MC::camera, isoLevel,
tempIsoLevel, volumeData, currentExplosionStrategy, postProcessor,
axisVAO, lineShaderProgram);
}
else
{
renderExplodedView(window, explodedView, shaderProgram, mesh, isoLevel, tempIsoLevel, volumeData,
axisVAO, lineShaderProgram, postProcessor);
}
// explosion distance update after being controlled by ui
if (explodedView.explosionDistance != lastExplosionDistance)
{
updateExplodedViewDisplacements(explodedView, explosionAxis, explodedView.explosionDistance);
lastExplosionDistance = explodedView.explosionDistance;
}
tempIsoLevel = volumeData.minValue + (MC::g_tempIsoLevelPercent / 100.0f) * (volumeData.maxValue - volumeData.minValue);
// regenerate mesh if iso value has been changed
if (isoLevel != lastIsoLevel)
{
isoLevel = volumeData.minValue + (MC::g_isoLevelPercent / 100.0f) * (volumeData.maxValue - volumeData.minValue);
handleIsoLevelChange(
volumeData, isoLevel, mesh, VAO, VBO, EBO,
explosionAxis, axisStart, axisEnd, axisVBO,
cuttingPlanes,
explodedView);
lastIsoLevel = isoLevel;
}
// if recalculation is required
bool shouldRecalculate = recalculateExplosionAxis;
if (shouldRecalculate)
{
recalculateExplosionAxisAndView(
mesh, explosionAxis, currentExplosionStrategy, lastStrategy,
axisStart, axisEnd, axisVBO,
cuttingPlanes,
explodedView, lastExplosionDistance);
recalculateExplosionAxis = false;
}
glfwSwapBuffers(window);
}
// clean
glDeleteVertexArrays(1, &VAO);
glDeleteBuffers(1, &VBO);
glDeleteBuffers(1, &EBO);
glDeleteVertexArrays(1, &axisVAO);
glDeleteBuffers(1, &axisVBO);
glDeleteProgram(shaderProgram);
glDeleteProgram(lineShaderProgram);
postProcessor.cleanup();
cleanupExplodedView(explodedView);
cleanupImGui();
imguiInitialized = false;
glfwDestroyWindow(window);
// terminate GLFW
glfwTerminate();
return 0;
}