34 cvInitMatHeader(&quaternion_mat, 4, 1, CV_64F, quaternion);
39 cvInitMatHeader(&quaternion_mat, 4, 1, CV_64F, quaternion);
45 cvInitMatHeader(&quaternion_mat, 4, 1, CV_64F, quaternion);
66 void Rotation::Transpose()
69 CvMat tmp_mat = cvMat(3, 3, CV_64F, tmp);
71 cvTranspose(&tmp_mat, &tmp_mat);
75 void Rotation::MirrorMat(CvMat *mat,
bool x,
bool y,
bool z) {
76 CvMat *mat_mul = cvCloneMat(mat);
77 cvSetIdentity(mat_mul);
78 if (x) cvmSet(mat_mul, 0, 0, -1);
79 if (y) cvmSet(mat_mul, 1, 1, -1);
80 if (z) cvmSet(mat_mul, 2, 2, -1);
81 cvMatMul(mat_mul, mat, mat);
82 cvReleaseMat(&mat_mul);
85 void Rotation::Mirror(
bool x,
bool y,
bool z)
88 CvMat tmp_mat = cvMat(3, 3, CV_64F, tmp);
90 MirrorMat(&tmp_mat, x, y, z);
94 void Rotation::Reset()
96 cvZero(&quaternion_mat); cvmSet(&quaternion_mat, 0, 0, 1);
99 void Rotation::Mat9ToRod(
double *mat,
double *rod)
102 cvInitMatHeader(&mat_m, 3, 3, CV_64F, mat);
103 cvInitMatHeader(&rod_m, 3, 1, CV_64F, rod);
104 cvRodrigues2(&mat_m, &rod_m);
107 void Rotation::RodToMat9(
double *rod,
double *mat)
110 cvInitMatHeader(&mat_m, 3, 3, CV_64F, mat);
111 cvInitMatHeader(&rod_m, 3, 1, CV_64F, rod);
112 cvRodrigues2(&rod_m, &mat_m, 0);
115 void Rotation::QuatInv(
const double *q,
double *qi)
123 void Rotation::QuatNorm(
double *q)
125 double l = sqrt(q[0]*q[0] + q[1]*q[1] + q[2]*q[2] + q[3]*q[3] );
128 for(
unsigned i = 0; i < 4; ++i)
132 void Rotation::QuatMul(
const double *q1,
const double *q2,
double *q3)
144 q3[0] = w1*w2 - x1*x2 - y1*y2 - z1*z2;
145 q3[1] = w1*x2 + x1*w2 + y1*z2 - z1*y2;
146 q3[2] = w1*y2 + y1*w2 + z1*x2 - x1*z2;
147 q3[3] = w1*z2 + z1*w2 + x1*y2 - y1*x2;
152 void Rotation::QuatToMat9(
const double *quat,
double *mat)
171 mat[0] = 1 - 2 * ( yy + zz );
172 mat[1] = 2 * ( xy - zw );
173 mat[2] = 2 * ( xz + yw );
175 mat[3] = 2 * ( xy + zw );
176 mat[4] = 1 - 2 * ( xx + zz );
177 mat[5] = 2 * ( yz - xw );
179 mat[6] = 2 * ( xz - yw );
180 mat[7] = 2 * ( yz + xw );
181 mat[8] = 1 - 2 * ( xx + yy );
185 void Rotation::QuatToMat16(
const double *quat,
double *mat)
206 mat[0] = 1 - 2 * ( yy + zz );
207 mat[1] = 2 * ( xy - zw );
208 mat[2] = 2 * ( xz + yw );
210 mat[4] = 2 * ( xy + zw );
211 mat[5] = 1 - 2 * ( xx + zz );
212 mat[6] = 2 * ( yz - xw );
214 mat[8] = 2 * ( xz - yw );
215 mat[9] = 2 * ( yz + xw );
216 mat[10] = 1 - 2 * ( xx + yy );
221 void Rotation::QuatToEul(
const double *q,
double *eul)
228 double heading = 0, bank = 0, attitude = 0;
230 if ((2*qx*qy + 2*qz*qw) == 1.0)
232 heading = 2 * atan2(qx,qw);
235 else if ((2*qx*qy + 2*qz*qw) == -1.0)
237 heading = -2 * atan2(qx,qw);
242 heading = atan2(2*qy*qw-2*qx*qz , 1 - 2*qy*qy - 2*qz*qz);
243 bank = atan2(2*qx*qw-2*qy*qz , 1 - 2*qx*qx - 2*qz*qz);
246 attitude = asin(2*qx*qy + 2*qz*qw);
248 heading = 180.0 * heading /
PI;
249 attitude = 180.0 * attitude /
PI;
250 bank = 180.0 * bank /
PI;
257 void Rotation::Mat9ToQuat(
const double *mat,
double *quat)
259 quat[0] = sqrt(max(0., 1 + mat[0] + mat[4] + mat[8])) / 2.0;
260 quat[1] = sqrt(max(0., 1 + mat[0] - mat[4] - mat[8])) / 2.0;
261 quat[2] = sqrt(max(0., 1 - mat[0] + mat[4] - mat[8])) / 2.0;
262 quat[3] = sqrt(max(0., 1 - mat[0] - mat[4] + mat[8])) / 2.0;
264 quat[1] = quat[1]*
Sign(mat[7] - mat[5]);
265 quat[2] = quat[2]*
Sign(mat[2] - mat[6]);
266 quat[3] = quat[3]*
Sign(mat[3] - mat[1]);
271 void Rotation::EulToQuat(
const double *eul,
double *quat)
273 double heading =
PI*eul[0]/180.0;
274 double attitude =
PI*eul[1]/180.0;
275 double bank =
PI*eul[2]/180.0;
277 double c1 = cos(heading/2.0);
278 double s1 = sin(heading/2.0);
279 double c2 = cos(attitude/2.0);
280 double s2 = sin(attitude/2.0);
281 double c3 = cos(bank/2.0);
282 double s3 = sin(bank/2.0);
286 quat[0] = c1c2*c3 - s1s2*s3;
287 quat[1] = c1c2*s3 + s1s2*c3;
288 quat[2] = s1*c2*c3 + c1*s2*s3;
289 quat[3] = c1*s2*c3 - s1*c2*s3;
294 void Rotation::SetQuaternion(CvMat *mat)
296 cvCopy(mat, &quaternion_mat);
297 QuatNorm(quaternion);
300 void Rotation::SetQuaternion(
const double *quat)
302 quaternion[0] = quat[0];
303 quaternion[1] = quat[1];
304 quaternion[2] = quat[2];
305 quaternion[3] = quat[3];
306 QuatNorm(quaternion);
309 void Rotation::SetEuler(
const CvMat *mat)
311 EulToQuat(mat->data.db, quaternion);
314 void Rotation::SetRodriques(
const CvMat *mat)
317 RodToMat9(mat->data.db, tmp);
318 Mat9ToQuat(tmp, quaternion);
321 void Rotation::SetMatrix(
const CvMat *mat)
324 for(
int i = 0; i < 3; ++i)
325 for(
int j = 0; j < 3; ++j)
326 tmp[i*3+j] = cvmGet(mat, i, j);
328 Mat9ToQuat(tmp, quaternion);
331 void Rotation::GetMatrix(CvMat *mat)
const 333 if (mat->width == 3) {
334 QuatToMat9(quaternion, mat->data.db);
335 }
else if (mat->width == 4) {
337 QuatToMat16(quaternion, mat->data.db);
341 void Rotation::GetRodriques(CvMat *mat)
const 344 QuatToMat9(quaternion, tmp);
345 Mat9ToRod(tmp, mat->data.db);
348 void Rotation::GetEuler(CvMat *mat)
const 350 QuatToEul(quaternion, mat->data.db);
353 void Rotation::GetQuaternion(CvMat *mat)
const 355 cvCopy(&quaternion_mat, mat);
361 memcpy(quaternion, r.
quaternion, 4*
sizeof(
double));
369 memcpy(quaternion, res.
quaternion, 4*
sizeof(
double));
RotationType
Rotation can be represented in four ways: quaternion (QUAT), matrix (MAT), euler angles (EUL) and exp...
This file implements a parametrized rotation.
int ALVAR_EXPORT Sign(const C &v)
Returns the sign of a number.
Rotation operator+(const Rotation &r1, const Rotation &r2)
Rotation structure and transformations between different parameterizations.
This file defines library export definitions, version numbers and build information.