Skip to content
Snippets Groups Projects
petrack.cpp 180 KiB
Newer Older
  • Learn to ignore specific revisions
  • d.kilic's avatar
    d.kilic committed
        mControlWidget->fy->setValue(1000);
        mControlWidget->cx->setValue(960);
        mControlWidget->cx->setMinimum(0);
        mControlWidget->cx->setMaximum(mControlWidget->cx->value()*2);
        mControlWidget->cy->setValue(540);
        mControlWidget->cx->setMinimum(0);
        mControlWidget->cy->setMaximum(mControlWidget->cy->value()*2);
        mControlWidget->r2->setValue(0);
        mControlWidget->r4->setValue(0);
        mControlWidget->r6->setValue(0);
        mControlWidget->tx->setValue(0);
        mControlWidget->ty->setValue(0);
        //mControlWidget->k4->setValue(0);
        //mControlWidget->k5->setValue(0);
        //mControlWidget->k6->setValue(0);
        mControlWidget->quadAspectRatio->setCheckState(Qt::Unchecked);
        mControlWidget->fixCenter->setCheckState(Qt::Unchecked);
        mControlWidget->tangDist->setCheckState(Qt::Checked);
        getAutoCalib()->setBoardSizeX(6);
        getAutoCalib()->setBoardSizeY(8);
        getAutoCalib()->setSquareSize(5.0);
        getAutoCalib()->setCalibFiles(QStringList());
    
        /// extrinsic params
        mControlWidget->trans1->setValue(0);
        mControlWidget->trans2->setValue(0);
        mControlWidget->trans3->setValue(-500);
        mControlWidget->rot1->setValue(0);
        mControlWidget->rot2->setValue(0);
        mControlWidget->rot3->setValue(0);
    
        getExtrCalibration()->setExtrCalibFile(nullptr);
    
    d.kilic's avatar
    d.kilic committed
    
        /// coord system
        mControlWidget->coordShow->setCheckState(Qt::Unchecked);
        mControlWidget->coordFix->setCheckState(Qt::Unchecked);
        mControlWidget->coordTab->setCurrentIndex(0);
        // 3D
        mControlWidget->coord3DTransX->setValue(0);
        mControlWidget->coord3DTransY->setValue(0);
        mControlWidget->coord3DTransZ->setValue(0);
        mControlWidget->coord3DAxeLen->setValue(200);
        mControlWidget->coord3DSwapX->setCheckState(Qt::Unchecked);
        mControlWidget->coord3DSwapY->setCheckState(Qt::Unchecked);
        mControlWidget->coord3DSwapZ->setCheckState(Qt::Unchecked);
        mControlWidget->extCalibPointsShow->setCheckState(Qt::Unchecked);
        mControlWidget->extVanishPointsShow->setCheckState(Qt::Unchecked);
        // 2D
        mControlWidget->coordTransX->setValue(0);
        mControlWidget->coordTransY->setValue(0);
        mControlWidget->coordRotate->setValue(0);
        mControlWidget->coordScale->setValue(100);
        mControlWidget->coordAltitude->setValue(535);
        mControlWidget->coordUnit->setValue(100);
        mControlWidget->coordUseIntrinsic->setCheckState(Qt::Unchecked);
    
        /// alignment grid
        mControlWidget->gridShow->setCheckState(Qt::Unchecked);
        mControlWidget->gridFix->setCheckState(Qt::Unchecked);
        mControlWidget->gridTab->setCurrentIndex(0);
        // 3D
        mControlWidget->grid3DTransX->setValue(0);
        mControlWidget->grid3DTransY->setValue(0);
        mControlWidget->grid3DTransZ->setValue(0);
        mControlWidget->grid3DResolution->setValue(100);
        // 2D
        mControlWidget->gridTransX->setValue(0);
        mControlWidget->gridTransY->setValue(0);
        mControlWidget->gridRotate->setValue(0);
        mControlWidget->gridScale->setValue(100);
    
        ///
        /// recognition params
        ///
        mControlWidget->performRecognition->setCheckState(Qt::Unchecked);
        mControlWidget->recoStep->setValue(1);
    
        // region of interest
        mControlWidget->roiFix->setCheckState(Qt::Unchecked);
        mControlWidget->roiShow->setCheckState(Qt::Unchecked);
        getRecoRoiItem()->setRect(0,0,0,0);
    
        // marker
        mControlWidget->recoMethod->setCurrentIndex(5);
        mControlWidget->markerBrightness->setValue(100);
        mControlWidget->markerIgnoreWithout->setCheckState(Qt::Checked);
    
        // size and color
        mControlWidget->recoShowColor->setCheckState(Qt::Checked);
        mControlWidget->recoAutoWB->setCheckState(Qt::Checked);
        mControlWidget->recoColorX->setCurrentIndex(0);
        mControlWidget->recoColorY->setCurrentIndex(1);
        mControlWidget->recoColorZ->setValue(255);
        mControlWidget->recoGreyLevel->setValue(50);
        mControlWidget->recoSymbolSize->setValue(10);
    
        // map
        mControlWidget->recoColorModel->setCurrentIndex(0);
        mControlWidget->colorPlot->getMapItem()->delMaps();
        mControlWidget->mapNr->setValue(0);
        mControlWidget->mapNr->setMinimum(0);
        mControlWidget->mapNr->setMaximum(0);
        mControlWidget->mapX->setValue(0);
        mControlWidget->mapY->setValue(0);
        mControlWidget->mapW->setValue(0);
        mControlWidget->mapH->setValue(0);
        mControlWidget->mapColor->setCheckState(Qt::Checked);
        mControlWidget->mapHeight->setValue(180);
    //    mControlWidget->colorPlot->replot();
        mControlWidget->mapDefaultHeight->setValue(180);
    
        ///
        /// tracking params
        ///
        mControlWidget->trackOnlineCalc->setCheckState(Qt::Checked);
        mControlWidget->trackRepeat->setCheckState(Qt::Checked);
        mControlWidget->trackRepeatQual->setValue(50);
        mControlWidget->trackExtrapolation->setCheckState(Qt::Checked);
        mControlWidget->trackMerge->setCheckState(Qt::Unchecked);
        mControlWidget->trackOnlyVisible->setCheckState(Qt::Checked);
        getTrackRoiItem()->setRect(0,0,0,0);
    
        // export options
        mControlWidget->trackMissingFrames->setCheckState(Qt::Checked);
        mControlWidget->trackRecalcHeight->setCheckState(Qt::Checked);
        mControlWidget->trackAlternateHeight->setCheckState(Qt::Unchecked);
        mControlWidget->exportElimTp->setCheckState(Qt::Unchecked);
        mControlWidget->exportElimTrj->setCheckState(Qt::Unchecked);
        mControlWidget->exportSmooth->setCheckState(Qt::Checked);
        mControlWidget->exportViewDir->setCheckState(Qt::Unchecked);
        mControlWidget->exportAngleOfView->setCheckState(Qt::Unchecked);
        mControlWidget->exportUseM->setCheckState(Qt::Unchecked);
        mControlWidget->exportComment->setCheckState(Qt::Unchecked);
        mControlWidget->exportMarkerID->setCheckState(Qt::Unchecked);
    
        // test options
        mControlWidget->testEqual->setCheckState(Qt::Checked);
        mControlWidget->testVelocity->setCheckState(Qt::Checked);
        mControlWidget->testInside->setCheckState(Qt::Checked);
        mControlWidget->testLength->setCheckState(Qt::Checked);
    
        setTrackFileName(nullptr);
    
    d.kilic's avatar
    d.kilic committed
    
        // search region
        mControlWidget->trackRegionScale->setValue(16);
        mControlWidget->trackRegionLevels->setValue(3);
        mControlWidget->trackErrorExponent->setValue(0);
        mControlWidget->trackShowSearchSize->setCheckState(Qt::Unchecked);
    
        // path params
        mControlWidget->trackShow->setCheckState(Qt::Checked);
        mControlWidget->trackFix->setCheckState(Qt::Unchecked);
    
        mControlWidget->trackShowOnlyVisible->setCheckState(Qt::Unchecked);
        mControlWidget->trackShowOnly->setCheckState(Qt::Unchecked);
        mControlWidget->trackShowOnlyNr->setValue(1);
        mControlWidget->trackShowOnlyList->setCheckState(Qt::Unchecked);
        mControlWidget->trackShowOnlyNrList->setEnabled(false);
        mControlWidget->trackShowOnlyListButton->setEnabled(false);
    
        mControlWidget->trackShowCurrentPoint->setCheckState(Qt::Checked);
        mControlWidget->trackShowPoints->setCheckState(Qt::Unchecked);
        mControlWidget->trackShowPath->setCheckState(Qt::Checked);
        mControlWidget->trackShowColColor->setCheckState(Qt::Checked);
        mControlWidget->trackShowColorMarker->setCheckState(Qt::Checked);
        mControlWidget->trackShowNumber->setCheckState(Qt::Checked);
        mControlWidget->trackShowGroundPosition->setCheckState(Qt::Unchecked);
        mControlWidget->trackShowGroundPath->setCheckState(Qt::Unchecked);
    
        mControlWidget->setTrackPathColor(Qt::red);
        mControlWidget->setTrackGroundPathColor(Qt::green);
        mControlWidget->trackHeadSized->setCheckState(Qt::Checked);
        mControlWidget->trackCurrentPointSize->setValue(60);
        mControlWidget->trackPointSize->setValue(7);
        mControlWidget->trackPathWidth->setValue(2);
        mControlWidget->trackColColorSize->setValue(11);
        mControlWidget->trackColorMarkerSize->setValue(14);
        mControlWidget->trackNumberSize->setValue(14);
        mControlWidget->trackGroundPositionSize->setValue(1);
        mControlWidget->trackGroundPathSize->setValue(1);
    
        mControlWidget->trackShowPointsColored->setCheckState(Qt::Checked);
        mControlWidget->trackNumberBold->setCheckState(Qt::Checked);
        mControlWidget->trackShowBefore->setValue(15);
        mControlWidget->trackShowAfter->setValue(15);
    
        /// analysis params
        mControlWidget->anaMissingFrames->setCheckState(Qt::Checked);
        mControlWidget->anaMarkAct->setCheckState(Qt::Unchecked);
        mControlWidget->anaStep->setValue(25);
        mControlWidget->anaConsiderX->setCheckState(Qt::Unchecked);
        mControlWidget->anaConsiderY->setCheckState(Qt::Checked);
        mControlWidget->anaConsiderAbs->setCheckState(Qt::Unchecked);
        mControlWidget->anaConsiderRev->setCheckState(Qt::Unchecked);
        mControlWidget->showVoronoiCells->setCheckState(Qt::Unchecked);
    
    //    updateImage();
    
    }
    
    void Petrack::setStatusStereo(float x, float y, float z)
    {
        if (mStatusLabelStereo)
        {
            if (z<0)
                mStatusLabelStereo->setText(QString("x= novalue  y= novalue  z= novalue  "));
            else
                mStatusLabelStereo->setText(QString("x=%1cm  y=%2cm  z=%3cm  ").arg(x, 6, 'f', 1).arg(y, 6, 'f', 1).arg(z, 6, 'f', 1));
        }
    }
    
    void Petrack::setStatusTime()
    {
        if (mStatusLabelTime)
            mStatusLabelTime->setText(mAnimation->getTimeString());
    }
    
    void Petrack::setStatusFPS()
    {
        if (mStatusLabelFPS)
        {
            mStatusLabelFPS->setText(QString("%1fps  ").arg(mShowFPS, 5, 'f', 1));
    
            QPalette pal = mStatusLabelFPS->palette(); // static moeglich?
            QColor color;//(qRed(col), qGreen(col), qBlue(col));
    
            double diff = mShowFPS-mAnimation->getFPS();
    
            int opacity = mPlayerWidget->getPlayerSpeedLimited() ? 128 : 20;
    
    d.kilic's avatar
    d.kilic committed
    
            if( diff < -6 ) // very slow ==> red
                color.setRgb(200,0,0,opacity);//mStatusLabelFPS->setStyleSheet("background-color: rgba(200,0,0,20);");// border-style: outset; border-width: 1px; border-color: yellow;");//, qRgba(250,0,0,50));//Qt::yellow);
            else if( diff < -2 ) // better ==> yellow
                color.setRgb(200,200,0,opacity);//mStatusLabelFPS->setStyleSheet("background-color: rgba(200,200,0,20);");// border-style: outset; border-width: 1px; border-color: yellow;");//, qRgba(250,0,0,50));//Qt::yellow);
            else if (diff > -2) // nearly ok ==> green
                color.setRgb(0,200,0,opacity);//mStatusLabelFPS->setStyleSheet("background-color: rgba(0,200,0,20);");// border-style: outset; border-width: 1px; border-color: red;");//, qRgba(250,0,0,50));//Qt::yellow);
    
            pal.setColor(QPalette::Window, color);
    //        pal.setColor(QPalette::WindowText,mPlayerWidget->getPlayerSpeedFixed() ? QColor(100,100,100) : QColor(0,0,0));
            //        pal.setBrush(QPalette::WindowText, mPlayerWidget->getPlayerSpeedFixed() ? QBrush(QColor(50,50,50),Qt::Dense2Pattern) : QBrush(QColor(0,0,0),Qt::SolidPattern));
    
            mStatusLabelFPS->setPalette(pal);
        }
    }
    void Petrack::setShowFPS(double fps)
    {
        if ((fps == 0.) || (mShowFPS == 0))
            mShowFPS = fps;
        else
            mShowFPS = mShowFPS*.9+fps*.1; // glaetten durch Hinzunahme des alten Wertes
        setStatusFPS();
    }
    
    
    /**
     * @brief Updates the FPS shown to the User
     *
     * This method calculates the FPS by remembering how long
     * it has been since it was called last time. If skipped is
    
     * true, it doesn't directly update the FPS since 2
    
     * skipped frames have essentially a time delay of 0 between
     * them, which would make calculations wonky.
     *
     * @param skipped True, if this is a skipped frame; default false
     */
    void Petrack::updateShowFPS(bool skipped) {
    
        static QElapsedTimer lastTime;
    
        static int skippedFrames = 0;
    
        if(skipped){
            skippedFrames++;
            return;
        }
    
    d.kilic's avatar
    d.kilic committed
    
    
        if (mPlayerWidget->getPaused())
    
    d.kilic's avatar
    d.kilic committed
        {
            setShowFPS(0.);
    
    d.kilic's avatar
    d.kilic committed
        }
        else
        {
    
            if (lastTime.isValid())
            {
                if (lastTime.elapsed() > 0)
                {
    
                    int numFrames = skippedFrames > 0 ? skippedFrames+1 : 1;
                    setShowFPS(numFrames*1000./lastTime.elapsed());
                    skippedFrames = 0;
    
    d.kilic's avatar
    d.kilic committed
        }
    }
    
    // ohne neue positionsangabe, sinnvoll, wenn berechnungsweise sich in getPosReal geaendert hat
    // gebraucht in control.cpp
    void Petrack::setStatusPosReal() // pos in cm
    {
        if (mImageItem)
            setStatusPosReal(mImageItem->getPosReal(mMousePosOnImage, getStatusPosRealHeight()));
    }
    
    d.kilic's avatar
    d.kilic committed
    void Petrack::setStatusPosReal(const QPointF &pos) // pos in cm
    {
        if (mStatusLabelPosReal)
        {
            QChar deg(0xB0);
            QString labelText = QString(" cm from ground:%1cm,%2cm,%3").arg(pos.x(), 6, 'f', 1).arg(pos.y(), 6, 'f', 1).arg(
                                        getImageItem()->getAngleToGround(mMousePosOnImage.x(), mMousePosOnImage.y(), getStatusPosRealHeight()), 5, 'f', 1);
            labelText.append(deg);
            mStatusLabelPosReal->setText(labelText);
        }
    }
    
    d.kilic's avatar
    d.kilic committed
    void Petrack::setStatusPos(const QPoint &pos) // pos in pixel
    {
        mStatusLabelPos->setText(QString("%1x%2").arg(pos.x(), 4).arg(pos.y(), 4));
    }
    
    d.kilic's avatar
    d.kilic committed
    void Petrack::setStatusColor(const QRgb &col)
    {
        QString s("#%1%2%3"); // static moeglich?
        s = s.arg(qRed(col), 2, 16, QChar('0')).arg(qGreen(col), 2, 16, QChar('0')).arg(qBlue(col), 2, 16, QChar('0'));
        if ((qRed(col)+qGreen(col)+qBlue(col))/3 < 128)
            mStatusLabelColor->setText(QString("<font color=\"#ffffff\">&nbsp;%1</font>").arg(s));
        else
            mStatusLabelColor->setText(QString("<font color=\"#000000\">&nbsp;%1</font>").arg(s));
    
        //     mStatusLabelColor->setBackgroundRole(QPalette::Text);
        QPalette pal = mStatusLabelColor->palette(); // static moeglich?
        QColor color(qRed(col), qGreen(col), qBlue(col));
    
        pal.setColor(QPalette::Window, color);
        mStatusLabelColor->setPalette(pal);
    
        mControlWidget->getColorPlot()->setCursor(color);
        mControlWidget->getColorPlot()->replot();
    
        //debout << "QColor: " << color << " height: " << mControlWidget->getColorPlot()->map(color) << endl;
    }
    
    d.kilic's avatar
    d.kilic committed
    void Petrack::setStatusColor()
    {
        QPointF pos = getMousePosOnImage();
    
        if (pos.x() >= 0 && pos.x() < mImage->width() && pos.y() > 0 && pos.y() < mImage->height())
    
    d.kilic's avatar
    d.kilic committed
        {
    
            setStatusColor(mImage->pixel(pos.toPoint()));
    
    d.kilic's avatar
    d.kilic committed
        }
    }
    
    double Petrack::getStatusPosRealHeight()
    {
        if (mStatusPosRealHeight)
            return mStatusPosRealHeight->value();
        else
            return 0.;
    }
    
    
    /**
     * @brief Reads (and applies) settings form platform-independent persistent storage
     *
     * The saved size and position of the application window get reconstructed. As well as
     * the options about antialiasing and the usage of OpenGL.
     * mSeqFileName and mProFileName get set, so the "Open Project" and "Open Sequence"
     * dialogues start at correct folder. The old project/sequence is NOT actually loaded.
     */
    
    d.kilic's avatar
    d.kilic committed
    void Petrack::readSettings()
    {
        QSettings settings("Forschungszentrum Juelich GmbH", "PeTrack by Maik Boltes, Daniel Salden");
        QPoint pos = settings.value("pos", QPoint(200, 200)).toPoint();
        QSize size = settings.value("size", QSize(400, 400)).toSize();
        mAntialiasAct->setChecked(settings.value("antialias", false).toBool());
        mOpenGLAct->setChecked(settings.value("opengl", false).toBool());
        mSeqFileName = settings.value("seqFileName", QDir::currentPath()).toString();
        mProFileName = settings.value("proFilePath", QDir::currentPath()).toString();
        // nicht ganz sauber, da so immer schon zu anfang in calib file list etwas drin steht und somit auto ausgefuehrt werden kann
        // wird aber beim ersten openCalib... ueberschrieben
        mAutoCalib.addCalibFile(settings.value("calibFile", QDir::currentPath()).toString());
        resize(size);
        move(pos);
        antialias();
        opengl();
    
        mSplitter->restoreState(settings.value("controlSplitterSizes").toByteArray());
    
    d.kilic's avatar
    d.kilic committed
    }
    
    
    /**
     * @brief Writes persistent setting.
     * @see Petrack::readSettings
     */
    
    d.kilic's avatar
    d.kilic committed
    void Petrack::writeSettings()
    {
        QSettings settings("Forschungszentrum Juelich GmbH", "PeTrack by Maik Boltes, Daniel Salden");
        settings.setValue("pos", pos());
        settings.setValue("size", size());
        settings.setValue("antialias", mAntialiasAct->isChecked());
        settings.setValue("opengl", mOpenGLAct->isChecked());
        settings.setValue("seqFileName", mSeqFileName);
        settings.setValue("proFilePath", QFileInfo(mProFileName).path()); // nur path, damit bei saveCurrentProject
        if (!mAutoCalib.isEmptyCalibFiles()) //!mCalibFiles.isEmpty()
            settings.setValue("calibFile", mAutoCalib.getCalibFile(0));
    
        settings.setValue("controlSplitterSizes", mSplitter->saveState());
    
    d.kilic's avatar
    d.kilic committed
    }
    
    bool Petrack::maybeSave()
    {
    
        int ret = PWarning(this, tr("PeTrack"),
                                       tr("Do you want to save "
    
    d.kilic's avatar
    d.kilic committed
                                          "the current project?\n"
    
                                          "Be sure to save trajectories, background "
    
    d.kilic's avatar
    d.kilic committed
                                          "and 3D calibration point separately!"),
    
                                       PMessageBox::StandardButton::Yes |
                                        PMessageBox::StandardButton::No |
                                        PMessageBox::StandardButton::Cancel,
                                       PMessageBox::StandardButton::Yes);
    
    d.kilic's avatar
    d.kilic committed
    
    
        if (ret == PMessageBox::StandardButton::Yes)
    
    d.kilic's avatar
    d.kilic committed
        {
            if (saveSameProject())
                return true;
            else
                return false;
        }
    
        else if (ret == PMessageBox::StandardButton::Cancel)
    
    d.kilic's avatar
    d.kilic committed
            return false;
        else
            return true;
    }
    
    void Petrack::closeEvent(QCloseEvent *event)
    {
    
        if (maybeSave())
        {
            writeSettings();
            event->accept();
        }
        else
            event->ignore();
    
    }
    
    
    /**
     * @brief Sets the mMousePosOnImage member variable and displayed pixel/real coordinates
     *
     * Gets called from ImageItem::hoverMoveEvent() and enables an easy access
     * to the mouse position.
     * @param pos Position of mouse cursor in image pixel coordinates
     */
    
    d.kilic's avatar
    d.kilic committed
    void Petrack::setMousePosOnImage(QPointF pos)
    {
        if (mImage)
        {
            mMousePosOnImage = pos;
            // real coordinate
            setStatusPosReal(mImageItem->getPosReal(pos, getStatusPosRealHeight()));
    
            // pixel coordinate
            QPoint pos1((int)(pos.x())+1, (int)(pos.y())+1);
    
            setStatusPos(pos1);
    
    d.kilic's avatar
    d.kilic committed
            
            // pixel color
    
            setStatusColor();
    
    d.kilic's avatar
    d.kilic committed
        }
    }
    
    void Petrack::keyPressEvent(QKeyEvent * event)
    {
        switch (event->key()) {
        case Qt::Key_Left:
            mPlayerWidget->frameBackward();
            break;
        case Qt::Key_Right:
            mPlayerWidget->frameForward();
            break;
        case Qt::Key_Down:
            mViewWidget->zoomOut(1);
            break;
        case Qt::Key_Up:
            mViewWidget->zoomIn(1);
            break;
        case Qt::Key_Space:
            // space wird von buttons, wenn focus drauf ist als Aktivierung vorher abgegriffen und nicht durchgereicht
            mPlayerWidget->togglePlayPause();
            break;
        case Qt::Key_D:
    //        debout << "test Key_D" << endl;
            break;
        default:
            ;
        }
    //    QMainWindow::keyPressEvent(event);
    }
    
    void Petrack::mousePressEvent(QMouseEvent *event)
    {
        // mouse click in fps status label ?
        if( event->pos().x() >= mStatusLabelFPS->pos().x() && event->pos().x() <= mStatusLabelFPS->pos().x()+mStatusLabelFPS->width() )
        {
    
            mPlayerWidget->togglePlayerSpeedLimited();
    
    d.kilic's avatar
    d.kilic committed
            setStatusFPS();
        }
    }
    
    
    /// update control widget, if image size changed (especially because of changing border)
    
    d.kilic's avatar
    d.kilic committed
    void Petrack::updateControlImage(Mat &img)
    {
    
        // auch moeglich hoehe und breite von bild stat border veraenderungen zu checken
        static int lastBorderSize = -1;
    
        if( isLoading() )
            lastBorderSize = -1;
    
        int diffBorderSize=0;
        int iW = img.cols;
        int iH = img.rows;
    
        // wird auch nochmal in ImageWidget gemacht, aber ist hier frueher noetig
        double cX = mControlWidget->getCalibCxValue(); // merken, da min/max value verandernkann wenn aus dem rahmen
        double cY = mControlWidget->getCalibCyValue();
    
        mControlWidget->setCalibCxMin(0  /*iW/2.-50.*/);
        mControlWidget->setCalibCxMax(iW /*iW/2.+50.*/);
        mControlWidget->setCalibCyMin(0  /*iH/2.-50.*/);
        mControlWidget->setCalibCyMax(iH /*iH/2.+50.*/);
    
        if (mControlWidget->fixCenter->checkState() == Qt::Checked)
        {
            mControlWidget->setCalibCxValue((iW-1)/2.);
            mControlWidget->setCalibCyValue((iH-1)/2.);
        }
        else
        {
            if (lastBorderSize != -1)
                diffBorderSize = getImageBorderSize()-lastBorderSize;
            lastBorderSize = getImageBorderSize();
    
            mControlWidget->setCalibCxValue(cX+diffBorderSize);
            mControlWidget->setCalibCyValue(cY+diffBorderSize);
        }
    }
    
    void Petrack::importTracker(QString dest) //default = ""
    {
        static QString lastFile;
    
        if (lastFile == "")
            lastFile = mTrcFileName;
    
        // if no destination file or folder is given
        if (dest.isEmpty())
        {
            dest = QFileDialog::getOpenFileName(this, tr("Select file for importing tracking pathes"), lastFile,
                                                tr("PeTrack tracker (*.trc *.txt);;All files (*.*)"));
        }
        // in control
        //     else
        //         dest = getExistingFile(dest); // ueberprueft die ;-getrennten Dateinamen auf existenz und gibt ersten zurueck - interessant fuer relativen und absoluten pfad
    
        if (!dest.isEmpty())
        {
            if (dest.right(4) == ".trc")
            {
                QFile file(dest);
                int i, sz;
    
                if (!file.open(QIODevice::ReadOnly | QIODevice::Text))
                {
                    // errorstring ist je nach Betriebssystem in einer anderen Sprache!!!!
    
                    PCritical(this, tr("PeTrack"), tr("Cannot open %1:\n%2").arg(dest).arg(file.errorString()));
    
    d.kilic's avatar
    d.kilic committed
                    return;
                }
    
                setTrackChanged(true);// flag changes of track parameters
                mTracker->reset();
    
                QTextStream in(&file);
                TrackPerson tp;
                QString comment;
    
                bool ok; // shows if int stands in first line - that was in the first version of trc file
                QString firstLine = in.readLine();
                sz = firstLine.toInt(&ok);
                if (!ok)
                {
    
                    if (firstLine.contains("version 4",Qt::CaseInsensitive))
                    {
                        trcVersion = 4;
                    }
                    else if (firstLine.contains("version 3",Qt::CaseInsensitive))
    
    d.kilic's avatar
    d.kilic committed
                    {
                        trcVersion = 3;
    
                    }
                    else if(firstLine.contains("version 2",Qt::CaseInsensitive))
    
    d.kilic's avatar
    d.kilic committed
                    {
                        trcVersion = 2;
    
    d.kilic's avatar
    d.kilic committed
                    {
                        debout << "Error: wrong header while reading TRC file." << endl;
    
                        QMessageBox::critical(this, tr("PeTrack"), tr("Could not import tracker:\nNot supported trc version in file: %1.").arg(dest));
                        return;
    
    d.kilic's avatar
    d.kilic committed
                    }
                    in >> sz;
                }
                else
                    trcVersion = 1;
    
                if ((sz > 0) && (mTracker->size() != 0))
                    debout << "Warning: Overlapping trajectories will be joined not until tracking adds new trackpoints." << endl;
                for (i = 0; i < sz; ++i)
                {
                    if( trcVersion == 2)
                    {
                        in >> tp;
                    }else if( trcVersion >= 3)
                    {
    
    //                    in.skipWhiteSpace(); // skip white spaces for reading the comment line without this the reading makes some problems
    //                    // Kommentarzeile lesen
    //                    comment = in.readLine();
    //                    // Kommentarzeichen entfernen - wird beim exportieren wieder hinzugefuegt
    //                    if( comment.startsWith("#"))
    //                    {
    //                        comment = comment.remove(0,2);
    //                    }
                        in >> tp;
    //                    tp.setComment(comment);
    
                    }
                    mTracker->append(tp);
                    tp.clear();  // loeschen, sonst immer weitere pfade angehangen werden
                }
    
                mControlWidget->trackNumberAll->setText(QString("%1").arg(mTracker->size()));
                mControlWidget->trackShowOnlyNr->setMaximum(MAX(mTracker->size(),1));
                mControlWidget->trackNumberVisible->setText(QString("%1").arg(mTracker->visible(mAnimation->getCurrentFrameNum())));
                mControlWidget->colorPlot->replot();
                file.close();
                debout << "import " << dest << " (" << sz << " person(s), file version " << trcVersion << ")" << endl;
                mTrcFileName = dest; // fuer Project-File, dann koennte track path direkt mitgeladen werden, wenn er noch da ist
            }else
            if (dest.right(4) == ".txt") // 3D Koordinaten als Tracking-Daten importieren Zeilenformat: Personennr, Framenr, x, y, z
            {
    
                PWarning(this,tr("PeTrack"), tr("Are you sure you want to import 3D data from TXT-File? You have to make sure that the coordinate system now is exactly at the same position and orientation than at export time!"));
    
    d.kilic's avatar
    d.kilic committed
    
                QFile file(dest);
                // size of person list
                int sz = 0;
    
                if (!file.open(QIODevice::ReadOnly | QIODevice::Text))
                {
                    // errorstring ist je nach Betriebssystem in einer anderen Sprache!!!!
    
                    PCritical(this, tr("PeTrack"), tr("Cannot open %1:\n%2").arg(dest).arg(file.errorString()));
    
    d.kilic's avatar
    d.kilic committed
                    return;
                }
    
                setTrackChanged(true);// flag changes of track parameters
                mTracker->reset();
    
                //mTrackerReal->reset(); ???
    
                QTextStream in(&file);
                TrackPerson tp;
                TrackPoint tPoint;
                Point2f p2d;
    
                QString line;
                QString headerline;
                bool exec_once_flag = false;
                double conversionFactorTo_cm = 1.0;
                int personNr = -1, frameNr = -1, current_personNr = 1;
                float x, y ,z;
    
                while( 1 )
                {
    
                    // Falls Datei am Ende letzte Person abspeichern und Lese-Schleife beenden
                    if( in.atEnd() )
                    {
                        tp.setLastFrame(frameNr);
                        mTracker->append(tp);
                        ++sz;
                        tp.clear();
                        break;
                    }
    
                    line = in.readLine();
    
                    // Kommentare ueberlesen
                    if( line.startsWith("#",Qt::CaseInsensitive) )
                    {
                        headerline = line;
                        continue;
                    }
    
                    // Test-Ausgabe
                    //debout << line << endl;
    
                    if ((!exec_once_flag) && (!headerline.contains("cm")))
                    {
                        conversionFactorTo_cm = 100.0;
                        exec_once_flag = true;
    
                        PWarning(this,tr("PeTrack"), tr("PeTrack will interpret position data as unit [m]. No header with [cm] found."));
    
    d.kilic's avatar
    d.kilic committed
                    }
    
                    QTextStream stream(&line);
    
                    // Zeile als Stream einlesen Format: [id frame x y z]
                    stream >> personNr
                           >> frameNr
                           >> x
                           >> y
                           >> z;
    
                    // convert data to cm
                    x = x * conversionFactorTo_cm;
                    y = y * conversionFactorTo_cm;
                    z = z * conversionFactorTo_cm;
    
                    // 3-dimensionale Berechnung/Anzeige des Punktes
                    if( mControlWidget->getCalibCoordDimension() == 0 )
                    {
                        p2d = mExtrCalibration.getImagePoint(Point3f(x,y,z));
                    }
                    // 2-dimensionale Berechnung/Anzeige des Punktes
                    else
                    {
                        QPointF pos = mImageItem->getPosImage(QPointF(x,y),z);
                        p2d.x = pos.x();
                        p2d.y = pos.y();
                    }
    
                    tPoint = TrackPoint(Vec2F(p2d.x,p2d.y),100);
                    tPoint.setSp(x,y,-mControlWidget->getCalibExtrTrans3()-z); // fuer den Abstand zur Kamera in z-Richtung wie bei einer Stereokamera
                    //tPoint.setSp(x,y,z); // Eigentlich bisher nur fuer Stereodaten verwendet
    
                    // Neue ID ? ==> letzte Person beendet ==> abspeichern
                    if( personNr > current_personNr )
                    {
                        mTracker->append(tp);
                        ++sz;
                        current_personNr++;
                        tp.clear();
                    }
    
                    // TrackPerson leer ? ==> Neue TrackPerson erstellen
                    if ( tp.isEmpty() )
                    {
                        tp = TrackPerson(personNr, frameNr, tPoint);
                        tp.setFirstFrame(frameNr);
                        tp.setHeight(z);
                    }
                    // TrackPoint an TrackPerson anhaengen
                    else
                    {
                        tp.setLastFrame(frameNr);
                        tp.append(tPoint);
                    }
    
                    // Test-Ausgabe
                    //debout << "Person: " << personNr << " at Frame: " << frameNr << " at 2D-Position: (" << p2d.x << ", " << p2d.y << ") and 3D-Position: (" << x << ", " << y << ", " << z << ")" << endl;
    
                }
    
                mControlWidget->trackNumberAll->setText(QString("%1").arg(mTracker->size()));
                mControlWidget->trackShowOnlyNr->setMaximum(MAX(mTracker->size(),1));
                mControlWidget->trackNumberVisible->setText(QString("%1").arg(mTracker->visible(mAnimation->getCurrentFrameNum())));
                mControlWidget->colorPlot->replot();
                file.close();
                debout << "import " << dest << " (" << sz << " person(s) )" << endl;
                mTrcFileName = dest; // fuer Project-File, dann koennte track path direkt mitgeladen werden, wenn er noch da ist
            }
            else
            {
    
                PCritical(this, tr("PeTrack"), tr("Cannot load %1 maybe because of wrong file extension.").arg(dest));
    
    d.kilic's avatar
    d.kilic committed
            }
            lastFile = dest;
        }
    }
    
    void Petrack::testTracker()
    {
        static int idx=0; // index in Fehlerliste, die als letztes angesprungen wurde
        QList<int> pers, frame;
    
        mTracker->checkPlausibility(pers, frame,
                                    mControlWidget->testEqual->isChecked(),
                                    mControlWidget->testVelocity->isChecked(),
                                    mControlWidget->testInside->isChecked(),
                                    mControlWidget->testLength->isChecked());
        if (pers.length()<=idx)
            idx=0;
        if (pers.length()>idx)
        {
            mControlWidget->trackShowOnly->setCheckState(Qt::Checked);
            mControlWidget->trackShowOnlyNr->setValue(pers[idx]);
    //        mControlWidget->trackShowOnlyNr->setValue(pers[idx]);
            mPlayerWidget->skipToFrame(frame[idx]); // QString().number(mAnimation->getCurrentFrameNum())
            //mControlWidget->trackGotoNr->click();
    
            ++idx;
        }
    
    }
    
    int Petrack::calculateRealTracker()
    {
        bool autoCorrectOnlyExport = (mControlWidget->getRecoMethod() == 5) && // multicolor
                mMultiColorMarkerWidget->autoCorrect->isChecked() &&
                mMultiColorMarkerWidget->autoCorrectOnlyExport->isChecked();
        //int anz = mTrackerReal->calculate(mTracker, mImageItem, mControlWidget->getColorPlot(), getImageBorderSize(), mControlWidget->anaMissingFrames->checkState());
        int anz = mTrackerReal->calculate(mTracker, mImageItem, mControlWidget->getColorPlot(), getImageBorderSize(),
                                mControlWidget->anaMissingFrames->checkState(), //mControlWidget->trackMissingFrames->checkState(),
                                mStereoWidget->stereoUseForExport->isChecked(),
                                mControlWidget->trackAlternateHeight->checkState(), mControlWidget->coordAltitude->value(), mStereoWidget->stereoUseCalibrationCenter->isChecked(),
                                mControlWidget->exportElimTp->isChecked(), mControlWidget->exportElimTrj->isChecked(), mControlWidget->exportSmooth->isChecked(),
                                mControlWidget->exportViewDir->isChecked(), mControlWidget->exportAngleOfView->isChecked(), mControlWidget->exportMarkerID->isChecked(),
                                autoCorrectOnlyExport);
    
        mTrackerReal->calcMinMax();
        return anz;
    }
    
    
    // Veraltet wird in extCalibration gemacht!
    //void Petrack::load3DCalibPoints(QString src) // default = ""
    //{
    
    //    bool all_ok = true;
    
    //    vector<Point3f> list3D;
    //    vector<Point2f> list2D;
    
    //    if( !src.isEmpty() )
    //    {
    //        if( src.right(4) == ".3dc" )
    //        {
    //            //mExtrCalibration->setExtrCalibFile(src);
    //            QFile file(src);
    //            if( !file.open(QIODevice::ReadOnly | QIODevice::Text) )
    //            {
    //                QMessageBox::critical(this, tr("PeTrack"), tr("Error: Cannot open %1:\n%2.").arg(src).arg(file.errorString()));
    //                return;
    //            }
    
    //            debout << "reading 3D calibration data from " << src << "..." << endl;
    
    //            QTextStream in(&file);
    //            QString line;
    //            int lineCount = 0;
    //            QStringList values;
    
    //            line = in.readLine();
    //            lineCount++;
    //            int sz_3d = line.toInt(&all_ok);
    //            if( !all_ok ){
    //                QMessageBox::critical(this, tr("PeTrack"), tr("Error: First line of 3D point file should only contain the number of listed 3D-Points."));
    //                return;
    //            }
    
    //            bool withImageData = false;
    //            double coords[5];
    //            while( !in.atEnd() )
    //            {
    //                line = in.readLine();
    
    //                lineCount++;
    //                // comment lines
    //                if( line.startsWith("#") || line.startsWith(";;") || line.startsWith("//") || line.startsWith("!") )
    //                    continue;
    
    //                //QStream stream(line);
    
    //                //stream >> x_3d >> y_3d << z_3d
    //                //       >> x_2d << y_2d;
    
    //                values = line.split(" ");
    //                //cout << line << " (" << values.size() << ")" << endl;
    //                if( lineCount == 2 && values.size() == 5 )
    //                {
    //                    withImageData = true;
    //                }
    //                if( values.size() == 3 || values.size() == 5 )
    //                {
    //                    //debout << values.at(0) << ", " << values.at(1) << ", " << values.at(2) << endl;
    //                    for(int i=0; i<3; i++)
    //                    {
    //                        coords[i] = values.at(i).toDouble(&all_ok);
    //                        if(!all_ok)
    //                        {
    //                            QMessageBox::critical(this, tr("PeTrack"), tr("Error: Reading Float-Value in line %1 column %2 crashed: %3 (File: %4)").arg(lineCount).arg(i).arg(values.at(i)).arg(src));
    //                            return;
    //                        }
    //                    }
    //                    list3D.push_back( Point3f( coords[0], coords[1], coords[2] ) );
    //                    if (values.size() == 5)
    //                    {
    //                        if( !withImageData ){
    //                            QMessageBox::critical(this, tr("PeTrack"), tr("Error: Unsupported File Format in: %1 (Only 3D-Points or 3D-Points and 2D-Points, but no mix)").arg(src));
    //                            return;
    //                        }
    //                        for(int i=3; i<5; i++)
    //                        {
    //                            coords[i] = values.at(i).toDouble(&all_ok);
    //                            if(!all_ok)
    //                            {
    //                                QMessageBox::critical(this, tr("PeTrack"), tr("Error: Reading Float-Value in line %1 column %2 crashed: %3 (File: %4)").arg(lineCount).arg(i).arg(values.at(i)).arg(src));
    //                                return;
    //                            }
    //                        }
    //                        list2D.push_back( Point2f( coords[3], coords[4] ) );
    
    //                    }else if( withImageData )
    //                    {
    //                        QMessageBox::critical(this, tr("PeTrack"), tr("Error: Unsupported File Format in: %1 (Only 3D-Points or 3D-Points and 2D-Points, but no mix)").arg(src));
    //                        return;
    //                    }
    //                }else
    //                {
    //                    QMessageBox::critical(this, tr("PeTrack"), tr("Error: Unsupported File Format in: %1").arg(src));
    //                    return;
    //                }
    
    
    //            }
    //            file.close();
    //            all_ok = true;
    //            int sz_2d = 0;
    //            if( !mTracker || mTracker->size() < 4 )
    //            {
    //                if( !withImageData )
    //                {
    //                    QMessageBox::critical(this, tr("PeTrack"), tr("Error: At minimum four 3D calibration points needed for 3D calibration."));
    //                }
    //            }else
    //            {
    //                sz_2d = mTracker->size();
    //                list2D.clear();
    
    //                if( sz_2d != sz_3d ){
    //                    if( !withImageData )
    //                    {
    //                        QMessageBox::critical(this, tr("PeTrack"), tr("Count of 2D-Points (%1) and 3D-Points (%2) disagree").arg(sz_2d).arg(sz_3d));
    //                        all_ok = false;
    //                    }
    //                }
    //                //debout << "Marked 2D-Image-Points: " << endl;
    //                if( all_ok )
    //                {
    //                    for(int i = 0; i < sz_2d; i++)
    //                    {
    //                        //debout << "[" << i << "]: (" << mTracker->at(i).at(0).x() << ", " << mTracker->at(i).at(0).y() << ")" << endl;
    //                        // Info: Tracker->TrackPerson->TrackPoint->Vec2F
    //                        list2D.push_back(Point2f(mTracker->at(i).at(0).x(),mTracker->at(i).at(0).y()));
    //                    }
    //                }
    //            }
    
    
    //            //            mExtrCalibration->set3DList(list3D);
    //            //            mExtrCalibration->set2DList(list2D);
    //            //debout << "3D.x " << "3D.y " << "3D.z " << "2D.x " << "2D.y " << endl;
    //            for( int i=0; i<sz_3d; i++ )
    //            {
    //                //                debout << ((Point3f) mExtrCalibration->get3DList().at(i)).x << " "
    //                //                       << ((Point3f) mExtrCalibration->get3DList().at(i)).y << " "
    //                //                       << ((Point3f) mExtrCalibration->get3DList().at(i)).z << " "
    //                //                       << ((Point2f) mExtrCalibration->get2DList().at(i)).x << " "
    //                //                       << ((Point2f) mExtrCalibration->get2DList().at(i)).y << endl;
    //            }
    
    //            if( !withImageData )
    //            {
    //                // Save corresponding image-points,
    //                QFile file(src);
    //                int i;
    
    //                if (!file.open(QIODevice::WriteOnly | QIODevice::Text))
    //                {
    //                    QMessageBox::critical(this, tr("PeTrack"), tr("Cannot open %1:\n%2.").arg(src).arg(file.errorString()));
    //                    return;
    //                }
    
    //                QTextStream out(&file);
    
    //                out << sz_3d << endl;
    //                for (i = 0; i < sz_3d; ++i)
    //                {
    //                    out << list3D.at(i).x << " " << list3D.at(i).y << " " << list3D.at(i).z << " " << list2D.at(i).x << " " << list2D.at(i).y << endl;
    //                }
    
    //                file.close();
    
    //            }
    
    //            //            mExtrCalibration->calibExtrinsicParams();
    //        }else // is 3dc-File ?
    //        {
    //            QMessageBox::critical(this, tr("PeTrack"), tr("File format is not supported for 3D calibration input files: %1 (supported: .3dc ").arg(src.right(4)));
    //            return;
    //        }
    //    }else // src empty ?
    //    {