1 #include "clitkTransformLandmarks_ggo.h"
3 #include "clitkTransformUtilities.h"
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;
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);
19 void read_points_pts(const std::string& fileName, PointArrayType& points);
20 void write_points_pts(const std::string& fileName, const PointArrayType& points);
22 void transform_points(const PointArrayType& input, const MatrixType& matrix, PointArrayType& output);
27 int main(int argc, char** argv)
29 GGO(clitkTransformLandmarks, args_info);
30 verbose = args_info.verbose_flag;
32 MatrixType matrix = clitk::ReadMatrix3D(args_info.matrix_arg);
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);
40 read_points_pts(args_info.input_arg, inputPoints);
43 PointArrayType outputPoints;
44 transform_points(inputPoints, matrix, outputPoints);
46 if (strcmp(args_info.type_arg, "txt") == 0) {
47 write_points_txt(args_info.output_arg, outputPoints, data);
50 write_points_pts(args_info.output_arg, outputPoints);
56 void read_points_txt(const std::string& fileName, PointArrayType& points, TxtDataArrayType& data)
58 std::ifstream landmarksFile(fileName.c_str());
59 if (landmarksFile.fail()) {
60 std::cout << "ERROR: could not open '" << fileName << "'" << std::endl;
65 std::getline(landmarksFile, line);
66 if (line.find("LANDMARKS") == std::string::npos) {
67 std::cout << "ERROR: invalid landmarks file '" << fileName << "'" << std::endl;
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())
83 std::cout << "point " << p << std::endl;
84 std::cout << "data " << " " << d[0] << " " << d[1] << std::endl;
92 void write_points_txt(const std::string& fileName, const PointArrayType& points, const TxtDataArrayType& data)
94 std::ofstream landmarksFile(fileName.c_str());
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;
101 void read_points_pts(const std::string& fileName, PointArrayType& points)
103 std::ifstream landmarksFile(fileName.c_str());
104 if (landmarksFile.fail()) {
105 std::cout << "ERROR: could not open '" << fileName << "'" << std::endl;
110 std::getline(landmarksFile, line);
111 if (line.find("#X") != 0) {
112 std::cout << "ERROR: invalid landmarks file '" << fileName << "'" << std::endl;
119 while (!landmarksFile.eof()) {
120 // read id, x, y, z, d1, d2
121 landmarksFile >> p[0] >> p[1] >> p[2];
122 if (landmarksFile.fail())
126 std::cout << "point " << p << std::endl;
133 void write_points_pts(const std::string& fileName, const PointArrayType& points)
135 std::ofstream landmarksFile(fileName.c_str());
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;
142 void transform_points(const PointArrayType& input, const MatrixType& matrix, PointArrayType& output)
144 for (size_t i = 0; i < input.size(); i++) {
145 output.push_back(matrix * input[i]);