You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

588 lines
14 KiB

  1. ///////////////////////////////////////////////////////////////////////////////////////////////////
  2. // OpenGL Mathematics Copyright (c) 2006 G-Truc Creation (www.g-truc.net)
  3. ///////////////////////////////////////////////////////////////////////////////////////////////////
  4. // Created : 2008-04-27
  5. // Updated : 2008-05-24
  6. // Licence : This source is under MIT License
  7. // File : glm/gtx/string_cast.hpp
  8. ///////////////////////////////////////////////////////////////////////////////////////////////////
  9. #include <cstdarg>
  10. #include <cstdio>
  11. namespace glm{
  12. namespace detail
  13. {
  14. GLM_FUNC_QUALIFIER std::string format(const char* msg, ...)
  15. {
  16. std::size_t const STRING_BUFFER(4096);
  17. char text[STRING_BUFFER];
  18. va_list list;
  19. if(msg == 0)
  20. return std::string();
  21. va_start(list, msg);
  22. #if((GLM_COMPILER & GLM_COMPILER_VC) && (GLM_COMPILER >= GLM_COMPILER_VC2005))
  23. vsprintf_s(text, STRING_BUFFER, msg, list);
  24. #else//
  25. vsprintf(text, msg, list);
  26. #endif//
  27. va_end(list);
  28. return std::string(text);
  29. }
  30. static const char* True = "true";
  31. static const char* False = "false";
  32. }//namespace detail
  33. ////////////////////////////////
  34. // Scalars
  35. GLM_FUNC_QUALIFIER std::string to_string(detail::half const & x)
  36. {
  37. return detail::format("half(%2.4f)", float(x));
  38. }
  39. GLM_FUNC_QUALIFIER std::string to_string(float x)
  40. {
  41. return detail::format("float(%f)", x);
  42. }
  43. GLM_FUNC_QUALIFIER std::string to_string(double x)
  44. {
  45. return detail::format("double(%f)", x);
  46. }
  47. GLM_FUNC_QUALIFIER std::string to_string(int x)
  48. {
  49. return detail::format("int(%d)", x);
  50. }
  51. GLM_FUNC_QUALIFIER std::string to_string(unsigned int x)
  52. {
  53. return detail::format("uint(%d)", x);
  54. }
  55. ////////////////////////////////
  56. // Bool vectors
  57. GLM_FUNC_QUALIFIER std::string to_string
  58. (
  59. detail::tvec2<bool> const & v
  60. )
  61. {
  62. return detail::format("bvec2(%s, %s)",
  63. v.x ? detail::True : detail::False,
  64. v.y ? detail::True : detail::False);
  65. }
  66. GLM_FUNC_QUALIFIER std::string to_string
  67. (
  68. detail::tvec3<bool> const & v
  69. )
  70. {
  71. return detail::format("bvec3(%s, %s, %s)",
  72. v.x ? detail::True : detail::False,
  73. v.y ? detail::True : detail::False,
  74. v.z ? detail::True : detail::False);
  75. }
  76. GLM_FUNC_QUALIFIER std::string to_string
  77. (
  78. detail::tvec4<bool> const & v
  79. )
  80. {
  81. return detail::format("bvec4(%s, %s, %s, %s)",
  82. v.x ? detail::True : detail::False,
  83. v.y ? detail::True : detail::False,
  84. v.z ? detail::True : detail::False,
  85. v.w ? detail::True : detail::False);
  86. }
  87. ////////////////////////////////
  88. // Half vectors
  89. template <>
  90. GLM_FUNC_QUALIFIER std::string to_string
  91. (
  92. detail::tvec2<detail::half> const & v
  93. )
  94. {
  95. return detail::format("hvec2(%2.4f, %2.4f)", v.x.toFloat(), v.y.toFloat());
  96. }
  97. template <>
  98. GLM_FUNC_QUALIFIER std::string to_string
  99. (
  100. detail::tvec3<detail::half> const & v
  101. )
  102. {
  103. return detail::format("hvec3(%2.4f, %2.4f, %2.4f)", v.x.toFloat(), v.y.toFloat(), v.z.toFloat());
  104. }
  105. template <>
  106. GLM_FUNC_QUALIFIER std::string to_string
  107. (
  108. detail::tvec4<detail::half> const & v
  109. )
  110. {
  111. return detail::format("hvec4(%2.4f, %2.4f, %2.4f, %2.4f)", v.x.toFloat(), v.y.toFloat(), v.z.toFloat(), v.w.toFloat());
  112. }
  113. ////////////////////////////////
  114. // Float vectors
  115. template <>
  116. GLM_FUNC_QUALIFIER std::string to_string
  117. (
  118. detail::tvec2<float> const & v
  119. )
  120. {
  121. return detail::format("fvec2(%f, %f)", v.x, v.y);
  122. }
  123. template <>
  124. GLM_FUNC_QUALIFIER std::string to_string
  125. (
  126. detail::tvec3<float> const & v
  127. )
  128. {
  129. return detail::format("fvec3(%f, %f, %f)", v.x, v.y, v.z);
  130. }
  131. template <>
  132. GLM_FUNC_QUALIFIER std::string to_string
  133. (
  134. detail::tvec4<float> const & v
  135. )
  136. {
  137. return detail::format("fvec4(%f, %f, %f, %f)", v.x, v.y, v.z, v.w);
  138. }
  139. ////////////////////////////////
  140. // Double vectors
  141. template <>
  142. GLM_FUNC_QUALIFIER std::string to_string
  143. (
  144. detail::tvec2<double> const & v
  145. )
  146. {
  147. return detail::format("dvec2(%f, %f)", v.x, v.y);
  148. }
  149. template <>
  150. GLM_FUNC_QUALIFIER std::string to_string
  151. (
  152. detail::tvec3<double> const & v
  153. )
  154. {
  155. return detail::format("dvec3(%f, %f, %f)", v.x, v.y, v.z);
  156. }
  157. template <>
  158. GLM_FUNC_QUALIFIER std::string to_string
  159. (
  160. detail::tvec4<double> const & v
  161. )
  162. {
  163. return detail::format("dvec4(%f, %f, %f, %f)", v.x, v.y, v.z, v.w);
  164. }
  165. ////////////////////////////////
  166. // Int vectors
  167. template <>
  168. GLM_FUNC_QUALIFIER std::string to_string
  169. (
  170. detail::tvec2<int> const & v
  171. )
  172. {
  173. return detail::format("ivec2(%d, %d)", v.x, v.y);
  174. }
  175. template <>
  176. GLM_FUNC_QUALIFIER std::string to_string
  177. (
  178. detail::tvec3<int> const & v
  179. )
  180. {
  181. return detail::format("ivec3(%d, %d, %d)", v.x, v.y, v.z);
  182. }
  183. template <>
  184. GLM_FUNC_QUALIFIER std::string to_string
  185. (
  186. detail::tvec4<int> const & v
  187. )
  188. {
  189. return detail::format("ivec4(%d, %d, %d, %d)", v.x, v.y, v.z, v.w);
  190. }
  191. ////////////////////////////////
  192. // Unsigned int vectors
  193. template <>
  194. GLM_FUNC_QUALIFIER std::string to_string
  195. (
  196. detail::tvec2<unsigned int> const & v
  197. )
  198. {
  199. return detail::format("uvec2(%d, %d)", v.x, v.y);
  200. }
  201. template <>
  202. GLM_FUNC_QUALIFIER std::string to_string
  203. (
  204. detail::tvec3<unsigned int> const & v
  205. )
  206. {
  207. return detail::format("uvec3(%d, %d, %d)", v.x, v.y, v.z);
  208. }
  209. template <>
  210. GLM_FUNC_QUALIFIER std::string to_string
  211. (
  212. detail::tvec4<unsigned int> const & v
  213. )
  214. {
  215. return detail::format("uvec4(%d, %d, %d, %d)", v.x, v.y, v.z, v.w);
  216. }
  217. ////////////////////////////////
  218. // Half matrices
  219. template <>
  220. GLM_FUNC_QUALIFIER std::string to_string
  221. (
  222. detail::tmat2x2<detail::half> const & m
  223. )
  224. {
  225. return detail::format("hmat2x2((%f, %f), (%f, %f))",
  226. m[0][0].toFloat(), m[0][1].toFloat(),
  227. m[1][0].toFloat(), m[1][1].toFloat());
  228. }
  229. template <>
  230. GLM_FUNC_QUALIFIER std::string to_string
  231. (
  232. detail::tmat2x3<detail::half> const & x
  233. )
  234. {
  235. return detail::format("hmat2x3((%f, %f, %f), (%f, %f, %f))",
  236. x[0][0].toFloat(), x[0][1].toFloat(), x[0][2].toFloat(),
  237. x[1][0].toFloat(), x[1][1].toFloat(), x[1][2].toFloat());
  238. }
  239. template <>
  240. GLM_FUNC_QUALIFIER std::string to_string
  241. (
  242. detail::tmat2x4<detail::half> const & x
  243. )
  244. {
  245. return detail::format("hmat2x4((%f, %f, %f, %f), (%f, %f, %f, %f))",
  246. x[0][0].toFloat(), x[0][1].toFloat(), x[0][2].toFloat(), x[0][3].toFloat(),
  247. x[1][0].toFloat(), x[1][1].toFloat(), x[1][2].toFloat(), x[1][3].toFloat());
  248. }
  249. template <>
  250. GLM_FUNC_QUALIFIER std::string to_string
  251. (
  252. detail::tmat3x2<detail::half> const & x
  253. )
  254. {
  255. return detail::format("hmat3x2((%f, %f), (%f, %f), (%f, %f))",
  256. x[0][0].toFloat(), x[0][1].toFloat(),
  257. x[1][0].toFloat(), x[1][1].toFloat(),
  258. x[2][0].toFloat(), x[2][1].toFloat());
  259. }
  260. template <>
  261. GLM_FUNC_QUALIFIER std::string to_string
  262. (
  263. detail::tmat3x3<detail::half> const & x
  264. )
  265. {
  266. return detail::format("hmat3x3((%f, %f, %f), (%f, %f, %f), (%f, %f, %f))",
  267. x[0][0].toFloat(), x[0][1].toFloat(), x[0][2].toFloat(),
  268. x[1][0].toFloat(), x[1][1].toFloat(), x[1][2].toFloat(),
  269. x[2][0].toFloat(), x[2][1].toFloat(), x[2][2].toFloat());
  270. }
  271. template <>
  272. GLM_FUNC_QUALIFIER std::string to_string
  273. (
  274. detail::tmat3x4<detail::half> const & x
  275. )
  276. {
  277. return detail::format("hmat3x4((%f, %f, %f, %f), (%f, %f, %f, %f), (%f, %f, %f, %f))",
  278. x[0][0].toFloat(), x[0][1].toFloat(), x[0][2].toFloat(), x[0][3].toFloat(),
  279. x[1][0].toFloat(), x[1][1].toFloat(), x[1][2].toFloat(), x[1][3].toFloat(),
  280. x[2][0].toFloat(), x[2][1].toFloat(), x[2][2].toFloat(), x[2][3].toFloat());
  281. }
  282. template <>
  283. GLM_FUNC_QUALIFIER std::string to_string
  284. (
  285. detail::tmat4x2<detail::half> const & x
  286. )
  287. {
  288. return detail::format("hmat4x2((%f, %f), (%f, %f), (%f, %f), (%f, %f))",
  289. x[0][0].toFloat(), x[0][1].toFloat(),
  290. x[1][0].toFloat(), x[1][1].toFloat(),
  291. x[2][0].toFloat(), x[2][1].toFloat(),
  292. x[3][0].toFloat(), x[3][1].toFloat());
  293. }
  294. template <>
  295. GLM_FUNC_QUALIFIER std::string to_string
  296. (
  297. detail::tmat4x3<detail::half> const & x
  298. )
  299. {
  300. return detail::format("hmat4x3((%f, %f, %f), (%f, %f, %f), (%f, %f, %f), (%f, %f, %f))",
  301. x[0][0].toFloat(), x[0][1].toFloat(), x[0][2].toFloat(),
  302. x[1][0].toFloat(), x[1][1].toFloat(), x[1][2].toFloat(),
  303. x[2][0].toFloat(), x[2][1].toFloat(), x[2][2].toFloat(),
  304. x[3][0].toFloat(), x[3][1].toFloat(), x[3][2].toFloat());
  305. }
  306. template <>
  307. GLM_FUNC_QUALIFIER std::string to_string
  308. (
  309. detail::tmat4x4<detail::half> const & x
  310. )
  311. {
  312. return detail::format("hmat4x4((%f, %f, %f, %f), (%f, %f, %f, %f), (%f, %f, %f, %f), (%f, %f, %f, %f))",
  313. x[0][0].toFloat(), x[0][1].toFloat(), x[0][2].toFloat(), x[0][3].toFloat(),
  314. x[1][0].toFloat(), x[1][1].toFloat(), x[1][2].toFloat(), x[1][3].toFloat(),
  315. x[2][0].toFloat(), x[2][1].toFloat(), x[2][2].toFloat(), x[2][3].toFloat(),
  316. x[3][0].toFloat(), x[3][1].toFloat(), x[3][2].toFloat(), x[3][3].toFloat());
  317. }
  318. ////////////////////////////////
  319. // Float matrices
  320. template <>
  321. GLM_FUNC_QUALIFIER std::string to_string
  322. (
  323. detail::tmat2x2<float> const & x
  324. )
  325. {
  326. return detail::format("mat2x2((%f, %f), (%f, %f))",
  327. x[0][0], x[0][1],
  328. x[1][0], x[1][1]);
  329. }
  330. template <>
  331. GLM_FUNC_QUALIFIER std::string to_string
  332. (
  333. detail::tmat2x3<float> const & x
  334. )
  335. {
  336. return detail::format("mat2x3((%f, %f, %f), (%f, %f, %f))",
  337. x[0][0], x[0][1], x[0][2],
  338. x[1][0], x[1][1], x[1][2]);
  339. }
  340. template <>
  341. GLM_FUNC_QUALIFIER std::string to_string
  342. (
  343. detail::tmat2x4<float> const & x
  344. )
  345. {
  346. return detail::format("mat2x4((%f, %f, %f, %f), (%f, %f, %f, %f))",
  347. x[0][0], x[0][1], x[0][2], x[0][3],
  348. x[1][0], x[1][1], x[1][2], x[1][3]);
  349. }
  350. template <>
  351. GLM_FUNC_QUALIFIER std::string to_string
  352. (
  353. detail::tmat3x2<float> const & x
  354. )
  355. {
  356. return detail::format("mat3x2((%f, %f), (%f, %f), (%f, %f))",
  357. x[0][0], x[0][1],
  358. x[1][0], x[1][1],
  359. x[2][0], x[2][1]);
  360. }
  361. template <>
  362. GLM_FUNC_QUALIFIER std::string to_string
  363. (
  364. detail::tmat3x3<float> const & x
  365. )
  366. {
  367. return detail::format("mat3x3((%f, %f, %f), (%f, %f, %f), (%f, %f, %f))",
  368. x[0][0], x[0][1], x[0][2],
  369. x[1][0], x[1][1], x[1][2],
  370. x[2][0], x[2][1], x[2][2]);
  371. }
  372. template <>
  373. GLM_FUNC_QUALIFIER std::string to_string
  374. (
  375. detail::tmat3x4<float> const & x
  376. )
  377. {
  378. return detail::format("mat3x4((%f, %f, %f, %f), (%f, %f, %f, %f), (%f, %f, %f, %f))",
  379. x[0][0], x[0][1], x[0][2], x[0][3],
  380. x[1][0], x[1][1], x[1][2], x[1][3],
  381. x[2][0], x[2][1], x[2][2], x[2][3]);
  382. }
  383. template <>
  384. GLM_FUNC_QUALIFIER std::string to_string
  385. (
  386. detail::tmat4x2<float> const & x
  387. )
  388. {
  389. return detail::format("mat4x2((%f, %f), (%f, %f), (%f, %f), (%f, %f))",
  390. x[0][0], x[0][1],
  391. x[1][0], x[1][1],
  392. x[2][0], x[2][1],
  393. x[3][0], x[3][1]);
  394. }
  395. template <>
  396. GLM_FUNC_QUALIFIER std::string to_string
  397. (
  398. detail::tmat4x3<float> const & x
  399. )
  400. {
  401. return detail::format("mat4x3((%f, %f, %f), (%f, %f, %f), (%f, %f, %f), (%f, %f, %f))",
  402. x[0][0], x[0][1], x[0][2],
  403. x[1][0], x[1][1], x[1][2],
  404. x[2][0], x[2][1], x[2][2],
  405. x[3][0], x[3][1], x[3][2]);
  406. }
  407. template <>
  408. GLM_FUNC_QUALIFIER std::string to_string
  409. (
  410. detail::tmat4x4<float> const & x
  411. )
  412. {
  413. return detail::format("mat4x4((%f, %f, %f, %f), (%f, %f, %f, %f), (%f, %f, %f, %f), (%f, %f, %f, %f))",
  414. x[0][0], x[0][1], x[0][2], x[0][3],
  415. x[1][0], x[1][1], x[1][2], x[1][3],
  416. x[2][0], x[2][1], x[2][2], x[2][3],
  417. x[3][0], x[3][1], x[3][2], x[3][3]);
  418. }
  419. ////////////////////////////////
  420. // Double matrices
  421. template <>
  422. GLM_FUNC_QUALIFIER std::string to_string
  423. (
  424. detail::tmat2x2<double> const & x
  425. )
  426. {
  427. return detail::format("dmat2x2((%f, %f), (%f, %f))",
  428. x[0][0], x[0][1],
  429. x[1][0], x[1][1]);
  430. }
  431. template <>
  432. GLM_FUNC_QUALIFIER std::string to_string
  433. (
  434. detail::tmat2x3<double> const & x
  435. )
  436. {
  437. return detail::format("dmat2x3((%f, %f, %f), (%f, %f, %f))",
  438. x[0][0], x[0][1], x[0][2],
  439. x[1][0], x[1][1], x[1][2]);
  440. }
  441. template <>
  442. GLM_FUNC_QUALIFIER std::string to_string
  443. (
  444. detail::tmat2x4<double> const & x
  445. )
  446. {
  447. return detail::format("dmat2x4((%f, %f, %f, %f), (%f, %f, %f, %f))",
  448. x[0][0], x[0][1], x[0][2], x[0][3],
  449. x[1][0], x[1][1], x[1][2], x[1][3]);
  450. }
  451. template <>
  452. GLM_FUNC_QUALIFIER std::string to_string
  453. (
  454. detail::tmat3x2<double> const & x
  455. )
  456. {
  457. return detail::format("dmat3x2((%f, %f), (%f, %f), (%f, %f))",
  458. x[0][0], x[0][1],
  459. x[1][0], x[1][1],
  460. x[2][0], x[2][1]);
  461. }
  462. template <>
  463. GLM_FUNC_QUALIFIER std::string to_string
  464. (
  465. detail::tmat3x3<double> const & x
  466. )
  467. {
  468. return detail::format("dmat3x3((%f, %f, %f), (%f, %f, %f), (%f, %f, %f))",
  469. x[0][0], x[0][1], x[0][2],
  470. x[1][0], x[1][1], x[1][2],
  471. x[2][0], x[2][1], x[2][2]);
  472. }
  473. template <>
  474. GLM_FUNC_QUALIFIER std::string to_string
  475. (
  476. detail::tmat3x4<double> const & x
  477. )
  478. {
  479. return detail::format("dmat3x4((%f, %f, %f, %f), (%f, %f, %f, %f), (%f, %f, %f, %f))",
  480. x[0][0], x[0][1], x[0][2], x[0][3],
  481. x[1][0], x[1][1], x[1][2], x[1][3],
  482. x[2][0], x[2][1], x[2][2], x[2][3]);
  483. }
  484. template <>
  485. GLM_FUNC_QUALIFIER std::string to_string
  486. (
  487. detail::tmat4x2<double> const & x
  488. )
  489. {
  490. return detail::format("dmat4x2((%f, %f), (%f, %f), (%f, %f), (%f, %f))",
  491. x[0][0], x[0][1],
  492. x[1][0], x[1][1],
  493. x[2][0], x[2][1],
  494. x[3][0], x[3][1]);
  495. }
  496. template <>
  497. GLM_FUNC_QUALIFIER std::string to_string
  498. (
  499. detail::tmat4x3<double> const & x
  500. )
  501. {
  502. return detail::format("dmat4x3((%f, %f, %f), (%f, %f, %f), (%f, %f, %f), (%f, %f, %f))",
  503. x[0][0], x[0][1], x[0][2],
  504. x[1][0], x[1][1], x[1][2],
  505. x[2][0], x[2][1], x[2][2],
  506. x[3][0], x[3][1], x[3][2]);
  507. }
  508. template <>
  509. GLM_FUNC_QUALIFIER std::string to_string
  510. (
  511. detail::tmat4x4<double> const & x
  512. )
  513. {
  514. return detail::format("dmat4x4((%f, %f, %f, %f), (%f, %f, %f, %f), (%f, %f, %f, %f), (%f, %f, %f, %f))",
  515. x[0][0], x[0][1], x[0][2], x[0][3],
  516. x[1][0], x[1][1], x[1][2], x[1][3],
  517. x[2][0], x[2][1], x[2][2], x[2][3],
  518. x[3][0], x[3][1], x[3][2], x[3][3]);
  519. }
  520. }//namespace glm