align_sequence.cpp
Go to the documentation of this file.
1 // kate: replace-tabs off; indent-width 4; indent-mode normal
2 // vim: ts=4:sw=4:noexpandtab
3 /*
4 
5 Copyright (c) 2010--2012,
6 François Pomerleau and Stephane Magnenat, ASL, ETHZ, Switzerland
7 You can contact the authors at <f dot pomerleau at gmail dot com> and
8 <stephane at magnenat dot net>
9 
10 All rights reserved.
11 
12 Redistribution and use in source and binary forms, with or without
13 modification, are permitted provided that the following conditions are met:
14  * Redistributions of source code must retain the above copyright
15  notice, this list of conditions and the following disclaimer.
16  * Redistributions in binary form must reproduce the above copyright
17  notice, this list of conditions and the following disclaimer in the
18  documentation and/or other materials provided with the distribution.
19  * Neither the name of the <organization> nor the
20  names of its contributors may be used to endorse or promote products
21  derived from this software without specific prior written permission.
22 
23 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
24 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
25 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
26 DISCLAIMED. IN NO EVENT SHALL ETH-ASL BE LIABLE FOR ANY
27 DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
28 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
29 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
30 ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
31 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
32 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33 
34 */
35 
37 #include "pointmatcher/IO.h"
38 #include <cassert>
39 #include <iostream>
40 #include <fstream>
41 
42 
43 using namespace std;
44 using namespace PointMatcherSupport;
45 
46 void validateArgs(int argc, char *argv[]);
47 
54 int main(int argc, char *argv[])
55 {
56  validateArgs(argc, argv);
57 
58  typedef PointMatcher<float> PM;
61  typedef PM::DataPoints DP;
62 
63  string outputFileName(argv[0]);
64 
65  // Rigid transformation
66  std::shared_ptr<PM::Transformation> rigidTrans;
67  rigidTrans = PM::get().REG(Transformation).create("RigidTransformation");
68 
69  // Create filters manually to clean the global map
70  std::shared_ptr<PM::DataPointsFilter> densityFilter =
71  PM::get().DataPointsFilterRegistrar.create(
72  "SurfaceNormalDataPointsFilter",
73  {
74  {"knn", "10"},
75  {"epsilon", "5"},
76  {"keepNormals", "0"},
77  {"keepDensities", "1"}
78  }
79  );
80 
81  std::shared_ptr<PM::DataPointsFilter> maxDensitySubsample =
82  PM::get().DataPointsFilterRegistrar.create(
83  "MaxDensityDataPointsFilter",
84  {{"maxDensity", toParam(30)}}
85  );
86  // Main algorithm definition
87  PM::ICP icp;
88 
89  // load YAML config
90  ifstream ifs(argv[1]);
91  validateFile(argv[1]);
92  icp.loadFromYaml(ifs);
93 
94  PMIO::FileInfoVector list(argv[2]);
95 
96  PM::DataPoints mapPointCloud, newCloud;
97  TP T_to_map_from_new = TP::Identity(4,4); // assumes 3D
98 
99  for(unsigned i=0; i < list.size(); i++)
100  {
101  cout << "---------------------\nLoading: " << list[i].readingFileName << endl;
102 
103  // It is assume that the point cloud is express in sensor frame
104  newCloud = DP::load(list[i].readingFileName);
105 
106  if(mapPointCloud.getNbPoints() == 0)
107  {
108  mapPointCloud = newCloud;
109  continue;
110  }
111 
112  // call ICP
113  try
114  {
115  // We use the last transformation as a prior
116  // this assumes that the point clouds were recorded in
117  // sequence.
118  const TP prior = T_to_map_from_new;
119 
120  T_to_map_from_new = icp(newCloud, mapPointCloud, prior);
121  }
122  catch (PM::ConvergenceError& error)
123  {
124  cout << "ERROR PM::ICP failed to converge: " << endl;
125  cout << " " << error.what() << endl;
126  continue;
127  }
128 
129  // This is not necessary in this example, but could be
130  // useful if the same matrix is composed in the loop.
131  T_to_map_from_new = rigidTrans->correctParameters(T_to_map_from_new);
132 
133  // Move the new point cloud in the map reference
134  newCloud = rigidTrans->compute(newCloud, T_to_map_from_new);
135 
136  // Merge point clouds to map
137  mapPointCloud.concatenate(newCloud);
138 
139  // Clean the map
140  mapPointCloud = densityFilter->filter(mapPointCloud);
141  mapPointCloud = maxDensitySubsample->filter(mapPointCloud);
142 
143  // Save the map at each iteration
144  stringstream outputFileNameIter;
145  outputFileNameIter << outputFileName << "_" << i << ".vtk";
146 
147  cout << "outputFileName: " << outputFileNameIter.str() << endl;
148  mapPointCloud.save(outputFileNameIter.str());
149  }
150 
151  return 0;
152 }
153 
154 void validateArgs(int argc, char *argv[])
155 {
156  if (!(argc == 3))
157  {
158  cerr << "Error in command line, usage " << argv[0] << " icpConfiguration.yaml listOfFiles.csv" << endl;
159  cerr << endl << "Example:" << endl;
160  cerr << argv[0] << " ../examples/data/default.yaml ../examples/data/carCloudList.csv" << endl;
161  cerr << endl << " - or - " << endl << endl;
162  cerr << argv[0] << " ../examples/data/default.yaml ../examples/data/cloudList.csv" << endl << endl;
163 
164  abort();
165  }
166 }
167 
168 
validateArgs
void validateArgs(int argc, char *argv[])
Definition: align_sequence.cpp:154
PointMatcherIO
IO Functions and classes that are dependant on scalar type are defined in this templatized class.
Definition: IO.h:43
align_sequence.T_to_map_from_new
T_to_map_from_new
Definition: align_sequence.py:55
PointMatcherSupport::toParam
std::string toParam(const S &value)
Return the string value using lexical_cast.
Definition: Parametrizable.h:123
align_sequence.prior
prior
Definition: align_sequence.py:72
PointMatcherSupport::validateFile
void validateFile(const std::string &fileName)
Throw a runtime_error exception if fileName cannot be opened.
Definition: pointmatcher/IO.cpp:341
DataPoints
PM::DataPoints DataPoints
Definition: pypoint_matcher_helper.h:16
PointMatcher< float >
align_sequence.icp
icp
Definition: align_sequence.py:40
main
int main(int argc, char *argv[])
Definition: align_sequence.cpp:54
PointMatcher::Transformation
A function that transforms points and their descriptors given a transformation matrix.
Definition: PointMatcher.h:404
PMIO
PointMatcherIO< float > PMIO
Definition: eval_solution.cpp:62
PointMatcherIO::FileInfoVector
A vector of file info, to be used in batch processing.
Definition: IO.h:245
ICP
PM::ICP ICP
Definition: pypoint_matcher_helper.h:33
std
TP
PM::TransformationParameters TP
Definition: eval_solution.cpp:63
icp
Definition: icp.py:1
PointMatcher< float >::get
static const PointMatcher & get()
Return instances.
Definition: Registry.cpp:147
PointMatcherSupport
Functions and classes that are not dependant on scalar type are defined in this namespace.
Definition: Bibliography.cpp:45
DP
PM::DataPoints DP
Definition: convert.cpp:45
PM
PointMatcher< float > PM
Definition: eval_solution.cpp:61
PointMatcher::DataPoints::load
static DataPoints load(const std::string &fileName)
Load a point cloud from a file, determine format from extension.
Definition: pointmatcher/IO.cpp:353
PointMatcher.h
public interface
IO.h
PointMatcher< float >::TransformationParameters
Matrix TransformationParameters
A matrix holding the parameters a transformation.
Definition: PointMatcher.h:182


libpointmatcher
Author(s):
autogenerated on Sun Dec 22 2024 03:21:52