/*========================================================================= Program: wxMaracas Module: $RCSfile: CutModel2Manager.cxx,v $ Language: C++ Date: $Date: 2009/12/08 13:42:39 $ Version: $Revision: 1.4 $ Copyright: (c) 2002, 2003 License: This software is distributed WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the above copyright notice for more information. =========================================================================*/ #include "CutModel2Manager.h" /** ** Start of the manager class **/ CutModel2Manager::CutModel2Manager(std::string path){ _path = path; _img = NULL; _img2 = NULL; _interactor = NULL; _render = NULL; _currentaction=0; } CutModel2Manager::~CutModel2Manager(){ std::string files = _path; files+="/infounrd_0_fig_0.info"; remove(files.c_str()); _vectordata.clear(); _img = NULL; _img2 = NULL; _interactor = NULL; _render = NULL; } void CutModel2Manager::setImageData(vtkImageData* img){ _img = img; if(_img2!=NULL){ _img2->Delete(); } _img2 = vtkImageData::New(); _img2->SetExtent(_img->GetExtent()); _img2->SetSpacing(_img->GetSpacing()); _img2->AllocateScalars(); _img2->DeepCopy(_img); } void CutModel2Manager::setInteractor(vtkRenderWindowInteractor* interactor){ _interactor = interactor; } void CutModel2Manager::setRenderer(vtkRenderer* renderer){ _render = renderer; } void CutModel2Manager::onAddCutModel2(int id, vtkCommand* observer) throw( CutModelException){ checkInvariant(); CutModelData* data = new CutModelData(id,_interactor, observer, _img2); _vectordata.push_back(data); _render->AddActor(data->getActor()); //_render->UpdateCamera(); _render->Render(); } void CutModel2Manager::checkInvariant() throw( CutModelException){ if(_img==NULL){ throw CutModelException("The image is not set"); } if(_img2==NULL){ throw CutModelException("The copyimage is not set"); } if(_interactor==NULL){ throw CutModelException("Interactor not set"); } if(_render==NULL){ throw CutModelException("Render not set"); } } double* CutModel2Manager::getImageRange()throw( CutModelException){ checkInvariant(); return _img2->GetScalarRange(); } void CutModel2Manager::changeOpacity(int id,int opacity)throw( CutModelException){ checkInvariant(); CutModelData* current = getCutModelData(id); current->changeOpacity(opacity); } void CutModel2Manager::ShowViewBox(int id,bool check)throw( CutModelException){ checkInvariant(); CutModelData* current = getCutModelData(id); current->ShowViewBox(check); } void CutModel2Manager::ChangeShape(int id,int selection)throw( CutModelException){ checkInvariant(); CutModelData* current = getCutModelData(id); current->ChangeShape(selection); _render->Render(); } CutModelData* CutModel2Manager::getCutModelData(int id)throw( CutModelException){ CutModelData* current = NULL; for(int i= 0; i < _vectordata.size();i++){ std::cout<<"id in CutModel2Manager:: "<getId()<getId()==id){ current = _vectordata[i]; } } if(current ==NULL){ throw CutModelException("Data not found"); } return current; } void CutModel2Manager::updateActorDirection(int id)throw( CutModelException){ checkInvariant(); CutModelData* current = getCutModelData(id); current->udapteActorDirection(); } void CutModel2Manager::changeColor(int id,double r,double g,double b)throw( CutModelException){ checkInvariant(); CutModelData* current = getCutModelData(id); current->changeColor(r,g,b); _render->Render(); } void CutModel2Manager::RemoveActor(int id)throw( CutModelException){ checkInvariant(); CutModelData* current = getCutModelData(id); for(int i = 0; i < _vectordata.size()-1;i++){ if(_vectordata[i]->getId()==id){ for(int j = i; j < _vectordata.size()-1;j++){ _vectordata[j]=_vectordata[j+1]; } i = _vectordata.size(); } } _render->RemoveActor(current->getActor()); delete current; _vectordata.pop_back(); _render->Render(); } void CutModel2Manager::ExecuteCut(int id, double* range, bool isinside)throw( CutModelException){ checkInvariant(); CutModelData* current = getCutModelData(id); current->ExecuteCut(range, isinside, _img); /* Setting extra information for the undo */ CutModelSaveBinInfo* undoaction = this->AddActionUndo(id, CUTMODEL_CUT); undoaction->setRange(range); undoaction->setIsInside(isinside); } vtkImageData* CutModel2Manager::GetResultImage(){ checkInvariant(); return _img2; } void CutModel2Manager::RefreshActor(int id){ checkInvariant(); CutModelData* current = getCutModelData(id); _render->RemoveActor(current->getActor()); _render->AddActor(current->getActor()); current->RefreshViewBox(); _render->Render(); } void CutModel2Manager::SaveCutModelData(std::string filename)throw( CutModelException){ throw CutModelException("not implemented"); } void CutModel2Manager::LoadCutModelData(std::string filename)throw( CutModelException){ throw CutModelException("not implemented"); } CutModelSaveBinInfo* CutModel2Manager::AddActionUndo(int idc, UNDOTYPE type)throw( CutModelException){ for(int i = _undoredo.size()-1; i > _currentaction;i--){ delete _undoredo[i]; _undoredo.pop_back(); } CutModelSaveBinInfo* cutmodel = new CutModelSaveBinInfo(idc, _currentaction, type, _path); if(type == CUTMODEL_CUT){ cutmodel->saveMatrix4x4(this->getCutModelData(idc)->getCurrentMatrix()->GetMatrix()); cutmodel->setCurrentShape(this->getCutModelData(idc)->getCurrentShape()); } _undoredo.push_back(cutmodel); _currentaction++;// = _undoredo.size(); //std::cout<<"current index "<<_currentaction; return cutmodel; } int CutModel2Manager::Undo() throw( CutModelException){ //&& _currentaction < _undoredo.size() if(_currentaction > 0){ int tempaction = _currentaction-1; CutModelSaveBinInfo* currentundo = _undoredo[tempaction]; CutModelData* currentmodel; if(currentundo->getActionType()==CUTMODEL_CUT){ //Undo the cut vtkTransform* transform = currentundo->getTransformFromMatrixFile(); currentmodel = getCutModelData(currentundo->getId()); currentmodel->setTransform(transform, _img); currentmodel->setCurrentShape(currentundo->getCurrentShape()); currentmodel->ExecuteUnCut(currentundo->getIsInside(), _img2, _img); } //Every thing ok _currentaction--; return 0; } return -1; } int CutModel2Manager::Redo() throw( CutModelException){ if(_currentaction >= 0 && _currentaction < _undoredo.size()){ CutModelSaveBinInfo* currentundo = _undoredo[_currentaction]; CutModelData* currentmodel; if(currentundo->getActionType()==CUTMODEL_CUT){ //Redo the cut vtkTransform* transform = currentundo->getTransformFromMatrixFile(); currentmodel = getCutModelData(currentundo->getId()); currentmodel->setTransform(transform, _img2); currentmodel->setCurrentShape(currentundo->getCurrentShape()); currentmodel->ExecuteCut(currentundo->getRange(), currentundo->getIsInside(), _img2); } _currentaction++; return 0; } return -1; }