Blender V4.5
collada_internal.cpp
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2010-2022 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
8
9#include "collada_internal.h"
10
11#include "BLI_math_matrix.h"
12#include "BLI_math_rotation.h"
13
14#include "BKE_armature.hh"
15
16#include "RNA_access.hh"
17
18#include <map>
19
20UnitConverter::UnitConverter() : up_axis(COLLADAFW::FileInfo::Z_UP)
21{
22 axis_angle_to_mat4_single(x_up_mat4, 'Y', -M_PI_2);
23 axis_angle_to_mat4_single(y_up_mat4, 'X', M_PI_2);
24
25 unit_m4(z_up_mat4);
26 unit_m4(scale_mat4);
27}
28
29void UnitConverter::read_asset(const COLLADAFW::FileInfo *asset)
30{
31 unit = asset->getUnit();
32 up_axis = asset->getUpAxisType();
33}
34
36{
37 switch (unit.getLinearUnitUnit()) {
38 case COLLADAFW::FileInfo::Unit::MILLIMETER:
39 case COLLADAFW::FileInfo::Unit::CENTIMETER:
40 case COLLADAFW::FileInfo::Unit::DECIMETER:
41 case COLLADAFW::FileInfo::Unit::METER:
42 case COLLADAFW::FileInfo::Unit::KILOMETER:
44 case COLLADAFW::FileInfo::Unit::INCH:
45 case COLLADAFW::FileInfo::Unit::FOOT:
46 case COLLADAFW::FileInfo::Unit::YARD:
48 default:
50 }
51}
52
54{
55 return float(unit.getLinearUnitMeter());
56}
57
58void UnitConverter::convertVector3(COLLADABU::Math::Vector3 &vec, float *v)
59{
60 v[0] = vec.x;
61 v[1] = vec.y;
62 v[2] = vec.z;
63}
64
65/* TODO: need also for angle conversion, time conversion... */
66
67void UnitConverter::dae_matrix_to_mat4_(float out[4][4], const COLLADABU::Math::Matrix4 &in)
68{
69 /* in DAE, matrices use columns vectors, (see comments in COLLADABUMathMatrix4.h)
70 * so here, to make a blender matrix, we swap columns and rows. */
71 for (int i = 0; i < 4; i++) {
72 for (int j = 0; j < 4; j++) {
73 out[i][j] = in[j][i];
74 }
75 }
76}
77
78void UnitConverter::mat4_to_dae(float out[4][4], float in[4][4])
79{
81}
82
83void UnitConverter::mat4_to_dae_double(double out[4][4], float in[4][4])
84{
85 float mat[4][4];
86
87 mat4_to_dae(mat, in);
88
89 for (int i = 0; i < 4; i++) {
90 for (int j = 0; j < 4; j++) {
91 out[i][j] = mat[i][j];
92 }
93 }
94}
95
97{
98 switch (up_axis) {
99 case COLLADAFW::FileInfo::X_UP:
100 return x_up_mat4;
101 break;
102 case COLLADAFW::FileInfo::Y_UP:
103 return y_up_mat4;
104 break;
105 default:
106 return z_up_mat4;
107 break;
108 }
109}
110
112{
113 return scale_mat4;
114}
115
117{
118 PointerRNA unit_settings;
119 PropertyRNA *system_ptr, *scale_ptr;
120 PointerRNA scene_ptr = RNA_id_pointer_create(&sce.id);
121
122 unit_settings = RNA_pointer_get(&scene_ptr, "unit_settings");
123 system_ptr = RNA_struct_find_property(&unit_settings, "system");
124 scale_ptr = RNA_struct_find_property(&unit_settings, "scale_length");
125
126 int type = RNA_property_enum_get(&unit_settings, system_ptr);
127
128 float bl_scale;
129
130 switch (type) {
131 case USER_UNIT_NONE:
132 bl_scale = 1.0; /* map 1 Blender unit to 1 Meter. */
133 break;
134
135 case USER_UNIT_METRIC:
136 bl_scale = RNA_property_float_get(&unit_settings, scale_ptr);
137 break;
138
139 default:
140 bl_scale = RNA_property_float_get(&unit_settings, scale_ptr);
141 /* It looks like the conversion to Imperial is done implicitly.
142 * So nothing to do here. */
143 break;
144 }
145
146 float rescale[3];
147 rescale[0] = rescale[1] = rescale[2] = getLinearMeter() / bl_scale;
148
149 size_to_mat4(scale_mat4, rescale);
150}
151
162
163 95, 95, 95, 95, 95, 95, 95, 95, 95, 95, 95, 95, 95, 95, 95, 95, 95, 95, 95,
164 95, 95, 95, 95, 95, 95, 95, 95, 95, 95, 95, 95, 95, 95, 95, 95, 95, 95, 95,
165 95, 95, 95, 95, 95, 95, 95, 95, 95, 95, 95, 95, 95, 95, 95, 95, 95, 95, 95,
166 95, 95, 95, 95, 95, 95, 95, 95, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75,
167 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 95, 95, 95, 95,
168 95, 95, 97, 98, 99, 100, 101, 102, 103, 104, 105, 106, 107, 108, 109, 110, 111, 112, 113,
169 114, 115, 116, 117, 118, 119, 120, 121, 122, 95, 95, 95, 95, 95,
170
171 128, 129, 130, 131, 132, 133, 134, 135, 136, 137, 138, 139, 140, 141, 142, 143, 144, 145, 146,
172 147, 148, 149, 150, 151, 152, 153, 154, 155, 156, 157, 158, 159, 160, 161, 162, 163, 164, 165,
173 166, 167, 168, 169, 170, 171, 172, 173, 174, 175, 176, 177, 178, 179, 180, 181, 182, 183, 184,
174 185, 186, 187, 188, 189, 190, 191, 192, 193, 194, 195, 196, 197, 198, 199, 200, 201, 202, 203,
175 204, 205, 206, 207, 208, 209, 210, 211, 212, 213, 214, 215, 216, 217, 218, 219, 220, 221, 222,
176 223, 224, 225, 226, 227, 228, 229, 230, 231, 232, 233, 234, 235, 236, 237, 238, 239, 240, 241,
177 242, 243, 244, 245, 246, 247, 248, 249, 250, 251, 252, 253, 254, 255,
178};
179
181
182 95, 95, 95, 95, 95, 95, 95, 95, 95, 95, 95, 95, 95, 95, 95, 95, 95, 95, 95,
183 95, 95, 95, 95, 95, 95, 95, 95, 95, 95, 95, 95, 95, 95, 95, 95, 95, 95, 95,
184 95, 95, 95, 95, 95, 95, 95, 45, 95, 95, 48, 49, 50, 51, 52, 53, 54, 55, 56,
185 57, 95, 95, 95, 95, 95, 95, 95, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75,
186 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 95, 95, 95, 95,
187 95, 95, 97, 98, 99, 100, 101, 102, 103, 104, 105, 106, 107, 108, 109, 110, 111, 112, 113,
188 114, 115, 116, 117, 118, 119, 120, 121, 122, 95, 95, 95, 95, 95,
189
190 128, 129, 130, 131, 132, 133, 134, 135, 136, 137, 138, 139, 140, 141, 142, 143, 144, 145, 146,
191 147, 148, 149, 150, 151, 152, 153, 154, 155, 156, 157, 158, 159, 160, 161, 162, 163, 164, 165,
192 166, 167, 168, 169, 170, 171, 172, 173, 174, 175, 176, 177, 178, 179, 180, 181, 182, 183, 184,
193 185, 186, 187, 188, 189, 190, 191, 192, 193, 194, 195, 196, 197, 198, 199, 200, 201, 202, 203,
194 204, 205, 206, 207, 208, 209, 210, 211, 212, 213, 214, 215, 216, 217, 218, 219, 220, 221, 222,
195 223, 224, 225, 226, 227, 228, 229, 230, 231, 232, 233, 234, 235, 236, 237, 238, 239, 240, 241,
196 242, 243, 244, 245, 246, 247, 248, 249, 250, 251, 252, 253, 254, 255,
197};
198
199using map_string_list = std::map<std::string, std::vector<std::string>>;
201
203{
204 global_id_map.clear();
205}
206
207std::string translate_id(const char *idString)
208{
209 std::string id = std::string(idString);
210 return translate_id(id);
211}
212
213std::string translate_id(const std::string &id)
214{
215 if (id.empty()) {
216 return id;
217 }
218
219 std::string id_translated = id;
220 id_translated[0] = translate_start_name_map[uint(id_translated[0])];
221 for (uint i = 1; i < id_translated.size(); i++) {
222 id_translated[i] = translate_name_map[uint(id_translated[i])];
223 }
224 /* It's so much workload now, the if () should speed up things. */
225 if (id_translated != id) {
226 /* Search duplicates. */
227 map_string_list::iterator iter = global_id_map.find(id_translated);
228 if (iter != global_id_map.end()) {
229 uint i = 0;
230 bool found = false;
231 for (i = 0; i < iter->second.size(); i++) {
232 if (id == iter->second[i]) {
233 found = true;
234 break;
235 }
236 }
237 bool convert = false;
238 if (found) {
239 if (i > 0) {
240 convert = true;
241 }
242 }
243 else {
244 convert = true;
245 global_id_map[id_translated].push_back(id);
246 }
247 if (convert) {
248 std::stringstream out;
249 out << ++i;
250 id_translated += out.str();
251 }
252 }
253 else {
254 global_id_map[id_translated].push_back(id);
255 }
256 }
257 return id_translated;
258}
259
260std::string id_name(void *id)
261{
262 return ((ID *)id)->name + 2;
263}
264
265std::string encode_xml(const std::string &xml)
266{
267 const std::map<char, std::string> escape{
268 {'<', "&lt;"}, {'>', "&gt;"}, {'"', "&quot;"}, {'\'', "&apos;"}, {'&', "&amp;"}};
269
270 std::map<char, std::string>::const_iterator it;
271 std::string encoded_xml;
272
273 for (char c : xml) {
274 it = escape.find(c);
275
276 if (it == escape.end()) {
277 encoded_xml += c;
278 }
279 else {
280 encoded_xml += it->second;
281 }
282 }
283 return encoded_xml;
284}
285
286std::string get_geometry_id(Object *ob)
287{
288 return translate_id(id_name(ob->data)) + "-mesh";
289}
290
291std::string get_geometry_id(Object *ob, bool use_instantiation)
292{
293 std::string geom_name = (use_instantiation) ? id_name(ob->data) : id_name(ob);
294
295 return translate_id(geom_name) + "-mesh";
296}
297
298std::string get_light_id(Object *ob)
299{
300 return translate_id(id_name(ob)) + "-light";
301}
302
303std::string get_joint_sid(Bone *bone)
304{
305 return translate_id(bone->name);
306}
307static std::string get_joint_sid(EditBone *bone)
308{
309 return translate_id(bone->name);
310}
311
312std::string get_camera_id(Object *ob)
313{
314 return translate_id(id_name(ob)) + "-camera";
315}
316
317std::string get_effect_id(Material *mat)
318{
319 return translate_id(id_name(mat)) + "-effect";
320}
321
322std::string get_material_id(Material *mat)
323{
324 return translate_id(id_name(mat)) + "-material";
325}
326
327std::string get_morph_id(Object *ob)
328{
329 return translate_id(id_name(ob)) + "-morph";
330}
#define M_PI_2
void transpose_m4_m4(float R[4][4], const float M[4][4])
void size_to_mat4(float R[4][4], const float size[3])
void unit_m4(float m[4][4])
void axis_angle_to_mat4_single(float R[4][4], char axis, float angle)
unsigned char uchar
unsigned int uint
@ USER_UNIT_METRIC
@ USER_UNIT_NONE
ATTR_WARN_UNUSED_RESULT const BMVert * v
void read_asset(const COLLADAFW::FileInfo *asset)
UnitConverter::UnitSystem isMetricSystem()
float(& get_scale())[4]
static void mat4_to_dae(float out[4][4], float in[4][4])
void convertVector3(COLLADABU::Math::Vector3 &vec, float *v)
void calculate_scale(Scene &sce)
static void mat4_to_dae_double(double out[4][4], float in[4][4])
float(& get_rotation())[4]
static void dae_matrix_to_mat4_(float out[4][4], const COLLADABU::Math::Matrix4 &in)
std::string get_camera_id(Object *ob)
std::string encode_xml(const std::string &xml)
std::string get_morph_id(Object *ob)
std::string get_joint_sid(Bone *bone)
std::string translate_id(const char *idString)
std::map< std::string, std::vector< std::string > > map_string_list
std::string get_geometry_id(Object *ob)
std::string get_material_id(Material *mat)
std::string get_light_id(Object *ob)
std::string get_effect_id(Material *mat)
void clear_global_id_map()
const uchar translate_name_map[256]
const uchar translate_start_name_map[256]
map_string_list global_id_map
std::string id_name(void *id)
#define in
#define out
float RNA_property_float_get(PointerRNA *ptr, PropertyRNA *prop)
PointerRNA RNA_pointer_get(PointerRNA *ptr, const char *name)
PropertyRNA * RNA_struct_find_property(PointerRNA *ptr, const char *identifier)
int RNA_property_enum_get(PointerRNA *ptr, PropertyRNA *prop)
PointerRNA RNA_id_pointer_create(ID *id)
char name[64]
char name[64]
Definition DNA_ID.h:404
i
Definition text_draw.cc:230