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