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 #pragma once
22 
23 #include "COLLADAFWColorOrTexture.h"
24 #include "COLLADAFWFloatOrDoubleArray.h"
25 #include "COLLADAFWGeometry.h"
26 #include "COLLADAFWMeshPrimitive.h"
27 #include "COLLADAFWTypes.h"
28 #include "COLLADASWEffectProfile.h"
29 
30 #include <algorithm>
31 #include <map>
32 #include <set>
33 #include <vector>
34 
35 #include "DNA_anim_types.h"
36 #include "DNA_camera_types.h"
37 #include "DNA_constraint_types.h"
38 #include "DNA_light_types.h"
39 #include "DNA_mesh_types.h"
40 #include "DNA_object_types.h"
41 
42 #include "DNA_customdata_types.h"
43 #include "DNA_scene_types.h"
44 #include "DNA_texture_types.h"
45 
46 #include "RNA_access.h"
47 
48 #include "BLI_linklist.h"
49 #include "BLI_string.h"
50 #include "BLI_utildefines.h"
51 
52 #include "BKE_context.h"
53 #include "BKE_idprop.h"
54 #include "BKE_main.h"
55 #include "BKE_node.h"
56 #include "BKE_object.h"
57 #include "BKE_scene.h"
58 
59 #include "DEG_depsgraph_query.h"
60 
61 #include "BCSampleData.h"
62 #include "BlenderContext.h"
63 #include "ExportSettings.h"
64 #include "ImportSettings.h"
65 #include "collada_internal.h"
66 
67 constexpr int LIMITTED_PRECISION = 6;
68 
69 typedef std::map<COLLADAFW::UniqueId, Image *> UidImageMap;
70 typedef std::map<std::string, Image *> KeyImageMap;
71 typedef std::map<COLLADAFW::TextureMapId, std::vector<MTex *>> TexIndexTextureArrayMap;
72 typedef std::set<Object *> BCObjectSet;
73 
74 extern void bc_update_scene(BlenderContext &blender_context, float ctime);
75 
76 /* Action helpers */
77 
78 std::vector<bAction *> bc_getSceneActions(const bContext *C, Object *ob, bool all_actions);
79 
80 /* Action helpers */
81 
bc_getSceneObjectAction(Object * ob)82 inline bAction *bc_getSceneObjectAction(Object *ob)
83 {
84   return (ob->adt && ob->adt->action) ? ob->adt->action : NULL;
85 }
86 
87 /* Returns Light Action or NULL */
bc_getSceneLightAction(Object * ob)88 inline bAction *bc_getSceneLightAction(Object *ob)
89 {
90   if (ob->type != OB_LAMP) {
91     return NULL;
92   }
93 
94   Light *lamp = (Light *)ob->data;
95   return (lamp->adt && lamp->adt->action) ? lamp->adt->action : NULL;
96 }
97 
98 /* Return Camera Action or NULL */
bc_getSceneCameraAction(Object * ob)99 inline bAction *bc_getSceneCameraAction(Object *ob)
100 {
101   if (ob->type != OB_CAMERA) {
102     return NULL;
103   }
104 
105   Camera *camera = (Camera *)ob->data;
106   return (camera->adt && camera->adt->action) ? camera->adt->action : NULL;
107 }
108 
109 /* returns material action or NULL */
bc_getSceneMaterialAction(Material * ma)110 inline bAction *bc_getSceneMaterialAction(Material *ma)
111 {
112   if (ma == NULL) {
113     return NULL;
114   }
115 
116   return (ma->adt && ma->adt->action) ? ma->adt->action : NULL;
117 }
118 
119 std::string bc_get_action_id(std::string action_name,
120                              std::string ob_name,
121                              std::string channel_type,
122                              std::string axis_name,
123                              std::string axis_separator = "_");
124 
125 extern float bc_get_float_value(const COLLADAFW::FloatOrDoubleArray &array, unsigned int index);
126 extern int bc_test_parent_loop(Object *par, Object *ob);
127 
128 extern bool bc_validateConstraints(bConstraint *con);
129 
130 bool bc_set_parent(Object *ob, Object *par, bContext *C, bool is_parent_space = true);
131 extern Object *bc_add_object(
132     Main *bmain, Scene *scene, ViewLayer *view_layer, int type, const char *name);
133 extern Mesh *bc_get_mesh_copy(BlenderContext &blender_context,
134                               Object *ob,
135                               BC_export_mesh_type export_mesh_type,
136                               bool apply_modifiers,
137                               bool triangulate);
138 
139 extern Object *bc_get_assigned_armature(Object *ob);
140 extern bool bc_has_object_type(LinkNode *export_set, short obtype);
141 
142 extern char *bc_CustomData_get_layer_name(const CustomData *data, int type, int n);
143 extern char *bc_CustomData_get_active_layer_name(const CustomData *data, int type);
144 
145 extern void bc_bubble_sort_by_Object_name(LinkNode *export_set);
146 extern bool bc_is_root_bone(Bone *aBone, bool deform_bones_only);
147 extern int bc_get_active_UVLayer(Object *ob);
148 
149 std::string bc_find_bonename_in_path(std::string path, std::string probe);
150 
bc_string_after(const std::string & s,const std::string probe)151 inline std::string bc_string_after(const std::string &s, const std::string probe)
152 {
153   size_t i = s.rfind(probe);
154   if (i != std::string::npos) {
155     return (s.substr(i + probe.length(), s.length() - i));
156   }
157   return s;
158 }
159 
bc_string_before(const std::string & s,const std::string probe)160 inline std::string bc_string_before(const std::string &s, const std::string probe)
161 {
162   size_t i = s.find(probe);
163   if (i != std::string::npos) {
164     return s.substr(0, i);
165   }
166   return s;
167 }
168 
bc_startswith(std::string const & value,std::string const & starting)169 inline bool bc_startswith(std::string const &value, std::string const &starting)
170 {
171   if (starting.size() > value.size()) {
172     return false;
173   }
174   return (value.substr(0, starting.size()) == starting);
175 }
176 
bc_endswith(const std::string & value,const std::string & ending)177 inline bool bc_endswith(const std::string &value, const std::string &ending)
178 {
179   if (ending.size() > value.size()) {
180     return false;
181   }
182 
183   return value.compare(value.size() - ending.size(), ending.size(), ending) == 0;
184 }
185 
186 #if 0 /* UNUSED */
187 inline bool bc_endswith(std::string const &value, std::string const &ending)
188 {
189   if (ending.size() > value.size()) {
190     return false;
191   }
192   return std::equal(ending.rbegin(), ending.rend(), value.rbegin());
193 }
194 #endif
195 
196 extern std::string bc_replace_string(std::string data,
197                                      const std::string &pattern,
198                                      const std::string &replacement);
199 extern std::string bc_url_encode(std::string data);
200 extern void bc_match_scale(Object *ob, UnitConverter &bc_unit, bool scale_to_scene);
201 extern void bc_match_scale(std::vector<Object *> *objects_done,
202                            UnitConverter &bc_unit,
203                            bool scale_to_scene);
204 
205 extern void bc_decompose(float mat[4][4], float *loc, float eul[3], float quat[4], float *size);
206 extern void bc_rotate_from_reference_quat(float quat_to[4],
207                                           float quat_from[4],
208                                           float mat_to[4][4]);
209 
210 extern void bc_triangulate_mesh(Mesh *me);
211 extern bool bc_is_leaf_bone(Bone *bone);
212 extern EditBone *bc_get_edit_bone(bArmature *armature, char *name);
213 extern int bc_set_layer(int bitfield, int layer, bool enable);
214 extern int bc_set_layer(int bitfield, int layer);
215 
bc_in_range(float a,float b,float range)216 inline bool bc_in_range(float a, float b, float range)
217 {
218   return fabsf(a - b) < range;
219 }
220 void bc_copy_m4_farray(float r[4][4], float *a);
221 void bc_copy_farray_m4(float *r, float a[4][4]);
222 void bc_copy_darray_m4d(double *r, double a[4][4]);
223 void bc_copy_m4d_v44(double (&r)[4][4], std::vector<std::vector<double>> &a);
224 void bc_copy_v44_m4d(std::vector<std::vector<double>> &r, double (&a)[4][4]);
225 
226 void bc_sanitize_v3(double v[3], int precision);
227 void bc_sanitize_v3(float v[3], int precision);
228 
229 extern IDProperty *bc_get_IDProperty(Bone *bone, std::string key);
230 extern void bc_set_IDProperty(EditBone *ebone, const char *key, float value);
231 extern void bc_set_IDPropertyMatrix(EditBone *ebone, const char *key, float mat[4][4]);
232 
233 extern float bc_get_property(Bone *bone, std::string key, float def);
234 extern void bc_get_property_vector(Bone *bone, std::string key, float val[3], const float def[3]);
235 extern bool bc_get_property_matrix(Bone *bone, std::string key, float mat[4][4]);
236 
237 extern void bc_enable_fcurves(bAction *act, char *bone_name);
238 extern bool bc_bone_matrix_local_get(Object *ob, Bone *bone, Matrix &mat, bool for_opensim);
239 extern bool bc_is_animated(BCMatrixSampleMap &values);
240 extern bool bc_has_animations(Scene *sce, LinkNode *export_set);
241 extern bool bc_has_animations(Object *ob);
242 
243 extern void bc_add_global_transform(Matrix &to_mat,
244                                     const Matrix &from_mat,
245                                     const BCMatrix &global_transform,
246                                     const bool invert = false);
247 extern void bc_add_global_transform(Vector &to_vec,
248                                     const Vector &from_vec,
249                                     const BCMatrix &global_transform,
250                                     const bool invert = false);
251 extern void bc_add_global_transform(Vector &to_vec,
252                                     const BCMatrix &global_transform,
253                                     const bool invert = false);
254 extern void bc_add_global_transform(Matrix &to_mat,
255                                     const BCMatrix &global_transform,
256                                     const bool invert = false);
257 extern void bc_apply_global_transform(Matrix &to_mat,
258                                       const BCMatrix &global_transform,
259                                       const bool invert = false);
260 extern void bc_apply_global_transform(Vector &to_vec,
261                                       const BCMatrix &global_transform,
262                                       const bool invert = false);
263 extern void bc_create_restpose_mat(BCExportSettings &export_settings,
264                                    Bone *bone,
265                                    float to_mat[4][4],
266                                    float from_mat[4][4],
267                                    bool use_local_space);
268 
269 class ColladaBaseNodes {
270  private:
271   std::vector<Object *> base_objects;
272 
273  public:
add(Object * ob)274   void add(Object *ob)
275   {
276     base_objects.push_back(ob);
277   }
278 
contains(Object * ob)279   bool contains(Object *ob)
280   {
281     std::vector<Object *>::iterator it = std::find(base_objects.begin(), base_objects.end(), ob);
282     return (it != base_objects.end());
283   }
284 
size()285   int size()
286   {
287     return base_objects.size();
288   }
289 
get(int index)290   Object *get(int index)
291   {
292     return base_objects[index];
293   }
294 };
295 
296 class BCPolygonNormalsIndices {
297   std::vector<unsigned int> normal_indices;
298 
299  public:
add_index(unsigned int index)300   void add_index(unsigned int index)
301   {
302     normal_indices.push_back(index);
303   }
304 
305   unsigned int operator[](unsigned int i)
306   {
307     return normal_indices[i];
308   }
309 };
310 
311 class BoneExtended {
312 
313  private:
314   char name[MAXBONENAME];
315   int chain_length;
316   bool is_leaf;
317   float tail[3];
318   float roll;
319 
320   int bone_layers;
321   int use_connect;
322   bool has_custom_tail;
323   bool has_custom_roll;
324 
325  public:
326   BoneExtended(EditBone *aBone);
327 
328   void set_name(char *aName);
329   char *get_name();
330 
331   void set_chain_length(const int aLength);
332   int get_chain_length();
333 
334   void set_leaf_bone(bool state);
335   bool is_leaf_bone();
336 
337   void set_bone_layers(std::string layers, std::vector<std::string> &layer_labels);
338   int get_bone_layers();
339   static std::string get_bone_layers(int bitfield);
340 
341   void set_roll(float roll);
342   bool has_roll();
343   float get_roll();
344 
345   void set_tail(const float vec[]);
346   float *get_tail();
347   bool has_tail();
348 
349   void set_use_connect(int use_connect);
350   int get_use_connect();
351 };
352 
353 /* a map to store bone extension maps
354  * std:string     : an armature name
355  * BoneExtended * : a map that contains extra data for bones
356  */
357 typedef std::map<std::string, BoneExtended *> BoneExtensionMap;
358 
359 /*
360  * A class to organize bone extension data for multiple Armatures.
361  * this is needed for the case where a Collada file contains 2 or more
362  * separate armatures.
363  */
364 class BoneExtensionManager {
365  private:
366   std::map<std::string, BoneExtensionMap *> extended_bone_maps;
367 
368  public:
369   BoneExtensionMap &getExtensionMap(bArmature *armature);
370   ~BoneExtensionManager();
371 };
372 
373 void bc_add_default_shader(bContext *C, Material *ma);
374 bNode *bc_get_master_shader(Material *ma);
375 
376 COLLADASW::ColorOrTexture bc_get_base_color(Material *ma);
377 COLLADASW::ColorOrTexture bc_get_emission(Material *ma);
378 COLLADASW::ColorOrTexture bc_get_ambient(Material *ma);
379 COLLADASW::ColorOrTexture bc_get_specular(Material *ma);
380 COLLADASW::ColorOrTexture bc_get_reflective(Material *ma);
381 
382 double bc_get_reflectivity(Material *ma);
383 double bc_get_alpha(Material *ma);
384 double bc_get_ior(Material *ma);
385 double bc_get_shininess(Material *ma);
386 
387 bool bc_get_float_from_shader(bNode *shader, double &val, std::string nodeid);
388 COLLADASW::ColorOrTexture bc_get_cot_from_shader(bNode *shader,
389                                                  std::string nodeid,
390                                                  Color &default_color,
391                                                  bool with_alpha = true);
392 
393 COLLADASW::ColorOrTexture bc_get_cot(float r, float g, float b, float a);
394 COLLADASW::ColorOrTexture bc_get_cot(Color col, bool with_alpha = true);
395