31 #ifndef EULER_TANSFORMATION_MATRICES 32 #define EULER_TANSFORMATION_MATRICES 45 double c_phi = cos(phi);
46 double s_phi = sin(phi);
47 double c_theta = cos(theta);
48 double s_theta = sin(theta);
49 double c_psi = cos(psi);
50 double s_psi = sin(psi);
53 if (matrix.n_cols < 3 || matrix.n_rows < 3)
54 matrix.set_size(3, 3);
56 matrix(0, 0) = c_phi * c_psi - s_phi * c_theta * s_psi;
57 matrix(0, 1) = -c_phi * s_psi - s_phi * c_theta * c_psi;
58 matrix(0, 2) = s_phi * s_theta;
60 matrix(1, 0) = s_phi * c_psi + c_phi * c_theta * s_psi;
61 matrix(1, 1) = -s_phi * s_psi + c_phi * c_theta * c_psi;
62 matrix(1, 2) = -c_phi * s_theta;
64 matrix(2, 0) = s_theta * s_psi;
65 matrix(2, 1) = s_theta * c_psi;
66 matrix(2, 2) = c_theta;
75 static void create_zxz_matrix(
double x,
double y,
double z,
double phi,
double theta,
double psi, arma::mat& matrix) {
76 double c_phi = cos(phi);
77 double s_phi = sin(phi);
78 double c_theta = cos(theta);
79 double s_theta = sin(theta);
80 double c_psi = cos(psi);
81 double s_psi = sin(psi);
84 if (matrix.n_cols < 4 || matrix.n_rows < 4) {
85 matrix.set_size(4, 4);
89 matrix(0, 0) = c_phi * c_psi - s_phi * c_theta * s_psi;
90 matrix(0, 1) = -c_phi * s_psi - s_phi * c_theta * c_psi;
91 matrix(0, 2) = s_phi * s_theta;
94 matrix(1, 0) = s_phi * c_psi + c_phi * c_theta * s_psi;
95 matrix(1, 1) = -s_phi * s_psi + c_phi * c_theta * c_psi;
96 matrix(1, 2) = -c_phi * s_theta;
99 matrix(2, 0) = s_theta * s_psi;
100 matrix(2, 1) = s_theta * c_psi;
101 matrix(2, 2) = c_theta;
114 double c_phi = cos(phi);
115 double s_phi = sin(phi);
116 double c_theta = cos(theta);
117 double s_theta = sin(theta);
118 double c_psi = cos(psi);
119 double s_psi = sin(psi);
122 if (matrix.n_cols < 3 || matrix.n_rows < 3)
123 matrix.set_size(3, 3);
125 matrix(0, 0) = c_theta * c_phi;
126 matrix(0, 1) = -c_psi * s_phi + s_psi * s_theta * c_phi;
127 matrix(0, 2) = s_psi * s_phi + c_psi * s_theta * c_phi;
129 matrix(1, 0) = c_theta * s_phi;
130 matrix(1, 1) = c_psi * c_phi + s_psi * s_theta * s_phi;
131 matrix(1, 2) = -s_psi * c_phi + c_psi * s_theta * s_phi;
133 matrix(2, 0) = -s_theta;
134 matrix(2, 1) = s_psi * c_theta;
135 matrix(2, 2) = c_psi * c_theta;
143 static void create_zyx_matrix(
double x,
double y,
double z,
double phi,
double theta,
double psi, arma::mat& matrix) {
144 double c_phi = cos(phi);
145 double s_phi = sin(phi);
146 double c_theta = cos(theta);
147 double s_theta = sin(theta);
148 double c_psi = cos(psi);
149 double s_psi = sin(psi);
152 if (matrix.n_cols < 4 || matrix.n_rows < 4) {
153 matrix.set_size(4, 4);
156 matrix(0, 0) = c_theta * c_phi;
157 matrix(0, 1) = -c_psi * s_phi + s_psi * s_theta * c_phi;
158 matrix(0, 2) = s_psi * s_phi + c_psi * s_theta * c_phi;
161 matrix(1, 0) = c_theta * s_phi;
162 matrix(1, 1) = c_psi * c_phi + s_psi * s_theta * s_phi;
163 matrix(1, 2) = -s_psi * c_phi + c_psi * s_theta * s_phi;
166 matrix(2, 0) = -s_theta;
167 matrix(2, 1) = s_psi * c_theta;
168 matrix(2, 2) = c_psi * c_theta;
174 static void zxz_to_zyx_angles(
const double in_a,
const double in_e,
const double in_r,
double& out_a,
double& out_e,
double& out_r) {
180 out_e = atan2(-r(2, 0), hypot(r(0, 0), r(1, 0)));
183 if (fabs(r(2, 1)) + fabs(r(2, 2)) < 10 * std::numeric_limits<double>::epsilon()) {
186 out_a = -atan2(-r(1, 2), r(0, 2));
188 out_a = atan2(-r(1, 2), -r(0, 2));
190 out_r = atan2(r(2, 1), r(2, 2));
191 out_a = atan2(r(1, 0), r(0, 0));
195 static void zyx_to_zxz_angles(
const double in_a,
const double in_e,
const double in_r,
double& out_a,
double& out_e,
double& out_r) {
200 out_e = acos(r(2, 2));
202 if (fabs(sin(out_e)) > 1E-8) {
203 out_r = atan2(r(2, 0) / sin(out_e), r(2, 1) / sin(out_e));
204 out_a = -atan2(r(0, 2) / sin(out_e), r(1, 2) / sin(out_e)) +
M_PI;
207 out_r = atan2(r(1, 0), r(1, 1)) +
M_PI;
211 static void zyx_from_matrix(
const arma::mat& matrix,
double& out_x,
double& out_y,
double& out_z,
double& out_azimuth,
double& out_elevation,
double& out_roll) {
212 double azimuth, elevation, roll;
215 elevation = atan2(-matrix(2, 0), hypot(matrix(0, 0), matrix(1, 0)));
218 if ((fabs(matrix(2, 1)) + fabs(matrix(2, 2))) < 10 * std::numeric_limits<double>::epsilon()) {
221 azimuth = -atan2(-matrix(1, 2), matrix(0, 2));
223 azimuth = atan2(-matrix(1, 2), -matrix(0, 2));
225 roll = atan2(matrix(2, 1), matrix(2, 2));
226 azimuth = atan2(matrix(1, 0), matrix(0, 0));
229 out_x = matrix(0, 3);
230 out_y = matrix(1, 3);
231 out_z = matrix(2, 3);
232 out_azimuth = azimuth;
233 out_elevation = elevation;