// File: tomfunkhouser[P]gaps/pkgs/R3Shapes/R3Mesh.cpp
// Function: gaps::R3Mesh::CreateSphere(gaps::R3Sphere const&, double)
void R3Mesh::
CreateSphere(const R3Sphere& sphere, RNLength vertex_spacing)
{
  // Create mesh elements for a sphere and add to mesh
  R3Point c = sphere.Centroid();
  RNLength r = sphere.Radius();
  if (vertex_spacing == 0) vertex_spacing = r/5.0;
  if (RNIsZero(vertex_spacing)) return;
  
  // Start with an icosahedron
  R3Mesh tmp;
  tmp.CreateIcosahedron(R3unit_sphere);

  // Iteratively refine until reach vertex spacing
  while (tmp.AverageEdgeLength() > vertex_spacing) {
    // Subdivide
    tmp.SubdivideFaces();

    // Push vertices to unit sphere
    for (int i = 0; i < tmp.NVertices(); i++) {
      R3MeshVertex *v = tmp.Vertex(i);
      R3Point p = tmp.VertexPosition(v);
      RNLength d = p.Vector().Length();
      if (RNIsPositive(d)) p /= d;
      tmp.SetVertexPosition(v, p);
    }
  }

  // Map vertices from unit sphere to specified sphere
  for (int i = 0; i < tmp.NVertices(); i++) {
    R3MeshVertex *v = tmp.Vertex(i);
    R3Point p = tmp.VertexPosition(v);
    RNLength d = p.Vector().Length();
    if (RNIsPositive(d)) p = c+(r/d)*p;
    tmp.SetVertexPosition(v, p);
  }

  // Copy into this mesh
  CreateCopy(tmp);
}

// Assembly:
subq $0x2a8, %rsp            # imm = 0x2A8
movq %rdi, 0x2a0(%rsp)
movq %rsi, 0x298(%rsp)
movsd %xmm0, 0x290(%rsp)
movq 0x2a0(%rsp), %rax
movq %rax, 0x68(%rsp)
movq 0x298(%rsp), %rsi
movq (%rsi), %rax
leaq 0x278(%rsp), %rdi
callq *0x68(%rax)
movq 0x298(%rsp), %rdi
callq 0x39f50
movsd %xmm0, 0x270(%rsp)
movsd 0x290(%rsp), %xmm0
xorps %xmm1, %xmm1
ucomisd %xmm1, %xmm0
jne 0x2a7ab
jp 0x2a7ab
movsd 0x270(%rsp), %xmm0
movsd 0x108aba(%rip), %xmm1   # 0x133258
divsd %xmm1, %xmm0
movsd %xmm0, 0x290(%rsp)
movsd 0x290(%rsp), %xmm0
leaq 0x14dd25(%rip), %rax    # 0x1784e0
movsd (%rax), %xmm1
callq 0x36b80
cmpl $0x0, %eax
je 0x2a7ce
jmp 0x2ab49
leaq 0x138(%rsp), %rdi
movq %rdi, 0x60(%rsp)
callq 0x13e70
movq 0x60(%rsp), %rdi
leaq 0x14def4(%rip), %rsi    # 0x1786e0
callq 0x29fb0
jmp 0x2a7f3
jmp 0x2a7f5
leaq 0x138(%rsp), %rdi
callq 0x15470
movsd %xmm0, 0x58(%rsp)
jmp 0x2a80a
movsd 0x58(%rsp), %xmm0
ucomisd 0x290(%rsp), %xmm0
jbe 0x2a99e
leaq 0x138(%rsp), %rdi
callq 0x1eed0
jmp 0x2a82e
movl $0x0, 0x128(%rsp)
movl 0x128(%rsp), %eax
movl %eax, 0x50(%rsp)
leaq 0x138(%rsp), %rdi
callq 0x135e0
movl %eax, 0x54(%rsp)
jmp 0x2a857
movl 0x50(%rsp), %eax
movl 0x54(%rsp), %ecx
cmpl %ecx, %eax
jge 0x2a999
movl 0x128(%rsp), %esi
leaq 0x138(%rsp), %rdi
callq 0x13630
movq %rax, 0x48(%rsp)
jmp 0x2a882
movq 0x48(%rsp), %rax
movq %rax, 0x120(%rsp)
movq 0x120(%rsp), %rsi
leaq 0x138(%rsp), %rdi
callq 0x13660
movq %rax, 0x40(%rsp)
jmp 0x2a8ab
movq 0x40(%rsp), %rsi
leaq 0x108(%rsp), %rdi
callq 0x6eb90
jmp 0x2a8bf
leaq 0xe8(%rsp), %rdi
leaq 0x108(%rsp), %rsi
callq 0x36d10
jmp 0x2a8d6
leaq 0xe8(%rsp), %rdi
callq 0x6fe90
movsd %xmm0, 0x38(%rsp)
jmp 0x2a8eb
movsd 0x38(%rsp), %xmm0
movsd %xmm0, 0x100(%rsp)
movsd 0x100(%rsp), %xmm0
leaq 0x14dbd6(%rip), %rax    # 0x1784e0
movsd (%rax), %xmm1
callq 0x36d50
movl %eax, 0x34(%rsp)
jmp 0x2a919
movl 0x34(%rsp), %eax
cmpl $0x0, %eax
je 0x2a962
movsd 0x100(%rsp), %xmm0
leaq 0x108(%rsp), %rdi
callq 0x6f960
jmp 0x2a93a
jmp 0x2a962
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x130(%rsp)
movl %eax, 0x12c(%rsp)
leaq 0x138(%rsp), %rdi
callq 0x14d70
jmp 0x2ab51
movq 0x120(%rsp), %rsi
leaq 0x138(%rsp), %rdi
leaq 0x108(%rsp), %rdx
callq 0x18060
jmp 0x2a981
jmp 0x2a983
movl 0x128(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x128(%rsp)
jmp 0x2a839
jmp 0x2a7f5
movl $0x0, 0xe4(%rsp)
movl 0xe4(%rsp), %eax
movl %eax, 0x2c(%rsp)
leaq 0x138(%rsp), %rdi
callq 0x135e0
movl %eax, 0x30(%rsp)
jmp 0x2a9c7
movl 0x2c(%rsp), %eax
movl 0x30(%rsp), %ecx
cmpl %ecx, %eax
jge 0x2ab28
movl 0xe4(%rsp), %esi
leaq 0x138(%rsp), %rdi
callq 0x13630
movq %rax, 0x20(%rsp)
jmp 0x2a9f2
movq 0x20(%rsp), %rax
movq %rax, 0xd8(%rsp)
movq 0xd8(%rsp), %rsi
leaq 0x138(%rsp), %rdi
callq 0x13660
movq %rax, 0x18(%rsp)
jmp 0x2aa1b
movq 0x18(%rsp), %rsi
leaq 0xc0(%rsp), %rdi
callq 0x6eb90
jmp 0x2aa2f
leaq 0xa0(%rsp), %rdi
leaq 0xc0(%rsp), %rsi
callq 0x36d10
jmp 0x2aa46
leaq 0xa0(%rsp), %rdi
callq 0x6fe90
movsd %xmm0, 0x10(%rsp)
jmp 0x2aa5b
movsd 0x10(%rsp), %xmm0
movsd %xmm0, 0xb8(%rsp)
movsd 0xb8(%rsp), %xmm0
leaq 0x14da66(%rip), %rax    # 0x1784e0
movsd (%rax), %xmm1
callq 0x36d50
movl %eax, 0xc(%rsp)
jmp 0x2aa89
movl 0xc(%rsp), %eax
cmpl $0x0, %eax
je 0x2aaf1
movsd 0x270(%rsp), %xmm0
movsd 0xb8(%rsp), %xmm1
divsd %xmm1, %xmm0
leaq 0x70(%rsp), %rdi
leaq 0xc0(%rsp), %rsi
callq 0x36b10
jmp 0x2aabc
leaq 0x88(%rsp), %rdi
leaq 0x278(%rsp), %rsi
leaq 0x70(%rsp), %rdx
callq 0x6fa40
jmp 0x2aad8
leaq 0xc0(%rsp), %rdi
leaq 0x88(%rsp), %rsi
callq 0x6f1e0
jmp 0x2aaef
jmp 0x2aaf1
movq 0xd8(%rsp), %rsi
leaq 0x138(%rsp), %rdi
leaq 0xc0(%rsp), %rdx
callq 0x18060
jmp 0x2ab10
jmp 0x2ab12
movl 0xe4(%rsp), %eax
addl $0x1, %eax
movl %eax, 0xe4(%rsp)
jmp 0x2a9a9
movq 0x68(%rsp), %rdi
leaq 0x138(%rsp), %rsi
callq 0x2ab60
jmp 0x2ab3c
leaq 0x138(%rsp), %rdi
callq 0x14d70
addq $0x2a8, %rsp            # imm = 0x2A8
retq
movq 0x130(%rsp), %rdi
callq 0xf890
nop