// File: /workspace/github_cpp_makefile_star2_O1/tomfunkhouser[P]gaps/pkgs/R3Shapes/R3MeshPropertySet.cpp
// Function: gaps::R3MeshPropertySet::ReadToronto(char const*)
int R3MeshPropertySet::
ReadToronto(const char *filename)
{
  // Get base property name
  char *name = NULL;
  char buffer[512];
  strcpy(buffer, filename);
  name = strrchr(buffer, '/');
  if (name) name++; 
  else name = buffer;
  char *end = strrchr(name, '.');
  if (end) *end = '\0';

  // Open file
  FILE *fp = fopen(filename, "r");
  if (!fp) {
    RNFail("Unable to open toronto file: %s\n", filename);
    return 0;
  }

  // Read header
  int nprops, nfaces, dummy;
  if (fscanf(fp, "%d%d%d\n", &nprops, &nfaces, &dummy) != (unsigned int) 3) {
    RNFail("Unable to read toronto file: %s\n", filename);
    return 0;
  }

  // Check header
  if ((nprops == 0) || (nfaces == 0) || (nfaces != mesh->NFaces())) {
    RNFail("Invalid header in toronto file: %s\n", filename);
    return 0;
  }

  // Allocate memory for face values
  double *face_values = new double [ nprops * nfaces ];
  if (!face_values) {
    RNFail("Unable to allocate memory for toronto file: %s\n", filename);
    return 0;
  }

  // Read face values
  for (int j = 0; j < nfaces; j++) {
    for (int i = 0; i < nprops; i++) {
      if (fscanf(fp, "%lf", &face_values[i*nfaces+j]) != (unsigned int) 1) {
        RNFail("Unable to read value %d for face %d in %s\n", i, j, filename);
        return 0;
      }
    }
  }

  // Create properties
  for (int i = 0; i < nprops; i++) {
    // Allocate property
    char property_name[1024];
    sprintf(property_name, "%s%d", name, i);
    R3MeshProperty *property = new R3MeshProperty(mesh, property_name);

    // Compute property values by interpolation from attached faces
    for (int j = 0; j < mesh->NVertices(); j++) {
      R3MeshVertex *vertex = mesh->Vertex(j);
      RNScalar total_value = 0;
      RNScalar total_weight = 0;
      for (int k = 0; k < mesh->VertexValence(vertex); k++) {
        R3MeshEdge *edge = mesh->EdgeOnVertex(vertex, k);
        R3MeshFace *face = mesh->FaceOnVertex(vertex, edge);
        if (!face) continue;
        int face_index = mesh->FaceID(face);
        RNScalar value = face_values[i*nfaces + face_index];
        RNScalar weight = mesh->FaceArea(face);
        total_value += weight * value;
        total_weight += weight;
      }
      if (total_weight > 0) {
        property->SetVertexValue(j, total_value / total_weight);
      }
    }

    // Insert property
    Insert(property);
  }

  // Close file
  fclose(fp);

  // Delete memory for face values
  delete [] face_values;

  // Return success
  return 1;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x668, %rsp            # imm = 0x668
movq %rsi, %r13
movq %rdi, 0x10(%rsp)
leaq 0x60(%rsp), %r14
movq %r14, %rdi
callq 0x217f0
movq %r14, %rdi
movl $0x2f, %esi
callq 0x219a0
leaq 0x1(%rax), %rdi
testq %rax, %rax
cmoveq %r14, %rdi
movq %rdi, 0x38(%rsp)
movl $0x2e, %esi
callq 0x219a0
testq %rax, %rax
je 0x47c6f
movb $0x0, (%rax)
leaq 0xa092d(%rip), %rsi     # 0xe85a3
movq %r13, %rdi
callq 0x21aa0
testq %rax, %rax
je 0x47d6a
movq %rax, %rbx
leaq 0x9b4f7(%rip), %rsi     # 0xe3188
xorl %ebp, %ebp
leaq 0xc(%rsp), %rdx
leaq 0x8(%rsp), %rcx
leaq 0x5c(%rsp), %r8
movq %rax, %rdi
xorl %eax, %eax
callq 0x22180
cmpl $0x3, %eax
jne 0x47d73
movl 0xc(%rsp), %eax
testl %eax, %eax
sete %dl
movl 0x8(%rsp), %ecx
testl %ecx, %ecx
sete %sil
orb %dl, %sil
jne 0x47d7c
movq 0x10(%rsp), %rdx
movq (%rdx), %rdx
cmpl 0x34(%rdx), %ecx
jne 0x47d7c
movq %rbx, 0x28(%rsp)
imull %eax, %ecx
movslq %ecx, %rax
leaq (,%rax,8), %rcx
testl %eax, %eax
movq $-0x1, %rdi
cmovnsq %rcx, %rdi
callq 0x21040
movq %rax, %r12
cmpl $0x0, 0x8(%rsp)
setg %bl
jle 0x47dc1
leaq 0x9bc2e(%rip), %rbp     # 0xe394d
xorl %r15d, %r15d
cmpl $0x0, 0xc(%rsp)
jle 0x47d5b
xorl %r14d, %r14d
movl 0x8(%rsp), %eax
imull %r14d, %eax
addl %r15d, %eax
cltq
leaq (%r12,%rax,8), %rdx
movq 0x28(%rsp), %rdi
movq %rbp, %rsi
xorl %eax, %eax
callq 0x22180
cmpl $0x1, %eax
jne 0x47da3
incl %r14d
cmpl 0xc(%rsp), %r14d
jl 0x47d2c
incl %r15d
cmpl 0x8(%rsp), %r15d
setl %bl
jl 0x47d22
jmp 0x47dc1
leaq 0x9b3f6(%rip), %rdi     # 0xe3167
jmp 0x47d83
leaq 0x9b416(%rip), %rdi     # 0xe3190
jmp 0x47d85
leaq 0x9b42e(%rip), %rdi     # 0xe31b1
xorl %ebp, %ebp
movq %r13, %rsi
xorl %eax, %eax
callq 0x8fd7a
movl %ebp, %eax
addq $0x668, %rsp            # imm = 0x668
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x9b42b(%rip), %rdi     # 0xe31d5
xorl %ebp, %ebp
movl %r14d, %esi
movl %r15d, %edx
movq %r13, %rcx
xorl %eax, %eax
callq 0x8fd7a
testb $0x1, %bl
jne 0x47d8f
cmpl $0x0, 0xc(%rsp)
movq 0x10(%rsp), %rbx
jle 0x47f53
leaq 0x8(%rbx), %rax
movq %rax, 0x30(%rsp)
xorl %ecx, %ecx
leaq 0x260(%rsp), %r14
movq %r14, %rdi
leaq 0x9a140(%rip), %rsi     # 0xe1f2e
movq 0x38(%rsp), %rdx
movl %ecx, 0x1c(%rsp)
xorl %eax, %eax
callq 0x21130
movl $0x448, %edi            # imm = 0x448
callq 0x218b0
movq (%rbx), %rsi
movq %rax, 0x20(%rsp)
movq %rax, %rdi
movq %r14, %rdx
xorl %ecx, %ecx
callq 0x487c8
movq (%rbx), %rax
cmpl $0x0, 0x14(%rax)
movq %rbx, %rsi
jle 0x47f2e
movslq 0x1c(%rsp), %r13
xorl %r15d, %r15d
movq 0x8(%rax), %rax
movq (%rax,%r15,8), %rbp
cmpl $0x0, 0x14(%rbp)
jle 0x47ef3
xorpd %xmm2, %xmm2
xorl %ebx, %ebx
xorpd %xmm0, %xmm0
movq 0x8(%rbp), %rax
movq (%rax,%rbx,8), %rax
xorl %ecx, %ecx
cmpq %rbp, 0x8(%rax)
setne %cl
movq 0x18(%rax,%rcx,8), %r14
testq %r14, %r14
je 0x47ee1
movslq 0xa4(%r14), %rax
movslq 0x8(%rsp), %rcx
imulq %r13, %rcx
addq %rax, %rcx
movsd (%r12,%rcx,8), %xmm3
testb $0x2, 0xa8(%r14)
jne 0x47ecc
movq 0x10(%rsp), %rax
movq (%rax), %rdi
movq (%rdi), %rax
movq %r14, %rsi
movsd %xmm0, 0x50(%rsp)
movsd %xmm2, 0x48(%rsp)
movsd %xmm3, 0x40(%rsp)
callq *0x2c0(%rax)
movsd 0x40(%rsp), %xmm3
movsd 0x48(%rsp), %xmm2
movsd 0x50(%rsp), %xmm0
movq 0x10(%rsp), %rsi
movsd 0x90(%r14), %xmm1
mulsd %xmm1, %xmm3
addsd %xmm3, %xmm0
addsd %xmm1, %xmm2
incq %rbx
movslq 0x14(%rbp), %rax
cmpq %rax, %rbx
jl 0x47e51
jmp 0x47efb
xorpd %xmm0, %xmm0
xorpd %xmm2, %xmm2
ucomisd 0x99175(%rip), %xmm2    # 0xe1078
jbe 0x47f1b
divsd %xmm2, %xmm0
movq 0x20(%rsp), %rdi
movl %r15d, %esi
callq 0x4a20c
movq 0x10(%rsp), %rsi
incq %r15
movq (%rsi), %rax
movslq 0x14(%rax), %rcx
cmpq %rcx, %r15
jl 0x47e35
movl 0x14(%rsi), %edx
movq %rsi, %rbx
movq 0x30(%rsp), %rdi
movq 0x20(%rsp), %rsi
callq 0x8cf92
movl 0x1c(%rsp), %ecx
incl %ecx
cmpl 0xc(%rsp), %ecx
jl 0x47ddc
movq 0x28(%rsp), %rdi
callq 0x218a0
movq %r12, %rdi
callq 0x21c10
movl $0x1, %ebp
jmp 0x47d8f
movq %rax, %rbx
movl $0x448, %esi            # imm = 0x448
movq 0x20(%rsp), %rdi
callq 0x218d0
movq %rbx, %rdi
callq 0x220b0
nop