// File: /workspace/github_cpp_makefile_star2_O1/tomfunkhouser[P]gaps/pkgs/R3Graphics/R3AreaLight.cpp
// Function: gaps::R3AreaLight::DiffuseReflection(gaps::R3Brdf const&, gaps::R3Point const&, gaps::R3Vector const&) const
RNRgb R3AreaLight::
DiffuseReflection(const R3Brdf& brdf, 
    const R3Point& point, const R3Vector& normal) const
{
    // Parameter ????
    const int max_samples = 4;

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

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

    // Get material properties
    const RNRgb& Dc = brdf.Diffuse();

    // Get light properties
    const RNRgb& Ic = Color();

    // 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;
    RNRgb sample_sum = RNblack_rgb;
    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 RNblack_rgb;
        RNLength d = R3Distance(point, sample_point);
        RNScalar denom = constant_attenuation;
        denom += d * linear_attenuation;
        denom += d * d * quadratic_attenuation;
        if (RNIsPositive(denom)) I /= denom;

        // Compute diffuse reflection from sample point
        RNScalar NL = normal.Dot(L);
        if (RNIsNegativeOrZero(NL)) continue;
        RNRgb diffuse = (I * NL) * Dc * Ic;

        // Add to result
        sample_sum += diffuse;
    }

    // Return diffuse reflection from area
    RNArea area = circle.Area();
    RNRgb sample_mean = sample_sum;
    if (sample_count > 0) sample_mean /= sample_count;
    return area * sample_mean;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x148, %rsp            # imm = 0x148
movq %r8, 0x68(%rsp)
movq %rdx, 0x40(%rsp)
movq %rdi, %rbx
cmpl $0x0, 0x20(%rsi)
je 0x415f5
movq %rcx, %r15
movq %rsi, %r13
leaq 0x78(%rsi), %r14
movq %r14, %rdi
movq %rcx, %rsi
callq 0x4ee87
xorpd %xmm1, %xmm1
ucomisd %xmm0, %xmm1
jae 0x415f5
movq %r15, 0x70(%rsp)
movq %rbx, 0x10(%rsp)
leaq 0x50(%r13), %rax
movq %rax, 0x38(%rsp)
leaq 0x130(%rsp), %rbx
movq %rbx, %rdi
movq %r14, 0x80(%rsp)
movq %r14, %rsi
callq 0x76d62
movq %rbx, %rdi
callq 0x76eaa
cltq
leaq (%rax,%rax,2), %rdx
shlq $0x3, %rdx
addq 0xfdd1c(%rip), %rdx     # 0x13efa0
leaq 0x118(%rsp), %r14
movq %r14, %rdi
movq %rbx, %rsi
callq 0x776a6
movq %r14, %rdi
callq 0x77074
leaq 0x100(%rsp), %r12
movq %r12, %rdi
movq %rbx, %rsi
movq %r14, %rdx
callq 0x776a6
movq %r12, %rdi
callq 0x77074
leaq 0x10731c(%rip), %rsi    # 0x1485e0
leaq 0xa0(%rsp), %rdi
callq 0x88512
leaq 0x58(%r13), %rax
movq %rax, 0x78(%rsp)
xorl %ebp, %ebp
leaq 0xe8(%rsp), %r12
leaq 0xd0(%rsp), %rbx
leaq 0xfede5(%rip), %r14     # 0x1400d8
movq %r14, %r15
callq 0x8a700
movapd %xmm0, %xmm1
addsd %xmm0, %xmm1
movsd 0x9b1c5(%rip), %xmm0    # 0xdc4d0
addsd %xmm0, %xmm1
movsd %xmm1, (%rsp)
callq 0x8a700
addsd %xmm0, %xmm0
addsd 0x9b1ab(%rip), %xmm0    # 0xdc4d0
movapd %xmm0, %xmm2
mulsd %xmm0, %xmm2
movsd (%rsp), %xmm1
mulsd %xmm1, %xmm1
addsd %xmm2, %xmm1
movl $0x2, %eax
ucomisd 0x9acd1(%rip), %xmm1    # 0xdc018
jbe 0x41351
movq %r15, %r14
jmp 0x41586
movl %ebp, 0x1c(%rsp)
movq %r12, %rdi
movq 0x78(%rsp), %rsi
movsd %xmm0, 0x20(%rsp)
callq 0x762ca
movsd (%rsp), %xmm0
mulsd 0x70(%r13), %xmm0
movq %rbx, %rdi
leaq 0x118(%rsp), %rsi
callq 0x77722
movq %r12, %rdi
movq %rbx, %rsi
callq 0x766a2
movsd 0x20(%rsp), %xmm0
mulsd 0x70(%r13), %xmm0
movq %rbx, %rdi
leaq 0x100(%rsp), %rsi
callq 0x77722
movq %r12, %rdi
movq %rbx, %rsi
callq 0x766a2
movq %rbx, %rdi
movq %r12, %rsi
movq %r12, %rbp
movq 0x70(%rsp), %r12
movq %r12, %rdx
callq 0x7651c
movq %rbx, %rdi
callq 0x77074
movsd 0x28(%r13), %xmm0
movsd %xmm0, (%rsp)
leaq 0xb8(%rsp), %r14
movq %r14, %rdi
movq %rbx, %rsi
callq 0x779e8
movq 0x80(%rsp), %rdi
movq %r14, %rsi
callq 0x76fea
mulsd (%rsp), %xmm0
movq %r15, %r14
movsd (%r15), %xmm1
ucomisd %xmm0, %xmm1
jae 0x4155f
movapd %xmm0, (%rsp)
movq %r12, %rdi
movq %rbp, %rsi
callq 0x4ec74
movsd %xmm0, 0x50(%rsp)
movsd 0x98(%r13), %xmm0
movsd %xmm0, 0x58(%rsp)
movsd 0xa0(%r13), %xmm0
movsd %xmm0, 0x60(%rsp)
movsd 0xa8(%r13), %xmm0
movsd %xmm0, 0x48(%rsp)
movsd (%r14), %xmm0
movapd %xmm0, 0x20(%rsp)
movq 0x68(%rsp), %rdi
movq %rbx, %rsi
callq 0x76fea
movapd %xmm0, %xmm2
movsd (%r14), %xmm0
movl $0x2, %eax
ucomisd %xmm2, %xmm0
movq %rbp, %r12
jae 0x41578
movsd 0x50(%rsp), %xmm3
movapd %xmm3, %xmm0
mulsd %xmm3, %xmm0
mulsd 0x48(%rsp), %xmm0
movsd 0x60(%rsp), %xmm1
mulsd %xmm3, %xmm1
addsd 0x58(%rsp), %xmm1
addsd %xmm0, %xmm1
movapd (%rsp), %xmm3
movapd %xmm3, %xmm0
divsd %xmm1, %xmm0
movapd 0x20(%rsp), %xmm4
cmpnltsd %xmm1, %xmm4
movapd %xmm4, %xmm1
andnpd %xmm0, %xmm1
andpd %xmm3, %xmm4
orpd %xmm1, %xmm4
mulsd %xmm4, %xmm2
movq 0x40(%rsp), %rax
movsd 0x38(%rax), %xmm0
mulsd %xmm2, %xmm0
movsd 0x40(%rax), %xmm1
mulsd %xmm2, %xmm1
mulsd 0x48(%rax), %xmm2
leaq 0x88(%rsp), %rdi
callq 0x88530
movsd 0x88(%rsp), %xmm0
movsd 0x90(%rsp), %xmm1
mulsd 0x30(%r13), %xmm0
mulsd 0x38(%r13), %xmm1
movsd 0x98(%rsp), %xmm2
mulsd 0x40(%r13), %xmm2
leaq 0xb8(%rsp), %rbx
movq %rbx, %rdi
callq 0x88530
leaq 0xa0(%rsp), %rdi
movq %rbx, %rsi
callq 0x885e2
movq %r15, %r14
xorl %eax, %eax
jmp 0x41578
movq 0x10(%rsp), %rdi
leaq 0x107075(%rip), %rsi    # 0x1485e0
callq 0x88512
movl $0x1, %eax
movq %rbp, %r12
movl 0x1c(%rsp), %ebp
incl %ebp
leaq 0xd0(%rsp), %rbx
testb $0x1, %al
jne 0x415ee
cmpl $0x4, %ebp
jl 0x412f3
movq 0x38(%rsp), %rdi
callq 0x6847a
movsd %xmm0, (%rsp)
leaq 0xe8(%rsp), %rbx
leaq 0xa0(%rsp), %rsi
movq %rbx, %rdi
callq 0x88512
xorps %xmm0, %xmm0
cvtsi2sd %ebp, %xmm0
movq %rbx, %rdi
callq 0x8868e
movsd (%rbx), %xmm0
movsd (%rsp), %xmm2
mulsd %xmm2, %xmm0
movsd 0x8(%rbx), %xmm1
mulsd %xmm2, %xmm1
mulsd 0x10(%rbx), %xmm2
movq 0x10(%rsp), %rdi
callq 0x88530
movq 0x10(%rsp), %rax
jmp 0x41607
leaq 0x106fe4(%rip), %rsi    # 0x1485e0
movq %rbx, %rdi
callq 0x88512
movq %rbx, %rax
addq $0x148, %rsp            # imm = 0x148
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
nop