1 /*# ---------------------------------------------------------------------
3 # Copyright (c) CREATIS (Centre de Recherche en Acquisition et Traitement de l'Image
5 # Authors : Eduardo Davila, Frederic Cervenansky, Claire Mouton
6 # Previous Authors : Laurent Guigues, Jean-Pierre Roux
7 # CreaTools website : www.creatis.insa-lyon.fr/site/fr/creatools_accueil
9 # This software is governed by the CeCILL-B license under French law and
10 # abiding by the rules of distribution of free software. You can use,
11 # modify and/ or redistribute the software under the terms of the CeCILL-B
12 # license as circulated by CEA, CNRS and INRIA at the following URL
13 # http://www.cecill.info/licences/Licence_CeCILL-B_V1-en.html
14 # or in the file LICENSE.txt.
16 # As a counterpart to the access to the source code and rights to copy,
17 # modify and redistribute granted by the license, users are provided only
18 # with a limited warranty and the software's author, the holder of the
19 # economic rights, and the successive licensors have only limited
22 # The fact that you are presently reading this means that you have had
23 # knowledge of the CeCILL-B license and that you accept its terms.
24 # ------------------------------------------------------------------------ */
26 /*=========================================================================
29 Module: $RCSfile: CutModelManager.cxx,v $
31 Date: $Date: 2012/11/15 14:15:48 $
32 Version: $Revision: 1.11 $
34 Copyright: (c) 2002, 2003
37 This software is distributed WITHOUT ANY WARRANTY; without even
38 the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
39 PURPOSE. See the above copyright notice for more information.
41 =========================================================================*/
42 #include "CutModelManager.h"
45 #include "CutModelMainPanel.h"
48 ** Start of the manager class
50 CutModelManager::CutModelManager(std::string path){
59 CutModelManager::~CutModelManager(){
60 std::string files = _path;
61 files+="/infounrd_0_fig_0.info";
62 remove(files.c_str());
71 vtkImageData* CutModelManager::getImageData(){
75 void CutModelManager::setImageData(vtkImageData* img){
76 int type = CutModelMainPanel::getInstance()->GetType();
84 _img2 = vtkImageData::New();
85 _img2->SetExtent(_img->GetExtent());
86 _img2->SetSpacing(_img->GetSpacing());
87 _img2->AllocateScalars();
89 _img2->DeepCopy(_img);
98 _img = vtkImageData::New();
99 _img->SetExtent(_img2->GetExtent());
100 _img->SetSpacing(_img2->GetSpacing());
101 _img->AllocateScalars();
103 _img->DeepCopy(_img2);
107 void CutModelManager::setInteractor(vtkRenderWindowInteractor* interactor){
108 _interactor = interactor;
111 void CutModelManager::setRenderer(vtkRenderer* renderer){
115 void CutModelManager::onAddCutModel(int id, vtkCommand* observer) throw( CutModelException){
119 CutModelData* data = new CutModelData(id,_interactor, observer, _img);
120 _vectordata.push_back(data);
122 _render->AddActor(data->getActor());
124 //_render->UpdateCamera();
128 void CutModelManager::checkInvariant() throw( CutModelException){
130 throw CutModelException("The image is not set");
133 throw CutModelException("The image is not set");
135 if(_interactor==NULL){
136 throw CutModelException("Interactor not set");
139 throw CutModelException("Render not set");
143 double* CutModelManager::getImageRange()throw( CutModelException){
145 return _img->GetScalarRange();
148 void CutModelManager::changeOpacity(int id,int opacity)throw( CutModelException){
150 CutModelData* current = getCutModelData(id);
151 current->changeOpacity(opacity);
154 void CutModelManager::ShowViewBox(int id,bool check)throw( CutModelException){
156 CutModelData* current = getCutModelData(id);
157 current->ShowViewBox(check);
160 void CutModelManager::ChangeShape(int id,int selection)throw( CutModelException){
162 CutModelData* current = getCutModelData(id);
163 current->ChangeShape(selection);
167 CutModelData* CutModelManager::getCutModelData(int id)throw( CutModelException){
169 CutModelData* current = NULL;
171 for(i= 0; i < (int)_vectordata.size();i++)
173 CutModelData* temp = _vectordata[i];
174 std::cout<<"id in CutModelManager:: "<<id<<std::endl;
175 std::cout<<"vectordataid in CutModelManager:: "<<temp->getId()<<std::endl;
177 if(temp->getId() == id){
183 throw CutModelException("Data not found");
188 void CutModelManager::updateActorDirection(int id)throw( CutModelException){
190 CutModelData* current = getCutModelData(id);
191 current->udapteActorDirection();
195 void CutModelManager::changeColor(int id,double r,double g,double b)throw( CutModelException){
198 CutModelData* current = getCutModelData(id);
199 current->changeColor(r,g,b);
202 void CutModelManager::RemoveActor(int id)throw( CutModelException){
206 CutModelData* current = getCutModelData(id);
208 for(i = 0; i < (int)_vectordata.size()-1;i++)
210 if(_vectordata[i]->getId()==id){
211 for(j = i; j < (int)_vectordata.size()-1;j++){
212 _vectordata[j]=_vectordata[j+1];
214 i = _vectordata.size();
217 _render->RemoveViewProp(current->getActor());
218 //_render->RemoveViewProp(current->get
220 _vectordata.pop_back();
225 void CutModelManager::ExecuteCut(int id, double* range, bool isinside)throw( CutModelException){
228 CutModelData* current = getCutModelData(id);
229 current->ExecuteCut(range, isinside,_img2);
233 Setting extra information for the undo
235 CutModelSaveBinInfo* undoaction = this->AddActionUndo(id, CUTMODEL_CUT);
236 undoaction->setRange(range);
237 undoaction->setIsInside(isinside);
241 vtkImageData* CutModelManager::GetResultImage(){
246 void CutModelManager::RefreshActor(int id){
248 CutModelData* current = getCutModelData(id);
249 _render->RemoveActor(current->getActor());
250 _render->AddActor(current->getActor());
251 current->RefreshViewBox();
255 void CutModelManager::SaveCutModelData(std::string filename)throw( CutModelException){
258 throw CutModelException("not implemented");
267 void CutModelManager::LoadCutModelData(std::string filename)throw( CutModelException){
269 throw CutModelException("not implemented");
273 CutModelSaveBinInfo* CutModelManager::AddActionUndo(int idc, UNDOTYPE type)throw( CutModelException){
275 for(int i = _undoredo.size()-1; i > _currentaction;i--){
277 _undoredo.pop_back();
280 CutModelSaveBinInfo* cutmodel = new CutModelSaveBinInfo(idc, _currentaction, type, _path);
281 if(type == CUTMODEL_CUT){
282 cutmodel->saveMatrix4x4(this->getCutModelData(idc)->getCurrentMatrix()->GetMatrix());
283 cutmodel->setCurrentShape(this->getCutModelData(idc)->getCurrentShape());
286 _undoredo.push_back(cutmodel);
288 _currentaction++;// = _undoredo.size();
289 //std::cout<<"current index "<<_currentaction;
294 int CutModelManager::Undo() throw( CutModelException){
295 //&& _currentaction < _undoredo.size()
296 if(_currentaction > 0){
297 int tempaction = _currentaction-1;
298 CutModelSaveBinInfo* currentundo = _undoredo[tempaction];
299 CutModelData* currentmodel;
301 if(currentundo->getActionType()==CUTMODEL_CUT){
303 vtkTransform* transform = currentundo->getTransformFromMatrixFile();
305 currentmodel = getCutModelData(currentundo->getId());
307 currentmodel->setTransform(transform, _img2);
309 currentmodel->setCurrentShape(currentundo->getCurrentShape());
311 currentmodel->ExecuteUnCut(currentundo->getIsInside(), _img, _img2);
321 int CutModelManager::Redo() throw( CutModelException){
323 if(_currentaction >= 0 && _currentaction < (int)_undoredo.size()){
325 CutModelSaveBinInfo* currentundo = _undoredo[_currentaction];
326 CutModelData* currentmodel;
328 if(currentundo->getActionType()==CUTMODEL_CUT){
330 vtkTransform* transform = currentundo->getTransformFromMatrixFile();
331 currentmodel = getCutModelData(currentundo->getId());
332 currentmodel->setTransform(transform, _img2);
333 currentmodel->setCurrentShape(currentundo->getCurrentShape());
334 currentmodel->ExecuteCut(currentundo->getRange(), currentundo->getIsInside(), _img2);
344 void CutModelManager::ParallelProjectionOn(){
345 _render->GetActiveCamera()->ParallelProjectionOn();
348 void CutModelManager::ParallelProjectionOff(){
349 _render->GetActiveCamera()->ParallelProjectionOff();
354 void CutModelManager::UpdatePolygon(bool mode)
358 cutterstyle->Delete();
359 cutterstyle = vtkInteractorStyleCutter::New();
361 cutterstyle->SetInteractor ( _interactor );
362 _interactor ->SetInteractorStyle( cutterstyle );
366 cutterstyle->VisibilityOff();
368 loop = vtkImplicitSelectionLoop::New();
369 mapper = vtkPolyDataMapper::New();
370 if(cutterstyle->Finished())
374 _render->RemoveActor(actor3D);
377 loop->SetLoop( cutterstyle->GetLoopPoints() );
378 loop->SetNormal( cutterstyle->GetDirection());
382 int numPoints = cutterstyle->GetLoopPoints()->GetNumberOfPoints();
383 contourDirection = cutterstyle->GetDirection();
385 contourPoints = cutterstyle->GetLoopPoints();
387 _polygonCutter = new CutModelPolygon();
389 cout<<"RaC Printing points......"<<endl;
390 for(int t=0;t<numPoints;t++)
393 cutterstyle->GetLoopPoints()->GetPoint(t,point);
394 cout<<"Initial Point:"<<t<<" XX:"<<point[0]<<" YY:"<<point[1]<<" ZZ:"<<point[2]<<endl;
398 sample = vtkSampleFunction::New();
400 sample->SetImplicitFunction(loop);
403 contour = vtkContourFilter::New();
404 contour->SetInput((vtkDataObject *)sample->GetOutput());
405 contour->SetValue(0,1);
407 actor = contour->GetOutput();
409 actor3D = vtkActor::New();
410 mapper->SetInput(actor);
411 mapper->SetScalarModeToUseCellData();
412 actor3D->SetMapper( mapper);
413 _render->AddActor(actor3D);
416 interactorstyle->SetInteractor ( _interactor );
417 _interactor->SetInteractorStyle( interactorstyle );
420 _interactor->Render();
421 _render->ResetCameraClippingRange();
423 void CutModelManager::ExecuteCutPolygon(bool inOutCut){
425 _polygonCutter->setInImage(_img2);
426 _polygonCutter->setPoints(contourPoints);
427 _polygonCutter->setDirection(contourDirection);
428 _polygonCutter->processOutImage(inOutCut);
431 void CutModelManager::InitializePolygonInteractorStyle(){
433 cutterstyle = vtkInteractorStyleCutter::New();
434 interactorstyle = vtkInteractorStyleTrackballCamera ::New();
435 interactorstyle->SetInteractor ( _interactor );
436 _interactor->SetInteractorStyle( interactorstyle );