MNE-CPP 0.1.9
A Framework for Electrophysiology
Loading...
Searching...
No Matches
custommesh.cpp
Go to the documentation of this file.
1//=============================================================================================================
36//=============================================================================================================
37// INCLUDES
38//=============================================================================================================
39
40#include "custommesh.h"
41
42//=============================================================================================================
43// QT INCLUDES
44//=============================================================================================================
45
46#include <QSharedPointer>
47#include <QVector3D>
48
49//=============================================================================================================
50// EIGEN INCLUDES
51//=============================================================================================================
52
53//=============================================================================================================
54// USED NAMESPACES
55//=============================================================================================================
56
57using namespace DISP3DLIB;
58using namespace Eigen;
59
60//=============================================================================================================
61// DEFINE MEMBER METHODS
62//=============================================================================================================
63
65: Qt3DRender::QGeometryRenderer()
66, m_iNumVert(0)
67{
68 init();
69}
70
71//=============================================================================================================
72
73CustomMesh::CustomMesh(const MatrixX3f& tMatVert,
74 const MatrixX3f& tMatNorm,
75 const MatrixXi& tMatTris,
76 const MatrixX4f& tMatColors,
77 Qt3DRender::QGeometryRenderer::PrimitiveType primitiveType)
78: Qt3DRender::QGeometryRenderer()
79, m_iNumVert(tMatVert.rows())
80{
81 init();
82
83 setMeshData(tMatVert,
84 tMatNorm,
85 tMatTris,
86 tMatColors,
87 primitiveType);
88}
89
90//=============================================================================================================
91
93{
94 m_pCustomGeometry = new QT_COMPATIBILITY_3D::QGeometry(this);
95
96 this->setGeometry(m_pCustomGeometry);
97
98 m_pVertexDataBuffer = new QT_COMPATIBILITY_3D::QBuffer();
99 m_pNormalDataBuffer = new QT_COMPATIBILITY_3D::QBuffer();
100 m_pColorDataBuffer = new QT_COMPATIBILITY_3D::QBuffer();
101 m_pIndexDataBuffer = new QT_COMPATIBILITY_3D::QBuffer();
102
103 m_pIndexAttribute = new QT_COMPATIBILITY_3D::QAttribute();
104 m_pIndexAttribute->setAttributeType(QT_COMPATIBILITY_3D::QAttribute::IndexAttribute);
105 m_pIndexAttribute->setVertexBaseType(QT_COMPATIBILITY_3D::QAttribute::UnsignedInt);
106 m_pIndexAttribute->setByteOffset(0);
108
109 m_pVertexAttribute = new QT_COMPATIBILITY_3D::QAttribute();
110 m_pVertexAttribute->setAttributeType(QT_COMPATIBILITY_3D::QAttribute::VertexAttribute);
111 m_pVertexAttribute->setVertexBaseType(QT_COMPATIBILITY_3D::QAttribute::Float);
112 m_pVertexAttribute->setVertexSize(3);
113 m_pVertexAttribute->setByteOffset(0);
114 m_pVertexAttribute->setByteStride(3 * sizeof(float));
115 m_pVertexAttribute->setName(QT_COMPATIBILITY_3D::QAttribute::defaultPositionAttributeName());
117
118 m_pNormalAttribute = new QT_COMPATIBILITY_3D::QAttribute();
119 m_pNormalAttribute->setAttributeType(QT_COMPATIBILITY_3D::QAttribute::VertexAttribute);
120 m_pNormalAttribute->setVertexBaseType(QT_COMPATIBILITY_3D::QAttribute::Float);
121 m_pNormalAttribute->setVertexSize(3);
122 m_pNormalAttribute->setByteOffset(0);
123 m_pNormalAttribute->setByteStride(3 * sizeof(float));
124 m_pNormalAttribute->setName(QT_COMPATIBILITY_3D::QAttribute::defaultNormalAttributeName());
126
127 m_pColorAttribute = new QT_COMPATIBILITY_3D::QAttribute();
128 m_pColorAttribute->setAttributeType(QT_COMPATIBILITY_3D::QAttribute::VertexAttribute);
129 m_pColorAttribute->setVertexBaseType(QT_COMPATIBILITY_3D::QAttribute::Float);
130 m_pColorAttribute->setVertexSize(4);
131 m_pColorAttribute->setByteOffset(0);
132 m_pColorAttribute->setByteStride(4 * sizeof(float));
133 m_pColorAttribute->setName(QT_COMPATIBILITY_3D::QAttribute::defaultColorAttributeName());
135
140}
141
142//=============================================================================================================
143
145{
146 m_pVertexDataBuffer->deleteLater();
147 m_pNormalDataBuffer->deleteLater();
148 m_pColorDataBuffer->deleteLater();
149 m_pIndexDataBuffer->deleteLater();
150 m_pCustomGeometry->deleteLater();
151 m_pIndexAttribute->deleteLater();
152 m_pVertexAttribute->deleteLater();
153 m_pNormalAttribute->deleteLater();
154 m_pColorAttribute->deleteLater();
155}
156
157//=============================================================================================================
158
159void CustomMesh::setColor(const Eigen::MatrixX4f& tMatColors)
160{
161 QByteArray colorBufferData;
162 colorBufferData.resize(tMatColors.rows() * 4 * (int)sizeof(float));
163 float *rawColorArray = reinterpret_cast<float *>(colorBufferData.data());
164
165 int idxColor = 0;
166
167 for(int i = 0; i < tMatColors.rows(); ++i) {
168 rawColorArray[idxColor++] = tMatColors(i,0);
169 rawColorArray[idxColor++] = tMatColors(i,1);
170 rawColorArray[idxColor++] = tMatColors(i,2);
171 rawColorArray[idxColor++] = tMatColors(i,3);
172 }
173
174 //Update color
175 m_pColorDataBuffer->setData(colorBufferData);
176
177 //m_pColorAttribute->setBuffer(m_pColorDataBuffer);
178 m_pColorAttribute->setCount(tMatColors.rows());
179}
180
181//=============================================================================================================
182
183void CustomMesh::setNormals(const Eigen::MatrixX3f& tMatNorm)
184{
185 QByteArray normalBufferData;
186 normalBufferData.resize(tMatNorm.rows() * 3 * (int)sizeof(float));
187 float *rawNormalArray = reinterpret_cast<float *>(normalBufferData.data());
188
189 int idxNorm = 0;
190 for(int i = 0; i < tMatNorm.rows(); ++i) {
191 //Normal
192 rawNormalArray[idxNorm++] = tMatNorm(i,0);
193 rawNormalArray[idxNorm++] = tMatNorm(i,1);
194 rawNormalArray[idxNorm++] = tMatNorm(i,2);
195 }
196
197 m_pNormalDataBuffer->setData(normalBufferData);
198
199 //m_pNormalAttribute->setBuffer(m_pNormalDataBuffer);
200 m_pNormalAttribute->setCount(tMatNorm.rows());
201}
202
203//=============================================================================================================
204
205void CustomMesh::setVertex(const Eigen::MatrixX3f& tMatVert)
206{
207 QByteArray vertexBufferData;
208 vertexBufferData.resize(tMatVert.rows() * 3 * (int)sizeof(float));
209 float *rawVertexArray = reinterpret_cast<float *>(vertexBufferData.data());
210
211 int idxVert = 0;
212 for(int i = 0; i < tMatVert.rows(); ++i) {
213 rawVertexArray[idxVert++] = (tMatVert(i,0));
214 rawVertexArray[idxVert++] = (tMatVert(i,1));
215 rawVertexArray[idxVert++] = (tMatVert(i,2));
216 }
217
218 m_pVertexDataBuffer->setData(vertexBufferData);
219
220 //m_pVertexAttribute->setBuffer(m_pVertexDataBuffer);
221 m_pVertexAttribute->setCount(tMatVert.rows());
222}
223
224//=============================================================================================================
225
226void CustomMesh::setIndex(const Eigen::MatrixXi& tMatTris)
227{
228 QByteArray indexBufferData;
229
230 const uint iIndicesCount = tMatTris.rows() * tMatTris.cols();
231 indexBufferData.resize(iIndicesCount * (int)sizeof(uint));
232
233 uint *rawIndexArray = reinterpret_cast<uint *>(indexBufferData.data());
234 int idxTris = 0;
235
236 for(int i = 0; i < tMatTris.rows(); ++i) {
237 //patches/tris/lines
238 for(int f = 0; f < tMatTris.cols(); ++f) {
239 rawIndexArray[idxTris++] = tMatTris(i,f);
240 }
241 }
242
243 m_pIndexDataBuffer->setData(indexBufferData);
244
245 m_pIndexAttribute->setCount(iIndicesCount);
246
247 //Set the final geometry and primitive type
248 this->setVerticesPerPatch(tMatTris.cols());
249 this->setVertexCount(tMatTris.rows()*3);
250}
251
252//=============================================================================================================
253
254void CustomMesh::setMeshData(const MatrixX3f& tMatVert,
255 const MatrixX3f& tMatNorm,
256 const MatrixXi& tMatTris,
257 const MatrixX4f& tMatColors,
258 Qt3DRender::QGeometryRenderer::PrimitiveType primitiveType)
259{
260 m_iNumVert = tMatVert.rows();
261
262 setVertex(tMatVert);
263 setNormals(tMatNorm);
264 setIndex(tMatTris);
265 setColor(tMatColors);
266
267 this->setPrimitiveType(primitiveType);
268
273}
274
275//=============================================================================================================
276
277void CustomMesh::addAttribute(QT_COMPATIBILITY_3D::QAttribute *pAttribute)
278{
279 m_pCustomGeometry->addAttribute(pAttribute);
280}
CustomMesh class declaration.
QPointer< QT_COMPATIBILITY_3D::QBuffer > m_pNormalDataBuffer
Definition custommesh.h:193
QPointer< QT_COMPATIBILITY_3D::QGeometry > m_pCustomGeometry
Definition custommesh.h:197
void setMeshData(const Eigen::MatrixX3f &tMatVert, const Eigen::MatrixX3f &tMatNorm, const Eigen::MatrixXi &tMatTris, const Eigen::MatrixX4f &tMatColors, Qt3DRender::QGeometryRenderer::PrimitiveType primitiveType=Qt3DRender::QGeometryRenderer::Triangles)
QPointer< QT_COMPATIBILITY_3D::QBuffer > m_pColorDataBuffer
Definition custommesh.h:194
QPointer< QT_COMPATIBILITY_3D::QAttribute > m_pColorAttribute
Definition custommesh.h:202
QPointer< QT_COMPATIBILITY_3D::QBuffer > m_pVertexDataBuffer
Definition custommesh.h:192
void setColor(const Eigen::MatrixX4f &tMatColors)
QPointer< QT_COMPATIBILITY_3D::QAttribute > m_pNormalAttribute
Definition custommesh.h:201
QPointer< QT_COMPATIBILITY_3D::QAttribute > m_pVertexAttribute
Definition custommesh.h:200
QPointer< QT_COMPATIBILITY_3D::QAttribute > m_pIndexAttribute
Definition custommesh.h:199
void addAttribute(QT_COMPATIBILITY_3D::QAttribute *pAttribute)
void setVertex(const Eigen::MatrixX3f &tMatVert)
void setIndex(const Eigen::MatrixXi &tMatTris)
QPointer< QT_COMPATIBILITY_3D::QBuffer > m_pIndexDataBuffer
Definition custommesh.h:195
void setNormals(const Eigen::MatrixX3f &tMatNorm)