Commit d029fb25 by Philipp Adolf

Rewrite findRegular to use neighbors map

parent 36f9cdb3
......@@ -89,18 +89,29 @@ void Subdivision::subdivide(Mesh *mesh, int level) {
Tables tables = precomputeTables(input);
Result result = runShader(input, tables);
QVector<unsigned int> regular;
QVector<unsigned int> irregular;
findRegular(tables.index_buffer, result.vertex_buffer, regular, irregular);
QVector<Triangle> all_triangles;
ibToTriangles(&result.vertex_buffer, tables.index_buffer, all_triangles);
QMap<Triangle, Triangle::Neighbors> neighbors;
buildNeighborsMap(result.vertex_buffer, all_triangles, neighbors);
QVector<Triangle> regular;
QVector<Triangle> irregular;
findRegular(all_triangles, neighbors, regular, irregular);
qCDebug(log_subdiv) << "Indices" << tables.index_buffer.length();
qCDebug(log_subdiv) << "subdivide: regular" << regular.length();
qCDebug(log_subdiv) << "subdivide: irregular" << irregular.length();
QVector<unsigned int> patches = getPatchIndexBuffer(regular, irregular,result.vertex_buffer);//todo is this the correct vb?
QVector<unsigned int> regular_ib;
trianglesToIB(regular, regular_ib);
QVector<unsigned int> irregular_ib;
trianglesToIB(irregular, irregular_ib);
QVector<unsigned int> patches = getPatchIndexBuffer(regular_ib, irregular_ib, result.vertex_buffer);
qCDebug(log_subdiv) << "patches" << patches.length();
current_mesh->update(result.vb_handle, result.vertex_buffer, irregular, patches);
current_mesh->update(result.vb_handle, result.vertex_buffer, irregular_ib, patches);
}
}
......@@ -545,15 +556,43 @@ void Subdivision::splitRegular(Mesh *mesh) {
Mesh::MeshEntry *current_mesh = mesh->getMeshEntry(first_mesh_index);
QVector<unsigned int> regular;
QVector<unsigned int> irregular;
QVector<Triangle> all_triangles;
qDebug() << current_mesh->buffers[0]->indices_irregular.size() << current_mesh->buffers[0]->indices_regular.size();
ibToTriangles(&current_mesh->buffers[0]->vertices, current_mesh->buffers[0]->indices_irregular, all_triangles);
QVector<unsigned int> old_regular_ib = patchIBToTriangleIB(current_mesh->buffers[0]->indices_regular);
ibToTriangles(&current_mesh->buffers[0]->vertices, old_regular_ib, all_triangles);
qDebug() << all_triangles.size();
findRegular(current_mesh->buffers[0]->indices_irregular, current_mesh->buffers[0]->vertices, regular, irregular);
findRegular(current_mesh->buffers[0]->indices_regular, current_mesh->buffers[0]->vertices, regular, irregular);
QMap<Triangle, Triangle::Neighbors> neighbors;
buildNeighborsMap(current_mesh->buffers[0]->vertices, all_triangles, neighbors);
QVectorIterator<Triangle> i(all_triangles);
while (i.hasNext()) {
Triangle triangle = i.next();
Triangle::Neighbors ns = neighbors.value(triangle);
if (ns.uv.triangle == NULL) {
qDebug() << qSetRealNumberPrecision(10) << "uv" << triangle;
}
if (ns.vw.triangle == NULL) {
qDebug() << qSetRealNumberPrecision(10) << "vw" << triangle;
}
if (ns.wu.triangle == NULL) {
qDebug() << "wu" << triangle;
}
}
QVector<unsigned int> patches = getPatchIndexBuffer(regular, irregular,current_mesh->buffers[0]->vertices);//TODO is this the corret vertex buffer?
QVector<Triangle> regular;
QVector<Triangle> irregular;
findRegular(all_triangles, neighbors, regular, irregular);
QVector<unsigned int> regular_ib;
trianglesToIB(regular, regular_ib);
QVector<unsigned int> irregular_ib;
trianglesToIB(irregular, irregular_ib);
QVector<unsigned int> patches = getPatchIndexBuffer(regular_ib, irregular_ib, current_mesh->buffers[0]->vertices);
current_mesh->buffers[0]->indices_regular = patches;
current_mesh->buffers[0]->indices_irregular = irregular;
current_mesh->buffers[0]->indices_irregular = irregular_ib;
current_mesh->buffers[0]->updateIndices();
......@@ -759,50 +798,44 @@ QVector<unsigned int> Subdivision::patchIBToTriangleIB(QVector<unsigned int> ib)
return new_ib;
}
/**
* Generates index buffers containing all regular and all irregular triangles in the input index buffer.
*
* @param index_buffer Index buffer describing triangles.
* @param vertex_buffer Vertices used by index_buffer.
* @param regular All regular triangles are appended to this index buffer.
* @param irregular All irregular triangles are appended to this index buffer.
*/
void Subdivision::findRegular(QVector<unsigned int> index_buffer, QVector<Vertex> vertex_buffer, QVector<unsigned int> &regular, QVector<unsigned int> &irregular) {
QTime totalTimer;
totalTimer.start();
qCDebug(log_timing)<<"Find Regular started";
for (int i = 0; i < index_buffer.length()-2; i += 3){
Vertex vx = vertex_buffer[index_buffer[i]];
Vertex vy = vertex_buffer[index_buffer[i+1]];
Vertex vz = vertex_buffer[index_buffer[i+2]];
int countx = 0;
int county = 0;
int countz = 0;
for (int j = 0; j < index_buffer.length(); j++){
if (j != i && j != i+1 && j != i+2){
if (vx.samePos(vertex_buffer[index_buffer[j]])){
countx++;
}
if (vy.samePos(vertex_buffer[index_buffer[j]])){
county++;
}
if (vz.samePos(vertex_buffer[index_buffer[j]])){
countz++;
}
}
bool isVertexRegular(Triangle &triangle, Triangle::Neighbor current_neighbor, QMap<Triangle, Triangle::Neighbors> neighbors) {
unsigned int count = 1;
while (count < 6 && *current_neighbor.triangle != triangle) {
count++;
current_neighbor = neighbors.value(*current_neighbor.triangle).get_neighbor(rotate_edge_name(current_neighbor.edge.name));
}
return count == 6 && *current_neighbor.triangle == triangle;
}
void Subdivision::findRegular(QVector<Triangle> triangles, QMap<Triangle, Triangle::Neighbors> neighbors, QVector<Triangle> &regular, QVector<Triangle> &irregular) {
QTime timer;
timer.start();
qCDebug(log_timing) << "findRegular started";
QVectorIterator<Triangle> it(triangles);
while (it.hasNext()) {
Triangle triangle = it.next();
Triangle::Neighbors ns = neighbors.value(triangle);
bool is_regular = true;
if (!isVertexRegular(triangle, ns.uv, neighbors)) {
is_regular = false;
}
if (is_regular && !isVertexRegular(triangle, ns.vw, neighbors)) {
is_regular = false;
}
if (countx == 5 && county == 5 && countz == 5){
regular.push_back(index_buffer[i]);
regular.push_back(index_buffer[i+1]);
regular.push_back(index_buffer[i+2]);
if (is_regular && !isVertexRegular(triangle, ns.wu, neighbors)) {
is_regular = false;
}
if (is_regular) {
regular.push_back(triangle);
} else {
irregular.push_back(index_buffer[i]);
irregular.push_back(index_buffer[i+1]);
irregular.push_back(index_buffer[i+2]);
irregular.push_back(triangle);
}
}
qCDebug(log_timing)<<"Find Regular done:"<<formatTimeMeasurement(totalTimer.elapsed());
qCDebug(log_timing) << "findRegular done:" << formatTimeMeasurement(timer.elapsed());
}
Subdivision::Result Subdivision::runShader(Input input, Tables &tables) {
......
......@@ -62,7 +62,7 @@ private:
void precomputeEdgeTable(Subdivision::Tables &tables, QVector<Triangle> &triangles, QMap<Triangle, Triangle::Neighbors> &neighbors, unsigned int offset);
void precomputeVertexTable(Subdivision::Tables &tables, Input &input, QMap<unsigned int, unsigned int> &modified_vertices);
void updateIndexBuffer(QVector<unsigned int> &index_buffer, QMap<unsigned int, unsigned int> map);
void findRegular(QVector<unsigned int> index_buffer, QVector<Vertex> vertex_buffer, QVector<unsigned int> &regular, QVector<unsigned int> &irregular);
void findRegular(QVector<Triangle> triangles, QMap<Triangle, Triangle::Neighbors> neighbors, QVector<Triangle> &regular, QVector<Triangle> &irregular);
Result runShader(Input input, Tables &tables);
void runCopyShader(GLuint size, GLuint vb_in, GLuint vb_out);
void runVertexShader(GLuint size, GLuint vb_handle, GLuint vertex_indices_handle, GLuint vertex_offsets_handle, GLuint output_handle, GLuint offset_handle);
......
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