# HG changeset patch # User Teemu Piippo # Date 1488671876 -7200 # Node ID 393babf1319d95d54a25a71e648ffcacc871bd6d # Parent caa20ed0272855bedacf07998689bb5108107ef1 Polar grid is now togglable. diff -r caa20ed02728 -r 393babf1319d src/canvas.cpp --- 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(); diff -r caa20ed02728 -r 393babf1319d src/configurationoptions.txt --- 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 diff -r caa20ed02728 -r 393babf1319d src/grid.cpp --- 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; +} diff -r caa20ed02728 -r 393babf1319d src/grid.h --- 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; }; diff -r caa20ed02728 -r 393babf1319d src/mainwindow.cpp --- 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(); } diff -r caa20ed02728 -r 393babf1319d src/mainwindow.ui --- 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 @@ 0 0 - 465 - 429 + 467 + 420 @@ -75,8 +75,8 @@ 0 0 - 465 - 429 + 467 + 420 @@ -157,8 +157,8 @@ 0 0 - 465 - 429 + 467 + 420 @@ -194,7 +194,7 @@ 0 0 1010 - 25 + 26 @@ -476,6 +476,7 @@ + @@ -1715,6 +1716,14 @@ Lighting + + + true + + + Polar grid + + diff -r caa20ed02728 -r 393babf1319d src/toolsets/movetoolset.cpp --- 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 diff -r caa20ed02728 -r 393babf1319d src/toolsets/movetoolset.h --- 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();