Fri, 23 Mar 2018 21:30:24 +0200
reworked Vertex, no longer a QVector3D subclass
--- a/src/algorithms/invert.cpp Fri Mar 23 19:30:53 2018 +0200 +++ b/src/algorithms/invert.cpp Fri Mar 23 21:30:24 2018 +0200 @@ -36,13 +36,13 @@ { Vertex const& v_i = subfileObject->vertex(i); - if (not qFuzzyCompare(v_i.x(), 0.f)) + if (not qFuzzyCompare(v_i.x, 0.0)) dimensions.removeOne(X); - if (not qFuzzyCompare(v_i.y(), 0.f)) + if (not qFuzzyCompare(v_i.y, 0.0)) dimensions.removeOne(Y); - if (not qFuzzyCompare(v_i.z(), 0.f)) + if (not qFuzzyCompare(v_i.z, 0.0)) dimensions.removeOne(Z); }
--- a/src/basics.cpp Fri Mar 23 19:30:53 2018 +0200 +++ b/src/basics.cpp Fri Mar 23 21:30:24 2018 +0200 @@ -20,106 +20,158 @@ #include "linetypes/modelobject.h" #include "lddocument.h" -Vertex::Vertex() : - QVector3D() {} - -Vertex::Vertex (const QVector3D& a) : - QVector3D (a) {} - -Vertex::Vertex (qreal xpos, qreal ypos, qreal zpos) : - QVector3D(xpos, ypos, zpos) {} +void Vertex::transform(const Matrix& matrix, const Vertex& pos) +{ + double x2 = (matrix(0, 0) * x) + (matrix(0, 1) * y) + (matrix(0, 2) * z) + pos.x; + double y2 = (matrix(1, 0) * x) + (matrix(1, 1) * y) + (matrix(1, 2) * z) + pos.y; + double z2 = (matrix(2, 0) * x) + (matrix(2, 1) * y) + (matrix(2, 2) * z) + pos.z; + this->x = x2; + this->y = y2; + this->z = z2; +} - -void Vertex::transform (const Matrix& matrix, const Vertex& pos) +void Vertex::apply(ApplyFunction func) { - double x2 = (matrix(0, 0) * x()) + (matrix(0, 1) * y()) + (matrix(0, 2) * z()) + pos.x(); - double y2 = (matrix(1, 0) * x()) + (matrix(1, 1) * y()) + (matrix(1, 2) * z()) + pos.y(); - double z2 = (matrix(2, 0) * x()) + (matrix(2, 1) * y()) + (matrix(2, 2) * z()) + pos.z(); - setX (x2); - setY (y2); - setZ (z2); + func(X, this->x); + func(Y, this->y); + func(Z, this->z); +} + +void Vertex::apply(ApplyConstFunction func) const +{ + func(X, this->x); + func(Y, this->y); + func(Z, this->z); } -void Vertex::apply (ApplyFunction func) +double& Vertex::operator[](Axis axis) { - double newX = x(), newY = y(), newZ = z(); - func (X, newX); - func (Y, newY); - func (Z, newZ); - *this = Vertex (newX, newY, newZ); -} + switch (axis) + { + case X: + return this->x; -void Vertex::apply (ApplyConstFunction func) const -{ - func (X, x()); - func (Y, y()); - func (Z, z()); + case Y: + return this->y; + + case Z: + return this->z; + + default: + return ::sink<double>(); + } } -double Vertex::operator[] (Axis ax) const +double Vertex::operator[] (Axis axis) const { - switch (ax) + switch (axis) { - case X: return x(); - case Y: return y(); - case Z: return z(); + case X: + return this->x; + + case Y: + return this->y; + + case Z: + return this->z; + + default: + return 0.0; } - - return 0.0; } -void Vertex::setCoordinate (Axis ax, qreal value) +void Vertex::setCoordinate (Axis axis, qreal value) { - switch (ax) - { - case X: setX (value); break; - case Y: setY (value); break; - case Z: setZ (value); break; - } + (*this)[axis] = value; } QString Vertex::toString (bool mangled) const { if (mangled) - return format ("(%1, %2, %3)", x(), y(), z()); - - return format ("%1 %2 %3", x(), y(), z()); + return ::format("(%1, %2, %3)", this->x, this->y, this->z); + else + return ::format("%1 %2 %3", this->x, this->y, this->z); } Vertex Vertex::operator* (qreal scalar) const { - return Vertex (x() * scalar, y() * scalar, z() * scalar); + return {this->x * scalar, this->y * scalar, this->z * scalar}; } -Vertex& Vertex::operator+= (const Vertex& other) +Vertex& Vertex::operator+=(const QVector3D& other) { - setX (x() + other.x()); - setY (y() + other.y()); - setZ (z() + other.z()); + this->x += other.x(); + this->y += other.y(); + this->z += other.z(); return *this; } -Vertex Vertex::operator+ (const Vertex& other) const +Vertex Vertex::operator+(const QVector3D& other) const { Vertex result (*this); result += other; return result; } +QVector3D Vertex::toVector() const +{ + return { + static_cast<float>(this->x), + static_cast<float>(this->y), + static_cast<float>(this->z) + }; +} + +Vertex Vertex::operator- (const QVector3D& vector) const +{ + Vertex result = *this; + result -= vector; + return result; +} + +Vertex& Vertex::operator-= (const QVector3D& vector) +{ + this->x -= vector.x(); + this->y -= vector.y(); + this->z -= vector.z(); + return *this; +} + +QVector3D Vertex::operator- (const Vertex& other) const +{ + return { + static_cast<float>(this->x - other.x), + static_cast<float>(this->y - other.y), + static_cast<float>(this->z - other.z) + }; +} + Vertex& Vertex::operator*= (qreal scalar) { - setX (x() * scalar); - setY (y() * scalar); - setZ (z() * scalar); + x *= scalar; + y *= scalar; + z *= scalar; return *this; } +bool Vertex::operator==(const Vertex& other) const +{ + return this->x == other.x and this->y == other.y and this->z == other.z; +} + +bool Vertex::operator!=(const Vertex& other) const +{ + return not (*this == other); +} + bool Vertex::operator< (const Vertex& other) const { - if (x() != other.x()) return x() < other.x(); - if (y() != other.y()) return y() < other.y(); - if (z() != other.z()) return z() < other.z(); - return false; + if (not qFuzzyCompare(this->x, other.x)) + return this->x < other.x; + else if (not qFuzzyCompare(this->y, other.y)) + return this->y < other.y; + else + return this->z < other.z; } /* @@ -128,9 +180,15 @@ Vertex Vertex::transformed(const GLRotationMatrix& matrix) const { return { - matrix(0, 0) * x() + matrix(0, 1) * y() + matrix(0, 2) * z(), - matrix(1, 0) * x() + matrix(1, 1) * y() + matrix(1, 2) * z(), - matrix(2, 0) * x() + matrix(2, 1) * y() + matrix(2, 2) * z(), + matrix(0, 0) * this->x + + matrix(0, 1) * this->y + + matrix(0, 2) * this->z, + matrix(1, 0) * this->x + + matrix(1, 1) * this->y + + matrix(1, 2) * this->z, + matrix(2, 0) * this->x + + matrix(2, 1) * this->y + + matrix(2, 2) * this->z, }; } @@ -153,12 +211,12 @@ // void BoundingBox::calcVertex (const Vertex& vertex) { - m_vertex0.setX (qMin (vertex.x(), m_vertex0.x())); - m_vertex0.setY (qMin (vertex.y(), m_vertex0.y())); - m_vertex0.setZ (qMin (vertex.z(), m_vertex0.z())); - m_vertex1.setX (qMax (vertex.x(), m_vertex1.x())); - m_vertex1.setY (qMax (vertex.y(), m_vertex1.y())); - m_vertex1.setZ (qMax (vertex.z(), m_vertex1.z())); + m_vertex0.x = qMin(vertex.x, m_vertex0.x); + m_vertex0.y = qMin(vertex.y, m_vertex0.y); + m_vertex0.z = qMin(vertex.z, m_vertex0.z); + m_vertex1.x = qMax(vertex.x, m_vertex1.x); + m_vertex1.y = qMax(vertex.y, m_vertex1.y); + m_vertex1.z = qMax(vertex.z, m_vertex1.z); m_isEmpty = false; } @@ -168,8 +226,8 @@ // void BoundingBox::reset() { - m_vertex0 = Vertex (10000.0, 10000.0, 10000.0); - m_vertex1 = Vertex (-10000.0, -10000.0, -10000.0); + m_vertex0 = {10000.0, 10000.0, 10000.0}; + m_vertex1 = {-10000.0, -10000.0, -10000.0}; m_isEmpty = true; } @@ -179,23 +237,11 @@ // double BoundingBox::longestMeasurement() const { - double xscale = (m_vertex0.x() - m_vertex1.x()); - double yscale = (m_vertex0.y() - m_vertex1.y()); - double zscale = (m_vertex0.z() - m_vertex1.z()); - double size = zscale; - - if (xscale > yscale) - { - if (xscale > zscale) - size = xscale; - } - else if (yscale > zscale) - size = yscale; - - if (qAbs (size) >= 2.0) - return qAbs (size / 2); - - return 1.0; + double xscale = m_vertex0.x - m_vertex1.x; + double yscale = m_vertex0.y - m_vertex1.y; + double zscale = m_vertex0.z - m_vertex1.z; + double size = qMax(xscale, qMax(yscale, zscale)); + return qMax(qAbs(size / 2.0), 1.0); } // ============================================================================= @@ -204,10 +250,11 @@ // Vertex BoundingBox::center() const { - return Vertex ( - (m_vertex0.x() + m_vertex1.x()) / 2, - (m_vertex0.y() + m_vertex1.y()) / 2, - (m_vertex0.z() + m_vertex1.z()) / 2); + return { + (m_vertex0.x + m_vertex1.x) / 2, + (m_vertex0.y + m_vertex1.y) / 2, + (m_vertex0.z + m_vertex1.z) / 2 + }; } bool BoundingBox::isEmpty() const @@ -240,7 +287,7 @@ uint qHash(const Vertex& key) { - return qHash(key.x()) ^ rotl10(qHash(key.y())) ^ rotl20(qHash(key.z())); + return qHash(key.x) ^ rotl10(qHash(key.y)) ^ rotl20(qHash(key.z)); } /*
--- a/src/basics.h Fri Mar 23 19:30:53 2018 +0200 +++ b/src/basics.h Fri Mar 23 21:30:24 2018 +0200 @@ -95,32 +95,33 @@ Winding operator^(Winding one, Winding other); Winding& operator^=(Winding& one, Winding other); -// -// Derivative of QVector3D: this class is used for the vertices. -// -class Vertex : public QVector3D +struct Vertex { -public: + qreal x, y, z; + using ApplyFunction = std::function<void (Axis, double&)>; using ApplyConstFunction = std::function<void (Axis, double)>; - Vertex(); - Vertex (const QVector3D& a); - Vertex (qreal xpos, qreal ypos, qreal zpos); - void apply (ApplyFunction func); void apply (ApplyConstFunction func) const; QString toString (bool mangled = false) const; + QVector3D toVector() const; void transform (const Matrix& matr, const Vertex& pos); Vertex transformed(const GLRotationMatrix& matrix) const; void setCoordinate (Axis ax, qreal value); - Vertex& operator+= (const Vertex& other); - Vertex operator+ (const Vertex& other) const; + Vertex& operator+= (const QVector3D& other); + Vertex operator+ (const QVector3D& other) const; + QVector3D operator- (const Vertex& other) const; + Vertex operator- (const QVector3D& vector) const; + Vertex& operator-= (const QVector3D& vector); Vertex& operator*= (qreal scalar); Vertex operator* (qreal scalar) const; bool operator< (const Vertex& other) const; + double& operator[] (Axis ax); double operator[] (Axis ax) const; + bool operator==(const Vertex& other) const; + bool operator!=(const Vertex& other) const; }; inline Vertex operator* (qreal scalar, const Vertex& vertex)
--- a/src/canvas.cpp Fri Mar 23 19:30:53 2018 +0200 +++ b/src/canvas.cpp Fri Mar 23 21:30:24 2018 +0200 @@ -96,8 +96,8 @@ { case Grid::Cartesian: { - qreal x0 = sign(topLeft.x()) * (fabs(topLeft.x()) - fmod(fabs(topLeft.x()), gridSize)); - qreal y0 = sign(topLeft.y()) * (fabs(topLeft.y()) - fmod(fabs(topLeft.y()), gridSize)); + qreal x0 = sign(topLeft.x) * (fabs(topLeft.x) - fmod(fabs(topLeft.x), gridSize)); + qreal y0 = sign(topLeft.y) * (fabs(topLeft.y) - fmod(fabs(topLeft.y), gridSize)); static const auto prepareGridLine = [](qreal value) -> bool { @@ -116,7 +116,7 @@ } }; - for (qreal x = x0; x < bottomRight.x(); x += gridSize) + for (qreal x = x0; x < bottomRight.x; x += gridSize) { if (prepareGridLine(x)) { @@ -125,7 +125,7 @@ } } - for (qreal y = y0; y < bottomRight.y(); y += gridSize) + for (qreal y = y0; y < bottomRight.y; y += gridSize) { if (prepareGridLine(y)) { @@ -142,10 +142,10 @@ const qreal size = grid()->coordinateSnap(); Vertex topLeft = currentCamera().idealize(currentCamera().convert2dTo3d({0, 0})); Vertex bottomRight = currentCamera().idealize(currentCamera().convert2dTo3d({width(), height()})); - QPointF topLeft2d {topLeft.x(), topLeft.y()}; - QPointF bottomLeft2d {topLeft.x(), bottomRight.y()}; - QPointF bottomRight2d {bottomRight.x(), bottomRight.y()}; - QPointF topRight2d {bottomRight.x(), topLeft.y()}; + QPointF topLeft2d {topLeft.x, topLeft.y}; + QPointF bottomLeft2d {topLeft.x, bottomRight.y}; + QPointF bottomRight2d {bottomRight.x, bottomRight.y}; + QPointF topRight2d {bottomRight.x, topLeft.y}; qreal smallestRadius = distanceFromPointToRectangle(pole, QRectF{topLeft2d, bottomRight2d}); qreal largestRadius = max(QLineF {topLeft2d, pole}.length(), QLineF {bottomLeft2d, pole}.length(),
--- a/src/dialogs/subfilereferenceeditor.cpp Fri Mar 23 19:30:53 2018 +0200 +++ b/src/dialogs/subfilereferenceeditor.cpp Fri Mar 23 21:30:24 2018 +0200 @@ -14,9 +14,9 @@ this->ui.referenceName->setText(reference->referenceName()); this->color = reference->color(); ::setColorButton(this->ui.colorButton, this->color); - this->ui.positionX->setValue(reference->position().x()); - this->ui.positionY->setValue(reference->position().y()); - this->ui.positionZ->setValue(reference->position().z()); + this->ui.positionX->setValue(reference->position().x); + this->ui.positionY->setValue(reference->position().y); + this->ui.positionZ->setValue(reference->position().z); connect( this->ui.colorButton, &QPushButton::clicked,
--- a/src/glShared.h Fri Mar 23 19:30:53 2018 +0200 +++ b/src/glShared.h Fri Mar 23 21:30:24 2018 +0200 @@ -29,7 +29,7 @@ inline void glVertex(const Vertex& vertex) { - glVertex3f(vertex.x(), vertex.y(), vertex.z()); + glVertex3f(vertex.x, vertex.y, vertex.z); } class LDObject;
--- a/src/glcompiler.cpp Fri Mar 23 19:30:53 2018 +0200 +++ b/src/glcompiler.cpp Fri Mar 23 21:30:24 2018 +0200 @@ -426,7 +426,7 @@ } // Determine the normals for the polygon. - Vertex normals[4]; + QVector3D normals[4]; auto vertexRing = ring(poly.vertices, vertexCount); for (int i = 0; i < vertexCount; ++i) @@ -434,7 +434,7 @@ const Vertex& v1 = vertexRing[i - 1]; const Vertex& v2 = vertexRing[i]; const Vertex& v3 = vertexRing[i + 1]; - normals[i] = Vertex::crossProduct(v3 - v2, v1 - v2).normalized(); + normals[i] = QVector3D::crossProduct(v3 - v2, v1 - v2).normalized(); } for (VboSubclass complement : iterateEnum<VboSubclass>()) @@ -448,9 +448,9 @@ if (complement == VboSubclass::Surfaces) { // Write coordinates. Apparently Z must be flipped too? - vbodata << poly.vertices[vert].x() - << -poly.vertices[vert].y() - << -poly.vertices[vert].z(); + vbodata << poly.vertices[vert].x + << -poly.vertices[vert].y + << -poly.vertices[vert].z; } else if (complement == VboSubclass::Normals) {
--- a/src/linetypes/modelobject.cpp Fri Mar 23 19:30:53 2018 +0200 +++ b/src/linetypes/modelobject.cpp Fri Mar 23 21:30:24 2018 +0200 @@ -259,19 +259,19 @@ // ============================================================================= // -// Moves this object using the given vertex as a movement List +// Moves this object using the given vector // -void LDObject::move (Vertex vect) +void LDObject::move (const QVector3D vector) { if (hasMatrix()) { LDMatrixObject* mo = static_cast<LDMatrixObject*> (this); - mo->setPosition (mo->position() + vect); + mo->setPosition (mo->position() + vector); } else { for (int i = 0; i < numVertices(); ++i) - setVertex (i, vertex (i) + vect); + setVertex (i, vertex (i) + vector); } } @@ -368,18 +368,11 @@ m_position (pos), m_transformationMatrix (transform) {} -void LDMatrixObject::setCoordinate (const Axis ax, double value) +void LDMatrixObject::setCoordinate (const Axis axis, double value) { - Vertex v = position(); - - switch (ax) - { - case X: v.setX (value); break; - case Y: v.setY (value); break; - case Z: v.setZ (value); break; - } - - setPosition (v); + Vertex position = this->position(); + position.setCoordinate(axis, value); + this->setPosition(position); } const Vertex& LDMatrixObject::position() const @@ -424,11 +417,10 @@ { if (t >= 0.0 and t <= 1.0) { - Vertex result; - result += pow (1.0 - t, 3) * vertex (0); - result += (3 * pow (1.0 - t, 2) * t) * vertex (2); - result += (3 * (1.0 - t) * pow (t, 2)) * vertex (3); - result += pow (t, 3) * vertex (1); + Vertex result = pow(1.0 - t, 3) * vertex(0); + result += (3 * pow(1.0 - t, 2) * t) * vertex(2).toVector(); + result += (3 * (1.0 - t) * pow(t, 2)) * vertex(3).toVector(); + result += pow(t, 3) * vertex(1).toVector(); return result; } else
--- a/src/linetypes/modelobject.h Fri Mar 23 19:30:53 2018 +0200 +++ b/src/linetypes/modelobject.h Fri Mar 23 21:30:24 2018 +0200 @@ -69,7 +69,7 @@ virtual bool isColored() const; bool isHidden() const; virtual bool isScemantic() const; // Does this object have meaning in the part model? - void move (Vertex vect); + void move (const QVector3D vector); virtual int numVertices() const; virtual int numPolygonVertices() const; virtual QString objectListText() const;
--- a/src/mathfunctions.cpp Fri Mar 23 19:30:53 2018 +0200 +++ b/src/mathfunctions.cpp Fri Mar 23 21:30:24 2018 +0200 @@ -27,9 +27,9 @@ void MathFunctions::rotateVertex(Vertex& vertex, const Vertex& rotationPoint, const Matrix& transformationMatrix) const { - vertex -= rotationPoint; + vertex -= rotationPoint.toVector(); vertex.transform (transformationMatrix, {0, 0, 0}); - vertex += rotationPoint; + vertex += rotationPoint.toVector(); }
--- a/src/toolsets/movetoolset.cpp Fri Mar 23 19:30:53 2018 +0200 +++ b/src/toolsets/movetoolset.cpp Fri Mar 23 21:30:24 2018 +0200 @@ -76,43 +76,43 @@ m_window->updateGridToolBar(); } -void MoveToolset::moveObjects (Vertex vect) +void MoveToolset::moveObjects(QVector3D vector) { // Apply the grid values - vect *= grid()->coordinateSnap(); + vector *= grid()->coordinateSnap(); for (LDObject* obj : selectedObjects()) - obj->move (vect); + obj->move (vector); } void MoveToolset::moveXNeg() { - moveObjects ({-1, 0, 0}); + moveObjects({-1, 0, 0}); } void MoveToolset::moveYNeg() { - moveObjects ({0, -1, 0}); + moveObjects({0, -1, 0}); } void MoveToolset::moveZNeg() { - moveObjects ({0, 0, -1}); + moveObjects({0, 0, -1}); } void MoveToolset::moveXPos() { - moveObjects ({1, 0, 0}); + moveObjects({1, 0, 0}); } void MoveToolset::moveYPos() { - moveObjects ({0, 1, 0}); + moveObjects({0, 1, 0}); } void MoveToolset::moveZPos() { - moveObjects ({0, 0, 1}); + moveObjects({0, 0, 1}); } double MoveToolset::getRotateActionAngle() @@ -172,9 +172,9 @@ } Vertex custompoint = m_config->customRotationPoint(); - ui.customX->setValue(custompoint.x()); - ui.customY->setValue(custompoint.y()); - ui.customZ->setValue(custompoint.z()); + ui.customX->setValue(custompoint.x); + ui.customY->setValue(custompoint.y); + ui.customZ->setValue(custompoint.z); if (dialog->exec() == QDialog::Accepted) { @@ -187,10 +187,10 @@ else pointType = CustomPoint; - custompoint.setX (ui.customX->value()); - custompoint.setY (ui.customY->value()); - custompoint.setZ (ui.customZ->value()); - m_config->setRotationPointType((int) pointType); - m_config->setCustomRotationPoint (custompoint); + custompoint.x = ui.customX->value(); + custompoint.y = ui.customY->value(); + custompoint.z = ui.customZ->value(); + m_config->setRotationPointType(pointType); + m_config->setCustomRotationPoint(custompoint); } }
--- a/src/toolsets/movetoolset.h Fri Mar 23 19:30:53 2018 +0200 +++ b/src/toolsets/movetoolset.h Fri Mar 23 21:30:24 2018 +0200 @@ -47,7 +47,7 @@ Q_INVOKABLE void rotateZPos(); private: - void moveSelection (bool up); - void moveObjects (Vertex vect); + void moveSelection(bool up); + void moveObjects(QVector3D vect); double getRotateActionAngle(); };