mathIO.h 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) 2012 GarageGames, LLC
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to
  6. // deal in the Software without restriction, including without limitation the
  7. // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  8. // sell copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  19. // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  20. // IN THE SOFTWARE.
  21. //-----------------------------------------------------------------------------
  22. #ifndef _MATHIO_H_
  23. #define _MATHIO_H_
  24. //Includes
  25. #ifndef _PLATFORM_H_
  26. #include "platform/platform.h"
  27. #endif
  28. #ifndef _STREAM_H_
  29. #include "core/stream/stream.h"
  30. #endif
  31. #ifndef _MMATH_H_
  32. #include "math/mMath.h"
  33. #endif
  34. //------------------------------------------------------------------------------
  35. //-------------------------------------- READING
  36. //
  37. inline bool mathRead(Stream& stream, Point2I* p)
  38. {
  39. bool success = stream.read(&p->x);
  40. success &= stream.read(&p->y);
  41. return success;
  42. }
  43. inline bool mathRead(Stream& stream, Point3I* p)
  44. {
  45. bool success = stream.read(&p->x);
  46. success &= stream.read(&p->y);
  47. success &= stream.read(&p->z);
  48. return success;
  49. }
  50. inline bool mathRead(Stream& stream, Point2F* p)
  51. {
  52. bool success = stream.read(&p->x);
  53. success &= stream.read(&p->y);
  54. return success;
  55. }
  56. inline bool mathRead(Stream& stream, Point3F* p)
  57. {
  58. bool success = stream.read(&p->x);
  59. success &= stream.read(&p->y);
  60. success &= stream.read(&p->z);
  61. return success;
  62. }
  63. inline bool mathRead(Stream& stream, Point4F* p)
  64. {
  65. bool success = stream.read(&p->x);
  66. success &= stream.read(&p->y);
  67. success &= stream.read(&p->z);
  68. success &= stream.read(&p->w);
  69. return success;
  70. }
  71. inline bool mathRead(Stream& stream, Point3D* p)
  72. {
  73. bool success = stream.read(&p->x);
  74. success &= stream.read(&p->y);
  75. success &= stream.read(&p->z);
  76. return success;
  77. }
  78. inline bool mathRead(Stream& stream, PlaneF* p)
  79. {
  80. bool success = stream.read(&p->x);
  81. success &= stream.read(&p->y);
  82. success &= stream.read(&p->z);
  83. success &= stream.read(&p->d);
  84. return success;
  85. }
  86. inline bool mathRead(Stream& stream, Box3F* b)
  87. {
  88. bool success = mathRead(stream, &b->minExtents);
  89. success &= mathRead(stream, &b->maxExtents);
  90. return success;
  91. }
  92. inline bool mathRead(Stream& stream, SphereF* s)
  93. {
  94. bool success = mathRead(stream, &s->center);
  95. success &= stream.read(&s->radius);
  96. return success;
  97. }
  98. inline bool mathRead(Stream& stream, RectI* r)
  99. {
  100. bool success = mathRead(stream, &r->point);
  101. success &= mathRead(stream, &r->extent);
  102. return success;
  103. }
  104. inline bool mathRead(Stream& stream, RectF* r)
  105. {
  106. bool success = mathRead(stream, &r->point);
  107. success &= mathRead(stream, &r->extent);
  108. return success;
  109. }
  110. inline bool mathRead(Stream& stream, MatrixF* m)
  111. {
  112. bool success = true;
  113. F32* pm = *m;
  114. for (U32 i = 0; i < 16; i++)
  115. success &= stream.read(&pm[i]);
  116. return success;
  117. }
  118. inline bool mathRead(Stream& stream, QuatF* q)
  119. {
  120. bool success = stream.read(&q->x);
  121. success &= stream.read(&q->y);
  122. success &= stream.read(&q->z);
  123. success &= stream.read(&q->w);
  124. return success;
  125. }
  126. inline bool mathRead(Stream& stream, EaseF* e)
  127. {
  128. bool success = stream.read( &e->mDir );
  129. success &= stream.read( &e->mType );
  130. success &= stream.read( &e->mParam[ 0 ] );
  131. success &= stream.read( &e->mParam[ 1 ] );
  132. return success;
  133. }
  134. inline bool mathRead(Stream& stream, RotationF* e)
  135. {
  136. bool success = stream.read(&e->x);
  137. success &= stream.read(&e->y);
  138. success &= stream.read(&e->z);
  139. success &= stream.read(&e->w);
  140. U32 rotType;
  141. success &= stream.read(&rotType);
  142. e->mRotationType = (RotationF::RotationTypes)rotType;
  143. return success;
  144. }
  145. //------------------------------------------------------------------------------
  146. //-------------------------------------- WRITING
  147. //
  148. inline bool mathWrite(Stream& stream, const Point2I& p)
  149. {
  150. bool success = stream.write(p.x);
  151. success &= stream.write(p.y);
  152. return success;
  153. }
  154. inline bool mathWrite(Stream& stream, const Point3I& p)
  155. {
  156. bool success = stream.write(p.x);
  157. success &= stream.write(p.y);
  158. success &= stream.write(p.z);
  159. return success;
  160. }
  161. inline bool mathWrite(Stream& stream, const Point2F& p)
  162. {
  163. bool success = stream.write(p.x);
  164. success &= stream.write(p.y);
  165. return success;
  166. }
  167. inline bool mathWrite(Stream& stream, const Point3F& p)
  168. {
  169. bool success = stream.write(p.x);
  170. success &= stream.write(p.y);
  171. success &= stream.write(p.z);
  172. return success;
  173. }
  174. inline bool mathWrite(Stream& stream, const Point4F& p)
  175. {
  176. bool success = stream.write(p.x);
  177. success &= stream.write(p.y);
  178. success &= stream.write(p.z);
  179. success &= stream.write(p.w);
  180. return success;
  181. }
  182. inline bool mathWrite(Stream& stream, const Point3D& p)
  183. {
  184. bool success = stream.write(p.x);
  185. success &= stream.write(p.y);
  186. success &= stream.write(p.z);
  187. return success;
  188. }
  189. inline bool mathWrite(Stream& stream, const PlaneF& p)
  190. {
  191. bool success = stream.write(p.x);
  192. success &= stream.write(p.y);
  193. success &= stream.write(p.z);
  194. success &= stream.write(p.d);
  195. return success;
  196. }
  197. inline bool mathWrite(Stream& stream, const Box3F& b)
  198. {
  199. bool success = mathWrite(stream, b.minExtents);
  200. success &= mathWrite(stream, b.maxExtents);
  201. return success;
  202. }
  203. inline bool mathWrite(Stream& stream, const SphereF& s)
  204. {
  205. bool success = mathWrite(stream, s.center);
  206. success &= stream.write(s.radius);
  207. return success;
  208. }
  209. inline bool mathWrite(Stream& stream, const RectI& r)
  210. {
  211. bool success = mathWrite(stream, r.point);
  212. success &= mathWrite(stream, r.extent);
  213. return success;
  214. }
  215. inline bool mathWrite(Stream& stream, const RectF& r)
  216. {
  217. bool success = mathWrite(stream, r.point);
  218. success &= mathWrite(stream, r.extent);
  219. return success;
  220. }
  221. inline bool mathWrite(Stream& stream, const MatrixF& m)
  222. {
  223. bool success = true;
  224. const F32* pm = m;
  225. for (U32 i = 0; i < 16; i++)
  226. success &= stream.write(pm[i]);
  227. return success;
  228. }
  229. inline bool mathWrite(Stream& stream, const QuatF& q)
  230. {
  231. bool success = stream.write(q.x);
  232. success &= stream.write(q.y);
  233. success &= stream.write(q.z);
  234. success &= stream.write(q.w);
  235. return success;
  236. }
  237. inline bool mathWrite(Stream& stream, const EaseF& e)
  238. {
  239. bool success = stream.write(e.mDir);
  240. success &= stream.write(e.mType);
  241. success &= stream.write(e.mParam[0]);
  242. success &= stream.write(e.mParam[1]);
  243. return success;
  244. }
  245. inline bool mathWrite(Stream& stream, const RotationF& e)
  246. {
  247. bool success = stream.write(e.x);
  248. success &= stream.write(e.y);
  249. success &= stream.write(e.z);
  250. success &= stream.write(e.w);
  251. success &= stream.write(e.mRotationType);
  252. return success;;
  253. }
  254. #endif //_MATHIO_H_