Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- This is the function where I fill the array data from a file.
- void read_egm(string fname, string &ftype, Header &egm, short * &data_e4, float * &data_e1, double * &dnew)
- {
- ifstream ifs; //opening the input filestream
- ifs.open(fname); // open the file
- if (!ifs) // checks to make sure the file was successfully opened
- {
- cerr << "cannot open file" << endl;
- }
- char* type = new char[2]; // declaring a char pointer to determine if the file is e1 or e4
- ifs.read(type, 2);
- ftype = type[1]; // assigns the variable ftype to the value of type (e1 or e4)
- cout << ftype << endl;
- delete[] type; // cleaning up when Im finished with the char pointer.
- type = nullptr; // setting type to null
- if (ftype == "1") // if the file was e1
- {
- ifs >> egm.nr >> egm.nc >> egm.min >> egm.max >> egm.Xscale >> egm.Yscale; // read in the header information using >> operator
- int size = egm.nc*egm.nr; // declaring the variable size to be equal to total number of values in file
- data_e1 = new float[size]; // creating a pointer array to hold the data in the file.
- cout << egm.nc << " " << egm.nr << endl;;
- for (int i = 0; i<size; i++) // for loop to fill the dynamically allocated array with values.
- {
- ifs >> data_e1[i];
- }
- cout << data_e1[0] << endl;
- }
- else if (ftype == "4")
- {
- ifs.close();
- ifs.open(fname, ios::binary);
- type = new char[8];
- ifs.read(type, 8);
- cout << type << endl;
- int * header = new int[6];
- ifs.read(reinterpret_cast<char*>(header), 6 * sizeof(int));
- egm.nc = header[0];
- egm.nr = header[1];
- egm.min = header[2];
- egm.max = header[3];
- egm.Xscale = header[4];
- egm.Yscale = header[5];
- cout << egm.nc << " " << egm.nr << " " << egm.min << " " << egm.max << " " << egm.Xscale << " " << egm.Yscale << endl;
- int size = egm.nc*egm.nr;
- data_e4 = new short[size];
- ifs.read(reinterpret_cast<char*>(data_e4), size * sizeof(short));
- dnew = new double[size];
- for (int i = 0; i<size; i++)
- {
- dnew[i] = data_e4[i];
- }
- ifs.close();
- }
- }
- Then i do calculations on the data and put the new values in another array
- void illuminate_PGM_from_EGM(string ftype, string outname, Header egm, short * &data_e4, Vec3d v, float* &data_e1, Vec3d sun, int* &newdat, double* &dnew)
- {
- Vec3d a, b;
- sun = get_sun_angle(); // prompts user for sun azimuth and elevation
- int* prev = new int[egm.nc*egm.nr];
- newdat = new int[egm.nc*egm.nr];
- for (int r = 0; r<egm.nr; r++) // for each row
- {
- for (int c = 0; c<egm.nc; c++)// for each column
- {
- a.x = egm.Xscale; // setting x in vec a to be the xscaling
- a.y = 1; // setting y in vec a to be 1;
- int pointa = data_e1[r*egm.nc + c + 1] - data_e1[r*egm.nc + c]; // finding the difference of a value and the value to its right
- a.z = pointa; // setting z in vec a to the difference of the points
- b.x = 1; // setting x in vec b to 1
- b.y = egm.Yscale; // setting y in vec b to the yscaling
- int pointb = data_e1[r + 1 * egm.nc + c] - data_e1[r*egm.nc + c]; // finding the change in elevation from the original elevation and the elevation directly beneath it
- b.z = pointb; // setting z in vec b to that value that was found above
- Vec3d anorm = unit_vector(a); // finding the unit vector of vec a
- Vec3d bnorm = unit_vector(b); // finding the unit vector of vec b
- Vec3d snorm = cross(bnorm, anorm); // finding the cross product of normalized vectors b and a
- double brightness = dot_prod(snorm, sun, brightness); // finding the brightness at each point by calculating the dot product of the surface normal and the normalized vector of the sun
- int bness = ((.9*brightness) + .1) * 255; // bness(brightness value) is the dot product times 255 (to get a value between 0 and 255 for pgm) .9 and +.1 added to represent the sky brighntess
- // if the brighness is negative (cosine of the angle was negative, meaning the brighness of that point is in a shadow) set the brightness to a positive number
- if (bness <0)
- {
- bness = 0; //- abs(bness) ;
- }
- newdat[r*egm.nc + c] = bness;
- prev[r*egm.nc + c] = bness;
- }
- }
- delete[] prev;
- prev = nullptr;
- }
- Then I write out the new values to a file
- void write_pgm(string outname, Header egm, float * &data_e1, int* newdat, double* &dnew)
- {
- ofstream opfs;
- opfs.open(outname + ".pgm");
- opfs << "P2" << endl;
- opfs << egm.nr << endl;
- opfs << egm.nc << endl;
- opfs << 255 << endl;
- for (int r = 0; r<egm.nr; r++)
- {
- for (int c = 0; c<egm.nc; c++)
- {
- opfs << newdat[r*egm.nc + c];
- opfs << "\t";
- }opfs << endl;
- }
- opfs.close();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement