00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031 #ifndef __VCG_TETRA_TRI_CONVERTER
00032 #define __VCG_TETRA_TRI_CONVERTER
00033 #include <map>
00034 #include <vector>
00035 #include<vcg/space/tetra3.h>
00036 namespace vcg {
00037 namespace tetra {
00038
00039
00045 template < class TETRA_MESH ,class TRI_MESH >
00046 class TriConverter
00047 {
00048
00049 public:
00050
00052 typedef TETRA_MESH TetraMeshType;
00054 typedef TRI_MESH TriangleMeshType;
00055
00057 typedef typename TetraMeshType::TetraType TetraType;
00059 typedef typename TriangleMeshType::FaceType FaceType;
00060
00062 typedef typename TetraMeshType::VertexType TetraVertexType;
00064 typedef typename TriangleMeshType::VertexType TriVertexType;
00065
00067 typedef typename TetraMeshType::VertexIterator TetraVertexIterator;
00069 typedef typename TriangleMeshType::VertexIterator TriVertexIterator;
00070
00072 typedef typename TetraMeshType::TetraIterator TetraIterator;
00074 typedef typename TriangleMeshType::FaceIterator FaceIterator;
00075
00077 typedef typename TetraMeshType::const_TetraIterator const_TetraIterator;
00079 typedef typename TriangleMeshType::ConstFaceIterator ConstFaceIterator;
00080
00082 typedef typename TetraMeshType::TetraContainer TetraContainer;
00083
00085 typedef typename TetraMeshType::const_VertexPointer const_VertexPointer;
00086
00087
00088 public:
00089
00090
00094
00096 void Convert(TetraContainer &tetra,TriangleMeshType &trim)
00097 {
00098 TetraIterator ti;
00099
00100 TetraVertexType *v0;
00101 TetraVertexType *v1;
00102 TetraVertexType *v2;
00103
00104 trim.Clear();
00105 for (ti=tetra.begin();ti<tetra.end();ti++)
00106 {
00107 if (!(ti->IsD()))
00108 {
00109 if ((ti->IsBorderF(0))||(ti->IsBorderF(1))||(ti->IsBorderF(2))||(ti->IsBorderF(3)))
00110 for (int i=0;i<4;i++)
00111 if (ti->IsBorderF(i))
00112 {
00113 v0=ti->V(Tetra::VofF(i,0));
00114 v1=ti->V(Tetra::VofF(i,1));
00115 v2=ti->V(Tetra::VofF(i,2));
00116 FaceType f=FaceType();
00117 f.ClearFlags();
00118 f.V(0)=v0;
00119 f.V(1)=v1;
00120 f.V(2)=v2;
00121 trim.face.push_back(f);
00122 }
00123 }
00124 }
00125
00126 }
00127
00128 struct InsertedV{
00129 InsertedV( TriVertexType *_v,
00130 FaceType* _f,
00131 int _z):v(_v),f(_f),z(_z){}
00132
00133 TriVertexType *v;
00134 FaceType* f;
00135 int z;
00136
00137 const bool operator <(const InsertedV & o){
00138 return (v<o.v);
00139 }
00140 const bool operator ==(const InsertedV & o){
00141 return (v==o.v);
00142 }
00143 const bool operator !=(const InsertedV & o){
00144 return (v!=o.v);
00145 }
00146 };
00147
00148
00150
00151 void ConvertCopy(TetraContainer &tetra,TriangleMeshType &trim)
00152 {
00153 vector<InsertedV > newVertices;
00154 typename vector<InsertedV>::iterator curr,next;
00155 TriVertexIterator vi;
00156 vector<TriVertexType*> redirect;
00157
00158 Convert(tetra,trim);
00159
00160 FaceIterator fi;
00161
00162 for(fi = trim.face.begin(); fi != trim.face.end(); ++fi){
00163 newVertices.push_back(InsertedV( (*fi).V(0),&(*fi),0));
00164 newVertices.push_back(InsertedV( (*fi).V(1),&(*fi),1));
00165 newVertices.push_back(InsertedV( (*fi).V(2),&(*fi),2));
00166 }
00167
00168 sort(newVertices.begin(),newVertices.end());
00169
00170
00171 int pos = 0;
00172 curr = next = newVertices.begin();
00173 while( next != newVertices.end()){
00174 if((*curr)!=(*next))
00175 pos++;
00176 (*next).f->V( (*next).z) = (TriVertexType*)pos;
00177 curr = next;
00178 next++;
00179 }
00180
00181 typename vector<InsertedV>::iterator newE = unique(newVertices.begin(),newVertices.end());
00182 for(curr = newVertices.begin();curr!= newE;++curr)
00183 trim.vert.push_back(*((*curr).v));
00184
00185 for(vi = trim.vert.begin(); vi != trim.vert.end(); ++vi)
00186 redirect.push_back(&(*vi));
00187
00188 for(fi = trim.face.begin(); fi != trim.face.end(); ++fi){
00189 (*fi).V(0) = redirect[(int)(*fi).V(0)];
00190 (*fi).V(1) = redirect[(int)(*fi).V(1)];
00191 (*fi).V(2) = redirect[(int)(*fi).V(2)];
00192 }
00193 trim.vn = trim.vert.size();
00194 trim.fn = trim.face.size();
00195 }
00196
00197 };
00198 }
00199 }
00200
00201
00202 #endif