Path: blob/master/modules/viz/src/vtk/vtkVizInteractorStyle.cpp
16358 views
/*M///////////////////////////////////////////////////////////////////////////////////////1//2// IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.3//4// By downloading, copying, installing or using the software you agree to this license.5// If you do not agree to this license, do not download, install,6// copy or use the software.7//8//9// License Agreement10// For Open Source Computer Vision Library11//12// Copyright (C) 2013, OpenCV Foundation, all rights reserved.13// Third party copyrights are property of their respective owners.14//15// Redistribution and use in source and binary forms, with or without modification,16// are permitted provided that the following conditions are met:17//18// * Redistribution's of source code must retain the above copyright notice,19// this list of conditions and the following disclaimer.20//21// * Redistribution's in binary form must reproduce the above copyright notice,22// this list of conditions and the following disclaimer in the documentation23// and/or other materials provided with the distribution.24//25// * The name of the copyright holders may not be used to endorse or promote products26// derived from this software without specific prior written permission.27//28// This software is provided by the copyright holders and contributors "as is" and29// any express or implied warranties, including, but not limited to, the implied30// warranties of merchantability and fitness for a particular purpose are disclaimed.31// In no event shall the Intel Corporation or contributors be liable for any direct,32// indirect, incidental, special, exemplary, or consequential damages33// (including, but not limited to, procurement of substitute goods or services;34// loss of use, data, or profits; or business interruption) however caused35// and on any theory of liability, whether in contract, strict liability,36// or tort (including negligence or otherwise) arising in any way out of37// the use of this software, even if advised of the possibility of such damage.38//39// Authors:40// * Ozan Tonkal, [email protected]41// * Anatoly Baksheev, Itseez Inc. myname.mysurname <> mycompany.com42//43//M*/4445#include "../precomp.hpp"4647namespace cv { namespace viz48{49vtkStandardNewMacro(vtkVizInteractorStyle)50}}5152//////////////////////////////////////////////////////////////////////////////////////////////5354cv::viz::vtkVizInteractorStyle::vtkVizInteractorStyle()55{56FlyMode = false;57MotionFactor = 10.0;5859keyboardCallback_ = 0;60keyboard_callback_cookie_ = 0;6162mouseCallback_ = 0;63mouse_callback_cookie_ = 0;6465// Set windows size (width, height) to unknown (-1)66win_size_ = Vec2i(-1, -1);67win_pos_ = Vec2i(0, 0);68max_win_size_ = Vec2i(-1, -1);6970stereo_anaglyph_redblue_ = true;7172//from fly73KeysDown = 0;74UseTimers = 1;7576DiagonalLength = 1.0;77MotionStepSize = 1.0/100.0;78MotionUserScale = 1.0; // +/- key adjustment79MotionAccelerationFactor = 10.0;80AngleStepSize = 1.0;81}8283cv::viz::vtkVizInteractorStyle::~vtkVizInteractorStyle() {}8485//////////////////////////////////////////////////////////////////////////////////////////////86void cv::viz::vtkVizInteractorStyle::saveScreenshot(const String &file)87{88FindPokedRenderer(Interactor->GetEventPosition()[0], Interactor->GetEventPosition()[1]);8990vtkSmartPointer<vtkWindowToImageFilter> wif = vtkSmartPointer<vtkWindowToImageFilter>::New();91wif->SetInput(Interactor->GetRenderWindow());9293vtkSmartPointer<vtkPNGWriter> snapshot_writer = vtkSmartPointer<vtkPNGWriter>::New();94snapshot_writer->SetInputConnection(wif->GetOutputPort());95snapshot_writer->SetFileName(file.c_str());96snapshot_writer->Write();9798cout << "Screenshot successfully captured (" << file.c_str() << ")" << endl;99}100101//////////////////////////////////////////////////////////////////////////////////////////////102103void cv::viz::vtkVizInteractorStyle::exportScene(const String &file)104{105vtkSmartPointer<vtkExporter> exporter;106if (file.size() > 5 && file.substr(file.size() - 5) == ".vrml")107{108exporter = vtkSmartPointer<vtkVRMLExporter>::New();109vtkVRMLExporter::SafeDownCast(exporter)->SetFileName(file.c_str());110}111else112{113exporter = vtkSmartPointer<vtkOBJExporter>::New();114vtkOBJExporter::SafeDownCast(exporter)->SetFilePrefix(file.c_str());115}116117exporter->SetInput(Interactor->GetRenderWindow());118exporter->Write();119120cout << "Scene successfully exported (" << file.c_str() << ")" << endl;121}122123void cv::viz::vtkVizInteractorStyle::exportScene()124{125// Export scene as in obj or vrml format126String format = Interactor->GetAltKey() ? "scene-%d.vrml" : "scene-%d";127exportScene(cv::format(format.c_str(), (unsigned int)time(0)));128}129130//////////////////////////////////////////////////////////////////////////////////////////////131132void cv::viz::vtkVizInteractorStyle::changePointsSize(float delta)133{134vtkSmartPointer<vtkActorCollection> ac = CurrentRenderer->GetActors();135vtkCollectionSimpleIterator ait;136137for (ac->InitTraversal(ait); vtkActor* actor = ac->GetNextActor(ait); )138for (actor->InitPathTraversal(); vtkAssemblyPath* path = actor->GetNextPath(); )139{140vtkActor* apart = vtkActor::SafeDownCast(path->GetLastNode()->GetViewProp());141float psize = apart->GetProperty()->GetPointSize() + delta;142psize = std::max(1.f, std::min(63.f, psize));143apart->GetProperty()->SetPointSize(psize);144}145}146147void cv::viz::vtkVizInteractorStyle::setRepresentationToPoints()148{149vtkSmartPointer<vtkActorCollection> ac = CurrentRenderer->GetActors();150vtkCollectionSimpleIterator ait;151for (ac->InitTraversal(ait); vtkActor* actor = ac->GetNextActor(ait); )152for (actor->InitPathTraversal(); vtkAssemblyPath* path = actor->GetNextPath(); )153{154vtkActor* apart = vtkActor::SafeDownCast(path->GetLastNode()->GetViewProp());155apart->GetProperty()->SetRepresentationToPoints();156}157}158159//////////////////////////////////////////////////////////////////////////////////////////////160161void cv::viz::vtkVizInteractorStyle::printCameraParams()162{163vtkSmartPointer<vtkCamera> cam = Interactor->GetRenderWindow()->GetRenderers()->GetFirstRenderer()->GetActiveCamera();164165Vec2d clip(cam->GetClippingRange());166Vec3d focal(cam->GetFocalPoint()), pos(cam->GetPosition()), view(cam->GetViewUp());167Vec2i win_pos(Interactor->GetRenderWindow()->GetPosition());168Vec2i win_size(Interactor->GetRenderWindow()->GetSize());169double angle = cam->GetViewAngle () / 180.0 * CV_PI;170171String data = cv::format("clip(%f,%f) focal(%f,%f,%f) pos(%f,%f,%f) view(%f,%f,%f) angle(%f) winsz(%d,%d) winpos(%d,%d)",172clip[0], clip[1], focal[0], focal[1], focal[2], pos[0], pos[1], pos[2], view[0], view[1], view[2],173angle, win_size[0], win_size[1], win_pos[0], win_pos[1]);174175std::cout << data.c_str() << std::endl;176}177178//////////////////////////////////////////////////////////////////////////////////////////////179180void cv::viz::vtkVizInteractorStyle::toggleFullScreen()181{182Vec2i screen_size(Interactor->GetRenderWindow()->GetScreenSize());183Vec2i win_size(Interactor->GetRenderWindow()->GetSize());184185// Is window size = max?186if (win_size == max_win_size_)187{188Interactor->GetRenderWindow()->SetSize(win_size_.val);189Interactor->GetRenderWindow()->SetPosition(win_pos_.val);190Interactor->Render();191}192// Set to max193else194{195win_pos_ = Vec2i(Interactor->GetRenderWindow()->GetPosition());196win_size_ = win_size;197198Interactor->GetRenderWindow()->SetSize(screen_size.val);199Interactor->Render();200max_win_size_ = Vec2i(Interactor->GetRenderWindow()->GetSize());201}202}203204//////////////////////////////////////////////////////////////////////////////////////////////205206void cv::viz::vtkVizInteractorStyle::resetViewerPose()207{208WidgetActorMap::iterator it = widget_actor_map_->begin();209// it might be that some actors don't have a valid transformation set -> we skip them to avoid a seg fault.210for (; it != widget_actor_map_->end(); ++it)211{212vtkProp3D * actor = vtkProp3D::SafeDownCast(it->second);213if (actor && actor->GetUserMatrix())214break;215}216217vtkSmartPointer<vtkCamera> cam = CurrentRenderer->GetActiveCamera();218219// if a valid transformation was found, use it otherwise fall back to default view point.220if (it != widget_actor_map_->end())221{222vtkMatrix4x4* m = vtkProp3D::SafeDownCast(it->second)->GetUserMatrix();223224cam->SetFocalPoint(m->GetElement(0, 3) - m->GetElement(0, 2),225m->GetElement(1, 3) - m->GetElement(1, 2),226m->GetElement(2, 3) - m->GetElement(2, 2));227228cam->SetViewUp (m->GetElement(0, 1), m->GetElement(1, 1), m->GetElement(2, 1));229cam->SetPosition(m->GetElement(0, 3), m->GetElement(1, 3), m->GetElement(2, 3));230}231else232{233cam->SetPosition(0, 0, 0);234cam->SetFocalPoint(0, 0, 1);235cam->SetViewUp(0, -1, 0);236}237238// go to the next actor for the next key-press event.239if (it != widget_actor_map_->end())240++it;241else242it = widget_actor_map_->begin();243244CurrentRenderer->SetActiveCamera(cam);245CurrentRenderer->ResetCameraClippingRange();246Interactor->Render();247}248249//////////////////////////////////////////////////////////////////////////////////////////////250251void cv::viz::vtkVizInteractorStyle::toggleStereo()252{253vtkSmartPointer<vtkRenderWindow> window = Interactor->GetRenderWindow();254if (!window->GetStereoRender())255{256static Vec2i red_blue(4, 3), magenta_green(2, 5);257window->SetAnaglyphColorMask (stereo_anaglyph_redblue_ ? red_blue.val : magenta_green.val);258stereo_anaglyph_redblue_ = !stereo_anaglyph_redblue_;259}260window->SetStereoRender(!window->GetStereoRender());261Interactor->Render();262263}264265//////////////////////////////////////////////////////////////////////////////////////////////266267void cv::viz::vtkVizInteractorStyle::printHelp()268{269std::cout << "| Help:\n"270"-------\n"271" p, P : switch to a point-based representation\n"272" w, W : switch to a wireframe-based representation (where available)\n"273" s, S : switch to a surface-based representation (where available)\n"274"\n"275" j, J : take a .PNG snapshot of the current window view\n"276" k, K : export scene to Wavefront .obj format\n"277" ALT + k, K : export scene to VRML format\n"278" c, C : display current camera/window parameters\n"279" F5 : enable/disable fly mode (changes control style)\n"280"\n"281" e, E : exit the interactor\n"282" q, Q : stop and call VTK's TerminateApp\n"283"\n"284" +/- : increment/decrement overall point size\n"285" +/- [+ ALT] : zoom in/out \n"286"\n"287" r, R [+ ALT] : reset camera [to viewpoint = {0, 0, 0} -> center_{x, y, z}]\n"288"\n"289" ALT + s, S : turn stereo mode on/off\n"290" ALT + f, F : switch between maximized window mode and original size\n"291"\n"292<< std::endl;293}294295//////////////////////////////////////////////////////////////////////////////////////////////296void cv::viz::vtkVizInteractorStyle::zoomIn()297{298FindPokedRenderer(Interactor->GetEventPosition()[0], Interactor->GetEventPosition()[1]);299// Zoom in300StartDolly();301double factor = 10.0 * 0.2 * .5;302Dolly(std::pow(1.1, factor));303EndDolly();304}305306//////////////////////////////////////////////////////////////////////////////////////////////307void cv::viz::vtkVizInteractorStyle::zoomOut()308{309FindPokedRenderer(Interactor->GetEventPosition()[0], Interactor->GetEventPosition()[1]);310// Zoom out311StartDolly();312double factor = 10.0 * -0.2 * .5;313Dolly(std::pow(1.1, factor));314EndDolly();315}316317//////////////////////////////////////////////////////////////////////////////////////////////318void cv::viz::vtkVizInteractorStyle::OnChar()319{320FindPokedRenderer(Interactor->GetEventPosition()[0], Interactor->GetEventPosition()[1]);321322String key(Interactor->GetKeySym());323if (key.find("XF86ZoomIn") != String::npos)324zoomIn();325else if (key.find("XF86ZoomOut") != String::npos)326zoomOut();327328switch (Interactor->GetKeyCode())329{330// // All of the options below simply exit331// case 'l': case 'L': case 'j': case 'J': case 'c': case 'C': case 'q': case 'Q':332// case 'f': case 'F': case 'g': case 'G': case 'o': case 'O': case 'u': case 'U':333case 'p': case 'P':334break;335336case '+':337if (FlyMode)338MotionUserScale = std::min(16.0, MotionUserScale*2.0);339break;340case '-':341if (FlyMode)342MotionUserScale = std::max(MotionUserScale * 0.5, 0.0625);343break;344345case 'r': case 'R': case 's': case 'S':346if (!Interactor->GetAltKey())347Superclass::OnChar();348break;349default:350Superclass::OnChar();351break;352}353}354355//////////////////////////////////////////////////////////////////////////////////////////////356void cv::viz::vtkVizInteractorStyle::registerMouseCallback(void (*callback)(const MouseEvent&, void*), void* cookie)357{358mouseCallback_ = callback;359mouse_callback_cookie_ = cookie;360}361362void cv::viz::vtkVizInteractorStyle::registerKeyboardCallback(void (*callback)(const KeyboardEvent&, void*), void *cookie)363{364keyboardCallback_ = callback;365keyboard_callback_cookie_ = cookie;366}367368//////////////////////////////////////////////////////////////////////////////////////////////369int cv::viz::vtkVizInteractorStyle::getModifiers()370{371int modifiers = KeyboardEvent::NONE;372373if (Interactor->GetAltKey())374modifiers |= KeyboardEvent::ALT;375376if (Interactor->GetControlKey())377modifiers |= KeyboardEvent::CTRL;378379if (Interactor->GetShiftKey())380modifiers |= KeyboardEvent::SHIFT;381return modifiers;382}383384//////////////////////////////////////////////////////////////////////////////////////////////385void cv::viz::vtkVizInteractorStyle::OnKeyDown()386{387FindPokedRenderer(Interactor->GetEventPosition()[0], Interactor->GetEventPosition()[1]);388389String key(Interactor->GetKeySym());390if (key.find("XF86ZoomIn") != String::npos)391zoomIn();392else if (key.find("XF86ZoomOut") != String::npos)393zoomOut();394else if (key.find("F5") != String::npos)395{396FlyMode = !FlyMode;397std::cout << (FlyMode ? "Fly mode: on" : "Fly mode: off") << std::endl;398}399400// Save the initial windows width/height401if (win_size_[0] == -1 || win_size_[1] == -1)402win_size_ = Vec2i(Interactor->GetRenderWindow()->GetSize());403404switch (Interactor->GetKeyCode())405{406case 'a': case 'A' : KeysDown |=16; break;407case 'z': case 'Z' : KeysDown |=32; break;408case 'h': case 'H' : printHelp(); break;409case 'p': case 'P' : setRepresentationToPoints(); break;410case 'k': case 'K' : exportScene(); break;411case 'j': case 'J' : saveScreenshot(cv::format("screenshot-%d.png", (unsigned int)time(0))); break;412case 'c': case 'C' : printCameraParams(); break;413case '=': zoomIn(); break;414case 43: // KEY_PLUS415{416if (FlyMode)417break;418if (Interactor->GetAltKey())419zoomIn();420else421changePointsSize(+1.f);422break;423}424case 45: // KEY_MINUS425{426if (FlyMode)427break;428if (Interactor->GetAltKey())429zoomOut();430else431changePointsSize(-1.f);432break;433}434// Switch between maximize and original window size435case 'f': case 'F':436{437if (Interactor->GetAltKey())438toggleFullScreen();439break;440}441// 's'/'S' w/out ALT442case 's': case 'S':443{444if (Interactor->GetAltKey())445toggleStereo();446break;447}448449case 'o': case 'O':450{451vtkSmartPointer<vtkCamera> cam = CurrentRenderer->GetActiveCamera();452cam->SetParallelProjection(!cam->GetParallelProjection());453Interactor->Render();454break;455}456457// Overwrite the camera reset458case 'r': case 'R':459{460if (Interactor->GetAltKey())461resetViewerPose();462break;463}464case 'q': case 'Q':465Interactor->ExitCallback(); return;466default:467Superclass::OnKeyDown(); break;468}469470KeyboardEvent event(KeyboardEvent::KEY_DOWN, Interactor->GetKeySym(), Interactor->GetKeyCode(), getModifiers());471if (keyboardCallback_)472keyboardCallback_(event, keyboard_callback_cookie_);473474if (FlyMode && (KeysDown & (32+16)) == (32+16))475{476if (State == VTKIS_FORWARDFLY || State == VTKIS_REVERSEFLY)477StopState();478}479else if (FlyMode && (KeysDown & 32) == 32)480{481if (State == VTKIS_FORWARDFLY)482StopState();483484if (State == VTKIS_NONE)485StartState(VTKIS_REVERSEFLY);486}487else if (FlyMode && (KeysDown & 16) == 16)488{489if (State == VTKIS_REVERSEFLY)490StopState();491492if (State == VTKIS_NONE)493StartState(VTKIS_FORWARDFLY);494}495496Interactor->Render();497}498499//////////////////////////////////////////////////////////////////////////////////////////////500void cv::viz::vtkVizInteractorStyle::OnKeyUp()501{502KeyboardEvent event(KeyboardEvent::KEY_UP, Interactor->GetKeySym(), Interactor->GetKeyCode(), getModifiers());503if (keyboardCallback_)504keyboardCallback_(event, keyboard_callback_cookie_);505506switch (Interactor->GetKeyCode())507{508case 'a': case 'A' : KeysDown &= ~16; break;509case 'z': case 'Z' : KeysDown &= ~32; break;510}511512if (State == VTKIS_FORWARDFLY && (KeysDown & 16) == 0)513StopState();514515if (State == VTKIS_REVERSEFLY && (KeysDown & 32) == 0)516StopState();517518Superclass::OnKeyUp();519}520521//////////////////////////////////////////////////////////////////////////////////////////////522void cv::viz::vtkVizInteractorStyle::OnMouseMove()523{524Vec2i p(Interactor->GetEventPosition());525MouseEvent event(MouseEvent::MouseMove, MouseEvent::NoButton, p, getModifiers());526if (mouseCallback_)527mouseCallback_(event, mouse_callback_cookie_);528529FindPokedRenderer(p[0], p[1]);530531if (State == VTKIS_ROTATE || State == VTKIS_PAN || State == VTKIS_DOLLY || State == VTKIS_SPIN)532{533switch (State)534{535case VTKIS_ROTATE: Rotate(); break;536case VTKIS_PAN: Pan(); break;537case VTKIS_DOLLY: Dolly(); break;538case VTKIS_SPIN: Spin(); break;539}540541InvokeEvent(vtkCommand::InteractionEvent, NULL);542}543544if (State == VTKIS_FORWARDFLY || State == VTKIS_REVERSEFLY)545{546vtkCamera *cam = CurrentRenderer->GetActiveCamera();547Vec2i thispos(Interactor->GetEventPosition());548Vec2i lastpos(Interactor->GetLastEventPosition());549550// we want to steer by an amount proportional to window viewangle and size551// compute dx and dy increments relative to last mouse click552Vec2i size(Interactor->GetSize());553double scalefactor = 5*cam->GetViewAngle()/size[0];554555double dx = - (thispos[0] - lastpos[0])*scalefactor*AngleStepSize;556double dy = (thispos[1] - lastpos[1])*scalefactor*AngleStepSize;557558// Temporary until I get smooth flight working559DeltaPitch = dy;560DeltaYaw = dx;561562InvokeEvent(vtkCommand::InteractionEvent, NULL);563}564}565566//////////////////////////////////////////////////////////////////////////////////////////////567void cv::viz::vtkVizInteractorStyle::OnLeftButtonDown()568{569Vec2i p(Interactor->GetEventPosition());570MouseEvent::Type type = (Interactor->GetRepeatCount() == 0) ? MouseEvent::MouseButtonPress : MouseEvent::MouseDblClick;571MouseEvent event(type, MouseEvent::LeftButton, p, getModifiers());572if (mouseCallback_)573mouseCallback_(event, mouse_callback_cookie_);574575FindPokedRenderer(p[0], p[1]);576if (!CurrentRenderer)577return;578579GrabFocus(EventCallbackCommand);580581if (FlyMode)582{583if(State == VTKIS_REVERSEFLY)584State = VTKIS_FORWARDFLY;585else586{587SetupMotionVars();588if (State == VTKIS_NONE)589StartState(VTKIS_FORWARDFLY);590}591}592else593{594if (Interactor->GetShiftKey())595{596if (Interactor->GetControlKey())597StartDolly();598else599StartPan();600}601else602{603if (Interactor->GetControlKey())604StartSpin();605else606StartRotate();607}608}609}610611//////////////////////////////////////////////////////////////////////////////////////////////612void cv::viz::vtkVizInteractorStyle::OnLeftButtonUp()613{614Vec2i p(Interactor->GetEventPosition());615MouseEvent event(MouseEvent::MouseButtonRelease, MouseEvent::LeftButton, p, getModifiers());616if (mouseCallback_)617mouseCallback_(event, mouse_callback_cookie_);618619switch (State)620{621case VTKIS_DOLLY: EndDolly(); break;622case VTKIS_PAN: EndPan(); break;623case VTKIS_SPIN: EndSpin(); break;624case VTKIS_ROTATE: EndRotate(); break;625case VTKIS_FORWARDFLY: StopState(); break;626}627628if (Interactor )629ReleaseFocus();630}631632//////////////////////////////////////////////////////////////////////////////////////////////633void cv::viz::vtkVizInteractorStyle::OnMiddleButtonDown()634{635Vec2i p(Interactor->GetEventPosition());636MouseEvent::Type type = (Interactor->GetRepeatCount() == 0) ? MouseEvent::MouseButtonPress : MouseEvent::MouseDblClick;637MouseEvent event(type, MouseEvent::MiddleButton, p, getModifiers());638if (mouseCallback_)639mouseCallback_(event, mouse_callback_cookie_);640641FindPokedRenderer(p[0], p[1]);642if (!CurrentRenderer)643return;644645GrabFocus(EventCallbackCommand);646StartPan();647}648649//////////////////////////////////////////////////////////////////////////////////////////////650void cv::viz::vtkVizInteractorStyle::OnMiddleButtonUp()651{652Vec2i p(Interactor->GetEventPosition());653MouseEvent event(MouseEvent::MouseButtonRelease, MouseEvent::MiddleButton, p, getModifiers());654if (mouseCallback_)655mouseCallback_(event, mouse_callback_cookie_);656657if (State == VTKIS_PAN)658{659EndPan();660if (Interactor)661ReleaseFocus();662}663}664665//////////////////////////////////////////////////////////////////////////////////////////////666void cv::viz::vtkVizInteractorStyle::OnRightButtonDown()667{668Vec2i p(Interactor->GetEventPosition());669MouseEvent::Type type = (Interactor->GetRepeatCount() == 0) ? MouseEvent::MouseButtonPress : MouseEvent::MouseDblClick;670MouseEvent event(type, MouseEvent::RightButton, p, getModifiers());671if (mouseCallback_)672mouseCallback_(event, mouse_callback_cookie_);673674FindPokedRenderer(p[0], p[1]);675if (!CurrentRenderer)676return;677678GrabFocus(EventCallbackCommand);679680if (FlyMode)681{682if (State == VTKIS_FORWARDFLY)683State = VTKIS_REVERSEFLY;684else685{686SetupMotionVars();687if (State == VTKIS_NONE)688StartState(VTKIS_REVERSEFLY);689}690691}692else693StartDolly();694}695696697//////////////////////////////////////////////////////////////////////////////////////////////698void cv::viz::vtkVizInteractorStyle::OnRightButtonUp()699{700Vec2i p(Interactor->GetEventPosition());701MouseEvent event(MouseEvent::MouseButtonRelease, MouseEvent::RightButton, p, getModifiers());702if (mouseCallback_)703mouseCallback_(event, mouse_callback_cookie_);704705if(State == VTKIS_DOLLY)706{707EndDolly();708if (Interactor)709ReleaseFocus();710}711712if (State == VTKIS_REVERSEFLY)713{714StopState();715if (Interactor)716ReleaseFocus();717}718}719720//////////////////////////////////////////////////////////////////////////////////////////////721void cv::viz::vtkVizInteractorStyle::OnMouseWheelForward()722{723Vec2i p(Interactor->GetEventPosition());724MouseEvent event(MouseEvent::MouseScrollUp, MouseEvent::VScroll, p, getModifiers());725if (mouseCallback_)726mouseCallback_(event, mouse_callback_cookie_);727if (Interactor->GetRepeatCount() && mouseCallback_)728mouseCallback_(event, mouse_callback_cookie_);729730if (Interactor->GetAltKey())731{732// zoom733vtkSmartPointer<vtkCamera> cam = CurrentRenderer->GetActiveCamera();734double opening_angle = cam->GetViewAngle();735if (opening_angle > 15.0)736opening_angle -= 1.0;737738cam->SetViewAngle(opening_angle);739cam->Modified();740CurrentRenderer->ResetCameraClippingRange();741CurrentRenderer->Modified();742Interactor->Render();743}744else745{746FindPokedRenderer(p[0], p[1]);747if (!CurrentRenderer)748return;749750GrabFocus(EventCallbackCommand);751StartDolly();752Dolly(pow(1.1, MotionFactor * 0.2 * MouseWheelMotionFactor));753EndDolly();754ReleaseFocus();755}756}757758//////////////////////////////////////////////////////////////////////////////////////////////759void cv::viz::vtkVizInteractorStyle::OnMouseWheelBackward()760{761Vec2i p(Interactor->GetEventPosition());762MouseEvent event(MouseEvent::MouseScrollDown, MouseEvent::VScroll, p, getModifiers());763if (mouseCallback_)764mouseCallback_(event, mouse_callback_cookie_);765766if (Interactor->GetRepeatCount() && mouseCallback_)767mouseCallback_(event, mouse_callback_cookie_);768769if (Interactor->GetAltKey())770{771// zoom772vtkSmartPointer<vtkCamera> cam = CurrentRenderer->GetActiveCamera();773double opening_angle = cam->GetViewAngle();774if (opening_angle < 170.0)775opening_angle += 1.0;776777cam->SetViewAngle(opening_angle);778cam->Modified();779CurrentRenderer->ResetCameraClippingRange();780CurrentRenderer->Modified();781Interactor->Render();782}783else784{785FindPokedRenderer(p[0], p[1]);786if (!CurrentRenderer)787return;788789GrabFocus(EventCallbackCommand);790StartDolly();791Dolly(pow(1.1, MotionFactor * -0.2 * MouseWheelMotionFactor));792EndDolly();793ReleaseFocus();794}795}796797//////////////////////////////////////////////////////////////////////////////////////////////798void cv::viz::vtkVizInteractorStyle::OnTimer()799{800if (State == VTKIS_FORWARDFLY || State == VTKIS_REVERSEFLY)801Fly();802803Interactor->Render();804}805806//////////////////////////////////////////////////////////////////////////////////////////////807808void cv::viz::vtkVizInteractorStyle::Rotate()809{810if (!CurrentRenderer)811return;812813Vec2i dxy = Vec2i(Interactor->GetEventPosition()) - Vec2i(Interactor->GetLastEventPosition());814Vec2i size(CurrentRenderer->GetRenderWindow()->GetSize());815816double delta_elevation = -20.0 / size[1];817double delta_azimuth = -20.0 / size[0];818819double rxf = dxy[0] * delta_azimuth * MotionFactor;820double ryf = dxy[1] * delta_elevation * MotionFactor;821822vtkCamera *camera = CurrentRenderer->GetActiveCamera();823camera->Azimuth(rxf);824camera->Elevation(ryf);825camera->OrthogonalizeViewUp();826827if (AutoAdjustCameraClippingRange)828CurrentRenderer->ResetCameraClippingRange();829830if (Interactor->GetLightFollowCamera())831CurrentRenderer->UpdateLightsGeometryToFollowCamera();832833Interactor->Render();834}835836//////////////////////////////////////////////////////////////////////////////////////////////837void cv::viz::vtkVizInteractorStyle::Spin()838{839if (!CurrentRenderer)840return;841842vtkRenderWindowInteractor *rwi = Interactor;843844double *center = CurrentRenderer->GetCenter();845846double newAngle = vtkMath::DegreesFromRadians( atan2( rwi->GetEventPosition()[1] - center[1], rwi->GetEventPosition()[0] - center[0] ) );847double oldAngle = vtkMath::DegreesFromRadians( atan2( rwi->GetLastEventPosition()[1] - center[1], rwi->GetLastEventPosition()[0] - center[0] ) );848849vtkCamera *camera = CurrentRenderer->GetActiveCamera();850camera->Roll( newAngle - oldAngle );851camera->OrthogonalizeViewUp();852853rwi->Render();854}855856//////////////////////////////////////////////////////////////////////////////////////////////857void cv::viz::vtkVizInteractorStyle::Pan()858{859if (!CurrentRenderer)860return;861862vtkRenderWindowInteractor *rwi = Interactor;863864double viewFocus[4], focalDepth, viewPoint[3];865double newPickPoint[4], oldPickPoint[4], motionVector[3];866867// Calculate the focal depth since we'll be using it a lot868869vtkCamera *camera = CurrentRenderer->GetActiveCamera();870camera->GetFocalPoint(viewFocus);871ComputeWorldToDisplay(viewFocus[0], viewFocus[1], viewFocus[2], viewFocus);872focalDepth = viewFocus[2];873874ComputeDisplayToWorld(rwi->GetEventPosition()[0], rwi->GetEventPosition()[1], focalDepth, newPickPoint);875876// Has to recalc old mouse point since the viewport has moved, so can't move it outside the loop877ComputeDisplayToWorld(rwi->GetLastEventPosition()[0], rwi->GetLastEventPosition()[1], focalDepth, oldPickPoint);878879// Camera motion is reversed880motionVector[0] = oldPickPoint[0] - newPickPoint[0];881motionVector[1] = oldPickPoint[1] - newPickPoint[1];882motionVector[2] = oldPickPoint[2] - newPickPoint[2];883884camera->GetFocalPoint(viewFocus);885camera->GetPosition(viewPoint);886camera->SetFocalPoint(motionVector[0] + viewFocus[0], motionVector[1] + viewFocus[1], motionVector[2] + viewFocus[2]);887camera->SetPosition( motionVector[0] + viewPoint[0], motionVector[1] + viewPoint[1], motionVector[2] + viewPoint[2]);888889if (Interactor->GetLightFollowCamera())890CurrentRenderer->UpdateLightsGeometryToFollowCamera();891892Interactor->Render();893}894895//////////////////////////////////////////////////////////////////////////////////////////////896897void cv::viz::vtkVizInteractorStyle::Dolly()898{899if (!CurrentRenderer)900return;901902int dy = Interactor->GetEventPosition()[1] - Interactor->GetLastEventPosition()[1];903Dolly(pow(1.1, MotionFactor * dy / CurrentRenderer->GetCenter()[1]));904}905906void cv::viz::vtkVizInteractorStyle::Dolly(double factor)907{908if (!CurrentRenderer)909return;910911vtkCamera *camera = CurrentRenderer->GetActiveCamera();912if (camera->GetParallelProjection())913camera->SetParallelScale(camera->GetParallelScale() / factor);914else915{916camera->Dolly(factor);917if (AutoAdjustCameraClippingRange)918CurrentRenderer->ResetCameraClippingRange();919}920921if (Interactor->GetLightFollowCamera())922CurrentRenderer->UpdateLightsGeometryToFollowCamera();923924Interactor->Render();925}926//////////////////////////////////////////////////////////////////////////////////////////////927928void cv::viz::vtkVizInteractorStyle::Fly()929{930if (CurrentRenderer == NULL)931return;932933if (KeysDown)934FlyByKey();935else936FlyByMouse();937938CurrentRenderer->GetActiveCamera()->OrthogonalizeViewUp();939940if (AutoAdjustCameraClippingRange)941CurrentRenderer->ResetCameraClippingRange();942943if (Interactor->GetLightFollowCamera())944CurrentRenderer->UpdateLightsGeometryToFollowCamera();945}946947void cv::viz::vtkVizInteractorStyle::SetupMotionVars()948{949Vec6d bounds;950CurrentRenderer->ComputeVisiblePropBounds(bounds.val);951952if ( !vtkMath::AreBoundsInitialized(bounds.val) )953DiagonalLength = 1.0;954else955DiagonalLength = norm(Vec3d(bounds[0], bounds[2], bounds[4]) - Vec3d(bounds[1], bounds[3], bounds[5]));956}957958void cv::viz::vtkVizInteractorStyle::MotionAlongVector(const Vec3d& vector, double amount, vtkCamera* cam)959{960// move camera and focus along DirectionOfProjection961Vec3d campos = Vec3d(cam->GetPosition()) - amount * vector;962Vec3d camfoc = Vec3d(cam->GetFocalPoint()) - amount * vector;963964cam->SetPosition(campos.val);965cam->SetFocalPoint(camfoc.val);966}967968void cv::viz::vtkVizInteractorStyle::FlyByMouse()969{970vtkCamera* cam = CurrentRenderer->GetActiveCamera();971double speed = DiagonalLength * MotionStepSize * MotionUserScale;972speed = speed * ( Interactor->GetShiftKey() ? MotionAccelerationFactor : 1.0);973974// Sidestep975if (Interactor->GetAltKey())976{977if (DeltaYaw!=0.0)978{979vtkMatrix4x4 *vtm = cam->GetViewTransformMatrix();980Vec3d a_vector(vtm->GetElement(0,0), vtm->GetElement(0,1), vtm->GetElement(0,2));981982MotionAlongVector(a_vector, -DeltaYaw*speed, cam);983}984if (DeltaPitch!=0.0)985{986Vec3d a_vector(cam->GetViewUp());987MotionAlongVector(a_vector, DeltaPitch*speed, cam);988}989}990else991{992cam->Yaw(DeltaYaw);993cam->Pitch(DeltaPitch);994DeltaYaw = 0;995DeltaPitch = 0;996}997//998if (!Interactor->GetControlKey())999{1000Vec3d a_vector(cam->GetDirectionOfProjection()); // reversed (use -speed)1001switch (State)1002{1003case VTKIS_FORWARDFLY: MotionAlongVector(a_vector, -speed, cam); break;1004case VTKIS_REVERSEFLY: MotionAlongVector(a_vector, speed, cam); break;1005}1006}1007}10081009void cv::viz::vtkVizInteractorStyle::FlyByKey()1010{1011vtkCamera* cam = CurrentRenderer->GetActiveCamera();10121013double speed = DiagonalLength * MotionStepSize * MotionUserScale;1014speed = speed * ( Interactor->GetShiftKey() ? MotionAccelerationFactor : 1.0);10151016// Left and right1017if (Interactor->GetAltKey())1018{ // Sidestep1019vtkMatrix4x4 *vtm = cam->GetViewTransformMatrix();1020Vec3d a_vector(vtm->GetElement(0,0), vtm->GetElement(0,1), vtm->GetElement(0,2));10211022if (KeysDown & 1)1023MotionAlongVector(a_vector, -speed, cam);10241025if (KeysDown & 2)1026MotionAlongVector(a_vector, speed, cam);1027}1028else1029{1030if (KeysDown & 1)1031cam->Yaw( AngleStepSize);10321033if (KeysDown & 2)1034cam->Yaw(-AngleStepSize);1035}10361037// Up and Down1038if (Interactor->GetControlKey())1039{ // Sidestep1040Vec3d a_vector = Vec3d(cam->GetViewUp());1041if (KeysDown & 4)1042MotionAlongVector(a_vector,-speed, cam);10431044if (KeysDown & 8)1045MotionAlongVector(a_vector, speed, cam);1046}1047else1048{1049if (KeysDown & 4)1050cam->Pitch(-AngleStepSize);10511052if (KeysDown & 8)1053cam->Pitch( AngleStepSize);1054}10551056// forward and backward1057Vec3d a_vector(cam->GetDirectionOfProjection());1058if (KeysDown & 16)1059MotionAlongVector(a_vector, speed, cam);10601061if (KeysDown & 32)1062MotionAlongVector(a_vector,-speed, cam);1063}10641065//////////////////////////////////////////////////////////////////////////////////////////////10661067void cv::viz::vtkVizInteractorStyle::PrintSelf(ostream& os, vtkIndent indent)1068{1069Superclass::PrintSelf(os, indent);1070os << indent << "MotionFactor: " << MotionFactor << "\n";1071os << indent << "MotionStepSize: " << MotionStepSize << "\n";1072os << indent << "MotionAccelerationFactor: "<< MotionAccelerationFactor << "\n";1073os << indent << "AngleStepSize: " << AngleStepSize << "\n";1074os << indent << "MotionUserScale: "<< MotionUserScale << "\n";1075}107610771078