Sun, 05 Mar 2017 01:57:56 +0200
Polar grid is now togglable.
src/canvas.cpp | file | annotate | diff | comparison | revisions | |
src/configurationoptions.txt | file | annotate | diff | comparison | revisions | |
src/grid.cpp | file | annotate | diff | comparison | revisions | |
src/grid.h | file | annotate | diff | comparison | revisions | |
src/mainwindow.cpp | file | annotate | diff | comparison | revisions | |
src/mainwindow.ui | file | annotate | diff | comparison | revisions | |
src/toolsets/movetoolset.cpp | file | annotate | diff | comparison | revisions | |
src/toolsets/movetoolset.h | file | annotate | diff | comparison | revisions |
--- a/src/canvas.cpp Sun Mar 05 01:48:46 2017 +0200 +++ b/src/canvas.cpp Sun Mar 05 01:57:56 2017 +0200 @@ -95,8 +95,6 @@ */ void Canvas::drawFixedCameraBackdrop() { - static const enum { Cartesian, Polar } gridType = Polar; - // Find the top left corner of the grid Vertex topLeft = currentCamera().idealize(currentCamera().convert2dTo3d({0, 0})); Vertex bottomRight = currentCamera().idealize(currentCamera().convert2dTo3d({width(), height()})); @@ -104,105 +102,111 @@ glEnable(GL_LINE_STIPPLE); glBegin(GL_LINES); - if (gridType == 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)); - - static const auto prepareGridLine = [](qreal value) -> bool - { - if (not isZero(value)) - { - if (isZero(fmod(value, 10.0))) - glColor4f(0, 0, 0, 0.6); - else - glColor4f(0, 0, 0, 0.25); - - return true; - } - else - { - return false; - } - }; - - for (qreal x = x0; x < bottomRight.x(); x += gridSize) - { - if (prepareGridLine(x)) - { - glVertex(currentCamera().realize({x, -10000, 999})); - glVertex(currentCamera().realize({x, 10000, 999})); - } - } - - for (qreal y = y0; y < bottomRight.y(); y += gridSize) - { - if (prepareGridLine(y)) - { - glVertex(currentCamera().realize({-10000, y, 999})); - glVertex(currentCamera().realize({10000, y, 999})); - } - } - } - else + switch (grid()->type()) { - const QPointF pole = grid()->pole(); - 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()}; - qreal smallestRadius = distanceFromPointToRectangle(pole, QRectF{topLeft2d, bottomRight2d}); - qreal largestRadius = max(QLineF {topLeft2d, pole}.length(), - QLineF {bottomLeft2d, pole}.length(), - QLineF {bottomRight2d, pole}.length(), - QLineF {topRight2d, pole}.length()); + 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)); - // Snap the radii to the grid. - smallestRadius = round(smallestRadius / size) * size; - largestRadius = round(largestRadius / size) * size; - - // Is the pole at (0, 0)? If so, then don't render the polar axes above the real ones. - bool poleIsOrigin = isZero(pole.x()) and isZero(pole.y()); - glColor4f(0, 0, 0, 0.25); - - // Render the axes - for (int i = 0; i < grid()->polarDivisions() / 2; ++i) - { - qreal azimuth = (2.0 * pi) * i / grid()->polarDivisions(); + static const auto prepareGridLine = [](qreal value) -> bool + { + if (not isZero(value)) + { + if (isZero(fmod(value, 10.0))) + glColor4f(0, 0, 0, 0.6); + else + glColor4f(0, 0, 0, 0.25); - if (not poleIsOrigin or not isZero(fmod(azimuth, pi / 4))) - { - QPointF extremum = {cos(azimuth) * 10000, sin(azimuth) * 10000}; - QPointF A = pole + extremum; - QPointF B = pole - extremum; - glVertex(currentCamera().realize({A.x(), A.y(), 999})); - glVertex(currentCamera().realize({B.x(), B.y(), 999})); - } - } + return true; + } + else + { + return false; + } + }; - for (qreal radius = smallestRadius; radius <= largestRadius; radius += size) - { - if (not isZero(radius)) + for (qreal x = x0; x < bottomRight.x(); x += gridSize) { - Vertex points[48]; - - for (int i = 0; i < grid()->polarDivisions(); ++i) + if (prepareGridLine(x)) { - qreal azimuth = (2.0 * pi) * i / grid()->polarDivisions(); - QPointF point = pole + QPointF {radius * cos(azimuth), radius * sin(azimuth)}; - points[i] = currentCamera().realize({point.x(), point.y(), 999}); + glVertex(currentCamera().realize({x, -10000, 999})); + glVertex(currentCamera().realize({x, 10000, 999})); } + } - for (int i = 0; i < grid()->polarDivisions(); ++i) + for (qreal y = y0; y < bottomRight.y(); y += gridSize) + { + if (prepareGridLine(y)) { - glVertex(points[i]); - glVertex(ring(points, grid()->polarDivisions())[i + 1]); + glVertex(currentCamera().realize({-10000, y, 999})); + glVertex(currentCamera().realize({10000, y, 999})); } } } + break; + + case Grid::Polar: + { + const QPointF pole = grid()->pole(); + 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()}; + qreal smallestRadius = distanceFromPointToRectangle(pole, QRectF{topLeft2d, bottomRight2d}); + qreal largestRadius = max(QLineF {topLeft2d, pole}.length(), + QLineF {bottomLeft2d, pole}.length(), + QLineF {bottomRight2d, pole}.length(), + QLineF {topRight2d, pole}.length()); + + // Snap the radii to the grid. + smallestRadius = round(smallestRadius / size) * size; + largestRadius = round(largestRadius / size) * size; + + // Is the pole at (0, 0)? If so, then don't render the polar axes above the real ones. + bool poleIsOrigin = isZero(pole.x()) and isZero(pole.y()); + glColor4f(0, 0, 0, 0.25); + + // Render the axes + for (int i = 0; i < grid()->polarDivisions() / 2; ++i) + { + qreal azimuth = (2.0 * pi) * i / grid()->polarDivisions(); + + if (not poleIsOrigin or not isZero(fmod(azimuth, pi / 2))) + { + QPointF extremum = {cos(azimuth) * 10000, sin(azimuth) * 10000}; + QPointF A = pole + extremum; + QPointF B = pole - extremum; + glVertex(currentCamera().realize({A.x(), A.y(), 999})); + glVertex(currentCamera().realize({B.x(), B.y(), 999})); + } + } + + for (qreal radius = smallestRadius; radius <= largestRadius; radius += size) + { + if (not isZero(radius)) + { + Vertex points[48]; + + for (int i = 0; i < grid()->polarDivisions(); ++i) + { + qreal azimuth = (2.0 * pi) * i / grid()->polarDivisions(); + QPointF point = pole + QPointF {radius * cos(azimuth), radius * sin(azimuth)}; + points[i] = currentCamera().realize({point.x(), point.y(), 999}); + } + + for (int i = 0; i < grid()->polarDivisions(); ++i) + { + glVertex(points[i]); + glVertex(ring(points, grid()->polarDivisions())[i + 1]); + } + } + } + } + break; } glEnd();
--- a/src/configurationoptions.txt Sun Mar 05 01:48:46 2017 +0200 +++ b/src/configurationoptions.txt Sun Mar 05 01:57:56 2017 +0200 @@ -18,6 +18,7 @@ option GridFineCoordinateSnap = 0.1 option GridFineAngleSnap = 7.5 option GridFineBezierCurveSegments = 32 +option PolarGrid = false option RotationPointType = 0 option CustomRotationPoint = Vertex {} option ColorizeObjectsList = true
--- a/src/grid.cpp Sun Mar 05 01:48:46 2017 +0200 +++ b/src/grid.cpp Sun Mar 05 01:57:56 2017 +0200 @@ -67,22 +67,27 @@ QPointF Grid::snap(QPointF point) const { - if (false) + switch (type()) { - // For each co-ordinate, extract the amount of grid steps the value is away from zero, round that to remove the remainder, - // and multiply back by the the grid size. - double size = coordinateSnap(); - return {round(point.x() / size) * size, round(point.y() / size) * size}; - } - else - { - qreal radius = hypot(point.x() - pole().x(), point.y() - -pole().y()); - qreal azimuth = atan2(point.y() - -pole().y(), point.x() - pole().x()); - double size = coordinateSnap(); - double angleStep = 2 * pi / polarDivisions(); - radius = round(radius / size) * size; - azimuth = round(azimuth / angleStep) * angleStep; - return {pole().x() + cos(azimuth) * radius, -pole().y() + sin(azimuth) * radius}; + default: + case Cartesian: + { + // For each co-ordinate, extract the amount of grid steps the value is away from zero, round that to remove the remainder, + // and multiply back by the the grid size. + double size = coordinateSnap(); + return {round(point.x() / size) * size, round(point.y() / size) * size}; + } + + case Polar: + { + qreal radius = hypot(point.x() - pole().x(), point.y() - -pole().y()); + qreal azimuth = atan2(point.y() - -pole().y(), point.x() - pole().x()); + double size = coordinateSnap(); + double angleStep = 2 * pi / polarDivisions(); + radius = round(radius / size) * size; + azimuth = round(azimuth / angleStep) * angleStep; + return {pole().x() + cos(azimuth) * radius, -pole().y() + sin(azimuth) * radius}; + } } } @@ -91,7 +96,7 @@ */ QPointF Grid::pole() const { - return {12, -17}; + return {0, 0}; } /* @@ -110,3 +115,11 @@ return HighResolution; } } + +/* + * Returns whether to use a cartesian or polar grid. + */ +Grid::Type Grid::type() const +{ + return m_config->polarGrid() ? Polar : Cartesian; +}
--- a/src/grid.h Sun Mar 05 01:48:46 2017 +0200 +++ b/src/grid.h Sun Mar 05 01:57:56 2017 +0200 @@ -24,7 +24,7 @@ public: Grid(QObject* parent); - enum Type + enum Size { Coarse, Medium, @@ -37,6 +37,12 @@ Angle }; + enum Type + { + Cartesian, + Polar + }; + qreal angleSnap() const; qreal angleAsRadians() const; int bezierCurveSegments() const; @@ -44,5 +50,6 @@ QPointF pole() const; int polarDivisions() const; QPointF snap(QPointF point) const; + Type type() const; };
--- a/src/mainwindow.cpp Sun Mar 05 01:48:46 2017 +0200 +++ b/src/mainwindow.cpp Sun Mar 05 01:57:56 2017 +0200 @@ -276,6 +276,7 @@ ui.actionGridCoarse->setChecked (grid == Grid::Coarse); ui.actionGridMedium->setChecked (grid == Grid::Medium); ui.actionGridFine->setChecked (grid == Grid::Fine); + ui.actionPolarGrid->setChecked(m_grid->type() == Grid::Polar); emit gridChanged(); }
--- a/src/mainwindow.ui Sun Mar 05 01:48:46 2017 +0200 +++ b/src/mainwindow.ui Sun Mar 05 01:57:56 2017 +0200 @@ -47,8 +47,8 @@ <rect> <x>0</x> <y>0</y> - <width>465</width> - <height>429</height> + <width>467</width> + <height>420</height> </rect> </property> <attribute name="label"> @@ -75,8 +75,8 @@ <rect> <x>0</x> <y>0</y> - <width>465</width> - <height>429</height> + <width>467</width> + <height>420</height> </rect> </property> <attribute name="label"> @@ -157,8 +157,8 @@ <rect> <x>0</x> <y>0</y> - <width>465</width> - <height>429</height> + <width>467</width> + <height>420</height> </rect> </property> <attribute name="label"> @@ -194,7 +194,7 @@ <x>0</x> <y>0</y> <width>1010</width> - <height>25</height> + <height>26</height> </rect> </property> <widget class="QMenu" name="menuFile"> @@ -476,6 +476,7 @@ <addaction name="actionGridCoarse"/> <addaction name="actionGridMedium"/> <addaction name="actionGridFine"/> + <addaction name="actionPolarGrid"/> </widget> <widget class="QToolBar" name="toolBarDisplay"> <property name="windowTitle"> @@ -1715,6 +1716,14 @@ <string>Lighting</string> </property> </action> + <action name="actionPolarGrid"> + <property name="checkable"> + <bool>true</bool> + </property> + <property name="text"> + <string>Polar grid</string> + </property> + </action> </widget> <resources> <include location="../ldforge.qrc"/>
--- a/src/toolsets/movetoolset.cpp Sun Mar 05 01:48:46 2017 +0200 +++ b/src/toolsets/movetoolset.cpp Sun Mar 05 01:57:56 2017 +0200 @@ -94,6 +94,12 @@ m_window->updateGridToolBar(); } +void MoveToolset::polarGrid() +{ + m_config->togglePolarGrid(); + m_window->updateGridToolBar(); +} + void MoveToolset::moveObjects (Vertex vect) { // Apply the grid values
--- a/src/toolsets/movetoolset.h Sun Mar 05 01:48:46 2017 +0200 +++ b/src/toolsets/movetoolset.h Sun Mar 05 01:57:56 2017 +0200 @@ -38,6 +38,7 @@ Q_INVOKABLE void moveYPos(); Q_INVOKABLE void moveZNeg(); Q_INVOKABLE void moveZPos(); + Q_INVOKABLE void polarGrid(); Q_INVOKABLE void rotateXNeg(); Q_INVOKABLE void rotateXPos(); Q_INVOKABLE void rotateYNeg();