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

OpenGL

开发平台:

Visual C++

  1. // timelinephase.cpp
  2. //
  3. // Object timeline phase
  4. //
  5. // Copyright (C) 2008, the Celestia Development Team
  6. // Initial version by Chris Laurel, claurel@gmail.com
  7. //
  8. // This program is free software; you can redistribute it and/or
  9. // modify it under the terms of the GNU General Public License
  10. // as published by the Free Software Foundation; either version 2
  11. // of the License, or (at your option) any later version.
  12. #include <cassert>
  13. #include "celengine/timelinephase.h"
  14. #include "celengine/frame.h"
  15. #include "celengine/orbit.h"
  16. #include "celengine/rotation.h"
  17. #include "celengine/universe.h"
  18. #include "celengine/frametree.h"
  19. TimelinePhase::TimelinePhase(Body* _body,
  20.                              double _startTime,
  21.                              double _endTime,
  22.                              ReferenceFrame* _orbitFrame,
  23.                              Orbit* _orbit,
  24.                              ReferenceFrame* _bodyFrame,
  25.                              RotationModel* _rotationModel,
  26.                              FrameTree* _owner) :
  27.     m_body(_body),
  28.     m_startTime(_startTime),
  29.     m_endTime(_endTime),
  30.     m_orbitFrame(_orbitFrame),
  31.     m_orbit(_orbit),
  32.     m_bodyFrame(_bodyFrame),
  33.     m_rotationModel(_rotationModel),
  34.     m_owner(_owner),
  35.     refCount(0)
  36. {
  37.     // assert(owner == orbitFrame->getCenter()->getFrameTree());
  38.     m_orbitFrame->addRef();
  39.     m_bodyFrame->addRef();
  40. }
  41. TimelinePhase::~TimelinePhase()
  42. {
  43.     m_orbitFrame->release();
  44.     m_bodyFrame->release();
  45. }
  46. // Declared private--should never be used
  47. TimelinePhase::TimelinePhase(const TimelinePhase&)
  48. {
  49.     assert(0);
  50. }
  51. // Declared private--should never be used
  52. TimelinePhase& TimelinePhase::operator=(const TimelinePhase&)
  53. {
  54.     assert(0);
  55.     return *this;
  56. }
  57. int TimelinePhase::addRef() const
  58. {
  59.     return ++refCount;
  60. }
  61. int TimelinePhase::release() const
  62. {
  63.     --refCount;
  64.     assert(refCount >= 0);
  65.     if (refCount <= 0)
  66.         delete this;
  67.     return refCount;
  68. }
  69. /*! Create a new timeline phase in the specified universe.
  70.  */
  71. TimelinePhase*
  72. TimelinePhase::CreateTimelinePhase(Universe& universe,
  73.                                    Body* body,
  74.                                    double startTime,
  75.                                    double endTime,
  76.                                    ReferenceFrame& orbitFrame,
  77.                                    Orbit& orbit,
  78.                                    ReferenceFrame& bodyFrame,
  79.                                    RotationModel& rotationModel)
  80. {
  81.     // Validate the time range.
  82.     if (endTime <= startTime)
  83.         return NULL;
  84.     // Get the frame tree to add the new phase to. Verify that the reference frame
  85.     // center is either a star or solar system body.
  86.     FrameTree* frameTree = NULL;
  87.     Selection center = orbitFrame.getCenter();
  88.     if (center.body() != NULL)
  89.     {
  90.         frameTree = center.body()->getOrCreateFrameTree();
  91.     }
  92.     else if (center.star() != NULL)
  93.     {
  94.         SolarSystem* solarSystem = universe.getSolarSystem(center.star());
  95.         if (solarSystem == NULL)
  96.         {
  97.             // No solar system defined for this star yet, so we need
  98.             // to create it.
  99.             solarSystem = universe.createSolarSystem(center.star());
  100.         }
  101.         frameTree = solarSystem->getFrameTree();
  102.     }
  103.     else
  104.     {
  105.         // Frame center is not a star or body.
  106.         return NULL;
  107.     }
  108.     TimelinePhase* phase = new TimelinePhase(body,
  109.                                              startTime,
  110.                                              endTime,
  111.                                              &orbitFrame,
  112.                                              &orbit,
  113.                                              &bodyFrame,
  114.                                              &rotationModel,
  115.                                              frameTree);
  116.     frameTree->addChild(phase);
  117.     return phase;
  118. }