// File: tomfunkhouser[P]gaps/pkgs/R3Shapes/R3Mesh.cpp
// Function: gaps::R3Mesh::CreateCylinder(gaps::R3Cylinder const&, double)
void R3Mesh::
CreateCylinder(const R3Cylinder& cylinder, RNLength vertex_spacing)
{
  // Determine number of vertices per circle
  int n = 32;
  if (vertex_spacing > 0) {
    n = (int) (2.0 * RN_PI * cylinder.Radius() / vertex_spacing);
    if (n < 8) n = 8;
  }

  // Allocate vertex pointers
  R3MeshVertex ***vertices = new R3MeshVertex **[2];
  vertices[0] = new R3MeshVertex * [ n ];
  vertices[1] = new R3MeshVertex * [ n ];

  // Create vertices
  for (int i = 0; i < n; i++) {
    RNAngle a = i*RN_TWO_PI/n;
    RNScalar x = cylinder.Radius()*cos(a);
    RNScalar y = cylinder.Radius()*sin(a);
    vertices[0][i] = CreateVertex(R3Point(x, y, -0.5*cylinder.Height()));
    vertices[1][i] = CreateVertex(R3Point(x, y,  0.5*cylinder.Height()));
  }

  // Create bottom faces
  R3MeshVertex *v0c = CreateVertex(R3Point(0, 0, -0.5*cylinder.Height()));
  for (int i = 0; i < n; i++) {
    R3MeshVertex *v00 = vertices[0][i];
    R3MeshVertex *v01 = vertices[0][(i+1)%n];
    CreateFace(v0c, v01, v00);
  }
  
  // Create top faces
  R3MeshVertex *v1c = CreateVertex(R3Point(0, 0, 0.5*cylinder.Height()));
  for (int i = 0; i < n; i++) {
    R3MeshVertex *v10 = vertices[1][i];
    R3MeshVertex *v11 = vertices[1][(i+1)%n];
    CreateFace(v1c, v10, v11);
  }
  
  // Create side faces
  for (int i = 0; i < n; i++) {
    R3MeshVertex *v00 = vertices[0][i];
    R3MeshVertex *v01 = vertices[0][(i+1)%n];
    R3MeshVertex *v10 = vertices[1][i];
    R3MeshVertex *v11 = vertices[1][(i+1)%n];
    CreateFace(v00, v01, v11);
    CreateFace(v00, v11, v10);
  }

  // Delete vertex pointers
  delete [] vertices[0];
  delete [] vertices[1];
  delete [] vertices;
}

// Assembly:
subq $0x188, %rsp            # imm = 0x188
movq %rdi, 0x180(%rsp)
movq %rsi, 0x178(%rsp)
movsd %xmm0, 0x170(%rsp)
movq 0x180(%rsp), %rax
movq %rax, 0x70(%rsp)
movl $0x20, 0x16c(%rsp)
movsd 0x170(%rsp), %xmm0
xorps %xmm1, %xmm1
ucomisd %xmm1, %xmm0
jbe 0x2b808
movsd 0x107a4e(%rip), %xmm0   # 0x133200
leaq 0x10b11f(%rip), %rax    # 0x1368d8
mulsd (%rax), %xmm0
movsd %xmm0, 0x68(%rsp)
movq 0x178(%rsp), %rdi
callq 0x39fd0
movaps %xmm0, %xmm1
movsd 0x68(%rsp), %xmm0
mulsd %xmm1, %xmm0
divsd 0x170(%rsp), %xmm0
cvttsd2si %xmm0, %eax
movl %eax, 0x16c(%rsp)
cmpl $0x8, 0x16c(%rsp)
jge 0x2b806
movl $0x8, 0x16c(%rsp)
jmp 0x2b808
movl $0x10, %edi
callq 0xf030
movq %rax, 0x160(%rsp)
movslq 0x16c(%rsp), %rax
movl $0x8, %ecx
mulq %rcx
movq %rax, %rdi
seto %cl
movq $-0x1, %rax
testb $0x1, %cl
cmovneq %rax, %rdi
callq 0xf030
movq %rax, %rcx
movq 0x160(%rsp), %rax
movq %rcx, (%rax)
movslq 0x16c(%rsp), %rax
movl $0x8, %ecx
mulq %rcx
movq %rax, %rdi
seto %cl
movq $-0x1, %rax
testb $0x1, %cl
cmovneq %rax, %rdi
callq 0xf030
movq %rax, %rcx
movq 0x160(%rsp), %rax
movq %rcx, 0x8(%rax)
movl $0x0, 0x15c(%rsp)
movl 0x15c(%rsp), %eax
cmpl 0x16c(%rsp), %eax
jge 0x2ba5f
cvtsi2sdl 0x15c(%rsp), %xmm0
leaq 0x10b040(%rip), %rax    # 0x1368f8
mulsd (%rax), %xmm0
cvtsi2sdl 0x16c(%rsp), %xmm1
divsd %xmm1, %xmm0
movsd %xmm0, 0x150(%rsp)
movq 0x178(%rsp), %rdi
callq 0x39fd0
movsd %xmm0, 0x38(%rsp)
movsd 0x150(%rsp), %xmm0
callq 0xf930
movaps %xmm0, %xmm1
movsd 0x38(%rsp), %xmm0
mulsd %xmm1, %xmm0
movsd %xmm0, 0x148(%rsp)
movq 0x178(%rsp), %rdi
callq 0x39fd0
movsd %xmm0, 0x40(%rsp)
movsd 0x150(%rsp), %xmm0
callq 0xf100
movaps %xmm0, %xmm1
movsd 0x40(%rsp), %xmm0
mulsd %xmm1, %xmm0
movsd %xmm0, 0x140(%rsp)
movsd 0x148(%rsp), %xmm0
movsd %xmm0, 0x50(%rsp)
movsd 0x140(%rsp), %xmm0
movsd %xmm0, 0x48(%rsp)
movq 0x178(%rsp), %rdi
callq 0x39ff0
movsd 0x48(%rsp), %xmm1
movaps %xmm0, %xmm3
movsd 0x50(%rsp), %xmm0
movsd 0x1078de(%rip), %xmm2   # 0x133260
mulsd %xmm3, %xmm2
leaq 0x128(%rsp), %rdi
callq 0x6eb50
movq 0x70(%rsp), %rdi
movq (%rdi), %rax
leaq 0x128(%rsp), %rsi
xorl %ecx, %ecx
movl %ecx, %edx
callq *0x20(%rax)
movq %rax, %rdx
movq 0x160(%rsp), %rax
movq (%rax), %rax
movslq 0x15c(%rsp), %rcx
movq %rdx, (%rax,%rcx,8)
movsd 0x148(%rsp), %xmm0
movsd %xmm0, 0x60(%rsp)
movsd 0x140(%rsp), %xmm0
movsd %xmm0, 0x58(%rsp)
movq 0x178(%rsp), %rdi
callq 0x39ff0
movsd 0x58(%rsp), %xmm1
movaps %xmm0, %xmm3
movsd 0x60(%rsp), %xmm0
movsd 0x10760a(%rip), %xmm2   # 0x133010
mulsd %xmm3, %xmm2
leaq 0x110(%rsp), %rdi
callq 0x6eb50
movq 0x70(%rsp), %rdi
movq (%rdi), %rax
leaq 0x110(%rsp), %rsi
xorl %ecx, %ecx
movl %ecx, %edx
callq *0x20(%rax)
movq %rax, %rdx
movq 0x160(%rsp), %rax
movq 0x8(%rax), %rax
movslq 0x15c(%rsp), %rcx
movq %rdx, (%rax,%rcx,8)
movl 0x15c(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x15c(%rsp)
jmp 0x2b894
movq 0x178(%rsp), %rdi
callq 0x39ff0
movsd 0x1077ec(%rip), %xmm2   # 0x133260
mulsd %xmm0, %xmm2
leaq 0xf0(%rsp), %rdi
xorps %xmm1, %xmm1
movaps %xmm1, %xmm0
callq 0x6eb50
movq 0x70(%rsp), %rdi
movq (%rdi), %rax
leaq 0xf0(%rsp), %rsi
xorl %ecx, %ecx
movl %ecx, %edx
callq *0x20(%rax)
movq %rax, 0x108(%rsp)
movl $0x0, 0xec(%rsp)
movl 0xec(%rsp), %eax
cmpl 0x16c(%rsp), %eax
jge 0x2bb5a
movq 0x70(%rsp), %rdi
movq 0x160(%rsp), %rax
movq (%rax), %rax
movslq 0xec(%rsp), %rcx
movq (%rax,%rcx,8), %rax
movq %rax, 0xe0(%rsp)
movq 0x160(%rsp), %rax
movq (%rax), %rax
movq %rax, 0x30(%rsp)
movl 0xec(%rsp), %eax
addl $0x1, %eax
cltd
idivl 0x16c(%rsp)
movq 0x30(%rsp), %rax
movslq %edx, %rcx
movq (%rax,%rcx,8), %rax
movq %rax, 0xd8(%rsp)
movq 0x108(%rsp), %rsi
movq 0xd8(%rsp), %rdx
movq 0xe0(%rsp), %rcx
movq (%rdi), %rax
xorl %r8d, %r8d
callq *0x48(%rax)
movl 0xec(%rsp), %eax
addl $0x1, %eax
movl %eax, 0xec(%rsp)
jmp 0x2bab5
movq 0x178(%rsp), %rdi
callq 0x39ff0
movsd 0x1074a1(%rip), %xmm2   # 0x133010
mulsd %xmm0, %xmm2
leaq 0xb8(%rsp), %rdi
xorps %xmm1, %xmm1
movaps %xmm1, %xmm0
callq 0x6eb50
movq 0x70(%rsp), %rdi
movq (%rdi), %rax
leaq 0xb8(%rsp), %rsi
xorl %ecx, %ecx
movl %ecx, %edx
callq *0x20(%rax)
movq %rax, 0xd0(%rsp)
movl $0x0, 0xb4(%rsp)
movl 0xb4(%rsp), %eax
cmpl 0x16c(%rsp), %eax
jge 0x2bc57
movq 0x70(%rsp), %rdi
movq 0x160(%rsp), %rax
movq 0x8(%rax), %rax
movslq 0xb4(%rsp), %rcx
movq (%rax,%rcx,8), %rax
movq %rax, 0xa8(%rsp)
movq 0x160(%rsp), %rax
movq 0x8(%rax), %rax
movq %rax, 0x28(%rsp)
movl 0xb4(%rsp), %eax
addl $0x1, %eax
cltd
idivl 0x16c(%rsp)
movq 0x28(%rsp), %rax
movslq %edx, %rcx
movq (%rax,%rcx,8), %rax
movq %rax, 0xa0(%rsp)
movq 0xd0(%rsp), %rsi
movq 0xa8(%rsp), %rdx
movq 0xa0(%rsp), %rcx
movq (%rdi), %rax
xorl %r8d, %r8d
callq *0x48(%rax)
movl 0xb4(%rsp), %eax
addl $0x1, %eax
movl %eax, 0xb4(%rsp)
jmp 0x2bbb0
movl $0x0, 0x9c(%rsp)
movl 0x9c(%rsp), %eax
cmpl 0x16c(%rsp), %eax
jge 0x2bd7b
movq 0x70(%rsp), %rdi
movq 0x160(%rsp), %rax
movq (%rax), %rax
movslq 0x9c(%rsp), %rcx
movq (%rax,%rcx,8), %rax
movq %rax, 0x90(%rsp)
movq 0x160(%rsp), %rax
movq (%rax), %rax
movq %rax, 0x18(%rsp)
movl 0x9c(%rsp), %eax
addl $0x1, %eax
cltd
idivl 0x16c(%rsp)
movq 0x18(%rsp), %rax
movslq %edx, %rcx
movq (%rax,%rcx,8), %rax
movq %rax, 0x88(%rsp)
movq 0x160(%rsp), %rax
movq 0x8(%rax), %rax
movslq 0x9c(%rsp), %rcx
movq (%rax,%rcx,8), %rax
movq %rax, 0x80(%rsp)
movq 0x160(%rsp), %rax
movq 0x8(%rax), %rax
movq %rax, 0x20(%rsp)
movl 0x9c(%rsp), %eax
addl $0x1, %eax
cltd
idivl 0x16c(%rsp)
movq 0x20(%rsp), %rax
movslq %edx, %rcx
movq (%rax,%rcx,8), %rax
movq %rax, 0x78(%rsp)
movq 0x90(%rsp), %rsi
movq 0x88(%rsp), %rdx
movq 0x78(%rsp), %rcx
movq (%rdi), %rax
xorl %r8d, %r8d
callq *0x48(%rax)
movq 0x70(%rsp), %rdi
movq 0x90(%rsp), %rsi
movq 0x78(%rsp), %rdx
movq 0x80(%rsp), %rcx
movq (%rdi), %rax
xorl %r8d, %r8d
callq *0x48(%rax)
movl 0x9c(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x9c(%rsp)
jmp 0x2bc62
movq 0x160(%rsp), %rax
movq (%rax), %rax
movq %rax, 0x10(%rsp)
cmpq $0x0, %rax
je 0x2bd9b
movq 0x10(%rsp), %rdi
callq 0xf670
movq 0x160(%rsp), %rax
movq 0x8(%rax), %rax
movq %rax, 0x8(%rsp)
cmpq $0x0, %rax
je 0x2bdbc
movq 0x8(%rsp), %rdi
callq 0xf670
movq 0x160(%rsp), %rax
movq %rax, (%rsp)
cmpq $0x0, %rax
je 0x2bdd7
movq (%rsp), %rdi
callq 0xf670
addq $0x188, %rsp            # imm = 0x188
retq
nop