//--------------------------------------------------------------
void testApp::update(){
	
	
	panel.update();
	nIterations				= panel.getValueI("N_ITERATIONS"); 
	nNearestNeighborsK		= panel.getValueI("N_NEIGHBORS");
	nMaxPointsToVisit		= nNearestNeighborsK + 6;
	totalCycleDurationInIterations = nIterations * 6;
	
	if (!bPause && (iterationCount < nIterations)){
		
		int nToAddPerFrame  = panel.getValueI("ADD_PER_FRAME");
		//addPositionsProbabilisticallyBasedOnSourceImage (nToAddPerFrame);
		addPointsOnTooLongConnections();
		
		applyImageForces();
		
		IppiSize allBufRoi = {nParticles, 1};
		
		// add random forces to velocities
		unsigned int pSeed;
		float fmax = panel.getValueF("FORCE_NOISE");
		ippiAddRandUniform_Direct_32f_C1IR(velocityx, particleStep32f, allBufRoi, -fmax,fmax, &pSeed);
		ippiAddRandUniform_Direct_32f_C1IR(velocityy, particleStep32f, allBufRoi, -fmax,fmax, &pSeed);
		
		// multiply velocities by damping factor
		Ipp32f damping = panel.getValueF("DAMPING");
		Ipp32f progressiveDamping = ofMap(iterationCount, 0,nIterations, 1.0, 0.0, true);
		
		ippiMulC_32f_C1IR(progressiveDamping, velocityx, particleStep32f, allBufRoi);
		ippiMulC_32f_C1IR(progressiveDamping, velocityy, particleStep32f, allBufRoi);
		
		// add velocities to positions 
		ippiAdd_32f_C1IR(velocityx, particleStep32f, positionx, particleStep32f, allBufRoi);
		ippiAdd_32f_C1IR(velocityy, particleStep32f, positiony, particleStep32f, allBufRoi);
		

		// clamp positions to acceptable ranges
		ippiThreshold_LTValGTVal_32f_C1IR (positionx, particleStep32f, allBufRoi, bounds.x, bounds.x, bounds.x+bounds.width,  bounds.x+bounds.width);
		ippiThreshold_LTValGTVal_32f_C1IR (positiony, particleStep32f, allBufRoi, bounds.y, bounds.y, bounds.y+bounds.height, bounds.y+bounds.height);
	
		
		//----------------------------------
		updateNearestNeighbors();
		updateParticles();
		
	}
	iterationCount++;
	
	if (iterationCount == nIterations){
		saveFBO(); // DO IT!
	}
	if (bCycleAutomatically && (iterationCount > totalCycleDurationInIterations)){
		initialize(false); 
	}

	
}
IppStatus FindLocalMax_2D(Image2D &image_bpass, Image2D &image_bpass_thresh, Image2D &image_subtracted,
						  const int intensity_threshold, const int dilation_radius)
{
	IppStatus status;
	Image2D image_dilated(image_bpass.get_length(), image_bpass.get_width());
	Dilation_Kernel DilationKernel(dilation_radius, image_bpass.get_width(), image_bpass.get_length());

	//Threshold darker pixels in bandpassed image (in preparation for later subtraction)
	RecenterImage(image_bpass);
	status = ippiThreshold_LTVal_32f_C1R(image_bpass.get_image2D(), image_bpass.get_stepsize(),
		image_bpass_thresh.get_image2D(), image_bpass_thresh.get_stepsize(),
		image_bpass.get_ROIfull(), intensity_threshold, intensity_threshold);

	//Dilate Bandpassed image with a circular kernel
	status = ippiSet_32f_C1R(intensity_threshold, image_dilated.get_image2D(), image_dilated.get_stepsize(),
		image_dilated.get_ROIfull());
	status = ippiDilate_32f_C1R(
		//image_bpass.get_image2D() + DilationKernel.get_offset(), image_bpass.get_stepsize(),
		image_bpass_thresh.get_image2D() + DilationKernel.get_offset(), image_bpass_thresh.get_stepsize(),
		image_dilated.get_image2D()+ DilationKernel.get_offset(), image_dilated.get_stepsize(),
		DilationKernel.get_ROI_size(), DilationKernel.get_dilation_kernel(), DilationKernel.get_mask_size(),
		DilationKernel.get_anchor_point());

	//subtract, such that resulting array is negative to zero (for later exponentation)
	status = ippiSub_32f_C1R(
		image_dilated.get_image2D(), image_dilated.get_stepsize(),
		image_bpass.get_image2D(), image_bpass.get_stepsize(),
		image_subtracted.get_image2D(), image_subtracted.get_stepsize(),
		image_bpass.get_ROIfull());

	//exponentiate subtracted array, then threshold
	status = ippiExp_32f_C1IR(image_subtracted.get_image2D(), image_subtracted.get_stepsize(),
		image_subtracted.get_ROIfull());
	status = ippiThreshold_LTValGTVal_32f_C1IR(image_subtracted.get_image2D(), image_subtracted.get_stepsize(),
		image_subtracted.get_ROIfull(), 1-epsilon, 0, 1-epsilon, 1);
	return status;
}