std::vector<CParticleF> 
vector2particle(const std::vector<float>& P,
				const int* dims)
{
	int nump = dims[0];
	vector<CParticleF> points(nump);
	for(int i=0; i<nump; ++i)
	{
		points[i].m_X = GetData2(P, i, 0, dims[0], dims[1], (float)0);
		points[i].m_Y = GetData2(P, i, 1, dims[0], dims[1], (float)0);
		/*if(dims[1] >= 3)
		{
			points[i].m_Z = GetData2(P, i, 2, dims[0], dims[1], (float)0);
		}
		if(dims[1] >= 4)
		{
			points[i].m_Life = GetData2(P, i, 3, dims[0], dims[1], (float)0);
		}*/
	}
	return points;
}
std::vector<Triangulation::_Internal::_indexed_triangle> 
indices2structs(const std::vector<CParticleF>& P, 
				const std::vector<int>& T, 
				const int* dimsT)
{
	int n = dimsT[0];
	vector<Triangulation::_Internal::_indexed_triangle> tr(n);
	for(int i=0; i<n; ++i)
	{
		for(int j=0; j<3; ++j)
		{
			int k = GetData2(T, i, j, dimsT[0], dimsT[1], -1);
			if(k<1) abort();
			tr[i].index[j] = k - 1; //change from 1 to 0 index.
		}
	}
	return tr;
}
bool
ParticleSimulator::LoadParticles(vector<float>& F, const int* dims)
{
	ParticleFactory* factory = ParticleFactory::getInstance();
	factory->clean();
	this->time = 0.0f;
	map<int, MovingParticle*> id2particle;
	vector<int> vchild1;
	vector<int> vchild2;
	vector<int> vparent1;
	vector<int> vparent2;
	vector<int> vprev;
	vector<int> vnext;
	vector<int> veq;
	vector<int> ver;
	for (int i = 0; i < dims[0]; ++i)
	{
		int id = (int)GetData2(F, i, 0, dims[0], dims[1], -1.0f);
		float x0 = GetData2(F, i, 1, dims[0], dims[1], std::numeric_limits<float>::quiet_NaN());
		float y0 = GetData2(F, i, 2, dims[0], dims[1], std::numeric_limits<float>::quiet_NaN());
		float x = GetData2(F, i, 3, dims[0], dims[1], std::numeric_limits<float>::quiet_NaN());
		float y = GetData2(F, i, 4, dims[0], dims[1], std::numeric_limits<float>::quiet_NaN());
		float vx = GetData2(F, i, 5, dims[0], dims[1], std::numeric_limits<float>::quiet_NaN());
		float vy = GetData2(F, i, 6, dims[0], dims[1], std::numeric_limits<float>::quiet_NaN());
		int pid = (int)GetData2(F, i, 7, dims[0], dims[1], -1.0f);
		int nid = (int)GetData2(F, i, 8, dims[0], dims[1], -1.0f);
		MovingParticleType type = int2ParticleType((int)GetData2(F, i, 9, dims[0], dims[1], -1.0f));
		float created = GetData2(F, i, 10, dims[0], dims[1], 0.0f);
		float time = GetData2(F, i, 11, dims[0], dims[1], 0.0f);
		float ref = GetData2(F, i, 12, dims[0], dims[1], 0.0f);
		bool bActive = GetData2(F, i, 13, dims[0], dims[1], 0.0f)>0.0f ? true : false;
		bool bInitialized = (bool)GetData2(F, i, 14, dims[0], dims[1], 0.0f)>0.0f ? true : false;
		bool bUnstable = (bool)GetData2(F, i, 15, dims[0], dims[1], 0.0f)>0.0f ? true : false;
		int parent1 = (int)GetData2(F, i, 16, dims[0], dims[1], -1.0f);
		int parent2 = (int)GetData2(F, i, 17, dims[0], dims[1], -1.0f);
		int child1 = (int)GetData2(F, i, 18, dims[0], dims[1], -1.0f);
		int child2 = (int)GetData2(F, i, 19, dims[0], dims[1], -1.0f);
		EventType etype = int2EventType((int)GetData2(F, i, 20, dims[0], dims[1], 0.0f));
		int eq = (int)GetData2(F, i, 21, dims[0], dims[1], -1.0f);
		int er = (int)GetData2(F, i, 22, dims[0], dims[1], -1.0f);
		float etime = GetData2(F, i, 23, dims[0], dims[1], 0.0f);
		MovingParticle* p = factory->makeParticle(CParticleF(x0, y0), type, created);
		p->p.m_X = x;
		p->p.m_Y = y;
		p->v[0] = vx;
		p->v[1] = vy;
		p->time = time;
		p->bActive = bActive;
		//p->bInitialized = bInitialized;
		p->bUnstable = bUnstable;
		p->event.type = etype;
		p->event.t = etime;
		p->reflexive = ref;
		vparent1.push_back(parent1);
		vparent2.push_back(parent2);
		vchild1.push_back(child1);
		vchild2.push_back(child2);
		p->event.p = p;
		vprev.push_back(pid);
		vnext.push_back(nid);
		veq.push_back(eq);
		ver.push_back(er);
		id2particle[p->id] = p;
		if (p->bActive==false)
		{
			factory->inactivate(p);
		}
		if (p->time > this->time)
		{
			this->time = p->time;
		}
	}
	//now  set prev, next, etc.
	for (int i = 0; i < factory->particles.size(); ++i)
	{
		MovingParticle* p = factory->particles[i];
		p->prev = vprev[i] >= 0 ? id2particle[vprev[i]] : NULL;
		p->next = vnext[i] >= 0 ? id2particle[vnext[i]] : NULL;
		p->event.q = veq[i] >= 0 ? id2particle[veq[i]] : NULL;
		p->event.r = ver[i] >= 0 ? id2particle[ver[i]] : NULL;
		p->parents[0] = vparent1[i] >= 0 ? id2particle[vparent1[i]] : NULL;
		p->parents[1] = vparent2[i] >= 0 ? id2particle[vparent2[i]] : NULL;
		p->children[0] = vchild1[i] >= 0 ? id2particle[vchild1[i]] : NULL;
		p->children[1] = vchild2[i] >= 0 ? id2particle[vchild2[i]] : NULL;
		if (p->id >= MovingParticle::_id)
		{
			MovingParticle::_id = p->id + 1;
		}
	}

	return true;
}