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