MOAB: Mesh Oriented datABase  (version 5.2.1)
SkinMesh.cpp
Go to the documentation of this file.
00001 #include <iostream>
00002 #include <stdlib.h>
00003 #include "MBCore.hpp"
00004 #include "MBRange.hpp"
00005 #include "MBTagConventions.hpp"
00006 
00007 // Hold edges in an array of vertex handles.
00008 struct edge
00009 {
00010     MBEntityHandle v0;
00011     MBEntityHandle v1;
00012 };
00013 
00014 // edge structure comparision function for qsort
00015 // If the first vertex handle is the same, compare the second.
00016 int compare_edge( const void* a, const void* b )
00017 {
00018     struct edge* ia = (struct edge*)a;
00019     struct edge* ib = (struct edge*)b;
00020     if( ia->v0 == ib->v0 ) { return (int)( 100.f * ia->v1 - 100.f * ib->v1 ); }
00021     else
00022     {
00023         return (int)( 100.f * ia->v0 - 100.f * ib->v0 );
00024     }
00025 }
00026 
00027 // This skinner is fast partly because it assumes that no edges exist in the MOAB
00028 // instance. Checking to see if an edge exists before creating a new one is slow.
00029 MBErrorCode skin_tris( MBInterface* mb, MBRange tris, MBRange& skin_edges )
00030 {
00031 
00032     // Empty the output range and make sure that the input range is only tris
00033     skin_edges.clear();
00034     if( tris.empty() ) return MB_ENTITY_NOT_FOUND;
00035     if( !tris.all_of_type( MBTRI ) ) return MB_FAILURE;
00036 
00037     // Remove edges from the instance.
00038     int n_edges;
00039     MBErrorCode rval = mb->get_number_entities_by_type( 0, MBEDGE, n_edges );
00040     if( MB_SUCCESS != rval ) return rval;
00041     if( 0 != n_edges )
00042     {
00043         std::cerr << "skin_tris: failed because " << n_edges << " edges exist in the MOAB instance" << std::endl;
00044         return MB_FAILURE;
00045     }
00046 
00047     // Get connectivity. Do not create MBEdges.
00048     edge* edges = new edge[3 * tris.size()];
00049     int n_verts;
00050     int ii = 0;
00051     for( MBRange::iterator i = tris.begin(); i != tris.end(); ++i )
00052     {
00053         const MBEntityHandle* conn;
00054         rval = mb->get_connectivity( *i, conn, n_verts );
00055         if( MB_SUCCESS != rval ) return rval;
00056         if( 3 != n_verts ) return MB_FAILURE;
00057         // points should not be degenerate
00058         if( conn[0] == conn[1] || conn[1] == conn[2] || conn[2] == conn[0] )
00059         {
00060             std::cerr << "skin_tris: degenerate triangle" << std::endl;
00061             return MB_FAILURE;
00062         }
00063 
00064         // make edges
00065         edges[3 * ii + 0].v0 = conn[0];
00066         edges[3 * ii + 0].v1 = conn[1];
00067         edges[3 * ii + 1].v0 = conn[1];
00068         edges[3 * ii + 1].v1 = conn[2];
00069         edges[3 * ii + 2].v0 = conn[2];
00070         edges[3 * ii + 2].v1 = conn[0];
00071         ii++;
00072     }
00073 
00074     // Ensure that the first vertex handle is the lowest
00075     for( unsigned int i = 0; i < 3 * tris.size(); ++i )
00076     {
00077         if( edges[i].v0 > edges[i].v1 )
00078         {
00079             MBEntityHandle temp = edges[i].v0;
00080             edges[i].v0         = edges[i].v1;
00081             edges[i].v1         = temp;
00082         }
00083     }
00084 
00085     // Sort by first handle, then second handle.
00086     qsort( edges, 3 * tris.size(), sizeof( struct edge ), compare_edge );
00087 
00088     // Go through array, saving edges that are not paired.
00089     for( unsigned int i = 0; i < 3 * tris.size(); i++ )
00090     {
00091         // If the last edge has not been paired, create it. This avoids overrunning
00092         // the edges array with i+1.
00093         if( 3 * tris.size() - 1 == i )
00094         {
00095             const MBEntityHandle conn[2] = { edges[i].v0, edges[i].v1 };
00096             MBEntityHandle edge;
00097             rval = mb->create_element( MBEDGE, conn, 2, edge );
00098             if( MB_SUCCESS != rval ) return rval;
00099             skin_edges.insert( edge );
00100 
00101             // If a match exists, skip ahead
00102         }
00103         else if( edges[i].v0 == edges[i + 1].v0 && edges[i].v1 == edges[i + 1].v1 )
00104         {
00105             i++;
00106             // test to make sure surface is manifold
00107             while( edges[i].v0 == edges[i + 1].v0 && edges[i].v1 == edges[i + 1].v1 )
00108             {
00109                 std::cout << "find_skin WARNING: non-manifold edge" << std::endl;
00110                 mb->list_entity( edges[i].v0 );
00111                 mb->list_entity( edges[i].v1 );
00112                 ++i;
00113             }
00114             // otherwise a skin edge has been found
00115         }
00116         else
00117         {
00118             const MBEntityHandle conn[2] = { edges[i].v0, edges[i].v1 };
00119             MBEntityHandle edge;
00120             rval = mb->create_element( MBEDGE, conn, 2, edge );
00121             if( MB_SUCCESS != rval ) return rval;
00122             skin_edges.insert( edge );
00123         }
00124     }
00125     delete[] edges;
00126     return MB_SUCCESS;
00127 }
00128 
00129 // Skin triangles to recover edges.
00130 // Triangles are contained in surface sets.
00131 int main( int argc, char** argv )
00132 {
00133     if( 1 == argc )
00134     {
00135         std::cout << "Usage: " << argv[0] << " <filename>" << std::endl;
00136         return 0;
00137     }
00138 
00139     // get MOAB instance and read the file
00140     MBCore* mb       = new MBCore();
00141     MBErrorCode rval = mb->load_file( argv[1] );
00142     if( MB_SUCCESS != rval ) return 0;
00143 
00144     // this optimized skinner requires removing all MBEdges from the MOAB instance
00145     MBRange edges;
00146     rval = mb->get_entities_by_type( 0, MBEDGE, edges );
00147     if( MB_SUCCESS != rval ) return 0;
00148     if( !edges.empty() ) std::cout << "Warning: deleting all MBEdges" << std::endl;
00149     rval = mb->delete_entities( edges );
00150     if( MB_SUCCESS != rval ) return 0;
00151 
00152     // get surface sets
00153     MBTag geom_tag;
00154     rval = mb->tag_get_handle( GEOM_DIMENSION_TAG_NAME, 1, MB_TYPE_INTEGER, geom_tag );
00155     if( MB_SUCCESS != rval ) return 0;
00156     MBRange surf_sets;
00157     int two     = 2;
00158     void* dim[] = { &two };
00159     rval        = mb->get_entities_by_type_and_tag( 0, MBENTITYSET, &geom_tag, dim, 1, surf_sets );
00160     if( MB_SUCCESS != rval ) return 0;
00161 
00162     // skin each surface
00163     for( MBRange::iterator i = surf_sets.begin(); i != surf_sets.end(); ++i )
00164     {
00165 
00166         // get triangles in the surface set
00167         MBRange tris;
00168         rval = mb->get_entities_by_type( *i, MBTRI, tris );
00169         if( MB_SUCCESS != rval ) return 0;
00170 
00171         // call the skinning function
00172         MBRange skin_edges;
00173         rval = skin_tris( mb, tris, skin_edges );
00174         if( MB_SUCCESS != rval ) return 0;
00175 
00176         // do something with the result
00177         std::cout << "surface has " << skin_edges.size() << " skin edges" << std::endl;
00178 
00179         // remove the edges for the optimized skinner
00180         rval = mb->delete_entities( skin_edges );
00181         if( MB_SUCCESS != rval ) return 0;
00182     }
00183 }
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines