rotation.h
上传用户:center1979
上传日期:2022-07-26
资源大小:50633k
文件大小:6k
源码类别:

OpenGL

开发平台:

Visual C++

  1. // rotation.h
  2. //
  3. // Copyright (C) 2004-2006 Chris Laurel <claurel@shatters.net>
  4. //
  5. // This program is free software; you can redistribute it and/or
  6. // modify it under the terms of the GNU General Public License
  7. // as published by the Free Software Foundation; either version 2
  8. // of the License, or (at your option) any later version.
  9. #ifndef _CELENGINE_ROTATION_H_
  10. #define _CELENGINE_ROTATION_H_
  11. #include <celmath/quaternion.h>
  12. #include <celengine/astro.h>
  13. /*! A RotationModel object describes the orientation of an object
  14.  *  over some time range.
  15.  */
  16. class RotationModel
  17. {
  18.  public:
  19.     virtual ~RotationModel() {};
  20.     /*! Return the orientation of an object in its reference frame at the
  21.      * specified time (TDB). Some rotations can be decomposed into two parts:
  22.      * a fixed or slowly varying part, and a much more rapidly varying part.
  23.      * The rotation of a planet is such an example. The rapidly varying part
  24.      * is referred to as spin; the slowly varying part determines the
  25.      * equatorial plane. When the rotation of an object can be decomposed
  26.      * in this way, the overall orientation = spin * equator. Otherwise,
  27.      * orientation = spin.
  28.      */
  29.     Quatd orientationAtTime(double tjd) const
  30.     {
  31.         return spin(tjd) * equatorOrientationAtTime(tjd);
  32.     }
  33. virtual Vec3d angularVelocityAtTime(double tjd) const;
  34.     /*! Return the orientation of the equatorial plane (normal to the primary
  35.      *  axis of rotation.) The overall orientation of the object is
  36.      *  spin * equator. If there is no primary axis of rotation, equator = 1
  37.      *  and orientation = spin.
  38.      */
  39.     virtual Quatd equatorOrientationAtTime(double /*tjd*/) const
  40.     {
  41.         return Quatd(1.0, 0.0, 0.0, 0.0);
  42.     }
  43.     /*! Return the rotation about primary axis of rotation (if there is one.)
  44.      *  The overall orientation is spin * equator. For objects without a
  45.      *  primary axis of rotation, spin *is* the orientation.
  46.      */
  47.     virtual Quatd spin(double tjd) const = 0;
  48.     virtual double getPeriod() const
  49.     {
  50.         return 0.0;
  51.     };
  52.     virtual bool isPeriodic() const
  53.     {
  54.         return false;
  55.     };
  56.     // Return the time range over which the orientation model is valid;
  57.     // if the model is always valid, begin and end should be equal.
  58.     virtual void getValidRange(double& begin, double& end) const
  59.     {
  60.         begin = 0.0;
  61.         end = 0.0;
  62.     };
  63. };
  64. /*! CachingRotationModel is an abstract base class for complicated rotation
  65.  *  models that are computationally expensive. The last calculated spin,
  66.  *  equator orientation, and angular velocity are all cached and reused in
  67.  *  order to avoid redundant calculation. Subclasses must override computeSpin(),
  68.  *  computeEquatorOrientation(), and getPeriod(). The default implementation
  69.  *  of computeAngularVelocity uses differentiation to approximate the
  70.  *  the instantaneous angular velocity. It may be overridden if there is some
  71.  *  better means to calculate the angular velocity for a specific rotation
  72.  *  model.
  73.  */
  74. class CachingRotationModel : public RotationModel
  75. {
  76.  public:
  77.     CachingRotationModel();
  78.     virtual ~CachingRotationModel();
  79.     
  80.     Quatd spin(double tjd) const;
  81.     Quatd equatorOrientationAtTime(double tjd) const;
  82.     Vec3d angularVelocityAtTime(double tjd) const;
  83.     
  84.     virtual Quatd computeEquatorOrientation(double tjd) const = 0;
  85.     virtual Quatd computeSpin(double tjd) const = 0;
  86.     virtual Vec3d computeAngularVelocity(double tjd) const;
  87.     virtual double getPeriod() const = 0;
  88.     virtual bool isPeriodic() const = 0;
  89.     
  90. private:
  91.     mutable Quatd lastSpin;
  92.     mutable Quatd lastEquator;
  93.     mutable Vec3d lastAngularVelocity;
  94.     mutable double lastTime;
  95.     mutable bool spinCacheValid;
  96.     mutable bool equatorCacheValid;
  97.     mutable bool angularVelocityCacheValid;
  98. };
  99. /*! The simplest rotation model is ConstantOrientation, which describes
  100.  *  an orientation that is fixed within a reference frame.
  101.  */
  102. class ConstantOrientation : public RotationModel
  103. {
  104.  public:
  105.     ConstantOrientation(const Quatd& q);
  106.     virtual ~ConstantOrientation();
  107.     virtual Quatd spin(double tjd) const;
  108. virtual Vec3d angularVelocityAtTime(double tjd) const;
  109.  private:
  110.     Quatd orientation;
  111. };
  112. /*! UniformRotationModel describes an object that rotates with a constant
  113.  *  angular velocity.
  114.  */
  115. class UniformRotationModel : public RotationModel
  116. {
  117.  public:
  118.     UniformRotationModel(double _period,
  119.                          float _offset,
  120.                          double _epoch,
  121.                          float _inclination,
  122.                          float _ascendingNode);
  123.     virtual ~UniformRotationModel();
  124.     virtual bool isPeriodic() const;
  125.     virtual double getPeriod() const;
  126.     virtual Quatd equatorOrientationAtTime(double tjd) const;
  127.     virtual Quatd spin(double tjd) const;
  128. virtual Vec3d angularVelocityAtTime(double tjd) const;
  129.  private:
  130.     double period;       // sidereal rotation period
  131.     float offset;        // rotation at epoch
  132.     double epoch;
  133.     float inclination;   // tilt of rotation axis w.r.t. reference plane
  134.     float ascendingNode; // longitude of ascending node of equator on the refernce plane
  135. };
  136. /*! PrecessingRotationModel describes an object with a spin axis that
  137.  *  precesses at a constant rate about some axis.
  138.  */
  139. class PrecessingRotationModel : public RotationModel
  140. {
  141.  public:
  142.     PrecessingRotationModel(double _period,
  143.                             float _offset,
  144.                             double _epoch,
  145.                             float _inclination,
  146.                             float _ascendingNode,
  147.                             double _precPeriod);
  148.     virtual ~PrecessingRotationModel();
  149.     virtual bool isPeriodic() const;
  150.     virtual double getPeriod() const;
  151.     virtual Quatd equatorOrientationAtTime(double tjd) const;
  152.     virtual Quatd spin(double tjd) const;
  153.  private:
  154.     double period;       // sidereal rotation period (in Julian days)
  155.     float offset;        // rotation at epoch
  156.     double epoch;
  157.     float inclination;   // tilt of rotation axis w.r.t. reference plane
  158.     float ascendingNode; // longitude of ascending node of equator on the refernce plane
  159.     double precessionPeriod; // period of precession (in Julian days)
  160. };
  161. #endif // _CELENGINE_ROTATION_H_