a48
2.0.2
|
This is an example of how to use the Stellar Policy paradigm using the a48 halfedge datastructure classes.
#include <map> #include <cstring> #include <mesh.hh> template< class Traits > class MyVertex : public a48::VertexT< Traits > { public: unsigned int id; float pos[2]; }; class MyTraits { public: typedef Vertex< MyTraits > vertex_type; typedef a48::HalfedgeT< MyTraits > halfedge_type; typedef a48::FaceT< MyTraits > face_type; }; typedef a48::StellarMeshT< MyTraits > MyMesh; template< class Traits > class MyItemsPolicy : public virtual a48::BaseItemsPolicy< Traits > { typedef typename Traits::vertex_type vertex_type; typedef typename Traits::halfedge_type halfedge_type; typedef typename Traits::face_type face_type; public: unsigned int num_verts, num_faces; float *vertices; unsigned int *faces; MyItemsPolicy() { num_verts = 3; num_faces = 1; const float _vertices[] = { 0.f, 0.f, 1.f, 0.f, 1.f, 1.f }; const unsigned int _faces[] = { 0, 1, 2 }; vertices = new float[ num_verts * 2 ]; faces = new unsigned int[ num_faces * 3 ]; memcpy( vertices, _vertices, num_verts * 2 * sizeof(float) ); memcpy( faces, _faces, num_faces * 3 * sizeof(unsigned int) ); } ~MyItemsPolicy() { delete [] vertices; delete [] faces; } void set_vertex_attributes(const unsigned int& i, vertex_type *v) { v->id = i; v->pos[0] = vertices[ i*2 + 0 ]; v->pos[1] = vertices[ i*2 + 1 ]; } void set_vertex_attributes(vertex_type *v1, const vertex_type *v0) { } void set_halfedge_attributes(halfedge_type *h1, const halfedge_type *h0) { } void set_halfedge_attributes(halfedge_type *h, halfedge_type *o) { } void set_halfedge_attributes(const unsigned int& i, const unsigned int& j, const unsigned int& hi, halfedge_type *h) { } void set_face_attributes(face_type *f1, const face_type *f0) { } void set_face_attributes(const unsigned int& i, face_type *f) { } }; template< class Traits > class MyStellarPolicy : public virtual a48::BaseStellarPolicy< Traits > { typedef typename Traits::vertex_type vertex_type; typedef typename Traits::halfedge_type halfedge_type; typedef typename Traits::face_type face_type; typedef std::pair< uint, uint > edge_pair; typedef std::map< edge_pair, vertex_type > edge_map; public: edge_map emap; void sample_vertex(const face_type *f, vertex_type *v) { sample_vertex( f->halfedge(), v ); sample_vertex( f->halfedge()->next(), v ); sample_vertex( f->halfedge()->previous(), v ); } void sample_vertex(const halfedge_type *h, vertex_type *v) { uint id0 = h->vertex()->id; uint id1 = h->previous()->vertex()->id; if( id1 < id0 ) { uint idt = id0; id0 = id1; id1 = idt; } edge_pair e( id0, id1 ); if( emap.find( e ) != emap.end() ) { v->id = emap[e].id; v->pos = emap[e].pos; } } void remove_vertex(const face_type *f, const vertex_type *v) { remove_vertex( f->halfedge(), v ); } void remove_vertex(const halfedge_type *h, const vertex_type *v) { uint id0 = h->next()->opposite()->next()->vertex()->id; uint id1 = h->previous()->vertex()->id; if( id1 < id0 ) { uint idt = id0; id0 = id1; id1 = idt; } edge_pair e( id0, id1 ); emap[e] = *v; } void flip_halfedge(const halfedge_type *h) { uint id0 = h->vertex()->id; uint id1 = h->previous()->vertex()->id; if( id1 < id0 ) { uint idt = id0; id0 = id1; id1 = idt; } edge_pair e( id0, id1 ); if( emap.find( e ) != emap.end() ) { id0 = h->next()->vertex()->id; id1 = h->opposite()->next()->vertex()->id; if( id1 < id0 ) { uint idt = id0; id0 = id1; id1 = idt; } edge_pair ne( id0, id1 ); emap[ne] = emap[e]; emap.erase(e); } } }; void example(void) { MyItemsPolicy< MyTraits > ip; MyStellarPolicy< MyTraits > sp; MyMesh m( &ip, &sp ); m.set_base( ip.num_verts, ip.num_faces, ip.faces ); }