-
Notifications
You must be signed in to change notification settings - Fork 0
/
coronalslicewidget.cpp
464 lines (373 loc) · 16.2 KB
/
coronalslicewidget.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
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
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
#include "coronalslicewidget.h"
#include "commands.h"
CoronalSliceWidget::CoronalSliceWidget(QWidget *parent) : QOpenGLWidget(parent),
displayType(SliceDisplayType::FatOnly), fatImage(NULL), waterImage(NULL),
sliceTexture(0), location(0, 0, 0, 0), startPan(false), moveID(CommandID::CoronalMove)
{
}
void CoronalSliceWidget::setup(NIFTImage *fat, NIFTImage *water)
{
if (!fat || !water)
return;
fatImage = fat;
waterImage = water;
location = QVector4D(0, 0, 0, 0);
}
bool CoronalSliceWidget::isLoaded() const
{
return (fatImage->isLoaded() && waterImage->isLoaded());
}
void CoronalSliceWidget::imageLoaded()
{
sliceTextureInit = false;
dirty |= Dirty::Slice;
update();
}
void CoronalSliceWidget::readSettings(QSettings &settings)
{
(void)settings;
}
void CoronalSliceWidget::writeSettings(QSettings &settings)
{
(void)settings;
}
void CoronalSliceWidget::setLocation(QVector4D location)
{
// If there is no fat or water image currently loaded then return with doing nothing.
if (!isLoaded())
return;
QVector4D transformedLocation = transformLocation(location);
QVector4D delta = transformedLocation - this->location;
this->location = transformedLocation;
// If Y value changed, then update the texture
if (delta.y())
dirty |= Dirty::Slice;
update();
}
QVector4D CoronalSliceWidget::getLocation() const
{
return location;
}
QVector4D CoronalSliceWidget::transformLocation(QVector4D location) const
{
// This function returns a QVector4D that replaces Location::NoChange's from location variable with actual location value
QVector4D temp(location.x() == Location::NoChange, location.y() == Location::NoChange, location.z() == Location::NoChange, location.w() == Location::NoChange);
QVector4D notTemp = QVector4D(1.0f, 1.0f, 1.0f, 1.0f) - temp;
return (location * notTemp) + (this->location * temp);
}
SliceDisplayType CoronalSliceWidget::getDisplayType() const
{
return displayType;
}
void CoronalSliceWidget::setDisplayType(SliceDisplayType type)
{
// If the display type is out of the acceptable range, then do nothing
if (type < SliceDisplayType::FatOnly || type > SliceDisplayType::WaterFraction)
{
qWarning() << "Invalid display type was specified for CoronalSliceWidget: " << (int)type;
return;
}
displayType = type;
// This will recreate the texture because the display type has changed
dirty |= Dirty::Slice;
update();
}
float &CoronalSliceWidget::rscaling()
{
return scaling;
}
QVector3D &CoronalSliceWidget::rtranslation()
{
return translation;
}
QMatrix4x4 CoronalSliceWidget::getMVPMatrix() const
{
// Calculate the ModelViewProjection (MVP) matrix to transform the location of the axial slices
QMatrix4x4 modelMatrix;
// Translate the modelMatrix according to translation vector
// Then scale according to scaling factor
modelMatrix.translate(translation);
modelMatrix.scale(scaling);
// Create the MVP matrix by M * V * P
return (projectionMatrix * viewMatrix * modelMatrix);
}
QMatrix4x4 CoronalSliceWidget::getWindowToNIFTIMatrix(bool includeMVP) const
{
return (getNIFTIToOpenGLMatrix(includeMVP, !includeMVP).inverted() * getWindowToOpenGLMatrix(false, true));
}
QMatrix4x4 CoronalSliceWidget::getWindowToOpenGLMatrix(bool includeMVP, bool flipY) const
{
QMatrix4x4 windowToOpenGLMatrix;
windowToOpenGLMatrix.translate(-1.0f, (flipY ? 1.0f : -1.0f));
windowToOpenGLMatrix.scale(2.0f / width(), (flipY ? -2.0f : 2.0f) / height());
if (includeMVP)
return (getMVPMatrix().inverted() * windowToOpenGLMatrix);
else
return windowToOpenGLMatrix;
}
QMatrix4x4 CoronalSliceWidget::getNIFTIToOpenGLMatrix(bool includeMVP, bool flipY) const
{
QMatrix4x4 NIFTIToOpenGLMatrix;
NIFTIToOpenGLMatrix.translate(-1.0f, (flipY ? 1.0f : -1.0f));
NIFTIToOpenGLMatrix.scale(2.0f / (fatImage->getXDim() - 1), (flipY ? -2.0f : 2.0f) / (fatImage->getZDim() - 1));
if (includeMVP)
return (getMVPMatrix() * NIFTIToOpenGLMatrix);
else
return NIFTIToOpenGLMatrix;
}
void CoronalSliceWidget::setUndoStack(QUndoStack *stack)
{
undoStack = stack;
}
void CoronalSliceWidget::setDirty(int bit)
{
dirty |= bit;
}
void CoronalSliceWidget::resetView()
{
// Reset translation and scaling factors
translation = QVector3D(0.0f, 0.0f, 0.0f);
scaling = 1.0f;
// Update the screen
update();
}
void CoronalSliceWidget::initializeGL()
{
if (!initializeOpenGLFunctions())
{
qCritical() << "Unable to initialize OpenGL functions for CoronalSliceWidget";
return;
}
glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
// Setup matrices and view options
projectionMatrix.setToIdentity();
viewMatrix.setToIdentity();
scaling = 1.0f;
translation = QVector3D(0.0f, 0.0f, 0.0f);
program = new QOpenGLShaderProgram();
program->addShaderFromSourceFile(QOpenGLShader::Vertex, ":/shaders/coronalslice.vert");
program->addShaderFromSourceFile(QOpenGLShader::Fragment, ":/shaders/coronalslice.frag");
program->link();
program->bind();
program->setUniformValue("tex", 0);
initializeSliceView();
}
void CoronalSliceWidget::initializeSliceView()
{
// get context opengl-version
qDebug() << "----------------- CoronalSliceWidget -------------------------";
qDebug() << "Widget OpenGL: " << format().majorVersion() << "." << format().minorVersion();
qDebug() << "Context valid: " << context()->isValid();
qDebug() << "Really used OpenGL: " << context()->format().majorVersion() << "." << context()->format().minorVersion();
qDebug() << "OpenGL information: VENDOR: " << (const char*)glGetString(GL_VENDOR);
qDebug() << " RENDERER: " << (const char*)glGetString(GL_RENDERER);
qDebug() << " VERSION: " << (const char*)glGetString(GL_VERSION);
qDebug() << " GLSL VERSION: " << (const char*)glGetString(GL_SHADING_LANGUAGE_VERSION);
qDebug() << "";
sliceTextureInit = false;
// Setup the axial slice vertices
sliceVertices.clear();
sliceVertices.append(VertexPT(QVector3D(-1.0f, -1.0f, 0.0f), QVector2D(0.0f, 0.0f)));
sliceVertices.append(VertexPT(QVector3D(-1.0f, 1.0f, 0.0f), QVector2D(0.0f, 1.0f)));
sliceVertices.append(VertexPT(QVector3D(1.0f, -1.0f, 0.0f), QVector2D(1.0f, 0.0f)));
sliceVertices.append(VertexPT(QVector3D(1.0f, 1.0f, 0.0f), QVector2D(1.0f, 1.0f)));
// Setup the axial slice indices
sliceIndices.clear();
sliceIndices.append({ 0, 1, 2, 3});
// Generate vertex buffer for the axial slice. The sliceVertices data is uploaded to the VBO
glGenBuffers(1, &sliceVertexBuf);
glBindBuffer(GL_ARRAY_BUFFER, sliceVertexBuf);
glBufferData(GL_ARRAY_BUFFER, sliceVertices.size() * sizeof(VertexPT), sliceVertices.constData(), GL_STATIC_DRAW);
// Generate index buffer for the axial slice. The sliceIndices data is uploaded to the IBO
glGenBuffers(1, &sliceIndexBuf);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, sliceIndexBuf);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sliceIndices.size() * sizeof(GLushort), sliceIndices.constData(), GL_STATIC_DRAW);
// Generate VAO for the axial slice vertices uploaded. Location 0 is the position and location 1 is the texture position
glGenVertexArrays(1, &sliceVertexObject);
glBindVertexArray(sliceVertexObject);
glEnableVertexAttribArray(0);
glEnableVertexAttribArray(1);
glVertexAttribPointer(0, VertexPT::PosTupleSize, GL_FLOAT, true, VertexPT::stride(), static_cast<const char *>(0) + VertexPT::posOffset());
glVertexAttribPointer(1, VertexPT::TexPosTupleSize, GL_FLOAT, true, VertexPT::stride(), static_cast<const char *>(0) + VertexPT::texPosOffset());
// Generate a blank texture for the axial slice
glGenTextures(1, &this->sliceTexture);
// Release (unbind) all
glBindBuffer(GL_ARRAY_BUFFER, 0);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
glBindVertexArray(0);
}
void CoronalSliceWidget::updateTexture()
{
cv::Mat matrix;
// Get the slice for the fat image. If the result is empty then there was an error retrieving the slice
matrix = fatImage->getCoronalSlice(location.y(), true);
if (matrix.empty())
{
qWarning() << "Unable to retrieve coronal slice " << location.y() << " from the fat image. Matrix returned empty.";
return;
}
// The normalize function does quite a bit here. It converts the matrix to a 32-bit float and normalizes it
// between 0.0f to 1.0f based on the min/max value. This does not affect the original 3D matrix in fatImage
cv::normalize(matrix.clone(), matrix, 0.0f, 1.0f, cv::NORM_MINMAX, CV_32FC1);
// Bind the texture and setup the parameters for it
glBindTexture(GL_TEXTURE_2D, sliceTexture);
// These parameters basically say the pixel value is equal to the average of the nearby pixel values when magnifying or minifying the values
// Essentially, when stretching or shrinking the texture to the screen, it will smooth out the pixel values instead of making it look blocky
// like GL_NEAREST would.
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glCheckError();
// Get the OpenGL datatype of the matrix
auto dataType = NumericType::OpenCV(matrix.type());
// Upload the texture data from the matrix to the texture. The internal format is 32 bit floats with one channel for red
glTexImage2D(GL_TEXTURE_2D, 0, GL_R32F, fatImage->getXDim(), fatImage->getZDim(), 0, dataType->openGLFormat, dataType->openGLType, matrix.data);
// If it hasnt been initialized yet or needs to be reinitialized to a different size, use glTexImage2D, otherwise use
// the quicker method glTexSubImage2D which just overwrites old data
if (!sliceTextureInit)
{
glTexImage2D(GL_TEXTURE_2D, 0, GL_R32F, fatImage->getXDim(), fatImage->getZDim(), 0, dataType->openGLFormat, dataType->openGLType, matrix.data);
sliceTextureInit = true;
}
else
glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, fatImage->getXDim(), fatImage->getZDim(), dataType->openGLFormat, dataType->openGLType, matrix.data);
glCheckError();
dirty &= ~Dirty::Slice;
}
void CoronalSliceWidget::resizeGL(int w, int h)
{
// Shuts compiler up about unused variables w and h.
(void)w;
(void)h;
// Do nothing if fat/water images are not loaded
if (!isLoaded())
return;
update();
}
void CoronalSliceWidget::paintGL()
{
// Do nothing if fat/water images are not loaded
if (!isLoaded())
return;
// Update relevant OpenGL objects if dirty
if (dirty & Dirty::Slice)
updateTexture();
// After updating, begin rendering
QPainter painter(this);
painter.beginNativePainting();
glClear(GL_COLOR_BUFFER_BIT);
glCheckError();
// Calculate the ModelViewProjection (MVP) matrix to transform the location of the axial slices
QMatrix4x4 mvpMatrix = getMVPMatrix();
program->bind();
program->setUniformValue("MVP", mvpMatrix);
// Bind the VAO, bind texture to GL_TEXTURE0, bind VBO, bind IBO
// The program that is bound is the index of the curColorMap.
glBindVertexArray(sliceVertexObject);
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, sliceTexture);
glBindBuffer(GL_ARRAY_BUFFER, sliceVertexBuf);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, sliceIndexBuf);
glCheckError();
// Draw a triangle strip of 4 elements which is two triangles. The indices are unsigned shorts
glDrawElements(GL_TRIANGLE_STRIP, 4, GL_UNSIGNED_SHORT, 0);
glCheckError();
// Release (unbind) the binded objects in reverse order
// This is a simple protocol to prevent anything happening to the objects outside of this function without
// explicitly binding the objects
glBindVertexArray(0);
glBindTexture(GL_TEXTURE_2D, 0);
glBindTexture(GL_TEXTURE_1D, 0);
glBindBuffer(GL_ARRAY_BUFFER, 0);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
program->release();
glCheckError();
painter.endNativePainting();
// Draw Crosshair Line (Set matrix to transform NIFTI coordinates -> Window coordinates)
// Then draw a line with a width of 1 from left of screen to right of screen at coronal location
painter.setTransform(getWindowToNIFTIMatrix().inverted().toTransform());
painter.setPen(QPen(Qt::red, 1, Qt::SolidLine, Qt::RoundCap));
painter.drawLine(QPoint(0, location.z()), QPoint(fatImage->getXDim() - 1, location.z()));
}
void CoronalSliceWidget::mouseMoveEvent(QMouseEvent *eventMove)
{
if (!isLoaded())
return;
if (startPan)
{
// Change in mouse x/y based on last mouse position
QPointF curMousePos = eventMove->pos();
QPointF lastMousePos_ = this->lastMousePos;
// Get matrix for converting from window to OpenGL coordinate system
// Note: Do not apply MVP because we do not want to see movement based on
// scaling (this means dont flip it either)
QMatrix4x4 windowToOpenGLMatrix = this->getWindowToOpenGLMatrix(false, true);
// Apply transformation to current and last mouse position
curMousePos = windowToOpenGLMatrix * curMousePos;
lastMousePos_ = windowToOpenGLMatrix * lastMousePos_;
// Get the delta
QPointF delta = (curMousePos - lastMousePos_);
// Push a new move command on the undoStack. This will call the command but also keep track
// of it if an undo or redo action is called. redo function is called immediately.
undoStack->push(new CoronalMoveCommand(delta, this, moveID));
// Set last mouse position to this one
lastMousePos = eventMove->pos();
}
}
void CoronalSliceWidget::mousePressEvent(QMouseEvent *eventPress)
{
if (!isLoaded())
return;
// There are button() and buttons() functions in QMouseEvent that have a very
// important distinction between one another. button() is only going to return
// the button that caused this event while buttons() is a state of all buttons
// held down during this event.
// Since multiple buttons down does not have functionality, the button() function
// is used and if an additional button is down with functionality, the old button
// is turned off and the new takes place.
if (eventPress->button() == Qt::LeftButton || eventPress->button() == Qt::MiddleButton)
{
// Flag to indicate that panning is occuring
// The starting position is stored so to know how much movement has occurred
startPan = true;
moveID = (((int)moveID + 1) > (int)CommandID::CoronalMoveEnd) ? CommandID::CoronalMove : (CommandID)((int)moveID + 1);
lastMousePos = eventPress->pos();
}
}
void CoronalSliceWidget::mouseReleaseEvent(QMouseEvent *eventRelease)
{
if (!isLoaded())
return;
if ((eventRelease->button() == Qt::LeftButton || eventRelease->button() == Qt::MiddleButton) && startPan)
{
startPan = false;
}
}
void CoronalSliceWidget::wheelEvent(QWheelEvent *event)
{
// If a mouse button is down while doing this event, do not scale
if (event->buttons() != Qt::NoButton)
return;
// The unit for angle delta is in eighths of a degree
QPoint numDegrees = event->angleDelta() / 8;
if (!numDegrees.isNull())
{
// Zoom in 5% every 15 degrees which is one step on most mouses
float scaleDelta = numDegrees.y() * (0.05f / 15);
// Clamp the scaleDelta so that the resulting scaling factor is between 0.05f to 3.0f
scaleDelta = std::max(std::min((scaling + scaleDelta), 3.0f), 0.05f) - scaling;
// Push a new scale command on the undo stack which will immediately call redo for an action.
// This keeps track of it if an undo or redo command is called
// Only push to the stack if there is a delta
if (scaleDelta != 0.0f)
undoStack->push(new CoronalScaleCommand(scaleDelta, this));
}
}
CoronalSliceWidget::~CoronalSliceWidget()
{
// Destroy the VAO, VBO, and IBO
glDeleteVertexArrays(1, &sliceVertexObject);
glDeleteBuffers(1, &sliceVertexBuf);
glDeleteBuffers(1, &sliceIndexBuf);
glDeleteTextures(1, &sliceTexture);
delete program;
}