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/03/04 15:09:57 $
00007 Version:   $Revision: 1.8 $
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         for(int i= 0; i < _vectordata.size();i++){
00146                 CutModelData* temp = _vectordata[i];
00147                 std::cout<<"id in CutModelManager:: "<<id<<std::endl;
00148                 std::cout<<"vectordataid in CutModelManager:: "<<temp->getId()<<std::endl;
00149 
00150                 if(temp->getId() == id){
00151                         current =  temp;
00152                 }
00153         }
00154         if(current ==NULL){
00155 
00156                 throw CutModelException("Data not found");
00157         }
00158         return current;
00159 }
00160 
00161 void CutModelManager::updateActorDirection(int id)throw( CutModelException){
00162         checkInvariant();
00163         CutModelData* current = getCutModelData(id);
00164         current->udapteActorDirection();
00165 
00166 }
00167 
00168 void CutModelManager::changeColor(int id,double r,double g,double b)throw( CutModelException){
00169 
00170         checkInvariant();
00171         CutModelData* current = getCutModelData(id);
00172         current->changeColor(r,g,b);
00173         _render->Render();
00174 }
00175 void CutModelManager::RemoveActor(int id)throw( CutModelException){
00176 
00177         checkInvariant();
00178 
00179         CutModelData* current = getCutModelData(id);
00180         for(int i = 0; i < _vectordata.size()-1;i++){
00181                 if(_vectordata[i]->getId()==id){                                
00182                         for(int j = i; j < _vectordata.size()-1;j++){
00183                                 _vectordata[j]=_vectordata[j+1];
00184                         }
00185                         i = _vectordata.size();
00186                 }
00187         }
00188         _render->RemoveViewProp(current->getActor());           
00189         //_render->RemoveViewProp(current->get
00190         delete current;
00191         _vectordata.pop_back();
00192         _render->Render();
00193 
00194 }
00195 
00196 void CutModelManager::ExecuteCut(int id, double* range, bool isinside)throw( CutModelException){
00197         checkInvariant();
00198 
00199         CutModelData* current = getCutModelData(id);
00200         current->ExecuteCut(range, isinside,_img2);
00201 
00202 
00203         /*
00204         Setting extra information for the undo
00205         */
00206         CutModelSaveBinInfo* undoaction = this->AddActionUndo(id, CUTMODEL_CUT);
00207         undoaction->setRange(range);
00208         undoaction->setIsInside(isinside);
00209 
00210 }
00211 
00212 vtkImageData* CutModelManager::GetResultImage(){
00213         checkInvariant();
00214         return _img2;
00215 }
00216 
00217 void CutModelManager::RefreshActor(int id){
00218         checkInvariant();
00219         CutModelData* current = getCutModelData(id);    
00220         _render->RemoveActor(current->getActor());
00221         _render->AddActor(current->getActor()); 
00222         current->RefreshViewBox();
00223         _render->Render();
00224 }
00225 
00226 void CutModelManager::SaveCutModelData(std::string filename)throw( CutModelException){  
00227 
00228 
00229         throw CutModelException("not implemented");
00230 
00231 
00232 
00233 
00234 }
00235 
00236 
00237 
00238 void CutModelManager::LoadCutModelData(std::string filename)throw( CutModelException){
00239 
00240         throw CutModelException("not implemented");
00241 
00242 }
00243 
00244 CutModelSaveBinInfo* CutModelManager::AddActionUndo(int idc, UNDOTYPE type)throw( CutModelException){
00245 
00246         for(int i = _undoredo.size()-1; i > _currentaction;i--){
00247                 delete _undoredo[i];
00248                 _undoredo.pop_back();           
00249         }
00250 
00251         CutModelSaveBinInfo* cutmodel = new CutModelSaveBinInfo(idc, _currentaction, type, _path);
00252         if(type == CUTMODEL_CUT){
00253                 cutmodel->saveMatrix4x4(this->getCutModelData(idc)->getCurrentMatrix()->GetMatrix());
00254                 cutmodel->setCurrentShape(this->getCutModelData(idc)->getCurrentShape());
00255         }
00256 
00257         _undoredo.push_back(cutmodel);
00258 
00259         _currentaction++;// = _undoredo.size();
00260         //std::cout<<"current index "<<_currentaction;
00261 
00262         return cutmodel;
00263 }
00264 
00265 int CutModelManager::Undo()     throw( CutModelException){
00266         //&& _currentaction < _undoredo.size()
00267         if(_currentaction > 0){
00268                 int tempaction = _currentaction-1;
00269                 CutModelSaveBinInfo* currentundo = _undoredo[tempaction];
00270                 CutModelData* currentmodel;
00271 
00272                 if(currentundo->getActionType()==CUTMODEL_CUT){
00273                         //Undo the cut
00274                         vtkTransform* transform = currentundo->getTransformFromMatrixFile();
00275 
00276                         currentmodel = getCutModelData(currentundo->getId());
00277 
00278                         currentmodel->setTransform(transform, _img2);
00279 
00280                         currentmodel->setCurrentShape(currentundo->getCurrentShape());
00281 
00282                         currentmodel->ExecuteUnCut(currentundo->getIsInside(), _img, _img2);
00283 
00284                 }
00285                 //Every thing ok
00286                 _currentaction--;
00287                 return 0;
00288         }
00289         return -1;
00290 }
00291 
00292 int CutModelManager::Redo()     throw( CutModelException){
00293 
00294         if(_currentaction >= 0 && _currentaction < _undoredo.size()){
00295 
00296                 CutModelSaveBinInfo* currentundo = _undoredo[_currentaction];
00297                 CutModelData* currentmodel;
00298 
00299                 if(currentundo->getActionType()==CUTMODEL_CUT){
00300                         //Redo the cut
00301                         vtkTransform* transform = currentundo->getTransformFromMatrixFile();
00302                         currentmodel = getCutModelData(currentundo->getId());
00303                         currentmodel->setTransform(transform, _img2);
00304                         currentmodel->setCurrentShape(currentundo->getCurrentShape());
00305                         currentmodel->ExecuteCut(currentundo->getRange(), currentundo->getIsInside(), _img2);
00306                 }
00307 
00308                 _currentaction++;
00309 
00310                 return 0;
00311         }
00312         return -1;
00313 }
00314 
00315 void CutModelManager::ParallelProjectionOn(){
00316         _render->GetActiveCamera()->ParallelProjectionOn();
00317 }
00318 
00319 void CutModelManager::ParallelProjectionOff(){
00320         _render->GetActiveCamera()->ParallelProjectionOff();
00321 }
00322 
00323 
00324 //RaC
00325 void CutModelManager::UpdatePolygon(bool mode)
00326 {
00327         if(mode)
00328         {
00329                 cutterstyle->Delete();
00330                 cutterstyle = vtkInteractorStyleCutter::New();
00331 
00332                 cutterstyle->SetInteractor ( _interactor );
00333                 _interactor ->SetInteractorStyle( cutterstyle );
00334         }
00335         else
00336         {
00337                 cutterstyle->VisibilityOff();
00338 
00339                 loop = vtkImplicitSelectionLoop::New();
00340                 mapper = vtkPolyDataMapper::New();
00341                 if(cutterstyle->Finished())
00342                 {
00343                         if(actor3D != NULL)
00344                         {
00345                                 _render->RemoveActor(actor3D);
00346                         }
00347 
00348                         loop->SetLoop( cutterstyle->GetLoopPoints() );
00349                         loop->SetNormal( cutterstyle->GetDirection());
00350 
00351 
00353                         int numPoints = cutterstyle->GetLoopPoints()->GetNumberOfPoints();
00354                         contourDirection = cutterstyle->GetDirection();
00355 
00356                         contourPoints = cutterstyle->GetLoopPoints();
00357 
00358                         _polygonCutter = new CutModelPolygon();
00359 
00360                         cout<<"RaC ContourDrawer::Update Printing points"<<endl;
00361                         for(int t=0;t<numPoints;t++)
00362                         {
00363                                 double point[3];
00364                                 cutterstyle->GetLoopPoints()->GetPoint(t,point);
00365                                 cout<<"Initial Point:"<<t<<" XX:"<<point[0]<<" YY:"<<point[1]<<" ZZ:"<<point[2]<<endl;
00366                         }
00367                         cout<<endl;
00368 
00369                         sample = vtkSampleFunction::New();
00370 
00371                         sample->SetImplicitFunction(loop);
00372                         sample->CappingOn();                    
00373 
00374                         contour = vtkContourFilter::New();
00375                         contour->SetInput((vtkDataObject *)sample->GetOutput());
00376                         contour->SetValue(0,1);
00377 
00378                         actor = contour->GetOutput();
00379 
00380                         actor3D = vtkActor::New();                      
00381                         mapper->SetInput(actor);                         
00382                         mapper->SetScalarModeToUseCellData();
00383                         actor3D->SetMapper( mapper);
00384                         _render->AddActor(actor3D);                     
00385                 }               
00386 
00387                 interactorstyle->SetInteractor (  _interactor );
00388                 _interactor->SetInteractorStyle( interactorstyle );
00389         } 
00390 
00391         _interactor->Render();
00392         _render->ResetCameraClippingRange();
00393 }
00394 void CutModelManager::ExecuteCutPolygon(bool inOutCut){
00395 
00396         _polygonCutter->setInImage(_img2);
00397         _polygonCutter->setPoints(contourPoints);
00398         _polygonCutter->setDirection(contourDirection);
00399         _polygonCutter->processOutImage(inOutCut);
00400 }
00401 
00402 void CutModelManager::InitializePolygonInteractorStyle(){
00403 
00404         cutterstyle = vtkInteractorStyleCutter::New();          
00405         interactorstyle = vtkInteractorStyleTrackballCamera ::New();
00406         interactorstyle->SetInteractor (  _interactor );
00407         _interactor->SetInteractorStyle( interactorstyle );
00408 }

Generated on 18 Mar 2010 for creaMaracasVisu_lib by  doxygen 1.6.1