6 //---------------------------------------------------------------------------
8 /// Computes the centroid of a list of vertices.
9 template < typename TRes, typename TIterator >
10 void mean(TIterator begin, TIterator end, TRes & res)
13 // TODO: OK, counting is not necessary for random access iterators. Maybe some partial specialization could help.
14 std::size_t count = 0;
15 for (; begin != end; ++begin)
20 // Can't use accumulate because I can't force my functors to cast to othr type
21 // there is another reason: a = a + x is inefficient for classes.
22 //res = std::accumulate(c.begin(), c.end(), TPoint3D());
24 //mul(res, static_cast<typename value_type<TRes>::type>(1.0/size(c)));
25 res *= static_cast<typename value_type_of<TRes>::type>(1.0/count);
28 //---------------------------------------------------------------------------
30 template < typename TPoint, typename TPointCollection >
31 void stdev(const TPointCollection & c, TPoint & res)
35 typename TPointCollection::const_iterator iPoint = c.begin();
36 for (; iPoint != c.end(); ++iPoint)
38 typename TPoint::iterator i = res.begin();
39 typename TPointCollection::value_type::const_iterator j = iPoint->begin();
40 for (; i != res.end(); ++i, ++j)
47 typename TPoint::iterator i = res.begin();
48 for (; i != res.end(); ++i)
53 //mul(res, 1.0/size(c));
54 //res.data() *= 1.0/size(c);
57 res[0] = std::sqrt(res[0] - square(center[0]));
58 res[1] = std::sqrt(res[1] - square(center[1]));
59 res[2] = std::sqrt(res[2] - square(center[2]));
62 //---------------------------------------------------------------------------
64 // TODO: add a test so that it works only for signed integers... or is >> working only on signed integers anyway?
65 template < typename T >
68 if (n < 2) return false;
71 if (n%2 == 1 && n != 1) return false;
76 //---------------------------------------------------------------------------
78 /// Returns the largest m so that n >= 2^m.
79 /// Only for signed integer... TODO: add a check for that.
80 // NB: reason for using int: well, because int is largely sufficient to cover all possible
81 // exponent, so I don't want to put extra computing e.g log2(i)-2.
82 template < typename T >
86 for (; n > 0; n >>= 1) ++res;
90 //---------------------------------------------------------------------------
92 // TODO: problem is, if an int is passed, it is converted silently into an unsigned int... so the security
93 // of using an unsigned argument is kind of lost... Better accept signed input and check for sign explicitely
94 // inside the function it seems :/
95 template < typename T >
96 inline T exp2(unsigned int n)
99 for (; n > 0; --n) res *= 2;
105 while (--n) res <<= 1;
110 //---------------------------------------------------------------------------
112 template < typename TIndexCollection >
113 //typename boost::enable_if<boost::is_same<typename TIndexCollection::value_type::value_type, std::size_t>,
114 std::vector<std::list<std::size_t> >
116 invertIndices(const std::vector<TIndexCollection> & c)
118 // Do a first pass to get maximum index
120 for (std::size_t i = 0; i < size(c); ++i)
121 for (std::size_t j = 0; j < size(c[i]); ++j)
122 mx = max(c[i][j], mx);
125 std::vector<std::list<std::size_t> > res(mx+1);
128 for (std::size_t i = 0; i < size(c); ++i)
129 for (std::size_t j = 0; j < size(c[i]); ++j)
130 res[c[i][j]].push_back(i);
135 //---------------------------------------------------------------------------
137 template < typename T >
138 inline std::size_t max_index(T x0, T x1, T x2)
153 //---------------------------------------------------------------------------
155 template < typename T >
156 inline std::size_t min_index(T x0, T x1, T x2)
171 //---------------------------------------------------------------------------
173 template < typename T >
174 inline bool is_nan(T x)
176 // TODO: COMPILER_MAINTENANCE : this has to be regularly checked for compilers :(
178 // GNU has its own implementation
179 return std::isnan(x);
181 // The IEEE-compliant way -- but I guess it is compiler-dependant.
186 //---------------------------------------------------------------------------
188 template < typename T >
189 inline T lower_dyadic(T n)
192 while (res <= n) res *= 2;