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