1 #include "float_math.h"
7 #pragma warning(disable:4786)
14 /*----------------------------------------------------------------------
15 Copyright (c) 2004 Open Dynamics Framework Group
19 Redistribution and use in source and binary forms, with or without modification, are permitted provided
20 that the following conditions are met:
22 Redistributions of source code must retain the above copyright notice, this list of conditions
23 and the following disclaimer.
25 Redistributions in binary form must reproduce the above copyright notice,
26 this list of conditions and the following disclaimer in the documentation
27 and/or other materials provided with the distribution.
29 Neither the name of the Open Dynamics Framework Group nor the names of its contributors may
30 be used to endorse or promote products derived from this software without specific prior written permission.
32 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 'AS IS' AND ANY EXPRESS OR IMPLIED WARRANTIES,
33 INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
34 DISCLAIMED. IN NO EVENT SHALL THE INTEL OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
35 EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
36 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
37 IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
38 THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
39 -----------------------------------------------------------------------*/
41 // http://codesuppository.blogspot.com
43 // mailto: jratcliff@infiniplex.net
45 // http://www.amillionpixels.us
48 // CodeSnippet provided by John W. Ratcliff
51 // mailto: jratcliff@infiniplex.net
53 // Personal website: http://jratcliffscarab.blogspot.com
54 // Coding Website: http://codesuppository.blogspot.com
55 // FundRaising Blog: http://amillionpixels.blogspot.com
56 // Fundraising site: http://www.amillionpixels.us
57 // New Temple Site: http://newtemple.blogspot.com
59 // This snippet shows how to 'hide' the complexity of
60 // the STL by wrapping some useful piece of functionality
61 // around a handful of discrete API calls.
63 // This API allows you to create an indexed triangle list
64 // from a collection of raw input triangles. Internally
65 // it uses an STL set to build the lookup table very rapidly.
67 // Here is how you would use it to build an indexed triangle
68 // list from a raw list of triangles.
70 // (1) create a 'VertexLookup' interface by calling
72 // VertexLook vl = Vl_createVertexLookup();
74 // (2) For each vertice in each triangle call:
76 // unsigned int i1 = Vl_getIndex(vl,p1);
77 // unsigned int i2 = Vl_getIndex(vl,p2);
78 // unsigned int i3 = Vl_getIndex(vl,p3);
80 // save the 3 indices into your triangle list array.
82 // (3) Get the vertex array by calling:
84 // const float *vertices = Vl_getVertices(vl);
86 // (4) Get the number of vertices so you can copy them into
88 // unsigned int vcount = Vl_getVcount(vl);
90 // (5) Release the VertexLookup interface when you are done with it.
91 // Vl_releaseVertexLookup(vl);
93 // Teaches the following lessons:
95 // How to wrap the complexity of STL and C++ classes around a
96 // simple API interface.
98 // How to use an STL set and custom comparator operator for
99 // a complex data type.
101 // How to create a template class.
103 // How to achieve significant performance improvements by
104 // taking advantage of built in STL containers in just
105 // a few lines of code.
107 // You could easily modify this code to support other vertex
108 // formats with any number of interpolants.
121 VertexPosition(void) { };
122 VertexPosition(const float *p)
129 void Set(int index,const float *pos)
131 const float * p = &pos[index*3];
139 float GetX(void) const { return mPos[0]; };
140 float GetY(void) const { return mPos[1]; };
141 float GetZ(void) const { return mPos[2]; };
146 typedef std::vector< VertexPosition > VertexVector;
150 VertexPosition mFind; // vertice to locate.
158 void SetSearch(const VertexPosition& match,VertexVector *list)
170 VertexID(int ID, Tracker* Tracker)
181 bool operator()(VertexID v1,VertexID v2) const;
184 const VertexPosition& Get(VertexID index) const
186 if ( index.mID == -1 ) return index.mTracker->mFind;
187 VertexVector &vlist = *index.mTracker->mList;
188 return vlist[index.mID];
192 template <class Type> class VertexPool
195 typedef std::set<VertexID, VertexLess > VertexSet;
196 typedef std::vector< Type > VertexVector;
198 int getVertex(const Type& vtx)
200 mTracker.SetSearch(vtx,&mVtxs);
201 VertexSet::iterator found;
202 found = mVertSet.find( VertexID(-1,&mTracker) );
203 if ( found != mVertSet.end() )
207 int idx = (int)mVtxs.size();
208 mVtxs.push_back( vtx );
209 mVertSet.insert( VertexID(idx,&mTracker) );
214 const float * GetPos(int idx) const
216 return mVtxs[idx].mPos;
219 const Type& Get(int idx) const
224 unsigned int GetSize(void) const
229 void Clear(int reservesize) // clear the vertice pool.
233 mVtxs.reserve(reservesize);
236 const VertexVector& GetVertexList(void) const { return mVtxs; };
238 void Set(const Type& vtx)
240 mVtxs.push_back(vtx);
243 unsigned int GetVertexCount(void) const
249 Type * getBuffer(void)
255 VertexSet mVertSet; // ordered list.
256 VertexVector mVtxs; // set of vertices.
261 bool VertexLess::operator()(VertexID v1,VertexID v2) const
264 const VertexPosition& a = Get(v1);
265 const VertexPosition& b = Get(v2);
267 int ixA = (int) (a.GetX()*10000.0f);
268 int ixB = (int) (b.GetX()*10000.0f);
270 if ( ixA < ixB ) return true;
271 if ( ixA > ixB ) return false;
273 int iyA = (int) (a.GetY()*10000.0f);
274 int iyB = (int) (b.GetY()*10000.0f);
276 if ( iyA < iyB ) return true;
277 if ( iyA > iyB ) return false;
279 int izA = (int) (a.GetZ()*10000.0f);
280 int izB = (int) (b.GetZ()*10000.0f);
282 if ( izA < izB ) return true;
283 if ( izA > izB ) return false;
294 using namespace Vlookup;
296 VertexLookup Vl_createVertexLookup(void)
298 VertexLookup ret = new VertexPool< VertexPosition >;
302 void Vl_releaseVertexLookup(VertexLookup vlook)
304 VertexPool< VertexPosition > *vp = (VertexPool< VertexPosition > *) vlook;
308 unsigned int Vl_getIndex(VertexLookup vlook,const float *pos) // get index.
310 VertexPool< VertexPosition > *vp = (VertexPool< VertexPosition > *) vlook;
311 VertexPosition p(pos);
312 return vp->getVertex(p);
315 const float * Vl_getVertices(VertexLookup vlook)
317 VertexPool< VertexPosition > *vp = (VertexPool< VertexPosition > *) vlook;
318 return vp->GetPos(0);
322 unsigned int Vl_getVcount(VertexLookup vlook)
324 VertexPool< VertexPosition > *vp = (VertexPool< VertexPosition > *) vlook;
325 return vp->GetVertexCount();