[vtkusers] Altering a rendered actor

Kerry Loux louxkr at gmail.com
Thu May 22 16:57:02 EDT 2008


That did the trick!!  It now works every time no matter how far I move the
object or zoom, etc.

Thanks Joe!!

-Kerry

On Thu, May 22, 2008 at 3:08 PM, Johannes Heitz <j.heitz at mediri.com> wrote:

> I am not saying it might be a solution... Been there, done that ;(
>
> double bounds[6];
> pRenderer->ComputeVisiblePropBounds(bounds);
> pRenderer->ResetCameraClippingRange(bounds);
>
> Would in theory get the extent of the currently visible "actors" and
> (re)set the clipping range accordingly.
>
> J.
>
>
> // Quick (and unclean) test app to find out how and when to get rendered
> actors to update
> // Top of the UI -> wxVTK Window with 8 cone actors
> // Bottom of UI -> wxTextCtrl to type commands into
> // Commands are
> //              either a number from 0 to 7 followed by an action
> //              Actions are "hide", "show", "color", "hop" and the numbers
> make the cone # x perform that action
> // or "renderon", "renderoff", "on" or "off", (all show that I was wrong
> ;P),
> // "boundson", "boundsoff" (use the set clipping range or not)
> // "allcolor", "allhop" (color, move all actors at once) or
> // clip NEAR FAR (setting the current cameras clipping range)
>
> // What this might show is that...
>
> //      vtkRenderer->ComputeVisiblePropBounds(bounds);
> //      vtkRenderer->ResetCameraClippingRange(bounds);
>
> // might 'render' all actors no matter what the clipping range is :|
>
> // Commands are 'sent' by hitting return
>
>
> // For compilers that support precompilation, includes "wx/wx.h".
> #include "wx/wxprec.h"
>
> #ifdef __BORLANDC__
>    #pragma hdrstop
> #endif
>
> // for all others, include the necessary headers (this file is usually all
> you
> // need because it includes almost all "standard" wxWindows headers)
> #ifndef WX_PRECOMP
>    #include "wx/wx.h"
> #endif
>
> #include "wxVTKRenderWindowInteractor.h"
> #include "vtkCamera.h"
> #include "vtkRenderer.h"
> #include "vtkRenderWindow.h"
> #include "vtkConeSource.h"
> #include "vtkPolyDataMapper.h"
> #include "vtkActor.h"
> #include "vtkPolyDataReader.h"
> #include "vtkProperty.h"
>
> #include "vtkPNGReader.h"
> #include "vtkImageMapper.h"
> #include "vtkImageShiftScale.h"
> #include "vtkInteractorStyleImage.h"
> #include "vtkActor2D.h"
>
> #include "vtkImageViewer2.h"
> #include "vtkImageData.h"
> #include "vtkTesting.h"
> #include "vtkTestUtilities.h"
>
>
> // the application icon
> #ifndef __WXMSW__
>    #include "mondrian.xpm"
> #endif
>
> class MyApp;
> class MyFrame;
>
> // Define a new application type, each program should derive a class from
> wxApp
> class MyApp : public wxApp
> {
> public:
>    // this one is called on application startup and is a good place for the
> app
>    // initialization (doing it here and not in the ctor allows to have an
> error
>    // return: if OnInit() returns false, the application terminates)
>    virtual bool OnInit();
> };
>
> // Define a new frame type: this is going to be our main frame
> class MyFrame : public wxFrame
> {
> public:
>    // ctor(s)
>    MyFrame(const wxString& title, const wxPoint& pos, const wxSize& size);
>    ~MyFrame();
>
>    // event handlers (these functions should _not_ be virtual)
>    void OnQuit(wxCommandEvent& event);
>    void OnAbout(wxCommandEvent& event);
>        void OnTextInput(wxCommandEvent& event);
>        void doCommand(wxString strCommand);
> protected:
>    void Setup();
>    void Cleanup();
>
> private:
>  wxVTKRenderWindowInteractor*  m_pVTKWindow;
>  vtkRenderer*                                  pRenderer;
>  vtkRenderWindow*                              pRenderWindow;
>  vtkPolyDataMapper*                    pConeMapper[8];
>  vtkActor*                                             pConeActor[8];
>  vtkConeSource*                                pConeSource[8];
>  wxTextCtrl*                                   m_pTextCtrl;
>  wxPanel*                                              panel;
>
>  bool                                                  m_autorender;
>  bool                                                  m_maxbounds;
>
> private:
>    // any class wishing to process wxWindows events must use this macro
>    DECLARE_EVENT_TABLE()
> };
>
> // IDs for the controls and the menu commands
> enum
> {
>    // menu items
>    Minimal_Quit = 1,
>    Minimal_About
> };
>
> #define MY_FRAME                101
> #define MY_VTK_WINDOW   102
> #define ID_TEXT                 103
>
> // the event tables connect the wxWindows events with the functions (event
> // handlers) which process them. It can be also done at run-time, but for
> the
> // simple menu events like this the static method is much simpler.
> BEGIN_EVENT_TABLE(MyFrame, wxFrame)
>    EVT_MENU(Minimal_Quit,  MyFrame::OnQuit)
>    EVT_MENU(Minimal_About, MyFrame::OnAbout)
>        EVT_TEXT_ENTER(ID_TEXT, MyFrame::OnTextInput)
> END_EVENT_TABLE()
>
> // Create a new application object: this macro will allow wxWindows to
> create
> // the application object during program execution (it's better than using
> a
> // static object for many reasons) and also declares the accessor function
> // wxGetApp() which will return the reference of the right type (i.e. MyApp
> and
> // not wxApp)
> IMPLEMENT_APP(MyApp)
>
> // 'Main program' equivalent: the program execution "starts" here
> bool MyApp::OnInit()
> {
>
>    MyFrame *frame = new MyFrame(_T("wxWindows-VTK App"),
>                                 wxPoint(50, 50), wxSize(450, 340));
>
>        frame->Show(TRUE);
>
>    return TRUE;
> }
>
> // frame constructor
> MyFrame::MyFrame(const wxString& title, const wxPoint& pos, const wxSize&
> size)
>       : wxFrame((wxFrame *)NULL, -1, title, pos, size)
> {
> #ifdef __WXMAC__
>    // we need this in order to allow the about menu relocation, since ABOUT
> is
>    // not the default id of the about menu
>    wxApp::s_macAboutMenuItemId = Minimal_About;
> #endif
>
>    SetIcon(wxICON(mondrian));
>
>    wxMenu *menuFile = new wxMenu(_T(""), wxMENU_TEAROFF);
>    wxMenu *helpMenu = new wxMenu;
>
>        helpMenu->Append(Minimal_About, _T("&About...\tCtrl-A"), _T("Show
> about dialog"));
>    menuFile->Append(Minimal_Quit, _T("E&xit\tAlt-X"), _T("Quit this
> program"));
>
>    wxMenuBar *menuBar = new wxMenuBar();
>    menuBar->Append(menuFile, _T("&File"));
>    menuBar->Append(helpMenu, _T("&Help"));
>
>    SetMenuBar(menuBar);
>
>
>
> #if wxUSE_STATUSBAR
>    // create a status bar just for fun (by default with 1 pane only)
>    CreateStatusBar(2);
>    SetStatusText(_T("Zoom out to see cones"));
> #endif // wxUSE_STATUSBAR
>
>        panel = new wxPanel(this, -1);
>    m_pVTKWindow = new wxVTKRenderWindowInteractor(panel, MY_VTK_WINDOW);
>    m_pVTKWindow->UseCaptureMouseOn();
>        m_pTextCtrl = new wxTextCtrl(panel, ID_TEXT, "", wxDefaultPosition,
> wxDefaultSize,wxTE_PROCESS_ENTER/*|wxTE_MULTILINE*/);
>        wxBoxSizer *vbox = new wxBoxSizer(wxVERTICAL);
>        vbox->Add(m_pVTKWindow, 2,wxEXPAND);
>        vbox->Add(m_pTextCtrl, 1,wxEXPAND);
>        panel->SetSizer(vbox);
>        m_maxbounds = false;
>        m_autorender = false;
>    Setup();
> }
>
> MyFrame::~MyFrame()
> {
>   Cleanup();
> }
>
>
> void MyFrame::Setup()
> {
>  pRenderer     = vtkRenderer::New();
>  pRenderWindow = m_pVTKWindow->GetRenderWindow();
>  pRenderWindow->AddRenderer(pRenderer);
>
>  for(int iLoop = 0; iLoop < 8; iLoop++)
>  {
>        pConeMapper[iLoop]   = vtkPolyDataMapper::New();
>        pConeActor[iLoop]    = vtkActor::New();
>        pConeSource[iLoop]   = vtkConeSource::New();
>        pConeSource[iLoop]->SetAngle(60-(5*iLoop));
>        pConeSource[iLoop]->SetResolution(2*(iLoop+1));
>        pConeMapper[iLoop]->SetInput(pConeSource[iLoop]->GetOutput());
>        pConeActor[iLoop]->SetMapper(pConeMapper[iLoop]);
>
>  pConeActor[iLoop]->SetPosition(iLoop*(rand()%2==1?-1:1),iLoop*(rand()%2==1?-1:1),iLoop*(rand()%2==1?-1:1));
>        pRenderer->AddActor(pConeActor[iLoop]);
>  }
>
>  pRenderer->SetBackground(0.4,0.4,0.4);
>  pRenderer->GetActiveCamera()->Zoom(1.0);
>  pRenderer->GetActiveCamera()->SetClippingRange(1,1000);
> }
>
> void MyFrame::Cleanup()
> {
>        // No check for != 0 and no try/catch
>        for(int iLoop = 0; iLoop < 8; iLoop++)
>        {
>                pConeMapper[iLoop]->Delete();
>                pConeActor[iLoop]->Delete();
>                pConeSource[iLoop]->Delete();
>        }
>        pRenderer->Delete();
>        m_pVTKWindow->Delete();
> }
>
>
> void MyFrame::OnQuit(wxCommandEvent& WXUNUSED(event))
> {
>    Close(TRUE);
> }
>
> void MyFrame::OnAbout(wxCommandEvent& WXUNUSED(event))
> {
>    wxString msg;
>    msg.Printf( _T("This is the about dialog of wx-vtk sample.\n"));
>    wxMessageBox(msg, _T("About wx-vtk"), wxOK | wxICON_INFORMATION, this);
> }
>
> void MyFrame::OnTextInput(wxCommandEvent& event)
> {
>        if (event.GetEventType() == wxEVT_COMMAND_TEXT_ENTER)
>        {
>                doCommand(m_pTextCtrl->GetValue());
>                m_pTextCtrl->Clear();
>        }
>        event.Skip();
> }
>
> void MyFrame::doCommand(wxString strCommand)
> {
>        // This is rather weak but no time for parsing proper...
>        int iPos = strCommand.Find(" ");
>        if(-1 != iPos)
>        {
>                wxString strOpCode = strCommand.Left(iPos);
>                strOpCode.Trim();
>                wxString strRemains =
> strCommand.Right(strCommand.Length()-iPos);
>                strRemains.Trim();
>
>                if(!strOpCode.IsEmpty())
>                {
>                        strOpCode.MakeLower();
>                        if(strOpCode.IsNumber())
>                        {
>
>                                double dVal;
>                strOpCode.ToDouble(&dVal);
>                                if(dVal>7) return;
>                                strRemains.MakeLower();
>                                strRemains.Trim(false);
>                if(0 == strRemains.CompareTo("hide"))
>
>  pRenderer->RemoveActor(pConeActor[(int)dVal]);
>                                if(0 == strRemains.CompareTo("show"))
>
>  pRenderer->AddActor(pConeActor[(int)dVal]);
>                                if(0 == strRemains.CompareTo("color"))
>
>  pConeActor[(int)dVal]->GetProperty()->SetColor((float)(rand()%10)/10,(float)(rand()%10)/10,(float)(rand()%10)/10);
>                                if(0 == strRemains.CompareTo("hop"))
>
>  pConeActor[(int)dVal]->SetPosition(rand()%10*(rand()%2==1?-1:1),rand()%10*(rand()%2==1?-1:1),rand()%10*(rand()%2==1?-1:1));
>                        }
>                        else
>                        {
>                                if(0 == strOpCode.CompareTo("clip"))
>                                {
>                                        wxString strNear;
>                                        wxString strFar;
>                                        wxString strRemains =
> strCommand.Right(strCommand.Length()-iPos);
>                                        strRemains.Trim(true);
>                                        strRemains.Trim(false);
>                                        int iPos = strRemains.Find(" ");
>                                        if(-1 != iPos)
>                                        {
>                                                strNear =
> strRemains.Left(iPos);
>                                                strFar  =
> strRemains.Right(strRemains.Length()-iPos);
>
>                                                strNear.Trim(true);
>                                                strNear.Trim(false);
>                                                strFar.Trim(false);
>                                                strFar.Trim(true);
>                                                if(!strNear.IsEmpty() &&
> !strFar.IsEmpty())
>                                                {
>                                                        double nearVal;
>                                                        double farVal;
>
>  strNear.ToDouble(&nearVal);
>
>  strFar.ToDouble(&farVal);
>
>  pRenderer->GetActiveCamera()->SetClippingRange(nearVal, farVal);
>                                                }
>                                        }
>                                }
>                        }
>                }
>        }
>        else
>        {
>                wxString strOpCode = strCommand.Left(iPos);
>                strOpCode.Trim();
>                strOpCode.MakeLower();
>
>                if(0 == strOpCode.CompareTo("allcolor"))
>                {
>                        for(int iLoop = 0; iLoop < 8; iLoop++)
>                        {
>
>  pConeActor[iLoop]->GetProperty()->SetColor((float)(rand()%10)/10,(float)(rand()%10)/10,(float)(rand()%10)/10);
>                        }
>                }
>
>                if(0 == strOpCode.CompareTo("allhop"))
>                {
>                        for(int iLoop = 0; iLoop < 8; iLoop++)
>                        {
>
>  pConeActor[iLoop]->SetPosition(rand()%10*(rand()%2==1?-1:1),rand()%10*(rand()%2==1?-1:1),rand()%10*(rand()%2==1?-1:1));
>                        }
>                }
>
>                if(0 == strOpCode.CompareTo("on"))
>                {
>                        m_pVTKWindow->SetRenderWhenDisabled(true);
>                        SetStatusText("RenderWhenDisabled = true");
>                }
>                if(0 == strOpCode.CompareTo("off"))
>                {
>                        m_pVTKWindow->SetRenderWhenDisabled(false);
>                        SetStatusText("RenderWhenDisabled = false");
>                }
>                if(0 == strOpCode.CompareTo("renderon"))
>                {
>                        m_autorender = true;
>                        SetStatusText("AutoRender = on");
>                }
>                if(0 == strOpCode.CompareTo("renderoff"))
>                {
>                        m_autorender = false;
>                        SetStatusText("AutoRender = off");
>                }
>                if(0 == strOpCode.CompareTo("boundsoff"))
>                {
>                        m_maxbounds = false;
>                        SetStatusText("Honor clipping range: use
> SetClippingRange");
>                }
>                if(0 == strOpCode.CompareTo("boundson"))
>                {
>                        m_maxbounds = true;
>                        SetStatusText("Ignore clipping range: use
> ComputeVisiblePropBounds");
>                }
>        }
>
>        double bounds[6];
>
>        /* DBG
>        double pos[3];
>        char tmp[1000];
>
>        for(int iLoop = 0; iLoop < 8; iLoop++)
>        {
>                pConeActor[iLoop]->GetBounds(bounds);
>                pConeActor[iLoop]->GetPosition(pos);
>                sprintf(tmp,"\r\nActor %i is  at %f,%f,%f",iLoop,
> pos[0],pos[1],pos[2]);
>                m_pTextCtrl->AppendText(tmp);
>                sprintf(tmp,"\r\nActor %i bounds %f,%f,%f",iLoop,
> bounds[0],bounds[1],bounds[2],bounds[3],bounds[4],bounds[5]);
>                m_pTextCtrl->AppendText(tmp);
>        }
>        */
>
>        if(m_maxbounds)
>        {
>                pRenderer->ComputeVisiblePropBounds(bounds);
>                pRenderer->ResetCameraClippingRange(bounds);
>        }
>
>        if(m_autorender)
>        {
>                m_pVTKWindow->GetRenderWindow()->Render();
>        }
>
> }
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.vtk.org/pipermail/vtkusers/attachments/20080522/b86f7c2e/attachment-0002.htm>


More information about the vtkusers mailing list