// File: /workspace/github_cpp_makefile_star2_O1/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
movsd (%rsi), %xmm1
movsd (%rdi), %xmm0
movsd 0x41047(%rip), %xmm2    # 0x6b078
addsd %xmm0, %xmm2
ucomisd %xmm2, %xmm1
jb 0x2a081
movq %rdi, %r15
movsd 0x8(%rdi), %xmm2
addsd 0x40ff5(%rip), %xmm2    # 0x6b040
ucomisd %xmm1, %xmm2
jb 0x2a081
movq %rsi, %r14
movsd 0x8(%rsi), %xmm1
movsd 0x10(%r15), %xmm2
addsd 0x41011(%rip), %xmm2    # 0x6b078
ucomisd %xmm2, %xmm1
jb 0x2a081
movsd 0x18(%r15), %xmm2
addsd 0x40fc5(%rip), %xmm2    # 0x6b040
ucomisd %xmm1, %xmm2
jae 0x2a092
xorl %eax, %eax
addq $0x68, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rdx, %rbx
xorpd %xmm1, %xmm1
movapd %xmm1, -0x60(%rbp)
movapd %xmm1, -0x70(%rbp)
movapd %xmm1, -0x80(%rbp)
movapd %xmm1, -0x90(%rbp)
movsd 0x8(%r15), %xmm1
movapd %xmm0, %xmm2
subsd %xmm1, %xmm2
andpd 0x40f4a(%rip), %xmm2    # 0x6b010
movsd 0x40f72(%rip), %xmm3    # 0x6b040
ucomisd %xmm2, %xmm3
movabsq $-0x4010000000000000, %rax # imm = 0xBFF0000000000000
movabsq $0x3ff0000000000000, %rcx # imm = 0x3FF0000000000000
jbe 0x2a0fe
movq %rcx, -0x40(%rbp)
movq $0x0, -0x38(%rbp)
xorpd 0x40f24(%rip), %xmm0    # 0x6b020
jmp 0x2a129
movsd 0x10(%r15), %xmm2
movapd %xmm2, %xmm3
subsd %xmm2, %xmm3
subsd %xmm0, %xmm1
divsd %xmm1, %xmm3
movsd %xmm3, -0x40(%rbp)
movq %rax, -0x38(%rbp)
mulsd %xmm0, %xmm3
subsd %xmm3, %xmm2
movapd %xmm2, %xmm0
leaq -0x40(%rbp), %rdi
movsd %xmm0, 0x10(%rdi)
leaq -0x90(%rbp), %rdx
movq %r14, %rsi
movq %rdx, -0x48(%rbp)
callq 0x29ecc
xorl %r12d, %r12d
testb %al, %al
je 0x2a1b0
movsd -0x90(%rbp), %xmm0
movsd (%r15), %xmm1
addsd 0x40f17(%rip), %xmm1    # 0x6b078
ucomisd %xmm1, %xmm0
jb 0x2a1b0
movsd 0x8(%r15), %xmm1
addsd 0x40ecb(%rip), %xmm1    # 0x6b040
ucomisd %xmm0, %xmm1
jb 0x2a1b0
movsd -0x88(%rbp), %xmm0
movsd 0x10(%r15), %xmm1
addsd 0x40ee7(%rip), %xmm1    # 0x6b078
ucomisd %xmm1, %xmm0
jb 0x2a1b0
movsd 0x18(%r15), %xmm1
addsd 0x40e9b(%rip), %xmm1    # 0x6b040
xorl %r12d, %r12d
ucomisd %xmm0, %xmm1
setae %r12b
movsd (%r15), %xmm0
movsd 0x8(%r15), %xmm1
movapd %xmm0, %xmm2
subsd %xmm1, %xmm2
andpd 0x40e45(%rip), %xmm2    # 0x6b010
movsd 0x40e6d(%rip), %xmm3    # 0x6b040
ucomisd %xmm2, %xmm3
jbe 0x2a1f9
movabsq $0x3ff0000000000000, %rax # imm = 0x3FF0000000000000
movq %rax, -0x40(%rbp)
movq $0x0, -0x38(%rbp)
xorpd 0x40e29(%rip), %xmm0    # 0x6b020
jmp 0x2a22e
movsd 0x18(%r15), %xmm2
movapd %xmm2, %xmm3
subsd %xmm2, %xmm3
subsd %xmm0, %xmm1
divsd %xmm1, %xmm3
movsd %xmm3, -0x40(%rbp)
movabsq $-0x4010000000000000, %rax # imm = 0xBFF0000000000000
movq %rax, -0x38(%rbp)
mulsd %xmm0, %xmm3
subsd %xmm3, %xmm2
movapd %xmm2, %xmm0
leaq -0x40(%rbp), %rdi
movsd %xmm0, 0x10(%rdi)
movl %r12d, %eax
shll $0x4, %eax
leaq (%rax,%rbp), %r13
addq $-0x90, %r13
movq %r14, %rsi
movq %r13, %rdx
callq 0x29ecc
testb %al, %al
je 0x2a2ba
movsd (%r15), %xmm1
addsd 0x40e14(%rip), %xmm1    # 0x6b078
movsd (%r13), %xmm0
xorl %eax, %eax
ucomisd %xmm1, %xmm0
jb 0x2a2b7
movsd 0x8(%r15), %xmm1
addsd 0x40dc0(%rip), %xmm1    # 0x6b040
ucomisd %xmm0, %xmm1
jb 0x2a2b7
movsd 0x8(%r13), %xmm0
movsd 0x10(%r15), %xmm1
addsd 0x40dde(%rip), %xmm1    # 0x6b078
ucomisd %xmm1, %xmm0
jb 0x2a2b7
movsd 0x18(%r15), %xmm1
addsd 0x40d92(%rip), %xmm1    # 0x6b040
xorl %eax, %eax
ucomisd %xmm0, %xmm1
setae %al
addl %eax, %r12d
movsd (%r15), %xmm0
movapd %xmm0, %xmm1
subsd %xmm0, %xmm1
movapd 0x40d41(%rip), %xmm2    # 0x6b010
andpd %xmm1, %xmm2
movsd 0x40d65(%rip), %xmm3    # 0x6b040
ucomisd %xmm2, %xmm3
jbe 0x2a301
movabsq $0x3ff0000000000000, %rax # imm = 0x3FF0000000000000
movq %rax, -0x40(%rbp)
movq $0x0, -0x38(%rbp)
xorpd 0x40d21(%rip), %xmm0    # 0x6b020
jmp 0x2a334
movsd 0x10(%r15), %xmm2
movsd 0x18(%r15), %xmm3
subsd %xmm2, %xmm3
divsd %xmm1, %xmm3
movsd %xmm3, -0x40(%rbp)
movabsq $-0x4010000000000000, %rax # imm = 0xBFF0000000000000
movq %rax, -0x38(%rbp)
mulsd %xmm3, %xmm0
subsd %xmm0, %xmm2
movapd %xmm2, %xmm0
leaq -0x40(%rbp), %rdi
movsd %xmm0, 0x10(%rdi)
movl %r12d, %eax
shll $0x4, %eax
leaq (%rax,%rbp), %r13
addq $-0x90, %r13
movq %r14, %rsi
movq %r13, %rdx
callq 0x29ecc
testb %al, %al
je 0x2a3c0
movsd (%r15), %xmm1
addsd 0x40d0e(%rip), %xmm1    # 0x6b078
movsd (%r13), %xmm0
xorl %eax, %eax
ucomisd %xmm1, %xmm0
jb 0x2a3bd
movsd 0x8(%r15), %xmm1
addsd 0x40cba(%rip), %xmm1    # 0x6b040
ucomisd %xmm0, %xmm1
jb 0x2a3bd
movsd 0x8(%r13), %xmm0
movsd 0x10(%r15), %xmm1
addsd 0x40cd8(%rip), %xmm1    # 0x6b078
ucomisd %xmm1, %xmm0
jb 0x2a3bd
movsd 0x18(%r15), %xmm1
addsd 0x40c8c(%rip), %xmm1    # 0x6b040
xorl %eax, %eax
ucomisd %xmm0, %xmm1
setae %al
addl %eax, %r12d
movsd 0x8(%r15), %xmm0
movapd %xmm0, %xmm1
subsd %xmm0, %xmm1
movapd 0x40c3a(%rip), %xmm2    # 0x6b010
andpd %xmm1, %xmm2
movsd 0x40c5e(%rip), %xmm3    # 0x6b040
ucomisd %xmm2, %xmm3
jbe 0x2a408
movabsq $0x3ff0000000000000, %rax # imm = 0x3FF0000000000000
movq %rax, -0x40(%rbp)
movq $0x0, -0x38(%rbp)
xorpd 0x40c1a(%rip), %xmm0    # 0x6b020
jmp 0x2a43b
movsd 0x10(%r15), %xmm2
movsd 0x18(%r15), %xmm3
subsd %xmm2, %xmm3
divsd %xmm1, %xmm3
movsd %xmm3, -0x40(%rbp)
movabsq $-0x4010000000000000, %rax # imm = 0xBFF0000000000000
movq %rax, -0x38(%rbp)
mulsd %xmm3, %xmm0
subsd %xmm0, %xmm2
movapd %xmm2, %xmm0
leaq -0x40(%rbp), %rdi
movsd %xmm0, 0x10(%rdi)
movl %r12d, %eax
shll $0x4, %eax
leaq (%rax,%rbp), %r13
addq $-0x90, %r13
movq %r14, %rsi
movq %r13, %rdx
callq 0x29ecc
testb %al, %al
je 0x2a4c7
movsd (%r15), %xmm1
addsd 0x40c07(%rip), %xmm1    # 0x6b078
movsd (%r13), %xmm0
xorl %eax, %eax
ucomisd %xmm1, %xmm0
jb 0x2a4c4
movsd 0x8(%r15), %xmm1
addsd 0x40bb3(%rip), %xmm1    # 0x6b040
ucomisd %xmm0, %xmm1
jb 0x2a4c4
movsd 0x8(%r13), %xmm0
movsd 0x10(%r15), %xmm1
addsd 0x40bd1(%rip), %xmm1    # 0x6b078
ucomisd %xmm1, %xmm0
jb 0x2a4c4
movsd 0x18(%r15), %xmm1
addsd 0x40b85(%rip), %xmm1    # 0x6b040
xorl %eax, %eax
ucomisd %xmm0, %xmm1
setae %al
addl %eax, %r12d
testl %r12d, %r12d
setne %al
je 0x2a083
cmpl $0x1, %r12d
je 0x2a567
xorl %ecx, %ecx
cmpl $0x2, %r12d
jl 0x2a555
movsd (%r14), %xmm0
movsd 0x8(%r14), %xmm1
leaq -0x78(%rbp), %rdx
movsd -0x18(%rdx), %xmm2
movsd -0x10(%rdx), %xmm3
subsd %xmm0, %xmm2
subsd %xmm1, %xmm3
mulsd %xmm3, %xmm3
mulsd %xmm2, %xmm2
addsd %xmm3, %xmm2
movl %r12d, %esi
movl $0x1, %edi
movsd -0x8(%rdx), %xmm3
movsd (%rdx), %xmm4
subsd %xmm0, %xmm3
subsd %xmm1, %xmm4
mulsd %xmm4, %xmm4
mulsd %xmm3, %xmm3
addsd %xmm4, %xmm3
ucomisd %xmm2, %xmm3
cmoval %edi, %ecx
maxsd %xmm2, %xmm3
incq %rdi
addq $0x10, %rdx
movapd %xmm3, %xmm2
cmpq %rdi, %rsi
jne 0x2a51a
movslq %ecx, %rcx
shlq $0x4, %rcx
addq %rbp, %rcx
addq $-0x90, %rcx
movq %rcx, -0x48(%rbp)
movq -0x48(%rbp), %rcx
movupd (%rcx), %xmm0
movupd %xmm0, (%rbx)
jmp 0x2a083