From a0875baa5ffffde3322b75839dd43e00296f63e5 Mon Sep 17 00:00:00 2001 From: Vivien Delmon Date: Mon, 25 Feb 2013 17:35:08 +0100 Subject: [PATCH] New tool to keep points in a mask --- tools/clitkMaskLandmarks.cxx | 104 +++++++++++++++++++++++++++++++++++ tools/clitkMaskLandmarks.ggo | 10 ++++ 2 files changed, 114 insertions(+) create mode 100644 tools/clitkMaskLandmarks.cxx create mode 100644 tools/clitkMaskLandmarks.ggo diff --git a/tools/clitkMaskLandmarks.cxx b/tools/clitkMaskLandmarks.cxx new file mode 100644 index 0000000..a63c85f --- /dev/null +++ b/tools/clitkMaskLandmarks.cxx @@ -0,0 +1,104 @@ +/*========================================================================= + Program: vv http://www.creatis.insa-lyon.fr/rio/vv + + Authors belong to: + - University of LYON http://www.universite-lyon.fr/ + - Léon Bérard cancer center http://www.centreleonberard.fr + - CREATIS CNRS laboratory http://www.creatis.insa-lyon.fr + + This software is distributed WITHOUT ANY WARRANTY; without even + the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR + PURPOSE. See the copyright notices for more information. + + It is distributed under dual licence + + - BSD See included LICENSE.txt file + - CeCILL-B http://www.cecill.info/licences/Licence_CeCILL-B_V1-en.html +===========================================================================*/ +#include "clitkMaskLandmarks_ggo.h" + +#include +#include +#include +#include + +#include +#include + +typedef itk::Point PointType; +typedef std::vector PointArrayType; + +static void read_points_pts(const std::string& fileName, PointArrayType& points, bool verbose) +{ + std::ifstream landmarksFile(fileName.c_str()); + if (landmarksFile.fail()) + { + std::cerr << "ERROR: could not open '" << fileName << "'" << std::endl; + exit(-2); + } + + std::string line; + std::getline(landmarksFile, line); + if (line.find("#X") != 0) + { + std::cerr << "ERROR: invalid landmarks file '" << fileName << "'" << std::endl; + exit(-3); + } + + PointType p; + while (!landmarksFile.eof()) + { + landmarksFile >> p[0] >> p[1] >> p[2]; + if (landmarksFile.fail()) + break; + if (verbose) + std::cout << "point " << p << std::endl; + points.push_back(p); + } +} + +void write_points_pts(const std::string& fileName, const PointArrayType& points, bool verbose) +{ + std::ofstream landmarksFile(fileName.c_str()); + + landmarksFile << "#X\tY\tZ" << std::endl; + for (size_t i = 0; i < points.size(); i++) + landmarksFile << points[i][0] << "\t" << points[i][1] << "\t" << points[i][2] << "\t" << std::endl; +} + +int main(int argc, char** argv) +{ + typedef itk::Image MaskType; + typedef MaskType::Pointer MaskPointer; + GGO(clitkMaskLandmarks, args_info); + + if (args_info.input_given != args_info.output_given) + { + std::cerr << "The number of inputs must be equal to the number of outputs" << std::endl;; + exit(1); + } + + std::vector inputs(args_info.input_given); + for (unsigned i = 0; i < args_info.input_given; ++i) + { + read_points_pts(args_info.input_arg[i], inputs[i], args_info.verbose_flag); + if (inputs[i].size() != inputs[0].size()) + { + std::cerr << "All input files must contain the same amount of points" << std::endl; + exit(2); + } + } + + MaskPointer mask = clitk::readImage(args_info.mask_arg, args_info.verbose_flag); + std::vector outputs(args_info.input_given); + for (unsigned i = 0; i < inputs[0].size(); ++i) + { + MaskType::IndexType idx; + if (mask->TransformPhysicalPointToIndex(inputs[0][i], idx) && (*mask)[idx]) + for (unsigned j = 0; j < args_info.input_given; j++) + outputs[j].push_back(inputs[j][i]); + } + + for (unsigned i = 0; i < args_info.input_given; ++i) + write_points_pts(args_info.output_arg[i], outputs[i], args_info.verbose_flag); +} diff --git a/tools/clitkMaskLandmarks.ggo b/tools/clitkMaskLandmarks.ggo new file mode 100644 index 0000000..82cfcc3 --- /dev/null +++ b/tools/clitkMaskLandmarks.ggo @@ -0,0 +1,10 @@ +package "clitkMaskLandmarks" +version "1.0" +purpose "Remove landmarks outside of a given mask also remove their corresponding points if multiple input are given." + +option "config" - "Config file" string no +option "verbose" v "Verbose" flag off + +option "input" i "Input landmarks filename" string yes multiple +option "mask" m "Input mask" string yes +option "output" o "Output landmarks filename" string yes multiple -- 2.47.1