ParameterData.hh 5.28 KB
/*
 * ParameterData.hh
 *
 *  Created on: 10 déc. 2013
 *      Author: CS
 */

#ifndef PARAMETERDATA_HH_
#define PARAMETERDATA_HH_

#include <vector>
#include <map>
#include <ostream>
#include <string>
#include <cmath>

#include "ParameterIndexesTool.hh"

namespace plot {

/**
 * @brief Structure that holds a parameter component values.
 */
class ComponentParameterData {
public:

	ComponentParameterData();

	virtual ~ComponentParameterData();

	/**
	 * @brief Component min value.
	 */
	double _min;

	/**
	 * @brief Component min strictly positive value (use to determine range of a logarithmic axis).
	 */
	double _minStrictPos;

	/**
	 * @brief Component max value.
	 */
	double _max;

	/**
	 * @brief Adds a value and updates min and max values.
	 */
	void addValue(double value_);

	/**
	 * @brief Returns a pointer to the first component value at
	 * given index (default is 0).
	 */
	double* getValues(int index_ = 0) {
		return &_values[index_];
	}

	/**
	 * Gets number of parameter values.
	 */
	size_t getNumberOfData(){
		return _values.size();
	}

	/*
	 * @brief
	 */
	void preAllocate(int nbData);

private:

	/**
	 * @brief Values container
	 */
	std::vector<double> _values;
};

/**
 * @brief Structure that holds parameter values by component
 */
class ParameterData {

public:

	ParameterData();

	virtual ~ParameterData();

	/**
	 * @brief Adds a value by time to parameter
	 */
	void addValue(double time_, double value_, double sampling, double gapSize,
			bool &gapDetected, AMDA::Common::ParameterIndexComponent& index_);

	/**
	 * @brief Returns a pointer to the given component value
	 * (index_) since from_ index.
	 */
	double* getValues(AMDA::Common::ParameterIndexComponent index_ = AMDA::Common::ParameterIndexComponent(-1,-1), int from_ = 0) {
		return _values[componentToValuesIndex(index_)].getValues(from_);
	}

	/**
	 * @brief
	 */
	double* getIntervalValues(double startTime, double stopTime, AMDA::Common::ParameterIndexComponent index_,
			int& startIndex, int& nbData)
	{
		nbData = 0;

		if (stopTime <= startTime)
			return NULL;

		startIndex = indexOf(startTime);

		int indexOfStopTime = indexOf(stopTime);
		int lastIndex;

		if (indexOfStopTime == -1)
			lastIndex = getSize() - 1;
		else
		{
			lastIndex  = indexOfStopTime;
			if (_dates[lastIndex] > stopTime)
				lastIndex -= 1;
		}

		if (startIndex >= lastIndex)
			return NULL;

		nbData = lastIndex - startIndex + 1;

		return getValues(index_, startIndex);
	}

	/**
	 * @brief Returns minimum value of component.
	 */
	double getMin(AMDA::Common::ParameterIndexComponent index_ = AMDA::Common::ParameterIndexComponent(-1,-1)) {
		return _values[componentToValuesIndex(index_)]._min;
	}

	/**
	 * @brief Returns min strictly positive value (use to determine range of a logarithmic axis)
	 */
	double getMinStrictPos(AMDA::Common::ParameterIndexComponent index_ = AMDA::Common::ParameterIndexComponent(-1,-1)) {
		return _values[componentToValuesIndex(index_)]._minStrictPos;
	}

	/**
	 * @brief Returns maximum value of component.
	 */
	double getMax(AMDA::Common::ParameterIndexComponent index_ = AMDA::Common::ParameterIndexComponent(-1,-1)) {
		return _values[componentToValuesIndex(index_)]._max;
	}

	int getSize() {
		return _dates.size();
	}

	/**
	 * @brief Returns a pointer to the first time equals or
	 * just after a given time
	 */
	double* getTimes(double time_ = nan("")) {
		return &_dates[indexOf(time_)];
	}

	/*
	 * @brief Get dim1 size
	 */
	int getDim1Size()
	{
		return _dim1Size;
	}

	/*
	 * @brief Set dim1 size
	 */
	void setDim1Size(int dim1Size)
	{
		_dim1Size = dim1Size;
	}

	/*
	 * @brief Get dim2 size
	 */
	int getDim2Size()
	{
		return _dim2Size;
	}

	/*
	 * @brief Set dim2 size
	 */
	void setDim2Size(int dim2Size)
	{
		_dim2Size = dim2Size;
	}

	/**
	 * Gets the index of the given time (or just after) in time array,
	 * returns -1 if not found
	 */
	int indexOf(double time_);

	/**
	 * @brief Computes an interpolated value at a given time
	 */
	double getInterpolatedValue (double atTime = nan(""), AMDA::Common::ParameterIndexComponent index = AMDA::Common::ParameterIndexComponent(-1,-1));

	/*
	 * @brief Reset ParamData
	 */
	void reset()
	{
		_values.clear();
		_dates.clear();
		_minTime = nan("");
		_maxTime = nan("");
	}

	/*
	 * @brief
	 */
	void preAllocate(int nbData);

	/**
	 * Dump properties, for TU
	 */
	void dump(std::ostream&, std::string& prefix_);

	/**
	 * @brief List of component indexes, empty if scalar parameter
	 */
	std::vector<AMDA::Common::ParameterIndexComponent> _indexes;

	/**
	 * @brief Min value time
	 */
	double _minTime;

	/**
	 * @brief Max value time
	 */
	double _maxTime;

protected:
	int componentToValuesIndex(AMDA::Common::ParameterIndexComponent index)
	{
		if ((index.getDim1Index() == -1) && (index.getDim2Index() == -1))
			return 0;
		if (index.getDim2Index() == -1)
			return index.getDim1Index();
		if (index.getDim1Index() == -1)
			return index.getDim2Index();
		return index.getDim1Index() * _dim2Size + index.getDim2Index();
	}

private:

	/**
	 * @brief Values container, by component
	 */
	std::vector<ComponentParameterData> _values;

	/**
	 * @brief Dates container, synchronized with values container
	 */
	std::vector<double> _dates;

	/*
	 * @brief Dim 1 Size
	 */
	int _dim1Size;

	/*
	 * @brief Dim 2 Size
	 */
	int _dim2Size;
};


} /* namespace plot */
#endif /* PARAMETERDATA_HH_ */