// File: tomfunkhouser[P]gaps/pkgs/R3Shapes/R3Mesh.cpp
// Function: gaps::R3Mesh::VertexLaplacianVector(gaps::R3MeshVertex const*) const
R3Vector R3Mesh::
VertexLaplacianVector(const R3MeshVertex *v1) const
{
  // Compute vector from vertex to cotan weighed average of neighbors 
  // From http://www.mpi-inf.mpg.de/~ag4-gm/handouts/06gm_surf3.pdf
  R3Vector laplacian = R3zero_vector;
  const R3Point& p1 = VertexPosition(v1);
  for (int j = 0; j < VertexValence(v1); j++) {
    R3MeshEdge *e = EdgeOnVertex(v1, j);
    R3MeshVertex *v2 = VertexAcrossEdge(e, v1);
    const R3Point& p2 = VertexPosition(v2);

    // Compute cotan weight
    double weight = 0;
    for (int k = 0; k < 2; k++) {
      R3MeshFace *f = FaceOnEdge(e, k);
      if (!f) continue;
      R3MeshVertex *v3 = VertexAcrossFace(f, e);
      const R3Point& p3 = VertexPosition(v3);
      R3Vector vec1 = p1 - p3; vec1.Normalize();
      R3Vector vec2 = p2 - p3; vec2.Normalize();
      RNAngle angle = R3InteriorAngle(vec1, vec2);
      if (angle == 0) continue; 
      double tan_angle = tan(angle);
      if (tan_angle == 0) continue; 
      weight += 1.0 / tan_angle;
    }

    // Add weighted position
    laplacian += weight * (p2 - p1);
  }

  // Normalize by area (maybe should be 3X -- i.e., for face areas, not vertex area)
  laplacian /= 4 * VertexArea(v1);

  // Return laplacian vector
  return laplacian;
}

// Assembly:
subq $0x108, %rsp            # imm = 0x108
movq %rdi, 0x18(%rsp)
movq %rdi, %rax
movq %rax, 0x20(%rsp)
movq %rdi, 0x100(%rsp)
movq %rsi, 0xf8(%rsp)
movq %rdx, 0xf0(%rsp)
movq 0xf8(%rsp), %rax
movq %rax, 0x28(%rsp)
leaq 0x562370(%rip), %rsi    # 0x5795a0
callq 0x6fcf0
movq 0x28(%rsp), %rdi
movq 0xf0(%rsp), %rsi
callq 0x13660
movq %rax, 0xe8(%rsp)
movl $0x0, 0xe4(%rsp)
movq 0x28(%rsp), %rdi
movl 0xe4(%rsp), %eax
movl %eax, 0x14(%rsp)
movq 0xf0(%rsp), %rsi
callq 0x36b50
movl %eax, %ecx
movl 0x14(%rsp), %eax
cmpl %ecx, %eax
jge 0x174aa
movq 0x28(%rsp), %rdi
movq 0xf0(%rsp), %rsi
movl 0xe4(%rsp), %edx
callq 0x36ec0
movq 0x28(%rsp), %rdi
movq %rax, 0xd8(%rsp)
movq 0xd8(%rsp), %rsi
movq 0xf0(%rsp), %rdx
callq 0x36f50
movq 0x28(%rsp), %rdi
movq %rax, 0xd0(%rsp)
movq 0xd0(%rsp), %rsi
callq 0x13660
movq %rax, 0xc8(%rsp)
xorps %xmm0, %xmm0
movsd %xmm0, 0xc0(%rsp)
movl $0x0, 0xbc(%rsp)
cmpl $0x2, 0xbc(%rsp)
jge 0x17447
movq 0x28(%rsp), %rdi
movq 0xd8(%rsp), %rsi
movl 0xbc(%rsp), %edx
callq 0x37050
movq %rax, 0xb0(%rsp)
cmpq $0x0, 0xb0(%rsp)
jne 0x17338
jmp 0x17431
movq 0x28(%rsp), %rdi
movq 0xb0(%rsp), %rsi
movq 0xd8(%rsp), %rdx
callq 0x37070
movq 0x28(%rsp), %rdi
movq %rax, 0xa8(%rsp)
movq 0xa8(%rsp), %rsi
callq 0x13660
movq %rax, 0xa0(%rsp)
movq 0xe8(%rsp), %rsi
movq 0xa0(%rsp), %rdx
leaq 0x88(%rsp), %rdi
callq 0x6eeb0
leaq 0x88(%rsp), %rdi
callq 0x70410
movq 0xc8(%rsp), %rsi
movq 0xa0(%rsp), %rdx
leaq 0x70(%rsp), %rdi
callq 0x6eeb0
leaq 0x70(%rsp), %rdi
callq 0x70410
leaq 0x88(%rsp), %rdi
leaq 0x70(%rsp), %rsi
callq 0x8c230
movsd %xmm0, 0x68(%rsp)
movsd 0x68(%rsp), %xmm0
xorps %xmm1, %xmm1
ucomisd %xmm1, %xmm0
jne 0x173ed
jp 0x173ed
jmp 0x17431
movsd 0x68(%rsp), %xmm0
callq 0xf2b0
movsd %xmm0, 0x60(%rsp)
movsd 0x60(%rsp), %xmm0
xorps %xmm1, %xmm1
ucomisd %xmm1, %xmm0
jne 0x17411
jp 0x17411
jmp 0x17431
movsd 0x11bbef(%rip), %xmm0   # 0x133008
divsd 0x60(%rsp), %xmm0
addsd 0xc0(%rsp), %xmm0
movsd %xmm0, 0xc0(%rsp)
movl 0xbc(%rsp), %eax
addl $0x1, %eax
movl %eax, 0xbc(%rsp)
jmp 0x172f9
movsd 0xc0(%rsp), %xmm0
movsd %xmm0, 0x8(%rsp)
movq 0xc8(%rsp), %rsi
movq 0xe8(%rsp), %rdx
leaq 0x30(%rsp), %rdi
callq 0x6eeb0
movsd 0x8(%rsp), %xmm0
leaq 0x48(%rsp), %rdi
leaq 0x30(%rsp), %rsi
callq 0x13800
movq 0x18(%rsp), %rdi
leaq 0x48(%rsp), %rsi
callq 0x70be0
movl 0xe4(%rsp), %eax
addl $0x1, %eax
movl %eax, 0xe4(%rsp)
jmp 0x1725a
movq 0x28(%rsp), %rdi
movq 0xf0(%rsp), %rsi
callq 0x16ce0
movq 0x18(%rsp), %rdi
movaps %xmm0, %xmm1
movsd 0x11bd24(%rip), %xmm0   # 0x1331f0
mulsd %xmm1, %xmm0
callq 0x70ec0
movq 0x20(%rsp), %rax
addq $0x108, %rsp            # imm = 0x108
retq
nopw %cs:(%rax,%rax)