]> Creatis software - clitk.git/blob - tools/clitkImageExtractLine.cxx
1f826b64602d8750361193733de118b3dca40084
[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   // Init command line
37   GGO(clitkImageExtractLine, args_info);
38   CLITK_INIT;
39
40   // Declare main types
41   typedef float PixelType;
42   const unsigned int Dimension=3;
43   typedef itk::Image<PixelType, Dimension> ImageType;
44   
45   // Check options
46   if (args_info.firstIndex_given != Dimension) {
47     std::cerr << "Please give " << Dimension << "values to --firstIndex option" << std::endl;
48     exit(0);
49   }
50   if (args_info.lastIndex_given != Dimension) {
51     std::cerr << "Please give " << Dimension << "values to --lastIndex option" << std::endl;
52     exit(0);
53   }
54
55   // Read image
56   ImageType::Pointer input = clitk::readImage<ImageType>(args_info.input_arg, args_info.verbose_flag);
57
58   // Get first and last index
59   typedef ImageType::IndexType IndexType;
60   IndexType firstIndex;
61   IndexType lastIndex;
62   ImageType::SpacingType spacing = input->GetSpacing();
63   double length = 0.0;
64   for(unsigned int i=0; i<Dimension; i++) {
65     firstIndex[i] = args_info.firstIndex_arg[i];
66     lastIndex[i] = args_info.lastIndex_arg[i];    
67     if (args_info.mm_flag) {
68       firstIndex[i] /= spacing[i];
69       lastIndex[i] /= spacing[i];
70     }
71     length += pow(lastIndex[i]*spacing[i]-firstIndex[i]*spacing[i],2);
72   }
73   length = sqrt(length);
74
75   // Loop
76   std::vector<double> depth;
77   std::vector<double> values;
78   itk::LineConstIterator<ImageType> iter(input, firstIndex, lastIndex);
79   iter.GoToBegin();
80   while (!iter.IsAtEnd()) {
81     values.push_back(iter.Get());
82     ++iter;
83   }
84   double step = length/values.size();
85   
86   // If isocenter is used
87   double isoDistance = 0.0;
88   if (args_info.isocenter_given) { // isoCenter is in mm
89     IndexType isoCenter;
90     for(unsigned int i=0; i<Dimension; i++) { 
91       isoCenter[i] = args_info.isocenter_arg[i]; 
92       isoDistance += pow(isoCenter[i] - firstIndex[i]*spacing[i],2);
93     }
94     DD(isoCenter);
95     isoDistance = sqrt(isoDistance);
96     DD(isoDistance);
97   }
98
99   // Write result
100   std::ofstream os(args_info.output_arg);
101   os << "# clitkImageExtractLine from " << args_info.input_arg << std::endl
102      << "# \t firstIndex = " << firstIndex << std::endl
103      << "# \t lastIndex  = " << lastIndex << std::endl
104      << "# \t nb values  = " << values.size() << std::endl
105      << "# \t length   = " << length << std::endl
106      << "# \t step       = " << step << std::endl;
107   if (args_info.depth_flag) {
108     double lg = -isoDistance;
109     for(unsigned int i=0; i<values.size(); i++) {
110       os << lg << " " << values[i] << std::endl;
111       lg += step;
112     }
113     os.close();
114   }
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