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 _nTicks; /* # 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 | _nTicks = 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, _nTicks = nTicks; |
---|
126 | } |
---|
127 | int NumTicks(void) { |
---|
128 | return _nTicks; |
---|
129 | } |
---|
130 | double GetTick(int i) { |
---|
131 | if ((i < 0) || (i >= _nTicks)) { |
---|
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 IsAutoScale(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 | char *_name; /* Name of the axis. Malloc-ed */ |
---|
190 | |
---|
191 | unsigned int _flags; |
---|
192 | |
---|
193 | char *_title; /* Title of the axis. */ |
---|
194 | |
---|
195 | double _valueMin, _valueMax; /* The limits of the data. */ |
---|
196 | |
---|
197 | double _reqMin, _reqMax; /* Requested axis bounds. Consult the |
---|
198 | * axisPtr->flags field for |
---|
199 | * Axis::CONFIG_MIN and Axis::CONFIG_MAX |
---|
200 | * to see if the requested bound have |
---|
201 | * been set. They override the |
---|
202 | * computed range of the axis |
---|
203 | * (determined by auto-scaling). */ |
---|
204 | |
---|
205 | double _min, _max; /* Smallest and largest major tick values for |
---|
206 | * the axis. If loose, the tick values lie |
---|
207 | * outside the range of data values. If |
---|
208 | * tight, they lie interior to the limits of |
---|
209 | * the data. */ |
---|
210 | |
---|
211 | double _range; /* Range of values on axis (_max - _min) */ |
---|
212 | double _scale; /* Scale factor for axis (1.0/_range) */ |
---|
213 | |
---|
214 | double _reqStep; /* If > 0.0, overrides the computed major tick |
---|
215 | * interval. Otherwise a stepsize is |
---|
216 | * automatically calculated, based upon the |
---|
217 | * range of elements mapped to the axis. The |
---|
218 | * default value is 0.0. */ |
---|
219 | |
---|
220 | Ticks _major, _minor; |
---|
221 | |
---|
222 | void LogScale(void); |
---|
223 | void LinearScale(void); |
---|
224 | bool InRange(double x); |
---|
225 | void MakeTicks(void); |
---|
226 | |
---|
227 | public: |
---|
228 | Axis(const char *name); |
---|
229 | |
---|
230 | void ResetRange(void); |
---|
231 | void FixRange(double min, double max); |
---|
232 | void SetScale(double min, double max); |
---|
233 | double Scale(void) { |
---|
234 | return _scale; |
---|
235 | } |
---|
236 | double Range(void) { |
---|
237 | return _range; |
---|
238 | } |
---|
239 | bool FirstMajor(TickIter &iter) { |
---|
240 | return _major.FirstTick(iter); |
---|
241 | } |
---|
242 | bool FirstMinor(TickIter &iter) { |
---|
243 | return _minor.FirstTick(iter); |
---|
244 | } |
---|
245 | void GetDataLimits(double min, double max); |
---|
246 | double Map(double x); |
---|
247 | double InvMap(double x); |
---|
248 | char *GetName(void) { |
---|
249 | return _name; |
---|
250 | } |
---|
251 | void SetName(const char *name) { |
---|
252 | _name = new char[strlen(name) + 1]; |
---|
253 | strcpy(_name, name); |
---|
254 | } |
---|
255 | void SetMin(double min) { |
---|
256 | _reqMin = min; // Setting to NaN resets min to "auto" |
---|
257 | } |
---|
258 | void SetMax(double min) { |
---|
259 | _reqMin = min; // Setting to NaN resets max to "auto" |
---|
260 | } |
---|
261 | void SetLimits(double min, double max) { |
---|
262 | SetMin(min), SetMax(max); |
---|
263 | } |
---|
264 | void UnsetLimits() { |
---|
265 | SetMin(_NaN), SetMax(_NaN); |
---|
266 | } |
---|
267 | void SetDescendingOption(bool value) { |
---|
268 | if (value) { |
---|
269 | _flags |= DESCENDING; |
---|
270 | } else { |
---|
271 | _flags &= ~DESCENDING; |
---|
272 | } |
---|
273 | } |
---|
274 | void SetTightMinOption(bool value) { |
---|
275 | if (value) { |
---|
276 | _flags |= TIGHT_MIN; |
---|
277 | } else { |
---|
278 | _flags &= ~TIGHT_MIN; |
---|
279 | } |
---|
280 | } |
---|
281 | void SetTightMaxOption(bool value) { |
---|
282 | if (value) { |
---|
283 | _flags |= TIGHT_MAX; |
---|
284 | } else { |
---|
285 | _flags &= ~TIGHT_MAX; |
---|
286 | } |
---|
287 | } |
---|
288 | void SetLogScaleOption(bool value) { |
---|
289 | if (value) { |
---|
290 | _flags |= LOGSCALE; |
---|
291 | } else { |
---|
292 | _flags &= ~LOGSCALE; |
---|
293 | } |
---|
294 | } |
---|
295 | void SetMajorStepOption(double value) { |
---|
296 | _reqStep = value; // Setting to 0.0 resets step to "auto" |
---|
297 | } |
---|
298 | void SetNumMinorTicksOption(int n) { |
---|
299 | _minor.reqNumTicks = n; |
---|
300 | } |
---|
301 | void SetNumMajorTicksOption(int n) { |
---|
302 | _major.reqNumTicks = n; |
---|
303 | } |
---|
304 | }; |
---|
305 | |
---|
306 | #endif /*_AXIS_H*/ |
---|