TrackPoint.cpp 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  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) {
  6. _origin = point;
  7. _normal = normal;
  8. _normalModifier = normalModifier;
  9. }
  10. osg::Vec3 TrackPoint::getTranslation() {
  11. return _origin;
  12. }
  13. osg::Vec3 TrackPoint::getRotation() {
  14. osg::Vec3 start = osg::Vec3(0.0f, 0.0f, 1.0f);
  15. // From https://en.wikipedia.org/wiki/Conversion_between_quaternions_and_Euler_angles
  16. osg::Quat quat = osg::Quat(0.0f, 0.0f, 0.0f, 0.0f);
  17. quat.makeRotate(start, _normal);
  18. float sinr_cosp = 2 * (quat.w() * quat.x() + quat.y() * quat.z());
  19. float cosr_cosp = 1 - 2 * (quat.x() * quat.x() + quat.y() * quat.y());
  20. float xRotation = std::atan2(sinr_cosp, cosr_cosp) * 180.0 / M_PI;
  21. float sinp = 2 * (quat.w() * quat.y() - quat.z() * quat.x());
  22. float yRotation;
  23. if (std::abs(sinp) >= 1) {
  24. yRotation = std::copysign(M_PI / 2, sinp) * 180.0 / M_PI;
  25. } else {
  26. yRotation = std::asin(sinp) * 180.0 / M_PI;
  27. }
  28. float siny_cosp = 2 * (quat.w() * quat.z() + quat.x() * quat.y());
  29. float cosy_cosp = 1 - 2 * (quat.y() * quat.y() + quat.z() * quat.z());
  30. float zRotation = std::atan2(siny_cosp, cosy_cosp) * 180.0 / M_PI;
  31. return osg::Vec3(xRotation, yRotation, zRotation);
  32. }
  33. osg::Vec3 TrackPoint::getNormal() {
  34. return _normal;
  35. }
  36. osg::Vec3 TrackPoint::getNormalModifier() {
  37. return _normalModifier;
  38. }
  39. osg::Vec3 TrackPoint::getTrackPoint() {
  40. return _trackOrigin;
  41. }