Files
GoldenCheetah/qwt/examples/refreshtest/CircularBuffer.cpp
Joachim Kohlhammer 49cf6340a4 Upgraded Qwt to 6.2 (branch: qwt-multiaxes) (#4427)
This commit is based on https://github.com/GoldenCheetah/GoldenCheetah/pull/3956
with the following additions / changes:
* Upgraded to the latest version of the multiaxes-branch, thus eliminating crashes of GoldenCheetah on startup
* Disabled the emitting of Layout Requests on geometry changes of QwtScaleWidget - without this, CPU utilization was up to 100% on one core
* Added the class SplineLookup, reusing small portions of code from Qwt 6.1
* Re-added the splines in WPrime and RideFile (resampling), using the new interface of QwtSpline
* Appveyor: qwt in cache-section now depends on qwt/qwtconfig.prin.in for refresh on version change
2024-01-06 18:59:55 -03:00

79 lines
1.8 KiB
C++

/*****************************************************************************
* Qwt Examples - Copyright (C) 2002 Uwe Rathmann
* This file may be used under the terms of the 3-clause BSD License
*****************************************************************************/
#include "CircularBuffer.h"
#include <QwtMath>
CircularBuffer::CircularBuffer( double interval, size_t numPoints )
: m_y( NULL )
, m_referenceTime( 0.0 )
, m_startIndex( 0 )
, m_offset( 0.0 )
{
fill( interval, numPoints );
}
void CircularBuffer::fill( double interval, size_t numPoints )
{
if ( interval <= 0.0 || numPoints < 2 )
return;
m_values.resize( numPoints );
m_values.fill( 0.0 );
if ( m_y )
{
m_step = interval / ( numPoints - 2 );
for ( size_t i = 0; i < numPoints; i++ )
m_values[i] = m_y( i * m_step );
}
m_interval = interval;
}
void CircularBuffer::setFunction( double ( * y )( double ) )
{
m_y = y;
}
void CircularBuffer::setReferenceTime( double timeStamp )
{
m_referenceTime = timeStamp;
const double startTime = std::fmod( m_referenceTime, m_values.size() * m_step );
m_startIndex = int( startTime / m_step ); // floor
m_offset = std::fmod( startTime, m_step );
}
double CircularBuffer::referenceTime() const
{
return m_referenceTime;
}
size_t CircularBuffer::size() const
{
return m_values.size();
}
QPointF CircularBuffer::sample( size_t i ) const
{
const int size = m_values.size();
int index = m_startIndex + i;
if ( index >= size )
index -= size;
const double x = i * m_step - m_offset - m_interval;
const double y = m_values.data()[index];
return QPointF( x, y );
}
QRectF CircularBuffer::boundingRect() const
{
return QRectF( -1.0, -m_interval, 2.0, m_interval );
}