]> Creatis software - clitk.git/blob - tools/clitkImageExtractLine.cxx
Reformatted using new coding style
[clitk.git] / tools / clitkImageExtractLine.cxx
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 #ifndef CLITKIMAGEEXTRACTLINE_CXX
19 #define CLITKIMAGEEXTRACTLINE_CXX
20 /**
21    -------------------------------------------------
22    * @file   clitkImageExtractLine.cxx
23    * @author David Sarrut <David.Sarrut@creatis.insa-lyon.fr>
24    * @date   23 Feb 2008 08:37:53
25    -------------------------------------------------*/
26
27 // clitk include
28 #include "clitkImageExtractLine_ggo.h"
29 #include "clitkIO.h"
30 #include "clitkImageCommon.h"
31 #include <itkLineConstIterator.h>
32
33 //--------------------------------------------------------------------
34 int main(int argc, char * argv[])
35 {
36
37   // Init command line
38   GGO(clitkImageExtractLine, args_info);
39   CLITK_INIT;
40
41   // Declare main types
42   typedef float PixelType;
43   const unsigned int Dimension=3;
44   typedef itk::Image<PixelType, Dimension> ImageType;
45
46   // Check options
47   if (args_info.firstIndex_given != Dimension) {
48     std::cerr << "Please give " << Dimension << "values to --firstIndex option" << std::endl;
49     exit(0);
50   }
51   if (args_info.lastIndex_given != Dimension) {
52     std::cerr << "Please give " << Dimension << "values to --lastIndex option" << std::endl;
53     exit(0);
54   }
55
56   // Read image
57   ImageType::Pointer input = clitk::readImage<ImageType>(args_info.input_arg, args_info.verbose_flag);
58
59   // Get first and last index
60   typedef ImageType::IndexType IndexType;
61   IndexType firstIndex;
62   IndexType lastIndex;
63   ImageType::SpacingType spacing = input->GetSpacing();
64   double length = 0.0;
65   for(unsigned int i=0; i<Dimension; i++) {
66     firstIndex[i] = args_info.firstIndex_arg[i];
67     lastIndex[i] = args_info.lastIndex_arg[i];
68     if (args_info.mm_flag) {
69       firstIndex[i] /= spacing[i];
70       lastIndex[i] /= spacing[i];
71     }
72     length += pow(lastIndex[i]*spacing[i]-firstIndex[i]*spacing[i],2);
73   }
74   length = sqrt(length);
75
76   // Loop
77   std::vector<double> depth;
78   std::vector<double> values;
79   itk::LineConstIterator<ImageType> iter(input, firstIndex, lastIndex);
80   iter.GoToBegin();
81   while (!iter.IsAtEnd()) {
82     values.push_back(iter.Get());
83     ++iter;
84   }
85   double step = length/values.size();
86
87   // If isocenter is used
88   double isoDistance = 0.0;
89   if (args_info.isocenter_given) { // isoCenter is in mm
90     IndexType isoCenter;
91     for(unsigned int i=0; i<Dimension; i++) {
92       isoCenter[i] = args_info.isocenter_arg[i];
93       isoDistance += pow(isoCenter[i] - firstIndex[i]*spacing[i],2);
94     }
95     DD(isoCenter);
96     isoDistance = sqrt(isoDistance);
97     DD(isoDistance);
98   }
99
100   // Write result
101   std::ofstream os(args_info.output_arg);
102   os << "# clitkImageExtractLine from " << args_info.input_arg << std::endl
103      << "# \t firstIndex = " << firstIndex << std::endl
104      << "# \t lastIndex  = " << lastIndex << std::endl
105      << "# \t nb values  = " << values.size() << std::endl
106      << "# \t length   = " << length << std::endl
107      << "# \t step       = " << step << std::endl;
108   if (args_info.depth_flag) {
109     double lg = -isoDistance;
110     for(unsigned int i=0; i<values.size(); i++) {
111       os << lg << " " << values[i] << std::endl;
112       lg += step;
113     }
114     os.close();
115   } else {
116     for(unsigned int i=0; i<values.size(); i++) {
117       os << values[i] << std::endl;
118     }
119     os.close();
120   }
121
122   // this is the end my friend
123   return 0;
124 } // end main
125
126 #endif //define CLITKIMAGEEXTRACTLINE_CXX