// File: /workspace/github_cpp_makefile_star2_O1/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), %r14
movq %r14, %rax
shlq $0x4, %rax
testq %r14, %r14
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 %r14, %r14
jle 0x173d3
movq 0x28(%rbx), %rax
movq 0x8(%rsp), %rcx
addq $0x2c, %rcx
xorl %edx, %edx
movq (%rax,%rdx,8), %rsi
movq 0x8(%rsi), %r9
movq 0x10(%rsi), %r10
movq 0x18(%rsi), %r11
movq 0x20(%rsi), %r8
movq 0x28(%rsi), %rdi
movq 0x30(%rsi), %rsi
movl 0x70(%r9), %r9d
movl %r9d, -0x2c(%rcx)
movl 0x30(%r8), %r9d
addl %ebp, %r9d
movl %r9d, -0x28(%rcx)
movl 0x30(%rsi), %r9d
addl %ebp, %r9d
movl %r9d, -0x24(%rcx)
movl 0x70(%r10), %r9d
movl %r9d, -0x20(%rcx)
movl 0x30(%rdi), %r9d
addl %ebp, %r9d
movl %r9d, -0x1c(%rcx)
movl 0x30(%r8), %r9d
addl %ebp, %r9d
movl %r9d, -0x18(%rcx)
movl 0x70(%r11), %r9d
movl %r9d, -0x14(%rcx)
movl 0x30(%rsi), %r9d
addl %ebp, %r9d
movl %r9d, -0x10(%rcx)
movl 0x30(%rdi), %r9d
addl %ebp, %r9d
movl %r9d, -0xc(%rcx)
movl 0x30(%r8), %r8d
addl %ebp, %r8d
movl %r8d, -0x8(%rcx)
movl 0x30(%rdi), %edi
addl %ebp, %edi
movl %edi, -0x4(%rcx)
movl 0x30(%rsi), %esi
addl %ebp, %esi
movl %esi, (%rcx)
incq %rdx
addq $0x30, %rcx
cmpq %rdx, %r14
jne 0x17333
movq %r14, 0x10(%rsp)
testl %r12d, %r12d
jle 0x17552
xorl %r14d, %r14d
movq %r12, 0x18(%rsp)
movq 0x18(%rbx), %rax
movq (%rax,%r14,8), %rax
movq 0x8(%rax), %r12
movq 0x10(%rax), %r13
movq 0x8(%rax), %rsi
movq 0x10(%rax), %rdx
addq $0x18, %rsi
addq $0x18, %rdx
leaq 0x50(%rsp), %rbp
movq %rbp, %rdi
callq 0x3d42a
leaq 0x38(%rsp), %r15
movq %r15, %rdi
movq %rbp, %rsi
movsd 0x8ac07(%rip), %xmm0    # 0xa2030
callq 0x3d470
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 0x8abe1(%rip), %xmm0    # 0xa2030
callq 0x690d5
leaq 0x48(%r13), %rsi
leaq 0x20(%rsp), %rdi
movsd 0x8abcb(%rip), %xmm0    # 0xa2030
callq 0x690d5
leaq 0x50(%rsp), %r15
movq %r15, %rdi
leaq 0x38(%rsp), %rsi
leaq 0x20(%rsp), %rdx
callq 0x69043
leaq 0x48(%rbp), %rdi
movq %r15, %rsi
callq 0x68f66
movsd 0x58(%r12), %xmm0
movsd 0x8ab94(%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 0x70460
movsd 0x58(%r13), %xmm0
movsd 0x8ab5d(%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 0x70460
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 0x70460
addq $0x58, %rbp
movq %rbp, %rdi
movq %r15, %rsi
callq 0x704f2
incq %r14
cmpq %r14, %r12
jne 0x173e9
jmp 0x17552
movq (%rbx), %rax
movq 0x28(%rbx), %rcx
movq (%rcx), %rsi
movq %rbx, %rdi
callq *0x70(%rax)
cmpl $0x0, 0x34(%rbx)
jg 0x17542
cmpl $0x0, 0x24(%rbx)
movq 0x10(%rsp), %r13
jle 0x17579
movq (%rbx), %rax
movq 0x18(%rbx), %rcx
movq (%rcx), %rsi
movq %rbx, %rdi
callq *0x68(%rax)
cmpl $0x0, 0x24(%rbx)
jg 0x17563
testl %r13d, %r13d
jle 0x175cd
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 0x1758c
incq %r14
addq $0x30, %r15
cmpq %r13, %r14
jne 0x17586
movq 0x8(%rsp), %rdi
callq 0xe5b0
addq $0x68, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq