00001
00032 #ifndef LL_LLREGIONHANDLE_H
00033 #define LL_LLREGIONHANDLE_H
00034
00035 #include "indra_constants.h"
00036 #include "v3math.h"
00037 #include "v3dmath.h"
00038
00039 inline U64 to_region_handle(const U32 x_origin, const U32 y_origin)
00040 {
00041 U64 region_handle;
00042 region_handle = ((U64)x_origin) << 32;
00043 region_handle |= (U64) y_origin;
00044 return region_handle;
00045 }
00046
00047 inline U64 to_region_handle(const LLVector3d& pos_global)
00048 {
00049 U32 global_x = (U32)pos_global.mdV[VX];
00050 global_x -= global_x % 256;
00051
00052 U32 global_y = (U32)pos_global.mdV[VY];
00053 global_y -= global_y % 256;
00054
00055 return to_region_handle(global_x, global_y);
00056 }
00057
00058 inline U64 to_region_handle_global(const F32 x_global, const F32 y_global)
00059 {
00060
00061 U32 x_origin = (U32)x_global;
00062 x_origin -= x_origin % REGION_WIDTH_U32;
00063 U32 y_origin = (U32)y_global;
00064 y_origin -= y_origin % REGION_WIDTH_U32;
00065 U64 region_handle;
00066 region_handle = ((U64)x_origin) << 32;
00067 region_handle |= (U64) y_origin;
00068 return region_handle;
00069 }
00070
00071 inline BOOL to_region_handle(const F32 x_pos, const F32 y_pos, U64 *region_handle)
00072 {
00073 U32 x_int, y_int;
00074 if (x_pos < 0.f)
00075 {
00076
00077 return FALSE;
00078 }
00079 else
00080 {
00081 x_int = (U32)llround(x_pos);
00082 }
00083 if (y_pos < 0.f)
00084 {
00085
00086 return FALSE;
00087 }
00088 else
00089 {
00090 y_int = (U32)llround(y_pos);
00091 }
00092 *region_handle = to_region_handle(x_int, y_int);
00093 return TRUE;
00094 }
00095
00096
00097 inline void from_region_handle(const U64 ®ion_handle, F32 *x_pos, F32 *y_pos)
00098 {
00099 *x_pos = (F32)((U32)(region_handle >> 32));
00100 *y_pos = (F32)((U32)(region_handle & 0xFFFFFFFF));
00101 }
00102
00103
00104 inline void from_region_handle(const U64 ®ion_handle, U32 *x_pos, U32 *y_pos)
00105 {
00106 *x_pos = ((U32)(region_handle >> 32));
00107 *y_pos = ((U32)(region_handle & 0xFFFFFFFF));
00108 }
00109
00110
00111 inline LLVector3d from_region_handle(const U64 ®ion_handle)
00112 {
00113 return LLVector3d(((U32)(region_handle >> 32)), (U32)(region_handle & 0xFFFFFFFF), 0.f);
00114 }
00115
00116
00117
00118 inline U64 grid_to_region_handle(U32 grid_x, U32 grid_y)
00119 {
00120 return to_region_handle(grid_x * REGION_WIDTH_UNITS,
00121 grid_y * REGION_WIDTH_UNITS);
00122 }
00123
00124 inline void grid_from_region_handle(const U64& region_handle, U32* grid_x, U32* grid_y)
00125 {
00126 from_region_handle(region_handle, grid_x, grid_y);
00127 *grid_x /= REGION_WIDTH_UNITS;
00128 *grid_y /= REGION_WIDTH_UNITS;
00129 }
00130
00131 #endif