// File: /workspace/github_cpp_makefile_star2_O3/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 $0x138, %rsp            # imm = 0x138
movq %r8, 0x50(%rsp)
movq %rdx, 0x38(%rsp)
movq %rdi, %rbx
cmpl $0x0, 0x20(%rsi)
je 0x41124
movq %rcx, %r15
movq %rsi, %r13
leaq 0x78(%rsi), %r14
movq %r14, %rdi
movq %rcx, %rsi
callq 0x4e2e7
xorpd %xmm1, %xmm1
ucomisd %xmm0, %xmm1
jae 0x41124
movq %rbx, 0x18(%rsp)
leaq 0x50(%r13), %rax
movq %rax, 0x30(%rsp)
leaq 0x120(%rsp), %rbx
movq %rbx, %rdi
movq %r14, 0x48(%rsp)
movq %r14, %rsi
callq 0x75e6e
movq %rbx, %rdi
callq 0x75faa
cltq
leaq (%rax,%rax,2), %rdx
shlq $0x3, %rdx
addq 0xfc1c6(%rip), %rdx     # 0x13cfa0
leaq 0x108(%rsp), %r14
movq %r14, %rdi
movq %rbx, %rsi
callq 0x76792
movq %r14, %rdi
callq 0x76174
leaq 0xf0(%rsp), %r12
movq %r12, %rdi
movq %rbx, %rsi
movq %r14, %rdx
callq 0x76792
movq %r12, %rdi
callq 0x76174
leaq 0x1057c6(%rip), %rsi    # 0x1465e0
leaq 0xa8(%rsp), %rdi
callq 0x873e2
leaq 0x58(%r13), %rax
movq %rax, 0x40(%rsp)
xorl %r12d, %r12d
leaq 0x90(%rsp), %r14
leaq 0xd8(%rsp), %rbx
leaq 0xfd28e(%rip), %rax     # 0x13e0d8
movq %rax, %rbp
callq 0x89368
movapd %xmm0, %xmm1
addsd %xmm0, %xmm1
movsd 0x9966e(%rip), %xmm0    # 0xda4d0
addsd %xmm0, %xmm1
movsd %xmm1, (%rsp)
callq 0x89368
addsd %xmm0, %xmm0
addsd 0x99654(%rip), %xmm0    # 0xda4d0
movapd %xmm0, %xmm2
mulsd %xmm0, %xmm2
movsd (%rsp), %xmm1
mulsd %xmm1, %xmm1
addsd %xmm2, %xmm1
ucomisd 0x9917f(%rip), %xmm1    # 0xda018
jbe 0x40ea3
movq %rbp, %rax
jmp 0x410b9
movl %r12d, 0x14(%rsp)
movq %r14, %rdi
movq 0x40(%rsp), %rsi
movsd %xmm0, 0x20(%rsp)
callq 0x753da
movsd (%rsp), %xmm0
mulsd 0x70(%r13), %xmm0
movq %rbx, %rdi
leaq 0x108(%rsp), %rsi
callq 0x767fa
movq %r14, %rdi
movq %rbx, %rsi
callq 0x757ac
movsd 0x20(%rsp), %xmm0
mulsd 0x70(%r13), %xmm0
movq %rbx, %rdi
leaq 0xf0(%rsp), %rsi
callq 0x767fa
movq %r14, %rdi
movq %rbx, %rsi
callq 0x757ac
movq %rbx, %rdi
movq %r14, %rsi
movq %r15, %rdx
callq 0x75626
movq %rbx, %rdi
callq 0x76174
movsd 0x28(%r13), %xmm0
movsd %xmm0, (%rsp)
movq %r15, %r12
leaq 0xc0(%rsp), %r15
movq %r15, %rdi
movq %rbx, %rsi
callq 0x76a98
movq 0x48(%rsp), %rdi
movq %r15, %rsi
callq 0x760ea
mulsd (%rsp), %xmm0
movsd (%rbp), %xmm1
ucomisd %xmm0, %xmm1
jae 0x41148
movapd %xmm0, (%rsp)
movq %r12, %rdi
movq %r14, %rsi
callq 0x4e0d4
movsd %xmm0, 0x60(%rsp)
movsd 0x98(%r13), %xmm0
movsd %xmm0, 0x68(%rsp)
movsd 0xa0(%r13), %xmm0
movsd %xmm0, 0x70(%rsp)
movsd 0xa8(%r13), %xmm0
movsd %xmm0, 0x58(%rsp)
movsd (%rbp), %xmm0
movapd %xmm0, 0x20(%rsp)
movq 0x50(%rsp), %rdi
movq %rbx, %rsi
callq 0x760ea
movq %rbp, %rax
movapd %xmm0, %xmm2
movsd (%rbp), %xmm0
ucomisd %xmm2, %xmm0
movq %r12, %r15
jae 0x410a1
movsd 0x60(%rsp), %xmm3
movapd %xmm3, %xmm0
mulsd %xmm3, %xmm0
mulsd 0x58(%rsp), %xmm0
movsd 0x70(%rsp), %xmm1
mulsd %xmm3, %xmm1
addsd 0x68(%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 0x38(%rsp), %rax
movsd 0x38(%rax), %xmm0
mulsd %xmm2, %xmm0
movsd 0x40(%rax), %xmm1
mulsd %xmm2, %xmm1
mulsd 0x48(%rax), %xmm2
leaq 0x78(%rsp), %rdi
callq 0x87400
movsd 0x78(%rsp), %xmm0
movsd 0x80(%rsp), %xmm1
mulsd 0x30(%r13), %xmm0
mulsd 0x38(%r13), %xmm1
movsd 0x88(%rsp), %xmm2
mulsd 0x40(%r13), %xmm2
movq %r15, %r14
leaq 0xc0(%rsp), %r15
movq %r15, %rdi
callq 0x87400
leaq 0xa8(%rsp), %rdi
movq %r15, %rsi
movq %r14, %r15
callq 0x874b2
movq %rbp, %rax
movl 0x14(%rsp), %r12d
incl %r12d
leaq 0x90(%rsp), %r14
leaq 0xd8(%rsp), %rbx
cmpl $0x4, %r12d
jl 0x40e4a
movq 0x30(%rsp), %rdi
callq 0x676ea
movsd %xmm0, (%rsp)
leaq 0x90(%rsp), %rbx
leaq 0xa8(%rsp), %rsi
movq %rbx, %rdi
callq 0x873e2
xorps %xmm0, %xmm0
cvtsi2sd %r12d, %xmm0
movq %rbx, %rdi
callq 0x8755e
movsd (%rbx), %xmm0
movsd (%rsp), %xmm2
mulsd %xmm2, %xmm0
movsd 0x8(%rbx), %xmm1
mulsd %xmm2, %xmm1
mulsd 0x10(%rbx), %xmm2
movq 0x18(%rsp), %rbx
movq %rbx, %rdi
callq 0x87400
jmp 0x41133
leaq 0x1054b5(%rip), %rsi    # 0x1465e0
movq %rbx, %rdi
callq 0x873e2
movq %rbx, %rax
addq $0x138, %rsp            # imm = 0x138
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x105491(%rip), %rsi    # 0x1465e0
movq 0x18(%rsp), %rbx
jmp 0x4112b