src/mathfunctions.cpp

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

mercurial