BlueNoise.h

00001 
00002 // $Id: PDSampling.h,v 1.6 2006/07/06 23:13:18 zr Exp $
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 
00045 class BlueNoise {
00046 protected:
00047         RNG m_rng;
00048         std::vector<int> m_neighbors;
00049         
00050         int (*m_grid)[kMaxPointsPerCell];
00051         int m_gridSize;
00052         float m_gridCellSize;
00053         
00054 public:
00055         std::vector<Vec2> points;
00056         float radius;
00057         bool isTiled;
00058 
00059 public:
00060         BlueNoise(float radius, bool isTiled=true, bool usesGrid=true);
00061         virtual ~BlueNoise() { };
00062 
00063         //
00064 
00065         bool pointInDomain(Vec2 &a);
00066 
00067                 // return shortest distance between _a_ 
00068                 // and _b_ (accounting for tiling)
00069         float getDistanceSquared(Vec2 &a, Vec2 &b) { Vec2 v = getTiled(b-a); return v.x*v.x + v.y*v.y; }
00070         float getDistance(Vec2 &a, Vec2 &b) { return sqrt(getDistanceSquared(a, b)); }
00071 
00072                 // generate a random point in square
00073         Vec2 randomPoint();
00074 
00075                 // return tiled coordinates of _v_
00076         Vec2 getTiled(Vec2 v);
00077 
00078                 // return grid x,y for point
00079         void getGridXY(Vec2 &v, int *gx_out, int *gy_out);
00080 
00081                 // add _pt_ to point list and grid
00082         void addPoint(Vec2 pt);
00083 
00084                 // populate m_neighbors with list of
00085                 // all points within _radius_ of _pt_
00086                 // and return number of such points
00087         int findNeighbors(Vec2 &pt, float radius);
00088 
00089                 // return distance to closest neighbor within _radius_
00090         float findClosestNeighbor(Vec2 &pt, float radius);
00091 
00092                 // find available angle ranges on boundary for candidate 
00093                 // by subtracting occluded neighbor ranges from _rl_
00094         void findNeighborRanges(int index, RangeList &rl);
00095 
00096                 // extend point set by boundary sampling until domain is 
00097                 // full
00098         void maximize();
00099 
00100                 // apply one step of Lloyd relaxation
00101         void relax();
00102 
00103         //
00104 
00105         void complete();
00106 
00107   void writeToBool(bool* noise, int size);
00108 };

Generated on Wed Oct 25 01:13:06 2006 for Lumos by  doxygen 1.4.6