// File: /workspace/github_cpp_makefile_star2_O2/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:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x48, %rsp
movsd %xmm1, 0x10(%rsp)
movsd %xmm0, (%rsp)
movq %rsi, %rbx
movq %rdi, %r14
xorl %ebp, %ebp
xorpd %xmm0, %xmm0
movslq 0x34(%r14), %rax
cmpq %rax, %rbp
movsd %xmm0, 0x8(%rsp)
jge 0x1482e
movq 0x28(%r14), %rax
movq (%rax,%rbp,8), %r15
movq %r14, %rdi
movq %r15, %rsi
callq 0x14938
movapd %xmm0, %xmm4
testq %rbx, %rbx
je 0x14752
movq 0x8(%r15), %rax
movq 0x10(%r15), %rcx
movq 0x18(%r15), %rdx
movslq 0x70(%rax), %rax
movq 0x410(%rbx), %rsi
movsd (%rsi,%rax,8), %xmm0
movapd 0x8997d(%rip), %xmm3    # 0x9e090
andpd %xmm3, %xmm0
movslq 0x70(%rcx), %rax
movsd (%rsi,%rax,8), %xmm1
andpd %xmm3, %xmm1
movslq 0x70(%rdx), %rax
movsd (%rsi,%rax,8), %xmm2
andpd %xmm3, %xmm2
addsd %xmm0, %xmm1
addsd %xmm2, %xmm1
divsd 0x89927(%rip), %xmm1    # 0x9e068
addsd 0x898e7(%rip), %xmm1    # 0x9e030
mulsd %xmm1, %xmm4
jmp 0x14813
movsd (%rsp), %xmm0
ucomisd 0x898f1(%rip), %xmm0    # 0x9e050
jbe 0x14813
movq 0x8(%r15), %rsi
movq 0x10(%r15), %r12
movq 0x18(%r15), %r13
movq %r14, %rdi
movsd %xmm4, 0x18(%rsp)
callq 0x266be
movapd %xmm0, %xmm1
movapd 0x89905(%rip), %xmm0    # 0x9e090
andpd %xmm0, %xmm1
movapd %xmm1, 0x30(%rsp)
movq %r14, %rdi
movq %r12, %rsi
callq 0x266be
andpd 0x898e8(%rip), %xmm0    # 0x9e090
movapd %xmm0, 0x20(%rsp)
movq %r14, %rdi
movq %r13, %rsi
callq 0x266be
andpd 0x898cf(%rip), %xmm0    # 0x9e090
movsd 0x10(%rsp), %xmm1
movapd 0x30(%rsp), %xmm2
minsd %xmm1, %xmm2
addsd 0x89877(%rip), %xmm2    # 0x9e050
movapd 0x20(%rsp), %xmm3
minsd %xmm1, %xmm3
addsd %xmm2, %xmm3
minsd %xmm1, %xmm0
addsd %xmm3, %xmm0
divsd 0x89871(%rip), %xmm0    # 0x9e068
addsd 0x89831(%rip), %xmm0    # 0x9e030
movsd (%rsp), %xmm1
callq 0xe810
movsd 0x18(%rsp), %xmm4
mulsd %xmm0, %xmm4
movsd %xmm4, 0xb0(%r15)
movsd 0x8(%rsp), %xmm0
addsd %xmm4, %xmm0
incq %rbp
jmp 0x146c0
testq %rbx, %rbx
je 0x1484e
movq %rbx, %rdi
callq 0x1925c
movl $0x448, %esi            # imm = 0x448
movq %rbx, %rdi
callq 0xe470
movsd 0x8(%rsp), %xmm0
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq