// File: /workspace/github_cpp_makefile_star2_O3/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 $0x228, %rsp            # imm = 0x228
movq %r8, 0xb8(%rsp)
movq %rcx, 0xc8(%rsp)
movq %rdx, 0xc0(%rsp)
movq %rsi, %r13
movq %rdi, %r12
movl $0x0, 0x74(%rsp)
movsd 0x10(%rsi), %xmm0
movsd 0x18(%rsi), %xmm1
mulsd 0x40(%rsi), %xmm1
divsd %xmm1, %xmm0
callq 0x3280
movsd %xmm0, 0x78(%rsp)
movsd 0x10(%r12), %xmm0
movsd %xmm0, 0x40(%rsp)
movsd 0x18(%r12), %xmm0
movsd %xmm0, 0x38(%rsp)
movsd 0x20(%r12), %xmm0
movsd %xmm0, 0x28(%rsp)
callq 0x30e0
xorl %ecx, %ecx
testb $0x1, %al
sete %cl
movl $0x28, %eax
movl $0xa, %edx
cmovel %eax, %edx
movl $0x3, %eax
movl $0xd, %esi
cmovel %eax, %esi
shll $0x4, %ecx
addl $0x1e, %ecx
movl %edx, (%r12)
movl $0x46, 0x4(%r12)
movl %ecx, 0x8(%r12)
movl %esi, 0xc(%r12)
movss 0x30(%r12), %xmm1
mulss 0x34(%r12), %xmm1
movss 0x2cd3(%rip), %xmm0     # 0x9404
divss %xmm1, %xmm0
xorps %xmm1, %xmm1
ucomiss %xmm1, %xmm0
jb 0x6743
sqrtss %xmm0, %xmm0
jmp 0x6748
callq 0x35e0
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, 0xe0(%rsp)
movapd %xmm2, 0x40(%r13)
movsd 0x10(%r13), %xmm1
divsd %xmm0, %xmm1
movsd %xmm1, 0x10(%r13)
movq %r12, %rdi
movq %r13, %rsi
movq 0xc0(%rsp), %rdx
callq 0x703c
xorl %r15d, %r15d
movq %r15, 0x40(%r12)
movq (%r13), %rbp
movl $0x38, %ecx
movq %rbp, %rax
mulq %rcx
movq $-0x1, %rbx
cmovoq %rbx, %rax
movq %rax, %rdi
callq 0x3030
movq 0xc8(%rsp), %rcx
movq %rax, (%rcx)
movslq 0x60(%r12), %rcx
movq %rbp, %rax
cqto
idivq %rcx
movl $0x28, %ecx
movq %rax, 0x128(%rsp)
mulq %rcx
movq %rax, %rbp
cmovnoq %rax, %rbx
movq %rbx, %rdi
callq 0x3030
movq 0xb8(%rsp), %rcx
movq %rax, (%rcx)
movq %rax, %rdi
xorl %esi, %esi
movq %rbp, %rdx
callq 0x3170
movq %r15, 0x40(%r12)
movsd 0x18(%r13), %xmm0
xorpd %xmm1, %xmm1
ucomisd %xmm1, %xmm0
fldz
jbe 0x701c
fstp %st(0)
movsd 0x38(%rsp), %xmm3
movapd %xmm3, %xmm0
mulsd %xmm3, %xmm0
movsd 0x40(%rsp), %xmm4
movapd %xmm4, %xmm1
mulsd %xmm4, %xmm1
addsd %xmm0, %xmm1
movsd 0x28(%rsp), %xmm2
movapd %xmm2, %xmm0
mulsd %xmm2, %xmm0
addsd %xmm1, %xmm0
sqrtsd %xmm0, %xmm0
divsd %xmm0, %xmm2
divsd %xmm0, %xmm3
divsd %xmm0, %xmm4
movaps 0xe0(%rsp), %xmm0
movlps %xmm0, 0x1d8(%rsp)
fldl 0x1d8(%rsp)
fstpt 0x98(%rsp)
xorps %xmm1, %xmm1
cvtsd2ss %xmm4, %xmm1
movss %xmm1, 0x94(%rsp)
xorps %xmm1, %xmm1
cvtsd2ss %xmm3, %xmm1
movss %xmm1, 0x90(%rsp)
xorps %xmm1, %xmm1
cvtsd2ss %xmm2, %xmm1
movss %xmm1, 0x8c(%rsp)
movaps %xmm0, %xmm1
movlhps %xmm0, %xmm1            # xmm1 = xmm1[0],xmm0[0]
movaps %xmm1, 0x1e0(%rsp)
movhlps %xmm0, %xmm0            # xmm0 = xmm0[1,1]
movaps %xmm0, 0x100(%rsp)
fldz
xorl %ebp, %ebp
xorps %xmm1, %xmm1
xorl %eax, %eax
cmpq (%r13), %rax
jge 0x701c
fstpt 0x40(%rsp)
movss %xmm1, 0x38(%rsp)
movslq 0x60(%r12), %rcx
cqto
idivq %rcx
movl %eax, %esi
imull %ecx, %esi
movl %esi, 0xa4(%rsp)
fldt 0x30(%r13)
fld %st(0)
fimull 0xa4(%rsp)
fld %st(0)
fstpt 0x28(%rsp)
movq %rdx, 0x1c8(%rsp)
fildll 0x1c8(%rsp)
fldt 0x50(%r13)
fmulp %st, %st(3)
fmulp %st, %st(2)
fxch %st(1)
fstpt 0x50(%rsp)
imulq %rcx, %rax
movq %rax, 0x1d0(%rsp)
fildll 0x1d0(%rsp)
fldt 0x20(%r13)
fmulp %st, %st(1)
fstpl 0x1c0(%rsp)
fstpt (%rsp)
callq 0x3390
fldt 0x98(%rsp)
fmulp %st, %st(1)
fstpl 0x1b8(%rsp)
fldt 0x28(%rsp)
fstpt (%rsp)
callq 0x3500
fldt 0x98(%rsp)
fmulp %st, %st(1)
fstpl 0x1b0(%rsp)
fldt 0x50(%rsp)
fstpt (%rsp)
callq 0x3390
fstpl 0x1a8(%rsp)
movsd 0x1a8(%rsp), %xmm0
movsd %xmm0, 0x60(%rsp)
fldt 0x50(%rsp)
fstpt (%rsp)
callq 0x3500
fstpl 0x198(%rsp)
movsd 0x198(%rsp), %xmm0
movaps %xmm0, 0x200(%rsp)
xorps 0x2b88(%rip), %xmm0     # 0x9560
movaps %xmm0, 0x50(%rsp)
movsd 0x78(%rsp), %xmm0
callq 0x3080
mulsd 0x50(%rsp), %xmm0
movsd %xmm0, 0xf0(%rsp)
movsd 0x78(%rsp), %xmm0
callq 0x3600
movaps %xmm0, 0x210(%rsp)
movsd 0x60(%rsp), %xmm0
movsd %xmm0, 0x1a0(%rsp)
fldl 0x1a0(%rsp)
fstpt 0x50(%rsp)
fldt 0x28(%rsp)
fstpt (%rsp)
callq 0x3390
fstpt 0x80(%rsp)
movsd 0xf0(%rsp), %xmm0
movsd %xmm0, 0x190(%rsp)
fldl 0x190(%rsp)
fstpt 0x60(%rsp)
fldt 0x28(%rsp)
fstpt (%rsp)
callq 0x3500
fldt 0x60(%rsp)
fmulp %st, %st(1)
fldt 0x50(%rsp)
fldt 0x80(%rsp)
fmulp %st, %st(1)
fsubp %st, %st(1)
fstpl 0x188(%rsp)
movsd 0x188(%rsp), %xmm0
movapd %xmm0, 0xf0(%rsp)
fldt 0x28(%rsp)
fstpt (%rsp)
callq 0x3500
fstpt 0x80(%rsp)
fldt 0x28(%rsp)
fstpt (%rsp)
callq 0x3390
fldt 0x60(%rsp)
fmulp %st, %st(1)
fldt 0x50(%rsp)
fldt 0x80(%rsp)
fmulp %st, %st(1)
faddp %st, %st(1)
fstpl 0x180(%rsp)
movsd 0x180(%rsp), %xmm5
movsd 0x1b8(%rsp), %xmm6
movhpd 0x1c0(%rsp), %xmm6      # xmm6 = xmm6[0],mem[0]
movapd %xmm5, %xmm4
mulsd 0x100(%rsp), %xmm4
addsd 0x1b0(%rsp), %xmm4
movq 0x40(%r12), %rax
imulq $0x38, %rax, %rbx
movq 0xc8(%rsp), %rcx
addq (%rcx), %rbx
movslq 0x60(%r12), %rcx
cqto
idivq %rcx
testq %rdx, %rdx
je 0x6b2c
movss 0x38(%rsp), %xmm1
fldt 0x40(%rsp)
jmp 0x6de5
movslq %r15d, %r14
cmpq %r14, 0x128(%rsp)
movss 0x38(%rsp), %xmm1
fldt 0x40(%rsp)
jle 0x6de5
fstp %st(0)
movq 0xb8(%rsp), %rdx
movq (%rdx), %rdx
movq %rdx, 0x118(%rsp)
leaq (%r14,%r14,4), %r15
xorps %xmm0, %xmm0
cvtsi2ss %rax, %xmm0
addss 0x27a0(%rip), %xmm0     # 0x9310
cvtss2sd %xmm0, %xmm0
xorps %xmm1, %xmm1
cvtsi2sd %ecx, %xmm1
mulsd %xmm0, %xmm1
addsd 0x26d1(%rip), %xmm1     # 0x9258
movsd %xmm1, 0x178(%rsp)
fldl 0x178(%rsp)
fldt 0x30(%r13)
fmul %st(1), %st
fld %st(0)
fstpt 0x28(%rsp)
fldt 0x20(%r13)
fmulp %st, %st(2)
fxch %st(1)
fstpl 0x170(%rsp)
fstpt (%rsp)
movsd %xmm5, 0x80(%rsp)
movsd %xmm4, 0x120(%rsp)
movapd %xmm6, 0x1f0(%rsp)
callq 0x3390
fldt 0x98(%rsp)
fmulp %st, %st(1)
fstpl 0x168(%rsp)
fldt 0x28(%rsp)
fstpt (%rsp)
callq 0x3500
fldt 0x98(%rsp)
fmulp %st, %st(1)
fstpl 0x160(%rsp)
fldz
fstpt (%rsp)
callq 0x3390
fstpl 0x158(%rsp)
movsd 0x158(%rsp), %xmm0
movsd %xmm0, 0x50(%rsp)
fldz
fstpt (%rsp)
callq 0x3500
fstpl 0x148(%rsp)
movsd 0x148(%rsp), %xmm0
movaps %xmm0, 0xd0(%rsp)
xorps 0x2916(%rip), %xmm0     # 0x9560
movaps %xmm0, 0x60(%rsp)
movsd 0x78(%rsp), %xmm0
callq 0x3080
mulsd 0x60(%rsp), %xmm0
movsd %xmm0, 0x60(%rsp)
movsd 0x78(%rsp), %xmm0
callq 0x3600
mulsd 0xd0(%rsp), %xmm0
movapd %xmm0, 0xd0(%rsp)
movsd 0x50(%rsp), %xmm0
movsd %xmm0, 0x150(%rsp)
fldl 0x150(%rsp)
fstpt 0x50(%rsp)
fldt 0x28(%rsp)
fstpt (%rsp)
callq 0x3390
fstpt 0xac(%rsp)
movsd 0x60(%rsp), %xmm0
movsd %xmm0, 0x140(%rsp)
fldl 0x140(%rsp)
fstpt 0x60(%rsp)
fldt 0x28(%rsp)
fstpt (%rsp)
callq 0x3500
fldt 0x60(%rsp)
fmulp %st, %st(1)
fldt 0x50(%rsp)
fldt 0xac(%rsp)
fmulp %st, %st(1)
fsubp %st, %st(1)
fstpl 0x138(%rsp)
fldt 0x28(%rsp)
fstpt (%rsp)
callq 0x3500
fstpt 0xac(%rsp)
fldt 0x28(%rsp)
fstpt (%rsp)
callq 0x3390
movapd 0x1f0(%rsp), %xmm6
movsd 0x80(%rsp), %xmm5
fldt 0x40(%rsp)
fldt 0x60(%rsp)
fmulp %st, %st(2)
fldt 0x50(%rsp)
fldt 0xac(%rsp)
fmulp %st, %st(1)
faddp %st, %st(2)
fxch %st(1)
fstpl 0x130(%rsp)
movsd 0x130(%rsp), %xmm0
movapd 0x100(%rsp), %xmm4
mulsd %xmm4, %xmm0
movsd 0x160(%rsp), %xmm1
addsd %xmm0, %xmm1
movsd 0x138(%rsp), %xmm2
unpcklpd 0xd0(%rsp), %xmm2       # xmm2 = xmm2[0],mem[0]
movsd 0x168(%rsp), %xmm3
movhpd 0x170(%rsp), %xmm3      # xmm3 = xmm3[0],mem[0]
mulpd %xmm4, %xmm2
movsd 0x120(%rsp), %xmm4
addpd %xmm2, %xmm3
subpd %xmm2, %xmm3
divpd 0x1e0(%rsp), %xmm3
movq 0x118(%rsp), %rax
movupd %xmm3, (%rax,%r15,8)
subsd %xmm0, %xmm1
divsd 0xe0(%rsp), %xmm1
movsd %xmm1, 0x10(%rax,%r15,8)
movq %rax, %rcx
movss 0x38(%rsp), %xmm1
movl 0x40(%r12), %eax
movl %eax, 0x20(%rcx,%r15,8)
incl %r14d
movl %r14d, %r15d
movapd 0x210(%rsp), %xmm2
mulsd 0x200(%rsp), %xmm2
movapd 0xf0(%rsp), %xmm3
movapd %xmm3, %xmm0
unpcklpd %xmm2, %xmm0            # xmm0 = xmm0[0],xmm2[0]
mulpd 0x100(%rsp), %xmm0
addpd %xmm6, %xmm0
divpd 0x1e0(%rsp), %xmm0
movupd %xmm0, 0x8(%rbx)
divsd 0xe0(%rsp), %xmm4
movsd %xmm4, 0x18(%rbx)
movsd %xmm3, 0x20(%rbx)
movsd %xmm2, 0x28(%rbx)
movsd %xmm5, 0x30(%rbx)
cmpl $0x1, 0x64(%r12)
movss 0x25ba(%rip), %xmm0     # 0x9408
jne 0x6e9c
fstp %st(0)
cvtsd2ss %xmm3, %xmm3
xorps %xmm4, %xmm4
cvtsd2ss %xmm2, %xmm4
cvtsd2ss %xmm5, %xmm5
movl $0x3f800000, (%rsp)     # imm = 0x3F800000
xorpd %xmm6, %xmm6
xorps %xmm7, %xmm7
movq %r12, %rdi
movss 0x94(%rsp), %xmm0
movss 0x90(%rsp), %xmm1
movss 0x8c(%rsp), %xmm2
callq 0x7b2a
fldt 0x40(%rsp)
movss 0x38(%rsp), %xmm1
addss %xmm0, %xmm1
ucomiss 0x2565(%rip), %xmm1     # 0x940c
jbe 0x6ff1
xorps %xmm0, %xmm0
cvtss2sd %xmm1, %xmm0
addsd 0x239c(%rip), %xmm0     # 0x9258
cvttsd2si %xmm0, %ebp
movl $0x96, %eax
cmpl %eax, %ebp
movq 0xc0(%rsp), %rsi
jl 0x6ed6
movl $0x96, %ebp
movss %xmm1, 0x38(%rsp)
cmpl $0x0, 0xb8(%rsi)
fldt 0x50(%r13)
fld %st(1)
fmuls 0x5669(%rip)            # 0xc558
fxch %st(1)
fmul %st(2), %st
faddp %st, %st(1)
je 0x6f18
fstpt 0x10(%rsp)
fstpt (%rsp)
movq %r12, %rdi
movq %r13, %rdx
leaq 0xa8(%rsp), %rcx
leaq 0x74(%rsp), %r8
callq 0x73a0
jmp 0x6f37
fstpt 0x10(%rsp)
fstpt (%rsp)
movq %r12, %rdi
movq %r13, %rdx
leaq 0xa8(%rsp), %rcx
leaq 0x74(%rsp), %r8
callq 0x7278
movslq %ebp, %rbp
testq %rax, %rax
je 0x6fdd
movl %eax, %ecx
shrl $0x10, %ecx
movl $0xff, %edx
andl %edx, %ecx
imulq %rbp, %rcx
xorps %xmm0, %xmm0
cvtsi2sd %rcx, %xmm0
mulsd 0x238d(%rip), %xmm0     # 0x92f0
cvttsd2si %xmm0, %rcx
movl $0xff, %esi
cmpq %rsi, %rcx
movss 0x38(%rsp), %xmm1
fldt 0x40(%rsp)
jl 0x6f81
movl $0xff, %ecx
movl %eax, %edx
shrl $0x8, %edx
movl $0xff, %edi
andl %edi, %edx
imulq %rbp, %rdx
xorps %xmm0, %xmm0
cvtsi2sd %rdx, %xmm0
mulsd 0x234f(%rip), %xmm0     # 0x92f0
cvttsd2si %xmm0, %rdx
cmpq %rsi, %rdx
jl 0x6fb0
movl $0xff, %edx
andq %rsi, %rax
imulq %rbp, %rax
xorps %xmm0, %xmm0
cvtsi2sd %rax, %xmm0
mulsd 0x2329(%rip), %xmm0     # 0x92f0
cvttsd2si %xmm0, %rax
cmpq %rsi, %rax
jl 0x6fd6
movl $0xff, %eax
movb %al, (%rbx)
movb %cl, 0x2(%rbx)
jmp 0x6fee
xorl %edx, %edx
movb %dl, (%rbx)
movb %dl, 0x2(%rbx)
movss 0x38(%rsp), %xmm1
fldt 0x40(%rsp)
movb %dl, 0x1(%rbx)
xorps %xmm0, %xmm0
cvtsi2ss %rbp, %xmm0
subss %xmm0, %xmm1
fldt 0x30(%r13)
faddp %st, %st(1)
movq 0x40(%r12), %rax
incq %rax
movq %rax, 0x40(%r12)
fldl 0x18(%r13)
fucompi %st(1), %st
ja 0x68dd
fstp %st(0)
movq $0x0, 0x40(%r12)
movb $0x1, %al
addq $0x228, %rsp            # imm = 0x228
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
nop