Berkeley SfM
pose.cpp
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: David Fridovich-Keil ( dfk@eecs.berkeley.edu )
35  * Erik Nelson ( eanelson@eecs.berkeley.edu )
36  */
37 
38 #include <pose/pose.h>
39 
40 #include <iostream>
41 
42 namespace bsfm {
43 
44 // Initialize to the identity.
46  Rt_ = Matrix4d::Identity();
47 }
48 
49 // Construct a new Pose from a rotation matrix and translation vector.
50 Pose::Pose(const Matrix3d& R, const Vector3d& t) {
51  Rt_ = Matrix4d::Identity();
52  Rt_.block(0, 0, 3, 3) = R;
53  Rt_.col(3).head(3) = t;
54 }
55 
56 // Construct a new Pose from a de-homogenized 3x4 [R | t] matrix.
57 Pose::Pose(const Matrix34d& Rt) {
58  Rt_ = Matrix4d::Identity();
59  Rt_.block(0, 0, 3, 4) = Rt;
60 }
61 
62 // Construct a new Pose from a 4x4 Rt matrix.
63 Pose::Pose(const Matrix4d& Rt) {
64  Rt_ << Rt;
65 }
66 
67 // Deep copy constructor.
68 Pose::Pose(const Pose& other) {
69  Rt_ << other.Rt_;
70 }
71 
72 // Access operators delegate to the Eigen access operators.
73 double& Pose::operator()(int i, int j) {
74  return Rt_(i, j);
75 }
76 
77 const double& Pose::operator()(int i, int j) const {
78  return Rt_(i, j);
79 }
80 
81 // Access the homogeneous transformation matrix.
82 Matrix4d& Pose::Get() {
83  return Rt_;
84 }
85 
86 const Matrix4d& Pose::Get() const {
87  return Rt_;
88 }
89 
90 // Get the transformation's rotation components.
91 Matrix3d Pose::Rotation() const {
92  return Rt_.block(0, 0, 3, 3);
93 }
94 
95 // Get the transformation's translation components.
96 Vector3d Pose::Translation() const {
97  return Rt_.block(0, 3, 3, 1);
98 }
99 
100 // Set the homogeneous transformation matrix.
101 void Pose::Set(const Matrix4d& transformation) {
102  Rt_ = transformation;
103 }
104 
105 // Set rotation and translation directly.
106 void Pose::SetRotation(const Matrix3d& rotation) {
107  Rt_.block(0, 0, 3, 3) = rotation;
108 }
109 
110 void Pose::SetTranslation(const Vector3d& translation) {
111  Rt_.block(0, 3, 3, 1) = translation;
112 }
113 
114 // Overloaded multiplication operator for composing two poses.
115 Pose Pose::operator*(const Pose& other) const {
116  Matrix4d Rt_out = Rt_ * other.Rt_;
117  return Pose(Rt_out);
118 }
119 
120 // Multiply a homgenized point into a Pose.
121 Vector4d Pose::Project(const Vector4d& pt3d) {
122  Vector4d proj = Rt_ * pt3d;
123  return proj;
124 }
125 
126 // Project a 3D point into this Pose.
127 Vector2d Pose::ProjectTo2D(const Vector3d& pt3d) {
128  Vector4d pt3d_h = Vector4d::Constant(1.0);
129  pt3d_h.head(3) = pt3d;
130 
131  const Vector4d proj_h = Rt_ * pt3d_h;
132  Vector2d proj = proj_h.head(2);
133  proj /= proj_h(2);
134 
135  return proj;
136 }
137 
138 // Test if this pose (Rt_ only) is approximately equal to another pose.
139 bool Pose::IsApprox(const Pose& other) const {
140  return Rt_.isApprox(other.Rt_);
141 }
142 
143 // Compose this Pose with the given pose so that both refer to the identity Pose as
144 // specified by the given Pose.
145 void Pose::Compose(const Pose& other) {
146  Rt_ *= other.Rt_;
147 }
148 
149 // Invert this Pose.
151  Matrix4d Rt_inverse = Rt_.inverse();
152  Pose out(Rt_inverse);
153  return out;
154 }
155 
156 // Extract just the extrinsics matrix as a 3x4 matrix.
158  return Rt_.block(0, 0, 3, 4);
159 }
160 
161 // Convert to axis-angle representation.
162 Vector3d Pose::AxisAngle() {
163  // From https://en.wikipedia.org/wiki/Axis-angle-representation.
164  const double angle = acos(0.5 * (Rt_.trace() - 2.0));
165  Vector3d axis = Vector3d(Rt_(2, 1) - Rt_(1, 2),
166  Rt_(0, 2) - Rt_(2, 0),
167  Rt_(1, 0) - Rt_(0, 1)) * 0.5 / sin(angle);
168 
169  axis /= axis.norm();
170  return axis * angle;
171 }
172 
173 // Convert to matrix representation.
174 Matrix4d Pose::FromAxisAngle(const Vector3d& aa) {
175  // From https://en.wikipedia.org/wiki/Rotation_matrix.
176  double angle = aa.norm();
177  Vector3d axis = aa / angle;
178 
179  Matrix3d cross;
180  cross <<
181  0.0, -axis(2), axis(1),
182  axis(2), 0.0, -axis(0),
183  -axis(1), axis(0), 0.0;
184 
185  Matrix3d tensor;
186  tensor <<
187  axis(0)*axis(0), axis(0)*axis(1), axis(0)*axis(2),
188  axis(0)*axis(1), axis(1)*axis(1), axis(1)*axis(2),
189  axis(0)*axis(2), axis(1)*axis(2), axis(2)*axis(2);
190 
191  Matrix3d R =
192  cos(angle) * Matrix3d::Identity() +
193  sin(angle) * cross + (1-cos(angle)) * tensor;
194  Rt_.block(0, 0, 3, 3) = R;
195 
196  return Rt_;
197 }
198 
199 void Pose::SetX(double x) {
200  Rt_(0, 3) = x;
201 }
202 
203 void Pose::SetY(double y) {
204  Rt_(1, 3) = y;
205 }
206 
207 void Pose::SetZ(double z) {
208  Rt_(2, 3) = z;
209 }
210 
211 const double& Pose::X() const {
212  return Rt_(0, 3);
213 }
214 
215 const double& Pose::Y() const {
216  return Rt_(1, 3);
217 }
218 
219 const double& Pose::Z() const {
220  return Rt_(2, 3);
221 }
222 
223 double& Pose::MutableX() {
224  return Rt_(0, 3);
225 }
226 
227 double& Pose::MutableY() {
228  return Rt_(1, 3);
229 }
230 
231 double& Pose::MutableZ() {
232  return Rt_(2, 3);
233 }
234 
235 void Pose::TranslateX(double dx) {
236  Rt_(0, 3) += dx;
237 }
238 
239 void Pose::TranslateY(double dy) {
240  Rt_(1, 3) += dy;
241 }
242 
243 void Pose::TranslateZ(double dz) {
244  Rt_(2, 3) += dz;
245 }
246 
247 // Print matrix entries.
248 void Pose::Print(const std::string& prefix) const {
249  if (!prefix.empty()) {
250  std::cout << prefix << std::endl;
251  }
252  std::cout << Rt_ << std::endl;
253 }
254 
255 // Get the relative transformation from this Pose to another one.
256 Pose Pose::Delta(const Pose& rhs) const {
257  return this->Inverse() * rhs;
258 }
259 
260 } // namespace bsfm
void SetZ(double z)
Definition: pose.cpp:207
Matrix4d & Get()
Definition: pose.cpp:82
Vector3d Translation() const
Definition: pose.cpp:96
Pose operator*(const Pose &other) const
Definition: pose.cpp:115
void SetY(double y)
Definition: pose.cpp:203
void SetTranslation(const Vector3d &translation)
Definition: pose.cpp:110
void SetX(double x)
Definition: pose.cpp:199
Matrix3d Rotation() const
Definition: pose.cpp:91
void Set(const Matrix4d &transformation)
Definition: pose.cpp:101
double & MutableZ()
Definition: pose.cpp:231
Pose Inverse() const
Definition: pose.cpp:150
Vector2d ProjectTo2D(const Vector3d &)
Definition: pose.cpp:127
Vector4d Project(const Vector4d &)
Definition: pose.cpp:121
bool IsApprox(const Pose &) const
Definition: pose.cpp:139
const double & X() const
Definition: pose.cpp:211
void TranslateY(double dy)
Definition: pose.cpp:239
const double & Z() const
Definition: pose.cpp:219
void Print(const std::string &prefix=std::string()) const
Definition: pose.cpp:248
double & MutableY()
Definition: pose.cpp:227
void TranslateZ(double dz)
Definition: pose.cpp:243
Definition: camera.cpp:50
void SetRotation(const Matrix3d &rotation)
Definition: pose.cpp:106
void Compose(const Pose &other)
Definition: pose.cpp:145
::Eigen::Matrix< double, 3, 4 > Matrix34d
Definition: types.h:87
Vector3d AxisAngle()
Definition: pose.cpp:162
EIGEN_MAKE_ALIGNED_OPERATOR_NEW Pose()
Definition: pose.cpp:45
double & operator()(int i, int j)
Definition: pose.cpp:73
Matrix34d Dehomogenize()
Definition: pose.cpp:157
double & MutableX()
Definition: pose.cpp:223
Matrix4d FromAxisAngle(const Vector3d &aa)
Definition: pose.cpp:174
void TranslateX(double dx)
Definition: pose.cpp:235
Pose Delta(const Pose &rhs) const
Definition: pose.cpp:256
Matrix4d Rt_
Definition: pose.h:152
const double & Y() const
Definition: pose.cpp:215