[MOAB-dev] r1629 - MOAB/trunk

kraftche at mcs.anl.gov kraftche at mcs.anl.gov
Mon Mar 3 11:47:33 CST 2008


Author: kraftche
Date: 2008-03-03 11:47:33 -0600 (Mon, 03 Mar 2008)
New Revision: 1629

Modified:
   MOAB/trunk/MBCore.cpp
   MOAB/trunk/WriteGmsh.cpp
   MOAB/trunk/WriteSTL.cpp
   MOAB/trunk/WriteSTL.hpp
   MOAB/trunk/WriteVtk.cpp
Log:
o Change default floating-point precision for node coordinates from 6 to 10 
  for VTK and Gmsh formats (not ASCII STL because STL is supposed to be 
  single-precision floats).
o Add "PRECISION" option to writers for VTK, Gmsh, and STL to control 
  floating point precision in text-based file formats.
o Make STL writer fail if there are no triangles to write.
o Fix broken Gmsh writer: revert to passing NULL entity_handles array
  to writers if array is empty because some writers (e.g. Gmsh) test 
  for a NULL array rather than a zero array length.



Modified: MOAB/trunk/MBCore.cpp
===================================================================
--- MOAB/trunk/MBCore.cpp	2008-02-29 21:01:17 UTC (rev 1628)
+++ MOAB/trunk/MBCore.cpp	2008-03-03 17:47:33 UTC (rev 1629)
@@ -478,7 +478,8 @@
   
     // write the file
   std::vector<std::string> qa_records;
-  rval = writer->write_file(file_name, overwrite, opts, &list[0], list.size(), qa_records );
+  const MBEntityHandle* list_ptr = list.empty() ? (MBEntityHandle*)0 : &list[0];
+  rval = writer->write_file(file_name, overwrite, opts, list_ptr, list.size(), qa_records );
   delete writer;
   
   return rval;

Modified: MOAB/trunk/WriteGmsh.cpp
===================================================================
--- MOAB/trunk/WriteGmsh.cpp	2008-02-29 21:01:17 UTC (rev 1628)
+++ MOAB/trunk/WriteGmsh.cpp	2008-03-03 17:47:33 UTC (rev 1629)
@@ -5,11 +5,14 @@
 #include "MBInterface.hpp"
 #include "MBRange.hpp"
 #include "MBWriteUtilIface.hpp"
+#include "FileOptions.hpp"
 
 #include <fstream>
 #include <map>
 #include <set>
 
+const int DEFAULT_PRECISION = 10;
+
 MBWriterIface *WriteGmsh::factory( MBInterface* iface )
   { return new WriteGmsh( iface ); }
 
@@ -48,7 +51,7 @@
     //! writes out a file
 MBErrorCode WriteGmsh::write_file(const char *file_name,
                                   const bool overwrite,
-                                  const FileOptions&,
+                                  const FileOptions& options,
                                   const MBEntityHandle *output_list,
                                   const int num_sets,
                                   std::vector<std::string>& ,
@@ -249,6 +252,12 @@
   out << "2.0 0 " << sizeof(double) << std::endl;
   out << "$EndMeshFormat" << std::endl;
 
+    // Set precision for node coordinates
+  int precision;
+  if (MB_SUCCESS != options.get_int_option( "PRECISION", precision ))
+    precision = DEFAULT_PRECISION;
+  const int old_precision = out.precision();
+  out.precision( precision );
   
     // Write nodes
   out << "$Nodes" << std::endl;
@@ -269,6 +278,8 @@
   out << "$EndNodes" << std::endl;
   coords.clear();
   
+    // Restore stream state
+  out.precision( old_precision );
 
     // Write elements
   out << "$Elements" << std::endl;

Modified: MOAB/trunk/WriteSTL.cpp
===================================================================
--- MOAB/trunk/WriteSTL.cpp	2008-02-29 21:01:17 UTC (rev 1628)
+++ MOAB/trunk/WriteSTL.cpp	2008-03-03 17:47:33 UTC (rev 1629)
@@ -57,6 +57,7 @@
 #  define _S_IWRITE (S_IWUSR|S_IWGRP|S_IWOTH)
 #endif
 
+const int DEFAULT_PRECISION = 6;
 
 MBWriterIface *WriteSTL::factory( MBInterface* iface )
   { return new WriteSTL( iface ); }
@@ -93,6 +94,10 @@
   if (MB_SUCCESS != rval)
     return rval;
 
+  if (triangles.empty()) {
+    mWriteIface->report_error( "No triangles to write." );
+    return MB_ENTITY_NOT_FOUND;
+  }
  
   bool is_ascii = false, is_binary = false;
   if (MB_SUCCESS == opts.get_null_option( "ASCII" ))
@@ -121,8 +126,14 @@
   
   if (is_binary)
     rval = binary_write_triangles( file, header, byte_order, triangles );
-  else
-    rval = ascii_write_triangles( file, header, triangles );
+  else {
+      // Get precision for node coordinates
+    int precision;
+    if (MB_SUCCESS != opts.get_int_option( "PRECISION", precision ))
+      precision = DEFAULT_PRECISION;
+
+    rval = ascii_write_triangles( file, header, triangles, precision );
+  }
   fclose( file );
   return rval;
 }
@@ -246,7 +257,8 @@
 
 MBErrorCode WriteSTL::ascii_write_triangles( FILE* file,
                                              const char header[81],
-                                             const MBRange& triangles )
+                                             const MBRange& triangles,
+                                             int prec )
 {
   const char solid_name[] = "MOAB";
   
@@ -283,9 +295,9 @@
    
     fprintf( file,"facet normal %e %e %e\n", n[0], n[1], n[2] );
     fprintf( file,"outer loop\n" );
-    fprintf( file,"vertex %e %e %e\n", v1[0], v1[1], v1[2] );
-    fprintf( file,"vertex %e %e %e\n", v2[0], v2[1], v2[2] );
-    fprintf( file,"vertex %e %e %e\n", v3[0], v3[1], v3[2] );
+    fprintf( file,"vertex %.*e %.*e %.*e\n", prec, v1[0], prec, v1[1], prec, v1[2] );
+    fprintf( file,"vertex %.*e %.*e %.*e\n", prec, v2[0], prec, v2[1], prec, v2[2] );
+    fprintf( file,"vertex %.*e %.*e %.*e\n", prec, v3[0], prec, v3[1], prec, v3[2] );
     fprintf( file,"endloop\n" );
     fprintf( file,"endfacet\n" );
   }

Modified: MOAB/trunk/WriteSTL.hpp
===================================================================
--- MOAB/trunk/WriteSTL.hpp	2008-02-29 21:01:17 UTC (rev 1628)
+++ MOAB/trunk/WriteSTL.hpp	2008-03-03 17:47:33 UTC (rev 1629)
@@ -69,7 +69,8 @@
     //! Write list of triangles to an STL file.  
   MBErrorCode ascii_write_triangles( FILE* file,
                                      const char header[82],
-                                     const MBRange& triangles );
+                                     const MBRange& triangles,
+                                     int precision );
     //! Write list of triangles to an STL file.  
   MBErrorCode binary_write_triangles( FILE* file,
                                       const char header[82],

Modified: MOAB/trunk/WriteVtk.cpp
===================================================================
--- MOAB/trunk/WriteVtk.cpp	2008-02-29 21:01:17 UTC (rev 1628)
+++ MOAB/trunk/WriteVtk.cpp	2008-03-03 17:47:33 UTC (rev 1629)
@@ -40,10 +40,13 @@
 #include "MBTagConventions.hpp"
 #include "MBWriteUtilIface.hpp"
 #include "MBInternals.hpp"
+#include "FileOptions.hpp"
 
 #define INS_ID(stringvar, prefix, id) \
 sprintf(stringvar, prefix, id)
 
+const int DEFAULT_PRECISION = 10;
+
 MBWriterIface *WriteVtk::factory( MBInterface* iface )
   { return new WriteVtk( iface ); }
 
@@ -70,13 +73,18 @@
 
 MBErrorCode WriteVtk::write_file(const char *file_name, 
                                  const bool overwrite,
-                                 const FileOptions&,
+                                 const FileOptions& opts,
                                  const MBEntityHandle *output_list,
                                  const int num_sets,
                                  std::vector<std::string>& ,
                                  int )
 {
   MBErrorCode rval;
+
+    // Get precision for node coordinates
+  int precision;
+  if (MB_SUCCESS != opts.get_int_option( "PRECISION", precision ))
+    precision = DEFAULT_PRECISION;
   
     // Get entities to write
   MBRange nodes, elems;
@@ -99,6 +107,7 @@
     writeTool->report_error("Could not open file: %s\n", file_name );
     return MB_FILE_WRITE_ERROR;
   }
+  file.precision( precision );
   
     // Write file
   if ((rval = write_header(file              )) != MB_SUCCESS ||
@@ -232,7 +241,7 @@
   stream << "POINTS " << nodes.size() << " double" << std::endl;
   for (unsigned long i = 0; i < n; ++i, ++x, ++y, ++z )
     stream << *x << ' ' << *y << ' ' << *z << std::endl;
-
+  
   return MB_SUCCESS;
 }
 




More information about the moab-dev mailing list