llregionhandle.h 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. /**
  2. * @file llregionhandle.h
  3. * @brief Routines for converting positions to/from region handles.
  4. *
  5. * $LicenseInfo:firstyear=2002&license=viewergpl$
  6. *
  7. * Copyright (c) 2002-2009, Linden Research, Inc.
  8. *
  9. * Second Life Viewer Source Code
  10. * The source code in this file ("Source Code") is provided by Linden Lab
  11. * to you under the terms of the GNU General Public License, version 2.0
  12. * ("GPL"), unless you have obtained a separate licensing agreement
  13. * ("Other License"), formally executed by you and Linden Lab. Terms of
  14. * the GPL can be found in doc/GPL-license.txt in this distribution, or
  15. * online at http://secondlifegrid.net/programs/open_source/licensing/gplv2
  16. *
  17. * There are special exceptions to the terms and conditions of the GPL as
  18. * it is applied to this Source Code. View the full text of the exception
  19. * in the file doc/FLOSS-exception.txt in this software distribution, or
  20. * online at
  21. * http://secondlifegrid.net/programs/open_source/licensing/flossexception
  22. *
  23. * By copying, modifying or distributing this software, you acknowledge
  24. * that you have read and understood your obligations described above,
  25. * and agree to abide by those obligations.
  26. *
  27. * ALL LINDEN LAB SOURCE CODE IS PROVIDED "AS IS." LINDEN LAB MAKES NO
  28. * WARRANTIES, EXPRESS, IMPLIED OR OTHERWISE, REGARDING ITS ACCURACY,
  29. * COMPLETENESS OR PERFORMANCE.
  30. * $/LicenseInfo$
  31. */
  32. #ifndef LL_LLREGIONHANDLE_H
  33. #define LL_LLREGIONHANDLE_H
  34. #include "indra_constants.h"
  35. #include "llpreprocessor.h"
  36. #include "llvector3.h"
  37. #include "llvector3d.h"
  38. LL_INLINE U64 to_region_handle(U32 x_origin, U32 y_origin)
  39. {
  40. U64 region_handle;
  41. region_handle = ((U64)x_origin) << 32;
  42. region_handle |= (U64) y_origin;
  43. return region_handle;
  44. }
  45. LL_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. U32 global_y = (U32)pos_global.mdV[VY];
  50. global_y -= global_y % 256;
  51. return to_region_handle(global_x, global_y);
  52. }
  53. LL_INLINE U64 to_region_handle_global(F32 x_global, 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. LL_INLINE bool to_region_handle(F32 x_pos, F32 y_pos, U64* region_handle)
  66. {
  67. U32 x_int, y_int;
  68. if (x_pos < 0.f)
  69. {
  70. #if 0
  71. llwarns << "to_region_handle:Clamping negative x position " << x_pos
  72. << " to zero !" << llendl;
  73. #endif
  74. return false;
  75. }
  76. else
  77. {
  78. x_int = (U32)ll_roundp(x_pos);
  79. }
  80. if (y_pos < 0.f)
  81. {
  82. #if 0
  83. llwarns << "to_region_handle:Clamping negative y position " << y_pos
  84. << " to zero !" << llendl;
  85. #endif
  86. return false;
  87. }
  88. else
  89. {
  90. y_int = (U32)ll_roundp(y_pos);
  91. }
  92. *region_handle = to_region_handle(x_int, y_int);
  93. return true;
  94. }
  95. // Stuffs the word-frame XY location of sim's SouthWest corner in x_pos, y_pos
  96. LL_INLINE void from_region_handle(U64 region_handle, F32* x_pos, F32* y_pos)
  97. {
  98. *x_pos = (F32)((U32)(region_handle >> 32));
  99. *y_pos = (F32)((U32)(region_handle & 0xFFFFFFFF));
  100. }
  101. // Stuffs the word-frame XY location of sim's SouthWest corner in x_pos, y_pos
  102. LL_INLINE void from_region_handle(U64 region_handle, U32* x_pos, U32* y_pos)
  103. {
  104. *x_pos = (U32)(region_handle >> 32);
  105. *y_pos = (U32)(region_handle & 0xFFFFFFFF);
  106. }
  107. // Returns the word-frame XY location of sim's SouthWest corner in LLVector3d
  108. LL_INLINE LLVector3d from_region_handle(U64 region_handle)
  109. {
  110. return LLVector3d((U32)(region_handle >> 32),
  111. (U32)(region_handle & 0xFFFFFFFF), 0.f);
  112. }
  113. // Grid-based region handle encoding. pass in a grid position (eg: 1000,1000)
  114. // and this will return the region handle.
  115. LL_INLINE U64 grid_to_region_handle(U32 grid_x, U32 grid_y)
  116. {
  117. return to_region_handle(grid_x * REGION_WIDTH_UNITS,
  118. grid_y * REGION_WIDTH_UNITS);
  119. }
  120. LL_INLINE void grid_from_region_handle(U64 region_handle, U32* grid_x,
  121. U32* grid_y)
  122. {
  123. from_region_handle(region_handle, grid_x, grid_y);
  124. *grid_x /= REGION_WIDTH_UNITS;
  125. *grid_y /= REGION_WIDTH_UNITS;
  126. }
  127. #endif