Skeletal Animation Multithread Face
face.cpp
Go to the documentation of this file.
1#include "../include/face.h"
2
10{
11 // Get absolute file path
12 std::filesystem::path absolute_path = std::filesystem::current_path();
13
14 // Define absolute path of hand configuration file
17
18 // Read indices matrix
19 Eigen::MatrixXd tmp_faces =
21 m_surf_indices = tmp_faces.cast<int>();
22
23 // Setup communication with python server
25
26 // Initialize asychronous function
27 m_future_fun = std::async(&Face::incoming_data_callback, this);
28
29 // Initialize volume vertices
30 m_vol_mesh_vertices = Eigen::MatrixXd::Zero(m_vertices_num, 3);
31
32 // Set rotation matrix
33 m_rot_mat = EulerRotations::rotation(0, M_PI, M_PI);
34
35}
41Eigen::MatrixXd Face::get_vertices(void)
42{
43 // Set termination flag for asychronous function
45
46 // Get vertices
47 Eigen::MatrixXd vertices = m_future_fun.get();
48
49 // Reinitialize async function
51 m_future_fun = std::async(&Face::incoming_data_callback, this);
52
53 return vertices;
54}
55
63Eigen::MatrixXd Face::incoming_data_callback(void)
64{
65 while(!m_return_value)
66 {
67
68 // Initialize buffer
69 char buffer[m_buffer_size] = {0};
70
71 // Read value
72 int val_read = read(m_socket, buffer, m_buffer_size);
73
74 // Correct parsing flag
75 bool correct_parsing;
76
77 // Initialzie volume mesh
78 Eigen::MatrixXd vol_mesh_vertices;
79
80 // Initialize point cloud
81 nlohmann::basic_json<>::value_type x_pc, y_pc, z_pc;
82
83 try
84 {
85 // Parse json file
86 nlohmann::json incoming_data = nlohmann::json::parse(buffer);
87
88 // Pointcloud x
89 x_pc = incoming_data["x"];
90 y_pc = incoming_data["y"];
91 z_pc = incoming_data["z"];
92
93 vol_mesh_vertices.resize(x_pc.size(), 3);
94
95 // Check if the parsing is correct
96 correct_parsing = (x_pc.size() == m_vertices_num);
97 }
98 catch(nlohmann::json::parse_error& e)
99 {
100 // Parsing incorrect
101 correct_parsing = 0;
102 }
103
104 if (correct_parsing)
105 {
106 for (size_t i = 0; i < x_pc.size(); i++)
107 {
108 vol_mesh_vertices(i, 0) = x_pc[i];
109 vol_mesh_vertices(i, 1) = y_pc[i];
110 vol_mesh_vertices(i, 2) = z_pc[i];
111 }
112
113 // Rotate and scale vertices
115 vol_mesh_vertices.transpose()).transpose();
116
117 // Find centre of geometry
118 double x_cg = m_vol_mesh_vertices.col(0).sum() / m_vertices_num;
119 double y_cg = m_vol_mesh_vertices.col(1).sum() / m_vertices_num;
120 double z_cg = m_vol_mesh_vertices.col(2).sum() / m_vertices_num;
121
122 // Reset face offset
123 Eigen::Vector3d face_offset = m_face_offset +
124 Eigen::Vector3d({x_cg, y_cg, z_cg});
125
128 }
129 }
130
131 return m_vol_mesh_vertices;
132}
133
144{
145 // Sock addr struct
146 struct sockaddr_in serv_addr;
147
148 if ((m_socket = socket(AF_INET, SOCK_STREAM, 0)) < 0)
149 {
150 printf("\n Socket creation error \n");
151 }
152
153 serv_addr.sin_family = AF_INET;
154 serv_addr.sin_port = htons(m_tcp_port);
155
156 // Convert IPv4 and IPv6 addresses from text to binary form
157 if(inet_pton(AF_INET, m_local_ip_address.c_str(), &serv_addr.sin_addr)<=0)
158 {
159 printf("\nInvalid address/ Address not supported \n");
160 }
161
162 if (connect(m_socket, (struct sockaddr *)&serv_addr, sizeof(serv_addr)) < 0)
163 {
164 printf("\nConnection Failed \n");
165 }
166}
167
176Eigen::MatrixXd Face::translation_matrix(const Eigen::Vector3d& offset,
177 size_t vert_num)
178{
179 // Initialize matrix
180 Eigen::MatrixXd t_mat = Eigen::MatrixXd(vert_num, offset.rows());
181
182 // Ones vector
183 Eigen::VectorXd ones_vec = Eigen::VectorXd::Ones(vert_num);
184
185 for(size_t i = 0; i < offset.rows(); i++)
186 {
187 t_mat.col(i) = offset(i) * ones_vec;
188 }
189
190 return t_mat;
191}
static Eigen::Matrix3d rotation(double phi, double theta, double psi)
Euler rotation matrix z-y'-x''.
Eigen::MatrixXd incoming_data_callback(void)
Read incoming data.
Definition: face.cpp:63
const int m_vertices_num
Number of vertices provided by the mediapipe. This is constant.
Definition: face.h:82
const int m_buffer_size
Definition: face.h:79
Eigen::MatrixXi m_surf_indices
The surface element indices of the incoming mesh.
Definition: face.h:85
Eigen::MatrixXd get_vertices(void)
Get face vertices.
Definition: face.cpp:41
int m_socket
Socket handle.
Definition: face.h:73
Eigen::Matrix3d m_rot_mat
Face rotation matrix.
Definition: face.h:101
Eigen::Matrix3d m_scale_mat
Definition: face.h:106
std::string m_vertices_connections_rel_filename
Relative name of vertices connection files.
Definition: face.h:55
Eigen::Vector3d m_face_offset
Definition: face.h:105
Eigen::MatrixXd translation_matrix(const Eigen::Vector3d &offset, size_t vert_num)
This function offset a set of vertices by a given offset.
Definition: face.cpp:176
std::string m_local_ip_address
Start tcp client at this ip adress (localhost).
Definition: face.h:66
Eigen::MatrixXd m_vol_mesh_vertices
Volume mesh data.
Definition: face.h:98
const int m_tcp_port
Definition: face.h:70
bool m_return_value
Termination flag for callback function.
Definition: face.h:92
void setup_tcp_communication(void)
Setup communication with python server.
Definition: face.cpp:143
std::future< Eigen::MatrixXd > m_future_fun
Future function handle.
Definition: face.h:89
std::filesystem::path m_vertices_connections_abs_path
Absolute name of vertices connection files.
Definition: face.h:52
Face()
Constructor.
Definition: face.cpp:9
static Eigen::MatrixXd load_matrix_data(std::string filename)
Load Eigen double matrix from csv file.
Definition: utils.cpp:59