TrackPoint.cpp 2.0 KB

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