// File: /workspace/github_cpp_makefile_star2_O3/rurban[P]dgnlib/dgnwrite.cpp
// Function: DGNCreateConeElem
DGNElemCore *
DGNCreateConeElem( DGNHandle hDGN,
                   double dfCenter_1X, double dfCenter_1Y,
                   double dfCenter_1Z, double dfRadius_1,
                   double dfCenter_2X, double dfCenter_2Y,
                   double dfCenter_2Z, double dfRadius_2,
                   int *panQuaternion )
{
    DGNElemCone *psCone;
    DGNElemCore *psCore;
    DGNInfo *psDGN = (DGNInfo *) hDGN;
    DGNPoint sMin, sMax, sCenter_1, sCenter_2;
    double dfScaledRadius;

    DGNLoadTCB( hDGN );

/* -------------------------------------------------------------------- */
/*      Allocate element.                                               */
/* -------------------------------------------------------------------- */
    psCone = (DGNElemCone *) CPLCalloc( sizeof(DGNElemCone), 1 );
    psCore = &(psCone->core);

    DGNInitializeElemCore( hDGN, psCore );
    psCore->stype = DGNST_CONE;
    psCore->type = DGNT_CONE;

/* -------------------------------------------------------------------- */
/*      Set cone specific information in the structure.                 */
/* -------------------------------------------------------------------- */
    sCenter_1.x = dfCenter_1X;
    sCenter_1.y = dfCenter_1Y;
    sCenter_1.z = dfCenter_1Z;
    sCenter_2.x = dfCenter_2X;
    sCenter_2.y = dfCenter_2Y;
    sCenter_2.z = dfCenter_2Z;
    psCone->center_1 = sCenter_1;
    psCone->center_2 = sCenter_2;
    psCone->radius_1 = dfRadius_1;
    psCone->radius_2 = dfRadius_2;

    memset( psCone->quat, 0, sizeof(int) * 4 );
    if( panQuaternion != NULL )
    {
        memcpy( psCone->quat, panQuaternion, sizeof(long)*4 );
    }
    else {
      psCone->quat[0] = 1 << 31;
      psCone->quat[1] = 0;
      psCone->quat[2] = 0;
      psCone->quat[3] = 0;
    }

/* -------------------------------------------------------------------- */
/*      Setup Raw data for the cone.                                    */
/* -------------------------------------------------------------------- */
    psCore->raw_bytes = 118;
    psCore->raw_data = (unsigned char*) CPLCalloc(psCore->raw_bytes,1);

    /* unknown data */
    psCore->raw_data[36] = 0;
    psCore->raw_data[37] = 0;
    
    /* quaternion */
    DGN_WRITE_INT32( psCone->quat[0], psCore->raw_data + 38 );
    DGN_WRITE_INT32( psCone->quat[1], psCore->raw_data + 42 );
    DGN_WRITE_INT32( psCone->quat[2], psCore->raw_data + 46 );
    DGN_WRITE_INT32( psCone->quat[3], psCore->raw_data + 50 );

    /* center_1 */
    DGNInverseTransformPoint( psDGN, &sCenter_1 );
    memcpy( psCore->raw_data + 54, &sCenter_1.x, 8 );
    memcpy( psCore->raw_data + 62, &sCenter_1.y, 8 );
    memcpy( psCore->raw_data + 70, &sCenter_1.z, 8 );
    IEEE2DGNDouble( psCore->raw_data + 54 );
    IEEE2DGNDouble( psCore->raw_data + 62 );
    IEEE2DGNDouble( psCore->raw_data + 70 );

    /* radius_1 */
    dfScaledRadius = psCone->radius_1 / psDGN->scale;
    memcpy( psCore->raw_data + 78, &dfScaledRadius, 8 );
    IEEE2DGNDouble( psCore->raw_data + 78 );

    /* center_2 */
    DGNInverseTransformPoint( psDGN, &sCenter_2 );
    memcpy( psCore->raw_data + 86, &sCenter_2.x, 8 );
    memcpy( psCore->raw_data + 94, &sCenter_2.y, 8 );
    memcpy( psCore->raw_data + 102, &sCenter_2.z, 8 );
    IEEE2DGNDouble( psCore->raw_data + 86 );
    IEEE2DGNDouble( psCore->raw_data + 94 );
    IEEE2DGNDouble( psCore->raw_data + 102 );

    /* radius_2 */
    dfScaledRadius = psCone->radius_2 / psDGN->scale;
    memcpy( psCore->raw_data + 110, &dfScaledRadius, 8 );
    IEEE2DGNDouble( psCore->raw_data + 110 );
    
/* -------------------------------------------------------------------- */
/*      Set the core raw data, including the bounds.                    */
/* -------------------------------------------------------------------- */
    DGNUpdateElemCoreExtended( hDGN, psCore );

    //FIXME: Calculate bounds. Do we need to take the quaternion into account?
    // kintel 20030819

    // Old implementation attempt:
    // What if center_1.z > center_2.z ?
//     double largestRadius = 
//       psCone->radius_1>psCone->radius_2?psCone->radius_1:psCone->radius_2;
//     sMin.x = psCone->center_1.x-largestRadius;
//     sMin.y = psCone->center_1.y-largestRadius;
//     sMin.z = psCone->center_1.z;
//     sMax.x = psCone->center_2.x+largestRadius;
//     sMax.y = psCone->center_2.y+largestRadius;
//     sMax.z = psCone->center_2.z;

    DGNWriteBounds( psDGN, psCore, &sMin, &sMax );
    
    return psCore;
}

// Assembly:
pushq %r15
pushq %r14
pushq %rbx
subq $0xa0, %rsp
movq %rsi, %r15
movsd %xmm7, 0x38(%rsp)
movsd %xmm6, 0x28(%rsp)
movsd %xmm5, 0x20(%rsp)
movsd %xmm4, 0x18(%rsp)
movsd %xmm3, 0x30(%rsp)
movsd %xmm2, 0x10(%rsp)
movsd %xmm1, 0x8(%rsp)
movsd %xmm0, (%rsp)
movq %rdi, %rbx
callq 0xae76
movl $0xa8, %edi
movl $0x1, %esi
callq 0x2d20
movq %rax, %r14
xorps %xmm0, %xmm0
movups %xmm0, (%rax)
movups %xmm0, 0x10(%rax)
movups %xmm0, 0x20(%rax)
movups %xmm0, 0x30(%rax)
movups %xmm0, 0x40(%rax)
movl $0xffffffff, (%rax)     # imm = 0xFFFFFFFF
movabsq $0xcffffffff, %rax      # imm = 0xCFFFFFFFF
movq %rax, 0x8(%r14)
movl $0x17, 0x14(%r14)
movsd (%rsp), %xmm1
movsd %xmm1, 0x58(%rsp)
movsd 0x8(%rsp), %xmm1
movsd %xmm1, 0x60(%rsp)
movsd 0x10(%rsp), %xmm1
movsd %xmm1, 0x68(%rsp)
movsd 0x18(%rsp), %xmm1
movsd %xmm1, 0x40(%rsp)
movsd 0x20(%rsp), %xmm1
movsd %xmm1, 0x48(%rsp)
movsd 0x28(%rsp), %xmm1
movsd %xmm1, 0x50(%rsp)
movq 0x68(%rsp), %rax
movq %rax, 0x78(%r14)
movups 0x58(%rsp), %xmm1
movups %xmm1, 0x68(%r14)
movq 0x50(%rsp), %rax
movq %rax, 0x98(%r14)
movups 0x40(%rsp), %xmm1
movups %xmm1, 0x88(%r14)
movsd 0x30(%rsp), %xmm1
movsd %xmm1, 0x80(%r14)
movsd 0x38(%rsp), %xmm1
movsd %xmm1, 0xa0(%r14)
movups %xmm0, 0x54(%r14)
movq %r14, %rax
addq $0x54, %rax
testq %r15, %r15
je 0xca9e
movups (%r15), %xmm0
movups 0x10(%r15), %xmm1
movups %xmm1, 0x10(%rax)
jmp 0xcaa6
movss 0x3f7a(%rip), %xmm0     # 0x10a20
movups %xmm0, (%rax)
movl $0x76, 0x40(%r14)
movl $0x76, %edi
movl $0x1, %esi
callq 0x2d20
movq %rax, 0x48(%r14)
xorl %ecx, %ecx
movb %cl, 0x24(%rax)
movq 0x48(%r14), %rax
movb %cl, 0x25(%rax)
movl 0x54(%r14), %eax
movl %eax, %ecx
shrl $0x10, %ecx
movq 0x48(%r14), %rdx
movb %cl, 0x26(%rdx)
movl %eax, %ecx
shrl $0x18, %ecx
movq 0x48(%r14), %rdx
movb %cl, 0x27(%rdx)
movq 0x48(%r14), %rcx
movb %al, 0x28(%rcx)
movq 0x48(%r14), %rcx
movb %ah, 0x29(%rcx)
movl 0x58(%r14), %eax
movl %eax, %ecx
shrl $0x10, %ecx
movq 0x48(%r14), %rdx
movb %cl, 0x2a(%rdx)
movl %eax, %ecx
shrl $0x18, %ecx
movq 0x48(%r14), %rdx
movb %cl, 0x2b(%rdx)
movq 0x48(%r14), %rcx
movb %al, 0x2c(%rcx)
movq 0x48(%r14), %rcx
movb %ah, 0x2d(%rcx)
movl 0x5c(%r14), %eax
movl %eax, %ecx
shrl $0x10, %ecx
movq 0x48(%r14), %rdx
movb %cl, 0x2e(%rdx)
movl %eax, %ecx
shrl $0x18, %ecx
movq 0x48(%r14), %rdx
movb %cl, 0x2f(%rdx)
movq 0x48(%r14), %rcx
movb %al, 0x30(%rcx)
movq 0x48(%r14), %rcx
movb %ah, 0x31(%rcx)
movl 0x60(%r14), %eax
movl %eax, %ecx
shrl $0x10, %ecx
movq 0x48(%r14), %rdx
movb %cl, 0x32(%rdx)
movl %eax, %ecx
shrl $0x18, %ecx
movq 0x48(%r14), %rdx
movb %cl, 0x33(%rdx)
movq 0x48(%r14), %rcx
movb %al, 0x34(%rcx)
movq 0x48(%r14), %rcx
movb %ah, 0x35(%rcx)
leaq 0x58(%rsp), %r15
movq %rbx, %rdi
movq %r15, %rsi
callq 0xace4
movq 0x48(%r14), %rax
movq (%r15), %rcx
movq %rcx, 0x36(%rax)
movq 0x48(%r14), %rax
movq 0x8(%r15), %rcx
movq %rcx, 0x3e(%rax)
movq 0x48(%r14), %rax
movq 0x10(%r15), %rcx
movq %rcx, 0x46(%rax)
movq 0x48(%r14), %rdi
addq $0x36, %rdi
callq 0x7022
movq 0x48(%r14), %rdi
addq $0x3e, %rdi
callq 0x7022
movq 0x48(%r14), %rdi
addq $0x46, %rdi
callq 0x7022
movsd 0x80(%r14), %xmm0
divsd 0x20020(%rbx), %xmm0
movq 0x48(%r14), %rax
movsd %xmm0, 0x4e(%rax)
movq 0x48(%r14), %rdi
addq $0x4e, %rdi
callq 0x7022
leaq 0x40(%rsp), %r15
movq %rbx, %rdi
movq %r15, %rsi
callq 0xace4
movq 0x48(%r14), %rax
movq (%r15), %rcx
movq %rcx, 0x56(%rax)
movq 0x48(%r14), %rax
movq 0x8(%r15), %rcx
movq %rcx, 0x5e(%rax)
movq 0x48(%r14), %rax
movq 0x10(%r15), %rcx
movq %rcx, 0x66(%rax)
movq 0x48(%r14), %rdi
addq $0x56, %rdi
callq 0x7022
movq 0x48(%r14), %rdi
addq $0x5e, %rdi
callq 0x7022
movq 0x48(%r14), %rdi
addq $0x66, %rdi
callq 0x7022
movsd 0xa0(%r14), %xmm0
divsd 0x20020(%rbx), %xmm0
movq 0x48(%r14), %rax
movsd %xmm0, 0x6e(%rax)
movq 0x48(%r14), %rdi
addq $0x6e, %rdi
callq 0x7022
movq %r14, %rsi
callq 0xbf25
leaq 0x88(%rsp), %rdx
leaq 0x70(%rsp), %rcx
movq %rbx, %rdi
movq %r14, %rsi
callq 0xc286
movq %r14, %rax
addq $0xa0, %rsp
popq %rbx
popq %r14
popq %r15
retq