Ignore:
Timestamp:
Sep 19, 2014 12:04:45 PM (9 years ago)
Author:
ldelgass
Message:

screen coords command now takes a list of coordinates

File:
1 edited

Legend:

Unmodified
Added
Removed
  • geovis/trunk/RendererCmd.cpp

    r4635 r4636  
    767767    return (*proc) (clientData, interp, objc, objv);
    768768}
    769 #if 1
     769
    770770static int
    771771MapCoordsOp(ClientData clientData, Tcl_Interp *interp, int objc,
     
    815815            // altmode absolute
    816816            coordVec.push_back(osg::Vec3d(mapPoint.x(), mapPoint.y(), mapPoint.z()));
    817 #if 0
    818             if (outSRS != NULL) {
    819                 mapPoint = mapPoint.transform(outSRS);
    820                 if (!mapPoint.isValid()) {
    821                     Tcl_DecrRefCount(listObjPtr);
    822                     Tcl_AppendResult(interp, "Could not transform map point to requested SRS", (char*)NULL);
    823                     return TCL_ERROR;
    824                 }
    825             }
    826             Tcl_Obj *objPtr = Tcl_NewDoubleObj(mapPoint.x());
    827             Tcl_ListObjAppendElement(interp, listObjPtr, objPtr);
    828             objPtr = Tcl_NewDoubleObj(mapPoint.y());
    829             Tcl_ListObjAppendElement(interp, listObjPtr, objPtr);
    830             objPtr = Tcl_NewDoubleObj(mapPoint.z());
    831             Tcl_ListObjAppendElement(interp, listObjPtr, objPtr);
    832 #endif
    833817        } else {
    834818            coordVec.push_back(osg::Vec3d(std::numeric_limits<double>::quiet_NaN(),
    835819                                          std::numeric_limits<double>::quiet_NaN(),
    836820                                          std::numeric_limits<double>::quiet_NaN()));
    837 #if 0
    838             Tcl_Obj *objPtr = Tcl_NewStringObj("NaN", 3);
    839             Tcl_ListObjAppendElement(interp, listObjPtr, objPtr);
    840             Tcl_ListObjAppendElement(interp, listObjPtr, objPtr);
    841             Tcl_ListObjAppendElement(interp, listObjPtr, objPtr);
    842 #endif
    843821        }
    844822    }
     
    873851    return TCL_OK;
    874852}
    875 #else
    876 static int
    877 MapCoordsOp(ClientData clientData, Tcl_Interp *interp, int objc,
    878             Tcl_Obj *const *objv)
    879 {
    880     int x, y;
    881     if (Tcl_GetIntFromObj(interp, objv[3], &x) != TCL_OK ||
    882         Tcl_GetIntFromObj(interp, objv[4], &y) != TCL_OK) {
    883         return TCL_ERROR;
    884     }
    885 
    886     osgEarth::GeoPoint mapPoint;
    887     size_t length;
    888     char mesg[512];
    889     if (g_renderer->mapMouseCoords(x, y, mapPoint)) {
    890         std::string srsInit;
    891         std::string verticalDatum;
    892         if (objc > 5) {
    893             srsInit = Tcl_GetString(objv[5]);
    894             if (objc > 6) {
    895                 verticalDatum = Tcl_GetString(objv[6]);
    896             }
    897             osgEarth::SpatialReference *outSRS =
    898                 osgEarth::SpatialReference::get(srsInit, verticalDatum);
    899             if (outSRS == NULL) {
    900                 Tcl_AppendResult(interp, "bad SRS \"", srsInit.c_str(), "\"", (char*)NULL);
    901                 return TCL_ERROR;
    902             }
    903             mapPoint = mapPoint.transform(outSRS);
    904             if (!mapPoint.isValid()) {
    905                 Tcl_AppendResult(interp, "Could not transform map point to requested SRS", (char*)NULL);
    906                 return TCL_ERROR;
    907             }
    908         }
    909         // send coords to client
    910         length = snprintf(mesg, sizeof(mesg),
    911                           "nv>map coords %s %g %g %g %d %d {%s} {%s}\n",
    912                           Tcl_GetString(objv[2]), //g_stats.nCommands,
    913                           mapPoint.x(), mapPoint.y(), mapPoint.z(),
    914                           x, y,
    915                           mapPoint.getSRS()->getHorizInitString().c_str(),
    916                           mapPoint.getSRS()->getVertInitString().c_str());
    917 
    918         queueResponse(mesg, length, Response::VOLATILE);
    919     } else {
    920         // Out of range
    921         length = snprintf(mesg, sizeof(mesg),
    922                           "nv>map coords %s invalid %d %d\n",
    923                           Tcl_GetString(objv[2]), //g_stats.nCommands,
    924                           x, y);
    925 
    926         queueResponse(mesg, length, Response::VOLATILE);
    927     }
    928     return TCL_OK;
    929 }
    930 #endif
     853
    931854static int
    932855MapGraticuleOp(ClientData clientData, Tcl_Interp *interp, int objc,
     
    19461869               Tcl_Obj *const *objv)
    19471870{
    1948     double x, y, z;
    1949     if (Tcl_GetDoubleFromObj(interp, objv[3], &x) != TCL_OK ||
    1950         Tcl_GetDoubleFromObj(interp, objv[4], &y) != TCL_OK ||
    1951         Tcl_GetDoubleFromObj(interp, objv[5], &z) != TCL_OK) {
    1952         return TCL_ERROR;
    1953     }
     1871    int tokenLength;
     1872    const char *token = Tcl_GetStringFromObj(objv[2], &tokenLength);
     1873    int numCoords;
     1874    Tcl_Obj **coords;
     1875    if (Tcl_ListObjGetElements(interp, objv[3], &numCoords, &coords) != TCL_OK) {
     1876        return TCL_ERROR;
     1877    }
     1878    if (numCoords == 0) {
     1879        Tcl_AppendResult(interp, "no x,y,z coordinates in list", (char *)NULL);
     1880        return TCL_ERROR;
     1881    }
     1882    if (numCoords % 3 != 0) {
     1883        Tcl_AppendResult(interp, "invalid number of coordinates in list",
     1884                         (char *)NULL);
     1885        return TCL_ERROR;
     1886    }
     1887
    19541888    const osgEarth::SpatialReference *srs = NULL;
    1955     if (objc < 7) {
     1889    if (objc < 5) {
    19561890        srs = g_renderer->getMapSRS();
    19571891        if (srs == NULL) {
     
    19601894        }
    19611895    } else {
    1962         std::string srsInit(Tcl_GetString(objv[6]));
     1896        std::string srsInit(Tcl_GetString(objv[4]));
    19631897        std::string verticalDatum;
    1964         if (objc > 7) {
    1965             verticalDatum = Tcl_GetString(objv[7]);
     1898        if (objc > 5) {
     1899            verticalDatum = Tcl_GetString(objv[5]);
    19661900        }
    19671901        srs = osgEarth::SpatialReference::get(srsInit, verticalDatum);
     
    19711905        }
    19721906    }
    1973     // ALTMODE_RELATIVE is height above terrain, ALTMODE_ABSOLUTE means
    1974     // relative to the vertical datum
    1975     osgEarth::GeoPoint mapPoint(srs, x, y, z, osgEarth::ALTMODE_ABSOLUTE);
    1976     size_t length;
    1977     char mesg[256];
    1978     osg::Vec3d world;
    1979     osg::Vec3d screen;
    1980     if (g_renderer->getWorldCoords(mapPoint, &world) &&
    1981         g_renderer->worldToScreen(world, &screen)) {
    1982         // send coords to client
    1983         length = snprintf(mesg, sizeof(mesg),
    1984                           "nv>screen coords %s %g %g %g %g %g %g\n",
    1985                           Tcl_GetString(objv[2]),
    1986                           screen.x(), screen.y(), screen.z(),
    1987                           x, y, z);
    1988 
    1989         queueResponse(mesg, length, Response::VOLATILE);
    1990     } else {
    1991         // Out of range
    1992         length = snprintf(mesg, sizeof(mesg),
    1993                           "nv>screen coords %s invalid %g %g %g\n",
    1994                           Tcl_GetString(objv[2]),
    1995                           x, y, z);
    1996 
    1997         queueResponse(mesg, length, Response::VOLATILE);
    1998     }
     1907    Tcl_Obj *listObjPtr = Tcl_NewListObj(0, (Tcl_Obj **)NULL);
     1908    std::vector<osg::Vec3d> coordVec;
     1909    for (int i = 0; i < numCoords; i += 3) {
     1910        double x, y, z;
     1911        if (Tcl_GetDoubleFromObj(interp, coords[i], &x) != TCL_OK ||
     1912            Tcl_GetDoubleFromObj(interp, coords[i+1], &y) != TCL_OK ||
     1913            Tcl_GetDoubleFromObj(interp, coords[i+2], &z) != TCL_OK) {
     1914            return TCL_ERROR;
     1915        }
     1916        // ALTMODE_RELATIVE is height above terrain, ALTMODE_ABSOLUTE means
     1917        // relative to the vertical datum
     1918        osgEarth::GeoPoint mapPoint(srs, x, y, z, osgEarth::ALTMODE_ABSOLUTE);
     1919        osg::Vec3d world;
     1920        if (g_renderer->getWorldCoords(mapPoint, &world)) {
     1921            coordVec.push_back(world);
     1922        } else {
     1923            coordVec.push_back(osg::Vec3d(std::numeric_limits<double>::quiet_NaN(),
     1924                                          std::numeric_limits<double>::quiet_NaN(),
     1925                                          std::numeric_limits<double>::quiet_NaN()));
     1926        }
     1927    }
     1928    g_renderer->worldToScreen(coordVec);
     1929    for (std::vector<osg::Vec3d>::iterator itr = coordVec.begin();
     1930         itr != coordVec.end(); ++itr) {
     1931        Tcl_Obj *objPtr = Tcl_NewDoubleObj(itr->x());
     1932        Tcl_ListObjAppendElement(interp, listObjPtr, objPtr);
     1933        objPtr = Tcl_NewDoubleObj(itr->y());
     1934        Tcl_ListObjAppendElement(interp, listObjPtr, objPtr);
     1935        objPtr = Tcl_NewDoubleObj(itr->z());
     1936        Tcl_ListObjAppendElement(interp, listObjPtr, objPtr);
     1937    }
     1938    // send coords to client
     1939    int listLength;
     1940    const char *string = Tcl_GetStringFromObj(listObjPtr, &listLength);
     1941    size_t length = listLength + tokenLength + 22;
     1942    char *mesg = new char[length];
     1943    length = snprintf(mesg, length, "nv>screen coords %s {%s}\n", token, string);
     1944    Tcl_DecrRefCount(listObjPtr);
     1945    queueResponse(mesg, length, Response::VOLATILE);
     1946    delete [] mesg;
    19991947    return TCL_OK;
    20001948}
     
    20171965static CmdSpec screenOps[] = {
    20181966    {"bgcolor", 1, ScreenBgColorOp, 5, 5, "r g b"},
    2019     {"coords",  1, ScreenCoordsOp, 6, 8, "token x y z ?srs? ?verticalDatum?"},
     1967    {"coords",  1, ScreenCoordsOp, 4, 6, "token coords ?srs? ?verticalDatum?"},
    20201968    {"size",    1, ScreenSizeOp, 4, 4, "width height"}
    20211969};
Note: See TracChangeset for help on using the changeset viewer.