// File: tomfunkhouser[P]gaps/pkgs/R3Utils/R3Segmentation.cpp
// Function: gaps::R3Segmentation::WriteFile(char const*) const
int R3Segmentation::
WriteFile(const char *filename) const
{
  // Open file
  FILE *fp = fopen(filename, "w");
  if (!fp) {
    RNFail("Unable to open segmentation file %s\n", filename);
    return 0;
  }

  // Write clusters to file
  for (int i = 0; i < clusters.NEntries(); i++) {
    R3SegmentationCluster *cluster = clusters.Kth(i);
    R3Point center;
    RNScalar variances[3];
    R3Triad axes = cluster->PrincipleAxes(&center, variances);
    R3Box extent = cluster->AxisExtents();
    fprintf(fp, "%d %d %g %g %g %d  %g %g %g   %g %g %g %g   %g %g %g  %g  %g %g %g   %g %g %g  %g %g %g  %g %g %g   %g %g %g   %g %g %g  %g %g %g\n",
            i+1, cluster->points.NEntries(), cluster->area,
            cluster->total_affinity, cluster->possible_affinity, cluster->primitive.primitive_type,
            cluster->primitive.centroid.X(), cluster->primitive.centroid.Y(), cluster->primitive.centroid.Z(),
            cluster->primitive.plane.A(), cluster->primitive.plane.B(), cluster->primitive.plane.C(), cluster->primitive.plane.D(),
            cluster->color.R(), cluster->color.G(), cluster->color.B(),
            cluster->timestamp,
            center.X(), center.Y(), center.Z(),
            axes[0][0], axes[0][1], axes[0][2], axes[1][0], axes[1][1], axes[1][2], axes[2][0], axes[2][1], axes[2][2],
            variances[0], variances[1], variances[2],
            extent[0][0], extent[0][1], extent[0][2], extent[1][0], extent[1][1], extent[1][2]);
  }

  // Close file
  fclose(fp);

  // Return success
  return 1;
}

// Assembly:
subq $0x3b8, %rsp            # imm = 0x3B8
movq %rdi, 0x3a8(%rsp)
movq %rsi, 0x3a0(%rsp)
movq 0x3a8(%rsp), %rax
movq %rax, 0x2b8(%rsp)
movq 0x3a0(%rsp), %rdi
leaq 0x1bac18(%rip), %rsi    # 0x2013ae
callq 0x168d0
movq %rax, 0x398(%rsp)
cmpq $0x0, 0x398(%rsp)
jne 0x467d4
movq 0x3a0(%rsp), %rsi
leaq 0x1b739c(%rip), %rdi    # 0x1fdb59
movb $0x0, %al
callq 0x1779b0
movl $0x0, 0x3b4(%rsp)
jmp 0x47112
movl $0x0, 0x394(%rsp)
movq 0x2b8(%rsp), %rdi
movl 0x394(%rsp), %eax
movl %eax, 0x2b4(%rsp)
addq $0x18, %rdi
callq 0x1ecf0
movl %eax, %ecx
movl 0x2b4(%rsp), %eax
cmpl %ecx, %eax
jge 0x470fa
movq 0x2b8(%rsp), %rdi
addq $0x18, %rdi
movl 0x394(%rsp), %esi
callq 0x20270
movq %rax, 0x388(%rsp)
leaq 0x370(%rsp), %rdi
movq %rdi, 0x298(%rsp)
callq 0x12ad40
movq 0x298(%rsp), %rdx
movq 0x388(%rsp), %rsi
leaq 0x308(%rsp), %rdi
leaq 0x350(%rsp), %rcx
callq 0x3f030
movq 0x388(%rsp), %rsi
leaq 0x2d0(%rsp), %rdi
callq 0x3f440
movq 0x398(%rsp), %rax
movq %rax, 0x2a0(%rsp)
movl 0x394(%rsp), %eax
incl %eax
movl %eax, 0x2ac(%rsp)
movq 0x388(%rsp), %rdi
addq $0x8, %rdi
callq 0x1ecf0
movl %eax, 0x2b0(%rsp)
jmp 0x468b8
movq 0x388(%rsp), %rdi
movsd 0xe8(%rdi), %xmm0
movsd %xmm0, 0x270(%rsp)
movsd 0x128(%rdi), %xmm0
movsd %xmm0, 0x278(%rsp)
movsd 0x120(%rdi), %xmm0
movsd %xmm0, 0x280(%rsp)
movl 0x40(%rdi), %eax
movl %eax, 0x28c(%rsp)
subq $-0x80, %rdi
callq 0x1efb0
movsd %xmm0, 0x290(%rsp)
jmp 0x46911
movq 0x388(%rsp), %rdi
subq $-0x80, %rdi
callq 0x1efc0
movsd %xmm0, 0x268(%rsp)
jmp 0x4692d
movq 0x388(%rsp), %rdi
subq $-0x80, %rdi
callq 0x1efd0
movsd %xmm0, 0x260(%rsp)
jmp 0x46949
movq 0x388(%rsp), %rdi
addq $0xc8, %rdi
callq 0x48220
movsd %xmm0, 0x258(%rsp)
jmp 0x46968
movq 0x388(%rsp), %rdi
addq $0xc8, %rdi
callq 0x48240
movsd %xmm0, 0x250(%rsp)
jmp 0x46987
movq 0x388(%rsp), %rdi
addq $0xc8, %rdi
callq 0x48260
movsd %xmm0, 0x248(%rsp)
jmp 0x469a6
movq 0x388(%rsp), %rdi
addq $0xc8, %rdi
callq 0x48280
movsd %xmm0, 0x240(%rsp)
jmp 0x469c5
movq 0x388(%rsp), %rdi
addq $0xf0, %rdi
callq 0x1f700
movsd %xmm0, 0x238(%rsp)
jmp 0x469e4
movq 0x388(%rsp), %rdi
addq $0xf0, %rdi
callq 0x1f710
movsd %xmm0, 0x230(%rsp)
jmp 0x46a03
movq 0x388(%rsp), %rdi
addq $0xf0, %rdi
callq 0x1f720
movsd %xmm0, 0x228(%rsp)
jmp 0x46a22
movq 0x388(%rsp), %rax
movsd 0x108(%rax), %xmm0
movsd %xmm0, 0x218(%rsp)
leaq 0x370(%rsp), %rdi
callq 0x1efb0
movsd %xmm0, 0x220(%rsp)
jmp 0x46a53
leaq 0x370(%rsp), %rdi
callq 0x1efc0
movsd %xmm0, 0x210(%rsp)
jmp 0x46a6b
leaq 0x370(%rsp), %rdi
callq 0x1efd0
movsd %xmm0, 0x208(%rsp)
jmp 0x46a83
leaq 0x308(%rsp), %rdi
xorl %esi, %esi
callq 0x36020
movq %rax, 0x200(%rsp)
jmp 0x46a9c
movq 0x200(%rsp), %rdi
xorl %esi, %esi
callq 0x48290
movsd %xmm0, 0x1f8(%rsp)
jmp 0x46ab6
leaq 0x308(%rsp), %rdi
xorl %esi, %esi
callq 0x36020
movq %rax, 0x1f0(%rsp)
jmp 0x46acf
movq 0x1f0(%rsp), %rdi
movl $0x1, %esi
callq 0x48290
movsd %xmm0, 0x1e8(%rsp)
jmp 0x46aec
leaq 0x308(%rsp), %rdi
xorl %esi, %esi
callq 0x36020
movq %rax, 0x1e0(%rsp)
jmp 0x46b05
movq 0x1e0(%rsp), %rdi
movl $0x2, %esi
callq 0x48290
movsd %xmm0, 0x1d8(%rsp)
jmp 0x46b22
leaq 0x308(%rsp), %rdi
movl $0x1, %esi
callq 0x36020
movq %rax, 0x1d0(%rsp)
jmp 0x46b3e
movq 0x1d0(%rsp), %rdi
xorl %esi, %esi
callq 0x48290
movsd %xmm0, 0x1c8(%rsp)
jmp 0x46b58
leaq 0x308(%rsp), %rdi
movl $0x1, %esi
callq 0x36020
movq %rax, 0x1c0(%rsp)
jmp 0x46b74
movq 0x1c0(%rsp), %rdi
movl $0x1, %esi
callq 0x48290
movsd %xmm0, 0x1b8(%rsp)
jmp 0x46b91
leaq 0x308(%rsp), %rdi
movl $0x1, %esi
callq 0x36020
movq %rax, 0x1b0(%rsp)
jmp 0x46bad
movq 0x1b0(%rsp), %rdi
movl $0x2, %esi
callq 0x48290
movsd %xmm0, 0x1a8(%rsp)
jmp 0x46bca
leaq 0x308(%rsp), %rdi
movl $0x2, %esi
callq 0x36020
movq %rax, 0x1a0(%rsp)
jmp 0x46be6
movq 0x1a0(%rsp), %rdi
xorl %esi, %esi
callq 0x48290
movsd %xmm0, 0x198(%rsp)
jmp 0x46c00
leaq 0x308(%rsp), %rdi
movl $0x2, %esi
callq 0x36020
movq %rax, 0x190(%rsp)
jmp 0x46c1c
movq 0x190(%rsp), %rdi
movl $0x1, %esi
callq 0x48290
movsd %xmm0, 0x188(%rsp)
jmp 0x46c39
leaq 0x308(%rsp), %rdi
movl $0x2, %esi
callq 0x36020
movq %rax, 0x180(%rsp)
jmp 0x46c55
movq 0x180(%rsp), %rdi
movl $0x2, %esi
callq 0x48290
movsd %xmm0, 0x178(%rsp)
jmp 0x46c72
movsd 0x350(%rsp), %xmm0
movsd %xmm0, 0x158(%rsp)
movsd 0x358(%rsp), %xmm0
movsd %xmm0, 0x160(%rsp)
movsd 0x360(%rsp), %xmm0
movsd %xmm0, 0x168(%rsp)
leaq 0x2d0(%rsp), %rdi
xorl %esi, %esi
callq 0x1ec30
movq %rax, 0x170(%rsp)
jmp 0x46cc1
movq 0x170(%rsp), %rdi
xorl %esi, %esi
callq 0x1ec70
movq %rax, 0x150(%rsp)
jmp 0x46cda
movq 0x150(%rsp), %rax
movsd (%rax), %xmm0
movsd %xmm0, 0x140(%rsp)
leaq 0x2d0(%rsp), %rdi
xorl %esi, %esi
callq 0x1ec30
movq %rax, 0x148(%rsp)
jmp 0x46d08
movq 0x148(%rsp), %rdi
movl $0x1, %esi
callq 0x1ec70
movq %rax, 0x138(%rsp)
jmp 0x46d24
movq 0x138(%rsp), %rax
movsd (%rax), %xmm0
movsd %xmm0, 0x128(%rsp)
leaq 0x2d0(%rsp), %rdi
xorl %esi, %esi
callq 0x1ec30
movq %rax, 0x130(%rsp)
jmp 0x46d52
movq 0x130(%rsp), %rdi
movl $0x2, %esi
callq 0x1ec70
movq %rax, 0x120(%rsp)
jmp 0x46d6e
movq 0x120(%rsp), %rax
movsd (%rax), %xmm0
movsd %xmm0, 0x110(%rsp)
leaq 0x2d0(%rsp), %rdi
movl $0x1, %esi
callq 0x1ec30
movq %rax, 0x118(%rsp)
jmp 0x46d9f
movq 0x118(%rsp), %rdi
xorl %esi, %esi
callq 0x1ec70
movq %rax, 0x108(%rsp)
jmp 0x46db8
movq 0x108(%rsp), %rax
movsd (%rax), %xmm0
movsd %xmm0, 0xf8(%rsp)
leaq 0x2d0(%rsp), %rdi
movl $0x1, %esi
callq 0x1ec30
movq %rax, 0x100(%rsp)
jmp 0x46de9
movq 0x100(%rsp), %rdi
movl $0x1, %esi
callq 0x1ec70
movq %rax, 0xf0(%rsp)
jmp 0x46e05
movq 0xf0(%rsp), %rax
movsd (%rax), %xmm0
movsd %xmm0, 0xe0(%rsp)
leaq 0x2d0(%rsp), %rdi
movl $0x1, %esi
callq 0x1ec30
movq %rax, 0xe8(%rsp)
jmp 0x46e36
movq 0xe8(%rsp), %rdi
movl $0x2, %esi
callq 0x1ec70
movq %rax, 0xd8(%rsp)
jmp 0x46e52
movsd 0xf8(%rsp), %xmm10
movsd 0x110(%rsp), %xmm11
movsd 0x128(%rsp), %xmm12
movsd 0x140(%rsp), %xmm13
movsd 0x168(%rsp), %xmm14
movsd 0x160(%rsp), %xmm15
movsd 0x248(%rsp), %xmm9
movsd 0x250(%rsp), %xmm7
movsd 0x258(%rsp), %xmm6
movsd 0x260(%rsp), %xmm5
movsd 0x268(%rsp), %xmm4
movsd 0x290(%rsp), %xmm3
movl 0x28c(%rsp), %r8d
movsd 0x280(%rsp), %xmm2
movsd 0x278(%rsp), %xmm1
movsd 0x270(%rsp), %xmm0
movl 0x2b0(%rsp), %ecx
movl 0x2ac(%rsp), %edx
movq 0x2a0(%rsp), %rdi
movq 0xd8(%rsp), %rax
movsd (%rax), %xmm8
leaq 0x1b751e(%rip), %rsi    # 0x1fe430
movsd %xmm9, (%rsp)
movsd 0x240(%rsp), %xmm9
movsd %xmm9, 0x8(%rsp)
movsd 0x238(%rsp), %xmm9
movsd %xmm9, 0x10(%rsp)
movsd 0x230(%rsp), %xmm9
movsd %xmm9, 0x18(%rsp)
movsd 0x228(%rsp), %xmm9
movsd %xmm9, 0x20(%rsp)
movsd 0x218(%rsp), %xmm9
movsd %xmm9, 0x28(%rsp)
movsd 0x220(%rsp), %xmm9
movsd %xmm9, 0x30(%rsp)
movsd 0x210(%rsp), %xmm9
movsd %xmm9, 0x38(%rsp)
movsd 0x208(%rsp), %xmm9
movsd %xmm9, 0x40(%rsp)
movsd 0x1f8(%rsp), %xmm9
movsd %xmm9, 0x48(%rsp)
movsd 0x1e8(%rsp), %xmm9
movsd %xmm9, 0x50(%rsp)
movsd 0x1d8(%rsp), %xmm9
movsd %xmm9, 0x58(%rsp)
movsd 0x1c8(%rsp), %xmm9
movsd %xmm9, 0x60(%rsp)
movsd 0x1b8(%rsp), %xmm9
movsd %xmm9, 0x68(%rsp)
movsd 0x1a8(%rsp), %xmm9
movsd %xmm9, 0x70(%rsp)
movsd 0x198(%rsp), %xmm9
movsd %xmm9, 0x78(%rsp)
movsd 0x188(%rsp), %xmm9
movsd %xmm9, 0x80(%rsp)
movsd 0x178(%rsp), %xmm9
movsd %xmm9, 0x88(%rsp)
movsd 0x158(%rsp), %xmm9
movsd %xmm9, 0x90(%rsp)
movsd 0xe0(%rsp), %xmm9
movsd %xmm15, 0x98(%rsp)
movsd %xmm14, 0xa0(%rsp)
movsd %xmm13, 0xa8(%rsp)
movsd %xmm12, 0xb0(%rsp)
movsd %xmm11, 0xb8(%rsp)
movsd %xmm10, 0xc0(%rsp)
movsd %xmm9, 0xc8(%rsp)
movsd %xmm8, 0xd0(%rsp)
movb $0x8, %al
callq 0x16ab0
leaq 0x2d0(%rsp), %rdi
callq 0x1ec20
movl 0x394(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x394(%rsp)
jmp 0x467df
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x2c8(%rsp)
movl %eax, 0x2c4(%rsp)
leaq 0x2d0(%rsp), %rdi
callq 0x1ec20
jmp 0x47121
movq 0x398(%rsp), %rdi
callq 0x16730
movl $0x1, 0x3b4(%rsp)
movl 0x3b4(%rsp), %eax
addq $0x3b8, %rsp            # imm = 0x3B8
retq
movq 0x2c8(%rsp), %rdi
callq 0x16ca0
nop