b2VoronoiDiagram.h 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. /*
  2. * Copyright (c) 2013 Google, Inc.
  3. *
  4. * This software is provided 'as-is', without any express or implied
  5. * warranty. In no event will the authors be held liable for any damages
  6. * arising from the use of this software.
  7. * Permission is granted to anyone to use this software for any purpose,
  8. * including commercial applications, and to alter it and redistribute it
  9. * freely, subject to the following restrictions:
  10. * 1. The origin of this software must not be misrepresented; you must not
  11. * claim that you wrote the original software. If you use this software
  12. * in a product, an acknowledgment in the product documentation would be
  13. * appreciated but is not required.
  14. * 2. Altered source versions must be plainly marked as such, and must not be
  15. * misrepresented as being the original software.
  16. * 3. This notice may not be removed or altered from any source distribution.
  17. */
  18. #ifndef B2_VORONOI_DIAGRAM
  19. #define B2_VORONOI_DIAGRAM
  20. #include <Box2D/Common/b2Math.h>
  21. class b2StackAllocator;
  22. struct b2AABB;
  23. /// A field representing the nearest generator from each point.
  24. class b2VoronoiDiagram
  25. {
  26. public:
  27. b2VoronoiDiagram(b2StackAllocator* allocator, int32 generatorCapacity);
  28. ~b2VoronoiDiagram();
  29. /// Add a generator.
  30. /// @param the position of the generator.
  31. /// @param a tag used to identify the generator in callback functions.
  32. /// @param whether to callback for nodes associated with the generator.
  33. void AddGenerator(const b2Vec2& center, int32 tag, bool necessary);
  34. /// Generate the Voronoi diagram. It is rasterized with a given interval
  35. /// in the same range as the necessary generators exist.
  36. /// @param the interval of the diagram.
  37. /// @param margin for which the range of the diagram is extended.
  38. void Generate(float32 radius, float32 margin);
  39. /// Callback used by GetNodes().
  40. class NodeCallback
  41. {
  42. public:
  43. virtual ~NodeCallback() {}
  44. /// Receive tags for generators associated with a node.
  45. virtual void operator()(int32 a, int32 b, int32 c) = 0;
  46. };
  47. /// Enumerate all nodes that contain at least one necessary generator.
  48. /// @param a callback function object called for each node.
  49. void GetNodes(NodeCallback& callback) const;
  50. private:
  51. struct Generator
  52. {
  53. b2Vec2 center;
  54. int32 tag;
  55. bool necessary;
  56. };
  57. struct b2VoronoiDiagramTask
  58. {
  59. int32 m_x, m_y, m_i;
  60. Generator* m_generator;
  61. b2VoronoiDiagramTask() {}
  62. b2VoronoiDiagramTask(int32 x, int32 y, int32 i, Generator* g)
  63. {
  64. m_x = x;
  65. m_y = y;
  66. m_i = i;
  67. m_generator = g;
  68. }
  69. };
  70. b2StackAllocator *m_allocator;
  71. Generator* m_generatorBuffer;
  72. int32 m_generatorCapacity;
  73. int32 m_generatorCount;
  74. int32 m_countX, m_countY;
  75. Generator** m_diagram;
  76. };
  77. #endif