]> Creatis software - clitk.git/blob - tools/clitkTransformLandmarks.cxx
Romulo:
[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
13 void read_points(const std::string& fileName, PointArrayType& points);
14 void transform_points(const PointArrayType& input, const MatrixType& matrix, PointArrayType& output);
15 void write_points(const std::string& fileName, const PointArrayType& points);
16
17 bool verbose = false;
18
19 int main(int argc, char** argv)
20 {
21   GGO(clitkTransformLandmarks, args_info);
22   verbose = args_info.verbose_flag;
23
24   PointArrayType inputPoints;
25   read_points(args_info.input_arg, inputPoints);
26   
27   MatrixType matrix = clitk::ReadMatrix3D(args_info.matrix_arg);
28
29   PointArrayType outputPoints;
30   transform_points(inputPoints, matrix, outputPoints);
31   
32   write_points(args_info.output_arg, outputPoints);
33   return 0;
34 }
35
36 void read_points(const std::string& fileName, PointArrayType& points)
37 {
38   std::ifstream landmarksFile(fileName.c_str());
39   if (landmarksFile.fail()) {
40     std::cout << "ERROR: could not open '" << fileName << "'" << std::endl;
41     exit(-2);
42   }
43   
44   std::string line;
45   std::getline(landmarksFile, line);
46   if (line.find("LANDMARKS") == std::string::npos) {
47     std::cout << "ERROR: invalid landmarks file '" << fileName << "'" << std::endl;
48     exit(-3);
49   }
50   
51   int id = 0;
52   PointType point;
53   point.Fill(1);
54   
55   std::istringstream linestr; 
56   while (!landmarksFile.eof()) {
57     std::getline(landmarksFile, line);
58     if (verbose) std::cout << "line " << line << std::endl;
59     
60     if (!line.empty()) {
61       linestr.str(line);
62       linestr >> id >> point[0] >> point[1] >> point[2];
63       
64       if (verbose) std::cout << "point " << point << std::endl;
65       
66       points.push_back(point);
67     }
68   }
69 }
70
71 void transform_points(const PointArrayType& input, const MatrixType& matrix, PointArrayType& output)
72 {
73   for (size_t i = 0; i < input.size(); i++) {
74     output.push_back(matrix * input[i]);
75   }
76 }
77
78 void write_points(const std::string& fileName, const PointArrayType& points) 
79 {
80   std::ofstream landmarksFile(fileName.c_str());
81   
82   landmarksFile << "LANDMARKS1" << std::endl;
83   for (size_t i = 0; i < points.size(); i++)
84     landmarksFile << i << " " << points[i][0] << " " << points[i][1] << " " << points[i][2] << " " << "0" << " " << std::endl;
85 }