vnl_rotation_matrix.cxx
Go to the documentation of this file.
1 // This is core/vnl/vnl_rotation_matrix.cxx
2 #include <cmath>
3 #include "vnl_rotation_matrix.h"
4 
5 bool vnl_rotation_matrix(double const x[3], double **R)
6 {
7  // start with an identity matrix.
8  for (unsigned i=0; i<3; ++i)
9  for (unsigned j=0; j<3; ++j)
10  R[i][j] = (i==j ? 1 : 0);
11 
12  // normalize x to a unit vector u, of norm 'angle'.
13  double u[3] = {x[0], x[1], x[2]};
14  double angle = std::sqrt(u[0]*u[0] + u[1]*u[1] + u[2]*u[2]);
15  if (angle == 0)
16  return true;
17  u[0] /= angle;
18  u[1] /= angle;
19  u[2] /= angle;
20 
21  // add (cos(angle)-1)*(1 - u u').
22  double cos_angle = std::cos(angle);
23  for (unsigned i=0; i<3; ++i)
24  for (unsigned j=0; j<3; ++j)
25  R[i][j] += (cos_angle-1) * ((i==j ? 1:0) - u[i]*u[j]);
26 
27  // add sin(angle) * [u]
28  double sin_angle = std::sin(angle);
29  /* */ R[0][1] -= sin_angle*u[2]; R[0][2] += sin_angle*u[1];
30  R[1][0] += sin_angle*u[2]; /* */ R[1][2] -= sin_angle*u[0];
31  R[2][0] -= sin_angle*u[1]; R[2][1] += sin_angle*u[0]; /* */
32 
33  return true;
34 }
35 
36 bool vnl_rotation_matrix(double const axis[3], double R[3][3])
37 {
38  double *R_[3] = { R[0], R[1], R[2] };
39  return vnl_rotation_matrix(axis, R_);
40 }
41 
42 bool vnl_rotation_matrix(double const axis[3], double *R0, double *R1, double *R2)
43 {
44  double *R[3] = { R0, R1, R2 };
45  return vnl_rotation_matrix(axis, R);
46 }
47 
48 #include <vnl/vnl_vector_fixed.h>
49 #include <vnl/vnl_matrix_fixed.h>
50 
53 {
54  return vnl_rotation_matrix(&axis[0], R[0], R[1], R[2]);
55 }
56 
58 {
60  vnl_rotation_matrix(&axis[0], R[0], R[1], R[2]);
61  return R;
62 }
63 
65 {
66  return vnl_rotation_matrix(&axis[0], R.data_array());
67 }
68 
70 {
71  vnl_matrix<double> R(3, 3);
72  vnl_rotation_matrix(&axis[0], R.data_array());
73  return R;
74 }
VNL_EXPORT vnl_matrix_fixed< double, 3, 3 > vnl_rotation_matrix(vnl_vector_fixed< double, 3 > const &axis)
Returns an orthogonal 3x3 matrix which is a rotation about the axis, by an angle equal to ||axis||.
Fixed size, stack-stored, space-efficient matrix.
Definition: vnl_fwd.h:23
T const *const * data_array() const
Access the 2D array, so that elements can be accessed with array[row][col] directly.
Definition: vnl_matrix.h:609
Fixed length stack-stored, space-efficient vector.
Definition: vnl_fwd.h:22
fixed size matrix
Fixed length stack-stored vector.
T angle(const vnl_vector_fixed< T, n > &a, const vnl_vector_fixed< T, n > &b)
Functions to create a 3x3 rotation matrix.
VNL_EXPORT T cos_angle(m const &, m const &)