// File: /workspace/github_cpp_makefile_star2_O2/wrighteagle2d[P]wrighteaglebase/Debug/../src/Geometry.cpp
// Function: Rectangular::Intersection(Ray const&, Vector&) const
bool Rectangular::Intersection(const Ray &r, Vector &point) const {
  if (!IsWithin(r.Origin())) {
    return false; // do not deal with this condition
  }

  int n = 0;
  Array<Vector, 4> points; // there may be 4 intersections

  if (TopEdge().Intersection(r, points[n])) {
    if (IsWithin(points[n])) {
      n++;
    }
  }

  if (BottomEdge().Intersection(r, points[n])) {
    if (IsWithin(points[n])) {
      n++;
    }
  }

  if (LeftEdge().Intersection(r, points[n])) {
    if (IsWithin(points[n])) {
      n++;
    }
  }

  if (RightEdge().Intersection(r, points[n])) {
    if (IsWithin(points[n])) {
      n++;
    }
  }

  if (n == 0) {
    return false;
  } else if (n == 1) {
    point = points[0];
    return true;
  } else if (n >= 2) {
    int max_idx = 0;
    double max_dist2 = points[0].Dist2(r.Origin());

    for (int j = 1; j < n; ++j) {
      double dist2 = points[j].Dist2(r.Origin());
      if (dist2 > max_dist2) {
        max_idx = j;
        max_dist2 = dist2;
      }
    }

    point = points[max_idx];
    return true;
  }

  return false;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x68, %rsp
movq %rdx, %rbx
movq %rsi, %r14
movq %rdi, %r13
movabsq $0x3ed92a737110e454, %rax # imm = 0x3ED92A737110E454
leaq -0x90(%rbp), %rdx
movq %rax, (%rdx)
callq 0x12516
xorl %r15d, %r15d
testb %al, %al
je 0x23cad
movq %rbx, -0x50(%rbp)
xorpd %xmm0, %xmm0
leaq -0x90(%rbp), %r12
movapd %xmm0, 0x30(%r12)
movapd %xmm0, 0x20(%r12)
movapd %xmm0, 0x10(%r12)
movapd %xmm0, (%r12)
leaq -0x48(%rbp), %rbx
movq %rbx, %rdi
movq %r13, %rsi
callq 0x242ea
movq %rbx, %rdi
movq %r14, %rsi
movq %r12, %rdx
callq 0x23916
testb %al, %al
je 0x23afb
leaq -0x48(%rbp), %rdx
movabsq $0x3ed92a737110e454, %rax # imm = 0x3ED92A737110E454
movq %rax, (%rdx)
leaq -0x90(%rbp), %rsi
movq %r13, %rdi
callq 0x12516
movzbl %al, %r15d
leaq -0x48(%rbp), %r12
movq %r12, %rdi
movq %r13, %rsi
callq 0x24330
movl %r15d, %eax
shll $0x4, %eax
leaq (%rax,%rbp), %rbx
addq $-0x90, %rbx
movq %r12, %rdi
movq %r14, -0x30(%rbp)
movq %r14, %rsi
movq %rbx, %rdx
callq 0x23916
testb %al, %al
je 0x23b58
leaq -0x48(%rbp), %rdx
movabsq $0x3ed92a737110e454, %rax # imm = 0x3ED92A737110E454
movq %rax, (%rdx)
movq %r13, %rdi
movq %rbx, %rsi
callq 0x12516
movzbl %al, %eax
addl %eax, %r15d
movq %r15, %r14
jmp 0x23b5b
movl %r15d, %r14d
movq %r12, %rdi
movq %r13, %rsi
callq 0x24376
movl %r14d, %eax
shll $0x4, %eax
leaq (%rax,%rbp), %rbx
addq $-0x90, %rbx
movq %r12, %rdi
movq -0x30(%rbp), %rsi
movq %rbx, %rdx
callq 0x23916
testb %al, %al
je 0x23baf
leaq -0x48(%rbp), %rdx
movabsq $0x3ed92a737110e454, %rax # imm = 0x3ED92A737110E454
movq %rax, (%rdx)
movq %r13, %rdi
movq %rbx, %rsi
callq 0x12516
movzbl %al, %eax
addl %eax, %r15d
movq %r15, %r14
movq %r12, %rdi
movq %r13, %rsi
callq 0x243bc
shll $0x4, %r14d
leaq (%r14,%rbp), %rbx
addq $-0x90, %rbx
movq %r12, %rdi
movq -0x30(%rbp), %r14
movq %r14, %rsi
movq %rbx, %rdx
callq 0x23916
testb %al, %al
je 0x23c04
leaq -0x48(%rbp), %rdx
movabsq $0x3ed92a737110e454, %rax # imm = 0x3ED92A737110E454
movq %rax, (%rdx)
movq %r13, %rdi
movq %rbx, %rsi
callq 0x12516
testb %al, %al
je 0x23c04
incl %r15d
jmp 0x23c0d
testl %r15d, %r15d
je 0x23caa
cmpl $0x1, %r15d
movq -0x50(%rbp), %rdi
leaq -0x90(%rbp), %rax
je 0x23c9d
movsd (%r14), %xmm0
movsd 0x8(%r14), %xmm1
leaq -0x78(%rbp), %rax
movsd -0x18(%rax), %xmm2
movsd -0x10(%rax), %xmm3
subsd %xmm0, %xmm2
subsd %xmm1, %xmm3
mulsd %xmm3, %xmm3
mulsd %xmm2, %xmm2
addsd %xmm3, %xmm2
movl %r15d, %ecx
xorl %edx, %edx
pushq $0x1
popq %rsi
cmpq %rcx, %rsi
jae 0x23c8d
movsd -0x8(%rax), %xmm3
movsd (%rax), %xmm4
subsd %xmm0, %xmm3
subsd %xmm1, %xmm4
mulsd %xmm4, %xmm4
mulsd %xmm3, %xmm3
addsd %xmm4, %xmm3
ucomisd %xmm2, %xmm3
cmoval %esi, %edx
maxsd %xmm2, %xmm3
incq %rsi
addq $0x10, %rax
movapd %xmm3, %xmm2
jmp 0x23c53
movslq %edx, %rax
shlq $0x4, %rax
addq %rbp, %rax
addq $-0x90, %rax
movupd (%rax), %xmm0
movupd %xmm0, (%rdi)
movb $0x1, %r15b
jmp 0x23cad
xorl %r15d, %r15d
movl %r15d, %eax
addq $0x68, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
nop