Commit 49e0acb1 by Philipp Adolf

Add neighbor struct

parent cf654955
...@@ -100,24 +100,25 @@ void Subdivision::subdivide(Mesh *mesh, int level) { ...@@ -100,24 +100,25 @@ void Subdivision::subdivide(Mesh *mesh, int level) {
} }
} }
void insert_edge(QMap<Triangle, QVector<Triangle::Edge>> &opposite_edges, const Triangle &triangle, const Triangle::Edge &edge_a, const Triangle::Edge &edge_b) { 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::Edge> edges = opposite_edges.value(triangle, QVector<Triangle::Edge>()); QVector<Triangle::Neighbor> edges = opposite_edges.value(triangle_a, QVector<Triangle::Neighbor>());
edges.resize(3); edges.resize(3);
Triangle::Neighbor neighbor = { triangle_b, edge_b };
switch (edge_a.name) { switch (edge_a.name) {
case Triangle::Edge::Name::uv: case Triangle::Edge::Name::uv:
edges[0] = edge_b; edges[0] = neighbor;
break; break;
case Triangle::Edge::Name::vw: case Triangle::Edge::Name::vw:
edges[1] = edge_b; edges[1] = neighbor;
break; break;
case Triangle::Edge::Name::wu: case Triangle::Edge::Name::wu:
edges[2] = edge_b; edges[2] = neighbor;
break; break;
default: default:
qCWarning(log_subdiv) << "got" << edge_a.name << "as edge!"; qCWarning(log_subdiv) << "got" << edge_a.name << "as edge!";
break; break;
} }
opposite_edges.insert(triangle, edges); opposite_edges.insert(triangle_a, edges);
} }
struct Edge { struct Edge {
...@@ -193,14 +194,14 @@ void Subdivision::precomputeEdgeTable(Subdivision::Tables &tables, QVector<Trian ...@@ -193,14 +194,14 @@ void Subdivision::precomputeEdgeTable(Subdivision::Tables &tables, QVector<Trian
// for each triangle the edges of the neighbor triangles are stored. // for each triangle the edges of the neighbor triangles are stored.
// First the edge shared with the triangles uv edge, then vw and wu. // First the edge shared with the triangles uv edge, then vw and wu.
QMap<Triangle, QVector<Triangle::Edge>> opposite_edges; QMap<Triangle, QVector<Triangle::Neighbor>> opposite_edges;
// 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. // 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; QMap<Edge, unsigned int> edge_indices;
for (int i = 0; i < triangles.length(); i++){ for (int i = 0; i < triangles.length(); i++){
//schaue alle dreiecke an //schaue alle dreiecke an
Triangle triangle = triangles[i]; Triangle *triangle = &triangles[i];
QVector<unsigned int> edge_indices_buffer;//push all edge indices into this, then check if enough edge indices were found. if yes, copy to Tables and add to new index buffer. QVector<unsigned int> edge_indices_buffer;//push all edge indices into this, then check if enough edge indices were found. if yes, copy to Tables and add to new index buffer.
...@@ -214,83 +215,83 @@ void Subdivision::precomputeEdgeTable(Subdivision::Tables &tables, QVector<Trian ...@@ -214,83 +215,83 @@ void Subdivision::precomputeEdgeTable(Subdivision::Tables &tables, QVector<Trian
Triangle::Edge edge_a, edge_b; Triangle::Edge edge_a, edge_b;
for (int j = i + 1; j < triangles.length(); j++){ for (int j = i + 1; j < triangles.length(); j++){
if (triangle.get_shared_edge(triangles[j], edge_a, edge_b)) { if (triangle->get_shared_edge(triangles[j], edge_a, edge_b)) {
insert_edge(opposite_edges, triangle, edge_a, edge_b); insert_edge(opposite_edges, *triangle, edge_a, &triangles[j], edge_b);
insert_edge(opposite_edges, triangles[j], edge_b, edge_a); insert_edge(opposite_edges, triangles[j], edge_b, triangle, edge_a);
} }
} }
for (int j = 0; j < triangles_regular.length(); j++){ for (int j = 0; j < triangles_regular.length(); j++){
if (triangle.get_shared_edge(triangles_regular[j], edge_a, edge_b)) { if (triangle->get_shared_edge(triangles_regular[j], edge_a, edge_b)) {
insert_edge(opposite_edges, triangle, edge_a, edge_b); insert_edge(opposite_edges, *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 // no need to insert the opposite edge of a regular triangle - it won't be processed by the outer loop
} }
} }
QVector<Triangle::Edge> opposite = opposite_edges.value(triangle); QVector<Triangle::Neighbor> opposite = opposite_edges.value(*triangle);
// indices of the three vertices added to the edges of this triangle // indices of the three vertices added to the edges of this triangle
unsigned int uv, vw, wu; unsigned int uv, vw, wu;
Edge edge; Edge edge;
edge = { triangle.u_idx(), triangle.v_idx() }; edge = { triangle->u_idx(), triangle->v_idx() };
if (edge_indices.contains(edge)) { if (edge_indices.contains(edge)) {
uv = edge_indices.value(edge); uv = edge_indices.value(edge);
} else { } else {
uv = offset++; uv = offset++;
edge_indices.insert(edge, uv); edge_indices.insert(edge, uv);
edge = { opposite[0].a, opposite[0].b }; edge = { opposite[0].edge.a, opposite[0].edge.b };
edge_indices.insert(edge, uv); edge_indices.insert(edge, uv);
tables.edge_indices.push_back(triangle.u_idx()); tables.edge_indices.push_back(triangle->u_idx());
tables.edge_indices.push_back(triangle.v_idx()); tables.edge_indices.push_back(triangle->v_idx());
tables.edge_indices.push_back(triangle.w_idx()); tables.edge_indices.push_back(triangle->w_idx());
tables.edge_indices.push_back(opposite[0].c); tables.edge_indices.push_back(opposite[0].edge.c);
} }
edge = { triangle.v_idx(), triangle.w_idx() }; edge = { triangle->v_idx(), triangle->w_idx() };
if (edge_indices.contains(edge)) { if (edge_indices.contains(edge)) {
vw = edge_indices.value(edge); vw = edge_indices.value(edge);
} else { } else {
vw = offset++; vw = offset++;
edge_indices.insert(edge, vw); edge_indices.insert(edge, vw);
edge = { opposite[1].a, opposite[1].b }; edge = { opposite[1].edge.a, opposite[1].edge.b };
edge_indices.insert(edge, vw); edge_indices.insert(edge, vw);
tables.edge_indices.push_back(triangle.v_idx()); tables.edge_indices.push_back(triangle->v_idx());
tables.edge_indices.push_back(triangle.w_idx()); tables.edge_indices.push_back(triangle->w_idx());
tables.edge_indices.push_back(triangle.u_idx()); tables.edge_indices.push_back(triangle->u_idx());
tables.edge_indices.push_back(opposite[1].c); tables.edge_indices.push_back(opposite[1].edge.c);
} }
edge = { triangle.w_idx(), triangle.u_idx() }; edge = { triangle->w_idx(), triangle->u_idx() };
if (edge_indices.contains(edge)) { if (edge_indices.contains(edge)) {
wu = edge_indices.value(edge); wu = edge_indices.value(edge);
} else { } else {
wu = offset++; wu = offset++;
edge_indices.insert(edge, wu); edge_indices.insert(edge, wu);
edge = { opposite[2].a, opposite[2].b }; edge = { opposite[2].edge.a, opposite[2].edge.b };
edge_indices.insert(edge, wu); edge_indices.insert(edge, wu);
tables.edge_indices.push_back(triangle.w_idx()); tables.edge_indices.push_back(triangle->w_idx());
tables.edge_indices.push_back(triangle.u_idx()); tables.edge_indices.push_back(triangle->u_idx());
tables.edge_indices.push_back(triangle.v_idx()); tables.edge_indices.push_back(triangle->v_idx());
tables.edge_indices.push_back(opposite[2].c); tables.edge_indices.push_back(opposite[2].edge.c);
} }
//add indices to new index buffer //add indices to new index buffer
tables.index_buffer.push_back(triangle.u_idx()); tables.index_buffer.push_back(triangle->u_idx());
tables.index_buffer.push_back(uv); tables.index_buffer.push_back(uv);
tables.index_buffer.push_back(wu); tables.index_buffer.push_back(wu);
tables.index_buffer.push_back(triangle.v_idx()); tables.index_buffer.push_back(triangle->v_idx());
tables.index_buffer.push_back(vw); tables.index_buffer.push_back(vw);
tables.index_buffer.push_back(uv); tables.index_buffer.push_back(uv);
tables.index_buffer.push_back(triangle.w_idx()); tables.index_buffer.push_back(triangle->w_idx());
tables.index_buffer.push_back(wu); tables.index_buffer.push_back(wu);
tables.index_buffer.push_back(vw); tables.index_buffer.push_back(vw);
......
...@@ -21,6 +21,11 @@ class Triangle { ...@@ -21,6 +21,11 @@ class Triangle {
unsigned int c; unsigned int c;
}; };
struct Neighbor {
Triangle *triangle;
Edge edge;
};
Triangle(); Triangle();
Triangle(const Triangle &other); Triangle(const Triangle &other);
Triangle(const QVector<Vertex> *vertex_buffer, unsigned int u, unsigned int v, unsigned int w); 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