Ignore:
Timestamp:
Apr 12, 2011, 2:20:21 PM (13 years ago)
Author:
ldelgass
Message:

New commands to set and get absolute camera orientation/position, fix for
panning in 3D, better aspect ratio handling with 2D camera.

Location:
trunk/packages/vizservers/vtkvis
Files:
4 edited

Legend:

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

    r2194 r2200  
    7676    _renderer = vtkSmartPointer<vtkRenderer>::New();
    7777    _renderer->LightFollowCameraOn();
    78     storeCameraOrientation();
    7978    _cameraMode = PERSPECTIVE;
    8079    initAxes();
    8180    initCamera();
     81    storeCameraOrientation();
    8282    _renderWindow = vtkSmartPointer<vtkRenderWindow>::New();
    8383    _renderWindow->DoubleBufferOff();
     
    16351635}
    16361636
     1637void Renderer::setCameraOrientation(double position[3],
     1638                                    double focalPoint[3],
     1639                                    double viewUp[3])
     1640{
     1641    memcpy(_cameraPos, position, sizeof(double)*3);
     1642    memcpy(_cameraFocalPoint, focalPoint, sizeof(double)*3);
     1643    memcpy(_cameraUp, viewUp, sizeof(double)*3);
     1644    // Apply the new parameters to the VTK camera
     1645    restoreCameraOrientation();
     1646    _needsRedraw = true;
     1647}
     1648
     1649void Renderer::getCameraOrientation(double position[3],
     1650                                    double focalPoint[3],
     1651                                    double viewUp[3])
     1652{
     1653    memcpy(position, _cameraPos, sizeof(double)*3);
     1654    memcpy(focalPoint, _cameraFocalPoint, sizeof(double)*3);
     1655    memcpy(viewUp, _cameraUp, sizeof(double)*3);
     1656}
     1657
    16371658void Renderer::storeCameraOrientation()
    16381659{
     
    16771698    _cameraPan[0] = 0;
    16781699    _cameraPan[1] = 0;
     1700
    16791701    _needsRedraw = true;
    16801702}
     
    17041726 * \brief Perform a 2D translation of the camera
    17051727 *
    1706  * \param[in] x [0,1] Viewport coordinate horizontal panning
    1707  * \param[in] y [0,1] Viewport coordinate vertical panning (with origin at top)
     1728 * x,y pan amount are specified as signed absolute pan amount in viewport
     1729 * units -- i.e. 0 is no pan, .5 is half the viewport, 2 is twice the viewport,
     1730 * etc.
     1731 *
     1732 * \param[in] x Viewport coordinate horizontal panning
     1733 * \param[in] y Viewport coordinate vertical panning (with origin at top)
    17081734 * \param[in] absolute Control if pan amount is relative to current or absolute
    17091735 */
    17101736void Renderer::panCamera(double x, double y, bool absolute)
    17111737{
    1712     // Reverse x rather than y, since we are panning the camera, and client
    1713     // expects to be panning/moving the object
    1714     x = -x * _screenWorldCoords[2];
    1715     y = y * _screenWorldCoords[3];
    1716 
    1717     if (absolute) {
    1718         double panAbs[2];
    1719         panAbs[0] = x;
    1720         panAbs[1] = y;
    1721         x -= _cameraPan[0];
    1722         y -= _cameraPan[1];
    1723         _cameraPan[0] = panAbs[0];
    1724         _cameraPan[1] = panAbs[1];
    1725     } else {
    1726         _cameraPan[0] += x;
    1727         _cameraPan[1] += y;
    1728     }
    17291738    if (_cameraMode == IMAGE) {
     1739        // Reverse x rather than y, since we are panning the camera, and client
     1740        // expects to be panning/moving the object
     1741        x = -x * _screenWorldCoords[2];
     1742        y = y * _screenWorldCoords[3];
     1743
     1744        if (absolute) {
     1745            double panAbs[2];
     1746            panAbs[0] = x;
     1747            panAbs[1] = y;
     1748            x -= _cameraPan[0];
     1749            y -= _cameraPan[1];
     1750            _cameraPan[0] = panAbs[0];
     1751            _cameraPan[1] = panAbs[1];
     1752        } else {
     1753            _cameraPan[0] += x;
     1754            _cameraPan[1] += y;
     1755        }
     1756
    17301757        _imgWorldOrigin[0] += x;
    17311758        _imgWorldOrigin[1] += y;
     
    17331760                            _imgWorldDims[0], _imgWorldDims[1]);
    17341761    } else {
     1762        y = -y;
     1763        if (absolute) {
     1764            double panAbs[2];
     1765            panAbs[0] = x;
     1766            panAbs[1] = y;
     1767            x -= _cameraPan[0];
     1768            y -= _cameraPan[1];
     1769            _cameraPan[0] = panAbs[0];
     1770            _cameraPan[1] = panAbs[1];
     1771        } else {
     1772            _cameraPan[0] += x;
     1773            _cameraPan[1] += y;
     1774        }
     1775
    17351776        vtkSmartPointer<vtkCamera> camera = _renderer->GetActiveCamera();
    1736         vtkSmartPointer<vtkTransform> trans = vtkSmartPointer<vtkTransform>::New();
    1737         trans->Translate(x, y, 0);
    1738         camera->ApplyTransform(trans);
     1777        double viewFocus[4], focalDepth, viewPoint[3];
     1778        double newPickPoint[4], oldPickPoint[4], motionVector[3];
     1779
     1780        camera->GetFocalPoint(viewFocus);
     1781        computeWorldToDisplay(viewFocus[0], viewFocus[1], viewFocus[2],
     1782                              viewFocus);
     1783        focalDepth = viewFocus[2];
     1784
     1785        computeDisplayToWorld(( x * 2. + 1.) * _windowWidth / 2.0,
     1786                              ( y * 2. + 1.) * _windowHeight / 2.0,
     1787                              focalDepth,
     1788                              newPickPoint);
     1789
     1790        computeDisplayToWorld(_windowWidth / 2.0,
     1791                              _windowHeight / 2.0,
     1792                              focalDepth,
     1793                              oldPickPoint);
     1794 
     1795        // Camera motion is reversed
     1796        motionVector[0] = oldPickPoint[0] - newPickPoint[0];
     1797        motionVector[1] = oldPickPoint[1] - newPickPoint[1];
     1798        motionVector[2] = oldPickPoint[2] - newPickPoint[2];
     1799
     1800        camera->GetFocalPoint(viewFocus);
     1801        camera->GetPosition(viewPoint);
     1802        camera->SetFocalPoint(motionVector[0] + viewFocus[0],
     1803                              motionVector[1] + viewFocus[1],
     1804                              motionVector[2] + viewFocus[2]);
     1805
     1806        camera->SetPosition(motionVector[0] + viewPoint[0],
     1807                            motionVector[1] + viewPoint[1],
     1808                            motionVector[2] + viewPoint[2]);
     1809
    17391810        _renderer->ResetCameraClippingRange();
    17401811        storeCameraOrientation();
     
    17991870    int imgHeightPx = _windowHeight - pxOffsetY - outerGutter;
    18001871    int imgWidthPx = _windowWidth - pxOffsetX - outerGutter;
     1872
     1873    double imgAspect = width / height;
     1874    double winAspect = (double)_windowWidth / _windowHeight;
     1875
    18011876    double pxToWorld;
    1802     if (height > width)
     1877
     1878    if (imgAspect >= winAspect) {
     1879        pxToWorld = width / imgWidthPx;
     1880    } else {
    18031881        pxToWorld = height / imgHeightPx;
    1804     else
    1805         pxToWorld = width / imgWidthPx;
     1882    }
     1883
    18061884    double offsetX = pxOffsetX * pxToWorld;
    18071885    double offsetY = pxOffsetY * pxToWorld;
     
    18531931}
    18541932
     1933void Renderer::computeDisplayToWorld(double x, double y, double z, double worldPt[4])
     1934{
     1935    _renderer->SetDisplayPoint(x, y, z);
     1936    _renderer->DisplayToWorld();
     1937    _renderer->GetWorldPoint(worldPt);
     1938    if (worldPt[3]) {
     1939        worldPt[0] /= worldPt[3];
     1940        worldPt[1] /= worldPt[3];
     1941        worldPt[2] /= worldPt[3];
     1942        worldPt[3] = 1.0;
     1943    }
     1944}
     1945
     1946void Renderer::computeWorldToDisplay(double x, double y, double z, double displayPt[3])
     1947{
     1948    _renderer->SetWorldPoint(x, y, z, 1.0);
     1949    _renderer->WorldToDisplay();
     1950    _renderer->GetDisplayPoint(displayPt);
     1951}
     1952
    18551953void Renderer::computeScreenWorldCoords()
    18561954{
     
    18581956    double x0 = -1;
    18591957    double y0 = -1;
    1860     double z0 = 0;
     1958    double z0 = -1;
    18611959    double x1 = 1;
    18621960    double y1 = 1;
    1863     double z1 = 0;
     1961    double z1 = -1;
    18641962
    18651963    vtkMatrix4x4 *mat = vtkMatrix4x4::New();
     
    21002198void Renderer::printCameraInfo(vtkCamera *camera)
    21012199{
    2102     TRACE("Parallel Scale: %g, Cam pos: %g %g %g, Clipping range: %g %g",
     2200    TRACE("Parallel Scale: %g, Cam pos: %g %g %g, focal pt: %g %g %g, view up: %g %g %g, Clipping range: %g %g",
    21032201          camera->GetParallelScale(),
    21042202          camera->GetPosition()[0],
    21052203          camera->GetPosition()[1],
    21062204          camera->GetPosition()[2],
     2205          camera->GetFocalPoint()[0],
     2206          camera->GetFocalPoint()[1],
     2207          camera->GetFocalPoint()[2],
     2208          camera->GetViewUp()[0],
     2209          camera->GetViewUp()[1],
     2210          camera->GetViewUp()[2],
    21072211          camera->GetClippingRange()[0],
    21082212          camera->GetClippingRange()[1]);
  • trunk/packages/vizservers/vtkvis/RpVtkRenderer.h

    r2194 r2200  
    110110    void rotateCamera(double yaw, double pitch, double roll);
    111111
     112    void setCameraOrientation(double position[3],
     113                              double focalPoint[3],
     114                              double viewUp[3]);
     115
     116    void getCameraOrientation(double position[3],
     117                              double focalPoint[3],
     118                              double viewUp[3]);
     119
    112120    void panCamera(double x, double y, bool absolute = true);
    113121
     
    239247
    240248    void updateRanges(bool useCumulative);
     249
     250    void computeDisplayToWorld(double x, double y, double z, double worldPt[4]);
     251
     252    void computeWorldToDisplay(double x, double y, double z, double displayPt[3]);
    241253
    242254    void computeScreenWorldCoords();
  • trunk/packages/vizservers/vtkvis/RpVtkRendererCmd.cpp

    r2194 r2200  
    212212
    213213static int
     214CameraGetOrientationOp(ClientData clientData, Tcl_Interp *interp, int objc,
     215                       Tcl_Obj *const *objv)
     216{
     217    double pos[3];
     218    double focalPt[3];
     219    double viewUp[3];
     220
     221    g_renderer->getCameraOrientation(pos, focalPt, viewUp);
     222
     223    char buf[256];
     224    snprintf(buf, sizeof(buf), "nv>camera orient %.12e %.12e %.12e %.12e %.12e %.12e %.12e %.12e %.12e",
     225             pos[0], pos[1], pos[2], focalPt[0], focalPt[1], focalPt[2], viewUp[0], viewUp[1], viewUp[2]);
     226    ssize_t bytesWritten;
     227    size_t len = strlen(buf);
     228    size_t ofs = 0;
     229    while ((bytesWritten = write(g_fdOut, buf + ofs, len - ofs)) > 0) {
     230        ofs += bytesWritten;
     231        if (ofs == len)
     232            break;
     233    }
     234    if (bytesWritten < 0) {
     235        return TCL_ERROR;
     236    }
     237    return TCL_OK;
     238}
     239
     240static int
     241CameraOrientationOp(ClientData clientData, Tcl_Interp *interp, int objc,
     242                    Tcl_Obj *const *objv)
     243{
     244    double pos[3];
     245    double focalPt[3];
     246    double viewUp[3];
     247
     248    if (Tcl_GetDoubleFromObj(interp, objv[2], &pos[0]) != TCL_OK ||
     249        Tcl_GetDoubleFromObj(interp, objv[3], &pos[1]) != TCL_OK ||
     250        Tcl_GetDoubleFromObj(interp, objv[4], &pos[2]) != TCL_OK ||
     251        Tcl_GetDoubleFromObj(interp, objv[5], &focalPt[0]) != TCL_OK ||
     252        Tcl_GetDoubleFromObj(interp, objv[6], &focalPt[1]) != TCL_OK ||
     253        Tcl_GetDoubleFromObj(interp, objv[7], &focalPt[2]) != TCL_OK ||
     254        Tcl_GetDoubleFromObj(interp, objv[8], &viewUp[0]) != TCL_OK ||
     255        Tcl_GetDoubleFromObj(interp, objv[9], &viewUp[1]) != TCL_OK ||
     256        Tcl_GetDoubleFromObj(interp, objv[10], &viewUp[2]) != TCL_OK) {
     257        return TCL_ERROR;
     258    }
     259
     260    g_renderer->setCameraOrientation(pos, focalPt, viewUp);
     261    return TCL_OK;
     262}
     263
     264static int
    214265CameraOrthoOp(ClientData clientData, Tcl_Interp *interp, int objc,
    215266              Tcl_Obj *const *objv)
    216267{
    217     float x, y, width, height;
    218 
    219     if (GetFloatFromObj(interp, objv[2], &x) != TCL_OK ||
    220         GetFloatFromObj(interp, objv[3], &y) != TCL_OK ||
    221         GetFloatFromObj(interp, objv[4], &width) != TCL_OK ||
    222         GetFloatFromObj(interp, objv[5], &height) != TCL_OK) {
     268    double x, y, width, height;
     269
     270    if (Tcl_GetDoubleFromObj(interp, objv[2], &x) != TCL_OK ||
     271        Tcl_GetDoubleFromObj(interp, objv[3], &y) != TCL_OK ||
     272        Tcl_GetDoubleFromObj(interp, objv[4], &width) != TCL_OK ||
     273        Tcl_GetDoubleFromObj(interp, objv[5], &height) != TCL_OK) {
    223274        return TCL_ERROR;
    224275    }
     
    232283            Tcl_Obj *const *objv)
    233284{
    234     float x, y;
    235 
    236     if (GetFloatFromObj(interp, objv[2], &x) != TCL_OK ||
    237         GetFloatFromObj(interp, objv[3], &y) != TCL_OK) {
     285    double x, y;
     286
     287    if (Tcl_GetDoubleFromObj(interp, objv[2], &x) != TCL_OK ||
     288        Tcl_GetDoubleFromObj(interp, objv[3], &y) != TCL_OK) {
    238289        return TCL_ERROR;
    239290    }
     
    266317               Tcl_Obj *const *objv)
    267318{
    268     float yaw, pitch, roll;
    269 
    270     if (GetFloatFromObj(interp, objv[2], &yaw) != TCL_OK ||
    271         GetFloatFromObj(interp, objv[3], &pitch) != TCL_OK ||
    272         GetFloatFromObj(interp, objv[4], &roll) != TCL_OK) {
     319    double yaw, pitch, roll;
     320
     321    if (Tcl_GetDoubleFromObj(interp, objv[2], &yaw) != TCL_OK ||
     322        Tcl_GetDoubleFromObj(interp, objv[3], &pitch) != TCL_OK ||
     323        Tcl_GetDoubleFromObj(interp, objv[4], &roll) != TCL_OK) {
    273324        return TCL_ERROR;
    274325    }
     
    282333            Tcl_Obj *const *objv)
    283334{
    284     float z;
    285 
    286     if (GetFloatFromObj(interp, objv[2], &z) != TCL_OK) {
     335    double z;
     336
     337    if (Tcl_GetDoubleFromObj(interp, objv[2], &z) != TCL_OK) {
    287338        return TCL_ERROR;
    288339    }
     
    293344
    294345static Rappture::CmdSpec cameraOps[] = {
     346    {"get", 1, CameraGetOrientationOp, 2, 2, ""},
    295347    {"mode", 1, CameraModeOp, 3, 3, "mode"},
     348    {"orient", 3, CameraOrientationOp, 11, 11, "posX posY posZ focalPtX focalPtY focalPtZ viewUpX viewUpY viewUpZ"},
    296349    {"ortho", 1, CameraOrthoOp, 6, 6, "x y width height"},
    297350    {"pan", 1, CameraPanOp, 4, 4, "panX panY"},
  • trunk/packages/vizservers/vtkvis/protocol.txt

    r2194 r2200  
    1717     <axis> = x|y|z|all
    1818
     19camera get
     20       Request current camera orientation
    1921camera mode <mode>
    2022       <mode> = persp|ortho|image
     23camera orient <posX> <posY> <posZ> <focalPtX> <focalPtY> <focalPtZ> <viewUpX> <viewUpY> <viewUpZ>
     24       Set camera orientation using camera position, focal point and view up
     25       vector
    2126camera ortho <x> <y> <width> <height>
    2227       Supply world coordinate bounds of plot area for image camera mode
     
    8994Replies:
    9095
     96nv>camera orient <posX> <posY> <posZ> <focalPtX> <focalPtY> <focalPtZ> <viewUpX> <viewUpY> <viewUpZ>
     97   Reply to "camera get"
    9198nv>image -type image -bytes <nbytes>
    9299  <binary RGB data>
Note: See TracChangeset for help on using the changeset viewer.