Ignore:
Timestamp:
May 28, 2014 8:23:02 AM (10 years ago)
Author:
ldelgass
Message:

Add protocol to define color/alpha maps with spline knots

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/packages/vizservers/vtkvis/RendererCmd.cpp

    r4367 r4370  
    24662466
    24672467static int
     2468ColorMapAddSplineOp(ClientData clientData, Tcl_Interp *interp, int objc,
     2469                    Tcl_Obj *const *objv)
     2470{
     2471    const char *name = Tcl_GetString(objv[2]);
     2472    bool doSpline = false;
     2473    int nextArg = 3;
     2474    if (objc > 5) {
     2475        if (strcmp(Tcl_GetString(objv[nextArg]), "-spline") == 0) {
     2476            doSpline = true;
     2477            nextArg++;
     2478        }
     2479    }
     2480    int cmapc, omapc;
     2481    Tcl_Obj **cmapv = NULL;
     2482    Tcl_Obj **omapv = NULL;
     2483
     2484    if (Tcl_ListObjGetElements(interp, objv[nextArg++], &cmapc, &cmapv) != TCL_OK) {
     2485        return TCL_ERROR;
     2486    }
     2487    if (doSpline) {
     2488        if ((cmapc % 6) != 0) {
     2489            Tcl_AppendResult(interp, "wrong # elements in colormap: should be ",
     2490                             "{ value r g b midpoint sharpness ... }", (char*)NULL);
     2491            return TCL_ERROR;
     2492        }
     2493    } else {
     2494        if ((cmapc % 4) != 0) {
     2495            Tcl_AppendResult(interp, "wrong # elements in colormap: should be ",
     2496                             "{ value r g b ... }", (char*)NULL);
     2497            return TCL_ERROR;
     2498        }
     2499    }
     2500
     2501    ColorMap *colorMap = new ColorMap(name);
     2502    colorMap->setNumberOfTableEntries(256);
     2503
     2504    int numVals = doSpline ? 6 : 4;
     2505    for (int i = 0; i < cmapc; i += numVals) {
     2506        double val[numVals];
     2507        for (int j = 0; j < numVals; j++) {
     2508            if (Tcl_GetDoubleFromObj(interp, cmapv[i+j], &val[j]) != TCL_OK) {
     2509                delete colorMap;
     2510                return TCL_ERROR;
     2511            }
     2512            if ((val[j] < 0.0) || (val[j] > 1.0)) {
     2513                Tcl_AppendResult(interp, "bad colormap value \"",
     2514                                 Tcl_GetString(cmapv[i+j]),
     2515                                 "\": should be in the range [0,1]", (char*)NULL);
     2516                delete colorMap;
     2517                return TCL_ERROR;
     2518            }
     2519        }
     2520        ColorMap::ControlPoint cp;
     2521        cp.value = val[0];
     2522        for (int c = 0; c < 3; c++) {
     2523            cp.color[c] = val[c+1];
     2524        }
     2525        if (numVals > 4) {
     2526            cp.midpoint = val[4];
     2527            cp.sharpness = val[5];
     2528        }
     2529        colorMap->addControlPoint(cp);
     2530    }
     2531    doSpline = false;
     2532    if (objc > 5) {
     2533        if (strcmp(Tcl_GetString(objv[nextArg]), "-spline") == 0) {
     2534            doSpline = true;
     2535            nextArg++;
     2536        }
     2537    }
     2538    if (Tcl_ListObjGetElements(interp, objv[nextArg++], &omapc, &omapv) != TCL_OK) {
     2539        delete colorMap;
     2540        return TCL_ERROR;
     2541    }
     2542    if (doSpline) {
     2543        if ((omapc % 4) != 0) {
     2544            Tcl_AppendResult(interp, "wrong # elements in opacitymap: should be ",
     2545                             "{ value alpha midpoint sharpness ... }", (char*)NULL);
     2546            delete colorMap;
     2547            return TCL_ERROR;
     2548        }
     2549    } else {
     2550        if ((omapc % 2) != 0) {
     2551            Tcl_AppendResult(interp, "wrong # elements in opacitymap: should be ",
     2552                             "{ value alpha ... }", (char*)NULL);
     2553            delete colorMap;
     2554            return TCL_ERROR;
     2555        }
     2556    }
     2557    numVals = doSpline ? 4 : 2;
     2558    for (int i = 0; i < omapc; i += numVals) {
     2559        double val[numVals];
     2560        for (int j = 0; j < numVals; j++) {
     2561            if (Tcl_GetDoubleFromObj(interp, omapv[i+j], &val[j]) != TCL_OK) {
     2562                delete colorMap;
     2563                return TCL_ERROR;
     2564            }
     2565            if ((val[j] < 0.0) || (val[j] > 1.0)) {
     2566                Tcl_AppendResult(interp, "bad opacitymap value \"",
     2567                                 Tcl_GetString(omapv[i+j]),
     2568                                 "\": should be in the range [0,1]", (char*)NULL);
     2569                delete colorMap;
     2570                return TCL_ERROR;
     2571            }
     2572        }
     2573        ColorMap::OpacityControlPoint ocp;
     2574        ocp.value = val[0];
     2575        ocp.alpha = val[1];
     2576        if (numVals > 2) {
     2577            ocp.midpoint = val[2];
     2578            ocp.sharpness = val[3];
     2579        }
     2580        colorMap->addOpacityControlPoint(ocp);
     2581    }
     2582
     2583    colorMap->build();
     2584    g_renderer->addColorMap(name, colorMap);
     2585    return TCL_OK;
     2586}
     2587
     2588static int
    24682589ColorMapDeleteOp(ClientData clientData, Tcl_Interp *interp, int objc,
    24692590                 Tcl_Obj *const *objv)
     
    25092630static CmdSpec colorMapOps[] = {
    25102631    {"add",    1, ColorMapAddOp,             5, 5, "colorMapName colormap alphamap"},
    2511     {"define", 3, ColorMapAddOp,             5, 5, "colorMapName colormap alphamap"},
     2632    {"define", 3, ColorMapAddSplineOp,       5, 7, "colorMapName ?-spline? colormap ?-spline? alphamap"},
    25122633    {"delete", 3, ColorMapDeleteOp,          2, 3, "?colorMapName?"},
    25132634    {"res",    1, ColorMapNumTableEntriesOp, 3, 4, "numTableEntries ?colorMapName?"}
Note: See TracChangeset for help on using the changeset viewer.