diff --git a/Examples/LaparoscopicCamera/main.cpp b/Examples/LaparoscopicCamera/main.cpp index 2a70a71f53a573c49d5dd92bff95bb4c3aec0ae6..a97b312724ff8ac79b09e69e0b59cd443da089b6 100644 --- a/Examples/LaparoscopicCamera/main.cpp +++ b/Examples/LaparoscopicCamera/main.cpp @@ -332,19 +332,16 @@ void add2DOverlay(std::shared_ptr<VTKViewer> vtkViewer, /// \brief Create a Laparoscopic camera controller and connect /// it to the vtk viewer /// -std::shared_ptr<LaparoscopicCameraController> addCameraController(std::shared_ptr<SDK> sdk) +int addCameraController(std::shared_ptr<SDK> sdk) { - auto viewer = sdk->getViewerInstance(); - auto camClient = std::make_shared<VRPNForceDevice>(); + std::shared_ptr<VRPNForceDevice> camClient = std::make_shared<VRPNForceDevice>(); std::shared_ptr<VRPNDeviceServer> server; std::string input; if (SPACE_EXPLORER_DEVICE) { server = std::make_shared<VRPNDeviceServer>(); - - //get some user input and setup device url input = "navigator@localhost"; } else @@ -354,36 +351,32 @@ std::shared_ptr<LaparoscopicCameraController> addCameraController(std::shared_pt std::cout << "Enter the VRPN device URL(" << camClient->getDeviceURL() << "): "; std::getline(std::cin, input); } - - if (!input.empty()) + if (input.empty()) { - camClient->setDeviceURL(input); + return EXIT_FAILURE; } - auto camController = std::make_shared<LaparoscopicCameraController>(camClient); - camController->setScalingFactor(40.0); + camClient->setDeviceURL(input); + // Get vtkCamera + auto viewer = sdk->getViewerInstance(); std::shared_ptr<VTKViewer> vtkViewer = std::static_pointer_cast<VTKViewer>(viewer); - vtkCamera* cam = vtkViewer->getVtkCamera(); - - // set the view angle of the camera. 80 deg for laparoscopic camera cam->SetViewAngle(80.0); - // set the camera to be controlled by the camera controller + // Set Camera Controller + auto camController = std::make_shared<LaparoscopicCameraController>(camClient); + camController->setScalingFactor(40.0); camController->setCamera(cam); - // Connect the camera controller to the vtk viewer to enable camera manipulation - vtkViewer->setCameraControllerData(camController->getCameraData()); - + // Register modules sdk->registerModule(camClient); sdk->registerModule(camController); - if (SPACE_EXPLORER_DEVICE) { sdk->registerModule(server); } - return camController; + return EXIT_SUCCESS; } int main() @@ -391,12 +384,10 @@ int main() InitVTKRendering(); InitIODelegates(); - std::shared_ptr<SDK> sdk = SDK::createStandardSDK(); - //------------------------------------------------------- // Set up the viewer //------------------------------------------------------- - + std::shared_ptr<SDK> sdk = SDK::createStandardSDK(); std::shared_ptr<ViewerBase> viewer = sdk->getViewerInstance(); std::shared_ptr<VTKViewer> vtkViewer = std::static_pointer_cast<VTKViewer>(viewer); @@ -410,33 +401,35 @@ int main() //------------------------------------------------------- // Set up the scene //------------------------------------------------------- - std::shared_ptr<Scene> scene = sdk->getScene(0); viewer->registerScene(scene, SMRENDERTARGET_SCREEN, "Collision pipeline demo"); // Create camera navigation scene createCameraNavigationScene(scene, "./CameraNavAppData/target.png"); - // Initialize viewer with scene objects - // NOTE : Needs to be done before VTK Add ons since - // init create the needed renderer in the VTKView - viewer->init(); - //------------------------------------------------------- - // Add ons (VTK) - //------------------------------------------------------- - // Enable screenshot capture + //------------------------------------------------------- + // NOTE : Needs to be done before the viewer initialisation + // not to erase the changes on the observers made to the + // interactor in vtkViewer::addRenderer() vtkNew<ScreenCaptureInteractorStyle> style; style->initialize(vtkViewer->getRenderWindow()); vtkViewer->getVtkRenderWindowInteractor()->SetInteractorStyle(style.GetPointer()); style->SetCurrentRenderer(vtkViewer->getVtkRenderer()); + //------------------------------------------------------- + // Initialize viewer with scene objects + //------------------------------------------------------- + // NOTE : Needs to be done before VTK Add ons since + // init create the needed renderer in the VTKViewer + viewer->init(); + + //------------------------------------------------------- + // Add ons (VTK) + //------------------------------------------------------- // Add a camera controller - // NOTE: This has to come after the ScreenCaptureInteractorStyle initialization - // since for this to work the mouse events need disabled which are - // left as is after ScreenCaptureInteractorStyle initialization - std::shared_ptr<LaparoscopicCameraController> camController = addCameraController(sdk); + addCameraController(sdk); // Add a 2D overlay on the 3D scene add2DOverlay(vtkViewer,"./CameraNavAppData/viewfinder.png"); @@ -444,7 +437,6 @@ int main() //------------------------------------------------------- // Start //------------------------------------------------------- - // Run the SDK sdk->run(); diff --git a/VTKRendering/VTKViewer.cpp b/VTKRendering/VTKViewer.cpp index 4e710d3f76f8a057282728c25654b3e9abfff2f4..a8eea11cf786cee68f85b7fbeae5ebfd23159233 100644 --- a/VTKRendering/VTKViewer.cpp +++ b/VTKRendering/VTKViewer.cpp @@ -69,15 +69,6 @@ public: if(timerId == *static_cast<int*>(callData) && !this->viewer->isTerminated()) { - - if (this->cameraControllerData != nullptr) - { - vtkRenderer* renderer = - this->renderWindow->GetRenderers()->GetFirstRenderer(); - updateCamera(renderer); - renderer->ResetCameraClippingRange(); - } - this->renderWindow->Render(); } break; @@ -96,29 +87,6 @@ public: } } - /// - /// \brief Update the camera from external source (optional) - /// - void updateCamera(vtkRenderer* renderer) - { - vtkCamera* camera = renderer->GetActiveCamera(); - - camera->SetPosition( - cameraControllerData->position[0], - cameraControllerData->position[1], - cameraControllerData->position[2]); - - camera->SetViewUp( - cameraControllerData->upVector[0], - cameraControllerData->upVector[1], - cameraControllerData->upVector[2]); - - camera->SetFocalPoint( - cameraControllerData->focus[0], - cameraControllerData->focus[1], - cameraControllerData->focus[2]); - } - /// /// \brief Return the render window /// @@ -323,7 +291,6 @@ public: VTKViewer *viewer; vtkNew<vtkRenderWindow> renderWindow; vtkNew<vtkRenderWindowInteractor> renderWindowInteractor; - std::shared_ptr<cameraConfigurationData> cameraControllerData; }; VTKViewer::VTKViewer() : renderer(Core::make_unique<VTKRenderer> (this)) @@ -365,11 +332,6 @@ vtkCamera* VTKViewer::getVtkCamera() GetFirstRenderer()->GetActiveCamera(); } -void VTKViewer::setCameraControllerData(std::shared_ptr<cameraConfigurationData> camData) -{ - renderer->cameraControllerData = camData; -} - vtkRenderer* VTKViewer::getVtkRenderer() { return this->renderer->getRenderWindow()->GetRenderers()->GetFirstRenderer(); diff --git a/VTKRendering/VTKViewer.h b/VTKRendering/VTKViewer.h index a576d5c00a0a31a33e617a6054bdb632575fdf9f..27c376328d17f8329f619148597a8dfded1333bc 100644 --- a/VTKRendering/VTKViewer.h +++ b/VTKRendering/VTKViewer.h @@ -92,11 +92,6 @@ public: /// vtkCamera* getVtkCamera(); - /// - /// \brief Sets the position and orientation data from the camera controller - /// - void setCameraControllerData(std::shared_ptr<cameraConfigurationData> camData); - /// /// \brief /// diff --git a/VirtualTools/LaparoscopicCameraController.cpp b/VirtualTools/LaparoscopicCameraController.cpp index 55a649b7af0db03c56d622168b6fc9371666cc70..8d38446c8722bb73d5fad8b728b9f5f346e404d9 100644 --- a/VirtualTools/LaparoscopicCameraController.cpp +++ b/VirtualTools/LaparoscopicCameraController.cpp @@ -31,32 +31,22 @@ #include "Core/RenderDelegate.h" LaparoscopicCameraController::LaparoscopicCameraController( - std::shared_ptr< DeviceInterface > inputDevice, - vtkCamera* camera) + std::shared_ptr< DeviceInterface > inputDevice, vtkCamera* camera) { + this->name = "LaparoscopicCameraController"; this->inputDevice = inputDevice; - this->poolDelay = std::chrono::milliseconds(100); - this->initialTransform.setIdentity(); - this->name = "LaparoscopicCameraCoupler"; - - initializeCameraScopeConfiguration(); - this->camera = camera; - this->cameraPosOrientData = std::make_shared<cameraConfigurationData>(); + initializeCameraScopeConfiguration(); } LaparoscopicCameraController::LaparoscopicCameraController( std::shared_ptr<DeviceInterface> inputDevice) { + this->name = "LaparoscopicCameraController"; this->inputDevice = inputDevice; - this->poolDelay = std::chrono::milliseconds(100); - this->initialTransform.Identity(); - this->name = "LaparoscopicCameraCoupler"; initializeCameraScopeConfiguration(); - - this->cameraPosOrientData = std::make_shared<cameraConfigurationData>(); } LaparoscopicCameraController::~LaparoscopicCameraController() @@ -65,9 +55,11 @@ LaparoscopicCameraController::~LaparoscopicCameraController() void LaparoscopicCameraController::initializeCameraScopeConfiguration() { - this->bendingRadius = 1.0; // default bending radius - this->angleX = 0.0; - angleY = 0.0; + this->poolDelay = std::chrono::milliseconds(100); + this->bendingRadius = 1.0; + this->scalingFactor = 1.0; + this->currentAngleX = 0.0; + this->currentAngleY = 0.0; this->maxAngleX = this->maxAngleY = 11.0 / 7; // 90 deg this->minAngleX = this->minAngleY = -11.0 / 7; //- 90 deg this->deltaAngleXY = (4.0 / 360)*(22.0 / 7);// 2 deg @@ -94,19 +86,24 @@ vtkCamera* LaparoscopicCameraController::getcamera() const return this->camera; } +void LaparoscopicCameraController::setPoolDelay(const std::chrono::milliseconds& delay) +{ + this->poolDelay = delay; +} + const std::chrono::milliseconds &LaparoscopicCameraController::getPoolDelay() const { return this->poolDelay; } -void LaparoscopicCameraController::setPoolDelay(const std::chrono::milliseconds& delay) +void LaparoscopicCameraController::setBendingRadius(const double val) { - this->poolDelay = delay; + this->bendingRadius = val; } -const double& LaparoscopicCameraController::getScalingFactor() const +double LaparoscopicCameraController::getBendingRadius() const { - return this->scalingFactor; + return this->bendingRadius; } void LaparoscopicCameraController::setScalingFactor(const double factor) @@ -114,22 +111,13 @@ void LaparoscopicCameraController::setScalingFactor(const double factor) this->scalingFactor = factor; } -const Eigen::Quaternion<double> &LaparoscopicCameraController::getOrientation() const -{ - return this->orientation; -} - -void LaparoscopicCameraController::setOrientation( - const Eigen::Map<Eigen::Quaternion<double>>& newOrientation) +const double& LaparoscopicCameraController::getScalingFactor() const { - this->orientation = newOrientation; + return this->scalingFactor; } bool LaparoscopicCameraController::init() { - this->orientation.setIdentity(); - this->position.setZero(); - // Open communication for the device this->inputDevice->openDevice(); return true; @@ -175,82 +163,47 @@ bool LaparoscopicCameraController::updateCamera() { if (!this->inputDevice) { - std::cout << "Invalid input device" << std::endl; + std::cerr << "updateCamera: Invalid input device" << std::endl; return false; } - - if (this->inputDevice->getButton(3) && this->angleY < this->maxAngleY) + if (!this->camera) { - this->angleY += this->deltaAngleXY; + std::cerr << "updateCamera: Missing camera" << std::endl; + return false; } - if (this->inputDevice->getButton(4) && this->angleY > this->minAngleY) + if (this->inputDevice->getButton(3) && this->currentAngleY < this->maxAngleY) { - this->angleY -= this->deltaAngleXY; + this->currentAngleY += this->deltaAngleXY; } - - if (this->inputDevice->getButton(0) && this->angleX < this->maxAngleX) + if (this->inputDevice->getButton(4) && this->currentAngleY > this->minAngleY) { - this->angleX += this->deltaAngleXY; + this->currentAngleY -= this->deltaAngleXY; } - - if (this->inputDevice->getButton(1) && this->angleX > this->minAngleX) + if (this->inputDevice->getButton(0) && this->currentAngleX < this->maxAngleX) { - this->angleX -= this->deltaAngleXY; + this->currentAngleX += this->deltaAngleXY; + } + if (this->inputDevice->getButton(1) && this->currentAngleX > this->minAngleX) + { + this->currentAngleX -= this->deltaAngleXY; } - core::Quaterniond newDeviceRot = this->inputDevice->getOrientation(); core::Vec3d newDevicePos = this->inputDevice->getPosition()*this->scalingFactor; - core::Vec3d bendingOffset = core::Vec3d(0, 0, this->bendingRadius); - - core::Quaterniond bendingRot(cos(this->angleY/2), 0, sin(this->angleY/2), 0); + core::Quaterniond bendingRot(cos(this->currentAngleY/2), 0, sin(this->currentAngleY/2), 0); bendingRot.normalize(); - bendingRot = bendingRot*core::Quaterniond(cos(this->angleX/2), sin(this->angleX/2), 0, 0); + bendingRot = bendingRot*core::Quaterniond(cos(this->currentAngleX/2), sin(this->currentAngleX/2), 0, 0); bendingRot.normalize(); - // update the camera position, focus and up vector data - this->cameraPosOrientData->focus = newDeviceRot*bendingRot*core::Vec3d(0, 0, -200.0); - this->cameraPosOrientData->upVector = newDeviceRot*bendingRot*core::Vec3d(0, 1.0, 0); + // Update the camera position, focus and up vector data + core::Vec3d position = newDeviceRot*(bendingOffset - bendingRot*bendingOffset) + newDevicePos; + core::Vec3d upVector = newDeviceRot*bendingRot*core::Vec3d(0, 1.0, 0); + core::Vec3d focus = newDeviceRot*bendingRot*core::Vec3d(0, 0, -200.0); - this->cameraPosOrientData->position = - newDeviceRot*(bendingOffset - bendingRot*bendingOffset) + newDevicePos; + this->camera->SetPosition(position[0], position[1], position[2]); + this->camera->SetViewUp(upVector[0], upVector[1], upVector[2]); + this->camera->SetFocalPoint(focus[0], focus[1], focus[2]); return true; } - -void LaparoscopicCameraController::setOffsetOrientation( - const Eigen::Map<core::Quaterniond> &offsetOrientation) -{ - this->offsetOrientation = offsetOrientation; -} - -void LaparoscopicCameraController::setOffsetPosition(const core::Vec3d &offsetPosition) -{ - this->offsetPosition = offsetPosition; -} - -const core::Quaterniond & LaparoscopicCameraController::getOffsetOrientation() const -{ - return this->offsetOrientation; -} - -const core::Vec3d & LaparoscopicCameraController::getOffsetPosition() const -{ - return this->offsetPosition; -} - -std::shared_ptr<cameraConfigurationData> LaparoscopicCameraController::getCameraData() -{ - return this->cameraPosOrientData; -}; - -double LaparoscopicCameraController::getBendingRadius() const -{ - return this->bendingRadius; -} - -void LaparoscopicCameraController::setBendingRadius(const double val) -{ - this->bendingRadius = val; -} diff --git a/VirtualTools/LaparoscopicCameraController.h b/VirtualTools/LaparoscopicCameraController.h index f0e25e0766f2d631fcfe9659de906831a42ba0f6..2ce2297513cfcdfcb3c9d5baf1c3721b5c3b905c 100644 --- a/VirtualTools/LaparoscopicCameraController.h +++ b/VirtualTools/LaparoscopicCameraController.h @@ -41,16 +41,6 @@ class DeviceInterface; -struct cameraConfigurationData -{ - core::Vec3d position; - core::Vec3d focus; - core::Vec3d upVector; - - cameraConfigurationData() : position(0, 0, 0), focus(0, 0, -1), upVector(0, 1, 0){}; - ~cameraConfigurationData(){}; -}; - /// /// \class LaparoscopicCameraController /// @@ -86,158 +76,63 @@ public: void initializeCameraScopeConfiguration(); /// - /// \brief Set the input device for this tool - /// \param newDevice A pointer to an allocated device + /// \brief Set/Get the input device for this tool coupler /// void setInputDevice(std::shared_ptr<DeviceInterface> newDevice); - - /// - /// \brief Set the output device for this tool coupler - /// std::shared_ptr<DeviceInterface> getInputDevice(); /// - /// \brief Set the pointer to the mesh to control - /// \param newMesh A pointer to an allocated mesh + /// \brief Set/Get the pointer to the controlled camera /// void setCamera(vtkCamera* newCamera); - - /// - /// \brief Get the output device for this tool coupler - /// vtkCamera* getcamera() const; /// - /// \brief Get the current pooling delay - /// \return The currently set polling delay + /// \brief Set/Get the pooling delay of the controller /// + void setPoolDelay(const std::chrono::milliseconds& delay); const std::chrono::milliseconds &getPoolDelay() const; /// - /// \brief Set the pooling delay of the controller to get new data from the device - /// \param delay The new polling delay to set - /// - void setPoolDelay(const std::chrono::milliseconds &delay); - - /// - /// \brief Get the current scaling factor - /// \return The currently set scaling factor + /// \brief Set/Get the bending radius of the tip of the scope /// - const double &getScalingFactor() const; + void setBendingRadius(const double val); + double getBendingRadius() const; /// - /// \brief Set how much to scale the physical movement by in 3D space - /// \param The new scaling factor to set + /// \brief Set/Get the current scaling factor /// void setScalingFactor(const double factor); - - /// - /// \brief Get the current orientation - /// \return The orientation - /// - const core::Quaterniond &getOrientation() const; - - /// - /// \brief Set orientation much to scale the physical movement by in 3D space - /// \param The new scaling factor to set - /// - void setOrientation(const Eigen::Map<core::Quaterniond> &newOrientation); - - /// - /// \brief Get the current orientation - /// \return The currently set scaling factor - /// - const core::Vec3d &getPosition() const; - - /// - /// \brief Set orientation much to scale the physical movement by in 3D space - /// \param The new scaling factor to set - /// - void setPosition(const core::Vec3d &newPosition); - - /// - /// \brief Set offset orientation much to scale the physical movement by in 3D space - /// \param The new scaling factor to set - /// - void setOffsetOrientation(const Eigen::Map<core::Quaterniond> &offsetOrientation); - - /// - /// \brief Get the offset orientation - /// \return The orientation - /// - const core::Quaterniond &getOffsetOrientation() const; - - /// - /// \brief Get the offset position - /// \return The currently set scaling factor - /// - const core::Vec3d &getOffsetPosition() const; - - /// - /// \brief Set offset orientation much to scale the physical movement by in 3D space - /// \param The new scaling factor to set - /// - void setOffsetPosition(const core::Vec3d &offsetPosition); + const double &getScalingFactor() const; /// /// \brief Update position and orientation of the camera based on device data /// bool updateCamera(); - /// - /// \brief Returns the pointer to the camera data that is updated by this controller - /// - std::shared_ptr<cameraConfigurationData> getCameraData(); - - /// - /// \brief Getter and setter for the bending radius of the tip of the scope - /// - double getBendingRadius() const; - void setBendingRadius(const double val); - /// /// \brief Module overrides /// bool init() override; void beginFrame() override; void endFrame() override; - - /// - /// \brief Update tracker and - /// void exec() override; private: - TransformType initialTransform; //!< Transform applied to the position - // obtained from device - - core::Quaterniond orientation; //!< Previous rotation quaternion from device - - core::Quaterniond prevOrientation; //!< Previous rotation quaternion from phantom - core::Vec3d prevPosition; //!< Previous position from phantom - - core::Vec3d position; //!< Previous position from device - double scalingFactor; //!< Scaling factor for physical to virtual translation - - core::Quaterniond offsetOrientation; //!< Previous rotation quaternion from device - core::Vec3d offsetPosition; //!< Previous position from device - std::chrono::milliseconds poolDelay; //!< Pooling delay - vtkCamera* camera; //!< Pointer to rendering camera - std::shared_ptr<DeviceInterface> inputDevice; //!< Pointer to input device + vtkCamera* camera; //!< Pointer to rendering camera + std::shared_ptr<DeviceInterface> inputDevice; //!< Pointer to input device + std::chrono::milliseconds poolDelay; //!< Pooling delay + double bendingRadius; //!< bending radius of the tip of the scope + double scalingFactor; //!< Scaling factor for physical to virtual translation - double bendingRadius;//!< bending radius of the tip of the scope - - double angleY; - double maxAngleY; - double minAngleY; - - double angleX; + double currentAngleX; + double currentAngleY; double maxAngleX; + double maxAngleY; double minAngleX; + double minAngleY; double deltaAngleXY; - - std::shared_ptr<cameraConfigurationData> cameraPosOrientData;//!< camera config data }; #endif // iSMTK_LAPAROSCOPIC_CAMERA_COUPLER_H