TEST(TransferParticlesToPointCloudBehaviorTests, UpdateTest)
{
	auto runtime = std::make_shared<Framework::Runtime>();
	runtime->addManager(std::make_shared<Framework::BehaviorManager>());
	runtime->addManager(std::make_shared<Physics::PhysicsManager>());

	auto sceneElement = std::make_shared<Framework::BasicSceneElement>("Element");

	auto particles = std::make_shared<Particles::SphRepresentation>("Particles");
	particles->setMaxParticles(10);
	particles->setMassPerParticle(1.0);
	particles->setDensity(1.0);
	particles->setGasStiffness(1.0);
	particles->setKernelSupport(1.0);
	for (size_t particleId = 0; particleId < 10; particleId++)
	{
		particles->addParticle(Vector3d(static_cast<double>(particleId), 0.0, 0.0), Vector3d::Zero(), 100000);
	}
	sceneElement->addComponent(particles);

	auto pointCloud = std::make_shared<Graphics::OsgPointCloudRepresentation>("Graphics");
	sceneElement->addComponent(pointCloud);

	auto behavior = std::make_shared<TransferParticlesToPointCloudBehavior>("Behavior");
	behavior->setSource(particles);
	behavior->setTarget(pointCloud);
	sceneElement->addComponent(behavior);

	auto scene = runtime->getScene();
	scene->addSceneElement(sceneElement);

	particles->update(0.1);
	behavior->update(0.1);
	auto sourceVertices = particles->getParticles().safeGet()->getVertices();
	auto targetVertices = pointCloud->getVertices()->getVertices();
	ASSERT_EQ(sourceVertices.size(), targetVertices.size());

	auto sourceVertex = sourceVertices.begin();
	auto targetVertex = targetVertices.begin();
	for (; sourceVertex != sourceVertices.end(); ++sourceVertex, ++targetVertex)
	{
		EXPECT_TRUE(sourceVertex->position.isApprox(targetVertex->position));
	}

	particles->removeParticle(0);
	particles->removeParticle(1);
	particles->update(0.1);
	behavior->update(0.1);

	sourceVertices = particles->getParticles().safeGet()->getVertices();
	targetVertices = pointCloud->getVertices()->getVertices();
	ASSERT_EQ(sourceVertices.size(), targetVertices.size());

	sourceVertex = sourceVertices.begin();
	targetVertex = targetVertices.begin();
	for (; sourceVertex != sourceVertices.end(); ++sourceVertex, ++targetVertex)
	{
		EXPECT_TRUE(sourceVertex->position.isApprox(targetVertex->position));
	}
}
//------------------------------------------------------------------------------
void ParticleSystem::unserializeData(JsonBox::Value & o)
{
	Renderer::unserializeData(o);

	setMaxParticles(o["maxParticles"].getInt());

	zn::unserialize(o["particleRadius"], m_particleRadius);
	zn::unserialize(o["particleLifeTime"], m_particleLifeTime);
	zn::unserialize(o["emissionZone"], m_emissionZone);

	std::string textureName = o["texture"].getString();
	r_texture = AssetBank::current()->textures.get(textureName);
	if(r_texture == nullptr)
	{
		log.err() << "ParticleSystem::unserializeData: texture not found \""
			<< textureName << '"' << log.endl();
	}

	std::string atlasName = o["atlas"].getString();
	r_atlas = AssetBank::current()->atlases.get(atlasName);
	if(r_atlas == nullptr)
	{
		log.err() << "ParticleSystem::unserializeData: atlas not found \""
			<< atlasName << '"' << log.endl();
	}

	sf::IntRect atlasRect;
	zn::unserialize(o["atlasRect"], atlasRect);
	setTextureRect(atlasRect);
}