/** Read sgf formatted file, create a temporary grid and write a vtk file */ int main( int argc, char * argv[] ) { namespace sgf2vtk = tools::converter::sgf2vtk; namespace sgf2xx = tools::converter::sgf2xx; // Sanity check of the number of input arguments if ( (argc != 2) and (argc != 3) ) { std::cout << "Usage: " << argv[0] << " file.sgf [degree=1] \n\n"; return 0; } // Name of sgf input file, its basename and the vtk output file name const std::string sgfFile = boost::lexical_cast<std::string>( argv[1] ); const std::string base = sgfFile.substr(0, sgfFile.find( ".sgf") ); const std::string vtkFile = base + ".vtk"; // geomtry degree const unsigned degree = (argc == 2) ? 1 : boost::lexical_cast<unsigned>( argv[2] ); // Grid dimension unsigned dim; { // extract data from header std::ifstream sgf( sgfFile.c_str() ); dim = sgf2xx::readDimFromSGFHeader( sgf ); sgf.close(); } // Input and output file streams std::ifstream sgf( sgfFile.c_str() ); std::ofstream vtk( vtkFile.c_str() ); // Call generic conversion helper sgf2xx::Conversion< sgf2vtk::Converter >::apply( dim, degree, sgf, vtk ); // Close the streams sgf.close(); vtk.close(); return 0; }
GTPResponse GTP::gtp_load_sgf(vecstr args){ if(args.size() == 0) return GTPResponse(true, "load_sgf <filename>"); std::ifstream infile(args[0].c_str()); if(!infile) { return GTPResponse(false, "Error opening file " + args[0] + " for reading"); } SGFParser<Move> sgf(infile); if(sgf.game() != Board::name){ infile.close(); return GTPResponse(false, "File is for the wrong game: " + sgf.game()); } int size = sgf.size(); if(size != hist->get_size()){ if(hist.len() == 0){ hist = History(size); set_board(); time_control.new_game(); }else{ infile.close(); return GTPResponse(false, "File has the wrong boardsize to match the existing game"); } } Side s = Side::P1; while(sgf.next_node()){ Move m = sgf.move(); move(m); // push the game forward s = ~s; } if(sgf.has_children()) agent->load_sgf(sgf); assert(sgf.done_child()); infile.close(); return true; }
GTPResponse GTP::gtp_save_sgf(vecstr args){ int limit = -1; if(args.size() == 0) return GTPResponse(true, "save_sgf <filename> [work limit]"); std::ifstream infile(args[0].c_str()); if(infile) { infile.close(); return GTPResponse(false, "File " + args[0] + " already exists"); } std::ofstream outfile(args[0].c_str()); if(!outfile) return GTPResponse(false, "Opening file " + args[0] + " for writing failed"); if(args.size() > 1) limit = from_str<unsigned int>(args[1]); SGFPrinter<Move> sgf(outfile); sgf.game(Board::name); sgf.program(gtp_name(vecstr()).response, gtp_version(vecstr()).response); sgf.size(hist->get_size()); sgf.end_root(); Side s = Side::P1; for(auto m : hist){ sgf.move(s, m); s = ~s; } agent->gen_sgf(sgf, limit); sgf.end(); outfile.close(); return true; }
/** In- and output of a structured mesh. * Does the same job as ref01::unstructured, but is using the SGF format * (base::io::sgf) for structured meshes. These are meshes in which every * element is a hypercube, every interior node has 2 x DIM direct neighbors * (as connected by edges) and a lexicographic ordering of the indices is used. * Structured meshes have many convenience advantages and have therefore been * introduced to inSilico. Nevertheless, almost all applications are based * on unstructured meshes for the sake of generality and the power of structured * meshes has not been fully exploited. * * \param[in] argc Number of command line arguments * \param[in] argv Values of command line arguments */ int ref01::structured( int argc, char* argv[] ) { //-------------------------------------------------------------------------- // Definition of the spatial dimension const unsigned dim = SPACEDIM; // Definition of the polynomial degree of the geometry approximation const unsigned geomDeg = 1; // The output can be with a higher resolution const unsigned resolution = 2; // Boundary mesh shall be composed of simplex elements const bool boundarySimplex = true; // Check the number of input arguments if ( argc != 2 ) { // note argv[0] is the program name itself usageMessage2<dim,geomDeg>( argv[0] ); return 0; } // convert input argument to a string object const std::string sgfFileName = boost::lexical_cast<std::string>( argv[1] ); // extract the basename of the file const std::string baseName = base::io::baseName( sgfFileName, ".sgf" ); typedef base::Structured<dim,geomDeg> Grid; // Create an empty mesh object Grid grid; //-------------------------------------------------------------------------- { // input file stream from smf file std::ifstream sgf( sgfFileName.c_str() ); // read sgf mesh base::io::sgf::readGrid( sgf, grid ); // close the stream sgf.close(); } //-------------------------------------------------------------------------- // Creates a list of <Element,faceNo> pairs base::mesh::MeshBoundary gridBoundary; gridBoundary.create<dim>( grid.gridSizes() ); typedef base::mesh::BoundaryMeshBinder<Grid::Element, boundarySimplex>::Type BoundaryMesh; BoundaryMesh boundaryMesh; { // Create a real mesh object from this list base::mesh::generateBoundaryMesh( gridBoundary.begin(), gridBoundary.end(), grid, boundaryMesh ); } //-------------------------------------------------------------------------- // Output functions { // VTK file of the input mesh { // file name for vtk mesh file const std::string vtkMeshFileName = baseName + ".vtk"; // output file stream std::ofstream vtk( vtkMeshFileName.c_str() ); // create a VTK writer base::io::vtk::LegacyWriter vtkWriter( vtk, resolution ); // write the mesh vtkWriter.writeStructuredGrid( grid ); // close the stream vtk.close(); } // SMF file of the boundary mesh { // file name for boundary mesh output const std::string smfBoundaryFileName = baseName + "_boundary.smf"; // output stream std::ofstream smf( smfBoundaryFileName.c_str() ); // write to smf base::io::smf::writeMesh( boundaryMesh, smf ); // close stream smf.close(); } } return 0; }