// File: tomfunkhouser[P]gaps/pkgs/R3Surfels/R3SurfelBlock.cpp
// Function: gaps::R3SurfelBlock::ReadPly(_IO_FILE*)
int R3SurfelBlock::
ReadPly(FILE *fp)
{
  // Read into mesh
  R3Mesh mesh;
  if (!mesh.ReadPlyStream(fp)) return 0;

  // Determine the number of surfels
  nsurfels = mesh.NVertices();
  if (nsurfels == 0) return 1;

  // Determine position origin
  position_origin = mesh.Centroid();

  // Allocate array of surfels
  surfels = new R3Surfel [ nsurfels ];
  if (!surfels) {
    RNFail("Unable to allocate surfels\n");
    return 0;
  }

  // Fill array of surfels
  for (int i = 0; i < mesh.NVertices(); i++) {
    R3MeshVertex *vertex = mesh.Vertex(i);
    const R3Point& position = mesh.VertexPosition(vertex);
    R3Vector normal = mesh.VertexNormal(vertex);
    if (normal.Length() == 0) normal = R3posz_vector;
    const RNRgb& color = mesh.VertexColor(vertex);
    RNLength radius = mesh.VertexAverageEdgeLength(vertex);
    if (radius == 0) radius = 0.01;
    RNBoolean boundary = mesh.IsVertexOnBoundary(vertex);
    float x = (float) (position.X() - position_origin.X());
    float y = (float) (position.Y() - position_origin.Y());
    float z = (float) (position.Z() - position_origin.Z());
    surfels[i].SetPosition(x, y, z);
    surfels[i].SetNormal(normal.X(), normal.Y(), normal.Z());
    surfels[i].SetRadius(radius);
    surfels[i].SetIdentifier(i+1);
    surfels[i].SetColor(color);
    surfels[i].SetBorderBoundary(boundary);
    surfels[i].SetAerial(FALSE);
  }

  // Return success
  return 1;
}

// Assembly:
subq $0x2b8, %rsp            # imm = 0x2B8
movq %rdi, 0x2a8(%rsp)
movq %rsi, 0x2a0(%rsp)
movq 0x2a8(%rsp), %rax
movq %rax, 0xd8(%rsp)
leaq 0x168(%rsp), %rdi
movq %rdi, 0xe0(%rsp)
callq 0x98410
movq 0xe0(%rsp), %rdi
movq 0x2a0(%rsp), %rsi
callq 0xb3210
movl %eax, 0xec(%rsp)
jmp 0x4d9da
movl 0xec(%rsp), %eax
cmpl $0x0, %eax
jne 0x4da1a
movl $0x0, 0x2b4(%rsp)
movl $0x1, 0x158(%rsp)
jmp 0x4e03c
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x160(%rsp)
movl %eax, 0x15c(%rsp)
jmp 0x4e058
leaq 0x168(%rsp), %rdi
callq 0x500c0
movl %eax, 0xd4(%rsp)
jmp 0x4da30
movq 0xd8(%rsp), %rax
movl 0xd4(%rsp), %ecx
movl %ecx, 0x8(%rax)
cmpl $0x0, 0x8(%rax)
jne 0x4da63
movl $0x1, 0x2b4(%rsp)
movl $0x1, 0x158(%rsp)
jmp 0x4e03c
leaq 0x140(%rsp), %rdi
leaq 0x168(%rsp), %rsi
callq 0x993b0
jmp 0x4da7a
movq 0xd8(%rsp), %rdi
addq $0x10, %rdi
leaq 0x140(%rsp), %rsi
callq 0xf89e0
jmp 0x4da95
movq 0xd8(%rsp), %rax
movslq 0x8(%rax), %rax
movq %rax, 0xb8(%rsp)
movl $0x30, %ecx
mulq %rcx
movq %rax, %rdi
movq $-0x1, %rax
cmovoq %rax, %rdi
movq %rdi, 0xc0(%rsp)
callq 0x13030
movq %rax, 0xc8(%rsp)
jmp 0x4dad6
movq 0xb8(%rsp), %rax
cmpq $0x0, %rax
je 0x4db46
movq 0xc8(%rsp), %rax
movq 0xb8(%rsp), %rcx
imulq $0x30, %rcx, %rdx
movq %rax, %rcx
addq %rdx, %rcx
movq %rcx, 0xa8(%rsp)
movq %rax, 0xb0(%rsp)
movq 0xb0(%rsp), %rdi
movq %rdi, 0xa0(%rsp)
callq 0x47310
jmp 0x4db25
movq 0xa8(%rsp), %rcx
movq 0xa0(%rsp), %rax
addq $0x30, %rax
cmpq %rcx, %rax
movq %rax, 0xb0(%rsp)
jne 0x4db0e
movq 0xd8(%rsp), %rax
movq 0xc8(%rsp), %rcx
movq %rcx, (%rax)
cmpq $0x0, (%rax)
jne 0x4dbb8
leaq 0x1574ea(%rip), %rdi    # 0x1a5050
xorl %eax, %eax
callq 0x1464b0
jmp 0x4db6f
movl $0x0, 0x2b4(%rsp)
movl $0x1, 0x158(%rsp)
jmp 0x4e03c
movq 0xc0(%rsp), %rsi
movq 0xc8(%rsp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x160(%rsp)
movl %eax, 0x15c(%rsp)
callq 0x13a90
jmp 0x4e058
movl $0x0, 0x13c(%rsp)
movl 0x13c(%rsp), %eax
movl %eax, 0x98(%rsp)
leaq 0x168(%rsp), %rdi
callq 0x500c0
movl %eax, 0x9c(%rsp)
jmp 0x4dbe7
movl 0x98(%rsp), %eax
movl 0x9c(%rsp), %ecx
cmpl %ecx, %eax
jge 0x4e026
movl 0x13c(%rsp), %esi
leaq 0x168(%rsp), %rdi
callq 0x500e0
movq %rax, 0x90(%rsp)
jmp 0x4dc1b
movq 0x90(%rsp), %rax
movq %rax, 0x130(%rsp)
movq 0x130(%rsp), %rsi
leaq 0x168(%rsp), %rdi
callq 0x50110
movq %rax, 0x88(%rsp)
jmp 0x4dc4a
movq 0x88(%rsp), %rax
movq %rax, 0x128(%rsp)
movq 0x130(%rsp), %rsi
leaq 0x168(%rsp), %rdi
callq 0x50130
movq %rax, 0x80(%rsp)
jmp 0x4dc79
movq 0x80(%rsp), %rsi
leaq 0x110(%rsp), %rdi
callq 0xf94f0
jmp 0x4dc90
leaq 0x110(%rsp), %rdi
callq 0xf9690
movsd %xmm0, 0x78(%rsp)
jmp 0x4dca5
movsd 0x78(%rsp), %xmm0
xorps %xmm1, %xmm1
ucomisd %xmm1, %xmm0
jne 0x4dcce
jp 0x4dcce
leaq 0x5b0543(%rip), %rsi    # 0x5fe200
leaq 0x110(%rsp), %rdi
callq 0xfa170
jmp 0x4dccc
jmp 0x4dcce
movq 0x130(%rsp), %rsi
leaq 0x168(%rsp), %rdi
callq 0x501a0
movq %rax, 0x70(%rsp)
jmp 0x4dcea
movq 0x70(%rsp), %rax
movq %rax, 0x108(%rsp)
movq 0x130(%rsp), %rsi
leaq 0x168(%rsp), %rdi
callq 0x9b350
movsd %xmm0, 0x68(%rsp)
jmp 0x4dd14
movsd 0x68(%rsp), %xmm0
movsd %xmm0, 0x100(%rsp)
movsd 0x100(%rsp), %xmm0
xorps %xmm1, %xmm1
ucomisd %xmm1, %xmm0
jne 0x4dd48
jp 0x4dd48
movsd 0x156d99(%rip), %xmm0   # 0x1a4ad8
movsd %xmm0, 0x100(%rsp)
movq 0x130(%rsp), %rsi
leaq 0x168(%rsp), %rdi
callq 0x9c4a0
movl %eax, 0x64(%rsp)
jmp 0x4dd63
movl 0x64(%rsp), %eax
movl %eax, 0xfc(%rsp)
movq 0x128(%rsp), %rdi
callq 0x187b0
movsd %xmm0, 0x58(%rsp)
jmp 0x4dd83
movq 0xd8(%rsp), %rdi
addq $0x10, %rdi
callq 0x187b0
movsd %xmm0, 0x50(%rsp)
jmp 0x4dd9c
movsd 0x50(%rsp), %xmm1
movsd 0x58(%rsp), %xmm0
subsd %xmm1, %xmm0
cvtsd2ss %xmm0, %xmm0
movss %xmm0, 0xf8(%rsp)
movq 0x128(%rsp), %rdi
callq 0x187c0
movsd %xmm0, 0x48(%rsp)
jmp 0x4ddce
movq 0xd8(%rsp), %rdi
addq $0x10, %rdi
callq 0x187c0
movsd %xmm0, 0x40(%rsp)
jmp 0x4dde7
movsd 0x40(%rsp), %xmm1
movsd 0x48(%rsp), %xmm0
subsd %xmm1, %xmm0
cvtsd2ss %xmm0, %xmm0
movss %xmm0, 0xf4(%rsp)
movq 0x128(%rsp), %rdi
callq 0x187a0
movsd %xmm0, 0x38(%rsp)
jmp 0x4de19
movq 0xd8(%rsp), %rdi
addq $0x10, %rdi
callq 0x187a0
movsd %xmm0, 0x30(%rsp)
jmp 0x4de32
movq 0xd8(%rsp), %rax
movsd 0x30(%rsp), %xmm1
movsd 0x38(%rsp), %xmm0
subsd %xmm1, %xmm0
cvtsd2ss %xmm0, %xmm0
movss %xmm0, 0xf0(%rsp)
movq (%rax), %rdi
movslq 0x13c(%rsp), %rax
leaq (%rax,%rax,2), %rax
shlq $0x4, %rax
addq %rax, %rdi
movss 0xf8(%rsp), %xmm0
movss 0xf4(%rsp), %xmm1
movss 0xf0(%rsp), %xmm2
callq 0x44cd0
jmp 0x4de8f
movq 0xd8(%rsp), %rax
movq (%rax), %rax
movslq 0x13c(%rsp), %rcx
leaq (%rcx,%rcx,2), %rcx
shlq $0x4, %rcx
addq %rcx, %rax
movq %rax, 0x20(%rsp)
leaq 0x110(%rsp), %rdi
callq 0x31c70
movsd %xmm0, 0x28(%rsp)
jmp 0x4dec7
movsd 0x28(%rsp), %xmm0
cvtsd2ss %xmm0, %xmm0
movss %xmm0, 0x14(%rsp)
leaq 0x110(%rsp), %rdi
callq 0x31c80
movsd %xmm0, 0x18(%rsp)
jmp 0x4deec
movsd 0x18(%rsp), %xmm0
cvtsd2ss %xmm0, %xmm0
movss %xmm0, 0x4(%rsp)
leaq 0x110(%rsp), %rdi
callq 0x31c90
movsd %xmm0, 0x8(%rsp)
jmp 0x4df11
movss 0x4(%rsp), %xmm1
movss 0x14(%rsp), %xmm0
movq 0x20(%rsp), %rdi
movsd 0x8(%rsp), %xmm2
cvtsd2ss %xmm2, %xmm2
callq 0x47830
jmp 0x4df33
movq 0xd8(%rsp), %rax
movq (%rax), %rdi
movslq 0x13c(%rsp), %rax
leaq (%rax,%rax,2), %rax
shlq $0x4, %rax
addq %rax, %rdi
movsd 0x100(%rsp), %xmm0
cvtsd2ss %xmm0, %xmm0
callq 0x479d0
jmp 0x4df65
movq 0xd8(%rsp), %rax
movq (%rax), %rdi
movslq 0x13c(%rsp), %rax
movl %eax, %esi
leaq (%rax,%rax,2), %rax
shlq $0x4, %rax
addq %rax, %rdi
incl %esi
callq 0x4fab0
jmp 0x4df8e
movq 0xd8(%rsp), %rax
movq (%rax), %rdi
movslq 0x13c(%rsp), %rax
leaq (%rax,%rax,2), %rax
shlq $0x4, %rax
addq %rax, %rdi
movq 0x108(%rsp), %rsi
callq 0x4fc50
jmp 0x4dfbb
movq 0xd8(%rsp), %rax
movq (%rax), %rdi
movslq 0x13c(%rsp), %rax
leaq (%rax,%rax,2), %rax
shlq $0x4, %rax
addq %rax, %rdi
movl 0xfc(%rsp), %esi
callq 0x4fdf0
jmp 0x4dfe7
movq 0xd8(%rsp), %rax
movq (%rax), %rdi
movslq 0x13c(%rsp), %rax
leaq (%rax,%rax,2), %rax
shlq $0x4, %rax
addq %rax, %rdi
xorl %esi, %esi
callq 0x44d50
jmp 0x4e00e
jmp 0x4e010
movl 0x13c(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x13c(%rsp)
jmp 0x4dbc3
movl $0x1, 0x2b4(%rsp)
movl $0x1, 0x158(%rsp)
leaq 0x168(%rsp), %rdi
callq 0x99310
movl 0x2b4(%rsp), %eax
addq $0x2b8, %rsp            # imm = 0x2B8
retq
leaq 0x168(%rsp), %rdi
callq 0x99310
movq 0x160(%rsp), %rdi
callq 0x13970
nopw %cs:(%rax,%rax)