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;
46public:
49};
50
51#if 0
56class FSearchFiles
57{
58 std::list<std::string>& result;
59 const std::string pattern;
60public:
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
78void EXPORT_CORE sincosf(float x, float *sin, float *cos);
79
84void EXPORT_CORE sincos(double x, double *sin, double *cos);
85#endif
86
93template <typename T, bool is_float>
94struct __round {
95
96 static T apply(double x)
97 {
98 return x;
99 }
100};
101
102template <typename T>
103struct __round<T, false> {
104 static T apply(double x)
105 {
106 return static_cast<T>(rint(x));
107 }
108};
109
111
121template <typename T>
122T 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
134template <typename T, bool is_float>
135struct __round_clamped {
136
137 static T apply(double x)
138 {
139 return x;
140 }
141};
142
143template <>
144struct __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
155template <>
156struct __round_clamped<bool, false> {
157 static float apply(double x)
158 {
159 return x > 0.5;
160 }
161};
162
163
164template <typename T>
165struct __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
187template <typename T>
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
195inline 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
A Scope based helper class to save and restore the current working directory.
Definition utils.hh:44
#define NS_MIA_BEGIN
conveniance define to start the mia namespace
Definition defines.hh:33
#define EXPORT_CORE
Macro to manage Visual C++ style dllimport/dllexport.
Definition defines.hh:101
#define NS_MIA_END
conveniance define to end the mia namespace
Definition defines.hh:36
void EXPORT_CORE sincosf(float x, float *sin, float *cos)
void EXPORT_CORE sincos(double x, double *sin, double *cos)
T mia_round_clamped(double x)
Definition utils.hh:188
T mia_round(double x)
Definition utils.hh:122
void eat_char(std::istream &is, char expect_val, const char *message)
Definition utils.hh:195