]> Creatis software - creaContours.git/blob - lib/kernel_ManagerContour_NDimensions/KernelManagerContour.cxx
Parser Osirix
[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 manualBaseModel* KernelManagerContour::getOutlineByKeyName(std::string cloneName){
212         return modelManager->getOutlineByKeyName (cloneName )->getModel();
213 }
214
215 bool KernelManagerContour::onRedo(std::string& filename){
216         if(inredo > 0){
217                 inredo--;
218                 inundo++;
219                 std::string str = intToString(inundo);        
220                 filename = stundoredo + str + ".roi";   
221                 //loadState(temp);              
222                 return true;
223         }
224         return false;
225 }
226
227 bool KernelManagerContour::onUndo(std::string& filename){
228         if(inundo>0){           
229                 inredo++;
230                 inundo--;
231
232                 //char str[9000];
233                 //itoa(inundo, str, 10);
234                 std::string str = intToString(inundo);
235
236         
237                 filename = stundoredo + str + ".roi";
238         
239                 return true;//loadState(temp);
240         }
241         return false;
242 }
243
244 std::string KernelManagerContour::saveState(){
245         inredo=0;
246         std::string str = intToString(inundo);
247         std::string temp = stundoredo + str + ".roi";
248         inundo++;
249         return temp;
250 }
251
252 bool KernelManagerContour::onUndoSaveFile(std::string& filename){
253         if(inundo>0){
254                 if(inredo==0){
255
256                         //char str[9000];
257                         //itoa(inundo, str, 10);
258                         std::string str = intToString(inundo);
259
260                         filename = stundoredo + str + ".roi";
261                         return true;
262                         //saveFileWithContours(temp);                           
263
264                 }
265         }
266         return false;
267 }
268
269 void KernelManagerContour :: changeContourOfManager(std::string keyName, Instant *instant)
270 {
271         modelManager->ChangeContourOfList(keyName, instant);
272 }
273
274 void KernelManagerContour ::resetAppend(){
275         if(isInitContourPropagation()){
276                 _contourPropagation->resetAppend();
277         }       
278 }
279
280 bool KernelManagerContour::isInitContourPropagation(){
281         if(_contourPropagation == NULL){
282                 _contourPropagation = new ContourPropagation();
283         }else{
284                 return true;
285         }
286         return false;
287
288 }
289
290 std::string KernelManagerContour::onSpreadAdd( std::vector<double> *vecX, std::vector<double> *vecY, std::vector<double> *vecZ, std::vector<int> instants){
291         if(isInitContourPropagation()){
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         }
305         return "";
306 }
307
308 void KernelManagerContour::getMaxMinZ(double *minZ,double *maxZ){
309         if(isInitContourPropagation()){
310                 _contourPropagation->getMaxMinZ(minZ, maxZ);
311         }
312 }
313 void KernelManagerContour::CalculeSplinePropagation(){
314         _contourPropagation->setInterpolationNumber(100);
315         _contourPropagation->CalculeSplinePropagation();
316 }
317
318 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){
319     
320         
321         bool addedModel = false;
322         manualBaseModel* manModelContour=NULL;
323
324         if (_contourPropagation->ifSliceKeyContourExist(z)==false){
325
326                 manModelContour = factoryManualContourModel( typeofcontour );
327
328                 int idTmp = _contourPropagation->FindIdWithZ(z);
329
330                 if (type==0) // Initial Points
331                 {   
332                         _contourPropagation->GetInitialControlPoints( idTmp , vecCtrlPointX,vecCtrlPointY,vecCtrlPointZ);
333                 } 
334                 if (type==1)  // Automatique Method
335                 {
336                         _contourPropagation->GetControlPoints( idTmp  ,vecCtrlPointX,vecCtrlPointY,vecCtrlPointZ);
337                 }
338                 if (type==2)  // sampling
339                 {
340                         _contourPropagation->GetControlPoints( idTmp , 20.0 ,vecCtrlPointX,vecCtrlPointY,vecCtrlPointZ);
341                 }
342 //--------------------------------------------------------------------
343                 int sizeCtrPt = vecCtrlPointX->size();
344                 for (int j=0 ; j<sizeCtrPt ; j++)
345                 {
346 //JSTG_16-07-08_----------------------------------------------------------------
347                         manModelContour->AddPoint( (*vecCtrlPointX)[j] , (*vecCtrlPointY)[j] , 900  );
348 //--------------------------------------------------------------------
349                 } // for j
350
351                 tempVector[1]=z;
352                 theName = modelManager->createOutline( manModelContour, tempVector );
353                 addedModel = theName.compare("") != 0;
354                 if(!addedModel){
355                         manModelContour = NULL;
356                 }
357                 
358         }// ifSliceKeyContourExist
359         return manModelContour;
360 }
361
362 manualBaseModel * KernelManagerContour::factoryManualContourModel(int typeContour)
363 {       
364         manualBaseModel *manModelContour=NULL;
365
366         // Creating the model
367         // NOTE: The view and the controler are created in the wxVtkBaseView_SceneManager class, configureViewControlTo method
368
369         // spline
370         if (typeContour==0)
371         {
372                 manModelContour = new manualContourModel();
373         }
374
375         // spline
376         if (typeContour==1)
377         {
378                 manModelContour = new manualContourModel();
379         }
380
381         // rectangle
382         if (typeContour==2)
383         {
384                 manModelContour = new manualContourModelRoi();
385         }
386
387         // circle
388         if (typeContour==3)
389         {
390                 manModelContour = new manualContourModelCircle();
391         }
392
393         // line
394         if (typeContour==6)
395         {
396                 manModelContour = new manualContourModelLine();
397         }
398         
399         // points
400         if (typeContour==7)
401         {
402                 manModelContour = new manualBaseModel();
403         }
404
405         // polygon
406         if (typeContour==10)
407         {
408                 manModelContour = new manualContourModelPolygon();
409         }
410
411         return manModelContour;
412 }
413
414 std::vector<std::string> KernelManagerContour::getOutlinesNameAtInstant(std::vector<int> tempvector){
415         Instant instant(&tempvector);
416         std::vector<ContourThing**> vectcont = modelManager->getOutlinesAtInstant( &instant );
417         std::vector<std::string> vectname;
418         for(int i = 0; i < (int)(vectcont.size()); i++){
419                 ContourThing **contourthing = vectcont[i];
420                 vectname.push_back((*contourthing)->getName());
421         }
422     return vectname;    
423 }
424
425 std::vector<ContourThing**> KernelManagerContour::getOutlinesAtInstant(Instant* instant ){
426         return modelManager->getOutlinesAtInstant(instant);
427 }
428
429 std::vector<manualBaseModel*> KernelManagerContour::ExploseEachModel( std::vector<manualBaseModel*> lstManConMod ){
430         std::vector<manualBaseModel*> lstTmp;
431         std::vector<manualBaseModel*> lstResult;
432         int j,jSize;
433         int i,iSize=lstManConMod.size();
434         for (i=0;i<iSize;i++)
435         {
436                 lstTmp = lstManConMod[i]->ExploseModel();
437                 jSize=lstTmp.size();
438                 for (j=0;j<jSize;j++)
439                 {
440                         lstResult.push_back( lstTmp[j] );
441                 }
442         }
443         return lstResult;
444 }
445
446 void KernelManagerContour::getConceptsInformation(std::vector<std::string>& conceptNameVect, std::vector<int>& conceptSizeVect){
447         modelManager-> getConceptsInformation(conceptNameVect, conceptSizeVect);
448 }
449
450
451 vtkImageData* KernelManagerContour::getImageAtInstant(std::vector<int> inst){
452         int index = inst[5]-1;
453
454         if(index < (int)(vectimages.size())&&index!=_currentIndex){
455                 _currentIndex=index;
456                 return vectimages[index];
457         }
458         return NULL;
459 }
460
461 std::string KernelManagerContour::getCurrentFileName(){
462         return filename;
463 }
464
465 void KernelManagerContour::setCurrentFileName(std::string filenam){
466         this->filename = filenam;
467 }
468
469 std::string KernelManagerContour::parseOsirixFile(std::string filename){
470         
471
472 #ifdef ParserOsirix_BUILD
473         vtkImageData* sourceimage;
474         std::string xsdfile;
475
476         xsdfile = _datadir;
477
478         xsdfile.append("\\XML\\osirixschema.xsd");
479
480         sourceimage = getSourceImage();
481         OsirixParser p(xsdfile.c_str(), sourceimage->GetSpacing(), sourceimage->GetExtent());
482
483         if(p.ParseFile(filename.c_str())!= 0){
484
485         }
486
487         return p.getContoursFileName();
488 #else
489         return "";
490 #endif  
491
492
493 }
494
495