Beispiel #1
0
static void applySubdivs(Mesh &mesh, int subdivLevel) {
  for (int i = 0; i < subdivLevel; i++) {
    // subdivide faces
    for (int j = 0, n = mesh.getNumFaces(); j < n; j++) {
      Mesh::Face face = mesh.getFace(j);
      int verticesAroundFace = face.getNumVertices();
      Cvec3 vertexSum = Cvec3();
      for (int k = 0; k < verticesAroundFace; k++) {
        vertexSum += face.getVertex(k).getPosition();
      }
      vertexSum = vertexSum * (1.0 / verticesAroundFace); 
      mesh.setNewFaceVertex(face, vertexSum);
    }

    // subdivide edges
    for (int j = 0, n = mesh.getNumEdges(); j < n; j++) {
      Mesh::Edge edge = mesh.getEdge(j);
      Cvec3 vertexSum = (edge.getVertex(0).getPosition() +
        edge.getVertex(1).getPosition() +
        mesh.getNewFaceVertex(edge.getFace(0)) +
        mesh.getNewFaceVertex(edge.getFace(1))) * 0.25;
      mesh.setNewEdgeVertex(edge, vertexSum); 
    }

    // subdivide vertices
    for (int j = 0, n = mesh.getNumVertices(); j < n; j++) {
      Mesh::Vertex v = mesh.getVertex(j);
      int numOfVertices = 0;
      
      Mesh::VertexIterator vertexIter(v.getIterator()), iterOrigin(vertexIter);
      Cvec3 accumVertices = Cvec3();
      Cvec3 accumFaceVertices = Cvec3();

      do {
        accumVertices += vertexIter.getVertex().getPosition();
        accumFaceVertices += mesh.getNewFaceVertex(vertexIter.getFace());
        
        numOfVertices++;
      } while (++vertexIter != iterOrigin);
      
      double factor = 1.0 / (numOfVertices * numOfVertices);

      Cvec3 vertexVertex = 
        v.getPosition() * ((numOfVertices - 2.0) / numOfVertices) +
        accumVertices * factor +
        accumFaceVertices * factor; 
      mesh.setNewVertexVertex(mesh.getVertex(j), vertexVertex);
    }
    
    // subdivide for each level of subdivision we need 
    mesh.subdivide();
  }
}
Beispiel #2
0
void collectEdgeVertices(Mesh& m) {
  for (int i = 0; i < m.getNumEdges(); ++i) {
    Mesh::Edge e = m.getEdge(i);

    // get faces adjacent to edges
    Cvec3 f0 = m.getNewFaceVertex(e.getFace(0));
    Cvec3 f1 = m.getNewFaceVertex(e.getFace(1));

    Cvec3 pos0 = e.getVertex(0).getPosition();
    Cvec3 pos1 = e.getVertex(1).getPosition();

    vector<Cvec3> vertices;
    vertices.push_back(f0);
    vertices.push_back(f1);
    vertices.push_back(pos0);
    vertices.push_back(pos1);

    Cvec3 newEdge = getEdgeVertex(vertices);
    m.setNewEdgeVertex(e, newEdge);
  }
}