// File: tomfunkhouser[P]gaps/apps/msh2pts/msh2pts.cpp
// Function: SelectNearSurfacePoints(gaps::R3Mesh*, gaps::R3MeshProperty*, int)
static RNArray<Point *> *
SelectNearSurfacePoints(R3Mesh *mesh, R3MeshProperty *property, int npoints)
{
  // Allocate array of points
  RNArray<Point *> *points = new RNArray<Point *>();
  if (!points) {
    RNFail("Unable to allocate array of points\n");
    return NULL;
  }

  // Create kdtree
  R3MeshSearchTree kdtree(mesh);

  // Compute face sampling weights
  RNScalar total_weight = ComputeFaceSamplingWeights(mesh, property);
  if (total_weight == 0) return NULL;

  // Sample points randomly from mesh surface
  for (int i = 0; i < mesh->NFaces(); i++) {
    R3MeshFace *face = mesh->Face(i);
    RNScalar face_weight = mesh->FaceValue(face);

    // Determine number of points on face (twice needed)
    RNScalar ideal_face_npoints = 1.1 * 0.5 * npoints * face_weight / total_weight;
    int face_npoints = (int) ideal_face_npoints;
    RNScalar remainder = ideal_face_npoints - face_npoints;
    if (remainder > RNRandomScalar()) face_npoints++;

    // Sample points at random position on face
    for (int j = 0; j < face_npoints; j++) {
      R3Point position = mesh->RandomPointOnFace(face);

      // Get ray from point in normal direction
      R3Vector normal = R3zero_vector;
      R3Point barycentrics = mesh->FaceBarycentric(face, position);
      R3MeshVertex *v0 = mesh->VertexOnFace(face, 0);
      R3MeshVertex *v1 = mesh->VertexOnFace(face, 1);
      R3MeshVertex *v2 = mesh->VertexOnFace(face, 2);
      normal += barycentrics[0] * mesh->VertexNormal(v0);
      normal += barycentrics[1] * mesh->VertexNormal(v1);
      normal += barycentrics[2] * mesh->VertexNormal(v2);
      normal.Normalize();
  
      // Find ray intersection on outside
      R3MeshIntersection intersectionA;
      R3Ray rayA(position, normal);
      kdtree.FindIntersection(rayA, intersectionA, RN_EPSILON, max_distance);
      if (intersectionA.type == R3_MESH_NULL_TYPE) {
        intersectionA.t = max_distance;
      }

      // Find ray intersection on inside
      R3MeshIntersection intersectionB;
      R3Ray rayB(position, -normal);
      kdtree.FindIntersection(rayB, intersectionB, RN_EPSILON, max_distance);
      if (intersectionB.type == R3_MESH_NULL_TYPE) {
        intersectionB.t = max_distance;
      }

      // Find lesser of two ray intersection distances
      RNScalar t = intersectionA.t;
      if (intersectionB.t < t) t = intersectionB.t;
      if (t == FLT_MAX) continue;  // should not happen for watertight mesh or if max_distance is set

      // Adjust t based on max distance
      if (t > max_distance) t = max_distance;

      // Adjust t to bias towards smaller values
      t *= pow(RNRandomScalar(), near_surface_bias_exponent);

      // Create point on outside
      R3Point positionA = position + t * normal;
      if (bbox.IsEmpty() || R3Contains(bbox, positionA)) {
        // Compute sdf
        RNScalar sdfA = 1;
        if (!binary_sdf) {
          R3MeshIntersection closest;
          kdtree.FindClosest(positionA, closest, 0, max_distance);
          sdfA = (closest.type != R3_MESH_NULL_TYPE) ? closest.t : t;
        }

        // Create point
        Point *pointA = new Point();
        pointA->face = face;
        pointA->position = positionA;
        pointA->normal = normal;
        pointA->sdf = sdfA;
        points->Insert(pointA);
      }

      // Create point on inside
      R3Point positionB = position - t * normal;
      if (bbox.IsEmpty() || R3Contains(bbox, positionB)) {
        // Compute sdf
        RNScalar sdfB = -1;
        if (!binary_sdf) {
          R3MeshIntersection closest;
          kdtree.FindClosest(positionB, closest, 0, max_distance);
          sdfB = (closest.type != R3_MESH_NULL_TYPE) ? -closest.t : -t;
        }

        R3MeshIntersection closest;
        kdtree.FindClosest(positionB, closest, 0, max_distance);
        Point *pointB = new Point();
        pointB->face = face;
        pointB->position = positionB;
        pointB->normal = normal;
        pointB->sdf = sdfB;
        points->Insert(pointB);
      }
    }
  }

  // Return points
  return points;
}

// Assembly:
subq $0x418, %rsp            # imm = 0x418
movq %rdi, 0x408(%rsp)
movq %rsi, 0x400(%rsp)
movl %edx, 0x3fc(%rsp)
movl $0x10, %edi
callq 0xf4d0
movq %rax, %rdi
movq %rdi, %rax
movq %rax, 0x98(%rsp)
callq 0x1b5a0
jmp 0x1615d
movq 0x98(%rsp), %rax
movq %rax, 0x3f0(%rsp)
cmpq $0x0, 0x3f0(%rsp)
jne 0x161c2
leaq 0x134490(%rip), %rdi    # 0x14a60f
movb $0x0, %al
callq 0xebe00
movq $0x0, 0x410(%rsp)
jmp 0x16c21
movq 0x98(%rsp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x3e8(%rsp)
movl %eax, 0x3e4(%rsp)
movl $0x10, %esi
callq 0xf4e0
jmp 0x16c31
movq 0x408(%rsp), %rsi
leaq 0x3c8(%rsp), %rdi
callq 0x1f840
movq 0x408(%rsp), %rdi
movq 0x400(%rsp), %rsi
leaq 0x17b332(%rip), %rax    # 0x191520
movsd (%rax), %xmm1
xorps %xmm0, %xmm0
callq 0x19410
movsd %xmm0, 0x90(%rsp)
jmp 0x16205
movsd 0x90(%rsp), %xmm0
movsd %xmm0, 0x3c0(%rsp)
movsd 0x3c0(%rsp), %xmm0
xorps %xmm1, %xmm1
ucomisd %xmm1, %xmm0
jne 0x16260
jp 0x16260
movq $0x0, 0x410(%rsp)
movl $0x1, 0x3bc(%rsp)
jmp 0x16c03
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x3e8(%rsp)
movl %eax, 0x3e4(%rsp)
jmp 0x16c12
movl $0x0, 0x3b8(%rsp)
movl 0x3b8(%rsp), %eax
movl %eax, 0x8c(%rsp)
movq 0x408(%rsp), %rdi
callq 0x1b420
movl %eax, %ecx
movl 0x8c(%rsp), %eax
cmpl %ecx, %eax
jge 0x16be8
movq 0x408(%rsp), %rdi
movl 0x3b8(%rsp), %esi
callq 0x1b480
movq %rax, 0x80(%rsp)
jmp 0x162b5
movq 0x80(%rsp), %rax
movq %rax, 0x3b0(%rsp)
movq 0x408(%rsp), %rdi
movq 0x3b0(%rsp), %rsi
callq 0x1b890
movsd %xmm0, 0x3a8(%rsp)
cvtsi2sdl 0x3fc(%rsp), %xmm0
movsd 0x133d3c(%rip), %xmm1   # 0x14a030
mulsd %xmm1, %xmm0
movsd 0x3a8(%rsp), %xmm1
mulsd %xmm1, %xmm0
movsd 0x3c0(%rsp), %xmm1
divsd %xmm1, %xmm0
movsd %xmm0, 0x3a0(%rsp)
cvttsd2si 0x3a0(%rsp), %eax
movl %eax, 0x39c(%rsp)
movsd 0x3a0(%rsp), %xmm0
cvtsi2sdl 0x39c(%rsp), %xmm1
subsd %xmm1, %xmm0
movsd %xmm0, 0x390(%rsp)
movsd 0x390(%rsp), %xmm0
movsd %xmm0, 0x70(%rsp)
callq 0xeb5e0
movsd %xmm0, 0x78(%rsp)
jmp 0x16366
movsd 0x70(%rsp), %xmm0
movsd 0x78(%rsp), %xmm1
ucomisd %xmm1, %xmm0
jbe 0x16389
movl 0x39c(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x39c(%rsp)
movl $0x0, 0x38c(%rsp)
movl 0x38c(%rsp), %eax
cmpl 0x39c(%rsp), %eax
jge 0x16bd0
movq 0x408(%rsp), %rsi
movq 0x3b0(%rsp), %rdx
leaq 0x370(%rsp), %rdi
callq 0x51c70
jmp 0x163c7
leaq 0x57c4d2(%rip), %rsi    # 0x5928a0
leaq 0x358(%rsp), %rdi
callq 0x9de00
jmp 0x163dd
movq 0x408(%rsp), %rsi
movq 0x3b0(%rsp), %rdx
leaq 0x340(%rsp), %rdi
leaq 0x370(%rsp), %rcx
callq 0x44380
jmp 0x16404
movq 0x408(%rsp), %rdi
movq 0x3b0(%rsp), %rsi
xorl %edx, %edx
movl %edx, 0x5c(%rsp)
callq 0x1b870
movq %rax, 0x338(%rsp)
movq 0x408(%rsp), %rdi
movq 0x3b0(%rsp), %rsi
movl $0x1, %edx
callq 0x1b870
movq %rax, 0x330(%rsp)
movq 0x408(%rsp), %rdi
movq 0x3b0(%rsp), %rsi
movl $0x2, %edx
callq 0x1b870
movl 0x5c(%rsp), %esi
movq %rax, 0x328(%rsp)
leaq 0x340(%rsp), %rdi
callq 0x1b400
movsd (%rax), %xmm0
movsd %xmm0, 0x60(%rsp)
movq 0x408(%rsp), %rdi
movq 0x338(%rsp), %rsi
callq 0x1b760
movq %rax, 0x68(%rsp)
jmp 0x164a2
movq 0x68(%rsp), %rsi
movsd 0x60(%rsp), %xmm0
leaq 0x310(%rsp), %rdi
callq 0x1b8f0
jmp 0x164bc
leaq 0x358(%rsp), %rdi
leaq 0x310(%rsp), %rsi
callq 0x9ecf0
jmp 0x164d3
leaq 0x340(%rsp), %rdi
movl $0x1, %esi
callq 0x1b400
movsd (%rax), %xmm0
movsd %xmm0, 0x48(%rsp)
movq 0x408(%rsp), %rdi
movq 0x330(%rsp), %rsi
callq 0x1b760
movq %rax, 0x50(%rsp)
jmp 0x1650b
movq 0x50(%rsp), %rsi
movsd 0x48(%rsp), %xmm0
leaq 0x2f8(%rsp), %rdi
callq 0x1b8f0
jmp 0x16525
leaq 0x358(%rsp), %rdi
leaq 0x2f8(%rsp), %rsi
callq 0x9ecf0
jmp 0x1653c
leaq 0x340(%rsp), %rdi
movl $0x2, %esi
callq 0x1b400
movsd (%rax), %xmm0
movsd %xmm0, 0x38(%rsp)
movq 0x408(%rsp), %rdi
movq 0x328(%rsp), %rsi
callq 0x1b760
movq %rax, 0x40(%rsp)
jmp 0x16574
movq 0x40(%rsp), %rsi
movsd 0x38(%rsp), %xmm0
leaq 0x2e0(%rsp), %rdi
callq 0x1b8f0
jmp 0x1658e
leaq 0x358(%rsp), %rdi
leaq 0x2e0(%rsp), %rsi
callq 0x9ecf0
jmp 0x165a5
leaq 0x358(%rsp), %rdi
callq 0x9e520
jmp 0x165b4
leaq 0x2a0(%rsp), %rdi
callq 0x1dff0
jmp 0x165c3
leaq 0x270(%rsp), %rdi
leaq 0x370(%rsp), %rsi
leaq 0x358(%rsp), %rdx
xorl %ecx, %ecx
callq 0x9c550
jmp 0x165e4
leaq 0x17af25(%rip), %rax    # 0x191510
movsd (%rax), %xmm0
movsd 0x171f31(%rip), %xmm1   # 0x188528
xorl %eax, %eax
movl %eax, %r8d
leaq 0x3c8(%rsp), %rdi
leaq 0x270(%rsp), %rsi
leaq 0x2a0(%rsp), %rdx
movq %r8, %rcx
callq 0x25880
jmp 0x1661e
cmpl $0x0, 0x2a0(%rsp)
jne 0x16639
movsd 0x171ef8(%rip), %xmm0   # 0x188528
movsd %xmm0, 0x2d8(%rsp)
leaq 0x230(%rsp), %rdi
callq 0x1dff0
jmp 0x16648
leaq 0x1e8(%rsp), %rdi
leaq 0x358(%rsp), %rsi
callq 0x9f0b0
jmp 0x1665f
leaq 0x200(%rsp), %rdi
leaq 0x370(%rsp), %rsi
leaq 0x1e8(%rsp), %rdx
xorl %ecx, %ecx
callq 0x9c550
jmp 0x16680
leaq 0x17ae89(%rip), %rax    # 0x191510
movsd (%rax), %xmm0
movsd 0x171e95(%rip), %xmm1   # 0x188528
xorl %eax, %eax
movl %eax, %r8d
leaq 0x3c8(%rsp), %rdi
leaq 0x200(%rsp), %rsi
leaq 0x230(%rsp), %rdx
movq %r8, %rcx
callq 0x25880
jmp 0x166ba
cmpl $0x0, 0x230(%rsp)
jne 0x166d5
movsd 0x171e5c(%rip), %xmm0   # 0x188528
movsd %xmm0, 0x268(%rsp)
movsd 0x2d8(%rsp), %xmm0
movsd %xmm0, 0x1e0(%rsp)
movsd 0x268(%rsp), %xmm1
movsd 0x1e0(%rsp), %xmm0
ucomisd %xmm1, %xmm0
jbe 0x16711
movsd 0x268(%rsp), %xmm0
movsd %xmm0, 0x1e0(%rsp)
movsd 0x1e0(%rsp), %xmm0
movsd 0x1338e6(%rip), %xmm1   # 0x14a008
ucomisd %xmm1, %xmm0
jne 0x1672f
jp 0x1672f
jmp 0x16bba
movsd 0x1e0(%rsp), %xmm0
ucomisd 0x171de8(%rip), %xmm0   # 0x188528
jbe 0x16753
movsd 0x171dde(%rip), %xmm0   # 0x188528
movsd %xmm0, 0x1e0(%rsp)
callq 0xeb5e0
movsd %xmm0, 0x30(%rsp)
jmp 0x16760
movsd 0x30(%rsp), %xmm0
movsd 0x171dca(%rip), %xmm1   # 0x188538
callq 0xf890
movaps %xmm0, %xmm1
movsd 0x1e0(%rsp), %xmm0
mulsd %xmm1, %xmm0
movsd %xmm0, 0x1e0(%rsp)
movsd 0x1e0(%rsp), %xmm0
leaq 0x1b0(%rsp), %rdi
leaq 0x358(%rsp), %rsi
callq 0x1b8f0
jmp 0x167ac
leaq 0x1c8(%rsp), %rdi
leaq 0x370(%rsp), %rsi
leaq 0x1b0(%rsp), %rdx
callq 0x9d220
jmp 0x167cb
leaq 0x17adc6(%rip), %rdi    # 0x191598
callq 0x1e010
movl %eax, 0x2c(%rsp)
jmp 0x167dd
movl 0x2c(%rsp), %eax
cmpl $0x0, %eax
jne 0x1680d
leaq 0x17adab(%rip), %rdi    # 0x191598
leaq 0x1c8(%rsp), %rsi
callq 0x37000
movl %eax, 0x28(%rsp)
jmp 0x16800
movl 0x28(%rsp), %eax
cmpl $0x0, %eax
je 0x1696c
movsd 0x13380b(%rip), %xmm0   # 0x14a020
movsd %xmm0, 0x1a8(%rsp)
cmpl $0x0, 0x17add3(%rip)    # 0x1915f8
jne 0x168a1
leaq 0x168(%rsp), %rdi
callq 0x1dff0
jmp 0x16836
movsd 0x171cea(%rip), %xmm1   # 0x188528
xorl %eax, %eax
movl %eax, %r8d
leaq 0x3c8(%rsp), %rdi
leaq 0x1c8(%rsp), %rsi
leaq 0x168(%rsp), %rdx
xorps %xmm0, %xmm0
movq %r8, %rcx
callq 0x22c30
jmp 0x16868
cmpl $0x0, 0x168(%rsp)
je 0x16883
movsd 0x1a0(%rsp), %xmm0
movsd %xmm0, 0x20(%rsp)
jmp 0x16892
movsd 0x1e0(%rsp), %xmm0
movsd %xmm0, 0x20(%rsp)
movsd 0x20(%rsp), %xmm0
movsd %xmm0, 0x1a8(%rsp)
movl $0x48, %edi
callq 0xf4d0
movq %rax, 0x18(%rsp)
jmp 0x168b2
movq 0x18(%rsp), %rdi
callq 0x1ba90
jmp 0x168be
movq 0x18(%rsp), %rax
movq %rax, 0x160(%rsp)
movq 0x3b0(%rsp), %rcx
movq 0x160(%rsp), %rax
movq %rcx, 0x8(%rax)
movq 0x160(%rsp), %rdi
addq $0x10, %rdi
leaq 0x1c8(%rsp), %rsi
callq 0x9d2f0
jmp 0x168fa
movq 0x160(%rsp), %rdi
addq $0x28, %rdi
leaq 0x358(%rsp), %rsi
callq 0x9ea80
jmp 0x16915
movsd 0x1a8(%rsp), %xmm0
movq 0x160(%rsp), %rax
movsd %xmm0, 0x40(%rax)
movq 0x3f0(%rsp), %rdi
movq 0x160(%rsp), %rsi
callq 0x1b540
jmp 0x16942
jmp 0x1696c
movq 0x18(%rsp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x3e8(%rsp)
movl %eax, 0x3e4(%rsp)
movl $0x48, %esi
callq 0xf4e0
jmp 0x16c12
movsd 0x1e0(%rsp), %xmm0
leaq 0x130(%rsp), %rdi
leaq 0x358(%rsp), %rsi
callq 0x1b8f0
jmp 0x1698c
leaq 0x148(%rsp), %rdi
leaq 0x370(%rsp), %rsi
leaq 0x130(%rsp), %rdx
callq 0x9dbc0
jmp 0x169ab
leaq 0x17abe6(%rip), %rdi    # 0x191598
callq 0x1e010
movl %eax, 0x14(%rsp)
jmp 0x169bd
movl 0x14(%rsp), %eax
cmpl $0x0, %eax
jne 0x169ed
leaq 0x17abcb(%rip), %rdi    # 0x191598
leaq 0x148(%rsp), %rsi
callq 0x37000
movl %eax, 0x10(%rsp)
jmp 0x169e0
movl 0x10(%rsp), %eax
cmpl $0x0, %eax
je 0x16bb8
movsd 0x133643(%rip), %xmm0   # 0x14a038
movsd %xmm0, 0x128(%rsp)
cmpl $0x0, 0x17abf3(%rip)    # 0x1915f8
jne 0x16ab3
leaq 0xe8(%rsp), %rdi
callq 0x1dff0
jmp 0x16a1a
movsd 0x171b06(%rip), %xmm1   # 0x188528
xorl %eax, %eax
movl %eax, %r8d
leaq 0x3c8(%rsp), %rdi
leaq 0x148(%rsp), %rsi
leaq 0xe8(%rsp), %rdx
xorps %xmm0, %xmm0
movq %r8, %rcx
callq 0x22c30
jmp 0x16a4c
cmpl $0x0, 0xe8(%rsp)
je 0x16a7e
movsd 0x120(%rsp), %xmm0
movq %xmm0, %rax
movabsq $-0x8000000000000000, %rcx # imm = 0x8000000000000000
xorq %rcx, %rax
movq %rax, %xmm0
movsd %xmm0, 0x8(%rsp)
jmp 0x16aa4
movsd 0x1e0(%rsp), %xmm0
movq %xmm0, %rax
movabsq $-0x8000000000000000, %rcx # imm = 0x8000000000000000
xorq %rcx, %rax
movq %rax, %xmm0
movsd %xmm0, 0x8(%rsp)
movsd 0x8(%rsp), %xmm0
movsd %xmm0, 0x128(%rsp)
leaq 0xa8(%rsp), %rdi
callq 0x1dff0
jmp 0x16ac2
movsd 0x171a5e(%rip), %xmm1   # 0x188528
xorl %eax, %eax
movl %eax, %r8d
leaq 0x3c8(%rsp), %rdi
leaq 0x148(%rsp), %rsi
leaq 0xa8(%rsp), %rdx
xorps %xmm0, %xmm0
movq %r8, %rcx
callq 0x22c30
jmp 0x16af4
movl $0x48, %edi
callq 0xf4d0
movq %rax, (%rsp)
jmp 0x16b04
movq (%rsp), %rdi
callq 0x1ba90
jmp 0x16b0f
movq (%rsp), %rax
movq %rax, 0xa0(%rsp)
movq 0x3b0(%rsp), %rcx
movq 0xa0(%rsp), %rax
movq %rcx, 0x8(%rax)
movq 0xa0(%rsp), %rdi
addq $0x10, %rdi
leaq 0x148(%rsp), %rsi
callq 0x9d2f0
jmp 0x16b4a
movq 0xa0(%rsp), %rdi
addq $0x28, %rdi
leaq 0x358(%rsp), %rsi
callq 0x9ea80
jmp 0x16b65
movsd 0x128(%rsp), %xmm0
movq 0xa0(%rsp), %rax
movsd %xmm0, 0x40(%rax)
movq 0x3f0(%rsp), %rdi
movq 0xa0(%rsp), %rsi
callq 0x1b540
jmp 0x16b92
jmp 0x16bb8
movq (%rsp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x3e8(%rsp)
movl %eax, 0x3e4(%rsp)
movl $0x48, %esi
callq 0xf4e0
jmp 0x16c12
jmp 0x16bba
movl 0x38c(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x38c(%rsp)
jmp 0x16394
jmp 0x16bd2
movl 0x3b8(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x3b8(%rsp)
jmp 0x1626b
movq 0x3f0(%rsp), %rax
movq %rax, 0x410(%rsp)
movl $0x1, 0x3bc(%rsp)
leaq 0x3c8(%rsp), %rdi
callq 0x1fa00
jmp 0x16c21
leaq 0x3c8(%rsp), %rdi
callq 0x1fa00
jmp 0x16c31
movq 0x410(%rsp), %rax
addq $0x418, %rsp            # imm = 0x418
retq
movq 0x3e8(%rsp), %rdi
callq 0xf8a0
nop