vlookup.cpp 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326
  1. #include "float_math.h"
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <string.h>
  5. #include <assert.h>
  6. #pragma warning(disable:4786)
  7. #include <vector>
  8. #include <map>
  9. #include <set>
  10. /*----------------------------------------------------------------------
  11. Copyright (c) 2004 Open Dynamics Framework Group
  12. www.physicstools.org
  13. All rights reserved.
  14. Redistribution and use in source and binary forms, with or without modification, are permitted provided
  15. that the following conditions are met:
  16. Redistributions of source code must retain the above copyright notice, this list of conditions
  17. and the following disclaimer.
  18. Redistributions in binary form must reproduce the above copyright notice,
  19. this list of conditions and the following disclaimer in the documentation
  20. and/or other materials provided with the distribution.
  21. Neither the name of the Open Dynamics Framework Group nor the names of its contributors may
  22. be used to endorse or promote products derived from this software without specific prior written permission.
  23. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 'AS IS' AND ANY EXPRESS OR IMPLIED WARRANTIES,
  24. INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  25. DISCLAIMED. IN NO EVENT SHALL THE INTEL OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  26. EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  27. LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
  28. IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  29. THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  30. -----------------------------------------------------------------------*/
  31. // http://codesuppository.blogspot.com
  32. //
  33. // mailto: [email protected]
  34. //
  35. // http://www.amillionpixels.us
  36. //
  37. // CodeSnippet provided by John W. Ratcliff
  38. // on March 23, 2006.
  39. //
  40. // mailto: [email protected]
  41. //
  42. // Personal website: http://jratcliffscarab.blogspot.com
  43. // Coding Website: http://codesuppository.blogspot.com
  44. // FundRaising Blog: http://amillionpixels.blogspot.com
  45. // Fundraising site: http://www.amillionpixels.us
  46. // New Temple Site: http://newtemple.blogspot.com
  47. //
  48. // This snippet shows how to 'hide' the complexity of
  49. // the STL by wrapping some useful piece of functionality
  50. // around a handful of discrete API calls.
  51. //
  52. // This API allows you to create an indexed triangle list
  53. // from a collection of raw input triangles. Internally
  54. // it uses an STL set to build the lookup table very rapidly.
  55. //
  56. // Here is how you would use it to build an indexed triangle
  57. // list from a raw list of triangles.
  58. //
  59. // (1) create a 'VertexLookup' interface by calling
  60. //
  61. // VertexLook vl = Vl_createVertexLookup();
  62. //
  63. // (2) For each vertice in each triangle call:
  64. //
  65. // unsigned int i1 = Vl_getIndex(vl,p1);
  66. // unsigned int i2 = Vl_getIndex(vl,p2);
  67. // unsigned int i3 = Vl_getIndex(vl,p3);
  68. //
  69. // save the 3 indices into your triangle list array.
  70. //
  71. // (3) Get the vertex array by calling:
  72. //
  73. // const float *vertices = Vl_getVertices(vl);
  74. //
  75. // (4) Get the number of vertices so you can copy them into
  76. // your own buffer.
  77. // unsigned int vcount = Vl_getVcount(vl);
  78. //
  79. // (5) Release the VertexLookup interface when you are done with it.
  80. // Vl_releaseVertexLookup(vl);
  81. //
  82. // Teaches the following lessons:
  83. //
  84. // How to wrap the complexity of STL and C++ classes around a
  85. // simple API interface.
  86. //
  87. // How to use an STL set and custom comparator operator for
  88. // a complex data type.
  89. //
  90. // How to create a template class.
  91. //
  92. // How to achieve significant performance improvements by
  93. // taking advantage of built in STL containers in just
  94. // a few lines of code.
  95. //
  96. // You could easily modify this code to support other vertex
  97. // formats with any number of interpolants.
  98. #include "vlookup.h"
  99. namespace Vlookup
  100. {
  101. class VertexPosition
  102. {
  103. public:
  104. VertexPosition(void) { };
  105. VertexPosition(const float *p)
  106. {
  107. mPos[0] = p[0];
  108. mPos[1] = p[1];
  109. mPos[2] = p[2];
  110. };
  111. void Set(int index,const float *pos)
  112. {
  113. const float * p = &pos[index*3];
  114. mPos[0] = p[0];
  115. mPos[1] = p[1];
  116. mPos[2] = p[2];
  117. };
  118. float GetX(void) const { return mPos[0]; };
  119. float GetY(void) const { return mPos[1]; };
  120. float GetZ(void) const { return mPos[2]; };
  121. float mPos[3];
  122. };
  123. typedef std::vector< VertexPosition > VertexVector;
  124. struct Tracker
  125. {
  126. VertexPosition mFind; // vertice to locate.
  127. VertexVector *mList;
  128. Tracker()
  129. {
  130. mList = 0;
  131. }
  132. void SetSearch(const VertexPosition& match,VertexVector *list)
  133. {
  134. mFind = match;
  135. mList = list;
  136. };
  137. };
  138. struct VertexID
  139. {
  140. int mID;
  141. Tracker* mTracker;
  142. VertexID(int ID, Tracker* Tracker)
  143. {
  144. mID = ID;
  145. mTracker = Tracker;
  146. }
  147. };
  148. class VertexLess
  149. {
  150. public:
  151. bool operator()(VertexID v1,VertexID v2) const;
  152. private:
  153. const VertexPosition& Get(VertexID index) const
  154. {
  155. if ( index.mID == -1 ) return index.mTracker->mFind;
  156. VertexVector &vlist = *index.mTracker->mList;
  157. return vlist[index.mID];
  158. }
  159. };
  160. template <class Type> class VertexPool
  161. {
  162. public:
  163. typedef std::set<VertexID, VertexLess > VertexSet;
  164. typedef std::vector< Type > VertexVector;
  165. int getVertex(const Type& vtx)
  166. {
  167. mTracker.SetSearch(vtx,&mVtxs);
  168. VertexSet::iterator found;
  169. found = mVertSet.find( VertexID(-1,&mTracker) );
  170. if ( found != mVertSet.end() )
  171. {
  172. return found->mID;
  173. }
  174. int idx = (int)mVtxs.size();
  175. mVtxs.push_back( vtx );
  176. mVertSet.insert( VertexID(idx,&mTracker) );
  177. return idx;
  178. };
  179. const float * GetPos(int idx) const
  180. {
  181. return mVtxs[idx].mPos;
  182. }
  183. const Type& Get(int idx) const
  184. {
  185. return mVtxs[idx];
  186. };
  187. unsigned int GetSize(void) const
  188. {
  189. return mVtxs.size();
  190. };
  191. void Clear(int reservesize) // clear the vertice pool.
  192. {
  193. mVertSet.clear();
  194. mVtxs.clear();
  195. mVtxs.reserve(reservesize);
  196. };
  197. const VertexVector& GetVertexList(void) const { return mVtxs; };
  198. void Set(const Type& vtx)
  199. {
  200. mVtxs.push_back(vtx);
  201. }
  202. unsigned int GetVertexCount(void) const
  203. {
  204. return mVtxs.size();
  205. };
  206. Type * getBuffer(void)
  207. {
  208. return &mVtxs[0];
  209. };
  210. private:
  211. VertexSet mVertSet; // ordered list.
  212. VertexVector mVtxs; // set of vertices.
  213. Tracker mTracker;
  214. };
  215. bool VertexLess::operator()(VertexID v1,VertexID v2) const
  216. {
  217. const VertexPosition& a = Get(v1);
  218. const VertexPosition& b = Get(v2);
  219. int ixA = (int) (a.GetX()*10000.0f);
  220. int ixB = (int) (b.GetX()*10000.0f);
  221. if ( ixA < ixB ) return true;
  222. if ( ixA > ixB ) return false;
  223. int iyA = (int) (a.GetY()*10000.0f);
  224. int iyB = (int) (b.GetY()*10000.0f);
  225. if ( iyA < iyB ) return true;
  226. if ( iyA > iyB ) return false;
  227. int izA = (int) (a.GetZ()*10000.0f);
  228. int izB = (int) (b.GetZ()*10000.0f);
  229. if ( izA < izB ) return true;
  230. if ( izA > izB ) return false;
  231. return false;
  232. }
  233. }
  234. using namespace Vlookup;
  235. VertexLookup Vl_createVertexLookup(void)
  236. {
  237. VertexLookup ret = new VertexPool< VertexPosition >;
  238. return ret;
  239. }
  240. void Vl_releaseVertexLookup(VertexLookup vlook)
  241. {
  242. VertexPool< VertexPosition > *vp = (VertexPool< VertexPosition > *) vlook;
  243. delete vp;
  244. }
  245. unsigned int Vl_getIndex(VertexLookup vlook,const float *pos) // get index.
  246. {
  247. VertexPool< VertexPosition > *vp = (VertexPool< VertexPosition > *) vlook;
  248. VertexPosition p(pos);
  249. return vp->getVertex(p);
  250. }
  251. const float * Vl_getVertices(VertexLookup vlook)
  252. {
  253. VertexPool< VertexPosition > *vp = (VertexPool< VertexPosition > *) vlook;
  254. return vp->GetPos(0);
  255. }
  256. unsigned int Vl_getVcount(VertexLookup vlook)
  257. {
  258. VertexPool< VertexPosition > *vp = (VertexPool< VertexPosition > *) vlook;
  259. return vp->GetVertexCount();
  260. }