// File: tomfunkhouser[P]gaps/pkgs/R3Shapes/R3Mesh.cpp
// Function: gaps::R3Mesh::AverageRadius(gaps::R3Point const*) const
RNLength R3Mesh::
AverageRadius(const R3Point *center) const
{
  // Get centroid
  R3Point centroid = (center) ? *center : Centroid();

  // Check if mesh has faces
  if (NFaces() > 0) {
    // Compute average distance between a position on the surface and a center point
    RNArea area = 0;
    RNLength distance = 0;
    for (int i = 0; i < NFaces(); i++) {
      R3MeshFace *face = Face(i);
      RNArea face_area = FaceArea(face);
      R3Point face_centroid = FaceCentroid(face);
      distance += face_area * R3Distance(face_centroid, centroid);
      area += face_area;
    }

    // Return weighted average
    if (area == 0) return 0;
    else return distance / area;
  }
  else if (NVertices() > 0) {
    // Sum distances between vertices and center point
    RNLength sum = 0;
    for (int i = 0; i < NVertices(); i++) {
      R3MeshVertex *vertex = Vertex(i);
      const R3Point& position = VertexPosition(vertex);
      sum += R3Distance(position, centroid);
    }

    // Return average
    return sum / NVertices();
  }

  // Should not get here unless mesh is empty
  return 0.0;
}

// Assembly:
subq $0xb8, %rsp
movq %rdi, 0xa8(%rsp)
movq %rsi, 0xa0(%rsp)
movq 0xa8(%rsp), %rax
movq %rax, 0x20(%rsp)
cmpq $0x0, 0xa0(%rsp)
je 0x15176
movq 0xa0(%rsp), %rsi
leaq 0x88(%rsp), %rdi
callq 0x6eb90
jmp 0x15188
movq 0x20(%rsp), %rsi
leaq 0x88(%rsp), %rdi
callq 0x14e10
movq 0x20(%rsp), %rdi
callq 0x135a0
cmpl $0x0, %eax
jle 0x152b9
xorps %xmm0, %xmm0
movsd %xmm0, 0x80(%rsp)
xorps %xmm0, %xmm0
movsd %xmm0, 0x78(%rsp)
movl $0x0, 0x74(%rsp)
movq 0x20(%rsp), %rdi
movl 0x74(%rsp), %eax
movl %eax, 0x1c(%rsp)
callq 0x135a0
movl %eax, %ecx
movl 0x1c(%rsp), %eax
cmpl %ecx, %eax
jge 0x15277
movq 0x20(%rsp), %rdi
movl 0x74(%rsp), %esi
callq 0x136d0
movq 0x20(%rsp), %rdi
movq %rax, 0x68(%rsp)
movq 0x68(%rsp), %rsi
callq 0x36aa0
movq 0x20(%rsp), %rsi
movsd %xmm0, 0x60(%rsp)
movq 0x68(%rsp), %rdx
leaq 0x48(%rsp), %rdi
callq 0x14ff0
movsd 0x60(%rsp), %xmm0
movsd %xmm0, 0x10(%rsp)
leaq 0x48(%rsp), %rdi
leaq 0x88(%rsp), %rsi
callq 0x86c40
movaps %xmm0, %xmm2
movsd 0x10(%rsp), %xmm0
movsd 0x78(%rsp), %xmm1
mulsd %xmm2, %xmm0
addsd %xmm1, %xmm0
movsd %xmm0, 0x78(%rsp)
movsd 0x60(%rsp), %xmm0
addsd 0x80(%rsp), %xmm0
movsd %xmm0, 0x80(%rsp)
movl 0x74(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x74(%rsp)
jmp 0x151b8
movsd 0x80(%rsp), %xmm0
xorps %xmm1, %xmm1
ucomisd %xmm1, %xmm0
jne 0x1529c
jp 0x1529c
xorps %xmm0, %xmm0
movsd %xmm0, 0xb0(%rsp)
jmp 0x15386
movsd 0x78(%rsp), %xmm0
divsd 0x80(%rsp), %xmm0
movsd %xmm0, 0xb0(%rsp)
jmp 0x15386
movq 0x20(%rsp), %rdi
callq 0x135e0
cmpl $0x0, %eax
jle 0x15378
xorps %xmm0, %xmm0
movsd %xmm0, 0x40(%rsp)
movl $0x0, 0x3c(%rsp)
movq 0x20(%rsp), %rdi
movl 0x3c(%rsp), %eax
movl %eax, 0xc(%rsp)
callq 0x135e0
movl %eax, %ecx
movl 0xc(%rsp), %eax
cmpl %ecx, %eax
jge 0x1534b
movq 0x20(%rsp), %rdi
movl 0x3c(%rsp), %esi
callq 0x13630
movq 0x20(%rsp), %rdi
movq %rax, 0x30(%rsp)
movq 0x30(%rsp), %rsi
callq 0x13660
movq %rax, 0x28(%rsp)
movq 0x28(%rsp), %rdi
leaq 0x88(%rsp), %rsi
callq 0x86c40
addsd 0x40(%rsp), %xmm0
movsd %xmm0, 0x40(%rsp)
movl 0x3c(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x3c(%rsp)
jmp 0x152dd
movq 0x20(%rsp), %rdi
movsd 0x40(%rsp), %xmm0
movsd %xmm0, (%rsp)
callq 0x135e0
movsd (%rsp), %xmm0
cvtsi2sd %eax, %xmm1
divsd %xmm1, %xmm0
movsd %xmm0, 0xb0(%rsp)
jmp 0x15386
jmp 0x1537a
xorps %xmm0, %xmm0
movsd %xmm0, 0xb0(%rsp)
movsd 0xb0(%rsp), %xmm0
addq $0xb8, %rsp
retq
nopw (%rax,%rax)