Main MRPT website > C++ reference for MRPT 1.9.9
TMatchingPair.cpp
Go to the documentation of this file.
1 /* +------------------------------------------------------------------------+
2  | Mobile Robot Programming Toolkit (MRPT) |
3  | http://www.mrpt.org/ |
4  | |
5  | Copyright (c) 2005-2017, Individual contributors, see AUTHORS file |
6  | See: http://www.mrpt.org/Authors - All rights reserved. |
7  | Released under BSD License. See details in http://www.mrpt.org/License |
8  +------------------------------------------------------------------------+ */
9 
10 #include "base-precomp.h" // Precompiled headers
11 
14 #include <mrpt/utils/utils_defs.h>
15 #include <mrpt/poses/CPose2D.h>
16 #include <mrpt/system/os.h>
18 
19 using namespace mrpt;
20 using namespace mrpt::utils;
21 using namespace mrpt::math;
22 using namespace mrpt::poses;
23 using namespace mrpt::system;
24 using namespace std;
25 
26 /*---------------------------------------------------------------
27  dumpToFile
28  ---------------------------------------------------------------*/
29 void TMatchingPairList::dumpToFile(const std::string& fileName) const
30 {
31  CFileOutputStream f(fileName);
33 
34  for (const_iterator it = begin(); it != end(); ++it)
35  {
36  f.printf(
37  "%u %u %f %f %f %f %f %f %f\n", it->this_idx, it->other_idx,
38  it->this_x, it->this_y, it->this_z, it->other_x, it->other_y,
39  it->other_z, it->errorSquareAfterTransformation);
40  }
41 }
42 
43 /*---------------------------------------------------------------
44  saveAsMATLABScript
45  ---------------------------------------------------------------*/
47 {
48  FILE* f = os::fopen(filName.c_str(), "wt");
49 
50  fprintf(f, "%% ----------------------------------------------------\n");
51  fprintf(f, "%% File generated automatically by the MRPT method:\n");
52  fprintf(f, "%% saveAsMATLABScript \n");
53  fprintf(
54  f, "%% Before calling this script, define the color of lines, eg:\n");
55  fprintf(f, "%% colorLines=[1 1 1]");
56  fprintf(f, "%% J.L. Blanco (C) 2005-2012 \n");
57  fprintf(f, "%% ----------------------------------------------------\n\n");
58 
59  fprintf(f, "axis equal; hold on;\n");
60  for (const_iterator it = begin(); it != end(); ++it)
61  {
62  fprintf(
63  f, "line([%f %f],[%f %f],'Color',colorLines);\n", it->this_x,
64  it->other_x, it->this_y, it->other_y);
65  fprintf(
66  f,
67  "set(plot([%f %f],[%f "
68  "%f],'.'),'Color',colorLines,'MarkerSize',15);\n",
69  it->this_x, it->other_x, it->this_y, it->other_y);
70  }
71  os::fclose(f);
72 }
73 
74 /*---------------------------------------------------------------
75  indexOtherMapHasCorrespondence
76  ---------------------------------------------------------------*/
78 {
79  for (const_iterator it = begin(); it != end(); ++it)
80  {
81  if (it->other_idx == idx) return true;
82  }
83  return false;
84 }
85 
87 {
88  if (a.this_idx == b.this_idx)
89  return (a.this_idx < b.this_idx);
90  else
91  return (a.other_idx < b.other_idx);
92 }
93 
95 {
96  return (a.this_idx == b.this_idx) && (a.other_idx == b.other_idx);
97 }
98 
100  const TMatchingPairList& a, const TMatchingPairList& b)
101 {
102  if (a.size() != b.size()) return false;
103  for (TMatchingPairList::const_iterator it1 = a.begin(), it2 = b.begin();
104  it1 != a.end(); ++it1, ++it2)
105  if (!((*it1) == (*it2))) return false;
106  return true;
107 }
108 
109 /*---------------------------------------------------------------
110  overallSquareError
111  ---------------------------------------------------------------*/
113 {
114  vector<float> errs(size());
115  squareErrorVector(q, errs);
116  return math::sum(errs);
117 }
118 
119 /*---------------------------------------------------------------
120  overallSquareErrorAndPoints
121  ---------------------------------------------------------------*/
123  const CPose2D& q, vector<float>& xs, vector<float>& ys) const
124 {
125  vector<float> errs(size());
126  squareErrorVector(q, errs, xs, ys);
127  return math::sum(errs);
128 }
129 
130 /*---------------------------------------------------------------
131  TMatchingPairList::contains
132  ---------------------------------------------------------------*/
134 {
135  for (const_iterator corresp = begin(); corresp != end(); ++corresp)
136  if (*corresp == p) return true;
137  return false;
138 }
139 
140 /*---------------------------------------------------------------
141  squareErrorVector
142  ---------------------------------------------------------------*/
144  const CPose2D& q, vector<float>& out_sqErrs) const
145 {
146  out_sqErrs.resize(size());
147  // * \f[ e_i = | x_{this} - q \oplus x_{other} |^2 \f]
148 
149  const float ccos = cos(q.phi());
150  const float csin = sin(q.phi());
151  const float qx = q.x();
152  const float qy = q.y();
153 
154  const_iterator corresp;
156  for (corresp = begin(), e_i = out_sqErrs.begin(); corresp != end();
157  ++corresp, ++e_i)
158  {
159  float xx = qx + ccos * corresp->other_x - csin * corresp->other_y;
160  float yy = qy + csin * corresp->other_x + ccos * corresp->other_y;
161  *e_i = square(corresp->this_x - xx) + square(corresp->this_y - yy);
162  }
163 }
164 
165 /*---------------------------------------------------------------
166  squareErrorVector
167  ---------------------------------------------------------------*/
169  const CPose2D& q, vector<float>& out_sqErrs, vector<float>& xs,
170  vector<float>& ys) const
171 {
172  out_sqErrs.resize(size());
173  xs.resize(size());
174  ys.resize(size());
175 
176  // * \f[ e_i = | x_{this} - q \oplus x_{other} |^2 \f]
177 
178  const float ccos = cos(q.phi());
179  const float csin = sin(q.phi());
180  const float qx = q.x();
181  const float qy = q.y();
182 
183  const_iterator corresp;
184  vector<float>::iterator e_i, xx, yy;
185  for (corresp = begin(), e_i = out_sqErrs.begin(), xx = xs.begin(),
186  yy = ys.begin();
187  corresp != end(); ++corresp, ++e_i, ++xx, ++yy)
188  {
189  *xx = qx + ccos * corresp->other_x - csin * corresp->other_y;
190  *yy = qy + csin * corresp->other_x + ccos * corresp->other_y;
191  *e_i = square(corresp->this_x - *xx) + square(corresp->this_y - *yy);
192  }
193 }
194 
196  const size_t num_elements_this_map,
197  TMatchingPairList& out_filtered_list) const
198 {
199  std::vector<TMatchingPairConstPtr> bestMatchForThisMap(
200  num_elements_this_map, TMatchingPairConstPtr(nullptr));
201  out_filtered_list.clear();
202 
203  // 1) Go through all the correspondences and keep the best corresp.
204  // for each "global map" (this) point.
205  for (auto& c : *this)
206  {
207  if (bestMatchForThisMap[c.this_idx] == nullptr || // first one
208  c.errorSquareAfterTransformation <
209  bestMatchForThisMap[c.this_idx]
210  ->errorSquareAfterTransformation // or better
211  )
212  {
213  bestMatchForThisMap[c.this_idx] = &c;
214  }
215  }
216 
217  // 2) Go again through the list of correspondences and remove those
218  // who are not the best one for their corresponding global map.
219  for (auto& c : *this)
220  {
221  if (bestMatchForThisMap[c.this_idx] == &c)
222  out_filtered_list.push_back(c); // Add to the output
223  }
224 }
A class used to store a 2D pose, including the 2D coordinate point and a heading (phi) angle.
Definition: CPose2D.h:41
This CStream derived class allow using a file as a write-only, binary stream.
bool fileOpenCorrectly()
Returns true if the file was open without errors.
virtual int printf(const char *fmt,...) MRPT_printf_format_check(2
Writes a string to the stream in a textual form.
Definition: CStream.cpp:597
A list of TMatchingPair.
Definition: TMatchingPair.h:94
void filterUniqueRobustPairs(const size_t num_elements_this_map, TMatchingPairList &out_filtered_list) const
Creates a filtered list of pairings with those ones which have a single correspondence which coincide...
void saveAsMATLABScript(const std::string &filName) const
Saves the correspondences as a MATLAB script which draws them.
bool contains(const TMatchingPair &p) const
Test whether the given pair "p" is within the pairings.
void squareErrorVector(const mrpt::poses::CPose2D &q, std::vector< float > &out_sqErrs) const
Returns a vector with the square error between each pair of correspondences in the list,...
float overallSquareErrorAndPoints(const mrpt::poses::CPose2D &q, std::vector< float > &xs, std::vector< float > &ys) const
Computes the overall square error between the 2D points in the list of correspondences,...
float overallSquareError(const mrpt::poses::CPose2D &q) const
Computes the overall square error between the 2D points in the list of correspondences,...
void dumpToFile(const std::string &fileName) const
Saves the correspondences to a text file.
bool indexOtherMapHasCorrespondence(size_t idx) const
Checks if the given index from the "other" map appears in the list.
Scalar * iterator
Definition: eigen_plugins.h:26
const Scalar * const_iterator
Definition: eigen_plugins.h:27
const GLubyte * c
Definition: glext.h:6313
GLuint GLuint end
Definition: glext.h:3528
GLubyte GLubyte b
Definition: glext.h:6279
GLfloat GLfloat p
Definition: glext.h:6305
GLsizeiptr size
Definition: glext.h:3923
GLubyte GLubyte GLubyte a
Definition: glext.h:6279
GLsizei const GLchar ** string
Definition: glext.h:4101
GLdouble GLdouble GLdouble GLdouble q
Definition: glext.h:3721
int void fclose(FILE *f)
An OS-independent version of fclose.
Definition: os.cpp:272
FILE * fopen(const char *fileName, const char *mode) noexcept
An OS-independent version of fopen.
Definition: os.cpp:254
int fprintf(FILE *fil, const char *format,...) noexcept MRPT_printf_format_check(2
An OS-independent version of fprintf.
Definition: os.cpp:405
#define ASSERT_(f)
Definition: mrpt_macros.h:309
This base provides a set of functions for maths stuff.
Definition: CArrayNumeric.h:20
CONTAINER::Scalar sum(const CONTAINER &v)
Computes the sum of all the elements.
T square(const T x)
Inline function for the square of a number.
Definition: bits.h:55
Classes for 2D/3D geometry representation, both of single values and probability density distribution...
Definition: CPoint.h:18
This namespace provides a OS-independent interface to many useful functions: filenames manipulation,...
Definition: math_frwds.h:31
Classes for serialization, sockets, ini-file manipulation, streams, list of properties-values,...
bool operator==(const mrpt::utils::TCamera &a, const mrpt::utils::TCamera &b)
Definition: TCamera.cpp:204
const_iterator end() const
Definition: ts_hash_map.h:242
bool operator<(const TMatchingPair &a, const TMatchingPair &b)
A comparison operator, for sorting lists of TMatchingPair's, first order by this_idx,...
TMatchingPair const * TMatchingPairConstPtr
Definition: TMatchingPair.h:88
const_iterator begin() const
Definition: ts_hash_map.h:236
This is the global namespace for all Mobile Robot Programming Toolkit (MRPT) libraries.
This file implements several operations that operate element-wise on individual or pairs of container...
A structure for holding correspondences between two sets of points or points-like entities in 2D or 3...
Definition: TMatchingPair.h:32



Page generated by Doxygen 1.9.1 for MRPT 1.9.9 Git: 63ea9d1f1 Thu Nov 23 00:06:53 2017 +0100 at mar 26 may 2026 12:19:29 CEST