Changeset 2215 for branches/blt4/packages
- Timestamp:
- Apr 20, 2011 6:10:02 PM (13 years ago)
- Location:
- branches/blt4/packages/vizservers/vtkvis
- Files:
-
- 4 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/blt4/packages/vizservers/vtkvis/RpVtkRenderer.cpp
r2201 r2215 10 10 #include <cassert> 11 11 12 #include <vtkMath.h> 12 13 #include <vtkCamera.h> 13 14 #include <vtkCoordinate.h> … … 76 77 _renderer = vtkSmartPointer<vtkRenderer>::New(); 77 78 _renderer->LightFollowCameraOn(); 78 storeCameraOrientation();79 79 _cameraMode = PERSPECTIVE; 80 80 initAxes(); 81 81 initCamera(); 82 storeCameraOrientation(); 82 83 _renderWindow = vtkSmartPointer<vtkRenderWindow>::New(); 83 84 _renderWindow->DoubleBufferOff(); … … 1635 1636 } 1636 1637 1638 void Renderer::setSceneOrientation(double quat[4]) 1639 { 1640 vtkSmartPointer<vtkCamera> camera = _renderer->GetActiveCamera(); 1641 vtkSmartPointer<vtkTransform> trans = vtkSmartPointer<vtkTransform>::New(); 1642 double mat3[3][3]; 1643 vtkMath::QuaternionToMatrix3x3(quat, mat3); 1644 vtkSmartPointer<vtkMatrix4x4> mat4 = vtkSmartPointer<vtkMatrix4x4>::New(); 1645 for (int r = 0; r < 3; r++) { 1646 memcpy((*mat4)[r], mat3[r], sizeof(double)*3); 1647 } 1648 trans->Translate(+_cameraFocalPoint[0], +_cameraFocalPoint[1], +_cameraFocalPoint[2]); 1649 trans->Concatenate(mat4); 1650 trans->Translate(-_cameraFocalPoint[0], -_cameraFocalPoint[1], -_cameraFocalPoint[2]); 1651 camera->SetPosition(0, 0, 1); 1652 camera->SetFocalPoint(0, 0, 0); 1653 camera->SetViewUp(0, 1, 0); 1654 camera->ApplyTransform(trans); 1655 storeCameraOrientation(); 1656 if (_cameraPan[0] != 0.0 || _cameraPan[1] != 0.0) { 1657 panCamera(_cameraPan[0], _cameraPan[1], true); 1658 } 1659 _needsRedraw = true; 1660 } 1661 1662 void Renderer::setCameraOrientationAndPosition(double position[3], 1663 double focalPoint[3], 1664 double viewUp[3]) 1665 { 1666 memcpy(_cameraPos, position, sizeof(double)*3); 1667 memcpy(_cameraFocalPoint, focalPoint, sizeof(double)*3); 1668 memcpy(_cameraUp, viewUp, sizeof(double)*3); 1669 // Apply the new parameters to the VTK camera 1670 restoreCameraOrientation(); 1671 _needsRedraw = true; 1672 } 1673 1674 void Renderer::getCameraOrientationAndPosition(double position[3], 1675 double focalPoint[3], 1676 double viewUp[3]) 1677 { 1678 memcpy(position, _cameraPos, sizeof(double)*3); 1679 memcpy(focalPoint, _cameraFocalPoint, sizeof(double)*3); 1680 memcpy(viewUp, _cameraUp, sizeof(double)*3); 1681 } 1682 1637 1683 void Renderer::storeCameraOrientation() 1638 1684 { … … 1677 1723 _cameraPan[0] = 0; 1678 1724 _cameraPan[1] = 0; 1725 1679 1726 _needsRedraw = true; 1680 1727 } … … 1704 1751 * \brief Perform a 2D translation of the camera 1705 1752 * 1706 * \param[in] x [0,1] Viewport coordinate horizontal panning 1707 * \param[in] y [0,1] Viewport coordinate vertical panning (with origin at top) 1753 * x,y pan amount are specified as signed absolute pan amount in viewport 1754 * units -- i.e. 0 is no pan, .5 is half the viewport, 2 is twice the viewport, 1755 * etc. 1756 * 1757 * \param[in] x Viewport coordinate horizontal panning 1758 * \param[in] y Viewport coordinate vertical panning (with origin at top) 1708 1759 * \param[in] absolute Control if pan amount is relative to current or absolute 1709 1760 */ 1710 1761 void Renderer::panCamera(double x, double y, bool absolute) 1711 1762 { 1712 // Reverse x rather than y, since we are panning the camera, and client1713 // expects to be panning/moving the object1714 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 }1729 1763 if (_cameraMode == IMAGE) { 1764 // Reverse x rather than y, since we are panning the camera, and client 1765 // expects to be panning/moving the object 1766 x = -x * _screenWorldCoords[2]; 1767 y = y * _screenWorldCoords[3]; 1768 1769 if (absolute) { 1770 double panAbs[2]; 1771 panAbs[0] = x; 1772 panAbs[1] = y; 1773 x -= _cameraPan[0]; 1774 y -= _cameraPan[1]; 1775 _cameraPan[0] = panAbs[0]; 1776 _cameraPan[1] = panAbs[1]; 1777 } else { 1778 _cameraPan[0] += x; 1779 _cameraPan[1] += y; 1780 } 1781 1730 1782 _imgWorldOrigin[0] += x; 1731 1783 _imgWorldOrigin[1] += y; … … 1733 1785 _imgWorldDims[0], _imgWorldDims[1]); 1734 1786 } else { 1787 y = -y; 1788 if (absolute) { 1789 double panAbs[2]; 1790 panAbs[0] = x; 1791 panAbs[1] = y; 1792 x -= _cameraPan[0]; 1793 y -= _cameraPan[1]; 1794 _cameraPan[0] = panAbs[0]; 1795 _cameraPan[1] = panAbs[1]; 1796 } else { 1797 _cameraPan[0] += x; 1798 _cameraPan[1] += y; 1799 } 1800 1735 1801 vtkSmartPointer<vtkCamera> camera = _renderer->GetActiveCamera(); 1736 vtkSmartPointer<vtkTransform> trans = vtkSmartPointer<vtkTransform>::New(); 1737 trans->Translate(x, y, 0); 1738 camera->ApplyTransform(trans); 1802 double viewFocus[4], focalDepth, viewPoint[3]; 1803 double newPickPoint[4], oldPickPoint[4], motionVector[3]; 1804 1805 camera->GetFocalPoint(viewFocus); 1806 computeWorldToDisplay(viewFocus[0], viewFocus[1], viewFocus[2], 1807 viewFocus); 1808 focalDepth = viewFocus[2]; 1809 1810 computeDisplayToWorld(( x * 2. + 1.) * _windowWidth / 2.0, 1811 ( y * 2. + 1.) * _windowHeight / 2.0, 1812 focalDepth, 1813 newPickPoint); 1814 1815 computeDisplayToWorld(_windowWidth / 2.0, 1816 _windowHeight / 2.0, 1817 focalDepth, 1818 oldPickPoint); 1819 1820 // Camera motion is reversed 1821 motionVector[0] = oldPickPoint[0] - newPickPoint[0]; 1822 motionVector[1] = oldPickPoint[1] - newPickPoint[1]; 1823 motionVector[2] = oldPickPoint[2] - newPickPoint[2]; 1824 1825 camera->GetFocalPoint(viewFocus); 1826 camera->GetPosition(viewPoint); 1827 camera->SetFocalPoint(motionVector[0] + viewFocus[0], 1828 motionVector[1] + viewFocus[1], 1829 motionVector[2] + viewFocus[2]); 1830 1831 camera->SetPosition(motionVector[0] + viewPoint[0], 1832 motionVector[1] + viewPoint[1], 1833 motionVector[2] + viewPoint[2]); 1834 1739 1835 _renderer->ResetCameraClippingRange(); 1740 1836 storeCameraOrientation(); … … 1799 1895 int imgHeightPx = _windowHeight - pxOffsetY - outerGutter; 1800 1896 int imgWidthPx = _windowWidth - pxOffsetX - outerGutter; 1897 1898 double imgAspect = width / height; 1899 double winAspect = (double)_windowWidth / _windowHeight; 1900 1801 1901 double pxToWorld; 1802 if (height > width) 1902 1903 if (imgAspect >= winAspect) { 1904 pxToWorld = width / imgWidthPx; 1905 } else { 1803 1906 pxToWorld = height / imgHeightPx; 1804 else1805 pxToWorld = width / imgWidthPx; 1907 } 1908 1806 1909 double offsetX = pxOffsetX * pxToWorld; 1807 1910 double offsetY = pxOffsetY * pxToWorld; … … 1853 1956 } 1854 1957 1958 void Renderer::computeDisplayToWorld(double x, double y, double z, double worldPt[4]) 1959 { 1960 _renderer->SetDisplayPoint(x, y, z); 1961 _renderer->DisplayToWorld(); 1962 _renderer->GetWorldPoint(worldPt); 1963 if (worldPt[3]) { 1964 worldPt[0] /= worldPt[3]; 1965 worldPt[1] /= worldPt[3]; 1966 worldPt[2] /= worldPt[3]; 1967 worldPt[3] = 1.0; 1968 } 1969 } 1970 1971 void Renderer::computeWorldToDisplay(double x, double y, double z, double displayPt[3]) 1972 { 1973 _renderer->SetWorldPoint(x, y, z, 1.0); 1974 _renderer->WorldToDisplay(); 1975 _renderer->GetDisplayPoint(displayPt); 1976 } 1977 1855 1978 void Renderer::computeScreenWorldCoords() 1856 1979 { … … 1858 1981 double x0 = -1; 1859 1982 double y0 = -1; 1860 double z0 = 0;1983 double z0 = -1; 1861 1984 double x1 = 1; 1862 1985 double y1 = 1; 1863 double z1 = 0;1986 double z1 = -1; 1864 1987 1865 1988 vtkMatrix4x4 *mat = vtkMatrix4x4::New(); … … 2100 2223 void Renderer::printCameraInfo(vtkCamera *camera) 2101 2224 { 2102 TRACE("Parallel Scale: %g, Cam pos: %g %g %g, Clipping range: %g %g",2225 TRACE("Parallel Scale: %g, Cam pos: %g %g %g, focal pt: %g %g %g, view up: %g %g %g, Clipping range: %g %g", 2103 2226 camera->GetParallelScale(), 2104 2227 camera->GetPosition()[0], 2105 2228 camera->GetPosition()[1], 2106 2229 camera->GetPosition()[2], 2230 camera->GetFocalPoint()[0], 2231 camera->GetFocalPoint()[1], 2232 camera->GetFocalPoint()[2], 2233 camera->GetViewUp()[0], 2234 camera->GetViewUp()[1], 2235 camera->GetViewUp()[2], 2107 2236 camera->GetClippingRange()[0], 2108 2237 camera->GetClippingRange()[1]); -
branches/blt4/packages/vizservers/vtkvis/RpVtkRenderer.h
r2201 r2215 110 110 void rotateCamera(double yaw, double pitch, double roll); 111 111 112 void setSceneOrientation(double quat[4]); 113 114 void setCameraOrientationAndPosition(double position[3], 115 double focalPoint[3], 116 double viewUp[3]); 117 118 void getCameraOrientationAndPosition(double position[3], 119 double focalPoint[3], 120 double viewUp[3]); 121 112 122 void panCamera(double x, double y, bool absolute = true); 113 123 … … 239 249 240 250 void updateRanges(bool useCumulative); 251 252 void computeDisplayToWorld(double x, double y, double z, double worldPt[4]); 253 254 void computeWorldToDisplay(double x, double y, double z, double displayPt[3]); 241 255 242 256 void computeScreenWorldCoords(); -
branches/blt4/packages/vizservers/vtkvis/RpVtkRendererCmd.cpp
r2201 r2215 212 212 213 213 static int 214 CameraGetOrientationOp(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->getCameraOrientationAndPosition(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 240 static int 241 CameraOrientationOp(ClientData clientData, Tcl_Interp *interp, int objc, 242 Tcl_Obj *const *objv) 243 { 244 double quat[4]; 245 246 if (Tcl_GetDoubleFromObj(interp, objv[2], &quat[0]) != TCL_OK || 247 Tcl_GetDoubleFromObj(interp, objv[3], &quat[1]) != TCL_OK || 248 Tcl_GetDoubleFromObj(interp, objv[4], &quat[2]) != TCL_OK || 249 Tcl_GetDoubleFromObj(interp, objv[5], &quat[3]) != TCL_OK) { 250 return TCL_ERROR; 251 } 252 253 g_renderer->setSceneOrientation(quat); 254 return TCL_OK; 255 } 256 257 static int 214 258 CameraOrthoOp(ClientData clientData, Tcl_Interp *interp, int objc, 215 259 Tcl_Obj *const *objv) 216 260 { 217 floatx, 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) {261 double x, y, width, height; 262 263 if (Tcl_GetDoubleFromObj(interp, objv[2], &x) != TCL_OK || 264 Tcl_GetDoubleFromObj(interp, objv[3], &y) != TCL_OK || 265 Tcl_GetDoubleFromObj(interp, objv[4], &width) != TCL_OK || 266 Tcl_GetDoubleFromObj(interp, objv[5], &height) != TCL_OK) { 223 267 return TCL_ERROR; 224 268 } … … 232 276 Tcl_Obj *const *objv) 233 277 { 234 floatx, y;235 236 if ( GetFloatFromObj(interp, objv[2], &x) != TCL_OK ||237 GetFloatFromObj(interp, objv[3], &y) != TCL_OK) {278 double x, y; 279 280 if (Tcl_GetDoubleFromObj(interp, objv[2], &x) != TCL_OK || 281 Tcl_GetDoubleFromObj(interp, objv[3], &y) != TCL_OK) { 238 282 return TCL_ERROR; 239 283 } … … 266 310 Tcl_Obj *const *objv) 267 311 { 268 floatyaw, 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) {312 double yaw, pitch, roll; 313 314 if (Tcl_GetDoubleFromObj(interp, objv[2], &yaw) != TCL_OK || 315 Tcl_GetDoubleFromObj(interp, objv[3], &pitch) != TCL_OK || 316 Tcl_GetDoubleFromObj(interp, objv[4], &roll) != TCL_OK) { 273 317 return TCL_ERROR; 274 318 } 275 319 276 320 g_renderer->rotateCamera(yaw, pitch, roll); 321 return TCL_OK; 322 } 323 324 static int 325 CameraSetOp(ClientData clientData, Tcl_Interp *interp, int objc, 326 Tcl_Obj *const *objv) 327 { 328 double pos[3]; 329 double focalPt[3]; 330 double viewUp[3]; 331 332 if (Tcl_GetDoubleFromObj(interp, objv[2], &pos[0]) != TCL_OK || 333 Tcl_GetDoubleFromObj(interp, objv[3], &pos[1]) != TCL_OK || 334 Tcl_GetDoubleFromObj(interp, objv[4], &pos[2]) != TCL_OK || 335 Tcl_GetDoubleFromObj(interp, objv[5], &focalPt[0]) != TCL_OK || 336 Tcl_GetDoubleFromObj(interp, objv[6], &focalPt[1]) != TCL_OK || 337 Tcl_GetDoubleFromObj(interp, objv[7], &focalPt[2]) != TCL_OK || 338 Tcl_GetDoubleFromObj(interp, objv[8], &viewUp[0]) != TCL_OK || 339 Tcl_GetDoubleFromObj(interp, objv[9], &viewUp[1]) != TCL_OK || 340 Tcl_GetDoubleFromObj(interp, objv[10], &viewUp[2]) != TCL_OK) { 341 return TCL_ERROR; 342 } 343 344 g_renderer->setCameraOrientationAndPosition(pos, focalPt, viewUp); 277 345 return TCL_OK; 278 346 } … … 282 350 Tcl_Obj *const *objv) 283 351 { 284 floatz;285 286 if ( GetFloatFromObj(interp, objv[2], &z) != TCL_OK) {352 double z; 353 354 if (Tcl_GetDoubleFromObj(interp, objv[2], &z) != TCL_OK) { 287 355 return TCL_ERROR; 288 356 } … … 293 361 294 362 static Rappture::CmdSpec cameraOps[] = { 363 {"get", 1, CameraGetOrientationOp, 2, 2, ""}, 295 364 {"mode", 1, CameraModeOp, 3, 3, "mode"}, 365 {"orient", 3, CameraOrientationOp, 6, 6, "qx qy qz qw"}, 296 366 {"ortho", 1, CameraOrthoOp, 6, 6, "x y width height"}, 297 367 {"pan", 1, CameraPanOp, 4, 4, "panX panY"}, 298 368 {"reset", 2, CameraResetOp, 2, 3, "?all?"}, 299 369 {"rotate", 2, CameraRotateOp, 5, 5, "angle angle angle"}, 370 {"set", 1, CameraSetOp, 11, 11, "posX posY posZ focalPtX focalPtY focalPtZ viewUpX viewUpY viewUpZ"}, 300 371 {"zoom", 1, CameraZoomOp, 3, 3, "zoomAmount"} 301 372 }; -
branches/blt4/packages/vizservers/vtkvis/protocol.txt
r2201 r2215 17 17 <axis> = x|y|z|all 18 18 19 camera get 20 Request current camera orientation 19 21 camera mode <mode> 20 22 <mode> = persp|ortho|image 23 camera orient <quatX> <quatY> <quatZ> <quatW> 24 Set scene orientation using a quaternion 21 25 camera ortho <x> <y> <width> <height> 22 26 Supply world coordinate bounds of plot area for image camera mode … … 28 32 camera rotate <yaw> <pitch> <roll> 29 33 Specify relative rotation in Euler angles (FIXME) 34 camera set <posX> <posY> <posZ> <focalPtX> <focalPtY> <focalPtZ> <viewUpX> <viewUpY> <viewUpZ> 35 Set camera orientation using camera position, focal point and view up 36 vector 30 37 camera zoom <z> 31 38 Specify zoom ratio … … 89 96 Replies: 90 97 98 nv>camera orient <posX> <posY> <posZ> <focalPtX> <focalPtY> <focalPtZ> <viewUpX> <viewUpY> <viewUpZ> 99 Reply to "camera get" 91 100 nv>image -type image -bytes <nbytes> 92 101 <binary RGB data>
Note: See TracChangeset
for help on using the changeset viewer.