// File: /workspace/github_cpp_makefile_star2_O3/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), %r13
movq %r13, %rax
shlq $0x4, %rax
testq %r13, %r13
movl 0x14(%rdi), %ebp
movl 0x24(%rdi), %r12d
leaq (%rax,%rax,2), %rax
movq $-0x1, %rdi
cmovnsq %rax, %rdi
callq 0xe030
movq %rax, 0x8(%rsp)
testq %r13, %r13
jle 0x17312
movq 0x28(%rbx), %rax
movq 0x8(%rsp), %rcx
addq $0x2c, %rcx
xorl %edx, %edx
movq (%rax,%rdx,8), %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, -0x2c(%rcx)
movl 0x30(%r10), %edi
addl %ebp, %edi
movl %edi, -0x28(%rcx)
movl 0x30(%rsi), %esi
addl %ebp, %esi
movl %esi, -0x24(%rcx)
movl 0x70(%r8), %r8d
movl %r8d, -0x20(%rcx)
movl 0x30(%r11), %r8d
addl %ebp, %r8d
movl %r8d, -0x1c(%rcx)
movl %edi, -0x18(%rcx)
movl 0x70(%r9), %r9d
movl %r9d, -0x14(%rcx)
movl %esi, -0x10(%rcx)
movl %r8d, -0xc(%rcx)
movl %edi, -0x8(%rcx)
movl %r8d, -0x4(%rcx)
movl %esi, (%rcx)
incq %rdx
addq $0x30, %rcx
cmpq %rdx, %r13
jne 0x172a5
movl %r13d, %eax
testl %r12d, %r12d
jle 0x17483
movq %r13, 0x10(%rsp)
xorl %r14d, %r14d
movq %r12, 0x18(%rsp)
movq 0x18(%rbx), %rax
movq (%rax,%r14,8), %rax
movq 0x8(%rax), %r12
movq 0x10(%rax), %r13
leaq 0x18(%r12), %rsi
leaq 0x18(%r13), %rdx
leaq 0x50(%rsp), %rbp
movq %rbp, %rdi
callq 0x3cf30
leaq 0x38(%rsp), %r15
movq %r15, %rdi
movq %rbp, %rsi
movsd 0x8accc(%rip), %xmm0    # 0xa2030
callq 0x3cf76
movq (%rbx), %rax
movq %rbx, %rdi
movq %r15, %rsi
xorl %edx, %edx
callq *0x20(%rax)
movq %rax, %rbp
leaq 0x48(%r12), %rsi
movq %r15, %rdi
movsd 0x8aca6(%rip), %xmm0    # 0xa2030
callq 0x6834d
leaq 0x48(%r13), %rsi
leaq 0x20(%rsp), %rdi
movsd 0x8ac90(%rip), %xmm0    # 0xa2030
callq 0x6834d
leaq 0x50(%rsp), %r15
movq %r15, %rdi
leaq 0x38(%rsp), %rsi
leaq 0x20(%rsp), %rdx
callq 0x682bb
leaq 0x48(%rbp), %rdi
movq %r15, %rsi
callq 0x681de
movsd 0x58(%r12), %xmm0
movsd 0x8ac59(%rip), %xmm3    # 0xa2030
mulsd %xmm3, %xmm0
movsd 0x60(%r12), %xmm1
mulsd %xmm3, %xmm1
movsd 0x68(%r12), %xmm2
movq 0x18(%rsp), %r12
mulsd %xmm3, %xmm2
leaq 0x38(%rsp), %rdi
callq 0x6f680
movsd 0x58(%r13), %xmm0
movsd 0x8ac22(%rip), %xmm2    # 0xa2030
mulsd %xmm2, %xmm0
movsd 0x60(%r13), %xmm1
mulsd %xmm2, %xmm1
movapd %xmm2, %xmm3
movsd 0x68(%r13), %xmm2
mulsd %xmm3, %xmm2
leaq 0x20(%rsp), %rdi
callq 0x6f680
movsd 0x38(%rsp), %xmm0
movsd 0x40(%rsp), %xmm1
addsd 0x20(%rsp), %xmm0
addsd 0x28(%rsp), %xmm1
movsd 0x48(%rsp), %xmm2
addsd 0x30(%rsp), %xmm2
movq %r15, %rdi
callq 0x6f680
addq $0x58, %rbp
movq %rbp, %rdi
movq %r15, %rsi
callq 0x6f712
incq %r14
cmpq %r14, %r12
jne 0x1732b
movl 0x34(%rbx), %eax
movq 0x10(%rsp), %r13
testl %eax, %eax
jle 0x174af
movq (%rbx), %rax
movq 0x28(%rbx), %rcx
movq (%rcx), %rsi
movq %rbx, %rdi
callq *0x70(%rax)
cmpl $0x0, 0x34(%rbx)
jg 0x17487
jmp 0x174af
movq (%rbx), %rax
movq 0x18(%rbx), %rcx
movq (%rcx), %rsi
movq %rbx, %rdi
callq *0x68(%rax)
cmpl $0x0, 0x24(%rbx)
jg 0x1749f
testl %r13d, %r13d
jle 0x17509
xorl %r14d, %r14d
movq 0x8(%rsp), %r15
movl $0x8, %r12d
movslq -0x8(%r15,%r12), %rax
movq (%rbx), %r9
movq 0x8(%rbx), %rcx
movq (%rcx,%rax,8), %rsi
movslq -0x4(%r15,%r12), %rax
movq (%rcx,%rax,8), %rdx
movslq (%r15,%r12), %rax
movq (%rcx,%rax,8), %rcx
movq %rbx, %rdi
xorl %r8d, %r8d
callq *0x48(%r9)
addq $0xc, %r12
cmpq $0x38, %r12
jne 0x174c8
incq %r14
addq $0x30, %r15
cmpq %r13, %r14
jne 0x174c2
movq 0x8(%rsp), %rdi
callq 0xe5b0
addq $0x68, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq