26 void IO_Polytope::load(
const std::string& filename, boost::shared_ptr<PolyhedralCone_Rn> POLY)
27 throw (std::ios_base::failure,std::out_of_range) {
29 int dimension, numberOfGenerators, numberOfHalfSpaces;
30 std::ifstream file(filename.c_str(), std::ifstream::in);
33 std::string s(
"Unable to open ");
36 throw std::ios_base::failure(s);
40 std::vector< boost::shared_ptr<HalfSpace_Rn> > HSvect;
43 std::getline(file,line);
45 std::getline(file,line);
46 std::istringstream iline(line);
48 iline >> numberOfHalfSpaces;
49 iline >> numberOfGenerators;
54 if (numberOfHalfSpaces != 0) {
56 std::getline(file,line);
58 boost::shared_ptr<HalfSpace_Rn> HS;
59 for (
int hyp_count=0; hyp_count<numberOfHalfSpaces; hyp_count++) {
61 std::getline(file,line);
62 std::istringstream iline3(line);
67 for (
int coord_count=0; coord_count<dimension; coord_count++) {
69 HS->setCoefficient(coord_count, val);
71 POLY->addHalfSpace(HS);
76 if (numberOfGenerators == 0) {
85 std::getline(file,line);
87 boost::shared_ptr<Generator_Rn> VX;
88 for (
int vtx_count=0; vtx_count<numberOfGenerators; vtx_count++) {
90 std::getline(file,line);
91 std::istringstream iline2(line);
98 POLY->addGenerator(VX);
103 if (numberOfHalfSpaces != 0 && numberOfGenerators != 0) {
105 std::getline(file,line);
107 for (
int vtx_count=0; vtx_count<numberOfGenerators; vtx_count++) {
108 VX = POLY->getGenerator(vtx_count);
109 std::string lineOfFacets;
110 std::getline(file, lineOfFacets);
111 std::istringstream stream(lineOfFacets);
112 int facetForGenerator;
113 while (stream >> facetForGenerator) {
114 if (facetForGenerator < 0 || facetForGenerator >= numberOfHalfSpaces) {
115 std::ostringstream stream_;
116 stream_ <<
"Facet number ";
117 stream_ << facetForGenerator;
118 stream_ <<
" is outside the range [0, ";
119 stream_ << numberOfHalfSpaces;
120 stream_ <<
"] in function IO_Polytope::load() ";
121 std::string valString = stream_.str();
122 throw std::out_of_range(valString);
124 boost::shared_ptr<HalfSpace_Rn> Fi = HSvect[facetForGenerator];
133 void IO_Polytope::save(
const std::string& filename, boost::shared_ptr<PolyhedralCone_Rn> POLY)
throw (std::ios_base::failure) {
134 std::ofstream ofs(filename.c_str());
137 std::string s(
"Unable to open ");
140 throw std::ios_base::failure(s);
146 ofs <<
"# Dimension NumberOfHalfspaces NumberOfGenerators" << std::endl;
147 ofs << POLY->dimension() <<
" ";
148 ofs << POLY->numberOfHalfSpaces() <<
" ";
149 ofs << POLY->numberOfGenerators() << std::endl;
153 if (POLY->numberOfHalfSpaces() != 0) {
154 ofs <<
"# HALFSPACES : a0 + a1.x1 + ... + an.xn >= 0." << std::endl;
157 iteHS(POLY->getListOfHalfSpaces());
158 for (iteHS.begin(); iteHS.end()!=
true; iteHS.next()) {
159 boost::shared_ptr<HalfSpace_Rn> HS = iteHS.
current();
161 ofs << HS->getConstant() <<
" ";
162 for (
unsigned int coord_count=0; coord_count<POLY->dimension(); coord_count++) {
163 ofs << HS->getCoefficient(coord_count) <<
" ";
168 ofs <<
"# GENERATORS : V = (v1, ..., vn)" << std::endl;
173 boost::shared_ptr<Generator_Rn> VX;
174 for (
unsigned int vtx_count=0; vtx_count<POLY->numberOfGenerators(); vtx_count++) {
175 VX = POLY->getGenerator(vtx_count);
185 if (POLY->numberOfHalfSpaces() != 0) {
186 ofs <<
"# GENERATOR CONNECTION : Ha, Hb, ..." << std::endl;
188 for (
unsigned int vtx_count=0; vtx_count<POLY->numberOfGenerators(); vtx_count++) {
189 VX = POLY->getGenerator(vtx_count);
190 for (
unsigned int ngb_count=0; ngb_count<VX->numberOfFacets(); ngb_count++) {
191 boost::shared_ptr<HalfSpace_Rn> Fi = VX->getFacet(ngb_count);
192 ofs << POLY->getHalfSpaceNumber(Fi) <<
" ";
A half-space whose frontier is a linear (n-1) dimension space. _constant + _coefficients[0].x1 + ... + _coefficients[n-1].xn >= 0.
const GEOMETRIC_OBJECT current()
Return the current geometric element.
static polito_EXPORT void load(const std::string &filename, boost::shared_ptr< PolyhedralCone_Rn > POLY)
Load the main data format to store polytopes.
static polito_EXPORT void save(const std::string &filename, boost::shared_ptr< PolyhedralCone_Rn > POLY)
Save the polytope to the main data format.
A n-coordinates generator, which can be a vertex or an edge whether it is contained by a polytope or ...
This class is designed to run the list of all geometric objects representing a polytope.