#include "vtkLandmarkManager.h" #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include vtkCxxRevisionMacro(vtkLandmarkManager, "$Revision: 1.21 $"); vtkStandardNewMacro(vtkLandmarkManager); #ifdef WIN32 int rint(double a); #endif vtkLandmark::vtkLandmark() { this->SetName("Landmark#0"); this->SetScale (0.5); this->PolyData = vtkPolyData::New(); this->Property = vtkProperty::New(); this->Property->SetColor(1.0,1.0,0.0); this->Property->SetPointSize(2); this->Property->SetLineWidth(2); this->VoxelCoord[0] = 0; this->VoxelCoord[1] = 0; this->VoxelCoord[2] = 0; this->RealCoord[0] = 0.0; this->RealCoord[1] = 0.0; this->RealCoord[2] = 0.0; vtkPoints* points = vtkPoints::New(); double pt[3]; pt[0] = 0; pt[1] = 0; pt[2] = 0; points->InsertNextPoint(pt); pt[0] = -this->Scale; pt[1] = 0; pt[2] = 0; points->InsertNextPoint(pt); pt[0] = this->Scale; pt[1] = 0; pt[2] = 0; points->InsertNextPoint(pt); pt[0] = 0; pt[1] = -this->Scale; pt[2] = 0; points->InsertNextPoint(pt); pt[0] = 0; pt[1] = this->Scale; pt[2] = 0; points->InsertNextPoint(pt); pt[0] = 0; pt[1] = 0; pt[2] = -this->Scale; points->InsertNextPoint(pt); pt[0] = 0; pt[1] = 0; pt[2] = this->Scale; points->InsertNextPoint(pt); this->PolyData->SetPoints (points); points->Delete(); vtkCellArray* lines = vtkCellArray::New(); vtkIdType ids[2]; ids[0] = 1; ids[1] = 2; lines->InsertNextCell(2, ids); ids[0] = 3; ids[1] = 4; lines->InsertNextCell(2, ids); ids[0] = 5; ids[1] = 6; lines->InsertNextCell(2, ids); this->PolyData->SetLines (lines); lines->Delete(); } vtkLandmark::~vtkLandmark() { std::cout<<"deleting landmark "<GetName()<PolyData->Delete(); this->Property->Delete(); } void vtkLandmark::SetRealCoord (const double val[3]) { this->PolyData->GetPoints()->SetPoint (0, val); this->RealCoord[0] = val[0]; this->RealCoord[1] = val[1]; this->RealCoord[2] = val[2]; this->PositionHasChanged(); } void vtkLandmark::PositionHasChanged() { double pt[3]; double* origin = this->PolyData->GetPoints()->GetPoint (0); pt[0] = origin[0]-this->Scale; pt[1] = origin[1]; pt[2] = origin[2]; this->PolyData->GetPoints()->SetPoint (1, pt); pt[0] = origin[0]+this->Scale; pt[1] = origin[1]; pt[2] = origin[2]; this->PolyData->GetPoints()->SetPoint (2, pt); pt[0] = origin[0]; pt[1] = origin[1]-this->Scale; pt[2] = origin[2]; this->PolyData->GetPoints()->SetPoint (3, pt); pt[0] = origin[0]; pt[1] = origin[1]+this->Scale; pt[2] = origin[2]; this->PolyData->GetPoints()->SetPoint (4, pt); pt[0] = origin[0]; pt[1] = origin[1]; pt[2] = origin[2]-this->Scale; this->PolyData->GetPoints()->SetPoint (5, pt); pt[0] = origin[0]; pt[1] = origin[1]; pt[2] = origin[2]+this->Scale; this->PolyData->GetPoints()->SetPoint (6, pt); this->PolyData->Modified(); } void vtkLandmarkCallback::Execute ( vtkObject *caller, unsigned long event, void*) { if (event == vtkCommand::RightButtonPressEvent && this->Landmark) { vtkRenderWindowInteractor* interactor = vtkRenderWindowInteractor::SafeDownCast(caller); vtkViewImage* view = NULL; if (!interactor) return; for (unsigned int i=0; iViewList.size(); i++) if (this->ViewList[i] && (this->ViewList[i]->GetRenderWindowInteractor() == interactor)) view = this->ViewList[i]; if (!view) return; double* spacing = view->GetImage()->GetSpacing(); double* origin = view->GetImage()->GetOrigin(); double *imBounds = view->GetImage()->GetBounds(); double pos[3] = {0,0,0}; double newpos[3] = {0,0,0}; int voxelpos[3] = {0,0,0}; double x=0; double y=0; view->GetCurrentPoint(pos); // check if pos lies inside image bounds if( pos[0]imBounds[1] || pos[1]imBounds[3] || pos[2]imBounds[5]) { // we are outside image bounds return; } newpos[0] = double(rint ((pos[0]-origin[0])/spacing[0] ))*spacing[0]+origin[0]; newpos[1] = double(rint ((pos[1]-origin[1])/spacing[1] ))*spacing[1]+origin[1]; newpos[2] = double(rint ((pos[2]-origin[2])/spacing[2] ))*spacing[2]+origin[2]; view->GetVoxelCoordinates(newpos, voxelpos); this->Landmark->SetRealCoord (newpos); this->Landmark->SetVoxelCoord (voxelpos); view->SyncRender(); } } void vtkLandmarkCallback::AddViewImage (vtkViewImage* view) { if (!this->HasViewImage(view)) this->ViewList.push_back(view); } bool vtkLandmarkCallback::HasViewImage(vtkViewImage* view) { for (unsigned int i=0; iViewList.size(); i++) if (this->ViewList[i] == view) return true; return false; } vtkLandmarkManager::vtkLandmarkManager() { this->Callback = vtkLandmarkCallback::New(); this->SelectedProperty = vtkProperty::New(); this->Property = vtkProperty::New(); this->CurrentLandmark = NULL; this->SelectedProperty->SetPointSize(10); this->Property->SetPointSize(10); this->SelectedProperty->SetLineWidth(10); this->Property->SetLineWidth(10); this->SelectedProperty->SetColor(1.0,0.0,0.0); this->Property->SetColor(1.0,1.0,0.0); this->SelectedProperty->SetRepresentationToPoints(); this->Property->SetRepresentationToPoints(); } vtkLandmarkManager::~vtkLandmarkManager() { std::cout<<"deleting landmark manager"<RemoveAllLandmarks(); this->Property->Delete(); this->SelectedProperty->Delete(); // for (unsigned int i=0; iViewList.size(); i++) // { // vtkViewImage2D* view = vtkViewImage2D::SafeDownCast(this->ViewList[i]); // if (view && view->GetRenderWindowInteractor()) // view->GetRenderWindowInteractor()->RemoveObserver(this->Callback); // view->UnRegister (this); // } this->ViewList.clear(); this->Callback->Delete(); } void vtkLandmarkManager::Reset() { this->CurrentLandmark = NULL; this->RemoveAllLandmarks(); } unsigned int vtkLandmarkManager::AddLandmark(void) { vtkLandmark* landmark = vtkLandmark::New(); std::ostringstream os; os<<"Landmark#"<LandmarkList.size(); landmark->SetName(os.str().c_str()); if (this->ViewList.size()) landmark->SetRealCoord(this->ViewList[0]->GetCurrentPoint()); landmark->Register(this); this->LandmarkList.push_back (landmark); for (unsigned int i=0; iViewList.size(); i++) { vtkViewImage2D* view2D = vtkViewImage2D::SafeDownCast(this->ViewList[i]); vtkActor* actor = this->ViewList[i]->AddDataSet(landmark->GetPolyData(), landmark->GetProperty()); } this->SelectLandmark (landmark->GetName()); landmark->Delete(); return 0; } void vtkLandmarkManager::RemoveLandmark (const char* name) { this->RemoveLandmark(this->FindLandmark(name)); } void vtkLandmarkManager::RemoveAllLandmarks(void) { std::vector templist = this->LandmarkList; for (unsigned int i=0; iRemoveLandmark(templist[i]); } } void vtkLandmarkManager::SelectLandmark (vtkLandmark* landmark) { if (this->CurrentLandmark) this->CurrentLandmark->GetProperty()->DeepCopy(this->Property); landmark->GetProperty()->DeepCopy(this->SelectedProperty); this->CurrentLandmark = landmark; this->Callback->SetLandmark (landmark); for (unsigned int i=0; iViewList.size(); i++) { this->ViewList[i]->SetCurrentPoint (landmark->GetRealCoord()); this->ViewList[i]->Render(); } } void vtkLandmarkManager::SelectLandmark (const char* name) { vtkLandmark* landmark = this->FindLandmark(name); if (landmark) { this->SelectLandmark(landmark); } else { vtkWarningMacro(<<"There is no landmark named "<Callback->AddViewImage(view); if (!this->HasViewImage(view)) { //view->Register(this); this->ViewList.push_back(view); } } bool vtkLandmarkManager::HasViewImage(vtkViewImage* view) { for (unsigned int i=0; iViewList.size(); i++) if (this->ViewList[i] == view) return true; return false; } vtkLandmark* vtkLandmarkManager::FindLandmark(const char* name) const { for (unsigned int i=0; iLandmarkList.size(); i++) if ( strcmp (this->LandmarkList[i]->GetName(), name) == 0 ) return this->LandmarkList[i]; return NULL; } void vtkLandmarkManager::RemoveLandmark(vtkLandmark* landmark) { if (!landmark) return; if (this->CurrentLandmark == landmark) this->CurrentLandmark = NULL; for (unsigned int j=0; jViewList.size(); j++) this->ViewList[j]->RemoveDataSet (landmark->GetPolyData()); std::vector templist = this->LandmarkList; this->LandmarkList.clear(); unsigned int i=0; unsigned int l_id = 0; for (i=0; iLandmarkList.push_back(templist[i]); else l_id = i; } landmark->Delete(); this->Callback->SetLandmark (NULL); if (l_id > 0) this->SelectLandmark(this->GetLandmark ((unsigned int)0)); } void vtkLandmarkManager::ShowAll(void) { for (unsigned int j=0; jViewList.size(); j++) { for (unsigned int i=0; iLandmarkList.size(); i++) { vtkActor* actor = this->ViewList[j]->GetDataSetActor (this->LandmarkList[i]->GetPolyData()); if (actor) actor->SetVisibility (true); } } } void vtkLandmarkManager::HideAll(void) { for (unsigned int j=0; jViewList.size(); j++) { for (unsigned int i=0; iLandmarkList.size(); i++) { vtkActor* actor = this->ViewList[j]->GetDataSetActor (this->LandmarkList[i]->GetPolyData()); if (actor) actor->SetVisibility (false); } } } void vtkLandmarkManager::InteractionOn(void) { for (unsigned int i=0; iViewList.size(); i++) { vtkViewImage2D* view = vtkViewImage2D::SafeDownCast(this->ViewList[i]); if (view && view->GetRenderWindowInteractor()) if (!view->GetRenderWindowInteractor()->HasObserver(vtkCommand::RightButtonPressEvent, this->Callback)) view->GetRenderWindowInteractor()->AddObserver(vtkCommand::RightButtonPressEvent, this->Callback); } } void vtkLandmarkManager::InteractionOff(void) { for (unsigned int i=0; iViewList.size(); i++) { vtkViewImage2D* view = vtkViewImage2D::SafeDownCast(this->ViewList[i]); if (view && view->GetRenderWindowInteractor()) view->GetRenderWindowInteractor()->RemoveObserver(this->Callback); } }