// File: /workspace/github_cpp_makefile_star2_O2/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:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x168, %rsp            # imm = 0x168
movq %rsi, %r14
movq %rdi, %rbx
leaq 0x30(%rsp), %r15
movq %r15, %rdi
callq 0x4dc0c
movq %r15, %rdi
movq %r14, %rsi
callq 0x5a67e
testl %eax, %eax
je 0x2c4c9
movl 0x44(%rsp), %eax
movl %eax, 0x8(%rbx)
pushq $0x1
popq %rbp
testl %eax, %eax
je 0x2c4cb
leaq 0x8(%rsp), %rdi
leaq 0x30(%rsp), %rsi
callq 0x4e152
leaq 0x10(%rbx), %rdi
leaq 0x8(%rsp), %rsi
callq 0x784a6
movslq 0x8(%rbx), %r12
pushq $0x30
popq %rcx
movq %r12, %rax
mulq %rcx
pushq $-0x1
popq %r14
cmovnoq %rax, %r14
movq %r14, %rdi
callq 0x13030
movq %rax, %r15
movq %rbx, (%rsp)
testl %r12d, %r12d
je 0x2c330
imulq $0x30, %r12, %r12
xorl %r13d, %r13d
leaq (%r15,%r13), %rdi
callq 0x29118
addq $0x30, %r13
cmpq %r13, %r12
jne 0x2c31e
movq (%rsp), %rax
movq %r15, (%rax)
xorl %r15d, %r15d
leaq 0x30(%rsp), %r12
xorl %r14d, %r14d
movslq 0x44(%rsp), %rax
cmpq %rax, %r14
jge 0x2c4c4
movq 0x38(%rsp), %rax
movq (%rax,%r14,8), %r13
movq %r12, %rdi
movq %r13, %rsi
callq 0x2d1a8
leaq 0x8(%rsp), %rdi
movq %rax, %rsi
callq 0x7898c
leaq 0x8(%rsp), %rdi
callq 0x78a82
ucomisd 0x9bffd(%rip), %xmm0    # 0xc8380
jne 0x2c398
jp 0x2c398
leaq 0x8(%rsp), %rdi
leaq 0xa3e25(%rip), %rsi     # 0xd01b8
callq 0x78eec
movq %r12, %rdi
movq %r13, %rsi
callq 0x4f198
movapd %xmm0, 0x20(%rsp)
movq %r12, %rbx
movq %r12, %rdi
movq %r13, %rsi
callq 0x4fa6a
movl %eax, %ebp
leaq 0x58(%r13), %r12
movapd 0x20(%rsp), %xmm3
movapd %xmm3, %xmm2
cmpeqsd 0x9bfb0(%rip), %xmm2    # 0xc8380
movapd %xmm2, %xmm1
movsd 0x9c714(%rip), %xmm0    # 0xc8af0
andpd %xmm0, %xmm1
andnpd %xmm3, %xmm2
movapd %xmm2, %xmm3
movupd 0x18(%r13), %xmm0
movq (%rsp), %rax
movupd 0x10(%rax), %xmm2
subpd %xmm2, %xmm0
movsd 0x28(%r13), %xmm2
movq %rax, %r13
subsd 0x20(%rax), %xmm2
cvtsd2ss %xmm2, %xmm2
movq (%rax), %rax
cvtpd2ps %xmm0, %xmm0
movlpd %xmm0, (%rax,%r15)
movss %xmm2, 0x8(%rax,%r15)
cvtsd2ss 0x8(%rsp), %xmm0
orpd %xmm1, %xmm3
movapd %xmm3, 0x20(%rsp)
cvtsd2ss 0x10(%rsp), %xmm1
leaq (%rax,%r15), %rdi
cvtsd2ss 0x18(%rsp), %xmm2
callq 0x293b6
movq (%r13), %rax
leaq (%rax,%r15), %rdi
cvtsd2ss 0x20(%rsp), %xmm0
cvtss2sd %xmm0, %xmm0
mulsd 0x9c968(%rip), %xmm0    # 0xc8dc8
addsd 0x9bcd0(%rip), %xmm0    # 0xc8138
cvtsd2ss %xmm0, %xmm0
movss 0x9c964(%rip), %xmm1    # 0xc8dd8
minss %xmm0, %xmm1
cvttss2si %xmm1, %ecx
movw %cx, 0x1c(%rax,%r15)
movw %cx, 0x1e(%rax,%r15)
leal 0x1(%r14), %ecx
movl %ecx, 0x20(%rax,%r15)
movq %r12, %rsi
callq 0x2d162
movq (%r13), %rax
movb 0x2f(%rax,%r15), %cl
andb $-0x33, %cl
testl %ebp, %ebp
pushq $0x30
popq %rdx
cmovnel %edx, %ebp
orb %cl, %bpl
movb %bpl, 0x2f(%rax,%r15)
addq $0x30, %r15
incq %r14
movq %rbx, %r12
jmp 0x2c342
pushq $0x1
popq %rbp
jmp 0x2c4cb
xorl %ebp, %ebp
leaq 0x30(%rsp), %rdi
callq 0x4e0f4
movl %ebp, %eax
addq $0x168, %rsp            # imm = 0x168
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x2c503
jmp 0x2c503
movq %rax, %rbx
movq %r15, %rdi
movq %r14, %rsi
callq 0x139f0
jmp 0x2c506
jmp 0x2c503
jmp 0x2c503
jmp 0x2c503
movq %rax, %rbx
leaq 0x30(%rsp), %rdi
callq 0x4e0f4
movq %rbx, %rdi
callq 0x138f0