Advertisement
Guest User

Untitled

a guest
Feb 10th, 2016
57
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.31 KB | None | 0 0
  1. void Mesh::subdivide() {
  2.  
  3.   for (size_t step = 0; step < subdivSteps; step++) {
  4.     vector<Triangle> newTriangles;
  5.     vector<Vector> newVertices;
  6.  
  7.     printf("STEP %d: \n", step + 1);
  8.     /// Iterate every triangle and generate 4 new for each of them
  9.     for (auto &currentT : triangles) {
  10.       printf("START\n");
  11.  
  12.       /// All the neighbours of the current triangle
  13.       vector<Triangle> neighbours = getNeighbours(currentT);
  14.       /// All the direct adjacent vertices of each point from the triangle
  15.       vector<vector<Vector>> adjacents(3);
  16.       /// All of the neighbours with a common side with the current triangle
  17.       Triangle commonSideNeighbours[3];
  18.       /// Collect all the edgepoints in this array
  19.       Vector edgePoints[3];
  20.       /// Collect all the vertexpoints in this array
  21.       Vector vertexPoints[3];
  22.       /// Compute the Vector Points
  23.       computeVertexPoints(neighbours, currentT, commonSideNeighbours,
  24.                           adjacents);
  25.       /// Compute the Edge Points
  26.       computeEdgePoints(currentT, commonSideNeighbours, edgePoints);
  27.       /// Add the new Points
  28.       size_t newVSIZE = newVertices.size();;
  29.       size_t vertexPointsIndices[3];
  30.       size_t edgePointsIndices[3];
  31.       Vector vertexPoint, edgePoint;
  32.       int foundIndex;
  33.       for (size_t j = 0; j < 3; j++) {
  34.         vertexPoint = getVertexPoint(vertices[currentT.v[j]], adjacents[j]);
  35.         foundIndex = memberOf(newVertices, vertexPoint);
  36.         if (foundIndex == -1) {
  37.             newVertices.push_back(vertexPoint);
  38.             vertexPointsIndices[j] = newVSIZE;
  39.             newVSIZE++;
  40.         }
  41.         else {
  42.             vertexPointsIndices[j] = foundIndex;
  43.         }
  44.         // printf("V: %f, %f, %f\n", vertexPoint.x, vertexPoint.y,
  45.         // vertexPoint.z);
  46.       }
  47.       printf("\n");
  48.       for (size_t j = 0; j < 3; j++) {
  49.           foundIndex = memberOf(newVertices, edgePoints[j]);
  50.           if (foundIndex == -1) {
  51.               newVertices.push_back(edgePoints[j]);
  52.               edgePointsIndices[j] = newVSIZE;
  53.               newVSIZE++;;
  54.           }
  55.           else {
  56.               edgePointsIndices[j] = foundIndex;
  57.           }
  58.       }
  59.       addNewTriangles(newVertices, newTriangles, vertexPointsIndices, edgePointsIndices);
  60.  
  61.       printf("END\n\n\n");
  62.     }
  63.     // Change the current mesh's Ts, Vs, uvs and normals with the new one
  64.     triangles = newTriangles;
  65.     vertices = newVertices;
  66.   }
  67. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement