SLList.cpp 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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 "SLList.hpp"
  30. SLList::SLList (const char* file)
  31. {
  32. list.clear();
  33. QFile fin(QString::fromStdString (file));
  34. fin.open(QIODevice::ReadOnly);
  35. QByteArray ba = fin.readAll();
  36. QJsonParseError parseError;
  37. QJsonDocument doc = QJsonDocument::fromJson(ba, &parseError);
  38. QJsonArray lines = doc["lines"].toArray();
  39. for(int i=0; i<lines.size(); i++){
  40. QJsonArray line = lines[i].toArray();
  41. QJsonArray point1 = line[0].toArray();
  42. QJsonArray point2 = line[1].toArray();
  43. list << new StrengthLine(point1[0].toDouble(), point1[1].toDouble(), point2[0].toDouble(), point2[1].toDouble());
  44. }
  45. }
  46. std::pair<double, int> SLList::distance_avg(SLList* sllist, QImage* img) const
  47. {
  48. const SLList *list1 = this, *list2 = sllist;
  49. std::vector<double> mins;
  50. double sum = 0;
  51. double min;
  52. for(unsigned int i = 0 ; i < list1->size(); i++)
  53. {
  54. min = list1->get(i)->distance(list2->get(0), img->width(), img->height());
  55. for(unsigned int j = 1 ; j < list2->size(); j++)
  56. {
  57. double temp = list1->get(i)->distance(list2->get(j), img->width(), img->height());
  58. if(temp < min)
  59. min = temp;
  60. }
  61. mins.push_back(min);
  62. }
  63. unsigned int minsize = this->size();
  64. if(list2->size() < minsize)
  65. minsize = list2->size();
  66. sort(mins.begin(), mins.end());
  67. for(unsigned int i = 0 ; i < minsize ; i++)
  68. sum+=mins[i];
  69. sum/=minsize;
  70. return std::make_pair(sum, size()-sllist->size());
  71. }
  72. /*
  73. std::pair<double, int> SLList::distance_hausdorff(SLList* sllist, QImage* img) const
  74. {
  75. const SLList *list1 = this, *list2 = sllist;
  76. if(size() > sllist->size())
  77. {
  78. list1 = sllist;
  79. list2 = this;
  80. }
  81. double max = -1;
  82. double min;
  83. for(unsigned int i = 0 ; i < list1->size(); i++)
  84. {
  85. min = list1->get(i)->distance(list2->get(0), img->width(), img->height());
  86. for(unsigned int j = 1 ; j < list2->size(); j++)
  87. {
  88. double temp = list1->get(i)->distance(list2->get(j), img->width(), img->height());
  89. if(temp < min)
  90. min = temp;
  91. }
  92. if(max<0)
  93. max = min;
  94. else
  95. if(min>max)
  96. max=min;
  97. }
  98. return std::make_pair(max, size()-sllist->size());
  99. }
  100. */