Jpp 21.0.0
the software that should make you happy
Loading...
Searching...
No Matches
JWatershed.hh
Go to the documentation of this file.
1#ifndef __JMATH__JWATERSHED__
2#define __JMATH__JWATERSHED__
3
4#include <limits>
5#include <algorithm>
6#include <utility>
7#include <iterator>
8
9
10namespace JMATH {}
11namespace JPP { using namespace JMATH; }
12
13namespace JMATH {
14
15 /**
16 * Watershed algorithm.
17 *
18 * This algorithm can be used to find distict maxima in the data according the given function.
19 *
20 * \param __begin begin of data points
21 * \param __end end of data points
22 * \param getValue function to compute value of data point
23 * \param getDistance function to compute distance between data points
24 * \param Y margin
25 * \param N maximim number of selected elements
26 * \return end of data points
27 */
28 template<class T, class JFunction_t, class JDistance_t>
29 T watershed(T __begin,
30 T __end,
31 const JFunction_t& getValue,
32 const JDistance_t& getDistance,
33 double Y,
34 int N = std::numeric_limits<int>::max())
35 {
36 using namespace std;
37
38 typedef typename iterator_traits<T>::reference reference_type;
39
40 T out = __begin; // output
41
42 for (T p1 = __begin; p1 != __end && distance(__begin, out) < N; ) {
43
44 T q1 = p1; // q1 -> best-of-the-rest
45
46 for (T i = p1; i != __end; ++i) {
47 if (getValue(*i) > getValue(*q1)) {
48 q1 = i;
49 }
50 }
51
52 swap(*q1, *p1); // put best-of-the-rest at head
53
54 q1 = p1++; // q1 = head; p1 = next to head
55
56 if (p1 != __end) {
57
58 auto compare = [q1,getDistance](const reference_type first,
59 const reference_type second)
60 {
61 return getDistance(*q1,first) < getDistance(*q1,second);
62 };
63
64 sort(p1, __end, compare); // sort according distance with respect to current maximum
65
66 double y = getValue(*q1);
67
68 for (T p2 = next(p1); p2 != __end &&
69 (getValue(*p1) <= y + Y ||
70 getValue(*p2) <= y); // descend
71 ++p1, ++p2) {
72 y = getValue(*p1); // update
73 }
74 }
75
76 swap(*(out++), *q1); // output head
77 }
78
79 return out;
80 }
81}
82
83#endif
TPaveText * p1
std::vector< T >::difference_type distance(typename std::vector< T >::const_iterator first, typename PhysicsEvent::const_iterator< T > second)
Specialisation of STL distance.
T watershed(T __begin, T __end, const JFunction_t &getValue, const JDistance_t &getDistance, double Y, int N=std::numeric_limits< int >::max())
Watershed algorithm.
Definition JWatershed.hh:29
double getDistance(const JFirst_t &first, const JSecond_t &second)
Get distance between objects.
This name space includes all other name spaces (except KM3NETDAQ, KM3NET and ANTARES).