Blame c++/perspective/src/generator.h

531f67
#ifndef GENERATOR_H
531f67
#define GENERATOR_H
531f67
531f67
531f67
#include <climits></climits>
531f67
#include <map></map>
531f67
531f67
#include "surface.h"
531f67
#include "vector.h"
531f67
531f67
531f67
class Generator: public Shared {
531f67
public:
531f67
	struct CacheEntry;
531f67
	typedef std::map<ulongintvector2, cacheentry=""> Cache;</ulongintvector2,>
531f67
	
531f67
	struct CacheEntry {
531f67
		Cache::iterator next, previous;
531f67
		bool in_use;
531f67
		Vector4 value;
531f67
	};
531f67
	
531f67
	static inline ULongInt random(ULongInt x) {
077021
		++x;
077021
		x ^= x >> 2;
077021
		x ^= x << 3;
077021
		x ^= x >> 5;
077021
		x ^= x << 7;
077021
		x ^= x >> 11;
531f67
		x ^= x << 13;
077021
		x ^= x >> 17;
077021
		x ^= x << 19;
077021
		x ^= x >> 23;
077021
		x ^= x << 29;
077021
		x ^= x >> 31;
077021
		x ^= x << 37;
077021
		x ^= x >> 41;
077021
		x ^= x << 43;
077021
		x ^= x >> 47;
077021
		x ^= x << 53;
077021
		x ^= x >> 59;
077021
		x ^= x << 61;
077021
		++x;
531f67
		return x;
531f67
	}
a9c87c
531f67
	static inline Real random_to_real(ULongInt x)
531f67
		{ return Real(x)/Real(ULLONG_MAX); }
a9c87c
	static inline Real random_to_normal(ULongInt x, ULongInt y)
a9c87c
		{ return sqrt(-2*log(random_to_real(x))) * sin(2*M_PI*random_to_real(y)); }
a9c87c
	
a9c87c
	static inline Real mirror_repeat(Real x) {
a9c87c
		x *= 0.5;
a9c87c
		x -= floor(x);
a9c87c
		x *= 2;
a9c87c
		return x > 1 ? 2 - x : x;
a9c87c
	}
531f67
531f67
private:
531f67
	const ULongInt one;
531f67
	const int max_cache_count;
531f67
	
531f67
	mutable Cache m_cache;
531f67
	mutable Cache::iterator m_cache_top;
531f67
	mutable int m_cache_count;
531f67
	
531f67
	ULongInt m_seeds[2][4];
531f67
531f67
public:
531f67
	Generator();
531f67
	explicit Generator(ULongInt seed);
531f67
	
531f67
	inline const ULongInt* seeds_x() const { return m_seeds[0]; }
531f67
	inline const ULongInt* seeds_y() const { return m_seeds[1]; }
531f67
	inline ULongInt seed() const { return seeds_x()[0]; }
531f67
	void set_seed(ULongInt x);
531f67
531f67
	Real random(const ULongIntVector2 &coord, int index) const;
531f67
	Vector4 generate(const ULongIntVector2 &coord, bool shrink_cache = true) const;
531f67
	Vector4 generate(const Vector2 &coord, const Vector2 &precision) const;
4dc49c
	void generate(Surface &target, const Pair2 &bounds) const;
531f67
};
531f67
531f67
531f67
#endif