// File: tomfunkhouser[P]gaps/pkgs/R3Graphics/R3AreaLight.cpp
// Function: gaps::R3AreaLight::IntensityAtPoint(gaps::R3Point const&) const
RNScalar R3AreaLight::
IntensityAtPoint(const R3Point& point) const
{
    // Parameter ????
    const int max_samples = 4;

    // Check if light is active
    if (!IsActive()) return 0.0;

    // Check if point is in front of light 
    if (R3SignedDistance(circle.Plane(), point) <= 0) return 0.0;

    // Get circle axes
    R3Vector direction = circle.Normal();
    RNDimension dim = direction.MinDimension();
    R3Vector axis1 = direction % R3xyz_triad[dim];
    axis1.Normalize();
    R3Vector axis2 = direction % axis1;
    axis2.Normalize();

    // Sample points on light source
    int sample_count = 0;
    RNScalar sample_sum = 0.0;
    while (sample_count < max_samples) {
        // Sample point in circle
        RNScalar r1 = 2.0 * RNRandomScalar() - 1.0;
        RNScalar r2 = 2.0 * RNRandomScalar() - 1.0;
        if (r1*r1 + r2*r2 > 1) continue;
        R3Point sample_point = Position();
        sample_point += r1 * Radius() * axis1;
        sample_point += r2 * Radius() * axis2;
        sample_count++;
        
        // Compute direction at point
        R3Vector L = sample_point - point;
        L.Normalize();

        // Compute intensity at point
        RNScalar I = Intensity() * Direction().Dot(-L);
        if (RNIsNegativeOrZero(I)) return 0.0;
        RNLength d = R3Distance(point, sample_point);
        RNScalar denom = constant_attenuation;
        denom += d * linear_attenuation;
        denom += d * d * quadratic_attenuation;
        if (RNIsPositive(denom)) I /= denom;

        // Add to result
        sample_sum += I;
    }

    // Return average
    RNArea area = circle.Area();
    RNScalar sample_mean = sample_sum;
    if (sample_count > 0) sample_mean /= sample_count;
    return area * sample_mean;
}

// Assembly:
subq $0x158, %rsp            # imm = 0x158
movq %rdi, 0x148(%rsp)
movq %rsi, 0x140(%rsp)
movq 0x148(%rsp), %rdi
movq %rdi, 0x20(%rsp)
movl $0x4, 0x13c(%rsp)
callq 0x60940
cmpl $0x0, %eax
jne 0x5f4ca
xorps %xmm0, %xmm0
movsd %xmm0, 0x150(%rsp)
jmp 0x5f8c3
movq 0x20(%rsp), %rdi
addq $0x50, %rdi
callq 0x60950
movq %rax, %rdi
movq 0x140(%rsp), %rsi
callq 0x77d10
movaps %xmm0, %xmm1
xorps %xmm0, %xmm0
ucomisd %xmm1, %xmm0
jb 0x5f505
xorps %xmm0, %xmm0
movsd %xmm0, 0x150(%rsp)
jmp 0x5f8c3
movq 0x20(%rsp), %rdi
addq $0x50, %rdi
callq 0x4e7b0
movq %rax, %rsi
leaq 0x120(%rsp), %rdi
callq 0xcda10
leaq 0x120(%rsp), %rdi
callq 0xcdc30
movl %eax, 0x11c(%rsp)
movl 0x11c(%rsp), %esi
leaq 0x19f643(%rip), %rdi    # 0x1feb88
callq 0x60960
movq %rax, %rdx
leaq 0x100(%rsp), %rdi
leaq 0x120(%rsp), %rsi
callq 0xce810
leaq 0x100(%rsp), %rdi
callq 0xce130
leaq 0xe8(%rsp), %rdi
leaq 0x120(%rsp), %rsi
leaq 0x100(%rsp), %rdx
callq 0xce810
leaq 0xe8(%rsp), %rdi
callq 0xce130
movl $0x0, 0xe4(%rsp)
xorps %xmm0, %xmm0
movsd %xmm0, 0xd8(%rsp)
cmpl $0x4, 0xe4(%rsp)
jge 0x5f868
callq 0xf2580
addsd %xmm0, %xmm0
movsd 0x12efe1(%rip), %xmm1   # 0x18e5b0
addsd %xmm1, %xmm0
movsd %xmm0, 0xd0(%rsp)
callq 0xf2580
addsd %xmm0, %xmm0
movsd 0x12efc3(%rip), %xmm1   # 0x18e5b0
addsd %xmm1, %xmm0
movsd %xmm0, 0xc8(%rsp)
movsd 0xd0(%rsp), %xmm0
movsd 0xd0(%rsp), %xmm2
movsd 0xc8(%rsp), %xmm1
mulsd 0xc8(%rsp), %xmm1
mulsd %xmm2, %xmm0
addsd %xmm1, %xmm0
movsd 0x12e9e2(%rip), %xmm1   # 0x18e010
ucomisd %xmm1, %xmm0
jbe 0x5f639
jmp 0x5f5b0
movq 0x20(%rsp), %rdi
callq 0x4e6e0
movq %rax, %rsi
leaq 0xb0(%rsp), %rdi
callq 0xcc8b0
movq 0x20(%rsp), %rdi
movsd 0xd0(%rsp), %xmm0
movsd %xmm0, (%rsp)
callq 0x4e720
movaps %xmm0, %xmm1
movsd (%rsp), %xmm0
mulsd %xmm1, %xmm0
leaq 0x98(%rsp), %rdi
leaq 0x100(%rsp), %rsi
callq 0x2b630
leaq 0xb0(%rsp), %rdi
leaq 0x98(%rsp), %rsi
callq 0xccfb0
movq 0x20(%rsp), %rdi
movsd 0xc8(%rsp), %xmm0
movsd %xmm0, 0x8(%rsp)
callq 0x4e720
movaps %xmm0, %xmm1
movsd 0x8(%rsp), %xmm0
mulsd %xmm1, %xmm0
leaq 0x80(%rsp), %rdi
leaq 0xe8(%rsp), %rsi
callq 0x2b630
leaq 0xb0(%rsp), %rdi
leaq 0x80(%rsp), %rsi
callq 0xccfb0
movl 0xe4(%rsp), %eax
addl $0x1, %eax
movl %eax, 0xe4(%rsp)
movq 0x140(%rsp), %rdx
leaq 0x68(%rsp), %rdi
leaq 0xb0(%rsp), %rsi
callq 0xccbd0
leaq 0x68(%rsp), %rdi
callq 0xce130
movq 0x20(%rsp), %rdi
callq 0x60980
movq 0x20(%rsp), %rdi
movsd %xmm0, 0x18(%rsp)
callq 0x4e700
movq %rax, 0x10(%rsp)
leaq 0x48(%rsp), %rdi
leaq 0x68(%rsp), %rsi
callq 0xcecc0
movq 0x10(%rsp), %rdi
leaq 0x48(%rsp), %rsi
callq 0xcdfe0
movaps %xmm0, %xmm1
movsd 0x18(%rsp), %xmm0
mulsd %xmm1, %xmm0
movsd %xmm0, 0x60(%rsp)
movsd 0x60(%rsp), %xmm0
leaq 0x196b25(%rip), %rax    # 0x1f62a8
movsd (%rax), %xmm1
callq 0x49e40
cmpl $0x0, %eax
je 0x5f7a2
xorps %xmm0, %xmm0
movsd %xmm0, 0x150(%rsp)
jmp 0x5f8c3
movq 0x140(%rsp), %rdi
leaq 0xb0(%rsp), %rsi
callq 0x779c0
movq 0x20(%rsp), %rax
movsd %xmm0, 0x40(%rsp)
movsd 0x98(%rax), %xmm0
movsd %xmm0, 0x38(%rsp)
movsd 0x40(%rsp), %xmm0
movsd 0xa0(%rax), %xmm2
movsd 0x38(%rsp), %xmm1
mulsd %xmm2, %xmm0
addsd %xmm1, %xmm0
movsd %xmm0, 0x38(%rsp)
movsd 0x40(%rsp), %xmm0
mulsd 0x40(%rsp), %xmm0
movsd 0xa8(%rax), %xmm2
movsd 0x38(%rsp), %xmm1
mulsd %xmm2, %xmm0
addsd %xmm1, %xmm0
movsd %xmm0, 0x38(%rsp)
movsd 0x38(%rsp), %xmm0
leaq 0x196a81(%rip), %rax    # 0x1f62a8
movsd (%rax), %xmm1
callq 0x4a000
cmpl $0x0, %eax
je 0x5f84b
movsd 0x38(%rsp), %xmm1
movsd 0x60(%rsp), %xmm0
divsd %xmm1, %xmm0
movsd %xmm0, 0x60(%rsp)
movsd 0x60(%rsp), %xmm0
addsd 0xd8(%rsp), %xmm0
movsd %xmm0, 0xd8(%rsp)
jmp 0x5f5b0
movq 0x20(%rsp), %rdi
addq $0x50, %rdi
callq 0xac230
movsd %xmm0, 0x30(%rsp)
movsd 0xd8(%rsp), %xmm0
movsd %xmm0, 0x28(%rsp)
cmpl $0x0, 0xe4(%rsp)
jle 0x5f8ae
cvtsi2sdl 0xe4(%rsp), %xmm1
movsd 0x28(%rsp), %xmm0
divsd %xmm1, %xmm0
movsd %xmm0, 0x28(%rsp)
movsd 0x30(%rsp), %xmm0
mulsd 0x28(%rsp), %xmm0
movsd %xmm0, 0x150(%rsp)
movsd 0x150(%rsp), %xmm0
addq $0x158, %rsp            # imm = 0x158
retq
nopw %cs:(%rax,%rax)