Monthly Archives: May 2014

Why std::vector<>::pop_back() does not return the popped value?

I was curious why std::vector<>::pop_back() does not return the popped value. Instead, there is a pair of methods: void pop_back() and const_reference back() const.

The answer was obvious when I tried to implement a vector-like class myself. Here is the code for an implementation of a would-be T std::vector<>::pop_back():

	   T pop_back()
	   {
	      FSize -= 1;
	      // call copy ctor
	      T Copy( FArray[ FSize ] );
	      // call dtor
	      FArray[ FSize ].~T();
	      // return the copy - this can raise an exception,
	      // but the value has been already popped from the stack
	      return Copy;
	   }

The implementation of the canonical pop_back() is straightforward and does not perform any redundant fuss with copying:

	void pop_back()
	{
		FSize -= 1;
		// this is exception safe since the dtor never throws
		FArray[ FSize ].~T();
	}

Adding density maps to the poisson points generator

I wanted to add varying density maps to my poisson points generator. For example, I want to use this density field to generate foliage:

I started with multiplying the MinDist parameter by the density value. This broke the entire algorithm. The solution that works is to generate a rectangle full of poisson points and then roll a dice for every point and discard it if the roll of the dice is above the density value at the considered point:

			float R = RandomFloat();
			float P = g_DensityMap[ x + y * ImageSize ];
			if ( R > P ) continue;

Here is the resulting image:

The complete source code is on GitHub: https://github.com/corporateshark/poisson-disk-generator