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