File indexing completed on 2025-01-26 05:19:14

0001 /* Atelier KDE Printer Host for 3D Printing
0002     Copyright (C) <2017-2018>
0003     Author: Patrick José Pereira - patrickjp@kde.org
0004             Kevin Ottens - ervin@kde.org
0005 
0006     This program is free software; you can redistribute it and/or
0007     modify it under the terms of the GNU General Public License as
0008     published by the Free Software Foundation; either version 3 of
0009     the License or any later version accepted by the membership of
0010     KDE e.V. (or its successor approved by the membership of KDE
0011     e.V.), which shall act as a proxy defined in Section 14 of
0012     version 3 of the license.
0013 
0014     This program is distributed in the hope that it will be useful,
0015     but WITHOUT ANY WARRANTY; without even the implied warranty of
0016     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
0017     GNU General Public License for more details.
0018 
0019     You should have received a copy of the GNU General Public License
0020     along with this program.  If not, see <http://www.gnu.org/licenses/>.
0021 */
0022 
0023 #include "gridmesh.h"
0024 #include "linemeshgeometry.h"
0025 #include <QGeometryRenderer>
0026 #include <QSize>
0027 #include <QVector3D>
0028 
0029 GridMesh::GridMesh(Qt3DCore::QNode *parent)
0030     : Qt3DRender::QGeometryRenderer(parent)
0031 {
0032     setInstanceCount(1);
0033     setIndexOffset(0);
0034     setFirstInstance(0);
0035     setPrimitiveType(Qt3DRender::QGeometryRenderer::Lines);
0036     setMeshResolution(QSize(20, 20));
0037 }
0038 
0039 QSize GridMesh::meshResolution() const
0040 {
0041     return m_meshResolution;
0042 }
0043 
0044 void GridMesh::setMeshResolution(const QSize &meshResolution)
0045 {
0046     if (meshResolution == m_meshResolution) {
0047         return;
0048     }
0049 
0050     QVector<QVector3D> vertices;
0051 
0052     const float dx = 1.0f / static_cast<float>(meshResolution.width());
0053     for (int col = 0; col <= meshResolution.width(); col++) {
0054         const float x = col * dx;
0055         vertices.append(QVector3D(x, 0.0f, 0.0f));
0056         vertices.append(QVector3D(x, 1.0f, 0.0f));
0057     }
0058 
0059     const float dy = 1.0f / static_cast<float>(meshResolution.height());
0060     for (int row = 0; row <= meshResolution.height(); row++) {
0061         const float y = row * dy;
0062         vertices.append(QVector3D(0.0f, y, 0.0f));
0063         vertices.append(QVector3D(1.0f, y, 0.0f));
0064     }
0065 
0066     if (geometry()) {
0067         auto geom = geometry();
0068         setGeometry(nullptr);
0069         delete geom;
0070     }
0071 
0072     auto geom = new LineMeshGeometry(vertices, this);
0073     setVertexCount(geom->vertexCount());
0074     setGeometry(geom);
0075 
0076     m_meshResolution = meshResolution;
0077     emit meshResolutionChanged(m_meshResolution);
0078 }