point_series_xy.cpp
Go to the documentation of this file.
00001 #include "point_series_xy.h"
00002 #include <cmath>
00003 #include <cstdlib>
00004 
00005 PointSeriesXY::PointSeriesXY(const PlotData *y_axis, const PlotData *x_axis):
00006     DataSeriesBase( &_cached_curve ),
00007     _x_axis(x_axis),
00008     _y_axis(y_axis),
00009     _cached_curve("")
00010 {
00011     updateCache();
00012 }
00013 
00014 PlotData::RangeTimeOpt PointSeriesXY::getVisualizationRangeX()
00015 {
00016     if( this->size() < 2 )
00017         return  PlotData::RangeTimeOpt();
00018     else{
00019         return PlotData::RangeTimeOpt( { _bounding_box.left() ,
00020                                          _bounding_box.right()} );
00021     }
00022 }
00023 
00024 nonstd::optional<QPointF> PointSeriesXY::sampleFromTime(double t)
00025 {
00026     if( _cached_curve.size() == 0 )
00027     {
00028         return {};
00029     }
00030 
00031     int index = _y_axis->getIndexFromX(t);
00032     if(index <0)
00033     {
00034         return {};
00035     }
00036     const auto& p = _cached_curve.at( size_t(index) );
00037     return QPointF(p.x, p.y);
00038 }
00039 
00040 PlotData::RangeValueOpt PointSeriesXY::getVisualizationRangeY(PlotData::RangeTime range_X)
00041 {
00042     //TODO: improve?
00043     return PlotData::RangeValueOpt( { _bounding_box.bottom(),
00044                                       _bounding_box.top() } );
00045 }
00046 
00047 bool PointSeriesXY::updateCache()
00048 {
00049     if( _x_axis == nullptr )
00050     {
00051         throw std::runtime_error("the X axis is null");
00052     }
00053 
00054     const size_t data_size =  std::min(_x_axis->size(), _y_axis->size());
00055     
00056     if(data_size == 0)
00057     {
00058         _bounding_box = QRectF();
00059         _cached_curve.clear();
00060         return true;
00061     }
00062 
00063     double min_y =( std::numeric_limits<double>::max() );
00064     double max_y =(-std::numeric_limits<double>::max() );
00065     double min_x =( std::numeric_limits<double>::max() );
00066     double max_x =(-std::numeric_limits<double>::max() );
00067 
00068     _cached_curve.resize( data_size );
00069 
00070     const double EPS = std::numeric_limits<double>::epsilon();
00071 
00072     for (size_t i=0; i<data_size; i++ )
00073     {
00074         if( Abs( _x_axis->at(i).x  - _y_axis->at(i).x ) >  EPS)
00075         {
00076             _bounding_box = QRectF();
00077             _cached_curve.clear();
00078             throw std::runtime_error("X and Y axis don't share the same time axis");
00079         }
00080 
00081         const QPointF p(_x_axis->at(i).y,
00082                         _y_axis->at(i).y );
00083 
00084         _cached_curve.at(i) = { p.x(), p.y() };
00085 
00086         min_x = std::min( min_x, p.x() );
00087         max_x = std::max( max_x, p.x() );
00088         min_y = std::min( min_y, p.y() );
00089         max_y = std::max( max_y, p.y() );
00090     }
00091 
00092     _bounding_box.setLeft(  min_x );
00093     _bounding_box.setRight( max_x );
00094     _bounding_box.setBottom( min_y );
00095     _bounding_box.setTop( max_y );
00096 
00097     return true;
00098 }


plotjuggler
Author(s): Davide Faconti
autogenerated on Wed Jul 3 2019 19:28:04