//-----------------------------------------------------------------------------
// NastCell2d.cpp
//-----------------------------------------------------------------------------

#include "NastCell2d.h"
#include "NastDebug.h"

//-----------------------------------------------------------------------------
//                    Konstruktor + Destruktor
//-----------------------------------------------------------------------------

//  Defaultconstr.
CNastCell2d::CNastCell2d()
    :m_fluid(0),
     m_type(EMPTY)
{}

//  Zuweisungsoperator
const CNastCell2d&
CNastCell2d::operator=( const CNastCell2d &other )
{
    CNastObject::operator=(other);
    m_type  = other.m_type; 
    m_fluid = other.m_fluid;
    return *this;
}

// neuen Zelltyp setzen
// Falls es keine Nachbarzellen gibt (Raender des Felder) werden die betroffenen
// Zeiger auf 0 gesetzt
void 
CNastCell2d::type( Type newType,
        	   CNastCell2d *pWest,
        	   CNastCell2d *pEast,
        	   CNastCell2d *pSouth,
        	   CNastCell2d *pNorth )
{
    //  der Typ muss bekannt sein
    NAST_ASSERT( newType == EMPTY ||
        	 newType == FLUID ||
        	 newType == BARRIER );

    if( m_type == newType )	// keine Aenderung ?
        return;

    if( m_type == FLUID || newType == FLUID) // Fluideigenschaft geaendert ?
    {
        m_fluid = 0;			 

        // die Bits in den Nachbarzellen setzen
        if( newType == FLUID )		 // wird die Zelle zur Fluidzelle
        {
            m_fluid |= THIS;		 // die Zelle selbst bekommt Fluideigenschaft

            // in der Zelle die Bits setzen
            if( pWest  && pWest->isFluid() ) m_fluid |= WEST;
            if( pEast  && pEast->isFluid() ) m_fluid |= EAST;
            if( pSouth && pSouth->isFluid()) m_fluid |= SOUTH;
            if( pNorth && pNorth->isFluid()) m_fluid |= NORTH;

            // und danach in den Nachbarzellen
            if( pWest  ) pWest->m_fluid  |= EAST;
            if( pEast  ) pEast->m_fluid  |= WEST;
            if( pNorth ) pNorth->m_fluid |= SOUTH;
            if( pSouth ) pSouth->m_fluid |= NORTH;

        }
        else
        {
            // die Zelle war Fluidzelle, also muessen in den Nachbarzellen
            // die Bits wieder herausgefiltert werden
            if( pWest  ) pWest->m_fluid  &= (THIS |        WEST | NORTH | SOUTH );
            if( pEast  ) pEast->m_fluid  &= (THIS | EAST |        NORTH | SOUTH );
            if( pSouth ) pSouth->m_fluid &= (THIS | EAST | WEST |         SOUTH );
            if( pNorth ) pNorth->m_fluid &= (THIS | EAST | WEST | NORTH         );
        }
    }

    m_type = newType;		// neuen Typ setzen
}

// ----------------------------------------------------------------------------
//                                    Debug
// ----------------------------------------------------------------------------

void 
CNastCell2d::debugDump( CNastDumpContext &dumpContext ) const
{
    CNastObject::debugDump( dumpContext );
    dumpContext << "CNastCell2d : " << "\n";
    if( isFluid() )
        dumpContext << "Fluid \n";
    else if ( isEmpty())
        dumpContext << "Empty \n";
    else if ( isBarrier() )
        dumpContext << "Barrier \n";
}

void 
CNastCell2d::assertValid() const
{
    // zuerst einmal AssertValid der Basisklasse aufrufen
    CNastObject::assertValid(); 

    NAST_ASSERT( isFluid() || isEmpty() || isBarrier());
}


