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