/*========================================================================= Program: wxMaracas Module: $RCSfile: CutModel2Manager.cxx,v $ Language: C++ Date: $Date: 2009/11/19 15:24:57 $ Version: $Revision: 1.1 $ 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; _copyimg = NULL; _interactor = NULL; _render = NULL; _currentaction=0; } CutModel2Manager::~CutModel2Manager(){ std::string files = _path; files+="/infounrd_0_fig_0.info"; remove(files.c_str()); } void CutModel2Manager::setImageData(vtkImageData* img){ _img = img; if(_copyimg!=NULL){ _copyimg->Delete(); } _copyimg = vtkImageData::New(); _copyimg->SetExtent(_img->GetExtent()); _copyimg->SetSpacing(_img->GetSpacing()); _copyimg->AllocateScalars(); _copyimg->DeepCopy(_img); } void CutModel2Manager::setInteractor(vtkRenderWindowInteractor* interactor){ _interactor = interactor; } void CutModel2Manager::setRenderer(vtkRenderer* renderer){ _render = renderer; } void CutModel2Manager::onAddCutModel2(int id, vtkCommand* observer) throw( CutModel2Exception){ checkInvariant(); CutModel2Data* data = new CutModel2Data(id,_interactor, observer, _copyimg); _vectordata.push_back(data); _render->AddActor(data->getActor()); //_render->UpdateCamera(); _render->Render(); } void CutModel2Manager::checkInvariant() throw( CutModel2Exception){ if(_img==NULL){ throw CutModel2Exception("The image is not set"); } if(_copyimg==NULL){ throw CutModel2Exception("The image is not set"); } if(_interactor==NULL){ throw CutModel2Exception("Interactor not set"); } if(_render==NULL){ throw CutModel2Exception("Render not set"); } } double* CutModel2Manager::getImageRange()throw( CutModel2Exception){ checkInvariant(); return _copyimg->GetScalarRange(); } void CutModel2Manager::changeOpacity(int id,int opacity)throw( CutModel2Exception){ checkInvariant(); CutModel2Data* current = getCutModel2Data(id); current->changeOpacity(opacity); } void CutModel2Manager::ShowViewBox(int id,bool check)throw( CutModel2Exception){ checkInvariant(); CutModel2Data* current = getCutModel2Data(id); current->ShowViewBox(check); } void CutModel2Manager::ChangeShape(int id,int selection)throw( CutModel2Exception){ checkInvariant(); CutModel2Data* current = getCutModel2Data(id); current->ChangeShape(selection); _render->Render(); } CutModel2Data* CutModel2Manager::getCutModel2Data(int id)throw( CutModel2Exception){ CutModel2Data* 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 CutModel2Exception("Data not found"); } return current; } void CutModel2Manager::updateActorDirection(int id)throw( CutModel2Exception){ checkInvariant(); CutModel2Data* current = getCutModel2Data(id); current->udapteActorDirection(); } void CutModel2Manager::changeColor(int id,double r,double g,double b)throw( CutModel2Exception){ checkInvariant(); CutModel2Data* current = getCutModel2Data(id); current->changeColor(r,g,b); _render->Render(); } void CutModel2Manager::RemoveActor(int id)throw( CutModel2Exception){ checkInvariant(); CutModel2Data* current = getCutModel2Data(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( CutModel2Exception){ checkInvariant(); CutModel2Data* current = getCutModel2Data(id); current->ExecuteCut(range, isinside, _img); /* Setting extra information for the undo */ CutModel2SaveBinInfo* undoaction = this->AddActionUndo(id, CUTMODEL2_CUT); undoaction->setRange(range); undoaction->setIsInside(isinside); } vtkImageData* CutModel2Manager::GetResultImage(){ checkInvariant(); return _copyimg; } void CutModel2Manager::RefreshActor(int id){ checkInvariant(); CutModel2Data* current = getCutModel2Data(id); _render->RemoveActor(current->getActor()); _render->AddActor(current->getActor()); current->RefreshViewBox(); _render->Render(); } void CutModel2Manager::SaveCutModel2Data(std::string filename)throw( CutModel2Exception){ throw CutModel2Exception("not implemented"); } void CutModel2Manager::LoadCutModel2Data(std::string filename)throw( CutModel2Exception){ throw CutModel2Exception("not implemented"); } CutModel2SaveBinInfo* CutModel2Manager::AddActionUndo(int idc, UNDOTYPE type)throw( CutModel2Exception){ for(int i = _undoredo.size()-1; i > _currentaction;i--){ delete _undoredo[i]; _undoredo.pop_back(); } CutModel2SaveBinInfo* cutmodel = new CutModel2SaveBinInfo(idc, _currentaction, type, _path); if(type == CUTMODEL2_CUT){ cutmodel->saveMatrix4x4(this->getCutModel2Data(idc)->getCurrentMatrix()->GetMatrix()); cutmodel->setCurrentShape(this->getCutModel2Data(idc)->getCurrentShape()); } _undoredo.push_back(cutmodel); _currentaction++;// = _undoredo.size(); //std::cout<<"current index "<<_currentaction; return cutmodel; } int CutModel2Manager::Undo() throw( CutModel2Exception){ //&& _currentaction < _undoredo.size() if(_currentaction > 0){ int tempaction = _currentaction-1; CutModel2SaveBinInfo* currentundo = _undoredo[tempaction]; CutModel2Data* currentmodel; if(currentundo->getActionType()==CUTMODEL2_CUT){ //Undo the cut vtkTransform* transform = currentundo->getTransformFromMatrixFile(); currentmodel = getCutModel2Data(currentundo->getId()); currentmodel->setTransform(transform, _img); currentmodel->setCurrentShape(currentundo->getCurrentShape()); currentmodel->ExecuteUnCut(currentundo->getIsInside(), _copyimg, _img); } //Every thing ok _currentaction--; return 0; } return -1; } int CutModel2Manager::Redo() throw( CutModel2Exception){ if(_currentaction >= 0 && _currentaction < _undoredo.size()){ CutModel2SaveBinInfo* currentundo = _undoredo[_currentaction]; CutModel2Data* currentmodel; if(currentundo->getActionType()==CUTMODEL2_CUT){ //Redo the cut vtkTransform* transform = currentundo->getTransformFromMatrixFile(); currentmodel = getCutModel2Data(currentundo->getId()); currentmodel->setTransform(transform, _copyimg); currentmodel->setCurrentShape(currentundo->getCurrentShape()); currentmodel->ExecuteCut(currentundo->getRange(), currentundo->getIsInside(), _copyimg); } _currentaction++; return 0; } return -1; }