diff --git a/vcg/complex/trimesh/base.h b/vcg/complex/trimesh/base.h index ede3a0d1..906a3cc2 100644 --- a/vcg/complex/trimesh/base.h +++ b/vcg/complex/trimesh/base.h @@ -1,440 +1,242 @@ -/**************************************************************************** -* 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 $ - -****************************************************************************/ - -#pragma warning( disable : 4804 ) - -/* -People should subclass his vertex class from these one... -*/ - -#ifndef __VCG_MESH -#define __VCG_MESH - -#include -#include -#include -#include -#include -#include -#include -#include -//#include - -#include -#include - -namespace vcg { - -/** Class Mesh. - This is class for definition of a mesh. - @param STL_VERT_CONT (Template Parameter) Specifies the type of the vertices container any the vertex type. - @param STL_FACE_CONT (Template Parameter) Specifies the type of the faces container any the face type. - */ -template < class STL_VERT_CONT, class STL_FACE_CONT > -class Mesh{ - public: - /// The face container - typedef STL_FACE_CONT face_container; - /// The face container - typedef STL_VERT_CONT vertex_container; - /// The vertex type - typedef typename STL_VERT_CONT::value_type MVTYPE; - /// The face type - typedef typename STL_FACE_CONT::value_type MFTYPE; - /// The scalar type - typedef typename MVTYPE::scalar_type MCTYPE; - /// The type of the vectors - typedef typename MFTYPE::vectorial_type vectorial_type; - /// The type of the scalars - typedef MCTYPE scalar_type; - /// The vertex type - typedef MVTYPE vertex_type; - /// Tipo vertice originario - typedef typename MVTYPE::vertex_base vertex_base; - /// The face type - typedef MFTYPE face_type; - /// The type of vertex iterator - typedef typename STL_VERT_CONT::iterator vertex_iterator; - /// The type of face iterator - typedef typename STL_FACE_CONT::iterator face_iterator; - /// The type of constant vertex iterator - typedef typename STL_VERT_CONT::const_iterator const_vertex_iterator; - /// The type of constant face iterator - typedef typename STL_FACE_CONT::const_iterator const_face_iterator; - /// The vertex pointer type - typedef MVTYPE * vertex_pointer; - /// The face pointer type - typedef MFTYPE * face_pointer; - /// The type of the constant vertex pointer - typedef const MVTYPE * const_vertex_pointer; - /// The type of the constant face pointer - typedef const MFTYPE * const_face_pointer; - /// The vertex base pointer type - typedef typename MVTYPE::vertex_base * vertex_base_pointer; - /// The type of the constant vertex base pointer - typedef const typename MVTYPE::vertex_base * const_vertex_base_pointer; - /// The face base pointer type - typedef typename MFTYPE::face_base face_base; - typedef typename MFTYPE::face_base * face_base_pointer; - /// The type of the constant face base pointer - typedef const typename MFTYPE::face_base * const_face_base_pointer; - /// The mesh type - typedef Mesh MMTYPE; - /// The edge type for FF topology - typedef FEdgePosB fedgepos_type; - /// The edge type for VF topology - typedef VEdgePosB vedgepos_type; - /// The half edge type - typedef HEdgePosB hedgepos_type; - /// The half edge type with the normal - typedef HEdgePosBN hedgeposn_type; - /// The ear type - typedef Ear ear_type; - /// The Box3 type - typedef Box3 BOX_TYPE; - - /// Set of vertices - STL_VERT_CONT vert; - /// Real number of vertices - int vn; - /// Set of faces - STL_FACE_CONT face; - /// Real number of faces - int fn; - /// Bounding box of the mesh - Box3 bbox; - /// Internal status - int status; - - /// Nomi di textures - vector textures; - vector normalmaps; - - /// La camera - Camera camera; - - /// Il colore della mesh -private: - ColorUB c; -public: - - inline const ColorUB & C() const - { - return c; - } - - inline ColorUB & C() - { - return c; - } - - - /// Default constructor - Mesh() - { - fn = vn = 0; - imark = 0; - } - - inline int MemUsed() const - { - return sizeof(MMTYPE)+sizeof(MVTYPE)*vert.size()+sizeof(MFTYPE)*face.size(); - } - - inline int MemNeeded() const - { - return sizeof(MMTYPE)+sizeof(MVTYPE)*vn+sizeof(MFTYPE)*fn; - } - - - -/// Function to destroy the mesh -MMTYPE & Clear() -{ - vert.clear(); - face.clear(); - textures.clear(); - normalmaps.clear(); - vn = 0; - fn = 0; - return *this; -} - -/* Funzioni di info sulle caratteristiche della mesh */ - -static bool HasPerVertexNormal() { return bool(vertex_type::OBJ_TYPE & (vertex_type::OBJ_TYPE_N)); } -static bool HasPerVertexColor() { return bool(vertex_type::OBJ_TYPE & (vertex_type::OBJ_TYPE_C)); } -static bool HasPerVertexMark() { return bool(vertex_type::OBJ_TYPE & (vertex_type::OBJ_TYPE_M)); } -static bool HasPerVertexQuality() { return bool(vertex_type::OBJ_TYPE & (vertex_type::OBJ_TYPE_Q)); } -static bool HasPerVertexTexture() { return bool(vertex_type::OBJ_TYPE & (vertex_type::OBJ_TYPE_T)); } - -static bool HasPerFaceColor() { return bool(face_type::OBJ_TYPE & (face_type::OBJ_TYPE_C)); } -static bool HasPerFaceNormal() { return bool(face_type::OBJ_TYPE & (face_type::OBJ_TYPE_N)); } -static bool HasPerFaceMark() { return bool(face_type::OBJ_TYPE & (face_type::OBJ_TYPE_M)); } -static bool HasPerFaceQuality() { return bool(face_type::OBJ_TYPE & (face_type::OBJ_TYPE_Q)); } - -static bool HasPerWedgeColor() { return bool(face_type::OBJ_TYPE & (face_type::OBJ_TYPE_WC)); } -static bool HasPerWedgeNormal() { return bool(face_type::OBJ_TYPE & (face_type::OBJ_TYPE_WN)); } -static bool HasPerWedgeTexture() { return bool(face_type::OBJ_TYPE & (face_type::OBJ_TYPE_WT)); } - -static bool HasFFTopology() { return bool(face_type::OBJ_TYPE & (face_type::OBJ_TYPE_A)) || HasSTopology(); } -static bool HasVFTopology() { return bool(face_type::OBJ_TYPE & (face_type::OBJ_TYPE_V)) || HasSTopology(); } -static bool HasSTopology() { return bool(face_type::OBJ_TYPE & (face_type::OBJ_TYPE_S)); } -static bool HasTopology() { return HasFFTopology() || HasVFTopology(); } - - - - -/// Initialize the imark-system of the faces -void InitFaceIMark() -{ - face_iterator f; - - for(f=face.begin();f!=face.end();++f) - if( !(*f).IsDeleted() && (*f).IsR() && (*f).IsW() ) - (*f).InitIMark(); -} - -/// Initialize the imark-system of the vertices -void InitVertexIMark() -{ - vertex_iterator vi; - - for(vi=vert.begin();vi!=vert.end();++vi) - if( !(*vi).IsDeleted() && (*vi).IsRW() ) - (*vi).InitIMark(); -} - -// Warning assignament should take a const mesh in input -/** Assignment operator for mesh. The mesh content is losed. -*/ -inline MMTYPE & operator = (MMTYPE & m ) -{ - Clear(); - SelectedMerge(m,true); - return *this; -} -/// The incremental mark -int imark; - -/** Check if the vertex incremental mark matches the one of the mesh. - @param v Vertex pointer -*/ -inline bool IsMarked( MVTYPE * const v ) const { return v->IMark() == imark; } -/** Check if the face incremental mark matches the one of the mesh. - @param v Face pointer -*/ -inline bool IsMarked( MFTYPE * const f ) const { return f->IMark() == imark; } -/** Set the vertex incremental mark of the vertex to the one of the mesh. - @param v Vertex pointer -*/ -inline void Mark( MVTYPE * const v ) const { v->IMark() = imark; } -/** Set the face incremental mark of the vertex to the one of the mesh. - @param v Vertex pointer -*/ -inline void Mark( MFTYPE * const f ) const { f->IMark() = imark; } -/// Unmark the mesh -inline void UnMarkAll() { ++imark; } - - -/** Function to add n vertices to the mesh. The second parameter hold a vector of - pointers to pointer to elements of the mesh that should be updated after a - possible vector realloc. - @param n Il numero di vertici che si vuole aggiungere alla mesh. - @param local_var Vettore di variabili locali che rappresentano puntatori a vertici. - restituisce l'iteratore al primo elemento aggiunto. -*/ -vertex_iterator AddVertices(int n, vector &local_var) -{ - vertex_iterator oldbegin, newbegin; - oldbegin = vert.begin(); - vertex_iterator last=vert.end(); - if(vert.empty()) last=0; // if the vector is empty we cannot find the last valid element - else --last; - unsigned int siz=0; -#ifdef __STL_CONFIG_H -if(last!=0) distance(vert.begin(),last,siz); -#else -if(last!=0) siz=distance(vert.begin(),last); -#endif - for(int i=0; i local_var; - return AddVertices(n,local_var); -} - -/** Function to add n faces to the mesh. - @param n Il numero di facce che si vuole aggiungere alla mesh -*/ -face_iterator AddFaces(int n) -{ - vector local_var; - return AddFaces(n,local_var); -} -/** Function to add n faces to the mesh. - NOTA: Aggiorna fn; - The second parameter hold a vector of - pointers to pointer to elements of the mesh that should be updated after a - possible vector realloc. - @param n Facce da aggiungere - @param local_var Vettore di variabili locali che rappresentano puntatori a facce, occorre, - perche' questi valori siano consistenti, aggiornarli ogni qual volta venga eseguito un resize - del contenitore delle facce. -*/ -face_iterator AddFaces(int n, vector &local_var) -{ - face_iterator oldbegin, newbegin; - oldbegin = face.begin(); - face_iterator last=face.end(); - if(face.empty()) last=0; - else last--; - - unsigned int siz=0; -#ifdef __STL_CONFIG_H - if(last!=0) distance(face.begin(),last,siz); -#else - if(last!=0) siz=distance(face.begin(),last); -#endif - MFTYPE dum; - dum.Supervisor_Flags()=0; - for(int i=0; i::iterator jit; - for(jit=local_var.begin(); jit!=local_var.end(); ++jit) - if((**jit) !=0 ) **jit = **jit-&*oldbegin+&*newbegin; - - // deve restituire l'iteratore alla prima faccia aggiunta; - if(last!=0) - { - last = face.begin(); - advance(last,siz+1); - } - else last=face.begin(); - } - else // - { assert(newbegin == oldbegin); - // se non e'cambiato lo spazio (vector abbastanza grande o lista) - if(last==0) last = face.begin(); // se il vettore era vuoto si restituisce begin - else advance(last,1); // altrimenti il primo dopo quello che era in precedenza l'ultimo valido. - } - - return last; - -} - - -/// Calcolo del volume di una mesh chiusa -scalar_type Volume() -{ - - face_iterator f; - int j,k; - scalar_type V = 0; - vectorial_type T,N,B; - - for(f = face.begin(); f!=face.end(); ++f) - { - for(j = 0; j < 3; ++j) - { - /*calcolo tangente, normale e binormale (6 volte)*/ - k = (j+1)%3; - T = (*f).V(k)->P() - (*f).V(j)->P(); - T.Normalize(); - T = ( (*f).V( k )->P() - (*f).V(j)->P() ) ^ - ( (*f).V((k+1)%3)->P() - (*f).V(j)->P() ) ; - B.Normalize(); - N = T ^ B; - - vectorial_type pj = (*f).V(j)->P(); - vectorial_type pk = (*f).V(k)->P(); - - - V += (pj* T )*(pj*N)*(pj*B); - V += (pk*(-T))*(pk*N)*(pk*B); - } - } - return V/6; -} - - -}; // end class Mesh - - -} // end namespace - - -#endif - +/**************************************************************************** +* 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.1 2004/02/19 13:11:06 cignoni +Initial commit + + +****************************************************************************/ + +#pragma warning( disable : 4804 ) + +/* +People should subclass his vertex class from these one... +*/ + +#ifndef __VCG_MESH +#define __VCG_MESH + +namespace vcg { +namespace tri { + +/** Class Mesh. + This is class for definition of a mesh. + @param VertContainer (Template Parameter) Specifies the type of the vertices container any the vertex type. + @param STL_FACE_CONT (Template Parameter) Specifies the type of the faces container any the face type. + */ +template < class STL_VERT_CONT, class STL_FACE_CONT > +class TriMesh{ + public: + typedef STL_FACE_CONT FaceContainer; + typedef STL_VERT_CONT VertContainer; + typedef typename VertContainer::value_type VertexType; + typedef typename FaceContainer::value_type FaceType; + typedef typename VertexType::ScalarType ScalarType; + typedef typename VertexType::CoordType CoordType; + typedef typename VertContainer::iterator VertexIterator; + typedef typename FaceContainer::iterator FaceIterator; + typedef typename VertContainer::const_iterator ConstVertexIterator; + typedef typename FaceContainer::const_iterator ConstFaceIterator; + typedef VertexType * VertexPointer; + typedef const VertexType * ConstVertexPointer; + typedef FaceType * FacePointer; + typedef const FaceType * ConstFacePointer; + typedef Box3 BoxType; + + /// Set of vertices + VertContainer vert; + /// Real number of vertices + int vn; + /// Set of faces + FaceContainer face; + /// Real number of faces + int fn; + /// Bounding box of the mesh + Box3 bbox; + + /// Nomi di textures + //vector textures; + //vector normalmaps; + + /// La camera + //Camera camera; + + /// Il colore della mesh +private: + Color4b c; +public: + + inline const Color4b & C() const + { + return c; + } + + inline Color4b & C() + { + return c; + } + + + /// Default constructor + TriMesh() + { + fn = vn = 0; + imark = 0; + } + + inline int MemUsed() const + { + return sizeof(MMTYPE)+sizeof(MVTYPE)*vert.size()+sizeof(MFTYPE)*face.size(); + } + + inline int MemNeeded() const + { + return sizeof(MMTYPE)+sizeof(MVTYPE)*vn+sizeof(MFTYPE)*fn; + } + + + +/// Function to destroy the mesh +void Clear() +{ + vert.clear(); + face.clear(); +// textures.clear(); +// normalmaps.clear(); + vn = 0; + fn = 0; +} + +/* Funzioni di info sulle caratteristiche della mesh */ + +static bool HasPerVertexNormal() { return VertexType::HasNormal() ; } +static bool HasPerVertexColor() { return VertexType::HasColor() ; } +static bool HasPerVertexMark() { return VertexType::HasMark() ; } +static bool HasPerVertexQuality() { return VertexType::HasQuality(); } +static bool HasPerVertexTexture() { return VertexType::HasTexture(); } + +static bool HasPerFaceColor() { return FaceType::HasFaceNormal() ; } +static bool HasPerFaceNormal() { return FaceType::HasFaceColor() ; } +static bool HasPerFaceMark() { return FaceType::HasFaceMark() ; } +static bool HasPerFaceQuality() { return FaceType::HasFaceQuality(); } + +static bool HasPerWedgeColor() { return bool(FaceType::OBJ_TYPE & (FaceType::OBJ_TYPE_WC)); } +static bool HasPerWedgeNormal() { return bool(FaceType::OBJ_TYPE & (FaceType::OBJ_TYPE_WN)); } +static bool HasPerWedgeTexture() { return bool(FaceType::OBJ_TYPE & (FaceType::OBJ_TYPE_WT)); } + +static bool HasFFTopology() { return bool(FaceType::OBJ_TYPE & (FaceType::OBJ_TYPE_A)) || HasSTopology(); } +static bool HasVFTopology() { return bool(FaceType::OBJ_TYPE & (FaceType::OBJ_TYPE_V)) || HasSTopology(); } +static bool HasSTopology() { return bool(FaceType::OBJ_TYPE & (FaceType::OBJ_TYPE_S)); } +static bool HasTopology() { return HasFFTopology() || HasVFTopology(); } + + +/// Initialize the imark-system of the faces +void InitFaceIMark() +{ + face_iterator f; + + for(f=face.begin();f!=face.end();++f) + if( !(*f).IsDeleted() && (*f).IsR() && (*f).IsW() ) + (*f).InitIMark(); +} + +/// Initialize the imark-system of the vertices +void InitVertexIMark() +{ + vertex_iterator vi; + + for(vi=vert.begin();vi!=vert.end();++vi) + if( !(*vi).IsDeleted() && (*vi).IsRW() ) + (*vi).InitIMark(); +} + +/// The incremental mark +int imark; + +/** Check if the vertex incremental mark matches the one of the mesh. + @param v Vertex pointer +*/ +inline bool IsMarked( ConstVertexPointer v ) const { return v->IMark() == imark; } +/** Check if the face incremental mark matches the one of the mesh. + @param v Face pointer +*/ +inline bool IsMarked( ConstFacePointer f ) const { return f->IMark() == imark; } +/** Set the vertex incremental mark of the vertex to the one of the mesh. + @param v Vertex pointer +*/ +inline void Mark( ConstVertexPointer v ) const { v->IMark() = imark; } +/** Set the face incremental mark of the vertex to the one of the mesh. + @param v Vertex pointer +*/ +inline void Mark( ConstFacePointer f ) const { f->IMark() = imark; } +/// Unmark the mesh +inline void UnMarkAll() { ++imark; } + + + +/// Calcolo del volume di una mesh chiusa +ScalarType Volume() +{ + + face_iterator f; + int j,k; + ScalarType V = 0; + vectorial_type T,N,B; + + for(f = face.begin(); f!=face.end(); ++f) + { + for(j = 0; j < 3; ++j) + { + /*calcolo tangente, normale e binormale (6 volte)*/ + k = (j+1)%3; + T = (*f).V(k)->P() - (*f).V(j)->P(); + T.Normalize(); + T = ( (*f).V( k )->P() - (*f).V(j)->P() ) ^ + ( (*f).V((k+1)%3)->P() - (*f).V(j)->P() ) ; + B.Normalize(); + N = T ^ B; + + vectorial_type pj = (*f).V(j)->P(); + vectorial_type pk = (*f).V(k)->P(); + + + V += (pj* T )*(pj*N)*(pj*B); + V += (pk*(-T))*(pk*N)*(pk*B); + } + } + return V/6; +} + + +}; // end class Mesh + + +} // end namespace +} // end namespace + + +#endif + diff --git a/vcg/complex/trimesh/update/normals.h b/vcg/complex/trimesh/update/normals.h new file mode 100644 index 00000000..25bd2c40 --- /dev/null +++ b/vcg/complex/trimesh/update/normals.h @@ -0,0 +1,150 @@ +/**************************************************************************** +* 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.1 2004/02/19 13:11:06 cignoni +Initial commit + + +****************************************************************************/ +#ifndef __VCG_TRI_UPDATE_NORMALS +#define __VCG_TRI_UPDATE_NORMALS + +namespace vcg { +namespace tri { + +template +class UpdateNormals +{ + +public: +typedef ComputeMeshType MeshType; +typedef typename MeshType::VertexType VertexType; +typedef typename MeshType::VertexPointer VertexPointer; +typedef typename MeshType::VertexIterator VertexIterator; +typedef typename MeshType::FaceType FaceType; +typedef typename MeshType::FacePointer FacePointer; +typedef typename MeshType::FaceIterator FaceIterator; + +static void PerFaceRW(ComputeMeshType &m, bool normalize=false) +{ + if( !m.HasPerFaceNormal()) return; + + FaceIterator f; + bool cn = true; + + if(normalize) + { + for(f=m.m.face.begin();f!=m.m.face.end();++f) + if( !(*f).IsD() && (*f).IsRW() ) + { + for(int j=0; j<3); ++j) + if( !(*f).V(j)->IsR()) cn = false; + if( cn ) (*f).ComputeNormalizedNormal(); + cn = true; + } + } + else + { + for(f=m.m.face.begin();f!=m.m.face.end();++f) + if( !(*f).IsD() && (*f).IsRW() ) + { + for(int j=0; j<3; ++j) + if( !(*f).V(j)->IsR()) cn = false; + + if( cn ) + (*f).ComputeNormal(); + cn = true; + } + } +} + + +static void PerFaceNormalized(ComputeMeshType &m) +{ + if( !m.HasPerFaceNormal()) return; + FaceIterator f; + for(f=m.face.begin();f!=m.face.end();++f) + if( !(*f).IsD() ) (*f).ComputeNormalizedNormal(); +} + +static void PerFace(ComputeMeshType &m) +{ + if( !m.HasPerFaceNormal()) return; + FaceIterator f; + for(f=m.face.begin();f!=m.face.end();++f) + if( !(*f).IsD() ) (*f).ComputeNormal(); +} + +/// Calculates the vertex normal +static void PerVertexNormalized(ComputeMeshType &m) +{ + if( !m.HasPerVertexNormal()) return; + PerVertex(m); + for(VertexIterator vi=m.vert.begin();vi!=m.vert.end();++vi) + if( !(*vi).IsD() && (*vi).IsRW() ) + (*vi).N().Normalize(); +} + +/// Calculates the vertex normal +static void PerVertex(ComputeMeshType &m) +{ + if( !m.HasPerVertexNormal()) return; + + VertexIterator vi; + for(vi=m.vert.begin();vi!=m.vert.end();++vi) + if( !(*vi).IsD() && (*vi).IsRW() ) + (*vi).N() = VertexType::NormalType(0,0,0); + + FaceIterator f; + + for(f=m.face.begin();f!=m.face.end();++f) + if( !(*f).IsD() && (*f).IsR() ) + { + FaceType::NormalType t = (*f).Normal(); + + for(int j=0; j<3; ++j) + if( !(*f).V(j)->IsD() && (*f).V(j)->IsRW() ) + (*f).V(j)->N() += t; + } +} + + + +void ComputeE() +{ + FaceIterator f; + + for(f = m.face.begin(); f!=m.face.end(); ++f) + (*f).ComputeE(); +} + +}; // end class + +} // End namespace +} // End namespace + + +#endif