00001 #ifndef GRID_HPP
00002 #define GRID_HPP
00003
00004 #include <Geom.hpp>
00005
00006 #include <assert.h>
00007
00008
00009
00010 class Grid3
00011 {
00012 public:
00013 class Cell
00014 {
00015 public:
00016 Cell() { tris = 0; }
00017 ~Cell() { if(tris) delete [] tris; }
00018 void addTriangle(int t)
00019 {
00020 int *otris = tris;
00021 int ntris = otris ? otris[0]+1 : 1;
00022 tris = new int[ntris+1];
00023 tris[0] = ntris;
00024 if(otris)
00025 {
00026 std::copy(otris+1,otris+ntris,tris+1);
00027 delete [] otris;
00028 }
00029 tris[ntris] = t;
00030 }
00031 int getTriangleCount() { return tris ? tris[0] : 0; }
00032 int getTriangle(int i) { return tris[i+1]; }
00033 private:
00034 int* tris;
00035 };
00036 Grid3(int n, const Vector3& min, const Vector3& max);
00037 void rasterizeTriangle(const Vector3& v0, const Vector3& v1, const Vector3& v2, int id);
00038 bool insideBounds(const Vector3& p)
00039 {
00040 if( (p.x > gmin.x && p.x < gmax.x) &&
00041 (p.y > gmin.y && p.y < gmax.y) &&
00042 (p.z > gmin.z && p.z < gmax.z) )
00043 return true;
00044 else
00045 return false;
00046 }
00047
00048
00049
00050
00051
00052
00053
00054
00055
00056
00057
00058
00059
00060
00061
00062
00063
00064
00065 Vector3 getCellSize() const { return cellSize; }
00066
00067
00068 Vector3 getCoords(const Vector3i& ci)
00069 {
00070 Vector3 pos;
00071 pos.x = ci.x*cellSize.x;
00072 pos.y = ci.y*cellSize.y;
00073 pos.z = ci.z*cellSize.z;
00074 return pos+gmin;
00075 }
00076
00077
00078 Vector3i getGridCoords(const Vector3& pos)
00079 {
00080 Vector3 c = (pos-gmin).descale(cellSize);
00081 Vector3i ci(c.x,c.y,c.z);
00082 return ci;
00083 }
00084
00085
00086
00087 Cell& getCell(const Vector3& pos)
00088 {
00089 Vector3 c = (pos-gmin).descale(cellSize);
00090 Vector3i ci(c.x,c.y,c.z);
00091 return getCell(ci);
00092 }
00093 Cell& getCell(const Vector3i& ci)
00094 {
00095 assert(ci.x < n && ci.y < n && ci.z < n);
00096 return cells[ci.x*n*n+ci.y*n+ci.z];
00097 }
00098
00099 Cell *cells;
00100 Vector3 gmin,gmax;
00101 Vector3 cellSize;
00102 int n;
00103
00104
00105 };
00106
00107 #endif