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