// File: /workspace/github_cpp_makefile_star2_O3/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:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x1c8, %rsp            # imm = 0x1C8
movaps %xmm0, 0x20(%rsp)
movq %rsi, %rbx
movq %rdi, %r14
movq (%rsi), %rax
leaq 0x60(%rsp), %rdi
callq *0x68(%rax)
movsd 0x20(%rbx), %xmm0
movsd %xmm0, 0x40(%rsp)
divsd 0x84618(%rip), %xmm0    # 0xa2258
xorpd %xmm2, %xmm2
movapd 0x20(%rsp), %xmm3
cmpeqsd %xmm3, %xmm2
movapd %xmm2, %xmm1
andnpd %xmm3, %xmm1
andpd %xmm0, %xmm2
orpd %xmm1, %xmm2
leaq 0xc7f32(%rip), %rbp     # 0xe5b98
movsd (%rbp), %xmm0
movapd 0x843ad(%rip), %xmm1    # 0xa2020
xorpd %xmm0, %xmm1
ucomisd %xmm1, %xmm2
jb 0x1dc87
ucomisd %xmm2, %xmm0
jae 0x1de33
movapd %xmm2, 0x20(%rsp)
movq %r14, 0x38(%rsp)
leaq 0x90(%rsp), %rbx
movq %rbx, %rdi
callq 0x11528
leaq 0xc80e7(%rip), %rsi     # 0xe5d90
movq %rbx, %rdi
callq 0x1d6c8
leaq 0x90(%rsp), %r14
leaq 0x8(%rsp), %r15
leaq 0x48(%rsp), %r12
movq %r14, %rdi
callq 0x11e70
ucomisd 0x20(%rsp), %xmm0
jbe 0x1dd58
movq %r14, %rdi
callq 0x17252
cmpl $0x0, 0xa4(%rsp)
jle 0x1dcc3
xorl %r13d, %r13d
movq 0x98(%rsp), %rax
movq (%rax,%r13,8), %rbx
leaq 0x18(%rbx), %rsi
movq %r15, %rdi
callq 0x3c542
movsd 0x8(%rsp), %xmm0
movsd 0x10(%rsp), %xmm1
movsd 0x18(%rsp), %xmm2
movq %r12, %rdi
callq 0x3cfc6
movq %r12, %rdi
callq 0x3d0cc
ucomisd (%rbp), %xmm0
jbe 0x1dd35
movq %r15, %rdi
callq 0x3ceea
movq %r14, %rdi
movq %rbx, %rsi
movq %r15, %rdx
callq 0x13858
incq %r13
movslq 0xa4(%rsp), %rax
cmpq %rax, %r13
jl 0x1dcec
jmp 0x1dcc3
cmpl $0x0, 0xa4(%rsp)
jle 0x1de14
xorl %r12d, %r12d
leaq 0x8(%rsp), %r14
leaq 0x48(%rsp), %r15
leaq 0x78(%rsp), %r13
movq 0x98(%rsp), %rax
movq (%rax,%r12,8), %rbx
leaq 0x18(%rbx), %rsi
movq %r14, %rdi
callq 0x3c542
movsd 0x8(%rsp), %xmm0
movsd 0x10(%rsp), %xmm1
movsd 0x18(%rsp), %xmm2
movq %r15, %rdi
callq 0x3cfc6
movq %r15, %rdi
callq 0x3d0cc
ucomisd (%rbp), %xmm0
jbe 0x1dded
movsd 0x40(%rsp), %xmm1
divsd %xmm0, %xmm1
movq %r13, %rdi
movq %r14, %rsi
movapd %xmm1, %xmm0
callq 0x3cf76
movq %r15, %rdi
leaq 0x60(%rsp), %rsi
movq %r13, %rdx
callq 0x3cf30
movq %r14, %rdi
movq %r15, %rsi
callq 0x3c89a
leaq 0x90(%rsp), %rdi
movq %rbx, %rsi
movq %r14, %rdx
callq 0x13858
incq %r12
movslq 0xa4(%rsp), %rax
cmpq %rax, %r12
jl 0x1dd78
leaq 0x90(%rsp), %rsi
movq 0x38(%rsp), %rdi
callq 0x1de6e
leaq 0x90(%rsp), %rdi
callq 0x11a66
addq $0x1c8, %rsp            # imm = 0x1C8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x1de55
jmp 0x1de55
jmp 0x1de55
jmp 0x1de55
jmp 0x1de55
jmp 0x1de55
jmp 0x1de55
jmp 0x1de55
movq %rax, %rbx
leaq 0x90(%rsp), %rdi
callq 0x11a66
movq %rbx, %rdi
callq 0xe7e0
nop