// File: /workspace/github_cpp_makefile_star2_O2/tomfunkhouser[P]gaps/apps/msh2pts/msh2pts.cpp
// Function: WritePoints(gaps::R3Mesh*, gaps::RNArray<Point*> const&, char const*)
RNBoolean 
WritePoints(R3Mesh *mesh, const RNArray<Point *>& points, const char *filename)
{
  // Start statistics
  RNTime start_time;
  start_time.Read();

  // 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 file type
  if (!strcmp(extension, ".pid")) {
    // Open file
    FILE *fp = stdout;
    if (filename) {
      fp = fopen(filename, "w");
      if (!fp) {
        RNFail("Unable to open output file %s\n", filename);
        return FALSE;
      }
    }

    // Write vertex ids
    for (int i = 0; i < points.NEntries(); i++) {
      Point *point = points[i];
      if (!point->vertex) { RNFail("Incompatible output type: pid\n"); return 0; }
      int vertex_id = (point->vertex) ? mesh->VertexID(point->vertex) : -1;
      fprintf(fp, "%d\n", vertex_id);
    }

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

    // Write points
    for (int i = 0; i < points.NEntries(); i++) {
      Point *point = points[i];
      int vertex_id = (point->vertex) ? mesh->VertexID(point->vertex) : -1;
      const R3Point& position = point->position;
      fprintf(fp, "%d %g %g %g\n", vertex_id, position.X(), position.Y(), position.Z());
    }

    // Close file
    if (filename) fclose(fp);
  }
  else if (!strcmp(extension, ".xyzn")) {
    // Open file
    FILE *fp = stdout;
    if (filename) {
      fp = fopen(filename, "w");
      if (!fp) {
        RNFail("Unable to open output file %s\n", filename);
        return FALSE;
      }
    }

    // Write points
    for (int i = 0; i < points.NEntries(); i++) {
      Point *point = points[i];
      fprintf(fp, "%g %g %g   %g %g %g\n", 
        point->position.X(), point->position.Y(), point->position.Z(),
        point->normal.X(), point->normal.Y(), point->normal.Z());
    }

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

    // Write points
    for (int i = 0; i < points.NEntries(); i++) {
      Point *point = points[i];
      fprintf(fp, "%g %g %g\n", point->position.X(), point->position.Y(), point->position.Z());
    }

    // Close file
    if (filename) fclose(fp);
  }
  else if (!strcmp(extension, ".pts")) {
    // Open file
    FILE *fp = stdout;
    if (filename) {
      fp = fopen(filename, "wb");
      if (!fp) {
        RNFail("Unable to open output file %s\n", filename);
        return FALSE;
      }
    }

    // Write points
    float coordinates[6];
    for (int i = 0; i < points.NEntries(); i++) {
      Point *point = points[i];
      coordinates[0] = point->position.X();
      coordinates[1] = point->position.Y();
      coordinates[2] = point->position.Z();
      coordinates[3] = point->normal.X();
      coordinates[4] = point->normal.Y();
      coordinates[5] = point->normal.Z();
      if (fwrite(coordinates, sizeof(float), 6, fp) != (unsigned int) 6) {
        RNFail("Unable to write point to output file %s\n", filename);
        return FALSE;
      }
    }

    // Close file
    if (filename) fclose(fp);
  }
  else if (!strcmp(extension, ".sdf")) {
    // Open binary file
    FILE *fp = fopen(filename, "wb");
    if (!fp) {
      RNFail("Unable to open output file %s\n", filename);
      return 0;
    }

    // Write points
    float values[4];
    for (int i = 0; i < points.NEntries(); i++) {
      Point *point = points.Kth(i);
      values[0] = point->position.X();
      values[1] = point->position.Y();
      values[2] = point->position.Z();
      values[3] = point->sdf;
      if (fwrite(values, sizeof(float), 4, fp) != (unsigned int) 4) {
        RNFail("Unable to write point to output file %s\n", filename);
        return 0;
      }
    }
  }
  else {
    // Create mesh
    R3Mesh mesh;
    for (int i = 0; i < points.NEntries(); i++) {
      Point *point = points[i];
      mesh.CreateVertex(point->position, point->normal);
    }

    // Write mesh file
    if (!mesh.WriteFile(filename)) return FALSE;
  }

  // Print message
  if (print_verbose) {
    fprintf(stdout, "Wrote points to %s ...\n", (filename) ? filename : "stdout");
    printf("  Time = %.2f seconds\n", start_time.Elapsed());
    fflush(stdout);
  }

  // Return success
  return TRUE;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x148, %rsp            # imm = 0x148
movq %rdx, %rbx
movq %rsi, %r14
movq %rsp, %r15
movq %r15, %rdi
callq 0x70de6
movq %r15, %rdi
callq 0x14880
pushq $0x2e
popq %rsi
movq %rbx, %rdi
callq 0xe4f0
testq %rax, %rax
je 0x10552
movq %rax, %r15
leaq 0x8dc83(%rip), %rsi     # 0x9e117
movq %rax, %rdi
callq 0xe680
testl %eax, %eax
je 0x1056a
leaq 0x8dcaf(%rip), %rsi     # 0x9e15a
movq %r15, %rdi
callq 0xe680
testl %eax, %eax
je 0x1058b
leaq 0x8dcaa(%rip), %rsi     # 0x9e16c
movq %r15, %rdi
callq 0xe680
testl %eax, %eax
je 0x105b4
leaq 0x8dcae(%rip), %rsi     # 0x9e187
movq %r15, %rdi
callq 0xe680
testl %eax, %eax
je 0x10637
leaq 0x8dc9c(%rip), %rsi     # 0x9e18c
movq %r15, %rdi
callq 0xe680
testl %eax, %eax
je 0x106e1
leaq 0x8dcb6(%rip), %rsi     # 0x9e1bd
movq %r15, %rdi
callq 0xe680
testl %eax, %eax
je 0x10762
leaq 0x10(%rsp), %r15
movq %r15, %rdi
callq 0x24f50
xorl %r12d, %r12d
movslq 0xc(%r14), %rax
cmpq %rax, %r12
jge 0x106b9
movq (%r14), %rax
movq (%rax,%r12,8), %rdx
leaq 0x10(%rdx), %rsi
addq $0x28, %rdx
movq %r15, %rdi
xorl %ecx, %ecx
callq 0x27d4a
incq %r12
jmp 0x10527
leaq 0x8db93(%rip), %rdi     # 0x9e0ec
xorl %ebp, %ebp
movq %rbx, %rsi
xorl %eax, %eax
callq 0xe070
jmp 0x1090d
testq %rbx, %rbx
je 0x105dd
leaq 0x93963(%rip), %rsi     # 0xa3ed9
movq %rbx, %rdi
callq 0xe560
movq %rax, %r15
testq %rax, %rax
jne 0x105e7
jmp 0x107d8
testq %rbx, %rbx
je 0x10660
leaq 0x9393e(%rip), %rsi     # 0xa3ed9
movq %rbx, %rdi
callq 0xe560
movq %rax, %r15
testq %rax, %rax
jne 0x1066a
jmp 0x107d8
testq %rbx, %rbx
je 0x1070a
leaq 0x93915(%rip), %rsi     # 0xa3ed9
movq %rbx, %rdi
callq 0xe560
movq %rax, %r15
testq %rax, %rax
jne 0x10714
jmp 0x107d8
movq 0xd799c(%rip), %rax     # 0xe7f80
movq (%rax), %r15
leaq 0x8ee79(%rip), %r12     # 0x9f467
xorl %r13d, %r13d
movslq 0xc(%r14), %rax
cmpq %rax, %r13
jge 0x108b0
movq (%r14), %rax
movq (%rax,%r13,8), %rax
movq (%rax), %rax
testq %rax, %rax
je 0x10622
movl 0x70(%rax), %edx
movq %r15, %rdi
movq %r12, %rsi
xorl %eax, %eax
callq 0xe6b0
incq %r13
jmp 0x105f1
leaq 0x8db12(%rip), %rdi     # 0x9e13b
xorl %ebp, %ebp
xorl %eax, %eax
callq 0x731ee
jmp 0x1090d
testq %rbx, %rbx
je 0x107f0
leaq 0x93892(%rip), %rsi     # 0xa3ed9
movq %rbx, %rdi
callq 0xe560
movq %rax, %r15
testq %rax, %rax
jne 0x107fa
jmp 0x107d8
movq 0xd7919(%rip), %rax     # 0xe7f80
movq (%rax), %r15
xorl %r13d, %r13d
pushq $-0x1
popq %rbp
leaq 0x8dae8(%rip), %r12     # 0x9e15f
movslq 0xc(%r14), %rax
cmpq %rax, %r13
jge 0x108b0
movq (%r14), %rax
movq (%rax,%r13,8), %rax
movq (%rax), %rcx
movl %ebp, %edx
testq %rcx, %rcx
je 0x10698
movl 0x70(%rcx), %edx
movsd 0x10(%rax), %xmm0
movsd 0x18(%rax), %xmm1
movsd 0x20(%rax), %xmm2
movq %r15, %rdi
movq %r12, %rsi
movb $0x3, %al
callq 0xe6b0
incq %r13
jmp 0x10677
leaq 0x10(%rsp), %rdi
movq %rbx, %rsi
callq 0x33e7c
movl %eax, %ebp
leaq 0x10(%rsp), %rdi
callq 0x25438
testl %ebp, %ebp
jne 0x108bd
xorl %ebp, %ebp
jmp 0x1090d
testq %rbx, %rbx
je 0x10839
leaq 0x8daa0(%rip), %rsi     # 0x9e191
movq %rbx, %rdi
callq 0xe560
movq %rax, %r15
testq %rax, %rax
jne 0x10843
jmp 0x107d8
movq 0xd786f(%rip), %rax     # 0xe7f80
movq (%rax), %r15
xorl %r13d, %r13d
leaq 0x8da54(%rip), %r12     # 0x9e172
movslq 0xc(%r14), %rax
cmpq %rax, %r13
jge 0x108b0
movq (%r14), %rax
movq (%rax,%r13,8), %rax
movsd 0x10(%rax), %xmm0
movsd 0x18(%rax), %xmm1
movsd 0x20(%rax), %xmm2
movsd 0x28(%rax), %xmm3
movsd 0x30(%rax), %xmm4
movsd 0x38(%rax), %xmm5
movq %r15, %rdi
movq %r12, %rsi
movb $0x6, %al
callq 0xe6b0
incq %r13
jmp 0x1071e
leaq 0x8da28(%rip), %rsi     # 0x9e191
movq %rbx, %rdi
callq 0xe560
testq %rax, %rax
je 0x107d8
movq %rax, %r15
xorl %ebp, %ebp
leaq 0x10(%rsp), %r12
pushq $0x4
popq %r13
movslq 0xc(%r14), %rax
cmpq %rax, %rbp
jge 0x108bd
movq (%r14), %rax
movq (%rax,%rbp,8), %rax
movupd 0x10(%rax), %xmm0
movsd 0x20(%rax), %xmm1
movhpd 0x40(%rax), %xmm1       # xmm1 = xmm1[0],mem[0]
cvtpd2ps %xmm1, %xmm1
cvtpd2ps %xmm0, %xmm0
unpcklpd %xmm1, %xmm0            # xmm0 = xmm0[0],xmm1[0]
movapd %xmm0, 0x10(%rsp)
movq %r12, %rdi
movq %r13, %rsi
movq %r13, %rdx
movq %r15, %rcx
callq 0xe8f0
incq %rbp
cmpq $0x4, %rax
je 0x10784
jmp 0x108a4
leaq 0x8d93d(%rip), %rdi     # 0x9e11c
xorl %ebp, %ebp
movq %rbx, %rsi
xorl %eax, %eax
callq 0x731ee
jmp 0x1090d
movq 0xd7789(%rip), %rax     # 0xe7f80
movq (%rax), %r15
xorl %r13d, %r13d
leaq 0x8d979(%rip), %r12     # 0x9e17d
movslq 0xc(%r14), %rax
cmpq %rax, %r13
jge 0x108b0
movq (%r14), %rax
movq (%rax,%r13,8), %rax
movsd 0x10(%rax), %xmm0
movsd 0x18(%rax), %xmm1
movsd 0x20(%rax), %xmm2
movq %r15, %rdi
movq %r12, %rsi
movb $0x3, %al
callq 0xe6b0
incq %r13
jmp 0x10804
movq 0xd7740(%rip), %rax     # 0xe7f80
movq (%rax), %r15
xorl %r12d, %r12d
pushq $0x4
popq %r13
pushq $0x6
popq %rbp
movslq 0xc(%r14), %rax
cmpq %rax, %r12
jge 0x108b0
movq (%r14), %rax
movq (%rax,%r12,8), %rax
movupd 0x10(%rax), %xmm0
movupd 0x20(%rax), %xmm1
cvtpd2ps %xmm1, %xmm1
cvtpd2ps %xmm0, %xmm0
unpcklpd %xmm1, %xmm0            # xmm0 = xmm0[0],xmm1[0]
movapd %xmm0, 0x10(%rsp)
movupd 0x30(%rax), %xmm0
cvtpd2ps %xmm0, %xmm0
movlpd %xmm0, 0x20(%rsp)
leaq 0x10(%rsp), %rdi
movq %r13, %rsi
movq %rbp, %rdx
movq %r15, %rcx
callq 0xe8f0
incq %r12
cmpq $0x6, %rax
je 0x1084d
leaq 0x8d8e9(%rip), %rdi     # 0x9e194
jmp 0x107df
testq %rbx, %rbx
je 0x108bd
movq %r15, %rdi
callq 0xe450
pushq $0x1
popq %rbp
cmpb $0x1, 0xd83d9(%rip)     # 0xe8ca0
jne 0x1090d
movq 0xd76b0(%rip), %r14     # 0xe7f80
movq (%r14), %rdi
testq %rbx, %rbx
leaq 0x8d8fd(%rip), %rdx     # 0x9e1da
cmovneq %rbx, %rdx
leaq 0x8d8da(%rip), %rsi     # 0x9e1c2
xorl %eax, %eax
callq 0xe6b0
movq %rsp, %rdi
callq 0x70e20
leaq 0x8d8e3(%rip), %rdi     # 0x9e1e1
movb $0x1, %al
callq 0xe070
movq (%r14), %rdi
callq 0xe550
movl %ebp, %eax
addq $0x148, %rsp            # imm = 0x148
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x10923
movq %rax, %rbx
leaq 0x10(%rsp), %rdi
callq 0x25438
movq %rbx, %rdi
callq 0xe820