HelloSO3.cpp 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738
  1. #include <iostream>
  2. #include "sophus/geometry.hpp"
  3. int main() {
  4. // The following demonstrates the group multiplication of rotation matrices
  5. // Create rotation matrices from rotations around the x and y and z axes:
  6. const double kPi = Sophus::Constants<double>::pi();
  7. Sophus::SO3d R1 = Sophus::SO3d::rotX(kPi / 4);
  8. Sophus::SO3d R2 = Sophus::SO3d::rotY(kPi / 6);
  9. Sophus::SO3d R3 = Sophus::SO3d::rotZ(-kPi / 3);
  10. std::cout << "The rotation matrices are" << std::endl;
  11. std::cout << "R1:\n" << R1.matrix() << std::endl;
  12. std::cout << "R2:\n" << R2.matrix() << std::endl;
  13. std::cout << "R3:\n" << R3.matrix() << std::endl;
  14. std::cout << "Their product R1*R2*R3:\n"
  15. << (R1 * R2 * R3).matrix() << std::endl;
  16. std::cout << std::endl;
  17. // Rotation matrices can act on vectors
  18. Eigen::Vector3d x;
  19. x << 0.0, 0.0, 1.0;
  20. std::cout << "Rotation matrices can act on vectors" << std::endl;
  21. std::cout << "x\n" << x << std::endl;
  22. std::cout << "R2*x\n" << R2 * x << std::endl;
  23. std::cout << "R1*(R2*x)\n" << R1 * (R2 * x) << std::endl;
  24. std::cout << "(R1*R2)*x\n" << (R1 * R2) * x << std::endl;
  25. std::cout << std::endl;
  26. // SO(3) are internally represented as unit quaternions.
  27. std::cout << "R1 in matrix form:\n" << R1.matrix() << std::endl;
  28. std::cout << "R1 in unit quaternion form:\n"
  29. << R1.unit_quaternion().coeffs() << std::endl;
  30. // Note that the order of coefficiences of Eigen's quaternion class is
  31. // (imag0, imag1, imag2, real)
  32. std::cout << std::endl;
  33. }