]> 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         return modelManager->createCopyContourOf(anExistingKName, instantNoTouchData);
205 }
206 manualContourModel* KernelManagerContour::getOutlineByKeyName(std::string cloneName){
207         return modelManager->getOutlineByKeyName (cloneName )->getModel();
208 }
209
210 bool KernelManagerContour::onRedo(std::string& filename){
211         if(inredo > 0){
212                 inredo--;
213                 inundo++;
214                 std::string str = intToString(inundo);        
215                 filename = stundoredo + str + ".roi";   
216                 //loadState(temp);              
217                 return true;
218         }
219         return false;
220 }
221
222 bool KernelManagerContour::onUndo(std::string& filename){
223         if(inundo>0){           
224                 inredo++;
225                 inundo--;
226
227                 //char str[9000];
228                 //itoa(inundo, str, 10);
229                 std::string str = intToString(inundo);
230
231         
232                 filename = stundoredo + str + ".roi";
233         
234                 return true;//loadState(temp);
235         }
236         return false;
237 }
238
239 std::string KernelManagerContour::saveState(){
240         inredo=0;
241         std::string str = intToString(inundo);
242         std::string temp = stundoredo + str + ".roi";
243         inundo++;
244         return temp;
245 }
246
247 bool KernelManagerContour::onUndoSaveFile(std::string& filename){
248         if(inundo>0){
249                 if(inredo==0){
250
251                         //char str[9000];
252                         //itoa(inundo, str, 10);
253                         std::string str = intToString(inundo);
254
255                         filename = stundoredo + str + ".roi";
256                         return true;
257                         //saveFileWithContours(temp);                           
258
259                 }
260         }
261         return false;
262 }
263
264 void KernelManagerContour :: changeContourOfManager(std::string keyName, Instant *instant)
265 {
266         modelManager->ChangeContourOfList(keyName, instant);
267 }
268
269 void KernelManagerContour ::resetAppend(){
270         if(isInitContourPropagation()){
271                 _contourPropagation->resetAppend();
272         }       
273 }
274
275 bool KernelManagerContour::isInitContourPropagation(){
276         if(_contourPropagation == NULL){
277                 _contourPropagation = new ContourPropagation();
278         }else{
279                 return true;
280         }
281         return false;
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         if(isInitContourPropagation()){
287                 if (vecX->size()!=0){
288                         
289                         int i,size=vecZ->size();
290                         int actualSlice = instants[1];
291                         for ( i=0 ; i<size ; i++ )
292                         {
293                                 (*vecZ)[i] = actualSlice;
294                         } // for
295
296                         _contourPropagation->appendContour(vecX , vecY , vecZ);
297                         return intToString(actualSlice);                
298                 }
299         }
300         return "";
301 }
302
303 void KernelManagerContour::getMaxMinZ(double *minZ,double *maxZ){
304         if(isInitContourPropagation()){
305                 _contourPropagation->getMaxMinZ(minZ, maxZ);
306         }
307 }
308 void KernelManagerContour::CalculeSplinePropagation(){
309         _contourPropagation->setInterpolationNumber(100);
310         _contourPropagation->CalculeSplinePropagation();
311 }
312
313 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){
314     
315         
316         bool addedModel = false;
317         manualContourModel* manModelContour=NULL;
318
319         if (_contourPropagation->ifSliceKeyContourExist(z)==false){
320
321                 manModelContour = factoryManualContourModel( typeofcontour );
322
323                 int idTmp = _contourPropagation->FindIdWithZ(z);
324
325                 if (type==0) // Initial Points
326                 {   
327                         _contourPropagation->GetInitialControlPoints( idTmp , vecCtrlPointX,vecCtrlPointY,vecCtrlPointZ);
328                 } 
329                 if (type==1)  // Automatique Method
330                 {
331                         _contourPropagation->GetControlPoints( idTmp  ,vecCtrlPointX,vecCtrlPointY,vecCtrlPointZ);
332                 }
333                 if (type==2)  // sampling
334                 {
335                         _contourPropagation->GetControlPoints( idTmp , 20.0 ,vecCtrlPointX,vecCtrlPointY,vecCtrlPointZ);
336                 }
337 //--------------------------------------------------------------------
338                 int sizeCtrPt = vecCtrlPointX->size();
339                 for (int j=0 ; j<sizeCtrPt ; j++)
340                 {
341 //JSTG_16-07-08_----------------------------------------------------------------
342                         manModelContour->AddPoint( (*vecCtrlPointX)[j] , (*vecCtrlPointY)[j] , -900  );
343 //--------------------------------------------------------------------
344                 } // for j
345
346                 tempVector[1]=z;
347                 theName = modelManager->createOutline( manModelContour, tempVector );
348                 addedModel = theName.compare("") != 0;
349                 if(!addedModel){
350                         manModelContour = NULL;
351                 }
352                 
353         }// ifSliceKeyContourExist
354         return manModelContour;
355
356 }
357
358 manualContourModel * KernelManagerContour::factoryManualContourModel(int typeContour)
359 {       
360         manualContourModel *manModelContour=NULL;
361
362         // spline
363         if (typeContour==0)
364         {
365                 manModelContour = new manualContourModel();
366         }
367
368         // spline
369         if (typeContour==1)
370         {
371                 manModelContour = new manualContourModel();
372         }
373
374         // rectangle
375         if (typeContour==2)
376         {
377                 manModelContour = new manualContourModelRoi();
378         }
379
380         // circle
381         if (typeContour==3)
382         {
383                 manModelContour = new manualContourModelCircle();
384         }
385
386         // line
387         if (typeContour==6)
388         {
389                 manModelContour = new manualContourModelLine();
390         }
391
392         return manModelContour;
393 }
394
395 std::vector<std::string> KernelManagerContour::getOutlinesNameAtInstant(std::vector<int> tempvector){
396         Instant instant(&tempvector);
397         std::vector<ContourThing**> vectcont = modelManager->getOutlinesAtInstant( &instant );
398         std::vector<std::string> vectname;
399         for(int i = 0; i < vectcont.size(); i++){
400                 ContourThing **contourthing = vectcont[i];
401                 vectname.push_back((*contourthing)->getName());
402         }
403     return vectname;    
404 }
405
406 std::vector<ContourThing**> KernelManagerContour::getOutlinesAtInstant(Instant* instant ){
407         return modelManager->getOutlinesAtInstant(instant);
408 }
409
410 std::vector<manualContourModel*> KernelManagerContour::ExploseEachModel( std::vector<manualContourModel*> lstManConMod ){
411         std::vector<manualContourModel*> lstTmp;
412         std::vector<manualContourModel*> lstResult;
413         int j,jSize;
414         int i,iSize=lstManConMod.size();
415         for (i=0;i<iSize;i++)
416         {
417                 lstTmp = lstManConMod[i]->ExploseModel();
418                 jSize=lstTmp.size();
419                 for (j=0;j<jSize;j++)
420                 {
421                         lstResult.push_back( lstTmp[j] );
422                 }
423         }
424         return lstResult;
425 }
426
427 void KernelManagerContour::getConceptsInformation(std::vector<std::string>& conceptNameVect, std::vector<int>& conceptSizeVect){
428         modelManager-> getConceptsInformation(conceptNameVect, conceptSizeVect);
429 }
430
431
432 vtkImageData* KernelManagerContour::getImageAtInstant(std::vector<int> inst){
433         int index = inst[5]-1;
434
435         if(index < vectimages.size()&&index!=_currentIndex){
436                 _currentIndex=index;
437                 return vectimages[index];
438         }
439         return NULL;
440 }
441
442 std::string KernelManagerContour::getCurrentFileName(){
443         return filename;
444 }
445
446 void KernelManagerContour::setCurrentFileName(std::string filenam){
447         this->filename = filenam;
448 }