SLList.cpp 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. /*********************************************************************/
  2. /* */
  3. /* Copyright 2022-2023 Rémi Synave - remi.synave@univ-littoral.fr */
  4. /* */
  5. /* This file is part of DSL. */
  6. /* This software uses Qt to build the Graphical User Interface */
  7. /* https://www.qt.io/ */
  8. /* */
  9. /* DSL is free software: you can redistribute it and/or modify */
  10. /* it under the terms of the GNU General Public License as published */
  11. /* by the Free Software Foundation, either version 3 of the License, */
  12. /* or (at your option) any later version. */
  13. /* */
  14. /* DSL is distributed in the hope that it will be useful, */
  15. /* but WITHOUT ANY WARRANTY; without even the implied warranty of */
  16. /* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the */
  17. /* GNU General Public License for more details. */
  18. /* */
  19. /* You should have received a copy of the GNU General Public License */
  20. /* along with DSL. If not, see <http://www.gnu.org/licenses/>. */
  21. /* */
  22. /*********************************************************************/
  23. #include <QDebug>
  24. #include <QFile>
  25. #include <QJsonObject>
  26. #include <QJsonArray>
  27. #include <QJsonDocument>
  28. #include <vector>
  29. #include <cmath>
  30. #include "SLList.hpp"
  31. SLList::SLList (const char* file)
  32. {
  33. list.clear();
  34. QFile fin(QString::fromStdString (file));
  35. fin.open(QIODevice::ReadOnly);
  36. QByteArray ba = fin.readAll();
  37. QJsonParseError parseError;
  38. QJsonDocument doc = QJsonDocument::fromJson(ba, &parseError);
  39. QJsonArray lines = doc["lines"].toArray();
  40. for(int i=0; i<lines.size(); i++){
  41. QJsonArray line = lines[i].toArray();
  42. QJsonArray point1 = line[0].toArray();
  43. QJsonArray point2 = line[1].toArray();
  44. list << new StrengthLine(point1[0].toDouble(), point1[1].toDouble(), point2[0].toDouble(), point2[1].toDouble());
  45. }
  46. }
  47. std::pair<double, int> SLList::distance_avg(SLList* sllist, QImage* img) const
  48. {
  49. const SLList *list1 = this, *list2 = sllist;
  50. std::vector<double> mins;
  51. double sum = 0;
  52. double min;
  53. for(unsigned int i = 0 ; i < list1->size(); i++)
  54. {
  55. min = list1->get(i)->distance(list2->get(0), img->width(), img->height());
  56. for(unsigned int j = 1 ; j < list2->size(); j++)
  57. {
  58. double temp = list1->get(i)->distance(list2->get(j), img->width(), img->height());
  59. if(temp < min)
  60. min = temp;
  61. }
  62. mins.push_back(min);
  63. }
  64. unsigned int minsize = this->size();
  65. if(list2->size() < minsize)
  66. minsize = list2->size();
  67. sort(mins.begin(), mins.end());
  68. for(unsigned int i = 0 ; i < minsize ; i++)
  69. sum+=mins[i];
  70. sum/=minsize;
  71. return std::make_pair(sum, size()-sllist->size());
  72. }
  73. std::pair<double, int> SLList::distance(SLList* sllist1, SLList* sllist2, QImage* img)
  74. {
  75. std::pair<double, int> d1 = sllist1->distance_avg(sllist2, img);
  76. std::pair<double, int> d2 = sllist2->distance_avg(sllist1, img);
  77. return std::make_pair((std::get<0>(d1)+std::get<0>(d2))/2, abs(std::get<1>(d1)));
  78. }
  79. /*
  80. std::pair<double, int> SLList::distance_hausdorff(SLList* sllist, QImage* img) const
  81. {
  82. const SLList *list1 = this, *list2 = sllist;
  83. if(size() > sllist->size())
  84. {
  85. list1 = sllist;
  86. list2 = this;
  87. }
  88. double max = -1;
  89. double min;
  90. for(unsigned int i = 0 ; i < list1->size(); i++)
  91. {
  92. min = list1->get(i)->distance(list2->get(0), img->width(), img->height());
  93. for(unsigned int j = 1 ; j < list2->size(); j++)
  94. {
  95. double temp = list1->get(i)->distance(list2->get(j), img->width(), img->height());
  96. if(temp < min)
  97. min = temp;
  98. }
  99. if(max<0)
  100. max = min;
  101. else
  102. if(min>max)
  103. max=min;
  104. }
  105. return std::make_pair(max, size()-sllist->size());
  106. }
  107. */