void LineProfileTool::calculateLineProfile(const QPoint& start, const QPoint& end) { QRect rect = d_target->geometry(); if (!rect.contains(start) || !rect.contains(end)){ QMessageBox::warning(d_graph, tr("QtiPlot - Pixel selection warning"), tr("Please select the end line point inside the image rectangle!")); return; } QPoint o = d_target->pos(); QPixmap pic = d_target->pixmap(); QImage image = pic.convertToImage(); int x1 = start.x()-o.x(); int x2 = end.x()-o.x(); int y1 = start.y()-o.y(); int y2 = end.y()-o.y(); QSize realSize = pic.size(); QSize actualSize = d_target->size(); if (realSize != actualSize){ double ratioX = (double)realSize.width()/(double)actualSize.width(); double ratioY = (double)realSize.height()/(double)actualSize.height(); x1 = int(x1*ratioX); x2 = int(x2*ratioX); y1 = int(y1*ratioY); y2 = int(y2*ratioY); } QString text = tr("pixel") + "\tx\ty\t" + tr("intensity") + "\n"; //uses the fast Bresenham's line-drawing algorithm #define sgn(x) ((x<0)?-1:((x>0)?1:0)) int i = 0, n = 0; int dx = x2 - x1; //the horizontal distance of the line int dy = y2 - y1; //the vertical distance of the line int dxabs = abs(dx); int dyabs = abs(dy); int sdx = sgn(dx); int sdy = sgn(dy); int x = dyabs >> 1; int y = dxabs >> 1; int px = x1; int py = y1; if (dxabs>=dyabs){ //the line is more horizontal than vertical for(i=0;i<dxabs;i++){ y+=dyabs; if (y>=dxabs){ y-=dxabs; py+=sdy; } px+=sdx; n=dxabs; text+=QString::number(i)+"\t"; text+=QString::number(px)+"\t"; text+=QString::number(py)+"\t"; text+=QString::number(averageImagePixel(image, px, py, true))+"\n"; } } else {// the line is more vertical than horizontal for(i=0;i<dyabs;i++){ x+=dxabs; if (x>=dyabs){ x-=dyabs; px+=sdx; } py+=sdy; n=dyabs; text+=QString::number(i)+"\t"; text+=QString::number(px)+"\t"; text+=QString::number(py)+"\t"; text+=QString::number(averageImagePixel(image, px, py, false))+"\n"; } } Table *t = d_app->newTable(tr("Table") + "1", n, 4, text); MultiLayer* plot = d_app->multilayerPlot(t, QStringList(QString(t->objectName())+"_intensity"), 0); Graph *g = (Graph*)plot->activeLayer(); if (g){ g->setTitle(""); g->setXAxisTitle(tr("pixels")); g->setYAxisTitle(tr("pixel intensity (a.u.)")); } }
void ErrDialog::add() { ApplicationWindow *app = qobject_cast<ApplicationWindow *>(parent()); if (!app) return; MultiLayer *plot = (MultiLayer *)app->activeWindow(ApplicationWindow::MultiLayerWindow); if (!plot) return; Graph* g = plot->activeLayer(); if (!g) return; QString name = nameLabel->currentText(); DataCurve *curve = g->dataCurve(name); if (!curve){ QMessageBox::critical(app, tr("QtiPlot - Error"), tr("This feature is not available for user defined function curves!")); return; } int direction = xErrBox->isChecked() ? 0 : 1; ErrorBarsCurve *er = NULL; if (columnBox->isChecked()){ QString errColumnName = tableNamesBox->currentText() + "_" + colNamesBox->currentText(); Table *errTable = app->table(errColumnName); if (!errTable) return; /*if (w->numRows() != errTable->numRows()){ QMessageBox::critical(app, tr("QtiPlot - Error"), tr("The selected columns have different numbers of rows!")); return; }*/ if (errTable->isEmptyColumn(errTable->colIndex(errColumnName))){ QMessageBox::critical(app, tr("QtiPlot - Error"), tr("The selected error column is empty!")); return; } er = g->addErrorBars(curve, errTable, errColumnName, direction); } else { Table *t = curve->table(); if (!t) return; if (direction == ErrorBarsCurve::Horizontal) t->addCol(Table::xErr); else t->addCol(Table::yErr); int r = curve->dataSize(); int rows = t->numRows(); int col = t->numCols() - 1; int ycol = t->colIndex(curve->title().text()); if (!direction) ycol = t->colIndex(curve->xColumnName()); QVarLengthArray<double> Y(r); if (direction == ErrorBarsCurve::Horizontal){ for (int i = 0; i < r; i++) Y[i] = curve->x(i); } else { for (int i = 0; i < r; i++) Y[i] = curve->y(i); } if (percentBox->isChecked()){ double prc = 0.01*valueBox->value(); int aux = 0; for (int i = curve->startRow(); i <= curve->endRow(); i++){ if (!t->text(i, ycol).isEmpty() && aux < r){ t->setCell(i, col, Y[aux]*prc); aux++; } } } else if (standardBox->isChecked() || standardErrorBox->isChecked()){ double sd = gsl_stats_sd(Y.data(), 1, r); if (standardErrorBox->isChecked()) sd /= sqrt(r); for (int i = 0; i < rows; i++){ if (!t->text(i, ycol).isEmpty()) t->setCell(i, col, sd); } } er = g->addErrorBars(curve, t, t->colName(col), direction); } if (er){ er->setColor(curve->pen().color()); g->replot(); plot->notifyChanges(); } }
void PlotWizard::plot2D(const QStringList& colList) { ApplicationWindow *app = (ApplicationWindow *)this->parent(); if (!app) return; QApplication::setOverrideCursor(QCursor(Qt::WaitCursor)); MultiLayer* g = new MultiLayer(app); app->initMultilayerPlot(g, ""); Graph *ag = g->activeLayer(); app->setPreferences(ag); int curves = (int)colList.count(); int errorBars = 0; for (int i = 0; i < curves; i++) { if (colList[i].contains("(yErr)") || colList[i].contains("(xErr)")) errorBars++; } for (int i = 0; i < curves; i++){ QString s = colList[i]; int pos = s.find(":", 0); QString caption = s.left(pos) + "_"; Table *w = (Table *)app->table(caption); int posX = s.find("(X)", pos); QString xColName = caption + s.mid(pos+2, posX-pos-2); posX = s.find(",", posX); int posY = s.find("(Y)", posX); QString yColName = caption+s.mid(posX+2, posY-posX-2); PlotCurve *c = NULL; if (s.contains("(yErr)") || s.contains("(xErr)")){ posY = s.find(",", posY); int posErr, errType; if (s.contains("(yErr)")){ errType = ErrorBarsCurve::Vertical; posErr = s.find("(yErr)", posY); } else { errType = ErrorBarsCurve::Horizontal; posErr = s.find("(xErr)",posY); } QString errColName = caption+s.mid(posY+2, posErr-posY-2); c = (PlotCurve *)ag->addErrorBars(xColName, yColName, w, errColName, errType); } else c = (PlotCurve *)ag->insertCurve(w, xColName, yColName, app->defaultCurveStyle); CurveLayout cl = ag->initCurveLayout(app->defaultCurveStyle, curves - errorBars); cl.lWidth = app->defaultCurveLineWidth; cl.sSize = app->defaultSymbolSize; ag->updateCurveLayout(c, &cl); } ag->updatePlot(); ag->updateAxesTitles(); ag->newLegend(); g->arrangeLayers(false, true); QApplication::restoreOverrideCursor(); }