Sun, 17 Jun 2018 13:53:33 +0300
replaced the Matrix class with QMatrix4x4
1326 | 1 | /* |
2 | * LDForge: LDraw parts authoring CAD | |
3 | * Copyright (C) 2013 - 2018 Teemu Piippo | |
4 | * | |
5 | * This program is free software: you can redistribute it and/or modify | |
6 | * it under the terms of the GNU General Public License as published by | |
7 | * the Free Software Foundation, either version 3 of the License, or | |
8 | * (at your option) any later version. | |
9 | * | |
10 | * This program is distributed in the hope that it will be useful, | |
11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
13 | * GNU General Public License for more details. | |
14 | * | |
15 | * You should have received a copy of the GNU General Public License | |
16 | * along with this program. If not, see <http://www.gnu.org/licenses/>. | |
17 | */ | |
18 | ||
1319 | 19 | #include "geometry.h" |
1328
d68d1ce89d05
merged mathfunctions.cpp into algorithms/geometry.cpp
Teemu Piippo <teemu@hecknology.net>
parents:
1326
diff
changeset
|
20 | #include "../linetypes/modelobject.h" |
d68d1ce89d05
merged mathfunctions.cpp into algorithms/geometry.cpp
Teemu Piippo <teemu@hecknology.net>
parents:
1326
diff
changeset
|
21 | #include "../types/boundingbox.h" |
1319 | 22 | |
23 | /* | |
1325
f9abfc7ba676
Use 4 points of precision for circle point coordinates
Teemu Piippo <teemu@hecknology.net>
parents:
1319
diff
changeset
|
24 | * LDraw uses 4 points of precision for sin and cos values. Primitives must be generated |
f9abfc7ba676
Use 4 points of precision for circle point coordinates
Teemu Piippo <teemu@hecknology.net>
parents:
1319
diff
changeset
|
25 | * accordingly. |
1319 | 26 | */ |
1325
f9abfc7ba676
Use 4 points of precision for circle point coordinates
Teemu Piippo <teemu@hecknology.net>
parents:
1319
diff
changeset
|
27 | double ldrawsin(double angle) |
f9abfc7ba676
Use 4 points of precision for circle point coordinates
Teemu Piippo <teemu@hecknology.net>
parents:
1319
diff
changeset
|
28 | { |
f9abfc7ba676
Use 4 points of precision for circle point coordinates
Teemu Piippo <teemu@hecknology.net>
parents:
1319
diff
changeset
|
29 | return roundToDecimals(sin(angle), 4); |
f9abfc7ba676
Use 4 points of precision for circle point coordinates
Teemu Piippo <teemu@hecknology.net>
parents:
1319
diff
changeset
|
30 | } |
f9abfc7ba676
Use 4 points of precision for circle point coordinates
Teemu Piippo <teemu@hecknology.net>
parents:
1319
diff
changeset
|
31 | |
f9abfc7ba676
Use 4 points of precision for circle point coordinates
Teemu Piippo <teemu@hecknology.net>
parents:
1319
diff
changeset
|
32 | double ldrawcos(double angle) |
f9abfc7ba676
Use 4 points of precision for circle point coordinates
Teemu Piippo <teemu@hecknology.net>
parents:
1319
diff
changeset
|
33 | { |
f9abfc7ba676
Use 4 points of precision for circle point coordinates
Teemu Piippo <teemu@hecknology.net>
parents:
1319
diff
changeset
|
34 | return roundToDecimals(cos(angle), 4); |
f9abfc7ba676
Use 4 points of precision for circle point coordinates
Teemu Piippo <teemu@hecknology.net>
parents:
1319
diff
changeset
|
35 | } |
f9abfc7ba676
Use 4 points of precision for circle point coordinates
Teemu Piippo <teemu@hecknology.net>
parents:
1319
diff
changeset
|
36 | |
f9abfc7ba676
Use 4 points of precision for circle point coordinates
Teemu Piippo <teemu@hecknology.net>
parents:
1319
diff
changeset
|
37 | /* |
f9abfc7ba676
Use 4 points of precision for circle point coordinates
Teemu Piippo <teemu@hecknology.net>
parents:
1319
diff
changeset
|
38 | * Returns a point on a circumference. LDraw precision is used. |
f9abfc7ba676
Use 4 points of precision for circle point coordinates
Teemu Piippo <teemu@hecknology.net>
parents:
1319
diff
changeset
|
39 | */ |
f9abfc7ba676
Use 4 points of precision for circle point coordinates
Teemu Piippo <teemu@hecknology.net>
parents:
1319
diff
changeset
|
40 | QPointF pointOnLDrawCircumference(int segment, int divisions) |
1319 | 41 | { |
42 | double angle = (segment * 2 * pi) / divisions; | |
1325
f9abfc7ba676
Use 4 points of precision for circle point coordinates
Teemu Piippo <teemu@hecknology.net>
parents:
1319
diff
changeset
|
43 | return {ldrawcos(angle), ldrawsin(angle)}; |
1319 | 44 | } |
45 | ||
46 | /* | |
47 | * makeCircle | |
48 | * | |
49 | * Creates a possibly partial circle rim. | |
50 | * Divisions is how many segments the circle makes if up if it's full. | |
51 | * Segments is now many segments are added. | |
52 | * Radius is the radius of the circle. | |
53 | * | |
54 | * If divisions == segments, this yields a full circle rim. | |
55 | * The rendered circle is returned as a vector of lines. | |
56 | */ | |
57 | QVector<QLineF> makeCircle(int segments, int divisions, double radius) | |
58 | { | |
59 | QVector<QLineF> lines; | |
60 | ||
61 | for (int i = 0; i < segments; i += 1) | |
62 | { | |
1325
f9abfc7ba676
Use 4 points of precision for circle point coordinates
Teemu Piippo <teemu@hecknology.net>
parents:
1319
diff
changeset
|
63 | QPointF p0 = radius * ::pointOnLDrawCircumference(i, divisions); |
f9abfc7ba676
Use 4 points of precision for circle point coordinates
Teemu Piippo <teemu@hecknology.net>
parents:
1319
diff
changeset
|
64 | QPointF p1 = radius * ::pointOnLDrawCircumference(i + 1, divisions); |
1319 | 65 | lines.append({p0, p1}); |
66 | } | |
67 | ||
68 | return lines; | |
69 | } | |
70 | ||
1328
d68d1ce89d05
merged mathfunctions.cpp into algorithms/geometry.cpp
Teemu Piippo <teemu@hecknology.net>
parents:
1326
diff
changeset
|
71 | |
1403
7a2d84112983
replaced the Matrix class with QMatrix4x4
Teemu Piippo <teemu@hecknology.net>
parents:
1328
diff
changeset
|
72 | void rotateObjects(float l, float m, float n, double angle, const QVector<LDObject*>& objects) |
1328
d68d1ce89d05
merged mathfunctions.cpp into algorithms/geometry.cpp
Teemu Piippo <teemu@hecknology.net>
parents:
1326
diff
changeset
|
73 | { |
1403
7a2d84112983
replaced the Matrix class with QMatrix4x4
Teemu Piippo <teemu@hecknology.net>
parents:
1328
diff
changeset
|
74 | QVector3D rotationPoint = getRotationPoint (objects).toVector(); |
7a2d84112983
replaced the Matrix class with QMatrix4x4
Teemu Piippo <teemu@hecknology.net>
parents:
1328
diff
changeset
|
75 | QQuaternion orientation = QQuaternion::fromAxisAndAngle({l, m, n}, angle); |
1328
d68d1ce89d05
merged mathfunctions.cpp into algorithms/geometry.cpp
Teemu Piippo <teemu@hecknology.net>
parents:
1326
diff
changeset
|
76 | |
d68d1ce89d05
merged mathfunctions.cpp into algorithms/geometry.cpp
Teemu Piippo <teemu@hecknology.net>
parents:
1326
diff
changeset
|
77 | // Apply the above matrix to everything |
d68d1ce89d05
merged mathfunctions.cpp into algorithms/geometry.cpp
Teemu Piippo <teemu@hecknology.net>
parents:
1326
diff
changeset
|
78 | for (LDObject* obj : objects) |
d68d1ce89d05
merged mathfunctions.cpp into algorithms/geometry.cpp
Teemu Piippo <teemu@hecknology.net>
parents:
1326
diff
changeset
|
79 | { |
d68d1ce89d05
merged mathfunctions.cpp into algorithms/geometry.cpp
Teemu Piippo <teemu@hecknology.net>
parents:
1326
diff
changeset
|
80 | if (obj->numVertices()) |
d68d1ce89d05
merged mathfunctions.cpp into algorithms/geometry.cpp
Teemu Piippo <teemu@hecknology.net>
parents:
1326
diff
changeset
|
81 | { |
d68d1ce89d05
merged mathfunctions.cpp into algorithms/geometry.cpp
Teemu Piippo <teemu@hecknology.net>
parents:
1326
diff
changeset
|
82 | for (int i = 0; i < obj->numVertices(); ++i) |
d68d1ce89d05
merged mathfunctions.cpp into algorithms/geometry.cpp
Teemu Piippo <teemu@hecknology.net>
parents:
1326
diff
changeset
|
83 | { |
d68d1ce89d05
merged mathfunctions.cpp into algorithms/geometry.cpp
Teemu Piippo <teemu@hecknology.net>
parents:
1326
diff
changeset
|
84 | Vertex v = obj->vertex (i); |
1403
7a2d84112983
replaced the Matrix class with QMatrix4x4
Teemu Piippo <teemu@hecknology.net>
parents:
1328
diff
changeset
|
85 | v += rotationPoint; |
7a2d84112983
replaced the Matrix class with QMatrix4x4
Teemu Piippo <teemu@hecknology.net>
parents:
1328
diff
changeset
|
86 | v.rotate(orientation); |
7a2d84112983
replaced the Matrix class with QMatrix4x4
Teemu Piippo <teemu@hecknology.net>
parents:
1328
diff
changeset
|
87 | v -= rotationPoint; |
1328
d68d1ce89d05
merged mathfunctions.cpp into algorithms/geometry.cpp
Teemu Piippo <teemu@hecknology.net>
parents:
1326
diff
changeset
|
88 | obj->setVertex (i, v); |
d68d1ce89d05
merged mathfunctions.cpp into algorithms/geometry.cpp
Teemu Piippo <teemu@hecknology.net>
parents:
1326
diff
changeset
|
89 | } |
d68d1ce89d05
merged mathfunctions.cpp into algorithms/geometry.cpp
Teemu Piippo <teemu@hecknology.net>
parents:
1326
diff
changeset
|
90 | } |
d68d1ce89d05
merged mathfunctions.cpp into algorithms/geometry.cpp
Teemu Piippo <teemu@hecknology.net>
parents:
1326
diff
changeset
|
91 | else if (obj->hasMatrix()) |
d68d1ce89d05
merged mathfunctions.cpp into algorithms/geometry.cpp
Teemu Piippo <teemu@hecknology.net>
parents:
1326
diff
changeset
|
92 | { |
d68d1ce89d05
merged mathfunctions.cpp into algorithms/geometry.cpp
Teemu Piippo <teemu@hecknology.net>
parents:
1326
diff
changeset
|
93 | LDMatrixObject* mo = dynamic_cast<LDMatrixObject*> (obj); |
1403
7a2d84112983
replaced the Matrix class with QMatrix4x4
Teemu Piippo <teemu@hecknology.net>
parents:
1328
diff
changeset
|
94 | QMatrix4x4 matrix = mo->transformationMatrix(); |
7a2d84112983
replaced the Matrix class with QMatrix4x4
Teemu Piippo <teemu@hecknology.net>
parents:
1328
diff
changeset
|
95 | matrix.translate(rotationPoint); |
7a2d84112983
replaced the Matrix class with QMatrix4x4
Teemu Piippo <teemu@hecknology.net>
parents:
1328
diff
changeset
|
96 | matrix.rotate(orientation); |
7a2d84112983
replaced the Matrix class with QMatrix4x4
Teemu Piippo <teemu@hecknology.net>
parents:
1328
diff
changeset
|
97 | matrix.translate(-rotationPoint); |
7a2d84112983
replaced the Matrix class with QMatrix4x4
Teemu Piippo <teemu@hecknology.net>
parents:
1328
diff
changeset
|
98 | mo->setTransformationMatrix(matrix); |
1328
d68d1ce89d05
merged mathfunctions.cpp into algorithms/geometry.cpp
Teemu Piippo <teemu@hecknology.net>
parents:
1326
diff
changeset
|
99 | } |
d68d1ce89d05
merged mathfunctions.cpp into algorithms/geometry.cpp
Teemu Piippo <teemu@hecknology.net>
parents:
1326
diff
changeset
|
100 | } |
d68d1ce89d05
merged mathfunctions.cpp into algorithms/geometry.cpp
Teemu Piippo <teemu@hecknology.net>
parents:
1326
diff
changeset
|
101 | } |
d68d1ce89d05
merged mathfunctions.cpp into algorithms/geometry.cpp
Teemu Piippo <teemu@hecknology.net>
parents:
1326
diff
changeset
|
102 | |
d68d1ce89d05
merged mathfunctions.cpp into algorithms/geometry.cpp
Teemu Piippo <teemu@hecknology.net>
parents:
1326
diff
changeset
|
103 | |
d68d1ce89d05
merged mathfunctions.cpp into algorithms/geometry.cpp
Teemu Piippo <teemu@hecknology.net>
parents:
1326
diff
changeset
|
104 | Vertex getRotationPoint(const QVector<LDObject*>& objs) |
d68d1ce89d05
merged mathfunctions.cpp into algorithms/geometry.cpp
Teemu Piippo <teemu@hecknology.net>
parents:
1326
diff
changeset
|
105 | { |
d68d1ce89d05
merged mathfunctions.cpp into algorithms/geometry.cpp
Teemu Piippo <teemu@hecknology.net>
parents:
1326
diff
changeset
|
106 | switch (static_cast<RotationPoint>(config::rotationPointType())) |
d68d1ce89d05
merged mathfunctions.cpp into algorithms/geometry.cpp
Teemu Piippo <teemu@hecknology.net>
parents:
1326
diff
changeset
|
107 | { |
d68d1ce89d05
merged mathfunctions.cpp into algorithms/geometry.cpp
Teemu Piippo <teemu@hecknology.net>
parents:
1326
diff
changeset
|
108 | case ObjectOrigin: |
d68d1ce89d05
merged mathfunctions.cpp into algorithms/geometry.cpp
Teemu Piippo <teemu@hecknology.net>
parents:
1326
diff
changeset
|
109 | { |
d68d1ce89d05
merged mathfunctions.cpp into algorithms/geometry.cpp
Teemu Piippo <teemu@hecknology.net>
parents:
1326
diff
changeset
|
110 | BoundingBox box; |
d68d1ce89d05
merged mathfunctions.cpp into algorithms/geometry.cpp
Teemu Piippo <teemu@hecknology.net>
parents:
1326
diff
changeset
|
111 | |
d68d1ce89d05
merged mathfunctions.cpp into algorithms/geometry.cpp
Teemu Piippo <teemu@hecknology.net>
parents:
1326
diff
changeset
|
112 | // Calculate center vertex |
d68d1ce89d05
merged mathfunctions.cpp into algorithms/geometry.cpp
Teemu Piippo <teemu@hecknology.net>
parents:
1326
diff
changeset
|
113 | for (LDObject* obj : objs) |
d68d1ce89d05
merged mathfunctions.cpp into algorithms/geometry.cpp
Teemu Piippo <teemu@hecknology.net>
parents:
1326
diff
changeset
|
114 | { |
1403
7a2d84112983
replaced the Matrix class with QMatrix4x4
Teemu Piippo <teemu@hecknology.net>
parents:
1328
diff
changeset
|
115 | for (int i = 0; i < obj->numVertices(); ++i) |
7a2d84112983
replaced the Matrix class with QMatrix4x4
Teemu Piippo <teemu@hecknology.net>
parents:
1328
diff
changeset
|
116 | box << obj->vertex(i); |
1328
d68d1ce89d05
merged mathfunctions.cpp into algorithms/geometry.cpp
Teemu Piippo <teemu@hecknology.net>
parents:
1326
diff
changeset
|
117 | } |
d68d1ce89d05
merged mathfunctions.cpp into algorithms/geometry.cpp
Teemu Piippo <teemu@hecknology.net>
parents:
1326
diff
changeset
|
118 | |
d68d1ce89d05
merged mathfunctions.cpp into algorithms/geometry.cpp
Teemu Piippo <teemu@hecknology.net>
parents:
1326
diff
changeset
|
119 | return box.center(); |
d68d1ce89d05
merged mathfunctions.cpp into algorithms/geometry.cpp
Teemu Piippo <teemu@hecknology.net>
parents:
1326
diff
changeset
|
120 | } |
d68d1ce89d05
merged mathfunctions.cpp into algorithms/geometry.cpp
Teemu Piippo <teemu@hecknology.net>
parents:
1326
diff
changeset
|
121 | |
d68d1ce89d05
merged mathfunctions.cpp into algorithms/geometry.cpp
Teemu Piippo <teemu@hecknology.net>
parents:
1326
diff
changeset
|
122 | case WorldOrigin: |
d68d1ce89d05
merged mathfunctions.cpp into algorithms/geometry.cpp
Teemu Piippo <teemu@hecknology.net>
parents:
1326
diff
changeset
|
123 | return Vertex(); |
d68d1ce89d05
merged mathfunctions.cpp into algorithms/geometry.cpp
Teemu Piippo <teemu@hecknology.net>
parents:
1326
diff
changeset
|
124 | |
d68d1ce89d05
merged mathfunctions.cpp into algorithms/geometry.cpp
Teemu Piippo <teemu@hecknology.net>
parents:
1326
diff
changeset
|
125 | case CustomPoint: |
d68d1ce89d05
merged mathfunctions.cpp into algorithms/geometry.cpp
Teemu Piippo <teemu@hecknology.net>
parents:
1326
diff
changeset
|
126 | return config::customRotationPoint(); |
d68d1ce89d05
merged mathfunctions.cpp into algorithms/geometry.cpp
Teemu Piippo <teemu@hecknology.net>
parents:
1326
diff
changeset
|
127 | } |
d68d1ce89d05
merged mathfunctions.cpp into algorithms/geometry.cpp
Teemu Piippo <teemu@hecknology.net>
parents:
1326
diff
changeset
|
128 | |
d68d1ce89d05
merged mathfunctions.cpp into algorithms/geometry.cpp
Teemu Piippo <teemu@hecknology.net>
parents:
1326
diff
changeset
|
129 | return Vertex(); |
d68d1ce89d05
merged mathfunctions.cpp into algorithms/geometry.cpp
Teemu Piippo <teemu@hecknology.net>
parents:
1326
diff
changeset
|
130 | } |
d68d1ce89d05
merged mathfunctions.cpp into algorithms/geometry.cpp
Teemu Piippo <teemu@hecknology.net>
parents:
1326
diff
changeset
|
131 | |
1319 | 132 | /* |
133 | * Computes the shortest distance from a point to a rectangle. | |
134 | * | |
135 | * The code originates from the Unity3D wiki, and was translated from C# to Qt by me (Teemu Piippo): | |
136 | * | |
137 | * Original code: | |
138 | * http://wiki.unity3d.com/index.php/Distance_from_a_point_to_a_rectangle | |
139 | * | |
140 | * Copyright 2013 Philip Peterson. | |
141 | * | |
142 | * Permission is hereby granted, free of charge, to any person obtaining a copy | |
143 | * of this software and associated documentation files (the "Software"), to | |
144 | * deal in the Software without restriction, including without limitation the | |
145 | * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or | |
146 | * sell copies of the Software, and to permit persons to whom the Software is | |
147 | * furnished to do so, subject to the following conditions: | |
148 | * | |
149 | * The above copyright notice and this permission notice shall be included in | |
150 | * all copies or substantial portions of the Software. | |
151 | * | |
152 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
153 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
154 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | |
155 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
156 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING | |
157 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS | |
158 | * IN THE SOFTWARE. | |
159 | */ | |
160 | qreal distanceFromPointToRectangle(const QPointF& point, const QRectF& rectangle) | |
161 | { | |
162 | // Calculate a distance between a point and a rectangle. | |
163 | // The area around/in the rectangle is defined in terms of | |
164 | // several regions: | |
165 | // | |
166 | // O--x | |
167 | // | | |
168 | // y | |
169 | // | |
170 | // | |
171 | // I | II | III | |
172 | // ======+==========+====== --yMin | |
173 | // VIII | IX (in) | IV | |
174 | // ======+==========+====== --yMax | |
175 | // VII | VI | V | |
176 | // | |
177 | // | |
178 | // Note that the +y direction is down because of Unity's GUI coordinates. | |
179 | // - I don't care which way is +y in this function because I want a distance --TP | |
180 | ||
181 | if (point.x() < rectangle.left()) | |
182 | { | |
183 | // Region I, VIII, or VII | |
184 | if (point.y() < rectangle.top()) // I | |
185 | return QLineF {point, rectangle.topLeft()}.length(); | |
186 | else if (point.y() > rectangle.bottom()) // VII | |
187 | return QLineF {point, rectangle.bottomLeft()}.length(); | |
188 | else // VIII | |
189 | return rectangle.left() - point.x(); | |
190 | } | |
191 | else if (point.x() > rectangle.right()) | |
192 | { | |
193 | // Region III, IV, or V | |
194 | if (point.y() < rectangle.top()) // III | |
195 | return QLineF {point, rectangle.topRight()}.length(); | |
196 | else if (point.y() > rectangle.bottom()) // V | |
197 | return QLineF {point, rectangle.bottomRight()}.length(); | |
198 | else // IV | |
199 | return point.x() - rectangle.right(); | |
200 | } | |
201 | else | |
202 | { | |
203 | // Region II, IX, or VI | |
204 | if (point.y() < rectangle.top()) // II | |
205 | return rectangle.top() - point.y(); | |
206 | else if (point.y() > rectangle.bottom()) // VI | |
207 | return point.y() - rectangle.bottom(); | |
208 | else // IX | |
209 | return 0; | |
210 | } | |
211 | } |