Blender  V2.93
SkinInfo.h
Go to the documentation of this file.
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 
21 #pragma once
22 
23 #include <map>
24 #include <vector>
25 
26 #include "COLLADAFWNode.h"
27 #include "COLLADAFWSkinController.h"
28 #include "COLLADAFWSkinControllerData.h"
29 #include "COLLADAFWTypes.h"
30 #include "COLLADAFWUniqueId.h"
31 
32 #include "BKE_context.h"
33 #include "DNA_object_types.h"
34 
35 #include "TransformReader.h"
36 #include "collada_internal.h"
37 
38 /* This is used to store data passed in write_controller_data.
39  * Arrays from COLLADAFW::SkinControllerData lose ownership, so do this class members
40  * so that arrays don't get freed until we free them explicitly. */
41 class SkinInfo {
42  private:
43  /* to build armature bones from inverse bind matrices */
44  struct JointData {
45  float inv_bind_mat[4][4]; /* joint inverse bind matrix */
46  COLLADAFW::UniqueId joint_uid; /* joint node UID */
47  // Object *ob_arm; /* armature object */
48  };
49 
50  float bind_shape_matrix[4][4];
51 
52  /* data from COLLADAFW::SkinControllerData, each array should be freed */
53  COLLADAFW::UIntValuesArray joints_per_vertex;
54  COLLADAFW::UIntValuesArray weight_indices;
55  COLLADAFW::IntValuesArray joint_indices;
56  /* COLLADAFW::FloatOrDoubleArray weights; */
57  std::vector<float> weights;
58 
59  std::vector<JointData> joint_data; /* index to this vector is joint index */
60 
61  UnitConverter *unit_converter;
62 
63  Object *ob_arm;
64  COLLADAFW::UniqueId controller_uid;
65  Object *parent;
66 
67  public:
69  SkinInfo(const SkinInfo &skin);
70  SkinInfo(UnitConverter *conv);
71 
72  /* nobody owns the data after this, so it should be freed manually with releaseMemory */
73  template<typename T> void transfer_array_data(T &src, T &dest);
74 
75  /* when src is const we cannot src.yieldOwnerShip, this is used by copy constructor */
76  void transfer_int_array_data_const(const COLLADAFW::IntValuesArray &src,
77  COLLADAFW::IntValuesArray &dest);
78 
79  void transfer_uint_array_data_const(const COLLADAFW::UIntValuesArray &src,
80  COLLADAFW::UIntValuesArray &dest);
81 
82  void borrow_skin_controller_data(const COLLADAFW::SkinControllerData *skin);
83 
84  void free();
85 
86  /* using inverse bind matrices to construct armature
87  * it is safe to invert them to get the original matrices
88  * because if they are inverse matrices, they can be inverted */
89  void add_joint(const COLLADABU::Math::Matrix4 &matrix);
90 
91  void set_controller(const COLLADAFW::SkinController *co);
92 
93  /* called from write_controller */
94  Object *create_armature(Main *bmain, Scene *scene, ViewLayer *view_layer);
95 
96  Object *set_armature(Object *ob_arm);
97 
98  bool get_joint_inv_bind_matrix(float inv_bind_mat[4][4], COLLADAFW::Node *node);
99 
101 
102  const COLLADAFW::UniqueId &get_controller_uid();
103 
104  /* check if this skin controller references a joint or any descendant of it
105  *
106  * some nodes may not be referenced by SkinController,
107  * in this case to determine if the node belongs to this armature,
108  * we need to search down the tree */
109  bool uses_joint_or_descendant(COLLADAFW::Node *node);
110 
111  void link_armature(bContext *C,
112  Object *ob,
113  std::map<COLLADAFW::UniqueId, COLLADAFW::Node *> &joint_by_uid,
114  TransformReader *tm);
115 
116  bPoseChannel *get_pose_channel_from_node(COLLADAFW::Node *node);
117 
118  void set_parent(Object *_parent);
119 
120  Object *get_parent();
121 
122  void find_root_joints(const std::vector<COLLADAFW::Node *> &root_joints,
123  std::map<COLLADAFW::UniqueId, COLLADAFW::Node *> &joint_by_uid,
124  std::vector<COLLADAFW::Node *> &result);
125 
126  bool find_node_in_tree(COLLADAFW::Node *node, COLLADAFW::Node *tree_root);
127 };
Object is a sort of wrapper for general info.
#define C
Definition: RandGen.cpp:39
void find_root_joints(const std::vector< COLLADAFW::Node * > &root_joints, std::map< COLLADAFW::UniqueId, COLLADAFW::Node * > &joint_by_uid, std::vector< COLLADAFW::Node * > &result)
Definition: SkinInfo.cpp:315
const COLLADAFW::UniqueId & get_controller_uid()
Definition: SkinInfo.cpp:190
void transfer_int_array_data_const(const COLLADAFW::IntValuesArray &src, COLLADAFW::IntValuesArray &dest)
Definition: SkinInfo.cpp:88
void set_controller(const COLLADAFW::SkinController *co)
Definition: SkinInfo.cpp:136
void link_armature(bContext *C, Object *ob, std::map< COLLADAFW::UniqueId, COLLADAFW::Node * > &joint_by_uid, TransformReader *tm)
Definition: SkinInfo.cpp:220
Object * set_armature(Object *ob_arm)
Definition: SkinInfo.cpp:161
void borrow_skin_controller_data(const COLLADAFW::SkinControllerData *skin)
Definition: SkinInfo.cpp:102
bool uses_joint_or_descendant(COLLADAFW::Node *node)
Definition: SkinInfo.cpp:200
Object * BKE_armature_from_object()
Definition: SkinInfo.cpp:185
bool get_joint_inv_bind_matrix(float inv_bind_mat[4][4], COLLADAFW::Node *node)
Definition: SkinInfo.cpp:171
void add_joint(const COLLADABU::Math::Matrix4 &matrix)
Definition: SkinInfo.cpp:129
void transfer_uint_array_data_const(const COLLADAFW::UIntValuesArray &src, COLLADAFW::UIntValuesArray &dest)
Definition: SkinInfo.cpp:95
void set_parent(Object *_parent)
Definition: SkinInfo.cpp:305
Object * get_parent()
Definition: SkinInfo.cpp:310
bool find_node_in_tree(COLLADAFW::Node *node, COLLADAFW::Node *tree_root)
Definition: SkinInfo.cpp:341
void transfer_array_data(T &src, T &dest)
Definition: SkinInfo.cpp:80
bPoseChannel * get_pose_channel_from_node(COLLADAFW::Node *node)
Definition: SkinInfo.cpp:300
Object * create_armature(Main *bmain, Scene *scene, ViewLayer *view_layer)
Definition: SkinInfo.cpp:155
void free()
Definition: SkinInfo.cpp:118
OperationNode * node
Scene scene
#define T
Definition: BKE_main.h:116