瀏覽代碼

Debut du developpement de ea-score

Rémi Synave 2 年之前
父節點
當前提交
94760f00aa
共有 4 個文件被更改,包括 69 次插入62 次删除
  1. 60 46
      StrengthLine/StrengthLine.cpp
  2. 8 2
      StrengthLine/StrengthLine.hpp
  3. 0 1
      clf/MainWindow.cpp
  4. 1 13
      clf/WorkArea.cpp

+ 60 - 46
StrengthLine/StrengthLine.cpp

@@ -24,9 +24,23 @@
 #include <cstdlib>
 #include <ctime>
 #include <stdexcept>
+#include <QDebug>
 
 #include "StrengthLine.hpp"
 
+StrengthLine::StrengthLine (double ax, double ay, double bx, double by)
+{
+  if (p1 == NULL || p2 == NULL)
+    throw std::
+      invalid_argument ("Points defining a strength line can't be NULL.");
+  if (ax == bx && ay ==  by)
+    throw std::
+      invalid_argument ("Points defining a strength line can't be equals.");
+
+  this->p1 = new QPoint(ax, ay);
+  this->p2 = new QPoint(bx, by);
+}
+
 StrengthLine::StrengthLine (QPoint * p1, QPoint * p2)
 {
   if (p1 == NULL || p2 == NULL)
@@ -62,34 +76,8 @@ StrengthLine::equation () const
   return std::make_pair (a, b);
 }
 
-std::pair < QPoint *, QPoint * >StrengthLine::toDraw (QImage * img) const
+std::pair < QPoint *, QPoint * >StrengthLine::toDraw (QImage * img) const 
 {
-  /*
-  // Methode qui va calculer les points pour l'affichage des droites
-  // sur l'image passées en paramètre. Calcul rapide donc
-  // les points peuvent sortir de l'image.
-
-  // ligne verticale
-  if (p1->x () == p2->x ())
-    return std::make_pair (new QPoint (p1->x (), 0),
-			   new QPoint (p1->x (), img->height ()));
-
-  // ligne horizontale
-  if (p1->y () == p2->y ())
-    return std::make_pair (new QPoint (0, p1->y ()),
-			   new QPoint (img->width (), p1->y ()));
-
-  // Equation de la droite et on prend les points gauche et droite de la ligne
-  std::pair < float, float >
-    eq = this->equation ();
-  float
-    a = std::get < 0 > (eq);
-  float
-    b = std::get < 1 > (eq);
-
-  return std::make_pair (new QPoint (0, b),
-			 new QPoint (img->width (), a * img->width () + b));
-  */
   return this->toDraw(img->width(), img->height());
 }
 
@@ -110,6 +98,7 @@ std::pair < QPoint *, QPoint * >StrengthLine::toDraw (int imgWidth, int imgHeigh
 			   new QPoint (imgWidth, p1->y ()));
 
   // Equation de la droite et on prend les points gauche et droite de la ligne
+  
   std::pair < float, float >
     eq = this->equation ();
   float
@@ -118,10 +107,15 @@ std::pair < QPoint *, QPoint * >StrengthLine::toDraw (int imgWidth, int imgHeigh
     b = std::get < 1 > (eq);
 
   return std::make_pair (new QPoint (0, b),
-			 new QPoint (imgWidth, a * imgWidth + b));
+			 new QPoint (imgWidth, imgWidth * a + b));
 }
 
 std::pair < QPoint *, QPoint * >StrengthLine::interpolateToEdge (QImage * img) const
+{
+  return this->interpolateToEdge(img->width(), img->height());
+}
+
+std::pair < QPoint *, QPoint * >StrengthLine::interpolateToEdge (int imgWidth, int imgHeight) const
 {
   // Methode qui va calculer les points sur les bords des images
   // en fonction des deux points attributs qui définissent
@@ -130,12 +124,12 @@ std::pair < QPoint *, QPoint * >StrengthLine::interpolateToEdge (QImage * img) c
   // ligne verticale
   if (p1->x () == p2->x ())
     return std::make_pair (new QPoint (p1->x (), 0),
-			   new QPoint (p1->x (), img->height ()));
+			   new QPoint (p1->x (), imgHeight));
 
   // ligne horizontale
   if (p1->y () == p2->y ())
     return std::make_pair (new QPoint (0, p1->y ()),
-			   new QPoint (img->width (), p1->y ()));
+			   new QPoint (imgWidth, p1->y ()));
 
   // Equation de la droite et on prend les points gauche et droite de la ligne
   std::pair < float, float >
@@ -149,7 +143,7 @@ std::pair < QPoint *, QPoint * >StrengthLine::interpolateToEdge (QImage * img) c
     {
       if(b<0)
 	{
-	  if((a*img->width()+b) > img->height())
+	  if((a*imgWidth+b) > imgHeight)
 	    {
 	      // |------------------------------------------|
 	      // |           *                              |
@@ -164,7 +158,7 @@ std::pair < QPoint *, QPoint * >StrengthLine::interpolateToEdge (QImage * img) c
 	      // |------------------------------------------|
 	      
 	      return std::make_pair (new QPoint (-1*(b/a), 0),
-				     new QPoint ((img->height()-b)/a, img->height ()));
+				     new QPoint ((imgHeight-b)/a, imgHeight));
 	    }
 	  else
 	    {
@@ -181,13 +175,13 @@ std::pair < QPoint *, QPoint * >StrengthLine::interpolateToEdge (QImage * img) c
 	      // |------------------------------------------|
 
 	      return std::make_pair (new QPoint (-1*(b/a), 0),
-				     new QPoint (img->width (), a * img->width () + b));
+				     new QPoint (imgWidth, a * imgWidth + b));
 	    }
 	}
       else // a>0 and b>0
 	{
 	  
-	  if((a*img->width()+b) > img->height())
+	  if((a*imgWidth+b) > imgHeight)
 	    {
 	      // |------------------------------------------|
 	      // |                                          |
@@ -202,7 +196,7 @@ std::pair < QPoint *, QPoint * >StrengthLine::interpolateToEdge (QImage * img) c
 	      // |------------------------------------------|
 	      
 	      return std::make_pair (new QPoint (0, b),
-				     new QPoint ((img->height () -b )/a, img->height ()));
+				     new QPoint ((imgHeight -b )/a, imgHeight));
 	    }
 	  else
 	    {
@@ -219,15 +213,15 @@ std::pair < QPoint *, QPoint * >StrengthLine::interpolateToEdge (QImage * img) c
 	      // |------------------------------------------|
 	      
 	      return std::make_pair (new QPoint (0, b),
-				     new QPoint (img->width(), a*img->width ()+b));
+				     new QPoint (imgWidth, a*imgWidth+b));
 	    }
 	}
     }
   else // a<0
     {
-      if((a*img->width()+b)>0)
+      if((a*imgWidth+b)>0)
 	{
-	  if(b > img->height())
+	  if(b > imgHeight)
 	    {
 	      // |------------------------------------------|
 	      // |                                          |
@@ -241,8 +235,8 @@ std::pair < QPoint *, QPoint * >StrengthLine::interpolateToEdge (QImage * img) c
 	      // |                            *             |
 	      // |------------------------------------------|
 	      
-	      return std::make_pair (new QPoint ((img->height()-b)/a, img->height()),
-				     new QPoint (img->width(), a*img->width()+b));
+	      return std::make_pair (new QPoint ((imgHeight-b)/a, imgHeight),
+				     new QPoint (imgWidth, a*imgWidth+b));
 	    }
 	  else
 	    {
@@ -259,13 +253,13 @@ std::pair < QPoint *, QPoint * >StrengthLine::interpolateToEdge (QImage * img) c
 	      // |------------------------------------------|
 
 	      return std::make_pair (new QPoint (0, b),
-				     new QPoint (img->width (), a * img->width () + b));
+				     new QPoint (imgWidth, a * imgWidth + b));
 	    }
 	}
-      else // a>0 and (a*img->width()+b) < 0
+      else // a>0 and (a*imgWidth+b) < 0
 	{
 	  
-	  if(b > img->height())
+	  if(b > imgHeight)
 	    {
 	      // |------------------------------------------|
 	      // |               *                          |
@@ -280,7 +274,7 @@ std::pair < QPoint *, QPoint * >StrengthLine::interpolateToEdge (QImage * img) c
 	      // |------------------------------------------|
 	      
 	      return std::make_pair (new QPoint (-1*(b/a), 0),
-				     new QPoint ((img->height()-b)/a, img->height ()));
+				     new QPoint ((imgHeight-b)/a, imgHeight));
 	    }
 	  else
 	    {
@@ -306,11 +300,31 @@ std::pair < QPoint *, QPoint * >StrengthLine::interpolateToEdge (QImage * img) c
 			 new QPoint (*p2));
 }
 
-double StrengthLine::distance(const StrengthLine *sl) const
+double StrengthLine::ea_score(const StrengthLine *sl, double imgWidth, double imgHeight) const
+{
+  return this->distance(sl, imgWidth, imgHeight);
+}
+
+double StrengthLine::distance(const StrengthLine *sl, double imgWidth, double imgHeight) const
 {
   std::srand(static_cast<unsigned int>(std::time(nullptr)+std::rand()));
+  std::pair < QPoint*, QPoint* > pointsSL1 = this->interpolateToEdge (imgWidth, imgHeight);
+  std::pair < QPoint*, QPoint* > pointsSL2 = sl->interpolateToEdge (imgWidth, imgHeight);
+  QPoint* p1SL1 = std::get < 0 > (pointsSL1);
+  QPoint* p2SL1 = std::get < 1 > (pointsSL1);
+  QPoint* p1SL2 = std::get < 0 > (pointsSL2);
+  QPoint* p2SL2 = std::get < 1 > (pointsSL2);
+
+
+  float midSL1X = ((p1SL1->x() + p2SL1->x())/2.0)/(imgWidth*1.0);
+  float midSL1Y = ((p1SL1->y() + p2SL1->y())/2.0)/(imgHeight*1.0);
+  float midSL2X = ((p1SL2->x() + p2SL2->x())/2.0)/(imgWidth*1.0);
+  float midSL2Y = ((p1SL2->y() + p2SL2->y())/2.0)/(imgHeight*1.0);
+
+
+  return sqrt(((midSL1X-midSL2X)*(midSL1X-midSL2X))+((midSL1Y-midSL2Y)*(midSL1Y-midSL2Y)));
   
-  return std::rand();
+  //return std::rand();
 }
 
 StrengthLine* StrengthLine::getRandomLine (QImage * img)

+ 8 - 2
StrengthLine/StrengthLine.hpp

@@ -24,17 +24,21 @@
 #ifndef STRENGTHLINE_HPP
 #define STRENGTHLINE_HPP
 
+#include <cmath>
+
 #include <QPoint>
 #include <QImage>
 
+
 class StrengthLine
 {
 private:
-  QPoint *p1, *p2;
+  QPoint *p1 = NULL, *p2 = NULL;
 
   std::pair < float, float > equation () const;
 
 public:
+  StrengthLine (double ax = 0.0, double ay = 0.0, double bx = 1.0, double by = 1.0);
   StrengthLine (QPoint * p1 = new QPoint (0, 0), QPoint * p2 =
 		new QPoint (1, 1));
   QPoint* getP1() const;
@@ -42,7 +46,9 @@ public:
   std::pair < QPoint *, QPoint * > toDraw (QImage * img) const;
   std::pair < QPoint *, QPoint * > toDraw (int imgWidth, int imgHeight) const;
   std::pair < QPoint *, QPoint * > interpolateToEdge (QImage * img) const;
-  double distance(const StrengthLine *sl) const;
+  std::pair < QPoint *, QPoint * > interpolateToEdge (int imgWidth, int imgHeight) const;
+  double ea_score(const StrengthLine *sl, double imgWidth, double imgHeight) const;
+  double distance(const StrengthLine *sl, double imgWidth, double imgHeight) const;
 
   static StrengthLine* getRandomLine (QImage * img);
   static StrengthLine* getRandomLine (int width, int height);

+ 0 - 1
clf/MainWindow.cpp

@@ -75,7 +75,6 @@ MainWindow::MainWindow ()
 void
 MainWindow::majDistance()
 {
-  qDebug() << wa->getDistance() << Qt::endl;
   la->setText(QString("Distance entre les deux lignes : ")+QString::number(wa->getDistance()));
 }
 

+ 1 - 13
clf/WorkArea.cpp

@@ -118,18 +118,6 @@ WorkArea::mousePressEvent (QMouseEvent * event)
 	  indexPointClicked = i;
 	}
     }
-
-  // Clic gauche
-  /*if (event->button () == Qt::LeftButton)
-    {
-      if (indexPointClicked == -1)
-	{
-	  if((maxLines == 0) || (liste_points.size()<(maxLines*2))){
-	    indexPointClicked = liste_points.size ();
-	    liste_points << new QPoint (event->x (), event->y ());
-	  }
-	}
-	}*/
   
   repaint ();
 }
@@ -159,5 +147,5 @@ WorkArea::getDistance() const
 {
   StrengthLine *sl1 = new StrengthLine(liste_points[0], liste_points[1]);
   StrengthLine *sl2 = new StrengthLine(liste_points[2], liste_points[3]);
-  return sl1->distance(sl2);
+  return sl1->distance(sl2, screenSizeX, screenSizeY);
 }