Commit da78c82d by Philipp Adolf

Move neighbor map generation to triangle.c

parent 272cbb7e
......@@ -100,29 +100,6 @@ void Subdivision::subdivide(Mesh *mesh, int level) {
}
}
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:
ns.uv = neighbor;
break;
case Triangle::Edge::Name::vw:
ns.vw = neighbor;
break;
case Triangle::Edge::Name::wu:
ns.wu = neighbor;
break;
default:
qCWarning(log_subdiv) << "got" << edge_a.name << "as edge!";
break;
}
neighbors.insert(triangle_a, ns);
}
struct Edge {
unsigned int a;
unsigned int b;
......@@ -195,6 +172,8 @@ void Subdivision::precomputeEdgeTable(Subdivision::Tables &tables, QVector<Trian
//Format: first two entries: edge vertices. last two entries: distant vertices.
QMap<Triangle, Triangle::Neighbors> neighbors;
QVector<Triangle> all_triangles = triangles + triangles_regular;
buildNeighborsMap(all_triangles, 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;
......@@ -213,20 +192,6 @@ void Subdivision::precomputeEdgeTable(Subdivision::Tables &tables, QVector<Trian
* - Each edge point will be computed twice. This allows us to implicitly know the adjacent points.
*/
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_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_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
}
}
Triangle::Neighbors ns = neighbors.value(*triangle);
// indices of the three vertices added to the edges of this triangle
......
......@@ -189,6 +189,42 @@ void ibToTriangles(QVector<Vertex> *vb, QVector<unsigned int> &ib, QVector<Trian
}
}
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:
ns.uv = neighbor;
break;
case Triangle::Edge::Name::vw:
ns.vw = neighbor;
break;
case Triangle::Edge::Name::wu:
ns.wu = neighbor;
break;
}
neighbors.insert(triangle_a, ns);
}
void buildNeighborsMap(QVector<Triangle> &triangles, QMap<Triangle, Triangle::Neighbors> &neighbors) {
Triangle::Edge edge_a, edge_b;
for (int i = 0; i < triangles.length(); i++){
Triangle *triangle_a = &triangles[i];
for (int j = i + 1; j < triangles.length(); j++){
Triangle *triangle_b = &triangles[j];
if (triangle_a->get_shared_edge(*triangle_b, edge_a, edge_b)) {
insert_neighbor(neighbors, *triangle_a, edge_a, triangle_b, edge_b);
insert_neighbor(neighbors, *triangle_b, edge_b, triangle_a, edge_a);
}
}
}
}
QDebug operator<<(QDebug d, const Triangle &triangle) {
QDebugStateSaver saver(d);
d.nospace() << "Triangle(" << triangle.u_idx() << ", " << triangle.v_idx() << ", " << triangle.w_idx() << "; " << triangle.u().pos << ", " << triangle.v().pos << ", " << triangle.w().pos << ")";
......
......@@ -69,6 +69,7 @@ class Triangle {
};
void ibToTriangles(QVector<Vertex> *vb, QVector<unsigned int> &ib, QVector<Triangle> &triangles);
void buildNeighborsMap(QVector<Triangle> &triangles, QMap<Triangle, Triangle::Neighbors> &neighbors);
QDebug operator<<(QDebug d, const Triangle &triangle);
......
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