RTAB-Map 0.25.2
Real-Time Appearance-Based Mapping
Loading...
Searching...
No Matches
UMath.h
Go to the documentation of this file.
1/*
2Copyright (c) 2010, Mathieu Labbe
3All rights reserved.
4
5Redistribution and use in source and binary forms, with or without
6modification, are permitted provided that the following conditions are met:
7 * Redistributions of source code must retain the above copyright
8 notice, this list of conditions and the following disclaimer.
9 * Redistributions in binary form must reproduce the above copyright
10 notice, this list of conditions and the following disclaimer in the
11 documentation and/or other materials provided with the distribution.
12 * Neither the name of the copyright holders nor the
13 names of its contributors may be used to endorse or promote products
14 derived from this software without specific prior written permission.
15
16THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
17ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY
20DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
21(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
23ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
25SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26*/
27
28#ifndef UMATH_H
29#define UMATH_H
30
35#include <cmath>
36#include <list>
37#include <vector>
38
39#if _MSC_VER
40 #undef min
41 #undef max
42#endif
43
47template<class T>
48inline bool uIsNan(const T & value)
49{
50#if _MSC_VER
51 return _isnan(value) != 0;
52#else
53 return std::isnan(value);
54#endif
55}
56
60template<class T>
61inline bool uIsFinite(const T & value)
62{
63#if _MSC_VER
64 return _finite(value) != 0;
65#else
66 return std::isfinite(value);
67#endif
68}
69
74template<class T>
75inline T uMin3( const T& a, const T& b, const T& c)
76{
77 T m=a<b?a:b;
78 return m<c?m:c;
79}
80
85template<class T>
86inline T uMax3( const T& a, const T& b, const T& c)
87{
88 T m=a>b?a:b;
89 return m>c?m:c;
90}
91
99template<class T>
100inline T uMax(const T * v, unsigned int size, unsigned int & index)
101{
102 T max = 0;
103 index = 0;
104 if(!v || size == 0)
105 {
106 return max;
107 }
108 max = v[0];
109 for(unsigned int i=1; i<size; ++i)
110 {
111 if(uIsNan(max) || (max < v[i] && !uIsNan(v[i])))
112 {
113 max = v[i];
114 index = i;
115 }
116 }
117
118 return max;
119}
120
127template<class T>
128inline T uMax(const std::vector<T> & v, unsigned int & index)
129{
130 return uMax(v.data(), v.size(), index);
131}
132
139template<class T>
140inline T uMax(const T * v, unsigned int size)
141{
142 unsigned int index;
143 return uMax(v, size, index);
144}
145
151template<class T>
152inline T uMax(const std::vector<T> & v)
153{
154 return uMax(v.data(), v.size());
155}
156
164template<class T>
165inline T uMin(const T * v, unsigned int size, unsigned int & index)
166{
167 T min = 0;
168 index = 0;
169 if(!v || size == 0)
170 {
171 return min;
172 }
173 min = v[0];
174 for(unsigned int i=1; i<size; ++i)
175 {
176 if(uIsNan(min) || (min > v[i] && !uIsNan(v[i])))
177 {
178 min = v[i];
179 index = i;
180 }
181 }
182
183 return min;
184}
185
192template<class T>
193inline T uMin(const std::vector<T> & v, unsigned int & index)
194{
195 return uMin(v.data(), v.size(), index);
196}
197
204template<class T>
205inline T uMin(const T * v, unsigned int size)
206{
207 unsigned int index;
208 return uMin(v, size, index);
209}
210
216template<class T>
217inline T uMin(const std::vector<T> & v)
218{
219 return uMin(v.data(), v.size());
220}
221
231template<class T>
232inline void uMinMax(const T * v, unsigned int size, T & min, T & max, unsigned int & indexMin, unsigned int & indexMax)
233{
234 min = 0;
235 max = 0;
236 indexMin = 0;
237 indexMax = 0;
238 if(!v || size == 0)
239 {
240 return;
241 }
242 min = v[0];
243 max = v[0];
244
245 for(unsigned int i=1; i<size; ++i)
246 {
247 if(uIsNan(min) || (min > v[i] && !uIsNan(v[i])))
248 {
249 min = v[i];
250 indexMin = i;
251 }
252
253 if(uIsNan(max) || (max < v[i] && !uIsNan(v[i])))
254 {
255 max = v[i];
256 indexMax = i;
257 }
258 }
259}
260
269template<class T>
270inline void uMinMax(const std::vector<T> & v, T & min, T & max, unsigned int & indexMin, unsigned int & indexMax)
271{
272 uMinMax(v.data(), v.size(), min, max, indexMin, indexMax);
273}
274
282template<class T>
283inline void uMinMax(const T * v, unsigned int size, T & min, T & max)
284{
285 unsigned int indexMin;
286 unsigned int indexMax;
287 uMinMax(v, size, min, max, indexMin, indexMax);
288}
289
296template<class T>
297inline void uMinMax(const std::vector<T> & v, T & min, T & max)
298{
299 uMinMax(v.data(), v.size(), min, max);
300}
301
307template<class T>
308inline int uSign(const T & v)
309{
310 if(v < 0)
311 {
312 return -1;
313 }
314 else
315 {
316 return 1;
317 }
318}
319
325template<class T>
326inline T uSum(const std::list<T> & list)
327{
328 T sum = 0;
329 for(typename std::list<T>::const_iterator i=list.begin(); i!=list.end(); ++i)
330 {
331 sum += *i;
332 }
333 return sum;
334}
335
342template<class T>
343inline T uSum(const T * v, unsigned int size)
344{
345 T sum = 0;
346 if(v && size)
347 {
348 for(unsigned int i=0; i<size; ++i)
349 {
350 sum += v[i];
351 }
352 }
353 return sum;
354}
355
361template<class T>
362inline T uSum(const std::vector<T> & v)
363{
364 return uSum(v.data(), (int)v.size());
365}
366
374template<class T>
375inline T uSumSquared(const T * v, unsigned int size, T subtract = T())
376{
377 T sum = 0;
378 if(v && size)
379 {
380 for(unsigned int i=0; i<size; ++i)
381 {
382 sum += (v[i]-subtract)*(v[i]-subtract);
383 }
384 }
385 return sum;
386}
387
394template<class T>
395inline T uSumSquared(const std::vector<T> & v, T subtract = T())
396{
397 return uSumSquared(v.data(), v.size(), subtract);
398}
399
406template<class T>
407inline T uMean(const T * v, unsigned int size)
408{
409 T buf = 0;
410 if(v && size)
411 {
412 for(unsigned int i=0; i<size; ++i)
413 {
414 buf += v[i];
415 }
416 buf /= size;
417 }
418 return buf;
419}
420
426template<class T>
427inline T uMean(const std::list<T> & list)
428{
429 T m = 0;
430 if(list.size())
431 {
432 for(typename std::list<T>::const_iterator i=list.begin(); i!=list.end(); ++i)
433 {
434 m += *i;
435 }
436 m /= list.size();
437 }
438 return m;
439}
440
446template<class T>
447inline T uMean(const std::vector<T> & v)
448{
449 return uMean(v.data(), v.size());
450}
451
460template<class T>
461inline T uMeanSquaredError(const T * x, unsigned int sizeX, const T * y, unsigned int sizeY)
462{
463 T sum = 0;
464 if(x && y && sizeX == sizeY)
465 {
466 for(unsigned int i=0; i<sizeX; ++i)
467 {
468 T diff = x[i]-y[i];
469 sum += diff*diff;
470 }
471 return sum/(T)sizeX;
472 }
473 return (T)-1;
474}
475
482template<class T>
483inline T uMeanSquaredError(const std::vector<T> & x, const std::vector<T> & y)
484{
485 return uMeanSquaredError(x.data(), x.size(), y.data(), y.size());
486}
487
496template<class T>
497inline T uVariance(const T * v, unsigned int size, T meanV)
498{
499 T buf = 0;
500 if(v && size>1)
501 {
502 float sum = 0;
503 for(unsigned int i=0; i<size; ++i)
504 {
505 sum += (v[i]-meanV)*(v[i]-meanV);
506 }
507 buf = sum/(size-1);
508 }
509 return buf;
510}
511
519template<class T>
520inline T uVariance(const std::list<T> & list, const T & m)
521{
522 T buf = 0;
523 if(list.size()>1)
524 {
525 float sum = 0;
526 for(typename std::list<T>::const_iterator i=list.begin(); i!=list.end(); ++i)
527 {
528 sum += (*i-m)*(*i-m);
529 }
530 buf = sum/(list.size()-1);
531 }
532 return buf;
533}
534
541template<class T>
542inline T uVariance(const T * v, unsigned int size)
543{
544 T m = uMean(v, size);
545 return uVariance(v, size, m);
546}
547
555template<class T>
556inline T uVariance(const std::vector<T> & v, const T & m)
557{
558 return uVariance(v.data(), v.size(), m);
559}
560
565template<class T>
566inline T uNormSquared(const std::vector<T> & v)
567{
568 float sum = 0.0f;
569 for(unsigned int i=0; i<v.size(); ++i)
570 {
571 sum += v[i]*v[i];
572 }
573 return sum;
574}
575
580template<class T>
581inline T uNorm(const std::vector<T> & v)
582{
583 return std::sqrt(uNormSquared(v));
584}
585
590template<class T>
591inline T uNormSquared(const T & x1, const T & x2)
592{
593 return x1*x1 + x2*x2;
594}
595
600template<class T>
601inline T uNorm(const T & x1, const T & x2)
602{
603 return std::sqrt(uNormSquared(x1, x2));
604}
605
610template<class T>
611inline T uNormSquared(const T & x1, const T & x2, const T & x3)
612{
613 return x1*x1 + x2*x2 + x3*x3;
614}
615
620template<class T>
621inline T uNorm(const T & x1, const T & x2, const T & x3)
622{
623 return std::sqrt(uNormSquared(x1, x2, x3));
624}
625
630template<class T>
631inline std::vector<T> uNormalize(const std::vector<T> & v)
632{
633 float norm = uNorm(v);
634 if(norm == 0)
635 {
636 return v;
637 }
638 else
639 {
640 std::vector<T> r(v.size());
641 for(unsigned int i=0; i<v.size(); ++i)
642 {
643 r[i] = v[i]/norm;
644 }
645 return r;
646 }
647}
648
652template<class T>
653inline std::list<unsigned int> uLocalMaxima(const T * v, unsigned int size)
654{
655 std::list<unsigned int> maxima;
656 if(size)
657 {
658 for(unsigned int i=0; i<size; ++i)
659 {
660 if(i == 0)
661 {
662 // first item
663 if((i+1 < size && v[i] > v[i+1]) ||
664 i+1 >= size)
665 {
666 maxima.push_back(i);
667 }
668 }
669 else if(i == size - 1)
670 {
671 //last item
672 if((i >= 1 && v[i] > v[i-1]) ||
673 i == 0)
674 {
675 maxima.push_back(i);
676 }
677 }
678 else
679 {
680 //all others, check previous and next
681 if(v[i] > v[i-1] && v[i] > v[i+1])
682 {
683 maxima.push_back(i);
684 }
685 }
686 }
687 }
688 return maxima;
689}
690
694template<class T>
695inline std::list<unsigned int> uLocalMaxima(const std::vector<T> & v)
696{
697 return uLocalMaxima(v.data(), v.size());
698}
699
704enum UXMatchMethod{UXCorrRaw, UXCorrBiased, UXCorrUnbiased, UXCorrCoeff, UXCovRaw, UXCovBiased, UXCovUnbiased, UXCovCoeff};
705
715template<class T>
716inline std::vector<T> uXMatch(const T * vA, const T * vB, unsigned int sizeA, unsigned int sizeB, UXMatchMethod method)
717{
718 if(!vA || !vB || sizeA == 0 || sizeB == 0)
719 {
720 return std::vector<T>();
721 }
722
723 std::vector<T> result(sizeA + sizeB - 1);
724
725 T meanA = 0;
726 T meanB = 0;
727 if(method > UXCorrCoeff)
728 {
729 meanA = uMean(vA, sizeA);
730 meanB = uMean(vB, sizeB);
731 }
732
733 T den = 1;
734 if(method == UXCorrCoeff || method == UXCovCoeff)
735 {
736 den = std::sqrt(uSumSquared(vA, sizeA, meanA) * uSumSquared(vB, sizeB, meanB));
737 }
738 else if(method == UXCorrBiased || method == UXCovBiased)
739 {
740 den = (T)std::max(sizeA, sizeB);
741 }
742
743 if(sizeA == sizeB)
744 {
745 T resultA;
746 T resultB;
747
748 int posA;
749 int posB;
750 unsigned int j;
751
752 // Optimization, filling two results at once
753 for(unsigned int i=0; i<sizeA; ++i)
754 {
755 if(method == UXCorrUnbiased || method == UXCovUnbiased)
756 {
757 den = 0;
758 }
759
760 posA = sizeA - i - 1;
761 posB = sizeB - i - 1;
762 resultA = 0;
763 resultB = 0;
764 for(j=0; (j + posB) < sizeB && (j + posA) < sizeA; ++j)
765 {
766 resultA += (vA[j] - meanA) * (vB[j + posB] - meanB);
767 resultB += (vA[j + posA] - meanA) * (vB[j] - meanB);
768 if(method == UXCorrUnbiased || method == UXCovUnbiased)
769 {
770 ++den;
771 }
772 }
773
774 result[i] = resultA / den;
775 result[result.size()-1 -i] = resultB / den;
776 }
777 }
778 else
779 {
780 for(unsigned int i=0; i<result.size(); ++i)
781 {
782 if(method == UXCorrUnbiased || method == UXCovUnbiased)
783 {
784 den = 0;
785 }
786
787 int posB = sizeB - i - 1;
788 T r = 0;
789 if(posB >= 0)
790 {
791 for(unsigned int j=0; (j + posB) < sizeB && j < sizeA; ++j)
792 {
793 r += (vA[j] - meanA) * (vB[j + posB] - meanB);
794 if(method == UXCorrUnbiased || method == UXCovUnbiased)
795 {
796 ++den;
797 }
798 }
799 }
800 else
801 {
802 int posA = posB*-1;
803 for(unsigned int i=0; (i+posA) < sizeA && i < sizeB; ++i)
804 {
805 r += (vA[i+posA] - meanA) * (vB[i] - meanB);
806 if(method == UXCorrUnbiased || method == UXCovUnbiased)
807 {
808 ++den;
809 }
810 }
811 }
812
813 result[i] = r / den;
814 }
815 }
816
817 return result;
818}
819
827template<class T>
828inline std::vector<T> uXMatch(const std::vector<T> & vA, const std::vector<T> & vB, UXMatchMethod method)
829{
830 return uXMatch(vA.data(), vB.data(), vA.size(), vB.size(), method);
831}
832
843template<class T>
844inline T uXMatch(const T * vA, const T * vB, unsigned int sizeA, unsigned int sizeB, unsigned int index, UXMatchMethod method)
845{
846 T result = 0;
847 if(!vA || !vB || sizeA == 0 || sizeB == 0)
848 {
849 return result;
850 }
851
852 T meanA = 0;
853 T meanB = 0;
854 if(method > UXCorrCoeff)
855 {
856 meanA = uMean(vA, sizeA);
857 meanB = uMean(vB, sizeB);
858 }
859 unsigned int size = sizeA + sizeB - 1;
860
861 T den = 1;
862 if(method == UXCorrCoeff || method == UXCovCoeff)
863 {
864 den = std::sqrt(uSumSquared(vA, sizeA, meanA) * uSumSquared(vB, sizeB, meanB));
865 }
866 else if(method == UXCorrBiased || method == UXCovBiased)
867 {
868 den = (T)std::max(sizeA, sizeB);
869 }
870 else if(method == UXCorrUnbiased || method == UXCovUnbiased)
871 {
872 den = 0;
873 }
874
875 if(index < size)
876 {
877 int posB = sizeB - index - 1;
878 unsigned int i;
879 if(posB >= 0)
880 {
881 for(i=0; (i + posB) < sizeB && i < sizeA; ++i)
882 {
883 result += (vA[i] - meanA) * (vB[i + posB] - meanB);
884 if(method == UXCorrUnbiased || method == UXCovUnbiased)
885 {
886 ++den;
887 }
888 }
889 }
890 else
891 {
892 int posA = posB*-1;
893 for(i=0; (i+posA) < sizeA && i < sizeB; ++i)
894 {
895 result += (vA[i+posA] - meanA) * (vB[i] - meanB);
896 if(method == UXCorrUnbiased || method == UXCovUnbiased)
897 {
898 ++den;
899 }
900 }
901 }
902 }
903 return result / den;
904}
905
916template<class T>
917inline T uXMatch(const std::vector<T> & vA, const std::vector<T> & vB, unsigned int index, UXMatchMethod method)
918{
919 return uXMatch(vA.data(), vB.data(), vA.size(), vB.size(), index, method);
920}
921
927inline std::vector<float> uHamming(unsigned int L)
928{
929 std::vector<float> w(L);
930 unsigned int N = L-1;
931 float pi = 3.14159265f;
932 for(unsigned int n=0; n<N; ++n)
933 {
934 w[n] = 0.54f-0.46f*std::cos(2.0f*pi*float(n)/float(N));
935 }
936 return w;
937}
938
939template <typename T>
940bool uIsInBounds(const T& value, const T& low, const T& high)
941{
942 return uIsFinite(value) && !(value < low) && !(value >= high);
943}
944
945#endif // UMATH_H
bool uIsNan(const T &value)
Definition UMath.h:48
T uMax(const T *v, unsigned int size, unsigned int &index)
Definition UMath.h:100
T uMeanSquaredError(const T *x, unsigned int sizeX, const T *y, unsigned int sizeY)
Definition UMath.h:461
std::vector< T > uNormalize(const std::vector< T > &v)
Definition UMath.h:631
std::list< unsigned int > uLocalMaxima(const T *v, unsigned int size)
Definition UMath.h:653
T uMax3(const T &a, const T &b, const T &c)
Definition UMath.h:86
int uSign(const T &v)
Definition UMath.h:308
T uMean(const T *v, unsigned int size)
Definition UMath.h:407
T uSum(const std::list< T > &list)
Definition UMath.h:326
std::vector< T > uXMatch(const T *vA, const T *vB, unsigned int sizeA, unsigned int sizeB, UXMatchMethod method)
Definition UMath.h:716
T uMin(const T *v, unsigned int size, unsigned int &index)
Definition UMath.h:165
T uNorm(const std::vector< T > &v)
Definition UMath.h:581
bool uIsFinite(const T &value)
Definition UMath.h:61
T uMin3(const T &a, const T &b, const T &c)
Definition UMath.h:75
T uVariance(const T *v, unsigned int size, T meanV)
Definition UMath.h:497
std::vector< float > uHamming(unsigned int L)
Definition UMath.h:927
UXMatchMethod
Definition UMath.h:704
T uSumSquared(const T *v, unsigned int size, T subtract=T())
Definition UMath.h:375
T uNormSquared(const std::vector< T > &v)
Definition UMath.h:566
void uMinMax(const T *v, unsigned int size, T &min, T &max, unsigned int &indexMin, unsigned int &indexMax)
Definition UMath.h:232