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