Visualization Library

A lightweight C++ OpenGL middleware for 2D/3D graphics
[Home] [Tutorials] [All Classes] [Grouped Classes]

X:/dropbox/visualizationlibrary/src/vlGraphics/TriangleStripGenerator.cpp

Go to the documentation of this file.
00001 /**************************************************************************************/
00002 /*                                                                                    */
00003 /*  Visualization Library                                                             */
00004 /*  http://www.visualizationlibrary.org                                               */
00005 /*                                                                                    */
00006 /*  Copyright (c) 2005-2010, Michele Bosi                                             */
00007 /*  All rights reserved.                                                              */
00008 /*                                                                                    */
00009 /*  Redistribution and use in source and binary forms, with or without modification,  */
00010 /*  are permitted provided that the following conditions are met:                     */
00011 /*                                                                                    */
00012 /*  - Redistributions of source code must retain the above copyright notice, this     */
00013 /*  list of conditions and the following disclaimer.                                  */
00014 /*                                                                                    */
00015 /*  - Redistributions in binary form must reproduce the above copyright notice, this  */
00016 /*  list of conditions and the following disclaimer in the documentation and/or       */
00017 /*  other materials provided with the distribution.                                   */
00018 /*                                                                                    */
00019 /*  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND   */
00020 /*  ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED     */
00021 /*  WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE            */
00022 /*  DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR  */
00023 /*  ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES    */
00024 /*  (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;      */
00025 /*  LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON    */
00026 /*  ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT           */
00027 /*  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS     */
00028 /*  SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.                      */
00029 /*                                                                                    */
00030 /**************************************************************************************/
00031 
00032 #include "../3rdparty/tristripper/tri_stripper.cpp"
00033 #include "../3rdparty/tristripper/policy.cpp"
00034 #include "../3rdparty/tristripper/connectivity_graph.cpp"
00035 
00036 using namespace triangle_stripper;
00037 
00038 #include <vlGraphics/TriangleStripGenerator.hpp>
00039 #include <vlGraphics/Geometry.hpp>
00040 #include <vlGraphics/DoubleVertexRemover.hpp>
00041 #include <vlCore/Log.hpp>
00042 #include <vlCore/Say.hpp>
00043 
00044 using namespace vl;
00045 
00046 namespace
00047 {
00048   void fillIndices(std::vector<unsigned int>& indices, const DrawCall* dc, bool substitute_quads)
00049   {
00050     indices.clear();
00051 
00052     if ( dc->primitiveType() == PT_QUADS && !substitute_quads )
00053       return;
00054 
00055     indices.reserve( 1000 );
00056 
00057     for(TriangleIterator trit = dc->triangleIterator(); trit.hasNext(); trit.next())
00058     {
00059       int a = trit.a();
00060       int b = trit.b();
00061       int c = trit.c();
00062       // skip degenerate triangles
00063       if (a != b && b != c)
00064       {
00065         indices.push_back(a);
00066         indices.push_back(b);
00067         indices.push_back(c);
00068       }
00069     }
00070   }
00071 }
00072 
00073 void TriangleStripGenerator::stripfy(Geometry* geom, int cache_size, bool merge_strips, bool remove_doubles, bool substitute_quads)
00074 {
00075   if (remove_doubles)
00076   {
00077     DoubleVertexRemover dvr;
00078     dvr.removeDoubles(geom);
00079   }
00080 
00081   for( int idraw=geom->drawCalls()->size(); idraw--; )
00082   {
00083     DrawCall* dc = geom->drawCalls()->at(idraw);
00084 
00085     triangle_stripper::indices indices;
00086 
00087     fillIndices(indices, dc, substitute_quads);
00088 
00089     std::vector<unsigned int> algo2_strip;
00090     std::vector<unsigned int> algo2_tris;
00091     // stripfyAlgo2(algo2_strip, algo2_tris, indices, cache_size);
00092 
00093     tri_stripper striper(indices);
00094     striper.SetCacheSize(cache_size);
00095     striper.SetMinStripSize(4);
00096     primitive_vector out;
00097     striper.Strip(&out);
00098 
00099     // install new strip
00100     if (out.size())
00101     {
00102       geom->drawCalls()->erase(idraw,1);
00103       algo2_strip.reserve(indices.size());
00104       for(unsigned s=0; s<out.size(); ++s)
00105       {
00106         if (out[s].Type == TRIANGLE_STRIP)
00107         {
00108           algo2_strip.clear();
00109           for(unsigned p=0; p<out[s].Indices.size(); ++p)
00110             algo2_strip.push_back(out[s].Indices[p]);
00111 
00112           ref<DrawElementsUInt> draw_elems = new DrawElementsUInt(PT_TRIANGLE_STRIP);
00113           draw_elems->indexBuffer()->resize(algo2_strip.size());
00114           memcpy(draw_elems->indexBuffer()->ptr(), &algo2_strip[0], sizeof(unsigned int)*algo2_strip.size());
00115           geom->drawCalls()->push_back(draw_elems.get());
00116         }
00117         else // TRIANGLES
00118         {
00119           algo2_tris.clear();
00120           for(unsigned p=0; p<out[s].Indices.size(); ++p)
00121             algo2_tris.push_back(out[s].Indices[p]);
00122 
00123           ref<DrawElementsUInt> draw_elems = new DrawElementsUInt(PT_TRIANGLES);
00124           draw_elems->indexBuffer()->resize(algo2_tris.size());
00125           memcpy(draw_elems->indexBuffer()->ptr(), &algo2_tris[0], sizeof(unsigned int)*algo2_tris.size());
00126           geom->drawCalls()->push_back(draw_elems.get());
00127         }
00128       }
00129     }
00130   }
00131 
00132   if (merge_strips)
00133     geom->mergeTriangleStrips();
00134 }
00135 

Visualization Library 2011.09.1160 Reference Documentation
Copyright 2005-2011 Michele Bosi. All rights reserved.
Updated on Thu May 2 2013 13:40:42.
Permission is granted to use this page to write and publish articles regarding Visualization Library.