]> Creatis software - clitk.git/blob - tools/clitkImageExtractLine.cxx
clitkImageCommon.h is not systematically included anymore
[clitk.git] / tools / clitkImageExtractLine.cxx
1 /*-------------------------------------------------------------------------
2                                                                                 
3   Copyright (c) CREATIS (Centre de Recherche et d'Applications en Traitement de
4   l'Image). All rights reserved. See Doc/License.txt or
5   http://www.creatis.insa-lyon.fr/Public/Gdcm/License.html for details.
6                                                                                 
7   This software is distributed WITHOUT ANY WARRANTY; without even
8   the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
9   PURPOSE.  See the above copyright notices for more information.
10                                                                              
11   -------------------------------------------------------------------------*/
12
13 #ifndef CLITKIMAGEEXTRACTLINE_CXX
14 #define CLITKIMAGEEXTRACTLINE_CXX
15
16 /**
17    -------------------------------------------------
18    * @file   clitkImageExtractLine.cxx
19    * @author David Sarrut <David.Sarrut@creatis.insa-lyon.fr>
20    * @date   23 Feb 2008 08:37:53
21    -------------------------------------------------*/
22
23 // clitk include
24 #include "clitkImageExtractLine_ggo.h"
25 #include "clitkIO.h"
26 #include "clitkImageCommon.h"
27 #include <itkLineConstIterator.h>
28
29 //--------------------------------------------------------------------
30 int main(int argc, char * argv[]) {
31
32   // Init command line
33   GGO(clitkImageExtractLine, args_info);
34   CLITK_INIT;
35
36   // Declare main types
37   typedef float PixelType;
38   const unsigned int Dimension=3;
39   typedef itk::Image<PixelType, Dimension> ImageType;
40   
41   // Check options
42   if (args_info.firstIndex_given != Dimension) {
43     std::cerr << "Please give " << Dimension << "values to --firstIndex option" << std::endl;
44     exit(0);
45   }
46   if (args_info.lastIndex_given != Dimension) {
47     std::cerr << "Please give " << Dimension << "values to --lastIndex option" << std::endl;
48     exit(0);
49   }
50
51   // Read image
52   ImageType::Pointer input = clitk::readImage<ImageType>(args_info.input_arg, args_info.verbose_flag);
53
54   // Get first and last index
55   typedef ImageType::IndexType IndexType;
56   IndexType firstIndex;
57   IndexType lastIndex;
58   ImageType::SpacingType spacing = input->GetSpacing();
59   double length = 0.0;
60   for(unsigned int i=0; i<Dimension; i++) {
61     firstIndex[i] = args_info.firstIndex_arg[i];
62     lastIndex[i] = args_info.lastIndex_arg[i];    
63     if (args_info.mm_flag) {
64       firstIndex[i] /= spacing[i];
65       lastIndex[i] /= spacing[i];
66     }
67     length += pow(lastIndex[i]*spacing[i]-firstIndex[i]*spacing[i],2);
68   }
69   length = sqrt(length);
70
71   // Loop
72   std::vector<double> depth;
73   std::vector<double> values;
74   itk::LineConstIterator<ImageType> iter(input, firstIndex, lastIndex);
75   iter.GoToBegin();
76   while (!iter.IsAtEnd()) {
77     values.push_back(iter.Get());
78     ++iter;
79   }
80   double step = length/values.size();
81   
82   // If isocenter is used
83   double isoDistance = 0.0;
84   if (args_info.isocenter_given) { // isoCenter is in mm
85     IndexType isoCenter;
86     for(unsigned int i=0; i<Dimension; i++) { 
87       isoCenter[i] = args_info.isocenter_arg[i]; 
88       isoDistance += pow(isoCenter[i] - firstIndex[i]*spacing[i],2);
89     }
90     DD(isoCenter);
91     isoDistance = sqrt(isoDistance);
92     DD(isoDistance);
93   }
94
95   // Write result
96   std::ofstream os(args_info.output_arg);
97   os << "# clitkImageExtractLine from " << args_info.input_arg << std::endl
98      << "# \t firstIndex = " << firstIndex << std::endl
99      << "# \t lastIndex  = " << lastIndex << std::endl
100      << "# \t nb values  = " << values.size() << std::endl
101      << "# \t length   = " << length << std::endl
102      << "# \t step       = " << step << std::endl;
103   if (args_info.depth_flag) {
104     double lg = -isoDistance;
105     for(unsigned int i=0; i<values.size(); i++) {
106       os << lg << " " << values[i] << std::endl;
107       lg += step;
108     }
109     os.close();
110   }
111   else {
112     for(unsigned int i=0; i<values.size(); i++) {
113       os << values[i] << std::endl;
114     }
115     os.close();
116   }
117
118   // this is the end my friend  
119   return 0;
120 } // end main
121
122 #endif //define CLITKIMAGEEXTRACTLINE_CXX