PageRenderTime 44ms CodeModel.GetById 30ms app.highlight 10ms RepoModel.GetById 1ms app.codeStats 0ms

/indra/llmessage/llregionhandle.h

https://bitbucket.org/lindenlab/viewer-beta/
C++ Header | 126 lines | 79 code | 14 blank | 33 comment | 2 complexity | fc2b28011e94195051206edd881810dc MD5 | raw file
  1/** 
  2 * @file llregionhandle.h
  3 * @brief Routines for converting positions to/from region handles.
  4 *
  5 * $LicenseInfo:firstyear=2002&license=viewerlgpl$
  6 * Second Life Viewer Source Code
  7 * Copyright (C) 2010, Linden Research, Inc.
  8 * 
  9 * This library is free software; you can redistribute it and/or
 10 * modify it under the terms of the GNU Lesser General Public
 11 * License as published by the Free Software Foundation;
 12 * version 2.1 of the License only.
 13 * 
 14 * This library is distributed in the hope that it will be useful,
 15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 17 * Lesser General Public License for more details.
 18 * 
 19 * You should have received a copy of the GNU Lesser General Public
 20 * License along with this library; if not, write to the Free Software
 21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 22 * 
 23 * Linden Research, Inc., 945 Battery Street, San Francisco, CA  94111  USA
 24 * $/LicenseInfo$
 25 */
 26
 27#ifndef LL_LLREGIONHANDLE_H
 28#define LL_LLREGIONHANDLE_H
 29
 30#include "indra_constants.h"
 31#include "v3math.h"
 32#include "v3dmath.h"
 33
 34inline U64 to_region_handle(const U32 x_origin, const U32 y_origin)
 35{
 36	U64 region_handle;
 37	region_handle =  ((U64)x_origin) << 32;
 38	region_handle |= (U64) y_origin;
 39	return region_handle;
 40}
 41
 42inline U64 to_region_handle(const LLVector3d& pos_global)
 43{
 44	U32 global_x = (U32)pos_global.mdV[VX];
 45	global_x -= global_x % 256;
 46
 47	U32 global_y = (U32)pos_global.mdV[VY];
 48	global_y -= global_y % 256;
 49
 50	return to_region_handle(global_x, global_y);
 51}
 52
 53inline U64 to_region_handle_global(const F32 x_global, const F32 y_global)
 54{
 55	// Round down to the nearest origin
 56	U32 x_origin = (U32)x_global;
 57	x_origin -= x_origin % REGION_WIDTH_U32;
 58	U32 y_origin = (U32)y_global;
 59	y_origin -= y_origin % REGION_WIDTH_U32;
 60	U64 region_handle;
 61	region_handle =  ((U64)x_origin) << 32;
 62	region_handle |= (U64) y_origin;
 63	return region_handle;
 64}
 65
 66inline BOOL to_region_handle(const F32 x_pos, const F32 y_pos, U64 *region_handle)
 67{
 68	U32 x_int, y_int;
 69	if (x_pos < 0.f)
 70	{
 71//		llwarns << "to_region_handle:Clamping negative x position " << x_pos << " to zero!" << llendl;
 72		return FALSE;
 73	}
 74	else
 75	{
 76		x_int = (U32)llround(x_pos);
 77	}
 78	if (y_pos < 0.f)
 79	{
 80//		llwarns << "to_region_handle:Clamping negative y position " << y_pos << " to zero!" << llendl;
 81		return FALSE;
 82	}
 83	else
 84	{
 85		y_int = (U32)llround(y_pos);
 86	}
 87	*region_handle = to_region_handle(x_int, y_int);
 88	return TRUE;
 89}
 90
 91// stuff the word-frame XY location of sim's SouthWest corner in x_pos, y_pos
 92inline void from_region_handle(const U64 &region_handle, F32 *x_pos, F32 *y_pos)
 93{
 94	*x_pos = (F32)((U32)(region_handle >> 32));
 95	*y_pos = (F32)((U32)(region_handle & 0xFFFFFFFF));
 96}
 97
 98// stuff the word-frame XY location of sim's SouthWest corner in x_pos, y_pos
 99inline void from_region_handle(const U64 &region_handle, U32 *x_pos, U32 *y_pos)
100{
101	*x_pos = ((U32)(region_handle >> 32));
102	*y_pos = ((U32)(region_handle & 0xFFFFFFFF));
103}
104
105// return the word-frame XY location of sim's SouthWest corner in LLVector3d
106inline LLVector3d from_region_handle(const U64 &region_handle)
107{
108	return LLVector3d(((U32)(region_handle >> 32)), (U32)(region_handle & 0xFFFFFFFF), 0.f);
109}
110
111// grid-based region handle encoding. pass in a grid position
112// (eg: 1000,1000) and this will return the region handle.
113inline U64 grid_to_region_handle(const U32 grid_x, const U32 grid_y)
114{
115	return to_region_handle(grid_x * REGION_WIDTH_UNITS,
116							grid_y * REGION_WIDTH_UNITS);
117}
118
119inline void grid_from_region_handle(const U64& region_handle, U32* grid_x, U32* grid_y)
120{
121	from_region_handle(region_handle, grid_x, grid_y);
122	*grid_x /= REGION_WIDTH_UNITS;
123	*grid_y /= REGION_WIDTH_UNITS;
124}
125	
126#endif