src/algorithms/geometry.cpp

Tue, 03 Apr 2018 17:37:33 +0300

author
Teemu Piippo <teemu@hecknology.net>
date
Tue, 03 Apr 2018 17:37:33 +0300
changeset 1375
689121d162b6
parent 1328
d68d1ce89d05
child 1403
7a2d84112983
permissions
-rw-r--r--

refactor

1326
69a90bd2dba2 Happy new year 2018
Teemu Piippo <teemu@hecknology.net>
parents: 1325
diff changeset
1 /*
69a90bd2dba2 Happy new year 2018
Teemu Piippo <teemu@hecknology.net>
parents: 1325
diff changeset
2 * LDForge: LDraw parts authoring CAD
69a90bd2dba2 Happy new year 2018
Teemu Piippo <teemu@hecknology.net>
parents: 1325
diff changeset
3 * Copyright (C) 2013 - 2018 Teemu Piippo
69a90bd2dba2 Happy new year 2018
Teemu Piippo <teemu@hecknology.net>
parents: 1325
diff changeset
4 *
69a90bd2dba2 Happy new year 2018
Teemu Piippo <teemu@hecknology.net>
parents: 1325
diff changeset
5 * This program is free software: you can redistribute it and/or modify
69a90bd2dba2 Happy new year 2018
Teemu Piippo <teemu@hecknology.net>
parents: 1325
diff changeset
6 * it under the terms of the GNU General Public License as published by
69a90bd2dba2 Happy new year 2018
Teemu Piippo <teemu@hecknology.net>
parents: 1325
diff changeset
7 * the Free Software Foundation, either version 3 of the License, or
69a90bd2dba2 Happy new year 2018
Teemu Piippo <teemu@hecknology.net>
parents: 1325
diff changeset
8 * (at your option) any later version.
69a90bd2dba2 Happy new year 2018
Teemu Piippo <teemu@hecknology.net>
parents: 1325
diff changeset
9 *
69a90bd2dba2 Happy new year 2018
Teemu Piippo <teemu@hecknology.net>
parents: 1325
diff changeset
10 * This program is distributed in the hope that it will be useful,
69a90bd2dba2 Happy new year 2018
Teemu Piippo <teemu@hecknology.net>
parents: 1325
diff changeset
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
69a90bd2dba2 Happy new year 2018
Teemu Piippo <teemu@hecknology.net>
parents: 1325
diff changeset
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
69a90bd2dba2 Happy new year 2018
Teemu Piippo <teemu@hecknology.net>
parents: 1325
diff changeset
13 * GNU General Public License for more details.
69a90bd2dba2 Happy new year 2018
Teemu Piippo <teemu@hecknology.net>
parents: 1325
diff changeset
14 *
69a90bd2dba2 Happy new year 2018
Teemu Piippo <teemu@hecknology.net>
parents: 1325
diff changeset
15 * You should have received a copy of the GNU General Public License
69a90bd2dba2 Happy new year 2018
Teemu Piippo <teemu@hecknology.net>
parents: 1325
diff changeset
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
69a90bd2dba2 Happy new year 2018
Teemu Piippo <teemu@hecknology.net>
parents: 1325
diff changeset
17 */
69a90bd2dba2 Happy new year 2018
Teemu Piippo <teemu@hecknology.net>
parents: 1325
diff changeset
18
1319
39d7a9642eea reorganized headers
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
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
39d7a9642eea reorganized headers
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
22
39d7a9642eea reorganized headers
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
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
39d7a9642eea reorganized headers
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
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
39d7a9642eea reorganized headers
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
41 {
39d7a9642eea reorganized headers
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
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
39d7a9642eea reorganized headers
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
44 }
39d7a9642eea reorganized headers
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
45
39d7a9642eea reorganized headers
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
46 /*
39d7a9642eea reorganized headers
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
47 * makeCircle
39d7a9642eea reorganized headers
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
48 *
39d7a9642eea reorganized headers
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
49 * Creates a possibly partial circle rim.
39d7a9642eea reorganized headers
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
50 * Divisions is how many segments the circle makes if up if it's full.
39d7a9642eea reorganized headers
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
51 * Segments is now many segments are added.
39d7a9642eea reorganized headers
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
52 * Radius is the radius of the circle.
39d7a9642eea reorganized headers
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
53 *
39d7a9642eea reorganized headers
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
54 * If divisions == segments, this yields a full circle rim.
39d7a9642eea reorganized headers
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
55 * The rendered circle is returned as a vector of lines.
39d7a9642eea reorganized headers
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
56 */
39d7a9642eea reorganized headers
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
57 QVector<QLineF> makeCircle(int segments, int divisions, double radius)
39d7a9642eea reorganized headers
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
58 {
39d7a9642eea reorganized headers
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
59 QVector<QLineF> lines;
39d7a9642eea reorganized headers
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
60
39d7a9642eea reorganized headers
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
61 for (int i = 0; i < segments; i += 1)
39d7a9642eea reorganized headers
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
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
39d7a9642eea reorganized headers
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
65 lines.append({p0, p1});
39d7a9642eea reorganized headers
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
66 }
39d7a9642eea reorganized headers
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
67
39d7a9642eea reorganized headers
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
68 return lines;
39d7a9642eea reorganized headers
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
69 }
39d7a9642eea reorganized headers
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
70
1328
d68d1ce89d05 merged mathfunctions.cpp into algorithms/geometry.cpp
Teemu Piippo <teemu@hecknology.net>
parents: 1326
diff changeset
71
d68d1ce89d05 merged mathfunctions.cpp into algorithms/geometry.cpp
Teemu Piippo <teemu@hecknology.net>
parents: 1326
diff changeset
72 void rotateVertex(Vertex& vertex, const Vertex& rotationPoint, const Matrix& transformationMatrix)
d68d1ce89d05 merged mathfunctions.cpp into algorithms/geometry.cpp
Teemu Piippo <teemu@hecknology.net>
parents: 1326
diff changeset
73 {
d68d1ce89d05 merged mathfunctions.cpp into algorithms/geometry.cpp
Teemu Piippo <teemu@hecknology.net>
parents: 1326
diff changeset
74 vertex -= rotationPoint.toVector();
d68d1ce89d05 merged mathfunctions.cpp into algorithms/geometry.cpp
Teemu Piippo <teemu@hecknology.net>
parents: 1326
diff changeset
75 vertex.transform (transformationMatrix, {0, 0, 0});
d68d1ce89d05 merged mathfunctions.cpp into algorithms/geometry.cpp
Teemu Piippo <teemu@hecknology.net>
parents: 1326
diff changeset
76 vertex += rotationPoint.toVector();
d68d1ce89d05 merged mathfunctions.cpp into algorithms/geometry.cpp
Teemu Piippo <teemu@hecknology.net>
parents: 1326
diff changeset
77 }
d68d1ce89d05 merged mathfunctions.cpp into algorithms/geometry.cpp
Teemu Piippo <teemu@hecknology.net>
parents: 1326
diff changeset
78
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 void rotateObjects(int l, int m, int n, double angle, const QVector<LDObject*>& objects)
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 Vertex rotationPoint = getRotationPoint (objects);
d68d1ce89d05 merged mathfunctions.cpp into algorithms/geometry.cpp
Teemu Piippo <teemu@hecknology.net>
parents: 1326
diff changeset
83 double cosAngle = cos(angle);
d68d1ce89d05 merged mathfunctions.cpp into algorithms/geometry.cpp
Teemu Piippo <teemu@hecknology.net>
parents: 1326
diff changeset
84 double sinAngle = sin(angle);
d68d1ce89d05 merged mathfunctions.cpp into algorithms/geometry.cpp
Teemu Piippo <teemu@hecknology.net>
parents: 1326
diff changeset
85
d68d1ce89d05 merged mathfunctions.cpp into algorithms/geometry.cpp
Teemu Piippo <teemu@hecknology.net>
parents: 1326
diff changeset
86 // ref: http://en.wikipedia.org/wiki/Transformation_matrix#Rotation_2
d68d1ce89d05 merged mathfunctions.cpp into algorithms/geometry.cpp
Teemu Piippo <teemu@hecknology.net>
parents: 1326
diff changeset
87 Matrix transformationMatrix (
d68d1ce89d05 merged mathfunctions.cpp into algorithms/geometry.cpp
Teemu Piippo <teemu@hecknology.net>
parents: 1326
diff changeset
88 {
d68d1ce89d05 merged mathfunctions.cpp into algorithms/geometry.cpp
Teemu Piippo <teemu@hecknology.net>
parents: 1326
diff changeset
89 (l * l * (1 - cosAngle)) + cosAngle,
d68d1ce89d05 merged mathfunctions.cpp into algorithms/geometry.cpp
Teemu Piippo <teemu@hecknology.net>
parents: 1326
diff changeset
90 (m * l * (1 - cosAngle)) - (n * sinAngle),
d68d1ce89d05 merged mathfunctions.cpp into algorithms/geometry.cpp
Teemu Piippo <teemu@hecknology.net>
parents: 1326
diff changeset
91 (n * l * (1 - cosAngle)) + (m * sinAngle),
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 (l * m * (1 - cosAngle)) + (n * sinAngle),
d68d1ce89d05 merged mathfunctions.cpp into algorithms/geometry.cpp
Teemu Piippo <teemu@hecknology.net>
parents: 1326
diff changeset
94 (m * m * (1 - cosAngle)) + cosAngle,
d68d1ce89d05 merged mathfunctions.cpp into algorithms/geometry.cpp
Teemu Piippo <teemu@hecknology.net>
parents: 1326
diff changeset
95 (n * m * (1 - cosAngle)) - (l * sinAngle),
d68d1ce89d05 merged mathfunctions.cpp into algorithms/geometry.cpp
Teemu Piippo <teemu@hecknology.net>
parents: 1326
diff changeset
96
d68d1ce89d05 merged mathfunctions.cpp into algorithms/geometry.cpp
Teemu Piippo <teemu@hecknology.net>
parents: 1326
diff changeset
97 (l * n * (1 - cosAngle)) - (m * sinAngle),
d68d1ce89d05 merged mathfunctions.cpp into algorithms/geometry.cpp
Teemu Piippo <teemu@hecknology.net>
parents: 1326
diff changeset
98 (m * n * (1 - cosAngle)) + (l * sinAngle),
d68d1ce89d05 merged mathfunctions.cpp into algorithms/geometry.cpp
Teemu Piippo <teemu@hecknology.net>
parents: 1326
diff changeset
99 (n * n * (1 - cosAngle)) + cosAngle
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 // Apply the above matrix to everything
d68d1ce89d05 merged mathfunctions.cpp into algorithms/geometry.cpp
Teemu Piippo <teemu@hecknology.net>
parents: 1326
diff changeset
103 for (LDObject* obj : objects)
d68d1ce89d05 merged mathfunctions.cpp into algorithms/geometry.cpp
Teemu Piippo <teemu@hecknology.net>
parents: 1326
diff changeset
104 {
d68d1ce89d05 merged mathfunctions.cpp into algorithms/geometry.cpp
Teemu Piippo <teemu@hecknology.net>
parents: 1326
diff changeset
105 if (obj->numVertices())
d68d1ce89d05 merged mathfunctions.cpp into algorithms/geometry.cpp
Teemu Piippo <teemu@hecknology.net>
parents: 1326
diff changeset
106 {
d68d1ce89d05 merged mathfunctions.cpp into algorithms/geometry.cpp
Teemu Piippo <teemu@hecknology.net>
parents: 1326
diff changeset
107 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
108 {
d68d1ce89d05 merged mathfunctions.cpp into algorithms/geometry.cpp
Teemu Piippo <teemu@hecknology.net>
parents: 1326
diff changeset
109 Vertex v = obj->vertex (i);
d68d1ce89d05 merged mathfunctions.cpp into algorithms/geometry.cpp
Teemu Piippo <teemu@hecknology.net>
parents: 1326
diff changeset
110 rotateVertex(v, rotationPoint, transformationMatrix);
d68d1ce89d05 merged mathfunctions.cpp into algorithms/geometry.cpp
Teemu Piippo <teemu@hecknology.net>
parents: 1326
diff changeset
111 obj->setVertex (i, v);
d68d1ce89d05 merged mathfunctions.cpp into algorithms/geometry.cpp
Teemu Piippo <teemu@hecknology.net>
parents: 1326
diff changeset
112 }
d68d1ce89d05 merged mathfunctions.cpp into algorithms/geometry.cpp
Teemu Piippo <teemu@hecknology.net>
parents: 1326
diff changeset
113 }
d68d1ce89d05 merged mathfunctions.cpp into algorithms/geometry.cpp
Teemu Piippo <teemu@hecknology.net>
parents: 1326
diff changeset
114 else if (obj->hasMatrix())
d68d1ce89d05 merged mathfunctions.cpp into algorithms/geometry.cpp
Teemu Piippo <teemu@hecknology.net>
parents: 1326
diff changeset
115 {
d68d1ce89d05 merged mathfunctions.cpp into algorithms/geometry.cpp
Teemu Piippo <teemu@hecknology.net>
parents: 1326
diff changeset
116 LDMatrixObject* mo = dynamic_cast<LDMatrixObject*> (obj);
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 // Transform the position
d68d1ce89d05 merged mathfunctions.cpp into algorithms/geometry.cpp
Teemu Piippo <teemu@hecknology.net>
parents: 1326
diff changeset
119 Vertex v = mo->position();
d68d1ce89d05 merged mathfunctions.cpp into algorithms/geometry.cpp
Teemu Piippo <teemu@hecknology.net>
parents: 1326
diff changeset
120 rotateVertex(v, rotationPoint, transformationMatrix);
d68d1ce89d05 merged mathfunctions.cpp into algorithms/geometry.cpp
Teemu Piippo <teemu@hecknology.net>
parents: 1326
diff changeset
121 mo->setPosition (v);
d68d1ce89d05 merged mathfunctions.cpp into algorithms/geometry.cpp
Teemu Piippo <teemu@hecknology.net>
parents: 1326
diff changeset
122
d68d1ce89d05 merged mathfunctions.cpp into algorithms/geometry.cpp
Teemu Piippo <teemu@hecknology.net>
parents: 1326
diff changeset
123 // Transform the matrix
d68d1ce89d05 merged mathfunctions.cpp into algorithms/geometry.cpp
Teemu Piippo <teemu@hecknology.net>
parents: 1326
diff changeset
124 mo->setTransformationMatrix(transformationMatrix * mo->transformationMatrix());
d68d1ce89d05 merged mathfunctions.cpp into algorithms/geometry.cpp
Teemu Piippo <teemu@hecknology.net>
parents: 1326
diff changeset
125 }
d68d1ce89d05 merged mathfunctions.cpp into algorithms/geometry.cpp
Teemu Piippo <teemu@hecknology.net>
parents: 1326
diff changeset
126 }
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
d68d1ce89d05 merged mathfunctions.cpp into algorithms/geometry.cpp
Teemu Piippo <teemu@hecknology.net>
parents: 1326
diff changeset
130 Vertex getRotationPoint(const QVector<LDObject*>& objs)
d68d1ce89d05 merged mathfunctions.cpp into algorithms/geometry.cpp
Teemu Piippo <teemu@hecknology.net>
parents: 1326
diff changeset
131 {
d68d1ce89d05 merged mathfunctions.cpp into algorithms/geometry.cpp
Teemu Piippo <teemu@hecknology.net>
parents: 1326
diff changeset
132 switch (static_cast<RotationPoint>(config::rotationPointType()))
d68d1ce89d05 merged mathfunctions.cpp into algorithms/geometry.cpp
Teemu Piippo <teemu@hecknology.net>
parents: 1326
diff changeset
133 {
d68d1ce89d05 merged mathfunctions.cpp into algorithms/geometry.cpp
Teemu Piippo <teemu@hecknology.net>
parents: 1326
diff changeset
134 case ObjectOrigin:
d68d1ce89d05 merged mathfunctions.cpp into algorithms/geometry.cpp
Teemu Piippo <teemu@hecknology.net>
parents: 1326
diff changeset
135 {
d68d1ce89d05 merged mathfunctions.cpp into algorithms/geometry.cpp
Teemu Piippo <teemu@hecknology.net>
parents: 1326
diff changeset
136 BoundingBox box;
d68d1ce89d05 merged mathfunctions.cpp into algorithms/geometry.cpp
Teemu Piippo <teemu@hecknology.net>
parents: 1326
diff changeset
137
d68d1ce89d05 merged mathfunctions.cpp into algorithms/geometry.cpp
Teemu Piippo <teemu@hecknology.net>
parents: 1326
diff changeset
138 // Calculate center vertex
d68d1ce89d05 merged mathfunctions.cpp into algorithms/geometry.cpp
Teemu Piippo <teemu@hecknology.net>
parents: 1326
diff changeset
139 for (LDObject* obj : objs)
d68d1ce89d05 merged mathfunctions.cpp into algorithms/geometry.cpp
Teemu Piippo <teemu@hecknology.net>
parents: 1326
diff changeset
140 {
d68d1ce89d05 merged mathfunctions.cpp into algorithms/geometry.cpp
Teemu Piippo <teemu@hecknology.net>
parents: 1326
diff changeset
141 if (obj->hasMatrix())
d68d1ce89d05 merged mathfunctions.cpp into algorithms/geometry.cpp
Teemu Piippo <teemu@hecknology.net>
parents: 1326
diff changeset
142 {
d68d1ce89d05 merged mathfunctions.cpp into algorithms/geometry.cpp
Teemu Piippo <teemu@hecknology.net>
parents: 1326
diff changeset
143 box << static_cast<LDMatrixObject*> (obj)->position();
d68d1ce89d05 merged mathfunctions.cpp into algorithms/geometry.cpp
Teemu Piippo <teemu@hecknology.net>
parents: 1326
diff changeset
144 }
d68d1ce89d05 merged mathfunctions.cpp into algorithms/geometry.cpp
Teemu Piippo <teemu@hecknology.net>
parents: 1326
diff changeset
145 else
d68d1ce89d05 merged mathfunctions.cpp into algorithms/geometry.cpp
Teemu Piippo <teemu@hecknology.net>
parents: 1326
diff changeset
146 {
d68d1ce89d05 merged mathfunctions.cpp into algorithms/geometry.cpp
Teemu Piippo <teemu@hecknology.net>
parents: 1326
diff changeset
147 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
148 box << obj->vertex(i);
d68d1ce89d05 merged mathfunctions.cpp into algorithms/geometry.cpp
Teemu Piippo <teemu@hecknology.net>
parents: 1326
diff changeset
149 }
d68d1ce89d05 merged mathfunctions.cpp into algorithms/geometry.cpp
Teemu Piippo <teemu@hecknology.net>
parents: 1326
diff changeset
150 }
d68d1ce89d05 merged mathfunctions.cpp into algorithms/geometry.cpp
Teemu Piippo <teemu@hecknology.net>
parents: 1326
diff changeset
151
d68d1ce89d05 merged mathfunctions.cpp into algorithms/geometry.cpp
Teemu Piippo <teemu@hecknology.net>
parents: 1326
diff changeset
152 return box.center();
d68d1ce89d05 merged mathfunctions.cpp into algorithms/geometry.cpp
Teemu Piippo <teemu@hecknology.net>
parents: 1326
diff changeset
153 }
d68d1ce89d05 merged mathfunctions.cpp into algorithms/geometry.cpp
Teemu Piippo <teemu@hecknology.net>
parents: 1326
diff changeset
154
d68d1ce89d05 merged mathfunctions.cpp into algorithms/geometry.cpp
Teemu Piippo <teemu@hecknology.net>
parents: 1326
diff changeset
155 case WorldOrigin:
d68d1ce89d05 merged mathfunctions.cpp into algorithms/geometry.cpp
Teemu Piippo <teemu@hecknology.net>
parents: 1326
diff changeset
156 return Vertex();
d68d1ce89d05 merged mathfunctions.cpp into algorithms/geometry.cpp
Teemu Piippo <teemu@hecknology.net>
parents: 1326
diff changeset
157
d68d1ce89d05 merged mathfunctions.cpp into algorithms/geometry.cpp
Teemu Piippo <teemu@hecknology.net>
parents: 1326
diff changeset
158 case CustomPoint:
d68d1ce89d05 merged mathfunctions.cpp into algorithms/geometry.cpp
Teemu Piippo <teemu@hecknology.net>
parents: 1326
diff changeset
159 return config::customRotationPoint();
d68d1ce89d05 merged mathfunctions.cpp into algorithms/geometry.cpp
Teemu Piippo <teemu@hecknology.net>
parents: 1326
diff changeset
160 }
d68d1ce89d05 merged mathfunctions.cpp into algorithms/geometry.cpp
Teemu Piippo <teemu@hecknology.net>
parents: 1326
diff changeset
161
d68d1ce89d05 merged mathfunctions.cpp into algorithms/geometry.cpp
Teemu Piippo <teemu@hecknology.net>
parents: 1326
diff changeset
162 return Vertex();
d68d1ce89d05 merged mathfunctions.cpp into algorithms/geometry.cpp
Teemu Piippo <teemu@hecknology.net>
parents: 1326
diff changeset
163 }
d68d1ce89d05 merged mathfunctions.cpp into algorithms/geometry.cpp
Teemu Piippo <teemu@hecknology.net>
parents: 1326
diff changeset
164
1319
39d7a9642eea reorganized headers
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
165 /*
39d7a9642eea reorganized headers
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
166 * Computes the shortest distance from a point to a rectangle.
39d7a9642eea reorganized headers
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
167 *
39d7a9642eea reorganized headers
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
168 * The code originates from the Unity3D wiki, and was translated from C# to Qt by me (Teemu Piippo):
39d7a9642eea reorganized headers
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
169 *
39d7a9642eea reorganized headers
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
170 * Original code:
39d7a9642eea reorganized headers
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
171 * http://wiki.unity3d.com/index.php/Distance_from_a_point_to_a_rectangle
39d7a9642eea reorganized headers
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
172 *
39d7a9642eea reorganized headers
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
173 * Copyright 2013 Philip Peterson.
39d7a9642eea reorganized headers
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
174 *
39d7a9642eea reorganized headers
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
175 * Permission is hereby granted, free of charge, to any person obtaining a copy
39d7a9642eea reorganized headers
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
176 * of this software and associated documentation files (the "Software"), to
39d7a9642eea reorganized headers
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
177 * deal in the Software without restriction, including without limitation the
39d7a9642eea reorganized headers
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
178 * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
39d7a9642eea reorganized headers
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
179 * sell copies of the Software, and to permit persons to whom the Software is
39d7a9642eea reorganized headers
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
180 * furnished to do so, subject to the following conditions:
39d7a9642eea reorganized headers
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
181 *
39d7a9642eea reorganized headers
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
182 * The above copyright notice and this permission notice shall be included in
39d7a9642eea reorganized headers
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
183 * all copies or substantial portions of the Software.
39d7a9642eea reorganized headers
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
184 *
39d7a9642eea reorganized headers
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
185 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
39d7a9642eea reorganized headers
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
186 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
39d7a9642eea reorganized headers
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
187 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
39d7a9642eea reorganized headers
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
188 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
39d7a9642eea reorganized headers
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
189 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
39d7a9642eea reorganized headers
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
190 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
39d7a9642eea reorganized headers
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
191 * IN THE SOFTWARE.
39d7a9642eea reorganized headers
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
192 */
39d7a9642eea reorganized headers
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
193 qreal distanceFromPointToRectangle(const QPointF& point, const QRectF& rectangle)
39d7a9642eea reorganized headers
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
194 {
39d7a9642eea reorganized headers
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
195 // Calculate a distance between a point and a rectangle.
39d7a9642eea reorganized headers
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
196 // The area around/in the rectangle is defined in terms of
39d7a9642eea reorganized headers
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
197 // several regions:
39d7a9642eea reorganized headers
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
198 //
39d7a9642eea reorganized headers
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
199 // O--x
39d7a9642eea reorganized headers
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
200 // |
39d7a9642eea reorganized headers
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
201 // y
39d7a9642eea reorganized headers
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
202 //
39d7a9642eea reorganized headers
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
203 //
39d7a9642eea reorganized headers
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
204 // I | II | III
39d7a9642eea reorganized headers
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
205 // ======+==========+====== --yMin
39d7a9642eea reorganized headers
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
206 // VIII | IX (in) | IV
39d7a9642eea reorganized headers
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
207 // ======+==========+====== --yMax
39d7a9642eea reorganized headers
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
208 // VII | VI | V
39d7a9642eea reorganized headers
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
209 //
39d7a9642eea reorganized headers
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
210 //
39d7a9642eea reorganized headers
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
211 // Note that the +y direction is down because of Unity's GUI coordinates.
39d7a9642eea reorganized headers
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
212 // - I don't care which way is +y in this function because I want a distance --TP
39d7a9642eea reorganized headers
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
213
39d7a9642eea reorganized headers
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
214 if (point.x() < rectangle.left())
39d7a9642eea reorganized headers
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
215 {
39d7a9642eea reorganized headers
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
216 // Region I, VIII, or VII
39d7a9642eea reorganized headers
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
217 if (point.y() < rectangle.top()) // I
39d7a9642eea reorganized headers
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
218 return QLineF {point, rectangle.topLeft()}.length();
39d7a9642eea reorganized headers
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
219 else if (point.y() > rectangle.bottom()) // VII
39d7a9642eea reorganized headers
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
220 return QLineF {point, rectangle.bottomLeft()}.length();
39d7a9642eea reorganized headers
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
221 else // VIII
39d7a9642eea reorganized headers
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
222 return rectangle.left() - point.x();
39d7a9642eea reorganized headers
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
223 }
39d7a9642eea reorganized headers
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
224 else if (point.x() > rectangle.right())
39d7a9642eea reorganized headers
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
225 {
39d7a9642eea reorganized headers
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
226 // Region III, IV, or V
39d7a9642eea reorganized headers
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
227 if (point.y() < rectangle.top()) // III
39d7a9642eea reorganized headers
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
228 return QLineF {point, rectangle.topRight()}.length();
39d7a9642eea reorganized headers
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
229 else if (point.y() > rectangle.bottom()) // V
39d7a9642eea reorganized headers
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
230 return QLineF {point, rectangle.bottomRight()}.length();
39d7a9642eea reorganized headers
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
231 else // IV
39d7a9642eea reorganized headers
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
232 return point.x() - rectangle.right();
39d7a9642eea reorganized headers
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
233 }
39d7a9642eea reorganized headers
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
234 else
39d7a9642eea reorganized headers
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
235 {
39d7a9642eea reorganized headers
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
236 // Region II, IX, or VI
39d7a9642eea reorganized headers
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
237 if (point.y() < rectangle.top()) // II
39d7a9642eea reorganized headers
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
238 return rectangle.top() - point.y();
39d7a9642eea reorganized headers
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
239 else if (point.y() > rectangle.bottom()) // VI
39d7a9642eea reorganized headers
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
240 return point.y() - rectangle.bottom();
39d7a9642eea reorganized headers
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
241 else // IX
39d7a9642eea reorganized headers
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
242 return 0;
39d7a9642eea reorganized headers
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
243 }
39d7a9642eea reorganized headers
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
244 }

mercurial