mathIO.h 6.7 KB

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