1 | /* -*- mode: c++; c-basic-offset: 4; indent-tabs-mode: nil -*- */ |
---|
2 | /* |
---|
3 | * Copyright (c) 2004-2013 HUBzero Foundation, LLC |
---|
4 | * |
---|
5 | */ |
---|
6 | #include <assert.h> |
---|
7 | #define _OPEN_SYS |
---|
8 | #include <fcntl.h> |
---|
9 | #define _XOPEN_SOURCE_EXTENDED 1 |
---|
10 | #include <sys/uio.h> |
---|
11 | #include <sys/stat.h> |
---|
12 | #include <stdlib.h> |
---|
13 | #include <stddef.h> |
---|
14 | #include <limits.h> |
---|
15 | #include <stdint.h> |
---|
16 | #include <unistd.h> |
---|
17 | #include <poll.h> |
---|
18 | |
---|
19 | #include <tcl.h> |
---|
20 | |
---|
21 | #include <RpOutcome.h> |
---|
22 | |
---|
23 | #include <vrmath/Vector3f.h> |
---|
24 | |
---|
25 | #include "nvconf.h" |
---|
26 | |
---|
27 | #include "nanovisServer.h" |
---|
28 | #include "nanovis.h" |
---|
29 | #include "CmdProc.h" |
---|
30 | #include "Command.h" |
---|
31 | #include "FlowCmd.h" |
---|
32 | #include "FlowTypes.h" |
---|
33 | #include "Flow.h" |
---|
34 | #include "FlowBox.h" |
---|
35 | #include "FlowParticles.h" |
---|
36 | #include "Switch.h" |
---|
37 | #include "TransferFunction.h" |
---|
38 | #include "NvLIC.h" |
---|
39 | #include "Unirect.h" |
---|
40 | #include "VelocityArrowsSlice.h" |
---|
41 | #include "Volume.h" |
---|
42 | #include "Trace.h" |
---|
43 | |
---|
44 | using namespace nv; |
---|
45 | using namespace vrmath; |
---|
46 | |
---|
47 | static Rappture::SwitchParseProc AxisSwitchProc; |
---|
48 | static Rappture::SwitchCustom axisSwitch = { |
---|
49 | AxisSwitchProc, NULL, 0, |
---|
50 | }; |
---|
51 | |
---|
52 | static Rappture::SwitchParseProc ColorSwitchProc; |
---|
53 | static Rappture::SwitchCustom colorSwitch = { |
---|
54 | ColorSwitchProc, NULL, 0, |
---|
55 | }; |
---|
56 | |
---|
57 | static Rappture::SwitchParseProc PointSwitchProc; |
---|
58 | static Rappture::SwitchCustom pointSwitch = { |
---|
59 | PointSwitchProc, NULL, 0, |
---|
60 | }; |
---|
61 | |
---|
62 | static Rappture::SwitchParseProc PositionSwitchProc; |
---|
63 | static Rappture::SwitchCustom positionSwitch = { |
---|
64 | PositionSwitchProc, NULL, 0, |
---|
65 | }; |
---|
66 | |
---|
67 | static Rappture::SwitchParseProc TransferFunctionSwitchProc; |
---|
68 | static Rappture::SwitchCustom transferFunctionSwitch = { |
---|
69 | TransferFunctionSwitchProc, NULL, 0, |
---|
70 | }; |
---|
71 | |
---|
72 | Rappture::SwitchSpec Flow::_switches[] = { |
---|
73 | {Rappture::SWITCH_FLOAT, "-ambient", "value", |
---|
74 | offsetof(FlowValues, ambient), 0}, |
---|
75 | {Rappture::SWITCH_BOOLEAN, "-arrows", "boolean", |
---|
76 | offsetof(FlowValues, showArrows), 0}, |
---|
77 | {Rappture::SWITCH_CUSTOM, "-axis", "axis", |
---|
78 | offsetof(FlowValues, slicePos.axis), 0, 0, &axisSwitch}, |
---|
79 | {Rappture::SWITCH_FLOAT, "-diffuse", "value", |
---|
80 | offsetof(FlowValues, diffuse), 0}, |
---|
81 | {Rappture::SWITCH_BOOLEAN, "-hide", "boolean", |
---|
82 | offsetof(FlowValues, isHidden), 0}, |
---|
83 | {Rappture::SWITCH_BOOLEAN, "-light2side", "boolean", |
---|
84 | offsetof(FlowValues, twoSidedLighting), 0}, |
---|
85 | {Rappture::SWITCH_FLOAT, "-opacity", "value", |
---|
86 | offsetof(FlowValues, opacity), 0}, |
---|
87 | {Rappture::SWITCH_BOOLEAN, "-outline", "boolean", |
---|
88 | offsetof(FlowValues, showOutline), 0}, |
---|
89 | {Rappture::SWITCH_CUSTOM, "-position", "number", |
---|
90 | offsetof(FlowValues, slicePos), 0, 0, &positionSwitch}, |
---|
91 | {Rappture::SWITCH_BOOLEAN, "-slice", "boolean", |
---|
92 | offsetof(FlowValues, sliceVisible), 0}, |
---|
93 | {Rappture::SWITCH_FLOAT, "-specularExp", "value", |
---|
94 | offsetof(FlowValues, specularExp), 0}, |
---|
95 | {Rappture::SWITCH_FLOAT, "-specularLevel", "value", |
---|
96 | offsetof(FlowValues, specular), 0}, |
---|
97 | {Rappture::SWITCH_CUSTOM, "-transferfunction", "name", |
---|
98 | offsetof(FlowValues, transferFunction), 0, 0, &transferFunctionSwitch}, |
---|
99 | {Rappture::SWITCH_BOOLEAN, "-volume", "boolean", |
---|
100 | offsetof(FlowValues, showVolume), 0}, |
---|
101 | {Rappture::SWITCH_END} |
---|
102 | }; |
---|
103 | |
---|
104 | Rappture::SwitchSpec FlowParticles::_switches[] = { |
---|
105 | {Rappture::SWITCH_CUSTOM, "-axis", "string", |
---|
106 | offsetof(FlowParticlesValues, position.axis), 0, 0, &axisSwitch}, |
---|
107 | {Rappture::SWITCH_CUSTOM, "-color", "{r g b a}", |
---|
108 | offsetof(FlowParticlesValues, color), 0, 0, &colorSwitch}, |
---|
109 | {Rappture::SWITCH_BOOLEAN, "-hide", "boolean", |
---|
110 | offsetof(FlowParticlesValues, isHidden), 0}, |
---|
111 | {Rappture::SWITCH_CUSTOM, "-position", "number", |
---|
112 | offsetof(FlowParticlesValues, position), 0, 0, &positionSwitch}, |
---|
113 | {Rappture::SWITCH_FLOAT, "-size", "float", |
---|
114 | offsetof(FlowParticlesValues, particleSize), 0}, |
---|
115 | {Rappture::SWITCH_END} |
---|
116 | }; |
---|
117 | |
---|
118 | Rappture::SwitchSpec FlowBox::_switches[] = { |
---|
119 | {Rappture::SWITCH_CUSTOM, "-color", "{r g b a}", |
---|
120 | offsetof(FlowBoxValues, color), 0, 0, &colorSwitch}, |
---|
121 | {Rappture::SWITCH_CUSTOM, "-corner1", "{x y z}", |
---|
122 | offsetof(FlowBoxValues, corner1), 0, 0, &pointSwitch}, |
---|
123 | {Rappture::SWITCH_CUSTOM, "-corner2", "{x y z}", |
---|
124 | offsetof(FlowBoxValues, corner2), 0, 0, &pointSwitch}, |
---|
125 | {Rappture::SWITCH_BOOLEAN, "-hide", "boolean", |
---|
126 | offsetof(FlowBoxValues, isHidden), 0}, |
---|
127 | {Rappture::SWITCH_FLOAT, "-linewidth", "number", |
---|
128 | offsetof(FlowBoxValues, lineWidth), 0}, |
---|
129 | {Rappture::SWITCH_END} |
---|
130 | }; |
---|
131 | |
---|
132 | static int |
---|
133 | FlowDataFileOp(ClientData clientData, Tcl_Interp *interp, int objc, |
---|
134 | Tcl_Obj *const *objv) |
---|
135 | { |
---|
136 | Rappture::Outcome result; |
---|
137 | |
---|
138 | const char *fileName; |
---|
139 | fileName = Tcl_GetString(objv[3]); |
---|
140 | TRACE("File: %s", fileName); |
---|
141 | |
---|
142 | int nComponents; |
---|
143 | if (Tcl_GetIntFromObj(interp, objv[4], &nComponents) != TCL_OK) { |
---|
144 | return TCL_ERROR; |
---|
145 | } |
---|
146 | if ((nComponents < 1) || (nComponents > 4)) { |
---|
147 | Tcl_AppendResult(interp, "bad # of components \"", |
---|
148 | Tcl_GetString(objv[4]), "\"", (char *)NULL); |
---|
149 | return TCL_ERROR; |
---|
150 | } |
---|
151 | Rappture::Buffer buf; |
---|
152 | if (!buf.load(result, fileName)) { |
---|
153 | Tcl_AppendResult(interp, "can't load data from \"", fileName, "\": ", |
---|
154 | result.remark(), (char *)NULL); |
---|
155 | return TCL_ERROR; |
---|
156 | } |
---|
157 | |
---|
158 | Rappture::Unirect3d *dataPtr; |
---|
159 | dataPtr = new Rappture::Unirect3d(nComponents); |
---|
160 | Flow *flow = (Flow *)clientData; |
---|
161 | size_t length = buf.size(); |
---|
162 | char *bytes = (char *)buf.bytes(); |
---|
163 | if ((length > 4) && (strncmp(bytes, "<DX>", 4) == 0)) { |
---|
164 | if (!dataPtr->importDx(result, nComponents, length-4, bytes+4)) { |
---|
165 | Tcl_AppendResult(interp, result.remark(), (char *)NULL); |
---|
166 | delete dataPtr; |
---|
167 | return TCL_ERROR; |
---|
168 | } |
---|
169 | } else if ((length > 10) && (strncmp(bytes, "unirect3d ", 10) == 0)) { |
---|
170 | if (dataPtr->parseBuffer(interp, buf) != TCL_OK) { |
---|
171 | delete dataPtr; |
---|
172 | return TCL_ERROR; |
---|
173 | } |
---|
174 | } else if ((length > 10) && (strncmp(bytes, "unirect2d ", 10) == 0)) { |
---|
175 | Rappture::Unirect2d *u2dPtr; |
---|
176 | u2dPtr = new Rappture::Unirect2d(nComponents); |
---|
177 | if (u2dPtr->parseBuffer(interp, buf) != TCL_OK) { |
---|
178 | delete u2dPtr; |
---|
179 | return TCL_ERROR; |
---|
180 | } |
---|
181 | dataPtr->convert(u2dPtr); |
---|
182 | delete u2dPtr; |
---|
183 | } else { |
---|
184 | TRACE("header is %.14s", buf.bytes()); |
---|
185 | if (!dataPtr->importDx(result, nComponents, length, bytes)) { |
---|
186 | Tcl_AppendResult(interp, result.remark(), (char *)NULL); |
---|
187 | delete dataPtr; |
---|
188 | return TCL_ERROR; |
---|
189 | } |
---|
190 | } |
---|
191 | if (dataPtr->nValues() == 0) { |
---|
192 | delete dataPtr; |
---|
193 | Tcl_AppendResult(interp, "no data found in \"", fileName, "\"", |
---|
194 | (char *)NULL); |
---|
195 | return TCL_ERROR; |
---|
196 | } |
---|
197 | flow->data(dataPtr); |
---|
198 | NanoVis::eventuallyRedraw(NanoVis::MAP_FLOWS); |
---|
199 | return TCL_OK; |
---|
200 | } |
---|
201 | |
---|
202 | /** |
---|
203 | * $flow data follows nbytes nComponents |
---|
204 | */ |
---|
205 | static int |
---|
206 | FlowDataFollowsOp(ClientData clientData, Tcl_Interp *interp, int objc, |
---|
207 | Tcl_Obj *const *objv) |
---|
208 | { |
---|
209 | Rappture::Outcome result; |
---|
210 | |
---|
211 | TRACE("Enter"); |
---|
212 | |
---|
213 | int nBytes; |
---|
214 | if (Tcl_GetIntFromObj(interp, objv[3], &nBytes) != TCL_OK) { |
---|
215 | ERROR("Bad nBytes \"%s\"", Tcl_GetString(objv[3])); |
---|
216 | return TCL_ERROR; |
---|
217 | } |
---|
218 | if (nBytes <= 0) { |
---|
219 | Tcl_AppendResult(interp, "bad # bytes request \"", |
---|
220 | Tcl_GetString(objv[3]), "\" for \"data follows\"", (char *)NULL); |
---|
221 | ERROR("Bad nbytes %d", nBytes); |
---|
222 | return TCL_ERROR; |
---|
223 | } |
---|
224 | int nComponents; |
---|
225 | if (Tcl_GetIntFromObj(interp, objv[4], &nComponents) != TCL_OK) { |
---|
226 | ERROR("Bad # of components \"%s\"", Tcl_GetString(objv[4])); |
---|
227 | return TCL_ERROR; |
---|
228 | } |
---|
229 | if (nComponents <= 0) { |
---|
230 | Tcl_AppendResult(interp, "bad # of components request \"", |
---|
231 | Tcl_GetString(objv[4]), "\" for \"data follows\"", (char *)NULL); |
---|
232 | ERROR("Bad # of components %d", nComponents); |
---|
233 | return TCL_ERROR; |
---|
234 | } |
---|
235 | Rappture::Buffer buf(nBytes); |
---|
236 | TRACE("Flow data loading bytes: %d components: %d", nBytes, nComponents); |
---|
237 | if (GetDataStream(interp, buf, nBytes) != TCL_OK) { |
---|
238 | return TCL_ERROR; |
---|
239 | } |
---|
240 | char *bytes = (char *)buf.bytes(); |
---|
241 | size_t length = buf.size(); |
---|
242 | |
---|
243 | Rappture::Unirect3d *dataPtr; |
---|
244 | dataPtr = new Rappture::Unirect3d(nComponents); |
---|
245 | |
---|
246 | Flow *flow = (Flow *)clientData; |
---|
247 | if ((length > 4) && (strncmp(bytes, "<DX>", 4) == 0)) { |
---|
248 | if (!dataPtr->importDx(result, nComponents, length - 4, bytes + 4)) { |
---|
249 | Tcl_AppendResult(interp, result.remark(), (char *)NULL); |
---|
250 | delete dataPtr; |
---|
251 | return TCL_ERROR; |
---|
252 | } |
---|
253 | } else if ((length > 10) && (strncmp(bytes, "unirect3d ", 10) == 0)) { |
---|
254 | if (dataPtr->parseBuffer(interp, buf) != TCL_OK) { |
---|
255 | delete dataPtr; |
---|
256 | return TCL_ERROR; |
---|
257 | } |
---|
258 | } else if ((length > 10) && (strncmp(bytes, "unirect2d ", 10) == 0)) { |
---|
259 | Rappture::Unirect2d *u2dPtr; |
---|
260 | u2dPtr = new Rappture::Unirect2d(nComponents); |
---|
261 | if (u2dPtr->parseBuffer(interp, buf) != TCL_OK) { |
---|
262 | delete u2dPtr; |
---|
263 | return TCL_ERROR; |
---|
264 | } |
---|
265 | dataPtr->convert(u2dPtr); |
---|
266 | delete u2dPtr; |
---|
267 | } else { |
---|
268 | TRACE("header is %.14s", buf.bytes()); |
---|
269 | if (!dataPtr->importDx(result, nComponents, length, bytes)) { |
---|
270 | Tcl_AppendResult(interp, result.remark(), (char *)NULL); |
---|
271 | delete dataPtr; |
---|
272 | return TCL_ERROR; |
---|
273 | } |
---|
274 | } |
---|
275 | if (dataPtr->nValues() == 0) { |
---|
276 | delete dataPtr; |
---|
277 | Tcl_AppendResult(interp, "no data found in stream", (char *)NULL); |
---|
278 | return TCL_ERROR; |
---|
279 | } |
---|
280 | TRACE("nx = %d ny = %d nz = %d", |
---|
281 | dataPtr->xNum(), dataPtr->yNum(), dataPtr->zNum()); |
---|
282 | TRACE("x0 = %lg y0 = %lg z0 = %lg", |
---|
283 | dataPtr->xMin(), dataPtr->yMin(), dataPtr->zMin()); |
---|
284 | TRACE("lx = %lg ly = %lg lz = %lg", |
---|
285 | dataPtr->xMax() - dataPtr->xMin(), |
---|
286 | dataPtr->yMax() - dataPtr->yMin(), |
---|
287 | dataPtr->zMax() - dataPtr->zMin()); |
---|
288 | TRACE("dx = %lg dy = %lg dz = %lg", |
---|
289 | dataPtr->xNum() > 1 ? (dataPtr->xMax() - dataPtr->xMin())/(dataPtr->xNum()-1) : 0, |
---|
290 | dataPtr->yNum() > 1 ? (dataPtr->yMax() - dataPtr->yMin())/(dataPtr->yNum()-1) : 0, |
---|
291 | dataPtr->zNum() > 1 ? (dataPtr->zMax() - dataPtr->zMin())/(dataPtr->zNum()-1) : 0); |
---|
292 | TRACE("magMin = %lg magMax = %lg", |
---|
293 | dataPtr->magMin(), dataPtr->magMax()); |
---|
294 | flow->data(dataPtr); |
---|
295 | { |
---|
296 | char info[1024]; |
---|
297 | int length = |
---|
298 | sprintf(info, "nv>data tag %s min %g max %g\n", |
---|
299 | flow->name(), dataPtr->magMin(), dataPtr->magMax()); |
---|
300 | if (SocketWrite(info, (size_t)length) < 0) { |
---|
301 | return TCL_ERROR; |
---|
302 | } |
---|
303 | } |
---|
304 | NanoVis::eventuallyRedraw(NanoVis::MAP_FLOWS); |
---|
305 | return TCL_OK; |
---|
306 | } |
---|
307 | |
---|
308 | static Rappture::CmdSpec flowDataOps[] = { |
---|
309 | {"file", 2, FlowDataFileOp, 5, 5, "fileName nComponents",}, |
---|
310 | {"follows", 2, FlowDataFollowsOp, 5, 5, "size nComponents",}, |
---|
311 | }; |
---|
312 | static int nFlowDataOps = NumCmdSpecs(flowDataOps); |
---|
313 | |
---|
314 | static int |
---|
315 | FlowDataOp(ClientData clientData, Tcl_Interp *interp, int objc, |
---|
316 | Tcl_Obj *const *objv) |
---|
317 | { |
---|
318 | Tcl_ObjCmdProc *proc; |
---|
319 | |
---|
320 | proc = Rappture::GetOpFromObj(interp, nFlowDataOps, flowDataOps, |
---|
321 | Rappture::CMDSPEC_ARG2, objc, objv, 0); |
---|
322 | if (proc == NULL) { |
---|
323 | return TCL_ERROR; |
---|
324 | } |
---|
325 | return (*proc) (clientData, interp, objc, objv); |
---|
326 | } |
---|
327 | |
---|
328 | /** |
---|
329 | * \brief Convert a Tcl_Obj representing the label of a child node into its |
---|
330 | * integer node id. |
---|
331 | * |
---|
332 | * \param clientData Flag indicating if the node is considered before or |
---|
333 | * after the insertion position. |
---|
334 | * \param interp Interpreter to send results back to |
---|
335 | * \param switchName Not used |
---|
336 | * \param objPtr String representation |
---|
337 | * \param record Structure record |
---|
338 | * \param offset Not used |
---|
339 | * \param flags Not used |
---|
340 | * |
---|
341 | * \return The return value is a standard Tcl result. |
---|
342 | */ |
---|
343 | static int |
---|
344 | AxisSwitchProc(ClientData clientData, Tcl_Interp *interp, |
---|
345 | const char *switchName, Tcl_Obj *objPtr, |
---|
346 | char *record, int offset, int flags) |
---|
347 | { |
---|
348 | const char *string = Tcl_GetString(objPtr); |
---|
349 | if (string[1] == '\0') { |
---|
350 | Flow::SliceAxis *axisPtr = (Flow::SliceAxis *)(record + offset); |
---|
351 | char c; |
---|
352 | c = tolower((unsigned char)string[0]); |
---|
353 | if (c == 'x') { |
---|
354 | *axisPtr = Flow::AXIS_X; |
---|
355 | return TCL_OK; |
---|
356 | } else if (c == 'y') { |
---|
357 | *axisPtr = Flow::AXIS_Y; |
---|
358 | return TCL_OK; |
---|
359 | } else if (c == 'z') { |
---|
360 | *axisPtr = Flow::AXIS_Z; |
---|
361 | return TCL_OK; |
---|
362 | } |
---|
363 | /*FALLTHRU*/ |
---|
364 | } |
---|
365 | Tcl_AppendResult(interp, "bad axis \"", string, |
---|
366 | "\": should be x, y, or z", (char*)NULL); |
---|
367 | return TCL_ERROR; |
---|
368 | } |
---|
369 | |
---|
370 | /** |
---|
371 | * \brief Convert a Tcl_Obj representing the label of a list of four color |
---|
372 | * components in to a RGBA color value. |
---|
373 | * |
---|
374 | * \param clientData Flag indicating if the node is considered before or |
---|
375 | * after the insertion position. |
---|
376 | * \param interp Interpreter to send results back to |
---|
377 | * \param switchName Not used |
---|
378 | * \param objPtr String representation |
---|
379 | * \param record Structure record |
---|
380 | * \param offset Not used |
---|
381 | * \param flags Not used |
---|
382 | * |
---|
383 | * \return The return value is a standard Tcl result. |
---|
384 | */ |
---|
385 | static int |
---|
386 | ColorSwitchProc(ClientData clientData, Tcl_Interp *interp, |
---|
387 | const char *switchName, Tcl_Obj *objPtr, |
---|
388 | char *record, int offset, int flags) |
---|
389 | { |
---|
390 | Tcl_Obj **objv; |
---|
391 | int objc; |
---|
392 | FlowColor *color = (FlowColor *)(record + offset); |
---|
393 | |
---|
394 | if (Tcl_ListObjGetElements(interp, objPtr, &objc, &objv) != TCL_OK) { |
---|
395 | return TCL_ERROR; |
---|
396 | } |
---|
397 | if ((objc < 3) || (objc > 4)) { |
---|
398 | Tcl_AppendResult(interp, "wrong # of elements in color definition", |
---|
399 | (char *)NULL); |
---|
400 | return TCL_ERROR; |
---|
401 | } |
---|
402 | float values[4]; |
---|
403 | int i; |
---|
404 | values[3] = 1.0f; |
---|
405 | for (i = 0; i < objc; i++) { |
---|
406 | float value; |
---|
407 | |
---|
408 | if (GetFloatFromObj(interp, objv[i], &value) != TCL_OK) { |
---|
409 | return TCL_ERROR; |
---|
410 | } |
---|
411 | if ((value < 0.0) || (value > 1.0)) { |
---|
412 | Tcl_AppendResult(interp, "bad component value in \"", |
---|
413 | Tcl_GetString(objPtr), "\": color values must be [0..1]", |
---|
414 | (char *)NULL); |
---|
415 | return TCL_ERROR; |
---|
416 | } |
---|
417 | values[i] = value; |
---|
418 | } |
---|
419 | color->r = values[0]; |
---|
420 | color->g = values[1]; |
---|
421 | color->b = values[2]; |
---|
422 | color->a = values[3]; |
---|
423 | return TCL_OK; |
---|
424 | } |
---|
425 | |
---|
426 | /** |
---|
427 | * \brief Convert a Tcl_Obj representing the a 3-D coordinate into a point. |
---|
428 | * |
---|
429 | * \param clientData Flag indicating if the node is considered before or |
---|
430 | * after the insertion position. |
---|
431 | * \param interp Interpreter to send results back to |
---|
432 | * \param switchName Not used |
---|
433 | * \param objPtr String representation |
---|
434 | * \param record Structure record |
---|
435 | * \param offset Not used |
---|
436 | * \param flags Not used |
---|
437 | * |
---|
438 | * \return The return value is a standard Tcl result. |
---|
439 | */ |
---|
440 | static int |
---|
441 | PointSwitchProc(ClientData clientData, Tcl_Interp *interp, |
---|
442 | const char *switchName, Tcl_Obj *objPtr, |
---|
443 | char *record, int offset, int flags) |
---|
444 | { |
---|
445 | FlowPoint *point = (FlowPoint *)(record + offset); |
---|
446 | int objc; |
---|
447 | Tcl_Obj **objv; |
---|
448 | |
---|
449 | if (Tcl_ListObjGetElements(interp, objPtr, &objc, &objv) != TCL_OK) { |
---|
450 | return TCL_ERROR; |
---|
451 | } |
---|
452 | if (objc != 3) { |
---|
453 | Tcl_AppendResult(interp, "wrong # of elements for box coordinates: " |
---|
454 | " should be \"x y z\"", (char *)NULL); |
---|
455 | return TCL_ERROR; |
---|
456 | } |
---|
457 | float values[3]; |
---|
458 | int i; |
---|
459 | for (i = 0; i < objc; i++) { |
---|
460 | float value; |
---|
461 | |
---|
462 | if (GetFloatFromObj(interp, objv[i], &value) != TCL_OK) { |
---|
463 | return TCL_ERROR; |
---|
464 | } |
---|
465 | values[i] = value; |
---|
466 | } |
---|
467 | point->x = values[0]; |
---|
468 | point->y = values[1]; |
---|
469 | point->z = values[2]; |
---|
470 | return TCL_OK; |
---|
471 | } |
---|
472 | |
---|
473 | /** |
---|
474 | * \brief Convert a Tcl_Obj representing the a 3-D coordinate into a point. |
---|
475 | * |
---|
476 | * \param clientData Flag indicating if the node is considered before or |
---|
477 | * after the insertion position. |
---|
478 | * \param interp Interpreter to send results back to |
---|
479 | * \param switchName Not used |
---|
480 | * \param objPtr String representation |
---|
481 | * \param record Structure record |
---|
482 | * \param offset Not used |
---|
483 | * \param flags Not used |
---|
484 | * |
---|
485 | * \return The return value is a standard Tcl result. |
---|
486 | */ |
---|
487 | static int |
---|
488 | PositionSwitchProc(ClientData clientData, Tcl_Interp *interp, |
---|
489 | const char *switchName, Tcl_Obj *objPtr, |
---|
490 | char *record, int offset, int flags) |
---|
491 | { |
---|
492 | FlowPosition *posPtr = (FlowPosition *)(record + offset); |
---|
493 | const char *string; |
---|
494 | char *p; |
---|
495 | |
---|
496 | string = Tcl_GetString(objPtr); |
---|
497 | p = strrchr((char *)string, '%'); |
---|
498 | if (p == NULL) { |
---|
499 | float value; |
---|
500 | |
---|
501 | if (GetFloatFromObj(interp, objPtr, &value) != TCL_OK) { |
---|
502 | return TCL_ERROR; |
---|
503 | } |
---|
504 | posPtr->value = value; |
---|
505 | posPtr->flags = ABSPOS; |
---|
506 | } else { |
---|
507 | double value; |
---|
508 | |
---|
509 | *p = '\0'; |
---|
510 | if (Tcl_GetDouble(interp, string, &value) != TCL_OK) { |
---|
511 | return TCL_ERROR; |
---|
512 | } |
---|
513 | posPtr->value = (float)value * 0.01; |
---|
514 | posPtr->flags = RELPOS; |
---|
515 | } |
---|
516 | return TCL_OK; |
---|
517 | } |
---|
518 | |
---|
519 | /** |
---|
520 | * \brief Convert a Tcl_Obj representing the transfer function into a |
---|
521 | * TransferFunction pointer. |
---|
522 | * |
---|
523 | * The transfer function must have been previously defined. |
---|
524 | * |
---|
525 | * \param clientData Flag indicating if the node is considered before or |
---|
526 | * after the insertion position. |
---|
527 | * \param interp Interpreter to send results back to |
---|
528 | * \param switchName Not used |
---|
529 | * \param objPtr String representation |
---|
530 | * \param record Structure record |
---|
531 | * \param offset Not used |
---|
532 | * \param flags Not used |
---|
533 | * |
---|
534 | * \return The return value is a standard Tcl result. |
---|
535 | */ |
---|
536 | static int |
---|
537 | TransferFunctionSwitchProc(ClientData clientData, Tcl_Interp *interp, |
---|
538 | const char *switchName, Tcl_Obj *objPtr, |
---|
539 | char *record, int offset, int flags) |
---|
540 | { |
---|
541 | TransferFunction **funcPtrPtr = (TransferFunction **)(record + offset); |
---|
542 | TransferFunction *tf = NanoVis::getTransferFunction(Tcl_GetString(objPtr)); |
---|
543 | if (tf == NULL) { |
---|
544 | Tcl_AppendResult(interp, "transfer function \"", Tcl_GetString(objPtr), |
---|
545 | "\" is not defined", (char*)NULL); |
---|
546 | return TCL_ERROR; |
---|
547 | } |
---|
548 | *funcPtrPtr = tf; |
---|
549 | return TCL_OK; |
---|
550 | } |
---|
551 | |
---|
552 | static int |
---|
553 | FlowConfigureOp(ClientData clientData, Tcl_Interp *interp, int objc, |
---|
554 | Tcl_Obj *const *objv) |
---|
555 | { |
---|
556 | Flow *flow = (Flow *)clientData; |
---|
557 | |
---|
558 | if (flow->parseSwitches(interp, objc - 2, objv + 2) != TCL_OK) { |
---|
559 | return TCL_ERROR; |
---|
560 | } |
---|
561 | NanoVis::eventuallyRedraw(NanoVis::MAP_FLOWS); |
---|
562 | return TCL_OK; |
---|
563 | } |
---|
564 | |
---|
565 | static int |
---|
566 | FlowParticlesAddOp(ClientData clientData, Tcl_Interp *interp, int objc, |
---|
567 | Tcl_Obj *const *objv) |
---|
568 | { |
---|
569 | Flow *flow = (Flow *)clientData; |
---|
570 | const char *particlesName = Tcl_GetString(objv[3]); |
---|
571 | FlowParticles *particles = flow->createParticles(particlesName); |
---|
572 | if (particles == NULL) { |
---|
573 | Tcl_AppendResult(interp, "Flow particle injection plane \"", |
---|
574 | particlesName, |
---|
575 | "\" already exists or could not be created", |
---|
576 | (char*)NULL); |
---|
577 | return TCL_ERROR; |
---|
578 | } |
---|
579 | if (particles->parseSwitches(interp, objc - 4, objv + 4) != TCL_OK) { |
---|
580 | flow->deleteParticles(particlesName); |
---|
581 | return TCL_ERROR; |
---|
582 | } |
---|
583 | particles->configure(); |
---|
584 | NanoVis::eventuallyRedraw(); |
---|
585 | Tcl_SetObjResult(interp, objv[3]); |
---|
586 | return TCL_OK; |
---|
587 | } |
---|
588 | |
---|
589 | static int |
---|
590 | FlowParticlesConfigureOp(ClientData clientData, Tcl_Interp *interp, int objc, |
---|
591 | Tcl_Obj *const *objv) |
---|
592 | { |
---|
593 | Flow *flow = (Flow *)clientData; |
---|
594 | const char *particlesName = Tcl_GetString(objv[3]); |
---|
595 | FlowParticles *particles = flow->getParticles(particlesName); |
---|
596 | if (particles == NULL) { |
---|
597 | Tcl_AppendResult(interp, "Flow particle injection plane \"", |
---|
598 | particlesName, "\" not found", |
---|
599 | (char*)NULL); |
---|
600 | return TCL_ERROR; |
---|
601 | } |
---|
602 | if (particles->parseSwitches(interp, objc - 4, objv + 4) != TCL_OK) { |
---|
603 | return TCL_ERROR; |
---|
604 | } |
---|
605 | particles->configure(); |
---|
606 | NanoVis::eventuallyRedraw(NanoVis::MAP_FLOWS); |
---|
607 | return TCL_OK; |
---|
608 | } |
---|
609 | |
---|
610 | static int |
---|
611 | FlowParticlesDeleteOp(ClientData clientData, Tcl_Interp *interp, int objc, |
---|
612 | Tcl_Obj *const *objv) |
---|
613 | { |
---|
614 | Flow *flow = (Flow *)clientData; |
---|
615 | for (int i = 3; i < objc; i++) { |
---|
616 | flow->deleteParticles(Tcl_GetString(objv[i])); |
---|
617 | } |
---|
618 | NanoVis::eventuallyRedraw(); |
---|
619 | return TCL_OK; |
---|
620 | } |
---|
621 | |
---|
622 | static int |
---|
623 | FlowParticlesNamesOp(ClientData clientData, Tcl_Interp *interp, int objc, |
---|
624 | Tcl_Obj *const *objv) |
---|
625 | { |
---|
626 | Flow *flow = (Flow *)clientData; |
---|
627 | Tcl_Obj *listObjPtr; |
---|
628 | listObjPtr = Tcl_NewListObj(0, (Tcl_Obj **) NULL); |
---|
629 | std::vector<std::string> names; |
---|
630 | flow->getParticlesNames(names); |
---|
631 | for (std::vector<std::string>::iterator itr = names.begin(); |
---|
632 | itr != names.end(); ++itr) { |
---|
633 | Tcl_Obj *objPtr = Tcl_NewStringObj(itr->c_str(), -1); |
---|
634 | Tcl_ListObjAppendElement(interp, listObjPtr, objPtr); |
---|
635 | } |
---|
636 | Tcl_SetObjResult(interp, listObjPtr); |
---|
637 | return TCL_OK; |
---|
638 | } |
---|
639 | |
---|
640 | static Rappture::CmdSpec flowParticlesOps[] = { |
---|
641 | {"add", 1, FlowParticlesAddOp, 4, 0, "name ?switches?",}, |
---|
642 | {"configure", 1, FlowParticlesConfigureOp, 4, 0, "name ?switches?",}, |
---|
643 | {"delete", 1, FlowParticlesDeleteOp, 4, 0, "name ?name...?"}, |
---|
644 | {"names", 1, FlowParticlesNamesOp, 3, 3, ""}, |
---|
645 | }; |
---|
646 | |
---|
647 | static int nFlowParticlesOps = NumCmdSpecs(flowParticlesOps); |
---|
648 | |
---|
649 | /** |
---|
650 | * \brief This procedure is invoked to process commands on behalf of the flow |
---|
651 | * object. |
---|
652 | * |
---|
653 | * Side effects: See the user documentation. |
---|
654 | * |
---|
655 | * $flow particles oper $name |
---|
656 | * |
---|
657 | * \return A standard Tcl result. |
---|
658 | */ |
---|
659 | static int |
---|
660 | FlowParticlesOp(ClientData clientData, Tcl_Interp *interp, int objc, |
---|
661 | Tcl_Obj *const *objv) |
---|
662 | { |
---|
663 | Tcl_ObjCmdProc *proc; |
---|
664 | proc = Rappture::GetOpFromObj(interp, nFlowParticlesOps, flowParticlesOps, |
---|
665 | Rappture::CMDSPEC_ARG2, objc, objv, 0); |
---|
666 | if (proc == NULL) { |
---|
667 | return TCL_ERROR; |
---|
668 | } |
---|
669 | Flow *flow = (Flow *)clientData; |
---|
670 | Tcl_Preserve(flow); |
---|
671 | int result; |
---|
672 | result = (*proc) (clientData, interp, objc, objv); |
---|
673 | Tcl_Release(flow); |
---|
674 | return result; |
---|
675 | } |
---|
676 | |
---|
677 | static int |
---|
678 | FlowBoxAddOp(ClientData clientData, Tcl_Interp *interp, int objc, |
---|
679 | Tcl_Obj *const *objv) |
---|
680 | { |
---|
681 | Flow *flow = (Flow *)clientData; |
---|
682 | const char *boxName = Tcl_GetString(objv[3]); |
---|
683 | FlowBox *box = flow->createBox(boxName); |
---|
684 | if (box == NULL) { |
---|
685 | Tcl_AppendResult(interp, "Flow box \"", boxName, |
---|
686 | "\" already exists or could not be created", |
---|
687 | (char*)NULL); |
---|
688 | return TCL_ERROR; |
---|
689 | } |
---|
690 | if (box->parseSwitches(interp, objc - 4, objv + 4) != TCL_OK) { |
---|
691 | flow->deleteBox(boxName); |
---|
692 | return TCL_ERROR; |
---|
693 | } |
---|
694 | NanoVis::eventuallyRedraw(); |
---|
695 | Tcl_SetObjResult(interp, objv[3]); |
---|
696 | return TCL_OK; |
---|
697 | } |
---|
698 | |
---|
699 | static int |
---|
700 | FlowBoxConfigureOp(ClientData clientData, Tcl_Interp *interp, int objc, |
---|
701 | Tcl_Obj *const *objv) |
---|
702 | { |
---|
703 | Flow *flow = (Flow *)clientData; |
---|
704 | const char *boxName = Tcl_GetString(objv[3]); |
---|
705 | FlowBox *box = flow->getBox(boxName); |
---|
706 | if (box == NULL) { |
---|
707 | Tcl_AppendResult(interp, "Flow box \"", boxName, "\" not found", |
---|
708 | (char*)NULL); |
---|
709 | return TCL_ERROR; |
---|
710 | } |
---|
711 | if (box->parseSwitches(interp, objc - 4, objv + 4) != TCL_OK) { |
---|
712 | return TCL_ERROR; |
---|
713 | } |
---|
714 | NanoVis::eventuallyRedraw(); |
---|
715 | return TCL_OK; |
---|
716 | } |
---|
717 | |
---|
718 | static int |
---|
719 | FlowBoxDeleteOp(ClientData clientData, Tcl_Interp *interp, int objc, |
---|
720 | Tcl_Obj *const *objv) |
---|
721 | { |
---|
722 | Flow *flow = (Flow *)clientData; |
---|
723 | for (int i = 3; i < objc; i++) { |
---|
724 | flow->deleteBox(Tcl_GetString(objv[i])); |
---|
725 | } |
---|
726 | NanoVis::eventuallyRedraw(); |
---|
727 | return TCL_OK; |
---|
728 | } |
---|
729 | |
---|
730 | static int |
---|
731 | FlowBoxNamesOp(ClientData clientData, Tcl_Interp *interp, int objc, |
---|
732 | Tcl_Obj *const *objv) |
---|
733 | { |
---|
734 | Flow *flow = (Flow *)clientData; |
---|
735 | Tcl_Obj *listObjPtr = Tcl_NewListObj(0, (Tcl_Obj **) NULL); |
---|
736 | std::vector<std::string> names; |
---|
737 | flow->getBoxNames(names); |
---|
738 | for (std::vector<std::string>::iterator itr = names.begin(); |
---|
739 | itr != names.end(); ++itr) { |
---|
740 | Tcl_Obj *objPtr = Tcl_NewStringObj(itr->c_str(), -1); |
---|
741 | Tcl_ListObjAppendElement(interp, listObjPtr, objPtr); |
---|
742 | } |
---|
743 | Tcl_SetObjResult(interp, listObjPtr); |
---|
744 | return TCL_OK; |
---|
745 | } |
---|
746 | |
---|
747 | static Rappture::CmdSpec flowBoxOps[] = { |
---|
748 | {"add", 1, FlowBoxAddOp, 4, 0, "name ?switches?",}, |
---|
749 | {"configure", 1, FlowBoxConfigureOp, 4, 0, "name ?switches?",}, |
---|
750 | {"delete", 1, FlowBoxDeleteOp, 4, 0, "name ?name...?"}, |
---|
751 | {"names", 1, FlowBoxNamesOp, 3, 3, ""}, |
---|
752 | }; |
---|
753 | |
---|
754 | static int nFlowBoxOps = NumCmdSpecs(flowBoxOps); |
---|
755 | |
---|
756 | /** |
---|
757 | * \brief This procedure is invoked to process commands on behalf of the flow |
---|
758 | * object. |
---|
759 | * |
---|
760 | * Side effects: See the user documentation. |
---|
761 | * |
---|
762 | * \return A standard Tcl result. |
---|
763 | */ |
---|
764 | static int |
---|
765 | FlowBoxOp(ClientData clientData, Tcl_Interp *interp, int objc, |
---|
766 | Tcl_Obj *const *objv) |
---|
767 | { |
---|
768 | Tcl_ObjCmdProc *proc; |
---|
769 | proc = Rappture::GetOpFromObj(interp, nFlowBoxOps, flowBoxOps, |
---|
770 | Rappture::CMDSPEC_ARG2, objc, objv, 0); |
---|
771 | if (proc == NULL) { |
---|
772 | return TCL_ERROR; |
---|
773 | } |
---|
774 | Flow *flow = (Flow *)clientData; |
---|
775 | Tcl_Preserve(flow); |
---|
776 | int result; |
---|
777 | result = (*proc) (clientData, interp, objc, objv); |
---|
778 | Tcl_Release(flow); |
---|
779 | return result; |
---|
780 | } |
---|
781 | |
---|
782 | /* |
---|
783 | * CLIENT COMMAND: |
---|
784 | * $flow legend <width> <height> |
---|
785 | * |
---|
786 | * Clients use this to generate a legend image for the specified |
---|
787 | * transfer function. The legend image is a color gradient from 0 |
---|
788 | * to one, drawn in the given transfer function. The resulting image |
---|
789 | * is returned in the size <width> x <height>. |
---|
790 | */ |
---|
791 | static int |
---|
792 | FlowLegendOp(ClientData clientData, Tcl_Interp *interp, int objc, |
---|
793 | Tcl_Obj *const *objv) |
---|
794 | { |
---|
795 | Flow *flow = (Flow *)clientData; |
---|
796 | |
---|
797 | const char *string = Tcl_GetString(objv[1]); |
---|
798 | TransferFunction *tf; |
---|
799 | tf = flow->getTransferFunction(); |
---|
800 | if (tf == NULL) { |
---|
801 | Tcl_AppendResult(interp, "unknown transfer function \"", string, "\"", |
---|
802 | (char*)NULL); |
---|
803 | return TCL_ERROR; |
---|
804 | } |
---|
805 | const char *label; |
---|
806 | label = Tcl_GetString(objv[0]); |
---|
807 | int w, h; |
---|
808 | if ((Tcl_GetIntFromObj(interp, objv[2], &w) != TCL_OK) || |
---|
809 | (Tcl_GetIntFromObj(interp, objv[3], &h) != TCL_OK)) { |
---|
810 | return TCL_ERROR; |
---|
811 | } |
---|
812 | if (NanoVis::flags & NanoVis::MAP_FLOWS) { |
---|
813 | NanoVis::mapFlows(); |
---|
814 | } |
---|
815 | NanoVis::renderLegend(tf, NanoVis::magMin, NanoVis::magMax, w, h, label); |
---|
816 | return TCL_OK; |
---|
817 | } |
---|
818 | |
---|
819 | static Rappture::CmdSpec flowInstOps[] = { |
---|
820 | {"box", 1, FlowBoxOp, 2, 0, "oper ?args?"}, |
---|
821 | {"configure", 1, FlowConfigureOp, 2, 0, "?switches?"}, |
---|
822 | {"data", 1, FlowDataOp, 2, 0, "oper ?args?"}, |
---|
823 | {"legend", 1, FlowLegendOp, 4, 4, "w h"}, |
---|
824 | {"particles", 1, FlowParticlesOp, 2, 0, "oper ?args?"} |
---|
825 | }; |
---|
826 | static int nFlowInstOps = NumCmdSpecs(flowInstOps); |
---|
827 | |
---|
828 | /** |
---|
829 | * \brief This procedure is invoked to process commands on behalf of the flow |
---|
830 | * object. |
---|
831 | * |
---|
832 | * Side effects: See the user documentation. |
---|
833 | * |
---|
834 | * \return A standard Tcl result. |
---|
835 | */ |
---|
836 | int |
---|
837 | nv::FlowInstObjCmd(ClientData clientData, Tcl_Interp *interp, int objc, |
---|
838 | Tcl_Obj *const *objv) |
---|
839 | { |
---|
840 | Tcl_ObjCmdProc *proc; |
---|
841 | proc = Rappture::GetOpFromObj(interp, nFlowInstOps, flowInstOps, |
---|
842 | Rappture::CMDSPEC_ARG1, objc, objv, 0); |
---|
843 | if (proc == NULL) { |
---|
844 | return TCL_ERROR; |
---|
845 | } |
---|
846 | assert(CheckGL(AT)); |
---|
847 | Flow *flow = (Flow *)clientData; |
---|
848 | Tcl_Preserve(flow); |
---|
849 | int result = (*proc) (clientData, interp, objc, objv); |
---|
850 | Tcl_Release(flow); |
---|
851 | return result; |
---|
852 | } |
---|
853 | |
---|
854 | /** |
---|
855 | * \brief Deletes the command associated with the flow |
---|
856 | * |
---|
857 | * This is called only when the command associated with the flow is destroyed. |
---|
858 | */ |
---|
859 | void |
---|
860 | nv::FlowInstDeleteProc(ClientData clientData) |
---|
861 | { |
---|
862 | Flow *flow = (Flow *)clientData; |
---|
863 | NanoVis::deleteFlow(flow->name()); |
---|
864 | } |
---|
865 | |
---|
866 | static int |
---|
867 | FlowAddOp(ClientData clientData, Tcl_Interp *interp, int objc, |
---|
868 | Tcl_Obj *const *objv) |
---|
869 | { |
---|
870 | const char *name = Tcl_GetString(objv[2]); |
---|
871 | Tcl_CmdInfo cmdInfo; |
---|
872 | if (Tcl_GetCommandInfo(interp, name, &cmdInfo)) { |
---|
873 | Tcl_AppendResult(interp, "an another command \"", name, |
---|
874 | "\" already exists.", (char *)NULL); |
---|
875 | return TCL_ERROR; |
---|
876 | } |
---|
877 | Flow *flow = NanoVis::createFlow(interp, name); |
---|
878 | if (flow == NULL) { |
---|
879 | Tcl_AppendResult(interp, "Flow \"", name, "\" already exists", |
---|
880 | (char*)NULL); |
---|
881 | return TCL_ERROR; |
---|
882 | } |
---|
883 | if (flow->parseSwitches(interp, objc - 3, objv + 3) != TCL_OK) { |
---|
884 | Tcl_DeleteCommand(interp, flow->name()); |
---|
885 | return TCL_ERROR; |
---|
886 | } |
---|
887 | Tcl_SetObjResult(interp, objv[2]); |
---|
888 | NanoVis::eventuallyRedraw(); |
---|
889 | return TCL_OK; |
---|
890 | } |
---|
891 | |
---|
892 | static int |
---|
893 | FlowDeleteOp(ClientData clientData, Tcl_Interp *interp, int objc, |
---|
894 | Tcl_Obj *const *objv) |
---|
895 | { |
---|
896 | for (int i = 2; i < objc; i++) { |
---|
897 | Flow *flow = NanoVis::getFlow(Tcl_GetString(objv[i])); |
---|
898 | if (flow != NULL) { |
---|
899 | Tcl_DeleteCommandFromToken(interp, flow->getCommandToken()); |
---|
900 | } |
---|
901 | } |
---|
902 | NanoVis::eventuallyRedraw(NanoVis::MAP_FLOWS); |
---|
903 | return TCL_OK; |
---|
904 | } |
---|
905 | |
---|
906 | static int |
---|
907 | FlowExistsOp(ClientData clientData, Tcl_Interp *interp, int objc, |
---|
908 | Tcl_Obj *const *objv) |
---|
909 | { |
---|
910 | bool value = false; |
---|
911 | Flow *flow = NanoVis::getFlow(Tcl_GetString(objv[2])); |
---|
912 | if (flow != NULL) { |
---|
913 | value = true; |
---|
914 | } |
---|
915 | Tcl_SetBooleanObj(Tcl_GetObjResult(interp), (int)value); |
---|
916 | return TCL_OK; |
---|
917 | } |
---|
918 | |
---|
919 | /** |
---|
920 | * \brief flow goto number |
---|
921 | */ |
---|
922 | static int |
---|
923 | FlowGotoOp(ClientData clientData, Tcl_Interp *interp, int objc, |
---|
924 | Tcl_Obj *const *objv) |
---|
925 | { |
---|
926 | int nSteps; |
---|
927 | if (Tcl_GetIntFromObj(interp, objv[2], &nSteps) != TCL_OK) { |
---|
928 | return TCL_ERROR; |
---|
929 | } |
---|
930 | if ((nSteps < 0) || (nSteps > SHRT_MAX)) { |
---|
931 | Tcl_AppendResult(interp, "flow goto: bad # of steps \"", |
---|
932 | Tcl_GetString(objv[2]), "\"", (char *)NULL); |
---|
933 | return TCL_ERROR; |
---|
934 | } |
---|
935 | NanoVis::resetFlows(); |
---|
936 | if (NanoVis::flags & NanoVis::MAP_FLOWS) { |
---|
937 | NanoVis::mapFlows(); |
---|
938 | } |
---|
939 | NanoVis::advectFlows(); |
---|
940 | for (int i = 0; i < nSteps; i++) { |
---|
941 | if (NanoVis::licRenderer->active()) { |
---|
942 | NanoVis::licRenderer->convolve(); |
---|
943 | } |
---|
944 | NanoVis::advectFlows(); |
---|
945 | } |
---|
946 | NanoVis::eventuallyRedraw(); |
---|
947 | return TCL_OK; |
---|
948 | } |
---|
949 | |
---|
950 | static int |
---|
951 | FlowNamesOp(ClientData clientData, Tcl_Interp *interp, int objc, |
---|
952 | Tcl_Obj *const *objv) |
---|
953 | { |
---|
954 | Tcl_Obj *listObjPtr; |
---|
955 | listObjPtr = Tcl_NewListObj(0, (Tcl_Obj **) NULL); |
---|
956 | for (NanoVis::FlowHashmap::iterator itr = NanoVis::flowTable.begin(); |
---|
957 | itr != NanoVis::flowTable.end(); ++itr) { |
---|
958 | Flow *flow = itr->second; |
---|
959 | Tcl_Obj *objPtr = Tcl_NewStringObj(flow->name(), -1); |
---|
960 | Tcl_ListObjAppendElement(interp, listObjPtr, objPtr); |
---|
961 | } |
---|
962 | Tcl_SetObjResult(interp, listObjPtr); |
---|
963 | return TCL_OK; |
---|
964 | } |
---|
965 | |
---|
966 | static int |
---|
967 | FlowNextOp(ClientData clientData, Tcl_Interp *interp, int objc, |
---|
968 | Tcl_Obj *const *objv) |
---|
969 | { |
---|
970 | assert(NanoVis::licRenderer != NULL); |
---|
971 | if (NanoVis::flags & NanoVis::MAP_FLOWS) { |
---|
972 | NanoVis::mapFlows(); |
---|
973 | } |
---|
974 | NanoVis::eventuallyRedraw(); |
---|
975 | NanoVis::licRenderer->convolve(); |
---|
976 | NanoVis::advectFlows(); |
---|
977 | return TCL_OK; |
---|
978 | } |
---|
979 | |
---|
980 | static int |
---|
981 | FlowResetOp(ClientData clientData, Tcl_Interp *interp, int objc, |
---|
982 | Tcl_Obj *const *objv) |
---|
983 | { |
---|
984 | NanoVis::resetFlows(); |
---|
985 | return TCL_OK; |
---|
986 | } |
---|
987 | |
---|
988 | #ifdef HAVE_FFMPEG |
---|
989 | |
---|
990 | /** |
---|
991 | * \brief Convert a Tcl_Obj representing the video format into its |
---|
992 | * integer id. |
---|
993 | * |
---|
994 | * \param clientData Not used |
---|
995 | * \param interp Interpreter to send results back to |
---|
996 | * \param switchName Not used |
---|
997 | * \param objPtr String representation |
---|
998 | * \param record Structure record |
---|
999 | * \param offset Not used |
---|
1000 | * \param flags Not used |
---|
1001 | * |
---|
1002 | * \return The return value is a standard Tcl result. |
---|
1003 | */ |
---|
1004 | static int |
---|
1005 | VideoFormatSwitchProc(ClientData clientData, Tcl_Interp *interp, |
---|
1006 | const char *switchName, Tcl_Obj *objPtr, |
---|
1007 | char *record, int offset, int flags) |
---|
1008 | { |
---|
1009 | Tcl_Obj **formatObjPtr = (Tcl_Obj **)(record + offset); |
---|
1010 | Tcl_Obj *fmtObjPtr; |
---|
1011 | const char *string; |
---|
1012 | char c; |
---|
1013 | int length; |
---|
1014 | |
---|
1015 | string = Tcl_GetStringFromObj(objPtr, &length); |
---|
1016 | c = string[0]; |
---|
1017 | if ((c == 'm') && (length > 1) && |
---|
1018 | (strncmp(string, "mpeg", length) == 0)) { |
---|
1019 | fmtObjPtr = Tcl_NewStringObj("mpeg1video", 10); |
---|
1020 | } else if ((c == 't') && (strncmp(string, "theora", length) == 0)) { |
---|
1021 | fmtObjPtr = Tcl_NewStringObj("theora", 6); |
---|
1022 | } else if ((c == 'm') && (length > 1) && |
---|
1023 | (strncmp(string, "mov", length) == 0)) { |
---|
1024 | fmtObjPtr = Tcl_NewStringObj("mov", 3); |
---|
1025 | } else { |
---|
1026 | Tcl_AppendResult(interp, "bad video format \"", string, |
---|
1027 | "\": should be mpeg, theora, or mov", (char*)NULL); |
---|
1028 | return TCL_ERROR; |
---|
1029 | } |
---|
1030 | if (*formatObjPtr != NULL) { |
---|
1031 | Tcl_DecrRefCount(*formatObjPtr); |
---|
1032 | } |
---|
1033 | Tcl_IncrRefCount(fmtObjPtr); |
---|
1034 | *formatObjPtr = fmtObjPtr; |
---|
1035 | return TCL_OK; |
---|
1036 | } |
---|
1037 | |
---|
1038 | struct FlowVideoSwitches { |
---|
1039 | float frameRate; /**< Frame rate */ |
---|
1040 | int bitRate; /**< Video bitrate */ |
---|
1041 | int width, height; /**< Dimensions of video frame. */ |
---|
1042 | int numFrames; |
---|
1043 | Tcl_Obj *formatObjPtr; |
---|
1044 | }; |
---|
1045 | |
---|
1046 | static Rappture::SwitchParseProc VideoFormatSwitchProc; |
---|
1047 | static Rappture::SwitchCustom videoFormatSwitch = { |
---|
1048 | VideoFormatSwitchProc, NULL, 0, |
---|
1049 | }; |
---|
1050 | |
---|
1051 | static Rappture::SwitchSpec flowVideoSwitches[] = { |
---|
1052 | {Rappture::SWITCH_INT, "-bitrate", "value", |
---|
1053 | offsetof(FlowVideoSwitches, bitRate), 0}, |
---|
1054 | {Rappture::SWITCH_CUSTOM, "-format", "string", |
---|
1055 | offsetof(FlowVideoSwitches, formatObjPtr), 0, 0, &videoFormatSwitch}, |
---|
1056 | {Rappture::SWITCH_FLOAT, "-framerate", "value", |
---|
1057 | offsetof(FlowVideoSwitches, frameRate), 0}, |
---|
1058 | {Rappture::SWITCH_INT, "-height", "integer", |
---|
1059 | offsetof(FlowVideoSwitches, height), 0}, |
---|
1060 | {Rappture::SWITCH_INT, "-numframes", "count", |
---|
1061 | offsetof(FlowVideoSwitches, numFrames), 0}, |
---|
1062 | {Rappture::SWITCH_INT, "-width", "integer", |
---|
1063 | offsetof(FlowVideoSwitches, width), 0}, |
---|
1064 | {Rappture::SWITCH_END} |
---|
1065 | }; |
---|
1066 | |
---|
1067 | static bool |
---|
1068 | ppmWriteToFile(const char *path, const unsigned char *imgData, |
---|
1069 | int width, int height) |
---|
1070 | { |
---|
1071 | /* Open the named file for writing. */ |
---|
1072 | int fdOut = creat(path, 0600); |
---|
1073 | if (fdOut < 0) { |
---|
1074 | ERROR("can't open temporary image file \"%s\": %s", path, strerror(errno)); |
---|
1075 | return false; |
---|
1076 | } |
---|
1077 | // Generate the PPM binary file header |
---|
1078 | char header[200]; |
---|
1079 | #define PPM_MAXVAL 255 |
---|
1080 | sprintf(header, "P6 %d %d %d\n", width, height, |
---|
1081 | PPM_MAXVAL); |
---|
1082 | |
---|
1083 | size_t header_length = strlen(header); |
---|
1084 | size_t wordsPerRow = (width * 24 + 31) / 32; |
---|
1085 | size_t bytesPerRow = wordsPerRow * 4; |
---|
1086 | size_t rowLength = width * 3; |
---|
1087 | size_t numRecords = height + 1; |
---|
1088 | |
---|
1089 | struct iovec *iov; |
---|
1090 | iov = (struct iovec *)malloc(sizeof(struct iovec) * numRecords); |
---|
1091 | |
---|
1092 | // Add the PPM image header. |
---|
1093 | iov[0].iov_base = header; |
---|
1094 | iov[0].iov_len = header_length; |
---|
1095 | |
---|
1096 | // Now add the image data, reversing the order of the rows. |
---|
1097 | int y; |
---|
1098 | unsigned char *srcRowPtr = const_cast<unsigned char *>(imgData); |
---|
1099 | /* Reversing the pointers for the image rows. PPM is top-to-bottom. */ |
---|
1100 | for (y = height; y >= 1; y--) { |
---|
1101 | iov[y].iov_base = srcRowPtr; |
---|
1102 | iov[y].iov_len = rowLength; |
---|
1103 | srcRowPtr += bytesPerRow; |
---|
1104 | } |
---|
1105 | if (writev(fdOut, iov, numRecords) < 0) { |
---|
1106 | ERROR("writing image to \"%s\" failed: %s", path, strerror(errno)); |
---|
1107 | free(iov); |
---|
1108 | close(fdOut); |
---|
1109 | return false; |
---|
1110 | } |
---|
1111 | close(fdOut); |
---|
1112 | free(iov); |
---|
1113 | return true; |
---|
1114 | } |
---|
1115 | |
---|
1116 | static bool |
---|
1117 | MakeImageFiles(char *tmpFileName, |
---|
1118 | int width, int height, int numFrames, |
---|
1119 | bool *cancelPtr) |
---|
1120 | { |
---|
1121 | struct pollfd pollResults; |
---|
1122 | pollResults.fd = g_fdIn; |
---|
1123 | pollResults.events = POLLIN; |
---|
1124 | #define PENDING_TIMEOUT 10 /* milliseconds. */ |
---|
1125 | int timeout = PENDING_TIMEOUT; |
---|
1126 | |
---|
1127 | int oldWidth, oldHeight; |
---|
1128 | oldWidth = NanoVis::winWidth; |
---|
1129 | oldHeight = NanoVis::winHeight; |
---|
1130 | |
---|
1131 | if (width != oldWidth || |
---|
1132 | height != oldHeight) { |
---|
1133 | // Resize to the requested size. |
---|
1134 | NanoVis::resizeOffscreenBuffer(width, height); |
---|
1135 | } |
---|
1136 | NanoVis::resetFlows(); |
---|
1137 | *cancelPtr = false; |
---|
1138 | bool result = true; |
---|
1139 | size_t length = strlen(tmpFileName); |
---|
1140 | for (int i = 1; i <= numFrames; i++) { |
---|
1141 | if (((i & 0xF) == 0) && (poll(&pollResults, 1, timeout) > 0)) { |
---|
1142 | /* If there's another command on stdin, that means the client is |
---|
1143 | * trying to cancel this operation. */ |
---|
1144 | *cancelPtr = true; |
---|
1145 | break; |
---|
1146 | } |
---|
1147 | if (NanoVis::licRenderer->active()) { |
---|
1148 | NanoVis::licRenderer->convolve(); |
---|
1149 | } |
---|
1150 | NanoVis::advectFlows(); |
---|
1151 | |
---|
1152 | int fboOrig; |
---|
1153 | glGetIntegerv(GL_FRAMEBUFFER_BINDING_EXT, &fboOrig); |
---|
1154 | |
---|
1155 | NanoVis::bindOffscreenBuffer(); |
---|
1156 | NanoVis::render(); |
---|
1157 | NanoVis::readScreen(); |
---|
1158 | |
---|
1159 | glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, fboOrig); |
---|
1160 | |
---|
1161 | sprintf(tmpFileName + length, "/image%d.ppm", i); |
---|
1162 | result = ppmWriteToFile(tmpFileName, NanoVis::screenBuffer, |
---|
1163 | width, height); |
---|
1164 | if (!result) { |
---|
1165 | break; |
---|
1166 | } |
---|
1167 | } |
---|
1168 | if (width != oldWidth || |
---|
1169 | height != oldHeight) { |
---|
1170 | NanoVis::resizeOffscreenBuffer(oldWidth, oldHeight); |
---|
1171 | } |
---|
1172 | tmpFileName[length] = '\0'; |
---|
1173 | NanoVis::resetFlows(); |
---|
1174 | return result; |
---|
1175 | } |
---|
1176 | |
---|
1177 | static int |
---|
1178 | MakeMovie(Tcl_Interp *interp, char *tmpFileName, const char *token, |
---|
1179 | FlowVideoSwitches *switchesPtr) |
---|
1180 | { |
---|
1181 | #ifndef FFMPEG |
---|
1182 | # define FFMPEG "/usr/bin/ffmpeg" |
---|
1183 | #endif |
---|
1184 | /* Generate the movie from the frame images by exec-ing ffmpeg */ |
---|
1185 | /* The ffmpeg command is |
---|
1186 | * ffmpeg -f image2 -i /var/tmp/xxxxx/image%d.ppm \ |
---|
1187 | * -b bitrate -f framerate /var/tmp/xxxxx/movie.mpeg |
---|
1188 | */ |
---|
1189 | char cmd[BUFSIZ]; |
---|
1190 | sprintf(cmd, "%s -f image2 -i %s/image%%d.ppm -f %s -b %d -r %f -", |
---|
1191 | FFMPEG, tmpFileName, Tcl_GetString(switchesPtr->formatObjPtr), |
---|
1192 | switchesPtr->bitRate, switchesPtr->frameRate); |
---|
1193 | TRACE("Enter: %s", cmd); |
---|
1194 | FILE *f = popen(cmd, "r"); |
---|
1195 | if (f == NULL) { |
---|
1196 | Tcl_AppendResult(interp, "can't run ffmpeg: ", |
---|
1197 | Tcl_PosixError(interp), (char *)NULL); |
---|
1198 | return TCL_ERROR; |
---|
1199 | } |
---|
1200 | Rappture::Buffer data; |
---|
1201 | size_t total = 0; |
---|
1202 | for (;;) { |
---|
1203 | char buffer[BUFSIZ]; |
---|
1204 | |
---|
1205 | ssize_t numRead = fread(buffer, sizeof(unsigned char), BUFSIZ, f); |
---|
1206 | total += numRead; |
---|
1207 | if (numRead == 0) { // EOF |
---|
1208 | break; |
---|
1209 | } |
---|
1210 | if (numRead < 0) { // Error |
---|
1211 | ERROR("Can't read movie data: %s", |
---|
1212 | Tcl_PosixError(interp)); |
---|
1213 | Tcl_AppendResult(interp, "can't read movie data: ", |
---|
1214 | Tcl_PosixError(interp), (char *)NULL); |
---|
1215 | return TCL_ERROR; |
---|
1216 | } |
---|
1217 | if (!data.append(buffer, numRead)) { |
---|
1218 | ERROR("Can't append movie data to buffer %d bytes", |
---|
1219 | numRead); |
---|
1220 | Tcl_AppendResult(interp, "can't append movie data to buffer", |
---|
1221 | (char *)NULL); |
---|
1222 | return TCL_ERROR; |
---|
1223 | } |
---|
1224 | } |
---|
1225 | if (data.size() == 0) { |
---|
1226 | ERROR("ffmpeg returned 0 bytes"); |
---|
1227 | } |
---|
1228 | // Send zero length to client so it can deal with error |
---|
1229 | sprintf(cmd,"nv>image -type movie -token \"%s\" -bytes %lu\n", |
---|
1230 | token, (unsigned long)data.size()); |
---|
1231 | nv::sendDataToClient(cmd, data.bytes(), data.size()); |
---|
1232 | return TCL_OK; |
---|
1233 | } |
---|
1234 | |
---|
1235 | static int |
---|
1236 | FlowVideoOp(ClientData clientData, Tcl_Interp *interp, int objc, |
---|
1237 | Tcl_Obj *const *objv) |
---|
1238 | { |
---|
1239 | FlowVideoSwitches switches; |
---|
1240 | const char *token; |
---|
1241 | |
---|
1242 | token = Tcl_GetString(objv[2]); |
---|
1243 | switches.frameRate = 25.0f; // Default frame rate 25 fps |
---|
1244 | switches.bitRate = 6000000; // Default video bit rate. |
---|
1245 | switches.width = NanoVis::winWidth; |
---|
1246 | switches.height = NanoVis::winHeight; |
---|
1247 | switches.numFrames = 100; |
---|
1248 | switches.formatObjPtr = Tcl_NewStringObj("mpeg1video", 10); |
---|
1249 | Tcl_IncrRefCount(switches.formatObjPtr); |
---|
1250 | if (Rappture::ParseSwitches(interp, flowVideoSwitches, |
---|
1251 | objc - 3, objv + 3, &switches, SWITCH_DEFAULTS) < 0) { |
---|
1252 | return TCL_ERROR; |
---|
1253 | } |
---|
1254 | if ((switches.width < 0) || (switches.width > SHRT_MAX) || |
---|
1255 | (switches.height < 0) || (switches.height > SHRT_MAX)) { |
---|
1256 | Tcl_AppendResult(interp, "bad dimensions for video", (char *)NULL); |
---|
1257 | return TCL_ERROR; |
---|
1258 | } |
---|
1259 | if ((switches.frameRate < 0.0f) || (switches.frameRate > 30.0f)) { |
---|
1260 | Tcl_AppendResult(interp, "bad frame rate.", (char *)NULL); |
---|
1261 | return TCL_ERROR; |
---|
1262 | } |
---|
1263 | if (switches.bitRate < 0) { |
---|
1264 | Tcl_AppendResult(interp, "bad bit rate.", (char *)NULL); |
---|
1265 | return TCL_ERROR; |
---|
1266 | } |
---|
1267 | if (NanoVis::licRenderer == NULL) { |
---|
1268 | Tcl_AppendResult(interp, "no lic renderer.", (char *)NULL); |
---|
1269 | return TCL_ERROR; |
---|
1270 | } |
---|
1271 | TRACE("FLOW started"); |
---|
1272 | |
---|
1273 | char *tmpFileName; |
---|
1274 | char nameTemplate[200]; |
---|
1275 | strcpy(nameTemplate,"/var/tmp/flowXXXXXX"); |
---|
1276 | tmpFileName = mkdtemp(nameTemplate); |
---|
1277 | int result = TCL_OK; |
---|
1278 | if (tmpFileName == NULL) { |
---|
1279 | Tcl_AppendResult(interp, "can't create temporary directory \"", |
---|
1280 | nameTemplate, "\" for frame image files: ", |
---|
1281 | Tcl_PosixError(interp), (char *)NULL); |
---|
1282 | return TCL_ERROR; |
---|
1283 | } |
---|
1284 | size_t length = strlen(tmpFileName); |
---|
1285 | bool canceled = false; |
---|
1286 | if (MakeImageFiles(tmpFileName, |
---|
1287 | switches.width, switches.height, switches.numFrames, |
---|
1288 | &canceled)) { |
---|
1289 | result = TCL_OK; |
---|
1290 | } else { |
---|
1291 | result = TCL_ERROR; |
---|
1292 | } |
---|
1293 | if ((result == TCL_OK) && (!canceled)) { |
---|
1294 | result = MakeMovie(interp, tmpFileName, token, &switches); |
---|
1295 | } |
---|
1296 | for (int i = 1; i <= switches.numFrames; i++) { |
---|
1297 | sprintf(tmpFileName + length, "/image%d.ppm", i); |
---|
1298 | unlink(tmpFileName); |
---|
1299 | } |
---|
1300 | tmpFileName[length] = '\0'; |
---|
1301 | rmdir(tmpFileName); |
---|
1302 | Rappture::FreeSwitches(flowVideoSwitches, &switches, 0); |
---|
1303 | return result; |
---|
1304 | } |
---|
1305 | #else |
---|
1306 | /** |
---|
1307 | * Not implemented |
---|
1308 | */ |
---|
1309 | static int |
---|
1310 | FlowVideoOp(ClientData clientData, Tcl_Interp *interp, int objc, |
---|
1311 | Tcl_Obj *const *objv) |
---|
1312 | { |
---|
1313 | return TCL_OK; |
---|
1314 | } |
---|
1315 | #endif /* HAVE_FFMPEG */ |
---|
1316 | |
---|
1317 | static Rappture::CmdSpec flowCmdOps[] = { |
---|
1318 | {"add", 1, FlowAddOp, 3, 0, "name ?option value...?",}, |
---|
1319 | {"delete", 1, FlowDeleteOp, 3, 0, "name ?name...?",}, |
---|
1320 | {"exists", 1, FlowExistsOp, 3, 3, "name",}, |
---|
1321 | {"goto", 1, FlowGotoOp, 3, 3, "nSteps",}, |
---|
1322 | {"names", 1, FlowNamesOp, 2, 2, "",}, |
---|
1323 | {"next", 2, FlowNextOp, 2, 2, "",}, |
---|
1324 | {"reset", 1, FlowResetOp, 2, 2, "",}, |
---|
1325 | {"video", 1, FlowVideoOp, 3, 0, "token ?switches...?",}, |
---|
1326 | }; |
---|
1327 | static int nFlowCmdOps = NumCmdSpecs(flowCmdOps); |
---|
1328 | |
---|
1329 | static int |
---|
1330 | FlowCmdProc(ClientData clientData, Tcl_Interp *interp, int objc, |
---|
1331 | Tcl_Obj *const *objv) |
---|
1332 | { |
---|
1333 | Tcl_ObjCmdProc *proc; |
---|
1334 | |
---|
1335 | proc = Rappture::GetOpFromObj(interp, nFlowCmdOps, flowCmdOps, |
---|
1336 | Rappture::CMDSPEC_ARG1, objc, objv, 0); |
---|
1337 | if (proc == NULL) { |
---|
1338 | return TCL_ERROR; |
---|
1339 | } |
---|
1340 | return (*proc) (clientData, interp, objc, objv); |
---|
1341 | } |
---|
1342 | |
---|
1343 | /** |
---|
1344 | *\brief This procedure is invoked to initialize the "flow" command. |
---|
1345 | * |
---|
1346 | * Side effects: |
---|
1347 | * Creates the new command and adds a new entry into a global Tcl |
---|
1348 | * associative array. |
---|
1349 | */ |
---|
1350 | void |
---|
1351 | nv::FlowCmdInitProc(Tcl_Interp *interp, ClientData clientData) |
---|
1352 | { |
---|
1353 | Tcl_CreateObjCommand(interp, "flow", FlowCmdProc, clientData, NULL); |
---|
1354 | } |
---|