MNE-CPP  beta 1.0
brainsurfacemesh.cpp
Go to the documentation of this file.
1 //=============================================================================================================
36 #ifndef _USE_MATH_DEFINES
37 # define _USE_MATH_DEFINES // For MSVC
38 #endif
39 
40 
41 //*************************************************************************************************************
42 //=============================================================================================================
43 // INCLUDES
44 //=============================================================================================================
45 
46 #include "brainsurfacemesh.h"
47 
48 
49 //*************************************************************************************************************
50 //=============================================================================================================
51 // QT INCLUDES
52 //=============================================================================================================
53 
54 #include "qtorusmesh.h"
55 #include "qbuffer.h"
56 #include "qattribute.h"
57 #include "qmeshdata.h"
58 
59 #include <cmath>
60 
61 
62 //*************************************************************************************************************
63 //=============================================================================================================
64 // USED NAMESPACES
65 //=============================================================================================================
66 
67 using namespace DISP3DNEWLIB;
68 
69 
70 //*************************************************************************************************************
71 //=============================================================================================================
72 // DEFINE MEMBER METHODS
73 //=============================================================================================================
74 
76 : QAbstractMesh(parent)
77 {
78  update();
79 }
80 
81 
82 //*************************************************************************************************************
83 
84 BrainSurfaceMesh::BrainSurfaceMesh(const Surface &surf, const QMap<int, QColor> &qmVertexColor, QNode *parent)
85 : QAbstractMesh(parent)
86 , m_surface(surf)
87 , m_qmVertexColor(qmVertexColor)
88 {
89  update();
90 }
91 
92 
93 //*************************************************************************************************************
94 
95 void BrainSurfaceMesh::copy(const QNode *ref)
96 {
97  QAbstractMesh::copy(ref);
98  const BrainSurfaceMesh *mesh = static_cast<const BrainSurfaceMesh*>(ref);
99  m_surface = mesh->m_surface;
100  m_qmVertexColor = mesh->m_qmVertexColor;
101 }
102 
103 
104 //*************************************************************************************************************
105 
106 QAbstractMeshFunctorPtr BrainSurfaceMesh::meshFunctor() const
107 {
108  return QAbstractMeshFunctorPtr(new BrainSurfaceMeshFunctor(m_surface, m_qmVertexColor));
109 }
110 
111 
112 //*************************************************************************************************************
113 
114 void BrainSurfaceMesh::updateActivation(const QMap<int, QColor> &qmVertexColor)
115 {
116  //std::cout<<"START::BrainSurfaceMesh::updateActivation"<<std::endl;
117  //std::cout<<"vertexColor.size()"<<qmVertexColor.size()<<std::endl;
118  //std::cout<<"m_qlVertexColor.size()"<<m_qmVertexColor.size()<<std::endl;
119  //std::cout<<"m_surface.rr().cols()"<<m_surface.rr().rows()<<std::endl;
120 
121  if(qmVertexColor.size() != m_qmVertexColor.size()) {
122  std::cout<<"newly provided colors from source estimate do not match loaded number of vertices"<<std::endl;
123  std::cout<<"vertexColor.size()"<<qmVertexColor.size()<<std::endl;
124  std::cout<<"m_qlVertexColor.size()"<<m_qmVertexColor.size()<<std::endl;
125 
126  return;
127  }
128 
129  m_qmVertexColor = qmVertexColor;
130 
131  update();
132  //std::cout<<"END::BrainSurfaceMesh::updateActivation"<<std::endl;
133 }
134 
135 
136 //*************************************************************************************************************
137 
138 int BrainSurfaceMesh::getNumberOfVertices()
139 {
140  return m_surface.rr().rows();
141 }
142 
143 
144 //*************************************************************************************************************
145 
146 BrainSurfaceMeshFunctor::BrainSurfaceMeshFunctor(const Surface &surf, const QMap<int, QColor> &qmVertexColor)
147 : QAbstractMeshFunctor()
148 , m_surface(surf)
149 , m_qmVertexColor(qmVertexColor)
150 {
151 }
152 
153 
154 //*************************************************************************************************************
155 
156 QMeshDataPtr BrainSurfaceMeshFunctor::operator ()()
157 {
158  m_qMeshDataPtr = createSurfaceMesh(m_surface, m_qmVertexColor);
159  return m_qMeshDataPtr;
160 }
161 
162 
163 //*************************************************************************************************************
164 
165 bool BrainSurfaceMeshFunctor::operator ==(const QAbstractMeshFunctor &other) const
166 {
167  const BrainSurfaceMeshFunctor *otherFunctor = dynamic_cast<const BrainSurfaceMeshFunctor *>(&other);
168 // if (otherFunctor != Q_NULLPTR)
169 // return (otherFunctor->m_surface == m_surface &&
170 // otherFunctor->m_qlVertexColor == m_qlVertexColor);
171 
172  return false;
173 }
174 
175 //*************************************************************************************************************
176 
177 QMeshDataPtr BrainSurfaceMeshFunctor::createSurfaceMesh(const Surface &surface, const QMap<int, QColor> &qmVertexColor)
178 {
179  QMeshDataPtr mesh(new QMeshData(QMeshData::Triangles));
180 
181  //Return if empty surface
182  if(surface.isEmpty() == -1) {
183  std::cout<<"BrainSurfaceMesh - createSurfaceMesh() - Given surface is empty"<<std::endl;
184  return mesh;
185  }
186 
187  //Create opengl data structure
188  Matrix3Xf vertices = surface.rr().transpose();
189  Matrix3Xf normals = surface.nn().transpose();
190  Matrix3Xi faces = surface.tris().transpose();
191 
192  int nVerts = vertices.cols();
193 
194  quint32 elementSizeVertNorm = 3 + 3; // vec3 pos, vec3 normal
195  quint32 elementSizeColor = 3; // vec3 color
196  quint32 strideVertNorm = elementSizeVertNorm * sizeof(float);
197  quint32 strideColor = elementSizeColor * sizeof(float);
198 
199  QByteArray bufferBytesVertNorm;
200  bufferBytesVertNorm.resize(strideVertNorm * nVerts);
201 
202  float* fptrVertNorm = reinterpret_cast<float*>(bufferBytesVertNorm.data());
203 
204  QByteArray bufferBytesColor;
205  bufferBytesColor.resize(strideColor * nVerts);
206 
207  float* fptrColor = reinterpret_cast<float*>(bufferBytesColor.data());
208 
209  for(int i = 0; i<nVerts; i++) {
210  //position x y z
211  *fptrVertNorm++ = vertices(0,i);
212  *fptrVertNorm++ = vertices(1,i);
213  *fptrVertNorm++ = vertices(2,i);
214 
215  //normals x y z
216  *fptrVertNorm++ = normals(0,i);
217  *fptrVertNorm++ = normals(1,i);
218  *fptrVertNorm++ = normals(2,i);
219 
220  //color rgb
221  *fptrColor++ = (float)qmVertexColor[i].redF();
222  *fptrColor++ = (float)qmVertexColor[i].greenF();
223  *fptrColor++ = (float)qmVertexColor[i].blueF();
224 
225  //std::cout<<qmVertexColor[i].redF()<<" "<<qmVertexColor[i].greenF()<<" "<<qmVertexColor[i].blueF()<<std::endl;
226  }
227 
228  //Create OpenGL buffers
229  BufferPtr bufVertNorm(new Buffer(QOpenGLBuffer::VertexBuffer));
230  bufVertNorm->setUsage(QOpenGLBuffer::StaticDraw);
231  bufVertNorm->setData(bufferBytesVertNorm);
232 
233  BufferPtr bufColor(new Buffer(QOpenGLBuffer::VertexBuffer));
234  bufColor->setUsage(QOpenGLBuffer::StaticDraw);
235  bufColor->setData(bufferBytesColor);
236 
237  //Set vertices to OpenGL buffer
238  mesh->addAttribute(QMeshData::defaultPositionAttributeName(), QAbstractAttributePtr(new Attribute(bufVertNorm, GL_FLOAT_VEC3, nVerts, 0, strideVertNorm)));
239  quint32 offset = sizeof(float) * 3;
240 
241  //Set normals to OpenGL buffer
242  mesh->addAttribute(QMeshData::defaultNormalAttributeName(), QAbstractAttributePtr(new Attribute(bufVertNorm, GL_FLOAT_VEC3, nVerts, offset, strideVertNorm)));
243 
244  //Set color to OpenGL buffer
245  mesh->addAttribute(QMeshData::defaultColorAttributeName(), QAbstractAttributePtr(new Attribute(bufColor, GL_FLOAT_VEC3, nVerts, 0, strideColor)));
246 
247  //Generate faces out of tri information
248  QByteArray indexBytes;
249  int number_faces = faces.cols();
250  int indices = number_faces * 3;
251 
252  indexBytes.resize(indices * sizeof(quint32));
253  quint32* indexPtr = reinterpret_cast<quint32*>(indexBytes.data());
254 
255  for(int i = 0; i < number_faces; i++)
256  {
257  *indexPtr++ = faces(0,i);
258  *indexPtr++ = faces(1,i);
259  *indexPtr++ = faces(2,i);
260  }
261 
262  BufferPtr indexBuffer(new Buffer(QOpenGLBuffer::IndexBuffer));
263  indexBuffer->setUsage(QOpenGLBuffer::StaticDraw);
264  indexBuffer->setData(indexBytes);
265  mesh->setIndexAttribute(AttributePtr(new Attribute(indexBuffer, GL_UNSIGNED_INT, indices, 0, 0)));
266 
267  mesh->computeBoundsFromAttribute(QMeshData::defaultPositionAttributeName());
268 
269  //std::cout<<"Created QMeshData"<<std::endl;
270 
271  return mesh;
272 }
273 
bool isEmpty() const
Definition: surface.h:312
Holds the data of one hemisphere in form of a mesh.
const MatrixX3f & nn() const
Definition: surface.h:344
const MatrixX3i & tris() const
Definition: surface.h:336
Declaration of BrainSurfaceMesh which holds the data of one hemisphere in form of a mesh...
FreeSurfer surface mesh.
Definition: surface.h:92
const MatrixX3f & rr() const
Definition: surface.h:328