You are welcome. <div><br>Glad that I was able to help. </div><div><br><br><div class="gmail_quote">On Tue, Dec 14, 2010 at 6:02 AM, Rodrigo M. <span dir="ltr"><<a href="mailto:rodrigo.geof@gmail.com">rodrigo.geof@gmail.com</a>></span> wrote:<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex;">Hi Aashish,<br>
<br>
Thanks a lot, this is exactly what I was looking for. =)<br>
<br>
2010/12/14 Aashish Chaudhary <<a href="mailto:aashish.chaudhary@kitware.com">aashish.chaudhary@kitware.com</a>>:<br>
<div><div></div><div class="h5">> Hi Rodrigo,<br>
> If you have not found the answer this might help:<br>
> On the style you can query GetStartPosition() and GetEndPosition()<br>
> Convert these positions to world coordinate using vtkViewPort functions<br>
> something like this:<br>
><br>
> double startWorldPosition[4], endWorldPosition[4];<br>
><br>
> this->MainRenderer->SetViewPoint(<br>
><br>
> (startPosition[0] - halfWidth) / halfWidth,<br>
><br>
> (startPosition[1] - halfHeight) / halfHeight,<br>
><br>
> 1);<br>
><br>
> this->MainRenderer->ViewToWorld();<br>
><br>
> this->MainRenderer->GetWorldPoint(startWorldPosition);<br>
><br>
> this->MainRenderer->SetViewPoint(<br>
><br>
> (endPosition[0] - halfWidth) / halfWidth,<br>
><br>
> (endPosition[1] - halfHeight) / halfHeight,<br>
><br>
> 1);<br>
><br>
> this->MainRenderer->ViewToWorld();<br>
><br>
> this->MainRenderer->GetWorldPoint(endWorldPosition);<br>
><br>
> int targetExtents[4] = {<br>
><br>
> startWorldPosition[0] < endWorldPosition[0] ? startWorldPosition[0] :<br>
> endWorldPosition[0],<br>
><br>
> startWorldPosition[0] > endWorldPosition[0] ? startWorldPosition[0] :<br>
> endWorldPosition[0],<br>
><br>
> startWorldPosition[1] < endWorldPosition[1] ? startWorldPosition[1] :<br>
> endWorldPosition[1],<br>
><br>
> startWorldPosition[1] > endWorldPosition[1] ? startWorldPosition[1] :<br>
> endWorldPosition[1]};<br>
><br>
> Once you get target extents you can set camera center to center of these<br>
> extents something like this:<br>
><br>
><br>
> camera->SetPosition(newCenter[0], newCenter[1], 1);<br>
><br>
> camera->SetFocalPoint(newCenter[0], newCenter[1], 0);<br>
><br>
> camera->SetViewUp(0, 1, 0); // just to make sure<br>
><br>
> camera->SetParallelScale((newExtents[3] - newExtents[2]) / 2.0);<br>
><br>
> Hope this helps.<br>
> On Tue, Dec 7, 2010 at 4:14 PM, rodrigo <<a href="mailto:rodrigo.geof@gmail.com">rodrigo.geof@gmail.com</a>> wrote:<br>
>><br>
>> Hello, I'm new to vtk, what I want to do is to zoom on a 2D image using a<br>
>> Rubber band box, and to have my mouse coordinates showed up.<br>
>><br>
>> I tried to modify one of the wiki examples, but I couldn't make the mouse<br>
>> coordinates be showed up and have the rubber band at the same time.<br>
>> Besides<br>
>> that, I have no idea on how to make the rubber band zoom my image. I<br>
>> really<br>
>> want to use the coordinates grabbed by the rubber box to zoom the image.<br>
>><br>
>> Here is my code:<br>
>><br>
>> #include <vtk-5.2/vtkAssemblyPath.h><br>
>> #include <vtk-5.2/vtkCell.h><br>
>> #include <vtk-5.2/vtkCommand.h><br>
>> #include <vtk-5.2/vtkCornerAnnotation.h><br>
>> #include <vtk-5.2/vtkImageActor.h><br>
>> #include <vtk-5.2/vtkImageData.h><br>
>> #include <vtk-5.2/vtkImageViewer2.h><br>
>> #include <vtk-5.2/vtkInteractorStyleImage.h><br>
>> #include <vtk-5.2/vtkJPEGReader.h><br>
>> #include <vtk-5.2/vtkPointData.h><br>
>> #include <vtk-5.2/vtkPropPicker.h><br>
>> #include <vtk-5.2/vtkRenderer.h><br>
>> #include <vtk-5.2/vtkRenderWindow.h><br>
>> #include <vtk-5.2/vtkRenderWindowInteractor.h><br>
>> #include <vtk-5.2/vtkSmartPointer.h><br>
>> #include <vtk-5.2/vtkTextProperty.h><br>
>> #include <vtkInteractorStyleRubberBand2D.h><br>
>> #include <vtkCallbackCommand.h><br>
>><br>
>> // The mouse motion callback, to pick the image and recover pixel values<br>
>> class vtkImageInteractionCallback1 : public vtkCommand<br>
>> {<br>
>> public:<br>
>><br>
>> static vtkImageInteractionCallback1 *New()<br>
>> {<br>
>> return new vtkImageInteractionCallback1;<br>
>> }<br>
>><br>
>> vtkImageInteractionCallback1()<br>
>> {<br>
>> this->Viewer = 0;<br>
>> this->Picker = 0;<br>
>> this->Annotation = 0;<br>
>> this->PointData = vtkPointData::New();<br>
>> }<br>
>><br>
>> ~vtkImageInteractionCallback1()<br>
>> {<br>
>> this->Viewer = 0;<br>
>> this->Picker = 0;<br>
>> this->Annotation = 0;<br>
>> this->PointData->Delete();<br>
>> }<br>
>><br>
>> void SetPicker(vtkPropPicker *picker)<br>
>> {<br>
>> this->Picker = picker;<br>
>> }<br>
>><br>
>> void SetAnnotation(vtkCornerAnnotation *annotation)<br>
>> {<br>
>> this->Annotation = annotation;<br>
>> }<br>
>><br>
>> void SetViewer(vtkImageViewer2 *viewer)<br>
>> {<br>
>> this->Viewer = viewer;<br>
>> }<br>
>><br>
>> virtual void Execute(vtkObject *, unsigned long vtkNotUsed(event),<br>
>> void *)<br>
>> {<br>
>> //this->Viewer;<br>
>> vtkRenderWindowInteractor *interactor =<br>
>> this->Viewer->GetRenderWindow()->GetInteractor();<br>
>> vtkRenderer* renderer = this->Viewer->GetRenderer();<br>
>> vtkImageActor* actor = this->Viewer->GetImageActor();<br>
>> vtkImageData* image = this->Viewer->GetInput();<br>
>> vtkInteractorStyle *style =<br>
>> vtkInteractorStyle::SafeDownCast(<br>
>> interactor->GetInteractorStyle());<br>
>><br>
>> image->Update();<br>
>><br>
>> // Pick at the mouse location provided by the interactor<br>
>> this->Picker->Pick( interactor->GetEventPosition()[0],<br>
>> interactor->GetEventPosition()[1],<br>
>> 0.0, renderer );<br>
>><br>
>> // There could be other props assigned to this picker, so<br>
>> // make sure we picked the image actor<br>
>> vtkAssemblyPath* path = this->Picker->GetPath();<br>
>> bool validPick = false;<br>
>><br>
>> if( path )<br>
>> {<br>
>> vtkCollectionSimpleIterator sit;<br>
>> path->InitTraversal( sit );<br>
>> vtkAssemblyNode *node;<br>
>> for( int i = 0; i < path->GetNumberOfItems() &&<br>
>> !validPick; ++i )<br>
>> {<br>
>> node = path->GetNextNode( sit );<br>
>> if( actor == vtkImageActor::SafeDownCast(<br>
>> node->GetViewProp() ) )<br>
>> {<br>
>> validPick = true;<br>
>> }<br>
>> }<br>
>> }<br>
>><br>
>> if( !validPick )<br>
>> {<br>
>> this->Annotation->SetText( 0, "Off Image" );<br>
>> interactor->Render();<br>
>> // Pass the event further on<br>
>> style->OnMouseMove();<br>
>> return;<br>
>> }<br>
>><br>
>> // Get the world coordinates of the pick<br>
>> double pos[3];<br>
>> this->Picker->GetPickPosition( pos );<br>
>> // Fixes some numerical problems with the picking<br>
>> double *bounds = actor->GetDisplayBounds();<br>
>> int axis = this->Viewer->GetSliceOrientation();<br>
>> pos[axis] = bounds[2*axis];<br>
>><br>
>> vtkPointData* pd = image->GetPointData();<br>
>> if( !pd )<br>
>> {<br>
>> return;<br>
>> }<br>
>><br>
>> this->PointData->InterpolateAllocate( pd, 1, 1 );<br>
>><br>
>> // Use tolerance as a function of size of source data<br>
>> double tol2 = image->GetLength();<br>
>> tol2 = tol2 ? tol2*tol2 / 1000.0 : 0.001;<br>
>><br>
>> // Find the cell that contains pos<br>
>> int subId;<br>
>> double pcoords[3], weights[8];<br>
>> vtkCell* cell = image->FindAndGetCell(<br>
>> pos, NULL, -1, tol2, subId, pcoords,<br>
>> weights );<br>
>> if( cell )<br>
>> {<br>
>> // Interpolate the point data<br>
>> this->PointData->InterpolatePoint( pd, 0,<br>
>> cell->PointIds, weights );<br>
>> int components =<br>
>><br>
>> this->PointData->GetScalars()->GetNumberOfComponents();<br>
>> double* tuple =<br>
>> this->PointData->GetScalars()->GetTuple( 0 );<br>
>><br>
>> std::string message = "Location: ( ";<br>
>> message += vtkVariant( pos[0] ).ToString();<br>
>> message += ", ";<br>
>> message += vtkVariant( pos[1] ).ToString();<br>
>> message += ", ";<br>
>> message += vtkVariant( pos[2] ).ToString();<br>
>> message += " )\nValue: ( ";<br>
>><br>
>> for( int c = 0; c < components; ++c )<br>
>> {<br>
>> message += vtkVariant( tuple[ c ]<br>
>> ).ToString();<br>
>> if( c != components - 1 )<br>
>> {<br>
>> message += ", ";<br>
>> }<br>
>> }<br>
>> message += " )";<br>
>> this->Annotation->SetText( 0, message.c_str() );<br>
>> interactor->Render();<br>
>> style->OnMouseMove();<br>
>> }<br>
>> }<br>
>><br>
>> private:<br>
>><br>
>> // Pointer to the viewer<br>
>> vtkImageViewer2 *Viewer;<br>
>><br>
>> // Pointer to the picker<br>
>> vtkPropPicker *Picker;<br>
>><br>
>> // Pointer to the annotation<br>
>> vtkCornerAnnotation *Annotation;<br>
>><br>
>> // Interpolator<br>
>> vtkPointData* PointData;<br>
>> };<br>
>><br>
>><br>
>> void SelectionChangedCallbackFunction ( vtkObject* vtkNotUsed(caller),<br>
>> long unsigned int<br>
>> vtkNotUsed(eventId), void* vtkNotUsed(clientData),<br>
>> void* callData )<br>
>> {<br>
>> std::cout << "SelectionChanged callback" << std::endl;<br>
>><br>
>> unsigned int* rect = reinterpret_cast<unsigned int*> ( callData );<br>
>> unsigned int pos1X = rect[0];<br>
>> unsigned int pos1Y = rect[1];<br>
>> unsigned int pos2X = rect[2];<br>
>> unsigned int pos2Y = rect[3];<br>
>><br>
>> std::cout << "Start x: " << pos1X << " Start y: " << pos1Y<br>
>> << " End x: " << pos2X << " End y: " << pos2Y << std::endl;<br>
>> }<br>
>><br>
>> int main ( int argc, char* argv[] )<br>
>> {<br>
>> //Verify input arguments<br>
>> if ( argc != 2 )<br>
>> {<br>
>> std::cout << "Usage: " << argv[0]<br>
>> << " Filename(jpeg)" << std::endl;<br>
>> return EXIT_FAILURE;<br>
>> }<br>
>><br>
>> //Parse input argument<br>
>> std::string inputFilename = argv[1];<br>
>><br>
>> //Read the image<br>
>> vtkSmartPointer<vtkJPEGReader> jpegReader =<br>
>> vtkSmartPointer<vtkJPEGReader>::New();<br>
>> if( !jpegReader->CanReadFile( inputFilename.c_str() ) )<br>
>> {<br>
>> std::cout << argv[0] << ": Error reading file "<br>
>> << inputFilename << endl << "Exiting..." << endl;<br>
>> return EXIT_FAILURE;<br>
>> }<br>
>> jpegReader->SetFileName ( inputFilename.c_str() );<br>
>><br>
>> // Picker to pick pixels<br>
>> vtkSmartPointer<vtkPropPicker> propPicker =<br>
>> vtkSmartPointer<vtkPropPicker>::New();<br>
>> propPicker->PickFromListOn();<br>
>><br>
>> // Give the picker a prop to pick<br>
>> vtkSmartPointer<vtkImageViewer2> imageViewer =<br>
>> vtkSmartPointer<vtkImageViewer2>::New();<br>
>> propPicker->AddPickList( imageViewer->GetImageActor() );<br>
>><br>
>> // Visualize<br>
>> vtkSmartPointer<vtkRenderWindowInteractor> renderWindowInteractor =<br>
>> vtkSmartPointer<vtkRenderWindowInteractor>::New();<br>
>> imageViewer->SetInputConnection( jpegReader->GetOutputPort() );<br>
>> imageViewer->SetupInteractor( renderWindowInteractor );<br>
>> imageViewer->SetSize( 600, 600 );<br>
>><br>
>> vtkRenderer* renderer = imageViewer->GetRenderer();<br>
>> renderer->ResetCamera();<br>
>> renderer->GradientBackgroundOn();<br>
>> renderer->SetBackground(0,0,0);<br>
>> renderer->SetBackground2(1,1,1);<br>
>><br>
>> // Annotate the image with window/level and mouse over pixel<br>
>> information<br>
>> vtkSmartPointer<vtkCornerAnnotation> cornerAnnotation =<br>
>> vtkSmartPointer<vtkCornerAnnotation>::New();<br>
>> cornerAnnotation->SetLinearFontScaleFactor( 2 );<br>
>> cornerAnnotation->SetNonlinearFontScaleFactor( 1 );<br>
>> cornerAnnotation->SetMaximumFontSize( 20 );<br>
>> cornerAnnotation->SetText( 0, "Off Image" );<br>
>> cornerAnnotation->SetText( 3, "<window>\n<level>" );<br>
>> cornerAnnotation->GetTextProperty()->SetColor( 1,0,0);<br>
>><br>
>> imageViewer->GetRenderer()->AddViewProp( cornerAnnotation );<br>
>><br>
>> // Callback listens to MouseMoveEvents invoked by the interactor's<br>
>> style<br>
>> vtkSmartPointer<vtkImageInteractionCallback1> callback =<br>
>> vtkSmartPointer<vtkImageInteractionCallback1>::New();<br>
>> callback->SetViewer( imageViewer );<br>
>> callback->SetAnnotation( cornerAnnotation );<br>
>> callback->SetPicker( propPicker );<br>
>><br>
>> // InteractorStyleImage allows for the following controls:<br>
>> // 1) middle mouse + move = camera pan<br>
>> // 2) left mouse + move = window/level<br>
>> // 3) right mouse + move = camera zoom<br>
>> // 4) middle mouse wheel scroll = zoom<br>
>> // 5) 'r' = reset window/level<br>
>> // 6) shift + 'r' = reset camera<br>
>> vtkInteractorStyleImage* imageStyle =<br>
>> imageViewer->GetInteractorStyle();<br>
>> imageStyle->AddObserver( vtkCommand::MouseMoveEvent, callback );<br>
>><br>
>> vtkSmartPointer<vtkCallbackCommand> selectionChangedCallback =<br>
>> vtkSmartPointer<vtkCallbackCommand>::New();<br>
>> selectionChangedCallback->SetCallback (<br>
>> SelectionChangedCallbackFunction );<br>
>><br>
>> vtkSmartPointer<vtkInteractorStyleRubberBand2D> style =<br>
>> vtkSmartPointer<vtkInteractorStyleRubberBand2D>::New();<br>
>> style->AddObserver ( vtkCommand::SelectionChangedEvent,<br>
>> selectionChangedCallback );<br>
>> renderWindowInteractor->SetInteractorStyle( style );<br>
>><br>
>> renderWindowInteractor->Initialize();<br>
>> renderWindowInteractor->Start();<br>
>><br>
>> return EXIT_SUCCESS;<br>
>> }<br>
>><br>
>> --<br>
>> View this message in context:<br>
>> <a href="http://vtk.1045678.n5.nabble.com/Rubber-band-zoom-and-pick-on-2d-image-tp3296495p3296495.html" target="_blank">http://vtk.1045678.n5.nabble.com/Rubber-band-zoom-and-pick-on-2d-image-tp3296495p3296495.html</a><br>
>> Sent from the VTK - Users mailing list archive at Nabble.com.<br>
>> _______________________________________________<br>
>> Powered by <a href="http://www.kitware.com" target="_blank">www.kitware.com</a><br>
>><br>
>> Visit other Kitware open-source projects at<br>
>> <a href="http://www.kitware.com/opensource/opensource.html" target="_blank">http://www.kitware.com/opensource/opensource.html</a><br>
>><br>
>> Please keep messages on-topic and check the VTK FAQ at:<br>
>> <a href="http://www.vtk.org/Wiki/VTK_FAQ" target="_blank">http://www.vtk.org/Wiki/VTK_FAQ</a><br>
>><br>
>> Follow this link to subscribe/unsubscribe:<br>
>> <a href="http://www.vtk.org/mailman/listinfo/vtkusers" target="_blank">http://www.vtk.org/mailman/listinfo/vtkusers</a><br>
><br>
><br>
><br>
> --<br>
> | Aashish Chaudhary<br>
> | R&D Engineer<br>
> | Kitware Inc.<br>
> | <a href="http://www.kitware.com" target="_blank">www.kitware.com</a><br>
><br>
</div></div></blockquote></div><br><br clear="all"><br>-- <br>| Aashish Chaudhary <br>| R&D Engineer <br>| Kitware Inc. <br>| <a href="http://www.kitware.com">www.kitware.com</a> <br>
</div>