Store information from a File in a Class object (C++) -
this question has answer here:
i want read in input file class object using c++.
here class object trying read file input array had problems reading in lines after "polygon" keyword.
class polygon{ public: int object_number; float point_count; float px[size][size]; float py[size][size]; float pz[size][size]; float nx[size], ny[size], nz[size]; };
i want able store information after polygon keyword until next polygon keyword.
so, first polygon in file.
object_number = 0; point_count = 4; px[0][0] = 100; py[0][0]= 100; pz[0][0] = 200; nx[0][0] = 0.398925; ny[0][0] =0.598388; nz[0][0] = -0.453324;
and forth until reaches next "poylgon" keyword in file. i'm going use information perform phong shading calculations
the file contains.
polygon 0 4 100 100 200 0.398925 0.598388 -0.453324 125 100 178 0.352837 0.646868 -0.490051 125 125 145 0.396981 0.595472 -0.551363 100 125 167 0.448836 0.550844 -0.510041 polygon 1 4 100 125 167 0.448836 0.550844 -0.510041 125 125 145 0.396981 0.595472 -0.551363 125 150 118 0.447405 0.521972 -0.621396 100 150 140 0.505846 0.482853 -0.574825 polygon 0 4 100 150 140 0.505846 0.482853 -0.574825 125 150 118 0.447405 0.521972 -0.621396 125 175 97 0.501037 0.417531 -0.695885 100 175 119 0.566484 0.386239 -0.643732 polygon 1 4 100 175 119 0.566484 0.386239 -0.643732 125 175 97 0.501037 0.417531 -0.695885 125 200 82 0.501037 0.417531 -0.695885 100 200 104 0.566484 0.386239 -0.643732 .......
unless have specific reason otherwise (such needing pass points gpu single block of memory), it's going lot easier break more manageable pieces.
class point { float px, py, pz, nx, ny, nz; // not sure order in file, may need fix this: friend std::istream &operator>>(std::istream &is, point &p) { return >> p.nx >> p.ny >> p.nz >> p.px >> p.py >> p.pz; } }; class polygon { int object_number; std::vector<point> points; friend std::istream &operator>>(std::istream &is, polygon &p) { int point_count; std::string check; >> check; if (check != "polygon") { is.setstate(std::ios::failbit); return is; } >> p.object_number >> point_count; (int i=0; i<p.object_number; i++) { point p2; >> p2; p.points.push_back(p2); } return is; } };
from there, can read entire file full of polygons easily:
std::ifstream in("mypolygons.txt"); std::vector<polygon> polygons { std::istream_iterator<polygon>(in), std::istream_iterator<polygon>() };
Comments
Post a Comment