#include "sifsolid.h"
#include "slices.h"

CSIFSolid::CSIFSolid()
{
  m_pCListVolumeProps = NULL;
}

CSIFSolid::~CSIFSolid()
{
  delete m_pCListVolumeProps;
}

VOID CSIFSolid::Init()
{
  m_CListShellSets.Init();
}

VOID CSIFSolid::AddShellSet(CSIFShellSet *pShellSet)
{
  m_CListShellSets.InsertLast(pShellSet, 0);
}


VOID CSIFSolid::OutputLayer(FLOAT fSliceZ, INT_32 iPrecisionExp)
{

  CListIter<CSIFShellSet *, UINT_8> ShellSetListIter;
  CSIFShellSet *pCurShellSet; 
  
  StartLayerOutput();
  //to take care of (self)-intersections, put each layer in a union
  StartUnionOutput();  

  ShellSetListIter.Init(&m_CListShellSets);
  pCurShellSet = ShellSetListIter.PeekFirst();

  while (pCurShellSet) {
	pCurShellSet->OutputLSIF(iPrecisionExp);
	pCurShellSet = ShellSetListIter.PeekNext();
  }

  // for ending outer union
  EndConstructOutput();

  EndConstructOutput();
}

BOOL CSIFSolidGeomIter::Valid() 
{
  if (m_pCSIFSolid == NULL) 
	return FALSE;
  else
	return TRUE;
}

VOID CSIFSolidGeomIter::Init(CSIFSolid *pCSIFSolid) 
{
  m_pCSIFSolid = pCSIFSolid;
  m_ShellSetListIter.Init(m_pCSIFSolid->GetListShellSets());
  m_pCurShellSet = m_ShellSetListIter.PeekFirst();
  if (m_pCurShellSet) {
	m_pShellSetGeomIter = m_pCurShellSet->AllocateGeomIter();
	m_pShellSetGeomIter->Init(m_pCurShellSet);
  }
}


 CLEDSGeometry *CSIFSolidGeomIter::PeekNext() 
{
  CLEDSGeometry *pGeom;

  if (m_pCurShellSet == NULL)
	return NULL;

  pGeom = m_pShellSetGeomIter->PeekNext();
  if (pGeom) {
	return pGeom;
  }
  else {
	m_pCurShellSet = m_ShellSetListIter.PeekNext();
	if (m_pCurShellSet) {
	  m_pShellSetGeomIter = m_pCurShellSet->AllocateGeomIter();
	  m_pShellSetGeomIter->Init(m_pCurShellSet);
	}
	return PeekNext();
  }
}