src/mathfunctions.cpp

changeset 1328
d68d1ce89d05
parent 1327
b179ab2f2c4e
child 1329
025578d6e491
equal deleted inserted replaced
1327:b179ab2f2c4e 1328:d68d1ce89d05
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
19 #include "mathfunctions.h"
20 #include "linetypes/modelobject.h"
21 #include "types/boundingbox.h"
22
23 MathFunctions::MathFunctions(QObject* parent) :
24 HierarchyElement(parent) {}
25
26
27 void MathFunctions::rotateVertex(Vertex& vertex, const Vertex& rotationPoint, const Matrix& transformationMatrix) const
28 {
29 vertex -= rotationPoint.toVector();
30 vertex.transform (transformationMatrix, {0, 0, 0});
31 vertex += rotationPoint.toVector();
32 }
33
34
35 void MathFunctions::rotateObjects(int l, int m, int n, double angle, const QVector<LDObject*>& objects) const
36 {
37 Vertex rotationPoint = getRotationPoint (objects);
38 double cosAngle = cos(angle);
39 double sinAngle = sin(angle);
40
41 // ref: http://en.wikipedia.org/wiki/Transformation_matrix#Rotation_2
42 Matrix transformationMatrix (
43 {
44 (l * l * (1 - cosAngle)) + cosAngle,
45 (m * l * (1 - cosAngle)) - (n * sinAngle),
46 (n * l * (1 - cosAngle)) + (m * sinAngle),
47
48 (l * m * (1 - cosAngle)) + (n * sinAngle),
49 (m * m * (1 - cosAngle)) + cosAngle,
50 (n * m * (1 - cosAngle)) - (l * sinAngle),
51
52 (l * n * (1 - cosAngle)) - (m * sinAngle),
53 (m * n * (1 - cosAngle)) + (l * sinAngle),
54 (n * n * (1 - cosAngle)) + cosAngle
55 });
56
57 // Apply the above matrix to everything
58 for (LDObject* obj : objects)
59 {
60 if (obj->numVertices())
61 {
62 for (int i = 0; i < obj->numVertices(); ++i)
63 {
64 Vertex v = obj->vertex (i);
65 rotateVertex(v, rotationPoint, transformationMatrix);
66 obj->setVertex (i, v);
67 }
68 }
69 else if (obj->hasMatrix())
70 {
71 LDMatrixObject* mo = dynamic_cast<LDMatrixObject*> (obj);
72
73 // Transform the position
74 Vertex v = mo->position();
75 rotateVertex(v, rotationPoint, transformationMatrix);
76 mo->setPosition (v);
77
78 // Transform the matrix
79 mo->setTransformationMatrix(transformationMatrix * mo->transformationMatrix());
80 }
81 }
82 }
83
84
85 Vertex MathFunctions::getRotationPoint(const QVector<LDObject*>& objs) const
86 {
87 switch (RotationPoint (config::rotationPointType()))
88 {
89 case ObjectOrigin:
90 {
91 BoundingBox box;
92
93 // Calculate center vertex
94 for (LDObject* obj : objs)
95 {
96 if (obj->hasMatrix())
97 {
98 box << static_cast<LDMatrixObject*> (obj)->position();
99 }
100 else
101 {
102 for (int i = 0; i < obj->numVertices(); ++i)
103 box << obj->vertex(i);
104 }
105 }
106
107 return box.center();
108 }
109
110 case WorldOrigin:
111 return Vertex();
112
113 case CustomPoint:
114 return config::customRotationPoint();
115 }
116
117 return Vertex();
118 }

mercurial