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