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
|
/// @ref gtx_io
/// @author Jan P Springer (regnirpsj@gmail.com)
#include <iomanip> // std::fixed, std::setfill<>, std::setprecision, std::right, std::setw
#include <ostream> // std::basic_ostream<>
#include "../gtc/matrix_access.hpp" // glm::col, glm::row
#include "../gtx/type_trait.hpp" // glm::type<>
namespace glm{
namespace io
{
template<typename CTy>
GLM_FUNC_QUALIFIER format_punct<CTy>::format_punct(size_t a)
: std::locale::facet(a)
, formatted(true)
, precision(3)
, width(1 + 4 + 1 + precision)
, separator(',')
, delim_left('[')
, delim_right(']')
, space(' ')
, newline('\n')
, order(column_major)
{}
template<typename CTy>
GLM_FUNC_QUALIFIER format_punct<CTy>::format_punct(format_punct const& a)
: std::locale::facet(0)
, formatted(a.formatted)
, precision(a.precision)
, width(a.width)
, separator(a.separator)
, delim_left(a.delim_left)
, delim_right(a.delim_right)
, space(a.space)
, newline(a.newline)
, order(a.order)
{}
template<typename CTy> std::locale::id format_punct<CTy>::id;
template<typename CTy, typename CTr>
GLM_FUNC_QUALIFIER basic_state_saver<CTy, CTr>::basic_state_saver(std::basic_ios<CTy, CTr>& a)
: state_(a)
, flags_(a.flags())
, precision_(a.precision())
, width_(a.width())
, fill_(a.fill())
, locale_(a.getloc())
{}
template<typename CTy, typename CTr>
GLM_FUNC_QUALIFIER basic_state_saver<CTy, CTr>::~basic_state_saver()
{
state_.imbue(locale_);
state_.fill(fill_);
state_.width(width_);
state_.precision(precision_);
state_.flags(flags_);
}
template<typename CTy, typename CTr>
GLM_FUNC_QUALIFIER basic_format_saver<CTy, CTr>::basic_format_saver(std::basic_ios<CTy, CTr>& a)
: bss_(a)
{
a.imbue(std::locale(a.getloc(), new format_punct<CTy>(get_facet<format_punct<CTy> >(a))));
}
template<typename CTy, typename CTr>
GLM_FUNC_QUALIFIER
basic_format_saver<CTy, CTr>::~basic_format_saver()
{}
GLM_FUNC_QUALIFIER precision::precision(unsigned a)
: value(a)
{}
GLM_FUNC_QUALIFIER width::width(unsigned a)
: value(a)
{}
template<typename CTy>
GLM_FUNC_QUALIFIER delimeter<CTy>::delimeter(CTy a, CTy b, CTy c)
: value()
{
value[0] = a;
value[1] = b;
value[2] = c;
}
GLM_FUNC_QUALIFIER order::order(order_type a)
: value(a)
{}
template<typename FTy, typename CTy, typename CTr>
GLM_FUNC_QUALIFIER FTy const& get_facet(std::basic_ios<CTy, CTr>& ios)
{
if(!std::has_facet<FTy>(ios.getloc()))
ios.imbue(std::locale(ios.getloc(), new FTy));
return std::use_facet<FTy>(ios.getloc());
}
template<typename CTy, typename CTr>
GLM_FUNC_QUALIFIER std::basic_ios<CTy, CTr>& formatted(std::basic_ios<CTy, CTr>& ios)
{
const_cast<format_punct<CTy>&>(get_facet<format_punct<CTy> >(ios)).formatted = true;
return ios;
}
template<typename CTy, typename CTr>
GLM_FUNC_QUALIFIER std::basic_ios<CTy, CTr>& unformatted(std::basic_ios<CTy, CTr>& ios)
{
const_cast<format_punct<CTy>&>(get_facet<format_punct<CTy> >(ios)).formatted = false;
return ios;
}
template<typename CTy, typename CTr>
GLM_FUNC_QUALIFIER std::basic_ostream<CTy, CTr>& operator<<(std::basic_ostream<CTy, CTr>& os, precision const& a)
{
const_cast<format_punct<CTy>&>(get_facet<format_punct<CTy> >(os)).precision = a.value;
return os;
}
template<typename CTy, typename CTr>
GLM_FUNC_QUALIFIER std::basic_ostream<CTy, CTr>& operator<<(std::basic_ostream<CTy, CTr>& os, width const& a)
{
const_cast<format_punct<CTy>&>(get_facet<format_punct<CTy> >(os)).width = a.value;
return os;
}
template<typename CTy, typename CTr>
GLM_FUNC_QUALIFIER std::basic_ostream<CTy, CTr>& operator<<(std::basic_ostream<CTy, CTr>& os, delimeter<CTy> const& a)
{
format_punct<CTy> & fmt(const_cast<format_punct<CTy>&>(get_facet<format_punct<CTy> >(os)));
fmt.delim_left = a.value[0];
fmt.delim_right = a.value[1];
fmt.separator = a.value[2];
return os;
}
template<typename CTy, typename CTr>
GLM_FUNC_QUALIFIER std::basic_ostream<CTy, CTr>& operator<<(std::basic_ostream<CTy, CTr>& os, order const& a)
{
const_cast<format_punct<CTy>&>(get_facet<format_punct<CTy> >(os)).order = a.value;
return os;
}
} // namespace io
namespace detail
{
template<typename CTy, typename CTr, typename V>
GLM_FUNC_QUALIFIER std::basic_ostream<CTy, CTr>&
print_vector_on(std::basic_ostream<CTy, CTr>& os, V const& a)
{
typename std::basic_ostream<CTy, CTr>::sentry const cerberus(os);
if(cerberus)
{
io::format_punct<CTy> const& fmt(io::get_facet<io::format_punct<CTy> >(os));
length_t const& components(type<V>::components);
if(fmt.formatted)
{
io::basic_state_saver<CTy> const bss(os);
os << std::fixed << std::right << std::setprecision(fmt.precision) << std::setfill(fmt.space) << fmt.delim_left;
for(length_t i(0); i < components; ++i)
{
os << std::setw(fmt.width) << a[i];
if(components-1 != i)
os << fmt.separator;
}
os << fmt.delim_right;
}
else
{
for(length_t i(0); i < components; ++i)
{
os << a[i];
if(components-1 != i)
os << fmt.space;
}
}
}
return os;
}
}//namespace detail
template<typename CTy, typename CTr, typename T, qualifier Q>
GLM_FUNC_QUALIFIER std::basic_ostream<CTy,CTr>& operator<<(std::basic_ostream<CTy,CTr>& os, qua<T, Q> const& a)
{
return detail::print_vector_on(os, a);
}
template<typename CTy, typename CTr, typename T, qualifier Q>
GLM_FUNC_QUALIFIER std::basic_ostream<CTy,CTr>& operator<<(std::basic_ostream<CTy,CTr>& os, vec<1, T, Q> const& a)
{
return detail::print_vector_on(os, a);
}
template<typename CTy, typename CTr, typename T, qualifier Q>
GLM_FUNC_QUALIFIER std::basic_ostream<CTy,CTr>& operator<<(std::basic_ostream<CTy,CTr>& os, vec<2, T, Q> const& a)
{
return detail::print_vector_on(os, a);
}
template<typename CTy, typename CTr, typename T, qualifier Q>
GLM_FUNC_QUALIFIER std::basic_ostream<CTy,CTr>& operator<<(std::basic_ostream<CTy,CTr>& os, vec<3, T, Q> const& a)
{
return detail::print_vector_on(os, a);
}
template<typename CTy, typename CTr, typename T, qualifier Q>
GLM_FUNC_QUALIFIER std::basic_ostream<CTy,CTr>& operator<<(std::basic_ostream<CTy,CTr>& os, vec<4, T, Q> const& a)
{
return detail::print_vector_on(os, a);
}
namespace detail
{
template<typename CTy, typename CTr, template<length_t, length_t, typename, qualifier> class M, length_t C, length_t R, typename T, qualifier Q>
GLM_FUNC_QUALIFIER std::basic_ostream<CTy, CTr>& print_matrix_on(std::basic_ostream<CTy, CTr>& os, M<C, R, T, Q> const& a)
{
typename std::basic_ostream<CTy,CTr>::sentry const cerberus(os);
if(cerberus)
{
io::format_punct<CTy> const& fmt(io::get_facet<io::format_punct<CTy> >(os));
length_t const& cols(type<M<C, R, T, Q> >::cols);
length_t const& rows(type<M<C, R, T, Q> >::rows);
if(fmt.formatted)
{
os << fmt.newline << fmt.delim_left;
switch(fmt.order)
{
case io::column_major:
{
for(length_t i(0); i < rows; ++i)
{
if (0 != i)
os << fmt.space;
os << row(a, i);
if(rows-1 != i)
os << fmt.newline;
}
}
break;
case io::row_major:
{
for(length_t i(0); i < cols; ++i)
{
if(0 != i)
os << fmt.space;
os << column(a, i);
if(cols-1 != i)
os << fmt.newline;
}
}
break;
}
os << fmt.delim_right;
}
else
{
switch (fmt.order)
{
case io::column_major:
{
for(length_t i(0); i < cols; ++i)
{
os << column(a, i);
if(cols - 1 != i)
os << fmt.space;
}
}
break;
case io::row_major:
{
for (length_t i(0); i < rows; ++i)
{
os << row(a, i);
if (rows-1 != i)
os << fmt.space;
}
}
break;
}
}
}
return os;
}
}//namespace detail
template<typename CTy, typename CTr, typename T, qualifier Q>
GLM_FUNC_QUALIFIER std::basic_ostream<CTy,CTr>& operator<<(std::basic_ostream<CTy,CTr>& os, mat<2, 2, T, Q> const& a)
{
return detail::print_matrix_on(os, a);
}
template<typename CTy, typename CTr, typename T, qualifier Q>
GLM_FUNC_QUALIFIER std::basic_ostream<CTy,CTr>& operator<<(std::basic_ostream<CTy,CTr>& os, mat<2, 3, T, Q> const& a)
{
return detail::print_matrix_on(os, a);
}
template<typename CTy, typename CTr, typename T, qualifier Q>
GLM_FUNC_QUALIFIER std::basic_ostream<CTy,CTr>& operator<<(std::basic_ostream<CTy,CTr>& os, mat<2, 4, T, Q> const& a)
{
return detail::print_matrix_on(os, a);
}
template<typename CTy, typename CTr, typename T, qualifier Q>
GLM_FUNC_QUALIFIER std::basic_ostream<CTy,CTr>& operator<<(std::basic_ostream<CTy,CTr>& os, mat<3, 2, T, Q> const& a)
{
return detail::print_matrix_on(os, a);
}
template<typename CTy, typename CTr, typename T, qualifier Q>
GLM_FUNC_QUALIFIER std::basic_ostream<CTy,CTr>& operator<<(std::basic_ostream<CTy,CTr>& os, mat<3, 3, T, Q> const& a)
{
return detail::print_matrix_on(os, a);
}
template<typename CTy, typename CTr, typename T, qualifier Q>
GLM_FUNC_QUALIFIER std::basic_ostream<CTy,CTr> & operator<<(std::basic_ostream<CTy,CTr>& os, mat<3, 4, T, Q> const& a)
{
return detail::print_matrix_on(os, a);
}
template<typename CTy, typename CTr, typename T, qualifier Q>
GLM_FUNC_QUALIFIER std::basic_ostream<CTy,CTr> & operator<<(std::basic_ostream<CTy,CTr>& os, mat<4, 2, T, Q> const& a)
{
return detail::print_matrix_on(os, a);
}
template<typename CTy, typename CTr, typename T, qualifier Q>
GLM_FUNC_QUALIFIER std::basic_ostream<CTy,CTr> & operator<<(std::basic_ostream<CTy,CTr>& os, mat<4, 3, T, Q> const& a)
{
return detail::print_matrix_on(os, a);
}
template<typename CTy, typename CTr, typename T, qualifier Q>
GLM_FUNC_QUALIFIER std::basic_ostream<CTy,CTr> & operator<<(std::basic_ostream<CTy,CTr>& os, mat<4, 4, T, Q> const& a)
{
return detail::print_matrix_on(os, a);
}
namespace detail
{
template<typename CTy, typename CTr, template<length_t, length_t, typename, qualifier> class M, length_t C, length_t R, typename T, qualifier Q>
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)
{
typename std::basic_ostream<CTy,CTr>::sentry const cerberus(os);
if(cerberus)
{
io::format_punct<CTy> const& fmt(io::get_facet<io::format_punct<CTy> >(os));
M<C, R, T, Q> const& ml(a.first);
M<C, R, T, Q> const& mr(a.second);
length_t const& cols(type<M<C, R, T, Q> >::cols);
length_t const& rows(type<M<C, R, T, Q> >::rows);
if(fmt.formatted)
{
os << fmt.newline << fmt.delim_left;
switch(fmt.order)
{
case io::column_major:
{
for(length_t i(0); i < rows; ++i)
{
if(0 != i)
os << fmt.space;
os << row(ml, i) << ((rows-1 != i) ? fmt.space : fmt.delim_right) << fmt.space << ((0 != i) ? fmt.space : fmt.delim_left) << row(mr, i);
if(rows-1 != i)
os << fmt.newline;
}
}
break;
case io::row_major:
{
for(length_t i(0); i < cols; ++i)
{
if(0 != i)
os << fmt.space;
os << column(ml, i) << ((cols-1 != i) ? fmt.space : fmt.delim_right) << fmt.space << ((0 != i) ? fmt.space : fmt.delim_left) << column(mr, i);
if(cols-1 != i)
os << fmt.newline;
}
}
break;
}
os << fmt.delim_right;
}
else
{
os << ml << fmt.space << mr;
}
}
return os;
}
}//namespace detail
template<typename CTy, typename CTr, typename T, qualifier Q>
GLM_FUNC_QUALIFIER std::basic_ostream<CTy, CTr>& operator<<(
std::basic_ostream<CTy, CTr> & os,
std::pair<mat<4, 4, T, Q> const,
mat<4, 4, T, Q> const> const& a)
{
return detail::print_matrix_pair_on(os, a);
}
}//namespace glm
|