MatrixX3f Surface::compute_normals(const MatrixX3f& rr, const MatrixX3i& tris) { printf("\tcomputing normals\n"); // first, compute triangle normals MatrixX3f r1(tris.rows(),3); MatrixX3f r2(tris.rows(),3); MatrixX3f r3(tris.rows(),3); for(qint32 i = 0; i < tris.rows(); ++i) { r1.row(i) = rr.row(tris(i, 0)); r2.row(i) = rr.row(tris(i, 1)); r3.row(i) = rr.row(tris(i, 2)); } MatrixX3f x = r2 - r1; MatrixX3f y = r3 - r1; MatrixX3f tri_nn(x.rows(),y.cols()); tri_nn.col(0) = x.col(1).cwiseProduct(y.col(2)) - x.col(2).cwiseProduct(y.col(1)); tri_nn.col(1) = x.col(2).cwiseProduct(y.col(0)) - x.col(0).cwiseProduct(y.col(2)); tri_nn.col(2) = x.col(0).cwiseProduct(y.col(1)) - x.col(1).cwiseProduct(y.col(0)); // Triangle normals and areas MatrixX3f tmp = tri_nn.cwiseProduct(tri_nn); VectorXf normSize = tmp.rowwise().sum(); normSize = normSize.cwiseSqrt(); for(qint32 i = 0; i < normSize.size(); ++i) if(normSize(i) != 0) tri_nn.row(i) /= normSize(i); MatrixX3f nn = MatrixX3f::Zero(rr.rows(), 3); for(qint32 p = 0; p < tris.rows(); ++p) { Vector3i verts = tris.row(p); for(qint32 j = 0; j < verts.size(); ++j) nn.row(verts(j)) = tri_nn.row(p); } tmp = nn.cwiseProduct(nn); normSize = tmp.rowwise().sum(); normSize = normSize.cwiseSqrt(); for(qint32 i = 0; i < normSize.size(); ++i) if(normSize(i) != 0) nn.row(i) /= normSize(i); return nn; }
void LabelView::initializeGL(QGLPainter *painter) { // in the constructor construct a builder on the stack QGLBuilder builder; float fac = 10.0f; builder << QGL::Faceted; m_pSceneNodeBrain = builder.currentNode(); builder.pushNode(); // Collor palette qint32 index; QSharedPointer<QGLMaterialCollection> palette = builder.sceneNode()->palette(); // register color palette within the root node // // Build each hemisphere in its separate node // for(qint32 h = 0; h < 2; ++h) { builder.newNode();//create new hemisphere node { MatrixX3i tris; MatrixX3f rr = m_surfSet[h].rr(); builder.pushNode(); // // Create each ROI in its own node // for(qint32 k = 0; k < m_qListLabels.size(); ++k) { //check if label hemi fits current hemi if(m_qListLabels[k].hemi != h) continue; //Ggenerate label tri information tris = m_qListLabels[k].selectTris(m_surfSet[h]); // add new ROI node when current ROI node is not empty if(builder.currentNode()->count() > 0) builder.newNode(); QGeometryData t_GeometryDataTri; MatrixXf t_TriCoords(3,3*tris.rows()); for(qint32 i = 0; i < tris.rows(); ++i) { t_TriCoords.col(i*3) = rr.row( tris(i,0) ).transpose(); t_TriCoords.col(i*3+1) = rr.row( tris(i,1) ).transpose(); t_TriCoords.col(i*3+2) = rr.row( tris(i,2) ).transpose(); } t_TriCoords *= fac; t_GeometryDataTri.appendVertexArray(QArray<QVector3D>::fromRawData( reinterpret_cast<const QVector3D*>(t_TriCoords.data()), t_TriCoords.cols() )); // // If triangles are available. // if (t_GeometryDataTri.count() > 0) { // // Add triangles to current node // builder.addTriangles(t_GeometryDataTri); // // Colorize ROI // QGLMaterial *t_pMaterialROI = new QGLMaterial(); int r, g, b; r = m_qListRGBAs[k][0]; g = m_qListRGBAs[k][1]; b = m_qListRGBAs[k][2]; t_pMaterialROI->setColor(QColor(r,g,b,200)); // t_pMaterialROI->setEmittedLight(QColor(100,100,100,255)); // t_pMaterialROI->setSpecularColor(QColor(10,10,10,20)); index = palette->addMaterial(t_pMaterialROI); builder.currentNode()->setMaterialIndex(index); } } } // Go one level up builder.popNode(); } // Go one level up builder.popNode(); // Optimze current scene for display and calculate lightning normals m_pSceneNode = builder.finalizedSceneNode(); m_pSceneNode->setParent(this); // // Create light models // m_pLightModel = new QGLLightModel(this); m_pLightModel->setAmbientSceneColor(Qt::white); m_pLightModel->setViewerPosition(QGLLightModel::LocalViewer); m_pLightModel = new QGLLightModel(this); m_pLightParametersScene = new QGLLightParameters(this); m_pLightParametersScene->setPosition(QVector3D(0.0f, 0.0f, 3.0f)); painter->setMainLight(m_pLightParametersScene); simCount = 0; // // Set stereo type // if (m_bStereo) { this->setStereoType(QGLView::RedCyanAnaglyph); camera()->setEyeSeparation(0.4f); m_pCameraFrontal->setEyeSeparation(0.1f); } }
CustomMesh::CustomMesh(const MatrixX3f &tMatVert, const MatrixX3f tMatNorm, const MatrixX3i &tMatTris, const Vector3f &tVecOffset) : Qt3DRender::QGeometryRenderer() , m_iNumVert(tMatVert.rows()) { Qt3DRender::QGeometry* customGeometry = new Qt3DRender::QGeometry(this); m_pVertexDataBuffer = new Qt3DRender::QBuffer(Qt3DRender::QBuffer::VertexBuffer, customGeometry);//QSharedPointer<Qt3DRender::QBuffer>(new Qt3DRender::QBuffer(Qt3DRender::QBuffer::VertexBuffer, customGeometry)); m_pNormalDataBuffer = new Qt3DRender::QBuffer(Qt3DRender::QBuffer::VertexBuffer, customGeometry);//QSharedPointer<Qt3DRender::QBuffer>(new Qt3DRender::QBuffer(Qt3DRender::QBuffer::VertexBuffer, customGeometry)); m_pColorDataBuffer = new Qt3DRender::QBuffer(Qt3DRender::QBuffer::VertexBuffer, customGeometry);//QSharedPointer<Qt3DRender::QBuffer>(new Qt3DRender::QBuffer(Qt3DRender::QBuffer::VertexBuffer, customGeometry)); m_pIndexDataBuffer = new Qt3DRender::QBuffer(Qt3DRender::QBuffer::IndexBuffer, customGeometry);//QSharedPointer<Qt3DRender::QBuffer>(new Qt3DRender::QBuffer(Qt3DRender::QBuffer::IndexBuffer, customGeometry)); //Fill vertexBuffer with data which hold the vertices, normals and colors QByteArray vertexBufferData; vertexBufferData.resize(tMatVert.rows() * 3 * (int)sizeof(float)); float *rawVertexArray = reinterpret_cast<float *>(vertexBufferData.data()); QByteArray normalBufferData; normalBufferData.resize(tMatVert.rows() * 3 * (int)sizeof(float)); float *rawNormalArray = reinterpret_cast<float *>(normalBufferData.data()); QByteArray colorBufferData; colorBufferData.resize(tMatVert.rows() * 3 * (int)sizeof(float)); float *rawColorArray = reinterpret_cast<float *>(colorBufferData.data()); int idxVert = 0; int idxNorm = 0; int idxColor = 0; for(int i = 0; i<tMatVert.rows(); i++) { //Vertex rawVertexArray[idxVert++] = tMatVert(i,0) + tVecOffset(0); rawVertexArray[idxVert++] = tMatVert(i,1) + tVecOffset(1); rawVertexArray[idxVert++] = tMatVert(i,2) + tVecOffset(2); //Normal rawNormalArray[idxNorm++] = tMatNorm(i,0); rawNormalArray[idxNorm++] = tMatNorm(i,1); rawNormalArray[idxNorm++] = tMatNorm(i,2); //Color (this is the default color and will be used until the updateVertColor function was called) rawColorArray[idxColor++] = 0.5f; rawColorArray[idxColor++] = 0.2f; rawColorArray[idxColor++] = 0.2f; } //Fill indexBufferData with data which holds the triangulation information (faces/tris) QByteArray indexBufferData; indexBufferData.resize(tMatTris.rows() * 3 * (int)sizeof(uint)); uint *rawIndexArray = reinterpret_cast<uint *>(indexBufferData.data()); int idxTris = 0; for(int i = 0; i<tMatTris.rows(); i++) { //Faces/Tris rawIndexArray[idxTris++] = tMatTris(i,0); rawIndexArray[idxTris++] = tMatTris(i,1); rawIndexArray[idxTris++] = tMatTris(i,2); } //Set data to buffers m_pVertexDataBuffer->setData(vertexBufferData); m_pNormalDataBuffer->setData(normalBufferData); m_pColorDataBuffer->setData(colorBufferData); m_pIndexDataBuffer->setData(indexBufferData); // Attributes Qt3DRender::QAttribute *positionAttribute = new Qt3DRender::QAttribute(); positionAttribute->setAttributeType(Qt3DRender::QAttribute::VertexAttribute); positionAttribute->setBuffer(m_pVertexDataBuffer); positionAttribute->setDataType(Qt3DRender::QAttribute::Float); positionAttribute->setDataSize(3); positionAttribute->setByteOffset(0); positionAttribute->setByteStride(3 * sizeof(float)); positionAttribute->setCount(tMatVert.rows()); positionAttribute->setName(Qt3DRender::QAttribute::defaultPositionAttributeName()); Qt3DRender::QAttribute *normalAttribute = new Qt3DRender::QAttribute(); normalAttribute->setAttributeType(Qt3DRender::QAttribute::VertexAttribute); normalAttribute->setBuffer(m_pNormalDataBuffer); normalAttribute->setDataType(Qt3DRender::QAttribute::Float); normalAttribute->setDataSize(3); normalAttribute->setByteOffset(0); normalAttribute->setByteStride(3 * sizeof(float)); normalAttribute->setCount(tMatVert.rows()); normalAttribute->setName(Qt3DRender::QAttribute::defaultNormalAttributeName()); Qt3DRender::QAttribute *colorAttribute = new Qt3DRender::QAttribute(); colorAttribute->setAttributeType(Qt3DRender::QAttribute::VertexAttribute); colorAttribute->setBuffer(m_pColorDataBuffer); colorAttribute->setDataType(Qt3DRender::QAttribute::Float); colorAttribute->setDataSize(3); colorAttribute->setByteOffset(0); colorAttribute->setByteStride(3 * sizeof(float)); colorAttribute->setCount(tMatVert.rows()); colorAttribute->setName(Qt3DRender::QAttribute::defaultColorAttributeName()); Qt3DRender::QAttribute *indexAttribute = new Qt3DRender::QAttribute(); indexAttribute->setAttributeType(Qt3DRender::QAttribute::IndexAttribute); indexAttribute->setBuffer(m_pIndexDataBuffer); indexAttribute->setDataType(Qt3DRender::QAttribute::UnsignedInt); indexAttribute->setDataSize(1); indexAttribute->setByteOffset(0); indexAttribute->setByteStride(0); indexAttribute->setCount(tMatTris.rows()); customGeometry->addAttribute(positionAttribute); customGeometry->addAttribute(normalAttribute); customGeometry->addAttribute(colorAttribute); customGeometry->addAttribute(indexAttribute); this->setInstanceCount(1); this->setBaseVertex(0); this->setBaseInstance(0); this->setPrimitiveType(Qt3DRender::QGeometryRenderer::Triangles); this->setGeometry(customGeometry); this->setPrimitiveCount(tMatTris.rows()*3); }