Grid.cpp 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. //
  2. // Created by wrede on 06.06.16.
  3. //
  4. #include "Grid.h"
  5. namespace util
  6. {
  7. Grid::Grid(int width_count, int height_count, double width, double height)
  8. : Grid(width_count, height_count, 1, width, height, 0.0)
  9. {
  10. /* EMPTY */
  11. }
  12. Grid::Grid(int width_count, int height_count, int depth_count,
  13. double width, double height, double depth)
  14. : width_count_(width_count),
  15. height_count_(height_count),
  16. depth_count_(depth_count),
  17. width_(width),
  18. height_(height),
  19. depth_(depth),
  20. cell_width_(width / width_count),
  21. cell_height_(height / height_count),
  22. cell_depth_(depth / depth_count)
  23. {
  24. for (int z = 0; z < depth_count; ++z)
  25. {
  26. values_.push_back(std::vector<std::vector<core::ObjectDataPtr>>());
  27. for (int y = 0; y < height_count; ++y)
  28. {
  29. values_[z].push_back(std::vector<core::ObjectDataPtr>());
  30. for (int x = 0; x < width_count; ++x)
  31. {
  32. values_[z][y].push_back(core::ObjectDataPtr());
  33. }
  34. }
  35. }
  36. }
  37. void Grid::PositionToIndex(double x, double y, double z,
  38. int& xi, int& yi, int& zi) const
  39. {
  40. xi = (int) (x / cell_width_);
  41. yi = (int) (y / cell_height_);
  42. if (depth_count_ > 1)
  43. {
  44. zi = (int) (z / cell_depth_);
  45. }
  46. else
  47. {
  48. zi = 0;
  49. }
  50. }
  51. void Grid::SetValue(core::ObjectDataPtr value, int x, int y, int z)
  52. {
  53. values_[z][y][x] = value;
  54. }
  55. void Grid::SetValue(core::ObjectDataPtr value, double x, double y, double z)
  56. {
  57. int xi, yi, zi;
  58. PositionToIndex(x, y, z, xi, yi, zi);
  59. SetValue(value, xi, yi, zi);
  60. }
  61. core::ObjectDataPtr Grid::GetValue(int x, int y, int z) const
  62. {
  63. return values_[z][y][x];
  64. }
  65. core::ObjectDataPtr Grid::GetValue(double x, double y, double z) const
  66. {
  67. int xi, yi, zi;
  68. PositionToIndex(x, y, z, xi, yi, zi);
  69. return GetValue(xi, yi, zi);
  70. }
  71. int Grid::GetWidthCount() const
  72. {
  73. return width_count_;
  74. }
  75. int Grid::GetHeightCount() const
  76. {
  77. return height_count_;
  78. }
  79. int Grid::GetDepthCount() const
  80. {
  81. return depth_count_;
  82. }
  83. double Grid::GetWidth() const
  84. {
  85. return width_;
  86. }
  87. double Grid::GetHeight() const
  88. {
  89. return height_;
  90. }
  91. double Grid::GetDepth() const
  92. {
  93. return depth_;
  94. }
  95. }