PageRenderTime 26ms CodeModel.GetById 1ms app.highlight 21ms RepoModel.GetById 2ms 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
  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
 29#include "linden_common.h"
 30
 31#include "llsdutil_math.h"
 32
 33#include "v3math.h"
 34#include "v4math.h"
 35#include "v3dmath.h"
 36#include "v2math.h"
 37#include "llquaternion.h"
 38#include "v4color.h"
 39
 40#if LL_WINDOWS
 41#	define WIN32_LEAN_AND_MEAN
 42#	include <winsock2.h>	// for htonl
 43#elif LL_LINUX || LL_SOLARIS
 44#	include <netinet/in.h>
 45#elif LL_DARWIN
 46#	include <arpa/inet.h>
 47#endif
 48
 49#include "llsdserialize.h"
 50
 51// vector3
 52LLSD ll_sd_from_vector3(const LLVector3& vec)
 53{
 54	LLSD rv;
 55	rv.append((F64)vec.mV[VX]);
 56	rv.append((F64)vec.mV[VY]);
 57	rv.append((F64)vec.mV[VZ]);
 58	return rv;
 59}
 60
 61LLVector3 ll_vector3_from_sd(const LLSD& sd, S32 start_index)
 62{
 63	LLVector3 rv;
 64	rv.mV[VX] = (F32)sd[start_index].asReal();
 65	rv.mV[VY] = (F32)sd[++start_index].asReal();
 66	rv.mV[VZ] = (F32)sd[++start_index].asReal();
 67	return rv;
 68}
 69
 70// vector4
 71LLSD ll_sd_from_vector4(const LLVector4& vec)
 72{
 73	LLSD rv;
 74	rv.append((F64)vec.mV[VX]);
 75	rv.append((F64)vec.mV[VY]);
 76	rv.append((F64)vec.mV[VZ]);
 77	rv.append((F64)vec.mV[VW]);
 78	return rv;
 79}
 80
 81LLVector4 ll_vector4_from_sd(const LLSD& sd, S32 start_index)
 82{
 83	LLVector4 rv;
 84	rv.mV[VX] = (F32)sd[start_index].asReal();
 85	rv.mV[VY] = (F32)sd[++start_index].asReal();
 86	rv.mV[VZ] = (F32)sd[++start_index].asReal();
 87	rv.mV[VW] = (F32)sd[++start_index].asReal();
 88	return rv;
 89}
 90
 91// vector3d
 92LLSD ll_sd_from_vector3d(const LLVector3d& vec)
 93{
 94	LLSD rv;
 95	rv.append(vec.mdV[VX]);
 96	rv.append(vec.mdV[VY]);
 97	rv.append(vec.mdV[VZ]);
 98	return rv;
 99}
100
101LLVector3d ll_vector3d_from_sd(const LLSD& sd, S32 start_index)
102{
103	LLVector3d rv;
104	rv.mdV[VX] = sd[start_index].asReal();
105	rv.mdV[VY] = sd[++start_index].asReal();
106	rv.mdV[VZ] = sd[++start_index].asReal();
107	return rv;
108}
109
110//vector2
111LLSD ll_sd_from_vector2(const LLVector2& vec)
112{
113	LLSD rv;
114	rv.append((F64)vec.mV[VX]);
115	rv.append((F64)vec.mV[VY]);
116	return rv;
117}
118
119LLVector2 ll_vector2_from_sd(const LLSD& sd)
120{
121	LLVector2 rv;
122	rv.mV[VX] = (F32)sd[0].asReal();
123	rv.mV[VY] = (F32)sd[1].asReal();
124	return rv;
125}
126
127// Quaternion
128LLSD ll_sd_from_quaternion(const LLQuaternion& quat)
129{
130	LLSD rv;
131	rv.append((F64)quat.mQ[VX]);
132	rv.append((F64)quat.mQ[VY]);
133	rv.append((F64)quat.mQ[VZ]);
134	rv.append((F64)quat.mQ[VW]);
135	return rv;
136}
137
138LLQuaternion ll_quaternion_from_sd(const LLSD& sd)
139{
140	LLQuaternion quat;
141	quat.mQ[VX] = (F32)sd[0].asReal();
142	quat.mQ[VY] = (F32)sd[1].asReal();
143	quat.mQ[VZ] = (F32)sd[2].asReal();
144	quat.mQ[VW] = (F32)sd[3].asReal();
145	return quat;
146}
147
148// color4
149LLSD ll_sd_from_color4(const LLColor4& c)
150{
151	LLSD rv;
152	rv.append(c.mV[0]);
153	rv.append(c.mV[1]);
154	rv.append(c.mV[2]);
155	rv.append(c.mV[3]);
156	return rv;
157}
158
159LLColor4 ll_color4_from_sd(const LLSD& sd)
160{
161	LLColor4 c;
162	c.setValue(sd);
163	return c;
164}