PageRenderTime 2571ms CodeModel.GetById 2528ms RepoModel.GetById 0ms 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
Possible License(s): LGPL-2.1
  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. #ifndef LL_LLREGIONHANDLE_H
  27. #define LL_LLREGIONHANDLE_H
  28. #include "indra_constants.h"
  29. #include "v3math.h"
  30. #include "v3dmath.h"
  31. inline U64 to_region_handle(const U32 x_origin, const U32 y_origin)
  32. {
  33. U64 region_handle;
  34. region_handle = ((U64)x_origin) << 32;
  35. region_handle |= (U64) y_origin;
  36. return region_handle;
  37. }
  38. inline U64 to_region_handle(const LLVector3d& pos_global)
  39. {
  40. U32 global_x = (U32)pos_global.mdV[VX];
  41. global_x -= global_x % 256;
  42. U32 global_y = (U32)pos_global.mdV[VY];
  43. global_y -= global_y % 256;
  44. return to_region_handle(global_x, global_y);
  45. }
  46. inline U64 to_region_handle_global(const F32 x_global, const F32 y_global)
  47. {
  48. // Round down to the nearest origin
  49. U32 x_origin = (U32)x_global;
  50. x_origin -= x_origin % REGION_WIDTH_U32;
  51. U32 y_origin = (U32)y_global;
  52. y_origin -= y_origin % REGION_WIDTH_U32;
  53. U64 region_handle;
  54. region_handle = ((U64)x_origin) << 32;
  55. region_handle |= (U64) y_origin;
  56. return region_handle;
  57. }
  58. inline BOOL to_region_handle(const F32 x_pos, const F32 y_pos, U64 *region_handle)
  59. {
  60. U32 x_int, y_int;
  61. if (x_pos < 0.f)
  62. {
  63. // llwarns << "to_region_handle:Clamping negative x position " << x_pos << " to zero!" << llendl;
  64. return FALSE;
  65. }
  66. else
  67. {
  68. x_int = (U32)llround(x_pos);
  69. }
  70. if (y_pos < 0.f)
  71. {
  72. // llwarns << "to_region_handle:Clamping negative y position " << y_pos << " to zero!" << llendl;
  73. return FALSE;
  74. }
  75. else
  76. {
  77. y_int = (U32)llround(y_pos);
  78. }
  79. *region_handle = to_region_handle(x_int, y_int);
  80. return TRUE;
  81. }
  82. // stuff the word-frame XY location of sim's SouthWest corner in x_pos, y_pos
  83. inline void from_region_handle(const U64 &region_handle, F32 *x_pos, F32 *y_pos)
  84. {
  85. *x_pos = (F32)((U32)(region_handle >> 32));
  86. *y_pos = (F32)((U32)(region_handle & 0xFFFFFFFF));
  87. }
  88. // stuff the word-frame XY location of sim's SouthWest corner in x_pos, y_pos
  89. inline void from_region_handle(const U64 &region_handle, U32 *x_pos, U32 *y_pos)
  90. {
  91. *x_pos = ((U32)(region_handle >> 32));
  92. *y_pos = ((U32)(region_handle & 0xFFFFFFFF));
  93. }
  94. // return the word-frame XY location of sim's SouthWest corner in LLVector3d
  95. inline LLVector3d from_region_handle(const U64 &region_handle)
  96. {
  97. return LLVector3d(((U32)(region_handle >> 32)), (U32)(region_handle & 0xFFFFFFFF), 0.f);
  98. }
  99. // grid-based region handle encoding. pass in a grid position
  100. // (eg: 1000,1000) and this will return the region handle.
  101. inline U64 grid_to_region_handle(const U32 grid_x, const U32 grid_y)
  102. {
  103. return to_region_handle(grid_x * REGION_WIDTH_UNITS,
  104. grid_y * REGION_WIDTH_UNITS);
  105. }
  106. inline void grid_from_region_handle(const U64& region_handle, U32* grid_x, U32* grid_y)
  107. {
  108. from_region_handle(region_handle, grid_x, grid_y);
  109. *grid_x /= REGION_WIDTH_UNITS;
  110. *grid_y /= REGION_WIDTH_UNITS;
  111. }
  112. #endif