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