blob: a0530dde7c15e0576cbd157fc6589e65f3c2c2a0 [file] [log] [blame]
Austin Schuh70cc9552019-01-21 19:46:48 -08001// Ceres Solver - A fast non-linear least squares minimizer
2// Copyright 2015 Google Inc. All rights reserved.
3// http://ceres-solver.org/
4//
5// Redistribution and use in source and binary forms, with or without
6// modification, are permitted provided that the following conditions are met:
7//
8// * Redistributions of source code must retain the above copyright notice,
9// this list of conditions and the following disclaimer.
10// * Redistributions in binary form must reproduce the above copyright notice,
11// this list of conditions and the following disclaimer in the documentation
12// and/or other materials provided with the distribution.
13// * Neither the name of Google Inc. nor the names of its contributors may be
14// used to endorse or promote products derived from this software without
15// specific prior written permission.
16//
17// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
18// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20// ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
21// LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23// SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25// CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27// POSSIBILITY OF SUCH DAMAGE.
28//
29// Author: keir@google.com (Keir Mierle)
30// sameeragarwal@google.com (Sameer Agarwal)
31//
32// Templated functions for manipulating rotations. The templated
33// functions are useful when implementing functors for automatic
34// differentiation.
35//
36// In the following, the Quaternions are laid out as 4-vectors, thus:
37//
38// q[0] scalar part.
39// q[1] coefficient of i.
40// q[2] coefficient of j.
41// q[3] coefficient of k.
42//
43// where: i*i = j*j = k*k = -1 and i*j = k, j*k = i, k*i = j.
44
45#ifndef CERES_PUBLIC_ROTATION_H_
46#define CERES_PUBLIC_ROTATION_H_
47
48#include <algorithm>
49#include <cmath>
50#include <limits>
51
52namespace ceres {
53
54// Trivial wrapper to index linear arrays as matrices, given a fixed
55// column and row stride. When an array "T* array" is wrapped by a
56//
57// (const) MatrixAdapter<T, row_stride, col_stride> M"
58//
59// the expression M(i, j) is equivalent to
60//
61// arrary[i * row_stride + j * col_stride]
62//
63// Conversion functions to and from rotation matrices accept
64// MatrixAdapters to permit using row-major and column-major layouts,
65// and rotation matrices embedded in larger matrices (such as a 3x4
66// projection matrix).
67template <typename T, int row_stride, int col_stride>
68struct MatrixAdapter;
69
70// Convenience functions to create a MatrixAdapter that treats the
71// array pointed to by "pointer" as a 3x3 (contiguous) column-major or
72// row-major matrix.
73template <typename T>
74MatrixAdapter<T, 1, 3> ColumnMajorAdapter3x3(T* pointer);
75
76template <typename T>
77MatrixAdapter<T, 3, 1> RowMajorAdapter3x3(T* pointer);
78
79// Convert a value in combined axis-angle representation to a quaternion.
80// The value angle_axis is a triple whose norm is an angle in radians,
81// and whose direction is aligned with the axis of rotation,
82// and quaternion is a 4-tuple that will contain the resulting quaternion.
83// The implementation may be used with auto-differentiation up to the first
84// derivative, higher derivatives may have unexpected results near the origin.
85template<typename T>
86void AngleAxisToQuaternion(const T* angle_axis, T* quaternion);
87
88// Convert a quaternion to the equivalent combined axis-angle representation.
89// The value quaternion must be a unit quaternion - it is not normalized first,
90// and angle_axis will be filled with a value whose norm is the angle of
91// rotation in radians, and whose direction is the axis of rotation.
92// The implementation may be used with auto-differentiation up to the first
93// derivative, higher derivatives may have unexpected results near the origin.
94template<typename T>
95void QuaternionToAngleAxis(const T* quaternion, T* angle_axis);
96
97// Conversions between 3x3 rotation matrix (in column major order) and
98// quaternion rotation representations. Templated for use with
99// autodifferentiation.
100template <typename T>
101void RotationMatrixToQuaternion(const T* R, T* quaternion);
102
103template <typename T, int row_stride, int col_stride>
104void RotationMatrixToQuaternion(
105 const MatrixAdapter<const T, row_stride, col_stride>& R,
106 T* quaternion);
107
108// Conversions between 3x3 rotation matrix (in column major order) and
109// axis-angle rotation representations. Templated for use with
110// autodifferentiation.
111template <typename T>
112void RotationMatrixToAngleAxis(const T* R, T* angle_axis);
113
114template <typename T, int row_stride, int col_stride>
115void RotationMatrixToAngleAxis(
116 const MatrixAdapter<const T, row_stride, col_stride>& R,
117 T* angle_axis);
118
119template <typename T>
120void AngleAxisToRotationMatrix(const T* angle_axis, T* R);
121
122template <typename T, int row_stride, int col_stride>
123void AngleAxisToRotationMatrix(
124 const T* angle_axis,
125 const MatrixAdapter<T, row_stride, col_stride>& R);
126
127// Conversions between 3x3 rotation matrix (in row major order) and
128// Euler angle (in degrees) rotation representations.
129//
130// The {pitch,roll,yaw} Euler angles are rotations around the {x,y,z}
131// axes, respectively. They are applied in that same order, so the
132// total rotation R is Rz * Ry * Rx.
133template <typename T>
134void EulerAnglesToRotationMatrix(const T* euler, int row_stride, T* R);
135
136template <typename T, int row_stride, int col_stride>
137void EulerAnglesToRotationMatrix(
138 const T* euler,
139 const MatrixAdapter<T, row_stride, col_stride>& R);
140
141// Convert a 4-vector to a 3x3 scaled rotation matrix.
142//
143// The choice of rotation is such that the quaternion [1 0 0 0] goes to an
144// identity matrix and for small a, b, c the quaternion [1 a b c] goes to
145// the matrix
146//
147// [ 0 -c b ]
148// I + 2 [ c 0 -a ] + higher order terms
149// [ -b a 0 ]
150//
151// which corresponds to a Rodrigues approximation, the last matrix being
152// the cross-product matrix of [a b c]. Together with the property that
153// R(q1 * q2) = R(q1) * R(q2) this uniquely defines the mapping from q to R.
154//
155// No normalization of the quaternion is performed, i.e.
156// R = ||q||^2 * Q, where Q is an orthonormal matrix
157// such that det(Q) = 1 and Q*Q' = I
158//
159// WARNING: The rotation matrix is ROW MAJOR
160template <typename T> inline
161void QuaternionToScaledRotation(const T q[4], T R[3 * 3]);
162
163template <typename T, int row_stride, int col_stride> inline
164void QuaternionToScaledRotation(
165 const T q[4],
166 const MatrixAdapter<T, row_stride, col_stride>& R);
167
168// Same as above except that the rotation matrix is normalized by the
169// Frobenius norm, so that R * R' = I (and det(R) = 1).
170//
171// WARNING: The rotation matrix is ROW MAJOR
172template <typename T> inline
173void QuaternionToRotation(const T q[4], T R[3 * 3]);
174
175template <typename T, int row_stride, int col_stride> inline
176void QuaternionToRotation(
177 const T q[4],
178 const MatrixAdapter<T, row_stride, col_stride>& R);
179
180// Rotates a point pt by a quaternion q:
181//
182// result = R(q) * pt
183//
184// Assumes the quaternion is unit norm. This assumption allows us to
185// write the transform as (something)*pt + pt, as is clear from the
186// formula below. If you pass in a quaternion with |q|^2 = 2 then you
187// WILL NOT get back 2 times the result you get for a unit quaternion.
188template <typename T> inline
189void UnitQuaternionRotatePoint(const T q[4], const T pt[3], T result[3]);
190
191// With this function you do not need to assume that q has unit norm.
192// It does assume that the norm is non-zero.
193template <typename T> inline
194void QuaternionRotatePoint(const T q[4], const T pt[3], T result[3]);
195
196// zw = z * w, where * is the Quaternion product between 4 vectors.
197template<typename T> inline
198void QuaternionProduct(const T z[4], const T w[4], T zw[4]);
199
200// xy = x cross y;
201template<typename T> inline
202void CrossProduct(const T x[3], const T y[3], T x_cross_y[3]);
203
204template<typename T> inline
205T DotProduct(const T x[3], const T y[3]);
206
207// y = R(angle_axis) * x;
208template<typename T> inline
209void AngleAxisRotatePoint(const T angle_axis[3], const T pt[3], T result[3]);
210
211// --- IMPLEMENTATION
212
213template<typename T, int row_stride, int col_stride>
214struct MatrixAdapter {
215 T* pointer_;
216 explicit MatrixAdapter(T* pointer)
217 : pointer_(pointer)
218 {}
219
220 T& operator()(int r, int c) const {
221 return pointer_[r * row_stride + c * col_stride];
222 }
223};
224
225template <typename T>
226MatrixAdapter<T, 1, 3> ColumnMajorAdapter3x3(T* pointer) {
227 return MatrixAdapter<T, 1, 3>(pointer);
228}
229
230template <typename T>
231MatrixAdapter<T, 3, 1> RowMajorAdapter3x3(T* pointer) {
232 return MatrixAdapter<T, 3, 1>(pointer);
233}
234
235template<typename T>
236inline void AngleAxisToQuaternion(const T* angle_axis, T* quaternion) {
237 const T& a0 = angle_axis[0];
238 const T& a1 = angle_axis[1];
239 const T& a2 = angle_axis[2];
240 const T theta_squared = a0 * a0 + a1 * a1 + a2 * a2;
241
242 // For points not at the origin, the full conversion is numerically stable.
243 if (theta_squared > T(0.0)) {
244 const T theta = sqrt(theta_squared);
245 const T half_theta = theta * T(0.5);
246 const T k = sin(half_theta) / theta;
247 quaternion[0] = cos(half_theta);
248 quaternion[1] = a0 * k;
249 quaternion[2] = a1 * k;
250 quaternion[3] = a2 * k;
251 } else {
252 // At the origin, sqrt() will produce NaN in the derivative since
253 // the argument is zero. By approximating with a Taylor series,
254 // and truncating at one term, the value and first derivatives will be
255 // computed correctly when Jets are used.
256 const T k(0.5);
257 quaternion[0] = T(1.0);
258 quaternion[1] = a0 * k;
259 quaternion[2] = a1 * k;
260 quaternion[3] = a2 * k;
261 }
262}
263
264template<typename T>
265inline void QuaternionToAngleAxis(const T* quaternion, T* angle_axis) {
266 const T& q1 = quaternion[1];
267 const T& q2 = quaternion[2];
268 const T& q3 = quaternion[3];
269 const T sin_squared_theta = q1 * q1 + q2 * q2 + q3 * q3;
270
271 // For quaternions representing non-zero rotation, the conversion
272 // is numerically stable.
273 if (sin_squared_theta > T(0.0)) {
274 const T sin_theta = sqrt(sin_squared_theta);
275 const T& cos_theta = quaternion[0];
276
277 // If cos_theta is negative, theta is greater than pi/2, which
278 // means that angle for the angle_axis vector which is 2 * theta
279 // would be greater than pi.
280 //
281 // While this will result in the correct rotation, it does not
282 // result in a normalized angle-axis vector.
283 //
284 // In that case we observe that 2 * theta ~ 2 * theta - 2 * pi,
285 // which is equivalent saying
286 //
287 // theta - pi = atan(sin(theta - pi), cos(theta - pi))
288 // = atan(-sin(theta), -cos(theta))
289 //
290 const T two_theta =
291 T(2.0) * ((cos_theta < 0.0)
292 ? atan2(-sin_theta, -cos_theta)
293 : atan2(sin_theta, cos_theta));
294 const T k = two_theta / sin_theta;
295 angle_axis[0] = q1 * k;
296 angle_axis[1] = q2 * k;
297 angle_axis[2] = q3 * k;
298 } else {
299 // For zero rotation, sqrt() will produce NaN in the derivative since
300 // the argument is zero. By approximating with a Taylor series,
301 // and truncating at one term, the value and first derivatives will be
302 // computed correctly when Jets are used.
303 const T k(2.0);
304 angle_axis[0] = q1 * k;
305 angle_axis[1] = q2 * k;
306 angle_axis[2] = q3 * k;
307 }
308}
309
310template <typename T>
311void RotationMatrixToQuaternion(const T* R, T* angle_axis) {
312 RotationMatrixToQuaternion(ColumnMajorAdapter3x3(R), angle_axis);
313}
314
315// This algorithm comes from "Quaternion Calculus and Fast Animation",
316// Ken Shoemake, 1987 SIGGRAPH course notes
317template <typename T, int row_stride, int col_stride>
318void RotationMatrixToQuaternion(
319 const MatrixAdapter<const T, row_stride, col_stride>& R,
320 T* quaternion) {
321 const T trace = R(0, 0) + R(1, 1) + R(2, 2);
322 if (trace >= 0.0) {
323 T t = sqrt(trace + T(1.0));
324 quaternion[0] = T(0.5) * t;
325 t = T(0.5) / t;
326 quaternion[1] = (R(2, 1) - R(1, 2)) * t;
327 quaternion[2] = (R(0, 2) - R(2, 0)) * t;
328 quaternion[3] = (R(1, 0) - R(0, 1)) * t;
329 } else {
330 int i = 0;
331 if (R(1, 1) > R(0, 0)) {
332 i = 1;
333 }
334
335 if (R(2, 2) > R(i, i)) {
336 i = 2;
337 }
338
339 const int j = (i + 1) % 3;
340 const int k = (j + 1) % 3;
341 T t = sqrt(R(i, i) - R(j, j) - R(k, k) + T(1.0));
342 quaternion[i + 1] = T(0.5) * t;
343 t = T(0.5) / t;
344 quaternion[0] = (R(k, j) - R(j, k)) * t;
345 quaternion[j + 1] = (R(j, i) + R(i, j)) * t;
346 quaternion[k + 1] = (R(k, i) + R(i, k)) * t;
347 }
348}
349
350// The conversion of a rotation matrix to the angle-axis form is
351// numerically problematic when then rotation angle is close to zero
352// or to Pi. The following implementation detects when these two cases
353// occurs and deals with them by taking code paths that are guaranteed
354// to not perform division by a small number.
355template <typename T>
356inline void RotationMatrixToAngleAxis(const T* R, T* angle_axis) {
357 RotationMatrixToAngleAxis(ColumnMajorAdapter3x3(R), angle_axis);
358}
359
360template <typename T, int row_stride, int col_stride>
361void RotationMatrixToAngleAxis(
362 const MatrixAdapter<const T, row_stride, col_stride>& R,
363 T* angle_axis) {
364 T quaternion[4];
365 RotationMatrixToQuaternion(R, quaternion);
366 QuaternionToAngleAxis(quaternion, angle_axis);
367 return;
368}
369
370template <typename T>
371inline void AngleAxisToRotationMatrix(const T* angle_axis, T* R) {
372 AngleAxisToRotationMatrix(angle_axis, ColumnMajorAdapter3x3(R));
373}
374
375template <typename T, int row_stride, int col_stride>
376void AngleAxisToRotationMatrix(
377 const T* angle_axis,
378 const MatrixAdapter<T, row_stride, col_stride>& R) {
379 static const T kOne = T(1.0);
380 const T theta2 = DotProduct(angle_axis, angle_axis);
381 if (theta2 > T(std::numeric_limits<double>::epsilon())) {
382 // We want to be careful to only evaluate the square root if the
383 // norm of the angle_axis vector is greater than zero. Otherwise
384 // we get a division by zero.
385 const T theta = sqrt(theta2);
386 const T wx = angle_axis[0] / theta;
387 const T wy = angle_axis[1] / theta;
388 const T wz = angle_axis[2] / theta;
389
390 const T costheta = cos(theta);
391 const T sintheta = sin(theta);
392
393 R(0, 0) = costheta + wx*wx*(kOne - costheta);
394 R(1, 0) = wz*sintheta + wx*wy*(kOne - costheta);
395 R(2, 0) = -wy*sintheta + wx*wz*(kOne - costheta);
396 R(0, 1) = wx*wy*(kOne - costheta) - wz*sintheta;
397 R(1, 1) = costheta + wy*wy*(kOne - costheta);
398 R(2, 1) = wx*sintheta + wy*wz*(kOne - costheta);
399 R(0, 2) = wy*sintheta + wx*wz*(kOne - costheta);
400 R(1, 2) = -wx*sintheta + wy*wz*(kOne - costheta);
401 R(2, 2) = costheta + wz*wz*(kOne - costheta);
402 } else {
403 // Near zero, we switch to using the first order Taylor expansion.
404 R(0, 0) = kOne;
405 R(1, 0) = angle_axis[2];
406 R(2, 0) = -angle_axis[1];
407 R(0, 1) = -angle_axis[2];
408 R(1, 1) = kOne;
409 R(2, 1) = angle_axis[0];
410 R(0, 2) = angle_axis[1];
411 R(1, 2) = -angle_axis[0];
412 R(2, 2) = kOne;
413 }
414}
415
416template <typename T>
417inline void EulerAnglesToRotationMatrix(const T* euler,
418 const int row_stride_parameter,
419 T* R) {
420 EulerAnglesToRotationMatrix(euler, RowMajorAdapter3x3(R));
421}
422
423template <typename T, int row_stride, int col_stride>
424void EulerAnglesToRotationMatrix(
425 const T* euler,
426 const MatrixAdapter<T, row_stride, col_stride>& R) {
427 const double kPi = 3.14159265358979323846;
428 const T degrees_to_radians(kPi / 180.0);
429
430 const T pitch(euler[0] * degrees_to_radians);
431 const T roll(euler[1] * degrees_to_radians);
432 const T yaw(euler[2] * degrees_to_radians);
433
434 const T c1 = cos(yaw);
435 const T s1 = sin(yaw);
436 const T c2 = cos(roll);
437 const T s2 = sin(roll);
438 const T c3 = cos(pitch);
439 const T s3 = sin(pitch);
440
441 R(0, 0) = c1*c2;
442 R(0, 1) = -s1*c3 + c1*s2*s3;
443 R(0, 2) = s1*s3 + c1*s2*c3;
444
445 R(1, 0) = s1*c2;
446 R(1, 1) = c1*c3 + s1*s2*s3;
447 R(1, 2) = -c1*s3 + s1*s2*c3;
448
449 R(2, 0) = -s2;
450 R(2, 1) = c2*s3;
451 R(2, 2) = c2*c3;
452}
453
454template <typename T> inline
455void QuaternionToScaledRotation(const T q[4], T R[3 * 3]) {
456 QuaternionToScaledRotation(q, RowMajorAdapter3x3(R));
457}
458
459template <typename T, int row_stride, int col_stride> inline
460void QuaternionToScaledRotation(
461 const T q[4],
462 const MatrixAdapter<T, row_stride, col_stride>& R) {
463 // Make convenient names for elements of q.
464 T a = q[0];
465 T b = q[1];
466 T c = q[2];
467 T d = q[3];
468 // This is not to eliminate common sub-expression, but to
469 // make the lines shorter so that they fit in 80 columns!
470 T aa = a * a;
471 T ab = a * b;
472 T ac = a * c;
473 T ad = a * d;
474 T bb = b * b;
475 T bc = b * c;
476 T bd = b * d;
477 T cc = c * c;
478 T cd = c * d;
479 T dd = d * d;
480
481 R(0, 0) = aa + bb - cc - dd; R(0, 1) = T(2) * (bc - ad); R(0, 2) = T(2) * (ac + bd); // NOLINT
482 R(1, 0) = T(2) * (ad + bc); R(1, 1) = aa - bb + cc - dd; R(1, 2) = T(2) * (cd - ab); // NOLINT
483 R(2, 0) = T(2) * (bd - ac); R(2, 1) = T(2) * (ab + cd); R(2, 2) = aa - bb - cc + dd; // NOLINT
484}
485
486template <typename T> inline
487void QuaternionToRotation(const T q[4], T R[3 * 3]) {
488 QuaternionToRotation(q, RowMajorAdapter3x3(R));
489}
490
491template <typename T, int row_stride, int col_stride> inline
492void QuaternionToRotation(const T q[4],
493 const MatrixAdapter<T, row_stride, col_stride>& R) {
494 QuaternionToScaledRotation(q, R);
495
496 T normalizer = q[0]*q[0] + q[1]*q[1] + q[2]*q[2] + q[3]*q[3];
497 normalizer = T(1) / normalizer;
498
499 for (int i = 0; i < 3; ++i) {
500 for (int j = 0; j < 3; ++j) {
501 R(i, j) *= normalizer;
502 }
503 }
504}
505
506template <typename T> inline
507void UnitQuaternionRotatePoint(const T q[4], const T pt[3], T result[3]) {
508 const T t2 = q[0] * q[1];
509 const T t3 = q[0] * q[2];
510 const T t4 = q[0] * q[3];
511 const T t5 = -q[1] * q[1];
512 const T t6 = q[1] * q[2];
513 const T t7 = q[1] * q[3];
514 const T t8 = -q[2] * q[2];
515 const T t9 = q[2] * q[3];
516 const T t1 = -q[3] * q[3];
517 result[0] = T(2) * ((t8 + t1) * pt[0] + (t6 - t4) * pt[1] + (t3 + t7) * pt[2]) + pt[0]; // NOLINT
518 result[1] = T(2) * ((t4 + t6) * pt[0] + (t5 + t1) * pt[1] + (t9 - t2) * pt[2]) + pt[1]; // NOLINT
519 result[2] = T(2) * ((t7 - t3) * pt[0] + (t2 + t9) * pt[1] + (t5 + t8) * pt[2]) + pt[2]; // NOLINT
520}
521
522template <typename T> inline
523void QuaternionRotatePoint(const T q[4], const T pt[3], T result[3]) {
524 // 'scale' is 1 / norm(q).
525 const T scale = T(1) / sqrt(q[0] * q[0] +
526 q[1] * q[1] +
527 q[2] * q[2] +
528 q[3] * q[3]);
529
530 // Make unit-norm version of q.
531 const T unit[4] = {
532 scale * q[0],
533 scale * q[1],
534 scale * q[2],
535 scale * q[3],
536 };
537
538 UnitQuaternionRotatePoint(unit, pt, result);
539}
540
541template<typename T> inline
542void QuaternionProduct(const T z[4], const T w[4], T zw[4]) {
543 zw[0] = z[0] * w[0] - z[1] * w[1] - z[2] * w[2] - z[3] * w[3];
544 zw[1] = z[0] * w[1] + z[1] * w[0] + z[2] * w[3] - z[3] * w[2];
545 zw[2] = z[0] * w[2] - z[1] * w[3] + z[2] * w[0] + z[3] * w[1];
546 zw[3] = z[0] * w[3] + z[1] * w[2] - z[2] * w[1] + z[3] * w[0];
547}
548
549// xy = x cross y;
550template<typename T> inline
551void CrossProduct(const T x[3], const T y[3], T x_cross_y[3]) {
552 x_cross_y[0] = x[1] * y[2] - x[2] * y[1];
553 x_cross_y[1] = x[2] * y[0] - x[0] * y[2];
554 x_cross_y[2] = x[0] * y[1] - x[1] * y[0];
555}
556
557template<typename T> inline
558T DotProduct(const T x[3], const T y[3]) {
559 return (x[0] * y[0] + x[1] * y[1] + x[2] * y[2]);
560}
561
562template<typename T> inline
563void AngleAxisRotatePoint(const T angle_axis[3], const T pt[3], T result[3]) {
564 const T theta2 = DotProduct(angle_axis, angle_axis);
565 if (theta2 > T(std::numeric_limits<double>::epsilon())) {
566 // Away from zero, use the rodriguez formula
567 //
568 // result = pt costheta +
569 // (w x pt) * sintheta +
570 // w (w . pt) (1 - costheta)
571 //
572 // We want to be careful to only evaluate the square root if the
573 // norm of the angle_axis vector is greater than zero. Otherwise
574 // we get a division by zero.
575 //
576 const T theta = sqrt(theta2);
577 const T costheta = cos(theta);
578 const T sintheta = sin(theta);
579 const T theta_inverse = T(1.0) / theta;
580
581 const T w[3] = { angle_axis[0] * theta_inverse,
582 angle_axis[1] * theta_inverse,
583 angle_axis[2] * theta_inverse };
584
585 // Explicitly inlined evaluation of the cross product for
586 // performance reasons.
587 const T w_cross_pt[3] = { w[1] * pt[2] - w[2] * pt[1],
588 w[2] * pt[0] - w[0] * pt[2],
589 w[0] * pt[1] - w[1] * pt[0] };
590 const T tmp =
591 (w[0] * pt[0] + w[1] * pt[1] + w[2] * pt[2]) * (T(1.0) - costheta);
592
593 result[0] = pt[0] * costheta + w_cross_pt[0] * sintheta + w[0] * tmp;
594 result[1] = pt[1] * costheta + w_cross_pt[1] * sintheta + w[1] * tmp;
595 result[2] = pt[2] * costheta + w_cross_pt[2] * sintheta + w[2] * tmp;
596 } else {
597 // Near zero, the first order Taylor approximation of the rotation
598 // matrix R corresponding to a vector w and angle w is
599 //
600 // R = I + hat(w) * sin(theta)
601 //
602 // But sintheta ~ theta and theta * w = angle_axis, which gives us
603 //
604 // R = I + hat(w)
605 //
606 // and actually performing multiplication with the point pt, gives us
607 // R * pt = pt + w x pt.
608 //
609 // Switching to the Taylor expansion near zero provides meaningful
610 // derivatives when evaluated using Jets.
611 //
612 // Explicitly inlined evaluation of the cross product for
613 // performance reasons.
614 const T w_cross_pt[3] = { angle_axis[1] * pt[2] - angle_axis[2] * pt[1],
615 angle_axis[2] * pt[0] - angle_axis[0] * pt[2],
616 angle_axis[0] * pt[1] - angle_axis[1] * pt[0] };
617
618 result[0] = pt[0] + w_cross_pt[0];
619 result[1] = pt[1] + w_cross_pt[1];
620 result[2] = pt[2] + w_cross_pt[2];
621 }
622}
623
624} // namespace ceres
625
626#endif // CERES_PUBLIC_ROTATION_H_