// File: /workspace/github_cpp_makefile_star2_O3/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, %r12
movq %r12, %rdi
callq 0x7b0ec
xorl %r15d, %r15d
movq %r12, %rdi
xorl %esi, %esi
callq 0xe770
movq %rbx, %rdi
movl $0x2e, %esi
callq 0xe4d0
testq %rax, %rax
je 0x10ca5
movq %rax, %r12
leaq 0x9d55c(%rip), %rsi     # 0xae117
movq %rax, %rdi
callq 0xe650
testl %eax, %eax
je 0x10cbb
leaq 0x9d588(%rip), %rsi     # 0xae15a
movq %r12, %rdi
callq 0xe650
testl %eax, %eax
je 0x10cdc
leaq 0x9d583(%rip), %rsi     # 0xae16c
movq %r12, %rdi
callq 0xe650
testl %eax, %eax
je 0x10d05
leaq 0x9d587(%rip), %rsi     # 0xae187
movq %r12, %rdi
callq 0xe650
testl %eax, %eax
je 0x10d7d
leaq 0x9d575(%rip), %rsi     # 0xae18c
movq %r12, %rdi
callq 0xe650
testl %eax, %eax
je 0x10e21
leaq 0x9d58f(%rip), %rsi     # 0xae1bd
movq %r12, %rdi
callq 0xe650
testl %eax, %eax
je 0x10eac
leaq 0x10(%rsp), %rdi
callq 0x27548
cmpl $0x0, 0xc(%r14)
jle 0x10c7c
xorl %r12d, %r12d
leaq 0x10(%rsp), %r15
movq (%r14), %rax
movq (%rax,%r12,8), %rdx
leaq 0x10(%rdx), %rsi
addq $0x28, %rdx
movq %r15, %rdi
xorl %ecx, %ecx
callq 0x2aa0c
incq %r12
movslq 0xc(%r14), %rax
cmpq %rax, %r12
jl 0x10c57
leaq 0x10(%rsp), %rdi
movq %rbx, %rsi
callq 0x3844a
movl %eax, %ebp
leaq 0x10(%rsp), %rdi
callq 0x27a86
testl %ebp, %ebp
jne 0x1101a
xorl %r15d, %r15d
jmp 0x1106d
leaq 0x9d440(%rip), %rdi     # 0xae0ec
movq %rbx, %rsi
xorl %eax, %eax
callq 0xe070
jmp 0x1106d
testq %rbx, %rbx
je 0x10d2e
leaq 0xa3262(%rip), %rsi     # 0xb3f29
movq %rbx, %rdi
callq 0xe540
movq %rax, %r15
testq %rax, %rax
jne 0x10d38
jmp 0x10f2e
testq %rbx, %rbx
je 0x10dbc
leaq 0xa323d(%rip), %rsi     # 0xb3f29
movq %rbx, %rdi
callq 0xe540
movq %rax, %r15
testq %rax, %rax
jne 0x10dc6
jmp 0x10f2e
testq %rbx, %rbx
je 0x10e4a
leaq 0xa3214(%rip), %rsi     # 0xb3f29
movq %rbx, %rdi
callq 0xe540
movq %rax, %r15
testq %rax, %rax
jne 0x10e54
jmp 0x10f2e
movq 0xe323b(%rip), %rax     # 0xf3f70
movq (%rax), %r15
cmpl $0x0, 0xc(%r14)
jle 0x1100d
leaq 0x9e76d(%rip), %r12     # 0xaf4b7
xorl %r13d, %r13d
movq (%r14), %rax
movq (%rax,%r13,8), %rax
movq (%rax), %rax
testq %rax, %rax
je 0x10da6
movl 0x70(%rax), %edx
movq %r15, %rdi
movq %r12, %rsi
xorl %eax, %eax
callq 0xe680
incq %r13
movslq 0xc(%r14), %rax
cmpq %rax, %r13
jl 0x10d4d
jmp 0x1100d
testq %rbx, %rbx
je 0x10f47
leaq 0xa319c(%rip), %rsi     # 0xb3f29
movq %rbx, %rdi
callq 0xe540
movq %rax, %r15
testq %rax, %rax
jne 0x10f51
jmp 0x10f2e
leaq 0x9d38e(%rip), %rdi     # 0xae13b
xorl %r15d, %r15d
xorl %eax, %eax
callq 0x7d84e
jmp 0x1106d
movq 0xe31ad(%rip), %rax     # 0xf3f70
movq (%rax), %r15
cmpl $0x0, 0xc(%r14)
jle 0x1100d
xorl %r13d, %r13d
leaq 0x9d384(%rip), %r12     # 0xae15f
movq (%r14), %rax
movq (%rax,%r13,8), %rax
movq (%rax), %rcx
testq %rcx, %rcx
je 0x10def
movl 0x70(%rcx), %edx
jmp 0x10df4
movl $0xffffffff, %edx       # imm = 0xFFFFFFFF
movsd 0x10(%rax), %xmm0
movsd 0x18(%rax), %xmm1
movsd 0x20(%rax), %xmm2
movq %r15, %rdi
movq %r12, %rsi
movb $0x3, %al
callq 0xe680
incq %r13
movslq 0xc(%r14), %rax
cmpq %rax, %r13
jl 0x10ddb
jmp 0x1100d
testq %rbx, %rbx
je 0x10f97
leaq 0x9d360(%rip), %rsi     # 0xae191
movq %rbx, %rdi
callq 0xe540
movq %rax, %r15
testq %rax, %rax
jne 0x10fa1
jmp 0x10f2e
movq 0xe311f(%rip), %rax     # 0xf3f70
movq (%rax), %r15
cmpl $0x0, 0xc(%r14)
jle 0x1100d
xorl %r13d, %r13d
leaq 0x9d309(%rip), %r12     # 0xae172
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 0xe680
incq %r13
movslq 0xc(%r14), %rax
cmpq %rax, %r13
jl 0x10e69
jmp 0x1100d
leaq 0x9d2de(%rip), %rsi     # 0xae191
movq %rbx, %rdi
callq 0xe540
testq %rax, %rax
je 0x10f2e
cmpl $0x0, 0xc(%r14)
jle 0x1101a
movq %rax, %r15
xorl %r13d, %r13d
leaq 0x10(%rsp), %r12
movq (%r14), %rax
movq (%rax,%r13,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)
movl $0x4, %esi
movl $0x4, %edx
movq %r12, %rdi
movq %r15, %rcx
callq 0xe8c0
cmpq $0x4, %rax
jne 0x11082
incq %r13
movslq 0xc(%r14), %rax
cmpq %rax, %r13
jl 0x10ed6
jmp 0x1101a
leaq 0x9d1e7(%rip), %rdi     # 0xae11c
xorl %r15d, %r15d
movq %rbx, %rsi
xorl %eax, %eax
callq 0x7d84e
jmp 0x1106d
movq 0xe3022(%rip), %rax     # 0xf3f70
movq (%rax), %r15
cmpl $0x0, 0xc(%r14)
jle 0x1100d
xorl %r13d, %r13d
leaq 0x9d217(%rip), %r12     # 0xae17d
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 0xe680
incq %r13
movslq 0xc(%r14), %rax
cmpq %rax, %r13
jl 0x10f66
jmp 0x1100d
movq 0xe2fd2(%rip), %rax     # 0xf3f70
movq (%rax), %r15
cmpl $0x0, 0xc(%r14)
jle 0x1100d
xorl %r13d, %r13d
leaq 0x10(%rsp), %r12
movq (%r14), %rax
movq (%rax,%r13,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)
movl $0x4, %esi
movl $0x6, %edx
movq %r12, %rdi
movq %r15, %rcx
callq 0xe8c0
cmpq $0x6, %rax
jne 0x11082
incq %r13
movslq 0xc(%r14), %rax
cmpq %rax, %r13
jl 0x10fb0
testq %rbx, %rbx
je 0x1101a
movq %r15, %rdi
callq 0xe440
movl $0x1, %r15d
cmpb $0x1, 0xe3c59(%rip)     # 0xf4c80
jne 0x1106d
movq 0xe2f40(%rip), %r14     # 0xf3f70
movq (%r14), %rdi
testq %rbx, %rbx
leaq 0x9d19d(%rip), %rdx     # 0xae1da
cmovneq %rbx, %rdx
leaq 0x9d17a(%rip), %rsi     # 0xae1c2
xorl %eax, %eax
callq 0xe680
movq %rsp, %rdi
callq 0x7b14e
leaq 0x9d183(%rip), %rdi     # 0xae1e1
movb $0x1, %al
callq 0xe070
movq (%r14), %rdi
callq 0xe530
movl %r15d, %eax
addq $0x148, %rsp            # imm = 0x148
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x9d10b(%rip), %rdi     # 0xae194
jmp 0x10f35
jmp 0x11090
movq %rax, %rbx
leaq 0x10(%rsp), %rdi
callq 0x27a86
movq %rbx, %rdi
callq 0xe7f0