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 the eigen vectors column major
94  const int eigenVeDim = eigenVe.cols();
95  typename PointMatcher<T>::Vector output(eigenVeDim*eigenVeDim);
96 
97  for (int k = 0; k < eigenVeDim; ++k)
98  output.segment(k * eigenVeDim, eigenVeDim) = eigenVe.col(k);
99 
100  return output;
101 }
102 
103 template<typename T>
105 {
106  //volume in meter
107  const T volume = (4./3.)*M_PI*std::pow(NN.colwise().norm().maxCoeff(), 3);
108 
109  //volume in decimeter
110  //T volume = (4./3.)*M_PI*std::pow(NN.colwise().norm().maxCoeff()*10.0, 3);
111  //const T minVolume = 4.18e-9; // minimum of volume of one millimeter radius
112  //const T minVolume = 0.42; // minimum of volume of one centimeter radius (in dm^3)
113 
114  //if(volume < minVolume)
115  // volume = minVolume;
116 
117  return T(NN.cols())/(volume);
118 }
119 template<typename T>
121 computeNormal(const typename PointMatcher<T>::Vector& eigenVa, const typename PointMatcher<T>::Matrix& eigenVe)
122 {
123  // Keep the smallest eigenvector as surface normal
124  const int nbEigenCol = eigenVe.cols();
125  int smallestId(0);
126  T smallestValue(std::numeric_limits<T>::max());
127  for(int j = 0; j < nbEigenCol ; ++j)
128  {
129  if (eigenVa(j) < smallestValue)
130  {
131  smallestId = j;
132  smallestValue = eigenVa(j);
133  }
134  }
135 
136  return eigenVe.col(smallestId);
137 }
138 
139 template<typename T>
140 size_t argMax(const typename PointMatcher<T>::Vector& v)
141 {
142  //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
143  const int size(v.size());
144  T maxVal(0);
145  size_t maxIdx(0);
146  for (int i = 0; i < size; ++i)
147  {
148  if (v[i] > maxVal)
149  {
150  maxVal = v[i];
151  maxIdx = i;
152  }
153  }
154  return maxIdx;
155 }
156 
157 };
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:121
PointMatcherSupport::sortIndexes
std::vector< size_t > sortIndexes(const typename PointMatcher< T >::Vector &v)
Definition: utils.h:66
build_map.T
T
Definition: build_map.py:34
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
PointMatcherSupport::computeDensity
T computeDensity(const typename PointMatcher< T >::Matrix &NN)
Definition: utils.h:104
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:140
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


libpointmatcher
Author(s):
autogenerated on Mon Jul 1 2024 02:22:43