// File: tomfunkhouser[P]gaps/apps/prpview/prpview.cpp
// Function: ReadPoints(gaps::R3Mesh*, char const*)
static RNArray<Point *> *
ReadPoints(R3Mesh *mesh, const char *filename)
{
  // Start statistics
  RNTime start_time;
  start_time.Read();

  // Allocate array of points
  RNArray<Point *> *points = new RNArray<Point *>();
  if (!points) {
    RNFail("Unable to allocate array of points\n");
    return NULL;
  }

  // Parse input filename extension
  const char *extension;
  if (!(extension = strrchr(filename, '.'))) {
    printf("Filename %s has no extension (e.g., .ply)\n", filename);
    return 0;
  }

  // Check input filename extension
  if (!strcmp(extension, ".pts")) {
    // Open points file
    FILE *fp = fopen(filename, "rb");
    if (!fp) {
      RNFail("Unable to open points file: %s\n", filename);
      return NULL;
    }

    // Read points
    float coordinates[6];
    while (fread(coordinates, sizeof(float), 6, fp) == (unsigned int) 6) {
      Point *point = new Point();
      point->position.Reset(coordinates[0], coordinates[1], coordinates[2]);
      point->normal.Reset(coordinates[3], coordinates[4], coordinates[5]);
      points->Insert(point);
    }

    // Close points file
    fclose(fp);
  }
  else if (!strcmp(extension, ".xyz")) {
    // Open points file
    FILE *fp = fopen(filename, "r");
    if (!fp) {
      RNFail("Unable to open points file: %s\n", filename);
      return NULL;
    }

    // Read points
    double x, y, z;
    while (fscanf(fp, "%lf%lf%lf", &x, &y, &z) == (unsigned int) 3) {
      Point *point = new Point();
      point->position.Reset(x, y, z);
      point->normal.Reset(0, 0, 0);
      points->Insert(point);
    }

    // Close points file
    fclose(fp);
  }
  else if (!strcmp(extension, ".vts")) {
    // Open points file
    FILE *fp = fopen(filename, "r");
    if (!fp) {
      RNFail("Unable to open points file: %s\n", filename);
      return NULL;
    }

    // Read points
    int vertex_index;
    double x, y, z;
    while (fscanf(fp, "%d%lf%lf%lf", &vertex_index, &x, &y, &z) == (unsigned int) 4) {
      Point *point = new Point();
      point->position.Reset(x, y, z);
      point->normal.Reset(0, 0, 0);
      points->Insert(point);
    }

    // Close points file
    fclose(fp);
  }
  else if (!strcmp(extension, ".pid")) {
    // Open points file
    FILE *fp = fopen(filename, "r");
    if (!fp) {
      RNFail("Unable to open points file: %s\n", filename);
      return NULL;
    }

    // Read points
    int vertex_index;
    while (fscanf(fp, "%d", &vertex_index) == (unsigned int) 1) {
      if ((vertex_index >= 0) && (vertex_index < mesh->NVertices())) {
        R3MeshVertex *vertex = mesh->Vertex(vertex_index);
        Point *point = new Point();
        point->position = mesh->VertexPosition(vertex);
        point->normal = mesh->VertexNormal(vertex);
        points->Insert(point);
      }
    }

    // Close points file
    fclose(fp);
  }
  else {
    RNFail("Unrecognized point file extension: %s\n", extension);
    return NULL;
  }

  // Print statistics
  if (print_verbose) {
    printf("Read point file: %s ...\n", filename);
    printf("  Time = %.2f seconds\n", start_time.Elapsed());
    printf("  # Points = %d\n", points->NEntries());
    fflush(stdout);
  }

  // Return points
  return points;
}

// Assembly:
subq $0x128, %rsp            # imm = 0x128
movq %rdi, 0x118(%rsp)
movq %rsi, 0x110(%rsp)
leaq 0x100(%rsp), %rdi
movq %rdi, 0x30(%rsp)
callq 0xf6cb0
movq 0x30(%rsp), %rdi
callq 0x2ae50
movl $0x10, %edi
callq 0x229d0
movq %rax, %rdi
movq %rdi, %rax
movq %rax, 0x38(%rsp)
callq 0x2b3a0
jmp 0x29edf
movq 0x38(%rsp), %rax
movq %rax, 0xf8(%rsp)
cmpq $0x0, 0xf8(%rsp)
jne 0x29f3e
leaq 0x16c4f7(%rip), %rdi    # 0x1963f5
movb $0x0, %al
callq 0xfbc30
movq $0x0, 0x120(%rsp)
jmp 0x2a60d
movq 0x38(%rsp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0xf0(%rsp)
movl %eax, 0xec(%rsp)
movl $0x10, %esi
callq 0x229f0
jmp 0x2a61d
movq 0x110(%rsp), %rdi
movl $0x2e, %esi
callq 0x22b00
movq %rax, 0xe0(%rsp)
cmpq $0x0, %rax
jne 0x29f85
movq 0x110(%rsp), %rsi
leaq 0x16c4ac(%rip), %rdi    # 0x196419
movb $0x0, %al
callq 0x22090
movq $0x0, 0x120(%rsp)
jmp 0x2a60d
movq 0xe0(%rsp), %rdi
leaq 0x16c4b0(%rip), %rsi    # 0x196444
callq 0x22ea0
cmpl $0x0, %eax
jne 0x2a112
movq 0x110(%rsp), %rdi
leaq 0x16c498(%rip), %rsi    # 0x196449
callq 0x22bf0
movq %rax, 0xd8(%rsp)
cmpq $0x0, 0xd8(%rsp)
jne 0x29ff0
movq 0x110(%rsp), %rsi
leaq 0x16c474(%rip), %rdi    # 0x19644c
movb $0x0, %al
callq 0xfbc30
movq $0x0, 0x120(%rsp)
jmp 0x2a60d
jmp 0x29ff2
leaq 0xc0(%rsp), %rdi
movq 0xd8(%rsp), %rcx
movl $0x4, %esi
movl $0x6, %edx
callq 0x22fd0
cmpq $0x6, %rax
jne 0x2a100
movl $0x30, %edi
callq 0x229d0
movq %rax, %rdi
movq %rdi, %rax
movq %rax, 0x28(%rsp)
xorps %xmm0, %xmm0
movaps %xmm0, 0x20(%rdi)
movaps %xmm0, 0x10(%rdi)
movaps %xmm0, (%rdi)
callq 0x2b3b0
jmp 0x2a045
movq 0x28(%rsp), %rax
movq %rax, 0xb8(%rsp)
movq 0xb8(%rsp), %rdi
movss 0xc0(%rsp), %xmm0
cvtss2sd %xmm0, %xmm0
movss 0xc4(%rsp), %xmm1
cvtss2sd %xmm1, %xmm1
movss 0xc8(%rsp), %xmm2
cvtss2sd %xmm2, %xmm2
callq 0x2af20
movq 0xb8(%rsp), %rdi
addq $0x18, %rdi
movss 0xcc(%rsp), %xmm0
cvtss2sd %xmm0, %xmm0
movss 0xd0(%rsp), %xmm1
cvtss2sd %xmm1, %xmm1
movss 0xd4(%rsp), %xmm2
cvtss2sd %xmm2, %xmm2
callq 0x2af60
movq 0xf8(%rsp), %rdi
movq 0xb8(%rsp), %rsi
callq 0x2b3e0
jmp 0x29ff2
movq 0x28(%rsp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0xf0(%rsp)
movl %eax, 0xec(%rsp)
movl $0x30, %esi
callq 0x229f0
jmp 0x2a61d
movq 0xd8(%rsp), %rdi
callq 0x229c0
jmp 0x2a597
movq 0xe0(%rsp), %rdi
leaq 0x16c34b(%rip), %rsi    # 0x19646c
callq 0x22ea0
cmpl $0x0, %eax
jne 0x2a283
movq 0x110(%rsp), %rdi
leaq 0x1737a5(%rip), %rsi    # 0x19d8e3
callq 0x22bf0
movq %rax, 0xb0(%rsp)
cmpq $0x0, 0xb0(%rsp)
jne 0x2a17d
movq 0x110(%rsp), %rsi
leaq 0x16c2e7(%rip), %rdi    # 0x19644c
movb $0x0, %al
callq 0xfbc30
movq $0x0, 0x120(%rsp)
jmp 0x2a60d
jmp 0x2a17f
movq 0xb0(%rsp), %rdi
leaq 0x16e821(%rip), %rsi    # 0x1989af
leaq 0xa8(%rsp), %rdx
leaq 0xa0(%rsp), %rcx
leaq 0x98(%rsp), %r8
movb $0x0, %al
callq 0x23350
cmpl $0x3, %eax
jne 0x2a271
movl $0x30, %edi
callq 0x229d0
movq %rax, %rdi
movq %rdi, %rax
movq %rax, 0x20(%rsp)
xorps %xmm0, %xmm0
movaps %xmm0, 0x20(%rdi)
movaps %xmm0, 0x10(%rdi)
movaps %xmm0, (%rdi)
callq 0x2b3b0
jmp 0x2a1e0
movq 0x20(%rsp), %rax
movq %rax, 0x90(%rsp)
movq 0x90(%rsp), %rdi
movsd 0xa8(%rsp), %xmm0
movsd 0xa0(%rsp), %xmm1
movsd 0x98(%rsp), %xmm2
callq 0x2af20
movq 0x90(%rsp), %rdi
addq $0x18, %rdi
xorps %xmm2, %xmm2
movaps %xmm2, %xmm0
movaps %xmm2, %xmm1
callq 0x2af60
movq 0xf8(%rsp), %rdi
movq 0x90(%rsp), %rsi
callq 0x2b3e0
jmp 0x2a17f
movq 0x20(%rsp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0xf0(%rsp)
movl %eax, 0xec(%rsp)
movl $0x30, %esi
callq 0x229f0
jmp 0x2a61d
movq 0xb0(%rsp), %rdi
callq 0x229c0
jmp 0x2a595
movq 0xe0(%rsp), %rdi
leaq 0x16c1df(%rip), %rsi    # 0x196471
callq 0x22ea0
cmpl $0x0, %eax
jne 0x2a3de
movq 0x110(%rsp), %rdi
leaq 0x173634(%rip), %rsi    # 0x19d8e3
callq 0x22bf0
movq %rax, 0x88(%rsp)
cmpq $0x0, 0x88(%rsp)
jne 0x2a2ee
movq 0x110(%rsp), %rsi
leaq 0x16c176(%rip), %rdi    # 0x19644c
movb $0x0, %al
callq 0xfbc30
movq $0x0, 0x120(%rsp)
jmp 0x2a60d
jmp 0x2a2f0
movq 0x88(%rsp), %rdi
leaq 0x16e6ae(%rip), %rsi    # 0x1989ad
leaq 0x84(%rsp), %rdx
leaq 0x78(%rsp), %rcx
leaq 0x70(%rsp), %r8
leaq 0x68(%rsp), %r9
movb $0x0, %al
callq 0x23350
cmpl $0x4, %eax
jne 0x2a3cc
movl $0x30, %edi
callq 0x229d0
movq %rax, %rdi
movq %rdi, %rax
movq %rax, 0x18(%rsp)
xorps %xmm0, %xmm0
movaps %xmm0, 0x20(%rdi)
movaps %xmm0, 0x10(%rdi)
movaps %xmm0, (%rdi)
callq 0x2b3b0
jmp 0x2a350
movq 0x18(%rsp), %rax
movq %rax, 0x60(%rsp)
movq 0x60(%rsp), %rdi
movsd 0x78(%rsp), %xmm0
movsd 0x70(%rsp), %xmm1
movsd 0x68(%rsp), %xmm2
callq 0x2af20
movq 0x60(%rsp), %rdi
addq $0x18, %rdi
xorps %xmm2, %xmm2
movaps %xmm2, %xmm0
movaps %xmm2, %xmm1
callq 0x2af60
movq 0xf8(%rsp), %rdi
movq 0x60(%rsp), %rsi
callq 0x2b3e0
jmp 0x2a2f0
movq 0x18(%rsp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0xf0(%rsp)
movl %eax, 0xec(%rsp)
movl $0x30, %esi
callq 0x229f0
jmp 0x2a61d
movq 0x88(%rsp), %rdi
callq 0x229c0
jmp 0x2a593
movq 0xe0(%rsp), %rdi
leaq 0x16c089(%rip), %rsi    # 0x196476
callq 0x22ea0
cmpl $0x0, %eax
jne 0x2a56d
movq 0x110(%rsp), %rdi
leaq 0x1734d9(%rip), %rsi    # 0x19d8e3
callq 0x22bf0
movq %rax, 0x58(%rsp)
cmpq $0x0, 0x58(%rsp)
jne 0x2a443
movq 0x110(%rsp), %rsi
leaq 0x16c021(%rip), %rdi    # 0x19644c
movb $0x0, %al
callq 0xfbc30
movq $0x0, 0x120(%rsp)
jmp 0x2a60d
jmp 0x2a445
movq 0x58(%rsp), %rdi
leaq 0x173109(%rip), %rsi    # 0x19d55a
leaq 0x54(%rsp), %rdx
movb $0x0, %al
callq 0x23350
cmpl $0x1, %eax
jne 0x2a561
cmpl $0x0, 0x54(%rsp)
jl 0x2a55c
movl 0x54(%rsp), %eax
movl %eax, 0x14(%rsp)
movq 0x118(%rsp), %rdi
callq 0x2abd0
movl %eax, %ecx
movl 0x14(%rsp), %eax
cmpl %ecx, %eax
jge 0x2a55c
movq 0x118(%rsp), %rdi
movl 0x54(%rsp), %esi
callq 0x2abf0
movq %rax, 0x48(%rsp)
movl $0x30, %edi
callq 0x229d0
movq %rax, %rdi
movq %rdi, %rax
movq %rax, 0x8(%rsp)
xorps %xmm0, %xmm0
movaps %xmm0, 0x20(%rdi)
movaps %xmm0, 0x10(%rdi)
movaps %xmm0, (%rdi)
callq 0x2b3b0
jmp 0x2a4d4
movq 0x8(%rsp), %rax
movq %rax, 0x40(%rsp)
movq 0x118(%rsp), %rdi
movq 0x48(%rsp), %rsi
callq 0x2ab40
movq %rax, %rsi
movq 0x40(%rsp), %rdi
callq 0xd5030
movq 0x118(%rsp), %rdi
movq 0x48(%rsp), %rsi
callq 0x2aab0
movq %rax, %rsi
movq 0x40(%rsp), %rdi
addq $0x18, %rdi
callq 0xd67c0
movq 0xf8(%rsp), %rdi
movq 0x40(%rsp), %rsi
callq 0x2b3e0
jmp 0x2a55c
movq 0x8(%rsp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0xf0(%rsp)
movl %eax, 0xec(%rsp)
movl $0x30, %esi
callq 0x229f0
jmp 0x2a61d
jmp 0x2a445
movq 0x58(%rsp), %rdi
callq 0x229c0
jmp 0x2a591
movq 0xe0(%rsp), %rsi
leaq 0x16beff(%rip), %rdi    # 0x19647b
movb $0x0, %al
callq 0xfbc30
movq $0x0, 0x120(%rsp)
jmp 0x2a60d
jmp 0x2a593
jmp 0x2a595
jmp 0x2a597
cmpl $0x0, 0x1da776(%rip)    # 0x204d14
je 0x2a5fd
movq 0x110(%rsp), %rsi
leaq 0x16bef3(%rip), %rdi    # 0x1964a2
movb $0x0, %al
callq 0x22090
leaq 0x100(%rsp), %rdi
callq 0xf6d70
leaq 0x16bde1(%rip), %rdi    # 0x1963ab
movb $0x1, %al
callq 0x22090
movq 0xf8(%rsp), %rdi
callq 0x2ac70
movl %eax, %esi
leaq 0x16bed4(%rip), %rdi    # 0x1964bb
movb $0x0, %al
callq 0x22090
movq 0x1d399b(%rip), %rax    # 0x1fdf90
movq (%rax), %rdi
callq 0x22be0
movq 0xf8(%rsp), %rax
movq %rax, 0x120(%rsp)
movq 0x120(%rsp), %rax
addq $0x128, %rsp            # imm = 0x128
retq
movq 0xf0(%rsp), %rdi
callq 0x23270
nopw (%rax,%rax)