aimstil  5.0.5
miscTools.h
Go to the documentation of this file.
1 #ifndef TIL_MISCTOOLS_H
2 #define TIL_MISCTOOLS_H
3 
4 // includes from STL
5 #include <cassert>
6 #include <iostream>
7 #include <limits>
8 #include <cstdio> // sprintf
9 
10 // includes from TIL library
11 #include "til/cat2type.h"
12 #include "til/traits.h"
13 
14 namespace til
15 {
16 
17  //---------------------------------------------------------------------------
18 
20  template < typename T >
21  T findValueOtherThan(T v1, T v2)
22  {
23  T res;
24  for (res = 0; res == v1 || res == v2; ++res) {}
25  return res;
26  }
27 
28  //---------------------------------------------------------------------------
29 
31  template < typename T >
32  T findValueOtherThan(T v1, T v2, T v3)
33  {
34  T res;
35  for (res = 0; res == v1 || res == v2 || res == v3; ++res) {}
36  return res;
37  }
38 
39  //---------------------------------------------------------------------------
40 
42  template < typename T >
43  T findValueOtherThan(T v1, T v2, T v3, T v4)
44  {
45  T res;
46  for (res = 0; res == v1 || res == v2 || res == v3 || res == v4; ++res) {}
47  return res;
48  }
49 
50  //---------------------------------------------------------------------------
51 
53  template < typename T >
54  T findValueOtherThan(T v1, T v2, T v3, T v4, T v5)
55  {
56  T res;
57  for (res = 0; res == v1 || res == v2 || res == v3 || res == v4 || res == v5; ++res) {}
58  return res;
59  }
60 
61  //---------------------------------------------------------------------------
62 
63 
64 namespace
65 {
66  template < typename TFrom, typename TTo >
67  inline TTo castValue_impl(TFrom value, bool_type<true>)
68  {
69  // TODO: I'm afraid we have to use a cat2type to avoid a warning here... :(
70  if (value < 0)
71  //if (std::numeric_limits<TFrom>::is_signed && value < 0)
72  {
73  return TTo(value - TFrom(0.5));
74  }
75  else
76  {
77  return TTo(value + TFrom(0.5));
78  }
79  }
80 
81  template < typename TFrom, typename TTo >
82  inline TTo castValue_impl(TFrom value, bool_type<false>)
83  {
84  return TTo(value);
85  }
86 }
87 
88 // Cast a numerical value from type TFrom to type TTo using the closest value
89 template < typename TFrom, typename TTo >
90 inline TTo castValue(TFrom value)
91 {
92  // NB: I used that instead of a more readable cat2type, because it involved two tests, and cat2type would get more
93  // complicated... Those two tests cannot be collapsed into one for cat2type because this test involves two template
94  // parameters, while cat2type is templated over a single templated parameter...
95  typedef bool_type<!std::numeric_limits<TFrom>::is_integer && std::numeric_limits<TTo>::is_integer> test_type;
96  return castValue_impl<TFrom,TTo>(value, test_type());
97 /*
98  if ( !std::numeric_limits<TFrom>::is_integer &&
99  std::numeric_limits<TTo >::is_integer)
100  {
101  // TODO: I'm afraid we have to use a cat2type to avoid a warning here... :(
102  if (value < 0)
103  //if (std::numeric_limits<TFrom>::is_signed && value < 0)
104  {
105  return TTo(value - TFrom(0.5));
106  }
107  else
108  {
109  return TTo(value + TFrom(0.5));
110  }
111  }
112  else
113  {
114  return TTo(value);
115  }
116  */
117 }
118 
119 // NB : the assignment makes it possible to use ImageAxis as an array index
120 // following this precise convention.
121 // TODO: this image axis crap should go away.
122 
123 enum ImageAxis { X_AXIS = 0, Y_AXIS = 1, Z_AXIS = 2 };
124 
126 
127 
128  //---------------------------------------------------------------------------
129 
133  template < typename T >
134  inline T type_min()
135  {
136  return std::numeric_limits<T>::is_integer?
139  }
140 
141  //---------------------------------------------------------------------------
142 
145  template < typename T >
146  inline
147  void shift_right(T a, T & b, T & c)
148  {
149  c = b;
150  b = a;
151  }
152 
153  //---------------------------------------------------------------------------
154 
157  template < typename T >
158  inline void shift_right(T a, T & b, T & c, T & d)
159  {
160  d = c;
161  c = b;
162  b = a;
163  }
164 
165  //---------------------------------------------------------------------------
166 
171  template < typename T >
173  {
174  public: // exceptions
175  class NoRoot : public std::exception {};
176  public: // operators
181  T operator()(T n, std::size_t N);
182  private: // functions
183  T compute(T n, std::size_t N, label::Passed<is_signed>);
184  T compute(T n, std::size_t N, label::Failed<is_signed>);
185  T solve(T n, std::size_t N);
186  };
187 
188  //---------------------------------------------------------------------------
189 
192  template < typename T >
193  T integer_root(T i, std::size_t n)
194  { return IntegerRoot<T>()(i, n); }
195 
196  //---------------------------------------------------------------------------
197 
199  template < typename T >
200  inline void sort(T & a, T & b, T & c);
201 
202  //---------------------------------------------------------------------------
203 
204  // Like sprintf, but returns the string -- usefull to concatenate in other
205  // functions.
206  // TODO: check std::string for similar use... that would spare us a .o file
207  TIL_API char * mySprintf(const char * format, ... );
208 
209  //---------------------------------------------------------------------------
210 
212  template < typename T >
213  inline void max_helper(T & maxx, T x)
214  { if (maxx < x) maxx = x; }
215 
216  //---------------------------------------------------------------------------
217 
219  template < typename T >
220  inline void min_helper(T & minx, T x)
221  { if (minx > x) minx = x; }
222 
223  //---------------------------------------------------------------------------
224 
225  template < typename TPIterator, typename TSIterator >
226  inline std::size_t pos2offset(TPIterator pbegin, TPIterator pend, TSIterator sbegin)
227  {
228  std::size_t res = *pbegin;
229  while (++pbegin != pend)
230  {
231  ++sbegin;
232  res *= *sbegin;
233  res += *pbegin;
234  }
235  return res;
236  }
237 
238  //---------------------------------------------------------------------------
239 
240  template < std::size_t D >
242  {
243  std::size_t i = D-1;
244  std::size_t res = pos[i];
245  // NB: we avoid using a 'for' loop because i is unsigned.
246  while (i != 0)
247  {
248  --i;
249  res *= size[i];
250  res += pos[i];
251  }
252  return res;
253  }
254 
255  //---------------------------------------------------------------------------
256 
257 } // namespace til
258 
259 // package include
260 #include "misc_tools.tpp"
261 
262 #endif
263 
void min_helper(T &minx, T x)
Little helper for something often used when looking for a minimum.
Definition: miscTools.h:220
Compute the N-root of an integer.
Definition: miscTools.h:172
T integer_root(T i, std::size_t n)
Return the n-th root of integer i.
Definition: miscTools.h:193
boost::enable_if< is_Image< TImage >, typename TImage::value_type >::type min(const TImage &im)
Belongs to package Box Do not include directly, include til/Box.h instead.
Definition: Accumulator.h:10
void sort(T &a, T &b, T &c)
Sort a, b, c in decreasing order (a>=b>=c).
T type_min()
Returns the min of a type.
Definition: miscTools.h:134
numeric_array< T, D > size(const Box< T, D > &box)
Return the size of a box.
Definition: boxTools.h:56
#define TIL_API
Definition: til_common.h:42
ImageAxis
Definition: miscTools.h:123
TTo castValue(TFrom value)
Definition: miscTools.h:90
TImage::value_type max(const TImage &im)
Returns the maximum intensity of the input image.
std::size_t pos2offset(TPIterator pbegin, TPIterator pend, TSIterator sbegin)
Definition: miscTools.h:226
TIL_API char * mySprintf(const char *format,...)
T findValueOtherThan(T v1, T v2)
Returns a number (a positive integer) with a different value than the input numbers.
Definition: miscTools.h:21
void max_helper(T &maxx, T x)
Little helper for something often used when looking for a maximum.
Definition: miscTools.h:213
A label corresponding to a positive answer of a specific is_a test.
Definition: cat2type.h:24
T operator()(T n, std::size_t N)
Return an N-root of integer n, that is, the integer i so that i^N = n.
void shift_right(T a, T &b, T &c)
Shifts value to the right, i.e c = b, b = a.
Definition: miscTools.h:147
A label corresponding to a negative answer of a specific is_a test.
Definition: cat2type.h:29
TIL_API ImageAxis operator++(ImageAxis &axis)