36 #ifndef THE_UTILS_HXX_
37 #define THE_UTILS_HXX_
40 #ifndef _USE_MATH_DEFINES
41 #define _USE_MATH_DEFINES
58 #include <Core/ITKCommon/the_dynamic_array.hxx>
64 #define array2d( T ) std::vector<std::vector<T> >
69 #define array3d( T ) std::vector<std::vector<std::vector<T> > >
74 static const double TWO_PI = 2.0 * M_PI;
81 clamp_angle(
const double & absolute_angle)
83 double a = fmod(absolute_angle + TWO_PI, TWO_PI);
84 assert(a >= 0.0 && a < TWO_PI);
92 calc_angle(
const double & x,
94 const double & reference_angle = 0.0)
96 return clamp_angle(fmod(atan2(y, x) + TWO_PI, TWO_PI) -
97 fmod(reference_angle, TWO_PI));
103 extern void sleep_msec(
size_t msec);
108 inline static double drand()
109 {
return double(rand()) / double(RAND_MAX); }
114 template <
typename scalar_t>
116 integer_power(scalar_t x,
size_t p)
118 scalar_t result = scalar_t(1);
121 if (p & 1) result *= x;
132 template <
typename scalar_t>
134 closest_power_of_two_larger_than_given(
const scalar_t & given)
136 size_t n =
sizeof(given) * 8;
137 scalar_t closest = scalar_t(1);
139 (i < n) && (closest < given);
140 i++, closest *= scalar_t(2)) {}
149 template <
typename data_t>
151 divide(
const data_t & numerator,
const data_t & denominator)
153 static data_t zero = data_t(0);
154 return (denominator != zero) ? (numerator / denominator) : zero;
160 template <
typename data_t>
162 clear_stack(std::stack<data_t> & s)
173 template <
class data_t>
175 resize(array2d(data_t) & array,
180 for (
size_t j = 0; j < rows; j++)
182 array[j].resize(cols);
189 template <
class data_t>
191 assign(array2d(data_t) & array,
194 const data_t & value)
197 for (
size_t j = 0; j < rows; j++)
199 array[j].assign(cols, value);
206 template <
class data_t>
208 resize(array3d(data_t) & array,
209 const size_t & slices,
213 array.resize(slices);
214 for (
size_t i = 0; i < slices; i++)
216 array[i].resize(rows);
217 for (
size_t j = 0; j < rows; j++)
219 array[i][j].resize(cols);
228 template <
class container_t,
typename data_t>
230 push_back_unique(container_t & container,
233 typename container_t::const_iterator where =
234 std::find(container.begin(), container.end(), data);
235 if (where != container.end())
return;
237 container.push_back(data);
243 template <
class container_t,
typename data_t>
245 push_front_unique(container_t & container,
248 typename container_t::const_iterator where =
249 std::find(container.begin(), container.end(), data);
250 if (where != container.end())
return;
252 container.push_front(data);
258 template <
typename data_t>
260 remove_head(std::list<data_t> & container)
262 data_t head = container.front();
263 container.pop_front();
270 template <
typename data_t>
272 remove_tail(std::list<data_t> & container)
274 data_t tail = container.back();
275 container.pop_back();
282 template <
typename data_t>
284 remove_head(std::vector<data_t> & container)
286 data_t head = container.front();
287 container.erase(container.begin());
294 template <
typename data_t>
296 remove_tail(std::vector<data_t> & container)
298 data_t tail = container.back();
299 container.pop_back();
306 template <
typename container_t>
308 is_size_two_or_larger(
const container_t & c)
310 typename container_t::const_iterator i = c.begin();
311 typename container_t::const_iterator e = c.end();
312 return (i != e) && (++i != e);
318 template <
typename container_t>
320 is_size_three_or_larger(
const container_t & c)
322 typename container_t::const_iterator i = c.begin();
323 typename container_t::const_iterator e = c.end();
324 return (i != e) && (++i != e) && (++i != e);
330 template <
typename container_t>
332 is_size_one(
const container_t & c)
334 typename container_t::const_iterator i = c.begin();
335 typename container_t::const_iterator e = c.end();
336 return (i != e) && (++i == e);
343 template <
class container_t,
typename data_t>
345 replace(container_t & container,
const data_t & a,
const data_t & b)
347 typename container_t::iterator end = container.end();
348 typename container_t::iterator i = std::find(container.begin(), end, a);
349 if (i == end)
return false;
359 template <
typename data_t>
360 typename std::list<data_t>::const_iterator
361 iterator_at_index(
const std::list<data_t> & container,
362 const size_t & index)
364 typename std::list<data_t>::const_iterator iter = container.begin();
365 for (
size_t i = 0; i < index && iter != container.end(); i++, ++iter) ;
372 template <
typename data_t>
373 typename std::list<data_t>::iterator
374 iterator_at_index(std::list<data_t> & container,
375 const size_t & index)
377 typename std::list<data_t>::iterator iter = container.begin();
378 for (
size_t i = 0; i < index && iter != container.end(); i++, ++iter) ;
385 template <
typename data_t>
387 index_of(
const std::list<data_t> & container,
const data_t & data)
389 typename std::list<data_t>::const_iterator iter = container.begin();
390 for (
size_t i = 0; iter != container.end(); i++, ++iter)
392 if (data == *iter)
return i;
401 template <
typename data_t>
403 has(
const std::list<data_t> & container,
const data_t & data)
405 typename std::list<data_t>::const_iterator iter =
406 std::find(container.begin(), container.end(), data);
408 return iter != container.end();
414 template <
class container_t>
416 expand(container_t & a,
417 const container_t & b,
418 const bool unique =
false)
420 for (
typename container_t::const_iterator i = b.begin(); i != b.end(); ++i)
424 push_back_unique(a, *i);
438 template <
class iterator_t>
440 next(
const iterator_t & curr)
442 iterator_t tmp(curr);
449 template <
class iterator_t>
451 prev(
const iterator_t & curr)
453 iterator_t tmp(curr);
461 template <
typename stream_t,
typename data_t>
463 dump(stream_t & so,
const std::list<data_t> & c)
465 for (
typename std::list<data_t>::const_iterator
466 i = c.begin(); i != c.end(); ++i)
478 template <
typename stream_t,
typename data_t>
480 operator << (stream_t & so, const std::list<data_t> & c)
482 return dump<stream_t, data_t>(so, c);
522 template <
class container_t,
typename data_t>
527 const typename container_t::iterator & iter,
528 const bool & expand):
529 container_(container),
536 if (iter_ == container_.end())
540 iter_ = container_.insert(iter_, data);
558 container_t & container_;
561 typename container_t::iterator iter_;
571 template <
typename data_t>
573 operator << (std::vector<data_t> & container,
const data_t & data)
576 inserter(container, container.begin(),
false);
577 return inserter << data;
583 template <
typename data_t>
585 operator << (std::list<data_t> & container,
const data_t & data)
588 inserter(container, container.begin(),
true);
589 return inserter << data;
596 template <
size_t dimensions,
typename data_t>
598 calc_euclidian_distance_sqrd(
const std::vector<data_t> & a,
599 const std::vector<data_t> & b)
601 data_t distance_sqrd = data_t(0);
602 for (
size_t i = 0; i < dimensions; i++)
604 data_t d = a[i] - b[i];
605 distance_sqrd += d * d;
608 return distance_sqrd;
614 template <
size_t dimensions,
typename data_t>
616 calc_euclidian_distance(
const std::vector<data_t> & a,
617 const std::vector<data_t> & b)
619 data_t dist_sqrd = calc_euclidian_distance_sqrd<dimensions, data_t>(a, b);
620 double dist = sqrt(
double(dist_sqrd));
627 template <
typename data_t>
629 calc_frobenius_norm_sqrd(
const std::vector<data_t> & vec)
631 data_t L2_norm_sqrd = data_t(0);
633 const size_t len = vec.size();
634 for (
size_t i = 0; i < len; i++)
636 L2_norm_sqrd += vec[i] * vec[i];
645 template <
typename data_t>
647 calc_frobenius_norm(
const std::vector<data_t> & vec)
649 data_t norm_sqrd = calc_frobenius_norm_sqrd<data_t>(vec);
650 double norm = sqrt(
double(norm_sqrd));
657 template <
typename data_t>
659 normalize(std::vector<data_t> & vec)
661 data_t norm = calc_frobenius_norm<data_t>(vec);
663 const size_t len = vec.size();
664 for (
size_t i = 0; i < len; i++)
676 the_sign(
const T & a)
678 if (a < 0)
return -1;
691 copy_a_to_b(
const std::list<T> & container_a,
692 std::vector<T> & container_b)
694 container_b.assign(container_a.begin(), container_a.end());
704 copy_a_to_b(
const std::list<T> & container_a,
707 container_b.resize(container_a.size());
709 const size_t size = container_a.size();
710 typename std::list<T>::const_iterator iter = container_a.begin();
711 for (
size_t i = 0; i < size; i++, ++iter)
713 container_b[i] = *iter;
725 std::vector<T> & container_b)
727 container_b.resize(container_a.size());
729 const size_t & size = container_a.size();
730 for (
size_t i = 0; i < size; i++)
732 container_b[i] = container_a[i];
740 template <
typename T>
744 the_lock_t(T * lock,
bool lock_immediately =
true):
747 {
if (lock_immediately) arm(); }
749 the_lock_t(T & lock,
bool lock_immediately =
true):
752 {
if (lock_immediately) arm(); }
759 if (!armed_ && lock_ != NULL)
768 if (armed_ && lock_ != NULL)
787 template <
typename T>
796 assert(lock_->try_lock() ==
false);
805 assert(lock_->try_lock() ==
false);
828 template <
typename T>
833 const T & in_scope_value,
834 const T & out_of_scope_value):
837 out_(out_of_scope_value)
859 template <
typename T>
884 #ifndef THROW_ARG2_IF_FALSE
885 #define THROW_ARG2_IF_FALSE(predicate, arg2) \
886 if (predicate) {} else throw arg2
895 restore_console_stdio();
902 #define off_t __int64
907 extern int open_utf8(
const char * filename_utf8,
911 extern void open_utf8(std::fstream & fstream_to_open,
912 const char * filename_utf8,
913 std::ios_base::openmode mode);
915 extern FILE * fopen_utf8(
const char * filename_utf8,
918 extern int rename_utf8(
const char * old_utf8,
const char * new_utf8);
919 extern int remove_utf8(
const char * filename_utf8);
921 extern int rmdir_utf8(
const char * path_utf8);
922 extern int mkdir_utf8(
const char * path_utf8);
924 extern int fseek64(FILE * file, off_t offset,
int whence);
925 extern off_t ftell64(
const FILE * file);
928 close_enough(
const float & ref,
930 const float tolerance = 1e-6f)
932 float err = fabsf(given - ref);
933 return err < tolerance;
937 close_enough(
const double & ref,
938 const double & given,
939 const double tolerance = 1e-6)
941 double err = fabs(given - ref);
942 return err < tolerance;
947 #endif // THE_UTILS_HXX_
Definition: the_utils.hxx:741
Definition: the_utils.hxx:788
Definition: the_utils.hxx:860
Definition: the_dynamic_array.hxx:45
Definition: the_utils.hxx:829
Definition: the_utils.hxx:523
Definition: the_utils.cxx:107