Celestia

Форк
0
/
timeline.cpp 
133 строки · 2.9 Кб
1
// timeline.cpp
2
//
3
// Object timelines.
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

13
#include "celengine/timeline.h"
14
#include "celengine/timelinephase.h"
15
#include "celengine/frametree.h"
16
#include "celengine/frame.h"
17

18
using namespace std;
19

20

21
/*! A Timeline is a list of TimelinePhases that covers a continuous
22
 *  interval of time.
23
 */
24

25
Timeline::~Timeline()
26
{
27
    for (auto phase : phases)
28
    {
29
        // Remove the phase from whatever phase tree contains it.
30
        phase->getFrameTree()->removeChild(phase);
31
    }
32
}
33

34

35
bool
36
Timeline::appendPhase(TimelinePhase::SharedConstPtr &phase)
37
{
38
    // Validate start and end times. If there are existing phases in the timeline,
39
    // startTime must be equal to endTime of the previous phases so that there are
40
    // no gaps and no overlaps.
41
    if (!phases.empty())
42
    {
43
        if (phase->startTime() != phases.back()->endTime())
44
            return false;
45
    }
46

47
    phases.push_back(phase);
48

49
    return true;
50
}
51

52

53
const TimelinePhase::SharedConstPtr&
54
Timeline::findPhase(double t) const
55
{
56
    // Find the phase containing time t. The overwhelming common case is
57
    // nPhases = 1, so we special case that. Otherwise, we do a simple linear search,
58
    // as the number of phases in a timeline should always be quite small.
59
    if (phases.size() == 1)
60
    {
61
        return phases[0];
62
    }
63
    else
64
    {
65
        for (const auto& phase : phases)
66
        {
67
            if (t < phase->endTime())
68
                return phase;
69
        }
70

71
        // Time is greater than the end time of the final phase. Just return the final phase.
72
        return phases.back();
73
    }
74
}
75

76

77
/*! Get the phase at the specified index.
78
 */
79
const TimelinePhase::SharedConstPtr&
80
Timeline::getPhase(unsigned int n) const
81
{
82
    return phases.at(n);
83
}
84

85

86
/*! Get the number of phases in this timeline.
87
 */
88
unsigned int
89
Timeline::phaseCount() const
90
{
91
    return phases.size();
92
}
93

94

95
double
96
Timeline::startTime() const
97
{
98
    return phases.front()->startTime();
99
}
100

101

102
double
103
Timeline::endTime() const
104
{
105
    return phases.back()->endTime();
106
}
107

108

109
/*! Check whether the timeline covers the specified time t. True if
110
 *  startTime <= t <= endTime. Note that this is deliberately different
111
 *  than the TimelinePhase::includes function, which is only true if
112
 *  t is strictly less than the end time.
113
 */
114
bool
115
Timeline::includes(double t) const
116
{
117
    return phases.front()->startTime() <= t && t <= phases.back()->endTime();
118
}
119

120

121
void
122
Timeline::markChanged()
123
{
124
    if (phases.size() == 1)
125
    {
126
        phases[0]->getFrameTree()->markChanged();
127
    }
128
    else
129
    {
130
        for (const auto &phase : phases)
131
            phase->getFrameTree()->markChanged();
132
    }
133
}
134

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.