// File: /workspace/github_cpp_makefile_star2_O2/intheswim[P]FractalTubes/m3d.cpp
// Function: M3DDrawer::ShowInPreviewWindow(ShapeParams*, DrawParams*, pointData**, corePointData**)
bool M3DDrawer::ShowInPreviewWindow (struct ShapeParams *sp, struct DrawParams *dp,
                                     struct pointData ** pointData,
                                     struct corePointData ** corePoints)
{
	double x_real, y_real, z_real;
	unsigned long raw_color = 0;
	int mandel = 0, fraction = 0;
	double l = sp->l, r = sp->r;
	long col = 0;
	double y_median = sp->y_range / 2;
	int red, green, blue;
    
	float bright = 0, bright0 = 0; // brightness
	long double y1 = 0, alp = 0;
    
#ifdef USE_RUNTIME
	double tilt = set_tilt;
	double cos_tilt, sin_tilt;
#endif

    //	struct MiniMax minimax;
	double slope = atan (sp->y_range / (sp->alpha_range * sp->r));
	double x_norm, y_norm, z_norm;
    
	// trying to allocate space for 640*350=224000 pixels
	// space is neccesary to store z-cordinates
    
    double light_x = light_dir_x, light_y = light_dir_y, light_z = light_dir_z;
    
    double normalizer = sqrt (light_x * light_x + light_y * light_y + light_z * light_z);
	light_x /= normalizer;
	light_y /= normalizer;
	light_z /= normalizer;
    
#ifdef USE_RUNTIME
	  cos_tilt = cos (tilt);
	  sin_tilt = sin (tilt);
#endif
    
	SetMaterialType (moBoth) ; //Metal);    
    
	divisor = sqrt (800.0f * 600.0f / ((client_width) * (client_height)));
    
	sp->r /= divisor;
	sp->l /= divisor;
	sp->y_range /= divisor;
	y_median = y_median / divisor;
	l = sp->l, r = sp->r;
    
	SetGlobals (sp, dp);
        
	pix_count = 0;
    
    *pointData = new struct pointData [sp->total_points];
    
    long core_total = (sp->total_points/m_points_in_cycle);
    
    *corePoints = new struct corePointData [core_total];
    
    if (pointData == NULL || corePoints == NULL)
    {
        m_total_points = 0;
		    return (false);
    }
    
    memset(*corePoints, 0, sizeof(struct corePointData) * core_total);
        
    pix_count = 0;
    
    int core_cnt = 0;
    
    // initial y1 is 0;
    // r is
	while ((alp < sp->alpha_range) && (pix_count < sp->total_points))
    {
        long double alp2 = (((int)(pix_count / m_points_in_cycle)) * m_points_in_cycle) * sp->delta_alpha;
        
        long double beta = (pix_count % m_points_in_cycle) * (sp->delta_alpha * sp->coef);
        
        y1 = ((pix_count / m_points_in_cycle) * m_points_in_cycle) * sp->delta_y;
        
        GetPixelCoordinates (y1, r, l, alp2, beta, sp->coef, slope,
                             x_real, y_real, z_real,
                             x_norm, y_norm, z_norm);
        
        struct pointData *pd = &((*pointData)[pix_count]);
        
        if ((pix_count % m_points_in_cycle) == 0 /*half_cycle*/ && (core_cnt < core_total))
        {
            struct corePointData * cpd = &((*corePoints)[core_cnt]);
            
            alp2 = ((((pix_count / m_points_in_cycle) + 0.5f) * (double)m_points_in_cycle) + 0.5f) * sp->delta_alpha;
            
            //double extra = 0.5f / m_points_in_cycle;
            
            y1 = ((((pix_count / m_points_in_cycle) + 0.5f) * (double)m_points_in_cycle) + 0.5f) * sp->delta_y;
            
            double x_real2, y_real2, z_real2;
            double x_norm2, y_norm2, z_norm2;
            
            GetPixelCoordinates (y1, r, l, alp2, 0, sp->coef, slope,
                                 x_real2, y_real2, z_real2,
                                 x_norm2, y_norm2, z_norm2);

            
            cpd->x = (x_real2 - x_norm2 * l) / r;
            cpd->y = (y_real2 - y_norm2 * l) / r;
            cpd->z = (z_real2 - z_norm2 * l) / r;
            
            cpd->index = (int)pix_count;
            
            core_cnt++;
        }
        
        pd->x = x_real / (r);
        pd->y = y_real / (r);
        pd->z = z_real / (r);
        
        pd->normx = x_norm;
        pd->normy = y_norm;
        pd->normz = z_norm;        
                
        if (true)
        {            
            if (bright_k == 1)
            {
                bright0 = CalcBrightness (light_x, light_y, light_z, x_norm, y_norm, z_norm, 0, 0, 1);
            }
            else
            {
                bright0 = 50;
            }            
            
            // all following manipulations are to smooth the surface:
            // if actual brightness is too high, part of it goes as a bonus
            // to the next point
            
            bright += bright0;
            
            if (bright > 0)
            {
                col = (int)(bright + 0.5);
                
                if (col > 150)
                    col = 150;
                
                // SetPixel(hdc, xs, ys, color);
                if (dp->method == dmOnePoint)
                {
                    raw_color = GetMandelColor (*dp, *sp, alp, alp * sp->coef + alp * currentParams.skew, mandel, fraction);
                }
                else
                {
                    raw_color = GetMandelColor3 (*dp, *sp, alp, alp * sp->coef + alp * currentParams.skew, mandel, fraction);
                }
                
                GetColor (raw_color, col, red, green, blue);
                                
                if (raw_color != 0)
                {                    
                    pd->ucRed = red ;
                    pd->ucBlue = blue ;
                    pd->ucGreen = green ;
                }
                else
                {
                    pd->ucRed = 0;
                    pd->ucBlue = 0;
                    pd->ucGreen = 0;
                }
            }
            bright-=col;
        }
        
        y1 += sp->delta_y ;
        alp += sp->delta_alpha ;
        pix_count++;
    }
    
	pix_count = 0;

    return (true);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x178, %rsp            # imm = 0x178
movq %r8, 0xa8(%rsp)
movq %rcx, %r15
movq %rdx, 0xb0(%rsp)
movq %rsi, %r13
movq %rdi, %r12
andl $0x0, 0x68(%rsp)
movsd 0x10(%rsi), %xmm0
movsd 0x18(%rsi), %xmm1
mulsd 0x40(%rsi), %xmm1
divsd %xmm1, %xmm0
callq 0x32a0
movsd %xmm0, 0xb8(%rsp)
movsd 0x10(%r12), %xmm2
movsd 0x18(%r12), %xmm3
movsd 0x20(%r12), %xmm4
movapd %xmm3, %xmm0
mulsd %xmm3, %xmm0
movapd %xmm2, %xmm1
mulsd %xmm2, %xmm1
addsd %xmm0, %xmm1
movapd %xmm4, %xmm0
mulsd %xmm4, %xmm0
addsd %xmm1, %xmm0
sqrtsd %xmm0, %xmm0
divsd %xmm0, %xmm2
movsd %xmm2, 0x48(%rsp)
divsd %xmm0, %xmm3
movsd %xmm3, 0x58(%rsp)
divsd %xmm0, %xmm4
movsd %xmm4, 0x80(%rsp)
pushq $0x2
popq %rsi
movq %r12, %rdi
callq 0x6c26
movss 0x30(%r12), %xmm1
mulss 0x34(%r12), %xmm1
movss 0x2f9b(%rip), %xmm0     # 0x9404
divss %xmm1, %xmm0
xorps %xmm1, %xmm1
ucomiss %xmm1, %xmm0
jb 0x647b
sqrtss %xmm0, %xmm0
jmp 0x6480
callq 0x3600
movss %xmm0, 0x38(%r12)
cvtss2sd %xmm0, %xmm0
movapd 0x40(%r13), %xmm2
movaps %xmm0, %xmm1
movlhps %xmm0, %xmm1            # xmm1 = xmm1[0],xmm0[0]
divpd %xmm1, %xmm2
movapd %xmm2, 0x90(%rsp)
movapd %xmm2, 0x40(%r13)
movsd 0x10(%r13), %xmm1
divsd %xmm0, %xmm1
movsd %xmm1, 0x10(%r13)
movq %r12, %rdi
movq %r13, %rsi
movq 0xb0(%rsp), %rdx
callq 0x6a3c
andq $0x0, 0x40(%r12)
movq (%r13), %rbx
pushq $0x38
popq %rcx
movq %rbx, %rax
mulq %rcx
pushq $-0x1
popq %rbp
cmovoq %rbp, %rax
movq %rax, %rdi
callq 0x3030
movq %rax, (%r15)
movslq 0x60(%r12), %rcx
movq %rbx, %rax
cqto
idivq %rcx
pushq $0x28
popq %rcx
movq %rax, 0xd0(%rsp)
mulq %rcx
movq %rax, %rbx
cmovnoq %rax, %rbp
movq %rbp, %rdi
callq 0x3030
movq 0xa8(%rsp), %rcx
movq %rax, (%rcx)
xorl %ebp, %ebp
movq %rax, %rdi
xorl %esi, %esi
movq %rbx, %rdx
callq 0x3190
cvtsd2ss 0x48(%rsp), %xmm0
movss %xmm0, 0x74(%rsp)
cvtsd2ss 0x58(%rsp), %xmm0
movss %xmm0, 0x70(%rsp)
cvtsd2ss 0x80(%rsp), %xmm0
movss %xmm0, 0x6c(%rsp)
movaps 0x90(%rsp), %xmm1
movaps %xmm1, %xmm0
movlhps %xmm1, %xmm0            # xmm0 = xmm0[0],xmm1[0]
movaps %xmm0, 0x160(%rsp)
movaps %xmm1, %xmm0
unpckhpd %xmm1, %xmm0            # xmm0 = xmm0[1],xmm1[1]
movaps %xmm0, 0x80(%rsp)
fldz
xorps %xmm1, %xmm1
movl $0x0, 0x64(%rsp)
xorl %ebx, %ebx
movq %r15, 0xc0(%rsp)
movq %rbp, 0x40(%r12)
fldl 0x18(%r13)
fucompi %st(1), %st
jbe 0x6a20
cmpq (%r13), %rbp
jge 0x6a20
movss %xmm1, 0x58(%rsp)
fstpt 0x48(%rsp)
movslq 0x60(%r12), %rcx
movq %rbp, %rax
cqto
idivq %rcx
movl %eax, %esi
imull %ecx, %esi
movl %esi, 0x78(%rsp)
fldt 0x30(%r13)
fld %st(0)
fimull 0x78(%rsp)
movq %rdx, 0xf0(%rsp)
fildll 0xf0(%rsp)
fldt 0x50(%r13)
fmulp %st, %st(3)
fmulp %st, %st(2)
imulq %rcx, %rax
movq %rax, 0xf8(%rsp)
fildll 0xf8(%rsp)
fldt 0x20(%r13)
fmulp %st, %st(1)
fstpl 0xe8(%rsp)
movsd 0xe8(%rsp), %xmm0
leaq 0x130(%rsp), %rax
movq %rax, 0x30(%rsp)
fxch %st(1)
fstpt 0x10(%rsp)
fstpt (%rsp)
movapd 0x90(%rsp), %xmm1
movapd 0x80(%rsp), %xmm2
movsd 0xb8(%rsp), %xmm3
leaq 0x158(%rsp), %rsi
leaq 0x150(%rsp), %rdx
leaq 0x148(%rsp), %rcx
leaq 0x140(%rsp), %r8
leaq 0x138(%rsp), %r9
callq 0x7348
movq 0x40(%r12), %rax
imulq $0x38, %rax, %rbp
addq (%r15), %rbp
movslq 0x60(%r12), %rcx
cqto
idivq %rcx
testq %rdx, %rdx
je 0x66a0
fldt 0x48(%rsp)
jmp 0x680f
movslq 0x64(%rsp), %r14
cmpq %r14, 0xd0(%rsp)
fldt 0x48(%rsp)
jle 0x680f
fstp %st(0)
cvtsi2ss %rax, %xmm0
movq 0xa8(%rsp), %rax
movq (%rax), %r15
movq %rbx, 0xc8(%rsp)
imulq $0x28, %r14, %rbx
addss 0x2c33(%rip), %xmm0     # 0x9310
cvtss2sd %xmm0, %xmm0
cvtsi2sd %ecx, %xmm1
mulsd %xmm0, %xmm1
addsd 0x2b67(%rip), %xmm1     # 0x9258
movsd %xmm1, 0xe0(%rsp)
fldl 0xe0(%rsp)
fldt 0x30(%r13)
fmul %st(1), %st
fldt 0x20(%r13)
fmulp %st, %st(2)
fxch %st(1)
fstpl 0xd8(%rsp)
movsd 0xd8(%rsp), %xmm0
leaq 0x100(%rsp), %rax
movq %rax, 0x30(%rsp)
fldz
fstpt 0x10(%rsp)
fstpt (%rsp)
movaps 0x90(%rsp), %xmm1
movaps 0x80(%rsp), %xmm2
movsd 0xb8(%rsp), %xmm3
leaq 0x128(%rsp), %rsi
leaq 0x120(%rsp), %rdx
leaq 0x118(%rsp), %rcx
leaq 0x110(%rsp), %r8
leaq 0x108(%rsp), %r9
callq 0x7348
fldt 0x48(%rsp)
movsd 0x110(%rsp), %xmm0
movhpd 0x108(%rsp), %xmm0      # xmm0 = xmm0[0],mem[0]
movsd 0x128(%rsp), %xmm1
movhpd 0x120(%rsp), %xmm1      # xmm1 = xmm1[0],mem[0]
movapd 0x80(%rsp), %xmm2
mulpd %xmm2, %xmm0
subpd %xmm0, %xmm1
divpd 0x160(%rsp), %xmm1
movupd %xmm1, (%r15,%rbx)
movsd 0x118(%rsp), %xmm0
movsd 0x100(%rsp), %xmm1
mulsd %xmm2, %xmm1
subsd %xmm1, %xmm0
divsd 0x90(%rsp), %xmm0
movsd %xmm0, 0x10(%r15,%rbx)
movl 0x40(%r12), %eax
movl %eax, 0x20(%r15,%rbx)
movq 0xc8(%rsp), %rbx
movq 0xc0(%rsp), %r15
incl %r14d
movl %r14d, 0x64(%rsp)
movsd 0x158(%rsp), %xmm0
movhpd 0x150(%rsp), %xmm0      # xmm0 = xmm0[0],mem[0]
divpd 0x160(%rsp), %xmm0
movupd %xmm0, 0x8(%rbp)
movsd 0x148(%rsp), %xmm0
divsd 0x90(%rsp), %xmm0
movsd %xmm0, 0x18(%rbp)
movsd 0x140(%rsp), %xmm1
movsd %xmm1, 0x20(%rbp)
movsd 0x138(%rsp), %xmm2
movsd %xmm2, 0x28(%rbp)
movsd 0x130(%rsp), %xmm5
movsd %xmm5, 0x30(%rbp)
cmpl $0x1, 0x64(%r12)
movss 0x2b8a(%rip), %xmm0     # 0x9408
jne 0x68b9
fstp %st(0)
cvtsd2ss %xmm1, %xmm3
cvtsd2ss %xmm2, %xmm4
cvtsd2ss %xmm5, %xmm5
movl $0x3f800000, (%rsp)     # imm = 0x3F800000
xorps %xmm6, %xmm6
xorps %xmm7, %xmm7
movq %r12, %rdi
movss 0x74(%rsp), %xmm0
movss 0x70(%rsp), %xmm1
movss 0x6c(%rsp), %xmm2
callq 0x7516
fldt 0x48(%rsp)
movss 0x58(%rsp), %xmm1
addss %xmm0, %xmm1
ucomiss 0x2b42(%rip), %xmm1     # 0x940c
jbe 0x6a04
cvtss2sd %xmm1, %xmm0
addsd 0x297c(%rip), %xmm0     # 0x9258
cvttsd2si %xmm0, %ebx
movl $0x96, %eax
cmpl %eax, %ebx
jl 0x68ee
movl $0x96, %ebx
movss %xmm1, 0x58(%rsp)
movq 0xb0(%rsp), %rsi
cmpl $0x0, 0xb8(%rsi)
fldt 0x50(%r13)
fld %st(1)
fmuls 0x6c59(%rip)            # 0xd568
fxch %st(1)
fmul %st(2), %st
faddp %st, %st(1)
je 0x6935
fstpt 0x10(%rsp)
fstpt (%rsp)
movq %r12, %rdi
movq %r13, %rdx
leaq 0x7c(%rsp), %rcx
leaq 0x68(%rsp), %r8
callq 0x6d92
jmp 0x6951
fstpt 0x10(%rsp)
fstpt (%rsp)
movq %r12, %rdi
movq %r13, %rdx
leaq 0x7c(%rsp), %rcx
leaq 0x68(%rsp), %r8
callq 0x6c6a
movslq %ebx, %rbx
testq %rax, %rax
je 0x69ef
movl %eax, %ecx
shrl $0x10, %ecx
movl $0xff, %edx
andl %edx, %ecx
imulq %rbx, %rcx
cvtsi2sd %rcx, %xmm0
mulsd 0x2976(%rip), %xmm0     # 0x92f0
cvttsd2si %xmm0, %rcx
movl $0xff, %esi
cmpq %rsi, %rcx
fldt 0x48(%rsp)
movss 0x58(%rsp), %xmm1
jl 0x6998
movl $0xff, %ecx
movl %eax, %edx
shrl $0x8, %edx
movl $0xff, %edi
andl %edi, %edx
imulq %rbx, %rdx
cvtsi2sd %rdx, %xmm0
mulsd 0x293b(%rip), %xmm0     # 0x92f0
cvttsd2si %xmm0, %rdx
cmpq %rsi, %rdx
jl 0x69c4
movl $0xff, %edx
andq %rsi, %rax
imulq %rbx, %rax
cvtsi2sd %rax, %xmm0
mulsd 0x2918(%rip), %xmm0     # 0x92f0
cvttsd2si %xmm0, %rax
cmpq %rsi, %rax
jl 0x69e7
movl $0xff, %eax
movb %al, (%rbp)
movb %cl, 0x2(%rbp)
jmp 0x6a01
xorl %edx, %edx
movb %dl, (%rbp)
movb %dl, 0x2(%rbp)
fldt 0x48(%rsp)
movss 0x58(%rsp), %xmm1
movb %dl, 0x1(%rbp)
cvtsi2ss %rbx, %xmm0
subss %xmm0, %xmm1
fldt 0x30(%r13)
faddp %st, %st(1)
movq 0x40(%r12), %rbp
incq %rbp
jmp 0x6599
fstp %st(0)
andq $0x0, 0x40(%r12)
movb $0x1, %al
addq $0x178, %rsp            # imm = 0x178
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq