My Project
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 {
45  char *cwd;
46 public:
49 };
50 
51 #if 0
52 
56 class FSearchFiles
57 {
58  std::list<std::string>& result;
59  const std::string pattern;
60 public:
65  FSearchFiles(std::list<std::string>& __result, const std::string& __pattern);
66 
68  void operator()(const std::string& path);
69 };
70 #endif
71 
72 
73 #ifndef _GNU_SOURCE
74 
78 void EXPORT_CORE sincosf(float x, float *sin, float *cos);
79 
84 void EXPORT_CORE sincos(double x, double *sin, double *cos);
85 #endif
86 
93 template <typename T, bool is_float>
94 struct __round {
95 
96  static T apply(double x)
97  {
98  return x;
99  }
100 };
101 
102 template <typename T>
103 struct __round<T, false> {
104  static T apply(double x)
105  {
106  return static_cast<T>(rint(x));
107  }
108 };
109 
111 
121 template <typename T>
122 T mia_round(double x)
123 {
124  const bool is_floating_point = std::is_floating_point<T>::value;
125  return __round<T, is_floating_point>::apply(x);
126 }
127 
134 template <typename T, bool is_float>
135 struct __round_clamped {
136 
137  static T apply(double x)
138  {
139  return x;
140  }
141 };
142 
143 template <>
144 struct __round_clamped<float, true> {
145 
146  static float apply(double x)
147  {
148  double y = x < std::numeric_limits<float>::max() ?
149  ( x > -std::numeric_limits<float>::max() ? x : -std::numeric_limits<float>::max()) :
150  std::numeric_limits<float>::max();
151  return static_cast<float>(y);
152  }
153 };
154 
155 template <>
156 struct __round_clamped<bool, false> {
157  static float apply(double x)
158  {
159  return x > 0.5;
160  }
161 };
162 
163 
164 template <typename T>
165 struct __round_clamped<T, false> {
166  static T apply(double x)
167  {
168  const double y = rint(x);
169  const double yy = y < std::numeric_limits<T>::max() ?
170  ( y > std::numeric_limits<T>::min() ? y : std::numeric_limits<T>::min()) :
171  std::numeric_limits<T>::max();
172  return static_cast<T>(yy);
173  }
174 };
175 
177 
187 template <typename T>
188 T mia_round_clamped(double x)
189 {
190  const bool is_floating_point = std::is_floating_point<T>::value;
191  return __round_clamped<T, is_floating_point>::apply(x);
192 }
193 
194 
195 inline void eat_char( std::istream& is, char expect_val, const char *message)
196 {
197  char c;
198  is >> c;
199 
200  if ( c != expect_val)
201  throw std::runtime_error(message);
202 }
203 
205 
206 #endif
sincos
void EXPORT_CORE sincos(double x, double *sin, double *cos)
sincosf
void EXPORT_CORE sincosf(float x, float *sin, float *cos)
mia_round
T mia_round(double x)
Definition: utils.hh:122
NS_MIA_BEGIN
#define NS_MIA_BEGIN
conveniance define to start the mia namespace
Definition: defines.hh:33
CCWDSaver::~CCWDSaver
~CCWDSaver()
mia_round_clamped
T mia_round_clamped(double x)
Definition: utils.hh:188
NS_MIA_END
#define NS_MIA_END
conveniance define to end the mia namespace
Definition: defines.hh:36
CCWDSaver::CCWDSaver
CCWDSaver()
eat_char
void eat_char(std::istream &is, char expect_val, const char *message)
Definition: utils.hh:195
CCWDSaver
A Scope based helper class to save and restore the current working directory.
Definition: utils.hh:44
EXPORT_CORE
#define EXPORT_CORE
Macro to manage Visual C++ style dllimport/dllexport.
Definition: defines.hh:101
defines.hh