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