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