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