utils.h
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--2018,
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 #pragma once
36 
37 #include "PointMatcher.h"
38 
39 #include <vector>
40 #include <algorithm>
41 #include <cmath>
42 
43 namespace PointMatcherSupport
44 {
45 
46 template<class T>
47 inline constexpr T pow(const T base, const std::size_t exponent)
48 {
49  return exponent == 0 ? 1 : base * pow(base, exponent - 1);
50 }
51 
52 template<typename T>
53 struct IdxCompare
54 {
55  typedef typename PointMatcher<T>::Vector Vector;
56  const Vector& target;
57 
59 
60  bool operator()(size_t a, size_t b) const { return target(a, 0) < target(b, 0); }
61 };
62 
63 
64 template<typename T>
65 std::vector<size_t>
67 {
68  // initialize original index locations
69  const size_t idxSize = v.size();
70  std::vector<size_t> idx(idxSize);
71  for(size_t i=0; i < idxSize; ++i) idx[i]=i;
72 
73  // sort indexes based on comparing values in v
74  std::sort(idx.begin(), idx.end(), IdxCompare<T>(v));
75 
76  return idx;
77 }
78 
79 template<typename T>
81 sortEigenValues(const typename PointMatcher<T>::Vector& eigenVa)
82 {
83  // sort the eigenvalues in ascending order
84  typename PointMatcher<T>::Vector eigenVaSort = eigenVa;
85  std::sort(eigenVaSort.data(), eigenVaSort.data() + eigenVaSort.size());
86  return eigenVaSort;
87 }
88 
89 template<typename T>
91 serializeEigVec(const typename PointMatcher<T>::Matrix& eigenVe)
92 {
93  // serialize row major
94  const int eigenVeDim = eigenVe.cols();
95  typename PointMatcher<T>::Vector output(eigenVeDim*eigenVeDim);
96  for(int k=0; k < eigenVeDim; ++k)
97  {
98  output.segment(k*eigenVeDim, eigenVeDim) =
99  eigenVe.row(k).transpose();
100  }
101 
102  return output;
103 }
104 
105 template<typename T>
107 {
108  //volume in meter
109  const T volume = (4./3.)*M_PI*std::pow(NN.colwise().norm().maxCoeff(), 3);
110 
111  //volume in decimeter
112  //T volume = (4./3.)*M_PI*std::pow(NN.colwise().norm().maxCoeff()*10.0, 3);
113  //const T minVolume = 4.18e-9; // minimum of volume of one millimeter radius
114  //const T minVolume = 0.42; // minimum of volume of one centimeter radius (in dm^3)
115 
116  //if(volume < minVolume)
117  // volume = minVolume;
118 
119  return T(NN.cols())/(volume);
120 }
121 template<typename T>
123 computeNormal(const typename PointMatcher<T>::Vector& eigenVa, const typename PointMatcher<T>::Matrix& eigenVe)
124 {
125  // Keep the smallest eigenvector as surface normal
126  const int nbEigenCol = eigenVe.cols();
127  int smallestId(0);
128  T smallestValue(std::numeric_limits<T>::max());
129  for(int j = 0; j < nbEigenCol ; ++j)
130  {
131  if (eigenVa(j) < smallestValue)
132  {
133  smallestId = j;
134  smallestValue = eigenVa(j);
135  }
136  }
137 
138  return eigenVe.col(smallestId);
139 }
140 
141 template<typename T>
142 size_t argMax(const typename PointMatcher<T>::Vector& v)
143 {
144  //FIXME: Change that to use the new API. the new Eigen API (3.2.8) allows this with the call maxCoeff. See the section Visitors in https://eigen.tuxfamily.org/dox/group__TutorialReductionsVisitorsBroadcasting.html
145  const int size(v.size());
146  T maxVal(0);
147  size_t maxIdx(0);
148  for (int i = 0; i < size; ++i)
149  {
150  if (v[i] > maxVal)
151  {
152  maxVal = v[i];
153  maxIdx = i;
154  }
155  }
156  return maxIdx;
157 }
158 
159 };
PointMatcherSupport::IdxCompare
Definition: utils.h:53
PointMatcherSupport::computeNormal
PointMatcher< T >::Vector computeNormal(const typename PointMatcher< T >::Vector &eigenVa, const typename PointMatcher< T >::Matrix &eigenVe)
Definition: utils.h:123
PointMatcherSupport::sortIndexes
std::vector< size_t > sortIndexes(const typename PointMatcher< T >::Vector &v)
Definition: utils.h:66
PointMatcherSupport::IdxCompare::operator()
bool operator()(size_t a, size_t b) const
Definition: utils.h:60
PointMatcherSupport::IdxCompare::Vector
PointMatcher< T >::Vector Vector
Definition: utils.h:55
PointMatcher::Matrix
Eigen::Matrix< T, Eigen::Dynamic, Eigen::Dynamic > Matrix
A dense matrix over ScalarType.
Definition: PointMatcher.h:169
PointMatcher::Vector
Eigen::Matrix< T, Eigen::Dynamic, 1 > Vector
A vector over ScalarType.
Definition: PointMatcher.h:161
PointMatcherSupport::IdxCompare::target
const Vector & target
Definition: utils.h:56
M_PI
#define M_PI
PointMatcherSupport::computeDensity
T computeDensity(const typename PointMatcher< T >::Matrix &NN)
Definition: utils.h:106
PointMatcherSupport
Functions and classes that are not dependant on scalar type are defined in this namespace.
Definition: Bibliography.cpp:45
PointMatcherSupport::IdxCompare::IdxCompare
IdxCompare(const typename PointMatcher< T >::Vector &target)
Definition: utils.h:58
PointMatcherSupport::sortEigenValues
PointMatcher< T >::Vector sortEigenValues(const typename PointMatcher< T >::Vector &eigenVa)
Definition: utils.h:81
PointMatcherSupport::argMax
size_t argMax(const typename PointMatcher< T >::Vector &v)
Definition: utils.h:142
PointMatcher.h
public interface
PointMatcherSupport::pow
constexpr T pow(const T base, const std::size_t exponent)
Definition: utils.h:47
PointMatcherSupport::serializeEigVec
PointMatcher< T >::Vector serializeEigVec(const typename PointMatcher< T >::Matrix &eigenVe)
Definition: utils.h:91


mrpt_local_obstacles
Author(s): Jose-Luis Blanco-Claraco
autogenerated on Mon Aug 14 2023 02:09:04