summaryrefslogtreecommitdiff
path: root/gr-qtgui/lib/timeRasterGlobalData.cc
blob: 1b9e7a0332de8801a7120a28797d5d4c346f0167 (plain)
1
2
3
4
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
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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
/* -*- c++ -*- */
/*
 * Copyright 2012,2013 Free Software Foundation, Inc.
 *
 * This file is part of GNU Radio
 *
 * SPDX-License-Identifier: GPL-3.0-or-later
 *
 */

#ifndef TIMERASTER_GLOBAL_DATA_CPP
#define TIMERASTER_GLOBAL_DATA_CPP

#include <gnuradio/qtgui/timeRasterGlobalData.h>

#include <cmath>
#include <cstdio>

TimeRasterData::TimeRasterData(const double rows, const double cols)
#if QWT_VERSION < 0x060000
    : QwtRasterData(QwtDoubleRect(0, 0, rows, cols))
#else
    : QwtRasterData()
#endif
{
    d_nitems = 0;
    d_rows = rows;
    d_cols = cols;
    d_resid = 0;

    // We add 1 here so we always have the next row already started
    // (helps when d_cols is fractional and we have to slide).
    d_totalitems = static_cast<int>((d_rows + 1) * floor(d_cols));

    d_intensityRange = QwtDoubleInterval(0.0, 10.0);
    d_data.resize(d_totalitems + static_cast<int>(floor(d_cols)));

#if QWT_VERSION >= 0x060000
    setInterval(Qt::XAxis, QwtInterval(0, cols));
    setInterval(Qt::YAxis, QwtInterval(0, rows));
    setInterval(Qt::ZAxis, QwtInterval(0.0, 10.0));
#endif

    reset();
}

TimeRasterData::~TimeRasterData() {}

void TimeRasterData::reset()
{
    d_resid = 0;
    d_nitems = 0;
    std::fill(std::begin(d_data), std::end(d_data), 0.0);
}

void TimeRasterData::copy(const TimeRasterData* rhs)
{
#if QWT_VERSION < 0x060000
    if ((d_cols != rhs->getNumCols()) || (boundingRect() != rhs->boundingRect())) {
        d_cols = rhs->getNumCols();
        d_rows = rhs->getNumRows();
        d_totalitems = static_cast<int>((d_rows + 1) * floor(d_cols));
        setBoundingRect(rhs->boundingRect());
        d_data.resize(d_totalitems + static_cast<int>(floor(d_cols)));
    }
#else
    if ((d_cols != rhs->getNumCols()) || (d_rows != rhs->getNumRows())) {
        d_cols = rhs->getNumCols();
        d_rows = rhs->getNumRows();
        d_totalitems = static_cast<int>((d_rows + 1) * floor(d_cols));
        d_data.resize(d_totalitems + static_cast<int>(floor(d_cols)));
    }
#endif

    reset();

#if QWT_VERSION < 0x060000
    setRange(rhs->range());
#else
    setInterval(Qt::XAxis, rhs->interval(Qt::XAxis));
    setInterval(Qt::YAxis, rhs->interval(Qt::YAxis));
    setInterval(Qt::ZAxis, rhs->interval(Qt::ZAxis));
#endif
}

void TimeRasterData::resizeData(const double rows, const double cols)
{
#if QWT_VERSION < 0x060000
    if ((cols != getNumCols()) || (boundingRect().width() != cols) ||
        (rows != getNumRows()) || (boundingRect().height() != cols)) {
        setBoundingRect(QwtDoubleRect(0, 0, cols, rows));
        d_cols = cols;
        d_rows = rows;
        d_totalitems = static_cast<int>((d_rows + 1) * floor(d_cols));
        d_data.resize(d_totalitems + static_cast<int>(floor(d_cols)));
    }

#else
    if ((cols != getNumCols()) || (interval(Qt::XAxis).width() != cols) ||
        (rows != getNumRows()) || (interval(Qt::YAxis).width() != rows)) {

        setInterval(Qt::XAxis, QwtInterval(0, cols));
        setInterval(Qt::YAxis, QwtInterval(0, rows));

        d_cols = cols;
        d_rows = rows;
        d_totalitems = static_cast<int>((d_rows + 1) * floor(d_cols));
        d_data.resize(d_totalitems + static_cast<int>(floor(d_cols)));
    }
#endif

    reset();
}

QwtRasterData* TimeRasterData::copy() const
{
#if QWT_VERSION < 0x060000
    TimeRasterData* returnData = new TimeRasterData(d_cols, d_rows);
#else
    TimeRasterData* returnData = new TimeRasterData(d_cols, d_rows);
#endif

    returnData->copy(this);
    return returnData;
}


#if QWT_VERSION < 0x060000
QwtDoubleInterval TimeRasterData::range() const { return d_intensityRange; }

void TimeRasterData::setRange(const QwtDoubleInterval& newRange)
{
    d_intensityRange = newRange;
}

#endif


double TimeRasterData::value(double x, double y) const
{
#if QWT_VERSION < 0x060000
    double top = boundingRect().top();
    double bottom = top - boundingRect().height();
    double left = boundingRect().left();
    double right = left - boundingRect().width();
#else
    double top = interval(Qt::YAxis).maxValue();
    double bottom = interval(Qt::YAxis).minValue();
    double left = interval(Qt::XAxis).minValue();
    double right = interval(Qt::XAxis).maxValue();
#endif

    if ((x < left) || (x > right) || (y < bottom) || (y > top))
        return 0.0;

    double _y = floor(top - y);
    double _loc = _y * (d_cols) + x + d_resid;
    const auto location = static_cast<size_t>(_loc);

    if (location < d_data.size()) {
        return d_data[location];
    }

    return 0.0;
}

void TimeRasterData::incrementResidual()
{
    // After a full set of rows are drawn, we want to add up the
    // residual due to any fractional value of d_cols to appropriately
    // shift the next data in.
    double icols = floor(d_cols);
    d_resid = fmod(d_resid + (d_cols - icols) * d_rows, icols);
}

double TimeRasterData::getNumCols() const { return d_cols; }

double TimeRasterData::getNumRows() const { return d_rows; }

void TimeRasterData::addData(const double* data, const int dataSize)
{
    int cols = static_cast<int>(d_cols);
    if (dataSize == cols) {
        if (d_nitems < d_totalitems) {
            memcpy(&d_data[d_nitems], data, cols * sizeof(double));
            d_nitems += cols;
        } else {
            memcpy(&d_data[d_nitems], data, cols * sizeof(double));
            memmove(d_data.data(), &d_data[cols], d_totalitems * sizeof(double));
        }
    }
}

#endif /* TIMERASTER_GLOBAL_DATA_CPP */