[MOAB-dev] r2892 - MOAB/trunk/tools/dagmc

bmsmith6 at wisc.edu bmsmith6 at wisc.edu
Mon May 18 08:58:35 CDT 2009


Author: bmsmith
Date: 2009-05-18 08:58:35 -0500 (Mon, 18 May 2009)
New Revision: 2892

Added:
   MOAB/trunk/tools/dagmc/quads_to_tris.cpp
   MOAB/trunk/tools/dagmc/quads_to_tris.hpp
   MOAB/trunk/tools/dagmc/quads_to_tris_driver.cpp
Modified:
   MOAB/trunk/tools/dagmc/Makefile.am
   MOAB/trunk/tools/dagmc/cub2h5m.cc
Log:
new tool to convert quadrilateral elements to triangles in the DAGMC workflow

Modified: MOAB/trunk/tools/dagmc/Makefile.am
===================================================================
--- MOAB/trunk/tools/dagmc/Makefile.am	2009-05-17 15:42:51 UTC (rev 2891)
+++ MOAB/trunk/tools/dagmc/Makefile.am	2009-05-18 13:58:35 UTC (rev 2892)
@@ -15,7 +15,7 @@
 
 libdagmc_la_SOURCES += cgm2moab.cc
 
-noinst_PROGRAMS = cub2h5m
+noinst_PROGRAMS = update_coords quads_to_tris
 bin_PROGRAMS = cgm2moab
 cgm2moab_SOURCES = main.cc cubfile.c
 cgm2moab_LDFLAGS = $(CGM_LTFLAGS) $(CGM_LDLFAGS)
@@ -38,15 +38,18 @@
 
 TESTS = test_geom 
 check_PROGRAMS = $(TESTS) pt_vol_test ray_fire_test
+
+quads_to_tris_SOURCES = quads_to_tris_driver.cpp quads_to_tris.cpp
+quads_to_tris_LDADD = $(top_builddir)/libMOAB.la
+
 test_geom_SOURCES = test_geom.cc
-cub2h5m_SOURCES = cub2h5m.cc
 test_geom_LDADD = libdagmc.la $(top_builddir)/libMOAB.la $(CGM_LTFLAGS)  $(CGM_LDFLAGS) $(CGM_LIBS)
-cub2h5m_LDADD = libdagmc.la $(top_builddir)/libMOAB.la $(CGM_LTFLAGS) $(CGM_LDFLAGS) $(CGM_LIBS)
 
+update_coords_SOURCES = cub2h5m.cc quads_to_tris.cpp
+update_coords_LDADD = libdagmc.la $(top_builddir)/libMOAB.la $(CGM_LTFLAGS) $(CGM_LDFLAGS) $(CGM_LIBS)
+
 pt_vol_test_SOURCES = pt_vol_test.cc
 pt_vol_test_LDADD = libdagmc.la $(top_builddir)/libMOAB.la $(CGM_LTFLAGS)  $(CGM_LDFLAGS) $(CGM_LIBS)
 
 ray_fire_test_SOURCES = ray_fire_test.cc
 ray_fire_test_LDADD = libdagmc.la $(top_builddir)/libMOAB.la $(CGM_LTFLAGS)  $(CGM_LDFLAGS) $(CGM_LIBS)
-
-

Modified: MOAB/trunk/tools/dagmc/cub2h5m.cc
===================================================================
--- MOAB/trunk/tools/dagmc/cub2h5m.cc	2009-05-17 15:42:51 UTC (rev 2891)
+++ MOAB/trunk/tools/dagmc/cub2h5m.cc	2009-05-18 13:58:35 UTC (rev 2892)
@@ -7,6 +7,7 @@
 #include "Tqdcfr.hpp"
 #include "FileOptions.hpp"
 #include "ReadNCDF.hpp"
+#include "quads_to_tris.hpp"
 
 #define GF_CUBIT_FILE_TYPE    "CUBIT"
 #define GF_STEP_FILE_TYPE     "STEP"
@@ -124,6 +125,9 @@
     //opts = "tdata=coord, 100, sum, temp.exo";
     result =  my_ex_reader.load_file(update_name, file_set, opts, NULL, 0 , 0);
 
+    // convert the quads to tris
+    quads_to_tris( my_impl, file_set );
+
     result = my_impl->write_mesh( output_name );
     assert(!result);
   }

Added: MOAB/trunk/tools/dagmc/quads_to_tris.cpp
===================================================================
--- MOAB/trunk/tools/dagmc/quads_to_tris.cpp	                        (rev 0)
+++ MOAB/trunk/tools/dagmc/quads_to_tris.cpp	2009-05-18 13:58:35 UTC (rev 2892)
@@ -0,0 +1,111 @@
+// Brandon Smith
+// January 20, 2009
+
+/*
+   loop over all surface meshsets
+     get all quads of surface meshset
+     loop over all quads
+       make tris from quad
+       add tris to the surface meshset
+       remove quad from surface meshset
+       delete quad
+     end loop
+   end loop
+*/
+
+#include "quads_to_tris.hpp"
+
+// Generic function to create two tris from a quad. This can be improved later.
+MBErrorCode make_tris_from_quad( MBInterface *MBI,
+                                 MBEntityHandle quad,  /* intput */
+                                 MBEntityHandle &tri0, /* output */
+				 MBEntityHandle &tri1  /* output */) {
+  
+  // get connectivity (ordered counterclockwise for 2D elements in MOAB)
+  MBErrorCode result;    
+  const MBEntityHandle *quad_conn;
+  int n_verts=0;
+  result = MBI->get_connectivity( quad, quad_conn, n_verts );
+    assert( 4 == n_verts );
+    assert( MB_SUCCESS == result);
+   
+  // make tris from quad
+  MBEntityHandle tri0_conn[] = {quad_conn[0], quad_conn[1], quad_conn[3]};
+  MBEntityHandle tri1_conn[] = {quad_conn[1], quad_conn[2], quad_conn[3]};
+  result = MBI->create_element(MBTRI, tri0_conn, 3, tri0);
+    assert( MB_SUCCESS == result);
+  result = MBI->create_element(MBTRI, tri1_conn, 3, tri1);
+    assert( MB_SUCCESS == result);
+
+  return MB_SUCCESS;
+}
+
+MBErrorCode quads_to_tris( MBInterface *MBI, MBEntityHandle input_meshset ) {
+
+  // create a geometry tag to find the surfaces with
+  MBErrorCode result;
+  MBTag geom_tag, id_tag;
+  result = MBI->tag_create( GEOM_DIMENSION_TAG_NAME, sizeof(int), MB_TAG_DENSE,
+                            MB_TYPE_INTEGER, geom_tag, 0, true );
+    assert( MB_SUCCESS==result || MB_ALREADY_ALLOCATED==result);
+
+  // create an id tag to find the surface id with
+  result = MBI->tag_create( GLOBAL_ID_TAG_NAME, sizeof(int), MB_TAG_DENSE,
+                            MB_TYPE_INTEGER, id_tag, 0, true );
+    assert( MB_SUCCESS==result || MB_ALREADY_ALLOCATED==result);
+
+  // get all surface meshsets
+  MBRange surface_meshsets;
+  int dim = 2;
+  void* input_dim[] = {&dim};
+  result = MBI->get_entities_by_type_and_tag( input_meshset, MBENTITYSET, &geom_tag,
+                                              input_dim, 1, surface_meshsets);
+    assert( MB_SUCCESS == result );
+  std::cout << surface_meshsets.size() << " surfaces found." << std::endl;
+
+  // ******************************************************************
+  // Loop over every surface meshset and grab each surface's quads.
+  // ******************************************************************
+  for( MBRange::iterator i=surface_meshsets.begin(); i!=surface_meshsets.end(); i++ ) {
+
+    // get the surface id of the surface meshset
+    int surf_id=0;
+    result = MBI->tag_get_data( id_tag, &(*i), 1, &surf_id );
+      assert(MB_SUCCESS == result);
+    std::cout << "  Surface " << surf_id << " has ";
+
+    // get all quads of the surface
+    MBRange quads;
+    result = MBI->get_entities_by_type( *i, MBQUAD, quads );
+      assert( MB_SUCCESS == result );
+    std::cout << quads.size() << " quads." << std::endl;
+
+    // ******************************************************************
+    // For each quad, make two triangles then delete the quad.
+    // ******************************************************************
+    for(MBRange::iterator j=quads.begin(); j!=quads.end(); j++ ) {
+    
+      // make the tris
+      MBEntityHandle tri0 = 0, tri1 = 0;
+      result = make_tris_from_quad( MBI, *j, tri0, tri1 );
+        assert( MB_SUCCESS == result );
+      
+      // add all the tris to the same surface meshset as the quads were inside.
+      result = MBI->add_entities( *i, &tri0, 1 );
+        if ( MB_SUCCESS != result ) std::cout << "result=" << result << std::endl;
+	assert( MB_SUCCESS == result);
+      result = MBI->add_entities( *i, &tri1, 1 );
+        assert( MB_SUCCESS == result);
+
+      // remove the quad from the surface meshset
+      result = MBI->remove_entities( *i, &(*j), 1 );
+        assert( MB_SUCCESS == result);
+
+      // delete the quad
+      result = MBI->delete_entities( &(*j), 1 );
+        assert( MB_SUCCESS == result);
+
+    } // end quad loop
+  }   // end surface meshset loop
+  return MB_SUCCESS; 
+}

Added: MOAB/trunk/tools/dagmc/quads_to_tris.hpp
===================================================================
--- MOAB/trunk/tools/dagmc/quads_to_tris.hpp	                        (rev 0)
+++ MOAB/trunk/tools/dagmc/quads_to_tris.hpp	2009-05-18 13:58:35 UTC (rev 2892)
@@ -0,0 +1,18 @@
+// Takes a DagMC-style meshset of quads and converts it to triangles.
+// It is assumed that quads are only in surface meshsets. Meshset
+// membership for tris is only preserved for surfaces meshsets of their
+// parent quads.
+//
+
+#include <iostream>
+#include <assert.h>
+#include "MBCore.hpp"
+#include "MBTagConventions.hpp"
+#include "MBRange.hpp"
+
+MBErrorCode make_tris_from_quad( MBInterface *MBI,
+                                 MBEntityHandle quad,  /* input  */
+                                 MBEntityHandle &tri0, /* output */
+				 MBEntityHandle &tri1  /* output */);
+
+MBErrorCode quads_to_tris( MBInterface *MBI, MBEntityHandle input_meshset );

Added: MOAB/trunk/tools/dagmc/quads_to_tris_driver.cpp
===================================================================
--- MOAB/trunk/tools/dagmc/quads_to_tris_driver.cpp	                        (rev 0)
+++ MOAB/trunk/tools/dagmc/quads_to_tris_driver.cpp	2009-05-18 13:58:35 UTC (rev 2892)
@@ -0,0 +1,46 @@
+#include <iostream>
+#include <assert.h>
+#include "MBCore.hpp"
+#include "quads_to_tris.hpp"
+
+#define MBI mb_instance()
+MBInterface* mb_instance();
+
+
+// Read a DAGMC-style file of quads and convert it to tris
+// Input argument is the input filename.
+// Output file will be called input_filename_tris.h5m.
+int main(int argc, char **argv) {
+
+  clock_t start_time;
+  start_time = clock();
+  if( 2 > argc ) {
+    std::cout << "Need name of input file with quads." << std::endl;
+    return 0;
+  }
+
+  // load file from input argument
+  MBErrorCode result;
+  std::string filename = argv[1];
+  MBEntityHandle input_meshset;
+  result = MBI->load_file( filename.c_str(), input_meshset );
+    assert( MB_SUCCESS == result );
+
+  result = quads_to_tris( MBI, input_meshset );
+    assert( MB_SUCCESS == result );
+
+
+  // Write the file that has been converted from quads to tris.
+  // Cut off the .h5m
+  int len1 = filename.length();
+  filename.erase(len1 - 4);
+  std::string filename_new = filename + "_tris.h5m";
+  result = MBI->write_mesh( filename_new.c_str());
+    assert(MB_SUCCESS == result);
+
+  return 0;
+}
+MBInterface* mb_instance() {
+  static MBCore inst;
+  return &inst;
+}



More information about the moab-dev mailing list