* @brief Renders the infinite grid overlay as a 2D pixmap. */
| 904 | * @brief Renders the infinite grid overlay as a 2D pixmap. |
| 905 | */ |
| 906 | QImage Widgets::Plot3D::renderGrid(const QMatrix4x4& matrix) |
| 907 | { |
| 908 | QImage img(widgetSize(), QImage::Format_ARGB32_Premultiplied); |
| 909 | img.setDevicePixelRatio(qApp->devicePixelRatio()); |
| 910 | img.fill(Qt::transparent); |
| 911 | |
| 912 | QPainter painter(&img); |
| 913 | painter.setRenderHint(QPainter::Antialiasing, true); |
| 914 | |
| 915 | const double numSteps = 10; |
| 916 | const double step = gridStep(); |
| 917 | const double l = numSteps * step; |
| 918 | const float cx = std::round(m_centerPoint.x() / step) * step; |
| 919 | const float cy = std::round(m_centerPoint.y() / step) * step; |
| 920 | |
| 921 | QVector<QPair<QVector3D, QVector3D>> gridLines; |
| 922 | for (int i = -numSteps; i <= numSteps; ++i) { |
| 923 | if (i == 0) |
| 924 | continue; |
| 925 | |
| 926 | float x = cx + i * step; |
| 927 | float y = cy + i * step; |
| 928 | const auto x1 = QVector3D(x, cy + l, 0); |
| 929 | const auto x2 = QVector3D(x, cy - l, 0); |
| 930 | const auto y1 = QVector3D(cx + l, y, 0); |
| 931 | const auto y2 = QVector3D(cx - l, y, 0); |
| 932 | |
| 933 | gridLines.append({x1, x2}); |
| 934 | gridLines.append({y1, y2}); |
| 935 | } |
| 936 | |
| 937 | const float ax = m_centerPoint.x(); |
| 938 | const float ay = m_centerPoint.y(); |
| 939 | QPair<QVector3D, QVector3D> xAxis = {QVector3D(ax - l, ay, 0), QVector3D(ax + l, ay, 0)}; |
| 940 | QPair<QVector3D, QVector3D> yAxis = {QVector3D(ax, ay - l, 0), QVector3D(ax, ay + l, 0)}; |
| 941 | |
| 942 | auto color = m_gridMinorColor; |
| 943 | color.setAlpha(100); |
| 944 | for (const auto& line : gridLines) |
| 945 | drawLine3D(painter, matrix, line.first, line.second, color, 1, Qt::DashLine); |
| 946 | |
| 947 | drawLine3D(painter, matrix, xAxis.first, xAxis.second, m_xAxisColor, 1.5, Qt::SolidLine); |
| 948 | drawLine3D(painter, matrix, yAxis.first, yAxis.second, m_yAxisColor, 1.5, Qt::SolidLine); |
| 949 | |
| 950 | const QString stepLabel = tr("Grid Interval: %1 unit(s)").arg(step); |
| 951 | painter.setPen(m_textColor); |
| 952 | painter.setFont(Misc::CommonFonts::instance().monoFont()); |
| 953 | painter.drawText(QPoint(8, height() - 8), stepLabel); |
| 954 | |
| 955 | return img; |
| 956 | } |
| 957 | |
| 958 | /** |
| 959 | * @brief Renders the camera orientation indicator as a 2D pixmap. |