| 1 | /**
|
|---|
| 2 | * @file helper.hpp
|
|---|
| 3 | * @author Julian Iseringhausen <isering@ins.uni-bonn.de>
|
|---|
| 4 | * @date Tue Apr 5 21:03:47 2011
|
|---|
| 5 | *
|
|---|
| 6 | * @brief Provides various helper functions.
|
|---|
| 7 | *
|
|---|
| 8 | */
|
|---|
| 9 |
|
|---|
| 10 | #ifndef HELPER_HPP_
|
|---|
| 11 | #define HELPER_HPP_
|
|---|
| 12 |
|
|---|
| 13 | #include <cassert>
|
|---|
| 14 | #include <cstdio>
|
|---|
| 15 | #include <limits>
|
|---|
| 16 | #include <sstream>
|
|---|
| 17 | #include <string>
|
|---|
| 18 |
|
|---|
| 19 | namespace VMG
|
|---|
| 20 | {
|
|---|
| 21 |
|
|---|
| 22 | class Grid;
|
|---|
| 23 | class Vector;
|
|---|
| 24 |
|
|---|
| 25 | namespace Helper
|
|---|
| 26 | {
|
|---|
| 27 | char* GetCharArray(const std::string& str);
|
|---|
| 28 | std::string ReplaceWhitespaces(const char* buffer, const char* replace);
|
|---|
| 29 |
|
|---|
| 30 | template <class T>
|
|---|
| 31 | std::string ToString(const T& val)
|
|---|
| 32 | {
|
|---|
| 33 | std::stringstream str;
|
|---|
| 34 | str << std::scientific << val;
|
|---|
| 35 | return str.str();
|
|---|
| 36 | }
|
|---|
| 37 |
|
|---|
| 38 | template <class T>
|
|---|
| 39 | T ToVal(const char* val_str)
|
|---|
| 40 | {
|
|---|
| 41 | T val;
|
|---|
| 42 | std::stringstream str;
|
|---|
| 43 | str << val_str;
|
|---|
| 44 | str >> val;
|
|---|
| 45 |
|
|---|
| 46 | return val;
|
|---|
| 47 | }
|
|---|
| 48 |
|
|---|
| 49 | template <class T>
|
|---|
| 50 | T ToValWithDefault(const char* val_str, const T& def)
|
|---|
| 51 | {
|
|---|
| 52 | T val;
|
|---|
| 53 | std::stringstream str(val_str);
|
|---|
| 54 | str >> val;
|
|---|
| 55 |
|
|---|
| 56 | if (str.fail() || str.bad() || !str.eof()) {
|
|---|
| 57 | #ifdef DEBUG_VERBOSE
|
|---|
| 58 | std::printf("VMG::Helper::ToValWithDefault: Using default value.\n");
|
|---|
| 59 | #endif
|
|---|
| 60 | val = def;
|
|---|
| 61 | }
|
|---|
| 62 |
|
|---|
| 63 | return val;
|
|---|
| 64 | }
|
|---|
| 65 |
|
|---|
| 66 | /**
|
|---|
| 67 | * Checks a number for validity, i.e. it is neither nan nor inf.
|
|---|
| 68 | */
|
|---|
| 69 | inline bool CheckNumber(const vmg_float& number)
|
|---|
| 70 | {
|
|---|
| 71 | bool valid = true;
|
|---|
| 72 |
|
|---|
| 73 | if (std::numeric_limits<vmg_float>::has_quiet_NaN) {
|
|---|
| 74 | valid &= number != std::numeric_limits<vmg_float>::quiet_NaN();
|
|---|
| 75 | assert(number != std::numeric_limits<vmg_float>::quiet_NaN());
|
|---|
| 76 | }
|
|---|
| 77 |
|
|---|
| 78 | if (std::numeric_limits<vmg_float>::has_signaling_NaN) {
|
|---|
| 79 | valid &= number != std::numeric_limits<vmg_float>::signaling_NaN();
|
|---|
| 80 | assert(number != std::numeric_limits<vmg_float>::signaling_NaN());
|
|---|
| 81 | }
|
|---|
| 82 |
|
|---|
| 83 | if (std::numeric_limits<vmg_float>::has_infinity) {
|
|---|
| 84 | valid &= number != std::numeric_limits<vmg_float>::infinity();
|
|---|
| 85 | valid &= number != -1 * std::numeric_limits<vmg_float>::infinity();
|
|---|
| 86 | assert(number != std::numeric_limits<vmg_float>::infinity());
|
|---|
| 87 | assert(number != -1 * std::numeric_limits<vmg_float>::infinity());
|
|---|
| 88 | }
|
|---|
| 89 |
|
|---|
| 90 | return valid;
|
|---|
| 91 | }
|
|---|
| 92 |
|
|---|
| 93 | inline int intpow(int base, unsigned int power)
|
|---|
| 94 | {
|
|---|
| 95 | int result = 1;
|
|---|
| 96 | while (power != 0) {
|
|---|
| 97 | if (power & 1)
|
|---|
| 98 | result *= base;
|
|---|
| 99 | base *= base;
|
|---|
| 100 | power >>= 1;
|
|---|
| 101 | }
|
|---|
| 102 | return result;
|
|---|
| 103 | }
|
|---|
| 104 |
|
|---|
| 105 | inline vmg_float pow(vmg_float base, unsigned int power)
|
|---|
| 106 | {
|
|---|
| 107 | vmg_float result = 1.0;
|
|---|
| 108 | while (power != 0) {
|
|---|
| 109 | if (power & 1)
|
|---|
| 110 | result *= base;
|
|---|
| 111 | base *= base;
|
|---|
| 112 | power >>= 1;
|
|---|
| 113 | }
|
|---|
| 114 | return result;
|
|---|
| 115 | }
|
|---|
| 116 |
|
|---|
| 117 | inline unsigned int fact(unsigned int number)
|
|---|
| 118 | {
|
|---|
| 119 | unsigned int result = 1;
|
|---|
| 120 | for (unsigned int i=2; i<=number; ++i)
|
|---|
| 121 | result *= i;
|
|---|
| 122 | return result;
|
|---|
| 123 | }
|
|---|
| 124 |
|
|---|
| 125 | inline vmg_float pow_3(vmg_float val)
|
|---|
| 126 | {
|
|---|
| 127 | return val*val*val;
|
|---|
| 128 | }
|
|---|
| 129 |
|
|---|
| 130 |
|
|---|
| 131 | /**
|
|---|
| 132 | * Tests two arbitrary objects for equality and prints
|
|---|
| 133 | * a warning if they differ.
|
|---|
| 134 | */
|
|---|
| 135 | template <class T>
|
|---|
| 136 | bool IsEq(const T& val1, const T& val2, const char name[])
|
|---|
| 137 | {
|
|---|
| 138 | bool rval = (val1 == val2);
|
|---|
| 139 |
|
|---|
| 140 | #ifdef DEBUG_OUTPUT
|
|---|
| 141 | if (!rval)
|
|---|
| 142 | printf("WARNING: Values are not equal (%s)\n", name);
|
|---|
| 143 | #endif /* DEBUG_OUTPUT */
|
|---|
| 144 |
|
|---|
| 145 | assert(rval);
|
|---|
| 146 |
|
|---|
| 147 | return rval;
|
|---|
| 148 | }
|
|---|
| 149 |
|
|---|
| 150 | vmg_float InterpolateTrilinear(const Vector& point, const Grid& grid);
|
|---|
| 151 |
|
|---|
| 152 | }
|
|---|
| 153 |
|
|---|
| 154 | }
|
|---|
| 155 |
|
|---|
| 156 | #endif /* HELPER_HPP_ */
|
|---|