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