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 //EED 2017-01-01 Migration VTK7
88 #if VTK_MAJOR_VERSION <= 5
89 _img2->AllocateScalars();
93 _img2->DeepCopy(_img);
102 _img = vtkImageData::New();
103 _img->SetExtent(_img2->GetExtent());
104 _img->SetSpacing(_img2->GetSpacing());
105 //EED 2017-01-01 Migration VTK7
106 #if VTK_MAJOR_VERSION <= 5
107 _img->AllocateScalars();
112 _img->DeepCopy(_img2);
116 void CutModelManager::setInteractor(vtkRenderWindowInteractor* interactor){
117 _interactor = interactor;
120 void CutModelManager::setRenderer(vtkRenderer* renderer){
124 void CutModelManager::onAddCutModel(int id, vtkCommand* observer) throw( CutModelException){
128 CutModelData* data = new CutModelData(id,_interactor, observer, _img);
129 _vectordata.push_back(data);
131 _render->AddActor(data->getActor());
133 //_render->UpdateCamera();
137 void CutModelManager::checkInvariant() throw( CutModelException){
139 throw CutModelException("The image is not set");
142 throw CutModelException("The image is not set");
144 if(_interactor==NULL){
145 throw CutModelException("Interactor not set");
148 throw CutModelException("Render not set");
152 double* CutModelManager::getImageRange()throw( CutModelException){
154 return _img->GetScalarRange();
157 void CutModelManager::changeOpacity(int id,int opacity)throw( CutModelException){
159 CutModelData* current = getCutModelData(id);
160 current->changeOpacity(opacity);
163 void CutModelManager::ShowViewBox(int id,bool check)throw( CutModelException){
165 CutModelData* current = getCutModelData(id);
166 current->ShowViewBox(check);
169 void CutModelManager::ChangeShape(int id,int selection)throw( CutModelException){
171 CutModelData* current = getCutModelData(id);
172 current->ChangeShape(selection);
176 CutModelData* CutModelManager::getCutModelData(int id)throw( CutModelException){
178 CutModelData* current = NULL;
180 for(i= 0; i < (int)_vectordata.size();i++)
182 CutModelData* temp = _vectordata[i];
183 std::cout<<"id in CutModelManager:: "<<id<<std::endl;
184 std::cout<<"vectordataid in CutModelManager:: "<<temp->getId()<<std::endl;
186 if(temp->getId() == id){
192 throw CutModelException("Data not found");
197 void CutModelManager::updateActorDirection(int id)throw( CutModelException){
199 CutModelData* current = getCutModelData(id);
200 current->udapteActorDirection();
204 void CutModelManager::changeColor(int id,double r,double g,double b)throw( CutModelException){
207 CutModelData* current = getCutModelData(id);
208 current->changeColor(r,g,b);
211 void CutModelManager::RemoveActor(int id)throw( CutModelException){
215 CutModelData* current = getCutModelData(id);
217 for(i = 0; i < (int)_vectordata.size()-1;i++)
219 if(_vectordata[i]->getId()==id){
220 for(j = i; j < (int)_vectordata.size()-1;j++){
221 _vectordata[j]=_vectordata[j+1];
223 i = _vectordata.size();
226 _render->RemoveViewProp(current->getActor());
227 //_render->RemoveViewProp(current->get
229 _vectordata.pop_back();
234 void CutModelManager::ExecuteCut(int id, double* range, bool isinside)throw( CutModelException){
237 CutModelData* current = getCutModelData(id);
238 current->ExecuteCut(range, isinside,_img2);
242 Setting extra information for the undo
244 CutModelSaveBinInfo* undoaction = this->AddActionUndo(id, CUTMODEL_CUT);
245 undoaction->setRange(range);
246 undoaction->setIsInside(isinside);
250 vtkImageData* CutModelManager::GetResultImage(){
255 void CutModelManager::RefreshActor(int id){
257 CutModelData* current = getCutModelData(id);
258 _render->RemoveActor(current->getActor());
259 _render->AddActor(current->getActor());
260 current->RefreshViewBox();
264 void CutModelManager::SaveCutModelData(std::string filename)throw( CutModelException){
267 throw CutModelException("not implemented");
276 void CutModelManager::LoadCutModelData(std::string filename)throw( CutModelException){
278 throw CutModelException("not implemented");
282 CutModelSaveBinInfo* CutModelManager::AddActionUndo(int idc, UNDOTYPE type)throw( CutModelException){
284 for(int i = _undoredo.size()-1; i > _currentaction;i--){
286 _undoredo.pop_back();
289 CutModelSaveBinInfo* cutmodel = new CutModelSaveBinInfo(idc, _currentaction, type, _path);
290 if(type == CUTMODEL_CUT){
291 cutmodel->saveMatrix4x4(this->getCutModelData(idc)->getCurrentMatrix()->GetMatrix());
292 cutmodel->setCurrentShape(this->getCutModelData(idc)->getCurrentShape());
295 _undoredo.push_back(cutmodel);
297 _currentaction++;// = _undoredo.size();
298 //std::cout<<"current index "<<_currentaction;
303 int CutModelManager::Undo() throw( CutModelException){
304 //&& _currentaction < _undoredo.size()
305 if(_currentaction > 0){
306 int tempaction = _currentaction-1;
307 CutModelSaveBinInfo* currentundo = _undoredo[tempaction];
308 CutModelData* currentmodel;
310 if(currentundo->getActionType()==CUTMODEL_CUT){
312 vtkTransform* transform = currentundo->getTransformFromMatrixFile();
314 currentmodel = getCutModelData(currentundo->getId());
316 currentmodel->setTransform(transform, _img2);
318 currentmodel->setCurrentShape(currentundo->getCurrentShape());
320 currentmodel->ExecuteUnCut(currentundo->getIsInside(), _img, _img2);
330 int CutModelManager::Redo() throw( CutModelException){
332 if(_currentaction >= 0 && _currentaction < (int)_undoredo.size()){
334 CutModelSaveBinInfo* currentundo = _undoredo[_currentaction];
335 CutModelData* currentmodel;
337 if(currentundo->getActionType()==CUTMODEL_CUT){
339 vtkTransform* transform = currentundo->getTransformFromMatrixFile();
340 currentmodel = getCutModelData(currentundo->getId());
341 currentmodel->setTransform(transform, _img2);
342 currentmodel->setCurrentShape(currentundo->getCurrentShape());
343 currentmodel->ExecuteCut(currentundo->getRange(), currentundo->getIsInside(), _img2);
353 void CutModelManager::ParallelProjectionOn(){
354 _render->GetActiveCamera()->ParallelProjectionOn();
357 void CutModelManager::ParallelProjectionOff(){
358 _render->GetActiveCamera()->ParallelProjectionOff();
363 void CutModelManager::UpdatePolygon(bool mode)
367 cutterstyle->Delete();
368 cutterstyle = vtkInteractorStyleCutter::New();
370 cutterstyle->SetInteractor ( _interactor );
371 _interactor ->SetInteractorStyle( cutterstyle );
375 cutterstyle->VisibilityOff();
377 loop = vtkImplicitSelectionLoop::New();
378 mapper = vtkPolyDataMapper::New();
379 if(cutterstyle->Finished())
383 _render->RemoveActor(actor3D);
386 loop->SetLoop( cutterstyle->GetLoopPoints() );
387 loop->SetNormal( cutterstyle->GetDirection());
391 int numPoints = cutterstyle->GetLoopPoints()->GetNumberOfPoints();
392 contourDirection = cutterstyle->GetDirection();
394 contourPoints = cutterstyle->GetLoopPoints();
396 _polygonCutter = new CutModelPolygon();
398 cout<<"RaC Printing points......"<<endl;
399 for(int t=0;t<numPoints;t++)
402 cutterstyle->GetLoopPoints()->GetPoint(t,point);
403 cout<<"Initial Point:"<<t<<" XX:"<<point[0]<<" YY:"<<point[1]<<" ZZ:"<<point[2]<<endl;
407 sample = vtkSampleFunction::New();
409 sample->SetImplicitFunction(loop);
412 contour = vtkContourFilter::New();
413 //EED 2017-01-01 Migration VTK7
414 #if VTK_MAJOR_VERSION <= 5
415 contour->SetInput((vtkDataObject *)sample->GetOutput());
417 contour->SetInputData((vtkDataObject *)sample->GetOutput());
419 contour->SetValue(0,1);
421 actor = contour->GetOutput();
423 actor3D = vtkActor::New();
424 //EED 2017-01-01 Migration VTK7
425 #if VTK_MAJOR_VERSION <= 5
426 mapper->SetInput(actor);
428 mapper->SetInputData(actor);
430 mapper->SetScalarModeToUseCellData();
431 actor3D->SetMapper( mapper);
432 _render->AddActor(actor3D);
435 interactorstyle->SetInteractor ( _interactor );
436 _interactor->SetInteractorStyle( interactorstyle );
439 _interactor->Render();
440 _render->ResetCameraClippingRange();
442 void CutModelManager::ExecuteCutPolygon(bool inOutCut){
444 _polygonCutter->setInImage(_img2);
445 _polygonCutter->setPoints(contourPoints);
446 _polygonCutter->setDirection(contourDirection);
447 _polygonCutter->processOutImage(inOutCut);
450 void CutModelManager::InitializePolygonInteractorStyle(){
452 cutterstyle = vtkInteractorStyleCutter::New();
453 interactorstyle = vtkInteractorStyleTrackballCamera ::New();
454 interactorstyle->SetInteractor ( _interactor );
455 _interactor->SetInteractorStyle( interactorstyle );