1 /*=========================================================================
4 Module: $RCSfile: CutModelManager.cxx,v $
6 Date: $Date: 2010/02/24 13:56:08 $
7 Version: $Revision: 1.7 $
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"
18 #include "CutModelMainPanel.h"
21 ** Start of the manager class
23 CutModelManager::CutModelManager(std::string path){
32 CutModelManager::~CutModelManager(){
33 std::string files = _path;
34 files+="/infounrd_0_fig_0.info";
35 remove(files.c_str());
44 vtkImageData* CutModelManager::getImageData(){
48 void CutModelManager::setImageData(vtkImageData* img){
49 int type = CutModelMainPanel::getInstance()->GetType();
57 _img2 = vtkImageData::New();
58 _img2->SetExtent(_img->GetExtent());
59 _img2->SetSpacing(_img->GetSpacing());
60 _img2->AllocateScalars();
62 _img2->DeepCopy(_img);
71 _img = vtkImageData::New();
72 _img->SetExtent(_img2->GetExtent());
73 _img->SetSpacing(_img2->GetSpacing());
74 _img->AllocateScalars();
76 _img->DeepCopy(_img2);
80 void CutModelManager::setInteractor(vtkRenderWindowInteractor* interactor){
81 _interactor = interactor;
84 void CutModelManager::setRenderer(vtkRenderer* renderer){
88 void CutModelManager::onAddCutModel(int id, vtkCommand* observer) throw( CutModelException){
92 CutModelData* data = new CutModelData(id,_interactor, observer, _img);
93 _vectordata.push_back(data);
95 _render->AddActor(data->getActor());
97 //_render->UpdateCamera();
101 void CutModelManager::checkInvariant() throw( CutModelException){
103 throw CutModelException("The image is not set");
106 throw CutModelException("The image is not set");
108 if(_interactor==NULL){
109 throw CutModelException("Interactor not set");
112 throw CutModelException("Render not set");
116 double* CutModelManager::getImageRange()throw( CutModelException){
118 return _img->GetScalarRange();
121 void CutModelManager::changeOpacity(int id,int opacity)throw( CutModelException){
123 CutModelData* current = getCutModelData(id);
124 current->changeOpacity(opacity);
127 void CutModelManager::ShowViewBox(int id,bool check)throw( CutModelException){
129 CutModelData* current = getCutModelData(id);
130 current->ShowViewBox(check);
133 void CutModelManager::ChangeShape(int id,int selection)throw( CutModelException){
135 CutModelData* current = getCutModelData(id);
136 current->ChangeShape(selection);
140 CutModelData* CutModelManager::getCutModelData(int id)throw( CutModelException){
142 CutModelData* current = NULL;
143 for(int i= 0; i < _vectordata.size();i++){
144 CutModelData* temp = _vectordata[i];
145 std::cout<<"id in CutModelManager:: "<<id<<std::endl;
146 std::cout<<"vectordataid in CutModelManager:: "<<temp->getId()<<std::endl;
148 if(temp->getId() == id){
154 throw CutModelException("Data not found");
159 void CutModelManager::updateActorDirection(int id)throw( CutModelException){
161 CutModelData* current = getCutModelData(id);
162 current->udapteActorDirection();
166 void CutModelManager::changeColor(int id,double r,double g,double b)throw( CutModelException){
169 CutModelData* current = getCutModelData(id);
170 current->changeColor(r,g,b);
173 void CutModelManager::RemoveActor(int id)throw( CutModelException){
177 CutModelData* current = getCutModelData(id);
178 for(int i = 0; i < _vectordata.size()-1;i++){
179 if(_vectordata[i]->getId()==id){
180 for(int j = i; j < _vectordata.size()-1;j++){
181 _vectordata[j]=_vectordata[j+1];
183 i = _vectordata.size();
186 _render->RemoveViewProp(current->getActor());
187 //_render->RemoveViewProp(current->get
189 _vectordata.pop_back();
194 void CutModelManager::ExecuteCut(int id, double* range, bool isinside)throw( CutModelException){
197 CutModelData* current = getCutModelData(id);
198 current->ExecuteCut(range, isinside,_img2);
202 Setting extra information for the undo
204 CutModelSaveBinInfo* undoaction = this->AddActionUndo(id, CUTMODEL_CUT);
205 undoaction->setRange(range);
206 undoaction->setIsInside(isinside);
210 vtkImageData* CutModelManager::GetResultImage(){
215 void CutModelManager::RefreshActor(int id){
217 CutModelData* current = getCutModelData(id);
218 _render->RemoveActor(current->getActor());
219 _render->AddActor(current->getActor());
220 current->RefreshViewBox();
224 void CutModelManager::SaveCutModelData(std::string filename)throw( CutModelException){
227 throw CutModelException("not implemented");
236 void CutModelManager::LoadCutModelData(std::string filename)throw( CutModelException){
238 throw CutModelException("not implemented");
242 CutModelSaveBinInfo* CutModelManager::AddActionUndo(int idc, UNDOTYPE type)throw( CutModelException){
244 for(int i = _undoredo.size()-1; i > _currentaction;i--){
246 _undoredo.pop_back();
249 CutModelSaveBinInfo* cutmodel = new CutModelSaveBinInfo(idc, _currentaction, type, _path);
250 if(type == CUTMODEL_CUT){
251 cutmodel->saveMatrix4x4(this->getCutModelData(idc)->getCurrentMatrix()->GetMatrix());
252 cutmodel->setCurrentShape(this->getCutModelData(idc)->getCurrentShape());
255 _undoredo.push_back(cutmodel);
257 _currentaction++;// = _undoredo.size();
258 //std::cout<<"current index "<<_currentaction;
263 int CutModelManager::Undo() throw( CutModelException){
264 //&& _currentaction < _undoredo.size()
265 if(_currentaction > 0){
266 int tempaction = _currentaction-1;
267 CutModelSaveBinInfo* currentundo = _undoredo[tempaction];
268 CutModelData* currentmodel;
270 if(currentundo->getActionType()==CUTMODEL_CUT){
272 vtkTransform* transform = currentundo->getTransformFromMatrixFile();
274 currentmodel = getCutModelData(currentundo->getId());
276 currentmodel->setTransform(transform, _img2);
278 currentmodel->setCurrentShape(currentundo->getCurrentShape());
280 currentmodel->ExecuteUnCut(currentundo->getIsInside(), _img, _img2);
290 int CutModelManager::Redo() throw( CutModelException){
292 if(_currentaction >= 0 && _currentaction < _undoredo.size()){
294 CutModelSaveBinInfo* currentundo = _undoredo[_currentaction];
295 CutModelData* currentmodel;
297 if(currentundo->getActionType()==CUTMODEL_CUT){
299 vtkTransform* transform = currentundo->getTransformFromMatrixFile();
300 currentmodel = getCutModelData(currentundo->getId());
301 currentmodel->setTransform(transform, _img2);
302 currentmodel->setCurrentShape(currentundo->getCurrentShape());
303 currentmodel->ExecuteCut(currentundo->getRange(), currentundo->getIsInside(), _img2);
313 void CutModelManager::ParallelProjectionOn(){
314 _render->GetActiveCamera()->ParallelProjectionOn();
317 void CutModelManager::ParallelProjectionOff(){
318 _render->GetActiveCamera()->ParallelProjectionOff();
323 void CutModelManager::UpdatePolygon(bool mode)
327 cutterstyle->Delete();
328 cutterstyle = vtkInteractorStyleCutter::New();
330 cutterstyle->SetInteractor ( _interactor );
331 _interactor ->SetInteractorStyle( cutterstyle );
335 cutterstyle->VisibilityOff();
337 loop = vtkImplicitSelectionLoop::New();
338 mapper = vtkPolyDataMapper::New();
339 if(cutterstyle->Finished())
343 _render->RemoveActor(actor3D);
346 loop->SetLoop( cutterstyle->GetLoopPoints() );
347 loop->SetNormal( cutterstyle->GetDirection());
351 int numPoints = cutterstyle->GetLoopPoints()->GetNumberOfPoints();
352 contourDirection = cutterstyle->GetDirection();
354 contourPoints = cutterstyle->GetLoopPoints();
356 _polygonCutter = new CutModelPolygon();
358 cout<<"RaC ContourDrawer::Update Printing points"<<endl;
359 for(int t=0;t<numPoints;t++)
362 cutterstyle->GetLoopPoints()->GetPoint(t,point);
363 cout<<"Initial Point:"<<t<<" XX:"<<point[0]<<" YY:"<<point[1]<<" ZZ:"<<point[2]<<endl;
367 sample = vtkSampleFunction::New();
369 sample->SetImplicitFunction(loop);
372 contour = vtkContourFilter::New();
373 contour->SetInput((vtkDataObject *)sample->GetOutput());
374 contour->SetValue(0,1);
376 actor = contour->GetOutput();
378 actor3D = vtkActor::New();
379 mapper->SetInput(actor);
380 mapper->SetScalarModeToUseCellData();
381 actor3D->SetMapper( mapper);
382 _render->AddActor(actor3D);
385 interactorstyle->SetInteractor ( _interactor );
386 _interactor->SetInteractorStyle( interactorstyle );
389 _interactor->Render();
390 _render->ResetCameraClippingRange();
392 void CutModelManager::ExecuteCutPolygon(bool inOutCut){
394 _polygonCutter->setInImage(_img2);
395 _polygonCutter->setPoints(contourPoints);
396 _polygonCutter->setDirection(contourDirection);
397 _polygonCutter->processOutImage(inOutCut);
400 void CutModelManager::InitializePolygonInteractorStyle(){
402 cutterstyle = vtkInteractorStyleCutter::New();
403 interactorstyle = vtkInteractorStyleTrackballCamera ::New();
404 interactorstyle->SetInteractor ( _interactor );
405 _interactor->SetInteractorStyle( interactorstyle );