TsnePlott.cpp 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. #include "pch.h"
  2. #include "TsnePlott.h"
  3. #include <limits>
  4. TsnePlott::TsnePlott(QWidget *parent)
  5. : SearchSpacePlott(parent)
  6. {
  7. setAxisLegend("X Axis", "Y Axis");
  8. }
  9. TsnePlott::~TsnePlott()
  10. {
  11. }
  12. void TsnePlott::assignMatrix(std::vector<SolutionPointData>::iterator begin, std::vector<SolutionPointData>::iterator end, double* yMatrixFromTsneAlgo, int n, ColorGradient& gradient)
  13. {
  14. this->begin = begin;
  15. this->end = end;
  16. this->yMatrixFromTsneAlgo = yMatrixFromTsneAlgo;
  17. this->N = n;
  18. updateColors(gradient);
  19. }
  20. void TsnePlott::clear()
  21. {
  22. begin = end = std::vector<SolutionPointData>::iterator();
  23. yMatrixFromTsneAlgo = nullptr;
  24. N = 0;
  25. colorPointDataVec.clear();
  26. update();
  27. }
  28. void TsnePlott::updateColors(ColorGradient& gradient)
  29. {
  30. colorPointDataVec.clear();
  31. for (auto iterator = begin; iterator != end; iterator++) {
  32. colorPointDataVec.push_back(gradient.getColorFromValue(iterator->objectiveFunction));
  33. }
  34. update();
  35. }
  36. void TsnePlott::frameGraphInView()
  37. {
  38. if (begin == end || !yMatrixFromTsneAlgo) {
  39. return;
  40. }
  41. VisibleWindow nextWindow(0,0,0,0);
  42. QPointF first(yMatrixFromTsneAlgo[0], yMatrixFromTsneAlgo[1]);
  43. nextWindow.xMin = nextWindow.xMax = first.x();
  44. nextWindow.yMin = nextWindow.yMax = first.y();
  45. for (int i = 1; i < this->N; i++) {
  46. QPointF point(yMatrixFromTsneAlgo[2 * i], yMatrixFromTsneAlgo[2 * i + 1]);
  47. if (point.x() < nextWindow.xMin) {
  48. nextWindow.xMin = point.x();
  49. }
  50. else if (point.x() > nextWindow.xMax) {
  51. nextWindow.xMax = point.x();
  52. }
  53. if (point.y() < nextWindow.yMin) {
  54. nextWindow.yMin = point.y();
  55. }
  56. else if (point.y() > nextWindow.yMax) {
  57. nextWindow.yMax = point.y();
  58. }
  59. }
  60. nextWindow.ZoomOut(0.05);
  61. if (std::abs(nextWindow.xMax - nextWindow.xMin) > std::numeric_limits<double>::min()) {
  62. window.xMin = nextWindow.xMin;
  63. window.xMax = nextWindow.xMax;
  64. }
  65. if (std::abs(nextWindow.yMax - nextWindow.yMin) > std::numeric_limits<double>::min()) {
  66. window.yMin = nextWindow.yMin;
  67. window.yMax = nextWindow.yMax;
  68. }
  69. update();
  70. }
  71. void TsnePlott::searchForPointUnderCursor()
  72. {
  73. //check if mouse stayed still
  74. QPoint globalPosition = QCursor::pos();
  75. QPointF pos = this->mapFromGlobal(globalPosition);
  76. if (pos != lastPosition) {
  77. return;
  78. }
  79. if (begin == end || !yMatrixFromTsneAlgo) {
  80. return;
  81. }
  82. QRect graphDisplayRect = getDisplayRect();
  83. QPointF translation(graphDisplayRect.left(), graphDisplayRect.top());
  84. double stregth_factorX = graphDisplayRect.width() / std::abs(window.xMax - window.xMin);
  85. double stregth_factorY = graphDisplayRect.height() / std::abs(window.yMax - window.yMin);
  86. QPointF minPoint = transformGraphToView(QPointF(yMatrixFromTsneAlgo[0], yMatrixFromTsneAlgo[1]));
  87. std::vector<SolutionPointData>::iterator minData = begin;
  88. double minSqaredDistance = sqaredDistance(transformGraphToView(minPoint, stregth_factorX, stregth_factorY, translation), pos);
  89. auto iter = begin;
  90. for (int i = 1; i < this->N; i++, iter++) {
  91. if (iter->iteration < minIter) {
  92. continue;
  93. }
  94. if (iter->iteration > maxIter) {
  95. break;
  96. }
  97. QPointF point(yMatrixFromTsneAlgo[2 * i], yMatrixFromTsneAlgo[2 * i + 1]);
  98. double distance = sqaredDistance(transformGraphToView(point, stregth_factorX, stregth_factorY, translation), pos);
  99. if (distance < minSqaredDistance) {
  100. minSqaredDistance = distance;
  101. minPoint = point;
  102. minData = iter;
  103. }
  104. }
  105. //if curser is radius + 3pixel away
  106. if (minSqaredDistance <= circleSize * circleSize + 9) {
  107. QPointF pointInWidget = transformGraphToView(minPoint, stregth_factorX, stregth_factorY, translation);
  108. QToolTip::showText(this->mapToGlobal(QPoint(pointInWidget.x(), pointInWidget.y())) - QPoint(0, 35), QString::fromStdString(minData->bitstringToStdString()));
  109. }
  110. }
  111. void TsnePlott::addPointsInWindowToScratchPad(VisibleWindow& window)
  112. {
  113. if (!pad) {
  114. qDebug() << "NoPad";
  115. return;
  116. }
  117. if (begin == end || !yMatrixFromTsneAlgo) {
  118. return;
  119. }
  120. auto iter = begin;
  121. for (int i = 1; i < this->N; i++, iter++) {
  122. if (iter->iteration < minIter) {
  123. continue;
  124. }
  125. if (iter->iteration > maxIter) {
  126. break;
  127. }
  128. QPointF point(yMatrixFromTsneAlgo[2 * i], yMatrixFromTsneAlgo[2 * i + 1]);
  129. if (window.inBound(point)) {
  130. pad->addPoint(*iter);
  131. }
  132. }
  133. }
  134. void TsnePlott::drawData(QPainter& painter)
  135. {
  136. if (begin == end || !yMatrixFromTsneAlgo) {
  137. return;
  138. }
  139. QRect graphDisplayRect = getDisplayRect();
  140. QPointF translation(graphDisplayRect.left(), graphDisplayRect.top());
  141. double stregth_factorX = graphDisplayRect.width() / std::abs(window.xMax - window.xMin);
  142. double stregth_factorY = graphDisplayRect.height() / std::abs(window.yMax - window.yMin);
  143. auto iter = begin;
  144. painter.setPen(Qt::transparent);
  145. for (int i = 0; i < this->N; i++, iter++) {
  146. if (iter->iteration < minIter) {
  147. continue;
  148. }
  149. if (iter->iteration > maxIter) {
  150. break;
  151. }
  152. QColor color = colorPointDataVec[i];
  153. color.setAlphaF(this->transparentAlphaValue);
  154. painter.setBrush(color);
  155. painter.drawEllipse(transformGraphToView(QPointF(yMatrixFromTsneAlgo[2 * i], yMatrixFromTsneAlgo[2 * i + 1]), stregth_factorX, stregth_factorY, translation), circleSize, circleSize);
  156. }
  157. painter.setBrush(Qt::BrushStyle::NoBrush);
  158. }