// File: tomfunkhouser[P]gaps/pkgs/R3Utils/R3Segmentation.cpp
// Function: gaps::R3SegmentationPrimitive::Update(gaps::R3SegmentationPrimitive, gaps::R3SegmentationPrimitive, double, double)
void R3SegmentationPrimitive::
Update(R3SegmentationPrimitive primitive1, R3SegmentationPrimitive primitive2, RNScalar weight1, RNScalar weight2)
{
  // Just checking
  if (weight1 == 0) {
    primitive_type = primitive2.primitive_type;
    bbox = primitive2.bbox;
    centroid = primitive2.centroid;
    line = primitive2.line;
    plane = primitive2.plane;
  }
  else if (weight2 == 0) {
    primitive_type = primitive1.primitive_type;
    bbox = primitive1.bbox;
    centroid = primitive1.centroid;
    line = primitive1.line;
    plane = primitive1.plane;
  }
  else {
    // Update primitive type
    if (primitive1.primitive_type > primitive2.primitive_type) {
      primitive_type = primitive1.primitive_type;
      weight2 = 0;
    }
    else if (primitive2.primitive_type > primitive1.primitive_type) {
      primitive_type = primitive2.primitive_type;
      weight1 = 0;
    }
    else {
      primitive_type = primitive1.primitive_type;
    }

    // Update centroid
    centroid = R3zero_point;
    centroid += weight1 * primitive1.centroid;
    centroid += weight2 * primitive2.centroid;
    centroid /= weight1 + weight2;

    // Update bbox
    bbox = R3null_box;
    bbox.Union(primitive1.bbox);
    bbox.Union(primitive2.bbox);

    // Update other stuff
    line = R3null_line;
    plane = R3null_plane;
    if (primitive_type == R3_SEGMENTATION_LINE_PRIMITIVE_TYPE) {
      // Compute line
      R3Vector vector1 = primitive1.line.Vector();
      R3Vector vector2 = primitive2.line.Vector();
      if (vector1.Dot(vector2) < 0) vector2.Flip();
      R3Vector vector = R3zero_vector;
      vector += weight1 * vector1;
      vector += weight2 * vector2;
      vector /= weight1 + weight2;
      vector.Normalize();
      line.Reset(centroid, vector);
    }
    else if (primitive_type == R3_SEGMENTATION_PLANE_PRIMITIVE_TYPE) {
      // Compute plane
      R3Vector normal1 = primitive1.plane.Normal();
      R3Vector normal2 = primitive2.plane.Normal();
      if (normal1.Dot(normal2) < 0) normal2.Flip();
      R3Vector normal = R3zero_vector;
      normal += weight1 * normal1;
      normal += weight2 * normal2;
      normal /= weight1 + weight2;
      normal.Normalize();
      plane.Reset(centroid, normal);
    }
  }
}

// Assembly:
subq $0x168, %rsp            # imm = 0x168
movq %rsi, 0x8(%rsp)
movq %rdx, 0x10(%rsp)
movq %rdi, 0x160(%rsp)
movq %rsi, 0x158(%rsp)
movq %rdx, 0x150(%rsp)
movsd %xmm0, 0x148(%rsp)
movsd %xmm1, 0x140(%rsp)
movq 0x160(%rsp), %rax
movq %rax, 0x18(%rsp)
movsd 0x148(%rsp), %xmm0
xorps %xmm1, %xmm1
ucomisd %xmm1, %xmm0
jne 0x3cd77
jp 0x3cd77
movq 0x18(%rsp), %rdi
movq 0x10(%rsp), %rsi
movl (%rsi), %eax
movl %eax, (%rdi)
addq $0x8, %rsi
addq $0x8, %rdi
callq 0x1f7f0
movq 0x10(%rsp), %rsi
movq 0x18(%rsp), %rdi
addq $0x40, %rsi
addq $0x40, %rdi
callq 0x12b3e0
movq 0x10(%rsp), %rsi
movq 0x18(%rsp), %rdi
addq $0x58, %rsi
addq $0x58, %rdi
callq 0x474e0
movq 0x10(%rsp), %rsi
movq 0x18(%rsp), %rdi
addq $0x88, %rsi
addq $0x88, %rdi
callq 0x47520
jmp 0x3d1cf
movsd 0x140(%rsp), %xmm0
xorps %xmm1, %xmm1
ucomisd %xmm1, %xmm0
jne 0x3cdf6
jp 0x3cdf6
movq 0x18(%rsp), %rdi
movq 0x8(%rsp), %rsi
movl (%rsi), %eax
movl %eax, (%rdi)
addq $0x8, %rsi
addq $0x8, %rdi
callq 0x1f7f0
movq 0x8(%rsp), %rsi
movq 0x18(%rsp), %rdi
addq $0x40, %rsi
addq $0x40, %rdi
callq 0x12b3e0
movq 0x8(%rsp), %rsi
movq 0x18(%rsp), %rdi
addq $0x58, %rsi
addq $0x58, %rdi
callq 0x474e0
movq 0x8(%rsp), %rsi
movq 0x18(%rsp), %rdi
addq $0x88, %rsi
addq $0x88, %rdi
callq 0x47520
jmp 0x3d1cd
movq 0x10(%rsp), %rcx
movq 0x8(%rsp), %rax
movl (%rax), %eax
cmpl (%rcx), %eax
jle 0x3ce22
movq 0x18(%rsp), %rax
movq 0x8(%rsp), %rcx
movl (%rcx), %ecx
movl %ecx, (%rax)
xorps %xmm0, %xmm0
movsd %xmm0, 0x140(%rsp)
jmp 0x3ce5e
movq 0x8(%rsp), %rcx
movq 0x10(%rsp), %rax
movl (%rax), %eax
cmpl (%rcx), %eax
jle 0x3ce4e
movq 0x18(%rsp), %rax
movq 0x10(%rsp), %rcx
movl (%rcx), %ecx
movl %ecx, (%rax)
xorps %xmm0, %xmm0
movsd %xmm0, 0x148(%rsp)
jmp 0x3ce5c
movq 0x18(%rsp), %rax
movq 0x8(%rsp), %rcx
movl (%rcx), %ecx
movl %ecx, (%rax)
jmp 0x3ce5e
movq 0x18(%rsp), %rdi
addq $0x40, %rdi
leaq 0x62d5c2(%rip), %rsi    # 0x66a430
callq 0x12b3e0
movq 0x8(%rsp), %rsi
movsd 0x148(%rsp), %xmm0
addq $0x40, %rsi
leaq 0x128(%rsp), %rdi
callq 0x27d30
movq 0x18(%rsp), %rdi
addq $0x40, %rdi
leaq 0x128(%rsp), %rsi
callq 0x12ba20
movq 0x10(%rsp), %rsi
movsd 0x140(%rsp), %xmm0
addq $0x40, %rsi
leaq 0x110(%rsp), %rdi
callq 0x27d30
movq 0x18(%rsp), %rdi
addq $0x40, %rdi
leaq 0x110(%rsp), %rsi
callq 0x12ba20
movq 0x18(%rsp), %rdi
movsd 0x148(%rsp), %xmm0
addsd 0x140(%rsp), %xmm0
addq $0x40, %rdi
callq 0x12bb60
movq 0x18(%rsp), %rdi
addq $0x8, %rdi
leaq 0x22c77b(%rip), %rsi    # 0x269688
callq 0x1f7f0
movq 0x8(%rsp), %rsi
movq 0x18(%rsp), %rdi
addq $0x8, %rdi
addq $0x8, %rsi
callq 0xfe780
movq 0x10(%rsp), %rsi
movq 0x18(%rsp), %rdi
addq $0x8, %rdi
addq $0x8, %rsi
callq 0xfe780
movq 0x18(%rsp), %rdi
addq $0x58, %rdi
leaq 0x62d390(%rip), %rsi    # 0x66a2e0
callq 0x474e0
movq 0x18(%rsp), %rdi
addq $0x88, %rdi
leaq 0x62d0f8(%rip), %rsi    # 0x66a060
callq 0x47520
movq 0x18(%rsp), %rax
cmpl $0x2, (%rax)
jne 0x3d0af
movq 0x8(%rsp), %rdi
addq $0x58, %rdi
callq 0x47560
movq %rax, %rsi
leaq 0xf8(%rsp), %rdi
callq 0x12bef0
movq 0x10(%rsp), %rdi
addq $0x58, %rdi
callq 0x47560
movq %rax, %rsi
leaq 0xe0(%rsp), %rdi
callq 0x12bef0
leaq 0xf8(%rsp), %rdi
leaq 0xe0(%rsp), %rsi
callq 0x12c4c0
movaps %xmm0, %xmm1
xorps %xmm0, %xmm0
ucomisd %xmm1, %xmm0
jbe 0x3cfe5
leaq 0xe0(%rsp), %rdi
callq 0x12c690
leaq 0xc8(%rsp), %rdi
leaq 0x62d52c(%rip), %rsi    # 0x66a520
callq 0x12bef0
movsd 0x148(%rsp), %xmm0
leaq 0xb0(%rsp), %rdi
leaq 0xf8(%rsp), %rsi
callq 0x282b0
leaq 0xc8(%rsp), %rdi
leaq 0xb0(%rsp), %rsi
callq 0x12cde0
movsd 0x140(%rsp), %xmm0
leaq 0x98(%rsp), %rdi
leaq 0xe0(%rsp), %rsi
callq 0x282b0
leaq 0xc8(%rsp), %rdi
leaq 0x98(%rsp), %rsi
callq 0x12cde0
movsd 0x148(%rsp), %xmm0
addsd 0x140(%rsp), %xmm0
leaq 0xc8(%rsp), %rdi
callq 0x12d0c0
leaq 0xc8(%rsp), %rdi
callq 0x12c610
movq 0x18(%rsp), %rsi
movq %rsi, %rdi
addq $0x58, %rdi
addq $0x40, %rsi
leaq 0xc8(%rsp), %rdx
xorl %ecx, %ecx
callq 0x47570
jmp 0x3d1cb
movq 0x18(%rsp), %rax
cmpl $0x3, (%rax)
jne 0x3d1c9
movq 0x8(%rsp), %rdi
addq $0x88, %rdi
callq 0x2dca0
movq %rax, %rsi
leaq 0x80(%rsp), %rdi
callq 0x12bef0
movq 0x10(%rsp), %rdi
addq $0x88, %rdi
callq 0x2dca0
movq %rax, %rsi
leaq 0x68(%rsp), %rdi
callq 0x12bef0
leaq 0x80(%rsp), %rdi
leaq 0x68(%rsp), %rsi
callq 0x12c4c0
movaps %xmm0, %xmm1
xorps %xmm0, %xmm0
ucomisd %xmm1, %xmm0
jbe 0x3d124
leaq 0x68(%rsp), %rdi
callq 0x12c690
leaq 0x50(%rsp), %rdi
leaq 0x62d3f0(%rip), %rsi    # 0x66a520
callq 0x12bef0
movsd 0x148(%rsp), %xmm0
leaq 0x38(%rsp), %rdi
leaq 0x80(%rsp), %rsi
callq 0x282b0
leaq 0x50(%rsp), %rdi
leaq 0x38(%rsp), %rsi
callq 0x12cde0
movsd 0x140(%rsp), %xmm0
leaq 0x20(%rsp), %rdi
leaq 0x68(%rsp), %rsi
callq 0x282b0
leaq 0x50(%rsp), %rdi
leaq 0x20(%rsp), %rsi
callq 0x12cde0
movsd 0x148(%rsp), %xmm0
addsd 0x140(%rsp), %xmm0
leaq 0x50(%rsp), %rdi
callq 0x12d0c0
leaq 0x50(%rsp), %rdi
callq 0x12c610
movq 0x18(%rsp), %rsi
movq %rsi, %rdi
addq $0x88, %rdi
addq $0x40, %rsi
leaq 0x50(%rsp), %rdx
callq 0x1295b0
jmp 0x3d1cb
jmp 0x3d1cd
jmp 0x3d1cf
addq $0x168, %rsp            # imm = 0x168
retq
nopw (%rax,%rax)