MNE-CPP 0.1.9
A Framework for Electrophysiology
Loading...
Searching...
No Matches
gpuinterpolationitem.cpp
Go to the documentation of this file.
1//=============================================================================================================
36//=============================================================================================================
37// INCLUDES
38//=============================================================================================================
39
41#include "../../materials/gpuinterpolationmaterial.h"
42#include "../../3dhelpers/custommesh.h"
43
44//=============================================================================================================
45// QT INCLUDES
46//=============================================================================================================
47
48#include <Qt3DCore/QEntity>
49#include <Qt3DCore/QTransform>
50#include <Qt3DRender/QComputeCommand>
51#include <Qt3DRender/QGeometryRenderer>
52#include <QBuffer>
53
54//=============================================================================================================
55// EIGEN INCLUDES
56//=============================================================================================================
57
58//=============================================================================================================
59// USED NAMESPACES
60//=============================================================================================================
61
62using namespace DISP3DLIB;
63using namespace Qt3DRender;
64using namespace Qt3DCore;
65using namespace Eigen;
66
67//=============================================================================================================
68// DEFINE GLOBAL METHODS
69//=============================================================================================================
70
71//=============================================================================================================
72// DEFINE MEMBER METHODS
73//=============================================================================================================
74
75GpuInterpolationItem::GpuInterpolationItem(Qt3DCore::QEntity *p3DEntityParent, int iType, const QString &text)
76: Abstract3DTreeItem(p3DEntityParent, iType, text)
77, m_bIsDataInit(false)
78, m_pGPUMaterial(new GpuInterpolationMaterial())
79, m_pCustomMesh(new CustomMesh)
80, m_pInterpolationMatBuffer(new QT_COMPATIBILITY_3D::QBuffer())
81, m_pOutputColorBuffer(new QT_COMPATIBILITY_3D::QBuffer())
82, m_pSignalDataBuffer(new QT_COMPATIBILITY_3D::QBuffer())
83{
84}
85
86//=============================================================================================================
87
94
95//=============================================================================================================
96
97void GpuInterpolationItem::initData(const MatrixX3f &matVertices,
98 const MatrixX3f &matNormals,
99 const MatrixX3i &matTriangles)
100{
101 if(m_bIsDataInit == true)
102 {
103 qDebug("GpuInterpolationItem::initData data already initialized");
104 return;
105 }
106
107 //Create and add interpolated color signal attribute
108 QT_COMPATIBILITY_3D::QAttribute* pInterpolatedSignalAttrib = new QAttribute;
109 pInterpolatedSignalAttrib->setAttributeType(QT_COMPATIBILITY_3D::QAttribute::VertexAttribute);
110 pInterpolatedSignalAttrib->setVertexBaseType(QT_COMPATIBILITY_3D::QAttribute::Float);
111 pInterpolatedSignalAttrib->setVertexSize(4);
112 pInterpolatedSignalAttrib->setByteOffset(0);
113 pInterpolatedSignalAttrib->setByteStride(4 * sizeof(float));
114 pInterpolatedSignalAttrib->setName(QStringLiteral("OutputColor"));
115 pInterpolatedSignalAttrib->setBuffer(m_pOutputColorBuffer);
116
117 m_pCustomMesh->addAttribute(pInterpolatedSignalAttrib);
118
119 //Create material, init and connect all necessary buffers
120 this->addComponent(m_pGPUMaterial);
121 this->addComponent(m_pCustomMesh);
122
124 this->setMaterialParameter(QVariant::fromValue(m_pInterpolationMatBuffer.data()), QStringLiteral("InterpolationMat"));
126 this->setMaterialParameter(QVariant::fromValue(m_pOutputColorBuffer.data()), QStringLiteral("OutputColor"));
128 this->setMaterialParameter(QVariant::fromValue(m_pSignalDataBuffer.data()), QStringLiteral("InputVec"));
129
130 //Set custom mesh data
131 //generate mesh base color
132 MatrixX4f matVertColor = createVertColor(matVertices.rows(), QColor(0,0,0));
133
134 //Set renderable 3D entity mesh and color data
135 m_pCustomMesh->setMeshData(matVertices,
136 matNormals,
137 matTriangles,
138 matVertColor,
139 Qt3DRender::QGeometryRenderer::Triangles);
140
141 m_bIsDataInit = true;
142}
143
144//=============================================================================================================
145
146void GpuInterpolationItem::setInterpolationMatrix(QSharedPointer<Eigen::SparseMatrix<float> > pMatInterpolationMatrix)
147{
148 if(m_bIsDataInit == false)
149 {
150 qDebug("GpuInterpolationItem::setInterpolationMatrix - item data is not initialized!");
151 return;
152 }
153
154 //qDebug("GpuInterpolationItem::setInterpolationMatrix - buildInterpolationMatrixBuffer");
155 QByteArray interpolationBufferData = buildInterpolationMatrixBuffer(pMatInterpolationMatrix);
156
157 //Init and set interpolation buffer
158 if(m_pInterpolationMatBuffer->data().size() != interpolationBufferData.size()) {
159
160 //Set Rows and Cols
161 this->setMaterialParameter(QVariant::fromValue(pMatInterpolationMatrix->cols()), QStringLiteral("cols"));
162 this->setMaterialParameter(QVariant::fromValue(pMatInterpolationMatrix->rows()), QStringLiteral("rows"));
163
164 m_pOutputColorBuffer->setData(buildZeroBuffer(4 * pMatInterpolationMatrix->rows()));
165 m_pSignalDataBuffer->setData(buildZeroBuffer(pMatInterpolationMatrix->cols()));
166
167 //Set work group size
168 if(!m_pComputeCommand) {
169 m_pComputeCommand = new QComputeCommand();
170 this->addComponent(m_pComputeCommand);
171 }
172 const uint iWorkGroupsSize = static_cast<uint>(std::ceil(std::sqrt(pMatInterpolationMatrix->rows())));
173 m_pComputeCommand->setWorkGroupX(iWorkGroupsSize);
174 m_pComputeCommand->setWorkGroupY(iWorkGroupsSize);
175 m_pComputeCommand->setWorkGroupZ(1);
176
177 //qDebug() << "4 * pMatInterpolationMatrix->rows()"<<4 * pMatInterpolationMatrix->rows();
178 //qDebug() << "pMatInterpolationMatrix->rows()*pMatInterpolationMatrix->cols()"<<pMatInterpolationMatrix->rows()*pMatInterpolationMatrix->cols();
179 }
180
181 m_pInterpolationMatBuffer->setData(interpolationBufferData);
182
183// QByteArray updateData;
184// updateData.resize(pMatInterpolationMatrix->cols() * sizeof(float));
185// float *rawVertexArray = reinterpret_cast<float *>(updateData.data());
186
187// int pos = 0; //matrix element offset
188
189// for(uint i = 0; i < pMatInterpolationMatrix->rows(); ++i) {
190// //qDebug()<<"row "<<i;
191
192// //Extract row
193// int itr = 0;
194// for(uint j = 0; j < pMatInterpolationMatrix->cols(); ++j) {
195// rawVertexArray[itr] = pMatInterpolationMatrix->coeff(i,j);
196// itr++;
197// }
198
199// m_pInterpolationMatBuffer->updateData(pos, updateData);
200// pos += pMatInterpolationMatrix->cols() * sizeof(float); //stride
201// }
202
203 qDebug("GpuInterpolationItem::setInterpolationMatrix - finished");
204}
205
206//=============================================================================================================
207
208void GpuInterpolationItem::addNewRtData(const VectorXf &tSignalVec)
209{
210 if(m_bIsDataInit == false)
211 {
212 qDebug("GpuInterpolationItem::addNewRtData - item data is not initialized!");
213 return;
214 }
215
216 const uint iSignalSize = tSignalVec.rows();
217
218 QByteArray bufferData (iSignalSize * (int)sizeof(float), '0');
219 float *rawVertexArray = reinterpret_cast<float *>(bufferData.data());
220
221 for(uint i = 0; i < iSignalSize; ++i)
222 {
223 rawVertexArray[i] = static_cast<float>(tSignalVec[i]);
224 }
225
226 m_pSignalDataBuffer->setData(bufferData);
227}
228
229//=============================================================================================================
230
231void GpuInterpolationItem::setThresholds(const QVector3D &tVecThresholds)
232{
233 this->setMaterialParameter(QVariant::fromValue(tVecThresholds.x()), QStringLiteral("fThresholdX"));
234 this->setMaterialParameter(QVariant::fromValue(tVecThresholds.z()), QStringLiteral("fThresholdZ"));
235}
236
237//=============================================================================================================
238
239void GpuInterpolationItem::setColormapType(const QString &tColormapType)
240{
241 uint colorMapId = 0;
242 if(tColormapType == QStringLiteral("Hot")) {
243 colorMapId = 0;
244 } else if(tColormapType == QStringLiteral("Hot Negative 1")) {
245 colorMapId = 1;
246 } else if(tColormapType == QStringLiteral("Hot Negative 2")) {
247 colorMapId = 2;
248 } else if(tColormapType == QStringLiteral("Jet")) {
249 colorMapId = 3;
250 }
251
252 this->setMaterialParameter(QVariant::fromValue(colorMapId), QStringLiteral("ColormapType"));
253}
254
255//=============================================================================================================
256
257QByteArray GpuInterpolationItem::buildInterpolationMatrixBuffer(QSharedPointer<Eigen::SparseMatrix<float> > pMatInterpolationMatrix)
258{
259 const uint iRows = pMatInterpolationMatrix->rows();
260 const uint iCols = pMatInterpolationMatrix->cols();
261
262// qDebug() << "(int)sizeof(float)" << (int)sizeof(float);
263// qDebug() << "GpuInterpolationItem::buildInterpolationMatrixBuffer - iRows" << iRows ;
264// qDebug() << "GpuInterpolationItem::buildInterpolationMatrixBuffer - iCols" << iCols;
265// qDebug() << "GpuInterpolationItem::buildInterpolationMatrixBuffer - iRows * iCols " << iRows * iCols;
266// qDebug() << "GpuInterpolationItem::buildInterpolationMatrixBuffer - iRows * iCols * (int)sizeof(float) " << iRows * iCols * (int)sizeof(float);
267
268 QByteArray bufferData (iRows * iCols * (int)sizeof(float), '0');
269
270 // We do not need to use the buildZeroBuffer because we need to go through the non-zero entries again
271 //qDebug() << "GpuInterpolationItem::buildInterpolationMatrixBuffer - Fill Byte Array";
272 float *rawVertexArray = reinterpret_cast<float *>(bufferData.data());
273
274 unsigned int iCtr = 0;
275 for(uint i = 0; i < iRows; ++i) {
276 for(uint j = 0; j < iCols; ++j) {
277 rawVertexArray[iCtr] = static_cast<float>(pMatInterpolationMatrix->coeff(i, j));
278 iCtr++;
279 }
280 }
281
282 //qDebug() << "GpuInterpolationItem::buildInterpolationMatrixBuffer - Finished";
283
284 return bufferData;
285}
286
287//=============================================================================================================
288
289QByteArray GpuInterpolationItem::buildZeroBuffer(const uint tSize)
290{
291 //qDebug() << "GpuInterpolationItem::buildZeroBuffer 0 "<<tSize;
292 QByteArray bufferData (tSize * (int)sizeof(float), '0');
293 float *rawVertexArray = reinterpret_cast<float *>(bufferData.data());
294
295 //qDebug() << "GpuInterpolationItem::buildZeroBuffer 1 ";
296
297 //Set default values
298 for(uint i = 0; i < tSize; ++i) {
299 rawVertexArray[i] = 0.0f;
300 }
301 //qDebug() << "GpuInterpolationItem::buildZeroBuffer 2 ";
302
303 return bufferData;
304}
GpuInterpolationItem class declaration.
Custom mesh functionality.
Definition custommesh.h:94
virtual void setMaterialParameter(const QVariant &data, const QString &sParameterName)
Provides the basic tree item.
static Eigen::MatrixX4f createVertColor(int numVert, const QColor &color=QColor(0, 49, 69))
QPointer< QT_COMPATIBILITY_3D::QBuffer > m_pSignalDataBuffer
QPointer< QT_COMPATIBILITY_3D::QBuffer > m_pInterpolationMatBuffer
virtual void setInterpolationMatrix(QSharedPointer< Eigen::SparseMatrix< float > > pMatInterpolationMatrix)
virtual QByteArray buildInterpolationMatrixBuffer(QSharedPointer< Eigen::SparseMatrix< float > > pMatInterpolationMatrix)
virtual void initData(const Eigen::MatrixX3f &matVertices, const Eigen::MatrixX3f &matNormals, const Eigen::MatrixX3i &matTriangles)
virtual void addNewRtData(const Eigen::VectorXf &tSignalVec)
virtual QByteArray buildZeroBuffer(const uint tSize)
QPointer< GpuInterpolationMaterial > m_pGPUMaterial
QPointer< QT_COMPATIBILITY_3D::QBuffer > m_pOutputColorBuffer
GpuInterpolationItem(Qt3DCore::QEntity *p3DEntityParent=Q_NULLPTR, int iType=Data3DTreeModelItemTypes::GpuInterpolationItem, const QString &text="3D Plot")
virtual void setColormapType(const QString &tColormapType)
QPointer< Qt3DRender::QComputeCommand > m_pComputeCommand
virtual void setThresholds(const QVector3D &tVecThresholds)
Compute shader interpolation material.