26 void IO_Polytope::load(
const std::string& filename, boost::shared_ptr<PolyhedralCone_Rn> POLY) {
28 int dimension, numberOfGenerators, numberOfHalfSpaces;
29 std::ifstream file(filename.c_str(), std::ifstream::in);
32 std::string s(
"Unable to open ");
35 throw std::ios_base::failure(s);
39 std::vector< boost::shared_ptr<HalfSpace_Rn> > HSvect;
42 std::getline(file,line);
44 std::getline(file,line);
45 std::istringstream iline(line);
47 iline >> numberOfHalfSpaces;
48 iline >> numberOfGenerators;
53 if (numberOfHalfSpaces != 0) {
55 std::getline(file,line);
57 boost::shared_ptr<HalfSpace_Rn> HS;
58 for (
int hyp_count=0; hyp_count<numberOfHalfSpaces; hyp_count++) {
60 std::getline(file,line);
61 std::istringstream iline3(line);
66 for (
int coord_count=0; coord_count<dimension; coord_count++) {
68 HS->setCoefficient(coord_count, val);
70 POLY->addHalfSpace(HS);
75 if (numberOfGenerators == 0) {
84 std::getline(file,line);
86 boost::shared_ptr<Generator_Rn> VX;
87 for (
int vtx_count=0; vtx_count<numberOfGenerators; vtx_count++) {
89 std::getline(file,line);
90 std::istringstream iline2(line);
97 POLY->addGenerator(VX);
102 if (numberOfHalfSpaces != 0 && numberOfGenerators != 0) {
104 std::getline(file,line);
106 for (
int vtx_count=0; vtx_count<numberOfGenerators; vtx_count++) {
107 VX = POLY->getGenerator(vtx_count);
108 std::string lineOfFacets;
109 std::getline(file, lineOfFacets);
110 std::istringstream stream(lineOfFacets);
111 int facetForGenerator;
112 while (stream >> facetForGenerator) {
113 if (facetForGenerator < 0 || facetForGenerator >= numberOfHalfSpaces) {
114 std::ostringstream stream_;
115 stream_ <<
"Facet number ";
116 stream_ << facetForGenerator;
117 stream_ <<
" is outside the range [0, ";
118 stream_ << numberOfHalfSpaces;
119 stream_ <<
"] in function IO_Polytope::load() ";
120 std::string valString = stream_.str();
121 throw std::out_of_range(valString);
123 boost::shared_ptr<HalfSpace_Rn> Fi = HSvect[facetForGenerator];
132 void IO_Polytope::save(
const std::string& filename, boost::shared_ptr<PolyhedralCone_Rn> POLY) {
133 std::ofstream ofs(filename.c_str());
136 std::string s(
"Unable to open ");
139 throw std::ios_base::failure(s);
145 ofs <<
"# Dimension NumberOfHalfspaces NumberOfGenerators" << std::endl;
146 ofs << POLY->dimension() <<
" ";
147 ofs << POLY->numberOfHalfSpaces() <<
" ";
148 ofs << POLY->numberOfGenerators() << std::endl;
152 if (POLY->numberOfHalfSpaces() != 0) {
153 ofs <<
"# HALFSPACES : a0 + a1.x1 + ... + an.xn >= 0." << std::endl;
156 iteHS(POLY->getListOfHalfSpaces());
158 boost::shared_ptr<HalfSpace_Rn> HS = iteHS.
current();
160 ofs << HS->getConstant() <<
" ";
161 for (
unsigned int coord_count=0; coord_count<POLY->dimension(); coord_count++) {
162 ofs << HS->getCoefficient(coord_count) <<
" ";
167 ofs <<
"# GENERATORS : V = (v1, ..., vn)" << std::endl;
172 boost::shared_ptr<Generator_Rn> VX;
173 for (
unsigned int vtx_count=0; vtx_count<POLY->numberOfGenerators(); vtx_count++) {
174 VX = POLY->getGenerator(vtx_count);
184 if (POLY->numberOfHalfSpaces() != 0) {
185 ofs <<
"# GENERATOR CONNECTION : Ha, Hb, ..." << std::endl;
187 for (
unsigned int vtx_count=0; vtx_count<POLY->numberOfGenerators(); vtx_count++) {
188 VX = POLY->getGenerator(vtx_count);
189 for (
unsigned int ngb_count=0; ngb_count<VX->numberOfFacets(); ngb_count++) {
190 boost::shared_ptr<HalfSpace_Rn> Fi = VX->getFacet(ngb_count);
191 ofs << POLY->getHalfSpaceNumber(Fi) <<
" ";