//----------------------------------------------------------------------------------------
// Iterate
void CCudaFDKAlgorithm3D::run(int _iNrIterations)
{
	// check initialized
	ASTRA_ASSERT(m_bIsInitialized);

	const CProjectionGeometry3D* projgeom = m_pSinogram->getGeometry();
	const CConeProjectionGeometry3D* conegeom = dynamic_cast<const CConeProjectionGeometry3D*>(projgeom);
	const CVolumeGeometry3D& volgeom = *m_pReconstruction->getGeometry();

	ASTRA_ASSERT(conegeom);

	CFloat32ProjectionData3DMemory* pSinoMem = dynamic_cast<CFloat32ProjectionData3DMemory*>(m_pSinogram);
	ASTRA_ASSERT(pSinoMem);
	CFloat32VolumeData3DMemory* pReconMem = dynamic_cast<CFloat32VolumeData3DMemory*>(m_pReconstruction);
	ASTRA_ASSERT(pReconMem);


	bool ok = true;

	ok = astraCudaFDK(pReconMem->getData(), pSinoMem->getDataConst(),
	                  &volgeom, conegeom,
	                  m_bShortScan, m_iGPUIndex, m_iVoxelSuperSampling);

	ASTRA_ASSERT(ok);

}
//----------------------------------------------------------------------------------------
// Iterate
void CCudaBackProjectionAlgorithm3D::run(int _iNrIterations)
{
	// check initialized
	ASTRA_ASSERT(m_bIsInitialized);

	CFloat32ProjectionData3DMemory* pSinoMem = dynamic_cast<CFloat32ProjectionData3DMemory*>(m_pSinogram);
	ASTRA_ASSERT(pSinoMem);
	CFloat32VolumeData3DMemory* pReconMem = dynamic_cast<CFloat32VolumeData3DMemory*>(m_pReconstruction);
	ASTRA_ASSERT(pReconMem);

	const CProjectionGeometry3D* projgeom = pSinoMem->getGeometry();
	const CVolumeGeometry3D& volgeom = *pReconMem->getGeometry();

	if (m_bSIRTWeighting) {
		astraCudaBP_SIRTWeighted(pReconMem->getData(),
		                         pSinoMem->getDataConst(),
		                         &volgeom, projgeom,
		                         m_iGPUIndex, m_iVoxelSuperSampling);
	} else {

#if 1
		CCompositeGeometryManager cgm;

		cgm.doBP(m_pProjector, pReconMem, pSinoMem);
#else
		astraCudaBP(pReconMem->getData(), pSinoMem->getDataConst(),
		            &volgeom, projgeom,
		            m_iGPUIndex, m_iVoxelSuperSampling);
#endif
	}

}
//---------------------------------------------------------------------------------------
// Initialize - Config
bool CCudaBackProjectionAlgorithm3D::initialize(const Config& _cfg)
{
	ASTRA_ASSERT(_cfg.self);
	ConfigStackCheck<CAlgorithm> CC("CudaBackProjectionAlgorithm3D", this, _cfg);	

	// if already initialized, clear first
	if (m_bIsInitialized) {
		clear();
	}

	// initialization of parent class
	if (!CReconstructionAlgorithm3D::initialize(_cfg)) {
		return false;
	}

	CCudaProjector3D* pCudaProjector = 0;
	pCudaProjector = dynamic_cast<CCudaProjector3D*>(m_pProjector);
	if (!pCudaProjector) {
		// TODO: Report
	}

	m_iGPUIndex = (int)_cfg.self.getOptionNumerical("GPUindex", -1);
	CC.markOptionParsed("GPUindex");


	m_iVoxelSuperSampling = 1;
	if (pCudaProjector)
		m_iVoxelSuperSampling = pCudaProjector->getVoxelSuperSampling();
	m_iVoxelSuperSampling = (int)_cfg.self.getOptionNumerical("VoxelSuperSampling", m_iVoxelSuperSampling);
	CC.markOptionParsed("VoxelSuperSampling");

	CFloat32ProjectionData3DMemory* pSinoMem = dynamic_cast<CFloat32ProjectionData3DMemory*>(m_pSinogram);
	ASTRA_ASSERT(pSinoMem);
	const CProjectionGeometry3D* projgeom = pSinoMem->getGeometry();
const CParallelProjectionGeometry3D* par3dgeom = dynamic_cast<const CParallelProjectionGeometry3D*>(projgeom);
	const CParallelVecProjectionGeometry3D* parvec3dgeom = dynamic_cast<const CParallelVecProjectionGeometry3D*>(projgeom);
	if (parvec3dgeom || par3dgeom) {
		// This option is only supported for Par3D currently
		m_bSIRTWeighting = _cfg.self.getOptionBool("SIRTWeighting", false);
		CC.markOptionParsed("SIRTWeighting");
	}

	// success
	m_bIsInitialized = _check();
	return m_bIsInitialized;
}
//----------------------------------------------------------------------------------------
// Iterate
void CCudaSirtAlgorithm3D::run(int _iNrIterations)
{
	// check initialized
	ASTRA_ASSERT(m_bIsInitialized);

	const CProjectionGeometry3D* projgeom = m_pSinogram->getGeometry();
	const CVolumeGeometry3D& volgeom = *m_pReconstruction->getGeometry();

	bool ok = true;

	if (!m_bAstraSIRTInit) {

		ok &= m_pSirt->setGPUIndex(m_iGPUIndex);

		ok &= m_pSirt->setGeometry(&volgeom, projgeom);

		ok &= m_pSirt->enableSuperSampling(m_iVoxelSuperSampling, m_iDetectorSuperSampling);

		if (m_bUseReconstructionMask)
			ok &= m_pSirt->enableVolumeMask();
		if (m_bUseSinogramMask)
			ok &= m_pSirt->enableSinogramMask();

		ASTRA_ASSERT(ok);

		ok &= m_pSirt->init();

		ASTRA_ASSERT(ok);

		m_bAstraSIRTInit = true;

	}

	CFloat32ProjectionData3DMemory* pSinoMem = dynamic_cast<CFloat32ProjectionData3DMemory*>(m_pSinogram);
	ASTRA_ASSERT(pSinoMem);

	ok = m_pSirt->setSinogram(pSinoMem->getDataConst(), m_pSinogram->getGeometry()->getDetectorColCount());

	ASTRA_ASSERT(ok);

	if (m_bUseReconstructionMask) {
		CFloat32VolumeData3DMemory* pRMaskMem = dynamic_cast<CFloat32VolumeData3DMemory*>(m_pReconstructionMask);
		ASTRA_ASSERT(pRMaskMem);
		ok &= m_pSirt->setVolumeMask(pRMaskMem->getDataConst(), volgeom.getGridColCount());
	}
	if (m_bUseSinogramMask) {
		CFloat32ProjectionData3DMemory* pSMaskMem = dynamic_cast<CFloat32ProjectionData3DMemory*>(m_pSinogramMask);
		ASTRA_ASSERT(pSMaskMem);
		ok &= m_pSirt->setSinogramMask(pSMaskMem->getDataConst(), m_pSinogramMask->getGeometry()->getDetectorColCount());
	}

	CFloat32VolumeData3DMemory* pReconMem = dynamic_cast<CFloat32VolumeData3DMemory*>(m_pReconstruction);
	ASTRA_ASSERT(pReconMem);
	ok &= m_pSirt->setStartReconstruction(pReconMem->getDataConst(),
	                                      volgeom.getGridColCount());

	ASTRA_ASSERT(ok);

	if (m_bUseMinConstraint)
		ok &= m_pSirt->setMinConstraint(m_fMinValue);
	if (m_bUseMaxConstraint)
		ok &= m_pSirt->setMaxConstraint(m_fMaxValue);

	ok &= m_pSirt->iterate(_iNrIterations);
	ASTRA_ASSERT(ok);

	ok &= m_pSirt->getReconstruction(pReconMem->getData(),
	                                 volgeom.getGridColCount());
	ASTRA_ASSERT(ok);


}
//----------------------------------------------------------------------------------------
// Iterate
void CCudaBackProjectionAlgorithm3D::run(int _iNrIterations)
{
	// check initialized
	ASTRA_ASSERT(m_bIsInitialized);

	CFloat32ProjectionData3DMemory* pSinoMem = dynamic_cast<CFloat32ProjectionData3DMemory*>(m_pSinogram);
	ASTRA_ASSERT(pSinoMem);
	CFloat32VolumeData3DMemory* pReconMem = dynamic_cast<CFloat32VolumeData3DMemory*>(m_pReconstruction);
	ASTRA_ASSERT(pReconMem);

	const CProjectionGeometry3D* projgeom = pSinoMem->getGeometry();
	const CConeProjectionGeometry3D* conegeom = dynamic_cast<const CConeProjectionGeometry3D*>(projgeom);
	const CParallelProjectionGeometry3D* par3dgeom = dynamic_cast<const CParallelProjectionGeometry3D*>(projgeom);
	const CConeVecProjectionGeometry3D* conevecgeom = dynamic_cast<const CConeVecProjectionGeometry3D*>(projgeom);
	const CParallelVecProjectionGeometry3D* parvec3dgeom = dynamic_cast<const CParallelVecProjectionGeometry3D*>(projgeom);
	const CVolumeGeometry3D& volgeom = *pReconMem->getGeometry();

	if (conegeom) {
		astraCudaConeBP(pReconMem->getData(), pSinoMem->getDataConst(),
		                volgeom.getGridColCount(),
		                volgeom.getGridRowCount(),
		                volgeom.getGridSliceCount(),
		                conegeom->getProjectionCount(),
		                conegeom->getDetectorColCount(),
		                conegeom->getDetectorRowCount(),
		                conegeom->getOriginSourceDistance(),
		                conegeom->getOriginDetectorDistance(),
		                conegeom->getDetectorSpacingX(),
		                conegeom->getDetectorSpacingY(),
		                conegeom->getProjectionAngles(),
		                m_iGPUIndex, m_iVoxelSuperSampling);
	} else if (par3dgeom) {
		if (!m_bSIRTWeighting) {
			astraCudaPar3DBP(pReconMem->getData(), pSinoMem->getDataConst(),
			                 volgeom.getGridColCount(),
			                 volgeom.getGridRowCount(),
			                 volgeom.getGridSliceCount(),
			                 par3dgeom->getProjectionCount(),
			                 par3dgeom->getDetectorColCount(),
			                 par3dgeom->getDetectorRowCount(),
			                 par3dgeom->getDetectorSpacingX(),
			                 par3dgeom->getDetectorSpacingY(),
			                 par3dgeom->getProjectionAngles(),
			                 m_iGPUIndex, m_iVoxelSuperSampling);
		} else {
			astraCudaPar3DBP_SIRTWeighted(pReconMem->getData(),
			                 pSinoMem->getDataConst(),
			                 volgeom.getGridColCount(),
			                 volgeom.getGridRowCount(),
			                 volgeom.getGridSliceCount(),
			                 par3dgeom->getProjectionCount(),
			                 par3dgeom->getDetectorColCount(),
			                 par3dgeom->getDetectorRowCount(),
			                 par3dgeom->getDetectorSpacingX(),
			                 par3dgeom->getDetectorSpacingY(),
			                 par3dgeom->getProjectionAngles(),
			                 m_iGPUIndex, m_iVoxelSuperSampling);
		}
	} else if (parvec3dgeom) {
		if (!m_bSIRTWeighting) {
			astraCudaPar3DBP(pReconMem->getData(), pSinoMem->getDataConst(),
			                 volgeom.getGridColCount(),
			                 volgeom.getGridRowCount(),
			                 volgeom.getGridSliceCount(),
			                 parvec3dgeom->getProjectionCount(),
			                 parvec3dgeom->getDetectorColCount(),
			                 parvec3dgeom->getDetectorRowCount(),
			                 parvec3dgeom->getProjectionVectors(),
			                 m_iGPUIndex, m_iVoxelSuperSampling);
		} else {
			astraCudaPar3DBP_SIRTWeighted(pReconMem->getData(),
			                 pSinoMem->getDataConst(),
			                 volgeom.getGridColCount(),
			                 volgeom.getGridRowCount(),
			                 volgeom.getGridSliceCount(),
			                 parvec3dgeom->getProjectionCount(),
			                 parvec3dgeom->getDetectorColCount(),
			                 parvec3dgeom->getDetectorRowCount(),
			                 parvec3dgeom->getProjectionVectors(),
			                 m_iGPUIndex, m_iVoxelSuperSampling);
		}
	} else if (conevecgeom) {
		astraCudaConeBP(pReconMem->getData(), pSinoMem->getDataConst(),
		                volgeom.getGridColCount(),
		                volgeom.getGridRowCount(),
		                volgeom.getGridSliceCount(),
		                conevecgeom->getProjectionCount(),
		                conevecgeom->getDetectorColCount(),
		                conevecgeom->getDetectorRowCount(),
		                conevecgeom->getProjectionVectors(),
		                m_iGPUIndex, m_iVoxelSuperSampling);
	} else {
		ASTRA_ASSERT(false);
	}

}