// File: /workspace/github_cpp_makefile_star2_O2/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:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x78, %rsp
movapd %xmm0, %xmm2
movq %rdx, %r14
movq %rdi, %r15
xorpd %xmm0, %xmm0
ucomisd %xmm0, %xmm2
jne 0x27abe
jp 0x27abe
movl (%r14), %eax
movl %eax, (%r15)
leaq 0x8(%r14), %rsi
leaq 0x8(%r15), %rdi
callq 0x1a054
leaq 0x40(%r14), %rsi
leaq 0x40(%r15), %rdi
callq 0x9067a
leaq 0x58(%r14), %rsi
leaq 0x58(%r15), %rdi
callq 0x2c19c
movl $0x88, %eax
addq %rax, %r14
addq %rax, %r15
movq %r15, %rdi
movq %r14, %rsi
jmp 0x27b09
movq %rsi, %r12
ucomisd %xmm0, %xmm1
movl (%rsi), %ecx
jne 0x27b1c
jp 0x27b1c
movl %ecx, (%r15)
leaq 0x8(%r12), %rsi
leaq 0x8(%r15), %rdi
callq 0x1a054
leaq 0x40(%r12), %rsi
leaq 0x40(%r15), %rdi
callq 0x9067a
leaq 0x58(%r12), %rsi
leaq 0x58(%r15), %rdi
callq 0x2c19c
movl $0x88, %eax
addq %rax, %r12
addq %rax, %r15
movq %r15, %rdi
movq %r12, %rsi
addq $0x78, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x2c1c6
movl (%r14), %eax
movsd %xmm0, (%rsp)
cmpl %ecx, %eax
jg 0x27b2c
movapd %xmm2, %xmm0
cmpl %eax, %ecx
cmovgl %ecx, %eax
jg 0x27b3c
movapd %xmm0, %xmm2
movsd %xmm1, (%rsp)
movsd %xmm2, 0x8(%rsp)
movl %eax, (%r15)
leaq 0x40(%r15), %rbx
leaq 0x535de0(%rip), %rsi    # 0x55d930
movq %rbx, %rdi
callq 0x9067a
leaq 0x40(%r12), %rsi
leaq 0x30(%rsp), %r13
movq %r13, %rdi
movsd 0x8(%rsp), %xmm0
callq 0x90b04
movq %rbx, %rdi
movq %r13, %rsi
callq 0x909fa
leaq 0x40(%r14), %rsi
leaq 0x30(%rsp), %r13
movq %r13, %rdi
movsd (%rsp), %xmm0
callq 0x90b04
movq %rbx, %rdi
movq %r13, %rsi
callq 0x909fa
movsd 0x8(%rsp), %xmm0
addsd (%rsp), %xmm0
movq %rbx, %rdi
movsd %xmm0, 0x10(%rsp)
callq 0x90a78
leaq 0x8(%r15), %r13
leaq 0x1350c8(%rip), %rsi    # 0x15cc88
movq %r13, %rdi
callq 0x1a054
leaq 0x8(%r12), %rsi
movq %r13, %rdi
callq 0x7c90a
leaq 0x8(%r14), %rsi
movq %r13, %rdi
callq 0x7c90a
leaq 0x58(%r15), %r13
leaq 0x535bf4(%rip), %rsi    # 0x55d7e0
movq %r13, %rdi
callq 0x2c19c
leaq 0x88(%r15), %rbp
leaq 0x53595e(%rip), %rsi    # 0x55d560
movq %rbp, %rdi
callq 0x2c1c6
movl (%r15), %eax
cmpl $0x3, %eax
je 0x27cea
cmpl $0x2, %eax
jne 0x27db2
addq $0x70, %r12
leaq 0x30(%rsp), %r15
movq %r15, %rdi
movq %r12, %rsi
callq 0x90b60
addq $0x70, %r14
leaq 0x18(%rsp), %r12
movq %r12, %rdi
movq %r14, %rsi
callq 0x90b60
movq %r15, %rdi
movq %r12, %rsi
callq 0x90dca
xorpd %xmm1, %xmm1
ucomisd %xmm0, %xmm1
jbe 0x27c66
leaq 0x18(%rsp), %rdi
callq 0x90ea6
leaq 0xcf86b(%rip), %rsi     # 0xf74d8
leaq 0x60(%rsp), %r14
movq %r14, %rdi
callq 0x90b60
leaq 0x48(%rsp), %r15
leaq 0x30(%rsp), %rsi
movq %r15, %rdi
movsd 0x8(%rsp), %xmm0
callq 0x91246
movq %r14, %rdi
movq %r15, %rsi
callq 0x91268
leaq 0x48(%rsp), %r15
leaq 0x18(%rsp), %rsi
movq %r15, %rdi
movsd (%rsp), %xmm0
callq 0x91246
movq %r14, %rdi
movq %r15, %rsi
callq 0x91268
movq %r14, %rdi
movsd 0x10(%rsp), %xmm0
callq 0x9144e
movq %r14, %rdi
callq 0x90e52
movq %r13, %rdi
movq %rbx, %rsi
movq %r14, %rdx
xorl %ecx, %ecx
callq 0x2c1ec
jmp 0x27db2
movl $0x88, %r13d
addq %r13, %r12
leaq 0x30(%rsp), %r15
movq %r15, %rdi
movq %r12, %rsi
callq 0x90b60
addq %r13, %r14
leaq 0x18(%rsp), %r12
movq %r12, %rdi
movq %r14, %rsi
callq 0x90b60
movq %r15, %rdi
movq %r12, %rsi
callq 0x90dca
xorpd %xmm1, %xmm1
ucomisd %xmm0, %xmm1
jbe 0x27d35
leaq 0x18(%rsp), %rdi
callq 0x90ea6
leaq 0xcf79c(%rip), %rsi     # 0xf74d8
leaq 0x60(%rsp), %r14
movq %r14, %rdi
callq 0x90b60
leaq 0x48(%rsp), %r15
leaq 0x30(%rsp), %rsi
movq %r15, %rdi
movsd 0x8(%rsp), %xmm0
callq 0x91246
movq %r14, %rdi
movq %r15, %rsi
callq 0x91268
leaq 0x48(%rsp), %r15
leaq 0x18(%rsp), %rsi
movq %r15, %rdi
movsd (%rsp), %xmm0
callq 0x91246
movq %r14, %rdi
movq %r15, %rsi
callq 0x91268
movq %r14, %rdi
movsd 0x10(%rsp), %xmm0
callq 0x9144e
movq %r14, %rdi
callq 0x90e52
movq %rbp, %rdi
movq %rbx, %rsi
movq %r14, %rdx
callq 0x8f756
addq $0x78, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
nop