| 1 | /* |
|---|
| 2 | * ORXONOX - the hottest 3D action shooter ever to exist |
|---|
| 3 | * > www.orxonox.net < |
|---|
| 4 | * |
|---|
| 5 | * |
|---|
| 6 | * License notice: |
|---|
| 7 | * |
|---|
| 8 | * This program is free software; you can redistribute it and/or |
|---|
| 9 | * modify it under the terms of the GNU General Public License |
|---|
| 10 | * as published by the Free Software Foundation; either version 2 |
|---|
| 11 | * of the License, or (at your option) any later version. |
|---|
| 12 | * |
|---|
| 13 | * This program is distributed in the hope that it will be useful, |
|---|
| 14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
|---|
| 15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|---|
| 16 | * GNU General Public License for more details. |
|---|
| 17 | * |
|---|
| 18 | * You should have received a copy of the GNU General Public License |
|---|
| 19 | * along with this program; if not, write to the Free Software |
|---|
| 20 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
|---|
| 21 | * |
|---|
| 22 | * Author: |
|---|
| 23 | * Fabian 'x3n' Landau |
|---|
| 24 | * Co-authors: |
|---|
| 25 | * ... |
|---|
| 26 | * |
|---|
| 27 | */ |
|---|
| 28 | |
|---|
| 29 | /** |
|---|
| 30 | @file |
|---|
| 31 | @brief Implementation of several math-functions. |
|---|
| 32 | */ |
|---|
| 33 | |
|---|
| 34 | #include "Math.h" |
|---|
| 35 | |
|---|
| 36 | #include "MathConvert.h" |
|---|
| 37 | #include "SubString.h" |
|---|
| 38 | |
|---|
| 39 | namespace orxonox |
|---|
| 40 | { |
|---|
| 41 | /** |
|---|
| 42 | @brief Function for writing a Radian to a stream. |
|---|
| 43 | */ |
|---|
| 44 | std::ostream& operator<<(std::ostream& out, const orxonox::Radian& radian) |
|---|
| 45 | { |
|---|
| 46 | out << radian.valueRadians(); |
|---|
| 47 | return out; |
|---|
| 48 | } |
|---|
| 49 | |
|---|
| 50 | /** |
|---|
| 51 | @brief Function for reading a Radian from a stream. |
|---|
| 52 | */ |
|---|
| 53 | std::istream& operator>>(std::istream& in, orxonox::Radian& radian) |
|---|
| 54 | { |
|---|
| 55 | float temp; |
|---|
| 56 | in >> temp; |
|---|
| 57 | radian = temp; |
|---|
| 58 | return in; |
|---|
| 59 | } |
|---|
| 60 | |
|---|
| 61 | /** |
|---|
| 62 | @brief Function for writing a Degree to a stream. |
|---|
| 63 | */ |
|---|
| 64 | std::ostream& operator<<(std::ostream& out, const orxonox::Degree& degree) |
|---|
| 65 | { |
|---|
| 66 | out << degree.valueDegrees(); |
|---|
| 67 | return out; |
|---|
| 68 | } |
|---|
| 69 | |
|---|
| 70 | /** |
|---|
| 71 | @brief Function for reading a Degree from a stream. |
|---|
| 72 | */ |
|---|
| 73 | std::istream& operator>>(std::istream& in, orxonox::Degree& degree) |
|---|
| 74 | { |
|---|
| 75 | float temp; |
|---|
| 76 | in >> temp; |
|---|
| 77 | degree = temp; |
|---|
| 78 | return in; |
|---|
| 79 | } |
|---|
| 80 | |
|---|
| 81 | unsigned long getUniqueNumber() |
|---|
| 82 | { |
|---|
| 83 | static unsigned long aNumber = 135; |
|---|
| 84 | return aNumber++; |
|---|
| 85 | } |
|---|
| 86 | |
|---|
| 87 | |
|---|
| 88 | ////////////////////////// |
|---|
| 89 | // Conversion functions // |
|---|
| 90 | ////////////////////////// |
|---|
| 91 | |
|---|
| 92 | // std::string to Vector2 |
|---|
| 93 | bool ConverterFallback<std::string, orxonox::Vector2>::convert(orxonox::Vector2* output, const std::string& input) |
|---|
| 94 | { |
|---|
| 95 | size_t opening_parenthesis, closing_parenthesis = input.find(')'); |
|---|
| 96 | if ((opening_parenthesis = input.find('(')) == std::string::npos) |
|---|
| 97 | opening_parenthesis = 0; |
|---|
| 98 | else |
|---|
| 99 | opening_parenthesis++; |
|---|
| 100 | |
|---|
| 101 | SubString tokens(input.substr(opening_parenthesis, closing_parenthesis - opening_parenthesis), |
|---|
| 102 | ",", SubString::WhiteSpaces, false, '\\', true, '"', true, '\0', '\0', true, '\0'); |
|---|
| 103 | if (tokens.size() >= 2) |
|---|
| 104 | { |
|---|
| 105 | if (!convertValue(&(output->x), tokens[0])) |
|---|
| 106 | return false; |
|---|
| 107 | if (!convertValue(&(output->y), tokens[1])) |
|---|
| 108 | return false; |
|---|
| 109 | |
|---|
| 110 | return true; |
|---|
| 111 | } |
|---|
| 112 | return false; |
|---|
| 113 | } |
|---|
| 114 | |
|---|
| 115 | // std::string to Vector3 |
|---|
| 116 | bool ConverterFallback<std::string, orxonox::Vector3>::convert(orxonox::Vector3* output, const std::string& input) |
|---|
| 117 | { |
|---|
| 118 | size_t opening_parenthesis, closing_parenthesis = input.find(')'); |
|---|
| 119 | if ((opening_parenthesis = input.find('(')) == std::string::npos) |
|---|
| 120 | opening_parenthesis = 0; |
|---|
| 121 | else |
|---|
| 122 | opening_parenthesis++; |
|---|
| 123 | |
|---|
| 124 | SubString tokens(input.substr(opening_parenthesis, closing_parenthesis - opening_parenthesis), |
|---|
| 125 | ",", SubString::WhiteSpaces, false, '\\', true, '"', true, '\0', '\0', true, '\0'); |
|---|
| 126 | if (tokens.size() >= 3) |
|---|
| 127 | { |
|---|
| 128 | if (!convertValue(&(output->x), tokens[0])) |
|---|
| 129 | return false; |
|---|
| 130 | if (!convertValue(&(output->y), tokens[1])) |
|---|
| 131 | return false; |
|---|
| 132 | if (!convertValue(&(output->z), tokens[2])) |
|---|
| 133 | return false; |
|---|
| 134 | |
|---|
| 135 | return true; |
|---|
| 136 | } |
|---|
| 137 | return false; |
|---|
| 138 | } |
|---|
| 139 | |
|---|
| 140 | // std::string to Vector4 |
|---|
| 141 | bool ConverterFallback<std::string, orxonox::Vector4>::convert(orxonox::Vector4* output, const std::string& input) |
|---|
| 142 | { |
|---|
| 143 | size_t opening_parenthesis, closing_parenthesis = input.find(')'); |
|---|
| 144 | if ((opening_parenthesis = input.find('(')) == std::string::npos) |
|---|
| 145 | opening_parenthesis = 0; |
|---|
| 146 | else |
|---|
| 147 | opening_parenthesis++; |
|---|
| 148 | |
|---|
| 149 | SubString tokens(input.substr(opening_parenthesis, closing_parenthesis - opening_parenthesis), |
|---|
| 150 | ",", SubString::WhiteSpaces, false, '\\', true, '"', true, '\0', '\0', true, '\0'); |
|---|
| 151 | if (tokens.size() >= 4) |
|---|
| 152 | { |
|---|
| 153 | if (!convertValue(&(output->x), tokens[0])) |
|---|
| 154 | return false; |
|---|
| 155 | if (!convertValue(&(output->y), tokens[1])) |
|---|
| 156 | return false; |
|---|
| 157 | if (!convertValue(&(output->z), tokens[2])) |
|---|
| 158 | return false; |
|---|
| 159 | if (!convertValue(&(output->w), tokens[3])) |
|---|
| 160 | return false; |
|---|
| 161 | |
|---|
| 162 | return true; |
|---|
| 163 | } |
|---|
| 164 | return false; |
|---|
| 165 | } |
|---|
| 166 | |
|---|
| 167 | // std::string to Quaternion |
|---|
| 168 | bool ConverterFallback<std::string, orxonox::Quaternion>::convert(orxonox::Quaternion* output, const std::string& input) |
|---|
| 169 | { |
|---|
| 170 | size_t opening_parenthesis, closing_parenthesis = input.find(')'); |
|---|
| 171 | if ((opening_parenthesis = input.find('(')) == std::string::npos) { opening_parenthesis = 0; } else { opening_parenthesis++; } |
|---|
| 172 | |
|---|
| 173 | SubString tokens(input.substr(opening_parenthesis, closing_parenthesis - opening_parenthesis), ",", SubString::WhiteSpaces, false, '\\', true, '"', true, '\0', '\0', true, '\0'); |
|---|
| 174 | if (tokens.size() >= 4) |
|---|
| 175 | { |
|---|
| 176 | if (!convertValue(&(output->w), tokens[0])) |
|---|
| 177 | return false; |
|---|
| 178 | if (!convertValue(&(output->x), tokens[1])) |
|---|
| 179 | return false; |
|---|
| 180 | if (!convertValue(&(output->y), tokens[2])) |
|---|
| 181 | return false; |
|---|
| 182 | if (!convertValue(&(output->z), tokens[3])) |
|---|
| 183 | return false; |
|---|
| 184 | |
|---|
| 185 | return true; |
|---|
| 186 | } |
|---|
| 187 | return false; |
|---|
| 188 | } |
|---|
| 189 | |
|---|
| 190 | // std::string to ColourValue |
|---|
| 191 | bool ConverterFallback<std::string, orxonox::ColourValue>::convert(orxonox::ColourValue* output, const std::string& input) |
|---|
| 192 | { |
|---|
| 193 | size_t opening_parenthesis, closing_parenthesis = input.find(')'); |
|---|
| 194 | if ((opening_parenthesis = input.find('(')) == std::string::npos) { opening_parenthesis = 0; } else { opening_parenthesis++; } |
|---|
| 195 | |
|---|
| 196 | SubString tokens(input.substr(opening_parenthesis, closing_parenthesis - opening_parenthesis), ",", SubString::WhiteSpaces, false, '\\', true, '"', true, '\0', '\0', true, '\0'); |
|---|
| 197 | if (tokens.size() >= 3) |
|---|
| 198 | { |
|---|
| 199 | if (!convertValue(&(output->r), tokens[0])) |
|---|
| 200 | return false; |
|---|
| 201 | if (!convertValue(&(output->g), tokens[1])) |
|---|
| 202 | return false; |
|---|
| 203 | if (!convertValue(&(output->b), tokens[2])) |
|---|
| 204 | return false; |
|---|
| 205 | if (tokens.size() >= 4) |
|---|
| 206 | { |
|---|
| 207 | if (!convertValue(&(output->a), tokens[3])) |
|---|
| 208 | return false; |
|---|
| 209 | } |
|---|
| 210 | else |
|---|
| 211 | output->a = 1.0; |
|---|
| 212 | |
|---|
| 213 | return true; |
|---|
| 214 | } |
|---|
| 215 | return false; |
|---|
| 216 | } |
|---|
| 217 | } |
|---|