Program.cs 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using Microsoft.Xna.Framework;
  7. using System.Diagnostics;
  8. namespace ABTest
  9. {
  10. class SpacialHashTag
  11. {
  12. public int id;
  13. public SpacialHashTag(int id)
  14. {
  15. this.id = id;
  16. }
  17. public override int GetHashCode()
  18. {
  19. return id;
  20. }
  21. public override string ToString()
  22. {
  23. return id.ToString();
  24. }
  25. }
  26. interface ITest
  27. {
  28. void Foo();
  29. }
  30. class TestOne : ITest
  31. {
  32. public void Foo()
  33. {
  34. Console.WriteLine("One!");
  35. }
  36. }
  37. class TestTwo : TestOne, ITest
  38. {
  39. new public void Foo()
  40. {
  41. Console.WriteLine("Two, then -");
  42. base.Foo();
  43. }
  44. }
  45. class Program
  46. {
  47. static void Main(string[] args)
  48. {
  49. ITest foo = new TestTwo();
  50. foo.Foo();
  51. var iterations = 100000;
  52. var cubeSize = 128;
  53. var hash = new SpacialHash.SpatialHash<SpacialHashTag>();
  54. var octtree = new Oct.NaiveOctTreeNode<SpacialHashTag>(new Vector3(0, 0, 0),
  55. new Vector3(cubeSize, cubeSize, cubeSize));
  56. var ioct = new IntegerOct.IntegerOctTreeNode<SpacialHashTag>(new SpacialHash.Point3(0, 0, 0),
  57. new SpacialHash.Point3(cubeSize, cubeSize, cubeSize));
  58. var random = new Random();
  59. var tag = new SpacialHashTag(0); // Use same tag to avoid measuring object creation time.
  60. Debug.WriteLine("Running test: SpacialHash vs OctTree.");
  61. Debug.WriteLine("- Using Same Data -");
  62. Debug.WriteLine("-- Generating {0} random items.", iterations);
  63. var randomItems = new List<Tuple<SpacialHashTag, SpacialHash.Point3>>();
  64. for (var i = 0; i < iterations; ++i)
  65. randomItems.Add(Tuple.Create(new SpacialHashTag(i),
  66. new SpacialHash.Point3(random.Next(cubeSize), random.Next(cubeSize), random.Next(cubeSize))));
  67. hash = new SpacialHash.SpatialHash<SpacialHashTag>();
  68. octtree = new Oct.NaiveOctTreeNode<SpacialHashTag>(
  69. new Vector3(0, 0, 0),
  70. new Vector3(cubeSize, cubeSize, cubeSize));
  71. // Test insertion
  72. Debug.WriteLine("-- Adding generated items.");
  73. var shInsertTestStart = DateTime.Now;
  74. foreach (var item in randomItems)
  75. hash.AddItem(item.Item2, item.Item1);
  76. var shInsertTestEnd = DateTime.Now;
  77. var otInsertTestStart = DateTime.Now;
  78. foreach (var item in randomItems)
  79. octtree.AddItem(item.Item1, item.Item2.AsVector3());
  80. var otInsertTestEnd = DateTime.Now;
  81. var ioInsertTestStart = DateTime.Now;
  82. foreach (var item in randomItems)
  83. ioct.AddItem(item.Item1, item.Item2);
  84. var ioInsertTestEnd = DateTime.Now;
  85. Debug.WriteLine("SH: {0}ms OT: {1}ms IO: {2}ms",
  86. (shInsertTestEnd - shInsertTestStart).TotalMilliseconds,
  87. (otInsertTestEnd - otInsertTestStart).TotalMilliseconds,
  88. (ioInsertTestEnd - ioInsertTestStart).TotalMilliseconds);
  89. Debug.WriteLine("");
  90. // Test searching
  91. Debug.WriteLine("-- Generating {0} random boxes.", iterations);
  92. var randomBoxes = new List<Microsoft.Xna.Framework.BoundingBox>();
  93. var randomBoxesIO = new List<IntegerOct.BoundingBox>();
  94. for (var i = 0; i < iterations; ++i)
  95. {
  96. var box = new SpacialHash.Point3(random.Next(cubeSize), random.Next(cubeSize), random.Next(cubeSize));
  97. var corner = new Microsoft.Xna.Framework.Vector3(box.X, box.Y, box.Z);
  98. randomBoxes.Add(new Microsoft.Xna.Framework.BoundingBox(
  99. corner, corner + new Microsoft.Xna.Framework.Vector3(10, 10, 5)));
  100. randomBoxesIO.Add(new IntegerOct.BoundingBox(randomBoxes[i]));
  101. }
  102. Debug.WriteLine("-- Find items in generated boxes.");
  103. var container = new HashSet<SpacialHashTag>();
  104. var shFindTestStart = DateTime.Now;
  105. foreach (var box in randomBoxes)
  106. hash.GetItemsInBox(box, container);
  107. var shFindTestEnd = DateTime.Now;
  108. container = new HashSet<SpacialHashTag>();
  109. var otFindTestStart = DateTime.Now;
  110. foreach (var box in randomBoxes)
  111. octtree.FindItems(box, container);
  112. var otFindTestEnd = DateTime.Now;
  113. container = new HashSet<SpacialHashTag>();
  114. var ioFindTestStart = DateTime.Now;
  115. foreach (var box in randomBoxesIO)
  116. ioct.FindItemsInBox(box, container);
  117. var ioFindTestEnd = DateTime.Now;
  118. Debug.WriteLine("SH: {0}ms OT: {1}ms IO: {2}ms",
  119. (shFindTestEnd - shFindTestStart).TotalMilliseconds,
  120. (otFindTestEnd - otFindTestStart).TotalMilliseconds,
  121. (ioFindTestEnd - ioFindTestStart).TotalMilliseconds);
  122. Debug.WriteLine("");
  123. Debug.WriteLine("Consistency check: Same items found?");
  124. for (var i = 0; i < 10; ++i)
  125. {
  126. /*Debug.WriteLine("Test " + i + ": {0},{3} {1},{4} {2},{5}",
  127. randomBoxes[i].Min.X,
  128. randomBoxes[i].Min.Y,
  129. randomBoxes[i].Min.Z,
  130. randomBoxes[i].Max.X,
  131. randomBoxes[i].Max.Y,
  132. randomBoxes[i].Max.Z);*/
  133. var hashContainer = new HashSet<SpacialHashTag>();
  134. hash.GetItemsInBox(randomBoxes[i], hashContainer);
  135. var octContainer = new HashSet<SpacialHashTag>();
  136. octtree.FindItems(randomBoxes[i], octContainer);
  137. var ioContainer = new HashSet<SpacialHashTag>();
  138. ioct.FindItemsInBox(randomBoxesIO[i], ioContainer);
  139. #region HashTag Comparisions
  140. List<SpacialHashTag> inAAndB = new List<SpacialHashTag>();
  141. List<SpacialHashTag> inANotB = new List<SpacialHashTag>();
  142. List<SpacialHashTag> inBNotA = new List<SpacialHashTag>();
  143. foreach(SpacialHashTag aTag in hashContainer)
  144. {
  145. bool inB = false;
  146. foreach(SpacialHashTag bTag in octContainer)
  147. {
  148. if (aTag.id == bTag.id)
  149. {
  150. inAAndB.Add(aTag);
  151. inB = true;
  152. break;
  153. }
  154. }
  155. if (!inB) inANotB.Add(aTag);
  156. }
  157. foreach (SpacialHashTag bTag in octContainer)
  158. {
  159. bool inA = false;
  160. foreach (SpacialHashTag aTag in hashContainer)
  161. {
  162. if (aTag.id == bTag.id)
  163. {
  164. inA = true;
  165. break;
  166. }
  167. }
  168. if (!inA) inBNotA.Add(bTag);
  169. }
  170. bool allSame = false;
  171. if (inANotB.Count == 0 && inBNotA.Count == 0) allSame = true;
  172. if (allSame)
  173. {
  174. inAAndB.Clear();
  175. inANotB.Clear();
  176. inBNotA.Clear();
  177. foreach (SpacialHashTag aTag in hashContainer)
  178. {
  179. bool inB = false;
  180. foreach (SpacialHashTag bTag in ioContainer)
  181. {
  182. if (aTag.id == bTag.id)
  183. {
  184. inAAndB.Add(aTag);
  185. inB = true;
  186. break;
  187. }
  188. }
  189. if (!inB) inANotB.Add(aTag);
  190. }
  191. foreach (SpacialHashTag bTag in ioContainer)
  192. {
  193. bool inA = false;
  194. foreach (SpacialHashTag aTag in hashContainer)
  195. {
  196. if (aTag.id == bTag.id)
  197. {
  198. inA = true;
  199. break;
  200. }
  201. }
  202. if (!inA) inBNotA.Add(bTag);
  203. }
  204. }
  205. if (inANotB.Count == 0 && inBNotA.Count == 0) allSame = true;
  206. #endregion
  207. Debug.WriteLine("SH: {0} OT: {1} IO: {2} AllSame: {3}", hashContainer.Count, octContainer.Count, ioContainer.Count, allSame);
  208. /* foreach (var found in hashContainer)
  209. {
  210. Debug.Write("Item {0} - {1} {2} {3}", found.id,
  211. randomItems[found.id].Item2.X,
  212. randomItems[found.id].Item2.Y,
  213. randomItems[found.id].Item2.Z);
  214. if (octContainer.Contains(found))
  215. Debug.Write(" (FOUND BY OCT)");
  216. else
  217. {
  218. octtree.AddItem(randomItems[found.id].Item1, randomItems[found.id].Item2.AsVector3());
  219. }
  220. Debug.WriteLine();
  221. }*/
  222. }
  223. //Debug.ReadLine();
  224. }
  225. }
  226. }