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