// File: /workspace/github_cpp_makefile_star2_O2/tomfunkhouser[P]gaps/pkgs/R3Surfels/R3SurfelObjectProperty.cpp
// Function: gaps::R3SurfelObjectProperty::UpdateOperands()
void R3SurfelObjectProperty::
UpdateOperands(void)
{
  // Check if already uptodate
  if (noperands > 0) return;

  // Check object
  if (!object) return;

  // Check property type
  switch(type) {
  case R3_SURFEL_OBJECT_PCA_PROPERTY: {
    // PCA Properties
    // noperands = 21
    // operands[0-2]: centroid
    // operands[3-5]: xyz of axis1
    // operands[6-8]: xyz of axis2
    // operands[9-11]: xyz of axis3
    // operands[12-14]: variances
    // operands[15-20]: extents 

    // Allocate operands
    noperands = 21;
    operands = new RNScalar [ noperands ];
    for (int i = 0; i < noperands; i++) operands[i] = 0;

    // Create pointset
    R3SurfelPointSet *pointset = object->PointSet();
    if (!pointset) return;
    if (pointset->NPoints() < 3) { delete pointset; return; }

    // Compute principle axes
    RNScalar variances[3];
    R3Point centroid = pointset->Centroid();
    R3Triad triad = pointset->PrincipleAxes(&centroid, variances);

    // Check if positive z of triad is facing away from viewpoint(s)
    int npositive = 0, nnegative = 0;
    R3Plane plane(centroid, triad.Axis(2));
    for (int i = 0; i < pointset->NPoints(); i++) {
      R3SurfelPoint *point = pointset->Point(i);
      R3SurfelBlock *block = point->Block();
      if (!block) continue;
      R3SurfelNode *node = block->Node();
      if (!node) continue;
      R3SurfelScan *scan = node->Scan();
      if (!scan) continue;
      RNScalar d = R3SignedDistance(plane, scan->Viewpoint());
      if (d < 0) nnegative++;
      else if (d > 0) npositive++;
    }

    // Rotate triad so that z is not backfacing to viewpoints
    if (nnegative > npositive) triad = R3Triad(triad[0], -triad[1], -triad[2]);

    // Find extents
    R3Box extent = R3null_box;
    for (int i = 0; i < pointset->NPoints(); i++) {
      R3SurfelPoint *point = pointset->Point(i);
      R3Point position = point->Position();
      R3Vector vector = position - centroid;
      RNScalar x = triad.Axis(0).Dot(vector);
      RNScalar y = triad.Axis(1).Dot(vector);
      RNScalar z = triad.Axis(2).Dot(vector);
      extent.Union(R3Point(x, y, z));
    }

    // Fill operands
    operands[0] = centroid.X();
    operands[1] = centroid.Y();
    operands[2] = centroid.Z();
    operands[3] = triad.Axis(0).X();
    operands[4] = triad.Axis(0).Y();
    operands[5] = triad.Axis(0).Z();
    operands[6] = triad.Axis(1).X();
    operands[7] = triad.Axis(1).Y();
    operands[8] = triad.Axis(1).Z();
    operands[9] = triad.Axis(2).X();
    operands[10] = triad.Axis(2).Y();
    operands[11] = triad.Axis(2).Z();
    operands[12] = variances[0];
    operands[13] = variances[1];
    operands[14] = variances[2];
    operands[15] = extent[0][0];
    operands[16] = extent[0][1];
    operands[17] = extent[0][2];
    operands[18] = extent[1][0];
    operands[19] = extent[1][1];
    operands[20] = extent[1][2];

    // Delete pointset
    delete pointset;
    break; }

  case R3_SURFEL_OBJECT_AMODAL_OBB_PROPERTY: {
    // PCA Properties
    // noperands = 16
    // operands[0-2]: centroid
    // operands[3-5]: xyz of axis1
    // operands[6-8]: xyz of axis2
    // operands[9-11]: xyz of axis3
    // operands[12-14]: radii
    // operands[15]: confidence
    // operands[16]: originator
    // operands[17-19]: reserved

    // Allocate operands
    noperands = 20;
    operands = new RNScalar [ noperands ];
    for (int i = 0; i < noperands; i++) operands[i] = 0;

    // Estimate obb
    R3OrientedBox obb = EstimateOrientedBBox(object);
    double confidence = 0.01;
    int originator = R3_SURFEL_MACHINE_ORIGINATOR;
    
    // Fill operands
    operands[0] = obb.Center().X();
    operands[1] = obb.Center().Y();
    operands[2] = obb.Center().Z();
    operands[3] = obb.Axis(0).X();
    operands[4] = obb.Axis(0).Y();
    operands[5] = obb.Axis(0).Z();
    operands[6] = obb.Axis(1).X();
    operands[7] = obb.Axis(1).Y();
    operands[8] = obb.Axis(1).Z();
    operands[9] = obb.Axis(2).X();
    operands[10] = obb.Axis(2).Y();
    operands[11] = obb.Axis(2).Z();
    operands[12] = obb.Radius(0);
    operands[13] = obb.Radius(1);
    operands[14] = obb.Radius(2);
    operands[15] = confidence;
    operands[16] = originator;
    operands[17] = 0;
    operands[18] = 0;
    operands[19] = 0;
    break; }
  }

  // Mark scene as dirty
  if (scene) scene->SetDirty();
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x188, %rsp            # imm = 0x188
cmpl $0x0, 0x28(%rdi)
jle 0x3a1cd
addq $0x188, %rsp            # imm = 0x188
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rdi, %rbx
movq 0x18(%rdi), %r14
testq %r14, %r14
je 0x3a1bb
movl 0x2c(%rbx), %eax
cmpl $0x2, %eax
je 0x3a24b
cmpl $0x1, %eax
jne 0x3a5d0
movl $0x15, 0x28(%rbx)
movl $0xa8, %edi
callq 0x13030
movq %rax, 0x20(%rbx)
xorl %ecx, %ecx
cmpq $0x15, %rcx
je 0x3a211
andq $0x0, (%rax,%rcx,8)
incq %rcx
jmp 0x3a201
xorpd %xmm0, %xmm0
movq %r14, %rdi
xorl %esi, %esi
callq 0x16b9c
testq %rax, %rax
je 0x3a1bb
movq %rax, %r14
cmpl $0x2, 0x10(%rax)
jg 0x3a350
movq (%r14), %rax
movq %r14, %rdi
addq $0x188, %rsp            # imm = 0x188
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmpq *0x8(%rax)
movl $0x14, 0x28(%rbx)
movl $0xa0, %edi
callq 0x13030
movq %rax, 0x20(%rbx)
xorl %ecx, %ecx
cmpq $0x14, %rcx
je 0x3a272
andq $0x0, (%rax,%rcx,8)
incq %rcx
jmp 0x3a262
leaq 0x50(%rsp), %r15
movq %r15, %rdi
movq %r14, %rsi
callq 0x2617c
movsd 0x8(%r15), %xmm0
movq 0x20(%rbx), %rax
movsd %xmm0, (%rax)
movsd 0x10(%r15), %xmm0
movsd %xmm0, 0x8(%rax)
movsd 0x18(%r15), %xmm0
movsd %xmm0, 0x10(%rax)
movsd 0x20(%r15), %xmm0
movsd %xmm0, 0x18(%rax)
movsd 0x28(%r15), %xmm0
movsd %xmm0, 0x20(%rax)
movsd 0x30(%r15), %xmm0
movsd %xmm0, 0x28(%rax)
movsd 0x38(%r15), %xmm0
movsd %xmm0, 0x30(%rax)
movsd 0x40(%r15), %xmm0
movsd %xmm0, 0x38(%rax)
movsd 0x48(%r15), %xmm0
movsd %xmm0, 0x40(%rax)
movsd 0x50(%r15), %xmm0
movsd %xmm0, 0x48(%rax)
movsd 0x58(%r15), %xmm0
movsd %xmm0, 0x50(%rax)
movsd 0x60(%r15), %xmm0
movsd %xmm0, 0x58(%rax)
movsd 0x68(%r15), %xmm0
movsd %xmm0, 0x60(%rax)
movsd 0x70(%r15), %xmm0
movsd %xmm0, 0x68(%rax)
movsd 0x78(%r15), %xmm0
movsd %xmm0, 0x70(%rax)
movapd 0x8f48e(%rip), %xmm0    # 0xc97c0
movupd %xmm0, 0x78(%rax)
xorpd %xmm0, %xmm0
movupd %xmm0, 0x88(%rax)
andq $0x0, 0x98(%rax)
jmp 0x3a5c8
leaq 0x18(%rsp), %r12
movq %r12, %rdi
movq %r14, %rsi
callq 0x2f6e6
leaq 0x50(%rsp), %rdi
leaq 0x30(%rsp), %rcx
movq %r14, %rsi
movq %r12, %rdx
callq 0x2f7aa
leaq 0x80(%rsp), %rdx
leaq 0x150(%rsp), %rdi
movq %r12, %rsi
callq 0x76e1c
xorl %r12d, %r12d
pushq $0x1
popq %rbp
xorl %r15d, %r15d
xorl %r13d, %r13d
movl $0x0, 0x8(%rsp)
movslq 0x10(%r14), %rax
cmpq %rax, %r15
jge 0x3a405
movq 0x8(%r14), %rax
movq (%rax,%r12), %rax
testq %rax, %rax
je 0x3a3fc
movq 0xb8(%rax), %rdi
testq %rdi, %rdi
je 0x3a3fc
movl %ebp, %esi
callq 0x3398a
testq %rax, %rax
je 0x3a3fc
addq $0x28, %rax
leaq 0x150(%rsp), %rdi
movq %rax, %rsi
callq 0x497d0
xorpd %xmm1, %xmm1
ucomisd %xmm0, %xmm1
jbe 0x3a3f2
incl %r13d
jmp 0x3a3fc
ucomisd %xmm1, %xmm0
jbe 0x3a3fc
incl 0x8(%rsp)
incq %r15
addq $0x10, %r12
jmp 0x3a3a1
cmpl 0x8(%rsp), %r13d
jle 0x3a462
leaq 0x68(%rsp), %rsi
leaq 0xf0(%rsp), %r15
movq %r15, %rdi
callq 0x792e2
leaq 0xd8(%rsp), %r12
movq %r12, %rdi
leaq 0x80(%rsp), %rsi
callq 0x792e2
leaq 0x108(%rsp), %r13
leaq 0x50(%rsp), %rbp
movq %r13, %rdi
movq %rbp, %rsi
movq %r15, %rdx
movq %r12, %rcx
callq 0x672fa
movq %rbp, %rdi
movq %r13, %rsi
callq 0x28a5c
leaq 0xeb57f(%rip), %rsi     # 0x1259e8
leaq 0x108(%rsp), %rdi
callq 0x63ce6
xorl %r13d, %r13d
leaq 0xd8(%rsp), %r15
xorl %ebp, %ebp
movslq 0x10(%r14), %rax
cmpq %rax, %rbp
jge 0x3a529
movq 0x8(%r14), %rsi
addq %r13, %rsi
leaq 0xf0(%rsp), %r12
movq %r12, %rdi
callq 0x16310
movq %r15, %rdi
movq %r12, %rsi
leaq 0x18(%rsp), %rdx
callq 0x78399
leaq 0x50(%rsp), %rdi
movq %r15, %rsi
callq 0x78bf6
movsd %xmm0, 0x8(%rsp)
leaq 0x68(%rsp), %rdi
movq %r15, %rsi
callq 0x78bf6
movsd %xmm0, 0x10(%rsp)
leaq 0x80(%rsp), %rdi
movq %r15, %rsi
callq 0x78bf6
movapd %xmm0, %xmm2
leaq 0x170(%rsp), %r12
movq %r12, %rdi
movsd 0x8(%rsp), %xmm0
movsd 0x10(%rsp), %xmm1
callq 0x781bc
leaq 0x108(%rsp), %rdi
movq %r12, %rsi
callq 0x6464c
incq %rbp
addq $0x10, %r13
jmp 0x3a483
movsd 0x18(%rsp), %xmm0
movq 0x20(%rbx), %rax
movsd %xmm0, (%rax)
movsd 0x20(%rsp), %xmm0
movsd %xmm0, 0x8(%rax)
movsd 0x28(%rsp), %xmm0
movsd %xmm0, 0x10(%rax)
movaps 0x50(%rsp), %xmm0
movups %xmm0, 0x18(%rax)
movaps 0x60(%rsp), %xmm0
movups %xmm0, 0x28(%rax)
movaps 0x70(%rsp), %xmm0
movups %xmm0, 0x38(%rax)
movaps 0x80(%rsp), %xmm0
movups %xmm0, 0x48(%rax)
movsd 0x90(%rsp), %xmm0
movsd %xmm0, 0x58(%rax)
movaps 0x30(%rsp), %xmm0
movups %xmm0, 0x60(%rax)
movsd 0x40(%rsp), %xmm0
movsd %xmm0, 0x70(%rax)
leaq 0x108(%rsp), %r15
movups 0x8(%r15), %xmm0
movups %xmm0, 0x78(%rax)
movups 0x18(%r15), %xmm0
movups %xmm0, 0x88(%rax)
movups 0x28(%r15), %xmm0
movups %xmm0, 0x98(%rax)
movq (%r14), %rax
movq %r14, %rdi
callq *0x8(%rax)
movq %r15, %rdi
callq 0x649ae
movq 0x8(%rbx), %rax
testq %rax, %rax
je 0x3a1bb
orb $0x1, 0x1e8(%rax)
jmp 0x3a1bb
jmp 0x3a5f3
jmp 0x3a5f3
jmp 0x3a5f3
jmp 0x3a5f3
jmp 0x3a5f3
movq %rax, %rbx
leaq 0x108(%rsp), %rdi
callq 0x649ae
movq %rbx, %rdi
callq 0x138f0
nop