simple_temporary_data.h
Go to the documentation of this file.
00001 /****************************************************************************
00002 * VCGLib                                                            o o     *
00003 * Visual and Computer Graphics Library                            o     o   *
00004 *                                                                _   O  _   *
00005 * Copyright(C) 2004                                                \/)\/    *
00006 * Visual Computing Lab                                            /\/|      *
00007 * ISTI - Italian National Research Council                           |      *
00008 *                                                                    \      *
00009 * All rights reserved.                                                      *
00010 *                                                                           *
00011 * This program is free software; you can redistribute it and/or modify      *
00012 * it under the terms of the GNU General Public License as published by      *
00013 * the Free Software Foundation; either version 2 of the License, or         *
00014 * (at your option) any later version.                                       *
00015 *                                                                           *
00016 * This program is distributed in the hope that it will be useful,           *
00017 * but WITHOUT ANY WARRANTY; without even the implied warranty of            *
00018 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the             *
00019 * GNU General Public License (http://www.gnu.org/licenses/gpl.txt)          *
00020 * for more details.                                                         *
00021 *                                                                           *
00022 ****************************************************************************/
00023 
00024 #ifndef __VCGLIB_SIMPLE__
00025 #define __VCGLIB_SIMPLE__
00026 
00027 #include <string.h>
00028 
00029 namespace vcg {
00030 
00031 class SimpleTempDataBase{
00032 public:
00033   virtual ~SimpleTempDataBase() {}
00034   SimpleTempDataBase() {}
00035     virtual void Resize(size_t sz) = 0;
00036     virtual void Reorder(std::vector<size_t> & newVertIndex)=0;
00037     virtual size_t SizeOf() const  = 0;
00038     virtual void * DataBegin() = 0;
00039     virtual void * At(size_t i ) = 0;
00040 };
00041 
00042 template <class TYPE>
00043 class VectorNBW: public std::vector<TYPE> {};
00044 
00045 template <>
00046 class VectorNBW<bool>{
00047 public:
00048     VectorNBW():data(0),datasize(0),datareserve(0){}
00049     bool * data ;
00050 
00051     void reserve (const int & sz)       {
00052         if(sz<=datareserve) return;
00053         bool * newdataLoc = new bool[ sz ];
00054         if(datasize!=0) memcpy(newdataLoc,data,sizeof(datasize));
00055         std::swap(data,newdataLoc);
00056         if(newdataLoc != 0) delete[] newdataLoc;
00057         datareserve = sz;
00058     }
00059 
00060     void resize  (const int & sz)       {
00061         int oldDatasize = datasize;
00062         if(sz <= oldDatasize) return;
00063         if(sz > datareserve)
00064             reserve(sz);
00065         datasize = sz;
00066         memset(&data[oldDatasize],0,datasize-oldDatasize);
00067         }
00068     void push_back(const bool & v)      { resize(datasize+1); data[datasize] = v;}
00069 
00070     void clear(){ datasize = 0;}
00071 
00072     unsigned int  size() const { return datasize;}
00073 
00074     bool empty() const {return datasize==0;}
00075 
00076     bool * begin() const {return data;}
00077 
00078     bool  & operator [](const int & i){return data[i];}
00079 
00080 private:
00081     int datasize;
00082     int datareserve;
00083 };
00084 
00085 template <class STL_CONT, class ATTR_TYPE>
00086 class SimpleTempData:public SimpleTempDataBase{
00087 
00088     public:
00089     typedef SimpleTempData<STL_CONT,ATTR_TYPE> SimpTempDataType;
00090     typedef ATTR_TYPE AttrType;
00091 
00092     STL_CONT& c;
00093     VectorNBW<ATTR_TYPE> data;
00094     int padding;
00095 
00096     SimpleTempData(STL_CONT  &_c):c(_c),padding(0){data.reserve(c.capacity());data.resize(c.size());};
00097     SimpleTempData(STL_CONT  &_c, const ATTR_TYPE &val):c(_c){
00098         data.reserve(c.capacity());data.resize(c.size());
00099         Init(val);
00100     };
00101 
00102     ~SimpleTempData(){data.clear();}
00103 
00104     void Init(const ATTR_TYPE &val)
00105     {
00106         std::fill(data.begin(),data.end(),val);
00107     }
00108     // access to data
00109     ATTR_TYPE & operator[](const typename STL_CONT::value_type & v){return data[&v-&*c.begin()];}
00110     ATTR_TYPE & operator[](const typename STL_CONT::value_type * v){return data[v-&*c.begin()];}
00111     ATTR_TYPE & operator[](const typename STL_CONT::iterator & cont){return data[&(*cont)-&*c.begin()];}
00112     ATTR_TYPE & operator[](size_t i){return data[i];}
00113 
00114     void * At(size_t i ) {return &(*this)[i];};
00115 
00116     // update temporary data size
00117     bool UpdateSize(){
00118             if(data.size() != c.size())
00119                 {
00120                     data.resize(c.size());
00121                     return false;
00122                 }
00123             return true;
00124         }
00125 
00126     void Resize(size_t sz){
00127         data.resize(sz);
00128     }
00129 
00130     void Reorder(std::vector<size_t> & newVertIndex){
00131         for(unsigned int i = 0 ; i < data.size(); ++i){
00132             if( newVertIndex[i] != (std::numeric_limits<size_t>::max)())
00133                 data[newVertIndex[i]] = data[i];
00134         }
00135     }
00136 
00137     size_t SizeOf() const {return sizeof(ATTR_TYPE);}
00138     void * DataBegin() {return data.empty()?NULL:&(*data.begin());}
00139 };
00140 
00141 template <class ATTR_TYPE>
00142 class Attribute: public SimpleTempDataBase   {
00143 public:
00144     typedef ATTR_TYPE AttrType;
00145     AttrType * attribute;
00146     Attribute(){attribute = new ATTR_TYPE();}
00147     ~Attribute(){delete attribute;}
00148     size_t SizeOf()const {return sizeof(ATTR_TYPE);}
00149     void * DataBegin(){return attribute;}
00150 
00151     void Resize(size_t  ) {assert(0);}
00152     void Reorder(std::vector<size_t> &  ){assert(0);}
00153     void * At(size_t ) {assert(0);return (void*)0;}
00154 };
00155 
00156 } // end namespace vcg
00157 
00158 #endif


shape_reconstruction
Author(s): Roberto Martín-Martín
autogenerated on Sat Jun 8 2019 18:35:23