vlookup.cpp
Go to the documentation of this file.
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <string.h>
4 #include <assert.h>
5 
6 #pragma warning(disable:4786)
7 
8 #include <vector>
9 #include <map>
10 #include <set>
68 // CodeSnippet provided by John W. Ratcliff
69 // on March 23, 2006.
70 //
71 // mailto: jratcliff@infiniplex.net
72 //
73 // Personal website: http://jratcliffscarab.blogspot.com
74 // Coding Website: http://codesuppository.blogspot.com
75 // FundRaising Blog: http://amillionpixels.blogspot.com
76 // Fundraising site: http://www.amillionpixels.us
77 // New Temple Site: http://newtemple.blogspot.com
78 //
79 // This snippet shows how to 'hide' the complexity of
80 // the STL by wrapping some useful piece of functionality
81 // around a handful of discrete API calls.
82 //
83 // This API allows you to create an indexed triangle list
84 // from a collection of raw input triangles. Internally
85 // it uses an STL set to build the lookup table very rapidly.
86 //
87 // Here is how you would use it to build an indexed triangle
88 // list from a raw list of triangles.
89 //
90 // (1) create a 'VertexLookup' interface by calling
91 //
92 // VertexLook vl = Vl_createVertexLookup();
93 //
94 // (2) For each vertice in each triangle call:
95 //
96 // unsigned int i1 = Vl_getIndex(vl,p1);
97 // unsigned int i2 = Vl_getIndex(vl,p2);
98 // unsigned int i3 = Vl_getIndex(vl,p3);
99 //
100 // save the 3 indices into your triangle list array.
101 //
102 // (3) Get the vertex array by calling:
103 //
104 // const double *vertices = Vl_getVertices(vl);
105 //
106 // (4) Get the number of vertices so you can copy them into
107 // your own buffer.
108 // unsigned int vcount = Vl_getVcount(vl);
109 //
110 // (5) Release the VertexLookup interface when you are done with it.
111 // Vl_releaseVertexLookup(vl);
112 //
113 // Teaches the following lessons:
114 //
115 // How to wrap the complexity of STL and C++ classes around a
116 // simple API interface.
117 //
118 // How to use an STL set and custom comparator operator for
119 // a complex data type.
120 //
121 // How to create a template class.
122 //
123 // How to achieve significant performance improvements by
124 // taking advantage of built in STL containers in just
125 // a few lines of code.
126 //
127 // You could easily modify this code to support other vertex
128 // formats with any number of interpolants.
129 
130 
131 
132 
133 #include "vlookup.h"
134 
135 namespace ConvexDecomposition
136 {
137 
139 {
140 public:
141  VertexPosition(void) { };
142  VertexPosition(const double *p)
143  {
144  mPos[0] = p[0];
145  mPos[1] = p[1];
146  mPos[2] = p[2];
147  };
148 
149  void Set(int index,const double *pos)
150  {
151  const double * p = &pos[index*3];
152 
153  mPos[0] = p[0];
154  mPos[1] = p[1];
155  mPos[2] = p[2];
156 
157  };
158 
159  double GetX(void) const { return mPos[0]; };
160  double GetY(void) const { return mPos[1]; };
161  double GetZ(void) const { return mPos[2]; };
162 
163  double mPos[3];
164 };
165 
166 
167 template <typename Type> class VertexLess
168 {
169 public:
170  typedef std::vector< Type > VertexVector;
171 
172  bool operator()(int v1,int v2) const;
173 
174  static void SetSearch(const Type& match,VertexVector *list)
175  {
176  mFind = match;
177  mList = list;
178  };
179 
180 private:
181  const Type& Get(int index) const
182  {
183  if ( index == -1 ) return mFind;
184  VertexVector &vlist = *mList;
185  return vlist[index];
186  }
187  static Type mFind; // vertice to locate.
189 };
190 
191 template <typename Type> class VertexPool
192 {
193 public:
194  typedef std::set<int, VertexLess<Type> > VertexSet;
195  typedef std::vector< Type > VertexVector;
196 
197  int GetVertex(const Type& vtx)
198  {
200  typename VertexSet::iterator found;
201  found = mVertSet.find( -1 );
202  if ( found != mVertSet.end() )
203  {
204  return *found;
205  }
206  int idx = (int)mVtxs.size();
207  mVtxs.push_back( vtx );
208  mVertSet.insert( idx );
209  return idx;
210  };
211 
212  const double * GetPos(int idx) const
213  {
214  return mVtxs[idx].mPos;
215  }
216 
217  const Type& Get(int idx) const
218  {
219  return mVtxs[idx];
220  };
221 
222  unsigned int GetSize(void) const
223  {
224  return mVtxs.size();
225  };
226 
227  void Clear(int reservesize) // clear the vertice pool.
228  {
229  mVertSet.clear();
230  mVtxs.clear();
231  mVtxs.reserve(reservesize);
232  };
233 
234  const VertexVector& GetVertexList(void) const { return mVtxs; };
235 
236  void Set(const Type& vtx)
237  {
238  mVtxs.push_back(vtx);
239  }
240 
241  unsigned int GetVertexCount(void) const
242  {
243  return mVtxs.size();
244  };
245 
246 
247  Type * GetBuffer(void)
248  {
249  return &mVtxs[0];
250  };
251 
252 private:
253  VertexSet mVertSet; // ordered list.
254  VertexVector mVtxs; // set of vertices.
255 };
256 
257 double tmpp[3] = {0,0,0};
259 template<> std::vector<VertexPosition > *VertexLess<VertexPosition>::mList =0;
260 
261 enum RDIFF
262 {
266 };
267 
268 static RDIFF relativeDiff(const double *a,const double *b,double magnitude)
269 {
270  RDIFF ret = RD_EQUAL;
271 
272  double m2 = magnitude*magnitude;
273  double dx = a[0]-b[0];
274  double dy = a[1]-b[1];
275  double dz = a[2]-b[2];
276  double d2 = (dx*dx)+(dy*dy)+(dz*dz);
277 
278  if ( d2 > m2 )
279  {
280  if ( a[0] < b[0] ) ret = RD_LESS;
281  else if ( a[0] > b[0] ) ret = RD_GREATER;
282  else if ( a[1] < b[1] ) ret = RD_LESS;
283  else if ( a[1] > b[1] ) ret = RD_GREATER;
284  else if ( a[2] < b[2] ) ret = RD_LESS;
285  else if ( a[2] > b[2] ) ret = RD_GREATER;
286  }
287  return ret;
288 }
289 
290 
291 template<>
292 bool VertexLess<VertexPosition>::operator()(int v1,int v2) const
293 {
294  bool ret = false;
295 
296  const VertexPosition& a = Get(v1);
297  const VertexPosition& b = Get(v2);
298 
299  RDIFF d = relativeDiff(a.mPos,b.mPos,0.0001f);
300  if ( d == RD_LESS ) ret = true;
301 
302  return ret;
303 
304 };
305 
306 
307 
309 {
311  return ret;
312 }
313 
315 {
317  delete vp;
318 }
319 
320 unsigned int Vl_getIndex(VertexLookup vlook,const double *pos) // get index.
321 {
323  VertexPosition p(pos);
324  return vp->GetVertex(p);
325 }
326 
327 const double * Vl_getVertices(VertexLookup vlook)
328 {
330  return vp->GetPos(0);
331 }
332 
333 
334 unsigned int Vl_getVcount(VertexLookup vlook)
335 {
337  return vp->GetVertexCount();
338 }
339 
340 }; // end of namespace
ConvexDecomposition::VertexLess::mList
static VertexVector * mList
Definition: vlookup.cpp:188
ConvexDecomposition::VertexPool::GetVertex
int GetVertex(const Type &vtx)
Definition: vlookup.cpp:197
ConvexDecomposition::VertexPool::Set
void Set(const Type &vtx)
Definition: vlookup.cpp:236
ConvexDecomposition::VertexPool::mVertSet
VertexSet mVertSet
Definition: vlookup.cpp:250
ConvexDecomposition::VertexLess
Definition: vlookup.cpp:167
ConvexDecomposition::VertexPosition::Set
void Set(int index, const double *pos)
Definition: vlookup.cpp:149
ConvexDecomposition::VertexPosition::GetX
double GetX(void) const
Definition: vlookup.cpp:159
ConvexDecomposition::VertexPool::mVtxs
VertexVector mVtxs
Definition: vlookup.cpp:254
ConvexDecomposition::VertexPosition::VertexPosition
VertexPosition(const double *p)
Definition: vlookup.cpp:142
ConvexDecomposition::VertexLess::operator()
bool operator()(int v1, int v2) const
ConvexDecomposition::VertexPool::VertexVector
std::vector< Type > VertexVector
Definition: vlookup.cpp:195
ConvexDecomposition::VertexPool::GetVertexList
const VertexVector & GetVertexList(void) const
Definition: vlookup.cpp:234
ConvexDecomposition
Definition: bestfit.cpp:75
ConvexDecomposition::VertexPool
Definition: vlookup.cpp:191
ConvexDecomposition::VertexPosition::VertexPosition
VertexPosition(void)
Definition: vlookup.cpp:141
ConvexDecomposition::VertexPool::Get
const Type & Get(int idx) const
Definition: vlookup.cpp:217
ConvexDecomposition::Vl_releaseVertexLookup
void Vl_releaseVertexLookup(VertexLookup vlook)
Definition: vlookup.cpp:314
ConvexDecomposition::Vl_createVertexLookup
VertexLookup Vl_createVertexLookup(void)
Definition: vlookup.cpp:308
ConvexDecomposition::VertexLess::mFind
static Type mFind
Definition: vlookup.cpp:187
ConvexDecomposition::RD_GREATER
@ RD_GREATER
Definition: vlookup.cpp:265
ConvexDecomposition::VertexPosition::mPos
double mPos[3]
Definition: vlookup.cpp:161
ConvexDecomposition::VertexPool::GetVertexCount
unsigned int GetVertexCount(void) const
Definition: vlookup.cpp:241
ConvexDecomposition::VertexPosition
Definition: vlookup.cpp:138
ConvexDecomposition::RD_LESS
@ RD_LESS
Definition: vlookup.cpp:264
ConvexDecomposition::VertexLess::VertexVector
std::vector< Type > VertexVector
Definition: vlookup.cpp:170
ConvexDecomposition::RDIFF
RDIFF
Definition: vlookup.cpp:261
ConvexDecomposition::VertexLess::SetSearch
static void SetSearch(const Type &match, VertexVector *list)
Definition: vlookup.cpp:174
ConvexDecomposition::Vl_getVertices
const double * Vl_getVertices(VertexLookup vlook)
Definition: vlookup.cpp:327
ConvexDecomposition::VertexPool::GetPos
const double * GetPos(int idx) const
Definition: vlookup.cpp:212
ConvexDecomposition::Vl_getIndex
unsigned int Vl_getIndex(VertexLookup vlook, const double *pos)
Definition: vlookup.cpp:320
ConvexDecomposition::VertexLookup
void * VertexLookup
Definition: vlookup.h:130
ConvexDecomposition::VertexPool::GetSize
unsigned int GetSize(void) const
Definition: vlookup.cpp:222
ConvexDecomposition::VertexPosition::GetY
double GetY(void) const
Definition: vlookup.cpp:160
ConvexDecomposition::RD_EQUAL
@ RD_EQUAL
Definition: vlookup.cpp:263
ConvexDecomposition::tmpp
double tmpp[3]
Definition: vlookup.cpp:257
ConvexDecomposition::VertexPool::Clear
void Clear(int reservesize)
Definition: vlookup.cpp:227
ConvexDecomposition::relativeDiff
static RDIFF relativeDiff(const double *a, const double *b, double magnitude)
Definition: vlookup.cpp:268
ConvexDecomposition::Vl_getVcount
unsigned int Vl_getVcount(VertexLookup vlook)
Definition: vlookup.cpp:334
ConvexDecomposition::VertexLess::Get
const Type & Get(int index) const
Definition: vlookup.cpp:181
ConvexDecomposition::VertexPool::GetBuffer
Type * GetBuffer(void)
Definition: vlookup.cpp:247
ConvexDecomposition::magnitude
double magnitude(const double3 &v)
Definition: cd_hull.cpp:724
ConvexDecomposition::VertexPosition::GetZ
double GetZ(void) const
Definition: vlookup.cpp:161
vlookup.h
ConvexDecomposition::VertexPool::VertexSet
std::set< int, VertexLess< Type > > VertexSet
Definition: vlookup.cpp:194


convex_decomposition
Author(s): John W. Ratcliff
autogenerated on Wed Mar 2 2022 00:04:59