1 /*=========================================================================
4 Module: $RCSfile: CutModelManager.cxx,v $
6 Date: $Date: 2010/09/29 21:21:05 $
7 Version: $Revision: 1.10 $
9 Copyright: (c) 2002, 2003
12 This software is distributed WITHOUT ANY WARRANTY; without even
13 the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
14 PURPOSE. See the above copyright notice for more information.
16 =========================================================================*/
17 #include "CutModelManager.h"
20 #include "CutModelMainPanel.h"
23 ** Start of the manager class
25 CutModelManager::CutModelManager(std::string path){
34 CutModelManager::~CutModelManager(){
35 std::string files = _path;
36 files+="/infounrd_0_fig_0.info";
37 remove(files.c_str());
46 vtkImageData* CutModelManager::getImageData(){
50 void CutModelManager::setImageData(vtkImageData* img){
51 int type = CutModelMainPanel::getInstance()->GetType();
59 _img2 = vtkImageData::New();
60 _img2->SetExtent(_img->GetExtent());
61 _img2->SetSpacing(_img->GetSpacing());
62 _img2->AllocateScalars();
64 _img2->DeepCopy(_img);
73 _img = vtkImageData::New();
74 _img->SetExtent(_img2->GetExtent());
75 _img->SetSpacing(_img2->GetSpacing());
76 _img->AllocateScalars();
78 _img->DeepCopy(_img2);
82 void CutModelManager::setInteractor(vtkRenderWindowInteractor* interactor){
83 _interactor = interactor;
86 void CutModelManager::setRenderer(vtkRenderer* renderer){
90 void CutModelManager::onAddCutModel(int id, vtkCommand* observer) throw( CutModelException){
94 CutModelData* data = new CutModelData(id,_interactor, observer, _img);
95 _vectordata.push_back(data);
97 _render->AddActor(data->getActor());
99 //_render->UpdateCamera();
103 void CutModelManager::checkInvariant() throw( CutModelException){
105 throw CutModelException("The image is not set");
108 throw CutModelException("The image is not set");
110 if(_interactor==NULL){
111 throw CutModelException("Interactor not set");
114 throw CutModelException("Render not set");
118 double* CutModelManager::getImageRange()throw( CutModelException){
120 return _img->GetScalarRange();
123 void CutModelManager::changeOpacity(int id,int opacity)throw( CutModelException){
125 CutModelData* current = getCutModelData(id);
126 current->changeOpacity(opacity);
129 void CutModelManager::ShowViewBox(int id,bool check)throw( CutModelException){
131 CutModelData* current = getCutModelData(id);
132 current->ShowViewBox(check);
135 void CutModelManager::ChangeShape(int id,int selection)throw( CutModelException){
137 CutModelData* current = getCutModelData(id);
138 current->ChangeShape(selection);
142 CutModelData* CutModelManager::getCutModelData(int id)throw( CutModelException){
144 CutModelData* current = NULL;
146 for(i= 0; i < (int)_vectordata.size();i++)
148 CutModelData* temp = _vectordata[i];
149 std::cout<<"id in CutModelManager:: "<<id<<std::endl;
150 std::cout<<"vectordataid in CutModelManager:: "<<temp->getId()<<std::endl;
152 if(temp->getId() == id){
158 throw CutModelException("Data not found");
163 void CutModelManager::updateActorDirection(int id)throw( CutModelException){
165 CutModelData* current = getCutModelData(id);
166 current->udapteActorDirection();
170 void CutModelManager::changeColor(int id,double r,double g,double b)throw( CutModelException){
173 CutModelData* current = getCutModelData(id);
174 current->changeColor(r,g,b);
177 void CutModelManager::RemoveActor(int id)throw( CutModelException){
181 CutModelData* current = getCutModelData(id);
183 for(i = 0; i < (int)_vectordata.size()-1;i++)
185 if(_vectordata[i]->getId()==id){
186 for(j = i; j < (int)_vectordata.size()-1;j++){
187 _vectordata[j]=_vectordata[j+1];
189 i = _vectordata.size();
192 _render->RemoveViewProp(current->getActor());
193 //_render->RemoveViewProp(current->get
195 _vectordata.pop_back();
200 void CutModelManager::ExecuteCut(int id, double* range, bool isinside)throw( CutModelException){
203 CutModelData* current = getCutModelData(id);
204 current->ExecuteCut(range, isinside,_img2);
208 Setting extra information for the undo
210 CutModelSaveBinInfo* undoaction = this->AddActionUndo(id, CUTMODEL_CUT);
211 undoaction->setRange(range);
212 undoaction->setIsInside(isinside);
216 vtkImageData* CutModelManager::GetResultImage(){
221 void CutModelManager::RefreshActor(int id){
223 CutModelData* current = getCutModelData(id);
224 _render->RemoveActor(current->getActor());
225 _render->AddActor(current->getActor());
226 current->RefreshViewBox();
230 void CutModelManager::SaveCutModelData(std::string filename)throw( CutModelException){
233 throw CutModelException("not implemented");
242 void CutModelManager::LoadCutModelData(std::string filename)throw( CutModelException){
244 throw CutModelException("not implemented");
248 CutModelSaveBinInfo* CutModelManager::AddActionUndo(int idc, UNDOTYPE type)throw( CutModelException){
250 for(int i = _undoredo.size()-1; i > _currentaction;i--){
252 _undoredo.pop_back();
255 CutModelSaveBinInfo* cutmodel = new CutModelSaveBinInfo(idc, _currentaction, type, _path);
256 if(type == CUTMODEL_CUT){
257 cutmodel->saveMatrix4x4(this->getCutModelData(idc)->getCurrentMatrix()->GetMatrix());
258 cutmodel->setCurrentShape(this->getCutModelData(idc)->getCurrentShape());
261 _undoredo.push_back(cutmodel);
263 _currentaction++;// = _undoredo.size();
264 //std::cout<<"current index "<<_currentaction;
269 int CutModelManager::Undo() throw( CutModelException){
270 //&& _currentaction < _undoredo.size()
271 if(_currentaction > 0){
272 int tempaction = _currentaction-1;
273 CutModelSaveBinInfo* currentundo = _undoredo[tempaction];
274 CutModelData* currentmodel;
276 if(currentundo->getActionType()==CUTMODEL_CUT){
278 vtkTransform* transform = currentundo->getTransformFromMatrixFile();
280 currentmodel = getCutModelData(currentundo->getId());
282 currentmodel->setTransform(transform, _img2);
284 currentmodel->setCurrentShape(currentundo->getCurrentShape());
286 currentmodel->ExecuteUnCut(currentundo->getIsInside(), _img, _img2);
296 int CutModelManager::Redo() throw( CutModelException){
298 if(_currentaction >= 0 && _currentaction < (int)_undoredo.size()){
300 CutModelSaveBinInfo* currentundo = _undoredo[_currentaction];
301 CutModelData* currentmodel;
303 if(currentundo->getActionType()==CUTMODEL_CUT){
305 vtkTransform* transform = currentundo->getTransformFromMatrixFile();
306 currentmodel = getCutModelData(currentundo->getId());
307 currentmodel->setTransform(transform, _img2);
308 currentmodel->setCurrentShape(currentundo->getCurrentShape());
309 currentmodel->ExecuteCut(currentundo->getRange(), currentundo->getIsInside(), _img2);
319 void CutModelManager::ParallelProjectionOn(){
320 _render->GetActiveCamera()->ParallelProjectionOn();
323 void CutModelManager::ParallelProjectionOff(){
324 _render->GetActiveCamera()->ParallelProjectionOff();
329 void CutModelManager::UpdatePolygon(bool mode)
333 cutterstyle->Delete();
334 cutterstyle = vtkInteractorStyleCutter::New();
336 cutterstyle->SetInteractor ( _interactor );
337 _interactor ->SetInteractorStyle( cutterstyle );
341 cutterstyle->VisibilityOff();
343 loop = vtkImplicitSelectionLoop::New();
344 mapper = vtkPolyDataMapper::New();
345 if(cutterstyle->Finished())
349 _render->RemoveActor(actor3D);
352 loop->SetLoop( cutterstyle->GetLoopPoints() );
353 loop->SetNormal( cutterstyle->GetDirection());
357 int numPoints = cutterstyle->GetLoopPoints()->GetNumberOfPoints();
358 contourDirection = cutterstyle->GetDirection();
360 contourPoints = cutterstyle->GetLoopPoints();
362 _polygonCutter = new CutModelPolygon();
364 cout<<"RaC Printing points......"<<endl;
365 for(int t=0;t<numPoints;t++)
368 cutterstyle->GetLoopPoints()->GetPoint(t,point);
369 cout<<"Initial Point:"<<t<<" XX:"<<point[0]<<" YY:"<<point[1]<<" ZZ:"<<point[2]<<endl;
373 sample = vtkSampleFunction::New();
375 sample->SetImplicitFunction(loop);
378 contour = vtkContourFilter::New();
379 contour->SetInput((vtkDataObject *)sample->GetOutput());
380 contour->SetValue(0,1);
382 actor = contour->GetOutput();
384 actor3D = vtkActor::New();
385 mapper->SetInput(actor);
386 mapper->SetScalarModeToUseCellData();
387 actor3D->SetMapper( mapper);
388 _render->AddActor(actor3D);
391 interactorstyle->SetInteractor ( _interactor );
392 _interactor->SetInteractorStyle( interactorstyle );
395 _interactor->Render();
396 _render->ResetCameraClippingRange();
398 void CutModelManager::ExecuteCutPolygon(bool inOutCut){
400 _polygonCutter->setInImage(_img2);
401 _polygonCutter->setPoints(contourPoints);
402 _polygonCutter->setDirection(contourDirection);
403 _polygonCutter->processOutImage(inOutCut);
406 void CutModelManager::InitializePolygonInteractorStyle(){
408 cutterstyle = vtkInteractorStyleCutter::New();
409 interactorstyle = vtkInteractorStyleTrackballCamera ::New();
410 interactorstyle->SetInteractor ( _interactor );
411 _interactor->SetInteractorStyle( interactorstyle );