void Mesh::removeRow(int rowId) { // Cannot remove first and last columns Q_ASSERT(rowId >= 1 && rowId < nRows()-1); // Temporary containers that will be used to rebuild new vertex space. IndexVector2d newVertices2d; resizeVertices2d(newVertices2d, nColumns(), nRows()-1); QVector<QPointF> newVertices(vertices.size()-nColumns()); // Bottom displacement of points already there. qreal bottomMoveProp = 1.0f/(nRows()-2) - 1.0f/(nRows()-1); // Process all columns. int k = 0; for (int x=0; x<nColumns(); x++) { // Get top and bottom vertices. QPointF top = getVertex2d(x, 0); QPointF bottom = getVertex2d(x, nRows()-1); QPointF diff = bottom - top; // Move all rows. for (int y=0; y<nRows(); y++) { // Ignore points from target row. if (y == rowId) continue; // Get current vertex. QPointF p = getVertex2d( x, y ); // The y value of this point in the new space. int newY = y < rowId ? y : y-1; // Move middle points. if (y > 0 && y < nRows()-1) { p += (y < rowId ? +1 : -1) * diff * newY * bottomMoveProp; } // Assign new containers. newVertices[k] = p; newVertices2d[x][newY] = k; k++; } } // Copy new mapping. vertices = newVertices; _vertices2d = newVertices2d; // Decrement number of rows. _nRows--; // Reorder. _reorderVertices(); }
// vertices 0..3 = 4 corners // void Mesh::addColumn() { // Create new vertices 2d (temporary). IndexVector2d newVertices2d; resizeVertices2d(newVertices2d, nColumns()+1, nRows()); // Left displacement of points already there. qreal leftMoveProp = 1.0f/(nColumns()-1) - 1.0f/nColumns(); // Add a point at each row. int k = nVertices(); for (int y=0; y<nRows(); y++) { // Get left and right vertices. QPointF left = getVertex2d( 0, y ); QPointF right = getVertex2d( nColumns()-1, y ); QPointF diff = right - left; // First pass: move middle points. for (int x=1; x<nColumns()-1; x++) { QPointF p = getVertex2d(x, y); p -= diff * x * leftMoveProp; _rawSetVertex( _vertices2d[x][y], p ); } // Create and add new point. QPointF newPoint = right - diff * 1.0f/nColumns(); _addVertex(newPoint); // Assign new vertices 2d. for (int x=0; x<nColumns()-1; x++) newVertices2d[x][y] = _vertices2d[x][y]; // The new point. newVertices2d[nColumns()-1][y] = k; // The rightmost point. newVertices2d[nColumns()][y] = _vertices2d[nColumns()-1][y]; k++; } // Copy new mapping. _vertices2d = newVertices2d; // Increment number of columns. _nColumns++; // Reorder. _reorderVertices(); }
void Mesh::addRow() { // Create new vertices 2d (temporary). IndexVector2d newVertices2d; resizeVertices2d(newVertices2d, nColumns(), nRows()+1); // Top displacement of points already there. qreal topMoveProp = 1.0f/(nRows()-1) - 1.0f/nRows(); // Add a point at each row. int k = nVertices(); for (int x=0; x<nColumns(); x++) { // Get left and right vertices. QPointF top = getVertex2d(x, 0); QPointF bottom = getVertex2d(x, nRows()-1); QPointF diff = bottom - top; // First pass: move middle points. for (int y=1; y<nRows()-1; y++) { QPointF p = getVertex2d(x, y); p -= diff * y * topMoveProp; _rawSetVertex( _vertices2d[x][y], p ); } // Create and add new point. QPointF newPoint = bottom - diff * 1.0f/nRows(); _addVertex(newPoint); // Assign new vertices 2d. for (int y=0; y<nRows()-1; y++) newVertices2d[x][y] = _vertices2d[x][y]; // The new point. newVertices2d[x][nRows()-1] = k; // The rightmost point. newVertices2d[x][nRows()] = _vertices2d[x][nRows()-1]; k++; } // Copy new mapping. _vertices2d = newVertices2d; // Increment number of columns. _nRows++; // Reorder. _reorderVertices(); }
QPolygonF Mesh::toPolygon() const { QPolygonF polygon; for (int i=0; i<nColumns(); i++) polygon.append(getVertex2d(i, 0)); for (int i=0; i<nRows(); i++) polygon.append(getVertex2d(nColumns()-1, i)); for (int i=nColumns()-1; i>=0; i--) polygon.append(getVertex2d(i, nRows()-1)); for (int i=nRows()-1; i>=1; i--) polygon.append(getVertex2d(0, i)); return polygon; }
/* * Solve Ax = b. Vector b is overwritten on exit with x. */ int SquareMatrix::solve(doublereal * b) { if (useQR_) { return solveQR(b); } int info=0; /* * Check to see whether the matrix has been factored. */ if (!m_factored) { int retn = factor(); if (retn) { return retn; } } /* * Solve the factored system */ ct_dgetrs(ctlapack::NoTranspose, static_cast<int>(nRows()), 1, &(*(begin())), static_cast<int>(nRows()), DATA_PTR(ipiv()), b, static_cast<int>(nColumns()), info); if (info != 0) { if (m_printLevel) { writelogf("SquareMatrix::solve(): DGETRS returned INFO = %d\n", info); } if (! m_useReturnErrorCode) { throw CELapackError("SquareMatrix::solve()", "DGETRS returned INFO = " + int2str(info)); } } return info; }
void Mesh::_reorderVertices() { // Populate new vertices vector. QVector<QPointF> newVertices(vertices.size()); int k = 0; for (int y=0; y<nRows(); y++) for (int x=0; x<nColumns(); x++) newVertices[k++] = getVertex2d( x, y ); // Populate _vertices2d. k = 0; for (int y=0; y<nRows(); y++) for (int x=0; x<nColumns(); x++) _vertices2d[x][y] = k++; // Copy. vertices = newVertices; }
void Mesh::removeColumn(int columnId) { // Cannot remove first and last columns Q_ASSERT(columnId >= 1 && columnId < nColumns()-1); // Temporary containers that will be used to rebuild new vertex space. IndexVector2d newVertices2d; resizeVertices2d(newVertices2d, nColumns()-1, nRows()); QVector<QPointF> newVertices(vertices.size()-nRows()); // Right displacement of points already there. qreal rightMoveProp = 1.0f/(nColumns()-2) - 1.0f/(nColumns()-1); // Process all rows. int k = 0; for (int y=0; y<nRows(); y++) { // Get left and right vertices. QPointF left = getVertex2d( 0, y ); QPointF right = getVertex2d( nColumns()-1, y ); QPointF diff = right - left; // Move all columns. for (int x=0; x<nColumns(); x++) { // Ignore points from target column. if (x == columnId) continue; // Get current vertex. QPointF p = getVertex2d( x, y ); // The x value of this point in the new space. int newX = x < columnId ? x : x-1; // Move middle points. if (x > 0 && x < nColumns()-1) { p += (x < columnId ? +1 : -1) * diff * newX * rightMoveProp; } // Assign new containers. newVertices[k] = p; newVertices2d[newX][y] = k; k++; } } // Copy new mapping. vertices = newVertices; _vertices2d = newVertices2d; // Decrement number of columns. _nColumns--; // Reorder. _reorderVertices(); }
void Mesh::setVertex(int i, const QPointF& v) { // Extract column and row of vertex. int col = i % nColumns(); int row = i / nColumns(); // Make a copy. QPointF realV = v; // Constrain vertex to stay within the internal quads it is part of. if (col < nColumns()-1) { if (row < nRows() - 1) { Quad quad(getVertex2d(col, row), getVertex2d(col+1, row), getVertex2d(col+1, row+1), getVertex2d(col, row+1)); _constrainVertex(quad.toPolygon(), 0, realV); } if (row > 0) { Quad quad(getVertex2d(col, row), getVertex2d(col+1, row), getVertex2d(col+1, row-1), getVertex2d(col, row-1)); _constrainVertex(quad.toPolygon(), 0, realV); } } if (col > 0) { if (row < nRows() - 1) { Quad quad(getVertex2d(col, row), getVertex2d(col-1, row), getVertex2d(col-1, row+1), getVertex2d(col, row+1)); _constrainVertex(quad.toPolygon(), 0, realV); } if (row > 0) { Quad quad(getVertex2d(col, row), getVertex2d(col-1, row), getVertex2d(col-1, row-1), getVertex2d(col, row-1)); _constrainVertex(quad.toPolygon(), 0, realV); } } // Do set vertex. _rawSetVertex(i, realV); }
int BandMatrix::solve(doublereal* b, size_t nrhs, size_t ldb) { int info = 0; if (!m_factored) { info = factor(); } if (ldb == 0) { ldb = nColumns(); } if (info == 0) ct_dgbtrs(ctlapack::NoTranspose, nColumns(), nSubDiagonals(), nSuperDiagonals(), nrhs, DATA_PTR(ludata), ldim(), DATA_PTR(ipiv()), b, ldb, info); // error handling if (info != 0) { ofstream fout("bandmatrix.csv"); fout << *this << endl; fout.close(); } return info; }
void DenseMatrix::leftMult(const double* b, double* prod) const { int nc = static_cast<int>(nColumns()); int nr = static_cast<int>(nRows()); int n, i; double sum = 0.0; for (n = 0; n < nc; n++) { sum = 0.0; for (i = 0; i < nr; i++) { sum += value(i,n)*b[i]; } prod[n] = sum; } }
//==================================================================================================================== void DenseMatrix::leftMult(const double* const b, double* const prod) const { size_t nc = nColumns(); size_t nr = nRows(); double sum = 0.0; for (size_t n = 0; n < nc; n++) { sum = 0.0; for (size_t i = 0; i < nr; i++) { sum += value(i,n)*b[i]; } prod[n] = sum; } }
void Mesh::resize(int nColumns_, int nRows_) { // Brutal: if asked to reduce columns or rows, just delete and redo. if (nColumns_ < nColumns()) { while (nColumns_ != nColumns()) removeColumn(nColumns()-2); } if (nRows_ < nRows()) { while (nRows_ != nRows()) removeRow(nRows()-2); } if (nColumns_ > nColumns()) { while (nColumns_ != nColumns()) addColumn(); } if (nRows_ > nRows()) { while (nRows_ != nRows()) addRow(); } }
int BandMatrix::factor() { int info=0; ludata = data; ct_dgbtrf(nRows(), nColumns(), nSubDiagonals(), nSuperDiagonals(), ludata.data(), ldim(), ipiv().data(), info); // if info = 0, LU decomp succeeded. if (info == 0) { m_factored = true; } else { m_factored = false; ofstream fout("bandmatrix.csv"); fout << *this << endl; } return info; }
void BandMatrix::leftMult(const doublereal* const b, doublereal* const prod) const { int kl = static_cast<int>(m_kl); int ku = static_cast<int>(m_ku); int nc = static_cast<int>(nColumns()); doublereal sum = 0.0; for (int n = 0; n < nc; n++) { sum = 0.0; for (int i = n - ku; i <= n + kl; i++) { if (i >= 0 && i < (int) m_n) { size_t ii = i; sum += _value(ii,n) * b[ii]; } } prod[n] = sum; } }
int BandMatrix::factor() { int info=0; copy(data.begin(), data.end(), ludata.begin()); ct_dgbtrf(nRows(), nColumns(), nSubDiagonals(), nSuperDiagonals(), DATA_PTR(ludata), ldim(), DATA_PTR(ipiv()), info); // if info = 0, LU decomp succeeded. if (info == 0) { m_factored = true; } else { m_factored = false; ofstream fout("bandmatrix.csv"); fout << *this << endl; fout.close(); } return info; }
/** * Solve Ax = b. Vector b is overwritten on exit with x. */ int SquareMatrix::solve(double* b) { int info=0; /* * Check to see whether the matrix has been factored. */ if (!m_factored) { factor(); } /* * Solve the factored system */ ct_dgetrs(ctlapack::NoTranspose, static_cast<int>(nRows()), 1, &(*(begin())), static_cast<int>(nRows()), DATA_PTR(ipiv()), b, static_cast<int>(nColumns()), info); if (info != 0) throw CanteraError("SquareMatrix::solve", "DGETRS returned INFO = "+int2str(info)); return 0; }
cholmod_dense* DenseMatrix<Quaternion> :: to_cholmod( void ) // returns pointer to underlying cholmod_dense data structure { assert( nColumns() == 1 ); if( cData ) { cholmod_l_free_dense( &cData, context ); cData = NULL; } cData = cholmod_l_allocate_dense( m*4, 1, m*4, CHOLMOD_REAL, context ); double* x = (double*) cData->x; for( int i = 0; i < m*n; i++ ) { for( int k = 0; k < 4; k++ ) { x[i*4+k] = data[i][k]; } } return cData; }
int MipData::nRows() const { if (nColumns() < 1) return 0; return data[0].size(); }
bool MipData::loadMy4DImage(const My4DImage* img, const My4DImage* maskImg) { // Validate data in this thread if (!img) return false; benchmark.start(); dataMin = 1e9; dataMax = -1e9; data.assign(img->getXDim(), MipData::Column(img->getYDim(), MipData::Pixel(img->getCDim()))); // 50 ms // qDebug() << "size = " << data.size(); // qDebug() << "nColumns = " << nColumns(); // MipData& t = *this; volume4DImage = img; My4DImage * mutable_img = const_cast<My4DImage*>(img); Image4DProxy<My4DImage> imgProxy(mutable_img); numNeurons = 0; NeuronChannelIntegratorList neuronColors; // First loop "quickly" updates intensity, without updating neuron masks for (int x = 0; x < nColumns(); ++x) { for (int y = 0; y < nRows(); ++y) { for (int z = 0; z < img->getZDim(); ++z) { int neuronIndex = -1; if (maskImg) { neuronIndex = maskImg->at(x, y, z); if (neuronIndex >= numNeurons) { numNeurons = neuronIndex + 1; neuronColors.resize(numNeurons, NeuronChannelIntegrator(nChannels(), 0.0)); } } float intensity = 0.0; for (int c = 0; c < nChannels(); ++c) { float val = (float)imgProxy.value_at(x,y,z,c); // Update minimum and maximum values if (val > dataMax) dataMax = val; if (val < dataMin) dataMin = val; // Update current voxel intensity intensity += val; if (neuronIndex >= 0) neuronColors[neuronIndex][c] += val; } assert(intensity >= 0.0); // Maximum intensity projection - regardless of neuron masks if (intensity > data[x][y].intensity) { for (int c = 0; c < nChannels(); ++c) data[x][y][c] = (float)imgProxy.value_at(x,y,z,c); data[x][y].z = z; // remember z-value of max intensity pixel data[x][y].intensity = intensity; if (maskImg) { data[x][y].neuronIndex = (int) maskImg->at(x, y, z); } } } } if (! (x % 10)) emit processedXColumn(x + 1); // qDebug() << "processed column " << x + 1; } qDebug() << "Computing MIP took " << benchmark.restart() << " milliseconds"; for (int n = 0; n < neuronColors.size(); ++n) { NeuronChannelIntegrator& neuronColor = neuronColors[n]; // find maximum double maxCount = -1e9; for (int c = 0; c < neuronColor.size(); ++c) { double channelCount = neuronColor[c]; if (channelCount > maxCount) maxCount = channelCount; } // scale by maximum for (int c = 0; c < neuronColor.size(); ++c) { neuronColor[c] /= maxCount; } } qDebug() << "Computing neuron colors took " << benchmark.restart() << " milliseconds"; // TODO - actually use the color information emit intensitiesUpdated(); // Populate individual neuron mip layers if (maskImg && (numNeurons > 0)) { if (neuronLayers) delete [] neuronLayers; neuronLayers = new MipLayer*[numNeurons]; for (int i = 0; i < numNeurons; ++i) neuronLayers[i] = new MipLayer(QSize(nColumns(), nRows()), this); qDebug() << "processing MIP masks"; for (int x = 0; x < nColumns(); ++x) { for (int y = 0; y < nRows(); ++y) { for (int z = 0; z < img->getZDim(); ++z) { int neuronMaskId = maskImg->at(x,y,z); if (neuronMaskId < 0) continue; float intensity = 0.0; for (int c = 0; c < nChannels(); ++c) intensity += (float)imgProxy.value_at(x,y,z,c); assert(intensity >= 0.0); MipLayer::Pixel& currentPixel = neuronLayers[neuronMaskId]->getPixel(x, y); if ( (currentPixel.neuronIndex != neuronMaskId) // no data for this neuron so far || (intensity > currentPixel.intensity) ) // brightest intensity seen so far { currentPixel.neuronIndex = neuronMaskId; currentPixel.zCoordinate = z; currentPixel.intensity = intensity; } } } } qDebug() << "finished creating MIP masks; took " << benchmark.restart() << " milliseconds"; // TODO create binary tree of mip layers leading to combined image std::vector<MipLayer*> layers; for (int n = 0; n < numNeurons; ++n) { layers.push_back(neuronLayers[n]); } while (layers.size() > 1) { std::vector<MipLayer*> nextLevel; while (layers.size() > 0) { MipLayer* node1 = layers.back(); layers.pop_back(); MipLayer* node2 = NULL; if (layers.size() > 0) { node2 = layers.back(); layers.pop_back(); } nextLevel.push_back(new MipLayer(node1, node2, this)); } layers = nextLevel; qDebug() << "layers size = " << layers.size(); } assert(layers.size() == 1); combinedMipLayer = layers.back(); connect(combinedMipLayer, SIGNAL(layerChanged()), this, SLOT(onCombinedMipLayerUpdated())); qDebug() << "Creating MIP layer binary tree took " << benchmark.restart() << " milliseconds"; } return true; }