1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469
|
/*
Copyright 2008 Brain Research Institute, Melbourne, Australia
Written by J-Donald Tournier, 27/06/08.
This file is part of MRtrix.
MRtrix is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
MRtrix is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with MRtrix. If not, see <http://www.gnu.org/licenses/>.
22-07-2008 J-Donald Tournier <d.tournier@brain.org.au>
* fix va_list handing in printf()
09-07-2008 J-Donald Tournier <d.tournier@brain.org.au>
* added getline() method to handle Unix/DOS end-of-line
02-09-2008 J-Donald Tournier <d.tournier@brain.org.au>
* added is_temporary() method to identify temporary files
02-10-2008 J-Donald Tournier <d.tournier@brain.org.au>
* change Exception silencing to a priority level changing approach
15-10-2008 J-Donald Tournier <d.tournier@brain.org.au>
* remove MR::DICOM_DW_gradients_PRS flag
31-10-2008 J-Donald Tournier <d.tournier@brain.org.au>
* include <cstring> & <cstdlib> to allow compilation on Fedora 9
15-03-2010 J-Donald Tournier <d.tournier@brain.org.au>
* add shorten() function to reduce long filenames
15-07-2010 J-Donald Tournier <d.tournier@brain.org.au>
* version upped to 9
*/
#ifndef __mrtrix_h__
#define __mrtrix_h__
#include <limits.h>
#include <stdarg.h>
#include <errno.h>
#include <assert.h>
#include <sstream>
#include <algorithm>
#include <cctype>
#include <iostream>
#include <vector>
#include <string>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <glib.h>
#include <glibmm/timer.h>
#include <glibmm/miscutils.h>
#include <gsl/gsl_version.h>
#include <gsl/gsl_math.h>
#define MRTRIX_MAJOR_VERSION 0
#define MRTRIX_MINOR_VERSION 2
#define MRTRIX_MICRO_VERSION 12
/** Prints the file and line number. Useful for debugging purposes. */
#define TEST std::cerr << Glib::get_application_name() << ": line " << __LINE__ << " in " << __func__ << "() from file " << __FILE__ << "\n"
/** Prints a variable name and its value, followed by the file and line number. Useful for debugging purposes. */
#define VAR(variable) std::cerr << Glib::get_application_name() << ": " << #variable << " = " << (variable) << " (in " << __func__ << "() from " << __FILE__ << ": " << __LINE__ << ")\n"
#define TRACE std::cerr << "in " << __PRETTY_FUNCTION__ << " (" << __FILE__ << ": " << __LINE__ << ")\n"
#define MRTRIX_MAX_NDIMS 16
#define BUSY_INTERVAL 0.1
#define GUI_SPACING 5
#ifdef __APPLE__
# define CTRL_CMD_MASK GDK_MOD2_MASK
#define MODIFIERS ( \
GDK_SHIFT_MASK | \
GDK_CONTROL_MASK | \
GDK_MOD2_MASK | \
GDK_BUTTON1_MASK | \
GDK_BUTTON2_MASK | \
GDK_BUTTON3_MASK | \
GDK_BUTTON4_MASK | \
GDK_BUTTON5_MASK | \
GDK_SUPER_MASK | \
GDK_HYPER_MASK | \
GDK_META_MASK )
#else
# define CTRL_CMD_MASK GDK_CONTROL_MASK
#define MODIFIERS ( \
GDK_SHIFT_MASK | \
GDK_CONTROL_MASK | \
GDK_BUTTON1_MASK | \
GDK_BUTTON2_MASK | \
GDK_BUTTON3_MASK | \
GDK_BUTTON4_MASK | \
GDK_BUTTON5_MASK | \
GDK_SUPER_MASK | \
GDK_HYPER_MASK | \
GDK_META_MASK )
#endif
#define TMPFILE_ROOT "mrtrix-"
#define TMPFILE_ROOT_LEN 7
#ifdef G_OS_WIN32
typedef struct _stati64 struct_stat64;
# define STAT64 _stati64
#else
# ifdef __APPLE__
typedef struct stat struct_stat64;
# define STAT64 stat
# else
typedef struct stat64 struct_stat64;
# define STAT64 stat64
# endif
#endif
namespace std {
template <class T> inline ostream& operator<< (ostream& stream, const vector<T>& V)
{
stream << "[ ";
for (guint n = 0; n < V.size(); n++) stream << V[n] << " ";
stream << "]";
return (stream);
}
}
namespace MR {
extern const guint mrtrix_major_version;
extern const guint mrtrix_minor_version;
extern const guint mrtrix_micro_version;
typedef std::string String;
typedef gfloat float32;
typedef gdouble float64;
extern void (*print) (const String& msg);
extern void (*error) (const String& msg);
extern void (*info) (const String& msg);
extern void (*debug) (const String& msg);
class Exception {
public:
Exception (const String& msg, int log_level = 1) :
description (msg),
level (log_level) { display(); }
const String description;
const int level;
void display () const {
if (level + level_offset < 2) error (description);
else if (level + level_offset == 2) info (description);
else debug (description);
}
class Lower {
public:
Lower (int amount = 1) { level_offset = amount; }
~Lower () { level_offset = 0; }
friend class Exception;
};
private:
static int level_offset;
};
namespace ProgressBar {
extern String message;
extern guint current_val, percent;
extern float multiplier;
extern bool display, stop;
extern Glib::Timer stop_watch;
extern void (*init_func)();
extern void (*display_func)();
extern void (*done_func)();
void init (guint target, const String& msg);
int inc ();
void done ();
}
namespace Image {
typedef enum {
Default,
Magnitude,
Real,
Imaginary,
Phase,
RealImag
} OutputType;
}
namespace ByteOrder {
gint16 swap (gint16 v);
guint16 swap (guint16 v);
gint32 swap (gint32 v);
guint32 swap (guint32 v);
float32 swap (float32 v);
float64 swap (float64 v);
gint16 LE (gint16 v);
gint16 BE (gint16 v);
guint16 LE (guint16 v);
guint16 BE (guint16 v);
gint32 LE (gint32 v);
gint32 BE (gint32 v);
guint32 LE (guint32 v);
guint32 BE (guint32 v);
float32 LE (float32 v);
float32 BE (float32 v);
float64 LE (float64 v);
float64 BE (float64 v);
}
inline bool is_temporary (const String& file) { return (Glib::path_get_basename(file).compare (0, TMPFILE_ROOT_LEN, TMPFILE_ROOT) == 0); }
inline std::istream& getline (std::istream& stream, String& string)
{
std::getline (stream, string);
if (string[string.size()-1] == 015) string.resize (string.size()-1);
return (stream);
}
template <class T> inline String str (const T& value)
{
std::ostringstream stream;
try { stream << value; }
catch (...) { throw Exception ("error converting value to string"); }
return (stream.str());
}
inline String shorten (const String& string, guint longest = 40, guint prefix = 10)
{
if (string.size() > longest)
return (string.substr (0,prefix) + "..." + string.substr (string.size()-longest+prefix-3));
else return (string);
}
template <class T> inline T to (const String& string)
{
std::istringstream stream (string);
T value;
try { stream >> value; }
catch (...) { throw Exception ("error converting string \"" + string + "\""); }
return (value);
}
inline String printf (const gchar* format, ...)
{
va_list list;
va_start (list, format);
guint len = g_vsnprintf (NULL, 0, format, list) + 1;
va_end (list);
std::vector<gchar> buf (len);
va_start (list, format);
g_vsnprintf (&buf[0], len, format, list);
va_end (list);
return (&buf[0]);
}
inline String strip (const String& string, const gchar* ws = " \t\n", bool left = true, bool right = true)
{
std::string::size_type start = ( left ? string.find_first_not_of (ws) : 0 );
if (start == String::npos) return ("");
std::string::size_type end = ( right ? string.find_last_not_of (ws) + 1 : String::npos );
return (string.substr (start, end - start));
}
inline void replace (String& string, gchar orig, gchar final)
{
for (String::iterator i = string.begin(); i != string.end(); ++i)
if (*i == orig) *i = final;
}
inline String& lowercase (String& string)
{
for_each (string.begin(), string.end(), tolower);
return (string);
}
inline String& uppercase (String& string)
{
for_each (string.begin(), string.end(), toupper);
return (string);
}
inline String lowercase (const String& string)
{
String ret; ret.resize (string.size());
transform (string.begin(), string.end(), ret.begin(), tolower);
return (ret);
}
inline String uppercase (const String& string)
{
String ret; ret.resize (string.size());
transform (string.begin(), string.end(), ret.begin(), toupper);
return (ret);
}
std::vector<String> split (const String& string, const gchar* delimiters = " \t\n", bool ignore_empty_fields = false);
inline String join (std::vector<String>& V, const String& delimiter)
{
String ret;
if (V.empty()) return (ret);
ret = V[0];
for (std::vector<String>::iterator i = V.begin()+1; i != V.end(); ++i)
ret += delimiter + *i;
return (ret);
}
std::vector<gfloat> parse_floats (const String& spec);
std::vector<int> parse_ints (const String& spec, int last = G_MAXINT);
inline int round (float x) { return (int (x + (x > 0.0 ? 0.5 : -0.5))); }
template <class T> inline T maxvalue (const T& v0, const T& v1, const T& v2)
{
return (v0 > v1 ? ( v0 > v2 ? v0 : ( v1 > v2 ? v1 : v2 ) ) : ( v1 > v2 ? v1 : ( v0 > v2 ? v0 : v2 )));
}
template <class T> inline int maxindex (const T& v0, const T& v1, const T& v2)
{
return (v0 > v1 ? ( v0 > v2 ? 0 : ( v1 > v2 ? 1 : 2 ) ) : ( v1 > v2 ? 1 : ( v0 > v2 ? 0 : 2 )));
}
template <class T> inline T maxvalue (const T v[3]) { return (maxvalue (v[0], v[1], v[2])); }
template <class T> inline int maxindex (const T v[3]) { return (maxindex (v[0], v[1], v[2])); }
template <class T> inline T minvalue (const T& v0, const T& v1, const T& v2)
{
return (v0 < v1 ? ( v0 < v2 ? v0 : ( v1 < v2 ? v1 : v2 ) ) : ( v1 < v2 ? v1 : ( v0 < v2 ? v0 : v2 )));
}
template <class T> inline int minindex (const T& v0, const T& v1, const T& v2)
{
return (v0 < v1 ? ( v0 < v2 ? 0 : ( v1 < v2 ? 1 : 2 ) ) : ( v1 < v2 ? 1 : ( v0 < v2 ? 0 : 2 )));
}
template <class T> inline T minvalue (const T v[3]) { return (minvalue (v[0], v[1], v[2])); }
template <class T> inline int minindex (const T v[3]) { return (minindex (v[0], v[1], v[2])); }
template <class T> inline void set_all (std::vector<T>& V, const T& value)
{
for (guint n = 0; n < V.size(); n++) V[n] = value;
}
template <class T> inline bool get_next (std::vector<T>& pos, const std::vector<T>& limits)
{
guint axis = 0;
while (axis < limits.size()) {
pos[axis]++;
if (pos[axis] < limits[axis]) return (true);
pos[axis] = 0;
axis++;
}
return (false);
}
namespace ProgressBar {
inline int inc ()
{
current_val++;
if (!display) return (stop);
guint t = gsl_isnan (multiplier) ? (guint) (stop_watch.elapsed() / BUSY_INTERVAL) : (guint) (multiplier*current_val);
if (percent != t) {
percent = t;
display_func();
}
return (stop);
}
inline void done () { if (display) done_func(); }
}
}
#endif
|