Skeletal Animation Multithread Face
hand.cpp
Go to the documentation of this file.
1#include "../include/hand.h"
2
15Hand::Hand(Exoskeleton* exo_handler, AnimatedHand* anim_hand,
16 const std::string& mesh_name, const std::string& graph_name,
17 const std::string& texture_name, bool type, const Eigen::Vector3d& origin)
18{
19 // Read volume mesh
20 igl::readMESH(mesh_name, m_vol_mesh_vert, m_tetr_indices, m_surf_indices);
21
22 // Set initial mesh vertices
24
25 // Read graph file
26 igl::readTGF(graph_name, m_vertex_pos, m_bone_edges_indices);
27
28 // Retrieve parents for forward kinematics
29 igl::directed_edge_parents(m_bone_edges_indices, m_point_handles);
30
31 // Set boundary conditions
32 igl::boundary_conditions(m_vol_mesh_vert, m_tetr_indices, m_vertex_pos,
33 Eigen::VectorXi(), m_bone_edges_indices , Eigen::MatrixXi(),
35
36 // Only a few iterations
37 m_bbw_data.active_set_params.max_iter = 8;
38 m_bbw_data.verbosity = 2;
39
42 {
43 std::cout << "Error: BBW Error" << std::endl;
44 }
45
46 // Normalize weights to sum to one
47 igl::normalize_row_sums(m_weights, m_weights);
48
49 // Precompute linear blend skinning matrix
50 igl::lbs_matrix(m_vol_mesh_vert, m_weights, m_g_weights);
51
52 // Get exoskeleton handler pointer
53 m_exo_handler = exo_handler;
54
55 // Get animation hand pointer
56 m_anim_hand = anim_hand;
57
58 // Get hand index iterator
60
61 // Initialize animation pose
63
64 for (size_t i = 0; i < m_pose_size; i++)
65 {
66 m_anim_pose.at(i) = Eigen::Quaterniond::Identity();
67 }
68
69 // Read texture
70 igl::png::readPNG(texture_name, m_texture.red, m_texture.green,
72
73 // Get hand type
74 m_hand_type = type;
75
76 // Get hand origin
77 m_origin = origin;
78}
79
87void Hand::update(const std::vector<AnimatedHand::EulerID>& euler_id)
88{
89 for (size_t i = 0; i < euler_id.size(); i++)
90 {
91 // Get euler id i
92 std::vector<double> euler_i = euler_id.at(i).euler_angles;
93
94 // // Get frame id i
95 int frame_id_i = euler_id.at(i).frame_id;
96
97 // Transform angles to quaternions
100 euler_i.at(1), euler_i.at(2));
101
102 // Set animation pose
103 m_anim_pose.at(frame_id_i) = Eigen::Quaterniond(quatern.w, quatern.x,
104 quatern.y, quatern.z);
105 }
106
107 // Propagate relative rotations via FK to retrieve absolute transformations
108 RotationList v_rot;
109 std::vector<Eigen::Vector3d> v_tran;
110
111 // Forward kinematics
112 igl::forward_kinematics(m_vertex_pos, m_bone_edges_indices,
113 m_point_handles, m_anim_pose, v_rot, v_tran);
114
115 // Get verted dimension
116 int vert_dim = m_vertex_pos.cols();
117
118 // Total transformation matrix
119 Eigen::MatrixXd t_mat(m_bone_edges_indices.rows()*(vert_dim+1), vert_dim);
120
121 // Perform trasnformation
122 for (int i = 0 ; i < m_bone_edges_indices.rows(); i++)
123 {
124 Eigen::Affine3d a = Eigen::Affine3d::Identity();
125
126 // Translate (has a trick to account for mirroring in case of right hand)
127 a.translate(v_tran.at(i) + (-2.0 * (double)m_hand_type + 1) * m_origin);
128
129 // Rotate
130 a.rotate(v_rot.at(i));
131
132 t_mat.block(i*(vert_dim+1), 0, vert_dim+1, vert_dim) =
133 a.matrix().transpose().block(0, 0, vert_dim+1, vert_dim);
134 }
135
136 // Compute deformation via LBS as matrix multiplication
138
139 if (m_hand_type) // check if it's the right hand and mirror it
140 {
141 Eigen::Matrix3d mirror = Eigen::Matrix3d::Identity();
142 mirror(0, 0) = -1.0;
143 m_vol_mesh_vert = (mirror * m_vol_mesh_vert.transpose()).transpose();
144 }
145}
std::vector< int > get_hand_idx_iterator(void)
Definition: animated_hand.h:46
static Quaternions euler_to_quaternions(double phi, double theta, double psi)
Convert Euler angles to quaternions.
Class Exoskeleton.
Definition: exoskeleton.h:20
Eigen::MatrixXd m_vertex_pos
List of vertex positions (C)
Definition: hand.h:82
Eigen::MatrixXi m_tetr_indices
Volume mesh tetrahedral indices(T) and surface element indices(F)
Definition: hand.h:73
PngImage m_texture
Texture image.
Definition: hand.h:117
igl::BBWData m_bbw_data
BBW weights matrix (bbw_data)
Definition: hand.h:91
Eigen::MatrixXd m_weights
List of weights (W)
Definition: hand.h:94
void update(const std::vector< AnimatedHand::EulerID > &euler_id)
Updates the hand.
Definition: hand.cpp:87
Eigen::VectorXi m_boundary_indices
List of boundary indices (b)
Definition: hand.h:85
Eigen::MatrixXd m_vol_mesh_vert_init
Definition: hand.h:70
Eigen::Vector3d m_origin
Hand origin.
Definition: hand.h:123
Eigen::MatrixXi m_bone_edges_indices
Bone-edges skeleton indices (BE)
Definition: hand.h:76
Eigen::MatrixXi m_surf_indices
Definition: hand.h:73
RotationList m_anim_pose
Animation pose.
Definition: hand.h:111
Hand(Exoskeleton *exo_handler, AnimatedHand *anim_hand, const std::string &mesh_name, const std::string &graph_name, const std::string &texture_name, bool type, const Eigen::Vector3d &origin)
Constructor.
Definition: hand.cpp:15
Eigen::VectorXi m_point_handles
Point handles list (P)
Definition: hand.h:79
std::vector< int > m_hand_idx_iter
Hand index iterator.
Definition: hand.h:108
Eigen::MatrixXd m_vol_mesh_vert
Volume mesh Vertices (V)
Definition: hand.h:70
Eigen::MatrixXd m_g_weights
Global weights (M)
Definition: hand.h:97
int m_pose_size
Pose size.
Definition: hand.h:114
Exoskeleton * m_exo_handler
Exoskeleton handler pointer.
Definition: hand.h:102
AnimatedHand * m_anim_hand
Animated hand pointer.
Definition: hand.h:105
Eigen::MatrixXd m_boundary_conditions
List of boundary conditions of each weight function (bc)
Definition: hand.h:88
bool m_hand_type
Hand type.
Definition: hand.h:120
std::vector< Eigen::Quaterniond, Eigen::aligned_allocator< Eigen::Quaterniond > > RotationList
Definition: hand.h:22
Custom quaternions struct: .
Eigen::Matrix< unsigned char, Eigen::Dynamic, Eigen::Dynamic > red
Definition: hand.h:46
Eigen::Matrix< unsigned char, Eigen::Dynamic, Eigen::Dynamic > blue
Definition: hand.h:48
Eigen::Matrix< unsigned char, Eigen::Dynamic, Eigen::Dynamic > a
Definition: hand.h:49
Eigen::Matrix< unsigned char, Eigen::Dynamic, Eigen::Dynamic > green
Definition: hand.h:47