Range.cc 2.27 KB
/*
 * Range.cc
 *
 *  Created on: 17 déc. 2013
 *      Author: CS
 */

#include "Range.hh"
#include "ParamData.hh"
#include "iostream"

namespace plot {

Range extendRange(Range pRange) {
	// Check in term of value which is the maximum value
	// and which is the minimum.

	double lMin = pRange.getMin();
	double lMax = pRange.getMax();
	if(lMin < lMax) {
		if ( (lMin < 1 && lMin > -1) &&
			(lMax < 1 && lMax > -1) ) {
			lMin = extend2LowerValue(lMin);
			lMax = extend2UpperValue(lMax);
		} else {
			lMax = ceil(lMax);
			lMin = floor(lMin);
		}
	} else {
		if ( (lMin < 1 && lMin > -1) &&
			(lMax < 1 && lMax > -1) ) {
			lMin = extend2UpperValue(lMin);
			lMax = extend2LowerValue(lMax);
		} else {
			lMin = ceil(lMin);
			lMax = floor(lMax);
		}
	}

	Range lRange (lMin, lMax);
	lRange._extend = pRange._extend;
	return lRange;
}

void fixRange(Range &pRange, bool isLogScale) {
	if (isNAN(pRange.getMin()) && isNAN(pRange.getMax())) {
		if (isLogScale) {
			pRange.setMin(1.);
			pRange.setMax(10.);
		}
		else {
			pRange.setMin(0.);
			pRange.setMax(1.);
		}
	}
	else if (isNAN(pRange.getMin())) {
		if (isLogScale) {
			pRange.setMin(pRange.getMax() / 10.);
		}
		else {
			pRange.setMin(pRange.getMax() - 1.);
		}
	}
	else if (isNAN(pRange.getMax())) {
		if (isLogScale) {
			pRange.setMax(pRange.getMin() * 10.);
		}
		else {
			pRange.setMax(pRange.getMin() + 1.);
		}
	}
	else if (isApproximatelyEqual(pRange.getMin(),pRange.getMax())) {
		pRange.setMin((int)pRange.getMin());
		pRange.setMax((int)pRange.getMin()+1);
	}
}

double extend2UpperValue(double lValue) {
	// this give -0.0035 => 0.0035 => -2.4... => -3
	int lExp;
	if(lValue > 0) {
		lExp = floor(log10(lValue));
	} else {
		lExp = floor(log10(-1 * lValue));
	}
	// -0.0035 => -3.5
	double lTmpValue = lValue * pow(10, abs(lExp));
	// -3.5 => -3
	lTmpValue = ceil(lTmpValue);

	// -3 => -0.003
	lValue = lTmpValue * pow(10, lExp);

	return lValue;
}

double extend2LowerValue(double lValue) {
	// this give -0.0035 => 0.0035 => -2.4... => -3
	int lExp;
	if(lValue > 0) {
		lExp = floor(log10(lValue));
	} else {
		lExp = floor(log10(-1 * lValue));
	}
	// -0.0035 => -3.5
	double lTmpValue = lValue * pow(10, abs(lExp));
	// -3.5 => -4
	lTmpValue = floor(lTmpValue);

	// -4 => -0.004
	lValue = lTmpValue * pow(10, lExp);

	return lValue;
}

}