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-2017 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 <iostream>
28 #include <vector>
29 #include <cmath>
30 #include <stdexcept>
31 #include <mia/core/defines.hh>
32 
33 
35 
44  char *cwd;
45 public:
46  CCWDSaver();
47  ~CCWDSaver();
48 };
49 
50 #if 0
51 
55 class FSearchFiles {
56  std::list<std::string>& result;
57  const std::string pattern;
58 public:
63  FSearchFiles(std::list<std::string>& __result, const std::string& __pattern);
64 
66  void operator()(const std::string& path);
67 };
68 #endif
69 
70 
71 #ifndef _GNU_SOURCE
72 
76 void EXPORT_CORE sincosf(float x, float *sin, float *cos);
77 
82 void EXPORT_CORE sincos(double x, double *sin, double *cos);
83 #endif
84 
91 template <typename T, bool is_float>
92 struct __round {
93 
94  static T apply(double x) {
95  return x;
96  }
97 };
98 
99 template <typename T>
100 struct __round<T, false> {
101  static T apply(double x) {
102  return static_cast<T>(rint(x));
103  }
104 };
105 
107 
117 template <typename T>
118 T mia_round(double x)
119 {
120  const bool is_floating_point = std::is_floating_point<T>::value;
121  return __round<T, is_floating_point>::apply(x);
122 }
123 
130 template <typename T, bool is_float>
131 struct __round_clamped {
132 
133  static T apply(double x) {
134  return x;
135  }
136 };
137 
138 template <>
139 struct __round_clamped<float, true> {
140 
141  static float apply(double x) {
142  double y = x < std::numeric_limits<float>::max() ?
143  ( x > -std::numeric_limits<float>::max() ? x : -std::numeric_limits<float>::max()) :
144  std::numeric_limits<float>::max();
145  return static_cast<float>(y);
146  }
147 };
148 
149 template <>
150 struct __round_clamped<bool, false> {
151  static float apply(double x) {
152  return x > 0.5;
153  }
154 };
155 
156 
157 template <typename T>
158 struct __round_clamped<T, false> {
159  static T apply(double x) {
160  const double y = rint(x);
161  const double yy = y < std::numeric_limits<T>::max() ?
162  ( y > std::numeric_limits<T>::min() ? y : std::numeric_limits<T>::min()) :
163  std::numeric_limits<T>::max();
164  return static_cast<T>(yy);
165  }
166 };
167 
169 
179 template <typename T>
180 T mia_round_clamped(double x)
181 {
182  const bool is_floating_point = std::is_floating_point<T>::value;
183  return __round_clamped<T, is_floating_point>::apply(x);
184 }
185 
186 
187 inline void eat_char( std::istream& is, char expect_val, const char *message)
188 {
189  char c;
190  is >> c;
191  if ( c != expect_val)
192  throw std::runtime_error(message);
193 }
194 
196 
197 #endif
T mia_round_clamped(double x)
Definition: utils.hh:180
void EXPORT_CORE sincos(double x, double *sin, double *cos)
#define NS_MIA_BEGIN
conveniance define to start the mia namespace
Definition: defines.hh:33
T mia_round(double x)
Definition: utils.hh:118
A Scope based helper class to save and restore the current working directory.
Definition: utils.hh:43
#define EXPORT_CORE
Macro to manage Visual C++ style dllimport/dllexport.
Definition: defines.hh:101
void EXPORT_CORE sincosf(float x, float *sin, float *cos)
void eat_char(std::istream &is, char expect_val, const char *message)
Definition: utils.hh:187
#define NS_MIA_END
conveniance define to end the mia namespace
Definition: defines.hh:36