SparseVoxelTree.cs 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using DwarfCorp;
  7. namespace ABTest
  8. {
  9. public class SparseVoxelTree
  10. {
  11. public IntegerBoundingBox Bounds;
  12. public SparseVoxelTree[] Children;
  13. private Point3 Mid;
  14. private int Voxel;
  15. private int[,,] RawBuffer;
  16. public SparseVoxelTree(Point3 Min, Point3 Max, int Voxel)
  17. {
  18. Bounds = new IntegerBoundingBox(Min, Max);
  19. Bounds.Max.X = Bounds.Min.X + NextPowerOfTwo(Bounds.Width);
  20. Bounds.Max.Y = Bounds.Min.Y + NextPowerOfTwo(Bounds.Height);
  21. Bounds.Max.Z = Bounds.Min.Z + NextPowerOfTwo(Bounds.Depth);
  22. Mid = new Point3(Min.X + Bounds.Width / 2, Min.Y + Bounds.Height / 2, Min.Z + Bounds.Depth / 2);
  23. this.Voxel = Voxel;
  24. if (Bounds.Width == 2 && Bounds.Height == 2 && Bounds.Depth == 2)
  25. {
  26. RawBuffer = new int[2, 2, 2];
  27. for (var x = 0; x < 2; ++x)
  28. for (var y = 0; y < 2; ++y)
  29. for (var z = 0; z < 2; ++z)
  30. RawBuffer[x, y, z] = Voxel;
  31. }
  32. }
  33. private int NextPowerOfTwo(int N)
  34. {
  35. var r = 1;
  36. while (r < N)
  37. r <<= 1;
  38. return r;
  39. }
  40. private void Subdivide()
  41. {
  42. var Min = Bounds.Min;
  43. var Max = Bounds.Max;
  44. if (Bounds.Height == 64)
  45. {
  46. Children = new SparseVoxelTree[4]
  47. {
  48. new SparseVoxelTree(Min, new Point3(Max.X, Min.Y + 16, Max.Z), Voxel),
  49. new SparseVoxelTree(new Point3(Min.X, Min.Y + 16, Min.Z), new Point3(Max.X, Min.Y + 32, Max.Z), Voxel),
  50. new SparseVoxelTree(new Point3(Min.X, Min.Y + 32, Min.Z), new Point3(Max.X, Min.Y + 48, Max.Z), Voxel),
  51. new SparseVoxelTree(new Point3(Min.X, Min.Y + 48, Min.Z), new Point3(Max.X, Min.Y + 64, Max.Z), Voxel)
  52. };
  53. }
  54. else
  55. {
  56. Children = new SparseVoxelTree[8]
  57. {
  58. /*000*/ new SparseVoxelTree(new Point3(Min.X, Min.Y, Min.Z), new Point3(Mid.X, Mid.Y, Mid.Z), Voxel),
  59. /*001*/ new SparseVoxelTree(new Point3(Mid.X, Min.Y, Min.Z), new Point3(Max.X, Mid.Y, Mid.Z), Voxel),
  60. /*010*/ new SparseVoxelTree(new Point3(Min.X, Mid.Y, Min.Z), new Point3(Mid.X, Max.Y, Mid.Z), Voxel),
  61. /*011*/ new SparseVoxelTree(new Point3(Mid.X, Mid.Y, Min.Z), new Point3(Max.X, Max.Y, Mid.Z), Voxel),
  62. /*100*/ new SparseVoxelTree(new Point3(Min.X, Min.Y, Mid.Z), new Point3(Mid.X, Mid.Y, Max.Z), Voxel),
  63. /*101*/ new SparseVoxelTree(new Point3(Mid.X, Min.Y, Mid.Z), new Point3(Max.X, Mid.Y, Max.Z), Voxel),
  64. /*110*/ new SparseVoxelTree(new Point3(Min.X, Mid.Y, Mid.Z), new Point3(Mid.X, Max.Y, Max.Z), Voxel),
  65. /*111*/ new SparseVoxelTree(new Point3(Mid.X, Mid.Y, Mid.Z), new Point3(Max.X, Max.Y, Max.Z), Voxel)
  66. };
  67. }
  68. }
  69. public int GetVoxel(Point3 Coordinate)
  70. {
  71. if (RawBuffer != null)
  72. return RawBuffer[Coordinate.X - Bounds.Min.X, Coordinate.Y - Bounds.Min.Y, Coordinate.Z - Bounds.Min.Z];
  73. if (Children == null)
  74. return Voxel;
  75. foreach (var c in Children)
  76. if (c.Bounds.Contains(Coordinate))
  77. return c.GetVoxel(Coordinate);
  78. return 0;
  79. }
  80. public void SetVoxel(Point3 Coordinate, int Voxel)
  81. {
  82. if (RawBuffer != null)
  83. RawBuffer[Coordinate.X - Bounds.Min.X, Coordinate.Y - Bounds.Min.Y, Coordinate.Z - Bounds.Min.Z] = Voxel;
  84. else
  85. {
  86. if (Children == null)
  87. {
  88. if (this.Voxel != Voxel)
  89. Subdivide();
  90. else
  91. return;
  92. }
  93. foreach (var c in Children)
  94. if (c.Bounds.Contains(Coordinate))
  95. c.SetVoxel(Coordinate, Voxel);
  96. }
  97. }
  98. public Tuple<int, int> CalculateMemoryUsage()
  99. {
  100. var r = 24 + 12 + (4 * 5);
  101. var voxels = 1;
  102. if (RawBuffer != null)
  103. {
  104. r += 4;
  105. voxels += 8;
  106. }
  107. if (Children != null)
  108. {
  109. r += (4 * 8);
  110. foreach (var c in Children)
  111. {
  112. var sum = c.CalculateMemoryUsage();
  113. r += sum.Item1;
  114. voxels += sum.Item2;
  115. }
  116. }
  117. return Tuple.Create(r, voxels);
  118. }
  119. }
  120. }