/*
 $Id: triang1tr.cc,v 1.2 1996/10/04 15:07:52 roitzsch Exp $
 (C)opyright 1996 by Konrad-Zuse-Center, Berlin
 All rights reserved.
 Part of the Kaskade distribution
*/

#include "triang1tr.h"
#include "numerics.h"

//-------------------------------------------------------------------------


const PATCH* MESH1:: findPatch(const Vector<Real>& x, Vector<Real>& xUnit,
			       const PATCH* /*newPatch*/) const
{
    EDG1* ed;
    for (ed=edgList[0]->first; ed; ed=ed->next)
    {
	if (ed->inPatch(x,xUnit)) return ed->findPatch(x,xUnit);
    }
    patchNotFound();
    return 0;
}
//-------------------------------------------------------------------------


const PATCH* EDG1:: findPatch(const Vector<Real>& x, Vector<Real>& xUnit) const
{
    const PATCH* patch;

    if (refined()) 
    {
	if (x[1] <= pm->x) patch = firstSon->findPatch(x,xUnit);
	else  	           patch = firstSon->next->findPatch(x,xUnit);
    }
    else 
    {
	unitCoordinates(x,xUnit);
	patch = this;
    }
    return patch;
}
//-------------------------------------------------------------------------


Bool EDG1:: inPatch(const Vector<Real>& x, Vector<Real>& /*xUnit*/) const
{
    static const Real tiny = 1000.0*machPrec(Real(0.0));

    return ((x[1] >= p1->x - tiny)  &&  (x[1] <= p2->x + tiny));
}
//-------------------------------------------------------------------------
//-------------------------------------------------------------------------


MESH1Trans:: MESH1Trans(const char* inFileName, MESH1* prevMesh0)

	: MESH1(inFileName,False), 
	  prevMesh(prevMesh0), edgTrAlloc(ElementsInBlock)
{
    readTriangulation(fileName);
    if (prevMesh) setInitialPartners(prevMesh);
}
//-------------------------------------------------------------------------


void MESH1Trans:: Refine()
{
    MESH1::Refine();
    if (prevMesh) setPartners();
}
//-------------------------------------------------------------------------


const PATCH* MESH1Trans:: findPatch(const Vector<Real>& x, Vector<Real>& xUnit,
				    const PATCH* newPatch) const
{
    return newPatch->Partner()->findPatch(x,xUnit);
}
