IDMath.hpp 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. /// @file Math utility functions used in inverse dynamics library.
  2. /// Defined here as they may not be provided by the math library.
  3. #ifndef IDMATH_HPP_
  4. #define IDMATH_HPP_
  5. #include "IDConfig.hpp"
  6. namespace btInverseDynamics {
  7. /// set all elements to zero
  8. void setZero(vec3& v);
  9. /// set all elements to zero
  10. void setZero(vecx& v);
  11. /// set all elements to zero
  12. void setZero(mat33& m);
  13. /// create a skew symmetric matrix from a vector (useful for cross product abstraction, e.g. v x a = V * a)
  14. void skew(vec3& v, mat33* result);
  15. /// return maximum absolute value
  16. idScalar maxAbs(const vecx& v);
  17. #ifndef ID_LINEAR_MATH_USE_EIGEN
  18. /// return maximum absolute value
  19. idScalar maxAbs(const vec3& v);
  20. #endif //ID_LINEAR_MATH_USE_EIGEN
  21. #if (defined BT_ID_HAVE_MAT3X)
  22. idScalar maxAbsMat3x(const mat3x& m);
  23. void setZero(mat3x&m);
  24. // define math functions on mat3x here to avoid allocations in operators.
  25. void mul(const mat33&a, const mat3x&b, mat3x* result);
  26. void add(const mat3x&a, const mat3x&b, mat3x* result);
  27. void sub(const mat3x&a, const mat3x&b, mat3x* result);
  28. #endif
  29. /// get offset vector & transform matrix from DH parameters
  30. /// TODO: add documentation
  31. void getVecMatFromDH(idScalar theta, idScalar d, idScalar a, idScalar alpha, vec3* r, mat33* T);
  32. /// Check if a 3x3 matrix is positive definite
  33. /// @param m a 3x3 matrix
  34. /// @return true if m>0, false otherwise
  35. bool isPositiveDefinite(const mat33& m);
  36. /// Check if a 3x3 matrix is positive semi definite
  37. /// @param m a 3x3 matrix
  38. /// @return true if m>=0, false otherwise
  39. bool isPositiveSemiDefinite(const mat33& m);
  40. /// Check if a 3x3 matrix is positive semi definite within numeric limits
  41. /// @param m a 3x3 matrix
  42. /// @return true if m>=-eps, false otherwise
  43. bool isPositiveSemiDefiniteFuzzy(const mat33& m);
  44. /// Determinant of 3x3 matrix
  45. /// NOTE: implemented here for portability, as determinant operation
  46. /// will be implemented differently for various matrix/vector libraries
  47. /// @param m a 3x3 matrix
  48. /// @return det(m)
  49. idScalar determinant(const mat33& m);
  50. /// Test if a 3x3 matrix satisfies some properties of inertia matrices
  51. /// @param I a 3x3 matrix
  52. /// @param index body index (for error messages)
  53. /// @param has_fixed_joint: if true, positive semi-definite matrices are accepted
  54. /// @return true if I satisfies inertia matrix properties, false otherwise.
  55. bool isValidInertiaMatrix(const mat33& I, int index, bool has_fixed_joint);
  56. /// Check if a 3x3 matrix is a valid transform (rotation) matrix
  57. /// @param m a 3x3 matrix
  58. /// @return true if m is a rotation matrix, false otherwise
  59. bool isValidTransformMatrix(const mat33& m);
  60. /// Transform matrix from parent to child frame,
  61. /// when the child frame is rotated about @param axis by @angle
  62. /// (mathematically positive)
  63. /// @param axis the axis of rotation
  64. /// @param angle rotation angle
  65. /// @param T pointer to transform matrix
  66. void bodyTParentFromAxisAngle(const vec3& axis, const idScalar& angle, mat33* T);
  67. /// Check if this is a unit vector
  68. /// @param vector
  69. /// @return true if |vector|=1 within numeric limits
  70. bool isUnitVector(const vec3& vector);
  71. /// @input a vector in R^3
  72. /// @returns corresponding spin tensor
  73. mat33 tildeOperator(const vec3& v);
  74. /// @param alpha angle in radians
  75. /// @returns transform matrix for ratation with @param alpha about x-axis
  76. mat33 transformX(const idScalar& alpha);
  77. /// @param beta angle in radians
  78. /// @returns transform matrix for ratation with @param beta about y-axis
  79. mat33 transformY(const idScalar& beta);
  80. /// @param gamma angle in radians
  81. /// @returns transform matrix for ratation with @param gamma about z-axis
  82. mat33 transformZ(const idScalar& gamma);
  83. ///calculate rpy angles (x-y-z Euler angles) from a given rotation matrix
  84. /// @param rot rotation matrix
  85. /// @returns x-y-z Euler angles
  86. vec3 rpyFromMatrix(const mat33&rot);
  87. }
  88. #endif // IDMATH_HPP_