#include "vtkView.h" #include "vtkObjectFactory.h" #include "vtkInteractorObserver.h" #include "vtkInteractorStyleSwitch.h" #include "vtkRenderer.h" #include "vtkRenderWindow.h" #include "vtkRenderWindowInteractor.h" #include "vtkRendererCollection.h" #include "vtkProp.h" #include "vtkTextActor.h" #include "vtkCoordinate.h" #include "vtkProperty.h" #include "vtkProperty2D.h" #include "vtkTextProperty.h" #include "vtkOrientationAnnotation.h" #include "assert.h" #include #ifdef WIN32 int rint(double a) { double test = fabs ( a - double(int(a)) ); int res = 0; if(a>0) (test>0.5)?(res=a+1):res=a; else (test>0.5)?(res=a-1):res=a; return res; } #endif // WIN32 vtkCxxRevisionMacro(vtkView, "$Revision: 1.8 $"); vtkStandardNewMacro(vtkView); vtkView::vtkView() { this->IsProcessed = false; this->InteractionOn = true; this->ShowAnnotations = true; this->Renderer = 0; this->RenderWindow = 0; this->RenderWindowInteractor = 0; // Initilize Annotations this->CornerAnnotation = vtkCornerAnnotation::New(); this->CornerAnnotation->SetNonlinearFontScaleFactor (0.3); this->TextProperty = vtkTextProperty::New(); this->CornerAnnotation->SetTextProperty ( this->TextProperty ); this->OrientationAnnotation = vtkOrientationAnnotation::New(); this->OrientationAnnotation->SetNonlinearFontScaleFactor (0.25); this->InteractorStyle = vtkInteractorStyleSwitch::New(); this->Parent = 0; } vtkView::~vtkView() { //this->RemoveAllViewToObserve(); //this->Uninitialize(); this->TextProperty->Delete(); this->CornerAnnotation->Delete(); this->OrientationAnnotation->Delete(); if( this->InteractorStyle ) this->InteractorStyle->Delete(); if( this->RenderWindow ) this->RenderWindow->Delete(); if( this->Renderer ) this->Renderer->Delete(); if( this->RenderWindowInteractor ) this->RenderWindowInteractor->Delete(); } //---------------------------------------------------------------------------- void vtkView::SetRenderWindow(vtkRenderWindow *arg) { if (!arg) { this->Uninitialize(); if (this->RenderWindow) { this->RenderWindow->UnRegister(this); } this->RenderWindow = NULL; return; } if (this->RenderWindow == arg) { return; } this->Uninitialize(); if (this->RenderWindow) { this->RenderWindow->UnRegister(this); } this->RenderWindow = arg; if (this->RenderWindow) { this->RenderWindow->Register(this); } if (this->RenderWindow->GetInteractor()) { this->SetInteractor (this->RenderWindow->GetInteractor()); } this->Initialize(); } //---------------------------------------------------------------------------- void vtkView::SetRenderer(vtkRenderer *arg) { if (!arg) { this->Uninitialize(); if (this->Renderer) { this->Renderer->UnRegister(this); } this->Renderer = NULL; return; } if (this->Renderer == arg) { return; } this->Uninitialize(); if (this->Renderer) { this->Renderer->UnRegister(this); } this->Renderer = arg; if (this->Renderer) { this->Renderer->Register(this); } this->Initialize(); } //---------------------------------------------------------------------------- void vtkView::SetInteractor(vtkRenderWindowInteractor *arg) { if (this->RenderWindowInteractor == arg) { return; } this->Uninitialize(); if (this->RenderWindowInteractor) { this->RenderWindowInteractor->UnRegister(this); } this->RenderWindowInteractor = arg; if (this->RenderWindowInteractor) { this->RenderWindowInteractor->Register(this); } this->Initialize(); } void vtkView::SetInteractorStyle(vtkInteractorStyle* style) { if (this->InteractorStyle == style) { return; } if (this->InteractorStyle) { this->InteractorStyle->UnRegister(this); } this->InteractorStyle = style; if (this->InteractorStyle) { this->InteractorStyle->Register(this); } this->Modified(); this->SetInteraction(); } //---------------------------------------------------------------------------- void vtkView::Initialize() { if (this->Renderer) { this->Renderer->SetBackground(0.9,0.9,0.9); } if (this->RenderWindow && this->Renderer) { this->RenderWindow->AddRenderer(this->Renderer); } this->AddActor ( this->CornerAnnotation ); this->AddActor ( this->OrientationAnnotation ); if (this->RenderWindowInteractor && this->InteractorStyle) { this->RenderWindowInteractor->SetInteractorStyle(this->InteractorStyle); this->RenderWindowInteractor->SetRenderWindow(this->RenderWindow); } this->SetInteraction(); } //---------------------------------------------------------------------------- void vtkView::Uninitialize() { if (this->Renderer) { this->Renderer->RemoveAllViewProps(); } if (this->RenderWindow && this->Renderer) { this->RenderWindow->RemoveRenderer(this->Renderer); } if (this->RenderWindowInteractor) { this->RenderWindowInteractor->SetInteractorStyle(NULL); this->RenderWindowInteractor->SetRenderWindow(NULL); } } std::vector < vtkView* > vtkView::GetViewToObserve(void) const { return this->GetChildren(); } void vtkView::AddActor (vtkProp* actor) { if (this->GetRenderer()) { this->GetRenderer()->AddActor(actor); } } void vtkView::RemoveActor (vtkProp* actor) { if (this->GetRenderer()) { this->GetRenderer()->RemoveActor(actor); } } void vtkView::SetInteractionOff() { this->InteractionOn = false; this->SetInteraction(); } void vtkView::SetInteractionOn() { this->InteractionOn = true; this->SetInteraction(); } void vtkView::SetInteraction() { if (this->RenderWindowInteractor) { if (!this->GetInteractionOn()) { this->GetRenderWindowInteractor()->SetInteractorStyle(NULL); } else { if( this->GetInteractorStyle() ) { this->GetRenderWindowInteractor()->SetInteractorStyle( this->InteractorStyle ); } } } } void vtkView::SetUpLeftAnnotation(const char *p_annotation) { this->upLeftAnnotation = p_annotation; this->UpdateAnnotations(); } const char * vtkView::GetUpLeftAnnotation() { return this->upLeftAnnotation.c_str(); } void vtkView::SetUpRightAnnotation(const char *p_annotation) { this->upRightAnnotation = p_annotation; this->UpdateAnnotations(); } const char * vtkView::GetUpRightAnnotation() { return this->upRightAnnotation.c_str(); } void vtkView::SetDownLeftAnnotation(const char *p_annotation) { this->downLeftAnnotation = p_annotation; this->UpdateAnnotations(); } const char * vtkView::GetDownLeftAnnotation() { return this->downLeftAnnotation.c_str(); } void vtkView::SetDownRightAnnotation(const char *p_annotation) { this->downRightAnnotation = p_annotation; this->UpdateAnnotations(); } const char * vtkView::GetDownRightAnnotation() { return this->downRightAnnotation.c_str(); } void vtkView::SetNorthAnnotation(const char *p_annotation) { this->northAnnotation = p_annotation; this->UpdateAnnotations(); } const char * vtkView::GetNorthAnnotation() { return this->northAnnotation.c_str(); } void vtkView::SetSouthAnnotation(const char *p_annotation) { this->southAnnotation = p_annotation; this->UpdateAnnotations(); } const char * vtkView::GetSouthAnnotation() { return this->southAnnotation.c_str(); } void vtkView::SetEastAnnotation(const char *p_annotation) { this->eastAnnotation = p_annotation; this->UpdateAnnotations(); } const char * vtkView::GetEastAnnotation() { return this->eastAnnotation.c_str(); } void vtkView::SetWestAnnotation(const char *p_annotation) { this->ouestAnnotation = p_annotation; this->UpdateAnnotations(); } const char * vtkView::GetWestAnnotation() { return this->ouestAnnotation.c_str(); } void vtkView::UpdateAnnotations() { if ( this->GetShowAnnotations() ) { this->CornerAnnotation->SetText (1, this->GetDownRightAnnotation() ); this->CornerAnnotation->SetText (2, this->GetUpLeftAnnotation() ); this->CornerAnnotation->SetText (3, this->GetUpRightAnnotation() ); } else { this->CornerAnnotation->SetText (1, "" ); this->CornerAnnotation->SetText (2, "" ); this->CornerAnnotation->SetText (3, "" ); } //always show about data... this->CornerAnnotation->SetText (0, this->GetDownLeftAnnotation() ); this->OrientationAnnotation->SetText (0, this->GetEastAnnotation()); this->OrientationAnnotation->SetText (1, this->GetNorthAnnotation()); this->OrientationAnnotation->SetText (2, this->GetWestAnnotation()); this->OrientationAnnotation->SetText (3, this->GetSouthAnnotation()); } void vtkView::SetBackgroundColor(double r, double g, double b) { if (this->Renderer) { this->Renderer->SetBackground(r,g,b); } } // void // vtkView::Display (bool a) // { // if( !this->GetIsProcessed() ) // { // this->Show (a); // this->Lock(); // for( unsigned int i=0; iViewToObserve.size(); i++) // this->ViewToObserve[i]->Display (a); // this->UnLock(); // } // } // void // vtkView::DisplayInFullPage(bool a) // { // if( !this->GetIsProcessed() ) // { // this->SetIsFullScreen ( a ); // this->Show (true); // this->Lock(); // for( unsigned int i=0; iViewToObserve.size(); i++) // this->ViewToObserve[i]->Display ( !a ); // this->UnLock(); // } // // force the call to the Layout() method: if the Parent // // is set, then I get its sizer and call Layout(). Implies // // that all linked vtkView have the same paremt, that may // // not be true all the time. // if( this->GetParent()!=NULL ) // { // if( this->GetParent()->GetSizer() != NULL ) // this->GetParent()->GetSizer()->Layout(); // } // } bool vtkView::HasChild (vtkView* view) const { if( !view ) { return false; } for( unsigned int i=0; iChildren.size(); i++) { if ( this->Children[i]==view ) { return true; } } return false; } bool vtkView::HasView (vtkView* view) const { // @deprecated HasView is deprecated, use HasChild instead. return this->HasChild (view); } void vtkView::AddViewToObserve (vtkView* view) { // @deprecated AddViewToObserve is deprecated, use AddChild instead. this->AddChild (view); /* if( this->HasView(view) ) { return; } if( view ) { view->Register(this); this->ViewToObserve.push_back (view); }*/ } void vtkView::RemoveViewToObserve(vtkView* view) { // @deprecated RemoveViewToObserve is deprecated, use RemoveChild instead. this->RemoveChild (view); /* if( !view ) { return; } vtkstd::vector< vtkView* > newViewsToObserve; for( unsigned int i=0; iViewToObserve.size(); i++) { if( this->ViewToObserve[i]==view ) { this->ViewToObserve[i]->UnRegister (this); } else { newViewsToObserve.push_back(this->ViewToObserve[i]); } } this->ViewToObserve = newViewsToObserve; */ } void vtkView::RemoveAllViewToObserve() { // @deprecated RemoveAllViewToObserve is deprecated, use Detach instead. this->Detach(); /* for( unsigned int i=0; iViewToObserve.size(); i++) { if( this->ViewToObserve[i] ) { this->ViewToObserve[i]->UnRegister (this); } } this->ViewToObserve.clear(); */ } void vtkView::ResetCamera() { if (this->Renderer) { this->Renderer->ResetCamera(); } } void vtkView::Render() { if (this->RenderWindow) { this->RenderWindow->Render(); } } void vtkView::SyncRender() { if( this->IsLocked() ) { return; } this->Render (); // this boolean is used so that the other observe won't call // Render() again and again and again... this->Lock(); for( unsigned int i=0; iChildren.size(); i++) { vtkView* view = reinterpret_cast ( this->Children[i] ); if( view ) { view->SyncRender (); } } this->UnLock(); } void vtkView::PrintSelf(ostream& os, vtkIndent indent) { this->Superclass::PrintSelf(os, indent); os << indent << "Interaction-State = " << this->GetInteractionOn() << "\n"; os << indent << "Render Window = " << this->RenderWindow << "\n"; if (this->GetRenderWindow()) this->GetRenderWindow()->PrintSelf (os, indent); } void vtkView::DrawOn() { if (this->Renderer) { // this->Renderer->DrawOn(); } } void vtkView::DrawOff() { if (this->Renderer) { // this->Renderer->DrawOff(); } } void vtkView::SetParent (vtkView* parent) { this->Parent = parent; } vtkView* vtkView::GetParent() const { return this->Parent; } void vtkView::AddChild (vtkView* child) { if( this->HasChild(child) || child==this ) { return; } if( child ) { /** We store temporary the child's parent */ vtkView* parent = child->GetParent(); child->Register (this); child->SetParent (this); this->Children.push_back (child); /** Now that the child has changed its parent, we remove the child from its previous parent's children list. If we have done that earlier, this could result in a call to Delete(), since the RemoveChild function unregsiter the object. */ if( parent ) { parent->RemoveChild (child); } } } void vtkView::AddChildren (std::vector children) { for( unsigned int i=0; iAddChild (children[i]); } } void vtkView::RemoveChild (vtkView* view) { if( !view || !this->Children.size() ) { return; } std::vector::iterator it = find (this->Children.begin(), this->Children.end(), view); if( it==this->Children.end() ) // view is not in the list { return; } (*it)->UnRegister (this); this->Children.erase (it); } void vtkView::RemoveAllChildren() { for( unsigned int i=0; iChildren.size(); i++) { this->Children[i]->UnRegister (this); } this->Children.clear(); } void vtkView::Detach (void) { if( this->GetParent() ) { this->GetParent()->AddChildren ( this->Children ); this->GetParent()->RemoveChild (this); this->Parent = 0; } this->RemoveAllChildren(); // surely not useful because the call to AddChildren already removed // the children of this view } void vtkView::Lock() { this->SetIsProcessed (true); } void vtkView::UnLock() { this->SetIsProcessed (false); }