vcglib/vcg/simplex/vertexplus/component_ocf.h

242 lines
7.6 KiB
C
Raw Normal View History

2005-10-14 17:07:59 +02:00
/****************************************************************************
* VCGLib o o *
* Visual and Computer Graphics Library o o *
* _ O _ *
* Copyright(C) 2004 \/)\/ *
* 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. *
* *
****************************************************************************/
/****************************************************************************
History
$Log: not supported by cvs2svn $
Revision 1.2 2005/12/12 11:17:32 cignoni
Corrected update function, now only the needed simplexes should be updated.
Revision 1.1 2005/10/14 15:07:59 cignoni
First Really Working version
2005-10-14 17:07:59 +02:00
****************************************************************************/
/*
Note
OCF = Optional Component Fast (hopefully)
compare with OCC(Optional Component Compact)
Mainly the trick here is to store a base pointer in each simplex...
****************************************************************************/
#ifndef __VCG_VERTEX_PLUS_COMPONENT_OCF
#define __VCG_VERTEX_PLUS_COMPONENT_OCF
#include <vcg/simplex/vertexplus/component.h>
#include <vector>
namespace vcg {
namespace vert {
/*
All the Components that can be added to a vertex should be defined in the namespace vert:
*/
template <class VALUE_TYPE>
class vector_ocf: public std::vector<VALUE_TYPE> {
typedef std::vector<VALUE_TYPE> BaseType;
typedef typename vector_ocf<VALUE_TYPE>::iterator ThisTypeIterator;
public:
vector_ocf():std::vector<VALUE_TYPE>(){
ColorEnabled=false;
NormalEnabled=false;
VFAdjacencyEnabled=false;
}
// override di tutte le funzioni che possono spostare
// l'allocazione in memoria del container
void push_back(const VALUE_TYPE & v)
{
ThisTypeIterator oldbegin=(*this).begin();
ThisTypeIterator oldend=(*this).end();
2005-10-14 17:07:59 +02:00
BaseType::push_back(v);
if(oldbegin!=(*this).begin()) _updateOVP((*this).begin(),(*this).end());
else _updateOVP(oldend, (*this).end());
2005-10-14 17:07:59 +02:00
}
void pop_back();
void resize(const unsigned int & _size)
{
ThisTypeIterator oldbegin=(*this).begin();
ThisTypeIterator oldend=(*this).end();
2005-10-14 17:07:59 +02:00
BaseType::resize(_size);
if(oldbegin!=(*this).begin()) _updateOVP((*this).begin(),(*this).end());
else _updateOVP(oldend, (*this).end());
2005-10-14 17:07:59 +02:00
if(ColorEnabled) CV.resize(_size);
if(NormalEnabled) NV.resize(_size);
}
void reserve(const unsigned int & _size)
{
ThisTypeIterator oldbegin=(*this).begin();
2005-10-14 17:07:59 +02:00
BaseType::reserve(_size);
if (ColorEnabled) CV.reserve(_size);
if (NormalEnabled) NV.reserve(_size);
if(oldbegin!=(*this).begin()) _updateOVP((*this).begin(),(*this).end());
2005-10-14 17:07:59 +02:00
}
void _updateOVP(ThisTypeIterator lbegin, ThisTypeIterator lend)
2005-10-14 17:07:59 +02:00
{
ThisTypeIterator vi;
for(vi=lbegin;vi!=lend;++vi)
//for(vi=(*this).begin();vi!=(*this).end();++vi)
(*vi)._ovp=this;
2005-10-14 17:07:59 +02:00
}
////////////////////////////////////////
// Enabling Eunctions
void EnableColor() {
assert(VALUE_TYPE::HasColorOcf());
ColorEnabled=true;
CV.resize((*this).size());
2005-10-14 17:07:59 +02:00
}
void DisableColor() {
assert(VALUE_TYPE::HasColorOcf());
ColorEnabled=false;
CV.clear();
}
void EnableNormal() {
assert(VALUE_TYPE::HasNormalOcf());
NormalEnabled=true;
NV.resize((*this).size());
2005-10-14 17:07:59 +02:00
}
void DisableNormal() {
assert(VALUE_TYPE::HasNormalOcf());
NormalEnabled=false;
NV.clear();
}
void EnableVFAdjacency() {
assert(VALUE_TYPE::HasVFAdjacencyOcf());
VFAdjacencyEnabled=true;
AV.resize((*this).size());
2005-10-14 17:07:59 +02:00
}
void DisableVFAdjacency() {
assert(VALUE_TYPE::HasVFAdjacencyOcf());
VFAdjacencyEnabled=false;
AV.clear();
}
struct VFAdjType {
typename VALUE_TYPE::FacePointer _fp ;
int _zp ;
};
public:
std::vector<typename VALUE_TYPE::ColorType> CV;
std::vector<typename VALUE_TYPE::NormalType> NV;
std::vector<struct VFAdjType> AV;
bool ColorEnabled;
bool NormalEnabled;
bool VFAdjacencyEnabled;
};
//template<> void EnableAttribute<typename VALUE_TYPE::NormalType>(){ NormalEnabled=true;}
/*------------------------- COORD -----------------------------------------*/
/*----------------------------- VFADJ ------------------------------*/
template <class T> class VFAdjOcf: public T {
public:
typename T::FacePointer &VFp() {
assert((*this).Base().VFAdjacencyEnabled);
return (*this).Base().AV[(*this).Index()()]._fp;
2005-10-14 17:07:59 +02:00
}
typename T::FacePointer cVFp() const {
if(! (*this).Base().VFAdjacencyEnabled ) return 0;
else return (*this).Base().AV[(*this).Index()()]._fp;
2005-10-14 17:07:59 +02:00
}
int &VFi() {
assert((*this).Base().VFAdjacencyEnabled);
return (*this).Base().AV[(*this).Index()()]._zp;
2005-10-14 17:07:59 +02:00
}
static bool HasVFAdjacency() { return true; }
static bool HasVFAdjacencyOcf() { return true; }
private:
};
/*------------------------- Normal -----------------------------------------*/
template <class A, class T> class NormalOcf: public T {
public:
typedef A NormalType;
static bool HasNormal() { return true; }
static bool HasNormalOcf() { return true; }
NormalType &N() {
// you cannot use Normals before enabling them with: yourmesh.vert.EnableNormal()
assert((*this).Base().NormalEnabled);
return (*this).Base().NV[(*this).Index()()]; }
2005-10-14 17:07:59 +02:00
};
template <class T> class Normal3sOcf: public NormalOcf<vcg::Point3s, T> {};
template <class T> class Normal3fOcf: public NormalOcf<vcg::Point3f, T> {};
template <class T> class Normal3dOcf: public NormalOcf<vcg::Point3d, T> {};
///*-------------------------- COLOR ----------------------------------*/
template <class A, class T> class ColorOcf: public T {
public:
typedef A ColorType;
ColorType &C() { assert((*this).Base().NormalEnabled); return (*this).Base().CV[(*this).Index()()]; }
2005-10-14 17:07:59 +02:00
static bool HasColor() { return true; }
static bool HasColorOcf() { return true; }
};
template <class T> class Color4bOcf: public ColorOcf<vcg::Color4b, T> {};
///*-------------------------- InfoOpt ----------------------------------*/
template < class T> class InfoOcf: public T {
public:
vector_ocf<typename T::VertType> &Base() const { return *_ovp;}
2005-10-14 17:07:59 +02:00
inline int Index() const {
typename T::VertType const *tp=static_cast<typename T::VertType const*>(this);
int tt2=tp- &*(_ovp->begin());
2005-10-14 17:07:59 +02:00
return tt2;
}
public:
vector_ocf<typename T::VertType> *_ovp;
2005-10-14 17:07:59 +02:00
};
} // end namespace vert
}// end namespace vcg
#endif