add vcglib
This commit is contained in:
1 parent
6f6c0c8eb7
commit
af8100f851
1537 files changed
+634197
No files matched your search
@@ -0,0 +1,355 @@
|
||||
/****************************************************************************
|
||||
* VCGLib o o *
|
||||
* Visual and Computer Graphics Library o o *
|
||||
* _ O _ *
|
||||
* Copyright(C) 2004-2016 \/)\/ *
|
||||
* Visual Computing Lab /\/| *
|
||||
* ISTI - Italian National Research Council | *
|
||||
* \ *
|
||||
* All rights reserved. *
|
||||
* *
|
||||
* This program is free software; you can redistribute it and/or modify *
|
||||
* it under the terms of the GNU General Public License as published by *
|
||||
* the Free Software Foundation; either version 2 of the License, or *
|
||||
* (at your option) any later version. *
|
||||
* *
|
||||
* This program is distributed in the hope that it will be useful, *
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
|
||||
* GNU General Public License (http://www.gnu.org/licenses/gpl.txt) *
|
||||
* for more details. *
|
||||
* *
|
||||
****************************************************************************/
|
||||
|
||||
|
||||
#ifndef __VCGLIB_CAT__
|
||||
#define __VCGLIB_CAT__
|
||||
|
||||
#include <vector>
|
||||
#include <map>
|
||||
#include <string>
|
||||
#include <list>
|
||||
#include <algorithm>
|
||||
#include <assert.h>
|
||||
#include <vcg/container/entries_allocation_table.h>
|
||||
|
||||
namespace vcg {
|
||||
/*@{*/
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
/*!
|
||||
* CATBase is the abstract class for all the allocation tables. These table keep track of
|
||||
* where the traced vector (see traced_ector.h) are kept in memory.
|
||||
* The goal is to know (given a pointer to a memory location), which is the vector the pointed
|
||||
* element is in
|
||||
*
|
||||
*/
|
||||
|
||||
struct CATBaseBase{
|
||||
|
||||
};
|
||||
|
||||
|
||||
|
||||
template <typename STL_CONT>
|
||||
class CATBase: public CATBaseBase{
|
||||
public:
|
||||
typedef typename STL_CONT::value_type ValueType;
|
||||
|
||||
virtual void Resort(ValueType*,ValueType*) =0;
|
||||
virtual void Remove(const STL_CONT&) = 0;
|
||||
virtual void AddDataElem(ValueType*,int)=0;
|
||||
virtual void Resize(ValueType*,int)=0;
|
||||
|
||||
public:
|
||||
// ID serves as a type trait.
|
||||
static int & Id(){
|
||||
static int id=0;
|
||||
return id;
|
||||
}
|
||||
};
|
||||
|
||||
/// CATEntry: first derivation templated on the type of entry
|
||||
/// It implements all the methods to trace and access vector_occ's elements
|
||||
template <typename STL_CONT, class ENTRY_TYPE>
|
||||
class CATEntry: public CATBase<STL_CONT>{
|
||||
public:
|
||||
typedef typename STL_CONT::value_type ValueType;
|
||||
typedef ENTRY_TYPE EntryType;
|
||||
|
||||
CATEntry(){if(Id()==0){
|
||||
Id() = CATBase<STL_CONT>::Id()+1;
|
||||
CATBase<STL_CONT>::Id() = Id();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
static unsigned int Ord(const ValueType *);
|
||||
static ENTRY_TYPE & GetEntry(typename STL_CONT::value_type*pt);
|
||||
|
||||
static void Insert( STL_CONT & c,bool cond=false ); // insert a vector to trace
|
||||
virtual void Remove( const STL_CONT & c); // remove the container c
|
||||
static void RemoveIfEmpty( const STL_CONT & c); // remove the container c
|
||||
static void Remove( ValueType * v); // remove the container that contains v
|
||||
|
||||
virtual void Resort( ValueType* old_start, // resort the allocation table
|
||||
ValueType* new_start); // after a container was moved
|
||||
|
||||
protected:
|
||||
|
||||
static std::list<ENTRY_TYPE >& AT(){ // tallocation table
|
||||
static std::list<ENTRY_TYPE > allocation_table;
|
||||
return allocation_table;
|
||||
}
|
||||
static bool & UTD(){
|
||||
static bool upToDate; // true if Lower() and Upper()
|
||||
return upToDate; // are up to date
|
||||
}
|
||||
|
||||
static ValueType *& Lower() {
|
||||
static ValueType * lower; // pointer to the first element
|
||||
return lower; // of the last container accessed
|
||||
}
|
||||
static ValueType *& Upper() {
|
||||
static ValueType * upper; // pointer to the first element
|
||||
return upper; // if the container next to the last accessed
|
||||
}
|
||||
|
||||
static typename std::list<ENTRY_TYPE>::iterator & Curr(){ // container that was last accessed
|
||||
static typename std::list<ENTRY_TYPE>::iterator currEntry;
|
||||
return currEntry;
|
||||
}
|
||||
|
||||
|
||||
static bool IsTheSameAsLast(const ValueType *pt); // true if pt is in the container
|
||||
// that was accessed last
|
||||
static void Update(const ValueType*); // set Upper() e Lower()
|
||||
static typename std::list<ENTRY_TYPE>::iterator FindBase(const ValueType * pt);
|
||||
// find the container that contains pt (naive)
|
||||
virtual void AddDataElem(typename STL_CONT::value_type * pt,int n);// add n element to the auxiliary data
|
||||
virtual void Resize(typename STL_CONT::value_type * pt,int n);// resize the auxiliary data
|
||||
|
||||
public:
|
||||
static int & Id(){ // unique identifier of the istance
|
||||
static int id=0; // (used as type trait)
|
||||
return id;
|
||||
}
|
||||
};
|
||||
|
||||
// --------------------------- CATEntry: implementation --------------------
|
||||
// derivazione fatta per i membri Occ (Optional Component Compact)
|
||||
template <typename STL_CONT, class ENTRY_TYPE>
|
||||
unsigned int CATEntry<STL_CONT,ENTRY_TYPE>::
|
||||
|
||||
Ord(const ValueType * pt)
|
||||
{
|
||||
Update(pt);
|
||||
return (pt-Lower());
|
||||
}
|
||||
|
||||
|
||||
template <typename STL_CONT, class ENTRY_TYPE>
|
||||
typename std::list<ENTRY_TYPE>::iterator CATEntry<STL_CONT,ENTRY_TYPE>::
|
||||
|
||||
FindBase(const ValueType * pt)
|
||||
{
|
||||
typename std::list<ENTRY_TYPE>::iterator ite,curr_base,_;
|
||||
ite = AT().begin();
|
||||
curr_base = AT().end();
|
||||
|
||||
for(;ite != AT().end();ite++)
|
||||
if( pt < (*ite).Start())
|
||||
return curr_base;
|
||||
else
|
||||
curr_base = ite;
|
||||
|
||||
return curr_base;
|
||||
}
|
||||
|
||||
|
||||
template <typename STL_CONT, class ENTRY_TYPE>
|
||||
bool CATEntry< STL_CONT, ENTRY_TYPE>::
|
||||
|
||||
IsTheSameAsLast(const ValueType * pt)
|
||||
{
|
||||
return ( UTD() && ( !(Lower()> pt)) && (pt < Upper()) );
|
||||
}
|
||||
|
||||
template <typename STL_CONT, class ENTRY_TYPE>
|
||||
void CATEntry< STL_CONT, ENTRY_TYPE>::
|
||||
|
||||
Update(const ValueType * pt)
|
||||
{
|
||||
if(!IsTheSameAsLast(pt)){
|
||||
typename std::list<ENTRY_TYPE>::iterator lower_ite,upper_ite;
|
||||
lower_ite = FindBase(pt);
|
||||
|
||||
assert( lower_ite!=AT().end());
|
||||
|
||||
Lower() = (*lower_ite).Start();
|
||||
if( (*lower_ite).Start() == AT().back().Start())
|
||||
Upper() = (ValueType *) 0xffffffff;
|
||||
else
|
||||
{
|
||||
upper_ite = lower_ite; ++upper_ite;
|
||||
Upper() = (*upper_ite).Start();
|
||||
}
|
||||
|
||||
Curr() = lower_ite;
|
||||
UTD() = true;
|
||||
}
|
||||
}
|
||||
|
||||
template <typename STL_CONT, class ENTRY_TYPE>
|
||||
void CATEntry< STL_CONT, ENTRY_TYPE>::
|
||||
Resort(ValueType* old_start,ValueType* new_start)
|
||||
{
|
||||
AT().sort();
|
||||
UTD() = false;
|
||||
}
|
||||
|
||||
template <typename STL_CONT, class ENTRY_TYPE>
|
||||
void CATEntry<STL_CONT, ENTRY_TYPE>::
|
||||
|
||||
Remove( const STL_CONT & c )
|
||||
{
|
||||
typename std::list<ENTRY_TYPE>::iterator ite;
|
||||
for(ite = AT().begin(); ite != AT().end(); ++ite)
|
||||
if((*ite).C() == &c)
|
||||
{
|
||||
AT().erase(ite);
|
||||
break;
|
||||
}
|
||||
UTD() = false;
|
||||
}
|
||||
|
||||
template <typename STL_CONT, class ENTRY_TYPE>
|
||||
void CATEntry<STL_CONT, ENTRY_TYPE>::
|
||||
|
||||
RemoveIfEmpty( const STL_CONT & c )
|
||||
{
|
||||
typename std::list<ENTRY_TYPE>::iterator ite;
|
||||
for(ite = AT().begin(); ite != AT().end(); ++ite)
|
||||
if((*ite).C() == &c)
|
||||
if(!(*ite).Empty())
|
||||
AT().erase(ite);
|
||||
UTD() = false;
|
||||
}
|
||||
|
||||
template <typename STL_CONT, class ENTRY_TYPE>
|
||||
void CATEntry<STL_CONT, ENTRY_TYPE>::
|
||||
|
||||
Remove(ValueType * pt)
|
||||
{
|
||||
typename std::list<ENTRY_TYPE>::iterator lower_ite;
|
||||
lower_ite = FindBase(pt);
|
||||
AT().erase(lower_ite);
|
||||
UTD() = false;
|
||||
|
||||
}
|
||||
|
||||
template <typename STL_CONT, class ENTRY_TYPE>
|
||||
void CATEntry<STL_CONT, ENTRY_TYPE>::
|
||||
|
||||
Insert( STL_CONT & c,bool cond )
|
||||
{
|
||||
ENTRY_TYPE entry(c);
|
||||
typename std::list<ENTRY_TYPE>::iterator lower_ite,upper_ite;
|
||||
upper_ite = FindBase( c.Pointer2begin());
|
||||
bool isIn = (upper_ite != AT().end());
|
||||
if(isIn){
|
||||
if((*upper_ite).C() != &c )
|
||||
++upper_ite;
|
||||
else
|
||||
return;
|
||||
}
|
||||
lower_ite = AT().insert(upper_ite,entry);
|
||||
lower_ite->Reserve(c.capacity());
|
||||
lower_ite->Resize(c.size());
|
||||
UTD() = false;
|
||||
}
|
||||
|
||||
template <typename STL_CONT, class ENTRY_TYPE>
|
||||
ENTRY_TYPE & CATEntry<STL_CONT, ENTRY_TYPE>::
|
||||
GetEntry(typename STL_CONT::value_type*pt){
|
||||
Update(pt);
|
||||
return *Curr();
|
||||
}
|
||||
|
||||
template <typename STL_CONT, class ENTRY_TYPE>
|
||||
void CATEntry<STL_CONT, ENTRY_TYPE>::
|
||||
|
||||
AddDataElem(typename STL_CONT::value_type * pt,int n)
|
||||
{
|
||||
Update(pt);
|
||||
Curr()->Push_back(n);
|
||||
}
|
||||
|
||||
|
||||
template <typename STL_CONT, class ENTRY_TYPE>
|
||||
void CATEntry<STL_CONT, ENTRY_TYPE>::
|
||||
|
||||
Resize(typename STL_CONT::value_type * pt,int n)
|
||||
{
|
||||
Update(pt);
|
||||
Curr()->Resize(n);
|
||||
}
|
||||
|
||||
|
||||
//--------------------------------------------------------------------------------------------
|
||||
template <typename STL_CONT,class ATTR_TYPE>
|
||||
class CAT:public CATEntry<STL_CONT, EntryCAT<STL_CONT,ATTR_TYPE> >{
|
||||
typedef typename STL_CONT::value_type ValueType;
|
||||
typedef CATEntry<STL_CONT, EntryCAT<STL_CONT,ATTR_TYPE> > TT;
|
||||
public:
|
||||
static ATTR_TYPE & Get(const ValueType * pt);
|
||||
static CAT<STL_CONT,ATTR_TYPE> * New();
|
||||
static CAT<STL_CONT,ATTR_TYPE> *& Instance(){ static CAT<STL_CONT,ATTR_TYPE> * instance=NULL; return instance;}
|
||||
};
|
||||
//---------------------- CAT: implementation---------------------------------------------------
|
||||
template <typename STL_CONT, class ATTR_TYPE>
|
||||
ATTR_TYPE & CAT<STL_CONT,ATTR_TYPE>::
|
||||
|
||||
Get(const ValueType * pt)
|
||||
{
|
||||
int ord = Ord(pt);
|
||||
return TT::Curr()->Data()[ord];
|
||||
}
|
||||
|
||||
struct Env{
|
||||
|
||||
static std::map< std::string,CATBaseBase *> & TypeNameBounds(){
|
||||
static std::map< std::string,CATBaseBase *> ntb; return ntb;}
|
||||
|
||||
template <class TYPE_1,class TYPE_2>
|
||||
static CAT<TYPE_1,TYPE_2> * newCAT(){
|
||||
std::string n = std::string(typeid(TYPE_1).name())+std::string(typeid(TYPE_2).name());
|
||||
std::map< std::string,CATBaseBase *>::iterator ti = TypeNameBounds().find(n);
|
||||
if(ti == TypeNameBounds().end()){
|
||||
CAT<TYPE_1,TYPE_2> * res = new CAT<TYPE_1,TYPE_2>();
|
||||
TypeNameBounds().insert(std::pair<std::string,CATBaseBase*>(n,res));
|
||||
return res;
|
||||
}
|
||||
else
|
||||
return (CAT<TYPE_1,TYPE_2> *) (*ti).second;
|
||||
}
|
||||
};
|
||||
|
||||
template <typename STL_CONT, class ATTR_TYPE>
|
||||
CAT<STL_CONT,ATTR_TYPE> * CAT<STL_CONT,ATTR_TYPE>::
|
||||
New(){
|
||||
if(Instance()==NULL)
|
||||
{
|
||||
// Instance() = new CAT<STL_CONT,ATTR_TYPE>();
|
||||
Instance() = Env::newCAT<STL_CONT,ATTR_TYPE>();
|
||||
}
|
||||
return Instance();
|
||||
}
|
||||
|
||||
|
||||
};//end namespace vcg
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,195 @@
|
||||
/****************************************************************************
|
||||
* VCGLib o o *
|
||||
* Visual and Computer Graphics Library o o *
|
||||
* _ O _ *
|
||||
* Copyright(C) 2004-2016 \/)\/ *
|
||||
* Visual Computing Lab /\/| *
|
||||
* ISTI - Italian National Research Council | *
|
||||
* \ *
|
||||
* All rights reserved. *
|
||||
* *
|
||||
* This program is free software; you can redistribute it and/or modify *
|
||||
* it under the terms of the GNU General Public License as published by *
|
||||
* the Free Software Foundation; either version 2 of the License, or *
|
||||
* (at your option) any later version. *
|
||||
* *
|
||||
* This program is distributed in the hope that it will be useful, *
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
|
||||
* GNU General Public License (http://www.gnu.org/licenses/gpl.txt) *
|
||||
* for more details. *
|
||||
* *
|
||||
****************************************************************************/
|
||||
|
||||
#ifndef __VCG_DERIVATION_CHAIN
|
||||
#define __VCG_DERIVATION_CHAIN
|
||||
|
||||
namespace vcg{
|
||||
/*------------------------------------------------------------------*/
|
||||
|
||||
// Metaprogramming Core
|
||||
template < typename T=int>
|
||||
class DefaultDeriver : public T {};
|
||||
|
||||
template <
|
||||
class Base,
|
||||
template <typename> class A>
|
||||
class Arity1: public A<Base > {
|
||||
};
|
||||
|
||||
template <
|
||||
class Base,
|
||||
template <typename> class A, template <typename> class B>
|
||||
class Arity2: public B<Arity1<Base, A> > {};
|
||||
|
||||
template <
|
||||
class Base,
|
||||
template <typename> class A, template <typename> class B,
|
||||
template <typename> class C >
|
||||
class Arity3: public C<Arity2<Base, A, B> > {};
|
||||
|
||||
template <
|
||||
class Base,
|
||||
template <typename> class A, template <typename> class B,
|
||||
template <typename> class C, template <typename> class D>
|
||||
class Arity4: public D<Arity3<Base, A, B, C> > {};
|
||||
|
||||
template <
|
||||
class Base,
|
||||
template <typename> class A, template <typename> class B,
|
||||
template <typename> class C, template <typename> class D,
|
||||
template <typename> class E >
|
||||
class Arity5: public E<Arity4<Base, A, B, C, D> > {};
|
||||
|
||||
template <
|
||||
class Base,
|
||||
template <typename> class A, template <typename> class B,
|
||||
template <typename> class C, template <typename> class D,
|
||||
template <typename> class E, template <typename> class F >
|
||||
class Arity6: public F<Arity5<Base, A, B, C, D, E> > {};
|
||||
|
||||
template <
|
||||
class Base,
|
||||
template <typename> class A, template <typename> class B,
|
||||
template <typename> class C, template <typename> class D,
|
||||
template <typename> class E, template <typename> class F,
|
||||
template <typename> class G>
|
||||
class Arity7: public G<Arity6<Base, A, B, C, D, E, F> > {};
|
||||
|
||||
template <
|
||||
class Base,
|
||||
template <typename> class A, template <typename> class B,
|
||||
template <typename> class C, template <typename> class D,
|
||||
template <typename> class E, template <typename> class F,
|
||||
template <typename> class G, template <typename> class H>
|
||||
class Arity8: public H<Arity7<Base, A, B, C, D, E, F, G > > {};
|
||||
|
||||
template <
|
||||
class Base,
|
||||
template <typename> class A, template <typename> class B,
|
||||
template <typename> class C, template <typename> class D,
|
||||
template <typename> class E, template <typename> class F,
|
||||
template <typename> class G, template <typename> class H,
|
||||
template <typename> class I>
|
||||
class Arity9: public I<Arity8<Base, A, B, C, D, E, F, G, H > > {};
|
||||
|
||||
template <
|
||||
class Base,
|
||||
template <typename> class A, template <typename> class B,
|
||||
template <typename> class C, template <typename> class D,
|
||||
template <typename> class E, template <typename> class F,
|
||||
template <typename> class G, template <typename> class H,
|
||||
template <typename> class I, template <typename> class J>
|
||||
class Arity10: public J<Arity9<Base, A, B, C, D, E, F, G, H, I > > {};
|
||||
|
||||
template <
|
||||
class Base,
|
||||
template <typename> class A, template <typename> class B,
|
||||
template <typename> class C, template <typename> class D,
|
||||
template <typename> class E, template <typename> class F,
|
||||
template <typename> class G, template <typename> class H,
|
||||
template <typename> class I, template <typename> class J,
|
||||
template <typename> class K>
|
||||
class Arity11: public K<Arity10<Base, A, B, C, D, E, F, G, H, I, J> > {};
|
||||
|
||||
|
||||
template <
|
||||
class Base,
|
||||
template <typename> class A, template <typename> class B,
|
||||
template <typename> class C, template <typename> class D,
|
||||
template <typename> class E, template <typename> class F,
|
||||
template <typename> class G, template <typename> class H,
|
||||
template <typename> class I, template <typename> class J,
|
||||
template <typename> class K, template <typename> class L>
|
||||
class Arity12: public L<Arity11<Base, A, B, C, D, E, F, G, H, I, J, K> > {};
|
||||
|
||||
template <
|
||||
class Base,
|
||||
template <typename> class A, template <typename> class B,
|
||||
template <typename> class C, template <typename> class D,
|
||||
template <typename> class E, template <typename> class F,
|
||||
template <typename> class G, template <typename> class H,
|
||||
template <typename> class I, template <typename> class J,
|
||||
template <typename> class K, template <typename> class L,
|
||||
template <typename> class M >
|
||||
class Arity13: public L<Arity12<Base, A, B, C, D, E, F, G, H, I, J, K, L> > {};
|
||||
|
||||
|
||||
// chain with 2 template arguments on the derivers
|
||||
template <
|
||||
class Base,
|
||||
class TA,
|
||||
template <typename,typename> class A >
|
||||
class MArity1: public A< Base, TA > {
|
||||
};
|
||||
|
||||
template <
|
||||
class Base,
|
||||
class TA,
|
||||
template <typename,typename> class A,
|
||||
class TB,
|
||||
template <typename,typename> class B>
|
||||
class MArity2: public B< MArity1<Base, TA, A>,TB > {};
|
||||
|
||||
template <
|
||||
class Base,
|
||||
class TA,
|
||||
template <typename,typename> class A,
|
||||
class TB,
|
||||
template <typename,typename> class B,
|
||||
class TC,
|
||||
template <typename,typename> class C >
|
||||
class MArity3: public C<MArity2<Base, TA,A,TB, B>,TC > {};
|
||||
|
||||
template <
|
||||
class Base,
|
||||
class TA,
|
||||
template <typename,typename> class A,
|
||||
class TB,
|
||||
template <typename,typename> class B,
|
||||
class TC,
|
||||
template <typename,typename> class C,
|
||||
class TD,
|
||||
template <typename,typename> class D>
|
||||
class MArity4: public D<MArity3<Base, TA,A,TB, B, TC,C>,TD > {};
|
||||
|
||||
template <
|
||||
class Base,
|
||||
class TA,
|
||||
template <typename,typename> class A,
|
||||
class TB,
|
||||
template <typename,typename> class B,
|
||||
class TC,
|
||||
template <typename,typename> class C,
|
||||
class TD,
|
||||
template <typename,typename> class D,
|
||||
class TE,
|
||||
template <typename,typename> class E>
|
||||
class MArity5: public E<MArity4<Base, TA,A,TB, B, TC,C, TD, D>, TE> {};
|
||||
|
||||
|
||||
|
||||
class DumClass {};
|
||||
|
||||
}// end namespace vcg
|
||||
#endif
|
||||
@@ -0,0 +1,85 @@
|
||||
/****************************************************************************
|
||||
* VCGLib o o *
|
||||
* Visual and Computer Graphics Library o o *
|
||||
* _ O _ *
|
||||
* Copyright(C) 2004-2016 \/)\/ *
|
||||
* Visual Computing Lab /\/| *
|
||||
* ISTI - Italian National Research Council | *
|
||||
* \ *
|
||||
* All rights reserved. *
|
||||
* *
|
||||
* This program is free software; you can redistribute it and/or modify *
|
||||
* it under the terms of the GNU General Public License as published by *
|
||||
* the Free Software Foundation; either version 2 of the License, or *
|
||||
* (at your option) any later version. *
|
||||
* *
|
||||
* This program is distributed in the hope that it will be useful, *
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
|
||||
* GNU General Public License (http://www.gnu.org/licenses/gpl.txt) *
|
||||
* for more details. *
|
||||
* *
|
||||
****************************************************************************/
|
||||
|
||||
#ifndef __VCGLIB_ENTRIES__
|
||||
#define __VCGLIB_ENTRIES__
|
||||
|
||||
|
||||
namespace vcg {
|
||||
|
||||
// EntryCATBase: base class for the entry of the allocation table
|
||||
// templated over the container type
|
||||
template <class STL_CONT>
|
||||
class EntryCATBase{
|
||||
public:
|
||||
EntryCATBase(STL_CONT & _c):c(_c){};
|
||||
typename STL_CONT::value_type * Start() const;
|
||||
virtual bool Empty(){return true;};
|
||||
const STL_CONT * C();
|
||||
virtual void Push_back(const int &){};
|
||||
|
||||
virtual void Reserve(const int & s){};
|
||||
virtual void Resize(const int & s){};
|
||||
|
||||
const bool operator < (const EntryCATBase<STL_CONT> & other) const;
|
||||
|
||||
private:
|
||||
STL_CONT & c;
|
||||
};
|
||||
|
||||
//EntryCAT: entry for the case of optional core types (matches with CAT)
|
||||
template <class STL_CONT,class ATTR_TYPE >
|
||||
struct EntryCAT: public EntryCATBase<STL_CONT>{
|
||||
typedef ATTR_TYPE attr_type;
|
||||
EntryCAT(STL_CONT & _c) : EntryCATBase<STL_CONT>(_c){};
|
||||
std::vector<ATTR_TYPE> & Data(){return data;}
|
||||
void Push_back(const int & n){ for(int i = 0; i < n ; ++i) data.push_back(ATTR_TYPE());}
|
||||
virtual void Reserve(const int & s){data.reserve(s);};
|
||||
virtual void Resize(const int & s){data.resize(s);};
|
||||
|
||||
|
||||
private:
|
||||
std::vector<ATTR_TYPE> data;
|
||||
};
|
||||
|
||||
//----------------------EntryCAT: implementation ----------------------------------------
|
||||
template <class STL_CONT>
|
||||
const bool EntryCATBase<STL_CONT>:: operator < (const EntryCATBase<STL_CONT> & other) const{
|
||||
return (Start() < other.Start());
|
||||
}
|
||||
|
||||
template <class STL_CONT>
|
||||
typename STL_CONT::value_type * EntryCATBase<STL_CONT>::Start()const {
|
||||
return c.Pointer2begin();
|
||||
}
|
||||
|
||||
template <class STL_CONT>
|
||||
const STL_CONT * EntryCATBase<STL_CONT>::C(){
|
||||
return &c;
|
||||
}
|
||||
|
||||
|
||||
|
||||
}; // end namespace vcg
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,239 @@
|
||||
/****************************************************************************
|
||||
* VCGLib o o *
|
||||
* Visual and Computer Graphics Library o o *
|
||||
* _ O _ *
|
||||
* Copyright(C) 2004-2016 \/)\/ *
|
||||
* Visual Computing Lab /\/| *
|
||||
* ISTI - Italian National Research Council | *
|
||||
* \ *
|
||||
* All rights reserved. *
|
||||
* *
|
||||
* This program is free software; you can redistribute it and/or modify *
|
||||
* it under the terms of the GNU General Public License as published by *
|
||||
* the Free Software Foundation; either version 2 of the License, or *
|
||||
* (at your option) any later version. *
|
||||
* *
|
||||
* This program is distributed in the hope that it will be useful, *
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
|
||||
* GNU General Public License (http://www.gnu.org/licenses/gpl.txt) *
|
||||
* for more details. *
|
||||
* *
|
||||
****************************************************************************/
|
||||
|
||||
#ifndef __VCGLIB_SIMPLE__
|
||||
#define __VCGLIB_SIMPLE__
|
||||
|
||||
#include <string>
|
||||
#include <cstring>
|
||||
#include <limits>
|
||||
#include <vector>
|
||||
#include <cassert>
|
||||
|
||||
namespace vcg
|
||||
{
|
||||
|
||||
class SimpleTempDataBase
|
||||
{
|
||||
public:
|
||||
virtual ~SimpleTempDataBase() {}
|
||||
SimpleTempDataBase() {}
|
||||
virtual void Resize(size_t sz) = 0;
|
||||
virtual void Reorder(std::vector<size_t> &newVertIndex) = 0;
|
||||
virtual size_t SizeOf() const = 0;
|
||||
virtual void *DataBegin() = 0;
|
||||
virtual const void* DataBegin() const = 0;
|
||||
|
||||
virtual void *At(size_t i) = 0;
|
||||
virtual const void *At(size_t i) const = 0;
|
||||
virtual void CopyValue(const size_t to, const size_t from, const SimpleTempDataBase *other) = 0;
|
||||
};
|
||||
|
||||
template <class TYPE, class ...p>
|
||||
class VectorNBW : public std::vector<TYPE, p...>
|
||||
{
|
||||
};
|
||||
|
||||
template <class ...p>
|
||||
class VectorNBW<bool, p...>
|
||||
{
|
||||
public:
|
||||
VectorNBW() : booldata(nullptr), datasize(0), datareserve(0) {}
|
||||
|
||||
~VectorNBW()
|
||||
{
|
||||
if (booldata)
|
||||
delete[] booldata;
|
||||
}
|
||||
|
||||
void reserve(size_t sz)
|
||||
{
|
||||
if (sz <= datareserve)
|
||||
return;
|
||||
bool* newdataLoc = new bool[sz];
|
||||
if (datasize != 0) {
|
||||
std::copy(booldata, booldata+datasize, newdataLoc);
|
||||
// memcpy(newdataLoc, booldata, sizeof(bool) * sizeof(datasize));
|
||||
}
|
||||
|
||||
std::swap(booldata, newdataLoc);
|
||||
if (newdataLoc != 0)
|
||||
delete[] newdataLoc;
|
||||
datareserve = sz;
|
||||
}
|
||||
|
||||
void resize(size_t sz)
|
||||
{
|
||||
int oldDatasize = datasize;
|
||||
if ((int)sz <= oldDatasize)
|
||||
{
|
||||
datasize = sz;
|
||||
return;
|
||||
}
|
||||
if (sz > datareserve)
|
||||
reserve(sz);
|
||||
datasize = sz;
|
||||
for (unsigned int i = oldDatasize; i < datasize; ++i)
|
||||
booldata[i] = false;
|
||||
}
|
||||
void push_back(const bool &v)
|
||||
{
|
||||
resize(datasize + 1);
|
||||
booldata[datasize] = v;
|
||||
}
|
||||
|
||||
void clear() { datasize = 0; }
|
||||
|
||||
unsigned int size() const { return datasize; }
|
||||
|
||||
bool empty() const { return datasize == 0; }
|
||||
|
||||
bool* data() {return booldata;}
|
||||
const bool *data() const { return booldata; }
|
||||
|
||||
bool &operator[](size_t i) { return booldata[i]; }
|
||||
const bool &operator[](size_t i) const { return booldata[i]; }
|
||||
|
||||
private:
|
||||
bool *booldata;
|
||||
size_t datasize;
|
||||
size_t datareserve;
|
||||
};
|
||||
|
||||
template <class STL_CONT, class ATTR_TYPE>
|
||||
class SimpleTempData : public SimpleTempDataBase
|
||||
{
|
||||
|
||||
public:
|
||||
typedef SimpleTempData<STL_CONT, ATTR_TYPE> SimpTempDataType;
|
||||
typedef ATTR_TYPE AttrType;
|
||||
|
||||
const STL_CONT &c;
|
||||
VectorNBW<ATTR_TYPE> data;
|
||||
int padding;
|
||||
|
||||
SimpleTempData(const STL_CONT &_c) : c(_c), padding(0)
|
||||
{
|
||||
data.reserve(c.capacity());
|
||||
data.resize(c.size());
|
||||
};
|
||||
SimpleTempData(const STL_CONT &_c, const ATTR_TYPE &val) : c(_c)
|
||||
{
|
||||
data.reserve(c.capacity());
|
||||
data.resize(c.size());
|
||||
Init(val);
|
||||
};
|
||||
|
||||
~SimpleTempData()
|
||||
{
|
||||
data.clear();
|
||||
}
|
||||
|
||||
void Init(const ATTR_TYPE &val)
|
||||
{
|
||||
std::fill(data.begin(), data.end(), val);
|
||||
}
|
||||
// access to data
|
||||
ATTR_TYPE &operator[](const typename STL_CONT::value_type &v) { return data[&v - &*c.begin()]; }
|
||||
ATTR_TYPE &operator[](const typename STL_CONT::value_type *v) { return data[v - &*c.begin()]; }
|
||||
ATTR_TYPE &operator[](const typename STL_CONT::const_iterator &cont) { return data[&(*cont) - &*c.begin()]; }
|
||||
ATTR_TYPE &operator[](const typename STL_CONT::iterator &cont) { return data[&(*cont) - &*c.begin()]; }
|
||||
ATTR_TYPE &operator[](size_t i) { return data[i]; }
|
||||
|
||||
const ATTR_TYPE &operator[](const typename STL_CONT::value_type &v) const { return data[&v - &*c.begin()]; }
|
||||
const ATTR_TYPE &operator[](const typename STL_CONT::value_type *v) const { return data[v - &*c.begin()]; }
|
||||
const ATTR_TYPE &operator[](const typename STL_CONT::const_iterator &cont) const { return data[&(*cont) - &*c.begin()]; }
|
||||
const ATTR_TYPE &operator[](const typename STL_CONT::iterator &cont) const { return data[&(*cont) - &*c.begin()]; }
|
||||
const ATTR_TYPE &operator[](size_t i) const { return data[i]; }
|
||||
|
||||
void *At(size_t i) { return &(*this)[i]; }
|
||||
const void *At(size_t i) const { return &(*this)[i]; }
|
||||
|
||||
void CopyValue(const size_t to, const size_t from, const SimpleTempDataBase *other)
|
||||
{
|
||||
assert(other != nullptr);
|
||||
data[to] = *(static_cast<const ATTR_TYPE *>(other->At(from)));
|
||||
}
|
||||
|
||||
// update temporary data size
|
||||
bool UpdateSize()
|
||||
{
|
||||
if (data.size() != c.size())
|
||||
{
|
||||
data.resize(c.size());
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
void Resize(size_t sz)
|
||||
{
|
||||
data.resize(sz);
|
||||
}
|
||||
|
||||
void Reorder(std::vector<size_t> &newVertIndex)
|
||||
{
|
||||
for (size_t i = 0; i < data.size(); ++i)
|
||||
{
|
||||
if (newVertIndex[i] != (std::numeric_limits<size_t>::max)())
|
||||
data[newVertIndex[i]] = data[i];
|
||||
}
|
||||
}
|
||||
|
||||
size_t SizeOf() const { return sizeof(ATTR_TYPE); }
|
||||
void *DataBegin() { return data.empty() ? nullptr : data.data(); }
|
||||
const void *DataBegin() const { return data.empty() ? nullptr : data.data(); }
|
||||
};
|
||||
|
||||
template <class ATTR_TYPE>
|
||||
class Attribute : public SimpleTempDataBase
|
||||
{
|
||||
public:
|
||||
typedef ATTR_TYPE AttrType;
|
||||
Attribute() { attribute = new ATTR_TYPE(); }
|
||||
~Attribute() { delete attribute; }
|
||||
size_t SizeOf() const { return sizeof(ATTR_TYPE); }
|
||||
void *DataBegin() { return attribute; }
|
||||
const void* DataBegin() const {return attribute;}
|
||||
|
||||
void Resize(size_t) { assert(0); }
|
||||
void Reorder(std::vector<size_t> &) { assert(0); }
|
||||
|
||||
void *At(size_t)
|
||||
{
|
||||
assert(0);
|
||||
return (void *)0;
|
||||
}
|
||||
const void *At(size_t) const
|
||||
{
|
||||
assert(0);
|
||||
return (void *)0;
|
||||
}
|
||||
void CopyValue(const size_t, const size_t, const SimpleTempDataBase *) { assert(0); }
|
||||
private:
|
||||
AttrType *attribute;
|
||||
};
|
||||
|
||||
} // end namespace vcg
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,162 @@
|
||||
/****************************************************************************
|
||||
* VCGLib o o *
|
||||
* Visual and Computer Graphics Library o o *
|
||||
* _ O _ *
|
||||
* Copyright(C) 2004-2016 \/)\/ *
|
||||
* Visual Computing Lab /\/| *
|
||||
* ISTI - Italian National Research Council | *
|
||||
* \ *
|
||||
* All rights reserved. *
|
||||
* *
|
||||
* This program is free software; you can redistribute it and/or modify *
|
||||
* it under the terms of the GNU General Public License as published by *
|
||||
* the Free Software Foundation; either version 2 of the License, or *
|
||||
* (at your option) any later version. *
|
||||
* *
|
||||
* This program is distributed in the hope that it will be useful, *
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
|
||||
* GNU General Public License (http://www.gnu.org/licenses/gpl.txt) *
|
||||
* for more details. *
|
||||
* *
|
||||
****************************************************************************/
|
||||
|
||||
#ifndef __VCGLIB_TRACED_VECTOR__
|
||||
#define __VCGLIB_TRACED_VECTOR__
|
||||
|
||||
|
||||
#include <vcg/container/container_allocation_table.h>
|
||||
#include <vcg/container/entries_allocation_table.h>
|
||||
|
||||
#include <assert.h>
|
||||
|
||||
namespace vcg {
|
||||
/*@{*/
|
||||
/*!
|
||||
* This class represent a vector_occ. A vector_occ is derived by a std::vector.
|
||||
* The characteristic of a vector_occ is that you can add (at run time) new attributes
|
||||
* to the container::value_type elements contained in the vector. (see the example..)
|
||||
* The position in memory of a traced vector is kept by the Container Allocation Table,
|
||||
* which is a (unique) list of vector_occ positions.
|
||||
*/
|
||||
|
||||
template <class VALUE_TYPE>
|
||||
class vector_occ: public std::vector<VALUE_TYPE>{
|
||||
typedef vector_occ<VALUE_TYPE> ThisType;
|
||||
typedef std::vector<VALUE_TYPE> TT;
|
||||
|
||||
public:
|
||||
vector_occ():std::vector<VALUE_TYPE>(){id = ID(); ID()=ID()+1; reserve(1);}
|
||||
~vector_occ();
|
||||
|
||||
VALUE_TYPE * Pointer2begin(){
|
||||
if(TT::empty()) return (VALUE_TYPE *)id; else return &*std::vector<VALUE_TYPE>::begin();
|
||||
}
|
||||
std::list < CATBase<ThisType>* > attributes;
|
||||
// override di tutte le funzioni che possono spostare
|
||||
// l'allocazione in memoria del container
|
||||
void push_back(const VALUE_TYPE & v);
|
||||
void pop_back();
|
||||
void resize(const unsigned int & size);
|
||||
void reserve(const unsigned int & size);
|
||||
|
||||
/// this function enable the use of an optional attribute (see...)
|
||||
template <class ATTR_TYPE>
|
||||
void Enable(){
|
||||
CAT<ThisType,ATTR_TYPE> * cat = CAT<ThisType,ATTR_TYPE>::New();
|
||||
cat->Insert(*this);
|
||||
attributes.push_back(cat);
|
||||
}
|
||||
|
||||
/// this function returns true if the attribute in the template parameter is enabled
|
||||
/// Note: once an attribute is disabled, its data is lost (the memory freed)
|
||||
template <class ATTR_TYPE>
|
||||
bool IsEnabled() const{
|
||||
typename std::list < CATBase<ThisType> * >::const_iterator ia;
|
||||
for(ia = attributes.begin(); ia != attributes.end(); ++ia)
|
||||
if((*ia)->Id() == CAT<ThisType,ATTR_TYPE>::Id())
|
||||
return true;
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
/// this function disable the use of an optional attribute (see...)
|
||||
/// Note: once an attribute is disabled, its data is lost (the memory freed)
|
||||
template <class ATTR_TYPE>
|
||||
void Disable(){
|
||||
typename std::list < CATBase<ThisType> * >::iterator ia;
|
||||
for(ia = attributes.begin(); ia != attributes.end(); ++ia)
|
||||
if((*ia)->Id() == CAT<ThisType,ATTR_TYPE>::Id())
|
||||
{
|
||||
(*ia)->Remove(*this);
|
||||
//delete (*ia);
|
||||
attributes.erase(ia);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
private:
|
||||
VALUE_TYPE * old_start;
|
||||
int id;
|
||||
static int & ID(){static int id; return id;}
|
||||
void Update();
|
||||
};
|
||||
|
||||
/*@}*/
|
||||
|
||||
template <class VALUE_TYPE>
|
||||
void vector_occ<VALUE_TYPE>::push_back(const VALUE_TYPE & v){
|
||||
std::vector<VALUE_TYPE>::push_back(v);
|
||||
Update();
|
||||
typename std::list < CATBase<ThisType> * >::iterator ia;
|
||||
for(ia = attributes.begin(); ia != attributes.end(); ++ia)
|
||||
(*ia)->AddDataElem(&(*(this->begin())),1);
|
||||
|
||||
}
|
||||
template <class VALUE_TYPE>
|
||||
void vector_occ<VALUE_TYPE>::pop_back(){
|
||||
std::vector<VALUE_TYPE>::pop_back();
|
||||
Update();
|
||||
}
|
||||
|
||||
template <class VALUE_TYPE>
|
||||
void vector_occ<VALUE_TYPE>::resize(const unsigned int & size){
|
||||
std::vector<VALUE_TYPE>::resize(size);
|
||||
Update();
|
||||
typename std::list < CATBase<ThisType> * >::iterator ia;
|
||||
for(ia = attributes.begin(); ia != attributes.end(); ++ia)
|
||||
(*ia)->
|
||||
Resize(&(*(this->begin())),size);
|
||||
}
|
||||
|
||||
template <class VALUE_TYPE>
|
||||
void vector_occ<VALUE_TYPE>::reserve(const unsigned int & size){
|
||||
std::vector<VALUE_TYPE>::reserve(size);
|
||||
Update();
|
||||
}
|
||||
|
||||
template <class VALUE_TYPE>
|
||||
void vector_occ<VALUE_TYPE>::
|
||||
Update(){
|
||||
typename std::list < CATBase<ThisType> * >::iterator ia;
|
||||
if(Pointer2begin() != old_start)
|
||||
for(ia = attributes.begin(); ia != attributes.end(); ++ia)
|
||||
(*ia)->Resort(old_start,Pointer2begin());
|
||||
|
||||
old_start = Pointer2begin();
|
||||
}
|
||||
|
||||
|
||||
|
||||
template <class VALUE_TYPE>
|
||||
vector_occ<VALUE_TYPE>::~vector_occ(){
|
||||
typename std::list < CATBase<ThisType> * >::iterator ia;
|
||||
for(ia = attributes.begin(); ia != attributes.end(); ++ia)
|
||||
{
|
||||
(*ia)->Remove(*this);
|
||||
delete *ia;
|
||||
}
|
||||
}
|
||||
|
||||
}; // end namespace
|
||||
#endif
|
||||
Reference in new issue
Block a user