// File: /workspace/github_cpp_makefile_star2_O2/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 $0x1b8, %rsp            # imm = 0x1B8
movaps %xmm0, (%rsp)
movq %rsi, %rbx
movq %rdi, %r14
movq (%rsi), %rax
leaq 0x50(%rsp), %rdi
callq *0x68(%rax)
movsd 0x20(%rbx), %xmm0
movsd %xmm0, 0x18(%rsp)
divsd 0x77875(%rip), %xmm0    # 0x93258
xorpd %xmm2, %xmm2
movapd (%rsp), %xmm3
cmpeqsd %xmm3, %xmm2
movapd %xmm2, %xmm1
andnpd %xmm3, %xmm1
andpd %xmm0, %xmm2
orpd %xmm1, %xmm2
leaq 0xbf1a0(%rip), %rbp     # 0xdaba8
movsd (%rbp), %xmm0
movapd 0x7761b(%rip), %xmm1    # 0x93030
xorpd %xmm0, %xmm1
ucomisd %xmm1, %xmm2
jb 0x1ba29
ucomisd %xmm2, %xmm0
jae 0x1bb9b
movapd %xmm2, (%rsp)
movq %r14, 0x10(%rsp)
leaq 0x80(%rsp), %rbx
movq %rbx, %rdi
callq 0x10df4
leaq 0xbf356(%rip), %rsi     # 0xdada0
movq %rbx, %rdi
callq 0x1b462
leaq 0x80(%rsp), %r14
leaq 0x38(%rsp), %r15
leaq 0x20(%rsp), %r12
movq %r14, %rdi
callq 0x1169e
ucomisd (%rsp), %xmm0
jbe 0x1bad8
movq %r14, %rdi
callq 0x15ec8
xorl %r13d, %r13d
movslq 0x94(%rsp), %rax
cmpq %rax, %r13
jge 0x1ba64
movq 0x88(%rsp), %rax
movq (%rax,%r13,8), %rbx
leaq 0x18(%rbx), %rsi
movq %r15, %rdi
callq 0x37b5c
movq %r12, %rdi
movq %r15, %rsi
callq 0x212f8
movq %r12, %rdi
callq 0x38412
ucomisd (%rbp), %xmm0
jbe 0x1bac5
movq %r15, %rdi
callq 0x38234
movq %r14, %rdi
movq %rbx, %rsi
movq %r15, %rdx
callq 0x12ccc
incq %r13
jmp 0x1ba7e
xorl %r12d, %r12d
leaq 0x38(%rsp), %r14
leaq 0x20(%rsp), %r15
leaq 0x68(%rsp), %r13
movslq 0x94(%rsp), %rax
cmpq %rax, %r12
jge 0x1bb7c
movq 0x88(%rsp), %rax
movq (%rax,%r12,8), %rbx
leaq 0x18(%rbx), %rsi
movq %r14, %rdi
callq 0x37b5c
movq %r15, %rdi
movq %r14, %rsi
callq 0x212f8
movq %r15, %rdi
callq 0x38412
ucomisd (%rbp), %xmm0
jbe 0x1bb61
movsd 0x18(%rsp), %xmm1
divsd %xmm0, %xmm1
movq %r13, %rdi
movq %r14, %rsi
movapd %xmm1, %xmm0
callq 0x382c0
movq %r15, %rdi
leaq 0x50(%rsp), %rsi
movq %r13, %rdx
callq 0x3827a
movq %r14, %rdi
movq %r15, %rsi
callq 0x37e36
leaq 0x80(%rsp), %rdi
movq %rbx, %rsi
movq %r14, %rdx
callq 0x12ccc
incq %r12
jmp 0x1baea
leaq 0x80(%rsp), %rsi
movq 0x10(%rsp), %rdi
callq 0x1bbd6
leaq 0x80(%rsp), %rdi
callq 0x112dc
addq $0x1b8, %rsp            # imm = 0x1B8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x1bbbd
jmp 0x1bbbd
jmp 0x1bbbd
jmp 0x1bbbd
jmp 0x1bbbd
jmp 0x1bbbd
jmp 0x1bbbd
jmp 0x1bbbd
movq %rax, %rbx
leaq 0x80(%rsp), %rdi
callq 0x112dc
movq %rbx, %rdi
callq 0xe810
nop