CutModelManager.cxx

Go to the documentation of this file.
00001 /*=========================================================================
00002 
00003 Program:   wxMaracas
00004 Module:    $RCSfile: CutModelManager.cxx,v $
00005 Language:  C++
00006 Date:      $Date: 2010/09/29 21:21:05 $
00007 Version:   $Revision: 1.10 $
00008 
00009 Copyright: (c) 2002, 2003
00010 License:
00011 
00012 This software is distributed WITHOUT ANY WARRANTY; without even
00013 the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
00014 PURPOSE.  See the above copyright notice for more information.
00015 
00016 =========================================================================*/
00017 #include "CutModelManager.h"
00018 
00019 //Machete
00020 #include "CutModelMainPanel.h"
00021 
00025 CutModelManager::CutModelManager(std::string path){
00026         _path = path;
00027         _img = NULL;
00028         _img2 = NULL;
00029         _interactor = NULL;     
00030         _render = NULL;
00031         _currentaction=0;
00032 
00033 }
00034 CutModelManager::~CutModelManager(){
00035         std::string files = _path;
00036         files+="/infounrd_0_fig_0.info";        
00037         remove(files.c_str());  
00038         _vectordata.clear();
00039         _img = NULL;
00040         _img2 = NULL;
00041         _interactor = NULL;     
00042         _render = NULL;
00043 }
00044 
00045 
00046 vtkImageData* CutModelManager::getImageData(){
00047         return _img2;
00048 }
00049 
00050 void CutModelManager::setImageData(vtkImageData* img){
00051         int type = CutModelMainPanel::getInstance()->GetType();
00052         if( type == 0)
00053         {
00054                 _img = img;
00055 
00056                 if(_img2!=NULL){
00057                         _img2->Delete();
00058                 }
00059                 _img2 = vtkImageData::New();
00060                 _img2->SetExtent(_img->GetExtent());
00061                 _img2->SetSpacing(_img->GetSpacing());
00062                 _img2->AllocateScalars();
00063 
00064                 _img2->DeepCopy(_img);
00065         }
00066         else
00067         {
00068                 _img2 = img;
00069 
00070                 if(_img!=NULL){
00071                         _img->Delete();
00072                 }
00073                 _img = vtkImageData::New();
00074                 _img->SetExtent(_img2->GetExtent());
00075                 _img->SetSpacing(_img2->GetSpacing());
00076                 _img->AllocateScalars();
00077 
00078                 _img->DeepCopy(_img2);
00079         }
00080 }
00081 
00082 void CutModelManager::setInteractor(vtkRenderWindowInteractor* interactor){
00083         _interactor = interactor;
00084 }
00085 
00086 void CutModelManager::setRenderer(vtkRenderer* renderer){
00087         _render = renderer;
00088 }
00089 
00090 void CutModelManager::onAddCutModel(int id, vtkCommand* observer) throw( CutModelException){
00091         checkInvariant();
00092 
00093 
00094         CutModelData* data = new CutModelData(id,_interactor, observer, _img);
00095         _vectordata.push_back(data);
00096 
00097         _render->AddActor(data->getActor());
00098 
00099         //_render->UpdateCamera();
00100         _render->Render();
00101 }
00102 
00103 void CutModelManager::checkInvariant() throw( CutModelException){
00104         if(_img==NULL){
00105                 throw CutModelException("The image is not set");
00106         }
00107         if(_img2==NULL){
00108                 throw CutModelException("The image is not set");
00109         }
00110         if(_interactor==NULL){
00111                 throw CutModelException("Interactor not set");
00112         }
00113         if(_render==NULL){
00114                 throw CutModelException("Render not set");
00115         }
00116 }
00117 
00118 double* CutModelManager::getImageRange()throw( CutModelException){
00119         checkInvariant();
00120         return _img->GetScalarRange();
00121 }
00122 
00123 void CutModelManager::changeOpacity(int id,int opacity)throw( CutModelException){
00124         checkInvariant();
00125         CutModelData* current = getCutModelData(id);
00126         current->changeOpacity(opacity);
00127 }
00128 
00129 void CutModelManager::ShowViewBox(int id,bool check)throw( CutModelException){
00130         checkInvariant();
00131         CutModelData* current = getCutModelData(id);
00132         current->ShowViewBox(check);
00133 }
00134 
00135 void CutModelManager::ChangeShape(int id,int selection)throw( CutModelException){
00136         checkInvariant();
00137         CutModelData* current = getCutModelData(id);
00138         current->ChangeShape(selection);                        
00139         _render->Render();
00140 }
00141 
00142 CutModelData* CutModelManager::getCutModelData(int id)throw( CutModelException){
00143 
00144         CutModelData* current = NULL;
00145         int i;
00146         for(i= 0; i < (int)_vectordata.size();i++)
00147         {
00148                 CutModelData* temp = _vectordata[i];
00149                 std::cout<<"id in CutModelManager:: "<<id<<std::endl;
00150                 std::cout<<"vectordataid in CutModelManager:: "<<temp->getId()<<std::endl;
00151 
00152                 if(temp->getId() == id){
00153                         current =  temp;
00154                 }
00155         }
00156         if(current ==NULL){
00157 
00158                 throw CutModelException("Data not found");
00159         }
00160         return current;
00161 }
00162 
00163 void CutModelManager::updateActorDirection(int id)throw( CutModelException){
00164         checkInvariant();
00165         CutModelData* current = getCutModelData(id);
00166         current->udapteActorDirection();
00167 
00168 }
00169 
00170 void CutModelManager::changeColor(int id,double r,double g,double b)throw( CutModelException){
00171 
00172         checkInvariant();
00173         CutModelData* current = getCutModelData(id);
00174         current->changeColor(r,g,b);
00175         _render->Render();
00176 }
00177 void CutModelManager::RemoveActor(int id)throw( CutModelException){
00178 
00179         checkInvariant();
00180 
00181         CutModelData* current = getCutModelData(id);
00182         int i,j;
00183         for(i = 0; i < (int)_vectordata.size()-1;i++)
00184         {
00185                 if(_vectordata[i]->getId()==id){                                
00186                         for(j = i; j < (int)_vectordata.size()-1;j++){
00187                                 _vectordata[j]=_vectordata[j+1];
00188                         }
00189                         i = _vectordata.size();
00190                 }
00191         }
00192         _render->RemoveViewProp(current->getActor());           
00193         //_render->RemoveViewProp(current->get
00194         delete current;
00195         _vectordata.pop_back();
00196         _render->Render();
00197 
00198 }
00199 
00200 void CutModelManager::ExecuteCut(int id, double* range, bool isinside)throw( CutModelException){
00201         checkInvariant();
00202 
00203         CutModelData* current = getCutModelData(id);
00204         current->ExecuteCut(range, isinside,_img2);
00205 
00206 
00207         /*
00208         Setting extra information for the undo
00209         */
00210         CutModelSaveBinInfo* undoaction = this->AddActionUndo(id, CUTMODEL_CUT);
00211         undoaction->setRange(range);
00212         undoaction->setIsInside(isinside);
00213 
00214 }
00215 
00216 vtkImageData* CutModelManager::GetResultImage(){
00217         checkInvariant();
00218         return _img2;
00219 }
00220 
00221 void CutModelManager::RefreshActor(int id){
00222         checkInvariant();
00223         CutModelData* current = getCutModelData(id);    
00224         _render->RemoveActor(current->getActor());
00225         _render->AddActor(current->getActor()); 
00226         current->RefreshViewBox();
00227         _render->Render();
00228 }
00229 
00230 void CutModelManager::SaveCutModelData(std::string filename)throw( CutModelException){  
00231 
00232 
00233         throw CutModelException("not implemented");
00234 
00235 
00236 
00237 
00238 }
00239 
00240 
00241 
00242 void CutModelManager::LoadCutModelData(std::string filename)throw( CutModelException){
00243 
00244         throw CutModelException("not implemented");
00245 
00246 }
00247 
00248 CutModelSaveBinInfo* CutModelManager::AddActionUndo(int idc, UNDOTYPE type)throw( CutModelException){
00249 
00250         for(int i = _undoredo.size()-1; i > _currentaction;i--){
00251                 delete _undoredo[i];
00252                 _undoredo.pop_back();           
00253         }
00254 
00255         CutModelSaveBinInfo* cutmodel = new CutModelSaveBinInfo(idc, _currentaction, type, _path);
00256         if(type == CUTMODEL_CUT){
00257                 cutmodel->saveMatrix4x4(this->getCutModelData(idc)->getCurrentMatrix()->GetMatrix());
00258                 cutmodel->setCurrentShape(this->getCutModelData(idc)->getCurrentShape());
00259         }
00260 
00261         _undoredo.push_back(cutmodel);
00262 
00263         _currentaction++;// = _undoredo.size();
00264         //std::cout<<"current index "<<_currentaction;
00265 
00266         return cutmodel;
00267 }
00268 
00269 int CutModelManager::Undo()     throw( CutModelException){
00270         //&& _currentaction < _undoredo.size()
00271         if(_currentaction > 0){
00272                 int tempaction = _currentaction-1;
00273                 CutModelSaveBinInfo* currentundo = _undoredo[tempaction];
00274                 CutModelData* currentmodel;
00275 
00276                 if(currentundo->getActionType()==CUTMODEL_CUT){
00277                         //Undo the cut
00278                         vtkTransform* transform = currentundo->getTransformFromMatrixFile();
00279 
00280                         currentmodel = getCutModelData(currentundo->getId());
00281 
00282                         currentmodel->setTransform(transform, _img2);
00283 
00284                         currentmodel->setCurrentShape(currentundo->getCurrentShape());
00285 
00286                         currentmodel->ExecuteUnCut(currentundo->getIsInside(), _img, _img2);
00287 
00288                 }
00289                 //Every thing ok
00290                 _currentaction--;
00291                 return 0;
00292         }
00293         return -1;
00294 }
00295 
00296 int CutModelManager::Redo()     throw( CutModelException){
00297 
00298         if(_currentaction >= 0 && _currentaction < (int)_undoredo.size()){
00299 
00300                 CutModelSaveBinInfo* currentundo = _undoredo[_currentaction];
00301                 CutModelData* currentmodel;
00302 
00303                 if(currentundo->getActionType()==CUTMODEL_CUT){
00304                         //Redo the cut
00305                         vtkTransform* transform = currentundo->getTransformFromMatrixFile();
00306                         currentmodel = getCutModelData(currentundo->getId());
00307                         currentmodel->setTransform(transform, _img2);
00308                         currentmodel->setCurrentShape(currentundo->getCurrentShape());
00309                         currentmodel->ExecuteCut(currentundo->getRange(), currentundo->getIsInside(), _img2);
00310                 }
00311 
00312                 _currentaction++;
00313 
00314                 return 0;
00315         }
00316         return -1;
00317 }
00318 
00319 void CutModelManager::ParallelProjectionOn(){
00320         _render->GetActiveCamera()->ParallelProjectionOn();
00321 }
00322 
00323 void CutModelManager::ParallelProjectionOff(){
00324         _render->GetActiveCamera()->ParallelProjectionOff();
00325 }
00326 
00327 
00328 //RaC
00329 void CutModelManager::UpdatePolygon(bool mode)
00330 {
00331         if(mode)
00332         {
00333                 cutterstyle->Delete();
00334                 cutterstyle = vtkInteractorStyleCutter::New();
00335 
00336                 cutterstyle->SetInteractor ( _interactor );
00337                 _interactor ->SetInteractorStyle( cutterstyle );
00338         }
00339         else
00340         {
00341                 cutterstyle->VisibilityOff();
00342 
00343                 loop = vtkImplicitSelectionLoop::New();
00344                 mapper = vtkPolyDataMapper::New();
00345                 if(cutterstyle->Finished())
00346                 {
00347                         if(actor3D != NULL)
00348                         {
00349                                 _render->RemoveActor(actor3D);
00350                         }
00351 
00352                         loop->SetLoop( cutterstyle->GetLoopPoints() );
00353                         loop->SetNormal( cutterstyle->GetDirection());
00354 
00355 
00357                         int numPoints = cutterstyle->GetLoopPoints()->GetNumberOfPoints();
00358                         contourDirection = cutterstyle->GetDirection();
00359 
00360                         contourPoints = cutterstyle->GetLoopPoints();
00361 
00362                         _polygonCutter = new CutModelPolygon();
00363 
00364                         cout<<"RaC Printing points......"<<endl;
00365                         for(int t=0;t<numPoints;t++)
00366                         {
00367                                 double point[3];
00368                                 cutterstyle->GetLoopPoints()->GetPoint(t,point);
00369                                 cout<<"Initial Point:"<<t<<" XX:"<<point[0]<<" YY:"<<point[1]<<" ZZ:"<<point[2]<<endl;
00370                         }
00371                         cout<<endl;
00372 
00373                         sample = vtkSampleFunction::New();
00374 
00375                         sample->SetImplicitFunction(loop);
00376                         sample->CappingOn();                    
00377 
00378                         contour = vtkContourFilter::New();
00379                         contour->SetInput((vtkDataObject *)sample->GetOutput());
00380                         contour->SetValue(0,1);
00381 
00382                         actor = contour->GetOutput();
00383 
00384                         actor3D = vtkActor::New();                      
00385                         mapper->SetInput(actor);                         
00386                         mapper->SetScalarModeToUseCellData();
00387                         actor3D->SetMapper( mapper);
00388                         _render->AddActor(actor3D);                     
00389                 }               
00390 
00391                 interactorstyle->SetInteractor (  _interactor );
00392                 _interactor->SetInteractorStyle( interactorstyle );
00393         } 
00394 
00395         _interactor->Render();
00396         _render->ResetCameraClippingRange();
00397 }
00398 void CutModelManager::ExecuteCutPolygon(bool inOutCut){
00399 
00400         _polygonCutter->setInImage(_img2);
00401         _polygonCutter->setPoints(contourPoints);
00402         _polygonCutter->setDirection(contourDirection);
00403         _polygonCutter->processOutImage(inOutCut);
00404 }
00405 
00406 void CutModelManager::InitializePolygonInteractorStyle(){
00407 
00408         cutterstyle = vtkInteractorStyleCutter::New();          
00409         interactorstyle = vtkInteractorStyleTrackballCamera ::New();
00410         interactorstyle->SetInteractor (  _interactor );
00411         _interactor->SetInteractorStyle( interactorstyle );
00412 }

Generated on 20 Oct 2010 for creaMaracasVisu_lib by  doxygen 1.6.1