llsdutil_math.cpp 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  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=viewergpl$
  8. *
  9. * Copyright (c) 2006-2009, Linden Research, Inc.
  10. *
  11. * Second Life Viewer Source Code
  12. * The source code in this file ("Source Code") is provided by Linden Lab
  13. * to you under the terms of the GNU General Public License, version 2.0
  14. * ("GPL"), unless you have obtained a separate licensing agreement
  15. * ("Other License"), formally executed by you and Linden Lab. Terms of
  16. * the GPL can be found in doc/GPL-license.txt in this distribution, or
  17. * online at http://secondlifegrid.net/programs/open_source/licensing/gplv2
  18. *
  19. * There are special exceptions to the terms and conditions of the GPL as
  20. * it is applied to this Source Code. View the full text of the exception
  21. * in the file doc/FLOSS-exception.txt in this software distribution, or
  22. * online at
  23. * http://secondlifegrid.net/programs/open_source/licensing/flossexception
  24. *
  25. * By copying, modifying or distributing this software, you acknowledge
  26. * that you have read and understood your obligations described above,
  27. * and agree to abide by those obligations.
  28. *
  29. * ALL LINDEN LAB SOURCE CODE IS PROVIDED "AS IS." LINDEN LAB MAKES NO
  30. * WARRANTIES, EXPRESS, IMPLIED OR OTHERWISE, REGARDING ITS ACCURACY,
  31. * COMPLETENESS OR PERFORMANCE.
  32. * $/LicenseInfo$
  33. */
  34. #include "linden_common.h"
  35. #include "llsdutil_math.h"
  36. #include "llvector3.h"
  37. #include "llvector4.h"
  38. #include "llvector3d.h"
  39. #include "llvector2.h"
  40. #include "llquaternion.h"
  41. #include "llcolor4.h"
  42. #if LL_WINDOWS
  43. # define WIN32_LEAN_AND_MEAN
  44. # include <winsock2.h> // For htonl
  45. #elif LL_DARWIN
  46. # include <arpa/inet.h>
  47. #endif
  48. #include "llsdserialize.h"
  49. // vector3
  50. LLSD ll_sd_from_vector3(const LLVector3& vec)
  51. {
  52. LLSD rv;
  53. rv.append((F64)vec.mV[VX]);
  54. rv.append((F64)vec.mV[VY]);
  55. rv.append((F64)vec.mV[VZ]);
  56. return rv;
  57. }
  58. LLVector3 ll_vector3_from_sd(const LLSD& sd, S32 start_index)
  59. {
  60. LLVector3 rv;
  61. rv.mV[VX] = (F32)sd[start_index].asReal();
  62. rv.mV[VY] = (F32)sd[++start_index].asReal();
  63. rv.mV[VZ] = (F32)sd[++start_index].asReal();
  64. return rv;
  65. }
  66. // vector4
  67. LLSD ll_sd_from_vector4(const LLVector4& vec)
  68. {
  69. LLSD rv;
  70. rv.append((F64)vec.mV[VX]);
  71. rv.append((F64)vec.mV[VY]);
  72. rv.append((F64)vec.mV[VZ]);
  73. rv.append((F64)vec.mV[VW]);
  74. return rv;
  75. }
  76. LLVector4 ll_vector4_from_sd(const LLSD& sd, S32 start_index)
  77. {
  78. LLVector4 rv;
  79. rv.mV[VX] = (F32)sd[start_index].asReal();
  80. rv.mV[VY] = (F32)sd[++start_index].asReal();
  81. rv.mV[VZ] = (F32)sd[++start_index].asReal();
  82. rv.mV[VW] = (F32)sd[++start_index].asReal();
  83. return rv;
  84. }
  85. // vector3d
  86. LLSD ll_sd_from_vector3d(const LLVector3d& vec)
  87. {
  88. LLSD rv;
  89. rv.append(vec.mdV[VX]);
  90. rv.append(vec.mdV[VY]);
  91. rv.append(vec.mdV[VZ]);
  92. return rv;
  93. }
  94. LLVector3d ll_vector3d_from_sd(const LLSD& sd, S32 start_index)
  95. {
  96. LLVector3d rv;
  97. rv.mdV[VX] = sd[start_index].asReal();
  98. rv.mdV[VY] = sd[++start_index].asReal();
  99. rv.mdV[VZ] = sd[++start_index].asReal();
  100. return rv;
  101. }
  102. //vector2
  103. LLSD ll_sd_from_vector2(const LLVector2& vec)
  104. {
  105. LLSD rv;
  106. rv.append((F64)vec.mV[VX]);
  107. rv.append((F64)vec.mV[VY]);
  108. return rv;
  109. }
  110. LLVector2 ll_vector2_from_sd(const LLSD& sd)
  111. {
  112. LLVector2 rv;
  113. rv.mV[VX] = (F32)sd[0].asReal();
  114. rv.mV[VY] = (F32)sd[1].asReal();
  115. return rv;
  116. }
  117. // Quaternion
  118. LLSD ll_sd_from_quaternion(const LLQuaternion& quat)
  119. {
  120. LLSD rv;
  121. rv.append((F64)quat.mQ[VX]);
  122. rv.append((F64)quat.mQ[VY]);
  123. rv.append((F64)quat.mQ[VZ]);
  124. rv.append((F64)quat.mQ[VW]);
  125. return rv;
  126. }
  127. LLQuaternion ll_quaternion_from_sd(const LLSD& sd)
  128. {
  129. LLQuaternion quat;
  130. quat.mQ[VX] = (F32)sd[0].asReal();
  131. quat.mQ[VY] = (F32)sd[1].asReal();
  132. quat.mQ[VZ] = (F32)sd[2].asReal();
  133. quat.mQ[VW] = (F32)sd[3].asReal();
  134. return quat;
  135. }
  136. // color4
  137. LLSD ll_sd_from_color4(const LLColor4& c)
  138. {
  139. LLSD rv;
  140. rv.append(c.mV[0]);
  141. rv.append(c.mV[1]);
  142. rv.append(c.mV[2]);
  143. rv.append(c.mV[3]);
  144. return rv;
  145. }
  146. LLColor4 ll_color4_from_sd(const LLSD& sd)
  147. {
  148. LLColor4 c;
  149. c.setValue(sd);
  150. return c;
  151. }