// File: /workspace/github_cpp_makefile_star2_O3/tomfunkhouser[P]gaps/apps/mshalign/mshalign.cpp
// Function: CreatePoints(gaps::R3Mesh*, double*, int, gaps::R3Point*, int, int)
static int
CreatePoints(R3Mesh *mesh, RNScalar *weights, int nweights, R3Point *points, int max_points, int sample_method)
{
  // Start statistics
  RNTime start_time;
  start_time.Read();

  // Initialize return value
  int npoints = 0;

  // Check maximum number of points
  if (max_points <= 0) return 0;

  // Check sample method
  if (sample_method == 0) { 
    // Sample faces

    // Check weights
    if (nweights > 0) {
      if (weights == NULL) nweights = 0;
      else if (nweights != mesh->NFaces()) {
        RNFail("Invalid number of weights (%d) for sampling faces (%d)\n", nweights, mesh->NFaces());
        return 0;
      }
    }

    // Count total weighted area of faces
    RNArea total_area = 0.0;
    for (int i = 0; i < mesh->NFaces(); i++) {
      R3MeshFace *face = mesh->Face(i);
      RNScalar face_area = mesh->FaceArea(face);
      if (nweights > i) face_area *= weights[i];
      mesh->SetFaceValue(face, face_area);
      total_area += face_area;
    }

    // Generate points with a uniform distribution over surface area
    RNSeedRandomScalar();
    for (int i = 0; i < mesh->NFaces(); i++) {
      R3MeshFace *face = mesh->Face(i);

      // Get vertex positions
      R3MeshVertex *v0 = mesh->VertexOnFace(face, 0);
      R3MeshVertex *v1 = mesh->VertexOnFace(face, 1);
      R3MeshVertex *v2 = mesh->VertexOnFace(face, 2);
      const R3Point& p0 = mesh->VertexPosition(v0);
      const R3Point& p1 = mesh->VertexPosition(v1);
      const R3Point& p2 = mesh->VertexPosition(v2);

      // Determine number of points for face 
      RNScalar ideal_face_npoints = max_points * mesh->FaceValue(face) / total_area;
      int face_npoints = (int) ideal_face_npoints;
      RNScalar remainder = ideal_face_npoints - face_npoints;
      if (remainder > RNRandomScalar()) face_npoints++;

      // Generate random points in face
      for (int j = 0; j < face_npoints; j++) {
        RNScalar r1 = sqrt(RNRandomScalar());
        RNScalar r2 = RNRandomScalar();
        RNScalar t0 = (1.0 - r1);
        RNScalar t1 = r1 * (1.0 - r2);
        RNScalar t2 = r1 * r2;
        points[npoints++] = t0*p0 + t1*p1 + t2*p2;
        if (npoints >= max_points) break;
      }

      // Check number of points created
      if (npoints >= max_points) break;
    }
  }
  else if (sample_method == 1) {
    // Sample edges

    // Check weights
    if (nweights > 0) {
      if (weights == NULL) nweights = 0;
      else if (nweights != mesh->NEdges()) {
        RNFail("Invalid number of weights (%d) for sampling edges (%d)\n", nweights, mesh->NEdges());
        return 0;
      }
    }

    // Count total weighted length of edges
    RNLength total_length = 0.0;
    for (int i = 0; i < mesh->NEdges(); i++) {
      R3MeshEdge *edge = mesh->Edge(i);
      if ((nweights > i) && (weights[i] < min_weight)) continue;
      RNScalar edge_length = mesh->EdgeLength(edge);
      if (nweights > i) edge_length *= weights[i];
      mesh->SetEdgeValue(edge, edge_length);
      total_length += edge_length;
    }
    
    // Generate points with a uniform distribution over edge length
    RNSeedRandomScalar();
    for (int i = 0; i < mesh->NEdges(); i++) {
      R3MeshEdge *edge = mesh->Edge(i);
      if ((nweights > i) && (weights[i] < min_weight)) continue;

      // Get vertex positions
      R3MeshVertex *v0 = mesh->VertexOnEdge(edge, 0);
      R3MeshVertex *v1 = mesh->VertexOnEdge(edge, 1);
      const R3Point& p0 = mesh->VertexPosition(v0);
      const R3Point& p1 = mesh->VertexPosition(v1);

      // Determine number of points for edge 
      RNScalar ideal_edge_npoints = max_points * mesh->EdgeValue(edge) / total_length;
      int edge_npoints = (int) ideal_edge_npoints;
      RNScalar remainder = ideal_edge_npoints - edge_npoints;
      if (remainder > RNRandomScalar()) edge_npoints++;

      // Generate random points in edge
      for (int j = 0; j < edge_npoints; j++) {
        RNScalar t = RNRandomScalar();
        points[npoints++] = t*p0 + (1-t)*p1;
        if (npoints >= max_points) break;
      }

      // Check number of points created
      if (npoints >= max_points) break;
    }
  }
  else if (sample_method == 2) {
    // Sample vertices

    // Check if number of vertices is less than max_points
    if (max_points > mesh->NVertices()) {
      for (int i = 0; i < mesh->NVertices(); i++) {
        R3MeshVertex *vertex = mesh->Vertex(i);
        points[npoints++] = mesh->VertexPosition(vertex);
      }
    }
    else {
      // Check weights
      if (nweights > 0) {
        if (weights == NULL) nweights = 0;
        else if (nweights != mesh->NVertices()) {
          RNFail("Invalid number of weights (%d) for sampling vertices (%d)\n", nweights, mesh->NVertices());
          return 0;
        }
      }

      // Count total weight 
      RNScalar total_weight = 0.0;
      for (int i = 0; i < mesh->NVertices(); i++) {
        R3MeshVertex *vertex = mesh->Vertex(i);
        RNScalar vertex_weight = (nweights > i) ? weights[i] : mesh->VertexArea(vertex);
        mesh->SetVertexValue(vertex, vertex_weight);
        total_weight += vertex_weight;
      }

      // Check total weight (could be zero if there are vertices, but no weights or faces)
      if (total_weight == 0) {
        for (int i = 0; i < mesh->NVertices(); i++) {
          R3MeshVertex *vertex = mesh->Vertex(i);
          mesh->SetVertexValue(vertex, 1);
          total_weight += 1;
        }
      }

      // Generate points with probability related to vertex weights/areas
      RNSeedRandomScalar();
      for (int i = 0; i < mesh->NVertices(); i++) {
        R3MeshVertex *vertex = mesh->Vertex(i);
        const R3Point& position = mesh->VertexPosition(vertex);

        // Determine number of points for vertex
        RNScalar ideal_vertex_npoints = max_points * mesh->VertexValue(vertex) / total_weight;
        int vertex_npoints = (int) ideal_vertex_npoints;
        RNScalar remainder = ideal_vertex_npoints - vertex_npoints;
        if (remainder > RNRandomScalar()) vertex_npoints++;

        // Generate points at vertex
        for (int j = 0; j < vertex_npoints; j++) {
          points[npoints++] = position;
          if (npoints >= max_points) break;
        }

        // Check number of points created
        if (npoints >= max_points) break;
      }
    }
  }

#if 0
  if (1) {
    static int file_count = 1;
    char filename[256];
    sprintf(filename, "points%d.pts", file_count++);
    FILE *fp = fopen(filename, "wb");
    for (int i = 0; i < npoints; i++) {
      R3Point position = points[i];
      static float coordinates[6] = { 0 };
      coordinates[0] = points[i].X();
      coordinates[1] = points[i].Y();
      coordinates[2] = points[i].Z();
      fwrite(coordinates, sizeof(float), 6, fp);
    }
    fclose(fp);
  }
#endif

  // Print message
  if (print_debug) {
    printf("Created points ...\n");
    printf("  Time = %.2f seconds\n", start_time.Elapsed());
    printf("  # Points = %d\n", npoints);
  }

  // Return number of points actually created
  return npoints;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x118, %rsp            # imm = 0x118
movl %r9d, %ebp
movl %r8d, %r12d
movq %rcx, 0x50(%rsp)
movl %edx, %ebx
movq %rsi, 0x18(%rsp)
movq %rdi, %r13
leaq 0x90(%rsp), %r14
movq %r14, %rdi
callq 0x54c8c
movq $0x0, (%rsp)
movq %r14, %rdi
xorl %esi, %esi
callq 0xc5b0
movl %r12d, 0xc(%rsp)
testl %r12d, %r12d
jle 0x11579
movq $0x0, (%rsp)
cmpl $0x2, %ebp
movq %r13, 0x60(%rsp)
je 0x10e2f
cmpl $0x1, %ebp
je 0x10e04
testl %ebp, %ebp
jne 0x11537
testl %ebx, %ebx
jle 0x10ec1
movq 0x18(%rsp), %rbp
testq %rbp, %rbp
je 0x11100
movl 0x34(%r13), %edx
cmpl %ebx, %edx
je 0x11102
leaq 0x47607(%rip), %rdi     # 0x58406
jmp 0x10ea4
testl %ebx, %ebx
jle 0x10eba
movq 0x18(%rsp), %r12
testq %r12, %r12
je 0x10ecb
movl 0x24(%r13), %edx
cmpl %ebx, %edx
je 0x10ecd
leaq 0x47611(%rip), %rdi     # 0x5843e
jmp 0x10ea4
movl 0x14(%r13), %edx
cmpl 0xc(%rsp), %edx
movq 0x18(%rsp), %r12
jge 0x10e84
testl %edx, %edx
jle 0x11537
movq $0x0, (%rsp)
movq 0x50(%rsp), %rbx
movq 0x8(%r13), %rax
movq (%rsp), %r14
movq (%rax,%r14,8), %rsi
addq $0x18, %rsi
incq %r14
movq %rbx, %rdi
callq 0x41efa
movslq 0x14(%r13), %rax
addq $0x18, %rbx
movq %r14, (%rsp)
cmpq %rax, %r14
jl 0x10e53
jmp 0x11537
testl %ebx, %ebx
jle 0x1138c
testq %r12, %r12
je 0x1138a
cmpl %ebx, %edx
je 0x1138c
leaq 0x475d2(%rip), %rdi     # 0x58476
movq $0x0, (%rsp)
movl %ebx, %esi
xorl %eax, %eax
callq 0x573ee
jmp 0x11579
movq 0x18(%rsp), %r12
jmp 0x10ecd
movq 0x18(%rsp), %rbp
jmp 0x11102
xorl %ebx, %ebx
movl 0x24(%r13), %eax
movslq %ebx, %rbp
xorpd %xmm1, %xmm1
testl %eax, %eax
jle 0x10f46
xorl %r14d, %r14d
movq 0x18(%r13), %rcx
movq (%rcx,%r14,8), %rbx
cmpq %rbp, %r14
jge 0x10efc
movsd 0x6cbec(%rip), %xmm0    # 0x7dae0
ucomisd (%r12,%r14,8), %xmm0
ja 0x10f3b
testb $0x2, 0x38(%rbx)
jne 0x10f1e
movq (%r13), %rax
movq %r13, %rdi
movq %rbx, %rsi
movsd %xmm1, 0x20(%rsp)
callq *0x2b8(%rax)
movsd 0x20(%rsp), %xmm1
movsd 0x28(%rbx), %xmm0
cmpq %rbp, %r14
jge 0x10f2e
mulsd (%r12,%r14,8), %xmm0
movsd %xmm0, 0x40(%rbx)
addsd %xmm0, %xmm1
movl 0x24(%r13), %eax
incq %r14
movslq %eax, %rcx
cmpq %rcx, %r14
jl 0x10edf
movsd %xmm1, 0x20(%rsp)
xorpd %xmm0, %xmm0
callq 0x56fc1
movsd 0x20(%rsp), %xmm1
movl 0x24(%r13), %eax
testl %eax, %eax
jle 0x11537
movl 0xc(%rsp), %ecx
xorps %xmm0, %xmm0
cvtsi2sd %ecx, %xmm0
movsd %xmm0, 0x28(%rsp)
movl %ecx, %ecx
movq %rcx, 0x30(%rsp)
xorl %edx, %edx
movq $0x0, (%rsp)
movq %rbp, 0x58(%rsp)
movq 0x18(%r13), %rcx
movq (%rcx,%rdx,8), %rcx
cmpq %rbp, %rdx
jge 0x10faf
movsd 0x6cb3d(%rip), %xmm0    # 0x7dae0
ucomisd (%r12,%rdx,8), %xmm0
ja 0x110ec
movq %rdx, 0x68(%rsp)
movq 0x8(%rcx), %r14
movsd 0x40(%rcx), %xmm2
mulsd 0x28(%rsp), %xmm2
divsd %xmm1, %xmm2
cvttsd2si %xmm2, %ebx
cvttpd2dq %xmm2, %xmm0
movq 0x10(%rcx), %r15
cvtdq2pd %xmm0, %xmm0
subsd %xmm0, %xmm2
movapd %xmm2, 0x40(%rsp)
callq 0x57004
xorl %eax, %eax
movapd 0x40(%rsp), %xmm1
ucomisd %xmm0, %xmm1
seta %al
addl %ebx, %eax
movl %eax, 0x10(%rsp)
jle 0x110c0
addq $0x18, %r14
addq $0x18, %r15
movq %r15, 0x70(%rsp)
movslq (%rsp), %rax
movq %rax, 0x38(%rsp)
leaq (%rax,%rax,2), %rax
movq 0x50(%rsp), %rcx
leaq (%rcx,%rax,8), %rbx
movl $0x1, %ebp
callq 0x57004
movsd %xmm0, 0x40(%rsp)
leaq 0xb8(%rsp), %r12
movq %r12, %rdi
movq %r14, %r15
movq %r14, %rsi
callq 0x425d6
movsd 0x46fb6(%rip), %xmm0    # 0x58008
subsd 0x40(%rsp), %xmm0
leaq 0xa0(%rsp), %r13
movq %r13, %rdi
movq 0x70(%rsp), %rsi
callq 0x425d6
leaq 0xd0(%rsp), %r14
movq %r14, %rdi
movq %r12, %rsi
movq %r13, %rdx
callq 0x42590
movq 0x38(%rsp), %rax
leaq (%rax,%rbp), %r12
movq %rbx, %rdi
movq %r14, %rsi
callq 0x41efa
movq (%rsp), %rax
incl %eax
movq %rax, (%rsp)
cmpq 0x30(%rsp), %r12
jge 0x110c0
addq $0x18, %rbx
leaq 0x1(%rbp), %rax
cmpl 0x10(%rsp), %ebp
movq %rax, %rbp
movq %r15, %r14
jl 0x11029
movq (%rsp), %rax
cmpl 0xc(%rsp), %eax
jge 0x11537
movq 0x60(%rsp), %r13
movl 0x24(%r13), %eax
movq 0x18(%rsp), %r12
movq 0x58(%rsp), %rbp
movsd 0x20(%rsp), %xmm1
movq 0x68(%rsp), %rdx
incq %rdx
movslq %eax, %rcx
cmpq %rcx, %rdx
jl 0x10f8e
jmp 0x11537
xorl %ebx, %ebx
cmpl $0x0, 0x34(%r13)
jle 0x1116e
movslq %ebx, %r14
xorpd %xmm1, %xmm1
xorl %r12d, %r12d
movq 0x28(%r13), %rax
movq (%rax,%r12,8), %rbx
testb $0x2, 0xa8(%rbx)
jne 0x11140
movq (%r13), %rax
movq %r13, %rdi
movq %rbx, %rsi
movsd %xmm1, 0x28(%rsp)
callq *0x2c0(%rax)
movsd 0x28(%rsp), %xmm1
movsd 0x90(%rbx), %xmm0
cmpq %r14, %r12
jge 0x11154
mulsd (%rbp,%r12,8), %xmm0
movsd %xmm0, 0xb0(%rbx)
addsd %xmm0, %xmm1
incq %r12
movslq 0x34(%r13), %rax
cmpq %rax, %r12
jl 0x11113
jmp 0x11172
xorpd %xmm1, %xmm1
movsd %xmm1, 0x28(%rsp)
xorpd %xmm0, %xmm0
callq 0x56fc1
cmpl $0x0, 0x34(%r13)
jle 0x11537
movl 0xc(%rsp), %eax
xorps %xmm0, %xmm0
cvtsi2sd %eax, %xmm0
movsd %xmm0, 0x88(%rsp)
movl %eax, %eax
movq %rax, 0x68(%rsp)
xorl %ecx, %ecx
movq $0x0, (%rsp)
movq 0x28(%r13), %rax
movq %rcx, 0x58(%rsp)
movq (%rax,%rcx,8), %rax
movq 0x8(%rax), %r14
movq 0x10(%rax), %r12
movsd 0xb0(%rax), %xmm1
mulsd 0x88(%rsp), %xmm1
divsd 0x28(%rsp), %xmm1
cvttsd2si %xmm1, %ebx
cvttpd2dq %xmm1, %xmm0
movq 0x18(%rax), %r13
cvtdq2pd %xmm0, %xmm0
subsd %xmm0, %xmm1
movapd %xmm1, 0x40(%rsp)
callq 0x57004
xorl %eax, %eax
movapd 0x40(%rsp), %xmm1
ucomisd %xmm0, %xmm1
seta %al
addl %ebx, %eax
movl %eax, 0x38(%rsp)
jle 0x1135d
addq $0x18, %r14
addq $0x18, %r12
movq %r12, 0x30(%rsp)
addq $0x18, %r13
movq %r13, 0x20(%rsp)
movslq (%rsp), %rax
movq %rax, 0x18(%rsp)
leaq (%rax,%rax,2), %rax
movq 0x50(%rsp), %rcx
leaq (%rcx,%rax,8), %rbx
movl $0x1, %r12d
callq 0x57004
ucomisd 0x46dda(%rip), %xmm0    # 0x58030
movq %r12, 0x40(%rsp)
jb 0x11263
sqrtsd %xmm0, %xmm0
jmp 0x11268
callq 0xc2b0
movsd %xmm0, 0x10(%rsp)
callq 0x57004
movapd %xmm0, %xmm2
movsd 0x46d89(%rip), %xmm1    # 0x58008
movapd %xmm1, %xmm0
movsd 0x10(%rsp), %xmm3
subsd %xmm3, %xmm0
subsd %xmm2, %xmm1
mulsd %xmm3, %xmm1
movsd %xmm1, 0x70(%rsp)
mulsd %xmm3, %xmm2
movsd %xmm2, 0x10(%rsp)
leaq 0xa0(%rsp), %rbp
movq %rbp, %rdi
movq %r14, %r13
movq %r14, %rsi
callq 0x425d6
leaq 0x100(%rsp), %r12
movq %r12, %rdi
movq 0x30(%rsp), %rsi
movsd 0x70(%rsp), %xmm0
callq 0x425d6
leaq 0xb8(%rsp), %r14
movq %r14, %rdi
movq %rbp, %rsi
movq %r12, %rdx
callq 0x42590
leaq 0xe8(%rsp), %r12
movq %r12, %rdi
movq 0x20(%rsp), %rsi
movsd 0x10(%rsp), %xmm0
callq 0x425d6
leaq 0xd0(%rsp), %r15
movq %r15, %rdi
movq %r14, %rsi
movq %r12, %rdx
callq 0x42590
movq 0x18(%rsp), %rax
movq 0x40(%rsp), %r14
leaq (%rax,%r14), %rbp
movq %rbx, %rdi
movq %r15, %rsi
callq 0x41efa
movq (%rsp), %rax
incl %eax
movq %rax, (%rsp)
cmpq 0x68(%rsp), %rbp
jge 0x1135d
addq $0x18, %rbx
leaq 0x1(%r14), %r12
cmpl 0x38(%rsp), %r14d
movq %r13, %r14
jl 0x11249
movq (%rsp), %rax
cmpl 0xc(%rsp), %eax
jge 0x11537
movq 0x58(%rsp), %rcx
incq %rcx
movq 0x60(%rsp), %r13
movslq 0x34(%r13), %rax
cmpq %rax, %rcx
jl 0x111b1
jmp 0x11537
xorl %ebx, %ebx
movslq %ebx, %r14
xorpd %xmm1, %xmm1
xorl %r15d, %r15d
movq 0x8(%r13), %rax
movq (%rax,%r15,8), %rbx
cmpq %r14, %r15
jge 0x113ab
movsd (%r12,%r15,8), %xmm0
jmp 0x113c6
movq %r13, %rdi
movq %rbx, %rsi
movsd %xmm1, 0x10(%rsp)
callq 0x2367e
movsd 0x10(%rsp), %xmm1
movl 0x14(%r13), %edx
movsd %xmm0, 0x80(%rbx)
addsd %xmm0, %xmm1
incq %r15
movslq %edx, %rax
cmpq %rax, %r15
jl 0x11396
xorpd %xmm0, %xmm0
ucomisd %xmm0, %xmm1
setp %al
setne %cl
orb %al, %cl
testl %edx, %edx
setle %al
orb %cl, %al
jne 0x11427
movq 0x8(%r13), %rax
movl %edx, %ecx
xorl %edx, %edx
movabsq $0x3ff0000000000000, %rsi # imm = 0x3FF0000000000000
movsd 0x46bf8(%rip), %xmm0    # 0x58008
movq (%rax,%rdx,8), %rdi
movq %rsi, 0x80(%rdi)
addsd %xmm0, %xmm1
incq %rdx
cmpq %rdx, %rcx
jne 0x11410
movsd %xmm1, 0x10(%rsp)
xorpd %xmm0, %xmm0
callq 0x56fc1
cmpl $0x0, 0x14(%r13)
jle 0x1152f
movl 0xc(%rsp), %eax
xorps %xmm0, %xmm0
cvtsi2sd %eax, %xmm0
movsd %xmm0, 0x30(%rsp)
movl %eax, %eax
movq %rax, 0x40(%rsp)
xorl %ecx, %ecx
movq $0x0, (%rsp)
movq 0x8(%r13), %rax
movq %rcx, 0x38(%rsp)
movq (%rax,%rcx,8), %r15
movsd 0x80(%r15), %xmm1
mulsd 0x30(%rsp), %xmm1
divsd 0x10(%rsp), %xmm1
cvttsd2si %xmm1, %r14d
cvttpd2dq %xmm1, %xmm0
cvtdq2pd %xmm0, %xmm0
subsd %xmm0, %xmm1
movapd %xmm1, 0x70(%rsp)
callq 0x57004
xorl %ebp, %ebp
movapd 0x70(%rsp), %xmm1
ucomisd %xmm0, %xmm1
seta %bpl
addl %r14d, %ebp
jle 0x11509
addq $0x18, %r15
movslq (%rsp), %r13
leaq (,%r13,2), %rax
addq %r13, %rax
movq 0x50(%rsp), %rcx
leaq (%rcx,%rax,8), %r14
movl $0x1, %r12d
leaq (%r12,%r13), %rbx
movq %r14, %rdi
movq %r15, %rsi
callq 0x41efa
movq (%rsp), %rax
incl %eax
movq %rax, (%rsp)
cmpq 0x40(%rsp), %rbx
jge 0x11509
addq $0x18, %r14
leaq 0x1(%r12), %rax
cmpl %ebp, %r12d
movq %rax, %r12
jl 0x114d8
movq (%rsp), %rax
cmpl 0xc(%rsp), %eax
jge 0x11537
movq 0x38(%rsp), %rcx
incq %rcx
movq 0x60(%rsp), %r13
movslq 0x14(%r13), %rax
cmpq %rax, %rcx
jl 0x11463
jmp 0x11537
movq $0x0, (%rsp)
cmpb $0x1, 0x6c59e(%rip)     # 0x7dadc
jne 0x11579
leaq 0x47004(%rip), %rdi     # 0x5854b
callq 0xc560
leaq 0x90(%rsp), %rdi
callq 0x54cee
leaq 0x46c95(%rip), %rdi     # 0x581f5
movb $0x1, %al
callq 0xc070
leaq 0x46f43(%rip), %rdi     # 0x584b1
movq (%rsp), %rsi
xorl %eax, %eax
callq 0xc070
movq (%rsp), %rax
addq $0x118, %rsp            # imm = 0x118
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq