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 apply_spacing(const PointArrayType& input, const double* spacing, PointArrayType& output);
40 void transform_points(const PointArrayType& input, const MatrixType& matrix, PointArrayType& output);
45 int main(int argc, char** argv)
47 GGO(clitkTransformLandmarks, args_info);
48 verbose = args_info.verbose_flag;
50 TxtDataArrayType data;
51 PointArrayType inputPoints;
52 if (strcmp(args_info.type_arg, "txt") == 0) {
53 read_points_txt(args_info.input_arg, inputPoints, data);
56 read_points_pts(args_info.input_arg, inputPoints);
59 PointArrayType outputPoints;
60 PointArrayType spacingPoints;
61 PointArrayType* workingInputPoints = &inputPoints;
62 PointArrayType* workingOutputPoints = &outputPoints;
63 if (args_info.spacing_given) {
64 if (verbose) std::cout << "Processing spacing..." << std::endl;
66 apply_spacing(*workingInputPoints, args_info.spacing_arg, spacingPoints);
67 workingInputPoints = &spacingPoints;
68 workingOutputPoints = &spacingPoints;
72 if (args_info.matrix_given) {
73 matrix = clitk::ReadMatrix3D(args_info.matrix_arg);
74 transform_points(*workingInputPoints, matrix, outputPoints);
75 workingOutputPoints = &outputPoints;
78 if (strcmp(args_info.type_arg, "txt") == 0) {
79 write_points_txt(args_info.output_arg, *workingOutputPoints, data);
82 write_points_pts(args_info.output_arg, *workingOutputPoints);
88 void read_points_txt(const std::string& fileName, PointArrayType& points, TxtDataArrayType& data)
90 std::ifstream landmarksFile(fileName.c_str());
91 if (landmarksFile.fail()) {
92 std::cerr << "ERROR: could not open '" << fileName << "'" << std::endl;
97 std::getline(landmarksFile, line);
98 if (line.find("LANDMARKS") == std::string::npos) {
99 std::cerr << "ERROR: invalid landmarks file '" << fileName << "'" << std::endl;
108 while (!landmarksFile.eof()) {
109 // read id, x, y, z, d1, d2
110 landmarksFile >> d[0] >> p[0] >> p[1] >> p[2] >> d[1];// >> d[2];
111 if (landmarksFile.fail())
115 std::cout << "point " << p << std::endl;
116 std::cout << "data " << " " << d[0] << " " << d[1] << std::endl;
124 void write_points_txt(const std::string& fileName, const PointArrayType& points, const TxtDataArrayType& data)
126 std::ofstream landmarksFile(fileName.c_str());
128 landmarksFile << "LANDMARKS1" << std::endl;
129 for (size_t i = 0; i < points.size(); i++)
130 landmarksFile << data[i][0] << " " << points[i][0] << " " << points[i][1] << " " << points[i][2] << " " << data[i][1] << " " << std::endl;
133 void read_points_pts(const std::string& fileName, PointArrayType& points)
135 std::ifstream landmarksFile(fileName.c_str());
136 if (landmarksFile.fail()) {
137 std::cerr << "ERROR: could not open '" << fileName << "'" << std::endl;
142 std::getline(landmarksFile, line);
143 if (line.find("#X") != 0) {
144 std::cerr << "ERROR: invalid landmarks file '" << fileName << "'" << std::endl;
151 while (!landmarksFile.eof()) {
152 // read id, x, y, z, d1, d2
153 landmarksFile >> p[0] >> p[1] >> p[2];
154 if (landmarksFile.fail())
158 std::cout << "point " << p << std::endl;
165 void write_points_pts(const std::string& fileName, const PointArrayType& points)
167 std::ofstream landmarksFile(fileName.c_str());
169 landmarksFile << "#X\tY\tZ" << std::endl;
170 for (size_t i = 0; i < points.size(); i++)
171 landmarksFile << points[i][0] << "\t" << points[i][1] << "\t" << points[i][2] << "\t" << std::endl;
174 void apply_spacing(const PointArrayType& input, const double* spacing, PointArrayType& output)
179 for (size_t i = 0; i < input.size(); i++) {
180 out[0] = input[i][0] * spacing[0];
181 out[1] = input[i][1] * spacing[1];
182 out[2] = input[i][2] * spacing[2];
184 std::cout << "output " << out << std::endl;
186 output.push_back(out);
190 void transform_points(const PointArrayType& input, const MatrixType& matrix, PointArrayType& output)
192 for (size_t i = 0; i < input.size(); i++) {
193 output.push_back(matrix * input[i]);