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