18 |
18 |
19 #include "ldObjectMath.h" |
19 #include "ldObjectMath.h" |
20 #include "ldObject.h" |
20 #include "ldObject.h" |
21 #include "miscallenous.h" |
21 #include "miscallenous.h" |
22 |
22 |
23 // ============================================================================= |
23 |
24 // |
24 MathFunctions::MathFunctions(QObject* parent) : |
25 static void RotateVertex (Vertex& v, const Vertex& rotpoint, const Matrix& transformer) |
25 HierarchyElement(parent) {} |
|
26 |
|
27 |
|
28 void MathFunctions::rotateVertex(Vertex& vertex, const Vertex& rotationPoint, const Matrix& transformationMatrix) const |
26 { |
29 { |
27 v -= rotpoint; |
30 vertex -= rotationPoint; |
28 v.transform (transformer, Origin); |
31 vertex.transform (transformationMatrix, Origin); |
29 v += rotpoint; |
32 vertex += rotationPoint; |
30 } |
33 } |
31 |
34 |
32 // ============================================================================= |
35 |
33 // |
36 void MathFunctions::rotateObjects(int l, int m, int n, double angle, const LDObjectList& objects) const |
34 void RotateObjects (const int l, const int m, const int n, double angle, LDObjectList const& objects) |
|
35 { |
37 { |
36 QList<Vertex*> queue; |
38 Vertex rotationPoint = getRotationPoint (objects); |
37 const Vertex rotpoint = getRotationPoint (objects); |
39 double cosAngle = cos(angle); |
38 const double cosangle = cos (angle), |
40 double sinAngle = sin(angle); |
39 sinangle = sin (angle); |
|
40 |
41 |
41 // ref: http://en.wikipedia.org/wiki/Transformation_matrix#Rotation_2 |
42 // ref: http://en.wikipedia.org/wiki/Transformation_matrix#Rotation_2 |
42 Matrix transform ( |
43 Matrix transformationMatrix ( |
43 { |
44 { |
44 (l * l * (1 - cosangle)) + cosangle, |
45 (l * l * (1 - cosAngle)) + cosAngle, |
45 (m * l * (1 - cosangle)) - (n * sinangle), |
46 (m * l * (1 - cosAngle)) - (n * sinAngle), |
46 (n * l * (1 - cosangle)) + (m * sinangle), |
47 (n * l * (1 - cosAngle)) + (m * sinAngle), |
47 |
48 |
48 (l * m * (1 - cosangle)) + (n * sinangle), |
49 (l * m * (1 - cosAngle)) + (n * sinAngle), |
49 (m * m * (1 - cosangle)) + cosangle, |
50 (m * m * (1 - cosAngle)) + cosAngle, |
50 (n * m * (1 - cosangle)) - (l * sinangle), |
51 (n * m * (1 - cosAngle)) - (l * sinAngle), |
51 |
52 |
52 (l * n * (1 - cosangle)) - (m * sinangle), |
53 (l * n * (1 - cosAngle)) - (m * sinAngle), |
53 (m * n * (1 - cosangle)) + (l * sinangle), |
54 (m * n * (1 - cosAngle)) + (l * sinAngle), |
54 (n * n * (1 - cosangle)) + cosangle |
55 (n * n * (1 - cosAngle)) + cosAngle |
55 }); |
56 }); |
56 |
57 |
57 // Apply the above matrix to everything |
58 // Apply the above matrix to everything |
58 for (LDObject* obj : objects) |
59 for (LDObject* obj : objects) |
59 { |
60 { |
60 if (obj->numVertices()) |
61 if (obj->numVertices()) |
61 { |
62 { |
62 for (int i = 0; i < obj->numVertices(); ++i) |
63 for (int i = 0; i < obj->numVertices(); ++i) |
63 { |
64 { |
64 Vertex v = obj->vertex (i); |
65 Vertex v = obj->vertex (i); |
65 RotateVertex (v, rotpoint, transform); |
66 rotateVertex(v, rotationPoint, transformationMatrix); |
66 obj->setVertex (i, v); |
67 obj->setVertex (i, v); |
67 } |
68 } |
68 } |
69 } |
69 else if (obj->hasMatrix()) |
70 else if (obj->hasMatrix()) |
70 { |
71 { |
71 LDMatrixObject* mo = dynamic_cast<LDMatrixObject*> (obj); |
72 LDMatrixObject* mo = dynamic_cast<LDMatrixObject*> (obj); |
72 |
73 |
73 // Transform the position |
74 // Transform the position |
74 Vertex v = mo->position(); |
75 Vertex v = mo->position(); |
75 RotateVertex (v, rotpoint, transform); |
76 rotateVertex(v, rotationPoint, transformationMatrix); |
76 mo->setPosition (v); |
77 mo->setPosition (v); |
77 |
78 |
78 // Transform the matrix |
79 // Transform the matrix |
79 mo->setTransform (transform * mo->transform()); |
80 mo->setTransform(transformationMatrix * mo->transform()); |
80 } |
81 } |
81 } |
82 } |
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 } |