From 9f190e4cb61f287bb20e28dfb85677c3a96d7cec Mon Sep 17 00:00:00 2001
From: even <philippe.even@loria.fr>
Date: Sat, 17 Nov 2018 20:29:49 +0100
Subject: [PATCH] Preliminary step display

---
 Code/Seg/BSTools/bsaccumulatoritem.cpp |  2 +-
 Code/Seg/BSTools/bsdetectionwidget.cpp | 16 ++++----
 Code/Seg/BSTools/bsstructureitem.cpp   | 25 +++++++------
 Code/Seg/BSTools/bsstructureitem.h     |  7 +++-
 Code/Seg/BlurredSegment/biptlist.cpp   |  1 -
 Code/Seg/BlurredSegment/bsdetector.cpp | 26 +++++++------
 Code/Seg/BlurredSegment/bsdetector.h   | 51 ++++++++++++++++----------
 7 files changed, 75 insertions(+), 53 deletions(-)

diff --git a/Code/Seg/BSTools/bsaccumulatoritem.cpp b/Code/Seg/BSTools/bsaccumulatoritem.cpp
index 73308d0..9a1c178 100755
--- a/Code/Seg/BSTools/bsaccumulatoritem.cpp
+++ b/Code/Seg/BSTools/bsaccumulatoritem.cpp
@@ -53,7 +53,7 @@ void BSAccumulatorItem::paint (QPainter *painter,
   Q_UNUSED (option);
   Q_UNUSED (widget);
 
-  if (! (detector->getBlurredSegments(1).empty ()
+  if (! (detector->getBlurredSegments().empty ()
          && detector->isFiltering (selaccu)))
   {
     painter->fillRect (0, 0, width, height, QBrush (Qt::black));
diff --git a/Code/Seg/BSTools/bsdetectionwidget.cpp b/Code/Seg/BSTools/bsdetectionwidget.cpp
index fc57ea0..98c8ebc 100755
--- a/Code/Seg/BSTools/bsdetectionwidget.cpp
+++ b/Code/Seg/BSTools/bsdetectionwidget.cpp
@@ -269,7 +269,7 @@ void BSDetectionWidget::keyPressEvent (QKeyEvent *event)
       break;
 
     case Qt::Key_L : // Next multi-selected segment
-      if (! detector.getBlurredSegments(1).empty ())
+      if (! detector.getBlurredSegments().empty ())
       {
         displayNext (event->modifiers () & Qt::ShiftModifier ? -1 : 1);
         cout << "Selection du segment " << exam << endl;
@@ -574,7 +574,7 @@ void BSDetectionWidget::displayBackground ()
 
 void BSDetectionWidget::writeDetectionResult ()
 {
-  BlurredSegment *bs = detector.getBlurredSegment (1);
+  BlurredSegment *bs = detector.getBlurredSegment ();
   if (bs != NULL)
   {
     ofstream outf ("seg.txt", ios::out);
@@ -600,7 +600,7 @@ void BSDetectionWidget::displayDetectionResult (bool aux, int hnum)
   QPainter painter (&augmentedImage);
   if (paintItBlack) painter.fillRect (0, 0, width, height, QBrush (Qt::black));
   paintItBlack = false;
-  vector<BlurredSegment *> bss = detector.getBlurredSegments (1);
+  vector<BlurredSegment *> bss = detector.getBlurredSegments ();
   if (! bss.empty ())
   {
     int index = 0;
@@ -609,7 +609,7 @@ void BSDetectionWidget::displayDetectionResult (bool aux, int hnum)
       drawBlurredSegment (painter, *it++, index++ == hnum);
   }
   else
-    drawBlurredSegment (painter, detector.getBlurredSegment (1));
+    drawBlurredSegment (painter, detector.getBlurredSegment ());
   if (udef) drawSelection (painter, p1, p2);
   update (QRect (QPoint (0, 0), QPoint (width, height)));
 
@@ -634,7 +634,7 @@ void BSDetectionWidget::displayDetectionResult (bool aux, int hnum)
 
 void BSDetectionWidget::displayNext (int dir)
 {
-  int size = (int) (detector.getBlurredSegments (1).size ());
+  int size = (int) (detector.getBlurredSegments ().size ());
   if (size != 0)
   {
     exam = (exam + dir) % size;
@@ -666,7 +666,7 @@ void BSDetectionWidget::saveExtractedSegment ()
 {
   if (detector.isMultiSelection ())
   {
-    vector<BlurredSegment *> bss = detector.getBlurredSegments (1);
+    vector<BlurredSegment *> bss = detector.getBlurredSegments ();
     vector<BlurredSegment *>::const_iterator it = bss.begin ();
     while (it != bss.end ())
     {
@@ -680,7 +680,7 @@ void BSDetectionWidget::saveExtractedSegment ()
   }
   else
   {
-    BlurredSegment *bs = detector.getBlurredSegment (1);
+    BlurredSegment *bs = detector.getBlurredSegment ();
     if (bs != NULL)
     {
       ExtractedSegment es;
@@ -759,7 +759,7 @@ void BSDetectionWidget::extractAll ()
   if (! formerMultiMode) detector.switchMultiSelection ();
 
   detector.detectAll ();
-  vector<BlurredSegment *> mbs = detector.getBlurredSegments (1);
+  vector<BlurredSegment *> mbs = detector.getBlurredSegments ();
   cout << mbs.size () << " blurred segments detected" << endl;
   displayDetectionResult (true);
 
diff --git a/Code/Seg/BSTools/bsstructureitem.cpp b/Code/Seg/BSTools/bsstructureitem.cpp
index 68b3064..8ea6913 100755
--- a/Code/Seg/BSTools/bsstructureitem.cpp
+++ b/Code/Seg/BSTools/bsstructureitem.cpp
@@ -11,8 +11,9 @@ const int BSStructureItem::DISPLAY_FINAL_SCANS_AND_FILTER = 3;
 const int BSStructureItem::DISPLAY_INITIAL_BLURRED_SEGMENT = 4;
 const int BSStructureItem::DISPLAY_INITIAL_CONNECTED_COMPONENTS = 5;
 const int BSStructureItem::DISPLAY_INITIAL_SCANS_AND_FILTER = 6;
+const int BSStructureItem::DISPLAY_PRELIM_BLURRED_SEGMENT = 7;
 const int BSStructureItem::DISPLAY_MIN = DISPLAY_FINAL_BLURRED_SEGMENT;
-const int BSStructureItem::DISPLAY_MAX = DISPLAY_INITIAL_SCANS_AND_FILTER;
+const int BSStructureItem::DISPLAY_MAX = DISPLAY_PRELIM_BLURRED_SEGMENT;
 
 const int BSStructureItem::DEFAULT_PEN_WIDTH = 1;
 const int BSStructureItem::LEFT_MARGIN = 16;
@@ -85,25 +86,27 @@ QRectF BSStructureItem::boundingRect () const
 }
 
 
-void BSStructureItem::paint(QPainter *painter,
-                            const QStyleOptionGraphicsItem *option,
-                            QWidget *widget)
+void BSStructureItem::paint (QPainter *painter,
+                             const QStyleOptionGraphicsItem *option,
+                             QWidget *widget)
 {
   Q_UNUSED (option);
   Q_UNUSED (widget);
 
   if (displayItem == DISPLAY_FINAL_BLURRED_SEGMENT)
-    paintBlurredSegment (painter, 1);
+    paintBlurredSegment (painter, BSDetector::STEP_FINAL);
   else if (displayItem == DISPLAY_FINAL_CONNECTED_COMPONENTS)
-    paintConnectedComponents (painter, 1);
+    paintConnectedComponents (painter, BSDetector::STEP_FINAL);
   else if (displayItem == DISPLAY_FINAL_SCANS_AND_FILTER)
-    paintScansAndFilter (painter, 1);
-  if (displayItem == DISPLAY_INITIAL_BLURRED_SEGMENT)
-    paintBlurredSegment (painter, 0);
+    paintScansAndFilter (painter, BSDetector::STEP_FINAL);
+  else if (displayItem == DISPLAY_INITIAL_BLURRED_SEGMENT)
+    paintBlurredSegment (painter, BSDetector::STEP_INITIAL);
   else if (displayItem == DISPLAY_INITIAL_CONNECTED_COMPONENTS)
-    paintConnectedComponents (painter, 0);
+    paintConnectedComponents (painter, BSDetector::STEP_INITIAL);
   else if (displayItem == DISPLAY_INITIAL_SCANS_AND_FILTER)
-    paintScansAndFilter (painter, 0);
+    paintScansAndFilter (painter, BSDetector::STEP_INITIAL);
+  else if (displayItem == DISPLAY_PRELIM_BLURRED_SEGMENT)
+    paintBlurredSegment (painter, BSDetector::STEP_PRELIM);
 }
 
 
diff --git a/Code/Seg/BSTools/bsstructureitem.h b/Code/Seg/BSTools/bsstructureitem.h
index 73d19c0..deacfb3 100755
--- a/Code/Seg/BSTools/bsstructureitem.h
+++ b/Code/Seg/BSTools/bsstructureitem.h
@@ -78,8 +78,7 @@ public:
   /**
    * \brief Returns the displayed information title.
    */
-  inline QString itemTitle () const
-  {
+  inline QString itemTitle () const {
     if (displayItem == DISPLAY_FINAL_BLURRED_SEGMENT)
       return ("Final blurred segment");
     else if (displayItem == DISPLAY_FINAL_CONNECTED_COMPONENTS)
@@ -92,6 +91,8 @@ public:
       return ("Initial connected components");
     else if (displayItem == DISPLAY_INITIAL_SCANS_AND_FILTER)
       return ("Initial scans and filter");
+    else if (displayItem == DISPLAY_PRELIM_BLURRED_SEGMENT)
+      return ("Preliminary blurred segment");
     else return ("No info");
   }
 
@@ -121,6 +122,8 @@ private:
   static const int DISPLAY_INITIAL_CONNECTED_COMPONENTS;
   /** Available information : initial scans and filter output. */
   static const int DISPLAY_INITIAL_SCANS_AND_FILTER;
+  /** Available information : preliminary blurred segment points and bounds. */
+  static const int DISPLAY_PRELIM_BLURRED_SEGMENT;
   /** Number of the first information. */
   static const int DISPLAY_MIN;
   /** Number of the last information. */
diff --git a/Code/Seg/BlurredSegment/biptlist.cpp b/Code/Seg/BlurredSegment/biptlist.cpp
index 233932b..7682fd1 100755
--- a/Code/Seg/BlurredSegment/biptlist.cpp
+++ b/Code/Seg/BlurredSegment/biptlist.cpp
@@ -1,5 +1,4 @@
 #include "biptlist.h"
-#include <iostream>
 
 
 BiPtList::BiPtList (Pt2i pt)
diff --git a/Code/Seg/BlurredSegment/bsdetector.cpp b/Code/Seg/BlurredSegment/bsdetector.cpp
index 85ffdd2..f428a2a 100755
--- a/Code/Seg/BlurredSegment/bsdetector.cpp
+++ b/Code/Seg/BlurredSegment/bsdetector.cpp
@@ -2,6 +2,10 @@
 
 
 
+const int BSDetector::STEP_FINAL = 0;
+const int BSDetector::STEP_INITIAL = 1;
+const int BSDetector::STEP_PRELIM = 2;
+
 const int BSDetector::RESULT_UNDETERMINED = -1;
 const int BSDetector::RESULT_OK = 0;
 const int BSDetector::RESULT_PRELIM_NO_DETECTION = 1;
@@ -44,8 +48,9 @@ BSDetector::BSDetector ()
   densityTestOn = true;
   multiSelection = false;
   autoResol = DEFAULT_AUTO_RESOLUTION;
-  prelim = false;
+  prelimDetectionOn = false;
 
+  bspre = NULL;
   bsini = NULL;
   bsf = NULL;
   resultValue = RESULT_UNDETERMINED;
@@ -154,6 +159,8 @@ void BSDetector::detect (const Pt2i &p1, const Pt2i &p2, Pt2i *p0)
   bst2->clear ();
   if (prefilteringOn) lsf1->clear ();
   if (filteringOn) lsf2->clear ();
+  if (bspre != NULL) delete bspre;
+  bspre = NULL;
   if (bsini != NULL) delete bsini;
   bsini = NULL;
   if (bsf != NULL) delete bsf;
@@ -165,19 +172,18 @@ void BSDetector::detect (const Pt2i &p1, const Pt2i &p2, Pt2i *p0)
 
   // Preliminary based on highest gradient without orientation constraint
   //---------------------------------------------------------------------
-  if (prelim)
+  if (prelimDetectionOn)
   {
-    BlurredSegment *bs0 = bst1->fastTrack (p1, p2, p0);
-    if (bs0 == NULL || bs0->size () < bsMinSize)
+    bspre = bst1->fastTrack (p1, p2, p0);
+    if (bspre == NULL || bspre->size () < bsMinSize)
     {
-      resultValue = (bs0 == NULL ? RESULT_PRELIM_NO_DETECTION
-                                 : RESULT_PRELIM_TOO_FEW);
-      if (bs0 != NULL) delete bs0;
+      resultValue = (bspre == NULL ? RESULT_PRELIM_NO_DETECTION
+                                   : RESULT_PRELIM_TOO_FEW);
       return;
     }
 
-    Vr2i v0 = bs0->getSupportVector ();
-    Pt2i pc = bs0->getSegment()->centerOfIntersection (p1, p2);
+    Vr2i v0 = bspre->getSupportVector ();
+    Pt2i pc = bspre->getSegment()->centerOfIntersection (p1, p2);
     v0.setOrthog ();
     int l = v0.chessboard ();
     int dx = (int) ((v0.x () * PRELIM_SIZE) / l);
@@ -189,7 +195,6 @@ void BSDetector::detect (const Pt2i &p1, const Pt2i &p2, Pt2i *p0)
       p0 = NULL;
     }
     bst1->clear ();
-    delete bs0;
   }
 
   // Initial detection based on highest gradient without orientation constraint
@@ -258,7 +263,6 @@ void BSDetector::detect (const Pt2i &p1, const Pt2i &p2, Pt2i *p0)
   //---------------------------------------------------------------------
   bsf = bst2->fineTrack (pCenter, bsini->getSupportVector(),
                          scanwidth, bswidth, gRef);
-
   if (bsf == NULL || bsf->size () < bsMinSize)
   {
     resultValue = (bsf == NULL ? RESULT_FINAL_NO_DETECTION
diff --git a/Code/Seg/BlurredSegment/bsdetector.h b/Code/Seg/BlurredSegment/bsdetector.h
index f714849..377771b 100755
--- a/Code/Seg/BlurredSegment/bsdetector.h
+++ b/Code/Seg/BlurredSegment/bsdetector.h
@@ -17,6 +17,13 @@ class BSDetector
 
 public:
 
+  /** Identifier for the final detection step. */
+  static const int STEP_FINAL;
+  /** Identifier for the initial detection step. */
+  static const int STEP_INITIAL;
+  /** Identifier for the preliminary detection step. */
+  static const int STEP_PRELIM;
+
   /** Extraction result : successful extraction. */
   static const int RESULT_UNDETERMINED;
   /** Extraction result : successful extraction. */
@@ -93,17 +100,22 @@ public:
 
   /**
    * \brief Returns the last detected blurred segment at given step.
-   * @param step Initial step addressed if set to 0, final step otherwise.
+   * @param step Detection step.
    */
-  inline BlurredSegment *getBlurredSegment (int step) {
-    return (step == 0 ? bsini : bsf); }
-
+  inline BlurredSegment *getBlurredSegment (int step = STEP_FINAL) {
+    if (step == STEP_FINAL) return (bsf);
+    else if (step == STEP_INITIAL) return (bsini);
+    else if (step == STEP_PRELIM) return (bspre);
+    return (bsf);
+  }
+    
   /**
    * \brief Returns the list of detected blurred segments at given step.
-   * @param step Initial step addressed if set to 0, final step otherwise.
+   * @param step Detection step. Preliminary step not considered here.
    */
-  inline vector<BlurredSegment *> getBlurredSegments (int step) {
-    return (step == 0 ? mbsini : mbsf); }
+  inline vector<BlurredSegment *> getBlurredSegments (int step = STEP_FINAL) {
+    return (step == STEP_FINAL ? mbsf : mbsini);
+  }
 
   /**
    * \brief Avoids the deletion of the last extracted blurred segment.
@@ -186,12 +198,12 @@ public:
   /**
    * \brief Returns the preliminary detection modality status.
    */
-  inline bool isPreliminary () { return (prelim); }
+  inline bool isPreliminary () { return (prelimDetectionOn); }
 
   /**
    * \brief Switches preliminary detection modality.
    */
-  inline void switchPreliminary () { prelim = ! prelim; }
+  inline void switchPreliminary () { prelimDetectionOn = ! prelimDetectionOn; }
 
   /**
    * \brief Returns the edge direction constraint status.
@@ -431,6 +443,9 @@ private :
   static const int PRELIM_SIZE;
 
 
+  /** Gradient map. */
+  VMap *gMap;
+
   /** Direction constraint of the detected edge :
    *    +1 : complies to initial direction
    *    -1 : complies to initial direction opposite
@@ -453,21 +468,22 @@ private :
   bool multiSelection;
   /** Grid resolution for the automatic extraction. */
   int autoResol;
-  /** Preliminary stage modality. */
-  bool prelim;
-
+  /** Result of the blurred segment extraction */
+  int resultValue;
 
-  /** Gradient map. */
-  VMap *gMap;
+  /** Preliminary stage modality. */
+  bool prelimDetectionOn;
+  /** Preliminary detected blurred segment. */
+  BlurredSegment *bspre;
 
-  /** Initial rough tracker */
+  /** Initial rough tracker. */
   BSTracker *bst1;
   /** Initially detected blurred segment (initial step result). */
   BlurredSegment *bsini;
   /** Detected blurred segments in case of multi-detection (initial step). */
   vector<BlurredSegment *> mbsini;
 
-  /** Fine tracker */
+  /** Fine tracker. */
   BSTracker *bst2;
   /** Detected blurred segment (final result). */
   BlurredSegment *bsf;
@@ -484,9 +500,6 @@ private :
   /** Line space based post-filter. */
   LineSpaceFilter *lsf2;
 
-  /** Result of the blurred segment extraction */
-  int resultValue;
-
 
   /**
    * \brief Detects all blurred segments between two input points.
-- 
GitLab