]> Creatis software - clitk.git/blob - tools/clitkTransformLandmarks.cxx
ea84d18674c8712845468accfffb6707d6a28913
[clitk.git] / tools / clitkTransformLandmarks.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 "clitkTransformLandmarks_ggo.h"
19
20 #include "clitkTransformUtilities.h"
21 #include <string>
22 #include <vector>
23 #include <iostream>
24 #include <fstream>
25
26 typedef itk::Matrix<double, 4, 4> MatrixType;
27 typedef itk::Point<double, 4> PointType;
28 typedef std::vector<PointType> PointArrayType;
29 typedef itk::FixedArray<std::string, 2> TxtDataType;
30 typedef std::vector<TxtDataType> TxtDataArrayType;
31
32
33 void read_points_txt(const std::string& fileName, PointArrayType& points, TxtDataArrayType& data);
34 void write_points_txt(const std::string& fileName, const PointArrayType& points, const TxtDataArrayType& data);
35
36 void read_points_pts(const std::string& fileName, PointArrayType& points);
37 void write_points_pts(const std::string& fileName, const PointArrayType& points);
38
39 void transform_points(const PointArrayType& input, const MatrixType& matrix, PointArrayType& output);
40
41
42 bool verbose = false;
43
44 int main(int argc, char** argv)
45 {
46   GGO(clitkTransformLandmarks, args_info);
47   verbose = args_info.verbose_flag;
48
49   MatrixType matrix = clitk::ReadMatrix3D(args_info.matrix_arg);
50
51   TxtDataArrayType data;
52   PointArrayType inputPoints;
53   if (strcmp(args_info.type_arg, "txt") == 0) {
54     read_points_txt(args_info.input_arg, inputPoints, data);
55   }
56   else {
57     read_points_pts(args_info.input_arg, inputPoints);
58   }
59   
60   PointArrayType outputPoints;
61   transform_points(inputPoints, matrix, outputPoints);
62
63   if (strcmp(args_info.type_arg, "txt") == 0) {
64     write_points_txt(args_info.output_arg, outputPoints, data);
65   }
66   else {
67     write_points_pts(args_info.output_arg, outputPoints);
68   }
69   
70   return 0;
71 }
72
73 void read_points_txt(const std::string& fileName, PointArrayType& points, TxtDataArrayType& data)
74 {
75   std::ifstream landmarksFile(fileName.c_str());
76   if (landmarksFile.fail()) {
77     std::cout << "ERROR: could not open '" << fileName << "'" << std::endl;
78     exit(-2);
79   }
80   
81   std::string line;
82   std::getline(landmarksFile, line);
83   if (line.find("LANDMARKS") == std::string::npos) {
84     std::cout << "ERROR: invalid landmarks file '" << fileName << "'" << std::endl;
85     exit(-3);
86   }
87   
88   PointType p;
89   p.Fill(1);
90   
91   TxtDataType d;
92   
93   while (!landmarksFile.eof()) {
94     // read id, x, y, z, d1, d2
95     landmarksFile >> d[0] >> p[0] >> p[1] >> p[2] >> d[1];// >> d[2];
96     if (landmarksFile.fail())
97       break;
98     
99     if (verbose){
100       std::cout << "point " << p << std::endl;
101       std::cout << "data " << " " << d[0] << " " << d[1] << std::endl;
102     }
103     
104     points.push_back(p);
105     data.push_back(d);
106   }
107 }
108
109 void write_points_txt(const std::string& fileName, const PointArrayType& points, const TxtDataArrayType& data) 
110 {
111   std::ofstream landmarksFile(fileName.c_str());
112   
113   landmarksFile << "LANDMARKS1" << std::endl;
114   for (size_t i = 0; i < points.size(); i++)
115     landmarksFile << data[i][0] << " " << points[i][0] << " " << points[i][1] << " " << points[i][2] << " " << data[i][1] << " " << std::endl;
116 }
117
118 void read_points_pts(const std::string& fileName, PointArrayType& points)
119 {
120   std::ifstream landmarksFile(fileName.c_str());
121   if (landmarksFile.fail()) {
122     std::cout << "ERROR: could not open '" << fileName << "'" << std::endl;
123     exit(-2);
124   }
125   
126   std::string line;
127   std::getline(landmarksFile, line);
128   if (line.find("#X") != 0) {
129     std::cout << "ERROR: invalid landmarks file '" << fileName << "'" << std::endl;
130     exit(-3);
131   }
132   
133   PointType p;
134   p.Fill(1);
135   
136   while (!landmarksFile.eof()) {
137     // read id, x, y, z, d1, d2
138     landmarksFile >> p[0] >> p[1] >> p[2];
139     if (landmarksFile.fail())
140       break;
141     
142     if (verbose){
143       std::cout << "point " << p << std::endl;
144     }
145     
146     points.push_back(p);
147   }
148 }
149
150 void write_points_pts(const std::string& fileName, const PointArrayType& points)
151 {
152   std::ofstream landmarksFile(fileName.c_str());
153   
154   landmarksFile << "#X\tY\tZ" << std::endl;
155   for (size_t i = 0; i < points.size(); i++)
156     landmarksFile << points[i][0] << "\t" << points[i][1] << "\t" << points[i][2] << "\t" << std::endl;
157 }
158
159 void transform_points(const PointArrayType& input, const MatrixType& matrix, PointArrayType& output)
160 {
161   for (size_t i = 0; i < input.size(); i++) {
162     output.push_back(matrix * input[i]);
163   }
164 }
165