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