// File: /workspace/github_cpp_makefile_star2_O3/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 $0x58, %rsp
movq %rdx, %r15
movq %rdi, %rbx
leaq 0x111583(%rip), %rsi    # 0x155ec4
movq %rdx, %rdi
callq 0x27b20
testq %rax, %rax
je 0x44efa
movq %rax, %r14
leaq 0x108b2d(%rip), %rdi    # 0x14d489
movl $0xe, %esi
movl $0x1, %edx
movq %rax, %rcx
callq 0x28320
movl $0xa, %edi
movq %r14, %rsi
callq 0x27c30
leaq 0x108b16(%rip), %rdi    # 0x14d498
movl $0x1e, %esi
movl $0x1, %edx
movq %r14, %rcx
callq 0x28320
leaq 0x108b1c(%rip), %rdi    # 0x14d4b7
movl $0x1b, %esi
movl $0x1, %edx
movq %r14, %rcx
callq 0x28320
leaq 0x108b1f(%rip), %rdi    # 0x14d4d3
movl $0x1a, %esi
movl $0x1, %edx
movq %r14, %rcx
callq 0x28320
leaq 0x108b21(%rip), %rdi    # 0x14d4ee
movl $0x1a, %esi
movl $0x1, %edx
movq %r14, %rcx
callq 0x28320
leaq 0x108b23(%rip), %rdi    # 0x14d509
movl $0x1a, %esi
movl $0x1, %edx
movq %r14, %rcx
callq 0x28320
leaq 0x108b25(%rip), %rdi    # 0x14d524
movl $0x1a, %esi
movl $0x1, %edx
movq %r14, %rcx
callq 0x28320
leaq 0x108b27(%rip), %rdi    # 0x14d53f
movl $0x18, %esi
movl $0x1, %edx
movq %r14, %rcx
callq 0x28320
leaq 0x108b27(%rip), %rdi    # 0x14d558
movl $0x18, %esi
movl $0x1, %edx
movq %r14, %rcx
callq 0x28320
leaq 0x108b27(%rip), %rdi    # 0x14d571
movl $0x18, %esi
movl $0x1, %edx
movq %r14, %rcx
callq 0x28320
leaq 0x108b27(%rip), %rdi    # 0x14d58a
movl $0x18, %esi
movl $0x1, %edx
movq %r14, %rcx
callq 0x28320
leaq 0x108b27(%rip), %rdi    # 0x14d5a3
movl $0x17, %esi
movl $0x1, %edx
movq %r14, %rcx
callq 0x28320
leaq 0x108b26(%rip), %rdi    # 0x14d5bb
movl $0x1b, %esi
movl $0x1, %edx
movq %r14, %rcx
callq 0x28320
leaq 0x108b29(%rip), %rdi    # 0x14d5d7
movl $0x1e, %esi
movl $0x1, %edx
movq %r14, %rcx
callq 0x28320
leaq 0x108b2f(%rip), %rdi    # 0x14d5f6
movl $0x1b, %esi
movl $0x1, %edx
movq %r14, %rcx
callq 0x28320
leaq 0x108b32(%rip), %rdi    # 0x14d612
movl $0x20, %esi
movl $0x1, %edx
movq %r14, %rcx
callq 0x28320
leaq 0x108b3a(%rip), %rdi    # 0x14d633
movl $0x20, %esi
movl $0x1, %edx
movq %r14, %rcx
callq 0x28320
leaq 0x108b42(%rip), %rdi    # 0x14d654
movl $0x20, %esi
movl $0x1, %edx
movq %r14, %rcx
callq 0x28320
leaq 0x108b4a(%rip), %rdi    # 0x14d675
movl $0x1e, %esi
movl $0x1, %edx
movq %r14, %rcx
callq 0x28320
leaq 0x108b50(%rip), %rdi    # 0x14d694
movl $0x1e, %esi
movl $0x1, %edx
movq %r14, %rcx
callq 0x28320
leaq 0x108b56(%rip), %rdi    # 0x14d6b3
movl $0x1e, %esi
movl $0x1, %edx
movq %r14, %rcx
callq 0x28320
leaq 0x108b5c(%rip), %rdi    # 0x14d6d2
movl $0x19, %esi
movl $0x1, %edx
movq %r14, %rcx
callq 0x28320
leaq 0x108b5d(%rip), %rdi    # 0x14d6ec
movl $0x19, %esi
movl $0x1, %edx
movq %r14, %rcx
callq 0x28320
leaq 0x108b5e(%rip), %rdi    # 0x14d706
movl $0x19, %esi
movl $0x1, %edx
movq %r14, %rcx
callq 0x28320
leaq 0x108b5f(%rip), %rdi    # 0x14d720
movl $0x1d, %esi
movl $0x1, %edx
movq %r14, %rcx
callq 0x28320
movl $0xa, %edi
movq %r14, %rsi
callq 0x27c30
leaq 0x108b57(%rip), %rdi    # 0x14d73e
movl $0x7, %esi
movl $0x1, %edx
movq %r14, %rcx
callq 0x28320
cmpl $0x0, 0x14(%rbx)
jle 0x44eeb
leaq 0x109126(%rip), %rbp    # 0x14dd30
leaq 0x10912a(%rip), %r15    # 0x14dd3b
xorl %r13d, %r13d
movq 0x8(%rbx), %rax
movq (%rax,%r13,8), %r12
testb $0x2, 0x78(%r12)
jne 0x44c33
movq (%rbx), %rax
movq %rbx, %rdi
movq %r12, %rsi
callq *0x2b0(%rax)
movq 0x98(%r12), %rdi
testq %rdi, %rdi
je 0x44c54
callq 0x40e10
movsd %xmm0, (%rsp)
movq 0x98(%r12), %rsi
jmp 0x44c63
xorl %esi, %esi
movsd 0x107412(%rip), %xmm0   # 0x14c070
movsd %xmm0, (%rsp)
movsd 0x28(%r12), %xmm0
movsd %xmm0, 0x8(%rsp)
testq %rsi, %rsi
jne 0x44c7e
leaq 0x59a9c4(%rip), %rsi    # 0x5df640
jmp 0x44c82
addq $0x40, %rsi
leaq 0x40(%rsp), %rdi
callq 0xd43ee
leaq 0x28(%rsp), %rdi
movq %r12, %rdx
callq 0x42f0a
leaq 0x10(%rsp), %rdi
movq %r12, %rdx
callq 0x42f3c
movq 0x98(%r12), %rax
testq %rax, %rax
je 0x44cb8
movl 0x10(%rax), %edx
jmp 0x44cbd
movl $0xffffffff, %edx       # imm = 0xFFFFFFFF
movq %r14, %rdi
movq %rbp, %rsi
xorl %eax, %eax
callq 0x27e00
movl 0xa0(%r12), %edx
movq %r14, %rdi
movq %rbp, %rsi
xorl %eax, %eax
callq 0x27e00
movq 0x98(%r12), %rax
movsd 0x107381(%rip), %xmm0   # 0x14c070
testq %rax, %rax
je 0x44cf9
movsd 0x78(%rax), %xmm0
movsd 0x8(%rsp), %xmm1
subsd (%rsp), %xmm1
movsd %xmm1, 0x8(%rsp)
movq %r14, %rdi
movq %r15, %rsi
movb $0x1, %al
callq 0x27e00
movsd 0x18(%r12), %xmm0
movq %r14, %rdi
movq %r15, %rsi
movb $0x1, %al
callq 0x27e00
movsd 0x20(%r12), %xmm0
movq %r14, %rdi
movq %r15, %rsi
movb $0x1, %al
callq 0x27e00
movsd 0x28(%r12), %xmm0
movq %r14, %rdi
movq %r15, %rsi
movb $0x1, %al
callq 0x27e00
movsd 0x30(%r12), %xmm0
movq %r14, %rdi
movq %r15, %rsi
movb $0x1, %al
callq 0x27e00
movsd 0x38(%r12), %xmm0
movq %r14, %rdi
movq %r15, %rsi
movb $0x1, %al
callq 0x27e00
movsd 0x40(%r12), %xmm0
movq %r14, %rdi
movq %r15, %rsi
movb $0x1, %al
callq 0x27e00
movq %r14, %rdi
movq %r15, %rsi
movsd (%rsp), %xmm0
movb $0x1, %al
callq 0x27e00
movq %r14, %rdi
movq %r15, %rsi
movsd 0x8(%rsp), %xmm0
movb $0x1, %al
callq 0x27e00
movq %r12, %rsi
callq 0x42e52
movq %r14, %rdi
movq %r15, %rsi
movb $0x1, %al
callq 0x27e00
movq 0x98(%r12), %rsi
movsd 0x107297(%rip), %xmm0   # 0x14c070
testq %rsi, %rsi
je 0x44dec
leaq 0x18(%r12), %rdi
addq $0x40, %rsi
callq 0x9c174
movq %r14, %rdi
movq %r15, %rsi
movb $0x1, %al
callq 0x27e00
movq %r12, %rsi
callq 0x42d5c
movq %r14, %rdi
movq %r15, %rsi
movb $0x1, %al
callq 0x27e00
movsd 0x40(%rsp), %xmm0
movq %r14, %rdi
movq %r15, %rsi
movb $0x1, %al
callq 0x27e00
movsd 0x48(%rsp), %xmm0
movq %r14, %rdi
movq %r15, %rsi
movb $0x1, %al
callq 0x27e00
movsd 0x50(%rsp), %xmm0
movq %r14, %rdi
movq %r15, %rsi
movb $0x1, %al
callq 0x27e00
movsd 0x28(%rsp), %xmm0
movq %r14, %rdi
movq %r15, %rsi
movb $0x1, %al
callq 0x27e00
movsd 0x30(%rsp), %xmm0
movq %r14, %rdi
movq %r15, %rsi
movb $0x1, %al
callq 0x27e00
movsd 0x38(%rsp), %xmm0
movq %r14, %rdi
movq %r15, %rsi
movb $0x1, %al
callq 0x27e00
movsd 0x10(%rsp), %xmm0
movq %r14, %rdi
movq %r15, %rsi
movb $0x1, %al
callq 0x27e00
movsd 0x18(%rsp), %xmm0
movq %r14, %rdi
movq %r15, %rsi
movb $0x1, %al
callq 0x27e00
movsd 0x20(%rsp), %xmm0
movq %r14, %rdi
movq %r15, %rsi
movb $0x1, %al
callq 0x27e00
movl 0xa4(%r12), %edx
movq %r14, %rdi
movq %rbp, %rsi
xorl %eax, %eax
callq 0x27e00
movl $0xa, %edi
movq %r14, %rsi
callq 0x27c30
incq %r13
movslq 0x14(%rbx), %rax
cmpq %rax, %r13
jl 0x44c14
movq %r14, %rdi
callq 0x27920
movl $0x1, %ebx
jmp 0x44f0d
leaq 0x10856b(%rip), %rdi    # 0x14d46c
xorl %ebx, %ebx
movq %r15, %rsi
xorl %eax, %eax
callq 0xfb6f2
movl %ebx, %eax
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq