1 /*=========================================================================
2 Program: vv http://www.creatis.insa-lyon.fr/rio/vv
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
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.
13 It is distributed under dual licence
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"
20 #include "clitkTransformUtilities.h"
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;
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);
36 void read_points_pts(const std::string& fileName, PointArrayType& points);
37 void write_points_pts(const std::string& fileName, const PointArrayType& points);
39 void transform_points(const PointArrayType& input, const MatrixType& matrix, PointArrayType& output);
44 int main(int argc, char** argv)
46 GGO(clitkTransformLandmarks, args_info);
47 verbose = args_info.verbose_flag;
49 MatrixType matrix = clitk::ReadMatrix3D(args_info.matrix_arg);
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);
57 read_points_pts(args_info.input_arg, inputPoints);
60 PointArrayType outputPoints;
61 transform_points(inputPoints, matrix, outputPoints);
63 if (strcmp(args_info.type_arg, "txt") == 0) {
64 write_points_txt(args_info.output_arg, outputPoints, data);
67 write_points_pts(args_info.output_arg, outputPoints);
73 void read_points_txt(const std::string& fileName, PointArrayType& points, TxtDataArrayType& data)
75 std::ifstream landmarksFile(fileName.c_str());
76 if (landmarksFile.fail()) {
77 std::cout << "ERROR: could not open '" << fileName << "'" << std::endl;
82 std::getline(landmarksFile, line);
83 if (line.find("LANDMARKS") == std::string::npos) {
84 std::cout << "ERROR: invalid landmarks file '" << fileName << "'" << std::endl;
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())
100 std::cout << "point " << p << std::endl;
101 std::cout << "data " << " " << d[0] << " " << d[1] << std::endl;
109 void write_points_txt(const std::string& fileName, const PointArrayType& points, const TxtDataArrayType& data)
111 std::ofstream landmarksFile(fileName.c_str());
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;
118 void read_points_pts(const std::string& fileName, PointArrayType& points)
120 std::ifstream landmarksFile(fileName.c_str());
121 if (landmarksFile.fail()) {
122 std::cout << "ERROR: could not open '" << fileName << "'" << std::endl;
127 std::getline(landmarksFile, line);
128 if (line.find("#X") != 0) {
129 std::cout << "ERROR: invalid landmarks file '" << fileName << "'" << std::endl;
136 while (!landmarksFile.eof()) {
137 // read id, x, y, z, d1, d2
138 landmarksFile >> p[0] >> p[1] >> p[2];
139 if (landmarksFile.fail())
143 std::cout << "point " << p << std::endl;
150 void write_points_pts(const std::string& fileName, const PointArrayType& points)
152 std::ofstream landmarksFile(fileName.c_str());
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;
159 void transform_points(const PointArrayType& input, const MatrixType& matrix, PointArrayType& output)
161 for (size_t i = 0; i < input.size(); i++) {
162 output.push_back(matrix * input[i]);