src/grid.cpp

Wed, 08 Mar 2017 22:09:48 +0200

author
Teemu Piippo <teemu@hecknology.net>
date
Wed, 08 Mar 2017 22:09:48 +0200
changeset 1205
4597f9fc8738
parent 1199
613a981223a6
permissions
-rw-r--r--

Whoops

1023
9450ac3cd930 Split grid stuff into a new class Grid in grid.cpp/grid.h
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
1 /*
9450ac3cd930 Split grid stuff into a new class Grid in grid.cpp/grid.h
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
2 * LDForge: LDraw parts authoring CAD
1072
9ce9496427f2 Happy new year 2017!
Teemu Piippo <teemu@hecknology.net>
parents: 1026
diff changeset
3 * Copyright (C) 2013 - 2017 Teemu Piippo
1023
9450ac3cd930 Split grid stuff into a new class Grid in grid.cpp/grid.h
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
4 *
9450ac3cd930 Split grid stuff into a new class Grid in grid.cpp/grid.h
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
5 * This program is free software: you can redistribute it and/or modify
9450ac3cd930 Split grid stuff into a new class Grid in grid.cpp/grid.h
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
6 * it under the terms of the GNU General Public License as published by
9450ac3cd930 Split grid stuff into a new class Grid in grid.cpp/grid.h
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
7 * the Free Software Foundation, either version 3 of the License, or
9450ac3cd930 Split grid stuff into a new class Grid in grid.cpp/grid.h
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
8 * (at your option) any later version.
9450ac3cd930 Split grid stuff into a new class Grid in grid.cpp/grid.h
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
9 *
9450ac3cd930 Split grid stuff into a new class Grid in grid.cpp/grid.h
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
10 * This program is distributed in the hope that it will be useful,
9450ac3cd930 Split grid stuff into a new class Grid in grid.cpp/grid.h
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9450ac3cd930 Split grid stuff into a new class Grid in grid.cpp/grid.h
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
9450ac3cd930 Split grid stuff into a new class Grid in grid.cpp/grid.h
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
13 * GNU General Public License for more details.
9450ac3cd930 Split grid stuff into a new class Grid in grid.cpp/grid.h
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
14 *
9450ac3cd930 Split grid stuff into a new class Grid in grid.cpp/grid.h
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
15 * You should have received a copy of the GNU General Public License
9450ac3cd930 Split grid stuff into a new class Grid in grid.cpp/grid.h
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
9450ac3cd930 Split grid stuff into a new class Grid in grid.cpp/grid.h
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
17 */
9450ac3cd930 Split grid stuff into a new class Grid in grid.cpp/grid.h
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
18
9450ac3cd930 Split grid stuff into a new class Grid in grid.cpp/grid.h
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
19 #include "grid.h"
1193
b78b6cf02213 The Grid class is no longer a hierarchy element or a QObject.
Teemu Piippo <teemu@hecknology.net>
parents: 1192
diff changeset
20 #include "linetypes/modelobject.h"
1023
9450ac3cd930 Split grid stuff into a new class Grid in grid.cpp/grid.h
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
21
1199
613a981223a6 Grid is now a namespace.
Teemu Piippo <teemu@hecknology.net>
parents: 1193
diff changeset
22 qreal Grid::coordinateSnap()
1023
9450ac3cd930 Split grid stuff into a new class Grid in grid.cpp/grid.h
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
23 {
1192
317f4ce38f3f Made configuration a global singleton. I think I can give in at least that much because it makes solving a lot of problems easier.
Teemu Piippo <teemu@hecknology.net>
parents: 1184
diff changeset
24 switch (configuration().grid())
1023
9450ac3cd930 Split grid stuff into a new class Grid in grid.cpp/grid.h
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
25 {
9450ac3cd930 Split grid stuff into a new class Grid in grid.cpp/grid.h
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
26 default:
1192
317f4ce38f3f Made configuration a global singleton. I think I can give in at least that much because it makes solving a lot of problems easier.
Teemu Piippo <teemu@hecknology.net>
parents: 1184
diff changeset
27 case Grid::Coarse: return configuration().gridCoarseCoordinateSnap();
317f4ce38f3f Made configuration a global singleton. I think I can give in at least that much because it makes solving a lot of problems easier.
Teemu Piippo <teemu@hecknology.net>
parents: 1184
diff changeset
28 case Grid::Medium: return configuration().gridMediumCoordinateSnap();
317f4ce38f3f Made configuration a global singleton. I think I can give in at least that much because it makes solving a lot of problems easier.
Teemu Piippo <teemu@hecknology.net>
parents: 1184
diff changeset
29 case Grid::Fine: return configuration().gridFineCoordinateSnap();
1023
9450ac3cd930 Split grid stuff into a new class Grid in grid.cpp/grid.h
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
30 }
9450ac3cd930 Split grid stuff into a new class Grid in grid.cpp/grid.h
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
31 }
9450ac3cd930 Split grid stuff into a new class Grid in grid.cpp/grid.h
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
32
9450ac3cd930 Split grid stuff into a new class Grid in grid.cpp/grid.h
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
33
1199
613a981223a6 Grid is now a namespace.
Teemu Piippo <teemu@hecknology.net>
parents: 1193
diff changeset
34 qreal Grid::angleSnap()
1023
9450ac3cd930 Split grid stuff into a new class Grid in grid.cpp/grid.h
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
35 {
1192
317f4ce38f3f Made configuration a global singleton. I think I can give in at least that much because it makes solving a lot of problems easier.
Teemu Piippo <teemu@hecknology.net>
parents: 1184
diff changeset
36 switch (configuration().grid())
1023
9450ac3cd930 Split grid stuff into a new class Grid in grid.cpp/grid.h
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
37 {
9450ac3cd930 Split grid stuff into a new class Grid in grid.cpp/grid.h
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
38 default:
1192
317f4ce38f3f Made configuration a global singleton. I think I can give in at least that much because it makes solving a lot of problems easier.
Teemu Piippo <teemu@hecknology.net>
parents: 1184
diff changeset
39 case Grid::Coarse: return configuration().gridCoarseAngleSnap();
317f4ce38f3f Made configuration a global singleton. I think I can give in at least that much because it makes solving a lot of problems easier.
Teemu Piippo <teemu@hecknology.net>
parents: 1184
diff changeset
40 case Grid::Medium: return configuration().gridMediumAngleSnap();
317f4ce38f3f Made configuration a global singleton. I think I can give in at least that much because it makes solving a lot of problems easier.
Teemu Piippo <teemu@hecknology.net>
parents: 1184
diff changeset
41 case Grid::Fine: return configuration().gridFineAngleSnap();
1023
9450ac3cd930 Split grid stuff into a new class Grid in grid.cpp/grid.h
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
42 }
9450ac3cd930 Split grid stuff into a new class Grid in grid.cpp/grid.h
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
43 }
9450ac3cd930 Split grid stuff into a new class Grid in grid.cpp/grid.h
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
44
9450ac3cd930 Split grid stuff into a new class Grid in grid.cpp/grid.h
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
45
1199
613a981223a6 Grid is now a namespace.
Teemu Piippo <teemu@hecknology.net>
parents: 1193
diff changeset
46 qreal Grid::angleAsRadians()
1023
9450ac3cd930 Split grid stuff into a new class Grid in grid.cpp/grid.h
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
47 {
1026
fb320996cce0 Rename Pi to lowercase pi
Teemu Piippo <crimsondusk64@gmail.com>
parents: 1023
diff changeset
48 return (pi * angleSnap()) / 180;
1023
9450ac3cd930 Split grid stuff into a new class Grid in grid.cpp/grid.h
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
49 }
9450ac3cd930 Split grid stuff into a new class Grid in grid.cpp/grid.h
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
50
9450ac3cd930 Split grid stuff into a new class Grid in grid.cpp/grid.h
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
51
1199
613a981223a6 Grid is now a namespace.
Teemu Piippo <teemu@hecknology.net>
parents: 1193
diff changeset
52 int Grid::bezierCurveSegments()
1023
9450ac3cd930 Split grid stuff into a new class Grid in grid.cpp/grid.h
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
53 {
1192
317f4ce38f3f Made configuration a global singleton. I think I can give in at least that much because it makes solving a lot of problems easier.
Teemu Piippo <teemu@hecknology.net>
parents: 1184
diff changeset
54 switch (configuration().grid())
1023
9450ac3cd930 Split grid stuff into a new class Grid in grid.cpp/grid.h
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
55 {
9450ac3cd930 Split grid stuff into a new class Grid in grid.cpp/grid.h
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
56 default:
1192
317f4ce38f3f Made configuration a global singleton. I think I can give in at least that much because it makes solving a lot of problems easier.
Teemu Piippo <teemu@hecknology.net>
parents: 1184
diff changeset
57 case Grid::Coarse: return configuration().gridCoarseBezierCurveSegments();
317f4ce38f3f Made configuration a global singleton. I think I can give in at least that much because it makes solving a lot of problems easier.
Teemu Piippo <teemu@hecknology.net>
parents: 1184
diff changeset
58 case Grid::Medium: return configuration().gridMediumBezierCurveSegments();
317f4ce38f3f Made configuration a global singleton. I think I can give in at least that much because it makes solving a lot of problems easier.
Teemu Piippo <teemu@hecknology.net>
parents: 1184
diff changeset
59 case Grid::Fine: return configuration().gridFineBezierCurveSegments();
1023
9450ac3cd930 Split grid stuff into a new class Grid in grid.cpp/grid.h
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
60 }
9450ac3cd930 Split grid stuff into a new class Grid in grid.cpp/grid.h
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
61 }
9450ac3cd930 Split grid stuff into a new class Grid in grid.cpp/grid.h
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
62
9450ac3cd930 Split grid stuff into a new class Grid in grid.cpp/grid.h
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
63
1199
613a981223a6 Grid is now a namespace.
Teemu Piippo <teemu@hecknology.net>
parents: 1193
diff changeset
64 QPointF Grid::snap(QPointF point)
1023
9450ac3cd930 Split grid stuff into a new class Grid in grid.cpp/grid.h
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
65 {
1184
393babf1319d Polar grid is now togglable.
Teemu Piippo <teemu@hecknology.net>
parents: 1182
diff changeset
66 switch (type())
1182
813d020f92d4 Added polar grid snapping code.
Teemu Piippo <teemu@hecknology.net>
parents: 1181
diff changeset
67 {
1184
393babf1319d Polar grid is now togglable.
Teemu Piippo <teemu@hecknology.net>
parents: 1182
diff changeset
68 default:
393babf1319d Polar grid is now togglable.
Teemu Piippo <teemu@hecknology.net>
parents: 1182
diff changeset
69 case Cartesian:
393babf1319d Polar grid is now togglable.
Teemu Piippo <teemu@hecknology.net>
parents: 1182
diff changeset
70 {
393babf1319d Polar grid is now togglable.
Teemu Piippo <teemu@hecknology.net>
parents: 1182
diff changeset
71 // For each co-ordinate, extract the amount of grid steps the value is away from zero, round that to remove the remainder,
393babf1319d Polar grid is now togglable.
Teemu Piippo <teemu@hecknology.net>
parents: 1182
diff changeset
72 // and multiply back by the the grid size.
393babf1319d Polar grid is now togglable.
Teemu Piippo <teemu@hecknology.net>
parents: 1182
diff changeset
73 double size = coordinateSnap();
393babf1319d Polar grid is now togglable.
Teemu Piippo <teemu@hecknology.net>
parents: 1182
diff changeset
74 return {round(point.x() / size) * size, round(point.y() / size) * size};
393babf1319d Polar grid is now togglable.
Teemu Piippo <teemu@hecknology.net>
parents: 1182
diff changeset
75 }
393babf1319d Polar grid is now togglable.
Teemu Piippo <teemu@hecknology.net>
parents: 1182
diff changeset
76
393babf1319d Polar grid is now togglable.
Teemu Piippo <teemu@hecknology.net>
parents: 1182
diff changeset
77 case Polar:
393babf1319d Polar grid is now togglable.
Teemu Piippo <teemu@hecknology.net>
parents: 1182
diff changeset
78 {
393babf1319d Polar grid is now togglable.
Teemu Piippo <teemu@hecknology.net>
parents: 1182
diff changeset
79 qreal radius = hypot(point.x() - pole().x(), point.y() - -pole().y());
393babf1319d Polar grid is now togglable.
Teemu Piippo <teemu@hecknology.net>
parents: 1182
diff changeset
80 qreal azimuth = atan2(point.y() - -pole().y(), point.x() - pole().x());
393babf1319d Polar grid is now togglable.
Teemu Piippo <teemu@hecknology.net>
parents: 1182
diff changeset
81 double size = coordinateSnap();
393babf1319d Polar grid is now togglable.
Teemu Piippo <teemu@hecknology.net>
parents: 1182
diff changeset
82 double angleStep = 2 * pi / polarDivisions();
393babf1319d Polar grid is now togglable.
Teemu Piippo <teemu@hecknology.net>
parents: 1182
diff changeset
83 radius = round(radius / size) * size;
393babf1319d Polar grid is now togglable.
Teemu Piippo <teemu@hecknology.net>
parents: 1182
diff changeset
84 azimuth = round(azimuth / angleStep) * angleStep;
393babf1319d Polar grid is now togglable.
Teemu Piippo <teemu@hecknology.net>
parents: 1182
diff changeset
85 return {pole().x() + cos(azimuth) * radius, -pole().y() + sin(azimuth) * radius};
393babf1319d Polar grid is now togglable.
Teemu Piippo <teemu@hecknology.net>
parents: 1182
diff changeset
86 }
1182
813d020f92d4 Added polar grid snapping code.
Teemu Piippo <teemu@hecknology.net>
parents: 1181
diff changeset
87 }
1023
9450ac3cd930 Split grid stuff into a new class Grid in grid.cpp/grid.h
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
88 }
1181
ca6d0ef9aadb Added polar grid rendering (which is disabled for now).
Teemu Piippo <teemu@hecknology.net>
parents: 1180
diff changeset
89
ca6d0ef9aadb Added polar grid rendering (which is disabled for now).
Teemu Piippo <teemu@hecknology.net>
parents: 1180
diff changeset
90 /*
ca6d0ef9aadb Added polar grid rendering (which is disabled for now).
Teemu Piippo <teemu@hecknology.net>
parents: 1180
diff changeset
91 * Returns the pole of the grid, in ideal X/Y co-ordinates. Z is left up for the caller to decide.
ca6d0ef9aadb Added polar grid rendering (which is disabled for now).
Teemu Piippo <teemu@hecknology.net>
parents: 1180
diff changeset
92 */
1199
613a981223a6 Grid is now a namespace.
Teemu Piippo <teemu@hecknology.net>
parents: 1193
diff changeset
93 QPointF Grid::pole()
1181
ca6d0ef9aadb Added polar grid rendering (which is disabled for now).
Teemu Piippo <teemu@hecknology.net>
parents: 1180
diff changeset
94 {
1184
393babf1319d Polar grid is now togglable.
Teemu Piippo <teemu@hecknology.net>
parents: 1182
diff changeset
95 return {0, 0};
1181
ca6d0ef9aadb Added polar grid rendering (which is disabled for now).
Teemu Piippo <teemu@hecknology.net>
parents: 1180
diff changeset
96 }
ca6d0ef9aadb Added polar grid rendering (which is disabled for now).
Teemu Piippo <teemu@hecknology.net>
parents: 1180
diff changeset
97
ca6d0ef9aadb Added polar grid rendering (which is disabled for now).
Teemu Piippo <teemu@hecknology.net>
parents: 1180
diff changeset
98 /*
ca6d0ef9aadb Added polar grid rendering (which is disabled for now).
Teemu Piippo <teemu@hecknology.net>
parents: 1180
diff changeset
99 * Returns the amount of divisions (slices) to be used in the polar grid.
ca6d0ef9aadb Added polar grid rendering (which is disabled for now).
Teemu Piippo <teemu@hecknology.net>
parents: 1180
diff changeset
100 */
1199
613a981223a6 Grid is now a namespace.
Teemu Piippo <teemu@hecknology.net>
parents: 1193
diff changeset
101 int Grid::polarDivisions()
1181
ca6d0ef9aadb Added polar grid rendering (which is disabled for now).
Teemu Piippo <teemu@hecknology.net>
parents: 1180
diff changeset
102 {
1192
317f4ce38f3f Made configuration a global singleton. I think I can give in at least that much because it makes solving a lot of problems easier.
Teemu Piippo <teemu@hecknology.net>
parents: 1184
diff changeset
103 switch (configuration().grid())
1181
ca6d0ef9aadb Added polar grid rendering (which is disabled for now).
Teemu Piippo <teemu@hecknology.net>
parents: 1180
diff changeset
104 {
ca6d0ef9aadb Added polar grid rendering (which is disabled for now).
Teemu Piippo <teemu@hecknology.net>
parents: 1180
diff changeset
105 default:
ca6d0ef9aadb Added polar grid rendering (which is disabled for now).
Teemu Piippo <teemu@hecknology.net>
parents: 1180
diff changeset
106 case Coarse:
ca6d0ef9aadb Added polar grid rendering (which is disabled for now).
Teemu Piippo <teemu@hecknology.net>
parents: 1180
diff changeset
107 case Medium:
ca6d0ef9aadb Added polar grid rendering (which is disabled for now).
Teemu Piippo <teemu@hecknology.net>
parents: 1180
diff changeset
108 return LowResolution;
ca6d0ef9aadb Added polar grid rendering (which is disabled for now).
Teemu Piippo <teemu@hecknology.net>
parents: 1180
diff changeset
109
ca6d0ef9aadb Added polar grid rendering (which is disabled for now).
Teemu Piippo <teemu@hecknology.net>
parents: 1180
diff changeset
110 case Fine:
ca6d0ef9aadb Added polar grid rendering (which is disabled for now).
Teemu Piippo <teemu@hecknology.net>
parents: 1180
diff changeset
111 return HighResolution;
ca6d0ef9aadb Added polar grid rendering (which is disabled for now).
Teemu Piippo <teemu@hecknology.net>
parents: 1180
diff changeset
112 }
ca6d0ef9aadb Added polar grid rendering (which is disabled for now).
Teemu Piippo <teemu@hecknology.net>
parents: 1180
diff changeset
113 }
1184
393babf1319d Polar grid is now togglable.
Teemu Piippo <teemu@hecknology.net>
parents: 1182
diff changeset
114
393babf1319d Polar grid is now togglable.
Teemu Piippo <teemu@hecknology.net>
parents: 1182
diff changeset
115 /*
393babf1319d Polar grid is now togglable.
Teemu Piippo <teemu@hecknology.net>
parents: 1182
diff changeset
116 * Returns whether to use a cartesian or polar grid.
393babf1319d Polar grid is now togglable.
Teemu Piippo <teemu@hecknology.net>
parents: 1182
diff changeset
117 */
1199
613a981223a6 Grid is now a namespace.
Teemu Piippo <teemu@hecknology.net>
parents: 1193
diff changeset
118 Grid::Type Grid::type()
1184
393babf1319d Polar grid is now togglable.
Teemu Piippo <teemu@hecknology.net>
parents: 1182
diff changeset
119 {
1192
317f4ce38f3f Made configuration a global singleton. I think I can give in at least that much because it makes solving a lot of problems easier.
Teemu Piippo <teemu@hecknology.net>
parents: 1184
diff changeset
120 return configuration().polarGrid() ? Polar : Cartesian;
1184
393babf1319d Polar grid is now togglable.
Teemu Piippo <teemu@hecknology.net>
parents: 1182
diff changeset
121 }

mercurial