|
| 1 | +#include "Surface.h" |
| 2 | + |
| 3 | + |
| 4 | +////////// helper ////////// |
| 5 | + |
| 6 | +// computer quadric matrix by summing all K_p matrices of a vertice v0 |
| 7 | +glm::mat4 Surface::ComputeQuadric(VertexRecord v0) |
| 8 | +{ |
| 9 | + glm::mat4 quadric{ 0.0f }; |
| 10 | + // for each neighbouring face, compute K_p |
| 11 | + glm::vec3 position = v0.position; |
| 12 | + for (unsigned int faceIdx : v0.adjFacesIdx) |
| 13 | + { |
| 14 | + FaceRecord face = m_Faces[faceIdx]; |
| 15 | + glm::vec3 faceNormal = ComputeFaceNormal(face); |
| 16 | + glm::vec4 plane{ faceNormal, -glm::dot(faceNormal, position) }; // plane equation ax+by+cz+d = 0 |
| 17 | + |
| 18 | + quadric += glm::outerProduct(plane, plane); // K_p |
| 19 | + } |
| 20 | + |
| 21 | + return quadric; |
| 22 | +} |
| 23 | + |
| 24 | +Object Surface::GHOutputOBJ() |
| 25 | +{ |
| 26 | + // build new Object class |
| 27 | + std::vector<glm::vec3> VertexPos; |
| 28 | + std::unordered_map<float, std::unordered_map<float, std::unordered_map<float, unsigned int>>> VertLookup; |
| 29 | + std::vector<std::vector<unsigned int>> FaceIndices; |
| 30 | + std::unordered_map<unsigned int, unsigned int> NumberPolygons; |
| 31 | + |
| 32 | + for (VertexRecord vert : m_Vertices) |
| 33 | + { |
| 34 | + glm::vec3 vertPos = vert.position; |
| 35 | + getVertIndex(vertPos, VertexPos, VertLookup); |
| 36 | + } |
| 37 | + |
| 38 | + for (FaceRecord face : m_Faces) |
| 39 | + { |
| 40 | + std::vector<unsigned int> vertsIdx; |
| 41 | + for (unsigned int i = 0; i < 3; i++) |
| 42 | + { |
| 43 | + glm::vec3 vert = m_Vertices[face.verticesIdx[i]].position; |
| 44 | + vertsIdx.push_back(getVertIndex(vert, VertexPos, VertLookup)); |
| 45 | + } |
| 46 | + FaceIndices.push_back(vertsIdx); |
| 47 | + NumberPolygons[3] += 1; |
| 48 | + } |
| 49 | + |
| 50 | + // build object |
| 51 | + Object Obj; |
| 52 | + Obj.m_Min = m_Min; Obj.m_Max = m_Max; |
| 53 | + Obj.m_VertexPos = VertexPos; Obj.m_FaceIndices = FaceIndices; |
| 54 | + Obj.m_NumPolygons = NumberPolygons; |
| 55 | + Obj.TriangulateFaces(); |
| 56 | + |
| 57 | + return Obj; |
| 58 | +} |
| 59 | + |
| 60 | + |
| 61 | +////////// algorithms ////////// |
| 62 | + |
| 63 | +// Garland Heckbert simplification surface algorithm |
| 64 | +Object Surface::QEM(unsigned int desiredCount) |
| 65 | +{ |
| 66 | + unsigned int numVertices = static_cast<unsigned int>(m_Vertices.size()); |
| 67 | + // calculate quadric error for each vertex |
| 68 | + std::unordered_map<unsigned int, glm::mat4> quadricLookup; |
| 69 | + for (unsigned int i = 0; i < numVertices; i++) |
| 70 | + { |
| 71 | + quadricLookup.insert({ i, ComputeQuadric(m_Vertices[i]) }); |
| 72 | + } |
| 73 | + |
| 74 | + const float THRESHOLD = 0.05f; |
| 75 | + |
| 76 | + // select all valid pairs |
| 77 | + std::vector<std::set<unsigned int>> vertexPairLookup; // maintain vertex pairs |
| 78 | + vertexPairLookup.resize(numVertices); |
| 79 | + |
| 80 | + std::vector<ValidPair> validPairs; |
| 81 | + for (unsigned int firstV = 0; firstV < numVertices; firstV++) |
| 82 | + { |
| 83 | + for (unsigned int secondV = firstV + 1; secondV < numVertices; secondV++) |
| 84 | + { |
| 85 | + auto searchx = m_EdgeIdxLookup.find(firstV); |
| 86 | + if (searchx != m_EdgeIdxLookup.end()) |
| 87 | + { |
| 88 | + // search if ending vertex in our lookup |
| 89 | + auto searchy = searchx->second.find(secondV); |
| 90 | + if (searchy != searchx->second.end()) |
| 91 | + { |
| 92 | + // add the pair idx to the vertices |
| 93 | + vertexPairLookup[firstV].insert(static_cast<unsigned int>(validPairs.size())); |
| 94 | + vertexPairLookup[secondV].insert(static_cast<unsigned int>(validPairs.size())); |
| 95 | + // found edge, add to valid pairs |
| 96 | + ValidPair newPair{}; newPair.vertOne = firstV; newPair.vertTwo = secondV; newPair.edge = true; |
| 97 | + validPairs.push_back(newPair); |
| 98 | + } |
| 99 | + } |
| 100 | + // not found, check if the edges are close (distance smaller than threshold) |
| 101 | + glm::vec3 firstPos = m_Vertices[firstV].position; |
| 102 | + glm::vec3 secondPos = m_Vertices[secondV].position; |
| 103 | + if (glm::distance(firstPos, secondPos) < THRESHOLD) |
| 104 | + { |
| 105 | + // add the pair idx to the vertices |
| 106 | + vertexPairLookup[firstV].insert(static_cast<unsigned int>(validPairs.size())); |
| 107 | + vertexPairLookup[secondV].insert(static_cast<unsigned int>(validPairs.size())); |
| 108 | + // add to valid pairs |
| 109 | + ValidPair newPair{}; newPair.vertOne = firstV; newPair.vertTwo = secondV; newPair.edge = false; |
| 110 | + validPairs.push_back(newPair); |
| 111 | + } |
| 112 | + // else, do nothing, not a valid pair |
| 113 | + } |
| 114 | + } |
| 115 | + |
| 116 | + // compute the new point and error associated for each valid pair |
| 117 | + |
| 118 | + // each pair should contain a few pieces of information |
| 119 | + // vertex 1, vertex 2, the new vertex position, the error after contraction, the quadric matrices for both 1 and 2, and the new vertex after contraction |
| 120 | + for (ValidPair validPair : validPairs) |
| 121 | + { |
| 122 | + glm::mat4 firstQuad = quadricLookup[validPair.vertOne]; |
| 123 | + glm::mat4 secondQuad = quadricLookup[validPair.vertTwo]; |
| 124 | + glm::mat4 Quad = firstQuad + secondQuad; |
| 125 | + |
| 126 | + glm::mat4 MatQ = { |
| 127 | + Quad[1][1], Quad[1][2], Quad[1][3], Quad[1][4], |
| 128 | + Quad[1][2], Quad[2][2], Quad[2][3], Quad[2][4], |
| 129 | + Quad[1][3], Quad[2][3], Quad[3][3], Quad[3][4], |
| 130 | + 0, 0, 0, 1 |
| 131 | + }; |
| 132 | + if (glm::determinant(MatQ) != 0) |
| 133 | + { |
| 134 | + glm::vec4 newVPos = glm::inverse(MatQ) * glm::vec4{ 0, 0, 0, 1 }; |
| 135 | + |
| 136 | + // 1x4 vector * 4x4 matrix * 4x1 vector yields a 1x1 matrix |
| 137 | + validPair.error = (newVPos * Quad * newVPos)[0]; |
| 138 | + |
| 139 | + // change back to 3d coords from homogeneous coordinates |
| 140 | + validPair.newVert = glm::vec3(newVPos) / newVPos.w; |
| 141 | + } |
| 142 | + else |
| 143 | + { |
| 144 | + glm::vec4 end1 = { m_Vertices[validPair.vertOne].position, 1.0f }; |
| 145 | + // 1x4 vector * 4x4 matrix * 4x1 vector yields a 1x1 matrix |
| 146 | + float end1Error = (end1 * Quad * end1)[0]; |
| 147 | + |
| 148 | + glm::vec4 end2 = { m_Vertices[validPair.vertTwo].position, 1.0f }; |
| 149 | + // 1x4 vector * 4x4 matrix * 4x1 vector yields a 1x1 matrix |
| 150 | + float end2Error = (end2 * Quad * end2)[0]; |
| 151 | + |
| 152 | + glm::vec4 mid = (end1 + end2) / 2.0f; |
| 153 | + // 1x4 vector * 4x4 matrix * 4x1 vector yields a 1x1 matrix |
| 154 | + float midError = (mid * Quad * mid)[0]; |
| 155 | + |
| 156 | + float minError = std::min({ end1Error, end2Error, midError }); |
| 157 | + validPair.error = minError; |
| 158 | + if (minError == end1Error) |
| 159 | + { |
| 160 | + validPair.newVert = end1; |
| 161 | + } |
| 162 | + if (minError == end2Error) |
| 163 | + { |
| 164 | + validPair.newVert = end2; |
| 165 | + } |
| 166 | + if (minError == midError) |
| 167 | + { |
| 168 | + validPair.newVert = mid; |
| 169 | + } |
| 170 | + } |
| 171 | + } |
| 172 | + |
| 173 | + // create a min-heap to store all the valid pairs, ordered by error cost |
| 174 | + m_QuadricErrorHeap = std::priority_queue<ValidPair, std::vector<ValidPair>, CompareValidPairs>(validPairs.begin(), validPairs.end()); |
| 175 | + |
| 176 | + // iteratively remove the validpair with the lowest cost, until numFaces == desiredCount |
| 177 | + unsigned int numFaces = static_cast<unsigned int>(m_Faces.size()); |
| 178 | + while (numFaces > desiredCount) |
| 179 | + { |
| 180 | + ValidPair leastCost = m_QuadricErrorHeap.top(); |
| 181 | + m_QuadricErrorHeap.pop(); |
| 182 | + |
| 183 | + // contract the current pair |
| 184 | + // TODO: need to replace the pair with the new vertex, change all neighbours to use the new vertex |
| 185 | + // change m_Vertices[leastCost.vertOne] = newV? |
| 186 | + m_Vertices[leastCost.vertOne].position = leastCost.newVert; |
| 187 | + m_Vertices[leastCost.vertTwo].position = leastCost.newVert; |
| 188 | + |
| 189 | + // if there are faces that use this current edge, remove it |
| 190 | + if (leastCost.edge) |
| 191 | + { |
| 192 | + // get edge index |
| 193 | + unsigned int edgeIdx = getEdgeIndex({ leastCost.vertOne, leastCost.vertTwo }); |
| 194 | + |
| 195 | + for (auto it = m_Faces.begin(); it != m_Faces.end();) |
| 196 | + { |
| 197 | + FaceRecord face = *it; |
| 198 | + if (std::find(face.edgesIdx.begin(), face.edgesIdx.end(), edgeIdx) != face.edgesIdx.end()) |
| 199 | + it = m_Faces.erase(it); // Remove the element and update iterator |
| 200 | + else |
| 201 | + it++; |
| 202 | + } |
| 203 | + numFaces = static_cast<unsigned int>(m_Faces.size()); |
| 204 | + } |
| 205 | + |
| 206 | + |
| 207 | + // update the cost of all valid pairs involving the current pair |
| 208 | + //for (ValidPair validPair : validPairs) |
| 209 | + //{ |
| 210 | + // if ((validPair.vertOne == leastCost.vertOne) || (validPair.vertOne == leastCost.vertTwo)) |
| 211 | + // // update cost |
| 212 | + // else if ((validPair.vertTwo == leastCost.vertOne) || (validPair.vertTwo == leastCost.vertTwo)) |
| 213 | + // // update cost |
| 214 | + |
| 215 | + //} |
| 216 | + } |
| 217 | + |
| 218 | + return GHOutputOBJ(); |
| 219 | +} |
0 commit comments