mathIO.h 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  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->dir );
  129. success &= stream.read( &e->type );
  130. success &= stream.read( &e->param[ 0 ] );
  131. success &= stream.read( &e->param[ 1 ] );
  132. return success;
  133. }
  134. //------------------------------------------------------------------------------
  135. //-------------------------------------- WRITING
  136. //
  137. inline bool mathWrite(Stream& stream, const Point2I& p)
  138. {
  139. bool success = stream.write(p.x);
  140. success &= stream.write(p.y);
  141. return success;
  142. }
  143. inline bool mathWrite(Stream& stream, const Point3I& p)
  144. {
  145. bool success = stream.write(p.x);
  146. success &= stream.write(p.y);
  147. success &= stream.write(p.z);
  148. return success;
  149. }
  150. inline bool mathWrite(Stream& stream, const Point2F& p)
  151. {
  152. bool success = stream.write(p.x);
  153. success &= stream.write(p.y);
  154. return success;
  155. }
  156. inline bool mathWrite(Stream& stream, const Point3F& p)
  157. {
  158. bool success = stream.write(p.x);
  159. success &= stream.write(p.y);
  160. success &= stream.write(p.z);
  161. return success;
  162. }
  163. inline bool mathWrite(Stream& stream, const Point4F& p)
  164. {
  165. bool success = stream.write(p.x);
  166. success &= stream.write(p.y);
  167. success &= stream.write(p.z);
  168. success &= stream.write(p.w);
  169. return success;
  170. }
  171. inline bool mathWrite(Stream& stream, const Point3D& p)
  172. {
  173. bool success = stream.write(p.x);
  174. success &= stream.write(p.y);
  175. success &= stream.write(p.z);
  176. return success;
  177. }
  178. inline bool mathWrite(Stream& stream, const PlaneF& p)
  179. {
  180. bool success = stream.write(p.x);
  181. success &= stream.write(p.y);
  182. success &= stream.write(p.z);
  183. success &= stream.write(p.d);
  184. return success;
  185. }
  186. inline bool mathWrite(Stream& stream, const Box3F& b)
  187. {
  188. bool success = mathWrite(stream, b.minExtents);
  189. success &= mathWrite(stream, b.maxExtents);
  190. return success;
  191. }
  192. inline bool mathWrite(Stream& stream, const SphereF& s)
  193. {
  194. bool success = mathWrite(stream, s.center);
  195. success &= stream.write(s.radius);
  196. return success;
  197. }
  198. inline bool mathWrite(Stream& stream, const RectI& r)
  199. {
  200. bool success = mathWrite(stream, r.point);
  201. success &= mathWrite(stream, r.extent);
  202. return success;
  203. }
  204. inline bool mathWrite(Stream& stream, const RectF& r)
  205. {
  206. bool success = mathWrite(stream, r.point);
  207. success &= mathWrite(stream, r.extent);
  208. return success;
  209. }
  210. inline bool mathWrite(Stream& stream, const MatrixF& m)
  211. {
  212. bool success = true;
  213. const F32* pm = m;
  214. for (U32 i = 0; i < 16; i++)
  215. success &= stream.write(pm[i]);
  216. return success;
  217. }
  218. inline bool mathWrite(Stream& stream, const QuatF& q)
  219. {
  220. bool success = stream.write(q.x);
  221. success &= stream.write(q.y);
  222. success &= stream.write(q.z);
  223. success &= stream.write(q.w);
  224. return success;
  225. }
  226. inline bool mathWrite(Stream& stream, const EaseF& e)
  227. {
  228. bool success = stream.write(e.dir);
  229. success &= stream.write(e.type);
  230. success &= stream.write(e.param[0]);
  231. success &= stream.write(e.param[1]);
  232. return success;
  233. }
  234. #endif //_MATHIO_H_