// File: /workspace/github_cpp_makefile_star2_O2/tomfunkhouser[P]gaps/pkgs/R3Utils/R3Segmentation.cpp
// Function: gaps::R3SegmentationPrimitive::Update(gaps::R3SegmentationPoint*, gaps::RNArray<gaps::R3SegmentationPoint*> const*)
void R3SegmentationPrimitive::
Update(R3SegmentationPoint *seed_point, const RNArray<R3SegmentationPoint *> *points)
{
  // Remember stuff about primitive (so can set same orientation)
  R3Vector previous_vector(0,0,0);
  if (primitive_type == R3_SEGMENTATION_LINE_PRIMITIVE_TYPE) previous_vector = line.Vector();
  if (primitive_type == R3_SEGMENTATION_PLANE_PRIMITIVE_TYPE) previous_vector = plane.Normal();

  // Update bounding box
  bbox = R3null_box;
  if (points) {
    for (int i = 0; i < points->NEntries(); i++) {
      R3SegmentationPoint *point = points->Kth(i);
      bbox.Union(point->position);
    }
  }

  // Initialize everything
  if (seed_point) {
    R3Point seed_position = seed_point->position;
    centroid = seed_position;
    line.Reset(seed_position, line.Vector()); 
    plane.Reset(seed_position, seed_point->normal);
    bbox.Union(seed_position);
  }
  else {
    // Temporary
    RNAbort("Need seed point");
  }

  // Update based on points
  if (points && (points->NEntries() > 0)) {
    // Allocate arrays of point positions and weights
    const int max_positions = 1024;
    R3Point *positions = new R3Point [ max_positions ];
    RNScalar *weights = new RNScalar [ max_positions ];

    // Fill arrays of point positions and weights
    int npositions = 0;
    int skip = points->NEntries() / max_positions + 1;
    for (int i = 0; i < points->NEntries(); i += skip) {
      R3SegmentationPoint *point = points->Kth(i);
      if (npositions >= max_positions-1) break;
      positions[npositions] = point->position;
      if (primitive_type == R3_SEGMENTATION_PLANE_PRIMITIVE_TYPE)
        weights[npositions] = fabs(plane.Normal().Dot(point->normal));
      else if (primitive_type == R3_SEGMENTATION_LINE_PRIMITIVE_TYPE)
        weights[npositions] = 1.0 - fabs(plane.Normal().Dot(point->normal));
      else weights[npositions] = 1.0;
      npositions++;
    }

    // Add seed point with 20% of the total weight
    if (seed_point) {
      positions[npositions] = seed_point->position;
      weights[npositions] = 0.2 * points->NEntries();
      npositions++;
    }

    // Compute centroid
    centroid = R3Centroid(npositions, positions, weights);

    // Update primitive parameters
    if ((primitive_type == R3_SEGMENTATION_NULL_PRIMITIVE_TYPE) && (npositions >= 2)) {
      RNScalar variances[3];
      R3Triad axes = R3PrincipleAxes(centroid, npositions, positions, weights, variances);
      if (variances[0] > RN_EPSILON) {
        if (variances[1] > RN_EPSILON) {
          RNScalar ratio10 = variances[1] / variances[0];
          RNScalar ratio21 = variances[2] / variances[1];
          if (ratio10 < ratio21) {
            primitive_type = R3_SEGMENTATION_LINE_PRIMITIVE_TYPE;
            line.Reset(centroid, axes[0]);
          }
          else {
            primitive_type = R3_SEGMENTATION_PLANE_PRIMITIVE_TYPE;
            plane.Reset(centroid, axes[2]);
          }
        }
      }
    }
    else if ((primitive_type == R3_SEGMENTATION_LINE_PRIMITIVE_TYPE) && (npositions >= 2)) {
      // Compute principle directions
      RNScalar variances[3];
      R3Triad axes = R3PrincipleAxes(centroid, npositions, positions, weights, variances);
      if (variances[0] > RN_EPSILON) {
        // Update line
        R3Vector direction = axes[0];
        line.Reset(centroid, direction);

        // Check if should flip line
        RNScalar dot = direction.Dot(previous_vector);
        if (dot < 0) line.Flip();
      }
    }
    else if ((primitive_type == R3_SEGMENTATION_PLANE_PRIMITIVE_TYPE) && (npositions >= 3)) {
      // Compute principle directions
      RNScalar variances[3];
      R3Triad axes = R3PrincipleAxes(centroid, npositions, positions, weights, variances);
      if (variances[1] > RN_EPSILON) {
        // Update plane
        R3Vector normal = axes[2];
        plane.Reset(centroid, normal);

        // Check if should flip plane
        if (seed_point) {
          RNScalar dot = normal.Dot(seed_point->normal);
          if (dot < 0) plane.Flip();
        }
        else {
          RNScalar dot = normal.Dot(previous_vector);
          if (dot < 0) plane.Flip();
        }
      }
    }

#if 0
    // Rasterize planar grid
    if (primitive_type == R3_SEGMENTATION_PLANAR_GRID_PRIMITIVE_TYPE) {
      // Rasterize points into planar grid
      R3PlanarGrid density(plane, bbox, min_cluster_spacing);
      for (int i = 0; i < points->NEntries(); i++) {
        R3SegmentationPoint *point = points->Kth(i);
        R3Point position = point->position;
        density.RasterizeWorldR3SegmentationPoint(position.X(), position.Y(), position.Z(), 1.0);
      }

      // Reset planar grid
      planar_grid.Reset(plane, bbox, min_cluster_spacing);
      R3Point seed_position = seed_point->position;
      R2R3SegmentationPoint grid_position = planar_grid.GridPosition(seed_position);
      int ix = (int) (grid_position.X() + 0.5);
      int iy = (int) (grid_position.Y() + 0.5);
      FloodCopy(density.grid, planar_grid.grid, ix, iy);
    }
#endif

    // Delete array of point positions and weights
    delete [] positions;
    delete [] weights;
  }
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xc8, %rsp
movq %rdx, %r13
movq %rsi, %rbp
movq %rdi, %r12
leaq 0x50(%rsp), %rdi
xorpd %xmm0, %xmm0
xorpd %xmm1, %xmm1
xorpd %xmm2, %xmm2
callq 0x90b50
movl (%r12), %eax
cmpl $0x2, %eax
jne 0x27468
leaq 0x70(%r12), %rsi
leaq 0x50(%rsp), %rdi
callq 0x910c0
movl (%r12), %eax
cmpl $0x3, %eax
jne 0x2747f
leaq 0x88(%r12), %rsi
leaq 0x50(%rsp), %rdi
callq 0x910c0
leaq 0x8(%r12), %r14
leaq 0x1357fd(%rip), %rsi    # 0x15cc88
movq %r14, %rdi
callq 0x1a054
testq %r13, %r13
je 0x274bc
xorl %ebx, %ebx
movslq 0xc(%r13), %rax
cmpq %rax, %rbx
jge 0x274bc
movq (%r13), %rax
movq (%rax,%rbx,8), %rsi
addq $0x8, %rsi
movq %r14, %rdi
callq 0x7c858
incq %rbx
jmp 0x2749a
testq %rbp, %rbp
je 0x27517
leaq 0x8(%rbp), %rsi
leaq 0x80(%rsp), %r15
movq %r15, %rdi
callq 0x903a0
leaq 0x40(%r12), %rdi
movq %r15, %rsi
callq 0x9067a
leaq 0x58(%r12), %rdi
leaq 0x70(%r12), %rdx
movq %r15, %rsi
xorl %ecx, %ecx
callq 0x2c1ec
leaq 0x88(%r12), %rdi
leaq 0x20(%rbp), %rdx
movq %r15, %rsi
callq 0x8f756
movq %r14, %rdi
movq %r15, %rsi
callq 0x7c858
jmp 0x27525
leaq 0xc7d1e(%rip), %rdi     # 0xef23c
xorl %eax, %eax
callq 0xb4478
testq %r13, %r13
je 0x278ed
cmpl $0x0, 0xc(%r13)
jle 0x278ed
movl $0x6000, %edi           # imm = 0x6000
callq 0x15040
movq %rax, %r14
xorl %ebx, %ebx
leaq (%r14,%rbx), %rdi
callq 0x9038e
addq $0x18, %rbx
cmpq $0x6000, %rbx           # imm = 0x6000
jne 0x27548
movq %rbp, 0x18(%rsp)
movl $0x2000, %edi           # imm = 0x2000
callq 0x15040
movq %rax, 0x10(%rsp)
movl 0xc(%r13), %ecx
movl $0x400, %esi            # imm = 0x400
movl %ecx, %eax
cltd
idivl %esi
movq %r12, 0x20(%rsp)
leaq 0x88(%r12), %rdx
movq %rdx, 0x8(%rsp)
incl %eax
movslq %eax, %rbx
xorl %r15d, %r15d
movq %r14, 0x28(%rsp)
xorl %ebp, %ebp
movslq %ecx, %rax
cmpq %rax, %rbp
jge 0x27650
cmpq $0x3ff, %r15            # imm = 0x3FF
je 0x2764a
movq (%r13), %rax
movq (%rax,%rbp,8), %r12
leaq 0x8(%r12), %rsi
movq %r14, %rdi
callq 0x9067a
movq 0x20(%rsp), %rax
movl (%rax), %eax
cmpl $0x2, %eax
je 0x27603
movsd 0xc6a2d(%rip), %xmm0    # 0xee010
cmpl $0x3, %eax
jne 0x2762c
addq $0x20, %r12
movq 0x8(%rsp), %rdi
movq %r12, %rsi
callq 0x90dca
andpd 0xc6eef(%rip), %xmm0    # 0xee4f0
jmp 0x2762c
addq $0x20, %r12
movq 0x8(%rsp), %rdi
movq %r12, %rsi
callq 0x90dca
movapd %xmm0, %xmm1
andpd 0xc6ed0(%rip), %xmm1    # 0xee4f0
movsd 0xc69e8(%rip), %xmm0    # 0xee010
subsd %xmm1, %xmm0
movq 0x10(%rsp), %rax
movsd %xmm0, (%rax,%r15,8)
incq %r15
addq %rbx, %rbp
movl 0xc(%r13), %ecx
addq $0x18, %r14
jmp 0x275a1
movl $0x3ff, %r15d           # imm = 0x3FF
movq 0x18(%rsp), %rax
testq %rax, %rax
movq 0x20(%rsp), %r12
movq 0x28(%rsp), %rbp
je 0x2769a
leaq 0x8(%rax), %rsi
movl %r15d, %ebx
imulq $0x18, %rbx, %rax
movq %rbp, %rdi
addq %rax, %rdi
callq 0x9067a
cvtsi2sdl 0xc(%r13), %xmm0
mulsd 0xc7968(%rip), %xmm0    # 0xeeff0
movq 0x10(%rsp), %rax
movsd %xmm0, (%rax,%rbx,8)
movq %rax, %rbx
incl %r15d
jmp 0x2769f
movq 0x10(%rsp), %rbx
leaq 0x80(%rsp), %r14
movq %r14, %rdi
movl %r15d, %esi
movq %rbp, %rdx
movq %rbx, %rcx
callq 0x64ef0
leaq 0x40(%r12), %r13
movq %r13, %rdi
movq %r14, %rsi
callq 0x9067a
movl (%r12), %eax
testl %eax, %eax
setne %cl
cmpl $0x2, %r15d
setl %dl
orb %cl, %dl
jne 0x27775
leaq 0x80(%rsp), %rdi
leaq 0x30(%rsp), %r14
movq %r13, %rsi
movl %r15d, %edx
movq %rbp, %rcx
movq %rbx, %r8
movq %r14, %r9
callq 0x64f79
movsd (%r14), %xmm0
leaq 0x13465b(%rip), %rax    # 0x15bd68
movsd (%rax), %xmm2
ucomisd %xmm2, %xmm0
jbe 0x278dd
movsd 0x38(%rsp), %xmm1
ucomisd %xmm2, %xmm1
jbe 0x278dd
movapd %xmm1, %xmm2
movhpd 0x40(%rsp), %xmm2       # xmm2 = xmm2[0],mem[0]
unpcklpd %xmm1, %xmm0            # xmm0 = xmm0[0],xmm1[0]
divpd %xmm0, %xmm2
movapd %xmm2, %xmm0
unpckhpd %xmm2, %xmm0            # xmm0 = xmm0[1],xmm2[1]
ucomisd %xmm2, %xmm0
jbe 0x27896
movl $0x2, (%r12)
addq $0x58, %r12
leaq 0x80(%rsp), %rdx
movq %r12, %rdi
movq %r13, %rsi
xorl %ecx, %ecx
callq 0x2c1ec
jmp 0x278dd
cmpl $0x2, %r15d
setl %cl
cmpl $0x2, %eax
setne %dl
orb %cl, %dl
jne 0x27814
leaq 0x80(%rsp), %rdi
leaq 0x30(%rsp), %r14
movq %r13, %rsi
movl %r15d, %edx
movq %rbp, %rcx
movq %rbx, %r8
movq %r14, %r9
callq 0x64f79
movsd (%r14), %xmm0
leaq 0x1345b1(%rip), %rax    # 0x15bd68
ucomisd (%rax), %xmm0
jbe 0x278dd
leaq 0x68(%rsp), %r14
leaq 0x80(%rsp), %rsi
movq %r14, %rdi
callq 0x90b60
leaq 0x58(%r12), %rdi
movq %r13, %rsi
movq %r14, %rdx
xorl %ecx, %ecx
callq 0x2c1ec
leaq 0x50(%rsp), %rsi
movq %r14, %rdi
callq 0x90dca
xorpd %xmm1, %xmm1
ucomisd %xmm0, %xmm1
jbe 0x278dd
addq $0x70, %r12
movq %r12, %rdi
callq 0x90ea6
jmp 0x278dd
cmpl $0x3, %eax
setne %al
cmpl $0x3, %r15d
setl %cl
orb %al, %cl
jne 0x278dd
leaq 0x80(%rsp), %rdi
movq %rbx, %r8
leaq 0x30(%rsp), %rbx
movq %r13, %rsi
movl %r15d, %edx
movq %rbp, %rcx
movq %rbx, %r9
callq 0x64f79
movsd 0x8(%rbx), %xmm0
leaq 0x134512(%rip), %rax    # 0x15bd68
ucomisd (%rax), %xmm0
jbe 0x278d8
leaq 0xb0(%rsp), %rsi
leaq 0x68(%rsp), %rbx
movq %rbx, %rdi
callq 0x90b60
movq 0x8(%rsp), %rdi
movq %r13, %rsi
movq %rbx, %rdx
callq 0x8f756
movq 0x18(%rsp), %rsi
testq %rsi, %rsi
je 0x278b5
addq $0x20, %rsi
leaq 0x68(%rsp), %rdi
jmp 0x278bf
movl $0x3, (%r12)
leaq 0xb0(%rsp), %rdx
movq 0x8(%rsp), %rdi
movq %r13, %rsi
callq 0x8f756
jmp 0x278dd
leaq 0x68(%rsp), %rdi
leaq 0x50(%rsp), %rsi
callq 0x90dca
xorpd %xmm1, %xmm1
ucomisd %xmm0, %xmm1
jbe 0x278d8
movq 0x8(%rsp), %rdi
callq 0x2c222
movq 0x10(%rsp), %rbx
movq %rbp, %rdi
callq 0x15810
movq %rbx, %rdi
callq 0x15810
addq $0xc8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rax, %rbx
movl $0x6000, %esi           # imm = 0x6000
movq %r14, %rdi
callq 0x15c50
movq %rbx, %rdi
callq 0x15af0
nop