00001
00011 #pragma once
00012
00013 #include "../common/standard.h"
00014
00015 #include <list>
00016 #include <map>
00017 #include <vector>
00018 #include <set>
00019
00020 #include "MatrixTypes.h"
00021 #include "Shapes2d.h"
00022 #include "../ProgressiveMeshSupport/QEM_ProgMesh.h"
00023 #include "Viewpoint.h"
00024
00025 typedef struct
00026 {
00027 Vertex pos;
00028 RgbByte color;
00029 } ColorVertex;
00030
00031
00033 class VoxelPos : public Position< 3, unsigned short >
00034 {
00035 public:
00036 VoxelPos (unsigned short x, unsigned short y, unsigned short z)
00037 {
00038 array[0] = x;
00039 array[1] = y;
00040 array[2] = z;
00041 }
00042
00043 VoxelPos () {}
00044
00045 bool operator<(const VoxelPos &vp) const
00046 {
00047 return (memcmp (this, &vp, sizeof(VoxelPos)) < 0);
00048 }
00049 };
00050
00051
00052 typedef std::map< VoxelPos, RgbByte > ColorMap;
00053
00055 class ColoredVoxelPos : public VoxelPos
00056 {
00057 public:
00058 ColoredVoxelPos (VoxelPos &data, RgbByte &clr)
00059 {
00060 memcpy (array, &data[0], sizeof(array)); color = clr;
00061 }
00062
00063 RgbByte color;
00064
00065 bool operator<(const VoxelPos &vp) const
00066 {
00067 return (memcmp (this, &vp, sizeof(ColoredVoxelPos)) < 0);
00068 }
00069 };
00070
00071
00076 class SurfaceVoxelList
00077 {
00078 public:
00079 typedef std::list< VoxelPos >::iterator iterator;
00080
00082 SurfaceVoxelList() {}
00083
00085 iterator begin() { return voxelList.begin(); }
00086
00088 iterator end() { return voxelList.end(); }
00089
00091 void push_back (VoxelPos p)
00092 {
00093 if (voxelSet.find(p) == voxelSet.end())
00094 {
00095 voxelList.push_back(p);
00096 voxelSet.insert(p);
00097 }
00098 }
00099
00101 unsigned int size()
00102 {
00103 return voxelSet.size();
00104 }
00105
00107 iterator erase(iterator &i)
00108 {
00109
00110 VoxelPos &p = *i;
00111 voxelSet.erase(p);
00112 return voxelList.erase(i);
00113 }
00114
00116 void clear()
00117 {
00118 voxelSet.clear();
00119 voxelList.clear();
00120 }
00121
00122 private:
00123 std::set< VoxelPos > voxelSet;
00124 std::list< VoxelPos > voxelList;
00125 };
00126
00127
00128
00129 typedef std::vector< ColoredVoxelPos > ColoredSurfaceVoxelList;
00130
00131
00137 class VoxelCube
00138 {
00139 public:
00140 void draw(bool bTranslateToCenter);
00141
00142
00143
00144
00148 double getMinimumFarPlane (Vertex COP, Vertex lookatPoint) const;
00149
00151 bool isTransparent (int x, int y, int z) const;
00152 bool isTransparent (const VoxelPos &p) const;
00153
00155 int getVoxelsPerSide() const { return voxelsPerSide; }
00156
00160 void fillItemBufferArray (Viewpoint &image);
00161
00163 std::vector< Vertex > calculateVoxelCorners (int x, int y, int z) const;
00164
00166 Rect< double > getBoundingRect (std::vector< Vertex > &vertices, const double modelMatrix[16], const double projMatrix[16], const int viewport[4]) const;
00167
00169 unsigned int PosToIdentifier (int x, int y, int z) const;
00170
00173 unsigned int PosToIndex (int x, int y, int z) const;
00174
00176 Vertex getCenter() const { return center; }
00177
00179 double getVoxelSize () const { return voxelSize; }
00180
00182 unsigned int GetEdgeID(unsigned int nX, unsigned int nY, unsigned int nZ, unsigned int nEdgeNo);
00183
00184
00185
00186
00187 void setFinalColor (SurfaceVoxelList::iterator i, RgbByte &color);
00188
00189
00190
00191
00192 void setVoxelsPerSide (int vps);
00193
00196 void exposeSurroundingVoxels (SurfaceVoxelList::iterator i);
00197
00201 void VoxelCube::setTransparent(SurfaceVoxelList::iterator i);
00202
00203
00204 void setCenter (Vertex new_center) { center = new_center; }
00205
00206
00208 void setVoxelSize (double voxel_size)
00209 {
00210 if (voxel_size <= 0)
00211 throw ArgError("VoxelCube::setVoxelSize(): you can't set a voxel to a size <= 0.");
00212
00213 voxelSize = voxel_size;
00214 }
00215
00216 void makeMesh(float isoLevel, QEM::ProgMesh &mesh);
00217
00218 SurfaceVoxelList svl;
00219 private:
00220 ColorVertex calculateIntersection(ColVector< 3, unsigned int> &curOrigin, unsigned int nEdgeNo, float isoLevel, ColorMap &colorMap);
00221 ColoredSurfaceVoxelList svlColored;
00222 void constructColorMap (ColorMap &colorMap);
00223 Vertex center;
00224 double voxelSize;
00225 int voxelsPerSide;
00226
00227 typedef std::vector< bool > OpaqueLookupTable;
00228
00229
00230 OpaqueLookupTable solids;
00231 };