00001
00002
00003
00004 #include "RNG.h"
00005 #include <cmath>
00006 #include <vector>
00007
00008 #define kMaxPointsPerCell 9
00009
00010 class RangeList;
00011 class ScallopedRegion;
00012
00013 class Vec2 {
00014 public:
00015 Vec2() {};
00016 Vec2(float _x, float _y) : x(_x), y(_y) {};
00017
00018 float x,y;
00019
00020 float length() { return sqrt(x*x + y*y); }
00021
00022 bool operator ==(const Vec2 &b) const { return x==b.x && y==b.y; }
00023 Vec2 operator +(Vec2 b) { return Vec2(x+b.x, y+b.y); }
00024 Vec2 operator -(Vec2 b) { return Vec2(x-b.x, y-b.y); }
00025 Vec2 operator *(Vec2 b) { return Vec2(x*b.x, y*b.y); }
00026 Vec2 operator /(Vec2 b) { return Vec2(x/b.x, y*b.y); }
00027
00028 Vec2 operator +(float n) { return Vec2(x+n, y+n); }
00029 Vec2 operator -(float n) { return Vec2(x-n, y-n); }
00030 Vec2 operator *(float n) { return Vec2(x*n, y*n); }
00031 Vec2 operator /(float n) { return Vec2(x/n, y*n); }
00032
00033 Vec2 &operator +=(Vec2 b) { x+=b.x; y+=b.y; return *this; }
00034 Vec2 &operator -=(Vec2 b) { x-=b.x; y-=b.y; return *this; }
00035 Vec2 &operator *=(Vec2 b) { x*=b.x; y*=b.y; return *this; }
00036 Vec2 &operator /=(Vec2 b) { x/=b.x; y/=b.y; return *this; }
00037
00038 Vec2 &operator +=(float n) { x+=n; y+=n; return *this; }
00039 Vec2 &operator -=(float n) { x-=n; y-=n; return *this; }
00040 Vec2 &operator *=(float n) { x*=n; y*=n; return *this; }
00041 Vec2 &operator /=(float n) { x/=n; y/=n; return *this; }
00042 };
00043
00048 class BLUE_NOISE {
00049 protected:
00050 RNG m_rng;
00051 std::vector<int> m_neighbors;
00052
00053 int (*m_grid)[kMaxPointsPerCell];
00054 int m_gridSize;
00055 float m_gridCellSize;
00056
00057 public:
00058 std::vector<Vec2> points;
00059 float radius;
00060 bool isTiled;
00061
00062 public:
00063 BLUE_NOISE(float radius, bool isTiled=true, bool usesGrid=true);
00064 virtual ~BLUE_NOISE() { };
00065
00066
00067
00068 bool pointInDomain(Vec2 &a);
00069
00070
00071
00072 float getDistanceSquared(Vec2 &a, Vec2 &b) { Vec2 v = getTiled(b-a); return v.x*v.x + v.y*v.y; }
00073 float getDistance(Vec2 &a, Vec2 &b) { return sqrt(getDistanceSquared(a, b)); }
00074
00075
00076 Vec2 randomPoint();
00077
00078
00079 Vec2 getTiled(Vec2 v);
00080
00081
00082 void getGridXY(Vec2 &v, int *gx_out, int *gy_out);
00083
00084
00085 void addPoint(Vec2 pt);
00086
00087
00088
00089
00090 int findNeighbors(Vec2 &pt, float radius);
00091
00092
00093 float findClosestNeighbor(Vec2 &pt, float radius);
00094
00095
00096
00097 void findNeighborRanges(int index, RangeList &rl);
00098
00099
00100
00101 void maximize();
00102
00103
00104 void relax();
00105
00106 void complete();
00107
00108 void writeToBool(bool* noise, int size);
00109 };