# HG changeset patch # User Teemu Piippo # Date 1521282592 -7200 # Node ID adaad0b329e24537a596b1269ce3b989cdcca557 # Parent 52732b1db3f0fff2518986a086ea73d7b4b70650 Added a new tool for clamping vertices diff -r 52732b1db3f0 -r adaad0b329e2 CMakeLists.txt --- a/CMakeLists.txt Sat Mar 17 11:35:07 2018 +0200 +++ b/CMakeLists.txt Sat Mar 17 12:29:52 2018 +0200 @@ -182,6 +182,7 @@ src/dialogs/replacecoordinatesdialog.ui src/dialogs/rotationpointdialog.ui src/dialogs/ytruderdialog.ui + src/toolsets/fixroundingerrors.ui src/mainwindow.ui src/partdownloader.ui src/widgets/headeredit.ui diff -r 52732b1db3f0 -r adaad0b329e2 src/mainwindow.ui --- a/src/mainwindow.ui Sat Mar 17 11:35:07 2018 +0200 +++ b/src/mainwindow.ui Sat Mar 17 12:29:52 2018 +0200 @@ -327,6 +327,7 @@ + @@ -1734,6 +1735,11 @@ Polar grid + + + Fix rounding errors + + diff -r 52732b1db3f0 -r adaad0b329e2 src/toolsets/algorithmtoolset.cpp --- a/src/toolsets/algorithmtoolset.cpp Sat Mar 17 11:35:07 2018 +0200 +++ b/src/toolsets/algorithmtoolset.cpp Sat Mar 17 12:29:52 2018 +0200 @@ -20,6 +20,7 @@ #include #include #include +#include #include "../mainwindow.h" #include "../main.h" #include "../lddocument.h" @@ -39,6 +40,7 @@ #include "ui_replacecoordinatesdialog.h" #include "ui_editrawdialog.h" #include "ui_flipdialog.h" +#include "ui_fixroundingerrors.h" #include "algorithmtoolset.h" AlgorithmToolset::AlgorithmToolset (MainWindow* parent) : @@ -196,6 +198,93 @@ print (tr ("Rounded %1 values"), num); } +void AlgorithmToolset::fixRoundingErrors() +{ + QDialog dialog {m_window}; + Ui::FixRoundingErrors ui; + ui.setupUi(&dialog); + auto updateDialogButtonBox = [&]() + { + QPushButton* button = ui.buttonBox->button(QDialogButtonBox::Ok); + + if (button) + { + button->setEnabled( + ui.checkboxX->isChecked() + or ui.checkboxY->isChecked() + or ui.checkboxZ->isChecked() + ); + } + }; + updateDialogButtonBox(); + connect(ui.checkboxX, &QCheckBox::clicked, updateDialogButtonBox); + connect(ui.checkboxY, &QCheckBox::clicked, updateDialogButtonBox); + connect(ui.checkboxZ, &QCheckBox::clicked, updateDialogButtonBox); + const int result = dialog.exec(); + + if (result == QDialog::Accepted) + { + const Vertex referencePoint = { + ui.valueX->value(), + ui.valueY->value(), + ui.valueZ->value() + }; + + // Find out which axes to consider + QSet axes; + if (ui.checkboxX->isChecked()) + axes << X; + if (ui.checkboxY->isChecked()) + axes << Y; + if (ui.checkboxZ->isChecked()) + axes << Z; + + // Make a reference distance from the threshold value. + // If we're only comparing one dimension, this is the square of the threshold. + // If we're comparing multiple dimensions, the distance is multiplied to adjust. + double thresholdDistanceSquared = countof(axes) * std::pow(ui.threshold->value(), 2); + // Add some tiny leeway to fix rounding errors in the rounding error fixer. + thresholdDistanceSquared += 1e-10; + + auto fixVertex = [&](Vertex& vertex) + { + double distanceSquared = 0.0; + + for (Axis axis : axes) + distanceSquared += std::pow(vertex[axis] - referencePoint[axis], 2); + + if (distanceSquared < thresholdDistanceSquared) + { + // It's close enough, so clamp it + for (Axis axis : axes) + vertex.setCoordinate(axis, referencePoint[axis]); + } + }; + + for (const QModelIndex& index : m_window->selectedIndexes()) + { + LDObject* object = currentDocument()->lookup(index); + + if (object) + { + for (int i : range(0, 1, object->numVertices() - 1)) + { + Vertex point = object->vertex(i); + fixVertex(point); + object->setVertex(i, point); + } + if (object->type() == LDObjectType::SubfileReference) + { + LDSubfileReference* reference = static_cast(object); + Vertex point = reference->position(); + fixVertex(point); + reference->setPosition(point); + } + } + } + } +} + void AlgorithmToolset::replaceCoordinates() { QDialog dialog {m_window}; diff -r 52732b1db3f0 -r adaad0b329e2 src/toolsets/algorithmtoolset.h --- a/src/toolsets/algorithmtoolset.h Sat Mar 17 11:35:07 2018 +0200 +++ b/src/toolsets/algorithmtoolset.h Sat Mar 17 12:29:52 2018 +0200 @@ -28,6 +28,7 @@ Q_INVOKABLE void autocolor(); Q_INVOKABLE void demote(); Q_INVOKABLE void editRaw(); + Q_INVOKABLE void fixRoundingErrors(); Q_INVOKABLE void flip(); Q_INVOKABLE void makeBorders(); Q_INVOKABLE void replaceCoordinates(); diff -r 52732b1db3f0 -r adaad0b329e2 src/toolsets/fixroundingerrors.ui --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/toolsets/fixroundingerrors.ui Sat Mar 17 12:29:52 2018 +0200 @@ -0,0 +1,253 @@ + + + FixRoundingErrors + + + + 0 + 0 + 235 + 243 + + + + Fix rounding errors + + + + + + Co-ordinates + + + + + + X + + + + + + + = + + + valueX + + + + + + + false + + + 5 + + + 10000.000000000000000 + + + + + + + Y + + + + + + + = + + + valueY + + + + + + + false + + + 5 + + + 10000.000000000000000 + + + + + + + Z + + + + + + + = + + + valueZ + + + + + + + false + + + 5 + + + 10000.000000000000000 + + + + + + + + + + Threshold: + + + threshold + + + + + + + 5 + + + 0.000010000000000 + + + 10000.000000000000000 + + + 0.000100000000000 + + + 0.001000000000000 + + + + + + + Qt::Horizontal + + + QDialogButtonBox::Cancel|QDialogButtonBox::Ok + + + + + buttonBox + label_2 + groupBox + threshold + + + checkboxX + valueX + checkboxY + valueY + checkboxZ + valueZ + threshold + + + + + buttonBox + accepted() + FixRoundingErrors + accept() + + + 225 + 215 + + + 157 + 242 + + + + + buttonBox + rejected() + FixRoundingErrors + reject() + + + 225 + 221 + + + 234 + 242 + + + + + checkboxX + clicked(bool) + valueX + setEnabled(bool) + + + 49 + 53 + + + 93 + 47 + + + + + checkboxY + clicked(bool) + valueY + setEnabled(bool) + + + 60 + 94 + + + 132 + 91 + + + + + checkboxZ + clicked(bool) + valueZ + setEnabled(bool) + + + 61 + 129 + + + 107 + 133 + + + + +