]> Creatis software - clitk.git/blob - tools/clitkMaskLandmarks.cxx
close #59 #58 Change clitkAffineTransform --transform_grid
[clitk.git] / tools / clitkMaskLandmarks.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://www.centreleonberard.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 #include "clitkMaskLandmarks_ggo.h"
19
20 #include <string>
21 #include <vector>
22 #include <iostream>
23 #include <fstream>
24
25 #include <clitkCommon.h>
26 #include <clitkImageCommon.h>
27
28 typedef itk::Point<double, 3> PointType;
29 typedef std::vector<PointType> PointArrayType;
30
31 static void read_points_pts(const std::string& fileName, PointArrayType& points, bool verbose)
32 {
33   std::ifstream landmarksFile(fileName.c_str());
34   if (landmarksFile.fail())
35   {
36     std::cerr << "ERROR: could not open '" << fileName << "'" << std::endl;
37     exit(-2);
38   }
39
40   std::string line;
41   std::getline(landmarksFile, line);
42   if (line.find("#X") != 0)
43   {
44     std::cerr << "ERROR: invalid landmarks file '" << fileName << "'" << std::endl;
45     exit(-3);
46   }
47
48   PointType p;
49   while (!landmarksFile.eof())
50   {
51     landmarksFile >> p[0] >> p[1] >> p[2];
52     if (landmarksFile.fail())
53       break;
54     if (verbose)
55       std::cout << "point " << p << std::endl;
56     points.push_back(p);
57   }
58 }
59
60 void write_points_pts(const std::string& fileName, const PointArrayType& points, bool verbose)
61 {
62   std::ofstream landmarksFile(fileName.c_str());
63
64   landmarksFile << "#X\tY\tZ" << std::endl;
65   for (size_t i = 0; i < points.size(); i++)
66     landmarksFile << points[i][0] << "\t" << points[i][1] << "\t" << points[i][2] << "\t" << std::endl;
67 }
68
69 int main(int argc, char** argv)
70 {
71   typedef itk::Image<unsigned char, 3>  MaskType;
72   typedef MaskType::Pointer             MaskPointer;
73   GGO(clitkMaskLandmarks, args_info);
74
75   if (args_info.input_given != args_info.output_given)
76   {
77     std::cerr << "The number of inputs must be equal to the number of outputs" << std::endl;;
78     exit(1);
79   }
80
81   std::vector<PointArrayType> inputs(args_info.input_given);
82   for (unsigned i = 0; i < args_info.input_given; ++i)
83   {
84     read_points_pts(args_info.input_arg[i], inputs[i], args_info.verbose_flag);
85     if (inputs[i].size() != inputs[0].size())
86     {
87       std::cerr << "All input files must contain the same amount of points" << std::endl;
88       exit(2);
89     }
90   }
91
92   MaskPointer mask = clitk::readImage<MaskType>(args_info.mask_arg, args_info.verbose_flag);
93   std::vector<PointArrayType> outputs(args_info.input_given);
94   for (unsigned i = 0; i < inputs[0].size(); ++i)
95   {
96     MaskType::IndexType idx;
97     if (mask->TransformPhysicalPointToIndex(inputs[0][i], idx) && (*mask)[idx])
98       for (unsigned j = 0; j < args_info.input_given; j++)
99         outputs[j].push_back(inputs[j][i]);
100   }
101
102   for (unsigned i = 0; i < args_info.input_given; ++i)
103     write_points_pts(args_info.output_arg[i], outputs[i], args_info.verbose_flag);
104 }