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 # ------------------------------------------------------------------------ */
27 //----------------------------------------------------------------------------------------------------------------
28 // Class definition include
29 //----------------------------------------------------------------------------------------------------------------
30 #include "KernelManagerContour.h"
33 KernelManagerContour::KernelManagerContour()
35 /**THIS ALL SHOULD BE IN AN OTHER LIB CALLED PERSISTANCE
41 seconds = time (NULL);
43 stundoredo = "data/temp"+intToString(time);
45 _contourPropagation = NULL;
47 mkdir(stundoredo.c_str());
49 mkdir(stundoredo.c_str(),755);
51 stundoredo += "/cont";
57 KernelManagerContour::KernelManagerContour(std::vector<vtkImageData*> images,std::string datadir,std::string tmpdir)
59 /**THIS ALL SHOULD BE IN AN OTHER LIB CALLED PERSISTANCE
63 seconds = time (NULL);
67 stundoredo = tmpdir+"/temp"+intToString(time);
69 _contourPropagation = NULL;
71 mkdir(tmpdir.c_str());
72 mkdir(stundoredo.c_str());
74 mkdir(tmpdir.c_str(),755);
75 mkdir(stundoredo.c_str(),755);
77 stundoredo += "/cont";
81 setVectImages(images);
82 initializeEnvironment(datadir);
85 KernelManagerContour::~KernelManagerContour()
89 std::vector<vtkImageData*> KernelManagerContour::getVectImages()
94 void KernelManagerContour::changeImage(int id, vtkImageData *img)
96 if ( id<vectimages.size() )
101 std::vector<double> vectspc;
102 img->GetSpacing(spc);
103 vectspc.push_back(spc[0]);
104 vectspc.push_back(spc[1]);
105 vectspc.push_back(spc[2]);
106 vectimagesSpacing[id]=vectspc;
107 std::string imgstring = "Source Image "+intToString(id+1);
108 ImageSourceThing *imagesourcething = modelManager->getImageSourceThingByKeyName( imgstring );
109 imagesourcething->setSourceImage( img );
112 void KernelManagerContour::setVectImages(std::vector<vtkImageData*> vectimg)
115 std::vector<double> vectspc;
116 for(int i = 0; i < (int)(vectimg.size()); i++)
118 vtkImageData *img = vectimg[i];
119 vtkImageChangeInformation *change = vtkImageChangeInformation::New();
120 //EED 2017-01-01 Migration VTK7
121 #if VTK_MAJOR_VERSION <= 5
122 change->SetInformationInput(img);
123 change->SetInputConnection(img->GetProducerPort());
125 change->SetInputData(img);
127 img->GetSpacing(spc);
128 // change->SetOutputSpacing(1,1,1);
130 change->SetOutputOrigin (0, 0, 0);
133 vectspc.push_back(spc[0]);
134 vectspc.push_back(spc[1]);
135 vectspc.push_back(spc[2]);
136 vectimagesSpacing.push_back(vectspc);
137 vectimg[i] = change->GetOutput();
139 vectimages = vectimg;
142 void KernelManagerContour::GetSpacing(double *vecspc, int iImage)
144 vecspc[0] = vectimagesSpacing[iImage][0];
145 vecspc[1] = vectimagesSpacing[iImage][1];
146 vecspc[2] = vectimagesSpacing[iImage][2];
149 void KernelManagerContour::initializeEnvironment(std::string datadir)
152 std::string conceptsFN = datadir+"holaConceptsFile.cf";
153 std::string imageSourcesFN = datadir+"holaImagesInstantsFile.of";
154 std::string imageSectionsFN = "";
155 std::string axeThingsFN = "";
156 std::map<std::string, ImageSourceThing *> *sourcesMap = new std::map<std::string, ImageSourceThing *>();
157 std::map<std::string, ImageSectionThing *> *sectionsMap = new std::map<std::string, ImageSectionThing *>();
158 std::map<std::string, AxeThing *> *axesMap = new std::map<std::string, AxeThing *>();
159 std::map<std::string, ContourThing *> *outlinesMap = new std::map<std::string, ContourThing *>();
160 for(int i = 0; i < (int)(vectimages.size()); i++)
162 vtkImageData *selectedimage = vectimages[i];
163 ImageSourceThing *thing = new ImageSourceThing(selectedimage);
164 std::string imgstring = "Source Image "+intToString(i+1);
165 sourcesMap->insert(std::pair<std::string, ImageSourceThing *>( imgstring, thing));
167 OutlineModelBuilder *_builder = new OutlineModelBuilder( conceptsFN ,datadir);
168 _builder->buildImageSource_Envornment( imageSourcesFN, sourcesMap );
169 _builder->buildImageSection_Envornment( imageSectionsFN, sectionsMap );
170 _builder->buildAxe_Envornment(axeThingsFN, axesMap );
171 _builder->buildCountour_Envornment( imageSectionsFN, outlinesMap );
172 //Creating the objects to manage
173 modelManager = new OutlineModelManager( _builder->getImSourceEnv(), _builder->getImSectionEnv(), _builder->getAxesEnv(), _builder->getContourEnv() );
174 imageSource = modelManager->getImageSourceThingByKeyName( "Source Image 1" );
177 vtkImageData* KernelManagerContour::getSourceImage()
179 return imageSource->getSourceImage();
182 OutlineModelManager* KernelManagerContour::getOutlineModelManager()
187 std::string KernelManagerContour::createOutline(manualBaseModel * manModelContour,std::vector<int> instantVector)
189 return modelManager->createOutline( manModelContour, instantVector );
192 std::string KernelManagerContour::intToString(int num)
202 char temp = k % 10 + 48;
204 result = temp + result;
210 std::vector<std::string> KernelManagerContour::GetLstNameThingsStatic()
212 return modelManager->GetLstNameThingsStatic();
215 void KernelManagerContour::SaveThingName(FILE* pFile, FILE *pFileData, std::string name )
217 modelManager->SaveThingName(pFile, pFileData, name);
220 std::vector<std::string> KernelManagerContour::GetLstNameThings()
222 return modelManager->GetLstNameThings();
225 //int KernelManagerContour::IsPartOfStaticList(std::string keyName ){
226 // return modelManager->IsPartOfStaticList(keyName);
229 bool KernelManagerContour::IsPartOfStaticList(std::string theKeyName)
231 return modelManager->IsPartOfStaticList(theKeyName) == -1;
234 void KernelManagerContour::deleteCModel(std::string theKeyName)
236 manualBaseModel* cModel = modelManager->getOutlineByKeyName(theKeyName)->getModel();
237 modelManager->removeOutline( theKeyName );
241 void KernelManagerContour::removeAllOutlines()
243 modelManager->removeAllOutlines();
246 std::vector<NameWrapper *> KernelManagerContour::getActualInstantOutlines()
248 return modelManager->getActualInstantOutlines();
251 int KernelManagerContour::getNamesWrappingSize()
253 return getActualInstantOutlines().size();
256 std::string KernelManagerContour::getNameWrapping(int i)
258 return getActualInstantOutlines()[i]->getKeyName();
261 void KernelManagerContour::setInstant(Instant * theInstant)
263 modelManager->setInstant(theInstant);
264 //_actualInstant = theInstant;
267 Instant * KernelManagerContour::getCurrentInstant()
269 return modelManager->getInstant();
272 void KernelManagerContour::setInstant(std::vector<int> vectInstant)
274 Instant* act = new Instant ( &vectInstant );
275 modelManager->setInstant(act);
278 std::string KernelManagerContour::createCopyContourOf ( std::string anExistingKName, std::vector<int> &instantNoTouchData)
280 return modelManager->createCopyContourOf(anExistingKName, instantNoTouchData);
283 manualBaseModel* KernelManagerContour::getOutlineByKeyName(std::string cloneName)
285 return modelManager->getOutlineByKeyName (cloneName )->getModel();
288 bool KernelManagerContour::onRedo(std::string& filename)
294 std::string str = intToString(inundo);
295 filename = stundoredo + str + ".roi";
302 bool KernelManagerContour::onUndo(std::string& filename)
309 //itoa(inundo, str, 10);
310 std::string str = intToString(inundo);
311 filename = stundoredo + str + ".roi";
312 return true;//loadState(temp);
317 std::string KernelManagerContour::saveState()
320 std::string str = intToString(inundo);
321 std::string temp = stundoredo + str + ".roi";
326 bool KernelManagerContour::onUndoSaveFile(std::string& filename)
333 //itoa(inundo, str, 10);
334 std::string str = intToString(inundo);
335 filename = stundoredo + str + ".roi";
337 //saveFileWithContours(temp);
343 void KernelManagerContour :: changeContourOfManager(std::string keyName, Instant *instant)
345 modelManager->ChangeContourOfList(keyName, instant);
348 void KernelManagerContour ::resetAppend()
350 if (_contourPropagation!=NULL)
352 _contourPropagation->resetAppend();
357 std::string KernelManagerContour::onSpreadAdd( std::vector<double> *vecX, std::vector<double> *vecY, std::vector<double> *vecZ, std::vector<int> instants)
359 if (_contourPropagation==NULL)
361 _contourPropagation = new ContourPropagation();
365 int i,size=vecZ->size();
366 int actualSlice = instants[1];
367 for ( i=0 ; i<size ; i++ )
369 (*vecZ)[i] = actualSlice;
371 _contourPropagation->appendContour(vecX , vecY , vecZ);
372 return intToString(actualSlice);
377 void KernelManagerContour::getMaxMinZ(double *minZ,double *maxZ)
379 if (_contourPropagation!=NULL)
381 _contourPropagation->getMaxMinZ(minZ, maxZ);
385 void KernelManagerContour::CalculeSplinePropagation()
387 _contourPropagation->setInterpolationNumber(100);
388 _contourPropagation->CalculeSplinePropagation();
391 manualBaseModel* KernelManagerContour::GetPoints(int z,int type, std::vector<double>* vecCtrlPointX,std::vector<double>* vecCtrlPointY,std::vector<double>* vecCtrlPointZ, std::string& theName,int typeofcontour, std::vector<int> tempVector)
393 bool addedModel = false;
394 manualBaseModel* manModelContour=NULL;
395 if (_contourPropagation->ifSliceKeyContourExist(z)==false)
397 manModelContour = factoryManualContourModel( typeofcontour );
398 int idTmp = _contourPropagation->FindIdWithZ(z);
399 if (type==0) // Initial Points
401 _contourPropagation->GetInitialControlPoints( idTmp , vecCtrlPointX,vecCtrlPointY,vecCtrlPointZ);
403 if (type==1) // Automatique Method
405 _contourPropagation->GetControlPoints( idTmp ,vecCtrlPointX,vecCtrlPointY,vecCtrlPointZ);
407 if (type==2) // sampling
409 _contourPropagation->GetControlPoints( idTmp , 20.0 ,vecCtrlPointX,vecCtrlPointY,vecCtrlPointZ);
411 //--------------------------------------------------------------------
412 int sizeCtrPt = vecCtrlPointX->size();
413 for (int j=0 ; j<sizeCtrPt ; j++)
415 //JSTG_16-07-08_----------------------------------------------------------------
416 manModelContour->AddPoint( (*vecCtrlPointX)[j] , (*vecCtrlPointY)[j] , -900 );
417 //--------------------------------------------------------------------
420 theName = modelManager->createOutline( manModelContour, tempVector );
421 addedModel = theName.compare("") != 0;
424 manModelContour = NULL;
426 }// ifSliceKeyContourExist
427 return manModelContour;
430 manualBaseModel *KernelManagerContour::factoryManualContourModel(int typeContour)
432 manualBaseModel *manModelContour=NULL;
433 // Creating the model
434 // NOTE: The view and the controler are created in the wxVtkBaseView_SceneManager class, configureViewControlTo method
438 manModelContour = new manualContourModel();
443 manModelContour = new manualContourModel();
448 manModelContour = new manualContourModelRoi();
453 manModelContour = new manualContourModelCircle();
458 manModelContour = new manualContourModelLine();
463 manModelContour = new manualBaseModel();
468 manModelContour = new manualContourModelPolygon();
470 return manModelContour;
473 std::vector<std::string> KernelManagerContour::getOutlinesNameAtInstant(std::vector<int> tempvector){
474 Instant instant(&tempvector);
475 std::vector<ContourThing**> vectcont = modelManager->getOutlinesAtInstant( &instant );
476 std::vector<std::string> vectname;
477 for(int i = 0; i < (int)(vectcont.size()); i++){
478 ContourThing **contourthing = vectcont[i];
479 vectname.push_back((*contourthing)->getName());
484 std::vector<ContourThing**> KernelManagerContour::getOutlinesAtInstant(Instant* instant ){
485 return modelManager->getOutlinesAtInstant(instant);
488 std::vector<manualBaseModel*> KernelManagerContour::ExploseEachModel( std::vector<manualBaseModel*> lstManConMod ){
489 std::vector<manualBaseModel*> lstTmp;
490 std::vector<manualBaseModel*> lstResult;
492 int i,iSize=lstManConMod.size();
493 for (i=0;i<iSize;i++)
495 lstTmp = lstManConMod[i]->ExploseModel();
497 for (j=0;j<jSize;j++)
499 lstResult.push_back( lstTmp[j] );
505 void KernelManagerContour::getConceptsInformation(std::vector<std::string>& conceptNameVect, std::vector<int>& conceptSizeVect){
506 modelManager-> getConceptsInformation(conceptNameVect, conceptSizeVect);
509 vtkImageData* KernelManagerContour::getImageAtInstant(std::vector<int> inst)
511 int index = inst[5]-1;
512 if(index < (int)(vectimages.size())&&index!=_currentIndex)
514 _currentIndex = index;
515 return vectimages[index];
520 std::string KernelManagerContour::getCurrentFileName()
525 void KernelManagerContour::setCurrentFileName(std::string filenam)
527 this->filename = filenam;
530 std::string KernelManagerContour::parseOsirixFile(std::string filename)
532 #ifdef ParserOsirix_BUILD
533 vtkImageData* sourceimage;
536 xsdfile.append("\\XML\\osirixschema.xsd");
537 sourceimage = getSourceImage();
538 OsirixParser p(xsdfile.c_str(), sourceimage->GetSpacing(), sourceimage->GetExtent());
539 if(p.ParseFile(filename.c_str())!= 0)
542 return p.getContoursFileName();