1 /*=========================================================================
4 Module: $RCSfile: CutModelManager.cxx,v $
6 Date: $Date: 2010/03/04 15:09:57 $
7 Version: $Revision: 1.8 $
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;
145 for(int i= 0; i < _vectordata.size();i++){
146 CutModelData* temp = _vectordata[i];
147 std::cout<<"id in CutModelManager:: "<<id<<std::endl;
148 std::cout<<"vectordataid in CutModelManager:: "<<temp->getId()<<std::endl;
150 if(temp->getId() == id){
156 throw CutModelException("Data not found");
161 void CutModelManager::updateActorDirection(int id)throw( CutModelException){
163 CutModelData* current = getCutModelData(id);
164 current->udapteActorDirection();
168 void CutModelManager::changeColor(int id,double r,double g,double b)throw( CutModelException){
171 CutModelData* current = getCutModelData(id);
172 current->changeColor(r,g,b);
175 void CutModelManager::RemoveActor(int id)throw( CutModelException){
179 CutModelData* current = getCutModelData(id);
180 for(int i = 0; i < _vectordata.size()-1;i++){
181 if(_vectordata[i]->getId()==id){
182 for(int j = i; j < _vectordata.size()-1;j++){
183 _vectordata[j]=_vectordata[j+1];
185 i = _vectordata.size();
188 _render->RemoveViewProp(current->getActor());
189 //_render->RemoveViewProp(current->get
191 _vectordata.pop_back();
196 void CutModelManager::ExecuteCut(int id, double* range, bool isinside)throw( CutModelException){
199 CutModelData* current = getCutModelData(id);
200 current->ExecuteCut(range, isinside,_img2);
204 Setting extra information for the undo
206 CutModelSaveBinInfo* undoaction = this->AddActionUndo(id, CUTMODEL_CUT);
207 undoaction->setRange(range);
208 undoaction->setIsInside(isinside);
212 vtkImageData* CutModelManager::GetResultImage(){
217 void CutModelManager::RefreshActor(int id){
219 CutModelData* current = getCutModelData(id);
220 _render->RemoveActor(current->getActor());
221 _render->AddActor(current->getActor());
222 current->RefreshViewBox();
226 void CutModelManager::SaveCutModelData(std::string filename)throw( CutModelException){
229 throw CutModelException("not implemented");
238 void CutModelManager::LoadCutModelData(std::string filename)throw( CutModelException){
240 throw CutModelException("not implemented");
244 CutModelSaveBinInfo* CutModelManager::AddActionUndo(int idc, UNDOTYPE type)throw( CutModelException){
246 for(int i = _undoredo.size()-1; i > _currentaction;i--){
248 _undoredo.pop_back();
251 CutModelSaveBinInfo* cutmodel = new CutModelSaveBinInfo(idc, _currentaction, type, _path);
252 if(type == CUTMODEL_CUT){
253 cutmodel->saveMatrix4x4(this->getCutModelData(idc)->getCurrentMatrix()->GetMatrix());
254 cutmodel->setCurrentShape(this->getCutModelData(idc)->getCurrentShape());
257 _undoredo.push_back(cutmodel);
259 _currentaction++;// = _undoredo.size();
260 //std::cout<<"current index "<<_currentaction;
265 int CutModelManager::Undo() throw( CutModelException){
266 //&& _currentaction < _undoredo.size()
267 if(_currentaction > 0){
268 int tempaction = _currentaction-1;
269 CutModelSaveBinInfo* currentundo = _undoredo[tempaction];
270 CutModelData* currentmodel;
272 if(currentundo->getActionType()==CUTMODEL_CUT){
274 vtkTransform* transform = currentundo->getTransformFromMatrixFile();
276 currentmodel = getCutModelData(currentundo->getId());
278 currentmodel->setTransform(transform, _img2);
280 currentmodel->setCurrentShape(currentundo->getCurrentShape());
282 currentmodel->ExecuteUnCut(currentundo->getIsInside(), _img, _img2);
292 int CutModelManager::Redo() throw( CutModelException){
294 if(_currentaction >= 0 && _currentaction < _undoredo.size()){
296 CutModelSaveBinInfo* currentundo = _undoredo[_currentaction];
297 CutModelData* currentmodel;
299 if(currentundo->getActionType()==CUTMODEL_CUT){
301 vtkTransform* transform = currentundo->getTransformFromMatrixFile();
302 currentmodel = getCutModelData(currentundo->getId());
303 currentmodel->setTransform(transform, _img2);
304 currentmodel->setCurrentShape(currentundo->getCurrentShape());
305 currentmodel->ExecuteCut(currentundo->getRange(), currentundo->getIsInside(), _img2);
315 void CutModelManager::ParallelProjectionOn(){
316 _render->GetActiveCamera()->ParallelProjectionOn();
319 void CutModelManager::ParallelProjectionOff(){
320 _render->GetActiveCamera()->ParallelProjectionOff();
325 void CutModelManager::UpdatePolygon(bool mode)
329 cutterstyle->Delete();
330 cutterstyle = vtkInteractorStyleCutter::New();
332 cutterstyle->SetInteractor ( _interactor );
333 _interactor ->SetInteractorStyle( cutterstyle );
337 cutterstyle->VisibilityOff();
339 loop = vtkImplicitSelectionLoop::New();
340 mapper = vtkPolyDataMapper::New();
341 if(cutterstyle->Finished())
345 _render->RemoveActor(actor3D);
348 loop->SetLoop( cutterstyle->GetLoopPoints() );
349 loop->SetNormal( cutterstyle->GetDirection());
353 int numPoints = cutterstyle->GetLoopPoints()->GetNumberOfPoints();
354 contourDirection = cutterstyle->GetDirection();
356 contourPoints = cutterstyle->GetLoopPoints();
358 _polygonCutter = new CutModelPolygon();
360 cout<<"RaC ContourDrawer::Update Printing points"<<endl;
361 for(int t=0;t<numPoints;t++)
364 cutterstyle->GetLoopPoints()->GetPoint(t,point);
365 cout<<"Initial Point:"<<t<<" XX:"<<point[0]<<" YY:"<<point[1]<<" ZZ:"<<point[2]<<endl;
369 sample = vtkSampleFunction::New();
371 sample->SetImplicitFunction(loop);
374 contour = vtkContourFilter::New();
375 contour->SetInput((vtkDataObject *)sample->GetOutput());
376 contour->SetValue(0,1);
378 actor = contour->GetOutput();
380 actor3D = vtkActor::New();
381 mapper->SetInput(actor);
382 mapper->SetScalarModeToUseCellData();
383 actor3D->SetMapper( mapper);
384 _render->AddActor(actor3D);
387 interactorstyle->SetInteractor ( _interactor );
388 _interactor->SetInteractorStyle( interactorstyle );
391 _interactor->Render();
392 _render->ResetCameraClippingRange();
394 void CutModelManager::ExecuteCutPolygon(bool inOutCut){
396 _polygonCutter->setInImage(_img2);
397 _polygonCutter->setPoints(contourPoints);
398 _polygonCutter->setDirection(contourDirection);
399 _polygonCutter->processOutImage(inOutCut);
402 void CutModelManager::InitializePolygonInteractorStyle(){
404 cutterstyle = vtkInteractorStyleCutter::New();
405 interactorstyle = vtkInteractorStyleTrackballCamera ::New();
406 interactorstyle->SetInteractor ( _interactor );
407 _interactor->SetInteractorStyle( interactorstyle );