BasicQuadTreeNode.cs 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  1. /*
  2. * Copyright (c) Contributors, http://opensimulator.org/
  3. * See CONTRIBUTORS.TXT for a full list of copyright holders.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. * * Redistributions of source code must retain the above copyright
  8. * notice, this list of conditions and the following disclaimer.
  9. * * Redistributions in binary form must reproduce the above copyright
  10. * notice, this list of conditions and the following disclaimer in the
  11. * documentation and/or other materials provided with the distribution.
  12. * * Neither the name of the OpenSim Project nor the
  13. * names of its contributors may be used to endorse or promote products
  14. * derived from this software without specific prior written permission.
  15. *
  16. * THIS SOFTWARE IS PROVIDED BY THE DEVELOPERS ``AS IS'' AND ANY
  17. * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  18. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  19. * DISCLAIMED. IN NO EVENT SHALL THE CONTRIBUTORS BE LIABLE FOR ANY
  20. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  21. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  22. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  23. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  24. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  25. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  26. */
  27. using System;
  28. using System.Collections.Generic;
  29. using OpenSim.Region.Environment.Scenes;
  30. namespace OpenSim.Region.Environment.Types
  31. {
  32. public class BasicQuadTreeNode
  33. {
  34. private List<SceneObjectGroup> m_objects = new List<SceneObjectGroup>();
  35. private BasicQuadTreeNode[] m_childNodes = null;
  36. private BasicQuadTreeNode m_parent = null;
  37. private short m_leftX;
  38. private short m_leftY;
  39. private short m_width;
  40. private short m_height;
  41. //private int m_quadNumber;
  42. private string m_quadID;
  43. public BasicQuadTreeNode(BasicQuadTreeNode parent, string quadID, short leftX, short leftY, short width,
  44. short height)
  45. {
  46. m_parent = parent;
  47. m_quadID = quadID;
  48. m_leftX = leftX;
  49. m_leftY = leftY;
  50. m_width = width;
  51. m_height = height;
  52. // Console.WriteLine("creating quadtree node " + m_quadID);
  53. }
  54. public void AddObject(SceneObjectGroup obj)
  55. {
  56. if (m_childNodes == null)
  57. {
  58. if (!m_objects.Contains(obj))
  59. {
  60. m_objects.Add(obj);
  61. }
  62. }
  63. else
  64. {
  65. if (obj.AbsolutePosition.X < (m_leftX + (m_width/2)))
  66. {
  67. if (obj.AbsolutePosition.Y < (m_leftY + (m_height/2)))
  68. {
  69. m_childNodes[0].AddObject(obj);
  70. }
  71. else
  72. {
  73. m_childNodes[2].AddObject(obj);
  74. }
  75. }
  76. else
  77. {
  78. if (obj.AbsolutePosition.Y < (m_leftY + (m_height/2)))
  79. {
  80. m_childNodes[1].AddObject(obj);
  81. }
  82. else
  83. {
  84. m_childNodes[3].AddObject(obj);
  85. }
  86. }
  87. }
  88. }
  89. public void Subdivide()
  90. {
  91. if (m_childNodes == null)
  92. {
  93. m_childNodes = new BasicQuadTreeNode[4];
  94. m_childNodes[0] =
  95. new BasicQuadTreeNode(this, m_quadID + "1/", m_leftX, m_leftY, (short) (m_width/2),
  96. (short) (m_height/2));
  97. m_childNodes[1] =
  98. new BasicQuadTreeNode(this, m_quadID + "2/", (short) (m_leftX + (m_width/2)), m_leftY,
  99. (short) (m_width/2), (short) (m_height/2));
  100. m_childNodes[2] =
  101. new BasicQuadTreeNode(this, m_quadID + "3/", m_leftX, (short) (m_leftY + (m_height/2)),
  102. (short) (m_width/2), (short) (m_height/2));
  103. m_childNodes[3] =
  104. new BasicQuadTreeNode(this, m_quadID + "4/", (short) (m_leftX + (m_width/2)),
  105. (short) (m_height + (m_height/2)), (short) (m_width/2), (short) (m_height/2));
  106. }
  107. else
  108. {
  109. for (int i = 0; i < m_childNodes.Length; i++)
  110. {
  111. m_childNodes[i].Subdivide();
  112. }
  113. }
  114. }
  115. public List<SceneObjectGroup> GetObjectsFrom(float x, float y)
  116. {
  117. if (m_childNodes == null)
  118. {
  119. return new List<SceneObjectGroup>(m_objects);
  120. }
  121. else
  122. {
  123. if (x < m_leftX + (m_width/2))
  124. {
  125. if (y < m_leftY + (m_height/2))
  126. {
  127. return m_childNodes[0].GetObjectsFrom(x, y);
  128. }
  129. else
  130. {
  131. return m_childNodes[2].GetObjectsFrom(x, y);
  132. }
  133. }
  134. else
  135. {
  136. if (y < m_leftY + (m_height/2))
  137. {
  138. return m_childNodes[1].GetObjectsFrom(x, y);
  139. }
  140. else
  141. {
  142. return m_childNodes[3].GetObjectsFrom(x, y);
  143. }
  144. }
  145. }
  146. }
  147. public List<SceneObjectGroup> GetObjectsFrom(string nodeName)
  148. {
  149. if (nodeName == m_quadID)
  150. {
  151. return new List<SceneObjectGroup>(m_objects);
  152. }
  153. else if (m_childNodes != null)
  154. {
  155. for (int i = 0; i < 4; i++)
  156. {
  157. List<SceneObjectGroup> retVal;
  158. retVal = m_childNodes[i].GetObjectsFrom(nodeName);
  159. if (retVal != null)
  160. {
  161. return retVal;
  162. }
  163. }
  164. }
  165. return null;
  166. }
  167. public string GetNodeID(float x, float y)
  168. {
  169. if (m_childNodes == null)
  170. {
  171. return m_quadID;
  172. }
  173. else
  174. {
  175. if (x < m_leftX + (m_width/2))
  176. {
  177. if (y < m_leftY + (m_height/2))
  178. {
  179. return m_childNodes[0].GetNodeID(x, y);
  180. }
  181. else
  182. {
  183. return m_childNodes[2].GetNodeID(x, y);
  184. }
  185. }
  186. else
  187. {
  188. if (y < m_leftY + (m_height/2))
  189. {
  190. return m_childNodes[1].GetNodeID(x, y);
  191. }
  192. else
  193. {
  194. return m_childNodes[3].GetNodeID(x, y);
  195. }
  196. }
  197. }
  198. }
  199. public void Update()
  200. {
  201. if (m_childNodes != null)
  202. {
  203. for (int i = 0; i < 4; i++)
  204. {
  205. m_childNodes[i].Update();
  206. }
  207. }
  208. else
  209. {
  210. List<SceneObjectGroup> outBounds = new List<SceneObjectGroup>();
  211. foreach (SceneObjectGroup group in m_objects)
  212. {
  213. if (((group.AbsolutePosition.X > m_leftX) && (group.AbsolutePosition.X < (m_leftX + m_width))) &&
  214. ((group.AbsolutePosition.Y > m_leftY) && (group.AbsolutePosition.Y < (m_leftY + m_height))))
  215. {
  216. //still in bounds
  217. }
  218. else
  219. {
  220. outBounds.Add(group);
  221. }
  222. }
  223. foreach (SceneObjectGroup removee in outBounds)
  224. {
  225. m_objects.Remove(removee);
  226. if (m_parent != null)
  227. {
  228. m_parent.PassUp(removee);
  229. }
  230. }
  231. outBounds.Clear();
  232. }
  233. }
  234. public void PassUp(SceneObjectGroup group)
  235. {
  236. if (((group.AbsolutePosition.X > m_leftX) && (group.AbsolutePosition.X < (m_leftX + m_width))) &&
  237. ((group.AbsolutePosition.Y > m_leftY) && (group.AbsolutePosition.Y < (m_leftY + m_height))))
  238. {
  239. AddObject(group);
  240. }
  241. else
  242. {
  243. if (m_parent != null)
  244. {
  245. m_parent.PassUp(group);
  246. }
  247. }
  248. }
  249. public string[] GetNeighbours(string nodeName)
  250. {
  251. string[] retVal = new string[1];
  252. retVal[0] = String.Empty;
  253. return retVal;
  254. }
  255. }
  256. }