source: trunk/packages/vizservers/nanovis/Command.cpp @ 4056

Last change on this file since 4056 was 4056, checked in by ldelgass, 10 years ago

Use nv namespace for internal classes (not Rappture) to make it easier to find
dependencies on the Rappture libraries.

  • Property svn:eol-style set to native
File size: 73.3 KB
RevLine 
[2798]1/* -*- mode: c++; c-basic-offset: 4; indent-tabs-mode: nil -*- */
[834]2/*
3 * ----------------------------------------------------------------------
4 * Command.cpp
5 *
[917]6 *      This modules creates the Tcl interface to the nanovis server.  The
7 *      communication protocol of the server is the Tcl language.  Commands
8 *      given to the server by clients are executed in a safe interpreter and
9 *      the resulting image rendered offscreen is returned as BMP-formatted
10 *      image data.
[834]11 *
12 * ======================================================================
13 *  AUTHOR:  Wei Qiao <qiaow@purdue.edu>
14 *           Michael McLennan <mmclennan@purdue.edu>
15 *           Purdue Rendering and Perceptualization Lab (PURPL)
16 *
[3502]17 *  Copyright (c) 2004-2013  HUBzero Foundation, LLC
[834]18 *
19 *  See the file "license.terms" for information on usage and
20 *  redistribution of this file, and for a DISCLAIMER OF ALL WARRANTIES.
21 * ======================================================================
22 */
23
[877]24/*
25 * TODO:  In no particular order...
[887]26 *        o Use Tcl command option parser to reduce size of procedures, remove
[915]27 *          lots of extra error checking code. (almost there)
[1282]28 *        o Add bookkeeping for volumes, heightmaps, flows, etc. to track
[3597]29 *          1) simulation # 2) include/exclude.  The include/exclude
[923]30 *          is to indicate whether the item should contribute to the overall
31 *          limits of the axes.
[877]32 */
[834]33
[1328]34#include <assert.h>
35#include <stdlib.h>
[3559]36#include <unistd.h>                     /* Needed for getpid, gethostname,
37                                         * write, etc. */
[913]38#include <tcl.h>
[829]39
[1031]40#include <RpField1D.h>
41#include <RpEncode.h>
42#include <RpOutcome.h>
43#include <RpBuffer.h>
[829]44
[3492]45#include <vrmath/Vector3f.h>
46
[3605]47#include "nanovisServer.h"
[1328]48#include "nanovis.h"
[3605]49#include "ReadBuffer.h"
50#ifdef USE_THREADS
51#include "ResponseQueue.h"
52#endif
53#include "Command.h"
[1328]54#include "CmdProc.h"
[3597]55#include "FlowCmd.h"
[2846]56#include "dxReader.h"
[3870]57#ifdef USE_VTK
58#include "VtkDataSetReader.h"
59#endif
[3502]60#include "VtkReader.h"
[3605]61#include "BMPWriter.h"
62#include "PPMWriter.h"
[2831]63#include "Grid.h"
[829]64#include "HeightMap.h"
[3611]65#include "Camera.h"
66#include "ZincBlendeReconstructor.h"
[3605]67#include "OrientationIndicator.h"
[1374]68#include "Unirect.h"
[3362]69#include "Volume.h"
[2831]70#include "VolumeRenderer.h"
[3605]71#include "Trace.h"
[829]72
[3605]73using namespace nv;
[3463]74using namespace nv::graphics;
[3493]75using namespace vrmath;
[3463]76
[834]77// default transfer function
[1089]78static const char def_transfunc[] =
[917]79    "transfunc define default {\n\
[3505]80  0.00  0 0 1\n\
81  0.25  0 1 1\n\
82  0.50  0 1 0\n\
[3562]83  0.75  1 1 0\n\
[3505]84  1.00  1 0 0\n\
[834]85} {\n\
[3505]86  0.000000 0.0\n\
87  0.107847 0.0\n\
88  0.107857 1.0\n\
89  0.177857 1.0\n\
90  0.177867 0.0\n\
91  0.250704 0.0\n\
92  0.250714 1.0\n\
93  0.320714 1.0\n\
94  0.320724 0.0\n\
95  0.393561 0.0\n\
96  0.393571 1.0\n\
97  0.463571 1.0\n\
98  0.463581 0.0\n\
99  0.536419 0.0\n\
100  0.536429 1.0\n\
101  0.606429 1.0\n\
102  0.606439 0.0\n\
103  0.679276 0.0\n\
104  0.679286 1.0\n\
105  0.749286 1.0\n\
106  0.749296 0.0\n\
107  0.822133 0.0\n\
108  0.822143 1.0\n\
109  0.892143 1.0\n\
110  0.892153 0.0\n\
111  1.000000 0.0\n\
[834]112}";
[829]113
[3605]114static int lastCmdStatus;
115
116#ifdef USE_THREADS
117void
118nv::queueResponse(const void *bytes, size_t len,
[3611]119              Response::AllocationType allocType,
120              Response::ResponseType type)
[3605]121{
122    Response *response = new Response(type);
123    response->setMessage((unsigned char *)bytes, len, allocType);
124    g_queue->enqueue(response);
125}
126#else
127
128ssize_t
129nv::SocketWrite(const void *bytes, size_t len)
130{
131    size_t ofs = 0;
132    ssize_t bytesWritten;
133    while ((bytesWritten = write(g_fdOut, (const char *)bytes + ofs, len - ofs)) > 0) {
134        ofs += bytesWritten;
135        if (ofs == len)
136            break;
137    }
138    if (bytesWritten < 0) {
139        ERROR("write: %s", strerror(errno));
140    }
141    return bytesWritten;
142}
143
144#endif  /*USE_THREADS*/
145
[1374]146bool
[3605]147nv::SocketRead(char *bytes, size_t len)
148{
149    ReadBuffer::BufferStatus status;
150    status = g_inBufPtr->followingData((unsigned char *)bytes, len);
151    TRACE("followingData status: %d", status);
152    return (status == ReadBuffer::OK);
153}
154
155bool
156nv::SocketRead(Rappture::Buffer &buf, size_t len)
157{
158    ReadBuffer::BufferStatus status;
159    status = g_inBufPtr->followingData(buf, len);
160    TRACE("followingData status: %d", status);
161    return (status == ReadBuffer::OK);
162}
163
164static int
165ExecuteCommand(Tcl_Interp *interp, Tcl_DString *dsPtr)
166{
167    int result;
168#ifdef WANT_TRACE
169    char *str = Tcl_DStringValue(dsPtr);
170    std::string cmd(str);
171    cmd.erase(cmd.find_last_not_of(" \n\r\t")+1);
172    TRACE("command %lu: '%s'", g_stats.nCommands+1, cmd.c_str());
173#endif
174    lastCmdStatus = TCL_OK;
175    result = Tcl_EvalEx(interp, Tcl_DStringValue(dsPtr),
176                        Tcl_DStringLength(dsPtr),
177                        TCL_EVAL_DIRECT | TCL_EVAL_GLOBAL);
178    Tcl_DStringSetLength(dsPtr, 0);
179    if (lastCmdStatus == TCL_BREAK) {
180        return TCL_BREAK;
181    }
182    lastCmdStatus = result;
183    if (result != TCL_OK) {
184        TRACE("Error: %d", result);
185    }
186    return result;
187}
188
[3607]189int
[3611]190nv::GetBooleanFromObj(Tcl_Interp *interp, Tcl_Obj *objPtr, bool *boolPtr)
[927]191{
192    int value;
193
194    if (Tcl_GetBooleanFromObj(interp, objPtr, &value) != TCL_OK) {
195        return TCL_ERROR;
196    }
197    *boolPtr = (bool)value;
198    return TCL_OK;
199}
200
[1374]201int
[3611]202nv::GetFloatFromObj(Tcl_Interp *interp, Tcl_Obj *objPtr, float *valuePtr)
[834]203{
204    double value;
[829]205
[913]206    if (Tcl_GetDoubleFromObj(interp, objPtr, &value) != TCL_OK) {
[887]207        return TCL_ERROR;
[834]208    }
209    *valuePtr = (float)value;
[835]210    return TCL_OK;
[834]211}
212
[865]213static int
[1089]214GetCullMode(Tcl_Interp *interp, Tcl_Obj *objPtr,
[3463]215            RenderContext::CullMode *modePtr)
[865]216{
[1028]217    const char *string = Tcl_GetString(objPtr);
[865]218    if (strcmp(string, "none") == 0) {
[3463]219        *modePtr = RenderContext::NO_CULL;
[865]220    } else if (strcmp(string, "front") == 0) {
[3463]221        *modePtr = RenderContext::FRONT;
[865]222    } else if (strcmp(string, "back") == 0) {
[3463]223        *modePtr = RenderContext::BACK;
[865]224    } else {
[1089]225        Tcl_AppendResult(interp, "invalid cull mode \"", string,
[1028]226                         "\": should be front, back, or none\"", (char *)NULL);
[887]227        return TCL_ERROR;
[865]228    }
229    return TCL_OK;
230}
231
232static int
[1089]233GetShadingModel(Tcl_Interp *interp, Tcl_Obj *objPtr,
[3463]234                RenderContext::ShadingModel *modelPtr)
[865]235{
[1028]236    const char *string = Tcl_GetString(objPtr);
237
[865]238    if (strcmp(string,"flat") == 0) {
[3463]239        *modelPtr = RenderContext::FLAT;
[865]240    } else if (strcmp(string,"smooth") == 0) {
[3463]241        *modelPtr = RenderContext::SMOOTH;
[865]242    } else {
[1089]243        Tcl_AppendResult(interp, "bad shading model \"", string,
[887]244                         "\": should be flat or smooth", (char *)NULL);
245        return TCL_ERROR;
[865]246    }
247    return TCL_OK;
248}
249
250static int
[1089]251GetPolygonMode(Tcl_Interp *interp, Tcl_Obj *objPtr,
[3463]252               RenderContext::PolygonMode *modePtr)
[865]253{
[1028]254    const char *string = Tcl_GetString(objPtr);
255
[865]256    if (strcmp(string,"wireframe") == 0) {
[3463]257        *modePtr = RenderContext::LINE;
[865]258    } else if (strcmp(string,"fill") == 0) {
[3463]259        *modePtr = RenderContext::FILL;
[865]260    } else {
[1089]261        Tcl_AppendResult(interp, "invalid polygon mode \"", string,
[1028]262                         "\": should be wireframe or fill\"", (char *)NULL);
[887]263        return TCL_ERROR;
[865]264    }
265    return TCL_OK;
266}
267
[2930]268/**
269 * Creates a heightmap from the given the data. The format of the data
270 * should be as follows:
[915]271 *
[2930]272 *     xMin, xMax, xNum, yMin, yMax, yNum, heights...
[915]273 *
[2930]274 * xNum and yNum must be integer values, all others are real numbers.
275 * The number of heights must be xNum * yNum;
[915]276 */
277static HeightMap *
[1089]278CreateHeightMap(ClientData clientData, Tcl_Interp *interp, int objc,
[1028]279                Tcl_Obj *const *objv)
[915]280{
281    float xMin, yMin, xMax, yMax;
282    int xNum, yNum;
283
284    if (objc != 7) {
[1089]285        Tcl_AppendResult(interp,
[1028]286        "wrong # of values: should be xMin yMin xMax yMax xNum yNum heights",
287        (char *)NULL);
[915]288        return NULL;
289    }
[4056]290    if ((GetFloatFromObj(interp, objv[0], &xMin) != TCL_OK) ||
291        (GetFloatFromObj(interp, objv[1], &yMin) != TCL_OK) ||
292        (GetFloatFromObj(interp, objv[2], &xMax) != TCL_OK) ||
293        (GetFloatFromObj(interp, objv[3], &yMax) != TCL_OK) ||
[915]294        (Tcl_GetIntFromObj(interp, objv[4], &xNum) != TCL_OK) ||
295        (Tcl_GetIntFromObj(interp, objv[5], &yNum) != TCL_OK)) {
296        return NULL;
297    }
298    int nValues;
299    Tcl_Obj **elem;
300    if (Tcl_ListObjGetElements(interp, objv[6], &nValues, &elem) != TCL_OK) {
301        return NULL;
302    }
303    if ((xNum <= 0) || (yNum <= 0)) {
304        Tcl_AppendResult(interp, "bad number of x or y values", (char *)NULL);
305        return NULL;
306    }
307    if (nValues != (xNum * yNum)) {
308        Tcl_AppendResult(interp, "wrong # of heights", (char *)NULL);
309        return NULL;
310    }
311
312    float *heights;
313    heights = new float[nValues];
314    if (heights == NULL) {
[1089]315        Tcl_AppendResult(interp, "can't allocate array of heights",
[1028]316                         (char *)NULL);
[915]317        return NULL;
318    }
319
320    int i;
321    for (i = 0; i < nValues; i++) {
[4056]322        if (GetFloatFromObj(interp, elem[i], heights + i) != TCL_OK) {
[915]323            delete [] heights;
324            return NULL;
325        }
326    }
[3567]327    HeightMap *heightMap = new HeightMap();
328    heightMap->setHeight(xMin, yMin, xMax, yMax, xNum, yNum, heights);
329    heightMap->transferFunction(NanoVis::getTransferFunction("default"));
330    heightMap->setVisible(true);
331    heightMap->setLineContourVisible(true);
[915]332    delete [] heights;
[3567]333    return heightMap;
[915]334}
335
[932]336static int
337GetHeightMapFromObj(Tcl_Interp *interp, Tcl_Obj *objPtr, HeightMap **hmPtrPtr)
338{
[3567]339    const char *string = Tcl_GetString(objPtr);
[1544]340
[3567]341    NanoVis::HeightMapHashmap::iterator itr = NanoVis::heightMapTable.find(string);
342    if (itr == NanoVis::heightMapTable.end()) {
343        if (interp != NULL) {
344            Tcl_AppendResult(interp, "can't find a heightmap named \"",
[1544]345                         string, "\"", (char*)NULL);
[3567]346        }
[1544]347        return TCL_ERROR;
[932]348    }
[3567]349    *hmPtrPtr = itr->second;
[932]350    return TCL_OK;
351}
352
[915]353
[2930]354/**
[915]355 * Used internally to decode a series of volume index values and
356 * store then in the specified vector.  If there are no volume index
357 * arguments, this means "all volumes" to most commands, so all
358 * active volume indices are stored in the vector.
359 *
360 * Updates pushes index values into the vector.  Returns TCL_OK or
361 * TCL_ERROR to indicate an error.
362 */
[3605]363int
[3611]364nv::GetVolumeFromObj(Tcl_Interp *interp, Tcl_Obj *objPtr, Volume **volPtrPtr)
[915]365{
[3567]366    const char *string = Tcl_GetString(objPtr);
[1493]367
[3567]368    NanoVis::VolumeHashmap::iterator itr = NanoVis::volumeTable.find(string);
369    if (itr == NanoVis::volumeTable.end()) {
370        if (interp != NULL) {
371            Tcl_AppendResult(interp, "can't find a volume named \"",
[1493]372                         string, "\"", (char*)NULL);
[3567]373        }
[915]374        return TCL_ERROR;
375    }
[3567]376    *volPtrPtr = itr->second;
[915]377    return TCL_OK;
378}
379
[2930]380/**
[915]381 * Used internally to decode a series of volume index values and
382 * store then in the specified vector.  If there are no volume index
383 * arguments, this means "all volumes" to most commands, so all
384 * active volume indices are stored in the vector.
385 *
386 * Updates pushes index values into the vector.  Returns TCL_OK or
387 * TCL_ERROR to indicate an error.
388 */
389static int
[1028]390GetVolumes(Tcl_Interp *interp, int objc, Tcl_Obj *const *objv,
[2800]391           std::vector<Volume *>* vectorPtr)
[915]392{
393    if (objc == 0) {
[3567]394        // No arguments. Get all volumes.
395        NanoVis::VolumeHashmap::iterator itr;
396        for (itr = NanoVis::volumeTable.begin();
397             itr != NanoVis::volumeTable.end(); ++itr) {
398            vectorPtr->push_back(itr->second);
399        }
[915]400    } else {
[3567]401        // Get the volumes associated with the given index arguments.
[928]402        for (int n = 0; n < objc; n++) {
[3567]403            Volume *volume;
404            if (GetVolumeFromObj(interp, objv[n], &volume) != TCL_OK) {
[915]405                return TCL_ERROR;
406            }
[3567]407            vectorPtr->push_back(volume);
[932]408        }
409    }
410    return TCL_OK;
411}
412
[2930]413/**
[932]414 * Used internally to decode a series of volume index values and
415 * store then in the specified vector.  If there are no volume index
416 * arguments, this means "all volumes" to most commands, so all
417 * active volume indices are stored in the vector.
418 *
419 * Updates pushes index values into the vector.  Returns TCL_OK or
420 * TCL_ERROR to indicate an error.
421 */
422static int
[1028]423GetHeightMaps(Tcl_Interp *interp, int objc, Tcl_Obj *const *objv,
[2800]424              std::vector<HeightMap *>* vectorPtr)
[932]425{
426    if (objc == 0) {
[3567]427        // No arguments. Get all heightmaps.
428        NanoVis::HeightMapHashmap::iterator itr;
429        for (itr = NanoVis::heightMapTable.begin();
430             itr != NanoVis::heightMapTable.end(); ++itr) {
431            vectorPtr->push_back(itr->second);
432        }
[932]433    } else {
434        for (int n = 0; n < objc; n++) {
[3567]435            HeightMap *heightMap;
436            if (GetHeightMapFromObj(interp, objv[n], &heightMap) != TCL_OK) {
[932]437                return TCL_ERROR;
438            }
[3567]439            vectorPtr->push_back(heightMap);
[932]440        }
[915]441    }
442    return TCL_OK;
443}
444
[2930]445/**
[915]446 * Used internally to decode an axis value from a string ("x", "y",
447 * or "z") to its index (0, 1, or 2).  Returns TCL_OK if successful,
448 * along with a value in valPtr.  Otherwise, it returns TCL_ERROR
449 * and an error message in the interpreter.
450 */
451static int
452GetAxis(Tcl_Interp *interp, const char *string, int *indexPtr)
453{
454    if (string[1] == '\0') {
455        char c;
456
457        c = tolower((unsigned char)string[0]);
458        if (c == 'x') {
[932]459            *indexPtr = 0;
[915]460            return TCL_OK;
461        } else if (c == 'y') {
[932]462            *indexPtr = 1;
[915]463            return TCL_OK;
464        } else if (c == 'z') {
[932]465            *indexPtr = 2;
[915]466            return TCL_OK;
467        }
468        /*FALLTHRU*/
469    }
470    Tcl_AppendResult(interp, "bad axis \"", string,
[1028]471                     "\": should be x, y, or z", (char*)NULL);
[915]472    return TCL_ERROR;
473}
474
[2930]475/**
[915]476 * Used internally to decode an axis value from a string ("x", "y",
477 * or "z") to its index (0, 1, or 2).  Returns TCL_OK if successful,
478 * along with a value in indexPtr.  Otherwise, it returns TCL_ERROR
479 * and an error message in the interpreter.
480 */
[1374]481int
[3611]482nv::GetAxisFromObj(Tcl_Interp *interp, Tcl_Obj *objPtr, int *indexPtr)
[915]483{
484    return GetAxis(interp, Tcl_GetString(objPtr), indexPtr);
485}
486
[2930]487/**
[915]488 * Used internally to decode an axis value from a string ("x", "y",
489 * or "z") to its index (0, 1, or 2).  Returns TCL_OK if successful,
490 * along with a value in indexPtr.  Otherwise, it returns TCL_ERROR
491 * and an error message in the interpreter.
492 */
493static int
494GetAxisDirFromObj(Tcl_Interp *interp, Tcl_Obj *objPtr, int *indexPtr, int *dirPtr)
495{
[1028]496    const char *string = Tcl_GetString(objPtr);
[915]497
498    int sign = 1;
499    if (*string == '-') {
500        sign = -1;
501        string++;
502    }
503    if (GetAxis(interp, string, indexPtr) != TCL_OK) {
[923]504        return TCL_ERROR;
[915]505    }
506    if (dirPtr != NULL) {
[923]507        *dirPtr = sign;
[915]508    }
509    return TCL_OK;
510}
511
[2930]512/**
[915]513 * Used internally to decode a color value from a string ("R G B")
514 * as a list of three numbers 0-1.  Returns TCL_OK if successful,
515 * along with RGB values in rgbPtr.  Otherwise, it returns TCL_ERROR
516 * and an error message in the interpreter.
517 */
518static int
[1028]519GetColor(Tcl_Interp *interp, int objc, Tcl_Obj *const *objv, float *rgbPtr)
[915]520{
521    if (objc < 3) {
522        Tcl_AppendResult(interp, "missing color values\": ",
[1028]523                         "should list of R G B values 0.0 - 1.0", (char*)NULL);
[915]524        return TCL_ERROR;
525    }
526    if ((GetFloatFromObj(interp, objv[0], rgbPtr + 0) != TCL_OK) ||
527        (GetFloatFromObj(interp, objv[1], rgbPtr + 1) != TCL_OK) ||
528        (GetFloatFromObj(interp, objv[2], rgbPtr + 2) != TCL_OK)) {
529        return TCL_ERROR;
530    }
531    return TCL_OK;
532}
533
[2930]534/**
535 * Read the requested number of bytes from standard input into the given
536 * buffer.  The buffer is then decompressed and decoded.
[915]537 */
[1374]538int
[3611]539nv::GetDataStream(Tcl_Interp *interp, Rappture::Buffer &buf, int nBytes)
[915]540{
[3605]541    if (!SocketRead(buf, nBytes)) {
542        return TCL_ERROR;
[915]543    }
[1382]544    Rappture::Outcome err;
[3452]545    TRACE("Checking header[%.13s]", buf.bytes());
[1495]546    if (strncmp (buf.bytes(), "@@RP-ENC:", 9) == 0) {
[3567]547        /* There's a header on the buffer, use it to decode the data. */
548        if (!Rappture::encoding::decode(err, buf, RPENC_HDR)) {
549            Tcl_AppendResult(interp, err.remark(), (char*)NULL);
550            return TCL_ERROR;
551        }
[1508]552    } else if (Rappture::encoding::isBase64(buf.bytes(), buf.size())) {
[3567]553        /* No header, but it's base64 encoded.  It's likely that it's both
554         * base64 encoded and compressed. */
555        if (!Rappture::encoding::decode(err, buf, RPENC_B64 | RPENC_Z)) {
556            Tcl_AppendResult(interp, err.remark(), (char*)NULL);
557            return TCL_ERROR;
558        }
[1495]559    }
[915]560    return TCL_OK;
561}
[860]562
[915]563static int
[1431]564CameraAngleOp(ClientData clientData, Tcl_Interp *interp, int objc,
[1282]565              Tcl_Obj *const *objv)
[1215]566{
[1229]567    float theta, phi, psi;
568    if ((GetFloatFromObj(interp, objv[2], &phi) != TCL_OK) ||
569        (GetFloatFromObj(interp, objv[3], &theta) != TCL_OK) ||
570        (GetFloatFromObj(interp, objv[4], &psi) != TCL_OK)) {
[1215]571        return TCL_ERROR;
572    }
[3630]573    //NanoVis::orientCamera(phi, theta, psi);
574    //return TCL_OK;
575    Tcl_AppendResult(interp, "The 'camera angle' command is deprecated, use 'camera orient'", (char*)NULL);
576    return TCL_ERROR;
[1215]577}
578
579static int
[2880]580CameraOrientOp(ClientData clientData, Tcl_Interp *interp, int objc,
581               Tcl_Obj *const *objv)
582{
583    double quat[4];
584    if ((Tcl_GetDoubleFromObj(interp, objv[2], &quat[3]) != TCL_OK) ||
585        (Tcl_GetDoubleFromObj(interp, objv[3], &quat[0]) != TCL_OK) ||
586        (Tcl_GetDoubleFromObj(interp, objv[4], &quat[1]) != TCL_OK) ||
587        (Tcl_GetDoubleFromObj(interp, objv[5], &quat[2]) != TCL_OK)) {
588        return TCL_ERROR;
589    }
[3630]590    NanoVis::orientCamera(quat);
[2880]591    return TCL_OK;
592}
593
594static int
[1431]595CameraPanOp(ClientData clientData, Tcl_Interp *interp, int objc,
[1282]596             Tcl_Obj *const *objv)
[915]597{
[1238]598    float x, y;
599    if ((GetFloatFromObj(interp, objv[2], &x) != TCL_OK) ||
600        (GetFloatFromObj(interp, objv[3], &y) != TCL_OK)) {
[923]601        return TCL_ERROR;
[915]602    }
[3630]603    NanoVis::panCamera(x, y);
[834]604    return TCL_OK;
[829]605}
606
[1229]607static int
[3362]608CameraPositionOp(ClientData clientData, Tcl_Interp *interp, int objc,
609                 Tcl_Obj *const *objv)
610{
[3630]611    Vector3f pos;
612    if ((GetFloatFromObj(interp, objv[2], &pos.x) != TCL_OK) ||
613        (GetFloatFromObj(interp, objv[3], &pos.y) != TCL_OK) ||
614        (GetFloatFromObj(interp, objv[4], &pos.z) != TCL_OK)) {
[3362]615        return TCL_ERROR;
616    }
[3630]617
618    NanoVis::setCameraPosition(pos);
[3362]619    return TCL_OK;
620}
621
622static int
[3492]623CameraResetOp(ClientData clientData, Tcl_Interp *interp, int objc,
624              Tcl_Obj *const *objv)
625{
626    bool all = false;
627    if (objc == 3) {
628        const char *string = Tcl_GetString(objv[2]);
629        char c = string[0];
630        if ((c != 'a') || (strcmp(string, "all") != 0)) {
631            Tcl_AppendResult(interp, "bad camera reset option \"", string,
632                             "\": should be all", (char*)NULL);
633            return TCL_ERROR;
634        }
635        all = true;
636    }
637    NanoVis::resetCamera(all);
638    return TCL_OK;
639}
640
641static int
[1431]642CameraZoomOp(ClientData clientData, Tcl_Interp *interp, int objc,
[1282]643             Tcl_Obj *const *objv)
[1229]644{
[2854]645    float z;
646    if (GetFloatFromObj(interp, objv[2], &z) != TCL_OK) {
[1229]647        return TCL_ERROR;
648    }
[3630]649    NanoVis::zoomCamera(z);
[1229]650    return TCL_OK;
651}
652
[4056]653static CmdSpec cameraOps[] = {
[929]654    {"angle",   2, CameraAngleOp,    5, 5, "xAngle yAngle zAngle",},
[2880]655    {"orient",  1, CameraOrientOp,   6, 6, "qw qx qy qz",},
[3362]656    {"pan",     2, CameraPanOp,      4, 4, "x y",},
657    {"pos",     2, CameraPositionOp, 5, 5, "x y z",},
[3492]658    {"reset",   1, CameraResetOp,    2, 3, "?all?",},
[929]659    {"zoom",    1, CameraZoomOp,     3, 3, "factor",},
660};
[915]661static int nCameraOps = NumCmdSpecs(cameraOps);
662
[829]663static int
[1431]664CameraCmd(ClientData clientData, Tcl_Interp *interp, int objc,
[3567]665          Tcl_Obj *const *objv)
[915]666{
667    Tcl_ObjCmdProc *proc;
668
[4056]669    proc = GetOpFromObj(interp, nCameraOps, cameraOps,
670                        CMDSPEC_ARG1, objc, objv, 0);
[915]671    if (proc == NULL) {
[923]672        return TCL_ERROR;
[915]673    }
[1431]674    return (*proc) (clientData, interp, objc, objv);
[915]675}
676
677static int
[1431]678SnapshotCmd(ClientData clientData, Tcl_Interp *interp, int objc,
[3567]679            Tcl_Obj *const *objv)
[829]680{
[3605]681    int origWidth, origHeight, width, height;
[1028]682
[3605]683    origWidth = NanoVis::winWidth;
684    origHeight = NanoVis::winHeight;
685    width = 2048;
686    height = 2048;
[2930]687
[3605]688    NanoVis::resizeOffscreenBuffer(width, height);
689    NanoVis::bindOffscreenBuffer();
[3497]690    NanoVis::render();
[2877]691    NanoVis::readScreen();
[3605]692#ifdef USE_THREADS
693    queuePPM(g_queue, "nv>image -type print -bytes",
694             NanoVis::screenBuffer, width, height);
695#else
696    writePPM(g_fdOut, "nv>image -type print -bytes",
697             NanoVis::screenBuffer, width, height);
698#endif
699    NanoVis::resizeOffscreenBuffer(origWidth, origHeight);
[2930]700
[834]701    return TCL_OK;
[829]702}
703
[915]704static int
[1431]705CutplanePositionOp(ClientData clientData, Tcl_Interp *interp, int objc,
[1028]706                   Tcl_Obj *const *objv)
[915]707{
708    float relval;
709    if (GetFloatFromObj(interp, objv[2], &relval) != TCL_OK) {
[923]710        return TCL_ERROR;
[915]711    }
[1089]712
[915]713    // keep this just inside the volume so it doesn't disappear
[1089]714    if (relval < 0.01f) {
715        relval = 0.01f;
716    } else if (relval > 0.99f) {
717        relval = 0.99f;
[915]718    }
[1089]719
[915]720    int axis;
721    if (GetAxisFromObj(interp, objv[3], &axis) != TCL_OK) {
[923]722        return TCL_ERROR;
[915]723    }
[1089]724
[2800]725    std::vector<Volume *> ivol;
[923]726    if (GetVolumes(interp, objc - 4, objv + 4, &ivol) != TCL_OK) {
727        return TCL_ERROR;
[915]728    }
[2800]729    std::vector<Volume *>::iterator iter;
[915]730    for (iter = ivol.begin(); iter != ivol.end(); iter++) {
[3630]731        (*iter)->setCutplanePosition(axis, relval);
[915]732    }
733    return TCL_OK;
734}
735
736static int
[1431]737CutplaneStateOp(ClientData clientData, Tcl_Interp *interp, int objc,
[1028]738                Tcl_Obj *const *objv)
[915]739{
[927]740    bool state;
741    if (GetBooleanFromObj(interp, objv[2], &state) != TCL_OK) {
[923]742        return TCL_ERROR;
[915]743    }
[1089]744
[915]745    int axis;
746    if (GetAxisFromObj(interp, objv[3], &axis) != TCL_OK) {
[923]747        return TCL_ERROR;
[915]748    }
[1089]749
[2800]750    std::vector<Volume *> ivol;
[915]751    if (GetVolumes(interp, objc - 4, objv + 4, &ivol) != TCL_OK) {
[923]752        return TCL_ERROR;
[915]753    }
754    if (state) {
[2800]755        std::vector<Volume *>::iterator iter;
[923]756        for (iter = ivol.begin(); iter != ivol.end(); iter++) {
[2877]757            (*iter)->enableCutplane(axis);
[1089]758        }
[915]759    } else {
[2800]760        std::vector<Volume *>::iterator iter;
[923]761        for (iter = ivol.begin(); iter != ivol.end(); iter++) {
[2877]762            (*iter)->disableCutplane(axis);
[1089]763        }
[915]764    }
765    return TCL_OK;
766}
767
[3883]768static int
769CutplaneVisibleOp(ClientData clientData, Tcl_Interp *interp, int objc,
770                  Tcl_Obj *const *objv)
771{
772    bool state;
773    if (GetBooleanFromObj(interp, objv[2], &state) != TCL_OK) {
774        return TCL_ERROR;
775    }
776
777    std::vector<Volume *> ivol;
[3884]778    if (GetVolumes(interp, objc - 3, objv + 3, &ivol) != TCL_OK) {
[3883]779        return TCL_ERROR;
780    }
781    std::vector<Volume *>::iterator iter;
782    for (iter = ivol.begin(); iter != ivol.end(); iter++) {
783        (*iter)->cutplanesVisible(state);
784    }
785    return TCL_OK;
786}
787
[4056]788static CmdSpec cutplaneOps[] = {
[2292]789    {"position", 1, CutplanePositionOp, 4, 0, "relval axis ?indices?",},
790    {"state",    1, CutplaneStateOp,    4, 0, "bool axis ?indices?",},
[3883]791    {"visible",  1, CutplaneVisibleOp,  3, 0, "bool ?indices?",},
[929]792};
[915]793static int nCutplaneOps = NumCmdSpecs(cutplaneOps);
794
[829]795/*
796 * ----------------------------------------------------------------------
797 * CLIENT COMMAND:
798 *   cutplane state on|off <axis> ?<volume>...?
799 *   cutplane position <relvalue> <axis> ?<volume>...?
800 *
801 * Clients send these commands to manipulate the cutplanes in one or
802 * more data volumes.  The "state" command turns a cutplane on or
803 * off.  The "position" command changes the position to a relative
804 * value in the range 0-1.  The <axis> can be x, y, or z.  These
[867]805 * options are applied to the volumes represented by one or more
[829]806 * <volume> indices.  If no volumes are specified, then all volumes
807 * are updated.
808 * ----------------------------------------------------------------------
[834]809 */
[829]810static int
[1431]811CutplaneCmd(ClientData clientData, Tcl_Interp *interp, int objc,
[1028]812            Tcl_Obj *const *objv)
[829]813{
[915]814    Tcl_ObjCmdProc *proc;
[829]815
[4056]816    proc = GetOpFromObj(interp, nCutplaneOps, cutplaneOps,
817                        CMDSPEC_ARG1, objc, objv, 0);
[915]818    if (proc == NULL) {
[923]819        return TCL_ERROR;
[829]820    }
[1431]821    return (*proc) (clientData, interp, objc, objv);
[829]822}
823
[3330]824/*
825 * ClientInfoCmd --
826 *
[3377]827 *      Log initial values to stats file.  The first time this is called
828 *      "render_start" is written into the stats file.  Afterwards, it
829 *      is "render_info".
[3567]830 *       
[3377]831 *         clientinfo list
[3330]832 */
833static int
834ClientInfoCmd(ClientData clientData, Tcl_Interp *interp, int objc,
[3605]835              Tcl_Obj *const *objv)
[3330]836{
837    Tcl_DString ds;
[3394]838    Tcl_Obj *objPtr, *listObjPtr, **items;
[3330]839    int result;
[3394]840    int i, numItems, length;
[3330]841    char buf[BUFSIZ];
[3394]842    const char *string;
[3376]843    static int first = 1;
[3330]844
[3377]845    if (objc != 2) {
[3567]846        Tcl_AppendResult(interp, "wrong # of arguments: should be \"",
[3377]847                Tcl_GetString(objv[0]), " list\"", (char *)NULL);
[3567]848        return TCL_ERROR;
[3376]849    }
850#ifdef KEEPSTATS
[3377]851    /* Use the initial client key value pairs as the parts for a generating
852     * a unique file name. */
[4056]853    int f = getStatsFile(interp, objv[1]);
[3377]854    if (f < 0) {
[3567]855        Tcl_AppendResult(interp, "can't open stats file: ",
[3377]856                         Tcl_PosixError(interp), (char *)NULL);
[3567]857        return TCL_ERROR;
[3376]858    }
859#endif
[3394]860    listObjPtr = Tcl_NewListObj(0, (Tcl_Obj **)NULL);
861    Tcl_IncrRefCount(listObjPtr);
[3376]862    if (first) {
[3394]863        first = false;
864        objPtr = Tcl_NewStringObj("render_start", 12);
865        Tcl_ListObjAppendElement(interp, listObjPtr, objPtr);
866        /* server */
867        objPtr = Tcl_NewStringObj("server", 6);
868        Tcl_ListObjAppendElement(interp, listObjPtr, objPtr);
869        objPtr = Tcl_NewStringObj("nanovis", 7);
870        Tcl_ListObjAppendElement(interp, listObjPtr, objPtr);
871        /* pid */
872        objPtr = Tcl_NewStringObj("pid", 3);
873        Tcl_ListObjAppendElement(interp, listObjPtr, objPtr);
874        Tcl_ListObjAppendElement(interp, listObjPtr, Tcl_NewIntObj(getpid()));
875        /* machine */
876        objPtr = Tcl_NewStringObj("machine", 7);
877        Tcl_ListObjAppendElement(interp, listObjPtr, objPtr);
878        gethostname(buf, BUFSIZ-1);
879        buf[BUFSIZ-1] = '\0';
880        objPtr = Tcl_NewStringObj(buf, -1);
881        Tcl_ListObjAppendElement(interp, listObjPtr, objPtr);
[3376]882    } else {
[3394]883        objPtr = Tcl_NewStringObj("render_info", 11);
884        Tcl_ListObjAppendElement(interp, listObjPtr, objPtr);
[3376]885    }
[3394]886    Tcl_DStringInit(&ds);
[3330]887    /* date */
[3394]888    Tcl_ListObjAppendElement(interp, listObjPtr, Tcl_NewStringObj("date", 4));
[4056]889    strcpy(buf, ctime(&g_stats.start.tv_sec));
[3330]890    buf[strlen(buf) - 1] = '\0';
[3394]891    Tcl_ListObjAppendElement(interp, listObjPtr, Tcl_NewStringObj(buf, -1));
[3330]892    /* date_secs */
[3394]893    Tcl_ListObjAppendElement(interp, listObjPtr,
894                Tcl_NewStringObj("date_secs", 9));
895    Tcl_ListObjAppendElement(interp, listObjPtr,
[4056]896                Tcl_NewLongObj(g_stats.start.tv_sec));
[3330]897    /* Client arguments. */
[3394]898    if (Tcl_ListObjGetElements(interp, objv[1], &numItems, &items) != TCL_OK) {
[3567]899        return TCL_ERROR;
[3330]900    }
[3394]901    for (i = 0; i < numItems; i++) {
902        Tcl_ListObjAppendElement(interp, listObjPtr, items[i]);
[3376]903    }
[3394]904    Tcl_DStringInit(&ds);
905    string = Tcl_GetStringFromObj(listObjPtr, &length);
906    Tcl_DStringAppend(&ds, string, length);
[3330]907    Tcl_DStringAppend(&ds, "\n", 1);
[3362]908#ifdef KEEPSTATS
[4056]909    result = writeToStatsFile(f, Tcl_DStringValue(&ds),
910                              Tcl_DStringLength(&ds));
[3362]911#else
912    TRACE("clientinfo: %s", Tcl_DStringValue(&ds));
913#endif
[3330]914    Tcl_DStringFree(&ds);
[3394]915    Tcl_DecrRefCount(listObjPtr);
[3330]916    return result;
917}
918
[829]919/*
920 * ----------------------------------------------------------------------
921 * CLIENT COMMAND:
922 *   legend <volumeIndex> <width> <height>
923 *
924 * Clients use this to generate a legend image for the specified
925 * transfer function.  The legend image is a color gradient from 0
926 * to one, drawn in the given transfer function.  The resulting image
927 * is returned in the size <width> x <height>.
928 * ----------------------------------------------------------------------
929 */
930static int
[1431]931LegendCmd(ClientData clientData, Tcl_Interp *interp, int objc,
[3567]932          Tcl_Obj *const *objv)
[829]933{
[913]934    if (objc != 4) {
[1089]935        Tcl_AppendResult(interp, "wrong # args: should be \"",
[1248]936            Tcl_GetString(objv[0]), " transfunc width height\"", (char*)NULL);
[829]937        return TCL_ERROR;
938    }
939
[3605]940    const char *tfName = Tcl_GetString(objv[1]);
941    TransferFunction *tf = NanoVis::getTransferFunction(tfName);
[829]942    if (tf == NULL) {
[3605]943        Tcl_AppendResult(interp, "unknown transfer function \"", tfName, "\"",
[1282]944                             (char*)NULL);
[829]945        return TCL_ERROR;
946    }
[913]947    int w, h;
948    if ((Tcl_GetIntFromObj(interp, objv[2], &w) != TCL_OK) ||
949        (Tcl_GetIntFromObj(interp, objv[3], &h) != TCL_OK)) {
[829]950        return TCL_ERROR;
951    }
[2877]952    if (Volume::updatePending) {
953        NanoVis::setVolumeRanges();
[1250]954    }
[3605]955    NanoVis::renderLegend(tf, Volume::valueMin, Volume::valueMax, w, h, tfName);
[829]956    return TCL_OK;
957}
958
[3478]959static int
960ScreenBgColorOp(ClientData clientData, Tcl_Interp *interp, int objc,
961                Tcl_Obj *const *objv)
962{
963    float rgb[3];
964    if ((GetFloatFromObj(interp, objv[2], &rgb[0]) != TCL_OK) ||
965        (GetFloatFromObj(interp, objv[3], &rgb[1]) != TCL_OK) ||
966        (GetFloatFromObj(interp, objv[4], &rgb[2]) != TCL_OK)) {
967        return TCL_ERROR;
968    }
969    NanoVis::setBgColor(rgb);
970    return TCL_OK;
971}
972
[829]973/*
974 * ----------------------------------------------------------------------
975 * CLIENT COMMAND:
[3478]976 *   screen size <width> <height>
[829]977 *
978 * Clients send this command to set the size of the rendering area.
979 * Future images are generated at the specified width/height.
980 * ----------------------------------------------------------------------
981 */
982static int
[3478]983ScreenSizeOp(ClientData clientData, Tcl_Interp *interp, int objc,
984             Tcl_Obj *const *objv)
[829]985{
[913]986    int w, h;
[3478]987    if ((Tcl_GetIntFromObj(interp, objv[2], &w) != TCL_OK) ||
988        (Tcl_GetIntFromObj(interp, objv[3], &h) != TCL_OK)) {
[829]989        return TCL_ERROR;
990    }
[2877]991    NanoVis::resizeOffscreenBuffer(w, h);
[829]992    return TCL_OK;
993}
994
[4056]995static CmdSpec screenOps[] = {
[3478]996    {"bgcolor",  1, ScreenBgColorOp,  5, 5, "r g b",},
997    {"size",     1, ScreenSizeOp, 4, 4, "width height",},
998};
999static int nScreenOps = NumCmdSpecs(screenOps);
1000
1001static int
1002ScreenCmd(ClientData clientData, Tcl_Interp *interp, int objc,
1003          Tcl_Obj *const *objv)
1004{
1005    Tcl_ObjCmdProc *proc;
1006
[4056]1007    proc = GetOpFromObj(interp, nScreenOps, screenOps,
1008                        CMDSPEC_ARG1, objc, objv, 0);
[3478]1009    if (proc == NULL) {
1010        return TCL_ERROR;
1011    }
1012    return (*proc) (clientData, interp, objc, objv);
1013}
1014
[829]1015/*
1016 * ----------------------------------------------------------------------
1017 * CLIENT COMMAND:
1018 *   transfunc define <name> <colormap> <alphamap>
1019 *     where <colormap> = { <v> <r> <g> <b> ... }
1020 *           <alphamap> = { <v> <w> ... }
1021 *
1022 * Clients send these commands to manipulate the transfer functions.
1023 * ----------------------------------------------------------------------
1024 */
1025static int
[1431]1026TransfuncCmd(ClientData clientData, Tcl_Interp *interp, int objc,
[1028]1027             Tcl_Obj *const *objv)
[829]1028{
[913]1029    if (objc < 2) {
[1089]1030        Tcl_AppendResult(interp, "wrong # args: should be \"",
[1028]1031                Tcl_GetString(objv[0]), " option arg arg...\"", (char*)NULL);
[887]1032        return TCL_ERROR;
[829]1033    }
1034
[1028]1035    const char *string = Tcl_GetString(objv[1]);
[913]1036    char c = string[0];
[1028]1037    if ((c == 'd') && (strcmp(string, "define") == 0)) {
[913]1038        if (objc != 5) {
[1089]1039            Tcl_AppendResult(interp, "wrong # args: should be \"",
1040                Tcl_GetString(objv[0]), " define name colorMap alphaMap\"",
[1028]1041                (char*)NULL);
[829]1042            return TCL_ERROR;
1043        }
1044
1045        // decode the data and store in a series of fields
1046        Rappture::Field1D rFunc, gFunc, bFunc, wFunc;
[834]1047        int cmapc, wmapc, i;
[913]1048        Tcl_Obj **cmapv;
1049        Tcl_Obj **wmapv;
[829]1050
[887]1051        wmapv = cmapv = NULL;
[913]1052        if (Tcl_ListObjGetElements(interp, objv[3], &cmapc, &cmapv) != TCL_OK) {
[829]1053            return TCL_ERROR;
1054        }
[834]1055        if ((cmapc % 4) != 0) {
[973]1056            Tcl_AppendResult(interp, "wrong # elements is colormap: should be ",
[1028]1057                "{ v r g b ... }", (char*)NULL);
[887]1058            return TCL_ERROR;
[829]1059        }
[913]1060        if (Tcl_ListObjGetElements(interp, objv[4], &wmapc, &wmapv) != TCL_OK) {
[887]1061            return TCL_ERROR;
[829]1062        }
[834]1063        if ((wmapc % 2) != 0) {
[887]1064            Tcl_AppendResult(interp, "wrong # elements in alphamap: should be ",
[1028]1065                " { v w ... }", (char*)NULL);
[887]1066            return TCL_ERROR;
[829]1067        }
[913]1068        for (i = 0; i < cmapc; i += 4) {
[887]1069            int j;
[973]1070            double q[4];
[834]1071
[829]1072            for (j=0; j < 4; j++) {
[973]1073                if (Tcl_GetDoubleFromObj(interp, cmapv[i+j], &q[j]) != TCL_OK) {
[887]1074                    return TCL_ERROR;
[829]1075                }
[973]1076                if ((q[j] < 0.0) || (q[j] > 1.0)) {
[1089]1077                    Tcl_AppendResult(interp, "bad colormap value \"",
1078                        Tcl_GetString(cmapv[i+j]),
[1028]1079                        "\": should be in the range 0-1", (char*)NULL);
[887]1080                    return TCL_ERROR;
[829]1081                }
1082            }
[973]1083            rFunc.define(q[0], q[1]);
1084            gFunc.define(q[0], q[2]);
1085            bFunc.define(q[0], q[3]);
[829]1086        }
1087        for (i=0; i < wmapc; i += 2) {
[973]1088            double q[2];
[887]1089            int j;
[834]1090
[829]1091            for (j=0; j < 2; j++) {
[973]1092                if (Tcl_GetDoubleFromObj(interp, wmapv[i+j], &q[j]) != TCL_OK) {
[887]1093                    return TCL_ERROR;
[829]1094                }
[973]1095                if ((q[j] < 0.0) || (q[j] > 1.0)) {
[1089]1096                    Tcl_AppendResult(interp, "bad alphamap value \"",
[1028]1097                        Tcl_GetString(wmapv[i+j]),
1098                        "\": should be in the range 0-1", (char*)NULL);
[887]1099                    return TCL_ERROR;
[829]1100                }
1101            }
[973]1102            wFunc.define(q[0], q[1]);
[829]1103        }
1104        // sample the given function into discrete slots
1105        const int nslots = 256;
1106        float data[4*nslots];
1107        for (i=0; i < nslots; i++) {
[973]1108            double x = double(i)/(nslots-1);
1109            data[4*i]   = rFunc.value(x);
1110            data[4*i+1] = gFunc.value(x);
1111            data[4*i+2] = bFunc.value(x);
1112            data[4*i+3] = wFunc.value(x);
[829]1113        }
1114        // find or create this transfer function
[2877]1115        NanoVis::defineTransferFunction(Tcl_GetString(objv[2]), nslots, data);
[834]1116    } else {
[913]1117        Tcl_AppendResult(interp, "bad option \"", string,
[1028]1118                "\": should be define", (char*)NULL);
[887]1119        return TCL_ERROR;
[829]1120    }
[834]1121    return TCL_OK;
[829]1122}
1123
1124/*
1125 * ----------------------------------------------------------------------
1126 * CLIENT COMMAND:
1127 *   up axis
1128 *
1129 * Clients use this to set the "up" direction for all volumes.  Volumes
1130 * are oriented such that this direction points upward.
1131 * ----------------------------------------------------------------------
1132 */
1133static int
[1431]1134UpCmd(ClientData clientData, Tcl_Interp *interp, int objc, Tcl_Obj *const *objv)
[829]1135{
[913]1136    if (objc != 2) {
[1089]1137        Tcl_AppendResult(interp, "wrong # args: should be \"",
[1028]1138                         Tcl_GetString(objv[0]), " x|y|z|-x|-y|-z\"", (char*)NULL);
[829]1139        return TCL_ERROR;
1140    }
1141
[913]1142    int sign;
[829]1143    int axis;
[913]1144    if (GetAxisDirFromObj(interp, objv[1], &axis, &sign) != TCL_OK) {
[829]1145        return TCL_ERROR;
1146    }
[3630]1147    NanoVis::setCameraUpdir(Camera::AxisDirection((axis+1)*sign));
[829]1148    return TCL_OK;
1149}
1150
[927]1151static int
[1431]1152VolumeAnimationCaptureOp(ClientData clientData, Tcl_Interp *interp, int objc,
[1028]1153                         Tcl_Obj *const *objv)
[927]1154{
1155    int total;
1156    if (Tcl_GetIntFromObj(interp, objv[3], &total) != TCL_OK) {
[1028]1157        return TCL_ERROR;
[927]1158    }
[3605]1159    VolumeInterpolator *interpolator =
1160        NanoVis::volRenderer->getVolumeInterpolator();
[927]1161    interpolator->start();
[2877]1162    if (interpolator->isStarted()) {
[3605]1163        const char *dirName = (objc < 5) ? NULL : Tcl_GetString(objv[4]);
1164        for (int frameNum = 0; frameNum < total; ++frameNum) {
[1028]1165            float fraction;
[1089]1166
[3605]1167            fraction = ((float)frameNum) / (total - 1);
[3452]1168            TRACE("fraction : %f", fraction);
[1028]1169            interpolator->update(fraction);
[1089]1170
[2930]1171            int fboOrig;
1172            glGetIntegerv(GL_FRAMEBUFFER_BINDING_EXT, &fboOrig);
[1089]1173
[2930]1174            NanoVis::bindOffscreenBuffer();  //enable offscreen render
1175
[3497]1176            NanoVis::render();
[2877]1177            NanoVis::readScreen();
[1089]1178
[2930]1179            glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, fboOrig);
[1089]1180
[3605]1181            /* FIXME: this function requires 4-byte aligned RGB rows,
1182             * but screen buffer is now 1 byte aligned for PPM images.
1183             */
[4056]1184            writeBMPFile(frameNum, dirName,
1185                         NanoVis::screenBuffer,
1186                         NanoVis::winWidth, NanoVis::winHeight);
[1028]1187        }
[927]1188    }
1189    return TCL_OK;
1190}
1191
1192static int
[1431]1193VolumeAnimationClearOp(ClientData clientData, Tcl_Interp *interp, int objc,
[1028]1194                       Tcl_Obj *const *objv)
[927]1195{
[2877]1196    NanoVis::volRenderer->clearAnimatedVolumeInfo();
[927]1197    return TCL_OK;
1198}
1199
1200static int
[1431]1201VolumeAnimationStartOp(ClientData clientData, Tcl_Interp *interp, int objc,
[1028]1202                       Tcl_Obj *const *objv)
[927]1203{
[2877]1204    NanoVis::volRenderer->startVolumeAnimation();
[927]1205    return TCL_OK;
1206}
1207
1208static int
[1431]1209VolumeAnimationStopOp(ClientData clientData, Tcl_Interp *interp, int objc,
[1028]1210                      Tcl_Obj *const *objv)
[927]1211{
[2877]1212    NanoVis::volRenderer->stopVolumeAnimation();
[927]1213    return TCL_OK;
1214}
1215
1216static int
[1431]1217VolumeAnimationVolumesOp(ClientData clientData, Tcl_Interp *interp, int objc,
[1028]1218                         Tcl_Obj *const *objv)
[927]1219{
[2800]1220    std::vector<Volume *> volumes;
[1478]1221    if (GetVolumes(interp, objc - 3, objv + 3, &volumes) != TCL_OK) {
[1028]1222        return TCL_ERROR;
[927]1223    }
[3452]1224    TRACE("parsing volume data identifier");
[3567]1225    NanoVis::VolumeHashmap::iterator itr;
1226    for (itr = NanoVis::volumeTable.begin();
1227         itr != NanoVis::volumeTable.end(); ++itr) {
1228        NanoVis::volRenderer->addAnimatedVolume(itr->second);
[927]1229    }
1230    return TCL_OK;
1231}
1232
[4056]1233static CmdSpec volumeAnimationOps[] = {
[929]1234    {"capture",   2, VolumeAnimationCaptureOp,  4, 5, "numframes ?filename?",},
1235    {"clear",     2, VolumeAnimationClearOp,    3, 3, "",},
1236    {"start",     3, VolumeAnimationStartOp,    3, 3, "",},
1237    {"stop",      3, VolumeAnimationStopOp,     3, 3, "",},
1238    {"volumes",   1, VolumeAnimationVolumesOp,  3, 0, "?indices?",},
1239};
[927]1240
[923]1241static int nVolumeAnimationOps = NumCmdSpecs(volumeAnimationOps);
1242
1243static int
[1431]1244VolumeAnimationOp(ClientData clientData, Tcl_Interp *interp, int objc,
[1028]1245                  Tcl_Obj *const *objv)
[927]1246{
1247    Tcl_ObjCmdProc *proc;
1248
[4056]1249    proc = GetOpFromObj(interp, nVolumeAnimationOps,
1250                        volumeAnimationOps, CMDSPEC_ARG2, objc, objv, 0);
[927]1251    if (proc == NULL) {
1252        return TCL_ERROR;
1253    }
[1431]1254    return (*proc) (clientData, interp, objc, objv);
[927]1255}
1256
1257static int
[1431]1258VolumeDataFollowsOp(ClientData clientData, Tcl_Interp *interp, int objc,
[1028]1259                    Tcl_Obj *const *objv)
[923]1260{
[3452]1261    TRACE("Data Loading");
[1089]1262
[923]1263    int nbytes;
1264    if (Tcl_GetIntFromObj(interp, objv[3], &nbytes) != TCL_OK) {
1265        return TCL_ERROR;
1266    }
[3567]1267    const char *tag = Tcl_GetString(objv[4]);
[3605]1268
1269    Rappture::Buffer buf(nbytes);
[923]1270    if (GetDataStream(interp, buf, nbytes) != TCL_OK) {
1271        return TCL_ERROR;
1272    }
[3605]1273    const char *bytes = buf.bytes();
1274    size_t nBytes = buf.size();
[1089]1275
[3452]1276    TRACE("Checking header[%.20s]", bytes);
[1475]1277
[3567]1278    Volume *volume = NULL;
[2828]1279
[1526]1280    if ((nBytes > 5) && (strncmp(bytes, "<HDR>", 5) == 0)) {
[3502]1281        TRACE("ZincBlende Stream loading...");
[3611]1282        volume = (Volume *)ZincBlendeReconstructor::getInstance()->loadFromMemory(const_cast<char *>(bytes));
[3567]1283        if (volume == NULL) {
[923]1284            Tcl_AppendResult(interp, "can't get volume instance", (char *)NULL);
[1493]1285            return TCL_ERROR;
[923]1286        }
[3452]1287        TRACE("finish loading");
[3362]1288
[3567]1289        NanoVis::VolumeHashmap::iterator itr = NanoVis::volumeTable.find(tag);
1290        if (itr != NanoVis::volumeTable.end()) {
[1493]1291            Tcl_AppendResult(interp, "volume \"", tag, "\" already exists.",
[3567]1292                             (char *)NULL);
[1493]1293            return TCL_ERROR;
[3567]1294        }
1295        NanoVis::volumeTable[tag] = volume;
1296        volume->name(tag);
[3502]1297    } else if ((nBytes > 14) && (strncmp(bytes, "# vtk DataFile", 14) == 0)) {
1298        TRACE("VTK loading...");
1299        if (nBytes <= 0) {
[3567]1300            ERROR("data buffer is empty");
1301            abort();
[3502]1302        }
1303        Rappture::Outcome context;
[3870]1304#ifdef USE_VTK
1305        volume = load_vtk_volume_stream(context, tag, bytes, nBytes);
1306#else
1307        std::stringstream fdata;
1308        fdata.write(bytes, nBytes);
[3567]1309        volume = load_vtk_volume_stream(context, tag, fdata);
[3870]1310#endif
[3567]1311        if (volume == NULL) {
[3502]1312            Tcl_AppendResult(interp, context.remark(), (char*)NULL);
1313            return TCL_ERROR;
1314        }
[923]1315    } else {
[3502]1316        // **Deprecated** OpenDX format
1317        if ((nBytes > 5) && (strncmp(bytes, "<ODX>", 5) == 0)) {
1318            bytes += 5;
1319            nBytes -= 5;
[2708]1320        }
[3452]1321        TRACE("DX loading...");
[3502]1322        if (nBytes <= 0) {
1323            ERROR("data buffer is empty");
1324            abort();
1325        }
[3870]1326        std::stringstream fdata;
1327        fdata.write(bytes, nBytes);
[1382]1328        Rappture::Outcome context;
[3567]1329        volume = load_dx_volume_stream(context, tag, fdata);
1330        if (volume == NULL) {
[1382]1331            Tcl_AppendResult(interp, context.remark(), (char*)NULL);
[923]1332            return TCL_ERROR;
1333        }
1334    }
[956]1335
[3567]1336    if (volume != NULL) {
1337        volume->disableCutplane(0);
1338        volume->disableCutplane(1);
1339        volume->disableCutplane(2);
1340        volume->transferFunction(NanoVis::getTransferFunction("default"));
1341        volume->visible(true);
[1089]1342
[2877]1343        if (Volume::updatePending) {
1344            NanoVis::setVolumeRanges();
[923]1345        }
[1475]1346
[3605]1347        char info[1024];
[1282]1348        // FIXME: strlen(info) is the return value of sprintf
[3605]1349        int cmdLength =
1350            sprintf(info, "nv>data tag %s min %g max %g vmin %g vmax %g\n", tag,
1351                    volume->wAxis.min(), volume->wAxis.max(),
1352                    Volume::valueMin, Volume::valueMax);
1353#ifdef USE_THREADS
1354        queueResponse(info, cmdLength, Response::VOLATILE);
1355#else
[3608]1356        if (SocketWrite(info, (size_t)cmdLength) != (ssize_t)cmdLength) {
[3605]1357            ERROR("Short write");
[3608]1358            return TCL_ERROR;
[3605]1359        }
1360#endif
[923]1361    }
[3605]1362
[923]1363    return TCL_OK;
1364}
1365
1366static int
[1431]1367VolumeDataStateOp(ClientData clientData, Tcl_Interp *interp, int objc,
[1028]1368                  Tcl_Obj *const *objv)
[923]1369{
[927]1370    bool state;
1371    if (GetBooleanFromObj(interp, objv[3], &state) != TCL_OK) {
[923]1372        return TCL_ERROR;
1373    }
[2800]1374    std::vector<Volume *> ivol;
[923]1375    if (GetVolumes(interp, objc - 4, objv + 4, &ivol) != TCL_OK) {
1376        return TCL_ERROR;
1377    }
[2800]1378    std::vector<Volume *>::iterator iter;
[1474]1379    for (iter = ivol.begin(); iter != ivol.end(); iter++) {
[3567]1380        (*iter)->dataEnabled(state);
[923]1381    }
1382    return TCL_OK;
1383}
1384
[4056]1385static CmdSpec volumeDataOps[] = {
[3605]1386    {"follows",   1, VolumeDataFollowsOp, 5, 5, "nbytes tag",},
[929]1387    {"state",     1, VolumeDataStateOp,   4, 0, "bool ?indices?",},
1388};
[923]1389static int nVolumeDataOps = NumCmdSpecs(volumeDataOps);
1390
1391static int
[1431]1392VolumeDataOp(ClientData clientData, Tcl_Interp *interp, int objc,
[1028]1393             Tcl_Obj *const *objv)
[927]1394{
1395    Tcl_ObjCmdProc *proc;
1396
[4056]1397    proc = GetOpFromObj(interp, nVolumeDataOps, volumeDataOps,
1398                        CMDSPEC_ARG2, objc, objv, 0);
[927]1399    if (proc == NULL) {
1400        return TCL_ERROR;
1401    }
[1431]1402    return (*proc) (clientData, interp, objc, objv);
[927]1403}
1404
1405static int
[1493]1406VolumeDeleteOp(ClientData clientData, Tcl_Interp *interp, int objc,
[3567]1407               Tcl_Obj *const *objv)
[1493]1408{
[3567]1409    for (int i = 2; i < objc; i++) {
1410        Volume *volume;
1411        if (GetVolumeFromObj(interp, objv[i], &volume) != TCL_OK) {
1412            return TCL_ERROR;
1413        }
1414        NanoVis::removeVolume(volume);
[1493]1415    }
[2877]1416    NanoVis::eventuallyRedraw();
[1493]1417    return TCL_OK;
1418}
1419
1420static int
1421VolumeExistsOp(ClientData clientData, Tcl_Interp *interp, int objc,
[3567]1422               Tcl_Obj *const *objv)
[1493]1423{
1424    bool value;
[3567]1425    Volume *volume;
[1493]1426
1427    value = false;
[3567]1428    if (GetVolumeFromObj(NULL, objv[2], &volume) == TCL_OK) {
1429        value = true;
[1493]1430    }
1431    Tcl_SetBooleanObj(Tcl_GetObjResult(interp), (int)value);
1432    return TCL_OK;
1433}
1434
1435static int
1436VolumeNamesOp(ClientData clientData, Tcl_Interp *interp, int objc,
[3567]1437              Tcl_Obj *const *objv)
[1493]1438{
1439    Tcl_Obj *listObjPtr;
1440    listObjPtr = Tcl_NewListObj(0, (Tcl_Obj **) NULL);
[3567]1441    NanoVis::VolumeHashmap::iterator itr;
1442    for (itr = NanoVis::volumeTable.begin();
1443         itr != NanoVis::volumeTable.end(); ++itr) {
1444        Tcl_Obj *objPtr = Tcl_NewStringObj(itr->second->name(), -1);
1445        Tcl_ListObjAppendElement(interp, listObjPtr, objPtr);
[1493]1446    }
1447    Tcl_SetObjResult(interp, listObjPtr);
1448    return TCL_OK;
1449}
1450
1451static int
[1431]1452VolumeOutlineColorOp(ClientData clientData, Tcl_Interp *interp, int objc,
[1028]1453                     Tcl_Obj *const *objv)
[927]1454{
1455    float rgb[3];
1456    if (GetColor(interp, objc - 3, objv + 3, rgb) != TCL_OK) {
[1028]1457        return TCL_ERROR;
[927]1458    }
[2800]1459    std::vector<Volume *> ivol;
[927]1460    if (GetVolumes(interp, objc - 6, objv + 6, &ivol) != TCL_OK) {
[1028]1461        return TCL_ERROR;
[927]1462    }
[2800]1463    std::vector<Volume *>::iterator iter;
[927]1464    for (iter = ivol.begin(); iter != ivol.end(); iter++) {
[2877]1465        (*iter)->setOutlineColor(rgb);
[927]1466    }
1467    return TCL_OK;
1468}
1469
1470static int
[1431]1471VolumeOutlineStateOp(ClientData clientData, Tcl_Interp *interp, int objc,
[1028]1472                     Tcl_Obj *const *objv)
[923]1473{
[927]1474    bool state;
1475    if (GetBooleanFromObj(interp, objv[3], &state) != TCL_OK) {
[1028]1476        return TCL_ERROR;
[927]1477    }
[2800]1478    std::vector<Volume *> ivol;
[927]1479    if (GetVolumes(interp, objc - 4, objv + 4, &ivol) != TCL_OK) {
[1028]1480        return TCL_ERROR;
[927]1481    }
[2800]1482    std::vector<Volume *>::iterator iter;
[1474]1483    for (iter = ivol.begin(); iter != ivol.end(); iter++) {
[3567]1484        (*iter)->outline(state);
[927]1485    }
[923]1486    return TCL_OK;
1487}
[927]1488
[4056]1489static CmdSpec volumeOutlineOps[] = {
[929]1490    {"color",     1, VolumeOutlineColorOp,  6, 0, "r g b ?indices?",},
1491    {"state",     1, VolumeOutlineStateOp,  4, 0, "bool ?indices?",},
1492    {"visible",   1, VolumeOutlineStateOp,  4, 0, "bool ?indices?",},
1493};
[923]1494static int nVolumeOutlineOps = NumCmdSpecs(volumeOutlineOps);
1495
[927]1496static int
[1431]1497VolumeOutlineOp(ClientData clientData, Tcl_Interp *interp, int objc,
[1028]1498                Tcl_Obj *const *objv)
[927]1499{
1500    Tcl_ObjCmdProc *proc;
1501
[4056]1502    proc = GetOpFromObj(interp, nVolumeOutlineOps, volumeOutlineOps,
1503                        CMDSPEC_ARG2, objc, objv, 0);
[927]1504    if (proc == NULL) {
1505        return TCL_ERROR;
1506    }
[1431]1507    return (*proc) (clientData, interp, objc, objv);
[927]1508}
1509
1510static int
[3362]1511VolumeShadingAmbientOp(ClientData clientData, Tcl_Interp *interp, int objc,
1512                       Tcl_Obj *const *objv)
1513{
1514    float ambient;
1515    if (GetFloatFromObj(interp, objv[3], &ambient) != TCL_OK) {
1516        return TCL_ERROR;
1517    }
1518    if (ambient < 0.0f || ambient > 1.0f) {
1519        WARN("Invalid ambient coefficient requested: %g, should be [0,1]", ambient);
1520    }
1521    std::vector<Volume *> ivol;
1522    if (GetVolumes(interp, objc - 4, objv + 4, &ivol) != TCL_OK) {
1523        return TCL_ERROR;
1524    }
1525    std::vector<Volume *>::iterator iter;
1526    for (iter = ivol.begin(); iter != ivol.end(); iter++) {
1527        (*iter)->ambient(ambient);
1528    }
1529    return TCL_OK;
1530}
1531
1532static int
[1431]1533VolumeShadingDiffuseOp(ClientData clientData, Tcl_Interp *interp, int objc,
[1028]1534                       Tcl_Obj *const *objv)
[927]1535{
1536    float diffuse;
1537    if (GetFloatFromObj(interp, objv[3], &diffuse) != TCL_OK) {
[1028]1538        return TCL_ERROR;
[927]1539    }
[2877]1540    if (diffuse < 0.0f || diffuse > 1.0f) {
1541        WARN("Invalid diffuse coefficient requested: %g, should be [0,1]", diffuse);
1542    }
[2800]1543    std::vector<Volume *> ivol;
[927]1544    if (GetVolumes(interp, objc - 4, objv + 4, &ivol) != TCL_OK) {
[1028]1545        return TCL_ERROR;
[927]1546    }
[2800]1547    std::vector<Volume *>::iterator iter;
[927]1548    for (iter = ivol.begin(); iter != ivol.end(); iter++) {
[1474]1549        (*iter)->diffuse(diffuse);
[927]1550    }
1551    return TCL_OK;
1552}
1553
1554static int
[1431]1555VolumeShadingIsosurfaceOp(ClientData clientData, Tcl_Interp *interp, int objc,
[1028]1556                          Tcl_Obj *const *objv)
[927]1557{
1558    bool iso_surface;
1559    if (GetBooleanFromObj(interp, objv[3], &iso_surface) != TCL_OK) {
[1028]1560        return TCL_ERROR;
[927]1561    }
[2800]1562    std::vector<Volume *> ivol;
[927]1563    if (GetVolumes(interp, objc - 4, objv + 4, &ivol) != TCL_OK) {
[1028]1564        return TCL_ERROR;
[927]1565    }
[2800]1566    std::vector<Volume *>::iterator iter;
[927]1567    for (iter = ivol.begin(); iter != ivol.end(); iter++) {
[1478]1568        (*iter)->isosurface(iso_surface);
[927]1569    }
1570    return TCL_OK;
1571}
1572
1573static int
[3362]1574VolumeShadingLight2SideOp(ClientData clientData, Tcl_Interp *interp, int objc,
1575                          Tcl_Obj *const *objv)
1576{
1577    bool twoSidedLighting;
1578    if (GetBooleanFromObj(interp, objv[3], &twoSidedLighting) != TCL_OK) {
1579        return TCL_ERROR;
1580    }
1581    std::vector<Volume *> ivol;
1582    if (GetVolumes(interp, objc - 4, objv + 4, &ivol) != TCL_OK) {
1583        return TCL_ERROR;
1584    }
1585    std::vector<Volume *>::iterator iter;
1586    for (iter = ivol.begin(); iter != ivol.end(); iter++) {
1587        (*iter)->twoSidedLighting(twoSidedLighting);
1588    }
1589    return TCL_OK;
1590}
1591
1592static int
[1431]1593VolumeShadingOpacityOp(ClientData clientData, Tcl_Interp *interp, int objc,
[1028]1594                       Tcl_Obj *const *objv)
[927]1595{
[1000]1596
[927]1597    float opacity;
1598    if (GetFloatFromObj(interp, objv[3], &opacity) != TCL_OK) {
[1028]1599        return TCL_ERROR;
[927]1600    }
[3452]1601    TRACE("set opacity %f", opacity);
[2800]1602    std::vector<Volume *> ivol;
[927]1603    if (GetVolumes(interp, objc - 4, objv + 4, &ivol) != TCL_OK) {
[1028]1604        return TCL_ERROR;
[927]1605    }
[2800]1606    std::vector<Volume *>::iterator iter;
[927]1607    for (iter = ivol.begin(); iter != ivol.end(); iter++) {
[2877]1608        (*iter)->opacityScale(opacity);
[927]1609    }
1610    return TCL_OK;
1611}
1612
1613static int
[1431]1614VolumeShadingSpecularOp(ClientData clientData, Tcl_Interp *interp, int objc,
[1028]1615                        Tcl_Obj *const *objv)
[927]1616{
1617    float specular;
1618    if (GetFloatFromObj(interp, objv[3], &specular) != TCL_OK) {
[1028]1619        return TCL_ERROR;
[927]1620    }
[3362]1621    if (specular < 0.0f || specular > 1.0f) {
1622        WARN("Invalid specular coefficient requested: %g, should be [0,1]", specular);
[2877]1623    }
[2800]1624    std::vector<Volume *> ivol;
[927]1625    if (GetVolumes(interp, objc - 4, objv + 4, &ivol) != TCL_OK) {
[1028]1626        return TCL_ERROR;
[927]1627    }
[2800]1628    std::vector<Volume *>::iterator iter;
[927]1629    for (iter = ivol.begin(); iter != ivol.end(); iter++) {
[3362]1630        (*iter)->specularLevel(specular);
[927]1631    }
1632    return TCL_OK;
1633}
1634
1635static int
[3362]1636VolumeShadingSpecularExpOp(ClientData clientData, Tcl_Interp *interp, int objc,
1637                           Tcl_Obj *const *objv)
1638{
1639    float specularExp;
1640    if (GetFloatFromObj(interp, objv[3], &specularExp) != TCL_OK) {
1641        return TCL_ERROR;
1642    }
1643    if (specularExp < 0.0f || specularExp > 128.0f) {
1644        WARN("Invalid specular exponent requested: %g, should be [0,128]", specularExp);
1645    }
1646    std::vector<Volume *> ivol;
1647    if (GetVolumes(interp, objc - 4, objv + 4, &ivol) != TCL_OK) {
1648        return TCL_ERROR;
1649    }
1650    std::vector<Volume *>::iterator iter;
1651    for (iter = ivol.begin(); iter != ivol.end(); iter++) {
1652        (*iter)->specularExponent(specularExp);
1653    }
1654    return TCL_OK;
1655}
1656
1657static int
[1431]1658VolumeShadingTransFuncOp(ClientData clientData, Tcl_Interp *interp, int objc,
[1028]1659                         Tcl_Obj *const *objv)
[927]1660{
[1028]1661    const char *name = Tcl_GetString(objv[3]);
[3567]1662    TransferFunction *tf = NanoVis::getTransferFunction(name);
1663    if (tf == NULL) {
[1028]1664        Tcl_AppendResult(interp, "transfer function \"", name,
1665                         "\" is not defined", (char*)NULL);
1666        return TCL_ERROR;
[927]1667    }
[2800]1668    std::vector<Volume *> ivol;
[927]1669    if (GetVolumes(interp, objc - 4, objv + 4, &ivol) != TCL_OK) {
[1028]1670        return TCL_ERROR;
[927]1671    }
[2800]1672    std::vector<Volume *>::iterator iter;
[3568]1673    for (iter = ivol.begin(); iter != ivol.end(); ++iter) {
[3567]1674        TRACE("setting %s with transfer function %s", (*iter)->name(),
1675               tf->name());
1676        (*iter)->transferFunction(tf);
[927]1677    }
1678    return TCL_OK;
1679}
1680
[4056]1681static CmdSpec volumeShadingOps[] = {
[3362]1682    {"ambient",       1, VolumeShadingAmbientOp,     4, 0, "value ?indices?",},
1683    {"diffuse",       1, VolumeShadingDiffuseOp,     4, 0, "value ?indices?",},
1684    {"isosurface",    1, VolumeShadingIsosurfaceOp,  4, 0, "bool ?indices?",},
1685    {"light2side",    1, VolumeShadingLight2SideOp,  4, 0, "bool ?indices?",},
1686    {"opacity",       1, VolumeShadingOpacityOp,     4, 0, "value ?indices?",},
1687    {"specularExp",   9, VolumeShadingSpecularExpOp, 4, 0, "value ?indices?",},
1688    {"specularLevel", 9, VolumeShadingSpecularOp,    4, 0, "value ?indices?",},
1689    {"transfunc",     1, VolumeShadingTransFuncOp,   4, 0, "funcName ?indices?",},
[929]1690};
[923]1691static int nVolumeShadingOps = NumCmdSpecs(volumeShadingOps);
1692
[927]1693static int
[1431]1694VolumeShadingOp(ClientData clientData, Tcl_Interp *interp, int objc,
[1028]1695                Tcl_Obj *const *objv)
[927]1696{
1697    Tcl_ObjCmdProc *proc;
1698
[4056]1699    proc = GetOpFromObj(interp, nVolumeShadingOps, volumeShadingOps,
1700                        CMDSPEC_ARG2, objc, objv, 0);
[927]1701    if (proc == NULL) {
1702        return TCL_ERROR;
1703    }
[1431]1704    return (*proc) (clientData, interp, objc, objv);
[927]1705}
1706
1707static int
[1431]1708VolumeStateOp(ClientData clientData, Tcl_Interp *interp, int objc,
[1028]1709              Tcl_Obj *const *objv)
[927]1710{
1711    bool state;
1712    if (GetBooleanFromObj(interp, objv[2], &state) != TCL_OK) {
[1028]1713        return TCL_ERROR;
[927]1714    }
[2800]1715    std::vector<Volume *> ivol;
[927]1716    if (GetVolumes(interp, objc - 3, objv + 3, &ivol) != TCL_OK) {
[1028]1717        return TCL_ERROR;
[927]1718    }
[2800]1719    std::vector<Volume *>::iterator iter;
[1474]1720    for (iter = ivol.begin(); iter != ivol.end(); iter++) {
[3567]1721        (*iter)->visible(state);
[927]1722    }
1723    return TCL_OK;
1724}
1725
[4056]1726static CmdSpec volumeOps[] = {
[930]1727    {"animation", 2, VolumeAnimationOp,   3, 0, "oper ?args?",},
[1493]1728    {"data",      2, VolumeDataOp,        3, 0, "oper ?args?",},
1729    {"delete",    2, VolumeDeleteOp,      3, 0, "?name...?",},
1730    {"exists",    1, VolumeExistsOp,      3, 3, "name",},
[3605]1731    {"names",     1, VolumeNamesOp,       2, 2, "",},
[929]1732    {"outline",   1, VolumeOutlineOp,     3, 0, "oper ?args?",},
1733    {"shading",   2, VolumeShadingOp,     3, 0, "oper ?args?",},
1734    {"state",     2, VolumeStateOp,       3, 0, "bool ?indices?",},
1735};
[923]1736static int nVolumeOps = NumCmdSpecs(volumeOps);
1737
[829]1738static int
[1431]1739VolumeCmd(ClientData clientData, Tcl_Interp *interp, int objc,
[3567]1740          Tcl_Obj *const *objv)
[829]1741{
[927]1742    Tcl_ObjCmdProc *proc;
1743
[4056]1744    proc = GetOpFromObj(interp, nVolumeOps, volumeOps,
1745                        CMDSPEC_ARG1, objc, objv, 0);
[927]1746    if (proc == NULL) {
1747        return TCL_ERROR;
1748    }
[1431]1749    return (*proc) (clientData, interp, objc, objv);
[927]1750}
1751
[1282]1752static int
[1431]1753HeightMapDataFollowsOp(ClientData clientData, Tcl_Interp *interp, int objc,
[1028]1754                       Tcl_Obj *const *objv)
[913]1755{
1756    int nBytes;
1757    if (Tcl_GetIntFromObj(interp, objv[3], &nBytes) != TCL_OK) {
[923]1758        return TCL_ERROR;
[913]1759    }
[3567]1760    const char *tag = Tcl_GetString(objv[4]);
[1544]1761
[3605]1762    Rappture::Buffer buf(nBytes);
[913]1763    if (GetDataStream(interp, buf, nBytes) != TCL_OK) {
[923]1764        return TCL_ERROR;
[913]1765    }
[4056]1766    Unirect2d data(1);
[2922]1767    if (data.parseBuffer(interp, buf) != TCL_OK) {
[3567]1768        return TCL_ERROR;
[1374]1769    }
[1544]1770    if (data.nValues() == 0) {
[3567]1771        Tcl_AppendResult(interp, "no data found in stream", (char *)NULL);
1772        return TCL_ERROR;
[913]1773    }
[1544]1774    if (!data.isInitialized()) {
[3567]1775        return TCL_ERROR;
[1374]1776    }
[3567]1777    HeightMap *heightMap;
1778    NanoVis::HeightMapHashmap::iterator itr = NanoVis::heightMapTable.find(tag);
1779    if (itr != NanoVis::heightMapTable.end()) {
1780        heightMap = itr->second;
[1544]1781    } else {
[3567]1782        heightMap = new HeightMap();
1783        NanoVis::heightMapTable[tag] = heightMap;
[1544]1784    }
[3567]1785    TRACE("Number of heightmaps=%d", NanoVis::heightMapTable.size());
[1374]1786    // Must set units before the heights.
[3567]1787    heightMap->xAxis.units(data.xUnits());
1788    heightMap->yAxis.units(data.yUnits());
1789    heightMap->zAxis.units(data.vUnits());
1790    heightMap->wAxis.units(data.yUnits());
1791    heightMap->setHeight(data.xMin(), data.yMin(), data.xMax(), data.yMax(),
1792                         data.xNum(), data.yNum(), data.transferValues());
1793    heightMap->transferFunction(NanoVis::getTransferFunction("default"));
1794    heightMap->setVisible(true);
1795    heightMap->setLineContourVisible(true);
[2877]1796    NanoVis::eventuallyRedraw();
[1374]1797    return TCL_OK;
[913]1798}
1799
1800static int
[1431]1801HeightMapDataVisibleOp(ClientData clientData, Tcl_Interp *interp, int objc,
[1028]1802                       Tcl_Obj *const *objv)
[913]1803{
[927]1804    bool visible;
1805    if (GetBooleanFromObj(interp, objv[3], &visible) != TCL_OK) {
[923]1806        return TCL_ERROR;
[913]1807    }
[2800]1808    std::vector<HeightMap *> imap;
[932]1809    if (GetHeightMaps(interp, objc - 4, objv + 4, &imap) != TCL_OK) {
[923]1810        return TCL_ERROR;
[913]1811    }
[2800]1812    std::vector<HeightMap *>::iterator iter;
[932]1813    for (iter = imap.begin(); iter != imap.end(); iter++) {
[1028]1814        (*iter)->setVisible(visible);
[913]1815    }
[2877]1816    NanoVis::eventuallyRedraw();
[913]1817    return TCL_OK;
1818}
1819
[4056]1820static CmdSpec heightMapDataOps[] = {
[3605]1821    {"follows",  1, HeightMapDataFollowsOp, 5, 5, "size heightmapName",},
1822    {"visible",  1, HeightMapDataVisibleOp, 4, 0, "bool ?heightmapNames...?",},
[929]1823};
[913]1824static int nHeightMapDataOps = NumCmdSpecs(heightMapDataOps);
1825
[1089]1826static int
[1431]1827HeightMapDataOp(ClientData clientData, Tcl_Interp *interp, int objc,
[1028]1828                Tcl_Obj *const *objv)
[829]1829{
[913]1830    Tcl_ObjCmdProc *proc;
[837]1831
[4056]1832    proc = GetOpFromObj(interp, nHeightMapDataOps, heightMapDataOps,
1833                        CMDSPEC_ARG2, objc, objv, 0);
[913]1834    if (proc == NULL) {
[923]1835        return TCL_ERROR;
[913]1836    }
[1431]1837    return (*proc) (clientData, interp, objc, objv);
[913]1838}
[837]1839
1840
[913]1841static int
[1431]1842HeightMapLineContourColorOp(ClientData clientData, Tcl_Interp *interp, int objc,
[1028]1843                            Tcl_Obj *const *objv)
[913]1844{
1845    float rgb[3];
1846    if (GetColor(interp, objc - 3, objv + 3, rgb) != TCL_OK) {
[923]1847        return TCL_ERROR;
[1089]1848    }
[2800]1849    std::vector<HeightMap *> imap;
[932]1850    if (GetHeightMaps(interp, objc - 6, objv + 6, &imap) != TCL_OK) {
[923]1851        return TCL_ERROR;
[829]1852    }
[2800]1853    std::vector<HeightMap *>::iterator iter;
[932]1854    for (iter = imap.begin(); iter != imap.end(); iter++) {
[1028]1855        (*iter)->setLineContourColor(rgb);
[913]1856    }
[2877]1857    NanoVis::eventuallyRedraw();
[913]1858    return TCL_OK;
1859}
1860
1861static int
[1431]1862HeightMapLineContourVisibleOp(ClientData clientData, Tcl_Interp *interp,
[3567]1863                              int objc, Tcl_Obj *const *objv)
[913]1864{
1865    bool visible;
[927]1866    if (GetBooleanFromObj(interp, objv[3], &visible) != TCL_OK) {
[923]1867        return TCL_ERROR;
[913]1868    }
[2800]1869    std::vector<HeightMap *> imap;
[932]1870    if (GetHeightMaps(interp, objc - 4, objv + 4, &imap) != TCL_OK) {
[923]1871        return TCL_ERROR;
[913]1872    }
[2800]1873    std::vector<HeightMap *>::iterator iter;
[932]1874    for (iter = imap.begin(); iter != imap.end(); iter++) {
[1028]1875        (*iter)->setLineContourVisible(visible);
[913]1876    }
[2877]1877    NanoVis::eventuallyRedraw();
[913]1878    return TCL_OK;
1879}
1880
[4056]1881static CmdSpec heightMapLineContourOps[] = {
[3605]1882    {"color",   1, HeightMapLineContourColorOp,   6, 0, "r g b ?heightmapNames...?",},
1883    {"visible", 1, HeightMapLineContourVisibleOp, 4, 0, "bool ?heightmapNames...?",},
[929]1884};
[913]1885static int nHeightMapLineContourOps = NumCmdSpecs(heightMapLineContourOps);
1886
1887static int
[1431]1888HeightMapLineContourOp(ClientData clientData, Tcl_Interp *interp, int objc,
[1028]1889                       Tcl_Obj *const *objv)
[913]1890{
1891    Tcl_ObjCmdProc *proc;
1892
[4056]1893    proc = GetOpFromObj(interp, nHeightMapLineContourOps,
1894                        heightMapLineContourOps, CMDSPEC_ARG2, objc, objv, 0);
[913]1895    if (proc == NULL) {
[923]1896        return TCL_ERROR;
[913]1897    }
[1431]1898    return (*proc) (clientData, interp, objc, objv);
[913]1899}
1900
1901static int
[1431]1902HeightMapCullOp(ClientData clientData, Tcl_Interp *interp, int objc,
[1028]1903                Tcl_Obj *const *objv)
[913]1904{
[3463]1905    RenderContext::CullMode mode;
[913]1906    if (GetCullMode(interp, objv[2], &mode) != TCL_OK) {
[923]1907        return TCL_ERROR;
[913]1908    }
1909    NanoVis::renderContext->setCullMode(mode);
[2877]1910    NanoVis::eventuallyRedraw();
[913]1911    return TCL_OK;
1912}
1913
1914static int
[1431]1915HeightMapCreateOp(ClientData clientData, Tcl_Interp *interp, int objc,
[1028]1916                  Tcl_Obj *const *objv)
[913]1917{
[3567]1918    const char *tag = Tcl_GetString(objv[2]);
1919    NanoVis::HeightMapHashmap::iterator itr = NanoVis::heightMapTable.find(tag);
1920    if (itr != NanoVis::heightMapTable.end()) {
1921        Tcl_AppendResult(interp, "heightmap \"", tag, "\" already exists.",
1922                         (char *)NULL);
1923        return TCL_ERROR;
[1544]1924    }
[913]1925    /* heightmap create xmin ymin xmax ymax xnum ynum values */
[3567]1926    HeightMap *heightMap = CreateHeightMap(clientData, interp, objc - 3, objv + 3);
1927    if (heightMap == NULL) {
[923]1928        return TCL_ERROR;
[913]1929    }
[3567]1930    NanoVis::heightMapTable[tag] = heightMap;
[2877]1931    NanoVis::eventuallyRedraw();
[3567]1932    TRACE("Number of heightmaps=%d", NanoVis::heightMapTable.size());
[913]1933    return TCL_OK;
1934}
[829]1935
[913]1936static int
[1431]1937HeightMapLegendOp(ClientData clientData, Tcl_Interp *interp, int objc,
[1028]1938                  Tcl_Obj *const *objv)
[913]1939{
[932]1940    HeightMap *hmPtr;
1941    if (GetHeightMapFromObj(interp, objv[2], &hmPtr) != TCL_OK) {
[923]1942        return TCL_ERROR;
[913]1943    }
[1544]1944    const char *tag;
1945    tag = Tcl_GetString(objv[2]);
[1493]1946    TransferFunction *tfPtr;
1947    tfPtr = hmPtr->transferFunction();
1948    if (tfPtr == NULL) {
[1544]1949        Tcl_AppendResult(interp, "no transfer function defined for heightmap"
[3567]1950                         " \"", tag, "\"", (char*)NULL);
[923]1951        return TCL_ERROR;
[913]1952    }
1953    int w, h;
[1089]1954    if ((Tcl_GetIntFromObj(interp, objv[3], &w) != TCL_OK) ||
[923]1955        (Tcl_GetIntFromObj(interp, objv[4], &h) != TCL_OK)) {
1956        return TCL_ERROR;
[913]1957    }
[2877]1958    if (HeightMap::updatePending) {
1959        NanoVis::setHeightmapRanges();
[932]1960    }
[2877]1961    NanoVis::renderLegend(tfPtr, HeightMap::valueMin, HeightMap::valueMax,
1962                          w, h, tag);
[913]1963    return TCL_OK;
1964}
[834]1965
[913]1966static int
[1431]1967HeightMapPolygonOp(ClientData clientData, Tcl_Interp *interp, int objc,
[1028]1968                   Tcl_Obj *const *objv)
[913]1969{
[3463]1970    RenderContext::PolygonMode mode;
[913]1971    if (GetPolygonMode(interp, objv[2], &mode) != TCL_OK) {
[923]1972        return TCL_ERROR;
[913]1973    }
1974    NanoVis::renderContext->setPolygonMode(mode);
[2877]1975    NanoVis::eventuallyRedraw();
[913]1976    return TCL_OK;
1977}
[834]1978
[913]1979static int
[1431]1980HeightMapShadingOp(ClientData clientData, Tcl_Interp *interp, int objc,
[1028]1981                 Tcl_Obj *const *objv)
[913]1982{
[3463]1983    RenderContext::ShadingModel model;
[913]1984    if (GetShadingModel(interp, objv[2], &model) != TCL_OK) {
[923]1985        return TCL_ERROR;
[913]1986    }
1987    NanoVis::renderContext->setShadingModel(model);
[2877]1988    NanoVis::eventuallyRedraw();
[913]1989    return TCL_OK;
1990}
[829]1991
[913]1992static int
[1431]1993HeightMapTransFuncOp(ClientData clientData, Tcl_Interp *interp, int objc,
[1028]1994                     Tcl_Obj *const *objv)
[913]1995{
[1028]1996    const char *name;
[913]1997    name = Tcl_GetString(objv[2]);
[3567]1998    TransferFunction *tf = NanoVis::getTransferFunction(name);
1999    if (tf == NULL) {
[923]2000        Tcl_AppendResult(interp, "transfer function \"", name,
2001                         "\" is not defined", (char*)NULL);
2002        return TCL_ERROR;
[913]2003    }
[2800]2004    std::vector<HeightMap *> imap;
[932]2005    if (GetHeightMaps(interp, objc - 3, objv + 3, &imap) != TCL_OK) {
[923]2006        return TCL_ERROR;
[913]2007    }
[2800]2008    std::vector<HeightMap *>::iterator iter;
[932]2009    for (iter = imap.begin(); iter != imap.end(); iter++) {
[3567]2010        (*iter)->transferFunction(tf);
[913]2011    }
[2877]2012    NanoVis::eventuallyRedraw();
[913]2013    return TCL_OK;
2014}
2015
[1546]2016static int
2017HeightMapOpacityOp(ClientData clientData, Tcl_Interp *interp, int objc,
2018                   Tcl_Obj *const *objv)
2019{
2020    float opacity;
2021    if (GetFloatFromObj(interp, objv[2], &opacity) != TCL_OK) {
[3567]2022        return TCL_ERROR;
[1546]2023    }
[2800]2024    std::vector<HeightMap *> heightmaps;
[1546]2025    if (GetHeightMaps(interp, objc - 3, objv + 3, &heightmaps) != TCL_OK) {
2026        return TCL_ERROR;
2027    }
[2800]2028    std::vector<HeightMap *>::iterator iter;
[1546]2029    for (iter = heightmaps.begin(); iter != heightmaps.end(); iter++) {
2030        (*iter)->opacity(opacity);
2031    }
[2877]2032    NanoVis::eventuallyRedraw();
[1546]2033    return TCL_OK;
2034}
2035
[4056]2036static CmdSpec heightMapOps[] = {
[3605]2037    {"create",       2, HeightMapCreateOp,      10, 10, "heightmapName xmin ymin xmax ymax xnum ynum values",},
[929]2038    {"cull",         2, HeightMapCullOp,        3, 3, "mode",},
2039    {"data",         1, HeightMapDataOp,        3, 0, "oper ?args?",},
[3605]2040    {"legend",       2, HeightMapLegendOp,      5, 5, "heightmapName width height",},
[929]2041    {"linecontour",  2, HeightMapLineContourOp, 2, 0, "oper ?args?",},
[3605]2042    {"opacity",      1, HeightMapOpacityOp,     3, 0, "value ?heightmapNames...? ",},
[929]2043    {"polygon",      1, HeightMapPolygonOp,     3, 3, "mode",},
[932]2044    {"shading",      1, HeightMapShadingOp,     3, 3, "model",},
[3605]2045    {"transfunc",    2, HeightMapTransFuncOp,   3, 0, "name ?heightmapNames...?",},
[929]2046};
[913]2047static int nHeightMapOps = NumCmdSpecs(heightMapOps);
2048
[1089]2049static int
[1431]2050HeightMapCmd(ClientData clientData, Tcl_Interp *interp, int objc,
[3567]2051             Tcl_Obj *const *objv)
[1089]2052{
[913]2053    Tcl_ObjCmdProc *proc;
2054
[4056]2055    proc = GetOpFromObj(interp, nHeightMapOps, heightMapOps,
2056                        CMDSPEC_ARG1, objc, objv, 0);
[913]2057    if (proc == NULL) {
[923]2058        return TCL_ERROR;
[913]2059    }
[1431]2060    return (*proc) (clientData, interp, objc, objv);
[913]2061}
2062
[915]2063static int
[1431]2064GridAxisColorOp(ClientData clientData, Tcl_Interp *interp, int objc,
[1028]2065                Tcl_Obj *const *objv)
[829]2066{
[915]2067    float r, g, b, a;
2068    if ((GetFloatFromObj(interp, objv[2], &r) != TCL_OK) ||
[923]2069        (GetFloatFromObj(interp, objv[3], &g) != TCL_OK) ||
2070        (GetFloatFromObj(interp, objv[4], &b) != TCL_OK)) {
2071        return TCL_ERROR;
[867]2072    }
[915]2073    a = 1.0f;
2074    if ((objc == 6) && (GetFloatFromObj(interp, objv[5], &a) != TCL_OK)) {
[923]2075        return TCL_ERROR;
[1089]2076    }
[915]2077    if (NanoVis::grid) {
[923]2078        NanoVis::grid->setAxisColor(r, g, b, a);
[829]2079    }
[834]2080    return TCL_OK;
2081}
[829]2082
[834]2083static int
[1431]2084GridAxisNameOp(ClientData clientData, Tcl_Interp *interp, int objc,
[1028]2085               Tcl_Obj *const *objv)
[834]2086{
[1028]2087    int axis;
2088    if (GetAxisFromObj(interp, objv[2], &axis) != TCL_OK) {
[923]2089        return TCL_ERROR;
[829]2090    }
[1028]2091    if (NanoVis::grid != NULL) {
2092        Axis *axisPtr;
2093
[1089]2094        axisPtr = NULL;     /* Suppress compiler warning. */
[1028]2095        switch (axis) {
2096        case 0: axisPtr = &NanoVis::grid->xAxis; break;
2097        case 1: axisPtr = &NanoVis::grid->yAxis; break;
2098        case 2: axisPtr = &NanoVis::grid->zAxis; break;
2099        }
[1111]2100        axisPtr->name(Tcl_GetString(objv[3]));
2101        axisPtr->units(Tcl_GetString(objv[4]));
[829]2102    }
[834]2103    return TCL_OK;
[829]2104}
2105
[834]2106static int
[1431]2107GridLineColorOp(ClientData clientData, Tcl_Interp *interp, int objc,
[1028]2108                Tcl_Obj *const *objv)
[829]2109{
[915]2110    float r, g, b, a;
2111    if ((GetFloatFromObj(interp, objv[2], &r) != TCL_OK) ||
[923]2112        (GetFloatFromObj(interp, objv[3], &g) != TCL_OK) ||
2113        (GetFloatFromObj(interp, objv[4], &b) != TCL_OK)) {
2114        return TCL_ERROR;
[829]2115    }
[915]2116    a = 1.0f;
2117    if ((objc == 6) && (GetFloatFromObj(interp, objv[5], &a) != TCL_OK)) {
[923]2118        return TCL_ERROR;
[1089]2119    }
[915]2120    if (NanoVis::grid) {
[932]2121        NanoVis::grid->setLineColor(r, g, b, a);
[834]2122    }
2123    return TCL_OK;
2124}
[829]2125
[834]2126static int
[1431]2127GridVisibleOp(ClientData clientData, Tcl_Interp *interp, int objc,
[3567]2128              Tcl_Obj *const *objv)
[867]2129{
[927]2130    bool visible;
2131    if (GetBooleanFromObj(interp, objv[2], &visible) != TCL_OK) {
[923]2132        return TCL_ERROR;
[867]2133    }
[927]2134    NanoVis::grid->setVisible(visible);
[867]2135    return TCL_OK;
2136}
2137
[4056]2138static CmdSpec gridOps[] = {
[929]2139    {"axiscolor",  5, GridAxisColorOp,  5, 6, "r g b ?a?",},
[1028]2140    {"axisname",   5, GridAxisNameOp,   5, 5, "index title units",},
[929]2141    {"linecolor",  7, GridLineColorOp,  5, 6, "r g b ?a?",},
2142    {"visible",    1, GridVisibleOp,    3, 3, "bool",},
2143};
[915]2144static int nGridOps = NumCmdSpecs(gridOps);
[862]2145
[1089]2146static int
[1431]2147GridCmd(ClientData clientData, Tcl_Interp *interp, int objc,
[3567]2148        Tcl_Obj *const *objv)
[1089]2149{
[915]2150    Tcl_ObjCmdProc *proc;
[829]2151
[4056]2152    proc = GetOpFromObj(interp, nGridOps, gridOps,
2153                        CMDSPEC_ARG1, objc, objv, 0);
[915]2154    if (proc == NULL) {
[923]2155        return TCL_ERROR;
[913]2156    }
[1431]2157    return (*proc) (clientData, interp, objc, objv);
[913]2158}
2159
[1089]2160static int
[1431]2161AxisCmd(ClientData clientData, Tcl_Interp *interp, int objc,
[3567]2162        Tcl_Obj *const *objv)
[829]2163{
[915]2164    if (objc < 2) {
[1089]2165        Tcl_AppendResult(interp, "wrong # args: should be \"",
[1028]2166                Tcl_GetString(objv[0]), " option arg arg...\"", (char*)NULL);
[829]2167        return TCL_ERROR;
2168    }
[1028]2169    const char *string = Tcl_GetString(objv[1]);
[915]2170    char c = string[0];
2171    if ((c == 'v') && (strcmp(string, "visible") == 0)) {
[927]2172        bool visible;
[915]2173
[927]2174        if (GetBooleanFromObj(interp, objv[2], &visible) != TCL_OK) {
[915]2175            return TCL_ERROR;
2176        }
[3605]2177        NanoVis::orientationIndicator->setVisible(visible);
[915]2178    } else {
2179        Tcl_AppendResult(interp, "bad axis option \"", string,
2180                         "\": should be visible", (char*)NULL);
[829]2181        return TCL_ERROR;
2182    }
2183    return TCL_OK;
2184}
2185
[915]2186static int
[3605]2187ImageFlushCmd(ClientData clientData, Tcl_Interp *interp, int objc,
2188              Tcl_Obj *const *objv)
[1089]2189{
[3605]2190    lastCmdStatus = TCL_BREAK;
2191    return TCL_OK;
2192}
[829]2193
[3605]2194/**
2195 * \brief Execute commands from client in Tcl interpreter
2196 *
2197 * In this threaded model, the select call is for event compression.  We
2198 * want to execute render server commands as long as they keep coming. 
2199 * This lets us execute a stream of many commands but render once.  This
2200 * benefits camera movements, screen resizing, and opacity changes
2201 * (using a slider on the client).  The down side is you don't render
2202 * until there's a lull in the command stream.  If the client needs an
2203 * image, it can issue an "imgflush" command.  That breaks us out of the
2204 * read loop.
2205 */
2206int
2207nv::processCommands(Tcl_Interp *interp,
2208                    ReadBuffer *inBufPtr, int fdOut)
2209{
2210    int ret = 1;
2211    int status = TCL_OK;
2212
2213    Tcl_DString command;
2214    Tcl_DStringInit(&command);
2215    fd_set readFds;
2216    struct timeval tv, *tvPtr;
2217
2218    FD_ZERO(&readFds);
2219    FD_SET(inBufPtr->file(), &readFds);
2220    tvPtr = NULL;                       /* Wait for the first read. This is so
2221                                         * that we don't spin when no data is
2222                                         * available. */
2223    while (inBufPtr->isLineAvailable() ||
2224           (select(1, &readFds, NULL, NULL, tvPtr) > 0)) {
2225        size_t numBytes;
2226        unsigned char *buffer;
2227
2228        /* A short read is treated as an error here because we assume that we
2229         * will always get commands line by line. */
2230        if (inBufPtr->getLine(&numBytes, &buffer) != ReadBuffer::OK) {
2231            /* Terminate the server if we can't communicate with the client
2232             * anymore. */
2233            if (inBufPtr->status() == ReadBuffer::ENDFILE) {
2234                TRACE("Exiting server on EOF from client");
2235                return -1;
2236            } else {
2237                ERROR("Exiting server, failed to read from client: %s",
2238                      strerror(errno));
2239                return -1;
2240            }
2241        }
2242        Tcl_DStringAppend(&command, (char *)buffer, numBytes);
2243        if (Tcl_CommandComplete(Tcl_DStringValue(&command))) {
2244            struct timeval start, finish;
2245            gettimeofday(&start, NULL);
2246            status = ExecuteCommand(interp, &command);
2247            gettimeofday(&finish, NULL);
2248            g_stats.cmdTime += (MSECS_ELAPSED(start, finish) / 1.0e+3);
2249            g_stats.nCommands++;
2250            if (status == TCL_BREAK) {
2251                return 1;               /* This was caused by a "imgflush"
2252                                         * command. Break out of the read loop
2253                                         * and allow a new image to be
2254                                         * rendered. */
2255            } else { //if (status != TCL_OK) {
2256                ret = 0;
2257                if (handleError(interp, status, fdOut) < 0) {
2258                    return -1;
2259                }
2260            }
2261        }
2262
2263        tv.tv_sec = tv.tv_usec = 0L;    /* On successive reads, we break out
2264                                         * if no data is available. */
2265        FD_SET(inBufPtr->file(), &readFds);
2266        tvPtr = &tv;
2267    }
2268
2269    return ret;
[1374]2270}
[829]2271
[3605]2272/**
2273 * \brief Send error message to client socket
[1374]2274 */
[3605]2275int
2276nv::handleError(Tcl_Interp *interp, int status, int fdOut)
[1374]2277{
[3605]2278    const char *string;
2279    int nBytes;
[1111]2280
[3605]2281    if (status != TCL_OK) {
2282        string = Tcl_GetVar(interp, "errorInfo", TCL_GLOBAL_ONLY);
2283        nBytes = strlen(string);
2284        if (nBytes > 0) {
2285            TRACE("status=%d errorInfo=(%s)", status, string);
2286
2287            std::ostringstream oss;
2288            oss << "nv>viserror -type internal_error -token " << g_stats.nCommands << " -bytes " << nBytes << "\n" << string;
2289            nBytes = oss.str().length();
2290
2291#ifdef USE_THREADS
2292            queueResponse(oss.str().c_str(), nBytes, Response::VOLATILE, Response::ERROR);
2293#else
2294            if (write(fdOut, oss.str().c_str(), nBytes) < 0) {
2295                ERROR("write failed: %s", strerror(errno));
2296                return -1;
2297            }
2298#endif
2299        }
2300    }
2301
2302    string = getUserMessages();
2303    nBytes = strlen(string);
2304    if (nBytes > 0) {
2305        TRACE("userError=(%s)", string);
2306
2307        std::ostringstream oss;
2308        oss << "nv>viserror -type error -token " << g_stats.nCommands << " -bytes " << nBytes << "\n" << string;
2309        nBytes = oss.str().length();
2310
2311#ifdef USE_THREADS
2312        queueResponse(oss.str().c_str(), nBytes, Response::VOLATILE, Response::ERROR);
2313#else
2314        if (write(fdOut, oss.str().c_str(), nBytes) < 0) {
2315            ERROR("write failed: %s", strerror(errno));
2316            return -1;
2317        }
2318#endif
2319        clearUserMessages();
2320    }
2321
2322    return 0;
[915]2323}
[829]2324
[3605]2325void
2326nv::initTcl(Tcl_Interp *interp, ClientData clientData)
[915]2327{
[917]2328    /*
2329     * Ideally the connection is authenticated by nanoscale.  I still like the
2330     * idea of creating a non-safe master interpreter with a safe slave
2331     * interpreter.  Alias all the nanovis commands in the slave. That way we
2332     * can still run Tcl code within nanovis.  The eventual goal is to create
2333     * a test harness through the interpreter for nanovis.
2334     */
[3605]2335
[915]2336    Tcl_MakeSafe(interp);
[2828]2337
[3605]2338    Tcl_CreateObjCommand(interp, "axis",        AxisCmd,        clientData, NULL);
2339    Tcl_CreateObjCommand(interp, "camera",      CameraCmd,      clientData, NULL);
2340    Tcl_CreateObjCommand(interp, "clientinfo",  ClientInfoCmd,  clientData, NULL);
2341    Tcl_CreateObjCommand(interp, "cutplane",    CutplaneCmd,    clientData, NULL);
2342    FlowCmdInitProc(interp, clientData);
2343    Tcl_CreateObjCommand(interp, "grid",        GridCmd,        clientData, NULL);
2344    Tcl_CreateObjCommand(interp, "heightmap",   HeightMapCmd,   clientData, NULL);
2345    Tcl_CreateObjCommand(interp, "imgflush",    ImageFlushCmd,  clientData, NULL);
2346    Tcl_CreateObjCommand(interp, "legend",      LegendCmd,      clientData, NULL);
2347    Tcl_CreateObjCommand(interp, "screen",      ScreenCmd,      clientData, NULL);
2348    Tcl_CreateObjCommand(interp, "snapshot",    SnapshotCmd,    clientData, NULL);
2349    Tcl_CreateObjCommand(interp, "transfunc",   TransfuncCmd,   clientData, NULL);
2350    Tcl_CreateObjCommand(interp, "up",          UpCmd,          clientData, NULL);
2351    Tcl_CreateObjCommand(interp, "volume",      VolumeCmd,      clientData, NULL);
2352
[829]2353    // create a default transfer function
2354    if (Tcl_Eval(interp, def_transfunc) != TCL_OK) {
[3452]2355        WARN("bad default transfer function:\n%s",
[3567]2356             Tcl_GetStringResult(interp));
[829]2357    }
2358}
Note: See TracBrowser for help on using the repository browser.