// File: /workspace/github_cpp_makefile_star2_O1/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, %r14d
movl %r8d, %r12d
movq %rcx, 0x50(%rsp)
movl %edx, %ebp
movq %rsi, 0x10(%rsp)
movq %rdi, %r13
leaq 0x90(%rsp), %r15
movq %r15, %rdi
callq 0x55804
movq $0x0, (%rsp)
movq %r15, %rdi
xorl %esi, %esi
callq 0xc5b0
movl %r12d, 0xc(%rsp)
testl %r12d, %r12d
jle 0x11535
movq $0x0, (%rsp)
cmpl $0x2, %r14d
movq %r13, 0x60(%rsp)
je 0x10dd0
cmpl $0x1, %r14d
je 0x10da7
testl %r14d, %r14d
jne 0x114f3
testl %ebp, %ebp
jle 0x110af
cmpq $0x0, 0x10(%rsp)
je 0x110ad
movl 0x34(%r13), %edx
cmpl %ebp, %edx
je 0x110af
leaq 0x48667(%rip), %rdi     # 0x59409
jmp 0x10e46
testl %ebp, %ebp
jle 0x10e5e
cmpq $0x0, 0x10(%rsp)
je 0x10e5c
movl 0x24(%r13), %edx
cmpl %ebp, %edx
je 0x10e5e
leaq 0x48673(%rip), %rdi     # 0x59441
jmp 0x10e46
movl 0x14(%r13), %edx
cmpl 0xc(%rsp), %edx
jge 0x10e23
movq 0x50(%rsp), %r14
cmpl $0x0, 0x14(%r13)
jle 0x114f3
movq $0x0, (%rsp)
movq 0x8(%r13), %rax
movq (%rsp), %rbx
movq (%rax,%rbx,8), %rsi
addq $0x18, %rsi
incq %rbx
movq %r14, %rdi
callq 0x42828
movslq 0x14(%r13), %rax
addq $0x18, %r14
movq %rbx, (%rsp)
cmpq %rax, %rbx
jl 0x10df2
jmp 0x114f3
testl %ebp, %ebp
jle 0x11343
cmpq $0x0, 0x10(%rsp)
je 0x11341
cmpl %ebp, %edx
je 0x11343
leaq 0x48633(%rip), %rdi     # 0x59479
movq $0x0, (%rsp)
movl %ebp, %esi
xorl %eax, %eax
callq 0x581d2
jmp 0x11535
xorl %ebp, %ebp
cmpl $0x0, 0x24(%r13)
movslq %ebp, %rbp
xorpd %xmm1, %xmm1
jle 0x10edd
xorl %r15d, %r15d
movq 0x10(%rsp), %r12
movq 0x18(%r13), %rax
movq (%rax,%r15,8), %r14
cmpq %rbp, %r15
jge 0x10e91
movsd 0x6dc57(%rip), %xmm0    # 0x7eae0
ucomisd (%r12,%r15,8), %xmm0
ja 0x10ecf
testb $0x2, 0x38(%r14)
jne 0x10eb4
movq (%r13), %rax
movq %r13, %rdi
movq %r14, %rsi
movsd %xmm1, 0x20(%rsp)
callq *0x2b8(%rax)
movsd 0x20(%rsp), %xmm1
movsd 0x28(%r14), %xmm0
cmpq %rbp, %r15
jge 0x10ec5
mulsd (%r12,%r15,8), %xmm0
movsd %xmm0, 0x40(%r14)
addsd %xmm0, %xmm1
incq %r15
movslq 0x24(%r13), %rax
cmpq %rax, %r15
jl 0x10e74
jmp 0x10ee2
movq 0x10(%rsp), %r12
movsd %xmm1, 0x20(%rsp)
xorpd %xmm0, %xmm0
callq 0x57da1
movsd 0x20(%rsp), %xmm1
cmpl $0x0, 0x24(%r13)
jle 0x114f3
movl 0xc(%rsp), %eax
xorps %xmm0, %xmm0
cvtsi2sd %eax, %xmm0
movsd %xmm0, 0x58(%rsp)
movl %eax, %eax
movq %rax, 0x30(%rsp)
xorl %edx, %edx
movq $0x0, (%rsp)
movq %rbp, 0x28(%rsp)
movq 0x18(%r13), %rax
movq (%rax,%rdx,8), %rax
cmpq %rbp, %rdx
jge 0x10f4f
movsd 0x6dba2(%rip), %xmm0    # 0x7eae0
movl $0x10, %ecx
ucomisd (%r12,%rdx,8), %xmm0
ja 0x1108f
movq %rdx, 0x68(%rsp)
movq 0x8(%rax), %r15
movsd 0x40(%rax), %xmm2
mulsd 0x58(%rsp), %xmm2
divsd %xmm1, %xmm2
cvttsd2si %xmm2, %ebp
cvttpd2dq %xmm2, %xmm0
movq 0x10(%rax), %rbx
cvtdq2pd %xmm0, %xmm0
subsd %xmm0, %xmm2
movapd %xmm2, 0x40(%rsp)
callq 0x57de4
xorl %eax, %eax
movapd 0x40(%rsp), %xmm1
ucomisd %xmm0, %xmm1
seta %al
addl %ebp, %eax
movl %eax, 0x18(%rsp)
jle 0x11060
addq $0x18, %r15
addq $0x18, %rbx
movq %rbx, 0x70(%rsp)
movslq (%rsp), %rax
movq %rax, 0x38(%rsp)
leaq (%rax,%rax,2), %rax
movq 0x50(%rsp), %rcx
leaq (%rcx,%rax,8), %r14
movl $0x1, %ebp
callq 0x57de4
movsd %xmm0, 0x40(%rsp)
leaq 0xb8(%rsp), %r12
movq %r12, %rdi
movq %r15, %rbx
movq %r15, %rsi
callq 0x42f04
movsd 0x48016(%rip), %xmm0    # 0x59008
subsd 0x40(%rsp), %xmm0
leaq 0xa0(%rsp), %r13
movq %r13, %rdi
movq 0x70(%rsp), %rsi
callq 0x42f04
leaq 0xd0(%rsp), %r15
movq %r15, %rdi
movq %r12, %rsi
movq %r13, %rdx
callq 0x42ebe
movq 0x38(%rsp), %rax
leaq (%rax,%rbp), %r12
movq %r14, %rdi
movq %r15, %rsi
callq 0x42828
movq (%rsp), %rax
incl %eax
movq %rax, (%rsp)
cmpq 0x30(%rsp), %r12
jge 0x11060
addq $0x18, %r14
leaq 0x1(%rbp), %rax
cmpl 0x18(%rsp), %ebp
movq %rax, %rbp
movq %rbx, %r15
jl 0x10fc9
movq (%rsp), %rax
cmpl 0xc(%rsp), %eax
movl $0xe, %ecx
movl $0x0, %eax
cmovll %eax, %ecx
movq 0x60(%rsp), %r13
movq 0x10(%rsp), %r12
movq 0x28(%rsp), %rbp
movsd 0x20(%rsp), %xmm1
movq 0x68(%rsp), %rdx
testb $0xf, %cl
jne 0x114f3
incq %rdx
movslq 0x24(%r13), %rax
cmpq %rax, %rdx
jl 0x10f29
jmp 0x114f3
xorl %ebp, %ebp
cmpl $0x0, 0x34(%r13)
jle 0x11123
movslq %ebp, %r15
xorpd %xmm1, %xmm1
xorl %r12d, %r12d
movq 0x10(%rsp), %rbp
movq 0x28(%r13), %rax
movq (%rax,%r12,8), %r14
testb $0x2, 0xa8(%r14)
jne 0x110f3
movq (%r13), %rax
movq %r13, %rdi
movq %r14, %rsi
movsd %xmm1, 0x28(%rsp)
callq *0x2c0(%rax)
movsd 0x28(%rsp), %xmm1
movsd 0x90(%r14), %xmm0
cmpq %r15, %r12
jge 0x11108
mulsd (%rbp,%r12,8), %xmm0
movsd %xmm0, 0xb0(%r14)
addsd %xmm0, %xmm1
incq %r12
movslq 0x34(%r13), %rax
cmpq %rax, %r12
jl 0x110c5
jmp 0x11127
xorpd %xmm1, %xmm1
movsd %xmm1, 0x28(%rsp)
xorpd %xmm0, %xmm0
callq 0x57da1
cmpl $0x0, 0x34(%r13)
jle 0x114f3
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), %r15
movq 0x10(%rax), %r12
movsd 0xb0(%rax), %xmm1
mulsd 0x88(%rsp), %xmm1
divsd 0x28(%rsp), %xmm1
cvttsd2si %xmm1, %r14d
cvttpd2dq %xmm1, %xmm0
movq 0x18(%rax), %r13
cvtdq2pd %xmm0, %xmm0
subsd %xmm0, %xmm1
movapd %xmm1, 0x40(%rsp)
callq 0x57de4
xorl %eax, %eax
movapd 0x40(%rsp), %xmm1
ucomisd %xmm0, %xmm1
seta %al
addl %r14d, %eax
movl %eax, 0x38(%rsp)
jle 0x11314
addq $0x18, %r15
addq $0x18, %r12
movq %r12, 0x30(%rsp)
addq $0x18, %r13
movq %r13, 0x20(%rsp)
movslq (%rsp), %rax
movq %rax, 0x10(%rsp)
leaq (%rax,%rax,2), %rax
movq 0x50(%rsp), %rcx
leaq (%rcx,%rax,8), %r14
movl $0x1, %r12d
callq 0x57de4
ucomisd 0x47e23(%rip), %xmm0    # 0x59030
movq %r12, 0x40(%rsp)
jb 0x1121a
sqrtsd %xmm0, %xmm0
jmp 0x1121f
callq 0xc2b0
movsd %xmm0, 0x18(%rsp)
callq 0x57de4
movapd %xmm0, %xmm2
movsd 0x47dd2(%rip), %xmm1    # 0x59008
movapd %xmm1, %xmm0
movsd 0x18(%rsp), %xmm3
subsd %xmm3, %xmm0
subsd %xmm2, %xmm1
mulsd %xmm3, %xmm1
movsd %xmm1, 0x70(%rsp)
mulsd %xmm3, %xmm2
movsd %xmm2, 0x18(%rsp)
leaq 0xa0(%rsp), %rbp
movq %rbp, %rdi
movq %r15, %r13
movq %r15, %rsi
callq 0x42f04
leaq 0x100(%rsp), %r12
movq %r12, %rdi
movq 0x30(%rsp), %rsi
movsd 0x70(%rsp), %xmm0
callq 0x42f04
leaq 0xb8(%rsp), %r15
movq %r15, %rdi
movq %rbp, %rsi
movq %r12, %rdx
callq 0x42ebe
leaq 0xe8(%rsp), %r12
movq %r12, %rdi
movq 0x20(%rsp), %rsi
movsd 0x18(%rsp), %xmm0
callq 0x42f04
leaq 0xd0(%rsp), %rbx
movq %rbx, %rdi
movq %r15, %rsi
movq %r12, %rdx
callq 0x42ebe
movq 0x10(%rsp), %rax
movq 0x40(%rsp), %r15
leaq (%rax,%r15), %rbp
movq %r14, %rdi
movq %rbx, %rsi
callq 0x42828
movq (%rsp), %rax
incl %eax
movq %rax, (%rsp)
cmpq 0x68(%rsp), %rbp
jge 0x11314
addq $0x18, %r14
leaq 0x1(%r15), %r12
cmpl 0x38(%rsp), %r15d
movq %r13, %r15
jl 0x11200
movq (%rsp), %rax
cmpl 0xc(%rsp), %eax
jge 0x114f3
movq 0x58(%rsp), %rcx
incq %rcx
movq 0x60(%rsp), %r13
movslq 0x34(%r13), %rax
cmpq %rax, %rcx
jl 0x11166
jmp 0x114f3
xorl %ebp, %ebp
movl 0x14(%r13), %eax
xorpd %xmm0, %xmm0
xorpd %xmm1, %xmm1
testl %eax, %eax
jle 0x113a8
movslq %ebp, %r15
xorl %r12d, %r12d
movq 0x10(%rsp), %rbp
movq 0x8(%r13), %rax
movq (%rax,%r12,8), %r14
cmpq %r15, %r12
jge 0x11374
movsd (%rbp,%r12,8), %xmm0
jmp 0x1138b
movq %r13, %rdi
movq %r14, %rsi
movsd %xmm1, 0x18(%rsp)
callq 0x23c36
movsd 0x18(%rsp), %xmm1
movsd %xmm0, 0x80(%r14)
addsd %xmm0, %xmm1
incq %r12
movslq 0x14(%r13), %rax
cmpq %rax, %r12
jl 0x1135e
xorpd %xmm0, %xmm0
ucomisd %xmm0, %xmm1
setp %cl
setne %dl
orb %cl, %dl
testl %eax, %eax
setle %cl
orb %dl, %cl
jne 0x113ee
movq 0x8(%r13), %rcx
movl %eax, %eax
xorl %edx, %edx
movabsq $0x3ff0000000000000, %rsi # imm = 0x3FF0000000000000
movsd 0x47c31(%rip), %xmm0    # 0x59008
movq (%rcx,%rdx,8), %rdi
movq %rsi, 0x80(%rdi)
addsd %xmm0, %xmm1
incq %rdx
cmpq %rdx, %rax
jne 0x113d7
movsd %xmm1, 0x18(%rsp)
xorpd %xmm0, %xmm0
callq 0x57da1
cmpl $0x0, 0x14(%r13)
jle 0x114f3
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), %rbx
movsd 0x80(%rbx), %xmm1
mulsd 0x30(%rsp), %xmm1
divsd 0x18(%rsp), %xmm1
cvttsd2si %xmm1, %r15d
cvttpd2dq %xmm1, %xmm0
cvtdq2pd %xmm0, %xmm0
subsd %xmm0, %xmm1
movapd %xmm1, 0x70(%rsp)
callq 0x57de4
xorl %ebp, %ebp
movapd 0x70(%rsp), %xmm1
ucomisd %xmm0, %xmm1
seta %bpl
addl %r15d, %ebp
jle 0x114cf
addq $0x18, %rbx
movslq (%rsp), %r13
leaq (,%r13,2), %rax
addq %r13, %rax
movq 0x50(%rsp), %rcx
leaq (%rcx,%rax,8), %r15
movl $0x1, %r12d
leaq (%r12,%r13), %r14
movq %r15, %rdi
movq %rbx, %rsi
callq 0x42828
movq (%rsp), %rax
incl %eax
movq %rax, (%rsp)
cmpq 0x40(%rsp), %r14
jge 0x114cf
addq $0x18, %r15
leaq 0x1(%r12), %rax
cmpl %ebp, %r12d
movq %rax, %r12
jl 0x1149e
movq (%rsp), %rax
cmpl 0xc(%rsp), %eax
jge 0x114f3
movq 0x38(%rsp), %rcx
incq %rcx
movq 0x60(%rsp), %r13
movslq 0x14(%r13), %rax
cmpq %rax, %rcx
jl 0x1142a
cmpb $0x1, 0x6d5e2(%rip)     # 0x7eadc
jne 0x11535
leaq 0x4804b(%rip), %rdi     # 0x5954e
callq 0xc560
leaq 0x90(%rsp), %rdi
callq 0x55866
leaq 0x47cdc(%rip), %rdi     # 0x591f8
movb $0x1, %al
callq 0xc070
leaq 0x47f8a(%rip), %rdi     # 0x594b4
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