]> Creatis software - clitk.git/blob - segmentation/clitkRelativePositionList.txx
Merge branch 'master' of /home/dsarrut/clitk3.server
[clitk.git] / segmentation / clitkRelativePositionList.txx
1 /*=========================================================================
2   Program:   vv                     http://www.creatis.insa-lyon.fr/rio/vv
3
4   Authors belong to: 
5   - University of LYON              http://www.universite-lyon.fr/
6   - Léon Bérard cancer center       http://oncora1.lyon.fnclcc.fr
7   - CREATIS CNRS laboratory         http://www.creatis.insa-lyon.fr
8
9   This software is distributed WITHOUT ANY WARRANTY; without even
10   the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
11   PURPOSE.  See the copyright notices for more information.
12
13   It is distributed under dual licence
14
15   - BSD        See included LICENSE.txt file
16   - CeCILL-B   http://www.cecill.info/licences/Licence_CeCILL-B_V1-en.html
17   ======================================================================-====*/
18
19
20 //--------------------------------------------------------------------
21 template <class TImageType>
22 clitk::RelativePositionList<TImageType>::
23 RelativePositionList() {
24 }
25 //--------------------------------------------------------------------
26
27
28 //--------------------------------------------------------------------
29 template <class TImageType>
30 void
31 clitk::RelativePositionList<TImageType>::
32 Read(std::string filename) {
33   /*
34     The goal here is to read a text file that contains options for the
35     RelativePosition filter. The text file contains options in the
36     same form of the config file of the clitkRelativePosition tool. In
37     this text file, each time a "object" option is found, a new set of
38     options is considered.
39    */
40
41   // Open the file
42   std::ifstream is;
43   openFileForReading(is, filename);
44
45   // Read input -> the structure name that will be used as input
46   // (initial support)
47   skipComment(is);
48   std::string s;
49   is >> s;
50   if (s != "input") {
51     FATAL("while reading RelativePositionList file. This file must start with 'input = '");
52     exit(0);
53   }
54   is >> s; 
55   if (s != "=") {
56     FATAL("while reading RelativePositionList file. This file must start with 'input = '");
57     exit(0);
58   }
59   std::string name;
60   is >> name;
61   skipComment(is);
62
63   // Create a temporary filename 
64   char n[] = "tmp_clitkRelativePosition_XXXXXX";
65   mkstemp(n); // tmpnam(n); 
66   std::string tmpfilename(n);
67   
68   // Loop on the file text ; Every time we see a "object" token, we
69   // split the file.
70   while (is) {
71     bool stop=false;
72     std::ostringstream ss;
73     // first part of ss is the last 'object = ' found, stored in s
74     ss << s << std::endl; 
75     while (!stop) {
76       skipComment(is);
77       if (!is) stop = true;
78       else {
79         std::getline(is, s);
80         // DD(s);
81         if (s.find("object") != std::string::npos) stop=true;
82         else ss << s << std::endl;
83         if (!is) stop = true;
84       }
85     }
86     std::string text = ss.str();
87     if (text.size() > 10) { // if too small, it is not a list of option
88       std::ofstream os;
89       openFileForWriting(os, tmpfilename);
90       os << text;
91       os.close();
92
93       // Create a struct to store options. I use two step to allow to
94       // fill the args values with de default and then check
95       // automatically the options.
96       ArgsInfoType args_info;
97       std::vector<char> writable(tmpfilename.size() + 1);
98       std::copy(tmpfilename.begin(), tmpfilename.end(), writable.begin());
99       char ** argv;
100       cmdline_parser_clitkRelativePosition2(0, argv, &args_info, 1, 1, 0);
101       args_info.input_given = 1;
102       args_info.input_arg = new char[1];
103       args_info.output_given = 1;
104       args_info.output_arg = new char[1];
105       cmdline_parser_clitkRelativePosition_configfile(&writable[0], &args_info, 0, 0, 1);
106       
107       // Store the args
108       mArgsInfoList.push_back(args_info);
109       
110       // Delete the temporary file
111       std::remove(tmpfilename.c_str());
112     }
113   }
114
115   // Set the main input name
116   SetInputName(name);
117 }
118 //--------------------------------------------------------------------
119
120
121 //--------------------------------------------------------------------
122 template <class TImageType>
123 void 
124 clitk::RelativePositionList<TImageType>::
125 GenerateInputRequestedRegion() 
126 {
127   // Call default
128   itk::ImageToImageFilter<ImageType, ImageType>::GenerateInputRequestedRegion();
129   // Get input pointers and set requested region to common region
130   ImagePointer input1 = dynamic_cast<ImageType*>(itk::ProcessObject::GetInput(0));
131   input1->SetRequestedRegion(input1->GetLargestPossibleRegion());
132 }
133 //--------------------------------------------------------------------
134
135
136
137 //--------------------------------------------------------------------
138 template <class TImageType>
139 void
140 clitk::RelativePositionList<TImageType>::
141 GenerateOutputInformation() {
142
143   // Get input
144   m_working_input = dynamic_cast<ImageType*>(itk::ProcessObject::GetInput(0));
145
146   // Loop on RelativePositionList of operations
147   std::string s = GetInputName();
148   for(uint i=0; i<mArgsInfoList.size(); i++) {
149     std::string text = "["+s+"] limits "+
150       mArgsInfoList[i].orientation_arg[0]+" "+
151       mArgsInfoList[i].object_arg+" "+
152       toString(mArgsInfoList[i].threshold_arg);
153     if (mArgsInfoList[i].sliceBySlice_flag) {
154       text += " slice by slice";
155     }
156     else text += " 3D";
157
158     StartNewStep(text);  
159     typename RelPosFilterType::Pointer relPosFilter;
160
161     // Is it slice by slice or 3D ?
162     if (mArgsInfoList[i].sliceBySlice_flag) {
163       typename SliceRelPosFilterType::Pointer f = SliceRelPosFilterType::New();
164       relPosFilter = f;
165       SetFilterOptions(relPosFilter, mArgsInfoList[i]);  
166       f->SetDirection(2);
167       // Set SbS specific options
168       f->SetUniqueConnectedComponentBySliceFlag(mArgsInfoList[i].uniqueCCL_flag);
169       f->SetObjectCCLSelectionFlag(mArgsInfoList[i].uniqueObjectCCL_flag);
170       f->IgnoreEmptySliceObjectFlagOn();
171       //f->SetObjectCCLSelectionDimension(0);
172       //f->SetObjectCCLSelectionDirection(-1);
173       //f->SetAutoCropFlag(false);
174       // Print if needed
175       if (mArgsInfoList[i].verboseOptions_flag) f->PrintOptions();
176     }
177     else {
178       relPosFilter = clitk::AddRelativePositionConstraintToLabelImageFilter<ImageType>::New();
179       SetFilterOptions(relPosFilter, mArgsInfoList[i]);  
180       // Print if needed
181       if (mArgsInfoList[i].verboseOptions_flag) relPosFilter->PrintOptions();
182     }
183
184     // Set input
185     relPosFilter->SetInput(m_working_input);  
186   
187     // Run the filter
188     relPosFilter->Update();
189     m_working_input = relPosFilter->GetOutput();  
190     StopCurrentStep<ImageType>(m_working_input);
191   }
192 }
193 //--------------------------------------------------------------------
194
195
196 //--------------------------------------------------------------------
197 template <class TImageType>
198 void
199 clitk::RelativePositionList<TImageType>::
200 GenerateData() 
201 {
202   // Final Step -> set output
203   this->GraftOutput(m_working_input);
204 }
205 //--------------------------------------------------------------------
206
207
208 //--------------------------------------------------------------------
209 template <class TImageType>
210 void
211 clitk::RelativePositionList<TImageType>::
212 SetFilterOptions(typename RelPosFilterType::Pointer filter, ArgsInfoType & options) {
213
214   if (options.orientation_given != 1) {
215     DD("ERRROR DEBUG TODO no more than 1 orientation yet");
216     exit(0);
217   }
218
219   ImagePointer object = GetAFDB()->template GetImage<ImageType>(options.object_arg);
220   filter->SetInputObject(object);
221   filter->WriteStepFlagOff();
222   filter->SetVerboseImageSizeFlag(GetVerboseImageSizeFlag());
223   filter->SetFuzzyThreshold(options.threshold_arg);
224   filter->SetInverseOrientationFlag(options.inverse_flag); // MUST BE BEFORE AddOrientationTypeString
225   for(uint i=0; i<options.orientation_given; i++) 
226     filter->AddOrientationTypeString(options.orientation_arg[i]);
227   filter->SetIntermediateSpacing(options.spacing_arg);
228   if (options.spacing_arg == -1) filter->IntermediateSpacingFlagOff();
229   else filter->IntermediateSpacingFlagOn();
230   filter->SetVerboseStepFlag(options.verboseStep_flag);
231   filter->SetAutoCropFlag(!options.noAutoCrop_flag); 
232 }
233 //--------------------------------------------------------------------