Event.cpp 3.04 KB
Newer Older
1
2
3
4
#include "Event.hpp"

using namespace corryvreckan;

5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
double Event::start() const {
    return timestamp();
}

double Event::end() const {
    return end_;
}

double Event::duration() const {
    return (end_ - timestamp());
}

void Event::addTrigger(uint32_t trigger_id, double trigger_ts) {
    trigger_list_.emplace(trigger_id, trigger_ts);
}

bool Event::hasTriggerID(uint32_t trigger_id) const {
    return (trigger_list_.find(trigger_id) != trigger_list_.end());
}

double Event::getTriggerTime(uint32_t trigger_id) const {
    return trigger_list_.find(trigger_id)->second;
}

std::map<uint32_t, double> Event::triggerList() const {
    return trigger_list_;
}

33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
Event::Position Event::getTimestampPosition(double timestamp) const {
    if(timestamp < start()) {
        return Position::BEFORE;
    } else if(end() < timestamp) {
        return Position::AFTER;
    } else {
        return Position::DURING;
    }
}

Event::Position Event::getFramePosition(double frame_start, double frame_end, bool inclusive) const {
    // The frame is ill-defined, we have no idea what to do with this data:
    if(frame_end < frame_start) {
        return Position::UNKNOWN;
    }

    if(inclusive) {
        // Return DURING if there is any overlap
        if(frame_end < start()) {
            return Position::BEFORE;
        } else if(end() < frame_start) {
            return Position::AFTER;
        } else {
            return Position::DURING;
        }
    } else {
        // Return DURING only if fully covered
        if(frame_start < start()) {
            return Position::BEFORE;
        } else if(end() < frame_end) {
            return Position::AFTER;
        } else {
            return Position::DURING;
        }
    }
}

Event::Position Event::getTriggerPosition(uint32_t trigger_id) const {
    // If we have no triggers we cannot make a statement:
    if(trigger_list_.empty()) {
        return Position::UNKNOWN;
    }

    if(hasTriggerID(trigger_id)) {
        return Position::DURING;
    } else if(trigger_list_.upper_bound(trigger_id) == trigger_list_.begin()) {
        // Upper bound returns first element that is greater than the given key - in this case, the first map element
        // is greater than the provided trigger number - which consequently is before the event.
        return Position::BEFORE;
    } else if(trigger_list_.lower_bound(trigger_id) == trigger_list_.end()) {
        // Lower bound returns the first element that is *not less* than the given key - in this case, even the last
        // map element is less than the trigger number - which consequently is after the event.
        return Position::AFTER;
    } else {
        // We have not enough information to provide position information.
        return Position::UNKNOWN;
    }
}

92
93
94
95
96
97
98
99
100
101
void Event::print(std::ostream& out) const {
    out << "Start: " << start() << std::endl;
    out << "End:   " << end();
    if(!trigger_list_.empty()) {
        out << std::endl << "Trigger list: ";
        for(auto& trg : trigger_list_) {
            out << std::endl << trg.first << ": " << trg.second;
        }
    }
}