// File: tomfunkhouser[P]gaps/apps/msh2pts/msh2pts.cpp
// Function: ComputeFaceSamplingWeights(gaps::R3Mesh*, gaps::R3MeshProperty*, double, double)
static RNScalar
ComputeFaceSamplingWeights(R3Mesh *mesh, R3MeshProperty *property,
  RNScalar curvature_exponent = 0, RNScalar curvature_max = RN_INFINITY)
{
  // Compute face weights
  RNScalar total_weight = 0;
  for (int i = 0; i < mesh->NFaces(); i++) {
    R3MeshFace *face = mesh->Face(i);

    // Initailize weight by face area
    RNScalar weight = mesh->FaceArea(face);

    // Scale weight by curvatures
    if (property) {
      // Compute average value of vertices
      R3MeshVertex *v0 = mesh->VertexOnFace(face, 0);
      R3MeshVertex *v1 = mesh->VertexOnFace(face, 1);
      R3MeshVertex *v2 = mesh->VertexOnFace(face, 2);
      RNScalar c0 = fabs(property->VertexValue(v0));
      RNScalar c1 = fabs(property->VertexValue(v1));
      RNScalar c2 = fabs(property->VertexValue(v2));
      weight *= 1.0 + (c0 + c1 + c2) / 3.0;
    }
    else if (curvature_exponent > 0) {
      // Compute curvature
      R3MeshVertex *v0 = mesh->VertexOnFace(face, 0);
      R3MeshVertex *v1 = mesh->VertexOnFace(face, 1);
      R3MeshVertex *v2 = mesh->VertexOnFace(face, 2);
      RNScalar c0 = fabs(mesh->VertexMeanCurvature(v0));
      RNScalar c1 = fabs(mesh->VertexMeanCurvature(v1));
      RNScalar c2 = fabs(mesh->VertexMeanCurvature(v2));
      RNScalar curvature = 0;
      curvature += (c0 < curvature_max) ? c0 : curvature_max;
      curvature += (c1 < curvature_max) ? c1 : curvature_max;
      curvature += (c2 < curvature_max) ? c2 : curvature_max;
      curvature /= 3.0;

      // Adjust weight
      weight *= pow(1.0 + curvature, curvature_exponent);
    }

    // Set face sampling weight
    mesh->SetFaceValue(face, weight);
    total_weight += weight;
  }

  // Delete property
  if (property) delete property;

  // Return total weight
  return total_weight;
}

// Assembly:
subq $0xd8, %rsp
movq %rdi, 0xd0(%rsp)
movq %rsi, 0xc8(%rsp)
movsd %xmm0, 0xc0(%rsp)
movsd %xmm1, 0xb8(%rsp)
xorps %xmm0, %xmm0
movsd %xmm0, 0xb0(%rsp)
movl $0x0, 0xac(%rsp)
movl 0xac(%rsp), %eax
movl %eax, 0x2c(%rsp)
movq 0xd0(%rsp), %rdi
callq 0x1b420
movl %eax, %ecx
movl 0x2c(%rsp), %eax
cmpl %ecx, %eax
jge 0x1981a
movq 0xd0(%rsp), %rdi
movl 0xac(%rsp), %esi
callq 0x1b480
movq %rax, 0xa0(%rsp)
movq 0xd0(%rsp), %rdi
movq 0xa0(%rsp), %rsi
callq 0x1b930
movsd %xmm0, 0x98(%rsp)
cmpq $0x0, 0xc8(%rsp)
je 0x195d5
movq 0xd0(%rsp), %rdi
movq 0xa0(%rsp), %rsi
xorl %edx, %edx
callq 0x1b870
movq %rax, 0x90(%rsp)
movq 0xd0(%rsp), %rdi
movq 0xa0(%rsp), %rsi
movl $0x1, %edx
callq 0x1b870
movq %rax, 0x88(%rsp)
movq 0xd0(%rsp), %rdi
movq 0xa0(%rsp), %rsi
movl $0x2, %edx
callq 0x1b870
movq %rax, 0x80(%rsp)
movq 0xc8(%rsp), %rdi
movq 0x90(%rsp), %rsi
callq 0x1b9a0
movaps 0x130b32(%rip), %xmm1   # 0x14a070
pand %xmm1, %xmm0
movsd %xmm0, 0x78(%rsp)
movq 0xc8(%rsp), %rdi
movq 0x88(%rsp), %rsi
callq 0x1b9a0
movaps 0x130b0c(%rip), %xmm1   # 0x14a070
pand %xmm1, %xmm0
movsd %xmm0, 0x70(%rsp)
movq 0xc8(%rsp), %rdi
movq 0x80(%rsp), %rsi
callq 0x1b9a0
movaps 0x130ae6(%rip), %xmm1   # 0x14a070
pand %xmm1, %xmm0
movsd %xmm0, 0x68(%rsp)
movsd 0x78(%rsp), %xmm1
addsd 0x70(%rsp), %xmm1
addsd 0x68(%rsp), %xmm1
movsd 0x130aa2(%rip), %xmm0   # 0x14a050
divsd %xmm0, %xmm1
movsd 0x130a66(%rip), %xmm0   # 0x14a020
addsd %xmm1, %xmm0
mulsd 0x98(%rsp), %xmm0
movsd %xmm0, 0x98(%rsp)
jmp 0x197cb
movsd 0xc0(%rsp), %xmm0
xorps %xmm1, %xmm1
ucomisd %xmm1, %xmm0
jbe 0x197c9
movq 0xd0(%rsp), %rdi
movq 0xa0(%rsp), %rsi
xorl %edx, %edx
callq 0x1b870
movq %rax, 0x60(%rsp)
movq 0xd0(%rsp), %rdi
movq 0xa0(%rsp), %rsi
movl $0x1, %edx
callq 0x1b870
movq %rax, 0x58(%rsp)
movq 0xd0(%rsp), %rdi
movq 0xa0(%rsp), %rsi
movl $0x2, %edx
callq 0x1b870
movq %rax, 0x50(%rsp)
movq 0xd0(%rsp), %rdi
movq 0x60(%rsp), %rsi
callq 0x439f0
movaps 0x130a12(%rip), %xmm1   # 0x14a070
pand %xmm1, %xmm0
movsd %xmm0, 0x48(%rsp)
movq 0xd0(%rsp), %rdi
movq 0x58(%rsp), %rsi
callq 0x439f0
movaps 0x1309ef(%rip), %xmm1   # 0x14a070
pand %xmm1, %xmm0
movsd %xmm0, 0x40(%rsp)
movq 0xd0(%rsp), %rdi
movq 0x50(%rsp), %rsi
callq 0x439f0
movaps 0x1309cc(%rip), %xmm1   # 0x14a070
pand %xmm1, %xmm0
movsd %xmm0, 0x38(%rsp)
xorps %xmm0, %xmm0
movsd %xmm0, 0x30(%rsp)
movsd 0x48(%rsp), %xmm1
movsd 0xb8(%rsp), %xmm0
ucomisd %xmm1, %xmm0
jbe 0x196da
movsd 0x48(%rsp), %xmm0
movsd %xmm0, 0x20(%rsp)
jmp 0x196e9
movsd 0xb8(%rsp), %xmm0
movsd %xmm0, 0x20(%rsp)
movsd 0x20(%rsp), %xmm0
addsd 0x30(%rsp), %xmm0
movsd %xmm0, 0x30(%rsp)
movsd 0x40(%rsp), %xmm1
movsd 0xb8(%rsp), %xmm0
ucomisd %xmm1, %xmm0
jbe 0x1971e
movsd 0x40(%rsp), %xmm0
movsd %xmm0, 0x18(%rsp)
jmp 0x1972d
movsd 0xb8(%rsp), %xmm0
movsd %xmm0, 0x18(%rsp)
movsd 0x18(%rsp), %xmm0
addsd 0x30(%rsp), %xmm0
movsd %xmm0, 0x30(%rsp)
movsd 0x38(%rsp), %xmm1
movsd 0xb8(%rsp), %xmm0
ucomisd %xmm1, %xmm0
jbe 0x19762
movsd 0x38(%rsp), %xmm0
movsd %xmm0, 0x10(%rsp)
jmp 0x19771
movsd 0xb8(%rsp), %xmm0
movsd %xmm0, 0x10(%rsp)
movsd 0x10(%rsp), %xmm0
addsd 0x30(%rsp), %xmm0
movsd %xmm0, 0x30(%rsp)
movsd 0x30(%rsp), %xmm0
movsd 0x1308bf(%rip), %xmm1   # 0x14a050
divsd %xmm1, %xmm0
movsd %xmm0, 0x30(%rsp)
movsd 0x13087d(%rip), %xmm0   # 0x14a020
addsd 0x30(%rsp), %xmm0
movsd 0xc0(%rsp), %xmm1
callq 0xf890
mulsd 0x98(%rsp), %xmm0
movsd %xmm0, 0x98(%rsp)
jmp 0x197cb
movq 0xd0(%rsp), %rdi
movq 0xa0(%rsp), %rsi
movsd 0x98(%rsp), %xmm0
callq 0x1b9e0
movsd 0x98(%rsp), %xmm0
addsd 0xb0(%rsp), %xmm0
movsd %xmm0, 0xb0(%rsp)
movl 0xac(%rsp), %eax
addl $0x1, %eax
movl %eax, 0xac(%rsp)
jmp 0x19450
cmpq $0x0, 0xc8(%rsp)
je 0x19853
movq 0xc8(%rsp), %rax
movq %rax, 0x8(%rsp)
cmpq $0x0, %rax
je 0x19851
movq 0x8(%rsp), %rdi
callq 0x26680
movq 0x8(%rsp), %rdi
movl $0x448, %esi            # imm = 0x448
callq 0xf4e0
jmp 0x19853
movsd 0xb0(%rsp), %xmm0
addq $0xd8, %rsp
retq
nopw %cs:(%rax,%rax)