// File: /workspace/github_cpp_makefile_star2_O1/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 0x11306d(%rip), %rsi    # 0x157e98
movq %rdx, %rdi
callq 0x27b30
testq %rax, %rax
je 0x453c3
movq %rax, %r14
leaq 0x10a613(%rip), %rdi    # 0x14f459
movl $0xe, %esi
movl $0x1, %edx
movq %rax, %rcx
callq 0x28330
movl $0xa, %edi
movq %r14, %rsi
callq 0x27c40
leaq 0x10a5fc(%rip), %rdi    # 0x14f468
movl $0x1e, %esi
movl $0x1, %edx
movq %r14, %rcx
callq 0x28330
leaq 0x10a602(%rip), %rdi    # 0x14f487
movl $0x1b, %esi
movl $0x1, %edx
movq %r14, %rcx
callq 0x28330
leaq 0x10a605(%rip), %rdi    # 0x14f4a3
movl $0x1a, %esi
movl $0x1, %edx
movq %r14, %rcx
callq 0x28330
leaq 0x10a607(%rip), %rdi    # 0x14f4be
movl $0x1a, %esi
movl $0x1, %edx
movq %r14, %rcx
callq 0x28330
leaq 0x10a609(%rip), %rdi    # 0x14f4d9
movl $0x1a, %esi
movl $0x1, %edx
movq %r14, %rcx
callq 0x28330
leaq 0x10a60b(%rip), %rdi    # 0x14f4f4
movl $0x1a, %esi
movl $0x1, %edx
movq %r14, %rcx
callq 0x28330
leaq 0x10a60d(%rip), %rdi    # 0x14f50f
movl $0x18, %esi
movl $0x1, %edx
movq %r14, %rcx
callq 0x28330
leaq 0x10a60d(%rip), %rdi    # 0x14f528
movl $0x18, %esi
movl $0x1, %edx
movq %r14, %rcx
callq 0x28330
leaq 0x10a60d(%rip), %rdi    # 0x14f541
movl $0x18, %esi
movl $0x1, %edx
movq %r14, %rcx
callq 0x28330
leaq 0x10a60d(%rip), %rdi    # 0x14f55a
movl $0x18, %esi
movl $0x1, %edx
movq %r14, %rcx
callq 0x28330
leaq 0x10a60d(%rip), %rdi    # 0x14f573
movl $0x17, %esi
movl $0x1, %edx
movq %r14, %rcx
callq 0x28330
leaq 0x10a60c(%rip), %rdi    # 0x14f58b
movl $0x1b, %esi
movl $0x1, %edx
movq %r14, %rcx
callq 0x28330
leaq 0x10a60f(%rip), %rdi    # 0x14f5a7
movl $0x1e, %esi
movl $0x1, %edx
movq %r14, %rcx
callq 0x28330
leaq 0x10a615(%rip), %rdi    # 0x14f5c6
movl $0x1b, %esi
movl $0x1, %edx
movq %r14, %rcx
callq 0x28330
leaq 0x10a618(%rip), %rdi    # 0x14f5e2
movl $0x20, %esi
movl $0x1, %edx
movq %r14, %rcx
callq 0x28330
leaq 0x10a620(%rip), %rdi    # 0x14f603
movl $0x20, %esi
movl $0x1, %edx
movq %r14, %rcx
callq 0x28330
leaq 0x10a628(%rip), %rdi    # 0x14f624
movl $0x20, %esi
movl $0x1, %edx
movq %r14, %rcx
callq 0x28330
leaq 0x10a630(%rip), %rdi    # 0x14f645
movl $0x1e, %esi
movl $0x1, %edx
movq %r14, %rcx
callq 0x28330
leaq 0x10a636(%rip), %rdi    # 0x14f664
movl $0x1e, %esi
movl $0x1, %edx
movq %r14, %rcx
callq 0x28330
leaq 0x10a63c(%rip), %rdi    # 0x14f683
movl $0x1e, %esi
movl $0x1, %edx
movq %r14, %rcx
callq 0x28330
leaq 0x10a642(%rip), %rdi    # 0x14f6a2
movl $0x19, %esi
movl $0x1, %edx
movq %r14, %rcx
callq 0x28330
leaq 0x10a643(%rip), %rdi    # 0x14f6bc
movl $0x19, %esi
movl $0x1, %edx
movq %r14, %rcx
callq 0x28330
leaq 0x10a644(%rip), %rdi    # 0x14f6d6
movl $0x19, %esi
movl $0x1, %edx
movq %r14, %rcx
callq 0x28330
leaq 0x10a645(%rip), %rdi    # 0x14f6f0
movl $0x1d, %esi
movl $0x1, %edx
movq %r14, %rcx
callq 0x28330
movl $0xa, %edi
movq %r14, %rsi
callq 0x27c40
leaq 0x10a63d(%rip), %rdi    # 0x14f70e
movl $0x7, %esi
movl $0x1, %edx
movq %r14, %rcx
callq 0x28330
cmpl $0x0, 0x14(%rbx)
jle 0x453b4
leaq 0x10ac0c(%rip), %rbp    # 0x14fd00
leaq 0x10ac10(%rip), %r15    # 0x14fd0b
xorl %r13d, %r13d
movq 0x8(%rbx), %rax
movq (%rax,%r13,8), %r12
testb $0x2, 0x78(%r12)
jne 0x4511d
movq (%rbx), %rax
movq %rbx, %rdi
movq %r12, %rsi
callq *0x2b0(%rax)
movq 0x98(%r12), %rdi
movsd 0x108f43(%rip), %xmm0   # 0x14e070
testq %rdi, %rdi
je 0x45137
callq 0x412bc
movsd %xmm0, 0x8(%rsp)
movsd 0x28(%r12), %xmm0
movsd %xmm0, (%rsp)
leaq 0x40(%rsp), %rdi
movq %r12, %rdx
callq 0x433a8
leaq 0x28(%rsp), %rdi
movq %r12, %rdx
callq 0x433d2
leaq 0x10(%rsp), %rdi
movq %r12, %rdx
callq 0x43404
movq 0x98(%r12), %rax
testq %rax, %rax
je 0x45182
movl 0x10(%rax), %edx
jmp 0x45187
movl $0xffffffff, %edx       # imm = 0xFFFFFFFF
movq %r14, %rdi
movq %rbp, %rsi
xorl %eax, %eax
callq 0x27e10
movl 0xa0(%r12), %edx
movq %r14, %rdi
movq %rbp, %rsi
xorl %eax, %eax
callq 0x27e10
movq 0x98(%r12), %rax
movsd 0x108eb7(%rip), %xmm0   # 0x14e070
testq %rax, %rax
je 0x451c3
movsd 0x78(%rax), %xmm0
movsd (%rsp), %xmm1
subsd 0x8(%rsp), %xmm1
movsd %xmm1, (%rsp)
movq %r14, %rdi
movq %r15, %rsi
movb $0x1, %al
callq 0x27e10
movsd 0x18(%r12), %xmm0
movq %r14, %rdi
movq %r15, %rsi
movb $0x1, %al
callq 0x27e10
movsd 0x20(%r12), %xmm0
movq %r14, %rdi
movq %r15, %rsi
movb $0x1, %al
callq 0x27e10
movsd 0x28(%r12), %xmm0
movq %r14, %rdi
movq %r15, %rsi
movb $0x1, %al
callq 0x27e10
movsd 0x30(%r12), %xmm0
movq %r14, %rdi
movq %r15, %rsi
movb $0x1, %al
callq 0x27e10
movsd 0x38(%r12), %xmm0
movq %r14, %rdi
movq %r15, %rsi
movb $0x1, %al
callq 0x27e10
movsd 0x40(%r12), %xmm0
movq %r14, %rdi
movq %r15, %rsi
movb $0x1, %al
callq 0x27e10
movq %r14, %rdi
movq %r15, %rsi
movsd 0x8(%rsp), %xmm0
movb $0x1, %al
callq 0x27e10
movq %r14, %rdi
movq %r15, %rsi
movsd (%rsp), %xmm0
movb $0x1, %al
callq 0x27e10
movq %r12, %rsi
callq 0x4331a
movq %r14, %rdi
movq %r15, %rsi
movb $0x1, %al
callq 0x27e10
movq 0x98(%r12), %rsi
movsd 0x108dce(%rip), %xmm0   # 0x14e070
testq %rsi, %rsi
je 0x452b5
leaq 0x18(%r12), %rdi
addq $0x40, %rsi
callq 0x9d528
movq %r14, %rdi
movq %r15, %rsi
movb $0x1, %al
callq 0x27e10
movq %r12, %rsi
callq 0x43224
movq %r14, %rdi
movq %r15, %rsi
movb $0x1, %al
callq 0x27e10
movsd 0x40(%rsp), %xmm0
movq %r14, %rdi
movq %r15, %rsi
movb $0x1, %al
callq 0x27e10
movsd 0x48(%rsp), %xmm0
movq %r14, %rdi
movq %r15, %rsi
movb $0x1, %al
callq 0x27e10
movsd 0x50(%rsp), %xmm0
movq %r14, %rdi
movq %r15, %rsi
movb $0x1, %al
callq 0x27e10
movsd 0x28(%rsp), %xmm0
movq %r14, %rdi
movq %r15, %rsi
movb $0x1, %al
callq 0x27e10
movsd 0x30(%rsp), %xmm0
movq %r14, %rdi
movq %r15, %rsi
movb $0x1, %al
callq 0x27e10
movsd 0x38(%rsp), %xmm0
movq %r14, %rdi
movq %r15, %rsi
movb $0x1, %al
callq 0x27e10
movsd 0x10(%rsp), %xmm0
movq %r14, %rdi
movq %r15, %rsi
movb $0x1, %al
callq 0x27e10
movsd 0x18(%rsp), %xmm0
movq %r14, %rdi
movq %r15, %rsi
movb $0x1, %al
callq 0x27e10
movsd 0x20(%rsp), %xmm0
movq %r14, %rdi
movq %r15, %rsi
movb $0x1, %al
callq 0x27e10
movl 0xa4(%r12), %edx
movq %r14, %rdi
movq %rbp, %rsi
xorl %eax, %eax
callq 0x27e10
movl $0xa, %edi
movq %r14, %rsi
callq 0x27c40
incq %r13
movslq 0x14(%rbx), %rax
cmpq %rax, %r13
jl 0x450fe
movq %r14, %rdi
callq 0x27920
movl $0x1, %ebx
jmp 0x453d6
leaq 0x10a072(%rip), %rdi    # 0x14f43c
xorl %ebx, %ebx
movq %r15, %rsi
xorl %eax, %eax
callq 0xfd64a
movl %ebx, %eax
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
nop