00001 #include <stdio.h>
00002 #include <stdlib.h>
00003 #include <string.h>
00004 #include <assert.h>
00005
00006 #pragma warning(disable:4786)
00007
00008 #include <vector>
00009 #include <map>
00010 #include <set>
00068
00069
00070
00071
00072
00073
00074
00075
00076
00077
00078
00079
00080
00081
00082
00083
00084
00085
00086
00087
00088
00089
00090
00091
00092
00093
00094
00095
00096
00097
00098
00099
00100
00101
00102
00103
00104
00105
00106
00107
00108
00109
00110
00111
00112
00113
00114
00115
00116
00117
00118
00119
00120
00121
00122
00123
00124
00125
00126
00127
00128
00129
00130
00131
00132
00133 #include "vlookup.h"
00134
00135 namespace ConvexDecomposition
00136 {
00137
00138 class VertexPosition
00139 {
00140 public:
00141 VertexPosition(void) { };
00142 VertexPosition(const double *p)
00143 {
00144 mPos[0] = p[0];
00145 mPos[1] = p[1];
00146 mPos[2] = p[2];
00147 };
00148
00149 void Set(int index,const double *pos)
00150 {
00151 const double * p = &pos[index*3];
00152
00153 mPos[0] = p[0];
00154 mPos[1] = p[1];
00155 mPos[2] = p[2];
00156
00157 };
00158
00159 double GetX(void) const { return mPos[0]; };
00160 double GetY(void) const { return mPos[1]; };
00161 double GetZ(void) const { return mPos[2]; };
00162
00163 double mPos[3];
00164 };
00165
00166
00167 template <typename Type> class VertexLess
00168 {
00169 public:
00170 typedef std::vector< Type > VertexVector;
00171
00172 bool operator()(int v1,int v2) const;
00173
00174 static void SetSearch(const Type& match,VertexVector *list)
00175 {
00176 mFind = match;
00177 mList = list;
00178 };
00179
00180 private:
00181 const Type& Get(int index) const
00182 {
00183 if ( index == -1 ) return mFind;
00184 VertexVector &vlist = *mList;
00185 return vlist[index];
00186 }
00187 static Type mFind;
00188 static VertexVector *mList;
00189 };
00190
00191 template <typename Type> class VertexPool
00192 {
00193 public:
00194 typedef std::set<int, VertexLess<Type> > VertexSet;
00195 typedef std::vector< Type > VertexVector;
00196
00197 int GetVertex(const Type& vtx)
00198 {
00199 VertexLess<Type>::SetSearch(vtx,&mVtxs);
00200 typename VertexSet::iterator found;
00201 found = mVertSet.find( -1 );
00202 if ( found != mVertSet.end() )
00203 {
00204 return *found;
00205 }
00206 int idx = (int)mVtxs.size();
00207 mVtxs.push_back( vtx );
00208 mVertSet.insert( idx );
00209 return idx;
00210 };
00211
00212 const double * GetPos(int idx) const
00213 {
00214 return mVtxs[idx].mPos;
00215 }
00216
00217 const Type& Get(int idx) const
00218 {
00219 return mVtxs[idx];
00220 };
00221
00222 unsigned int GetSize(void) const
00223 {
00224 return mVtxs.size();
00225 };
00226
00227 void Clear(int reservesize)
00228 {
00229 mVertSet.clear();
00230 mVtxs.clear();
00231 mVtxs.reserve(reservesize);
00232 };
00233
00234 const VertexVector& GetVertexList(void) const { return mVtxs; };
00235
00236 void Set(const Type& vtx)
00237 {
00238 mVtxs.push_back(vtx);
00239 }
00240
00241 unsigned int GetVertexCount(void) const
00242 {
00243 return mVtxs.size();
00244 };
00245
00246
00247 Type * GetBuffer(void)
00248 {
00249 return &mVtxs[0];
00250 };
00251
00252 private:
00253 VertexSet mVertSet;
00254 VertexVector mVtxs;
00255 };
00256
00257 double tmpp[3] = {0,0,0};
00258 template<> VertexPosition VertexLess<VertexPosition>::mFind = tmpp;
00259 template<> std::vector<VertexPosition > *VertexLess<VertexPosition>::mList =0;
00260
00261 enum RDIFF
00262 {
00263 RD_EQUAL,
00264 RD_LESS,
00265 RD_GREATER
00266 };
00267
00268 static RDIFF relativeDiff(const double *a,const double *b,double magnitude)
00269 {
00270 RDIFF ret = RD_EQUAL;
00271
00272 double m2 = magnitude*magnitude;
00273 double dx = a[0]-b[0];
00274 double dy = a[1]-b[1];
00275 double dz = a[2]-b[2];
00276 double d2 = (dx*dx)+(dy*dy)+(dz*dz);
00277
00278 if ( d2 > m2 )
00279 {
00280 if ( a[0] < b[0] ) ret = RD_LESS;
00281 else if ( a[0] > b[0] ) ret = RD_GREATER;
00282 else if ( a[1] < b[1] ) ret = RD_LESS;
00283 else if ( a[1] > b[1] ) ret = RD_GREATER;
00284 else if ( a[2] < b[2] ) ret = RD_LESS;
00285 else if ( a[2] > b[2] ) ret = RD_GREATER;
00286 }
00287 return ret;
00288 }
00289
00290
00291 template<>
00292 bool VertexLess<VertexPosition>::operator()(int v1,int v2) const
00293 {
00294 bool ret = false;
00295
00296 const VertexPosition& a = Get(v1);
00297 const VertexPosition& b = Get(v2);
00298
00299 RDIFF d = relativeDiff(a.mPos,b.mPos,0.0001f);
00300 if ( d == RD_LESS ) ret = true;
00301
00302 return ret;
00303
00304 };
00305
00306
00307
00308 VertexLookup Vl_createVertexLookup(void)
00309 {
00310 VertexLookup ret = new VertexPool< VertexPosition >;
00311 return ret;
00312 }
00313
00314 void Vl_releaseVertexLookup(VertexLookup vlook)
00315 {
00316 VertexPool< VertexPosition > *vp = (VertexPool< VertexPosition > *) vlook;
00317 delete vp;
00318 }
00319
00320 unsigned int Vl_getIndex(VertexLookup vlook,const double *pos)
00321 {
00322 VertexPool< VertexPosition > *vp = (VertexPool< VertexPosition > *) vlook;
00323 VertexPosition p(pos);
00324 return vp->GetVertex(p);
00325 }
00326
00327 const double * Vl_getVertices(VertexLookup vlook)
00328 {
00329 VertexPool< VertexPosition > *vp = (VertexPool< VertexPosition > *) vlook;
00330 return vp->GetPos(0);
00331 }
00332
00333
00334 unsigned int Vl_getVcount(VertexLookup vlook)
00335 {
00336 VertexPool< VertexPosition > *vp = (VertexPool< VertexPosition > *) vlook;
00337 return vp->GetVertexCount();
00338 }
00339
00340 };