/* Reading problem and world configuration files. You should not edit this file. We will use exactly this version of it in grading; any modifications you make will be lost. */ #ifndef _INPUT_H #define _INPUT_H #include #include #include #include #include void skip_rest_of_line(std::istream &in) { in.ignore(1000000, '\n'); } void skip_white_comment(std::istream &in) { char c; while(!in.eof()) { in >> c; if(c == '#') // comment skip_rest_of_line(in); else if(c > 0 && !isspace(c)) { in.putback(c); break; } } } class World : public dolt::drawableObject { public: // access the polygon describing the boundary of the world inline const dolt::drawablePolygon & boundary() const { return b; } // access the list of polygonal obstacles in the world inline const std::list & obstacles() const { return o; } void drawGL( const dolt::Graphics &out ) const { b.drawGL(out); std::list::const_iterator i = o.begin(); for(; i != o.end(); ++i) i->drawGL(out); } void drawPS( const dolt::Graphics &out, dolt::psFile &ps, const dolt::Bbox &bounds, const double &scale ) const { b.drawPS(out, ps, bounds, scale); std::list::const_iterator i = o.begin(); for(; i != o.end(); ++i) i->drawPS(out, ps, bounds, scale); } bool read_file(const char *file) { std::ifstream in(file); if(!in) return false; // read boundary skip_white_comment(in); if(in.eof() || in.bad()) return true; in >> b; if(in.bad()) { std::cerr << "Bad world file " << file << " (reading boundary)" << std::endl; return false; } b.setFilled(true); b.setLineWidth(8); b.setColor("gray80"); b.setBorderColor("gray10"); // read obstacles dolt::Polygon p; while(!in.eof()) { p.clear(); skip_white_comment(in); if(in.eof() || in.bad()) return true; in >> p; if(in.bad()) return true; o.push_back(p); o.back().setFilled(true); o.back().setLineWidth(0); o.back().setColor("gray10"); } return true; } protected: dolt::drawablePolygon b; std::list o; }; const char RandomSampling[] = "random"; const char QuasirandomSampling[] = "quasirandom"; class Problem { public: enum Sampling { random, quasirandom }; Problem() : _N(100), _k(5), r(1), _method(random) {} // accessors for information from the problem description inline dolt::Point start() const { return s; } inline dolt::Point goal() const { return g; } inline double robot_radius() const { return r; } inline unsigned int N() const { return _N; } inline unsigned int k() const { return _k; } inline Sampling method() const { return _method; } bool read_file(const char *file, World &w) { std::ifstream in(file); if(!in) return false; if(!read_variable(in, "world", worldfile)) { std::cerr << "Bad problem file " << file << " (reading world)" << std::endl; return false; } if(!w.read_file(worldfile.c_str())) { std::cerr << "Error reading world file, giving up" << std::endl; return false; } if(!read_variable(in, "robot-start", s)) { std::cerr << "Bad problem file " << file << " (reading robot-start)" << std::endl; return false; } if(!read_variable(in, "robot-goal", g)) { std::cerr << "Bad problem file " << file << " (reading robot-goal)" << std::endl; return false; } if(!read_variable(in, "robot-radius", r)) { std::cerr << "Bad problem file " << file << " (reading robot-radius)" << std::endl; return false; } if(!read_variable(in, "N", _N)) { std::cerr << "Bad problem file " << file << " (reading N)" << std::endl; return false; } if(!read_variable(in, "k", _k)) { std::cerr << "Bad problem file " << file << " (reading k)" << std::endl; return false; } std::string sm; if (!read_variable(in, "sampling-method", sm)) { std::cerr << "Bad problem file " << file << " (reading k)" << std::endl; return false; } else if (sm == RandomSampling) _method = random; else if (sm == QuasirandomSampling) _method = quasirandom; else { std::cerr << "Bad problem file " << file << ": sampling-method " << "must be either \"" << RandomSampling << "\" or \"" << QuasirandomSampling << "\"" << std::endl; return false; } return true; } protected: template bool read_variable(std::ifstream &in, const char *name, T &value) { std::string n; char equal; skip_white_comment(in); if(in.eof() || in.bad()) return false; in >> n; if(in.bad() || n != std::string(name)) return false; skip_white_comment(in); in >> equal; if(in.bad() || equal != '=') return false; in >> value; if(in.bad()) return false; return true; } std::string worldfile; dolt::Point s, g; unsigned int _N, _k; double r; Sampling _method; }; #endif // _INPUT_H