diff --git a/core/include/Data/DataSeriesUtils.h b/core/include/Data/DataSeriesUtils.h index a8a4785..7be374d 100644 --- a/core/include/Data/DataSeriesUtils.h +++ b/core/include/Data/DataSeriesUtils.h @@ -32,10 +32,39 @@ struct SCIQLOP_CORE_EXPORT DataSeriesUtils { * => xAxisData = [0, 1, 3, 5, 7, 9, 11, 13, 14 ] * => valuesData = [0, 1, 2, 3, NaN, NaN, 4, 5, 6, 7, NaN, NaN, NaN, NaN, NaN, NaN, 8, 9] * + * It is also possible to set bounds for the data series. If these bounds are defined and exceed + * the limits of the data series, data holes are added to the series at the beginning and/or the + * end. + * + * The generation of data holes at the beginning/end of the data series is performed starting + * from the x-axis series limit and adding data holes at each 'resolution step' as long as the + * new bound is not reached. + * + * For example, with : + * - xAxisData = [3, 4, 5, 6, 7 ] + * - valuesData = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] + * - fillValue = NaN + * - minBound = 0 + * - maxBound = 12 + * - and resolution = 2; + * + * => Starting from 3 and decreasing 2 by 2 until reaching 0 : a data hole at value 1 will be + * added to the beginning of the series + * => Starting from 7 and increasing 2 by 2 until reaching 12 : data holes at values 9 and 11 + * will be added to the end of the series + * + * So : + * => xAxisData = [1, 3, 4, 5, 6, 7, 9, 11 ] + * => valuesData = [NaN, NaN, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, NaN, NaN, NaN, NaN] + * * @param xAxisData the x-axis data of the data series * @param valuesData the values data of the data series * @param resolution the resoultion (on x-axis) used to determinate data holes * @param fillValue the fill value used for data holes in the values data + * @param minBound the limit at which to start filling data holes for the series. If set to NaN, + * the limit is not used + * @param maxBound the limit at which to end filling data holes for the series. If set to NaN, + * the limit is not used * * @remarks There is no control over the consistency between x-axis data and values data. The * method considers that the data is well formed (the total number of values data is a multiple @@ -43,7 +72,9 @@ struct SCIQLOP_CORE_EXPORT DataSeriesUtils { */ static void fillDataHoles(std::vector &xAxisData, std::vector &valuesData, double resolution, - double fillValue = std::numeric_limits::quiet_NaN()); + double fillValue = std::numeric_limits::quiet_NaN(), + double minBound = std::numeric_limits::quiet_NaN(), + double maxBound = std::numeric_limits::quiet_NaN()); }; #endif // SCIQLOP_DATASERIESUTILS_H diff --git a/core/src/Data/DataSeriesUtils.cpp b/core/src/Data/DataSeriesUtils.cpp index c0de3ff..121e61a 100644 --- a/core/src/Data/DataSeriesUtils.cpp +++ b/core/src/Data/DataSeriesUtils.cpp @@ -3,7 +3,8 @@ Q_LOGGING_CATEGORY(LOG_DataSeriesUtils, "DataSeriesUtils") void DataSeriesUtils::fillDataHoles(std::vector &xAxisData, std::vector &valuesData, - double resolution, double fillValue) + double resolution, double fillValue, double minBound, + double maxBound) { if (resolution == 0. || std::isnan(resolution)) { qCWarning(LOG_DataSeriesUtils()) @@ -23,7 +24,28 @@ void DataSeriesUtils::fillDataHoles(std::vector &xAxisData, std::vector< // Generates fill values that will be used to complete values data std::vector fillValues(nbComponents, fillValue); - // Traverses the x-axis data + // Checks if there are data holes on the beginning of the data and generates the hole at the + // extremity if it's the case + auto minXAxisData = xAxisData.front(); + if (!std::isnan(minBound) && minBound < minXAxisData) { + auto holeSize = static_cast((minXAxisData - minBound) / resolution); + if (holeSize > 0) { + xAxisData.insert(xAxisData.begin(), minXAxisData - holeSize * resolution); + valuesData.insert(valuesData.begin(), fillValues.begin(), fillValues.end()); + } + } + + // Same for the end of the data + auto maxXAxisData = xAxisData.back(); + if (!std::isnan(maxBound) && maxBound > maxXAxisData) { + auto holeSize = static_cast((maxBound - maxXAxisData) / resolution); + if (holeSize > 0) { + xAxisData.insert(xAxisData.end(), maxXAxisData + holeSize * resolution); + valuesData.insert(valuesData.end(), fillValues.begin(), fillValues.end()); + } + } + + // Generates other data holes auto xAxisIt = xAxisData.begin(); while (xAxisIt != xAxisData.end()) { // Stops at first value which has a gap greater than resolution with the value next to it