doxygen: add newline after \file
[blender.git] / source / blender / collada / TransformWriter.cpp
1 /*
2  * This program is free software; you can redistribute it and/or
3  * modify it under the terms of the GNU General Public License
4  * as published by the Free Software Foundation; either version 2
5  * of the License, or (at your option) any later version.
6  *
7  * This program is distributed in the hope that it will be useful,
8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
10  * GNU General Public License for more details.
11  *
12  * You should have received a copy of the GNU General Public License
13  * along with this program; if not, write to the Free Software Foundation,
14  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
15  */
16
17 /** \file
18  * \ingroup collada
19  */
20
21
22 #include "BLI_math.h"
23 #include "BLI_sys_types.h"
24
25 #include "BKE_object.h"
26
27 #include "TransformWriter.h"
28
29 void TransformWriter::add_node_transform(COLLADASW::Node& node, float mat[4][4], float parent_mat[4][4], bool limit_precision)
30 {
31         float loc[3], rot[3], scale[3];
32         float local[4][4];
33
34         if (parent_mat) {
35                 float invpar[4][4];
36                 invert_m4_m4(invpar, parent_mat);
37                 mul_m4_m4m4(local, invpar, mat);
38         }
39         else {
40                 copy_m4_m4(local, mat);
41         }
42
43         double dmat[4][4];
44         UnitConverter *converter = new UnitConverter();
45         converter->mat4_to_dae_double(dmat, local);
46         delete converter;
47
48         bc_decompose(local, loc, rot, NULL, scale);
49
50         if (node.getType() == COLLADASW::Node::JOINT) {
51                 // XXX Why are joints handled differently ?
52                 node.addMatrix("transform", dmat);
53         }
54         else {
55                 add_transform(node, loc, rot, scale);
56         }
57 }
58
59 void TransformWriter::add_node_transform_ob(
60         COLLADASW::Node& node,
61         Object *ob,
62         BC_export_transformation_type transformation_type,
63         bool limit_precision)
64 {
65 #if 0
66         float rot[3], loc[3], scale[3];
67
68         if (ob->parent) {
69                 float C[4][4], tmat[4][4], imat[4][4], mat[4][4];
70
71                 // factor out scale from obmat
72
73                 copy_v3_v3(scale, ob->size);
74
75                 ob->size[0] = ob->size[1] = ob->size[2] = 1.0f;
76                 BKE_object_to_mat4(ob, C);
77                 copy_v3_v3(ob->size, scale);
78
79                 mul_m4_series(tmat, ob->parent->obmat, ob->parentinv, C);
80
81                 // calculate local mat
82
83                 invert_m4_m4(imat, ob->parent->obmat);
84                 mul_m4_m4m4(mat, imat, tmat);
85
86                 // done
87
88                 mat4_to_eul(rot, mat);
89                 copy_v3_v3(loc, mat[3]);
90         }
91         else {
92                 copy_v3_v3(loc, ob->loc);
93                 copy_v3_v3(rot, ob->rot);
94                 copy_v3_v3(scale, ob->size);
95         }
96
97         add_transform(node, loc, rot, scale);
98 #endif
99
100         /* Export the local Matrix (relative to the object parent, be it an object, bone or vertex(-tices)) */
101         float  f_obmat[4][4];
102         BKE_object_matrix_local_get(ob, f_obmat);
103
104         switch (transformation_type) {
105                 case BC_TRANSFORMATION_TYPE_MATRIX:
106                 {
107                         UnitConverter converter;
108                         double d_obmat[4][4];
109                         converter.mat4_to_dae_double(d_obmat, f_obmat);
110                         if (limit_precision)
111                                 bc_sanitize_mat(d_obmat, LIMITTED_PRECISION);
112                         node.addMatrix("transform",d_obmat);
113                         break;
114                 }
115                 case BC_TRANSFORMATION_TYPE_TRANSROTLOC:
116                 {
117                         float loc[3], rot[3], scale[3];
118                         bc_decompose(f_obmat, loc, rot, NULL, scale);
119                         if (limit_precision) {
120                                 bc_sanitize_v3(loc, LIMITTED_PRECISION);
121                                 bc_sanitize_v3(rot, LIMITTED_PRECISION);
122                                 bc_sanitize_v3(scale, LIMITTED_PRECISION);
123                         }
124                         add_transform(node, loc, rot, scale);
125                         break;
126                 }
127         }
128
129 }
130
131 void TransformWriter::add_node_transform_identity(COLLADASW::Node& node)
132 {
133         float loc[3] = {0.0f, 0.0f, 0.0f}, scale[3] = {1.0f, 1.0f, 1.0f}, rot[3] = {0.0f, 0.0f, 0.0f};
134         add_transform(node, loc, rot, scale);
135 }
136
137 void TransformWriter::add_transform(COLLADASW::Node& node, float loc[3], float rot[3], float scale[3])
138 {
139 #if 0
140         node.addRotateZ("rotationZ", COLLADABU::Math::Utils::radToDegF(rot[2]));
141         node.addRotateY("rotationY", COLLADABU::Math::Utils::radToDegF(rot[1]));
142         node.addRotateX("rotationX", COLLADABU::Math::Utils::radToDegF(rot[0]));
143 #endif
144         node.addTranslate("location", loc[0], loc[1], loc[2]);
145         node.addRotateZ("rotationZ", RAD2DEGF(rot[2]));
146         node.addRotateY("rotationY", RAD2DEGF(rot[1]));
147         node.addRotateX("rotationX", RAD2DEGF(rot[0]));
148         node.addScale("scale", scale[0], scale[1], scale[2]);
149
150 }