TrackPoint.cpp 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. // Include own headers
  2. #include "TrackPoint.hpp"
  3. // Include modules
  4. #include "OSGWidget.hpp"
  5. TrackPoint::TrackPoint(const osg::Vec3 point, const osg::Vec3 normal, const osg::Vec3 normalModifier, const float normalRotation, const bool compensation) {
  6. _origin = point;
  7. _normal = normal;
  8. _normalModifier = normalModifier;
  9. _normalRotation = normalRotation;
  10. _compensation = compensation;
  11. }
  12. osg::Vec3 TrackPoint::getTranslation() {
  13. return _origin;
  14. }
  15. osg::Vec3 TrackPoint::getRotation() {
  16. osg::Vec3 start = osg::Vec3(0.0f, 0.0f, 1.0f);
  17. osg::Matrix modifierRotation = osg::Matrix::rotate(_normalModifier.x() * M_PI / 180, osg::Vec3(1.0f, 0.0f, 0.0f), _normalModifier.y() * M_PI / 180, osg::Vec3(0.0f, 1.0f, 0.0f), _normalModifier.z() * M_PI / 180, osg::Vec3(0.0f, 0.0f, 1.0f));
  18. osg::Vec3 finalNormal = modifierRotation.preMult(_normal);
  19. // From https://en.wikipedia.org/wiki/Conversion_between_quaternions_and_Euler_angles
  20. osg::Quat quat = osg::Quat(0.0f, 0.0f, 0.0f, 0.0f);
  21. quat.makeRotate(start, finalNormal);
  22. osg::Matrix matrix;
  23. quat.get(matrix);
  24. matrix = matrix.operator*(osg::Matrix::rotate(_normalRotation * M_PI / 180, finalNormal));
  25. quat.set(matrix);
  26. float sinr_cosp = 2 * (quat.w() * quat.x() + quat.y() * quat.z());
  27. float cosr_cosp = 1 - 2 * (quat.x() * quat.x() + quat.y() * quat.y());
  28. float xRotation = std::atan2(sinr_cosp, cosr_cosp) * 180.0 / M_PI;
  29. float sinp = 2 * (quat.w() * quat.y() - quat.z() * quat.x());
  30. float yRotation;
  31. if (std::abs(sinp) >= 1) {
  32. yRotation = std::copysign(M_PI / 2, sinp) * 180.0 / M_PI;
  33. } else {
  34. yRotation = std::asin(sinp) * 180.0 / M_PI;
  35. }
  36. float siny_cosp = 2 * (quat.w() * quat.z() + quat.x() * quat.y());
  37. float cosy_cosp = 1 - 2 * (quat.y() * quat.y() + quat.z() * quat.z());
  38. float zRotation = std::atan2(siny_cosp, cosy_cosp) * 180.0 / M_PI;
  39. return osg::Vec3(xRotation, yRotation, zRotation);
  40. }
  41. osg::Vec3 TrackPoint::getNormal() {
  42. return _normal;
  43. }
  44. osg::Vec3 TrackPoint::getNormalModifier() {
  45. return _normalModifier;
  46. }
  47. osg::Vec3 TrackPoint::getTrackPoint() {
  48. return _trackOrigin;
  49. }
  50. float TrackPoint::getNormalRotation() {
  51. return _normalRotation;
  52. }
  53. bool TrackPoint::getCompensation() {
  54. return _compensation;
  55. }
  56. void TrackPoint::updateNormalModifier(osg::Vec3 normalModifier) {
  57. _normalModifier = normalModifier;
  58. }
  59. void TrackPoint::updatePositions(osg::Vec3 origin) {
  60. _origin = origin;
  61. }
  62. void TrackPoint::updateNormalRotation(float normalRotation) {
  63. _normalRotation = normalRotation;
  64. }
  65. void TrackPoint::updateCompensation(bool compensation) {
  66. _compensation = compensation;
  67. }