// File: /workspace/github_cpp_makefile_star2_O2/tomfunkhouser[P]gaps/pkgs/GSV/GSVMesh.cpp
// Function: gaps::GSVMesh::WriteARFFFile(gaps::GSVScan*, char const*) const
int GSVMesh::
WriteARFFFile(GSVScan *scan, const char *filename) const
{
  // Open file
  FILE *fp = fopen(filename, "w");
  if (!fp) {
    RNFail("Unable to open ARFF file %s\n", filename);
    return 0;
  }
  
  // Write header
  fprintf(fp, "@relation GSV\n");
  fprintf(fp, "\n");

  // Write property names
  fprintf(fp, "@attribute ScanlineIndex real\n");
  fprintf(fp, "@attribute PointIndex real\n");
  fprintf(fp, "@attribute Timestamp real\n");
  fprintf(fp, "@attribute PositionX real\n");
  fprintf(fp, "@attribute PositionY real\n");
  fprintf(fp, "@attribute PositionZ real\n");
  fprintf(fp, "@attribute NormalX real\n");
  fprintf(fp, "@attribute NormalY real\n");
  fprintf(fp, "@attribute NormalZ real\n");
  fprintf(fp, "@attribute GroundZ real\n");
  fprintf(fp, "@attribute Height real\n");
  fprintf(fp, "@attribute LaserDepth real\n");
  fprintf(fp, "@attribute LaserDistance real\n");
  fprintf(fp, "@attribute LaserAngle real\n");
  fprintf(fp, "@attribute LaserViewpointX real\n");
  fprintf(fp, "@attribute LaserViewpointY real\n");
  fprintf(fp, "@attribute LaserViewpointZ real\n");
  fprintf(fp, "@attribute LaserTowardsX real\n");
  fprintf(fp, "@attribute LaserTowardsY real\n");
  fprintf(fp, "@attribute LaserTowardsZ real\n");
  fprintf(fp, "@attribute LaserUpX real\n");
  fprintf(fp, "@attribute LaserUpY real\n");
  fprintf(fp, "@attribute LaserUpZ real\n");
  fprintf(fp, "@attribute BoundaryType real\n");
  fprintf(fp, "\n");
  
  // Write property values (one line per vertex)
  fprintf(fp, "@data\n\n");
  for (int i = 0; i < NVertices(); i++) {
    GSVMeshVertex *vertex = Vertex(i);
    const R3Point& position = VertexPosition(vertex);
    const R3Vector& normal = VertexNormal(vertex);
    GSVScanline *scanline = VertexScanline(vertex);
    RNCoord groundZ = (scanline) ? scanline->EstimatedGroundZ() : -1;
    RNLength height = position.Z() - groundZ;
    R3Point viewpoint = VertexLaserViewpoint(vertex);
    R3Vector towards = VertexLaserTowards(vertex);
    R3Vector up = VertexLaserUp(vertex);
    fprintf(fp, "%d ", VertexScanlineIndex(vertex));
    fprintf(fp, "%d ", VertexPointIndex(vertex));
    fprintf(fp, "%g ", VertexTimestamp(vertex));
    fprintf(fp, "%g ", position.X());
    fprintf(fp, "%g ", position.Y());
    fprintf(fp, "%g ", position.Z());
    fprintf(fp, "%g ", normal.X());
    fprintf(fp, "%g ", normal.Y());
    fprintf(fp, "%g ", normal.Z());
    fprintf(fp, "%g ", groundZ);
    fprintf(fp, "%g ", height);
    fprintf(fp, "%g ", VertexLaserDepth(vertex));
    fprintf(fp, "%g ", VertexLaserDistance(vertex));
    fprintf(fp, "%g ", VertexLaserAngle(vertex));
    fprintf(fp, "%g ", viewpoint.X());
    fprintf(fp, "%g ", viewpoint.Y());
    fprintf(fp, "%g ", viewpoint.Z());
    fprintf(fp, "%g ", towards.X());
    fprintf(fp, "%g ", towards.Y());
    fprintf(fp, "%g ", towards.Z());
    fprintf(fp, "%g ", up.X());
    fprintf(fp, "%g ", up.Y());
    fprintf(fp, "%g ", up.Z());
    fprintf(fp, "%d ", VertexBoundaryType(vertex));
    fprintf(fp, "\n");
  }

  // Close file
  fclose(fp);

  // Return success
  return 1;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x68, %rsp
movq %rdx, %r15
movq %rdi, %rbx
leaq 0xf372b(%rip), %rsi     # 0x134df4
movq %rdx, %rdi
callq 0x27b20
testq %rax, %rax
je 0x41bec
movq %rax, %r14
leaq 0xead65(%rip), %rdi     # 0x12c449
pushq $0xe
popq %rsi
pushq $0x1
popq %r15
movq %r15, %rdx
movq %rax, %rcx
callq 0x28350
pushq $0xa
popq %rdi
movq %r14, %rsi
callq 0x27c30
leaq 0xead50(%rip), %rdi     # 0x12c458
pushq $0x1e
popq %r12
movq %r12, %rsi
movq %r15, %rdx
movq %r14, %rcx
callq 0x28350
leaq 0xead56(%rip), %rdi     # 0x12c477
pushq $0x1b
popq %r13
movq %r13, %rsi
movq %r15, %rdx
movq %r14, %rcx
callq 0x28350
leaq 0xead59(%rip), %rdi     # 0x12c493
pushq $0x1a
popq %rbp
movq %rbp, %rsi
movq %r15, %rdx
movq %r14, %rcx
callq 0x28350
leaq 0xead5c(%rip), %rdi     # 0x12c4ae
movq %rbp, %rsi
movq %r15, %rdx
movq %r14, %rcx
callq 0x28350
leaq 0xead62(%rip), %rdi     # 0x12c4c9
movq %rbp, %rsi
movq %r15, %rdx
movq %r14, %rcx
callq 0x28350
leaq 0xead68(%rip), %rdi     # 0x12c4e4
movq %rbp, %rsi
movq %r15, %rdx
movq %r14, %rcx
callq 0x28350
leaq 0xead6e(%rip), %rdi     # 0x12c4ff
pushq $0x18
popq %rbp
movq %rbp, %rsi
movq %r15, %rdx
movq %r14, %rcx
callq 0x28350
leaq 0xead6f(%rip), %rdi     # 0x12c518
movq %rbp, %rsi
movq %r15, %rdx
movq %r14, %rcx
callq 0x28350
leaq 0xead73(%rip), %rdi     # 0x12c531
movq %rbp, %rsi
movq %r15, %rdx
movq %r14, %rcx
callq 0x28350
leaq 0xead77(%rip), %rdi     # 0x12c54a
movq %rbp, %rsi
movq %r15, %rdx
movq %r14, %rcx
callq 0x28350
leaq 0xead7b(%rip), %rdi     # 0x12c563
pushq $0x17
popq %rsi
movq %r15, %rdx
movq %r14, %rcx
callq 0x28350
leaq 0xead7e(%rip), %rdi     # 0x12c57b
movq %r13, %rsi
movq %r15, %rdx
movq %r14, %rcx
callq 0x28350
leaq 0xead85(%rip), %rdi     # 0x12c597
movq %r12, %rsi
movq %r15, %rdx
movq %r14, %rcx
callq 0x28350
leaq 0xead8f(%rip), %rdi     # 0x12c5b6
movq %r13, %rsi
movq %r15, %rdx
movq %r14, %rcx
callq 0x28350
leaq 0xead96(%rip), %rdi     # 0x12c5d2
pushq $0x20
popq %r13
movq %r13, %rsi
movq %r15, %rdx
movq %r14, %rcx
callq 0x28350
leaq 0xead9e(%rip), %rdi     # 0x12c5f3
movq %r13, %rsi
movq %r15, %rdx
movq %r14, %rcx
callq 0x28350
leaq 0xeadaa(%rip), %rdi     # 0x12c614
movq %r13, %rsi
movq %r15, %rdx
movq %r14, %rcx
callq 0x28350
leaq 0xeadb6(%rip), %rdi     # 0x12c635
movq %r12, %rsi
movq %r15, %rdx
movq %r14, %rcx
callq 0x28350
leaq 0xeadc0(%rip), %rdi     # 0x12c654
movq %r12, %rsi
movq %r15, %rdx
movq %r14, %rcx
callq 0x28350
leaq 0xeadca(%rip), %rdi     # 0x12c673
movq %r12, %rsi
movq %r15, %rdx
movq %r14, %rcx
callq 0x28350
leaq 0xeadd4(%rip), %rdi     # 0x12c692
pushq $0x19
popq %r12
movq %r12, %rsi
movq %r15, %rdx
movq %r14, %rcx
callq 0x28350
leaq 0xeadd5(%rip), %rdi     # 0x12c6ac
movq %r12, %rsi
movq %r15, %rdx
movq %r14, %rcx
callq 0x28350
leaq 0xeadda(%rip), %rdi     # 0x12c6c6
movq %r12, %rsi
movq %r15, %rdx
movq %r14, %rcx
callq 0x28350
leaq 0xeaddf(%rip), %rdi     # 0x12c6e0
pushq $0x1d
popq %rsi
movq %r15, %rdx
movq %r14, %rcx
callq 0x28350
pushq $0xa
popq %rdi
movq %r14, %rsi
callq 0x27c30
leaq 0xeaddd(%rip), %rdi     # 0x12c6fe
pushq $0x7
popq %rsi
movq %r15, %rdx
movq %r14, %rcx
callq 0x28350
leaq 0xeb3c5(%rip), %r15     # 0x12ccfb
xorl %ebp, %ebp
movq %rbx, 0x18(%rsp)
movslq 0x14(%rbx), %rax
cmpq %rax, %rbp
jge 0x41bdf
movq 0x8(%rbx), %rax
movq (%rax,%rbp,8), %r12
movq %rbx, %rdi
movq %r12, %rsi
callq 0x30eae
movq %rax, %r13
movq 0x98(%r12), %rdi
movsd 0xe96f8(%rip), %xmm0    # 0x12b068
testq %rdi, %rdi
je 0x4197a
callq 0x3e1c0
movsd %xmm0, 0x10(%rsp)
movsd 0x28(%r12), %xmm0
movsd %xmm0, 0x8(%rsp)
leaq 0x50(%rsp), %rdi
movq %r12, %rdx
callq 0x3fe76
leaq 0x38(%rsp), %rdi
movq %r12, %rdx
callq 0x3fea0
leaq 0x20(%rsp), %rdi
movq %r12, %rdx
callq 0x3fed2
movq 0x98(%r12), %rax
pushq $-0x1
popq %rdx
testq %rax, %rax
je 0x419c7
movl 0x10(%rax), %edx
movq %r14, %rdi
leaq 0xeb31f(%rip), %rbx     # 0x12ccf0
movq %rbx, %rsi
xorl %eax, %eax
callq 0x27e20
movl 0xa0(%r12), %edx
movq %r14, %rdi
movq %rbx, %rsi
xorl %eax, %eax
callq 0x27e20
movq 0x98(%r12), %rax
movsd 0xe9668(%rip), %xmm0    # 0x12b068
testq %rax, %rax
je 0x41a0a
movsd 0x78(%rax), %xmm0
movsd 0x8(%rsp), %xmm1
subsd 0x10(%rsp), %xmm1
movsd %xmm1, 0x8(%rsp)
movq %r14, %rdi
movq %r15, %rsi
movb $0x1, %al
callq 0x27e20
movsd 0x18(%r12), %xmm0
movq %r14, %rdi
movq %r15, %rsi
movb $0x1, %al
callq 0x27e20
movsd 0x20(%r12), %xmm0
movq %r14, %rdi
movq %r15, %rsi
movb $0x1, %al
callq 0x27e20
movsd 0x28(%r12), %xmm0
movq %r14, %rdi
movq %r15, %rsi
movb $0x1, %al
callq 0x27e20
movsd (%r13), %xmm0
movq %r14, %rdi
movq %r15, %rsi
movb $0x1, %al
callq 0x27e20
movsd 0x8(%r13), %xmm0
movq %r14, %rdi
movq %r15, %rsi
movb $0x1, %al
callq 0x27e20
movsd 0x10(%r13), %xmm0
movq %r14, %rdi
movq %r15, %rsi
movb $0x1, %al
callq 0x27e20
movq %r14, %rdi
movq %r15, %rsi
movsd 0x10(%rsp), %xmm0
movb $0x1, %al
callq 0x27e20
movq %r14, %rdi
movq %r15, %rsi
movsd 0x8(%rsp), %xmm0
movb $0x1, %al
callq 0x27e20
movq %r12, %rsi
callq 0x3fde8
movq %r14, %rdi
movq %r15, %rsi
movb $0x1, %al
callq 0x27e20
movq %r12, %rsi
callq 0x3fe50
movq %r14, %rdi
movq %r15, %rsi
movb $0x1, %al
callq 0x27e20
movq %r12, %rsi
callq 0x3fcf2
movq %r14, %rdi
movq %r15, %rsi
movb $0x1, %al
callq 0x27e20
movsd 0x50(%rsp), %xmm0
movq %r14, %rdi
movq %r15, %rsi
movb $0x1, %al
callq 0x27e20
movsd 0x58(%rsp), %xmm0
movq %r14, %rdi
movq %r15, %rsi
movb $0x1, %al
callq 0x27e20
movsd 0x60(%rsp), %xmm0
movq %r14, %rdi
movq %r15, %rsi
movb $0x1, %al
callq 0x27e20
movsd 0x38(%rsp), %xmm0
movq %r14, %rdi
movq %r15, %rsi
movb $0x1, %al
callq 0x27e20
movsd 0x40(%rsp), %xmm0
movq %r14, %rdi
movq %r15, %rsi
movb $0x1, %al
callq 0x27e20
movsd 0x48(%rsp), %xmm0
movq %r14, %rdi
movq %r15, %rsi
movb $0x1, %al
callq 0x27e20
movsd 0x20(%rsp), %xmm0
movq %r14, %rdi
movq %r15, %rsi
movb $0x1, %al
callq 0x27e20
movsd 0x28(%rsp), %xmm0
movq %r14, %rdi
movq %r15, %rsi
movb $0x1, %al
callq 0x27e20
movsd 0x30(%rsp), %xmm0
movq %r14, %rdi
movq %r15, %rsi
movb $0x1, %al
callq 0x27e20
movl 0xa4(%r12), %edx
movq %r14, %rdi
leaq 0xeb130(%rip), %rsi     # 0x12ccf0
xorl %eax, %eax
callq 0x27e20
pushq $0xa
popq %rdi
movq %r14, %rsi
callq 0x27c30
incq %rbp
movq 0x18(%rsp), %rbx
jmp 0x4193d
movq %r14, %rdi
callq 0x27930
pushq $0x1
popq %rbx
jmp 0x41bff
leaq 0xea839(%rip), %rdi     # 0x12c42c
xorl %ebx, %ebx
movq %r15, %rsi
xorl %eax, %eax
callq 0xe434a
movl %ebx, %eax
addq $0x68, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq