MNE-CPP 0.1.9
A Framework for Electrophysiology
Loading...
Searching...
No Matches
geometrymultiplier.cpp
Go to the documentation of this file.
1//=============================================================================================================
36//=============================================================================================================
37// INCLUDES
38//=============================================================================================================
39
40#include "geometrymultiplier.h"
41
42//=============================================================================================================
43// QT INCLUDES
44//=============================================================================================================
45
46#include <Qt3DCore/QNode>
47
48#include <QVector3D>
49#include <QMatrix4x4>
50#include <QColor>
51
52//=============================================================================================================
53// EIGEN INCLUDES
54//=============================================================================================================
55
56//=============================================================================================================
57// USED NAMESPACES
58//=============================================================================================================
59
60using namespace DISP3DLIB;
61using namespace Qt3DRender;
62
63//=============================================================================================================
64// DEFINE GLOBAL METHODS
65//=============================================================================================================
66
67//=============================================================================================================
68// DEFINE MEMBER METHODS
69//=============================================================================================================
70
71GeometryMultiplier::GeometryMultiplier(QSharedPointer<QT_COMPATIBILITY_3D::QGeometry> tGeometry,
72 Qt3DCore::QNode *tParent)
73: QGeometryRenderer(tParent)
74, m_pGeometry(tGeometry)
75, m_pTransformBuffer(new QT_COMPATIBILITY_3D::QBuffer())
76, m_pColorBuffer(new QT_COMPATIBILITY_3D::QBuffer())
77, m_pTransformAttribute(new QT_COMPATIBILITY_3D::QAttribute())
78, m_pColorAttribute(new QT_COMPATIBILITY_3D::QAttribute())
79{
80 init();
81}
82
83//=============================================================================================================
84
86{
87 m_pGeometry->deleteLater();
88 m_pTransformBuffer->deleteLater();
89 m_pColorBuffer->deleteLater();
90 m_pTransformAttribute->deleteLater();
91 m_pColorAttribute->deleteLater();
92}
93
94//=============================================================================================================#
95
96void GeometryMultiplier::setTransforms(const QVector<QMatrix4x4> &tInstanceTansform)
97{
98 //Update buffer content
99 m_pTransformBuffer->setData(buildTransformBuffer(tInstanceTansform));
100
101 this->setInstanceCount(tInstanceTansform.size());
102}
103
104//=============================================================================================================#
105
106void GeometryMultiplier::setColors(const QVector<QColor> &tInstanceColors)
107{
108 //Update buffer content
109 m_pColorBuffer->setData(buildColorBuffer(tInstanceColors));
110
111 if(tInstanceColors.size() > 1) {
112 m_pColorAttribute->setDivisor(1);
113 } else {
114 //enable 1 color for x transforms
115 m_pColorAttribute->setDivisor(0);
116 }
117
118 this->setInstanceCount(tInstanceColors.size());
119}
120
121//=============================================================================================================
122
123void GeometryMultiplier::init()
124{
125 //Set transform attribute parameter
126 m_pTransformAttribute->setName(QStringLiteral("instanceModelMatrix"));
127 m_pTransformAttribute->setAttributeType(QT_COMPATIBILITY_3D::QAttribute::VertexAttribute);
128 m_pTransformAttribute->setVertexBaseType(QT_COMPATIBILITY_3D::QAttribute::Float);
129 m_pTransformAttribute->setVertexSize(16);
130 m_pTransformAttribute->setDivisor(1);
131 m_pTransformAttribute->setByteOffset(0);
132 m_pTransformAttribute->setBuffer(m_pTransformBuffer);
133
134 //Set color attribute parameters
135 m_pColorAttribute->setName(QStringLiteral("instanceColor"));
136 m_pColorAttribute->setAttributeType(QT_COMPATIBILITY_3D::QAttribute::VertexAttribute);
137 m_pColorAttribute->setVertexBaseType(QT_COMPATIBILITY_3D::QAttribute::Float);
138 m_pColorAttribute->setVertexSize(4);
139
140 //Set divisor 0 to enable empty color buffer
141 m_pColorAttribute->setDivisor(0);
142 m_pColorAttribute->setByteOffset(0);
143 m_pColorAttribute->setBuffer(m_pColorBuffer);
144
145 //Set default instance color
146 QVector<QColor> tempColors;
147 tempColors.push_back(QColor(0, 0, 255));
148 setColors(tempColors);
149
150 //set default transforms
151 QVector<QMatrix4x4> tempTrans;
152 tempTrans.push_back(QMatrix4x4());
153 setTransforms(tempTrans);
154
155 //Add Attibute to Geometry
156 m_pGeometry->addAttribute(m_pTransformAttribute);
157 m_pGeometry->addAttribute(m_pColorAttribute);
158
159 //configure geometry renderer
160 this->setPrimitiveType(Qt3DRender::QGeometryRenderer::Triangles);
161 this->setIndexOffset(0);
162 this->setFirstInstance(0);
163 this->setGeometry(m_pGeometry.data());
164}
165
166//=============================================================================================================
167
168QByteArray GeometryMultiplier::buildTransformBuffer(const QVector<QMatrix4x4> &tInstanceTransform)
169{
170 const uint iVertNum = tInstanceTransform.size();
171 const uint iMatrixDim = 4;
172 const uint iMatrixSize = iMatrixDim * iMatrixDim;
173 //create byte array
174 QByteArray bufferData;
175 bufferData.resize(iVertNum * iMatrixSize * (int)sizeof(float));
176 float *rawVertexArray = reinterpret_cast<float *>(bufferData.data());
177
178 //copy transforms into buffer
179 for(uint i = 0 ; i < iVertNum; i++)
180 {
181 const float *rawMatrix = tInstanceTransform.at(i).data();
182 for(uint idx = 0; idx < iMatrixSize; idx++)
183 {
184 rawVertexArray[iMatrixSize * i + idx] = rawMatrix[idx];
185 }
186 }
187
188 return bufferData;
189}
190
191//=============================================================================================================
192
193QByteArray GeometryMultiplier::buildColorBuffer(const QVector<QColor> &tInstanceColor)
194{
195 const uint iVertSize = 4;
196 //create byte array
197 QByteArray bufferData;
198 bufferData.resize(tInstanceColor.size() * iVertSize * (int)sizeof(float));
199 float *rawVertexArray = reinterpret_cast<float *>(bufferData.data());
200
201 //copy colors into buffer
202 for(int i = 0; i < tInstanceColor.size(); i++)
203 {
204 rawVertexArray[iVertSize * i] = tInstanceColor[i].redF();
205 rawVertexArray[iVertSize * i + 1] = tInstanceColor[i].greenF();
206 rawVertexArray[iVertSize * i + 2] = tInstanceColor[i].blueF();
207 rawVertexArray[iVertSize * i + 3] = tInstanceColor[i].alphaF();
208 }
209
210 return bufferData;
211}
GeometryMultiplier class declaration.
GeometryMultiplier(QSharedPointer< QT_COMPATIBILITY_3D::QGeometry > tGeometry, Qt3DCore::QNode *tParent=nullptr)
void setColors(const QVector< QColor > &tInstanceColors)
void setTransforms(const QVector< QMatrix4x4 > &tInstanceTansform)