Berkeley SfM
rotation.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2015, The Regents of the University of California (Regents).
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are
7  * met:
8  *
9  * 1. Redistributions of source code must retain the above copyright
10  * notice, this list of conditions and the following disclaimer.
11  *
12  * 2. Redistributions in binary form must reproduce the above
13  * copyright notice, this list of conditions and the following
14  * disclaimer in the documentation and/or other materials provided
15  * with the distribution.
16  *
17  * 3. Neither the name of the copyright holder nor the names of its
18  * contributors may be used to endorse or promote products derived
19  * from this software without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS AS IS
22  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
25  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
26  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
27  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
28  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
29  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
30  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
31  * POSSIBILITY OF SUCH DAMAGE.
32  *
33  * Please contact the author(s) of this library if you have any questions.
34  * Authors: Erik Nelson ( eanelson@eecs.berkeley.edu )
35  * David Fridovich-Keil ( dfk@eecs.berkeley.edu )
36  */
37 
38 ///////////////////////////////////////////////////////////////////////////////
39 //
40 // This file defines rotation utilities that can be used to compose rotations or
41 // convert between rotation parameterizations.
42 //
43 ///////////////////////////////////////////////////////////////////////////////
44 
45 #ifndef BSFM_GEOMETRY_ROTATION_H
46 #define BSFM_GEOMETRY_ROTATION_H
47 
48 #include <Eigen/Core>
49 
50 namespace bsfm {
51 
52 using Eigen::Matrix3d;
53 using Eigen::Vector3d;
54 
55 // Convert from Euler angles to a rotation matrix. Phi, theta, and psi define the
56 // angles of the intermediate rotations about x (R_x), y (R_y), and z (R_z)
57 // respectively. See https://en.wikipedia.org/wiki/Rotation_matrix.
58 Matrix3d EulerAnglesToMatrix(double phi, double theta, double psi);
59 
60 // Same thing as above, but where phi, theta, and psi are specified as a vector.
61 Matrix3d EulerAnglesToMatrix(const Vector3d& euler_angles);
62 
63 // Convert from a rotation matrix to Euler angles.
64 // From: http://staff.city.ac.uk/~sbbh653/publications/euler.pdf
65 // Note that the solution that is returned is only unique when phi, theta, and
66 // psi are all < 0.5 * PI. If this is not the case, they will still be correct,
67 // but may not be unique!
68 Vector3d MatrixToEulerAngles(const Matrix3d& R);
69 
70 // Get roll angle from a rotation matrix.
71 // Just like above, the solution is only unique if roll < 0.5 * PI.
72 double Roll(const Matrix3d& R);
73 
74 // Get pitch angle from a rotation matrix.
75 // This solution is unique.
76 double Pitch(const Matrix3d& R);
77 
78 // Get yaw angle from a rotation matrix.
79 // Just like above, the solution is only unique if yaw < 0.5 * PI.
80 double Yaw(const Matrix3d& R);
81 
82 // Unroll an angle to be \in [0, 2*PI)
83 double Unroll(double angle);
84 
85 // Normalize an angle to be \in [-PI, PI)
86 double Normalize(double angle);
87 
88 // Computes the shortest distance between two angles on S^1.
89 // Found by manipulating the first answer on:
90 // stackoverflow.com/questions/1878907/the-smallest-difference-between-2-angles
91 double S1Distance(double from, double to);
92 
93 // Convert from degrees to radians.
94 double D2R(double angle);
95 
96 // Convert from radians to degrees.
97 double R2D(double angle);
98 
99 // An error metric between two rotation matrices on SO3.
100 double SO3Error(const Matrix3d& R1, const Matrix3d& R2);
101 
102 } //\namespace bsfm
103 
104 #endif
double Normalize(double angle)
Definition: rotation.cpp:135
double Roll(const Matrix3d &R)
Definition: rotation.cpp:104
double D2R(double angle)
Definition: rotation.cpp:153
double S1Distance(double from, double to)
Definition: rotation.cpp:145
Vector3d MatrixToEulerAngles(const Matrix3d &R)
Definition: rotation.cpp:81
double Unroll(double angle)
Definition: rotation.cpp:127
double Pitch(const Matrix3d &R)
Definition: rotation.cpp:113
double Yaw(const Matrix3d &R)
Definition: rotation.cpp:119
double R2D(double angle)
Definition: rotation.cpp:158
double SO3Error(const Matrix3d &R1, const Matrix3d &R2)
Definition: rotation.cpp:163
Definition: camera.cpp:50
Matrix3d EulerAnglesToMatrix(double phi, double theta, double psi)
Definition: rotation.cpp:49