Commit 272cbb7e by Philipp Adolf

Add neighbors struct

parent 49e0acb1
......@@ -100,25 +100,27 @@ void Subdivision::subdivide(Mesh *mesh, int level) {
}
}
void insert_edge(QMap<Triangle, QVector<Triangle::Neighbor>> &opposite_edges, const Triangle &triangle_a, const Triangle::Edge &edge_a, Triangle *triangle_b, const Triangle::Edge &edge_b) {
QVector<Triangle::Neighbor> edges = opposite_edges.value(triangle_a, QVector<Triangle::Neighbor>());
edges.resize(3);
void insert_neighbor(QMap<Triangle, Triangle::Neighbors> &neighbors, const Triangle &triangle_a, const Triangle::Edge &edge_a, Triangle *triangle_b, const Triangle::Edge &edge_b) {
if (!neighbors.contains(triangle_a)) {
neighbors.insert(triangle_a, Triangle::Neighbors());
}
Triangle::Neighbors &ns = neighbors[triangle_a];
Triangle::Neighbor neighbor = { triangle_b, edge_b };
switch (edge_a.name) {
case Triangle::Edge::Name::uv:
edges[0] = neighbor;
ns.uv = neighbor;
break;
case Triangle::Edge::Name::vw:
edges[1] = neighbor;
ns.vw = neighbor;
break;
case Triangle::Edge::Name::wu:
edges[2] = neighbor;
ns.wu = neighbor;
break;
default:
qCWarning(log_subdiv) << "got" << edge_a.name << "as edge!";
break;
}
opposite_edges.insert(triangle_a, edges);
neighbors.insert(triangle_a, ns);
}
struct Edge {
......@@ -192,9 +194,7 @@ void Subdivision::precomputeEdgeTable(Subdivision::Tables &tables, QVector<Trian
//compute edge table
//Format: first two entries: edge vertices. last two entries: distant vertices.
// for each triangle the edges of the neighbor triangles are stored.
// First the edge shared with the triangles uv edge, then vw and wu.
QMap<Triangle, QVector<Triangle::Neighbor>> opposite_edges;
QMap<Triangle, Triangle::Neighbors> neighbors;
// Maps edges to the index of the new vertex added on that edge. The keys are the indices of the two vertices defining the edge and must be in the order uv, vw or uw.
QMap<Edge, unsigned int> edge_indices;
......@@ -216,18 +216,18 @@ void Subdivision::precomputeEdgeTable(Subdivision::Tables &tables, QVector<Trian
Triangle::Edge edge_a, edge_b;
for (int j = i + 1; j < triangles.length(); j++){
if (triangle->get_shared_edge(triangles[j], edge_a, edge_b)) {
insert_edge(opposite_edges, *triangle, edge_a, &triangles[j], edge_b);
insert_edge(opposite_edges, triangles[j], edge_b, triangle, edge_a);
insert_neighbor(neighbors, *triangle, edge_a, &triangles[j], edge_b);
insert_neighbor(neighbors, triangles[j], edge_b, triangle, edge_a);
}
}
for (int j = 0; j < triangles_regular.length(); j++){
if (triangle->get_shared_edge(triangles_regular[j], edge_a, edge_b)) {
insert_edge(opposite_edges, *triangle, edge_a, &triangles_regular[j], edge_b);
insert_neighbor(neighbors, *triangle, edge_a, &triangles_regular[j], edge_b);
// no need to insert the opposite edge of a regular triangle - it won't be processed by the outer loop
}
}
QVector<Triangle::Neighbor> opposite = opposite_edges.value(*triangle);
Triangle::Neighbors ns = neighbors.value(*triangle);
// indices of the three vertices added to the edges of this triangle
unsigned int uv, vw, wu;
......@@ -241,13 +241,13 @@ void Subdivision::precomputeEdgeTable(Subdivision::Tables &tables, QVector<Trian
uv = offset++;
edge_indices.insert(edge, uv);
edge = { opposite[0].edge.a, opposite[0].edge.b };
edge = { ns.uv.edge.a, ns.uv.edge.b };
edge_indices.insert(edge, uv);
tables.edge_indices.push_back(triangle->u_idx());
tables.edge_indices.push_back(triangle->v_idx());
tables.edge_indices.push_back(triangle->w_idx());
tables.edge_indices.push_back(opposite[0].edge.c);
tables.edge_indices.push_back(ns.uv.edge.c);
}
edge = { triangle->v_idx(), triangle->w_idx() };
......@@ -257,13 +257,13 @@ void Subdivision::precomputeEdgeTable(Subdivision::Tables &tables, QVector<Trian
vw = offset++;
edge_indices.insert(edge, vw);
edge = { opposite[1].edge.a, opposite[1].edge.b };
edge = { ns.vw.edge.a, ns.vw.edge.b };
edge_indices.insert(edge, vw);
tables.edge_indices.push_back(triangle->v_idx());
tables.edge_indices.push_back(triangle->w_idx());
tables.edge_indices.push_back(triangle->u_idx());
tables.edge_indices.push_back(opposite[1].edge.c);
tables.edge_indices.push_back(ns.vw.edge.c);
}
edge = { triangle->w_idx(), triangle->u_idx() };
......@@ -273,13 +273,13 @@ void Subdivision::precomputeEdgeTable(Subdivision::Tables &tables, QVector<Trian
wu = offset++;
edge_indices.insert(edge, wu);
edge = { opposite[2].edge.a, opposite[2].edge.b };
edge = { ns.wu.edge.a, ns.wu.edge.b };
edge_indices.insert(edge, wu);
tables.edge_indices.push_back(triangle->w_idx());
tables.edge_indices.push_back(triangle->u_idx());
tables.edge_indices.push_back(triangle->v_idx());
tables.edge_indices.push_back(opposite[2].edge.c);
tables.edge_indices.push_back(ns.wu.edge.c);
}
//add indices to new index buffer
......
......@@ -26,6 +26,12 @@ class Triangle {
Edge edge;
};
struct Neighbors {
Neighbor uv;
Neighbor vw;
Neighbor wu;
};
Triangle();
Triangle(const Triangle &other);
Triangle(const QVector<Vertex> *vertex_buffer, unsigned int u, unsigned int v, unsigned int w);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment