Blame view

src/ParamOutputImpl/Plot/DigitalAxis.cc 6.33 KB
fbe3c2bb   Benjamin Renard   First commit
1
2
3
4
5
6
7
8
9
10
11
12
13
14
/*
 * DigitalAxis.cc
 *
 *  Created on: 3 déc. 2013
 *      Author: CS
 */

#include "DigitalAxis.hh"
#include "PlotLogger.hh"
#include "PanelPlotOutputException.hh"

#include <sstream>
#include <iomanip>
#include <iostream>
b358ee7d   Benjamin Renard   Adapt axes labels...
15
#include "plplot/plplotP.h"
fbe3c2bb   Benjamin Renard   First commit
16
17
18
19

namespace plot {

const int DigitalAxis::MAX_DIGIT_NUMBER = 3;
fbe3c2bb   Benjamin Renard   First commit
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
const int DigitalAxis::STANDARD_FORMAT_PRECISION = 1;
const int DigitalAxis::LOG_CONSTANT = 1000000;

std::string DigitalAxis::getPlotOpt() {
	std::string options = Axis::getPlotOpt();

	// Remove m or n plplot option if tickmark not visible
	// This will prevent plplot to draw any tickmark
	if (_showTickMark == false) {
		size_t pos;
		if ((pos = options.find("m")) != std::string::npos) {
			options.replace(pos, 1, "");
		}
		if ((pos = options.find("n")) != std::string::npos) {
			options.replace(pos, 1, "");
		}
	}

	if (_scale == Scale::LOGARITHMIC) {
			options += "l";
	} else if (getFormat() == Format::SCIENTIFIC) {
		// Check format to use for label
		options += "of";
	} else {
		options += "f";
	}

	return options;
}

Range DigitalAxis::getRange() {
	Range lRange = Axis::getRange();
	try {
		if (_scale == Scale::LOGARITHMIC) {

			double min;
			if (lRange.getMin() <= 0) {
				min = log10(lRange.getMax() / LOG_CONSTANT);
			} else {
				min = log10(lRange.getMin());
			}

			double max;
			if (lRange.getMax() <= 0) {
				max = log10(lRange.getMin() / LOG_CONSTANT);
			} else {
				max = log10(lRange.getMax());
			}

			lRange.setMin(min);
			lRange.setMax(max);

			if (lRange._extend) {
				return extendRange(lRange);
			} else {
				return lRange;
			}
		} else {
			if (lRange._extend) {
				return extendRange(lRange);
			} else {
				return lRange;
			}
		}
	} catch (std::exception& e) {
		// negative value for log ... stop process
		std::stringstream lError;
		lError << "DigitalAxis::getRange : range min and max have to be positive value (" << e.what() << ")";
		BOOST_THROW_EXCEPTION(
				PanelPlotOutputException() << AMDA::ex_msg(lError.str()));
	}
}

DigitalAxis::Format DigitalAxis::getFormat() {
	double lMin = pow(10, -MAX_DIGIT_NUMBER);
	double lMax = pow(10, MAX_DIGIT_NUMBER);

	double lDesiredMin = std::abs(getRange().getMin());
	double lDesiredMax = std::abs(getRange().getMax());


	// If maximum range doesn't include desired range,
	// label need to be formatted.
	if ( (lDesiredMin != 0 && (lDesiredMin <= lMin || lDesiredMin >= lMax)) ||
		(lDesiredMax != 0 && (lDesiredMax <= lMin || lDesiredMax >= lMax)) ) {
		return DigitalAxis::Format::SCIENTIFIC;
	} else {
		return DigitalAxis::Format::STANDARD;
	}
}

std::pair<int, int> DigitalAxis::getTickMarkSize() {
	// If tickmarks are not visible return a null size
	if (_showTickMark == false) {
		return std::pair<int , int> (0,0);
	}

	std::pair<int, int> tickMarkSize(0, getTickMarkLines());

	// If TickMark width is fixed, return it !
	if (_fixedTickMarkWidth != -1) {
		tickMarkSize.first = _fixedTickMarkWidth;
		return tickMarkSize;
	}

b358ee7d   Benjamin Renard   Adapt axes labels...
125
        Range lRange(getRange());
fbe3c2bb   Benjamin Renard   First commit
126
127
128
129
	if (_scale == Scale::LOGARITHMIC) {
		tickMarkSize.first = 3;
	} else if (getFormat() == Format::SCIENTIFIC){
		// (i.e. 1.3e⁴)
b358ee7d   Benjamin Renard   Adapt axes labels...
130
131
                _scientific_format_precision = computeScientificFormatPrecision(lRange.getMin(), lRange.getMax());
		tickMarkSize.first = _scientific_format_precision + 4 + (lRange.getMin() < 0 ? 1 : 0);
fbe3c2bb   Benjamin Renard   First commit
132
	} else {
fbe3c2bb   Benjamin Renard   First commit
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
		double dMinFloor = floor(fabs(lRange.getMin()));
		double dMaxFloor = floor(fabs(lRange.getMax()));

		// Check that min and max for is not null to avoid log10(0) !
		if ((dMinFloor == 0) && (dMaxFloor == 0)) {
			char	buf1 [64], buf2 [64];
			sprintf (buf1,"%.3f", lRange.getMin());
			sprintf (buf2,"%.3f", lRange.getMax());
			tickMarkSize.first = std::max(strlen (buf1), strlen (buf2));
		} else {

			int iMinFloor = log10 (dMinFloor) + 1;
			int iMaxFloor = log10 (dMaxFloor) + 1;

			double rangeSize = lRange.getMin() - lRange.getMax();
			int precision = abs(floor(log10(fabs(rangeSize))));

			tickMarkSize.first = precision + std::max(iMinFloor, iMaxFloor) + 1;

			if (lRange.getMin() < 0 || lRange.getMax() < 0) {
				tickMarkSize.first += 1;
			}
		}
	}

	return tickMarkSize;
}

/**
 * @overrides Axis::getComputedValues
 */
double* DigitalAxis::getComputedValues(double* originalValues_, int size_, double min, double max) {

	double *computedValues = new double [size_];

	// Duplicates data
	memcpy (computedValues, originalValues_, size_ * sizeof (double));

	// Need to filter min max values ?
46ca92d0   Elena.Budnik   add / move header...
172
	if ((std::isnan(min) == false) || (std::isnan(max) == false)) {
fbe3c2bb   Benjamin Renard   First commit
173
174
175
176
177
178
179
180
		for (int i = 0; i < size_; ++i) {
			if ((originalValues_[i] < min) || (originalValues_[i] > max)) {
				computedValues [i] = nan("");
			}
		}
	}

	// if scale is logarithmic, just calculate log for each value
381f60f0   Benjamin Renard   Fix bug with loga...
181
	if ((_scale == Scale::LOGARITHMIC) && !_isZAxis) {
fbe3c2bb   Benjamin Renard   First commit
182
183
184
185
186
187
188
189
190
191
192
193
194
195
		for (int i = 0; i < size_; ++i) {
			if (computedValues[i] <= 0) {
				computedValues [i] = log10(std::max(min, max) / DigitalAxis::LOG_CONSTANT);
				LOG4CXX_WARN(gLogger, "Negative value found, no log calculated");

			} else {
				computedValues [i] = log10(computedValues[i]);
			}
		}
	}

	return computedValues;
}

b358ee7d   Benjamin Renard   Adapt axes labels...
196
197
198
void generateDigitalLabel(PLINT /*axis*/, PLFLT value, char *label, PLINT length, PLPointer data) {
    DigitalAxis *pAxis = (DigitalAxis *)data;
    getDigitalLabel(value, pAxis->getScientificFormatPrecision(), label, length);
fbe3c2bb   Benjamin Renard   First commit
199
200
201
202
203
204
205
}

void generateNoDigitalLabel(PLINT /*axis*/, PLFLT /*value*/, char *label, PLINT /*length*/, PLPointer /*data*/) {
	// Builds an empty Time label
	label [0] = 0;
}

b358ee7d   Benjamin Renard   Adapt axes labels...
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
int computeScientificFormatPrecision(double min, double max) {
    int precision = 1;
    
    int lMinExp = floor(log10(fabs(min)));
    int lMaxExp = floor(log10(fabs(max)));
    
    if (lMaxExp != lMinExp) {
        return precision;
    }
    
    PLFLT tick = 0;
    PLINT nsubt = 0;
    pldtik( min, max, &tick, &nsubt, false);
    
    double v = tick / pow(10, lMinExp);
    
    precision = ceil(fabs(log10(v)));
    
    return precision;
}

void getDigitalLabel(double value, int precision, char* label, int length) {
    if (value != 0) {
        int lExp = floor(log10(fabs(value)));
	double lValue = value / pow(10, lExp);
	if (lExp == 0)
            snprintf( label, length, "%.*f", precision, lValue);
        else
            snprintf( label, length, "%.*fe#u%i#d", precision, lValue, lExp);
    } else {
        std::stringstream lStrValue;
	lStrValue << value;
	snprintf( label, length, "%s", lStrValue.str().c_str());
    }
}

fbe3c2bb   Benjamin Renard   First commit
242
} /* namespace plot */