[vtkusers] Information during callback / vtkImplicitPlaneWidget2
David Doria
daviddoria+vtk at gmail.com
Fri Mar 12 09:42:43 EST 2010
On Fri, Mar 12, 2010 at 7:17 AM, Sebastian Gatzka <
sebastian.gatzka at stud.tu-darmstadt.de> wrote:
> Or can I talk to the textActor like this?
>
> vtkTextActor *ta = reinterpret_cast<vtkTextActor*>(caller);
> ta->SetInput("TEST");
>
> or like this?
>
> this->TextActor->SetInput("TEST");
>
> No, does not work either way.
>
>
Ok, let's close this question - here is an example that updates a counter in
the lower left corner of the renderwindow every time an interaction occurs.
This should be easy to adapt to do what you want.
#include <sstream>
#include <vtkSmartPointer.h>
#include <vtkProperty.h>
#include <vtkTextProperty.h>
#include <vtkXMLPolyDataReader.h>
#include <vtkSphereSource.h>
#include <vtkClipPolyData.h>
#include <vtkPlane.h>
#include <vtkCommand.h>
#include <vtkImplicitPlaneWidget2.h>
#include <vtkImplicitPlaneRepresentation.h>
#include <vtkPolyDataMapper.h>
#include <vtkProperty.h>
#include <vtkActor.h>
#include <vtkTextActor.h>
#include <vtkRenderWindow.h>
#include <vtkRenderer.h>
#include <vtkRenderWindowInteractor.h>
// Callback for the interaction
// This does the actual work: updates the vtkPlane implicit function.
// This in turn causes the pipeline to update and clip the object.
class vtkIPWCallback : public vtkCommand
{
public:
static vtkIPWCallback *New()
{
return new vtkIPWCallback;
}
virtual void Execute(vtkObject *caller, unsigned long, void*)
{
vtkImplicitPlaneWidget2 *planeWidget =
reinterpret_cast<vtkImplicitPlaneWidget2*>(caller);
vtkImplicitPlaneRepresentation *rep =
reinterpret_cast<vtkImplicitPlaneRepresentation*>(planeWidget->GetRepresentation());
rep->GetPlane(this->Plane);
std::stringstream ss;
ss << this->counter;
this->TextActor->SetInput (ss.str().c_str());
this->counter++;
}
vtkIPWCallback():Plane(0),TextActor(0),counter(0) {}
void SetPlane(vtkPlane* plane) { this->Plane = plane;}
void SetTextActor(vtkTextActor* textActor) { this->TextActor =
textActor;}
private:
unsigned int counter;
vtkPlane* Plane;
vtkTextActor* TextActor;
};
int main(int argc, char *argv[])
{
vtkSmartPointer<vtkSphereSource> sphereSource =
vtkSmartPointer<vtkSphereSource>::New();
sphereSource->SetRadius(10.0);
vtkSmartPointer<vtkXMLPolyDataReader> reader =
vtkSmartPointer<vtkXMLPolyDataReader>::New();
// Setup a visualization pipeline
vtkSmartPointer<vtkPlane> plane =
vtkSmartPointer<vtkPlane>::New();
vtkSmartPointer<vtkClipPolyData> clipper =
vtkSmartPointer<vtkClipPolyData>::New();
clipper->SetClipFunction(plane);
clipper->InsideOutOn();
if (argc < 2)
{
clipper->SetInputConnection(sphereSource->GetOutputPort());
}
else
{
reader->SetFileName(argv[1]);
clipper->SetInputConnection(reader->GetOutputPort());
}
//Create a mapper and actor
vtkSmartPointer<vtkPolyDataMapper> mapper =
vtkSmartPointer<vtkPolyDataMapper>::New();
mapper->SetInputConnection(clipper->GetOutputPort());
vtkSmartPointer<vtkActor> actor =
vtkSmartPointer<vtkActor>::New();
actor->SetMapper(mapper);
vtkSmartPointer<vtkProperty> backFaces =
vtkSmartPointer<vtkProperty>::New();
backFaces->SetDiffuseColor(.8, .8, .4);
actor->SetBackfaceProperty(backFaces);
// a renderer and render window
vtkSmartPointer<vtkRenderer> renderer =
vtkSmartPointer<vtkRenderer>::New();
vtkSmartPointer<vtkRenderWindow> renderWindow =
vtkSmartPointer<vtkRenderWindow>::New();
renderWindow->AddRenderer(renderer);
renderer->AddActor(actor);
// an interactor
vtkSmartPointer<vtkRenderWindowInteractor> renderWindowInteractor =
vtkSmartPointer<vtkRenderWindowInteractor>::New();
renderWindowInteractor->SetRenderWindow(renderWindow);
renderWindow->Render();
// The callback will do the work
vtkSmartPointer<vtkIPWCallback> myCallback =
vtkSmartPointer<vtkIPWCallback>::New();
myCallback->SetPlane(plane);
vtkSmartPointer<vtkTextActor> textActor =
vtkSmartPointer<vtkTextActor>::New();
textActor->SetInput ( "Hello world" );
textActor->GetTextProperty()->SetColor ( 1.0,0.0,0.0 );
myCallback->SetTextActor(textActor);
renderer->AddActor2D ( textActor );
vtkSmartPointer<vtkImplicitPlaneRepresentation> rep =
vtkSmartPointer<vtkImplicitPlaneRepresentation>::New();
rep->SetPlaceFactor(1.25); // This must be set prior to placing the widget
rep->PlaceWidget(actor->GetBounds());
rep->SetNormal(plane->GetNormal());
rep->SetOrigin(0,0,50);
vtkSmartPointer<vtkImplicitPlaneWidget2> planeWidget =
vtkSmartPointer<vtkImplicitPlaneWidget2>::New();
planeWidget->SetInteractor(renderWindowInteractor);
planeWidget->SetRepresentation(rep);
planeWidget->AddObserver(vtkCommand::InteractionEvent,myCallback);
// render an image (lights and cameras are created automatically)
renderWindowInteractor->Initialize();
renderWindow->Render();
planeWidget->On();
// begin mouse interaction
renderWindowInteractor->Start();
return EXIT_SUCCESS;;
}
Thanks,
David
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.vtk.org/pipermail/vtkusers/attachments/20100312/73acde1e/attachment.htm>
More information about the vtkusers
mailing list