diff options
Diffstat (limited to 'linden/indra/llmessage/llregionhandle.h')
-rw-r--r-- | linden/indra/llmessage/llregionhandle.h | 129 |
1 files changed, 129 insertions, 0 deletions
diff --git a/linden/indra/llmessage/llregionhandle.h b/linden/indra/llmessage/llregionhandle.h new file mode 100644 index 0000000..d943374 --- /dev/null +++ b/linden/indra/llmessage/llregionhandle.h | |||
@@ -0,0 +1,129 @@ | |||
1 | /** | ||
2 | * @file llregionhandle.h | ||
3 | * @brief Routines for converting positions to/from region handles. | ||
4 | * | ||
5 | * Copyright (c) 2002-2007, Linden Research, Inc. | ||
6 | * | ||
7 | * The source code in this file ("Source Code") is provided by Linden Lab | ||
8 | * to you under the terms of the GNU General Public License, version 2.0 | ||
9 | * ("GPL"), unless you have obtained a separate licensing agreement | ||
10 | * ("Other License"), formally executed by you and Linden Lab. Terms of | ||
11 | * the GPL can be found in doc/GPL-license.txt in this distribution, or | ||
12 | * online at http://secondlife.com/developers/opensource/gplv2 | ||
13 | * | ||
14 | * There are special exceptions to the terms and conditions of the GPL as | ||
15 | * it is applied to this Source Code. View the full text of the exception | ||
16 | * in the file doc/FLOSS-exception.txt in this software distribution, or | ||
17 | * online at http://secondlife.com/developers/opensource/flossexception | ||
18 | * | ||
19 | * By copying, modifying or distributing this software, you acknowledge | ||
20 | * that you have read and understood your obligations described above, | ||
21 | * and agree to abide by those obligations. | ||
22 | * | ||
23 | * ALL LINDEN LAB SOURCE CODE IS PROVIDED "AS IS." LINDEN LAB MAKES NO | ||
24 | * WARRANTIES, EXPRESS, IMPLIED OR OTHERWISE, REGARDING ITS ACCURACY, | ||
25 | * COMPLETENESS OR PERFORMANCE. | ||
26 | */ | ||
27 | |||
28 | #ifndef LL_LLREGIONHANDLE_H | ||
29 | #define LL_LLREGIONHANDLE_H | ||
30 | |||
31 | #include <math.h> | ||
32 | |||
33 | #include "indra_constants.h" | ||
34 | #include "v3math.h" | ||
35 | #include "v3dmath.h" | ||
36 | |||
37 | inline U64 to_region_handle(const U32 x_origin, const U32 y_origin) | ||
38 | { | ||
39 | U64 region_handle; | ||
40 | region_handle = ((U64)x_origin) << 32; | ||
41 | region_handle |= (U64) y_origin; | ||
42 | return region_handle; | ||
43 | } | ||
44 | |||
45 | inline U64 to_region_handle(const LLVector3d& pos_global) | ||
46 | { | ||
47 | U32 global_x = (U32)pos_global.mdV[VX]; | ||
48 | global_x -= global_x % 256; | ||
49 | |||
50 | U32 global_y = (U32)pos_global.mdV[VY]; | ||
51 | global_y -= global_y % 256; | ||
52 | |||
53 | return to_region_handle(global_x, global_y); | ||
54 | } | ||
55 | |||
56 | inline U64 to_region_handle_global(const F32 x_global, const F32 y_global) | ||
57 | { | ||
58 | // Round down to the nearest origin | ||
59 | U32 x_origin = (U32)x_global; | ||
60 | x_origin -= x_origin % REGION_WIDTH_U32; | ||
61 | U32 y_origin = (U32)y_global; | ||
62 | y_origin -= y_origin % REGION_WIDTH_U32; | ||
63 | U64 region_handle; | ||
64 | region_handle = ((U64)x_origin) << 32; | ||
65 | region_handle |= (U64) y_origin; | ||
66 | return region_handle; | ||
67 | } | ||
68 | |||
69 | inline BOOL to_region_handle(const F32 x_pos, const F32 y_pos, U64 *region_handle) | ||
70 | { | ||
71 | U32 x_int, y_int; | ||
72 | if (x_pos < 0.f) | ||
73 | { | ||
74 | // llwarns << "to_region_handle:Clamping negative x position " << x_pos << " to zero!" << llendl; | ||
75 | return FALSE; | ||
76 | } | ||
77 | else | ||
78 | { | ||
79 | x_int = (U32)llround(x_pos); | ||
80 | } | ||
81 | if (y_pos < 0.f) | ||
82 | { | ||
83 | // llwarns << "to_region_handle:Clamping negative y position " << y_pos << " to zero!" << llendl; | ||
84 | return FALSE; | ||
85 | } | ||
86 | else | ||
87 | { | ||
88 | y_int = (U32)llround(y_pos); | ||
89 | } | ||
90 | *region_handle = to_region_handle(x_int, y_int); | ||
91 | return TRUE; | ||
92 | } | ||
93 | |||
94 | // stuff the word-frame XY location of sim's SouthWest corner in x_pos, y_pos | ||
95 | inline void from_region_handle(const U64 ®ion_handle, F32 *x_pos, F32 *y_pos) | ||
96 | { | ||
97 | *x_pos = (F32)((U32)(region_handle >> 32)); | ||
98 | *y_pos = (F32)((U32)(region_handle & 0xFFFFFFFF)); | ||
99 | } | ||
100 | |||
101 | // stuff the word-frame XY location of sim's SouthWest corner in x_pos, y_pos | ||
102 | inline void from_region_handle(const U64 ®ion_handle, U32 *x_pos, U32 *y_pos) | ||
103 | { | ||
104 | *x_pos = ((U32)(region_handle >> 32)); | ||
105 | *y_pos = ((U32)(region_handle & 0xFFFFFFFF)); | ||
106 | } | ||
107 | |||
108 | // return the word-frame XY location of sim's SouthWest corner in LLVector3d | ||
109 | inline LLVector3d from_region_handle(const U64 ®ion_handle) | ||
110 | { | ||
111 | return LLVector3d(((U32)(region_handle >> 32)), (U32)(region_handle & 0xFFFFFFFF), 0.f); | ||
112 | } | ||
113 | |||
114 | // grid-based region handle encoding. pass in a grid position | ||
115 | // (eg: 1000,1000) and this will return the region handle. | ||
116 | inline U64 grid_to_region_handle(U32 grid_x, U32 grid_y) | ||
117 | { | ||
118 | return to_region_handle(grid_x * REGION_WIDTH_UNITS, | ||
119 | grid_y * REGION_WIDTH_UNITS); | ||
120 | } | ||
121 | |||
122 | inline void grid_from_region_handle(const U64& region_handle, U32* grid_x, U32* grid_y) | ||
123 | { | ||
124 | from_region_handle(region_handle, grid_x, grid_y); | ||
125 | *grid_x /= REGION_WIDTH_UNITS; | ||
126 | *grid_y /= REGION_WIDTH_UNITS; | ||
127 | } | ||
128 | |||
129 | #endif | ||