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