// File: /workspace/github_cpp_makefile_star2_O2/tomfunkhouser[P]gaps/pkgs/R3Shapes/R3Mesh.cpp
// Function: gaps::R3Mesh::SubdivideFaces()
void R3Mesh::
SubdivideFaces(void)
{
  // Remember cardinalities
  int nfaces = NFaces();
  int nedges = NEdges();
  int nvertices = NVertices();

  // Allocate temporary memory for vertex indices
  int *vertex_indices = new int [ nfaces * 4 * 3 ];

  // Compute vertex indices for new faces
  for (int i = 0; i < nfaces; i++) {
    R3MeshFace *face = Face(i);
    R3MeshVertex *v0 = VertexOnFace(face, 0);
    R3MeshVertex *v1 = VertexOnFace(face, 1);
    R3MeshVertex *v2 = VertexOnFace(face, 2);
    R3MeshEdge *e0 = EdgeOnFace(face, 0);
    R3MeshEdge *e1 = EdgeOnFace(face, 1);
    R3MeshEdge *e2 = EdgeOnFace(face, 2);
    vertex_indices[i * 12 + 0] = VertexID(v0);
    vertex_indices[i * 12 + 1] = nvertices + EdgeID(e0);
    vertex_indices[i * 12 + 2] = nvertices + EdgeID(e2);
    vertex_indices[i * 12 + 3] = VertexID(v1);
    vertex_indices[i * 12 + 4] = nvertices + EdgeID(e1);
    vertex_indices[i * 12 + 5] = nvertices + EdgeID(e0);
    vertex_indices[i * 12 + 6] = VertexID(v2);
    vertex_indices[i * 12 + 7] = nvertices + EdgeID(e2);
    vertex_indices[i * 12 + 8] = nvertices + EdgeID(e1);
    vertex_indices[i * 12 + 9] = nvertices + EdgeID(e0);
    vertex_indices[i * 12 + 10] = nvertices + EdgeID(e1);
    vertex_indices[i * 12 + 11] = nvertices + EdgeID(e2);
  }

  // Create vertex at midpoint of every edge
  for (int i = 0; i < nedges; i++) {
    R3MeshEdge *edge = Edge(i);
    R3MeshVertex *v0 = VertexOnEdge(edge, 0);
    R3MeshVertex *v1 = VertexOnEdge(edge, 1);
    R3MeshVertex *vertex = CreateVertex(EdgeMidpoint(edge));
    SetVertexTextureCoords(vertex, 0.5*VertexTextureCoords(v0) + 0.5*VertexTextureCoords(v1)); 
    SetVertexColor(vertex, 0.5*VertexColor(v0) + 0.5*VertexColor(v1)); 
  }

  // Delete all edges and faces
  while (NFaces() > 0) DeleteFace(Face(0));
  while (NEdges() > 0) DeleteEdge(Edge(0));

  // Create new edges and faces
  for (int i = 0; i < nfaces; i++) {
    for (int j = 0; j < 4; j++) {
      R3MeshVertex *v0 = Vertex(vertex_indices[i*12 + j*3 + 0]);
      R3MeshVertex *v1 = Vertex(vertex_indices[i*12 + j*3 + 1]);
      R3MeshVertex *v2 = Vertex(vertex_indices[i*12 + j*3 + 2]);
      CreateFace(v0, v1, v2);
    }
  }

  // Delete temporary memory for vertex indices
  delete [] vertex_indices;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x68, %rsp
movq %rdi, %rbx
movslq 0x34(%rdi), %r15
imulq $0x30, %r15, %rax
testq %r15, %r15
movl 0x14(%rdi), %ebp
movl 0x24(%rdi), %r12d
pushq $-0x1
popq %rdi
cmovnsq %rax, %rdi
callq 0xe030
movq 0x28(%rbx), %r14
xorl %ecx, %ecx
testl %r15d, %r15d
movl $0x0, %edx
cmovgl %r15d, %edx
movq %rdx, 0x10(%rsp)
imulq $0x6, %rdx, %rdx
cmpq %rcx, %rdx
je 0x15f8d
movq (%r14), %rsi
movq 0x8(%rsi), %rdi
movq 0x10(%rsi), %r8
movq 0x18(%rsi), %r9
movq 0x20(%rsi), %r10
movq 0x28(%rsi), %r11
movq 0x30(%rsi), %rsi
movl 0x70(%rdi), %edi
movl %edi, (%rax,%rcx,8)
movl 0x30(%r10), %edi
addl %ebp, %edi
movl %edi, 0x4(%rax,%rcx,8)
movl 0x30(%rsi), %esi
addl %ebp, %esi
movl %esi, 0x8(%rax,%rcx,8)
movl 0x70(%r8), %r8d
movl %r8d, 0xc(%rax,%rcx,8)
movl 0x30(%r11), %r8d
addl %ebp, %r8d
movl %r8d, 0x10(%rax,%rcx,8)
movl %edi, 0x14(%rax,%rcx,8)
movl 0x70(%r9), %r9d
movl %r9d, 0x18(%rax,%rcx,8)
movl %esi, 0x1c(%rax,%rcx,8)
movl %r8d, 0x20(%rax,%rcx,8)
movl %edi, 0x24(%rax,%rcx,8)
movl %r8d, 0x28(%rax,%rcx,8)
movl %esi, 0x2c(%rax,%rcx,8)
addq $0x6, %rcx
addq $0x8, %r14
jmp 0x15f12
movq %rax, (%rsp)
xorl %ecx, %ecx
testl %r12d, %r12d
cmovlel %ecx, %r12d
movq %r12, 0x8(%rsp)
cmpq %rcx, 0x8(%rsp)
je 0x1608e
movq 0x18(%rbx), %rax
movq (%rax,%rcx,8), %rdx
movq 0x8(%rdx), %r13
movq 0x10(%rdx), %rbp
leaq 0x38(%rsp), %r15
movq %r15, %rdi
movq %rbx, %rsi
movq %rcx, 0x18(%rsp)
callq 0x2149c
movq (%rbx), %rax
movq %rbx, %rdi
movq %r15, %rsi
xorl %edx, %edx
callq *0x20(%rax)
movq %rax, %r12
leaq 0x48(%r13), %rsi
leaq 0x50(%rsp), %r14
movq %r14, %rdi
movsd 0x7d01c(%rip), %xmm0    # 0x93010
callq 0x5f833
leaq 0x48(%rbp), %rsi
leaq 0x20(%rsp), %rdi
movsd 0x7d006(%rip), %xmm0    # 0x93010
callq 0x5f833
leaq 0x38(%rsp), %r15
movq %r15, %rdi
movq %r14, %rsi
leaq 0x20(%rsp), %rdx
callq 0x5f7a1
leaq 0x48(%r12), %rdi
movq %r15, %rsi
callq 0x5f6c4
addq $0x58, %r13
movq %r14, %rdi
movq %r13, %rsi
movsd 0x7cfcd(%rip), %xmm0    # 0x93010
callq 0x223f6
addq $0x58, %rbp
leaq 0x20(%rsp), %r13
movq %r13, %rdi
movq %rbp, %rsi
movsd 0x7cfb1(%rip), %xmm0    # 0x93010
callq 0x223f6
movq %r15, %rdi
movq %r14, %rsi
movq %r13, %rdx
callq 0x213f3
addq $0x58, %r12
movq %r12, %rdi
movq %r15, %rsi
callq 0x66212
movq 0x18(%rsp), %rcx
incq %rcx
jmp 0x15f9f
movq (%rsp), %r14
cmpl $0x0, 0x34(%rbx)
jle 0x160ba
movq (%rbx), %rax
movq 0x28(%rbx), %rcx
movq (%rcx), %rsi
movq %rbx, %rdi
callq *0x70(%rax)
jmp 0x16092
movq (%rbx), %rax
movq 0x18(%rbx), %rcx
movq (%rcx), %rsi
movq %rbx, %rdi
callq *0x68(%rax)
cmpl $0x0, 0x24(%rbx)
jg 0x160aa
xorl %r15d, %r15d
pushq $0x8
popq %r12
movq %r14, %r13
cmpq 0x10(%rsp), %r15
je 0x16115
movq %r12, %rbp
cmpq $0x38, %rbp
je 0x1610c
movslq -0x8(%r13,%rbp), %rax
movq (%rbx), %r9
movq 0x8(%rbx), %rcx
movq (%rcx,%rax,8), %rsi
movslq -0x4(%r13,%rbp), %rax
movq (%rcx,%rax,8), %rdx
movslq (%r13,%rbp), %rax
movq (%rcx,%rax,8), %rcx
movq %rbx, %rdi
xorl %r8d, %r8d
callq *0x48(%r9)
addq $0xc, %rbp
jmp 0x160d4
incq %r15
addq $0x30, %r13
jmp 0x160ca
movq %r14, %rdi
callq 0xe5d0
addq $0x68, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq