From 0a74ba11a590d065b4e96e8db11a869bd4b59d33 Mon Sep 17 00:00:00 2001 From: ganovelli Date: Thu, 25 Mar 2010 16:51:49 +0000 Subject: [PATCH] [introduction of half edges as alternative representation] No modification should be necessary for the existing code. most relevant changes: creation of folder: vcg/connectors vcg/connectors/hedge.h vcg/connectors/hedge_component.h addition to the container of half edges to the trimesh: HEdgeContainer hedge; // container int hn; // number of half edges addition of vcg/trimesh/update/halfedge_indexed.h which contains: - the functions to compute the half edge representation from the indexed and vivecersa - the functions to add or remove an half edge --- vcg/connectors/hedge.h | 223 +++++++++++++++++++ vcg/connectors/hedge_component.h | 369 +++++++++++++++++++++++++++++++ 2 files changed, 592 insertions(+) create mode 100644 vcg/connectors/hedge.h create mode 100644 vcg/connectors/hedge_component.h diff --git a/vcg/connectors/hedge.h b/vcg/connectors/hedge.h new file mode 100644 index 00000000..0f548e1c --- /dev/null +++ b/vcg/connectors/hedge.h @@ -0,0 +1,223 @@ +/**************************************************************************** +* 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. * +* * +****************************************************************************/ + +#ifndef __VCG_HEDGE_ +#define __VCG_HEDGE_ + +//#include +//#include +//#include +#include +#include +#include + +namespace vcg { + +/*------------------------------------------------------------------*/ +/* +The base class of all the recusive definition chain. It is just a container of the typenames of the various simplexes. +These typenames must be known form all the derived classes. +*/ + +template + class HEdgeTypeHolder: public UserTypes{ + public: + + template < class LeftV> + void ImportLocal(const LeftV & /* left */ ) { } + static void Name(std::vector & name){} + +}; + +/* The base class form which we start to add our components. +it has the empty definition for all the standard members (coords, color flags) +Note: +in order to avoid both virtual classes and ambiguous definitions all +the subsequent overrides must be done in a sequence of derivation. + +In other words we cannot derive and add in a single derivation step +(with multiple ancestor), both the real (non-empty) normal and color but +we have to build the type a step a time (deriving from a single ancestor at a time). + + +*/ +template +class HEdgeBase: public + hedge::EmptyHEdgeData< + hedge::EmptyBitFlags< + HEdgeTypeHolder < UserTypes> > > { +}; + + +/* The Real Big Edge class; + +The class __VertexArityMax__ is the one that is the Last to be derived, +and therefore is the only one to know the real members +(after the many overrides) so all the functions with common behaviour +using the members defined in the various Empty/nonEmpty component classes +MUST be defined here. + +I.e. IsD() that uses the overridden Flags() member must be defined here. + +*/ + +template class A, template class B, + template class C, template class D, + template class E, template class F, + template class G, template class H, + template class I, template class J, + template class K> +class HEdgeArityMax: public K, A, B, C, D, E, F, G, H, I, J> > { + +// ----- Flags stuff ----- +public: + + + enum { + + DELETED = 0x0001, // This bit indicate that the edge is deleted from the mesh + NOTREAD = 0x0002, // This bit indicate that the edge of the mesh is not readable + NOTWRITE = 0x0004, // This bit indicate that the edge is not modifiable + MODIFIED = 0x0008, // This bit indicate that the edge is modified + VISITED = 0x0010, // This bit can be used to mark the visited edge + SELECTED = 0x0020, // This bit can be used to select + BORDER = 0x0100, // Border Flag + USER0 = 0x0200 // First user bit + }; + + inline int & UberFlags () { return this->Flags(); } + inline int UberFlags() const { return this->Flags(); } + + bool IsD() const {return (this->Flags() & DELETED) != 0;} /// checks if the vertex is deleted + bool IsR() const {return (this->Flags() & NOTREAD) == 0;} /// checks if the vertex is readable + bool IsW() const {return (this->Flags() & NOTWRITE)== 0;}/// checks if the vertex is modifiable + bool IsRW() const {return (this->Flags() & (NOTREAD | NOTWRITE)) == 0;}/// This funcion checks whether the vertex is both readable and modifiable + bool IsS() const {return (this->Flags() & SELECTED) != 0;}/// checks if the vertex is Selected + bool IsB() const {return (this->Flags() & BORDER) != 0;}/// checks if the vertex is a border one + bool IsV() const {return (this->Flags() & VISITED) != 0;}/// checks if the vertex Has been visited + + + /** Set the flag value + @param flagp Valore da inserire nel flagsimplex + */ + void SetFlags(int flagp) {this->Flags()=flagp;} + + /** Set the flag value + @param flagp Valore da inserire nel flag + */ + void ClearFlags() {this->Flags()=0;} + void SetD() {this->Flags() |=DELETED;}/// deletes the edge from the mesh + void ClearD() {this->Flags() &=(~DELETED);}/// un-delete a edge + void SetR() {this->Flags() &=(~NOTREAD);}/// marks the edge as readable + void ClearR() {this->Flags() |=NOTREAD;}/// marks the edge as not readable + void ClearW() {this->Flags() |=NOTWRITE;}/// marks the edge as writable + void SetW() {this->Flags() &=(~NOTWRITE);}/// marks the edge as not writable + void SetS() {this->Flags() |=SELECTED;}/// select the edge + void ClearS() {this->Flags() &= ~SELECTED;}/// Un-select a edge + void SetB() {this->Flags() |=BORDER;} + void ClearB() {this->Flags() &=~BORDER;} + void SetV() {this->Flags() |=VISITED;} + void ClearV() {this->Flags() &=~VISITED;} + +/// Return the first bit that is not still used +static int &LastBitFlag() + { + static int b =USER0; + return b; + } + +/// allocate a bit among the flags that can be used by user. +static inline int NewBitFlag() + { + LastBitFlag()=LastBitFlag()<<1; + return LastBitFlag(); + } +// de-allocate a bit among the flags that can be used by user. +static inline bool DeleteBitFlag(int bitval) + { + if(LastBitFlag()==bitval) { + LastBitFlag()= LastBitFlag()>>1; + return true; + } + assert(0); + return false; + } + /// This function checks if the given user bit is true + bool IsUserBit(int userBit){return (this->Flags() & userBit) != 0;} + /// This function set the given user bit + void SetUserBit(int userBit){this->Flags() |=userBit;} + /// This function clear the given user bit + void ClearUserBit(int userBit){this->Flags() &= (~userBit);} + + template + void GetBBox( BoxType & bb ) const { + bb.SetNull(); + bb.Add(this->cP(0)); + bb.Add(this->cP(1)); + } + + }; + + +/* + +These are the three main classes that are used by the library user to define its own edges. +The user MUST specify the names of all the type involved in a generic complex. +so for example when defining a vertex of a trimesh you must know the name of the type of the edge and of the face. +Typical usage example: + +A vertex with coords, flags and normal for use in a standard trimesh: + +class VertexNf : public VertexSimp2< VertexNf, EdgeProto, FaceProto, vert::Coord3d, vert::Flag, vert::Normal3f > {}; + + +A vertex with coords, and normal for use in a tetrahedral mesh AND in a standard trimesh: + +class TetraVertex : public VertexSimp3< TetraVertex, EdgeProto, FaceProto, TetraProto, vert::Coord3d, vert::Normal3f > {}; + + +A summary of the available vertex attributes (see component.h for more details): + +Coord3f, Coord3d, +Normal3s, Normal3f, Normal3d +Mark //a int component (incremental mark) +BitFlags +TexCoord2s, TexCoord2f, TexCoord2d +Color4b +Qualitys, Qualityf, Qualityd +VFAdj //topology (vertex->face adjacency) +*/ + +template class A = DefaultDeriver, template class B = DefaultDeriver, + template class C = DefaultDeriver, template class D = DefaultDeriver, + template class E = DefaultDeriver, template class F = DefaultDeriver, + template class G = DefaultDeriver, template class H = DefaultDeriver, + template class I = DefaultDeriver, template class J = DefaultDeriver, + template class K = DefaultDeriver> + class HEdge: public HEdgeArityMax { + public: typedef AllTypes::AHEdgeType IAm; typedef UserTypes TypesPool;}; + +}// end namespace +#endif diff --git a/vcg/connectors/hedge_component.h b/vcg/connectors/hedge_component.h new file mode 100644 index 00000000..48edb8fb --- /dev/null +++ b/vcg/connectors/hedge_component.h @@ -0,0 +1,369 @@ +/**************************************************************************** +* 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. * +* * +****************************************************************************/ + +#ifndef __VCG_HEDGE_COMPONENT +#define __VCG_HEDGE_COMPONENT +//#include +#include +//#include +//#include +#include + +namespace vcg { + namespace hedge { +/* +Some naming Rules +All the Components that can be added to a vertex should be defined in the namespace hedge: + +*/ +// +///*-------------------------- VERTEX ----------------------------------------*/ +//template class EmptyVertexRef: public T { +//public: +// // typedef typename T::VertexType VertexType; +// // typedef typename T::CoordType CoordType; +// inline typename T::VertexType * & V( const int j ) { assert(0); static typename T::VertexType *vp=0; return vp; } +// inline typename T::VertexType * const & V( const int j ) const { assert(0); static typename T::VertexType *vp=0; return vp; } +// inline typename T::VertexType * cV( const int j ) const { assert(0); static typename T::VertexType *vp=0; return vp; } +// inline typename T::CoordType & P( const int j ) { assert(0); static typename T::CoordType coord(0, 0, 0); return coord; } +// inline const typename T::CoordType & P( const int j ) const { assert(0); static typename T::CoordType coord(0, 0, 0); return coord; } +// inline const typename T::CoordType &cP( const int j ) const { assert(0); static typename T::CoordType coord(0, 0, 0); return coord; } +// template +// void ImportLocal(const LeftF & leftF) {T::ImportLocal(leftF);} +// static bool HasVertexRef() { return false; } +// static void Name(std::vector & name){T::Name(name);} +// +//}; +//template class VertexRef: public T { +//public: +// VertexRef(){ +// v[0]=0; +// v[1]=0; +// } +// +// inline typename T::VertexType * & V( const int j ) { assert(j>=0 && j<2); return v[j]; } +// inline typename T::VertexType * const & V( const int j ) const { assert(j>=0 && j<2); return v[j]; } +// inline typename T::VertexType * cV( const int j ) const { assert(j>=0 && j<2); return v[j]; } +// +// // Shortcut per accedere ai punti delle facce +// inline typename T::CoordType & P( const int j ) { assert(j>=0 && j<2); return v[j]->P(); } +// inline const typename T::CoordType &cP( const int j ) const { assert(j>=0 && j<2); return v[j]->cP(); } +// +// /** Return the pointer to the ((j+1)%3)-th vertex of the face. +// @param j Index of the face vertex. +// */ +// inline typename T::VertexType * & V0( const int j ) { return V(j);} +// inline typename T::VertexType * & V1( const int j ) { return V((j+1)%2);} +// inline const typename T::VertexType * const & V0( const int j ) const { return V(j);} +// inline const typename T::VertexType * const & V1( const int j ) const { return V((j+1)%2);} +// inline const typename T::VertexType * const & cV0( const int j ) const { return cV(j);} +// inline const typename T::VertexType * const & cV1( const int j ) const { return cV((j+1)%2);} +// +// /// Shortcut per accedere ai punti delle facce +// inline typename T::CoordType & P0( const int j ) { return V(j)->P();} +// inline typename T::CoordType & P1( const int j ) { return V((j+1)%2)->P();} +// inline const typename T::CoordType & P0( const int j ) const { return V(j)->P();} +// inline const typename T::CoordType & P1( const int j ) const { return V((j+1)%2)->P();} +// inline const typename T::CoordType & cP0( const int j ) const { return cV(j)->P();} +// inline const typename T::CoordType & cP1( const int j ) const { return cV((j+1)%2)->P();} +// +// inline typename T::VertexType * & UberV( const int j ) { assert(j>=0 && j<2); return v[j]; } +// inline const typename T::VertexType * const & UberV( const int j ) const { assert(j>=0 && j<2); return v[j]; } +// +// template +// void ImportLocal(const LeftF & leftF){ V(0) = NULL; V(1) = NULL; V(2) = NULL; T::ImportLocal(leftF);} +// +// static bool HasVertexRef() { return true; } +// static void Name(std::vector & name){name.push_back(std::string("VertexRef"));T::Name(name);} +// +// +// private: +// typename T::VertexType *v[2]; +//}; + + + +/*-------------------------- INCREMENTAL MARK ----------------------------------------*/ + +template class EmptyMark: public T { +public: + static bool HasMark() { return false; } + static bool HasMarkOcc() { return false; } + inline void InitIMark() { } + inline int & IMark() { assert(0); static int tmp=-1; return tmp;} + inline const int & IMark() const {return 0;} + template < class LeftV> + void ImportLocal(const LeftV & left ) { T::ImportLocal( left); } + static void Name(std::vector & name){T::Name(name);} + +}; +template class Mark: public T { +public: + static bool HasMark() { return true; } + static bool HasMarkOcc() { return true; } + inline void InitIMark() { _imark = 0; } + inline int & IMark() { return _imark;} + inline const int & IMark() const {return _imark;} + template < class LeftV> + void ImportLocal(const LeftV & left ) { IMark() = left.IMark(); T::ImportLocal( left); } + static void Name(std::vector & name){name.push_back(std::string("Mark"));T::Name(name);} + + private: + int _imark; +}; + +/*------------------------- FLAGS -----------------------------------------*/ +template class EmptyBitFlags: public T { +public: + typedef int FlagType; + /// Return the vector of Flags(), senza effettuare controlli sui bit + int &Flags() { static int dummyflags(0); assert(0); return dummyflags; } + int Flags() const { return 0; } + template < class LeftV> + void ImportLocal(const LeftV & left ) { T::ImportLocal( left); } + static bool HasFlags() { return false; } + static void Name(std::vector & name){T::Name(name);} + +}; + +template class BitFlags: public T { +public: + BitFlags(){_flags=0;} + typedef int FlagType; + int &Flags() {return _flags; } + int Flags() const {return _flags; } + template < class LeftV> + void ImportLocal(const LeftV & left ) { Flags() = left.Flags(); T::ImportLocal( left); } + static bool HasFlags() { return true; } + static void Name(std::vector & name){name.push_back(std::string("BitFlags"));T::Name(name);} + +private: + int _flags; +}; + +/*----------------------------- HEVADJ ------------------------------*/ +template class EmptyHVAdj: public T { +public: + typename T::VertexPointer &HVp() { static typename T::VertexPointer ep=0; assert(0); return ep; } + typename T::VertexPointer cHVp() { static typename T::VertexPointer ep=0; assert(0); return ep; } + int &HVi(){static int z=0; return z;}; + template < class LeftV> + void ImportLocal(const LeftV & left ) { T::ImportLocal( left); } + static bool HasHVAdjacency() { return false; } + static bool HasHVAdjacencyOcc() { return false; } + static void Name(std::vector & name){ T::Name(name);} +}; + +template class HVAdj: public T { +public: + HVAdj(){_vp =0;} + typename T::VertexPointer & HVp() {return _vp ; } + const typename T::VertexPointer cHVp() const {return _vp ; } + template < class LeftV> + void ImportLocal(const LeftV & left ) { this->V() = NULL; T::ImportLocal( left); } + static bool HasHVAdjacency() { return true; } + static bool HasHVAdjacencyOcc() { return true; } + static void Name(std::vector & name){name.push_back(std::string("HVAdj"));T::Name(name);} + +private: + typename T::VertexPointer _vp ; +}; + +/*----------------------------- HHADJ ------------------------------*/ +template class EmptyHHAdj: public T { +public: + typename T::HEdgePointer &HHp(const int & ) { static typename T::EdgePointer ep=0; assert(0); return ep; } + typename T::HEdgePointer cHHp(const int & ) { static typename T::EdgePointer ep=0; assert(0); return ep; } + int &HHi(){static int z=0; return z;}; + template < class LeftV> + void ImportLocal(const LeftV & left ) { T::ImportLocal( left); } + static bool HasHHAdjacency() { return false; } + static bool HasHHAdjacencyOcc() { return false; } + static void Name(std::vector & name){ T::Name(name);} +}; + +template class HHAdj: public T { +public: + HHAdj(){_ep=0;} + typename T::EdgePointer &HHp(const int & i) {return _ep[i]; } + typename T::EdgePointer cHHp(const int & i) {return _ep[i]; } + int &HHi(const int & i) {return _zp[i]; } + template < class LeftV> + void ImportLocal(const LeftV & left ) { HHp() = NULL; T::ImportLocal( left); } + static bool HasHHAdjacency() { return true; } + static bool HasHHAdjacencyOcc() { return true; } + static void Name(std::vector & name){name.push_back(std::string("HHAdj"));T::Name(name);} + +private: + typename T::HEdgePointer _ep[2] ; + int _zp[2] ; +}; + + + + +/*----------------------------- HENextADJ ------------------------------*/ +template class EmptyHNextAdj: public T { +public: + typename T::HEdgePointer &HNp( ) { static typename T::HEdgePointer ep=0; assert(0); return ep; } + typename T::HEdgePointer cHp( ) { static typename T::HEdgePointer ep=0; assert(0); return ep; } + template < class LeftV> + void ImportLocal(const LeftV & left ) { T::ImportLocal( left); } + static bool HasHNextAdjacency() { return false; } + static bool HasHNextAdjacencyOcc() { return false; } + static void Name(std::vector & name){ T::Name(name);} +}; + +template class HNextAdj: public T { +public: + HNextAdj(){_nep=0;} + typename T::HEdgePointer &HNp() {return _nep; } + typename T::HEdgePointer cHNp() {return _nep; } + template < class LeftV> + void ImportLocal(const LeftV & left ) { this->EEp() = NULL; T::ImportLocal( left); } + static bool HasHNextAdjacency() { return true; } + static void Name(std::vector & name){name.push_back(std::string("HNextAdj"));T::Name(name);} + +private: + typename T::HEdgePointer _nep ; +}; + +/*----------------------------- HEOppADJ ------------------------------*/ +template class EmptyHOppAdj: public T { +public: + typename T::HEdgePointer &HOp(const int & i ) { static typename T::HEdgePointer ep=0; assert(0); return ep; } + typename T::HEdgePointer cHOp(const int & i) { static typename T::HEdgePointer ep=0; assert(0); return ep; } + int &EEi(){static int z=0; return z;}; + template < class LeftV> + void ImportLocal(const LeftV & left ) { T::ImportLocal( left); } + static bool HasHOppAdjacency() { return false; } + static bool HasHOpptAdjacencyOcc() { return false; } + static void Name(std::vector & name){ T::Name(name);} +}; + +template class HOppAdj: public T { +public: + HOppAdj(){_oep=0;} + typename T::HEdgePointer &HOp() {return _oep; } + typename T::HEdgePointer cHOp() {return _oep; } + template < class LeftV> + void ImportLocal(const LeftV & left ) { this->HOp() = NULL; T::ImportLocal( left); } + static bool HasHOppAdjacency() { return true; } + static void Name(std::vector & name){name.push_back(std::string("HOppAdj"));T::Name(name);} + +private: + typename T::HEdgePointer _oep ; + +}; +/*----------------------------- HEPrevADJ ------------------------------*/ +template class EmptyHPrevAdj: public T { +public: + typename T::HEdgePointer &HPp() { static typename T::HEdgePointer ep=0; assert(0); return ep; } + typename T::HEdgePointer cHPp() { static typename T::HEdgePointer ep=0; assert(0); return ep; } + int &EEi(){static int z=0; return z;}; + template < class LeftV> + void ImportLocal(const LeftV & left ) { T::ImportLocal( left); } + static bool HasHPrevAdjacency() { return false; } + static bool HasHPrevAdjacencyOcc() { return false; } + static void Name(std::vector & name){ T::Name(name);} +}; + +template class HPrevAdj: public T { +public: + HPrevAdj(){_pep=0;} + typename T::EdgePointer &HPp() {return _pep; } + typename T::EdgePointer cHPp() {return _pep; } + int &EEi(const int & i) {return this->_nei[i]; } + template < class LeftV> + void ImportLocal(const LeftV & left ) { this->EEp() = NULL; T::ImportLocal( left); } + static bool HasHPrevAdjacency() { return true; } + static void Name(std::vector & name){name.push_back(std::string("HPrevAdj"));T::Name(name);} + +private: + typename T::EdgePointer _pep ; +}; +/*----------------------------- HFADJ ------------------------------*/ + +template class EmptyHFAdj: public T { +public: + typename T::FacePointer &HFp() { static typename T::FacePointer fp=0; assert(0); return fp; } + typename T::FacePointer cHFp() { static typename T::FacePointer fp=0; assert(0); return fp; } + int &EFi(){static int z=0; return z;}; + template < class LeftV> + void ImportLocal(const LeftV & left ) { T::ImportLocal( left); } + static bool HasHFAdjacency() { return false; } + static bool HasHFAdjacencyOcc() { return false; } + static void Name(std::vector & name){ T::Name(name);} +}; + +template class HFAdj: public T { +public: + HFAdj(){_fp=0;} + typename T::FacePointer &HFp() {return _fp; } + typename T::FacePointer cHFp() {return _fp; } + int &EFi() {return _zp; } + template < class LeftV> + void ImportLocal(const LeftV & left ) { this->EFp() = NULL; T::ImportLocal( left); } + static bool HasHFAdjacency() { return true; } + static bool HasHFAdjacencyOcc() { return true; } + static void Name(std::vector & name){name.push_back(std::string("HFAdj"));T::Name(name);} + +private: + typename T::FacePointer _fp ; + int _zp ; +}; + + +/*----------------------------- HFADJ ------------------------------*/ +/** + HEdgeData keep all the data for the half edge +*/ +template +class EmptyHEdgeData : public EmptyHFAdj< // pointer to the face + EmptyHOppAdj < // pointer to the opposite half edge + EmptyHNextAdj < // pointer to the next half edge along the face + EmptyHVAdj < // pointer to the vertex + EmptyHPrevAdj< + T > > > > >{}; + + +template +class HEdgeData : public HFAdj< // pointer to the face + HOppAdj < // pointer to the opposite half edge + HNextAdj < // pointer to the next half edge along the face + HVAdj < // pointer to the vertex + T > > > >{ + + // functions to make the half edge user confortable + typename T::VertexPointer & Vertex() { return this->HVp();} + const typename T::VertexPointer & cVertex() const { return this->cHVp();} + typename T::HEdgePointer Opposite() { return &this->HOp();} + const typename T::HEdgePointer & cOpposite() const { return this->cHOp();} + typename T::HEdgePointer & Next() { return this->HNp();} + const typename T::HEdgePointer & cNext() const { return this->HNp();} + +}; + + } // end namespace edge +}// end namespace vcg +#endif