]> Creatis software - creaContours.git/blob - lib/kernel_ManagerContour_NDimensions/KernelManagerContour.cxx
24307db8f8a38c16da8466286620a8f0d25c2dd2
[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 /**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 = tmpdir+"/temp"+intToString(time);
52         _currentIndex = 0;
53         
54         _contourPropagation = NULL;
55 #if(WIN32)
56         mkdir(tmpdir.c_str());
57         mkdir(stundoredo.c_str());
58 #else
59         mkdir(tmpdir.c_str(),755);
60         mkdir(stundoredo.c_str(),755);
61 #endif
62         stundoredo += "/cont";
63 /**
64 **  FINISH PERSISTANCE
65 **/
66         setVectImages(images);
67         initializeEnvironment(datadir);
68
69 }
70 KernelManagerContour::~KernelManagerContour(){
71 }       
72
73 std::vector<vtkImageData*> KernelManagerContour::getVectImages(){
74         return vectimages;
75 }
76 void KernelManagerContour::setVectImages(std::vector<vtkImageData*> vectimg){
77
78         for(int i = 0; i < vectimg.size(); i++){
79                 vtkImageData* img = vectimg[i];
80                 vtkImageChangeInformation* change = vtkImageChangeInformation::New();
81                 change->SetInformationInput(img);
82                 change->SetInputConnection(img->GetProducerPort());
83
84                 change->SetOutputSpacing(1,1,1);
85                 
86                 change->Update();
87
88                 vectimg[i] = change->GetOutput();
89         }
90
91         vectimages = vectimg;
92 }
93
94 void KernelManagerContour::initializeEnvironment(std::string datadir){
95         std::string conceptsFN                  =  datadir+"holaConceptsFile.cf";
96         std::string imageSourcesFN              =  datadir+"holaImagesInstantsFile.of";
97         std::string imageSectionsFN             = "";
98         std::string axeThingsFN                 = "";
99
100         std::map<std::string, ImageSourceThing *> * sourcesMap          = new std::map<std::string, ImageSourceThing *>();
101         std::map<std::string, ImageSectionThing *>* sectionsMap         = new std::map<std::string, ImageSectionThing *>();
102         std::map<std::string, AxeThing *>* axesMap                                      = new std::map<std::string, AxeThing *>();
103         std::map<std::string, ContourThing *>* outlinesMap                      = new std::map<std::string, ContourThing *>();  
104
105         for(int i = 0; i < vectimages.size(); i++){
106                 vtkImageData* selectedimage = vectimages[i];
107                 ImageSourceThing * thing                                                                        = new ImageSourceThing(selectedimage);
108                 std::string imgstring = "Source Image "+intToString(i+1);
109                 sourcesMap->insert(std::pair<std::string, ImageSourceThing *>( imgstring, thing));
110         }
111         
112
113         OutlineModelBuilder * _builder                                                          = new OutlineModelBuilder( conceptsFN ,datadir);
114         _builder->buildImageSource_Envornment( imageSourcesFN, sourcesMap );
115         _builder->buildImageSection_Envornment( imageSectionsFN, sectionsMap );         
116         _builder->buildAxe_Envornment(axeThingsFN, axesMap );
117         _builder->buildCountour_Envornment( imageSectionsFN, outlinesMap );
118
119         //Creating the objects to manage
120         modelManager                            = new OutlineModelManager( _builder->getImSourceEnv(), _builder->getImSectionEnv(), _builder->getAxesEnv(),  _builder->getContourEnv() );
121
122         imageSource     = modelManager->getImageSourceThingByKeyName( "Source Image 1" );
123
124 }
125 vtkImageData* KernelManagerContour::getSourceImage(){
126         return imageSource->getSourceImage();
127 }
128 OutlineModelManager* KernelManagerContour::getOutlineModelManager(){
129         return modelManager;
130 }
131
132 std::string KernelManagerContour::createOutline(manualContourModel * manModelContour,std::vector<int> instantVector){
133         return modelManager->createOutline( manModelContour, instantVector );
134 }
135
136 std::string KernelManagerContour::intToString(int num){
137         std::string result;
138         if(num == 0){
139                 result = "0";
140         }else{
141                 int k=num;
142                 while (k > 0){
143                         char temp = k % 10 + 48;
144                         k = k / 10;
145                         result = temp + result; 
146                 }
147         }
148         return result;
149 }
150
151 std::vector<std::string> KernelManagerContour::GetLstNameThingsStatic(){
152         return modelManager->GetLstNameThingsStatic();
153 }
154
155 void KernelManagerContour::SaveThingName(FILE* pFile, std::string name ){
156         modelManager->SaveThingName(pFile, name);
157 }
158 std::vector<std::string> KernelManagerContour::GetLstNameThings(){
159         return modelManager->GetLstNameThings();
160 }
161 //int KernelManagerContour::IsPartOfStaticList(std::string keyName ){
162 //      return modelManager->IsPartOfStaticList(keyName);
163 //}
164
165 bool KernelManagerContour::IsPartOfStaticList(std::string theKeyName){
166         return modelManager->IsPartOfStaticList(theKeyName) == -1;
167 }
168
169 void KernelManagerContour::deleteCModel(std::string theKeyName){
170         manualContourModel* cModel              = modelManager->getOutlineByKeyName(theKeyName)->getModel();
171         modelManager->removeOutline( theKeyName );
172
173         delete cModel;
174 }
175
176 void KernelManagerContour::removeAllOutlines(){
177         modelManager->removeAllOutlines();
178 }
179
180 std::vector<NameWrapper *> KernelManagerContour::getActualInstantOutlines(){
181         return modelManager->getActualInstantOutlines();
182 }
183
184 int KernelManagerContour::getNamesWrappingSize(){
185         return getActualInstantOutlines().size();
186 }
187
188 std::string KernelManagerContour::getNameWrapping(int i){
189         return getActualInstantOutlines()[i]->getKeyName();
190 }
191
192 void KernelManagerContour::setInstant(Instant * theInstant){
193         modelManager->setInstant(theInstant);
194         //_actualInstant = theInstant;
195 }
196
197 Instant * KernelManagerContour::getCurrentInstant(){
198         return modelManager->getInstant();
199 }
200 void KernelManagerContour::setInstant(std::vector<int> vectInstant){
201         Instant* act = new Instant ( &vectInstant );
202         modelManager->setInstant(act);
203 }
204
205 std::string KernelManagerContour::createCopyContourOf ( std::string anExistingKName, std::vector<int> &instantNoTouchData)
206 {
207         return modelManager->createCopyContourOf(anExistingKName, instantNoTouchData);
208 }
209 manualContourModel* KernelManagerContour::getOutlineByKeyName(std::string cloneName){
210         return modelManager->getOutlineByKeyName (cloneName )->getModel();
211 }
212
213 bool KernelManagerContour::onRedo(std::string& filename){
214         if(inredo > 0){
215                 inredo--;
216                 inundo++;
217                 std::string str = intToString(inundo);        
218                 filename = stundoredo + str + ".roi";   
219                 //loadState(temp);              
220                 return true;
221         }
222         return false;
223 }
224
225 bool KernelManagerContour::onUndo(std::string& filename){
226         if(inundo>0){           
227                 inredo++;
228                 inundo--;
229
230                 //char str[9000];
231                 //itoa(inundo, str, 10);
232                 std::string str = intToString(inundo);
233
234         
235                 filename = stundoredo + str + ".roi";
236         
237                 return true;//loadState(temp);
238         }
239         return false;
240 }
241
242 std::string KernelManagerContour::saveState(){
243         inredo=0;
244         std::string str = intToString(inundo);
245         std::string temp = stundoredo + str + ".roi";
246         inundo++;
247         return temp;
248 }
249
250 bool KernelManagerContour::onUndoSaveFile(std::string& filename){
251         if(inundo>0){
252                 if(inredo==0){
253
254                         //char str[9000];
255                         //itoa(inundo, str, 10);
256                         std::string str = intToString(inundo);
257
258                         filename = stundoredo + str + ".roi";
259                         return true;
260                         //saveFileWithContours(temp);                           
261
262                 }
263         }
264         return false;
265 }
266
267 void KernelManagerContour :: changeContourOfManager(std::string keyName, Instant *instant)
268 {
269         modelManager->ChangeContourOfList(keyName, instant);
270 }
271
272 void KernelManagerContour ::resetAppend(){
273         if(isInitContourPropagation()){
274                 _contourPropagation->resetAppend();
275         }       
276 }
277
278 bool KernelManagerContour::isInitContourPropagation(){
279         if(_contourPropagation == NULL){
280                 _contourPropagation = new ContourPropagation();
281         }else{
282                 return true;
283         }
284         return false;
285
286 }
287
288 std::string KernelManagerContour::onSpreadAdd( std::vector<double> *vecX, std::vector<double> *vecY, std::vector<double> *vecZ, std::vector<int> instants){
289         if(isInitContourPropagation()){
290                 if (vecX->size()!=0){
291                         
292                         int i,size=vecZ->size();
293                         int actualSlice = instants[1];
294                         for ( i=0 ; i<size ; i++ )
295                         {
296                                 (*vecZ)[i] = actualSlice;
297                         } // for
298
299                         _contourPropagation->appendContour(vecX , vecY , vecZ);
300                         return intToString(actualSlice);                
301                 }
302         }
303         return "";
304 }
305
306 void KernelManagerContour::getMaxMinZ(double *minZ,double *maxZ){
307         if(isInitContourPropagation()){
308                 _contourPropagation->getMaxMinZ(minZ, maxZ);
309         }
310 }
311 void KernelManagerContour::CalculeSplinePropagation(){
312         _contourPropagation->setInterpolationNumber(100);
313         _contourPropagation->CalculeSplinePropagation();
314 }
315
316 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){
317     
318         
319         bool addedModel = false;
320         manualContourModel* manModelContour=NULL;
321
322         if (_contourPropagation->ifSliceKeyContourExist(z)==false){
323
324                 manModelContour = factoryManualContourModel( typeofcontour );
325
326                 int idTmp = _contourPropagation->FindIdWithZ(z);
327
328                 if (type==0) // Initial Points
329                 {   
330                         _contourPropagation->GetInitialControlPoints( idTmp , vecCtrlPointX,vecCtrlPointY,vecCtrlPointZ);
331                 } 
332                 if (type==1)  // Automatique Method
333                 {
334                         _contourPropagation->GetControlPoints( idTmp  ,vecCtrlPointX,vecCtrlPointY,vecCtrlPointZ);
335                 }
336                 if (type==2)  // sampling
337                 {
338                         _contourPropagation->GetControlPoints( idTmp , 20.0 ,vecCtrlPointX,vecCtrlPointY,vecCtrlPointZ);
339                 }
340 //--------------------------------------------------------------------
341                 int sizeCtrPt = vecCtrlPointX->size();
342                 for (int j=0 ; j<sizeCtrPt ; j++)
343                 {
344 //JSTG_16-07-08_----------------------------------------------------------------
345                         manModelContour->AddPoint( (*vecCtrlPointX)[j] , (*vecCtrlPointY)[j] , -900  );
346 //--------------------------------------------------------------------
347                 } // for j
348
349                 tempVector[1]=z;
350                 theName = modelManager->createOutline( manModelContour, tempVector );
351                 addedModel = theName.compare("") != 0;
352                 if(!addedModel){
353                         manModelContour = NULL;
354                 }
355                 
356         }// ifSliceKeyContourExist
357         return manModelContour;
358
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 < 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 < 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