]> Creatis software - creaContours.git/blob - lib/kernel_ManagerContour_NDimensions/KernelManagerContour.cxx
#3383 NDimension changeImage
[creaContours.git] / lib / kernel_ManagerContour_NDimensions / KernelManagerContour.cxx
1 /*# ---------------------------------------------------------------------
2 #
3 # Copyright (c) CREATIS (Centre de Recherche en Acquisition et Traitement de l'Image
4 #                        pour la Sant�)
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
8 #
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.
15 #
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
20 #  liability.
21 #
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 # ------------------------------------------------------------------------ */
25
26
27 //----------------------------------------------------------------------------------------------------------------
28 // Class definition include
29 //----------------------------------------------------------------------------------------------------------------
30 #include "KernelManagerContour.h"
31
32
33 KernelManagerContour::KernelManagerContour()
34 {
35 /**THIS ALL SHOULD BE IN AN OTHER LIB CALLED PERSISTANCE
36 **
37 **/
38         inredo                          = 0;
39         inundo                          = 0;
40         time_t seconds;
41         seconds                         = time (NULL);
42         int time                        = seconds;
43         stundoredo                      = "data/temp"+intToString(time);
44         _currentIndex           = 0;
45         _contourPropagation = NULL;
46 #if(WIN32)
47                 mkdir(stundoredo.c_str());
48 #else
49                 mkdir(stundoredo.c_str(),755);
50 #endif
51         stundoredo += "/cont";
52 /**
53 **  FINISH PERSISTANCE
54 **/
55 }
56
57 KernelManagerContour::KernelManagerContour(std::vector<vtkImageData*> images,std::string datadir,std::string tmpdir)
58 {
59 /**THIS ALL SHOULD BE IN AN OTHER LIB CALLED PERSISTANCE
60 **
61 **/
62     time_t  seconds;
63     seconds     = time (NULL);
64     int time    = seconds;
65         inredo          = 0;
66         inundo          = 0;
67         stundoredo  = tmpdir+"/temp"+intToString(time);
68         _currentIndex = 0;
69         _contourPropagation = NULL;
70 #if(WIN32)
71         mkdir(tmpdir.c_str());
72         mkdir(stundoredo.c_str());
73 #else
74         mkdir(tmpdir.c_str(),755);
75         mkdir(stundoredo.c_str(),755);
76 #endif
77         stundoredo += "/cont";
78 /**
79 **  FINISH PERSISTANCE
80 **/
81         setVectImages(images);        
82         initializeEnvironment(datadir);
83 }
84
85 KernelManagerContour::~KernelManagerContour()
86 {
87 }
88
89 std::vector<vtkImageData*> KernelManagerContour::getVectImages()
90 {
91         return vectimages;
92 }
93
94 void KernelManagerContour::changeImage(int id, vtkImageData *img)
95 {
96     if ( id<vectimages.size() )
97     {
98         vectimages[id]=img;
99     }
100     double spc[3];
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 );
110 }
111
112 void KernelManagerContour::setVectImages(std::vector<vtkImageData*> vectimg)
113 {
114         double spc[3];
115         std::vector<double> vectspc;
116         for(int i = 0; i < (int)(vectimg.size()); i++)
117         {
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());
124 #else
125                 change->SetInputData(img);
126 #endif
127                 img->GetSpacing(spc);
128 //              change->SetOutputSpacing(1,1,1);
129 //EED 18 fev 2014
130                 change->SetOutputOrigin (0, 0, 0);
131                 change->Update();
132                 vectspc.clear();
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();
138         } // for i
139         vectimages = vectimg;
140 }
141
142 void KernelManagerContour::GetSpacing(double *vecspc, int iImage)
143 {
144         vecspc[0] = vectimagesSpacing[iImage][0];
145         vecspc[1] = vectimagesSpacing[iImage][1];
146         vecspc[2] = vectimagesSpacing[iImage][2];
147 }
148
149 void KernelManagerContour::initializeEnvironment(std::string datadir)
150 {
151                                                 _datadir            = 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++)
161         {
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));
166         } // for
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" );
175 }
176
177 vtkImageData* KernelManagerContour::getSourceImage()
178 {
179         return imageSource->getSourceImage();
180 }
181
182 OutlineModelManager* KernelManagerContour::getOutlineModelManager()
183 {
184         return modelManager;
185 }
186
187 std::string KernelManagerContour::createOutline(manualBaseModel * manModelContour,std::vector<int> instantVector)
188 {
189         return modelManager->createOutline( manModelContour, instantVector );
190 }
191
192 std::string KernelManagerContour::intToString(int num)
193 {
194         std::string result;
195         if(num == 0)
196         {
197                 result = "0";
198         }else{
199                 int k=num;
200                 while (k > 0)
201                 {
202                         char temp = k % 10 + 48;
203                         k = k / 10;
204                         result = temp + result;
205                 } // while
206         } // if num
207         return result;
208 }
209
210 std::vector<std::string> KernelManagerContour::GetLstNameThingsStatic()
211 {
212         return modelManager->GetLstNameThingsStatic();
213 }
214
215 void KernelManagerContour::SaveThingName(FILE* pFile, FILE *pFileData, std::string name )
216 {
217         modelManager->SaveThingName(pFile, pFileData, name);
218 }
219
220 std::vector<std::string> KernelManagerContour::GetLstNameThings()
221 {
222         return modelManager->GetLstNameThings();
223 }
224
225 //int KernelManagerContour::IsPartOfStaticList(std::string keyName ){
226 //      return modelManager->IsPartOfStaticList(keyName);
227 //}
228
229 bool KernelManagerContour::IsPartOfStaticList(std::string theKeyName)
230 {
231         return modelManager->IsPartOfStaticList(theKeyName) == -1;
232 }
233
234 void KernelManagerContour::deleteCModel(std::string theKeyName)
235 {
236         manualBaseModel* cModel         = modelManager->getOutlineByKeyName(theKeyName)->getModel();
237         modelManager->removeOutline( theKeyName );
238         delete cModel;
239 }
240
241 void KernelManagerContour::removeAllOutlines()
242 {
243         modelManager->removeAllOutlines();
244 }
245
246 std::vector<NameWrapper *> KernelManagerContour::getActualInstantOutlines()
247 {
248         return modelManager->getActualInstantOutlines();
249 }
250
251 int KernelManagerContour::getNamesWrappingSize()
252 {
253         return getActualInstantOutlines().size();
254 }
255
256 std::string KernelManagerContour::getNameWrapping(int i)
257 {
258         return getActualInstantOutlines()[i]->getKeyName();
259 }
260
261 void KernelManagerContour::setInstant(Instant * theInstant)
262 {
263         modelManager->setInstant(theInstant);
264         //_actualInstant = theInstant;
265 }
266
267 Instant * KernelManagerContour::getCurrentInstant()
268 {
269         return modelManager->getInstant();
270 }
271
272 void KernelManagerContour::setInstant(std::vector<int> vectInstant)
273 {
274         Instant* act = new Instant ( &vectInstant );
275         modelManager->setInstant(act);
276 }
277
278 std::string KernelManagerContour::createCopyContourOf ( std::string anExistingKName, std::vector<int> &instantNoTouchData)
279 {
280         return modelManager->createCopyContourOf(anExistingKName, instantNoTouchData);
281 }
282
283 manualBaseModel* KernelManagerContour::getOutlineByKeyName(std::string cloneName)
284 {
285         return modelManager->getOutlineByKeyName (cloneName )->getModel();
286 }
287
288 bool KernelManagerContour::onRedo(std::string& filename)
289 {
290         if(inredo > 0)
291         {
292                 inredo--;
293                 inundo++;
294                 std::string str = intToString(inundo);
295                 filename = stundoredo + str + ".roi";
296                 //loadState(temp);
297                 return true;
298         } // inredo
299         return false;
300 }
301
302 bool KernelManagerContour::onUndo(std::string& filename)
303 {
304         if(inundo>0)
305         {
306                 inredo++;
307                 inundo--;
308                 //char str[9000];
309                 //itoa(inundo, str, 10);
310                 std::string str = intToString(inundo);
311                 filename = stundoredo + str + ".roi";
312                 return true;//loadState(temp);
313         }
314         return false;
315 }
316
317 std::string KernelManagerContour::saveState()
318 {
319         inredo=0;
320         std::string str = intToString(inundo);
321         std::string temp = stundoredo + str + ".roi";
322         inundo++;
323         return temp;
324 }
325
326 bool KernelManagerContour::onUndoSaveFile(std::string& filename)
327 {
328         if(inundo>0)
329         {
330                 if(inredo==0)
331                 {
332                         //char str[9000];
333                         //itoa(inundo, str, 10);
334                         std::string str = intToString(inundo);
335                         filename = stundoredo + str + ".roi";
336                         return true;
337                         //saveFileWithContours(temp);
338                 }  // if inundo  ==0
339         }  // if inundo > 0
340         return false;
341 }
342
343 void KernelManagerContour :: changeContourOfManager(std::string keyName, Instant *instant)
344 {
345         modelManager->ChangeContourOfList(keyName, instant);
346 }
347
348 void KernelManagerContour ::resetAppend()
349 {
350     if (_contourPropagation!=NULL)
351         {
352                 _contourPropagation->resetAppend();
353         }
354 }
355
356
357 std::string KernelManagerContour::onSpreadAdd( std::vector<double> *vecX, std::vector<double> *vecY, std::vector<double> *vecZ, std::vector<int> instants)
358 {
359     if (_contourPropagation==NULL)
360     {
361                 _contourPropagation = new ContourPropagation();
362     }
363     if (vecX->size()!=0)
364         {
365         int i,size=vecZ->size();
366         int actualSlice = instants[1];
367         for ( i=0 ; i<size ; i++ )
368         {
369             (*vecZ)[i] = actualSlice;
370         } // for
371         _contourPropagation->appendContour(vecX , vecY , vecZ);
372         return intToString(actualSlice);
373     }
374         return "";
375 }
376
377 void KernelManagerContour::getMaxMinZ(double *minZ,double *maxZ)
378 {
379     if (_contourPropagation!=NULL)
380         {
381                 _contourPropagation->getMaxMinZ(minZ, maxZ);
382         }
383 }
384
385 void KernelManagerContour::CalculeSplinePropagation()
386 {
387         _contourPropagation->setInterpolationNumber(100);
388         _contourPropagation->CalculeSplinePropagation();
389 }
390
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)
392 {
393         bool addedModel = false;
394         manualBaseModel* manModelContour=NULL;
395         if (_contourPropagation->ifSliceKeyContourExist(z)==false)
396         {
397                 manModelContour = factoryManualContourModel( typeofcontour );
398                 int idTmp = _contourPropagation->FindIdWithZ(z);
399                 if (type==0) // Initial Points
400                 {
401                         _contourPropagation->GetInitialControlPoints( idTmp , vecCtrlPointX,vecCtrlPointY,vecCtrlPointZ);
402                 }
403                 if (type==1)  // Automatique Method
404                 {
405                         _contourPropagation->GetControlPoints( idTmp  ,vecCtrlPointX,vecCtrlPointY,vecCtrlPointZ);
406                 }
407                 if (type==2)  // sampling
408                 {
409                         _contourPropagation->GetControlPoints( idTmp , 20.0 ,vecCtrlPointX,vecCtrlPointY,vecCtrlPointZ);
410                 }
411 //--------------------------------------------------------------------
412                 int sizeCtrPt = vecCtrlPointX->size();
413                 for (int j=0 ; j<sizeCtrPt ; j++)
414                 {
415 //JSTG_16-07-08_----------------------------------------------------------------
416                         manModelContour->AddPoint( (*vecCtrlPointX)[j] , (*vecCtrlPointY)[j] , -900  );
417 //--------------------------------------------------------------------
418                 } // for j
419                 tempVector[1]=z;
420                 theName = modelManager->createOutline( manModelContour, tempVector );
421                 addedModel = theName.compare("") != 0;
422                 if(!addedModel)
423                 {
424                         manModelContour = NULL;
425                 }
426         }// ifSliceKeyContourExist
427         return manModelContour;
428 }
429
430 manualBaseModel *KernelManagerContour::factoryManualContourModel(int typeContour)
431 {
432         manualBaseModel *manModelContour=NULL;
433         // Creating the model
434         // NOTE: The view and the controler are created in the wxVtkBaseView_SceneManager class, configureViewControlTo method
435         // spline
436         if (typeContour==0)
437         {
438                 manModelContour = new manualContourModel();
439         }
440         // spline
441         if (typeContour==1)
442         {
443                 manModelContour = new manualContourModel();
444         }
445         // rectangle
446         if (typeContour==2)
447         {
448                 manModelContour = new manualContourModelRoi();
449         }
450         // circle
451         if (typeContour==3)
452         {
453                 manModelContour = new manualContourModelCircle();
454         }
455         // line
456         if (typeContour==6)
457         {
458                 manModelContour = new manualContourModelLine();
459         }
460         // points
461         if (typeContour==7)
462         {
463                 manModelContour = new manualBaseModel();
464         }
465         // polygon
466         if (typeContour==10)
467         {
468                 manModelContour = new manualContourModelPolygon();
469         }
470         return manModelContour;
471 }
472
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());
480         }
481     return vectname;
482 }
483
484 std::vector<ContourThing**> KernelManagerContour::getOutlinesAtInstant(Instant* instant ){
485         return modelManager->getOutlinesAtInstant(instant);
486 }
487
488 std::vector<manualBaseModel*> KernelManagerContour::ExploseEachModel( std::vector<manualBaseModel*> lstManConMod ){
489         std::vector<manualBaseModel*> lstTmp;
490         std::vector<manualBaseModel*> lstResult;
491         int j,jSize;
492         int i,iSize=lstManConMod.size();
493         for (i=0;i<iSize;i++)
494         {
495                 lstTmp = lstManConMod[i]->ExploseModel();
496                 jSize=lstTmp.size();
497                 for (j=0;j<jSize;j++)
498                 {
499                         lstResult.push_back( lstTmp[j] );
500                 }
501         }
502         return lstResult;
503 }
504
505 void KernelManagerContour::getConceptsInformation(std::vector<std::string>& conceptNameVect, std::vector<int>& conceptSizeVect){
506         modelManager-> getConceptsInformation(conceptNameVect, conceptSizeVect);
507 }
508
509 vtkImageData* KernelManagerContour::getImageAtInstant(std::vector<int> inst)
510 {
511         int index = inst[5]-1;
512         if(index < (int)(vectimages.size())&&index!=_currentIndex)
513     {
514                 _currentIndex = index;
515                 return vectimages[index];
516         }
517         return NULL;
518 }
519
520 std::string KernelManagerContour::getCurrentFileName()
521 {
522         return filename;
523 }
524
525 void KernelManagerContour::setCurrentFileName(std::string filenam)
526 {
527         this->filename = filenam;
528 }
529
530 std::string KernelManagerContour::parseOsirixFile(std::string filename)
531 {
532 #ifdef ParserOsirix_BUILD
533         vtkImageData* sourceimage;
534         std::string xsdfile;
535         xsdfile = _datadir;
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)
540     {
541         }
542         return p.getContoursFileName();
543 #else
544         return "";
545 #endif
546 }
547
548