-
Notifications
You must be signed in to change notification settings - Fork 0
/
ConvexHull.cpp
231 lines (171 loc) · 5.11 KB
/
ConvexHull.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
/*
Let There Be Light
Copyright (C) 2012 Eric Laukien
This software is provided 'as-is', without any express or implied
warranty. In no event will the authors be held liable for any damages
arising from the use of this software.
Permission is granted to anyone to use this software for any purpose,
including commercial applications, and to alter it and redistribute it
freely, subject to the following restrictions:
1. The origin of this software must not be misrepresented; you must not
claim that you wrote the original software. If you use this software
in a product, an acknowledgment in the product documentation would be
appreciated but is not required.
2. Altered source versions must be plainly marked as such, and must not be
misrepresented as being the original software.
3. This notice may not be removed or altered from any source distribution.
*/
#include "ConvexHull.h"
#include "Utils.h"
#include <assert.h>
#include <iostream>
#include <fstream>
#include <sstream>
namespace ltbl
{
ConvexHull::ConvexHull()
: m_worldCenter(0.0f, 0.0f),
m_aabbCalculated(false),
m_updateRequired(true), // Remains true permanently unless user purposely changes it
m_transparency(1.0f),
m_renderLightOverHull(true)
{
}
void ConvexHull::CenterHull()
{
// Calculate the average of all of the vertices, and then
// offset all of them to make this the new origin position (0,0)
const unsigned int numVertices = m_vertices.size();
Vec2f posSum(0.0f, 0.0f);
for(unsigned int i = 0; i < numVertices; i++)
posSum += m_vertices[i];
Vec2f averagePos(posSum / static_cast<float>(numVertices));
for(unsigned int i = 0; i < numVertices; i++)
m_vertices[i] -= averagePos;
}
bool ConvexHull::LoadShape(const char* fileName)
{
std::ifstream load(fileName);
if(!load)
{
load.close();
std::cout << "Could not load convex hull \"" << fileName << "\"!" << std::endl;
return false;
}
else
{
while(!load.eof())
{
std::string firstElement, secondElement;
load >> firstElement >> secondElement;
if(firstElement.size() == 0 || secondElement.size() == 0)
break;
m_vertices.push_back(Vec2f(GetFloatVal(firstElement), GetFloatVal(secondElement)));
}
load.close();
}
CenterHull();
CalculateNormals();
return true;
}
Vec2f ConvexHull::GetWorldVertex(unsigned int index) const
{
assert(index >= 0 && index < m_vertices.size());
return Vec2f(m_vertices[index].x + m_worldCenter.x, m_vertices[index].y + m_worldCenter.y);
}
void ConvexHull::CalculateNormals()
{
const unsigned int numVertices = m_vertices.size();
if(m_normals.size() != numVertices)
m_normals.resize(numVertices);
for(unsigned int i = 0; i < numVertices; i++) // Dots are wrong
{
unsigned int index2 = i + 1;
// Wrap
if(index2 >= numVertices)
index2 = 0;
m_normals[i].x = -(m_vertices[index2].y - m_vertices[i].y);
m_normals[i].y = m_vertices[index2].x - m_vertices[i].x;
}
}
void ConvexHull::RenderHull(float depth)
{
if(m_renderLightOverHull)
return;
glBegin(GL_TRIANGLE_FAN);
const unsigned int numVertices = m_vertices.size();
for(unsigned int i = 0; i < numVertices; i++)
{
Vec2f vPos(GetWorldVertex(i));
glVertex3f(vPos.x, vPos.y, depth);
}
glEnd();
}
void ConvexHull::CalculateAABB()
{
assert(m_vertices.size() > 0);
m_aabb.m_lowerBound = m_vertices[0];
m_aabb.m_upperBound = m_aabb.m_lowerBound;
for(unsigned int i = 0, size = m_vertices.size(); i < size; i++)
{
Vec2f* pPos = &m_vertices[i];
if(pPos->x > m_aabb.m_upperBound.x)
m_aabb.m_upperBound.x = pPos->x;
if(pPos->y > m_aabb.m_upperBound.y)
m_aabb.m_upperBound.y = pPos->y;
if(pPos->x < m_aabb.m_lowerBound.x)
m_aabb.m_lowerBound.x = pPos->x;
if(pPos->y < m_aabb.m_lowerBound.y)
m_aabb.m_lowerBound.y = pPos->y;
}
m_aabb.CalculateHalfDims();
m_aabb.CalculateCenter();
m_aabbCalculated = true;
}
bool ConvexHull::HasCalculatedAABB() const
{
return m_aabbCalculated;
}
void ConvexHull::SetWorldCenter(const Vec2f &newCenter)
{
m_worldCenter = newCenter;
m_aabb.SetCenter(m_worldCenter);
TreeUpdate();
}
void ConvexHull::IncWorldCenter(const Vec2f &increment)
{
m_worldCenter += increment;
m_aabb.IncCenter(increment);
TreeUpdate();
}
Vec2f ConvexHull::GetWorldCenter() const
{
return m_worldCenter;
}
bool ConvexHull::PointInsideHull(const Vec2f &point)
{
int sgn = 0;
for(unsigned int i = 0, numVertices = m_vertices.size(); i < numVertices; i++)
{
int wrappedIndex = Wrap(i + 1, numVertices);
Vec2f currentVertex(GetWorldVertex(i));
Vec2f side(GetWorldVertex(wrappedIndex) - currentVertex);
Vec2f toPoint(point - currentVertex);
float cpd = side.Cross(toPoint);
int cpdi = static_cast<int>(cpd / abs(cpd));
if(sgn == 0)
sgn = cpdi;
else if(cpdi != sgn)
return false;
}
return true;
}
void ConvexHull::DebugDraw()
{
const unsigned int numVertices = m_vertices.size();
glTranslatef(m_worldCenter.x, m_worldCenter.y, 0.0f);
glBegin(GL_LINE_LOOP);
for(unsigned int i = 0; i < numVertices; i++)
glVertex2f(m_vertices[i].x, m_vertices[i].y);
}
}