OdeMidpoint.h 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. // David Eberly, Geometric Tools, Redmond WA 98052
  2. // Copyright (c) 1998-2020
  3. // Distributed under the Boost Software License, Version 1.0.
  4. // https://www.boost.org/LICENSE_1_0.txt
  5. // https://www.geometrictools.com/License/Boost/LICENSE_1_0.txt
  6. // Version: 4.0.2019.08.13
  7. #pragma once
  8. #include <Mathematics/OdeSolver.h>
  9. // The TVector template parameter allows you to create solvers with
  10. // Vector<N,Real> when the dimension N is known at compile time or
  11. // GVector<Real> when the dimension N is known at run time. Both classes
  12. // have 'int GetSize() const' that allow OdeSolver-derived classes to query
  13. // for the dimension.
  14. namespace WwiseGTE
  15. {
  16. template <typename Real, typename TVector>
  17. class OdeMidpoint : public OdeSolver<Real, TVector>
  18. {
  19. public:
  20. // Construction and destruction.
  21. virtual ~OdeMidpoint() = default;
  22. OdeMidpoint(Real tDelta, std::function<TVector(Real, TVector const&)> const& F)
  23. :
  24. OdeSolver<Real, TVector>(tDelta, F)
  25. {
  26. }
  27. // Estimate x(t + tDelta) from x(t) using dx/dt = F(t,x). You may
  28. // allow xIn and xOut to be the same object.
  29. virtual void Update(Real tIn, TVector const& xIn, Real& tOut, TVector& xOut) override
  30. {
  31. // Compute the first step.
  32. Real halfTDelta = (Real)0.5 * this->mTDelta;
  33. TVector fVector = this->mFunction(tIn, xIn);
  34. TVector xTemp = xIn + halfTDelta * fVector;
  35. // Compute the second step.
  36. Real halfT = tIn + halfTDelta;
  37. fVector = this->mFunction(halfT, xTemp);
  38. tOut = tIn + this->mTDelta;
  39. xOut = xIn + this->mTDelta * fVector;
  40. }
  41. };
  42. }