| 1 | /// @ref gtx_io |
| 2 | /// @author Jan P Springer (regnirpsj@gmail.com) |
| 3 | |
| 4 | #include <iomanip> // std::fixed, std::setfill<>, std::setprecision, std::right, std::setw |
| 5 | #include <ostream> // std::basic_ostream<> |
| 6 | #include "../gtc/matrix_access.hpp" // glm::col, glm::row |
| 7 | #include "../gtx/type_trait.hpp" // glm::type<> |
| 8 | |
| 9 | #if GLM_COMPILER & GLM_COMPILER_CLANG |
| 10 | # pragma clang diagnostic push |
| 11 | # pragma clang diagnostic ignored "-Wpadded" |
| 12 | # pragma clang diagnostic ignored "-Wshorten-64-to-32" |
| 13 | # pragma clang diagnostic ignored "-Wglobal-constructors" |
| 14 | #endif |
| 15 | |
| 16 | namespace glm{ |
| 17 | namespace io |
| 18 | { |
| 19 | template<typename CTy> |
| 20 | GLM_FUNC_QUALIFIER format_punct<CTy>::format_punct(size_t a) |
| 21 | : std::locale::facet(a) |
| 22 | , formatted(true) |
| 23 | , precision(3) |
| 24 | , width(1 + 4 + 1 + precision) |
| 25 | , separator(',') |
| 26 | , delim_left('[') |
| 27 | , delim_right(']') |
| 28 | , space(' ') |
| 29 | , newline('\n') |
| 30 | , order(column_major) |
| 31 | {} |
| 32 | |
| 33 | template<typename CTy> |
| 34 | GLM_FUNC_QUALIFIER format_punct<CTy>::format_punct(format_punct const& a) |
| 35 | : std::locale::facet(0) |
| 36 | , formatted(a.formatted) |
| 37 | , precision(a.precision) |
| 38 | , width(a.width) |
| 39 | , separator(a.separator) |
| 40 | , delim_left(a.delim_left) |
| 41 | , delim_right(a.delim_right) |
| 42 | , space(a.space) |
| 43 | , newline(a.newline) |
| 44 | , order(a.order) |
| 45 | {} |
| 46 | |
| 47 | template<typename CTy> std::locale::id format_punct<CTy>::id; |
| 48 | |
| 49 | template<typename CTy, typename CTr> |
| 50 | GLM_FUNC_QUALIFIER basic_state_saver<CTy, CTr>::basic_state_saver(std::basic_ios<CTy, CTr>& a) |
| 51 | : state_(a) |
| 52 | , flags_(a.flags()) |
| 53 | , precision_(a.precision()) |
| 54 | , width_(a.width()) |
| 55 | , fill_(a.fill()) |
| 56 | , locale_(a.getloc()) |
| 57 | {} |
| 58 | |
| 59 | template<typename CTy, typename CTr> |
| 60 | GLM_FUNC_QUALIFIER basic_state_saver<CTy, CTr>::~basic_state_saver() |
| 61 | { |
| 62 | state_.imbue(locale_); |
| 63 | state_.fill(fill_); |
| 64 | state_.width(width_); |
| 65 | state_.precision(precision_); |
| 66 | state_.flags(flags_); |
| 67 | } |
| 68 | |
| 69 | template<typename CTy, typename CTr> |
| 70 | GLM_FUNC_QUALIFIER basic_format_saver<CTy, CTr>::basic_format_saver(std::basic_ios<CTy, CTr>& a) |
| 71 | : bss_(a) |
| 72 | { |
| 73 | a.imbue(std::locale(a.getloc(), new format_punct<CTy>(get_facet<format_punct<CTy> >(a)))); |
| 74 | } |
| 75 | |
| 76 | template<typename CTy, typename CTr> |
| 77 | GLM_FUNC_QUALIFIER |
| 78 | basic_format_saver<CTy, CTr>::~basic_format_saver() |
| 79 | {} |
| 80 | |
| 81 | GLM_FUNC_QUALIFIER precision::precision(unsigned a) |
| 82 | : value(a) |
| 83 | {} |
| 84 | |
| 85 | GLM_FUNC_QUALIFIER width::width(unsigned a) |
| 86 | : value(a) |
| 87 | {} |
| 88 | |
| 89 | template<typename CTy> |
| 90 | GLM_FUNC_QUALIFIER delimeter<CTy>::delimeter(CTy a, CTy b, CTy c) |
| 91 | : value() |
| 92 | { |
| 93 | value[0] = a; |
| 94 | value[1] = b; |
| 95 | value[2] = c; |
| 96 | } |
| 97 | |
| 98 | GLM_FUNC_QUALIFIER order::order(order_type a) |
| 99 | : value(a) |
| 100 | {} |
| 101 | |
| 102 | template<typename FTy, typename CTy, typename CTr> |
| 103 | GLM_FUNC_QUALIFIER FTy const& get_facet(std::basic_ios<CTy, CTr>& ios) |
| 104 | { |
| 105 | if(!std::has_facet<FTy>(ios.getloc())) |
| 106 | ios.imbue(std::locale(ios.getloc(), new FTy)); |
| 107 | |
| 108 | return std::use_facet<FTy>(ios.getloc()); |
| 109 | } |
| 110 | |
| 111 | template<typename CTy, typename CTr> |
| 112 | GLM_FUNC_QUALIFIER std::basic_ios<CTy, CTr>& formatted(std::basic_ios<CTy, CTr>& ios) |
| 113 | { |
| 114 | const_cast<format_punct<CTy>&>(get_facet<format_punct<CTy> >(ios)).formatted = true; |
| 115 | return ios; |
| 116 | } |
| 117 | |
| 118 | template<typename CTy, typename CTr> |
| 119 | GLM_FUNC_QUALIFIER std::basic_ios<CTy, CTr>& unformatted(std::basic_ios<CTy, CTr>& ios) |
| 120 | { |
| 121 | const_cast<format_punct<CTy>&>(get_facet<format_punct<CTy> >(ios)).formatted = false; |
| 122 | return ios; |
| 123 | } |
| 124 | |
| 125 | template<typename CTy, typename CTr> |
| 126 | GLM_FUNC_QUALIFIER std::basic_ostream<CTy, CTr>& operator<<(std::basic_ostream<CTy, CTr>& os, precision const& a) |
| 127 | { |
| 128 | const_cast<format_punct<CTy>&>(get_facet<format_punct<CTy> >(os)).precision = a.value; |
| 129 | return os; |
| 130 | } |
| 131 | |
| 132 | template<typename CTy, typename CTr> |
| 133 | GLM_FUNC_QUALIFIER std::basic_ostream<CTy, CTr>& operator<<(std::basic_ostream<CTy, CTr>& os, width const& a) |
| 134 | { |
| 135 | const_cast<format_punct<CTy>&>(get_facet<format_punct<CTy> >(os)).width = a.value; |
| 136 | return os; |
| 137 | } |
| 138 | |
| 139 | template<typename CTy, typename CTr> |
| 140 | GLM_FUNC_QUALIFIER std::basic_ostream<CTy, CTr>& operator<<(std::basic_ostream<CTy, CTr>& os, delimeter<CTy> const& a) |
| 141 | { |
| 142 | format_punct<CTy> & fmt(const_cast<format_punct<CTy>&>(get_facet<format_punct<CTy> >(os))); |
| 143 | |
| 144 | fmt.delim_left = a.value[0]; |
| 145 | fmt.delim_right = a.value[1]; |
| 146 | fmt.separator = a.value[2]; |
| 147 | |
| 148 | return os; |
| 149 | } |
| 150 | |
| 151 | template<typename CTy, typename CTr> |
| 152 | GLM_FUNC_QUALIFIER std::basic_ostream<CTy, CTr>& operator<<(std::basic_ostream<CTy, CTr>& os, order const& a) |
| 153 | { |
| 154 | const_cast<format_punct<CTy>&>(get_facet<format_punct<CTy> >(os)).order = a.value; |
| 155 | return os; |
| 156 | } |
| 157 | } // namespace io |
| 158 | |
| 159 | namespace detail |
| 160 | { |
| 161 | template<typename CTy, typename CTr, typename V> |
| 162 | GLM_FUNC_QUALIFIER std::basic_ostream<CTy, CTr>& |
| 163 | print_vector_on(std::basic_ostream<CTy, CTr>& os, V const& a) |
| 164 | { |
| 165 | typename std::basic_ostream<CTy, CTr>::sentry const cerberus(os); |
| 166 | |
| 167 | if(cerberus) |
| 168 | { |
| 169 | io::format_punct<CTy> const& fmt(io::get_facet<io::format_punct<CTy> >(os)); |
| 170 | |
| 171 | length_t const& components(type<V>::components); |
| 172 | |
| 173 | if(fmt.formatted) |
| 174 | { |
| 175 | io::basic_state_saver<CTy> const bss(os); |
| 176 | |
| 177 | os << std::fixed << std::right << std::setprecision(static_cast<std::streamsize>(fmt.precision)) << std::setfill(fmt.space) << fmt.delim_left; |
| 178 | |
| 179 | for(length_t i(0); i < components; ++i) |
| 180 | { |
| 181 | os << std::setw(static_cast<int>(fmt.width)) << a[i]; |
| 182 | if(components-1 != i) |
| 183 | os << fmt.separator; |
| 184 | } |
| 185 | |
| 186 | os << fmt.delim_right; |
| 187 | } |
| 188 | else |
| 189 | { |
| 190 | for(length_t i(0); i < components; ++i) |
| 191 | { |
| 192 | os << a[i]; |
| 193 | |
| 194 | if(components-1 != i) |
| 195 | os << fmt.space; |
| 196 | } |
| 197 | } |
| 198 | } |
| 199 | |
| 200 | return os; |
| 201 | } |
| 202 | }//namespace detail |
| 203 | |
| 204 | template<typename CTy, typename CTr, typename T, qualifier Q> |
| 205 | GLM_FUNC_QUALIFIER std::basic_ostream<CTy,CTr>& operator<<(std::basic_ostream<CTy,CTr>& os, qua<T, Q> const& a) |
| 206 | { |
| 207 | return detail::print_vector_on(os, a); |
| 208 | } |
| 209 | |
| 210 | template<typename CTy, typename CTr, typename T, qualifier Q> |
| 211 | GLM_FUNC_QUALIFIER std::basic_ostream<CTy,CTr>& operator<<(std::basic_ostream<CTy,CTr>& os, vec<1, T, Q> const& a) |
| 212 | { |
| 213 | return detail::print_vector_on(os, a); |
| 214 | } |
| 215 | |
| 216 | template<typename CTy, typename CTr, typename T, qualifier Q> |
| 217 | GLM_FUNC_QUALIFIER std::basic_ostream<CTy,CTr>& operator<<(std::basic_ostream<CTy,CTr>& os, vec<2, T, Q> const& a) |
| 218 | { |
| 219 | return detail::print_vector_on(os, a); |
| 220 | } |
| 221 | |
| 222 | template<typename CTy, typename CTr, typename T, qualifier Q> |
| 223 | GLM_FUNC_QUALIFIER std::basic_ostream<CTy,CTr>& operator<<(std::basic_ostream<CTy,CTr>& os, vec<3, T, Q> const& a) |
| 224 | { |
| 225 | return detail::print_vector_on(os, a); |
| 226 | } |
| 227 | |
| 228 | template<typename CTy, typename CTr, typename T, qualifier Q> |
| 229 | GLM_FUNC_QUALIFIER std::basic_ostream<CTy,CTr>& operator<<(std::basic_ostream<CTy,CTr>& os, vec<4, T, Q> const& a) |
| 230 | { |
| 231 | return detail::print_vector_on(os, a); |
| 232 | } |
| 233 | |
| 234 | namespace detail |
| 235 | { |
| 236 | template<typename CTy, typename CTr, template<length_t, length_t, typename, qualifier> class M, length_t C, length_t R, typename T, qualifier Q> |
| 237 | GLM_FUNC_QUALIFIER std::basic_ostream<CTy, CTr>& print_matrix_on(std::basic_ostream<CTy, CTr>& os, M<C, R, T, Q> const& a) |
| 238 | { |
| 239 | typename std::basic_ostream<CTy,CTr>::sentry const cerberus(os); |
| 240 | |
| 241 | if(cerberus) |
| 242 | { |
| 243 | io::format_punct<CTy> const& fmt(io::get_facet<io::format_punct<CTy> >(os)); |
| 244 | |
| 245 | length_t const& cols(type<M<C, R, T, Q> >::cols); |
| 246 | length_t const& rows(type<M<C, R, T, Q> >::rows); |
| 247 | |
| 248 | if(fmt.formatted) |
| 249 | { |
| 250 | os << fmt.newline << fmt.delim_left; |
| 251 | |
| 252 | switch(fmt.order) |
| 253 | { |
| 254 | case io::column_major: |
| 255 | { |
| 256 | for(length_t i(0); i < rows; ++i) |
| 257 | { |
| 258 | if (0 != i) |
| 259 | os << fmt.space; |
| 260 | |
| 261 | os << row(a, i); |
| 262 | |
| 263 | if(rows-1 != i) |
| 264 | os << fmt.newline; |
| 265 | } |
| 266 | } |
| 267 | break; |
| 268 | |
| 269 | case io::row_major: |
| 270 | { |
| 271 | for(length_t i(0); i < cols; ++i) |
| 272 | { |
| 273 | if(0 != i) |
| 274 | os << fmt.space; |
| 275 | |
| 276 | os << column(a, i); |
| 277 | |
| 278 | if(cols-1 != i) |
| 279 | os << fmt.newline; |
| 280 | } |
| 281 | } |
| 282 | break; |
| 283 | } |
| 284 | |
| 285 | os << fmt.delim_right; |
| 286 | } |
| 287 | else |
| 288 | { |
| 289 | switch (fmt.order) |
| 290 | { |
| 291 | case io::column_major: |
| 292 | { |
| 293 | for(length_t i(0); i < cols; ++i) |
| 294 | { |
| 295 | os << column(a, i); |
| 296 | |
| 297 | if(cols - 1 != i) |
| 298 | os << fmt.space; |
| 299 | } |
| 300 | } |
| 301 | break; |
| 302 | |
| 303 | case io::row_major: |
| 304 | { |
| 305 | for (length_t i(0); i < rows; ++i) |
| 306 | { |
| 307 | os << row(a, i); |
| 308 | |
| 309 | if (rows-1 != i) |
| 310 | os << fmt.space; |
| 311 | } |
| 312 | } |
| 313 | break; |
| 314 | } |
| 315 | } |
| 316 | } |
| 317 | |
| 318 | return os; |
| 319 | } |
| 320 | }//namespace detail |
| 321 | |
| 322 | template<typename CTy, typename CTr, typename T, qualifier Q> |
| 323 | GLM_FUNC_QUALIFIER std::basic_ostream<CTy,CTr>& operator<<(std::basic_ostream<CTy,CTr>& os, mat<2, 2, T, Q> const& a) |
| 324 | { |
| 325 | return detail::print_matrix_on(os, a); |
| 326 | } |
| 327 | |
| 328 | template<typename CTy, typename CTr, typename T, qualifier Q> |
| 329 | GLM_FUNC_QUALIFIER std::basic_ostream<CTy,CTr>& operator<<(std::basic_ostream<CTy,CTr>& os, mat<2, 3, T, Q> const& a) |
| 330 | { |
| 331 | return detail::print_matrix_on(os, a); |
| 332 | } |
| 333 | |
| 334 | template<typename CTy, typename CTr, typename T, qualifier Q> |
| 335 | GLM_FUNC_QUALIFIER std::basic_ostream<CTy,CTr>& operator<<(std::basic_ostream<CTy,CTr>& os, mat<2, 4, T, Q> const& a) |
| 336 | { |
| 337 | return detail::print_matrix_on(os, a); |
| 338 | } |
| 339 | |
| 340 | template<typename CTy, typename CTr, typename T, qualifier Q> |
| 341 | GLM_FUNC_QUALIFIER std::basic_ostream<CTy,CTr>& operator<<(std::basic_ostream<CTy,CTr>& os, mat<3, 2, T, Q> const& a) |
| 342 | { |
| 343 | return detail::print_matrix_on(os, a); |
| 344 | } |
| 345 | |
| 346 | template<typename CTy, typename CTr, typename T, qualifier Q> |
| 347 | GLM_FUNC_QUALIFIER std::basic_ostream<CTy,CTr>& operator<<(std::basic_ostream<CTy,CTr>& os, mat<3, 3, T, Q> const& a) |
| 348 | { |
| 349 | return detail::print_matrix_on(os, a); |
| 350 | } |
| 351 | |
| 352 | template<typename CTy, typename CTr, typename T, qualifier Q> |
| 353 | GLM_FUNC_QUALIFIER std::basic_ostream<CTy,CTr> & operator<<(std::basic_ostream<CTy,CTr>& os, mat<3, 4, T, Q> const& a) |
| 354 | { |
| 355 | return detail::print_matrix_on(os, a); |
| 356 | } |
| 357 | |
| 358 | template<typename CTy, typename CTr, typename T, qualifier Q> |
| 359 | GLM_FUNC_QUALIFIER std::basic_ostream<CTy,CTr> & operator<<(std::basic_ostream<CTy,CTr>& os, mat<4, 2, T, Q> const& a) |
| 360 | { |
| 361 | return detail::print_matrix_on(os, a); |
| 362 | } |
| 363 | |
| 364 | template<typename CTy, typename CTr, typename T, qualifier Q> |
| 365 | GLM_FUNC_QUALIFIER std::basic_ostream<CTy,CTr> & operator<<(std::basic_ostream<CTy,CTr>& os, mat<4, 3, T, Q> const& a) |
| 366 | { |
| 367 | return detail::print_matrix_on(os, a); |
| 368 | } |
| 369 | |
| 370 | template<typename CTy, typename CTr, typename T, qualifier Q> |
| 371 | GLM_FUNC_QUALIFIER std::basic_ostream<CTy,CTr> & operator<<(std::basic_ostream<CTy,CTr>& os, mat<4, 4, T, Q> const& a) |
| 372 | { |
| 373 | return detail::print_matrix_on(os, a); |
| 374 | } |
| 375 | |
| 376 | namespace detail |
| 377 | { |
| 378 | template<typename CTy, typename CTr, template<length_t, length_t, typename, qualifier> class M, length_t C, length_t R, typename T, qualifier Q> |
| 379 | GLM_FUNC_QUALIFIER std::basic_ostream<CTy, CTr>& print_matrix_pair_on(std::basic_ostream<CTy, CTr>& os, std::pair<M<C, R, T, Q> const, M<C, R, T, Q> const> const& a) |
| 380 | { |
| 381 | typename std::basic_ostream<CTy,CTr>::sentry const cerberus(os); |
| 382 | |
| 383 | if(cerberus) |
| 384 | { |
| 385 | io::format_punct<CTy> const& fmt(io::get_facet<io::format_punct<CTy> >(os)); |
| 386 | M<C, R, T, Q> const& ml(a.first); |
| 387 | M<C, R, T, Q> const& mr(a.second); |
| 388 | length_t const& cols(type<M<C, R, T, Q> >::cols); |
| 389 | length_t const& rows(type<M<C, R, T, Q> >::rows); |
| 390 | |
| 391 | if(fmt.formatted) |
| 392 | { |
| 393 | os << fmt.newline << fmt.delim_left; |
| 394 | |
| 395 | switch(fmt.order) |
| 396 | { |
| 397 | default: |
| 398 | case io::column_major: |
| 399 | { |
| 400 | for(length_t i(0); i < rows; ++i) |
| 401 | { |
| 402 | if(0 != i) |
| 403 | os << fmt.space; |
| 404 | |
| 405 | os << row(ml, i) << ((rows-1 != i) ? fmt.space : fmt.delim_right) << fmt.space << ((0 != i) ? fmt.space : fmt.delim_left) << row(mr, i); |
| 406 | |
| 407 | if(rows-1 != i) |
| 408 | os << fmt.newline; |
| 409 | } |
| 410 | } |
| 411 | break; |
| 412 | case io::row_major: |
| 413 | { |
| 414 | for(length_t i(0); i < cols; ++i) |
| 415 | { |
| 416 | if(0 != i) |
| 417 | os << fmt.space; |
| 418 | |
| 419 | os << column(ml, i) << ((cols-1 != i) ? fmt.space : fmt.delim_right) << fmt.space << ((0 != i) ? fmt.space : fmt.delim_left) << column(mr, i); |
| 420 | |
| 421 | if(cols-1 != i) |
| 422 | os << fmt.newline; |
| 423 | } |
| 424 | } |
| 425 | break; |
| 426 | } |
| 427 | |
| 428 | os << fmt.delim_right; |
| 429 | } |
| 430 | else |
| 431 | { |
| 432 | os << ml << fmt.space << mr; |
| 433 | } |
| 434 | } |
| 435 | |
| 436 | return os; |
| 437 | } |
| 438 | }//namespace detail |
| 439 | |
| 440 | template<typename CTy, typename CTr, typename T, qualifier Q> |
| 441 | GLM_FUNC_QUALIFIER std::basic_ostream<CTy, CTr>& operator<<( |
| 442 | std::basic_ostream<CTy, CTr> & os, |
| 443 | std::pair<mat<4, 4, T, Q> const, |
| 444 | mat<4, 4, T, Q> const> const& a) |
| 445 | { |
| 446 | return detail::print_matrix_pair_on(os, a); |
| 447 | } |
| 448 | }//namespace glm |
| 449 | |
| 450 | #if GLM_COMPILER & GLM_COMPILER_CLANG |
| 451 | # pragma clang diagnostic pop |
| 452 | #endif |
| 453 | |