-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgraph.cc
54 lines (46 loc) · 1.06 KB
/
graph.cc
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
#include "graph.h"
Graph::Graph(const std::vector<Vertex>& verts)
{
m_nodes.reserve(verts.size());
for(int i = 0, z = verts.size(); i < z; ++i)
{
const vmath::vec4& vec = verts[i].vertex;
m_nodes.emplace_back(
Node(vmath::vec3(vec[0], vec[1], vec[2]))
);
}
}
void Graph::ConnectMST()
{
//FIXME: A nasty Log(O^2) brute force algorithm
std::vector<Node*> graph;
std::vector<Node*> nodes;
nodes.reserve(m_nodes.size());
graph.reserve(m_nodes.size());
for(int i = 0, z = m_nodes.size(); i < z; ++i)
{
nodes.push_back(&m_nodes[i]);
}
graph.emplace_back(&m_nodes[0]);
Node* pThis = graph[0];
while(nodes.size())
{
auto it = nodes.begin();
std::vector<Node*>::iterator pMin = nodes.end();
float mindist = 0.f;
for(auto z = nodes.end(); it != z; ++it)
{
Node* pNode = *it;
float dist = pThis->DistanceTo(*pNode);
if(pMin == nodes.end() || dist < mindist)
{
mindist = dist;
pMin = it;
}
}
graph.push_back(*pMin);
m_edges.emplace_back(Edge(pThis->GetVert(), (*pMin)->GetVert()));
pThis = *pMin;
nodes.erase(pMin);
}
}