// File: tomfunkhouser[P]gaps/pkgs/RGBD/RGBDSurface.cpp
// Function: gaps::RGBDSurface::RGBDSurface(char const*, gaps::R3Mesh*, double)
RGBDSurface::
RGBDSurface(const char *texture_filename, R3Mesh *mesh, RNLength texel_spacing)
  : configuration(NULL),
    configuration_index(-1),
    channels(),
    width(0), height(0),
    opengl_texture_id(-1),
    texture_filename(NULL),
    mesh_filename(NULL),
    surface_bbox(FLT_MAX, FLT_MAX, -FLT_MAX, -FLT_MAX),
    color_resident_count(0),
    data(NULL),
    rectangle(NULL),
    mesh(mesh),
    mesh_face_index(NULL),
    thickness(0)
{
  // Check mesh
  if (mesh->NFaces() == 0) return;
  RNArea mesh_area = mesh->Area();
  if (RNIsZero(mesh_area)) {
    RNFail("Warning: RGBDSurface mesh has zero area\n");
    return;
  }

  // Compute bounding box of surface coordinates
  for (int i = 0; i < mesh->NVertices(); i++) {
    R3MeshVertex *vertex = mesh->Vertex(i);
    const R2Point& texcoords = mesh->VertexTextureCoords(vertex);
    surface_bbox.Union(texcoords);
  }

  // Check surface_bbox
  if (RNIsZero(surface_bbox.Area())) {
    // RNFail("Warning: RGBDSurface mesh does not have valid texture coordinates\n");
    surface_bbox.Reset(R2zero_point, R2ones_point);
  }

  // Compute width and height from texel_spacing
  if (texel_spacing <= 0) texel_spacing = 0.01;
  width = (int) (surface_bbox.XLength() / texel_spacing + 0.5);
  height = (int) (surface_bbox.YLength() / texel_spacing + 0.5);
  if (width <= 0) width = 1;
  if (height <= 0) height = 1;

  // Set texture filename
  SetTextureFilename(texture_filename);
}

// Assembly:
subq $0xb8, %rsp
movq %rdi, 0xb0(%rsp)
movq %rsi, 0xa8(%rsp)
movq %rdx, 0xa0(%rsp)
movsd %xmm0, 0x98(%rsp)
movq 0xb0(%rsp), %rdi
movq %rdi, 0x58(%rsp)
leaq 0x188dfc(%rip), %rax    # 0x1acd88
movq %rax, (%rdi)
movq $0x0, 0x8(%rdi)
movl $0xffffffff, 0x10(%rdi) # imm = 0xFFFFFFFF
addq $0x18, %rdi
movq %rdi, 0x50(%rsp)
callq 0x27780
movq 0x58(%rsp), %rdi
movl $0x0, 0x28(%rdi)
movl $0x0, 0x2c(%rdi)
movl $0xffffffff, 0x30(%rdi) # imm = 0xFFFFFFFF
movq $0x0, 0x38(%rdi)
movq $0x0, 0x40(%rdi)
addq $0x48, %rdi
movq %rdi, 0x60(%rsp)
movsd 0x142021(%rip), %xmm1   # 0x166008
movsd 0x142021(%rip), %xmm3   # 0x166010
movaps %xmm1, %xmm0
movaps %xmm3, %xmm2
callq 0xca300
jmp 0x23ffc
movq 0x58(%rsp), %rax
movl $0x0, 0x70(%rax)
movq $0x0, 0x78(%rax)
movq $0x0, 0x80(%rax)
movq 0xa0(%rsp), %rcx
movq %rcx, 0x88(%rax)
movq $0x0, 0x90(%rax)
movq $0x0, 0x98(%rax)
movq 0xa0(%rsp), %rdi
callq 0x19f00
movl %eax, 0x4c(%rsp)
jmp 0x24053
movl 0x4c(%rsp), %eax
cmpl $0x0, %eax
jne 0x2409d
jmp 0x242ce
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x90(%rsp)
movl %eax, 0x8c(%rsp)
jmp 0x242d6
movq 0x60(%rsp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x90(%rsp)
movl %eax, 0x8c(%rsp)
callq 0x1a200
jmp 0x242d6
movq 0xa0(%rsp), %rdi
callq 0x45500
movsd %xmm0, 0x40(%rsp)
jmp 0x240b2
movsd 0x40(%rsp), %xmm0
movsd %xmm0, 0x80(%rsp)
movsd 0x80(%rsp), %xmm0
leaq 0x18ddef(%rip), %rax    # 0x1b1ec0
movsd (%rax), %xmm1
callq 0x1a370
movl %eax, 0x3c(%rsp)
jmp 0x240e0
movl 0x3c(%rsp), %eax
cmpl $0x0, %eax
je 0x240fe
leaq 0x142da0(%rip), %rdi    # 0x166e90
xorl %eax, %eax
callq 0xdf990
jmp 0x240f9
jmp 0x242ce
movl $0x0, 0x7c(%rsp)
movl 0x7c(%rsp), %eax
movl %eax, 0x34(%rsp)
movq 0xa0(%rsp), %rdi
callq 0x19f40
movl %eax, 0x38(%rsp)
jmp 0x24121
movl 0x34(%rsp), %eax
movl 0x38(%rsp), %ecx
cmpl %ecx, %eax
jge 0x24199
movq 0xa0(%rsp), %rdi
movl 0x7c(%rsp), %esi
callq 0x22b40
movq %rax, 0x28(%rsp)
jmp 0x24145
movq 0x28(%rsp), %rax
movq %rax, 0x70(%rsp)
movq 0xa0(%rsp), %rdi
movq 0x70(%rsp), %rsi
callq 0x22b90
movq %rax, 0x20(%rsp)
jmp 0x24168
movq 0x58(%rsp), %rdi
movq 0x20(%rsp), %rax
movq %rax, 0x68(%rsp)
addq $0x48, %rdi
movq 0x68(%rsp), %rsi
callq 0xcb160
jmp 0x24187
jmp 0x24189
movl 0x7c(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x7c(%rsp)
jmp 0x24106
movq 0x58(%rsp), %rdi
addq $0x48, %rdi
callq 0xcaf70
movsd %xmm0, 0x18(%rsp)
jmp 0x241af
movsd 0x18(%rsp), %xmm0
leaq 0x18dd04(%rip), %rax    # 0x1b1ec0
movsd (%rax), %xmm1
callq 0x1a370
movl %eax, 0x14(%rsp)
jmp 0x241cb
movl 0x14(%rsp), %eax
cmpl $0x0, %eax
je 0x241f4
movq 0x58(%rsp), %rdi
addq $0x48, %rdi
leaq 0x1906a4(%rip), %rsi    # 0x1b4888
leaq 0x1906ad(%rip), %rdx    # 0x1b4898
callq 0xcb730
jmp 0x241f2
jmp 0x241f4
xorps %xmm0, %xmm0
ucomisd 0x98(%rsp), %xmm0
jb 0x24213
movsd 0x142c76(%rip), %xmm0   # 0x166e80
movsd %xmm0, 0x98(%rsp)
movq 0x58(%rsp), %rdi
addq $0x48, %rdi
callq 0x1a210
movsd %xmm0, 0x8(%rsp)
jmp 0x24229
movq 0x58(%rsp), %rdi
movsd 0x8(%rsp), %xmm0
movsd 0x98(%rsp), %xmm1
divsd %xmm1, %xmm0
movsd 0x141ddf(%rip), %xmm1   # 0x166028
addsd %xmm1, %xmm0
cvttsd2si %xmm0, %eax
movl %eax, 0x28(%rdi)
addq $0x48, %rdi
callq 0x277a0
movsd %xmm0, (%rsp)
jmp 0x24264
movq 0x58(%rsp), %rax
movsd (%rsp), %xmm0
divsd 0x98(%rsp), %xmm0
movsd 0x141da9(%rip), %xmm1   # 0x166028
addsd %xmm1, %xmm0
cvttsd2si %xmm0, %ecx
movl %ecx, 0x2c(%rax)
cmpl $0x0, 0x28(%rax)
jg 0x2429c
movq 0x58(%rsp), %rax
movl $0x1, 0x28(%rax)
movq 0x58(%rsp), %rax
cmpl $0x0, 0x2c(%rax)
jg 0x242b3
movq 0x58(%rsp), %rax
movl $0x1, 0x2c(%rax)
movq 0x58(%rsp), %rdi
movq 0xa8(%rsp), %rsi
movq (%rdi), %rax
movq 0xc0(%rax), %rax
callq *%rax
jmp 0x242ce
addq $0xb8, %rsp
retq
movq 0x50(%rsp), %rdi
callq 0x27790
movq 0x90(%rsp), %rdi
callq 0x12c20
nopl (%rax)