src/ldObjectMath.cpp

Tue, 07 Jul 2015 21:35:20 +0300

author
Teemu Piippo <crimsondusk64@gmail.com>
date
Tue, 07 Jul 2015 21:35:20 +0300
changeset 941
f895379d7fab
parent 928
0fef7bb7b7a2
permissions
-rw-r--r--

Refactoring update.
Removed all asserts.

869
7184f3bae695 - circle tool is now able to rotate the partial circular primitives spawned
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
1 /*
7184f3bae695 - circle tool is now able to rotate the partial circular primitives spawned
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
2 * LDForge: LDraw parts authoring CAD
7184f3bae695 - circle tool is now able to rotate the partial circular primitives spawned
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
3 * Copyright (C) 2013, 2014 Teemu Piippo
7184f3bae695 - circle tool is now able to rotate the partial circular primitives spawned
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
4 *
7184f3bae695 - circle tool is now able to rotate the partial circular primitives spawned
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
5 * This program is free software: you can redistribute it and/or modify
7184f3bae695 - circle tool is now able to rotate the partial circular primitives spawned
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
6 * it under the terms of the GNU General Public License as published by
7184f3bae695 - circle tool is now able to rotate the partial circular primitives spawned
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
7 * the Free Software Foundation, either version 3 of the License, or
7184f3bae695 - circle tool is now able to rotate the partial circular primitives spawned
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
8 * (at your option) any later version.
7184f3bae695 - circle tool is now able to rotate the partial circular primitives spawned
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
9 *
7184f3bae695 - circle tool is now able to rotate the partial circular primitives spawned
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
10 * This program is distributed in the hope that it will be useful,
7184f3bae695 - circle tool is now able to rotate the partial circular primitives spawned
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
7184f3bae695 - circle tool is now able to rotate the partial circular primitives spawned
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
7184f3bae695 - circle tool is now able to rotate the partial circular primitives spawned
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
13 * GNU General Public License for more details.
7184f3bae695 - circle tool is now able to rotate the partial circular primitives spawned
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
14 *
7184f3bae695 - circle tool is now able to rotate the partial circular primitives spawned
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
15 * You should have received a copy of the GNU General Public License
7184f3bae695 - circle tool is now able to rotate the partial circular primitives spawned
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
7184f3bae695 - circle tool is now able to rotate the partial circular primitives spawned
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
17 */
7184f3bae695 - circle tool is now able to rotate the partial circular primitives spawned
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
18
7184f3bae695 - circle tool is now able to rotate the partial circular primitives spawned
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
19 #include "ldObjectMath.h"
7184f3bae695 - circle tool is now able to rotate the partial circular primitives spawned
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
20 #include "ldObject.h"
7184f3bae695 - circle tool is now able to rotate the partial circular primitives spawned
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
21 #include "miscallenous.h"
7184f3bae695 - circle tool is now able to rotate the partial circular primitives spawned
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
22
7184f3bae695 - circle tool is now able to rotate the partial circular primitives spawned
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
23 // =============================================================================
7184f3bae695 - circle tool is now able to rotate the partial circular primitives spawned
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
24 //
941
f895379d7fab Refactoring update.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 928
diff changeset
25 static void rotateVertex (Vertex& v, const Vertex& rotpoint, const Matrix& transformer)
869
7184f3bae695 - circle tool is now able to rotate the partial circular primitives spawned
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
26 {
7184f3bae695 - circle tool is now able to rotate the partial circular primitives spawned
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
27 v -= rotpoint;
7184f3bae695 - circle tool is now able to rotate the partial circular primitives spawned
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
28 v.transform (transformer, Origin);
7184f3bae695 - circle tool is now able to rotate the partial circular primitives spawned
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
29 v += rotpoint;
7184f3bae695 - circle tool is now able to rotate the partial circular primitives spawned
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
30 }
7184f3bae695 - circle tool is now able to rotate the partial circular primitives spawned
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
31
7184f3bae695 - circle tool is now able to rotate the partial circular primitives spawned
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
32 // =============================================================================
7184f3bae695 - circle tool is now able to rotate the partial circular primitives spawned
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
33 //
941
f895379d7fab Refactoring update.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 928
diff changeset
34 void rotateObjects (const int l, const int m, const int n, double angle, const LDObjectList& objects)
869
7184f3bae695 - circle tool is now able to rotate the partial circular primitives spawned
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
35 {
7184f3bae695 - circle tool is now able to rotate the partial circular primitives spawned
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
36 const Vertex rotpoint = GetRotationPoint (objects);
941
f895379d7fab Refactoring update.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 928
diff changeset
37 const real cosangle = cos (angle);
f895379d7fab Refactoring update.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 928
diff changeset
38 const real sinangle = sin (angle);
869
7184f3bae695 - circle tool is now able to rotate the partial circular primitives spawned
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
39
7184f3bae695 - circle tool is now able to rotate the partial circular primitives spawned
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
40 // ref: http://en.wikipedia.org/wiki/Transformation_matrix#Rotation_2
7184f3bae695 - circle tool is now able to rotate the partial circular primitives spawned
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
41 Matrix transform (
7184f3bae695 - circle tool is now able to rotate the partial circular primitives spawned
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
42 {
941
f895379d7fab Refactoring update.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 928
diff changeset
43 (l * l * (1 - cosangle)) + cosangle,
f895379d7fab Refactoring update.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 928
diff changeset
44 (m * l * (1 - cosangle)) - (n* sinangle),
f895379d7fab Refactoring update.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 928
diff changeset
45 (n * l * (1 - cosangle)) + (m* sinangle),
869
7184f3bae695 - circle tool is now able to rotate the partial circular primitives spawned
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
46
941
f895379d7fab Refactoring update.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 928
diff changeset
47 (l * m * (1 - cosangle)) + (n* sinangle),
f895379d7fab Refactoring update.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 928
diff changeset
48 (m * m * (1 - cosangle)) + cosangle,
f895379d7fab Refactoring update.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 928
diff changeset
49 (n * m * (1 - cosangle)) - (l* sinangle),
869
7184f3bae695 - circle tool is now able to rotate the partial circular primitives spawned
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
50
941
f895379d7fab Refactoring update.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 928
diff changeset
51 (l * n * (1 - cosangle)) - (m* sinangle),
f895379d7fab Refactoring update.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 928
diff changeset
52 (m * n * (1 - cosangle)) + (l* sinangle),
f895379d7fab Refactoring update.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 928
diff changeset
53 (n * n * (1 - cosangle)) + cosangle
869
7184f3bae695 - circle tool is now able to rotate the partial circular primitives spawned
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
54 });
7184f3bae695 - circle tool is now able to rotate the partial circular primitives spawned
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
55
7184f3bae695 - circle tool is now able to rotate the partial circular primitives spawned
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
56 // Apply the above matrix to everything
7184f3bae695 - circle tool is now able to rotate the partial circular primitives spawned
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
57 for (LDObjectPtr obj : objects)
7184f3bae695 - circle tool is now able to rotate the partial circular primitives spawned
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
58 {
7184f3bae695 - circle tool is now able to rotate the partial circular primitives spawned
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
59 if (obj->numVertices())
7184f3bae695 - circle tool is now able to rotate the partial circular primitives spawned
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
60 {
7184f3bae695 - circle tool is now able to rotate the partial circular primitives spawned
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
61 for (int i = 0; i < obj->numVertices(); ++i)
7184f3bae695 - circle tool is now able to rotate the partial circular primitives spawned
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
62 {
7184f3bae695 - circle tool is now able to rotate the partial circular primitives spawned
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
63 Vertex v = obj->vertex (i);
941
f895379d7fab Refactoring update.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 928
diff changeset
64 rotateVertex (v, rotpoint, transform);
869
7184f3bae695 - circle tool is now able to rotate the partial circular primitives spawned
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
65 obj->setVertex (i, v);
7184f3bae695 - circle tool is now able to rotate the partial circular primitives spawned
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
66 }
7184f3bae695 - circle tool is now able to rotate the partial circular primitives spawned
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
67 }
941
f895379d7fab Refactoring update.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 928
diff changeset
68 else if (obj->hasMatrix())
869
7184f3bae695 - circle tool is now able to rotate the partial circular primitives spawned
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
69 {
7184f3bae695 - circle tool is now able to rotate the partial circular primitives spawned
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
70 LDMatrixObjectPtr mo = obj.dynamicCast<LDMatrixObject>();
7184f3bae695 - circle tool is now able to rotate the partial circular primitives spawned
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
71
7184f3bae695 - circle tool is now able to rotate the partial circular primitives spawned
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
72 // Transform the position
7184f3bae695 - circle tool is now able to rotate the partial circular primitives spawned
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
73 Vertex v = mo->position();
941
f895379d7fab Refactoring update.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 928
diff changeset
74 rotateVertex (v, rotpoint, transform);
869
7184f3bae695 - circle tool is now able to rotate the partial circular primitives spawned
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
75 mo->setPosition (v);
7184f3bae695 - circle tool is now able to rotate the partial circular primitives spawned
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
76
7184f3bae695 - circle tool is now able to rotate the partial circular primitives spawned
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
77 // Transform the matrix
7184f3bae695 - circle tool is now able to rotate the partial circular primitives spawned
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
78 mo->setTransform (transform * mo->transform());
7184f3bae695 - circle tool is now able to rotate the partial circular primitives spawned
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
79 }
7184f3bae695 - circle tool is now able to rotate the partial circular primitives spawned
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
80 }
7184f3bae695 - circle tool is now able to rotate the partial circular primitives spawned
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
81 }

mercurial