PageRenderTime 38ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/indra/llmath/llsdutil_math.cpp

https://bitbucket.org/lindenlab/viewer-beta/
C++ | 164 lines | 114 code | 17 blank | 33 comment | 1 complexity | e674d8f163d66e877ea826fb7e3cc564 MD5 | raw file
Possible License(s): LGPL-2.1
  1. /**
  2. * @file llsdutil_math.cpp
  3. * @author Phoenix
  4. * @date 2006-05-24
  5. * @brief Implementation of classes, functions, etc, for using structured data.
  6. *
  7. * $LicenseInfo:firstyear=2006&license=viewerlgpl$
  8. * Second Life Viewer Source Code
  9. * Copyright (C) 2010, Linden Research, Inc.
  10. *
  11. * This library is free software; you can redistribute it and/or
  12. * modify it under the terms of the GNU Lesser General Public
  13. * License as published by the Free Software Foundation;
  14. * version 2.1 of the License only.
  15. *
  16. * This library is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  19. * Lesser General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU Lesser General Public
  22. * License along with this library; if not, write to the Free Software
  23. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  24. *
  25. * Linden Research, Inc., 945 Battery Street, San Francisco, CA 94111 USA
  26. * $/LicenseInfo$
  27. */
  28. #include "linden_common.h"
  29. #include "llsdutil_math.h"
  30. #include "v3math.h"
  31. #include "v4math.h"
  32. #include "v3dmath.h"
  33. #include "v2math.h"
  34. #include "llquaternion.h"
  35. #include "v4color.h"
  36. #if LL_WINDOWS
  37. # define WIN32_LEAN_AND_MEAN
  38. # include <winsock2.h> // for htonl
  39. #elif LL_LINUX || LL_SOLARIS
  40. # include <netinet/in.h>
  41. #elif LL_DARWIN
  42. # include <arpa/inet.h>
  43. #endif
  44. #include "llsdserialize.h"
  45. // vector3
  46. LLSD ll_sd_from_vector3(const LLVector3& vec)
  47. {
  48. LLSD rv;
  49. rv.append((F64)vec.mV[VX]);
  50. rv.append((F64)vec.mV[VY]);
  51. rv.append((F64)vec.mV[VZ]);
  52. return rv;
  53. }
  54. LLVector3 ll_vector3_from_sd(const LLSD& sd, S32 start_index)
  55. {
  56. LLVector3 rv;
  57. rv.mV[VX] = (F32)sd[start_index].asReal();
  58. rv.mV[VY] = (F32)sd[++start_index].asReal();
  59. rv.mV[VZ] = (F32)sd[++start_index].asReal();
  60. return rv;
  61. }
  62. // vector4
  63. LLSD ll_sd_from_vector4(const LLVector4& vec)
  64. {
  65. LLSD rv;
  66. rv.append((F64)vec.mV[VX]);
  67. rv.append((F64)vec.mV[VY]);
  68. rv.append((F64)vec.mV[VZ]);
  69. rv.append((F64)vec.mV[VW]);
  70. return rv;
  71. }
  72. LLVector4 ll_vector4_from_sd(const LLSD& sd, S32 start_index)
  73. {
  74. LLVector4 rv;
  75. rv.mV[VX] = (F32)sd[start_index].asReal();
  76. rv.mV[VY] = (F32)sd[++start_index].asReal();
  77. rv.mV[VZ] = (F32)sd[++start_index].asReal();
  78. rv.mV[VW] = (F32)sd[++start_index].asReal();
  79. return rv;
  80. }
  81. // vector3d
  82. LLSD ll_sd_from_vector3d(const LLVector3d& vec)
  83. {
  84. LLSD rv;
  85. rv.append(vec.mdV[VX]);
  86. rv.append(vec.mdV[VY]);
  87. rv.append(vec.mdV[VZ]);
  88. return rv;
  89. }
  90. LLVector3d ll_vector3d_from_sd(const LLSD& sd, S32 start_index)
  91. {
  92. LLVector3d rv;
  93. rv.mdV[VX] = sd[start_index].asReal();
  94. rv.mdV[VY] = sd[++start_index].asReal();
  95. rv.mdV[VZ] = sd[++start_index].asReal();
  96. return rv;
  97. }
  98. //vector2
  99. LLSD ll_sd_from_vector2(const LLVector2& vec)
  100. {
  101. LLSD rv;
  102. rv.append((F64)vec.mV[VX]);
  103. rv.append((F64)vec.mV[VY]);
  104. return rv;
  105. }
  106. LLVector2 ll_vector2_from_sd(const LLSD& sd)
  107. {
  108. LLVector2 rv;
  109. rv.mV[VX] = (F32)sd[0].asReal();
  110. rv.mV[VY] = (F32)sd[1].asReal();
  111. return rv;
  112. }
  113. // Quaternion
  114. LLSD ll_sd_from_quaternion(const LLQuaternion& quat)
  115. {
  116. LLSD rv;
  117. rv.append((F64)quat.mQ[VX]);
  118. rv.append((F64)quat.mQ[VY]);
  119. rv.append((F64)quat.mQ[VZ]);
  120. rv.append((F64)quat.mQ[VW]);
  121. return rv;
  122. }
  123. LLQuaternion ll_quaternion_from_sd(const LLSD& sd)
  124. {
  125. LLQuaternion quat;
  126. quat.mQ[VX] = (F32)sd[0].asReal();
  127. quat.mQ[VY] = (F32)sd[1].asReal();
  128. quat.mQ[VZ] = (F32)sd[2].asReal();
  129. quat.mQ[VW] = (F32)sd[3].asReal();
  130. return quat;
  131. }
  132. // color4
  133. LLSD ll_sd_from_color4(const LLColor4& c)
  134. {
  135. LLSD rv;
  136. rv.append(c.mV[0]);
  137. rv.append(c.mV[1]);
  138. rv.append(c.mV[2]);
  139. rv.append(c.mV[3]);
  140. return rv;
  141. }
  142. LLColor4 ll_color4_from_sd(const LLSD& sd)
  143. {
  144. LLColor4 c;
  145. c.setValue(sd);
  146. return c;
  147. }