1 | /* -*- mode: c++; c-basic-offset: 4; indent-tabs-mode: nil -*- */ |
---|
2 | #ifndef AXIS_H |
---|
3 | #define AXIS_H |
---|
4 | |
---|
5 | #include "Chain.h" |
---|
6 | |
---|
7 | class Axis; |
---|
8 | |
---|
9 | class NaN |
---|
10 | { |
---|
11 | private: |
---|
12 | double _x; |
---|
13 | public: |
---|
14 | NaN() |
---|
15 | { |
---|
16 | union DoubleValue { |
---|
17 | unsigned int words[2]; |
---|
18 | double value; |
---|
19 | } result; |
---|
20 | |
---|
21 | #ifdef WORDS_BIGENDIAN |
---|
22 | result.words[0] = 0x7ff80000; |
---|
23 | result.words[1] = 0x00000000; |
---|
24 | #else |
---|
25 | result.words[0] = 0x00000000; |
---|
26 | result.words[1] = 0x7ff80000; |
---|
27 | #endif |
---|
28 | _x = result.value; |
---|
29 | } |
---|
30 | |
---|
31 | operator double() const |
---|
32 | { |
---|
33 | return _x; |
---|
34 | } |
---|
35 | }; |
---|
36 | |
---|
37 | extern NaN _NaN; |
---|
38 | |
---|
39 | class TickIter { |
---|
40 | ChainLink *linkPtr_; |
---|
41 | public: |
---|
42 | void SetStartingLink(ChainLink *linkPtr) { |
---|
43 | linkPtr_ = linkPtr; |
---|
44 | } |
---|
45 | bool Next(void) { |
---|
46 | if (linkPtr_ == NULL) { |
---|
47 | return false; |
---|
48 | } |
---|
49 | linkPtr_ = linkPtr_->Next(); |
---|
50 | return (linkPtr_ != NULL); |
---|
51 | } |
---|
52 | float GetValue(void) { |
---|
53 | union { |
---|
54 | float x; |
---|
55 | void *clientData; |
---|
56 | } value; |
---|
57 | value.clientData = linkPtr_->GetValue(); |
---|
58 | return value.x; |
---|
59 | } |
---|
60 | }; |
---|
61 | |
---|
62 | /* |
---|
63 | * ---------------------------------------------------------------------- |
---|
64 | * |
---|
65 | * Ticks -- |
---|
66 | * |
---|
67 | * Structure containing information where the ticks (major or |
---|
68 | * minor) will be displayed on the graph. |
---|
69 | * |
---|
70 | * ---------------------------------------------------------------------- |
---|
71 | */ |
---|
72 | class Ticks { |
---|
73 | bool autoscale_; /* Indicates if the ticks are autoscaled. */ |
---|
74 | /* |
---|
75 | * Tick locations may be generated in two fashions |
---|
76 | */ |
---|
77 | /* 1. an array of values provided by the user. */ |
---|
78 | int numTicks_; /* # of ticks on axis */ |
---|
79 | float *ticks_; /* Array of tick values (alloc-ed). Right now |
---|
80 | * it's a float so we don't have to allocate |
---|
81 | * store for the list of ticks generated. In |
---|
82 | * the future when we store both the tick |
---|
83 | * label and the value in the list, we may |
---|
84 | * extend this to a double. |
---|
85 | */ |
---|
86 | /* |
---|
87 | * 2. a defined sweep of initial, step, and nsteps. This in turn |
---|
88 | * will generate the an array of values. |
---|
89 | */ |
---|
90 | double initial_; /* Initial value */ |
---|
91 | double step_; /* Size of interval */ |
---|
92 | unsigned int nSteps_; /* Number of intervals. */ |
---|
93 | |
---|
94 | /* |
---|
95 | *This finally generates a list of ticks values that exclude duplicate |
---|
96 | * minor ticks (that are always represented by major ticks). In the |
---|
97 | * future, the string representing the tick label will be stored in |
---|
98 | * the chain. |
---|
99 | */ |
---|
100 | Chain chain_; |
---|
101 | |
---|
102 | void SetTicks(void); /* Routine used internally to create the array |
---|
103 | * of ticks as defined by a given sweep. */ |
---|
104 | void *GetClientData(float x) { |
---|
105 | union { |
---|
106 | float x; |
---|
107 | void *clientData; |
---|
108 | } value; |
---|
109 | value.x = x; |
---|
110 | return value.clientData; |
---|
111 | } |
---|
112 | |
---|
113 | public: |
---|
114 | int reqNumTicks; /* Default number of ticks to be displayed. */ |
---|
115 | |
---|
116 | Ticks(int numTicks) { |
---|
117 | reqNumTicks = numTicks; |
---|
118 | ticks_ = NULL; |
---|
119 | numTicks_ = 0; |
---|
120 | autoscale_ = true; |
---|
121 | } |
---|
122 | ~Ticks(void) { |
---|
123 | if (ticks_ != NULL) { |
---|
124 | delete [] ticks_; |
---|
125 | } |
---|
126 | chain_.Reset(); |
---|
127 | |
---|
128 | } |
---|
129 | void SetTicks(float *ticks, int nTicks) { |
---|
130 | ticks_ = ticks, numTicks_ = nTicks; |
---|
131 | } |
---|
132 | int numTicks(void) { |
---|
133 | return numTicks_; |
---|
134 | } |
---|
135 | double tick(int i) { |
---|
136 | if ((i < 0) || (i >= numTicks_)) { |
---|
137 | return _NaN; |
---|
138 | } |
---|
139 | return ticks_[i]; |
---|
140 | } |
---|
141 | void Reset(void) { |
---|
142 | chain_.Reset(); |
---|
143 | } |
---|
144 | float step() { |
---|
145 | return step_; |
---|
146 | } |
---|
147 | void Append (float x) { |
---|
148 | chain_.Append(GetClientData(x)); |
---|
149 | } |
---|
150 | void SetValues(double initial, double step, unsigned int nSteps) { |
---|
151 | initial_ = initial, step_ = step, nSteps_ = nSteps; |
---|
152 | } |
---|
153 | bool autoscale(void) { |
---|
154 | return autoscale_; |
---|
155 | } |
---|
156 | void SweepTicks(void) { |
---|
157 | if (autoscale_) { |
---|
158 | if (ticks_ != NULL) { |
---|
159 | delete [] ticks_; |
---|
160 | } |
---|
161 | SetTicks(); |
---|
162 | } |
---|
163 | } |
---|
164 | bool FirstTick(TickIter &iter) { |
---|
165 | ChainLink *linkPtr; |
---|
166 | |
---|
167 | linkPtr = chain_.FirstLink(); |
---|
168 | iter.SetStartingLink(linkPtr); |
---|
169 | return (linkPtr != NULL); |
---|
170 | } |
---|
171 | }; |
---|
172 | |
---|
173 | |
---|
174 | /* |
---|
175 | * ---------------------------------------------------------------------- |
---|
176 | * |
---|
177 | * Axis -- |
---|
178 | * |
---|
179 | * Structure contains options controlling how the axis will be |
---|
180 | * displayed. |
---|
181 | * |
---|
182 | * ---------------------------------------------------------------------- |
---|
183 | */ |
---|
184 | class Axis { |
---|
185 | /* Flags associated with the axis. */ |
---|
186 | enum AxisFlags { |
---|
187 | AUTOSCALE=(1<<0), |
---|
188 | DESCENDING=(1<<1), |
---|
189 | LOGSCALE=(1<<2), |
---|
190 | TIGHT_MIN=(1<<3), |
---|
191 | TIGHT_MAX=(1<<4) |
---|
192 | }; |
---|
193 | |
---|
194 | const char *name_; /* Name of the axis. Malloc-ed */ |
---|
195 | |
---|
196 | unsigned int flags_; |
---|
197 | |
---|
198 | const char *title_; /* Title of the axis. */ |
---|
199 | |
---|
200 | const char *units_; /* Units of the axis. */ |
---|
201 | |
---|
202 | double valueMin_, valueMax_; /* The limits of the data. */ |
---|
203 | |
---|
204 | double reqMin_, reqMax_; /* Requested axis bounds. Consult the |
---|
205 | * axisPtr->flags field for |
---|
206 | * Axis::CONFIG_MIN and Axis::CONFIG_MAX |
---|
207 | * to see if the requested bound have |
---|
208 | * been set. They override the |
---|
209 | * computed range of the axis |
---|
210 | * (determined by auto-scaling). */ |
---|
211 | |
---|
212 | double min_, max_; /* Smallest and largest major tick values for |
---|
213 | * the axis. If loose, the tick values lie |
---|
214 | * outside the range of data values. If |
---|
215 | * tight, they lie interior to the limits of |
---|
216 | * the data. */ |
---|
217 | |
---|
218 | double range_; /* Range of values on axis (max_ - min_) */ |
---|
219 | double scale_; /* Scale factor for axis (1.0/_range) */ |
---|
220 | |
---|
221 | double reqStep_; /* If > 0.0, overrides the computed major tick |
---|
222 | * interval. Otherwise a stepsize is |
---|
223 | * automatically calculated, based upon the |
---|
224 | * range of elements mapped to the axis. The |
---|
225 | * default value is 0.0. */ |
---|
226 | |
---|
227 | Ticks major_, minor_; |
---|
228 | |
---|
229 | void LogScale(void); |
---|
230 | void LinearScale(void); |
---|
231 | bool InRange(double x); |
---|
232 | void MakeTicks(void); |
---|
233 | |
---|
234 | public: |
---|
235 | Axis(const char *name); |
---|
236 | ~Axis(void) { |
---|
237 | if (name_ != NULL) { |
---|
238 | free((void *)name_); |
---|
239 | } |
---|
240 | if (units_ != NULL) { |
---|
241 | free((void *)units_); |
---|
242 | } |
---|
243 | if (title_ != NULL) { |
---|
244 | free((void *)title_); |
---|
245 | } |
---|
246 | } |
---|
247 | |
---|
248 | void ResetRange(void); |
---|
249 | void FixRange(double min, double max); |
---|
250 | void SetScale(double min, double max); |
---|
251 | double scale(void) { |
---|
252 | return scale_; |
---|
253 | } |
---|
254 | double range(void) { |
---|
255 | return range_; |
---|
256 | } |
---|
257 | bool FirstMajor(TickIter &iter) { |
---|
258 | return major_.FirstTick(iter); |
---|
259 | } |
---|
260 | bool FirstMinor(TickIter &iter) { |
---|
261 | return minor_.FirstTick(iter); |
---|
262 | } |
---|
263 | void GetDataLimits(double &min, double &max) { |
---|
264 | min = valueMin_, max = valueMax_; |
---|
265 | } |
---|
266 | double Map(double x); |
---|
267 | double InvMap(double x); |
---|
268 | const char *name(void) { |
---|
269 | return name_; |
---|
270 | } |
---|
271 | void name(const char *name) { |
---|
272 | if (name_ != NULL) { |
---|
273 | free((void *)name_); |
---|
274 | } |
---|
275 | name_ = strdup(name); |
---|
276 | } |
---|
277 | const char *units(void) { |
---|
278 | return units_; |
---|
279 | } |
---|
280 | void units(const char *units) { |
---|
281 | if (units_ != NULL) { |
---|
282 | free((void *)units_); |
---|
283 | } |
---|
284 | units_ = strdup(units); |
---|
285 | } |
---|
286 | const char *title(void) { |
---|
287 | return title_; |
---|
288 | } |
---|
289 | void title(const char *title) { |
---|
290 | if (title_ != NULL) { |
---|
291 | free((void *)title_); |
---|
292 | } |
---|
293 | title_ = strdup(title); |
---|
294 | } |
---|
295 | double min(void) { |
---|
296 | return min_; |
---|
297 | } |
---|
298 | void min(double min) { |
---|
299 | reqMin_ = min; |
---|
300 | } |
---|
301 | double max(void) { |
---|
302 | return max_; |
---|
303 | } |
---|
304 | void max(double max) { |
---|
305 | reqMax_ = max; |
---|
306 | } |
---|
307 | void SetLimits(double min, double max) { |
---|
308 | reqMin_ = min, reqMax_ = max; |
---|
309 | } |
---|
310 | void UnsetLimits() { |
---|
311 | min(_NaN), max(_NaN); |
---|
312 | } |
---|
313 | void SetDescendingOption(bool value) { |
---|
314 | if (value) { |
---|
315 | flags_ |= DESCENDING; |
---|
316 | } else { |
---|
317 | flags_ &= ~DESCENDING; |
---|
318 | } |
---|
319 | } |
---|
320 | void SetTightMinOption(bool value) { |
---|
321 | if (value) { |
---|
322 | flags_ |= TIGHT_MIN; |
---|
323 | } else { |
---|
324 | flags_ &= ~TIGHT_MIN; |
---|
325 | } |
---|
326 | } |
---|
327 | void SetTightMaxOption(bool value) { |
---|
328 | if (value) { |
---|
329 | flags_ |= TIGHT_MAX; |
---|
330 | } else { |
---|
331 | flags_ &= ~TIGHT_MAX; |
---|
332 | } |
---|
333 | } |
---|
334 | void SetLogScaleOption(bool value) { |
---|
335 | if (value) { |
---|
336 | flags_ |= LOGSCALE; |
---|
337 | } else { |
---|
338 | flags_ &= ~LOGSCALE; |
---|
339 | } |
---|
340 | } |
---|
341 | void SetMajorStepOption(double value) { |
---|
342 | reqStep_ = value; // Setting to 0.0 resets step to "auto" |
---|
343 | } |
---|
344 | void SetNumMinorTicksOption(int n) { |
---|
345 | minor_.reqNumTicks = n; |
---|
346 | } |
---|
347 | void SetNumMajorTicksOption(int n) { |
---|
348 | major_.reqNumTicks = n; |
---|
349 | } |
---|
350 | }; |
---|
351 | |
---|
352 | #endif |
---|