utils.hh
Go to the documentation of this file.
1 /* -*- mia-c++ -*-
2  *
3  * This file is part of MIA - a toolbox for medical image analysis
4  * Copyright (c) Leipzig, Madrid 1999-2013 Gert Wollny
5  *
6  * MIA is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 3 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with MIA; if not, see <http://www.gnu.org/licenses/>.
18  *
19  */
20 
21 #ifndef __MIA_TOOLS_HH
22 #define __MIA_TOOLS_HH 1
23 
24 #include <limits>
25 #include <string>
26 #include <sstream>
27 #include <vector>
28 #include <cmath>
29 #include <stdexcept>
30 #include <mia/core/defines.hh>
31 
32 
34 
43  char *cwd;
44 public:
45  CCWDSaver();
46  ~CCWDSaver();
47 };
48 
49 #if 0
50 
54 class FSearchFiles {
55  std::list<std::string>& result;
56  const std::string pattern;
57 public:
62  FSearchFiles(std::list<std::string>& __result, const std::string& __pattern);
63 
65  void operator()(const std::string& path);
66 };
67 #endif
68 
69 
70 #ifndef _GNU_SOURCE
71 
75 void sincosf(float x, float *sin, float *cos);
76 
81 void sincos(double x, double *sin, double *cos);
82 #endif
83 
90 template <typename T, bool is_float>
91 struct __round {
92 
93  static T apply(double x) {
94  return x;
95  }
96 };
97 
98 template <typename T>
99 struct __round<T, false> {
100  static T apply(double x) {
101  return static_cast<T>(rint(x));
102  }
103 };
104 
106 
116 template <typename T>
117 T mia_round(double x)
118 {
119  const bool is_floating_point = std::is_floating_point<T>::value;
120  return __round<T, is_floating_point>::apply(x);
121 }
122 
129 template <typename T, bool is_float>
130 struct __round_clamped {
131 
132  static T apply(double x) {
133  return x;
134  }
135 };
136 
137 template <>
138 struct __round_clamped<float, true> {
139 
140  static float apply(double x) {
141  double y = x < std::numeric_limits<float>::max() ?
142  ( x > -std::numeric_limits<float>::max() ? x : -std::numeric_limits<float>::max()) :
143  std::numeric_limits<float>::max();
144  return static_cast<float>(y);
145  }
146 };
147 
148 template <>
149 struct __round_clamped<bool, false> {
150  static float apply(double x) {
151  return x > 0.5;
152  }
153 };
154 
155 
156 template <typename T>
157 struct __round_clamped<T, false> {
158  static T apply(double x) {
159  const double y = rint(x);
160  const double yy = y < std::numeric_limits<T>::max() ?
161  ( y > std::numeric_limits<T>::min() ? y : std::numeric_limits<T>::min()) :
162  std::numeric_limits<T>::max();
163  return static_cast<T>(yy);
164  }
165 };
166 
168 
178 template <typename T>
179 T mia_round_clamped(double x)
180 {
181  const bool is_floating_point = std::is_floating_point<T>::value;
182  return __round_clamped<T, is_floating_point>::apply(x);
183 }
184 
186 
187 #endif