# Visualization Library

A lightweight C++ OpenGL middleware for 2D/3D graphics

[Home] [Tutorials] [All Classes] [Grouped Classes]

- Main Page
- Namespaces
- Classes
- Files

- File List
- File Members

# D:/VL/BuildSystem/VL_DISTRO/Visualization_Library_SDK/src/vlGraphics/DoubleVertexRemover.cpp

Go to the documentation of this file.

    00001 /**************************************************************************************/
    00002 /*                                                                                    */
    00003 /*  Visualization Library                                                             */
    00004 /*  http://www.visualizationlibrary.com                                               */
    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 <vlGraphics/DoubleVertexRemover.hpp>
    00033 #include <vlCore/Time.hpp>
    00034
    00035 using namespace vl;
    00036
    00037 //-----------------------------------------------------------------------------
    00038 template<class T>
    00039 ref<ArrayAbstract> VertexMapper::regenerateT(ArrayAbstract* data, const std::vector<size_t>& map_new_to_old) const
    00040 {
    00041   ref<T> in_data = dynamic_cast<T*>(data);
    00042   if (in_data)
    00043   {
    00044     ref<T> out_data = new T;
    00045     out_data->resize(map_new_to_old.size());
    00046     for(unsigned i=0; i<map_new_to_old.size(); ++i)
    00047         out_data->at(i) = in_data->at(map_new_to_old[i]);
    00048     return out_data;
    00049   }
    00050   return NULL;
    00051 }
    00052 //-----------------------------------------------------------------------------
    00053 ref<ArrayAbstract> VertexMapper::regenerate(ArrayAbstract* data, const std::vector<size_t>& map_new_to_old) const
    00054 {
    00055   ref<ArrayAbstract> out_data;
    00056
    00057   if ( (out_data = regenerateT<ArrayInt4>(data, map_new_to_old)) )
    00058     return out_data;
    00059   else
    00060   if ( (out_data = regenerateT<ArrayInt3>(data, map_new_to_old)) )
    00061     return out_data;
    00062   else
    00063   if ( (out_data = regenerateT<ArrayInt2>(data, map_new_to_old)) )
    00064     return out_data;
    00065   else
    00066   if ( (out_data = regenerateT<ArrayUInt4>(data, map_new_to_old)) )
    00067     return out_data;
    00068   else
    00069   if ( (out_data = regenerateT<ArrayUInt3>(data, map_new_to_old)) )
    00070     return out_data;
    00071   else
    00072   if ( (out_data = regenerateT<ArrayUInt2>(data, map_new_to_old)) )
    00073     return out_data;
    00074   else
    00075   if ( (out_data = regenerateT<ArrayFloat4>(data, map_new_to_old)) )
    00076     return out_data;
    00077   else
    00078   if ( (out_data = regenerateT<ArrayFloat3>(data, map_new_to_old)) )
    00079     return out_data;
    00080   else
    00081   if ( (out_data = regenerateT<ArrayFloat2>(data, map_new_to_old)) )
    00082     return out_data;
    00083   else
    00084   if ( (out_data = regenerateT<ArrayDouble4>(data, map_new_to_old)) )
    00085     return out_data;
    00086   else
    00087   if ( (out_data = regenerateT<ArrayDouble3>(data, map_new_to_old)) )
    00088     return out_data;
    00089   else
    00090   if ( (out_data = regenerateT<ArrayDouble2>(data, map_new_to_old)) )
    00091     return out_data;
    00092   else
    00093   if ( (out_data = regenerateT<ArrayFloat1>(data, map_new_to_old)) )
    00094     return out_data;
    00095   else
    00096   if ( (out_data = regenerateT<ArrayDouble1>(data, map_new_to_old)) )
    00097     return out_data;
    00098   else
    00099   if ( (out_data = regenerateT<ArrayUInt1>(data, map_new_to_old)) )
    00100     return out_data;
    00101   else
    00102   if ( (out_data = regenerateT<ArrayInt1>(data, map_new_to_old)) )
    00103     return out_data;
    00104   else
    00105   if ( (out_data = regenerateT<ArrayByte1>(data, map_new_to_old)) )
    00106     return out_data;
    00107   else
    00108   if ( (out_data = regenerateT<ArrayShort1>(data, map_new_to_old)) )
    00109     return out_data;
    00110   else
    00111   if ( (out_data = regenerateT<ArrayUByte1>(data, map_new_to_old)) )
    00112     return out_data;
    00113   else
    00114   if ( (out_data = regenerateT<ArrayUShort1>(data, map_new_to_old)) )
    00115     return out_data;
    00116   else
    00117   if ( (out_data = regenerateT<ArrayUByte2>(data, map_new_to_old)) )
    00118     return out_data;
    00119   else
    00120   if ( (out_data = regenerateT<ArrayUByte3>(data, map_new_to_old)) )
    00121     return out_data;
    00122   else
    00123   if ( (out_data = regenerateT<ArrayUByte4>(data, map_new_to_old)) )
    00124     return out_data;
    00125   else
    00126   if ( (out_data = regenerateT<ArrayByte2>(data, map_new_to_old)) )
    00127     return out_data;
    00128   else
    00129   if ( (out_data = regenerateT<ArrayByte3>(data, map_new_to_old)) )
    00130     return out_data;
    00131   else
    00132   if ( (out_data = regenerateT<ArrayByte4>(data, map_new_to_old)) )
    00133     return out_data;
    00134   else
    00135   if ( (out_data = regenerateT<ArrayShort2>(data, map_new_to_old)) )
    00136     return out_data;
    00137   else
    00138   if ( (out_data = regenerateT<ArrayShort3>(data, map_new_to_old)) )
    00139     return out_data;
    00140   else
    00141   if ( (out_data = regenerateT<ArrayShort4>(data, map_new_to_old)) )
    00142     return out_data;
    00143   else
    00144   if ( (out_data = regenerateT<ArrayUShort2>(data, map_new_to_old)) )
    00145     return out_data;
    00146   else
    00147   if ( (out_data = regenerateT<ArrayUShort3>(data, map_new_to_old)) )
    00148     return out_data;
    00149   else
    00150   if ( (out_data = regenerateT<ArrayUShort4>(data, map_new_to_old)) )
    00151     return out_data;
    00152
    00153   return NULL;
    00154 }
    00155 //-----------------------------------------------------------------------------
    00156 void DoubleVertexRemover::removeDoubles(Geometry* geom)
    00157 {
    00158   mMapNewToOld.clear();
    00159   mMapOldToNew.clear();
    00160
    00161   std::vector<unsigned int> verti;
    00162   verti.resize(geom->vertexArray()->size());
    00163   mMapOldToNew.resize(verti.size());
    00164   for(unsigned int i=0; i<verti.size(); ++i)
    00165   {
    00166     verti[i] = i;
    00167     mMapOldToNew[i] = 0xFFFFFFFF;
    00168   }
    00169   mMapNewToOld.reserve(verti.size());
    00170
    00171   std::sort(verti.begin(), verti.end(), CompareVertex(geom));
    00172
    00173   if (verti.empty())
    00174     return;
    00175
    00176   unsigned int unique_vert_idx = 0;
    00177   for(unsigned i=1; i<verti.size(); ++i)
    00178   {
    00179     if ( !CompareVertex(geom).equals(verti[unique_vert_idx],verti[i]) )
    00180     {
    00181       for(unsigned j=unique_vert_idx; j<i; ++j)
    00182         mMapOldToNew[verti[j]] = mMapNewToOld.size();
    00183       mMapNewToOld.push_back(verti[unique_vert_idx]);
    00184       unique_vert_idx = i;
    00185     }
    00186   }
    00187   for(unsigned j=unique_vert_idx; j<verti.size(); ++j)
    00188   {
    00189     mMapOldToNew[verti[j]] = mMapNewToOld.size();
    00190     mMapNewToOld.push_back(verti[unique_vert_idx]);
    00191   }
    00192
    00193   // regenerate vertices
    00194
    00195   geom->regenerateVertices(mMapNewToOld);
    00196
    00197   // regenerate DrawCall
    00198
    00199   std::vector< ref<DrawCall> > draw_cmd;
    00200   for(int idraw=0; idraw<geom->drawCalls()->size(); ++idraw)
    00201     draw_cmd.push_back( geom->drawCalls()->at(idraw) );
    00202   geom->drawCalls()->clear();
    00203
    00204   for(size_t idraw=0; idraw<draw_cmd.size(); ++idraw)
    00205   {
    00206     ref<DrawElementsUInt> tris = new DrawElementsUInt( draw_cmd[idraw]->primitiveType() );
    00207     geom->drawCalls()->push_back(tris.get());
    00208     const int idx_count = draw_cmd[idraw]->countIndices();
    00209     tris->indices()->resize(idx_count);
    00210     int i=0;
    00211     for(IndexIterator it = draw_cmd[idraw]->indexIterator(); !it.isEnd(); it.next(), ++i)
    00212       tris->indices()->at(i) = mMapOldToNew[it.index()];
    00213   }
    00214
    00215   #if 0
    00216     printf("DoubleVertexRemover = %d/%d, saved = %d, shrink=%.2f\n", (int)mMapNewToOld.size(), (int)verti.size(), (int)verti.size()-(int)mMapNewToOld.size(), (float)mMapNewToOld.size()/verti.size() );
    00217   #endif
    00218 }
    00219 //-----------------------------------------------------------------------------

Visualization Library v2011.05.1142 Reference Documentation
Copyright 2005-2011 Michele Bosi. All rights reserved.
Updated on Thu May 26 2011 22:51:23.
Permission is granted to use this page to write and publish articles regarding Visualization Library.