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