Browse Source

AJut d'un soft qui calcule la distance entre deux droites.

Rémi Synave 2 years ago
parent
commit
bdd31d6934
9 changed files with 495 additions and 0 deletions
  1. 38 0
      StrengthLine/StrengthLine.cpp
  2. 2 0
      StrengthLine/StrengthLine.hpp
  3. 81 0
      clf/MainWindow.cpp
  4. 57 0
      clf/MainWindow.hpp
  5. 163 0
      clf/WorkArea.cpp
  6. 80 0
      clf/WorkArea.hpp
  7. 39 0
      clf/clf.pro
  8. BIN
      clf/damier.png
  9. 35 0
      clf/main.cpp

+ 38 - 0
StrengthLine/StrengthLine.cpp

@@ -64,6 +64,7 @@ StrengthLine::equation () 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.
@@ -88,6 +89,36 @@ std::pair < QPoint *, QPoint * >StrengthLine::toDraw (QImage * img) const
 
   return std::make_pair (new QPoint (0, b),
 			 new QPoint (img->width (), a * img->width () + b));
+  */
+  return this->toDraw(img->width(), img->height());
+}
+
+std::pair < QPoint *, QPoint * >StrengthLine::toDraw (int imgWidth, int imgHeight) 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 (), imgHeight));
+
+  // ligne horizontale
+  if (p1->y () == p2->y ())
+    return std::make_pair (new QPoint (0, 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 >
+    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 (imgWidth, a * imgWidth + b));
 }
 
 std::pair < QPoint *, QPoint * >StrengthLine::interpolateToEdge (QImage * img) const
@@ -275,6 +306,13 @@ std::pair < QPoint *, QPoint * >StrengthLine::interpolateToEdge (QImage * img) c
 			 new QPoint (*p2));
 }
 
+double StrengthLine::distance(const StrengthLine *sl) const
+{
+  std::srand(static_cast<unsigned int>(std::time(nullptr)+std::rand()));
+  
+  return std::rand();
+}
+
 StrengthLine* StrengthLine::getRandomLine (QImage * img)
 {
   std::srand(static_cast<unsigned int>(std::time(nullptr)+std::rand())); 

+ 2 - 0
StrengthLine/StrengthLine.hpp

@@ -40,7 +40,9 @@ public:
   QPoint* getP1() const;
   QPoint* getP2() const;
   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;
 
   static StrengthLine* getRandomLine (QImage * img);
   static StrengthLine* getRandomLine (int width, int height);

+ 81 - 0
clf/MainWindow.cpp

@@ -0,0 +1,81 @@
+/*********************************************************************/
+/*                                                                   */
+/* Copyright 2022-2023 Rémi Synave - remi.synave@univ-littoral.fr    */
+/*                                                                   */
+/* This file is part of DSL.                                         */
+/* This software uses Qt to build the Graphical User Interface       */
+/* https://www.qt.io/                                                */
+/*                                                                   */
+/* DSL is free software: you can redistribute it and/or modify       */
+/* it under the terms of the GNU General Public License as published */
+/* by the Free Software Foundation, either version 3 of the License, */
+/* or (at your option) any later version.                            */
+/*                                                                   */
+/* DSL is distributed in the hope that it will be useful,            */
+/* but WITHOUT ANY WARRANTY; without even the implied warranty of    */
+/* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the      */
+/* GNU General Public License for more details.                      */
+/*                                                                   */
+/* You should have received a copy of the GNU General Public License */
+/* along with DSL. If not, see <http://www.gnu.org/licenses/>.       */
+/*                                                                   */
+/*********************************************************************/
+
+#include <QIcon>
+#include <QGuiApplication>
+#include <QScreen>
+#include <QWidget>
+#include <QMenuBar>
+#include <QFileDialog>
+#include <QUrl>
+#include <QFileInfo>
+#include <QInputDialog>
+#include <QDirIterator>
+#include <QVBoxLayout>
+
+#include <QDebug>
+
+#include <iostream>
+#include <filesystem>
+
+#include "MainWindow.hpp"
+
+
+MainWindow::MainWindow ()
+{
+  setWindowTitle (tr ("CLF"));
+
+  QWidget *central= new QWidget(this);
+  setCentralWidget (central);
+  setContentsMargins(0, 0, 0, 0);
+
+  QVBoxLayout *layoutV = new QVBoxLayout(central);
+  layoutV->setSpacing(0);
+  layoutV->setContentsMargins(0, 0, 0, 0);
+  central->setLayout(layoutV);
+  
+  wa = new WorkArea (sizeX, sizeY, central);
+  wa->setContentsMargins(0, 0, 0, 0);
+  wa->addSL(new StrengthLine(new QPoint((int)(sizeX*0.45),(int)(sizeY*0.1)),
+			     new QPoint((int)(sizeX*0.45),(int)(sizeY*0.9))));
+  wa->addSL(new StrengthLine(new QPoint((int)(sizeX*0.55),(int)(sizeY*0.1)),
+			     new QPoint((int)(sizeX*0.55),(int)(sizeY*0.9))));
+  wa->repaint();
+
+  layoutV->addWidget(wa);
+
+  la = new QLabel(QString("Distance entre les deux lignes : ")+QString::number(wa->getDistance()), central);
+  layoutV->addWidget(la);
+
+  connect (wa, SIGNAL(click()), this, SLOT(majDistance()));
+
+  setFixedSize (sizeX, sizeY+50);
+}
+
+void
+MainWindow::majDistance()
+{
+  qDebug() << wa->getDistance() << Qt::endl;
+  la->setText(QString("Distance entre les deux lignes : ")+QString::number(wa->getDistance()));
+}
+

+ 57 - 0
clf/MainWindow.hpp

@@ -0,0 +1,57 @@
+/*********************************************************************/
+/*                                                                   */
+/* Copyright 2022-2023 Rémi Synave - remi.synave@univ-littoral.fr    */
+/*                                                                   */
+/* This file is part of DSL.                                         */
+/* This software uses Qt to build the Graphical User Interface       */
+/* https://www.qt.io/                                                */
+/*                                                                   */
+/* DSL is free software: you can redistribute it and/or modify       */
+/* it under the terms of the GNU General Public License as published */
+/* by the Free Software Foundation, either version 3 of the License, */
+/* or (at your option) any later version.                            */
+/*                                                                   */
+/* DSL is distributed in the hope that it will be useful,            */
+/* but WITHOUT ANY WARRANTY; without even the implied warranty of    */
+/* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the      */
+/* GNU General Public License for more details.                      */
+/*                                                                   */
+/* You should have received a copy of the GNU General Public License */
+/* along with DSL. If not, see <http://www.gnu.org/licenses/>.       */
+/*                                                                   */
+/*********************************************************************/
+
+#ifndef MAINWINDOW_HPP
+#define MAINWINDOW_HPP
+
+#include <QMainWindow>
+#include <QAction>
+#include <QMenu>
+#include <QString>
+#include <QMessageBox>
+#include <QLabel>
+
+#include "WorkArea.hpp"
+
+
+class MainWindow: public QMainWindow
+{
+  Q_OBJECT 
+
+
+private:
+  const int sizeX = 1000;
+  const int sizeY = 600;
+  
+  WorkArea *wa;
+  QLabel *la;
+  
+public:
+  MainWindow ();
+
+public slots:
+  void majDistance();
+  
+};
+
+#endif

+ 163 - 0
clf/WorkArea.cpp

@@ -0,0 +1,163 @@
+/*********************************************************************/
+/*                                                                   */
+/* Copyright 2022-2023 Rémi Synave - remi.synave@univ-littoral.fr    */
+/*                                                                   */
+/* This file is part of DSL.                                         */
+/* This software uses Qt to build the Graphical User Interface       */
+/* https://www.qt.io/                                                */
+/*                                                                   */
+/* DSL is free software: you can redistribute it and/or modify       */
+/* it under the terms of the GNU General Public License as published */
+/* by the Free Software Foundation, either version 3 of the License, */
+/* or (at your option) any later version.                            */
+/*                                                                   */
+/* DSL is distributed in the hope that it will be useful,            */
+/* but WITHOUT ANY WARRANTY; without even the implied warranty of    */
+/* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the      */
+/* GNU General Public License for more details.                      */
+/*                                                                   */
+/* You should have received a copy of the GNU General Public License */
+/* along with DSL. If not, see <http://www.gnu.org/licenses/>.       */
+/*                                                                   */
+/*********************************************************************/
+
+#include <QFile>
+#include <QColor>
+#include <QTextStream>
+#include <QJsonObject>
+#include <QJsonArray>
+#include <QJsonDocument>
+#include <QPalette>
+
+#include <QDebug>
+#include <QStringList>
+
+#include <iostream>
+
+#include "WorkArea.hpp"
+
+
+WorkArea::WorkArea (int screenSizeX, int screenSizeY, QWidget * parent):QWidget(parent)
+{
+  this->screenSizeX = screenSizeX;
+  this->screenSizeY = screenSizeY;
+  this->setGeometry(0,0,screenSizeX,screenSizeY);
+
+  QPalette pal = QPalette();
+  pal.setColor(QPalette::Window, Qt::gray);
+
+  this->setAutoFillBackground(true); 
+  this->setPalette(pal);
+
+
+  setFixedSize (screenSizeX, screenSizeY);
+  
+  indexPointClicked = -1;
+}
+
+WorkArea::~WorkArea ()
+{
+}
+
+
+
+
+
+void
+WorkArea::addSL (const StrengthLine *sl)
+{
+  liste_points << new QPoint((int)(sl->getP1()->x()), (int)(sl->getP1()->y()));
+  liste_points << new QPoint((int)(sl->getP2()->x()), (int)(sl->getP2()->y()));
+}
+
+
+void
+WorkArea::paintEvent (QPaintEvent * event)
+{
+  QPainter painter (this);
+  paint (painter);
+}
+
+void
+WorkArea::paint (QPainter & painter)
+{
+  for (int i = 0; i < liste_points.length (); i++)
+    {
+ 
+	  painter.setBrush (QBrush (Qt::green));
+	  painter.setPen (QPen (Qt::black));
+	  painter.drawEllipse (liste_points[i]->x () - taillePoint / 2,
+			       liste_points[i]->y () - taillePoint / 2,
+			       taillePoint, taillePoint);
+    
+
+
+      if ((i % 2) == 1)
+	{
+	  painter.setPen (QPen (Qt::red, epaisseurLigne));
+	  std::pair < QPoint *, QPoint * >endPoints =
+	    StrengthLine (liste_points[i - 1], liste_points[i]).toDraw (this->screenSizeX, this->screenSizeY);
+	  painter.drawLine (*(std::get < 0 > (endPoints)),
+			    *(std::get < 1 > (endPoints)));
+	}
+
+    }
+
+}
+
+void
+WorkArea::mousePressEvent (QMouseEvent * event)
+{
+  // Est ce qu'on a cliqué sur un point existant ?
+  indexPointClicked = -1;
+  for (int i = 0; i < liste_points.length (); i++)
+    {
+      QPoint point = event->pos () - *(liste_points[i]);
+      if (point.manhattanLength () < taillePoint)
+	{
+	  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 ();
+}
+
+void
+WorkArea::mouseMoveEvent (QMouseEvent * event)
+{
+  if (indexPointClicked != -1)
+    {
+      liste_points[indexPointClicked]->setX (event->x ());
+      liste_points[indexPointClicked]->setY (event->y ());
+    }
+ 
+  repaint ();
+}
+
+void
+WorkArea::mouseReleaseEvent (QMouseEvent * event)
+{
+  indexPointClicked = -1;
+
+  emit click();
+}
+
+double
+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);
+}

+ 80 - 0
clf/WorkArea.hpp

@@ -0,0 +1,80 @@
+/*********************************************************************/
+/*                                                                   */
+/* Copyright 2022-2023 Rémi Synave - remi.synave@univ-littoral.fr    */
+/*                                                                   */
+/* This file is part of DSL.                                         */
+/* This software uses Qt to build the Graphical User Interface       */
+/* https://www.qt.io/                                                */
+/*                                                                   */
+/* DSL is free software: you can redistribute it and/or modify       */
+/* it under the terms of the GNU General Public License as published */
+/* by the Free Software Foundation, either version 3 of the License, */
+/* or (at your option) any later version.                            */
+/*                                                                   */
+/* DSL is distributed in the hope that it will be useful,            */
+/* but WITHOUT ANY WARRANTY; without even the implied warranty of    */
+/* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the      */
+/* GNU General Public License for more details.                      */
+/*                                                                   */
+/* You should have received a copy of the GNU General Public License */
+/* along with DSL. If not, see <http://www.gnu.org/licenses/>.       */
+/*                                                                   */
+/*********************************************************************/
+
+#ifndef WORKAREA_HPP
+#define WORKAREA_HPP
+
+#include <string>
+
+#include <QWidget>
+#include <QPainter>
+#include <QMouseEvent>
+#include <QPoint>
+#include <QImage>
+#include <QImageReader>
+
+#include "StrengthLine.hpp"
+
+
+
+class WorkArea: public QWidget
+{
+  
+  Q_OBJECT
+
+  
+private:
+  const int taillePoint = 10;
+  const int epaisseurLigne = 3;
+  
+  unsigned int screenSizeX, screenSizeY;
+
+  QList < QPoint * >liste_points;
+  int indexPointClicked;
+
+  
+protected:
+  void paintEvent (QPaintEvent * event) override;
+  void mousePressEvent (QMouseEvent * event);
+  void mouseMoveEvent (QMouseEvent * event);
+  void mouseReleaseEvent (QMouseEvent * event);
+
+  
+public:
+  WorkArea (int screenSizeX, int screenSizeY, QWidget * parent = nullptr);
+  WorkArea (int screenSizeX, int screenSizeY, const std::string & filename, QWidget * parent = nullptr);
+   ~WorkArea ();
+
+  void loadImage (const std::string & filename);
+  
+  void paint (QPainter & painter);
+  
+  void addSL(const StrengthLine *sl);
+  double getDistance() const;
+
+signals:
+  void click();
+
+};
+
+#endif

+ 39 - 0
clf/clf.pro

@@ -0,0 +1,39 @@
+#/*********************************************************************/
+#/*                                                                   */
+#/* Copyright 2022-2023 Rémi Synave - remi.synave@univ-littoral.fr    */
+#/*                                                                   */
+#/* This file is part of DSL.                                         */
+#/* This software uses Qt to build the Graphical User Interface       */
+#/* https://www.qt.io/                                                */
+#/*                                                                   */
+#/* DSL is free software: you can redistribute it and/or modify       */
+#/* it under the terms of the GNU General Public License as published */
+#/* by the Free Software Foundation, either version 3 of the License, */
+#/* or (at your option) any later version.                            */
+#/*                                                                   */
+#/* DSL is distributed in the hope that it will be useful,            */
+#/* but WITHOUT ANY WARRANTY; without even the implied warranty of    */
+#/* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the      */
+#/* GNU General Public License for more details.                      */
+#/*                                                                   */
+#/* You should have received a copy of the GNU General Public License */
+#/* along with DSL. If not, see <http://www.gnu.org/licenses/>.       */
+#/*                                                                   */
+#/*********************************************************************/
+
+QT += core gui widgets
+
+INCLUDEPATH = ../StrengthLine/
+
+HEADERS = ../StrengthLine/StrengthLine.hpp \
+          WorkArea.hpp \
+          MainWindow.hpp
+                
+SOURCES = ../StrengthLine/StrengthLine.cpp \
+          WorkArea.cpp \
+          MainWindow.cpp \
+          main.cpp
+                    
+# install
+target.path = $$[QT_INSTALL_EXAMPLES]/clf
+INSTALLS += target

BIN
clf/damier.png


+ 35 - 0
clf/main.cpp

@@ -0,0 +1,35 @@
+/*********************************************************************/
+/*                                                                   */
+/* Copyright 2022-2023 Rémi Synave - remi.synave@univ-littoral.fr    */
+/*                                                                   */
+/* This file is part of DSL.                                         */
+/* This software uses Qt to build the Graphical User Interface       */
+/* https://www.qt.io/                                                */
+/*                                                                   */
+/* DSL is free software: you can redistribute it and/or modify       */
+/* it under the terms of the GNU General Public License as published */
+/* by the Free Software Foundation, either version 3 of the License, */
+/* or (at your option) any later version.                            */
+/*                                                                   */
+/* DSL is distributed in the hope that it will be useful,            */
+/* but WITHOUT ANY WARRANTY; without even the implied warranty of    */
+/* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the      */
+/* GNU General Public License for more details.                      */
+/*                                                                   */
+/* You should have received a copy of the GNU General Public License */
+/* along with DSL. If not, see <http://www.gnu.org/licenses/>.       */
+/*                                                                   */
+/*********************************************************************/
+
+#include <QApplication>
+
+#include "MainWindow.hpp"
+
+int
+main (int argc, char *argv[])
+{
+  QApplication app (argc, argv);
+  MainWindow window;
+  window.show ();
+  return app.exec ();
+}