💿🐜 Antkeeper source code https://antkeeper.com
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

85 lines
2.1 KiB

  1. /*
  2. * Copyright (C) 2021 Christopher J. Howard
  3. *
  4. * This file is part of Antkeeper source code.
  5. *
  6. * Antkeeper source code is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation, either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * Antkeeper source code is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with Antkeeper source code. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. #ifndef ANTKEEPER_PHYSICS_ORBIT_TRAJECTORY_HPP
  20. #define ANTKEEPER_PHYSICS_ORBIT_TRAJECTORY_HPP
  21. #include "math/polynomial.hpp"
  22. #include "math/vector.hpp"
  23. #include <vector>
  24. namespace physics {
  25. namespace orbit {
  26. /**
  27. * Describes the trajectory of an orbit with Chebyshev polynomials.
  28. *
  29. * @tparam t Real type.
  30. */
  31. template <class T>
  32. struct trajectory
  33. {
  34. /// Start time of the trajectory.
  35. T t0;
  36. /// End time of the trajectory.
  37. T t1;
  38. /// Time step duration.
  39. T dt;
  40. /// Chebyshev polynomial degree.
  41. std::size_t n;
  42. /// Chebyshev polynomial coefficients.
  43. std::vector<T> a;
  44. /**
  45. * Calculates the Cartesian position of a trajectory at a given time.
  46. *
  47. * @param t Time, on `[t0, t1)`.
  48. * @return Trajectory position at time @p t.
  49. */
  50. math::vector<T, 3> position(T t) const;
  51. };
  52. template <class T>
  53. math::vector<T, 3> trajectory<T>::position(T t) const
  54. {
  55. t -= t0;
  56. std::size_t i = static_cast<std::size_t>(t / dt);
  57. const T* ax = &a[i * n * 3];
  58. const T* ay = ax + n;
  59. const T* az = ay + n;
  60. t = (t / dt - i) * T(2) - T(1);
  61. math::vector3<T> r;
  62. r.x() = math::polynomial::chebyshev::evaluate(ax, ay, t);
  63. r.y() = math::polynomial::chebyshev::evaluate(ay, az, t);
  64. r.z() = math::polynomial::chebyshev::evaluate(az, az + n, t);
  65. return r;
  66. }
  67. } // namespace orbit
  68. } // namespace physics
  69. #endif // ANTKEEPER_PHYSICS_ORBIT_TRAJECTORY_HPP