1 | |
---|
2 | #include <tcl.h> |
---|
3 | #include <Unirect.h> |
---|
4 | |
---|
5 | extern int GetFloatFromObj(Tcl_Interp *interp, Tcl_Obj *objPtr, |
---|
6 | float *valuePtr); |
---|
7 | extern int GetAxisFromObj(Tcl_Interp *interp, Tcl_Obj *objPtr, int *indexPtr); |
---|
8 | |
---|
9 | int |
---|
10 | Rappture::Unirect3d::LoadData(Tcl_Interp *interp, int objc, |
---|
11 | Tcl_Obj *const *objv) |
---|
12 | { |
---|
13 | int num[3], nValues; |
---|
14 | float min[3], max[3]; |
---|
15 | float *values; |
---|
16 | const char *units[4], *order; |
---|
17 | |
---|
18 | |
---|
19 | if ((objc & 0x01) == 0) { |
---|
20 | Tcl_AppendResult(interp, Tcl_GetString(objv[0]), ": ", |
---|
21 | "wrong number of arguments: should be key-value pairs", |
---|
22 | (char *)NULL); |
---|
23 | return TCL_ERROR; |
---|
24 | } |
---|
25 | |
---|
26 | /* Default order is z, y, x. */ |
---|
27 | int axis1, axis2, axis3; |
---|
28 | axis1 = 2; /* Z-axis */ |
---|
29 | axis2 = 1; /* Y-axis */ |
---|
30 | axis3 = 0; /* X-axis */ |
---|
31 | |
---|
32 | int extents = 1; |
---|
33 | values = NULL; |
---|
34 | num[0] = num[1] = num[2] = nValues = 0; |
---|
35 | min[0] = min[1] = min[2] = max[0] = max[1] = max[2] = 0.0f; |
---|
36 | order = units[0] = units[1] = units[2] = units[3] = NULL; |
---|
37 | |
---|
38 | int i; |
---|
39 | for (i = 1; i < objc; i += 2) { |
---|
40 | const char *string; |
---|
41 | char c; |
---|
42 | |
---|
43 | string = Tcl_GetString(objv[i]); |
---|
44 | c = string[0]; |
---|
45 | if ((c == 'x') && (strcmp(string, "xmin") == 0)) { |
---|
46 | if (GetFloatFromObj(interp, objv[i+1], min+2) != TCL_OK) { |
---|
47 | return TCL_ERROR; |
---|
48 | } |
---|
49 | } else if ((c == 'x') && (strcmp(string, "xmax") == 0)) { |
---|
50 | if (GetFloatFromObj(interp, objv[i+1], max+2) != TCL_OK) { |
---|
51 | return TCL_ERROR; |
---|
52 | } |
---|
53 | } else if ((c == 'x') && (strcmp(string, "xnum") == 0)) { |
---|
54 | if (Tcl_GetIntFromObj(interp, objv[i+1], num+2) != TCL_OK) { |
---|
55 | return TCL_ERROR; |
---|
56 | } |
---|
57 | if (num[2] <= 0) { |
---|
58 | Tcl_AppendResult(interp, "bad xnum value: must be > 0", |
---|
59 | (char *)NULL); |
---|
60 | return TCL_ERROR; |
---|
61 | } |
---|
62 | } else if ((c == 'x') && (strcmp(string, "xunits") == 0)) { |
---|
63 | units[2] = Tcl_GetString(objv[i+1]); |
---|
64 | } else if ((c == 'y') && (strcmp(string, "ymin") == 0)) { |
---|
65 | if (GetFloatFromObj(interp, objv[i+1], min+1) != TCL_OK) { |
---|
66 | return TCL_ERROR; |
---|
67 | } |
---|
68 | } else if ((c == 'y') && (strcmp(string, "ymax") == 0)) { |
---|
69 | if (GetFloatFromObj(interp, objv[i+1], max+1) != TCL_OK) { |
---|
70 | return TCL_ERROR; |
---|
71 | } |
---|
72 | } else if ((c == 'y') && (strcmp(string, "ynum") == 0)) { |
---|
73 | if (Tcl_GetIntFromObj(interp, objv[i+1], num+1) != TCL_OK) { |
---|
74 | return TCL_ERROR; |
---|
75 | } |
---|
76 | if (num[1] <= 0) { |
---|
77 | Tcl_AppendResult(interp, "bad ynum value: must be > 0", |
---|
78 | (char *)NULL); |
---|
79 | return TCL_ERROR; |
---|
80 | } |
---|
81 | } else if ((c == 'y') && (strcmp(string, "yunits") == 0)) { |
---|
82 | units[1] = Tcl_GetString(objv[i+1]); |
---|
83 | } else if ((c == 'z') && (strcmp(string, "zmin") == 0)) { |
---|
84 | if (GetFloatFromObj(interp, objv[i+1], min) != TCL_OK) { |
---|
85 | return TCL_ERROR; |
---|
86 | } |
---|
87 | } else if ((c == 'z') && (strcmp(string, "zmax") == 0)) { |
---|
88 | if (GetFloatFromObj(interp, objv[i+1], max) != TCL_OK) { |
---|
89 | return TCL_ERROR; |
---|
90 | } |
---|
91 | } else if ((c == 'z') && (strcmp(string, "znum") == 0)) { |
---|
92 | if (Tcl_GetIntFromObj(interp, objv[i+1], num) != TCL_OK) { |
---|
93 | return TCL_ERROR; |
---|
94 | } |
---|
95 | if (num[0] <= 0) { |
---|
96 | Tcl_AppendResult(interp, "bad znum value: must be > 0", |
---|
97 | (char *)NULL); |
---|
98 | return TCL_ERROR; |
---|
99 | } |
---|
100 | } else if ((c == 'z') && (strcmp(string, "zunits") == 0)) { |
---|
101 | units[0] = Tcl_GetString(objv[i+1]); |
---|
102 | } else if ((c == 'v') && (strcmp(string, "values") == 0)) { |
---|
103 | Tcl_Obj **vobj; |
---|
104 | |
---|
105 | if (Tcl_ListObjGetElements(interp, objv[i+1], &nValues, &vobj) |
---|
106 | != TCL_OK) { |
---|
107 | return TCL_ERROR; |
---|
108 | } |
---|
109 | values = new float[nValues]; |
---|
110 | int j; |
---|
111 | for (j = 0; j < nValues; j++) { |
---|
112 | if (GetFloatFromObj(interp, vobj[j], values + j)!=TCL_OK) { |
---|
113 | return TCL_ERROR; |
---|
114 | } |
---|
115 | } |
---|
116 | } else if ((c == 'u') && (strcmp(string, "units") == 0)) { |
---|
117 | vUnits_ = strdup(Tcl_GetString(objv[i+1])); |
---|
118 | } else if ((c == 'o') && (strcmp(string, "order") == 0)) { |
---|
119 | Tcl_Obj **axes; |
---|
120 | int n; |
---|
121 | |
---|
122 | if (Tcl_ListObjGetElements(interp, objv[i+1], &n, &axes) |
---|
123 | != TCL_OK) { |
---|
124 | return TCL_ERROR; |
---|
125 | } |
---|
126 | if (n != 3) { |
---|
127 | return TCL_ERROR; |
---|
128 | } |
---|
129 | if ((GetAxisFromObj(interp, axes[0], &axis1) != TCL_OK) || |
---|
130 | (GetAxisFromObj(interp, axes[1], &axis2) != TCL_OK) || |
---|
131 | (GetAxisFromObj(interp, axes[2], &axis3) != TCL_OK)) { |
---|
132 | return TCL_ERROR; |
---|
133 | } |
---|
134 | } else { |
---|
135 | Tcl_AppendResult(interp, "unknown key \"", string, |
---|
136 | (char *)NULL); |
---|
137 | return TCL_ERROR; |
---|
138 | } |
---|
139 | } |
---|
140 | if (values == NULL) { |
---|
141 | Tcl_AppendResult(interp, "missing \"values\" key", (char *)NULL); |
---|
142 | return TCL_ERROR; |
---|
143 | } |
---|
144 | if (nValues != (num[0] * num[1] * num[2] * extents)) { |
---|
145 | Tcl_AppendResult(interp, |
---|
146 | "wrong number of values: must be xnum*ynum*znum*extents", |
---|
147 | (char *)NULL); |
---|
148 | return TCL_ERROR; |
---|
149 | } |
---|
150 | |
---|
151 | fprintf(stderr, "generating %dx%dx%dx%d = %d points\n", |
---|
152 | num[0], num[1], num[2], extents, num[0]*num[1]*num[2]*extents); |
---|
153 | |
---|
154 | if ((axis1 != 2) || (axis2 != 1) || (axis3 != 0)) { |
---|
155 | // Reorder the data into x, y, z where x varies fastest and so on. |
---|
156 | int z; |
---|
157 | float *data, *dp; |
---|
158 | |
---|
159 | dp = data = new float[nValues]; |
---|
160 | for (z = 0; z < num[0]; z++) { |
---|
161 | int y; |
---|
162 | |
---|
163 | for (y = 0; y < num[1]; y++) { |
---|
164 | int x; |
---|
165 | |
---|
166 | for (x = 0; x < num[2]; x++) { |
---|
167 | int i, v; |
---|
168 | |
---|
169 | /* Compute the index from the data's described ordering. */ |
---|
170 | i = ((z*num[axis2]*num[axis3]) + (y*num[axis3]) + x) * 3; |
---|
171 | for(v = 0; v < extents; v++) { |
---|
172 | dp[v] = values[i+v]; |
---|
173 | } |
---|
174 | dp += extents; |
---|
175 | } |
---|
176 | } |
---|
177 | } |
---|
178 | delete [] values; |
---|
179 | values = data; |
---|
180 | } |
---|
181 | |
---|
182 | values_ = values; |
---|
183 | nValues_ = nValues; |
---|
184 | extents_ = extents; |
---|
185 | if (units[3] != NULL) { |
---|
186 | vUnits_ = strdup(units[3]); |
---|
187 | } |
---|
188 | xMin_ = min[axis3]; |
---|
189 | xMax_ = max[axis3]; |
---|
190 | xNum_ = num[axis3]; |
---|
191 | if (units[axis3] != NULL) { |
---|
192 | xUnits_ = strdup(units[axis3]); |
---|
193 | } |
---|
194 | yMin_ = min[axis2]; |
---|
195 | yMax_ = max[axis2]; |
---|
196 | yNum_ = num[axis2]; |
---|
197 | if (units[axis2] != NULL) { |
---|
198 | yUnits_ = strdup(units[axis2]); |
---|
199 | } |
---|
200 | zMin_ = min[axis1]; |
---|
201 | zMax_ = max[axis1]; |
---|
202 | zNum_ = num[axis1]; |
---|
203 | if (units[axis1] != NULL) { |
---|
204 | zUnits_ = strdup(units[axis1]); |
---|
205 | } |
---|
206 | initialized_ = true; |
---|
207 | return TCL_OK; |
---|
208 | } |
---|
209 | |
---|
210 | |
---|
211 | int |
---|
212 | Rappture::Unirect2d::LoadData(Tcl_Interp *interp, int objc, |
---|
213 | Tcl_Obj *const *objv) |
---|
214 | { |
---|
215 | |
---|
216 | if ((objc & 0x01) == 0) { |
---|
217 | Tcl_AppendResult(interp, Tcl_GetString(objv[0]), ": ", |
---|
218 | "wrong number of arguments: should be key-value pairs", |
---|
219 | (char *)NULL); |
---|
220 | return TCL_ERROR; |
---|
221 | } |
---|
222 | |
---|
223 | int axis[2]; |
---|
224 | axis[0] = 1; /* X-axis */ |
---|
225 | axis[1] = 0; /* Y-axis */ |
---|
226 | |
---|
227 | extents_ = 1; |
---|
228 | xNum_ = yNum_ = nValues_ = 0; |
---|
229 | xMin_ = yMin_ = xMax_ = yMax_ = 0.0f; |
---|
230 | if (xUnits_ != NULL) { |
---|
231 | free(xUnits_); |
---|
232 | } |
---|
233 | if (yUnits_ != NULL) { |
---|
234 | free(yUnits_); |
---|
235 | } |
---|
236 | if (vUnits_ != NULL) { |
---|
237 | free(vUnits_); |
---|
238 | } |
---|
239 | xUnits_ = yUnits_ = vUnits_ = NULL; |
---|
240 | if (values_ != NULL) { |
---|
241 | delete [] values_; |
---|
242 | } |
---|
243 | values_ = NULL; |
---|
244 | |
---|
245 | int i; |
---|
246 | for (i = 1; i < objc; i += 2) { |
---|
247 | const char *string; |
---|
248 | char c; |
---|
249 | |
---|
250 | string = Tcl_GetString(objv[i]); |
---|
251 | c = string[0]; |
---|
252 | if ((c == 'x') && (strcmp(string, "xmin") == 0)) { |
---|
253 | if (GetFloatFromObj(interp, objv[i+1], &xMin_) != TCL_OK) { |
---|
254 | return TCL_ERROR; |
---|
255 | } |
---|
256 | } else if ((c == 'x') && (strcmp(string, "xmax") == 0)) { |
---|
257 | if (GetFloatFromObj(interp, objv[i+1], &xMax_) != TCL_OK) { |
---|
258 | return TCL_ERROR; |
---|
259 | } |
---|
260 | } else if ((c == 'x') && (strcmp(string, "xnum") == 0)) { |
---|
261 | int n; |
---|
262 | if (Tcl_GetIntFromObj(interp, objv[i+1], &n) != TCL_OK) { |
---|
263 | return TCL_ERROR; |
---|
264 | } |
---|
265 | if (n <= 0) { |
---|
266 | Tcl_AppendResult(interp, "bad xnum value: must be > 0", |
---|
267 | (char *)NULL); |
---|
268 | return TCL_ERROR; |
---|
269 | } |
---|
270 | xNum_ = n; |
---|
271 | } else if ((c == 'x') && (strcmp(string, "xunits") == 0)) { |
---|
272 | xUnits_ = strdup(Tcl_GetString(objv[i+1])); |
---|
273 | } else if ((c == 'y') && (strcmp(string, "ymin") == 0)) { |
---|
274 | if (GetFloatFromObj(interp, objv[i+1], &yMin_) != TCL_OK) { |
---|
275 | return TCL_ERROR; |
---|
276 | } |
---|
277 | } else if ((c == 'y') && (strcmp(string, "ymax") == 0)) { |
---|
278 | if (GetFloatFromObj(interp, objv[i+1], &yMax_) != TCL_OK) { |
---|
279 | return TCL_ERROR; |
---|
280 | } |
---|
281 | } else if ((c == 'y') && (strcmp(string, "ynum") == 0)) { |
---|
282 | int n; |
---|
283 | if (Tcl_GetIntFromObj(interp, objv[i+1], &n) != TCL_OK) { |
---|
284 | return TCL_ERROR; |
---|
285 | } |
---|
286 | if (n <= 0) { |
---|
287 | Tcl_AppendResult(interp, "bad ynum value: must be > 0", |
---|
288 | (char *)NULL); |
---|
289 | return TCL_ERROR; |
---|
290 | } |
---|
291 | yNum_ = n; |
---|
292 | } else if ((c == 'y') && (strcmp(string, "yunits") == 0)) { |
---|
293 | yUnits_ = strdup(Tcl_GetString(objv[i+1])); |
---|
294 | } else if ((c == 'v') && (strcmp(string, "values") == 0)) { |
---|
295 | Tcl_Obj **vobj; |
---|
296 | int n; |
---|
297 | |
---|
298 | if (Tcl_ListObjGetElements(interp, objv[i+1], &n, &vobj) != TCL_OK){ |
---|
299 | return TCL_ERROR; |
---|
300 | } |
---|
301 | if (n <= 0) { |
---|
302 | Tcl_AppendResult(interp, "empty values list : must be > 0", |
---|
303 | (char *)NULL); |
---|
304 | return TCL_ERROR; |
---|
305 | } |
---|
306 | nValues_ = n; |
---|
307 | values_ = new float[nValues_]; |
---|
308 | size_t j; |
---|
309 | for (j = 0; j < nValues_; j++) { |
---|
310 | if (GetFloatFromObj(interp, vobj[j], values_ + j)!=TCL_OK) { |
---|
311 | return TCL_ERROR; |
---|
312 | } |
---|
313 | } |
---|
314 | } else if ((c == 'u') && (strcmp(string, "units") == 0)) { |
---|
315 | vUnits_ = strdup(Tcl_GetString(objv[i+1])); |
---|
316 | } else if ((c == 'e') && (strcmp(string, "extents") == 0)) { |
---|
317 | int n; |
---|
318 | |
---|
319 | if (Tcl_GetIntFromObj(interp, objv[i+1], &n) != TCL_OK) { |
---|
320 | return TCL_ERROR; |
---|
321 | } |
---|
322 | if (n <= 0) { |
---|
323 | Tcl_AppendResult(interp, "bad extents value: must be > 0", |
---|
324 | (char *)NULL); |
---|
325 | return TCL_ERROR; |
---|
326 | } |
---|
327 | extents_ = n; |
---|
328 | } else if ((c == 'a') && (strcmp(string, "axisorder") == 0)) { |
---|
329 | Tcl_Obj **order; |
---|
330 | int n; |
---|
331 | |
---|
332 | if (Tcl_ListObjGetElements(interp, objv[i+1], &n, &order) |
---|
333 | != TCL_OK) { |
---|
334 | return TCL_ERROR; |
---|
335 | } |
---|
336 | if (n != 2) { |
---|
337 | Tcl_AppendResult(interp, |
---|
338 | "wrong # of axes defined for ordering the data", |
---|
339 | (char *)NULL); |
---|
340 | return TCL_ERROR; |
---|
341 | } |
---|
342 | if ((GetAxisFromObj(interp, order[0], axis) != TCL_OK) || |
---|
343 | (GetAxisFromObj(interp, order[1], axis+1) != TCL_OK)) { |
---|
344 | return TCL_ERROR; |
---|
345 | } |
---|
346 | } else { |
---|
347 | Tcl_AppendResult(interp, "unknown key \"", string, |
---|
348 | (char *)NULL); |
---|
349 | return TCL_ERROR; |
---|
350 | } |
---|
351 | } |
---|
352 | if (values_ == NULL) { |
---|
353 | Tcl_AppendResult(interp, "missing \"values\" key", (char *)NULL); |
---|
354 | return TCL_ERROR; |
---|
355 | } |
---|
356 | if (nValues_ != (xNum_ * yNum_ * extents_)) { |
---|
357 | Tcl_AppendResult(interp, |
---|
358 | "wrong number of values: must be xnum*ynum*extents", |
---|
359 | (char *)NULL); |
---|
360 | return TCL_ERROR; |
---|
361 | } |
---|
362 | |
---|
363 | fprintf(stderr, "generating %dx%dx%d = %d points\n", |
---|
364 | xNum_, yNum_, extents_, xNum_ * yNum_ * extents_); |
---|
365 | |
---|
366 | #ifndef notdef |
---|
367 | if ((axis[0] != 1) || (axis[1] != 0)) { |
---|
368 | fprintf(stderr, "reordering data\n"); |
---|
369 | // Reorder the data into x, y where x varies fastest and so on. |
---|
370 | size_t y; |
---|
371 | float *data, *dp; |
---|
372 | |
---|
373 | dp = data = new float[nValues_]; |
---|
374 | for (y = 0; y < yNum_; y++) { |
---|
375 | size_t x; |
---|
376 | |
---|
377 | for (x = 0; x < xNum_; x++) { |
---|
378 | size_t i, v; |
---|
379 | |
---|
380 | /* Compute the index from the data's described ordering. */ |
---|
381 | i = (y + (yNum_ * x)) * extents_; |
---|
382 | for(v = 0; v < extents_; v++) { |
---|
383 | dp[v] = values_[i+v]; |
---|
384 | } |
---|
385 | dp += extents_; |
---|
386 | } |
---|
387 | } |
---|
388 | delete [] values_; |
---|
389 | values_ = data; |
---|
390 | } |
---|
391 | #endif |
---|
392 | initialized_ = true; |
---|
393 | return TCL_OK; |
---|
394 | } |
---|