From bcddee2ce84ac1ca343f7ab070c8215dadece926 Mon Sep 17 00:00:00 2001
From: "simon.fanetti" <simon.fanetti@etu.hesge.ch>
Date: Wed, 24 Jun 2020 19:13:56 +0200
Subject: [PATCH] fixe BeamerLocation routine

---
 app/SandboxSetup/beamerlocationgui.cpp | 44 ++++++++++++++++----------
 app/SandboxSetup/beamerlocationgui.h   |  3 +-
 2 files changed, 30 insertions(+), 17 deletions(-)

diff --git a/app/SandboxSetup/beamerlocationgui.cpp b/app/SandboxSetup/beamerlocationgui.cpp
index 3b2c2b8..3e0d143 100644
--- a/app/SandboxSetup/beamerlocationgui.cpp
+++ b/app/SandboxSetup/beamerlocationgui.cpp
@@ -21,6 +21,7 @@ BeamerLocationGui::BeamerLocationGui(SandboxSetup *_setup, MonitorGui *_mg, QWid
     winFullScreen = new QtFullScreen(mg->getResolution(), true, this);
     ui->setupUi(this);
     ui->txtInstructions->setAlignment(Qt::AlignJustify);
+    mut = new QMutex();
     myThread = new RefreshFrame(this);
     timer = new QTimer(this);
     connect(timer, &QTimer::timeout, this, &BeamerLocationGui::refreshRoutine);
@@ -30,6 +31,7 @@ BeamerLocationGui::~BeamerLocationGui()
 {
     delete timer;
     delete myThread;
+    delete mut;
     delete winFullScreen;
     delete ui;
 }
@@ -71,7 +73,7 @@ void BeamerLocationGui::startRoutine(){
     directions = std::vector<cv::Point3d>();
     bases = std::vector<cv::Point3d>();
     capturedPoints = std::vector<cv::Point3f>();
-    circle = std::vector<cv::Point3i>();
+    circles = std::vector<cv::Point3i>();
     updateLabelSteps();
 
     if(myThread->isRunning()){
@@ -95,32 +97,35 @@ void BeamerLocationGui::routineFrame(){
         depth = camera->getDepthFrame();
         rgb = camera->getColorFrame();
 
-        // Look for the circle target
+        // Look for the circles target
         double minDist = rgb.cols*(double)profil->getMinDistance()/100;
         double minRadius = (profil->getMinRadius()>0) ? rgb.cols*(double)profil->getMinRadius()/100 : 0;
         double maxRadius = (profil->getMaxRadius()>0) ? rgb.cols*(double)profil->getMaxRadius()/100 : 0;
-        circle = beamer->findCircles(  rgb,
-                                       profil->getContrast(),
-                                       profil->getBrightness(),
-                                       minDist,
-                                       (int)profil->getCannyEdgeThreshold(),
-                                       (int)profil->getHoughAccThreshold(),
-                                       minRadius,
-                                       maxRadius );
+        std::vector<cv::Point3i> tmp_circles = beamer->findCircles(  rgb,
+                                                                     profil->getContrast(),
+                                                                     profil->getBrightness(),
+                                                                     minDist,
+                                                                     (int)profil->getCannyEdgeThreshold(),
+                                                                     (int)profil->getHoughAccThreshold(),
+                                                                     minRadius,
+                                                                     maxRadius );
+        mut->lock();
+        circles = tmp_circles;
+        mut->unlock();
 
         // preview from the camera's POV
         cv::Mat gray, frameDisplayed;
         cv::cvtColor(rgb, gray, CV_BGR2GRAY);
         gray = setup->getBeamer()->editContrast(gray, (double)profil->getContrast(), (double)profil->getBrightness());
         cv::cvtColor(gray, frameDisplayed, CV_GRAY2RGB);
-        if(!circle.empty()){
-            cv::circle(frameDisplayed, cv::Point(circle[0].x, circle[0].y), circle[0].z, cv::Scalar(0,255,0), 3);
+        if(!circles.empty()){
+            cv::circle(frameDisplayed, cv::Point(circles[0].x, circles[0].y), circles[0].z, cv::Scalar(0,255,0), 3);
         }
         QImage img = QImage((uchar *)frameDisplayed.data, (int)frameDisplayed.cols, (int)frameDisplayed.rows, static_cast<int>(frameDisplayed.step.buf[0]), QImage::Format_RGB888);
         QPixmap px = QPixmap::fromImage(img);
 
         // Show black screen with cross
-        cv::Mat frameImage = beamer->getCrossFrame(cross, capturedPoints.size()+1, beamer->MAX_LINEAR_LINE_POINTS, !circle.empty());
+        cv::Mat frameImage = beamer->getCrossFrame(cross, capturedPoints.size()+1, beamer->MAX_LINEAR_LINE_POINTS, !circles.empty());
         cv::cvtColor(frameImage, frameImage, CV_BGR2RGB);
 
         ui->lblFrame->setPixmap(px);
@@ -131,9 +136,17 @@ void BeamerLocationGui::routineFrame(){
 
 void BeamerLocationGui::userValidePoint(){
 
-    if(!circle.empty()){
+    std::vector<cv::Point3i> tmp_circles;
+    mut->lock();
+    if(!circles.empty()){
+        tmp_circles = circles;
+        circles.clear();
+    }
+    mut->unlock();
 
-        capturedPoints.push_back( beamer->deprojectPixel(cv::Point2i(circle.at(0).x, circle.at(0).y), &depth, camera) );
+    if(!tmp_circles.empty()){
+        cv::Point2i circle_pos = cv::Point2i(tmp_circles.at(0).x, tmp_circles.at(0).y);
+        capturedPoints.push_back( beamer->deprojectPixel(circle_pos, &depth, camera) );
         updateLabelSteps();
 
         // enough points to perform linear regression and move into next step
@@ -141,7 +154,6 @@ void BeamerLocationGui::userValidePoint(){
 
             beamer->findLinearLineFrom(&capturedPoints, &bases, &directions);
             capturedPoints.clear();
-            circle.clear();
             stepCross++;
 
             if((uint)stepCross >= crosses.size()){
diff --git a/app/SandboxSetup/beamerlocationgui.h b/app/SandboxSetup/beamerlocationgui.h
index 3010601..f095bdd 100644
--- a/app/SandboxSetup/beamerlocationgui.h
+++ b/app/SandboxSetup/beamerlocationgui.h
@@ -63,7 +63,8 @@ private:
     std::vector<cv::Point3f> capturedPoints;
     cv::Mat depth;
     cv::Mat rgb;
-    std::vector<cv::Point3i> circle;
+    QMutex *mut;
+    std::vector<cv::Point3i> circles;
     int stepCross = 0;
     QTimer *timer;
     RefreshFrame *myThread;
-- 
GitLab