LiquidFun
 All Classes Files Functions Variables Enumerations Enumerator Macros Pages
b2VoronoiDiagram.h
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 
21 #include <Box2D/Common/b2Math.h>
22 
23 class b2StackAllocator;
24 struct b2AABB;
25 
28 {
29 
30 public:
31 
32  b2VoronoiDiagram(b2StackAllocator* allocator, int32 generatorCapacity);
34 
35  void AddGenerator(const b2Vec2& center, int32 tag);
36 
37  void Generate(float32 radius);
38 
39  template<class Callback>
40  void GetNodes(const Callback& callback) const
41  {
42  for (int32 y = 0; y < m_countY - 1; y++)
43  {
44  for (int32 x = 0; x < m_countX - 1; x++)
45  {
46  int32 i = x + y * m_countX;
47  const Generator* a = m_diagram[i];
48  const Generator* b = m_diagram[i + 1];
49  const Generator* c = m_diagram[i + m_countX];
50  const Generator* d = m_diagram[i + 1 + m_countX];
51  if (b != c)
52  {
53  if (a != b && a != c)
54  {
55  callback(a->tag, b->tag, c->tag);
56  }
57  if (d != b && d != c)
58  {
59  callback(b->tag, d->tag, c->tag);
60  }
61  }
62  }
63  }
64  }
65 
66 private:
67 
68  struct Generator
69  {
70  b2Vec2 center;
71  int32 tag;
72  };
73 
74  struct b2VoronoiDiagramTask
75  {
76  int32 m_x, m_y, m_i;
77  Generator* m_generator;
78 
79  b2VoronoiDiagramTask() {}
80  b2VoronoiDiagramTask(int32 x, int32 y, int32 i, Generator* g)
81  {
82  m_x = x;
83  m_y = y;
84  m_i = i;
85  m_generator = g;
86  }
87  };
88 
89  b2StackAllocator *m_allocator;
90  Generator* m_generatorBuffer;
91  int32 m_generatorCount;
92  int32 m_countX, m_countY;
93  Generator** m_diagram;
94 
95 };
96 
97 #endif
Definition: b2StackAllocator.h:37
A field representing the nearest generator from each point.
Definition: b2VoronoiDiagram.h:27
An axis aligned bounding box.
Definition: b2Collision.h:161
A 2D column vector.
Definition: b2Math.h:64