TrackPoint.cpp 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  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. osg::Vec3 finalNormal = _normal.operator+(_normalModifier);
  16. // From https://en.wikipedia.org/wiki/Conversion_between_quaternions_and_Euler_angles
  17. osg::Quat quat = osg::Quat(0.0f, 0.0f, 0.0f, 0.0f);
  18. quat.makeRotate(start, finalNormal);
  19. float sinr_cosp = 2 * (quat.w() * quat.x() + quat.y() * quat.z());
  20. float cosr_cosp = 1 - 2 * (quat.x() * quat.x() + quat.y() * quat.y());
  21. float xRotation = std::atan2(sinr_cosp, cosr_cosp) * 180.0 / M_PI;
  22. float sinp = 2 * (quat.w() * quat.y() - quat.z() * quat.x());
  23. float yRotation;
  24. if (std::abs(sinp) >= 1) {
  25. yRotation = std::copysign(M_PI / 2, sinp) * 180.0 / M_PI;
  26. } else {
  27. yRotation = std::asin(sinp) * 180.0 / M_PI;
  28. }
  29. float siny_cosp = 2 * (quat.w() * quat.z() + quat.x() * quat.y());
  30. float cosy_cosp = 1 - 2 * (quat.y() * quat.y() + quat.z() * quat.z());
  31. float zRotation = std::atan2(siny_cosp, cosy_cosp) * 180.0 / M_PI;
  32. return osg::Vec3(xRotation, yRotation, zRotation);
  33. }
  34. osg::Vec3 TrackPoint::getNormal() {
  35. return _normal;
  36. }
  37. osg::Vec3 TrackPoint::getNormalModifier() {
  38. return _normalModifier;
  39. }
  40. osg::Vec3 TrackPoint::getTrackPoint() {
  41. return _trackOrigin;
  42. }
  43. void TrackPoint::updateNormalModifier(osg::Vec3 normalModifier) {
  44. _normalModifier = normalModifier;
  45. }