// File: /workspace/github_cpp_makefile_star2_O1/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 0x7c0a8
xorl %r15d, %r15d
movq %r12, %rdi
xorl %esi, %esi
callq 0xe770
movq %rbx, %rdi
movl $0x2e, %esi
callq 0xe4d0
testq %rax, %rax
je 0x10cc2
movq %rax, %r12
leaq 0x9d53c(%rip), %rsi     # 0xae117
movq %rax, %rdi
callq 0xe650
testl %eax, %eax
je 0x10cd8
leaq 0x9d568(%rip), %rsi     # 0xae15a
movq %r12, %rdi
callq 0xe650
testl %eax, %eax
je 0x10cf9
leaq 0x9d563(%rip), %rsi     # 0xae16c
movq %r12, %rdi
callq 0xe650
testl %eax, %eax
je 0x10d22
leaq 0x9d567(%rip), %rsi     # 0xae187
movq %r12, %rdi
callq 0xe650
testl %eax, %eax
je 0x10db4
leaq 0x9d555(%rip), %rsi     # 0xae18c
movq %r12, %rdi
callq 0xe650
testl %eax, %eax
je 0x10e42
leaq 0x9d56f(%rip), %rsi     # 0xae1bd
movq %r12, %rdi
callq 0xe650
testl %eax, %eax
je 0x10ecd
leaq 0x10(%rsp), %rdi
callq 0x27d24
cmpl $0x0, 0xc(%r14)
jle 0x10c9c
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 0x2b178
incq %r12
movslq 0xc(%r14), %rax
cmpq %rax, %r12
jl 0x10c77
leaq 0x10(%rsp), %rdi
movq %rbx, %rsi
callq 0x38e82
movl %eax, %ebp
leaq 0x10(%rsp), %rdi
callq 0x28252
testl %ebp, %ebp
jne 0x10fbc
jmp 0x110bc
leaq 0x9d423(%rip), %rdi     # 0xae0ec
movq %rbx, %rsi
xorl %eax, %eax
callq 0xe070
jmp 0x110bf
testq %rbx, %rbx
je 0x10d4b
leaq 0xa3249(%rip), %rsi     # 0xb3f2d
movq %rbx, %rdi
callq 0xe540
movq %rax, %r15
testq %rax, %rax
jne 0x10d55
jmp 0x10f4c
testq %rbx, %rbx
je 0x10ddd
leaq 0xa3224(%rip), %rsi     # 0xb3f2d
movq %rbx, %rdi
callq 0xe540
movq %rax, %r15
testq %rax, %rax
jne 0x10de7
jmp 0x10f4c
testq %rbx, %rbx
je 0x10e6b
leaq 0xa31fb(%rip), %rsi     # 0xb3f2d
movq %rbx, %rdi
callq 0xe540
movq %rax, %r15
testq %rax, %rax
jne 0x10e75
jmp 0x10f4c
movq 0xe321e(%rip), %rax     # 0xf3f70
movq (%rax), %r15
cmpl $0x0, 0xc(%r14)
jle 0x10faf
leaq 0x9e750(%rip), %r13     # 0xaf4b7
xorl %ebp, %ebp
movq (%r14), %rax
movq (%rax,%rbp,8), %rax
movq (%rax), %r12
testq %r12, %r12
je 0x10d8c
movl 0x70(%r12), %edx
movq %r15, %rdi
movq %r13, %rsi
xorl %eax, %eax
callq 0xe680
jmp 0x10d9a
leaq 0x9d3a8(%rip), %rdi     # 0xae13b
xorl %eax, %eax
callq 0x7ea76
testq %r12, %r12
je 0x110bc
incq %rbp
movslq 0xc(%r14), %rax
cmpq %rax, %rbp
jl 0x10d69
jmp 0x10faf
testq %rbx, %rbx
je 0x10f65
leaq 0xa3169(%rip), %rsi     # 0xb3f2d
movq %rbx, %rdi
callq 0xe540
movq %rax, %r15
testq %rax, %rax
jne 0x10f6f
jmp 0x10f4c
movq 0xe318c(%rip), %rax     # 0xf3f70
movq (%rax), %r15
cmpl $0x0, 0xc(%r14)
jle 0x10faf
xorl %r13d, %r13d
leaq 0x9d363(%rip), %r12     # 0xae15f
movq (%r14), %rax
movq (%rax,%r13,8), %rax
movq (%rax), %rcx
testq %rcx, %rcx
je 0x10e10
movl 0x70(%rcx), %edx
jmp 0x10e15
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 0x10dfc
jmp 0x10faf
testq %rbx, %rbx
je 0x11018
leaq 0x9d33f(%rip), %rsi     # 0xae191
movq %rbx, %rdi
callq 0xe540
movq %rax, %r15
testq %rax, %rax
jne 0x11022
jmp 0x10f4c
movq 0xe30fe(%rip), %rax     # 0xf3f70
movq (%rax), %r15
cmpl $0x0, 0xc(%r14)
jle 0x10faf
xorl %r13d, %r13d
leaq 0x9d2e8(%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 0x10e8a
jmp 0x10faf
leaq 0x9d2bd(%rip), %rsi     # 0xae191
movq %rbx, %rdi
callq 0xe540
testq %rax, %rax
je 0x10f4c
cmpl $0x0, 0xc(%r14)
jle 0x10fbc
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 0x110d4
incq %r13
movslq 0xc(%r14), %rax
cmpq %rax, %r13
jl 0x10ef7
jmp 0x10fbc
leaq 0x9d1c9(%rip), %rdi     # 0xae11c
xorl %r15d, %r15d
movq %rbx, %rsi
xorl %eax, %eax
callq 0x7ea76
jmp 0x110bf
movq 0xe3004(%rip), %rax     # 0xf3f70
movq (%rax), %r15
cmpl $0x0, 0xc(%r14)
jle 0x10faf
xorl %r13d, %r13d
leaq 0x9d1fd(%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 0x10f80
testq %rbx, %rbx
je 0x10fbc
movq %r15, %rdi
callq 0xe440
movl $0x1, %r15d
cmpb $0x1, 0xe3cb7(%rip)     # 0xf4c80
jne 0x110bf
movq 0xe2f9a(%rip), %r14     # 0xf3f70
movq (%r14), %rdi
testq %rbx, %rbx
leaq 0x9d1f7(%rip), %rdx     # 0xae1da
cmovneq %rbx, %rdx
leaq 0x9d1d4(%rip), %rsi     # 0xae1c2
xorl %eax, %eax
callq 0xe680
movq %rsp, %rdi
callq 0x7c10a
leaq 0x9d1dd(%rip), %rdi     # 0xae1e1
movb $0x1, %al
callq 0xe070
movq (%r14), %rdi
callq 0xe530
jmp 0x110bf
movq 0xe2f51(%rip), %rax     # 0xf3f70
movq (%rax), %r15
cmpl $0x0, 0xc(%r14)
setg %bpl
jle 0x11092
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 0x110a1
incq %r13
movslq 0xc(%r14), %rax
cmpq %rax, %r13
setl %bpl
jl 0x11035
testq %rbx, %rbx
je 0x110b2
movq %r15, %rdi
callq 0xe440
jmp 0x110b2
leaq 0x9d0ec(%rip), %rdi     # 0xae194
movq %rbx, %rsi
xorl %eax, %eax
callq 0x7ea76
testb $0x1, %bpl
je 0x10fbc
xorl %r15d, %r15d
movl %r15d, %eax
addq $0x148, %rsp            # imm = 0x148
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x9d0b9(%rip), %rdi     # 0xae194
jmp 0x10f53
jmp 0x110e2
movq %rax, %rbx
leaq 0x10(%rsp), %rdi
callq 0x28252
movq %rbx, %rdi
callq 0xe7f0