]> 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
94       ArgsInfoType args_info;
95       args_info.input_given = 1;
96       args_info.input_arg = new char[1];
97       args_info.output_given = 1;
98       args_info.output_arg = new char[1];
99       std::vector<char> writable(tmpfilename.size() + 1);
100       std::copy(tmpfilename.begin(), tmpfilename.end(), writable.begin());
101       cmdline_parser_clitkRelativePosition_configfile(&writable[0], &args_info, 1, 1, 0);
102       
103       // Store the args
104       mArgsInfoList.push_back(args_info);
105       
106       // Delete the temporary file
107       std::remove(tmpfilename.c_str());
108     }
109   }
110
111   // Set the main input name
112   SetInputName(name);
113 }
114 //--------------------------------------------------------------------
115
116
117 //--------------------------------------------------------------------
118 template <class TImageType>
119 void 
120 clitk::RelativePositionList<TImageType>::
121 GenerateInputRequestedRegion() 
122 {
123   // Call default
124   itk::ImageToImageFilter<ImageType, ImageType>::GenerateInputRequestedRegion();
125   // Get input pointers and set requested region to common region
126   ImagePointer input1 = dynamic_cast<ImageType*>(itk::ProcessObject::GetInput(0));
127   input1->SetRequestedRegion(input1->GetLargestPossibleRegion());
128 }
129 //--------------------------------------------------------------------
130
131
132
133 //--------------------------------------------------------------------
134 template <class TImageType>
135 void
136 clitk::RelativePositionList<TImageType>::
137 GenerateOutputInformation() {
138
139   // Get input
140   m_working_input = dynamic_cast<ImageType*>(itk::ProcessObject::GetInput(0));
141
142   // Loop on RelativePositionList of operations
143   std::string s = GetInputName();
144   for(uint i=0; i<mArgsInfoList.size(); i++) {
145     std::string text = "["+s+"] limits "+
146       mArgsInfoList[i].orientation_arg[0]+" "+
147       mArgsInfoList[i].object_arg+" "+
148       toString(mArgsInfoList[i].threshold_arg);
149     if (mArgsInfoList[i].sliceBySlice_flag) {
150       text += " slice by slice";
151     }
152     else text += " 3D";
153
154     StartNewStep(text);  
155     typename RelPosFilterType::Pointer relPosFilter;
156
157     // DD(mArgsInfoList[i].sliceBySlice_flag);
158     if (mArgsInfoList[i].sliceBySlice_flag) {
159       relPosFilter = SliceRelPosFilterType::New();
160       dynamic_cast<SliceRelPosFilterType*>(&*relPosFilter)->SetDirection(2);
161     }
162     else {
163       relPosFilter = clitk::AddRelativePositionConstraintToLabelImageFilter<ImageType>::New();
164     }
165     relPosFilter->VerboseStepFlagOff();
166     relPosFilter->WriteStepFlagOff();
167     relPosFilter->SetVerboseImageSizeFlag(GetVerboseImageSizeFlag());
168     relPosFilter->SetInput(m_working_input);
169     SetFilterOptions(relPosFilter, mArgsInfoList[i]);    
170     //relPosFilter->PrintOptions();
171     relPosFilter->Update();
172     m_working_input = relPosFilter->GetOutput();  
173     StopCurrentStep<ImageType>(m_working_input);
174   }
175 }
176 //--------------------------------------------------------------------
177
178
179 //--------------------------------------------------------------------
180 template <class TImageType>
181 void
182 clitk::RelativePositionList<TImageType>::
183 GenerateData() 
184 {
185   // Final Step -> set output
186   this->GraftOutput(m_working_input);
187 }
188 //--------------------------------------------------------------------
189
190
191 //--------------------------------------------------------------------
192 template <class TImageType>
193 void
194 clitk::RelativePositionList<TImageType>::
195 SetFilterOptions(typename RelPosFilterType::Pointer filter, 
196                  ArgsInfoType & options) {
197
198   if (options.orientation_given != 1) {
199     DD("ERRROR DEBUG TODO no more than 1 orientation yet");
200     exit(0);
201   }
202
203   ImagePointer object = GetAFDB()->template GetImage<ImageType>(options.object_arg);
204   filter->SetInputObject(object);
205   filter->SetFuzzyThreshold(options.threshold_arg);
206   filter->SetInverseOrientationFlag(options.inverse_flag); // MUST BE BEFORE AddOrientationTypeString
207   for(uint i=0; i<options.orientation_given; i++) 
208     filter->AddOrientationTypeString(options.orientation_arg[i]);
209   filter->SetIntermediateSpacing(options.spacing_arg);
210   if (options.spacing_arg == -1) filter->IntermediateSpacingFlagOff();
211   filter->IntermediateSpacingFlagOn();
212   
213   if (options.sliceBySlice_flag) {
214     SliceRelPosFilterType * f = dynamic_cast<SliceRelPosFilterType*>(&*filter);
215     f->SetUniqueConnectedComponentBySliceFlag(options.uniqueCCL_flag);
216     f->SetObjectCCLSelectionFlag(options.uniqueObjectCCL_flag);
217     f->IgnoreEmptySliceObjectFlagOn();
218     //filter->SetObjectCCLSelectionDimension(0);
219     //filter->SetObjectCCLSelectionDirection(-1);
220     //filter->SetAutoCropFlag(false);
221   }
222   filter->SetAutoCropFlag(!options.noAutoCrop_flag); 
223 }
224 //--------------------------------------------------------------------