1 /*=========================================================================
2 Program: vv http://www.creatis.insa-lyon.fr/rio/vv
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
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.
13 It is distributed under dual licence
15 - BSD See included LICENSE.txt file
16 - CeCILL-B http://www.cecill.info/licences/Licence_CeCILL-B_V1-en.html
17 ======================================================================-====*/
20 //--------------------------------------------------------------------
21 template <class TImageType>
22 clitk::RelativePositionList<TImageType>::
23 RelativePositionList() {
25 //--------------------------------------------------------------------
28 //--------------------------------------------------------------------
29 template <class TImageType>
31 clitk::RelativePositionList<TImageType>::
32 Read(std::string filename) {
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.
43 openFileForReading(is, filename);
45 // Read input -> the structure name that will be used as input
51 FATAL("while reading RelativePositionList file. This file must start with 'input = '");
56 FATAL("while reading RelativePositionList file. This file must start with 'input = '");
63 // Create a temporary filename
64 char n[] = "tmp_clitkRelativePosition_XXXXXX";
65 mkstemp(n); // tmpnam(n);
66 std::string tmpfilename(n);
68 // Loop on the file text ; Every time we see a "object" token, we
72 std::ostringstream ss;
73 // first part of ss is the last 'object = ' found, stored in s
81 if (s.find("object") != std::string::npos) stop=true;
82 else ss << s << std::endl;
86 std::string text = ss.str();
87 if (text.size() > 10) { // if too small, it is not a list of option
89 openFileForWriting(os, tmpfilename);
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);
104 mArgsInfoList.push_back(args_info);
106 // Delete the temporary file
107 std::remove(tmpfilename.c_str());
111 // Set the main input name
114 //--------------------------------------------------------------------
117 //--------------------------------------------------------------------
118 template <class TImageType>
120 clitk::RelativePositionList<TImageType>::
121 GenerateInputRequestedRegion()
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());
129 //--------------------------------------------------------------------
133 //--------------------------------------------------------------------
134 template <class TImageType>
136 clitk::RelativePositionList<TImageType>::
137 GenerateOutputInformation() {
140 m_working_input = dynamic_cast<ImageType*>(itk::ProcessObject::GetInput(0));
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";
155 typename RelPosFilterType::Pointer relPosFilter;
157 // DD(mArgsInfoList[i].sliceBySlice_flag);
158 if (mArgsInfoList[i].sliceBySlice_flag) {
159 relPosFilter = SliceRelPosFilterType::New();
160 dynamic_cast<SliceRelPosFilterType*>(&*relPosFilter)->SetDirection(2);
163 relPosFilter = clitk::AddRelativePositionConstraintToLabelImageFilter<ImageType>::New();
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);
176 //--------------------------------------------------------------------
179 //--------------------------------------------------------------------
180 template <class TImageType>
182 clitk::RelativePositionList<TImageType>::
185 // Final Step -> set output
186 this->GraftOutput(m_working_input);
188 //--------------------------------------------------------------------
191 //--------------------------------------------------------------------
192 template <class TImageType>
194 clitk::RelativePositionList<TImageType>::
195 SetFilterOptions(typename RelPosFilterType::Pointer filter,
196 ArgsInfoType & options) {
198 if (options.orientation_given != 1) {
199 DD("ERRROR DEBUG TODO no more than 1 orientation yet");
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();
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);
222 filter->SetAutoCropFlag(!options.noAutoCrop_flag);
224 //--------------------------------------------------------------------