]> Creatis software - creaContours.git/blob - lib/kernel_ManagerContour_NDimensions/KernelManagerContour.cxx
3edce32a41f4b4b81d45ad16beb41ba2bd6779b5
[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         inredo          = 0;
63         inundo          = 0;
64         time_t seconds;
65         seconds         = time (NULL);
66         int time        = seconds;
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
95 void KernelManagerContour::setVectImages(std::vector<vtkImageData*> vectimg)
96 {
97         double spc[3];
98         std::vector<double> vectspc;
99         for(int i = 0; i < (int)(vectimg.size()); i++)
100         {
101                 vtkImageData* img = vectimg[i];
102                 vtkImageChangeInformation* change = vtkImageChangeInformation::New();
103 //EED 2017-01-01 Migration VTK7
104 #if VTK_MAJOR_VERSION <= 5
105                 change->SetInformationInput(img);
106                 change->SetInputConnection(img->GetProducerPort());
107 #else
108                 change->SetInputData(img);
109 #endif
110                 img->GetSpacing(spc);
111                 change->SetOutputSpacing(1,1,1);
112 //EED 18 fev 2014
113                 change->SetOutputOrigin (0, 0, 0);
114                 change->Update();
115                 vectspc.clear();
116                 vectspc.push_back(spc[0]);
117                 vectspc.push_back(spc[1]);
118                 vectspc.push_back(spc[2]);
119                 vectimagesSpacing.push_back(vectspc);
120                 vectimg[i] = change->GetOutput();
121         } // for i
122         vectimages = vectimg;
123 }
124
125 void KernelManagerContour::GetSpacing(double *vecspc, int iImage)
126 {
127         vecspc[0]=vectimagesSpacing[iImage][0];
128         vecspc[1]=vectimagesSpacing[iImage][1];
129         vecspc[2]=vectimagesSpacing[iImage][2];
130 }
131
132 void KernelManagerContour::initializeEnvironment(std::string datadir)
133 {
134         _datadir = datadir;
135         std::string conceptsFN                  =  datadir+"holaConceptsFile.cf";
136         std::string imageSourcesFN              =  datadir+"holaImagesInstantsFile.of";
137         std::string imageSectionsFN             = "";
138         std::string axeThingsFN                 = "";
139
140         std::map<std::string, ImageSourceThing *> * sourcesMap          = new std::map<std::string, ImageSourceThing *>();
141         std::map<std::string, ImageSectionThing *>* sectionsMap         = new std::map<std::string, ImageSectionThing *>();
142         std::map<std::string, AxeThing *>* axesMap                                      = new std::map<std::string, AxeThing *>();
143         std::map<std::string, ContourThing *>* outlinesMap                      = new std::map<std::string, ContourThing *>();
144
145         for(int i = 0; i < (int)(vectimages.size()); i++)
146         {
147                 vtkImageData* selectedimage = vectimages[i];
148                 ImageSourceThing * thing                                                                = new ImageSourceThing(selectedimage);
149                 std::string imgstring = "Source Image "+intToString(i+1);
150                 sourcesMap->insert(std::pair<std::string, ImageSourceThing *>( imgstring, thing));
151         } // for
152
153         OutlineModelBuilder * _builder                                                          = new OutlineModelBuilder( conceptsFN ,datadir);
154         _builder->buildImageSource_Envornment( imageSourcesFN, sourcesMap );
155         _builder->buildImageSection_Envornment( imageSectionsFN, sectionsMap );
156         _builder->buildAxe_Envornment(axeThingsFN, axesMap );
157         _builder->buildCountour_Envornment( imageSectionsFN, outlinesMap );
158         //Creating the objects to manage
159         modelManager                            = new OutlineModelManager( _builder->getImSourceEnv(), _builder->getImSectionEnv(), _builder->getAxesEnv(),  _builder->getContourEnv() );
160         imageSource     = modelManager->getImageSourceThingByKeyName( "Source Image 1" );
161 }
162
163 vtkImageData* KernelManagerContour::getSourceImage()
164 {
165         return imageSource->getSourceImage();
166 }
167
168 OutlineModelManager* KernelManagerContour::getOutlineModelManager()
169 {
170         return modelManager;
171 }
172
173 std::string KernelManagerContour::createOutline(manualBaseModel * manModelContour,std::vector<int> instantVector)
174 {
175         return modelManager->createOutline( manModelContour, instantVector );
176 }
177
178 std::string KernelManagerContour::intToString(int num)
179 {
180         std::string result;
181         if(num == 0)
182         {
183                 result = "0";
184         }else{
185                 int k=num;
186                 while (k > 0)
187                 {
188                         char temp = k % 10 + 48;
189                         k = k / 10;
190                         result = temp + result;
191                 } // while
192         } // if num
193         return result;
194 }
195
196 std::vector<std::string> KernelManagerContour::GetLstNameThingsStatic()
197 {
198         return modelManager->GetLstNameThingsStatic();
199 }
200
201 void KernelManagerContour::SaveThingName(FILE* pFile, FILE *pFileData, std::string name )
202 {
203         modelManager->SaveThingName(pFile, pFileData, name);
204 }
205
206 std::vector<std::string> KernelManagerContour::GetLstNameThings()
207 {
208         return modelManager->GetLstNameThings();
209 }
210
211 //int KernelManagerContour::IsPartOfStaticList(std::string keyName ){
212 //      return modelManager->IsPartOfStaticList(keyName);
213 //}
214
215 bool KernelManagerContour::IsPartOfStaticList(std::string theKeyName)
216 {
217         return modelManager->IsPartOfStaticList(theKeyName) == -1;
218 }
219
220 void KernelManagerContour::deleteCModel(std::string theKeyName)
221 {
222         manualBaseModel* cModel         = modelManager->getOutlineByKeyName(theKeyName)->getModel();
223         modelManager->removeOutline( theKeyName );
224         delete cModel;
225 }
226
227 void KernelManagerContour::removeAllOutlines()
228 {
229         modelManager->removeAllOutlines();
230 }
231
232 std::vector<NameWrapper *> KernelManagerContour::getActualInstantOutlines()
233 {
234         return modelManager->getActualInstantOutlines();
235 }
236
237 int KernelManagerContour::getNamesWrappingSize()
238 {
239         return getActualInstantOutlines().size();
240 }
241
242 std::string KernelManagerContour::getNameWrapping(int i)
243 {
244         return getActualInstantOutlines()[i]->getKeyName();
245 }
246
247 void KernelManagerContour::setInstant(Instant * theInstant)
248 {
249         modelManager->setInstant(theInstant);
250         //_actualInstant = theInstant;
251 }
252
253 Instant * KernelManagerContour::getCurrentInstant()
254 {
255         return modelManager->getInstant();
256 }
257
258 void KernelManagerContour::setInstant(std::vector<int> vectInstant)
259 {
260         Instant* act = new Instant ( &vectInstant );
261         modelManager->setInstant(act);
262 }
263
264 std::string KernelManagerContour::createCopyContourOf ( std::string anExistingKName, std::vector<int> &instantNoTouchData)
265 {
266         return modelManager->createCopyContourOf(anExistingKName, instantNoTouchData);
267 }
268
269 manualBaseModel* KernelManagerContour::getOutlineByKeyName(std::string cloneName)
270 {
271         return modelManager->getOutlineByKeyName (cloneName )->getModel();
272 }
273
274 bool KernelManagerContour::onRedo(std::string& filename)
275 {
276         if(inredo > 0)
277         {
278                 inredo--;
279                 inundo++;
280                 std::string str = intToString(inundo);
281                 filename = stundoredo + str + ".roi";
282                 //loadState(temp);
283                 return true;
284         } // inredo
285         return false;
286 }
287
288 bool KernelManagerContour::onUndo(std::string& filename)
289 {
290         if(inundo>0)
291         {
292                 inredo++;
293                 inundo--;
294                 //char str[9000];
295                 //itoa(inundo, str, 10);
296                 std::string str = intToString(inundo);
297                 filename = stundoredo + str + ".roi";
298                 return true;//loadState(temp);
299         }
300         return false;
301 }
302
303 std::string KernelManagerContour::saveState()
304 {
305         inredo=0;
306         std::string str = intToString(inundo);
307         std::string temp = stundoredo + str + ".roi";
308         inundo++;
309         return temp;
310 }
311
312 bool KernelManagerContour::onUndoSaveFile(std::string& filename)
313 {
314         if(inundo>0)
315         {
316                 if(inredo==0)
317                 {
318                         //char str[9000];
319                         //itoa(inundo, str, 10);
320                         std::string str = intToString(inundo);
321                         filename = stundoredo + str + ".roi";
322                         return true;
323                         //saveFileWithContours(temp);
324                 }  // if inundo  ==0
325         }  // if inundo > 0
326         return false;
327 }
328
329 void KernelManagerContour :: changeContourOfManager(std::string keyName, Instant *instant)
330 {
331         modelManager->ChangeContourOfList(keyName, instant);
332 }
333
334 void KernelManagerContour ::resetAppend()
335 {
336     if (_contourPropagation!=NULL)
337         {
338                 _contourPropagation->resetAppend();
339         }
340 }
341
342
343 std::string KernelManagerContour::onSpreadAdd( std::vector<double> *vecX, std::vector<double> *vecY, std::vector<double> *vecZ, std::vector<int> instants)
344 {
345     if (_contourPropagation==NULL)
346     {
347                 _contourPropagation = new ContourPropagation();
348     }
349     if (vecX->size()!=0)
350         {
351         int i,size=vecZ->size();
352         int actualSlice = instants[1];
353         for ( i=0 ; i<size ; i++ )
354         {
355             (*vecZ)[i] = actualSlice;
356         } // for
357         _contourPropagation->appendContour(vecX , vecY , vecZ);
358         return intToString(actualSlice);
359     }
360         return "";
361 }
362
363 void KernelManagerContour::getMaxMinZ(double *minZ,double *maxZ)
364 {
365     if (_contourPropagation!=NULL)
366         {
367                 _contourPropagation->getMaxMinZ(minZ, maxZ);
368         }
369 }
370
371 void KernelManagerContour::CalculeSplinePropagation()
372 {
373         _contourPropagation->setInterpolationNumber(100);
374         _contourPropagation->CalculeSplinePropagation();
375 }
376
377 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)
378 {
379         bool addedModel = false;
380         manualBaseModel* manModelContour=NULL;
381         if (_contourPropagation->ifSliceKeyContourExist(z)==false)
382         {
383                 manModelContour = factoryManualContourModel( typeofcontour );
384                 int idTmp = _contourPropagation->FindIdWithZ(z);
385                 if (type==0) // Initial Points
386                 {
387                         _contourPropagation->GetInitialControlPoints( idTmp , vecCtrlPointX,vecCtrlPointY,vecCtrlPointZ);
388                 }
389                 if (type==1)  // Automatique Method
390                 {
391                         _contourPropagation->GetControlPoints( idTmp  ,vecCtrlPointX,vecCtrlPointY,vecCtrlPointZ);
392                 }
393                 if (type==2)  // sampling
394                 {
395                         _contourPropagation->GetControlPoints( idTmp , 20.0 ,vecCtrlPointX,vecCtrlPointY,vecCtrlPointZ);
396                 }
397 //--------------------------------------------------------------------
398                 int sizeCtrPt = vecCtrlPointX->size();
399                 for (int j=0 ; j<sizeCtrPt ; j++)
400                 {
401 //JSTG_16-07-08_----------------------------------------------------------------
402                         manModelContour->AddPoint( (*vecCtrlPointX)[j] , (*vecCtrlPointY)[j] , -900  );
403 //--------------------------------------------------------------------
404                 } // for j
405                 tempVector[1]=z;
406                 theName = modelManager->createOutline( manModelContour, tempVector );
407                 addedModel = theName.compare("") != 0;
408                 if(!addedModel)
409                 {
410                         manModelContour = NULL;
411                 }
412         }// ifSliceKeyContourExist
413         return manModelContour;
414 }
415
416 manualBaseModel *KernelManagerContour::factoryManualContourModel(int typeContour)
417 {
418         manualBaseModel *manModelContour=NULL;
419         // Creating the model
420         // NOTE: The view and the controler are created in the wxVtkBaseView_SceneManager class, configureViewControlTo method
421         // spline
422         if (typeContour==0)
423         {
424                 manModelContour = new manualContourModel();
425         }
426         // spline
427         if (typeContour==1)
428         {
429                 manModelContour = new manualContourModel();
430         }
431         // rectangle
432         if (typeContour==2)
433         {
434                 manModelContour = new manualContourModelRoi();
435         }
436         // circle
437         if (typeContour==3)
438         {
439                 manModelContour = new manualContourModelCircle();
440         }
441         // line
442         if (typeContour==6)
443         {
444                 manModelContour = new manualContourModelLine();
445         }
446         // points
447         if (typeContour==7)
448         {
449                 manModelContour = new manualBaseModel();
450         }
451         // polygon
452         if (typeContour==10)
453         {
454                 manModelContour = new manualContourModelPolygon();
455         }
456         return manModelContour;
457 }
458
459 std::vector<std::string> KernelManagerContour::getOutlinesNameAtInstant(std::vector<int> tempvector){
460         Instant instant(&tempvector);
461         std::vector<ContourThing**> vectcont = modelManager->getOutlinesAtInstant( &instant );
462         std::vector<std::string> vectname;
463         for(int i = 0; i < (int)(vectcont.size()); i++){
464                 ContourThing **contourthing = vectcont[i];
465                 vectname.push_back((*contourthing)->getName());
466         }
467     return vectname;
468 }
469
470 std::vector<ContourThing**> KernelManagerContour::getOutlinesAtInstant(Instant* instant ){
471         return modelManager->getOutlinesAtInstant(instant);
472 }
473
474 std::vector<manualBaseModel*> KernelManagerContour::ExploseEachModel( std::vector<manualBaseModel*> lstManConMod ){
475         std::vector<manualBaseModel*> lstTmp;
476         std::vector<manualBaseModel*> lstResult;
477         int j,jSize;
478         int i,iSize=lstManConMod.size();
479         for (i=0;i<iSize;i++)
480         {
481                 lstTmp = lstManConMod[i]->ExploseModel();
482                 jSize=lstTmp.size();
483                 for (j=0;j<jSize;j++)
484                 {
485                         lstResult.push_back( lstTmp[j] );
486                 }
487         }
488         return lstResult;
489 }
490
491 void KernelManagerContour::getConceptsInformation(std::vector<std::string>& conceptNameVect, std::vector<int>& conceptSizeVect){
492         modelManager-> getConceptsInformation(conceptNameVect, conceptSizeVect);
493 }
494
495
496 vtkImageData* KernelManagerContour::getImageAtInstant(std::vector<int> inst){
497         int index = inst[5]-1;
498
499         if(index < (int)(vectimages.size())&&index!=_currentIndex){
500                 _currentIndex=index;
501                 return vectimages[index];
502         }
503         return NULL;
504 }
505
506 std::string KernelManagerContour::getCurrentFileName(){
507         return filename;
508 }
509
510 void KernelManagerContour::setCurrentFileName(std::string filenam){
511         this->filename = filenam;
512 }
513
514 std::string KernelManagerContour::parseOsirixFile(std::string filename){
515
516
517 #ifdef ParserOsirix_BUILD
518         vtkImageData* sourceimage;
519         std::string xsdfile;
520
521         xsdfile = _datadir;
522
523         xsdfile.append("\\XML\\osirixschema.xsd");
524
525         sourceimage = getSourceImage();
526         OsirixParser p(xsdfile.c_str(), sourceimage->GetSpacing(), sourceimage->GetExtent());
527
528         if(p.ParseFile(filename.c_str())!= 0){
529
530         }
531
532         return p.getContoursFileName();
533 #else
534         return "";
535 #endif
536
537
538 }
539
540