// File: tomfunkhouser[P]gaps/pkgs/R3Utils/R3SurfelViewer.cpp
// Function: gaps::R3SurfelViewer::MouseMotion(int, int)
int R3SurfelViewer::
MouseMotion(int x, int y)
{
  // Initialize
  int redraw = 0;

  // Compute mouse movement
  int dx = x - mouse_position[0];
  int dy = y - mouse_position[1];

  // Set viewing center point
  R3Point viewing_center_point = center_point;
  const R3Camera& camera = viewer.Camera();
  R3Plane camera_plane(camera.Origin(), camera.Towards());
  RNScalar signed_distance = R3SignedDistance(camera_plane, viewing_center_point);
  if (signed_distance < 0) viewing_center_point -= (signed_distance - 1) * camera.Towards();
  
  // World in hand navigation 
  if (shift_down && mouse_button[0]) viewer.ScaleWorld(2.0, viewing_center_point, x, y, dx, dy);
  else if (ctrl_down && mouse_button[0]) viewer.TranslateWorld(2.0, viewing_center_point, x, y, dx, dy);
  else if (mouse_button[0]) RotateWorld(1.0, viewing_center_point, x, y, dx, dy);
  else if (mouse_button[1]) viewer.ScaleWorld(2.0, viewing_center_point, x, y, dx, dy);
  else if (mouse_button[2]) viewer.TranslateWorld(2.0, viewing_center_point, x, y, dx, dy);
  if (mouse_button[0] || mouse_button[1] || mouse_button[2]) redraw = 1;

  // Remember mouse position 
  mouse_position[0] = x;
  mouse_position[1] = y;

  // Update mouse drag movement
  mouse_drag_distance_squared += dx*dx + dy*dy;

  // Return whether need redraw
  return redraw;
}

// Assembly:
subq $0xc8, %rsp
movq %rdi, 0xc0(%rsp)
movl %esi, 0xbc(%rsp)
movl %edx, 0xb8(%rsp)
movq 0xc0(%rsp), %rsi
movq %rsi, 0x8(%rsp)
movl $0x0, 0xb4(%rsp)
movl 0xbc(%rsp), %eax
subl 0x4a8(%rsi), %eax
movl %eax, 0xb0(%rsp)
movl 0xb8(%rsp), %eax
subl 0x4ac(%rsi), %eax
movl %eax, 0xac(%rsp)
addq $0x2b0, %rsi            # imm = 0x2B0
leaq 0x90(%rsp), %rdi
callq 0x162300
movq 0x8(%rsp), %rdi
addq $0x78, %rdi
callq 0x2f6d0
movq %rax, 0x88(%rsp)
movq 0x88(%rsp), %rdi
callq 0x2ee60
movq %rax, 0x10(%rsp)
movq 0x88(%rsp), %rsi
leaq 0x50(%rsp), %rdi
callq 0x2eea0
movq 0x10(%rsp), %rsi
leaq 0x68(%rsp), %rdi
leaq 0x50(%rsp), %rdx
callq 0x15fa20
leaq 0x68(%rsp), %rdi
leaq 0x90(%rsp), %rsi
callq 0xf1220
movsd %xmm0, 0x48(%rsp)
xorps %xmm0, %xmm0
ucomisd 0x48(%rsp), %xmm0
jbe 0x366a1
movsd 0x48(%rsp), %xmm0
movsd 0x20a9b0(%rip), %xmm1   # 0x241010
subsd %xmm1, %xmm0
movsd %xmm0, (%rsp)
movq 0x88(%rsp), %rsi
leaq 0x18(%rsp), %rdi
callq 0x2eea0
movsd (%rsp), %xmm0
leaq 0x30(%rsp), %rdi
leaq 0x18(%rsp), %rsi
callq 0x2f4b0
leaq 0x90(%rsp), %rdi
leaq 0x30(%rsp), %rsi
callq 0x163000
movq 0x8(%rsp), %rax
cmpl $0x0, 0x4bc(%rax)
je 0x366fe
movq 0x8(%rsp), %rax
cmpl $0x0, 0x49c(%rax)
je 0x366fe
movq 0x8(%rsp), %rdi
addq $0x78, %rdi
movl 0xbc(%rsp), %edx
movl 0xb8(%rsp), %ecx
movl 0xb0(%rsp), %r8d
movl 0xac(%rsp), %r9d
movsd 0x20ae9c(%rip), %xmm0   # 0x241588
leaq 0x90(%rsp), %rsi
callq 0xcdba0
jmp 0x36848
movq 0x8(%rsp), %rax
cmpl $0x0, 0x4c0(%rax)
je 0x3675b
movq 0x8(%rsp), %rax
cmpl $0x0, 0x49c(%rax)
je 0x3675b
movq 0x8(%rsp), %rdi
addq $0x78, %rdi
movl 0xbc(%rsp), %edx
movl 0xb8(%rsp), %ecx
movl 0xb0(%rsp), %r8d
movl 0xac(%rsp), %r9d
movsd 0x20ae3f(%rip), %xmm0   # 0x241588
leaq 0x90(%rsp), %rsi
callq 0xcd9d0
jmp 0x36846
movq 0x8(%rsp), %rax
cmpl $0x0, 0x49c(%rax)
je 0x367aa
movq 0x8(%rsp), %rdi
movl 0xbc(%rsp), %edx
movl 0xb8(%rsp), %ecx
movl 0xb0(%rsp), %r8d
movl 0xac(%rsp), %r9d
movq (%rdi), %rax
movsd 0x20a879(%rip), %xmm0   # 0x241010
leaq 0x90(%rsp), %rsi
callq *0x358(%rax)
jmp 0x36844
movq 0x8(%rsp), %rax
cmpl $0x0, 0x4a0(%rax)
je 0x367f6
movq 0x8(%rsp), %rdi
addq $0x78, %rdi
movl 0xbc(%rsp), %edx
movl 0xb8(%rsp), %ecx
movl 0xb0(%rsp), %r8d
movl 0xac(%rsp), %r9d
movsd 0x20ada1(%rip), %xmm0   # 0x241588
leaq 0x90(%rsp), %rsi
callq 0xcdba0
jmp 0x36842
movq 0x8(%rsp), %rax
cmpl $0x0, 0x4a4(%rax)
je 0x36840
movq 0x8(%rsp), %rdi
addq $0x78, %rdi
movl 0xbc(%rsp), %edx
movl 0xb8(%rsp), %ecx
movl 0xb0(%rsp), %r8d
movl 0xac(%rsp), %r9d
movsd 0x20ad55(%rip), %xmm0   # 0x241588
leaq 0x90(%rsp), %rsi
callq 0xcd9d0
jmp 0x36842
jmp 0x36844
jmp 0x36846
jmp 0x36848
movq 0x8(%rsp), %rax
cmpl $0x0, 0x49c(%rax)
jne 0x36872
movq 0x8(%rsp), %rax
cmpl $0x0, 0x4a0(%rax)
jne 0x36872
movq 0x8(%rsp), %rax
cmpl $0x0, 0x4a4(%rax)
je 0x3687d
movl $0x1, 0xb4(%rsp)
movq 0x8(%rsp), %rax
movl 0xbc(%rsp), %ecx
movl %ecx, 0x4a8(%rax)
movl 0xb8(%rsp), %ecx
movl %ecx, 0x4ac(%rax)
movl 0xb0(%rsp), %ecx
imull 0xb0(%rsp), %ecx
movl 0xac(%rsp), %edx
imull 0xac(%rsp), %edx
addl %edx, %ecx
addl 0x4b8(%rax), %ecx
movl %ecx, 0x4b8(%rax)
movl 0xb4(%rsp), %eax
addq $0xc8, %rsp
retq
nopw (%rax,%rax)