// File: /workspace/github_cpp_makefile_star2_O2/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 $0xf8, %rsp
movl %r9d, %ebp
movl %r8d, %r15d
movq %rcx, %r12
movl %edx, %ebx
movq %rsi, %r13
movq %rdi, 0x8(%rsp)
leaq 0x70(%rsp), %r14
movq %r14, %rdi
callq 0x4dc92
movq %r14, %rdi
callq 0x10fda
movl %r15d, 0x4(%rsp)
testl %r15d, %r15d
jle 0x100c5
cmpl $0x2, %ebp
je 0x10106
cmpl $0x1, %ebp
movq %r12, 0x38(%rsp)
je 0x100cd
xorl %r15d, %r15d
testl %ebp, %ebp
jne 0x106f3
testl %ebx, %ebx
jle 0x10176
testq %r13, %r13
movq 0x8(%rsp), %r12
je 0x10357
movl 0x34(%r12), %edx
cmpl %ebx, %edx
je 0x10359
leaq 0x41343(%rip), %rdi     # 0x51406
jmp 0x100f5
xorl %r15d, %r15d
jmp 0x10731
testl %ebx, %ebx
jle 0x1016f
testq %r13, %r13
movq 0x8(%rsp), %rbp
je 0x10180
movl 0x24(%rbp), %edx
cmpl %ebx, %edx
je 0x10182
leaq 0x41349(%rip), %rdi     # 0x5143e
xorl %r15d, %r15d
movl %ebx, %esi
xorl %eax, %eax
callq 0x5009a
jmp 0x10731
movq %r13, %rbp
movq 0x8(%rsp), %r13
movl 0x14(%r13), %edx
cmpl 0x4(%rsp), %edx
jge 0x10148
xorl %r15d, %r15d
movslq %edx, %rax
cmpq %rax, %r15
jge 0x106f3
movq 0x8(%r13), %rax
movq (%rax,%r15,8), %rsi
addq $0x18, %rsi
incq %r15
movq %r12, %rdi
callq 0x3cb0a
movl 0x14(%r13), %edx
addq $0x18, %r12
jmp 0x1011b
testl %ebx, %ebx
jle 0x10583
testq %rbp, %rbp
je 0x10581
cmpl %ebx, %edx
je 0x10583
leaq 0x4130e(%rip), %rdi     # 0x51476
xorl %r15d, %r15d
movl %ebx, %esi
jmp 0x100fa
movq 0x8(%rsp), %rbp
jmp 0x10182
movq 0x8(%rsp), %r12
jmp 0x10359
xorl %ebx, %ebx
movslq %ebx, %r15
xorpd %xmm0, %xmm0
movsd %xmm0, 0x28(%rsp)
xorl %r14d, %r14d
movslq 0x24(%rbp), %rax
cmpq %rax, %r14
jge 0x101ea
movq 0x18(%rbp), %rax
movq (%rax,%r14,8), %rbx
cmpq %r15, %r14
jge 0x101b9
movsd 0x68940(%rip), %xmm0    # 0x78af0
ucomisd (%r13,%r14,8), %xmm0
ja 0x101e5
movq %rbp, %rdi
movq %rbx, %rsi
callq 0x11122
cmpq %r15, %r14
jge 0x101d0
mulsd (%r13,%r14,8), %xmm0
movsd %xmm0, 0x40(%rbx)
movsd 0x28(%rsp), %xmm1
addsd %xmm0, %xmm1
movsd %xmm1, 0x28(%rsp)
incq %r14
jmp 0x10192
movq %r15, 0x20(%rsp)
movq %r13, 0x40(%rsp)
xorpd %xmm0, %xmm0
callq 0x4fca3
movl 0x4(%rsp), %eax
cvtsi2sd %eax, %xmm0
movsd %xmm0, 0x48(%rsp)
movl %eax, %eax
movq %rax, 0x58(%rsp)
xorl %edx, %edx
xorl %r15d, %r15d
movslq 0x24(%rbp), %rax
cmpq %rax, %rdx
jge 0x106f3
movq 0x18(%rbp), %rax
movq (%rax,%rdx,8), %rax
cmpq 0x20(%rsp), %rdx
jge 0x1024b
movsd 0x688b5(%rip), %xmm0    # 0x78af0
movq 0x40(%rsp), %rcx
ucomisd (%rcx,%rdx,8), %xmm0
ja 0x1034a
movq %rdx, 0x50(%rsp)
movq 0x8(%rax), %rcx
movq 0x10(%rax), %rdx
addq $0x18, %rcx
movq %rcx, 0x60(%rsp)
movsd 0x40(%rax), %xmm1
mulsd 0x48(%rsp), %xmm1
divsd 0x28(%rsp), %xmm1
cvttsd2si %xmm1, %ebx
cvttpd2dq %xmm1, %xmm0
addq $0x18, %rdx
movq %rdx, 0x30(%rsp)
cvtdq2pd %xmm0, %xmm0
subsd %xmm0, %xmm1
movapd %xmm1, 0x10(%rsp)
callq 0x4fce6
xorl %eax, %eax
movapd 0x10(%rsp), %xmm1
ucomisd %xmm0, %xmm1
seta %al
addl %ebx, %eax
movslq %r15d, %r15
testl %eax, %eax
movl $0x0, %ecx
cmovlel %ecx, %eax
addl %r15d, %eax
movslq %eax, %r14
imulq $0x18, %r15, %rbx
addq 0x38(%rsp), %rbx
cmpq %r15, %r14
je 0x1033a
callq 0x4fce6
movsd %xmm0, 0x10(%rsp)
leaq 0x98(%rsp), %r13
movq %r13, %rdi
movq 0x60(%rsp), %rsi
callq 0x3cf94
movsd 0x40d18(%rip), %xmm0    # 0x51008
subsd 0x10(%rsp), %xmm0
leaq 0x80(%rsp), %rbp
movq %rbp, %rdi
movq 0x30(%rsp), %rsi
callq 0x3cf94
leaq 0xb0(%rsp), %r12
movq %r12, %rdi
movq %r13, %rsi
movq %rbp, %rdx
callq 0x3cf4e
incq %r15
movq %rbx, %rdi
movq %r12, %rsi
callq 0x3cb0a
addq $0x18, %rbx
cmpq 0x58(%rsp), %r15
jl 0x102c3
cmpl 0x4(%rsp), %r15d
movq 0x50(%rsp), %rdx
jge 0x106f3
incq %rdx
movq 0x8(%rsp), %rbp
jmp 0x10217
xorl %ebx, %ebx
movslq %ebx, %r14
xorpd %xmm0, %xmm0
movsd %xmm0, 0x20(%rsp)
xorl %r15d, %r15d
movslq 0x34(%r12), %rax
cmpq %rax, %r15
jge 0x103b0
movq 0x28(%r12), %rax
movq (%rax,%r15,8), %rbx
movq %r12, %rdi
movq %rbx, %rsi
callq 0x110fe
cmpq %r14, %r15
jge 0x10393
mulsd (%r13,%r15,8), %xmm0
movsd %xmm0, 0xb0(%rbx)
movsd 0x20(%rsp), %xmm1
addsd %xmm0, %xmm1
movsd %xmm1, 0x20(%rsp)
incq %r15
jmp 0x10369
xorpd %xmm0, %xmm0
callq 0x4fca3
movl 0x4(%rsp), %eax
cvtsi2sd %eax, %xmm0
movsd %xmm0, 0x40(%rsp)
movl %eax, %eax
movq %rax, 0x50(%rsp)
xorl %edx, %edx
xorl %r15d, %r15d
movq 0x8(%rsp), %rcx
movslq 0x34(%rcx), %rax
cmpq %rax, %rdx
jge 0x106f3
movq 0x28(%rcx), %rax
movq %rdx, 0x48(%rsp)
movq (%rax,%rdx,8), %rax
movq 0x8(%rax), %rcx
movq 0x10(%rax), %rdx
movq 0x18(%rax), %rsi
addq $0x18, %rcx
movq %rcx, 0x30(%rsp)
addq $0x18, %rdx
movq %rdx, 0x58(%rsp)
movsd 0xb0(%rax), %xmm1
mulsd 0x40(%rsp), %xmm1
divsd 0x20(%rsp), %xmm1
cvttsd2si %xmm1, %ebx
cvttpd2dq %xmm1, %xmm0
addq $0x18, %rsi
movq %rsi, 0x28(%rsp)
cvtdq2pd %xmm0, %xmm0
subsd %xmm0, %xmm1
movapd %xmm1, 0x10(%rsp)
callq 0x4fce6
xorl %eax, %eax
movapd 0x10(%rsp), %xmm1
ucomisd %xmm0, %xmm1
seta %al
addl %ebx, %eax
movslq %r15d, %r15
testl %eax, %eax
movl $0x0, %ecx
cmovlel %ecx, %eax
addl %r15d, %eax
movslq %eax, %r14
imulq $0x18, %r15, %rbx
addq 0x38(%rsp), %rbx
cmpq %r15, %r14
je 0x10569
callq 0x4fce6
ucomisd 0x40ba5(%rip), %xmm0    # 0x51030
jb 0x10493
sqrtsd %xmm0, %xmm0
jmp 0x10498
callq 0xc2c0
movsd %xmm0, 0x10(%rsp)
callq 0x4fce6
movapd %xmm0, %xmm2
movsd 0x40b59(%rip), %xmm1    # 0x51008
movapd %xmm1, %xmm0
movsd 0x10(%rsp), %xmm3
subsd %xmm3, %xmm0
subsd %xmm2, %xmm1
mulsd %xmm3, %xmm1
movsd %xmm1, 0x60(%rsp)
mulsd %xmm3, %xmm2
movsd %xmm2, 0x10(%rsp)
leaq 0x80(%rsp), %r12
movq %r12, %rdi
movq 0x30(%rsp), %rsi
callq 0x3cf94
leaq 0xe0(%rsp), %r13
movq %r13, %rdi
movq 0x58(%rsp), %rsi
movsd 0x60(%rsp), %xmm0
callq 0x3cf94
leaq 0x98(%rsp), %rbp
movq %rbp, %rdi
movq %r12, %rsi
movq %r13, %rdx
callq 0x3cf4e
leaq 0xc8(%rsp), %r12
movq %r12, %rdi
movq 0x28(%rsp), %rsi
movsd 0x10(%rsp), %xmm0
callq 0x3cf94
leaq 0xb0(%rsp), %r13
movq %r13, %rdi
movq %rbp, %rsi
movq %r12, %rdx
callq 0x3cf4e
incq %r15
movq %rbx, %rdi
movq %r13, %rsi
callq 0x3cb0a
addq $0x18, %rbx
cmpq 0x50(%rsp), %r15
jl 0x10475
movq 0x48(%rsp), %rdx
incq %rdx
cmpl 0x4(%rsp), %r15d
jl 0x103d3
jmp 0x106f3
xorl %ebx, %ebx
movslq %ebx, %r14
xorpd %xmm1, %xmm1
xorl %r15d, %r15d
movslq %edx, %rax
cmpq %rax, %r15
jge 0x105d7
movq 0x8(%r13), %rax
movq (%rax,%r15,8), %rbx
cmpq %r14, %r15
jge 0x105ab
movsd (%rbp,%r15,8), %xmm0
jmp 0x105c6
movq %r13, %rdi
movq %rbx, %rsi
movsd %xmm1, 0x10(%rsp)
callq 0x2100e
movsd 0x10(%rsp), %xmm1
movl 0x14(%r13), %edx
movsd %xmm0, 0x80(%rbx)
addsd %xmm0, %xmm1
incq %r15
jmp 0x1058d
xorpd %xmm0, %xmm0
ucomisd %xmm0, %xmm1
movq %r12, 0x38(%rsp)
jne 0x1061e
jp 0x1061e
xorl %eax, %eax
testl %edx, %edx
cmovlel %eax, %edx
movabsq $0x3ff0000000000000, %rcx # imm = 0x3FF0000000000000
movsd 0x40a07(%rip), %xmm0    # 0x51008
cmpq %rax, %rdx
je 0x1061e
movq 0x8(%r13), %rsi
movq (%rsi,%rax,8), %rsi
movq %rcx, 0x80(%rsi)
addsd %xmm0, %xmm1
incq %rax
jmp 0x10601
movsd %xmm1, 0x10(%rsp)
xorpd %xmm0, %xmm0
callq 0x4fca3
movl 0x4(%rsp), %eax
cvtsi2sd %eax, %xmm0
movsd %xmm0, 0x30(%rsp)
movl %eax, %r12d
xorl %ebp, %ebp
xorl %r15d, %r15d
movsd 0x10(%rsp), %xmm0
movq 0x8(%rsp), %rcx
movslq 0x14(%rcx), %rax
cmpq %rax, %rbp
jge 0x106f3
movq 0x8(%rcx), %rax
movq (%rax,%rbp,8), %rbx
movsd 0x80(%rbx), %xmm1
mulsd 0x30(%rsp), %xmm1
divsd %xmm0, %xmm1
cvttsd2si %xmm1, %r14d
cvttpd2dq %xmm1, %xmm0
addq $0x18, %rbx
cvtdq2pd %xmm0, %xmm0
subsd %xmm0, %xmm1
movapd %xmm1, 0x60(%rsp)
callq 0x4fce6
xorl %eax, %eax
movapd 0x60(%rsp), %xmm1
ucomisd %xmm0, %xmm1
seta %al
addl %r14d, %eax
movslq %r15d, %r15
testl %eax, %eax
movl $0x0, %ecx
cmovlel %ecx, %eax
addl %r15d, %eax
movslq %eax, %r13
imulq $0x18, %r15, %r14
addq 0x38(%rsp), %r14
cmpq %r15, %r13
je 0x106df
incq %r15
movq %r14, %rdi
movq %rbx, %rsi
callq 0x3cb0a
addq $0x18, %r14
cmpq %r12, %r15
jl 0x106c3
incq %rbp
cmpl 0x4(%rsp), %r15d
movsd 0x10(%rsp), %xmm0
jl 0x10649
cmpb $0x1, 0x683f2(%rip)     # 0x78aec
jne 0x10731
leaq 0x40e48(%rip), %rdi     # 0x5154b
callq 0xc590
leaq 0x70(%rsp), %rdi
callq 0x4dccc
leaq 0x40adc(%rip), %rdi     # 0x511f5
movb $0x1, %al
callq 0xc070
leaq 0x40d8a(%rip), %rdi     # 0x514b1
movl %r15d, %esi
xorl %eax, %eax
callq 0xc070
movl %r15d, %eax
addq $0xf8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq