convex_hull.cpp
Go to the documentation of this file.
1 // MIT License
2 
3 // Copyright (c) 2019 Erin Catto
4 
5 // Permission is hereby granted, free of charge, to any person obtaining a copy
6 // of this software and associated documentation files (the "Software"), to deal
7 // in the Software without restriction, including without limitation the rights
8 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 // copies of the Software, and to permit persons to whom the Software is
10 // furnished to do so, subject to the following conditions:
11 
12 // The above copyright notice and this permission notice shall be included in all
13 // copies or substantial portions of the Software.
14 
15 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21 // SOFTWARE.
22 
23 #include "test.h"
24 
25 class ConvexHull : public Test
26 {
27 public:
28  enum
29  {
31  };
32 
34  {
35  Generate();
36  m_auto = false;
37  }
38 
39  void Generate()
40  {
41  b2Vec2 lowerBound(-8.0f, -8.0f);
42  b2Vec2 upperBound(8.0f, 8.0f);
43 
44  for (int32 i = 0; i < e_count; ++i)
45  {
46  float x = 10.0f * RandomFloat();
47  float y = 10.0f * RandomFloat();
48 
49  // Clamp onto a square to help create collinearities.
50  // This will stress the convex hull algorithm.
51  b2Vec2 v(x, y);
52  v = b2Clamp(v, lowerBound, upperBound);
53  m_points[i] = v;
54  }
55 
56  m_count = e_count;
57  }
58 
59  void Keyboard(int key) override
60  {
61  switch (key)
62  {
63  case GLFW_KEY_A:
64  m_auto = !m_auto;
65  break;
66 
67  case GLFW_KEY_G:
68  Generate();
69  break;
70  }
71  }
72 
73  void Step(Settings& settings) override
74  {
75  Test::Step(settings);
76 
77  b2PolygonShape shape;
78  shape.Set(m_points, m_count);
79 
80  g_debugDraw.DrawString(5, m_textLine, "Press g to generate a new random convex hull");
82 
83  g_debugDraw.DrawPolygon(shape.m_vertices, shape.m_count, b2Color(0.9f, 0.9f, 0.9f));
84 
85  for (int32 i = 0; i < m_count; ++i)
86  {
87  g_debugDraw.DrawPoint(m_points[i], 3.0f, b2Color(0.3f, 0.9f, 0.3f));
88  g_debugDraw.DrawString(m_points[i] + b2Vec2(0.05f, 0.05f), "%d", i);
89  }
90 
91  if (shape.Validate() == false)
92  {
93  m_textLine += 0;
94  }
95 
96  if (m_auto)
97  {
98  Generate();
99  }
100  }
101 
102  static Test* Create()
103  {
104  return new ConvexHull;
105  }
106 
109  bool m_auto;
110 };
111 
112 static int testIndex = RegisterTest("Geometry", "Convex Hull", ConvexHull::Create);
ConvexHull::Keyboard
void Keyboard(int key) override
Definition: convex_hull.cpp:59
Test::m_textIncrement
int32 m_textIncrement
Definition: test.h:135
g_debugDraw
DebugDraw g_debugDraw
Definition: draw.cpp:32
b2PolygonShape::m_vertices
b2Vec2 m_vertices[b2_maxPolygonVertices]
Definition: b2_polygon_shape.h:82
testIndex
static int testIndex
Definition: convex_hull.cpp:112
ConvexHull::Step
void Step(Settings &settings) override
Definition: convex_hull.cpp:73
ConvexHull
Definition: convex_hull.cpp:25
GLFW_KEY_A
#define GLFW_KEY_A
Definition: glfw3.h:378
b2PolygonShape::m_count
int32 m_count
Definition: b2_polygon_shape.h:84
b2Vec2
A 2D column vector.
Definition: b2_math.h:41
b2PolygonShape::Set
void Set(const b2Vec2 *points, int32 count)
Definition: b2_polygon_shape.cpp:118
f
f
b2Clamp
T b2Clamp(T a, T low, T high)
Definition: b2_math.h:648
DebugDraw::DrawPolygon
void DrawPolygon(const b2Vec2 *vertices, int32 vertexCount, const b2Color &color) override
Draw a closed polygon provided in CCW order.
Definition: draw.cpp:639
DebugDraw::DrawString
void DrawString(int x, int y, const char *string,...)
Definition: draw.cpp:772
b2Color
Color for debug drawing. Each value has the range [0,1].
Definition: b2_draw.h:30
GLFW_KEY_G
#define GLFW_KEY_G
Definition: glfw3.h:384
RandomFloat
float RandomFloat()
Random number in range [-1,1].
Definition: test.h:37
DebugDraw::DrawPoint
void DrawPoint(const b2Vec2 &p, float size, const b2Color &color) override
Draw a point.
Definition: draw.cpp:766
Settings
Definition: settings.h:25
ConvexHull::ConvexHull
ConvexHull()
Definition: convex_hull.cpp:33
b2PolygonShape
Definition: b2_polygon_shape.h:32
RegisterTest
int RegisterTest(const char *category, const char *name, TestCreateFcn *fcn)
Definition: test.cpp:458
ConvexHull::Create
static Test * Create()
Definition: convex_hull.cpp:102
Test::m_textLine
int32 m_textLine
Definition: test.h:127
ConvexHull::e_count
@ e_count
Definition: convex_hull.cpp:30
int32
signed int int32
Definition: b2_types.h:28
Test
Definition: test.h:80
ConvexHull::m_points
b2Vec2 m_points[b2_maxPolygonVertices]
Definition: convex_hull.cpp:107
Test::Step
virtual void Step(Settings &settings)
Definition: test.cpp:278
ConvexHull::m_count
int32 m_count
Definition: convex_hull.cpp:108
ConvexHull::Generate
void Generate()
Definition: convex_hull.cpp:39
ConvexHull::m_auto
bool m_auto
Definition: convex_hull.cpp:109
b2_maxPolygonVertices
#define b2_maxPolygonVertices
Definition: b2_settings.h:53
b2PolygonShape::Validate
bool Validate() const
Definition: b2_polygon_shape.cpp:433


mvsim
Author(s):
autogenerated on Wed May 28 2025 02:13:07