#include <i3d/image3d.h>
#include <i3d/transform.h>
#include <i3d/morphology.h>

#include <stdlib.h> //for rnd generator seeding
#include <time.h>
#include <unistd.h>
#include <string.h>
#include <fstream>
#include <float.h>

#include "params.h"
#include "graphics.h"
#include "TriangleMesh.h"

#define DO_PHASE_II_AND_III
#define DO_ANISOTROPIC_OUTPUT

//create some shared objects...
ParamsClass params;
void ParamsSetup(void);
//
ActiveMesh mesh;

//for filopodia tip tracking:
// GM addresses filopodia branches with pairs: filoTreeID + branchID;
// we linearize them with tipIndex() (e.g., as filoTreeID*100+branchID);
// this is the first index (primary key) in tipOffsets;
// the second index (secondary key) is time point and
// the value is actually a pixel offset (encoding a tip position)
//
//time-course (vector index) of pixel offsets (vector value)
class timeVector: public std::vector<int>
{
  public:
	void Init(const size_t length=200)
	{ this->insert(this->end(),length,-1); }
};
//
//this one holds the entire time-lapse of pixel offsets of all filopodia tips
//use tipIndex() below to address filopodia branches
std::map<int,timeVector> tipOffsets;
//
//this one, similar to the above, holds the entire time-lapse of tip coordinates
class timeVectorC: public std::vector<Vector3F>
{
  public:
	void Init(const size_t length=200)
	{ this->insert(this->end(),length,Vector3F(-1.f)); }
};
std::map<int,timeVectorC> tipCoords;
//
//use exclusively this one to address filopodia branches in tipOffsets above
int tipIndex(const int filoID,const int branchID)
{ return (filoID*100 +branchID); }

//save cell centres
std::vector<Vector3F> centreCoords;
std::vector<int> centreOffsets;

template <class T> void MergeImages(i3d::Image3d<T> *images, size_t num, i3d::Image3d<T> &img);

bool LoadNewMesh(const char* path,
					  const int ID,
					  const int fileNo,
					  const bool keepTrajectories=false);

int  RenderMesh(const char* path,
					 const int ID,
					 int fileNo,
					 i3d::Image3d<i3d::GRAY16> *mask,
					 i3d::Image3d<i3d::GRAY16> *texture);

template <class T> void CompleteSimulation(i3d::Image3d<T> &img);

int main(int argc,char **argv)
{
	if (argc != 5 && argc != 6 && argc != 7)
	{
		//I'm executed with no params, provide help
		std::cout << "ADD SOME HEADER INFORMATION HERE\n\n";
		std::cout << "params:  config_file  path_to_VTKs path_to_output  dataset_ID  [time_point] [no_of_time_points]\n\n";
		std::cout << "The program will try to load object files (cell body volumetric mesh and\n"
                   "filopodia skeleton definition) iteratively (starting from 0th time point)\n"
                   "until it fails loading cell body file. Filopodia files for the current time\n"
                   "point are tried from the index 1. If the optional time_point parameter is\n"
                   "given, it will start loading objects from this time point. If the second optional\n"
						 "parameter is given, maximum of no_of_time_points is processed.\n";
		return(-1);
	}

	//process the config file
	if (mesh.configFile.ParseConfigFile(argv[1]) != 0) return(-2);
	//access the config params as, e.g. :
	// mesh.configFile.texture_Resolution.x

	//determine the first time point to start with
	int i=0;
	if (argc >= 6) i=atoi(argv[5]);

	int framesToGoHome=999999999;
	if (argc == 7) framesToGoHome=atoi(argv[6]);

	//get dataset ID
	int ID=atoi(argv[4]);

	//I'm called as generator/simulator, init the situation...
	ParamsSetup();

	std::cout << "FIRST RUN: reading all meshes to figure out minimal bounding box.\n";
	//doubling iteration variables not to loose their values
	int ii(i);                           //starting index
	int fframesToGoHome(framesToGoHome); //how many indices to read
	Vector3F minBB(FLT_MAX, FLT_MAX, FLT_MAX),
				maxBB(-FLT_MAX, -FLT_MAX, -FLT_MAX); //bounding box corners
	bool readingSucceded = true;

	while ((fframesToGoHome > 0) && (readingSucceded))
	{
		 for (size_t ids=0; 
				(ids<ID) && (readingSucceded = LoadNewMesh(argv[2],ids+1,ii)); 
				ids++)
		{

		 /* jen vypisy
		std::cout << "mesh: #" << ii << "\n";
		std::cout << "vertices   #: " << mesh.Pos.size() << "\n";
		std::cout << "triangles  #: " << mesh.ID.size()/3 << "\n";
		std::cout << "tetrahedra #: " << mesh.VolID.size()/4 << "\n";
		std::cout << "normals    #: " << mesh.norm.size() << "\n";

		//std::cout << "filopodia #: " << mesh.Ftree.size() << "\n";
		for (size_t f=0; f < mesh.Ftree.size(); ++f)
		{
			if (mesh.Ftree[f].Pos.size() > 0)
			{
				std::cout << f << ": vertices  #: " << mesh.Ftree[f].Pos.size() << "\n";
				std::cout << f << ": triangles #: " << mesh.Ftree[f].ID.size()/3 << "\n";
				std::cout << f << ": normals   #: " << mesh.Ftree[f].norm.size() << "\n";
			}
		}
		*/

			mesh.UpdateMeshBBox(minBB,maxBB);

		}
		++ii;
		--fframesToGoHome;
	}

	//ii is the last timePoint -> allocate vector of cell centres large enough
	centreCoords.insert(centreCoords.end(),(unsigned)(ii)+1,Vector3F(-1.f));
	centreOffsets.insert(centreOffsets.end(),(unsigned)(ii)+1,-1);

	//given the discovered bounding box valid for the whole sequence, and given
	//that we DO want to keep mesh coordinates even in the produced images,
	//we have to utilize the images _offset_ attribute, instead of
	//translating all mesh points, to assure the meshes appear within
	//the produced images
	Vector3F borderSize( (maxBB-minBB)/100.f );
	borderSize.x *= mesh.configFile.texture_borderWidth.x;
	borderSize.y *= mesh.configFile.texture_borderWidth.y;
	borderSize.z *= mesh.configFile.texture_borderWidth.z;
	mesh.meshShift=minBB -borderSize;
	mesh.meshPlaygroundSize=(maxBB-minBB) +2.f*borderSize;
	//NB: We also introduce 1um thick border around the mesh
	std::cout << "corners of a detected bounding box:\n";
	std::cout << "[" << minBB.x << "," << minBB.y << "," << minBB.z << "] <-> ["
	          << maxBB.x << "," << maxBB.y << "," << maxBB.z << "] in microns\n";


	std::cout << "SECOND RUN: reading all meshes to conduct the texture simulation and rendering.\n";

	i3d::Image3d<i3d::GRAY16> *mask = new i3d::Image3d<i3d::GRAY16> [ID];
	i3d::Image3d<i3d::GRAY16> *texture = new i3d::Image3d<i3d::GRAY16> [ID];
	i3d::Image3d<i3d::GRAY16> maskUnified, textureUnified;
   char fileName[1024];
	readingSucceded = true;

	while ((framesToGoHome > 0) && (readingSucceded))
	{
		 for (size_t ids=0; 
				(ids<ID) && (readingSucceded = LoadNewMesh(argv[2],ids+1,i,true)); 
				ids++)
		 {
			 // here we render mesh into 'mask' and 'texture' image,
			 // i.e. the phantom should be ready
			 RenderMesh(argv[3], ids, i, mask, texture);
		 }

		 MergeImages(mask, ID, maskUnified);
				 
		 std::cout << "exporting isotropic mask #" << i << "\n";
		 sprintf(fileName,"%s/ISOmask_t%03d.ics",argv[3],i);
		 maskUnified.SaveImage(fileName);

		 MergeImages(texture, ID, textureUnified);

		 std::cout << "exporting phantom #" << i << "\n";
		 sprintf(fileName,"%s/phantom_t%03d.ics",argv[3],i);
		 textureUnified.SaveImage(fileName);

#ifdef DO_PHASE_II_AND_III
		 CompleteSimulation(textureUnified);

		 std::cout << "exporting texture #" << i << "\n";
		 sprintf(fileName,"%s/texture_t%03d.ics",argv[3],i);
		 textureUnified.SaveImage(fileName);
#endif

		 ++i;
		 --framesToGoHome;
	}

	delete [] mask;
	delete [] texture;

	//report the tip trajectories
	sprintf(fileName,"%s/ID%d_afterTM/tip_trajectories_pxOffsets.txt",argv[3],ID);
	std::ofstream oFile(fileName);
	sprintf(fileName,"%s/ID%d_afterTM/tip_trajectories_umCoords.txt",argv[3],ID);
	std::ofstream cFile(fileName);

	std::map<int,timeVector>::const_iterator tipIter=tipOffsets.begin();
	while (tipIter != tipOffsets.end())
	{
		oFile << tipIter->first << ":";
		cFile << tipIter->first << ":";
		for (int t=0; t < i; ++t)
		{
			oFile << tipIter->second.at((unsigned)t) << ",";
			cFile << tipCoords[tipIter->first].at((unsigned)t).x << ","
			      << tipCoords[tipIter->first].at((unsigned)t).y << ","
			      << tipCoords[tipIter->first].at((unsigned)t).z << ";";
		}
		oFile << "\n";
		cFile << "\n";
		++tipIter;
	}
	oFile.close();
	cFile.close();

	//report the cell centre coordinates
	sprintf(fileName,"%s/ID%d_afterTM/centre_trajectories_pxOffsets.txt",argv[3],ID);
	std::ofstream OFile(fileName);
	sprintf(fileName,"%s/ID%d_afterTM/centre_trajectories_umCoords.txt",argv[3],ID);
	std::ofstream CFile(fileName);

	OFile << "1:";
	CFile << "1:";
	for (unsigned int t=0; t < (unsigned)i; ++t)
	{
		//OFile << tipIter->second.at((unsigned)t) << ",";
		OFile << centreOffsets[t] << ",";
		CFile << centreCoords[t].x << ","
				<< centreCoords[t].y << ","
				<< centreCoords[t].z << ";";
	}
	OFile << "\n";
	CFile << "\n";

	OFile.close();
	CFile.close();

	return(0);
}


void ParamsSetup(void)
{
	//set up the environment
   params.sceneOffset=Vector3d<float>(0.f);
   params.sceneSize=Vector3d<float>(27.5f,27.5f,22.0f); //for sample cell

	params.sceneCentre=params.sceneSize;
	params.sceneCentre/=2.0f;
	params.sceneCentre+=params.sceneOffset;

	params.sceneOuterBorder=Vector3d<float>(2.f);
	params.sceneBorderColour.r=0.7f;
	params.sceneBorderColour.g=0.7f;
	params.sceneBorderColour.b=0.0f;

	params.inputCellsFilename="cells/cell%d.txt";
	params.numberOfAgents=2;

	params.friendshipDuration=10.f;
	params.maxCellSpeed=0.20f;

	//adapted for cell cycle length of 24 hours
	params.cellCycleLength=14.5*60; //[min]

	params.currTime=0.0f; //all three are in units of minutes
	params.incrTime=1.0f;
	params.stopTime=50.0f;

	params.imgSizeX=500; //pixels
	params.imgSizeY=500;
	params.imgResX=1.0f; //pixels per micrometer
	params.imgResY=1.0f;

	params.imgOutlineFilename="Outline%05d.tif";
	params.imgPhantomFilename="Phantom%05d.tif";
	params.imgMaskFilename="Mask%05d.tif";
	params.imgFluoFilename="FluoImg%05d.tif";
	params.imgPhCFilename="PhCImg%05d.tif";
	params.tracksFilename="tracks.txt";

	//re-seed the standard rnd generator
	//(which is used for Perlin texture)
	unsigned int seed=(1 << 20) * (unsigned int)time(NULL) * (unsigned int)getpid();
	srand(seed);
}


bool LoadNewMesh(const char* path,
					  const int ID,
					  const int fileNo,
					  const bool keepTrajectories)
{
	char fn[1024];

	//first, obtain a fresh volumetric mesh of the cell body
	sprintf(fn,"%s/C%d_nucleus_T%03d.vtk",path,ID,fileNo);
	std::cout << "reading cell body: " << fn << "\n";

	//keep the volumetric configuration of the first load
	bool saveAlsoTetrahedra=(mesh.GetVolIDsize() == 0);
	int retval=mesh.ImportVTK_Volumetric(fn,saveAlsoTetrahedra);
	if (retval) 
	{
		//report errors except for "cannot open file"
		if (retval > 1) 
			std::cout << "some error reading cell body: " << retval << "\n";
		return(false);
	}

	//if we are now recording the trajectories,
	//record also the cell body centre
	if (keepTrajectories)
	{
		//scan all volumetric vertices and calc their centre
		double x=0.0;
		double y=0.0;
		double z=0.0;
		for (size_t i=0; i < mesh.Pos.size(); ++i)
		{
			x+=(double)mesh.Pos[i].x;
			y+=(double)mesh.Pos[i].y;
			z+=(double)mesh.Pos[i].z;
		}
		x/=double(mesh.Pos.size());
		y/=double(mesh.Pos.size());
		z/=double(mesh.Pos.size());

		//and save it
		centreCoords[(unsigned)fileNo]=Vector3F(float(x),float(y),float(z));
	}

	//start loading filopodia objects
	mesh.DiscardAllFilopodiaObjects();
	int filoNo=0;

	do
	{
		++filoNo;
		sprintf(fn,"%s/C%d_fTree%02d_T%03d.vtk",path,ID,filoNo,fileNo);

		retval=mesh.ImportVTK_Ftree(fn,999999);
		if (retval != 1) std::cout << "reading filopodium: " << fn << "\n";
		if (retval == 0 && keepTrajectories == true)
		{
			//managed to import filopodium (possibly with its offspring branches)
			//remember at what indices in mesh.Ftree are these stored
			//(as we cannot yet remember directly the pixel offset, we at least
			//remember index within mesh.Ftree and translate to pixel offset later)
			//
			//iterate over the internal mapping (just_branchID -> Ftree indices)
			std::map<int,unsigned int>::const_iterator iterTips=mesh.tipOffsets.begin();
			while (iterTips != mesh.tipOffsets.end())
			{
				const int idx=tipIndex(filoNo,iterTips->first);
				if (tipOffsets[idx].size() == 0)
				{
					//we have found a new pair filoID+branchID
					tipOffsets[idx].Init();
					tipCoords[idx].Init();
				}

				std::cout << "before check " << std::endl;
				std::cout << "idx: " << idx << std::endl;
				std::cout << "fileNo: " << fileNo << std::endl;
				std::cout << "tippOffsets.size: " << tipOffsets[idx].size() << "\n";
				tipOffsets[idx].at((unsigned)fileNo)=(signed)iterTips->second;
				std::cout << "after check" << std::endl;

				++iterTips;
			}
		}
	} while (retval < 2 && filoNo < 100);
	//NB: retval == 0 when read successfully; == 1 when file open error

	//report errors except for "cannot open file"
	if (retval > 1) 
	{
		std::cout << "some error reading filopodium: " << retval << "\n";
		return(false);
	}

	//remove last filopodium mesh (which is usually empty...)
	mesh.DiscardLastEmptyFilopodiumObject();

	std::cout << "loaded cell #" << ID << " fileNo #" << fileNo << "\n";

	//keep (by rotating) vertex positions
	mesh.RotateVolPos();

	return(true);
}


int RenderMesh(const char* path,
					const int ID,
					int fileNo,
					i3d::Image3d<i3d::GRAY16> *mask,
					i3d::Image3d<i3d::GRAY16> *texture)
{
   char fileName[1024];

	//setup isotropic(!) image for mask-output of the simulated cell
	mask[ID].SetOffset(i3d::Offset(mesh.meshShift.x,mesh.meshShift.y,mesh.meshShift.z));
	const float isoRes = mesh.configFile.texture_Resolution.x;
	mask[ID].SetResolution(i3d::Resolution(isoRes,isoRes,isoRes));
	//use "optimal" calculated size
	mask[ID].MakeRoom((size_t)(mesh.meshPlaygroundSize.x*isoRes),
                 (size_t)(mesh.meshPlaygroundSize.y*isoRes),
                 (size_t)(mesh.meshPlaygroundSize.z*isoRes));
	mask[ID].GetVoxelData()=0;

	//renders the isotropic mask into the image
	std::cout << "rendering mask #" << fileNo << "\n";
	mesh.RenderMask_bodyVol(mask[ID]); //full volume
	std::map<int,timeVector>::iterator tipIter=tipOffsets.begin();
	while (tipIter != tipOffsets.end())
	{
		bool checkCollisionOfTip = false;
		//if this filopodium existed already in the previous two frames, check for collision
		//NB: we do not want to check for collision for tips of filopodium that just appeared
		//    in the scene because this one is usually still "buried" inside its mother-filopodium;
		//    often its remains burried for the first two frames (of its life)
		if ((fileNo > 1) && (tipIter->second.at((unsigned)fileNo-2) > -1)) checkCollisionOfTip = true;
		//else std::cout << "RENDERING ID " << tipIter->first << " with no detection\n";

		if (tipIter->second.at((unsigned)fileNo) > -1)
			mesh.RenderMask_filo(mask[ID],(unsigned)tipIter->second.at((unsigned)fileNo),(unsigned short)tipIter->first,false,checkCollisionOfTip);
		++tipIter;
	}

	/*
	std::cout << "exporting isotropic mask #" << fileNo << "\n";
	sprintf(fileName,"%s/tmp_%d_ISOmask_t%03d.ics",path,ID,fileNo);

	mask[ID].SaveImage(fileName);
	*/ 

	//updates the texture fl. points
	if (mesh.DotsFirstRun())
	{
		//we are now simulating the very first image in the sequence,
		//we need to initiate the textures
		//
		//first, prepare the cell body texture
		std::cout << "rendering texture first run\n";
		mesh.InitDots_body(mask[ID]);
		//slightly displace the fl. points to "hide" the FF shift-effects
		//NB: the filopodia texture dots are now empty
		mesh.BrownDots(mask[ID]);

		//second, create texture for filopodia
		mesh.dots_filo.clear();
		mesh.dots_filo.reserve(300000);
		for (unsigned int i=0; i < mesh.GetFilopodiaNumber(); ++i)
			mesh.InitDots_filo(mask[ID],i); //filopodium one by one
		std::cout << "filo dots size=" << mesh.dots_filo.size() << "\n";
	}
	else
	{
		//we are now simulating some next image, we should update the
		//previously created texture (at least for the cell body)
		std::cout << "rendering texture iteration\n";

		//internal-affairs (movement due to cell will, internal processes)

		//this is not considered in this project
		//mesh.BrownDots(mask);


		//external-affairs (movement due to cell movement itself)

		//obtain fresh flow field, so that point positions can be updated
		std::cout << "oldVol.size=" << mesh.oldVolPos.size() << ", newVol.size=" << mesh.newVolPos.size() << "\n";

		FlowField<float> FF;
		FF.x=new i3d::Image3d<float>;
		FF.y=new i3d::Image3d<float>;
		FF.z=new i3d::Image3d<float>;
		FF.x->CopyMetaData(mask[ID]);
		FF.y->CopyMetaData(mask[ID]);
		FF.z->CopyMetaData(mask[ID]);

		//create a FF (FlowField) by "rendering displacement triangles"
		mesh.ConstructFF_T(FF);
/*
		//uncomment if you want to see the FF for the cell body
		char n[1024];
		sprintf(n,"%s/ID%d_afterTM/new_vx_%03d.ics",path,ID,fileNo);
		FF.x->SaveImage(n);
		sprintf(n,"%s/ID%d_afterTM/new_vy_%03d.ics",path,ID,fileNo);
		FF.y->SaveImage(n);
		sprintf(n,"%s/ID%d_afterTM/new_vz_%03d.ics",path,ID,fileNo);
		FF.z->SaveImage(n);
*/

		//apply the FF on the fl. dots
		mesh.FFDots(mask[ID],FF);

		//FF destructor will delete dyn. allocated images in the FF...

		//also update filopodia (by re-creating them from the scratch)
		mesh.dots_filo.clear();
		mesh.dots_filo.reserve(300000);
		for (unsigned int i=0; i < mesh.GetFilopodiaNumber(); ++i)
			mesh.InitDots_filo(mask[ID],i); //filopodium one by one
		std::cout << "filo dots size=" << mesh.dots_filo.size() << "\n";
	}

	//now the texture is fresh and in place of the new mask

	
	//renders the isotropic texture
	std::cout << "rendering texture I   #" << fileNo << "\n";
	// i3d::Image3d<i3d::GRAY16> texture;
	mesh.RenderDots(mask[ID],texture[ID]);
	//
	//or always one shot (for debugging, always a new texture, incoherent):
	//i3d::Image3d<i3d::GRAY16> texture;
	//mesh.RenderOneTimeTexture(mask,texture);
 
	/*
	sprintf(fileName,"%s/tmp_%d_phantom_t%03d.ics",path,ID,fileNo);
	texture[ID].SaveImage(fileName);
   */

/*
	std::cout << "exporting mask #" << fileNo << "\n";
	sprintf(fileName,"%s/ID%d_afterTM/mask_t%03d.ics",path,ID,fileNo);
#ifdef DO_PHASE_II_AND_III

#ifdef DO_ANISOTROPIC_OUTPUT
	i3d::Image3d<i3d::GRAY16> maskNoIso;
	//re-render (!, instead of resampling) also the mask before exporting
	//new size (lanczos_resample() rounds size, we must too)
	mask.MakeRoom((size_t)roundf(factor[0]*mask.GetSizeX()),
	              (size_t)roundf(factor[1]*mask.GetSizeY()),
	              (size_t)roundf(factor[2]*mask.GetSizeZ()));
	//new resolution
	i3d::Resolution res=mask.GetResolution();
	res.SetX(factor[0]*res.GetX());
	res.SetY(factor[1]*res.GetY());
	res.SetZ(factor[2]*res.GetZ());
	mask.SetResolution(res);
	//re-render
	mask.GetVoxelData()=0;
	mesh.RenderMask_bodyVol(mask); //full volume
	tipIter=tipOffsets.begin();
	while (tipIter != tipOffsets.end())
	{
		if (tipIter->second.at((unsigned)fileNo) > -1)
			mesh.RenderMask_filo(mask,(unsigned)tipIter->second.at((unsigned)fileNo),(unsigned short)tipIter->first);
		++tipIter;
	}
 #endif
 
#endif
	//save the mask image
	maskUnified.SaveImage(fileName);

	//record voxel offsets within a mask image of all filopodia tips
	const float xRes=maskUnified.GetResolution().GetX();
	const float yRes=maskUnified.GetResolution().GetY();
	const float zRes=maskUnified.GetResolution().GetZ();
	const float xOff=maskUnified.GetOffset().x;
	const float yOff=maskUnified.GetOffset().y;
	const float zOff=maskUnified.GetOffset().z;

	//scan over all branches we recognize at this time-point
	std::map<int,timeVector>::iterator tipIter=tipOffsets.begin();
	while (tipIter != tipOffsets.end())
	{
		//if there is a record for the present time-point, translate it
		if (tipIter->second.at((unsigned)fileNo) > -1)
		{
			const unsigned int i=(unsigned)tipIter->second.at((unsigned)fileNo);
			const Vector3FC& realPos=mesh.Ftree[i].fPoints[mesh.Ftree[i].segToPoint.back()];
			//get pixel coordinate
			const size_t x=size_t((realPos.x-xOff) *xRes);
			const size_t y=size_t((realPos.y-yOff) *yRes);
			const size_t z=size_t((realPos.z-zOff) *zRes);
			tipIter->second.at((unsigned)fileNo)=(int)maskUnified.GetIndex(x,y,z);

			//save also the micron coordinate at the corresponding place
			//into the coordinate structure
			tipCoords[tipIter->first].at((unsigned)fileNo)=realPos;
		}
		++tipIter;
	}

	//translate cell centre into offset:
	//get pixel coordinate
	const size_t x=size_t((centreCoords[(unsigned)fileNo].x-xOff) *xRes);
	const size_t y=size_t((centreCoords[(unsigned)fileNo].y-yOff) *yRes);
	const size_t z=size_t((centreCoords[(unsigned)fileNo].z-zOff) *zRes);
	centreOffsets[(unsigned)fileNo]=(int)maskUnified.GetIndex(x,y,z);
*/
	return(0);
}

template <class T> void MergeImages(i3d::Image3d<T> *images, 
												size_t num, 
												i3d::Image3d<T> &img)
{
	i3d::Offset 
			  upperLeftCorner = i3d::Vector3d<float>(FLT_MAX, FLT_MAX, FLT_MAX), 
			  lowerRightCorner = i3d::Vector3d<float>(-FLT_MAX, -FLT_MAX, -FLT_MAX);
	
	 for (size_t i=0; i<num; i++)
	 {
		// get current offset and size
		i3d::Offset currOffset = images[i].GetOffset();
		i3d::Offset currSize = i3d::PixelsToMicrons(images[i].GetSize(), 
																  images[i].GetResolution());
		std::cout << "image no: " << i << std::endl;
		std::cout << "offset: " << currOffset << std::endl;
		std::cout << "size: " << currSize << std::endl;

		// update the overall ragnes
		upperLeftCorner = i3d::min(upperLeftCorner, currOffset);
		lowerRightCorner = i3d::max(lowerRightCorner, currOffset + currSize);
	 }

	std::cout << "overall upper left corner" << upperLeftCorner << std::endl;
	std::cout << "overall lower right corner" << lowerRightCorner << std::endl;

	i3d::Vector3d<size_t> sz = 
			  i3d::MicronsToPixels(lowerRightCorner - upperLeftCorner, 
										  images[0].GetResolution()) + 1.0;

	// allocate space for new images
	img.MakeRoom(sz);
	img.SetOffset(upperLeftCorner);
	img.SetResolution(images[0].GetResolution());
	img.Clear();

	for (size_t i=0; i<num; i++)
	{
		 i3d::Offset off = images[i].GetOffset();
		 i3d::Resolution res = images[i].GetResolution();

		 for (size_t idx=0; idx<images[i].GetImageSize(); idx++)
		 {
			  // get value 
			  T value = images[i].GetVoxel(idx);

			  // get real position
			  i3d::Offset pos = 
						 off + i3d::PixelsToMicrons(images[i].GetPos(idx), res);

			  // set relative position
			  i3d::Vector3d<size_t> rpos = 
						 i3d::MicronsToPixels(pos - upperLeftCorner, res);

			  if (value != T(0))
					img.SetVoxel(rpos, value);
	 }
	}
}

template <class T> void CompleteSimulation(i3d::Image3d<T> &img)
{
	//do phase II on the isotropic image
	i3d::Image3d<float> intermediate;

	std::cout << "simulating optical system\n";
	mesh.PhaseII(img,intermediate);

#ifdef DO_ANISOTROPIC_OUTPUT
	//downsample now:
	const float factor[3]={
		 1.f, //NB: isotropic phantom has resolution of the x-axis
		 mesh.configFile.texture_Resolution.y/mesh.configFile.texture_Resolution.x,
		 mesh.configFile.texture_Resolution.z/mesh.configFile.texture_Resolution.x};
	i3d::Image3d<float>* tmp=NULL;
	i3d::lanczos_resample(&intermediate,tmp,factor,2);
	intermediate=*tmp;
	delete tmp;
#endif

	//do phase III on the final image (can be iso or aniso now)
	std::cout << "simulating virtual camera\n";
mesh.PhaseIII(intermediate, img);
}
