1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146
|
#include <string>
#include <sstream>
#include <fstream>
#include <iostream>
#include <stdio.h>
using namespace std;
struct particle
{
double x;
double y;
double z;
double radius;
double dencity;
int type;
};
// Define input and output operators to make code easier:
std::istream& operator>>(std::istream& is, particle& p)
{
is >> p.x;
is >> p.y;
is >> p.z;
is >> p.radius;
is >> p.dencity;
is >> p.type;
return is;
}
// Now you can write: in >> p;
std::ostream& operator<<(std::ostream& os, const particle& p)
{
os << p.x << '\t';
os << p.y << '\t';
os << p.z << '\t';
os << p.radius << '\t';
os << p.dencity << '\t';
os << p.type << '\n';
return os;
}
// Now you can write: out << p;
// Helpful function to read in a line as a particle
std::istream& getparticle(std::istream& is, particle& p)
{
std::string line;
if(std::getline(is, line))
{
std::istringstream iss(line);
iss >> p;
}
return is;
}
int process(std::ifstream& ifs, std::ostream& os, double y)
{
// reset pos is required for when first element
// of current y value was read from previous call
// This forces a recalculation of file position
ifs.seekg(0);
bool next = false;
int count = 0;
particle p;
while(getparticle(ifs, p))
{
if (p.y != y && count == 0) // For jumping once only
{
int line = int (2024807438/15000); //no. of particles / layers
int size = 84; // average size of each line
int jump = int (y/0.0021334 + 0.5); // layers to be skipped
// My test data
// line = 10; // debug
// size = 28; // debug
// jump = int(y/0.001 + 0.5); // debug
// ensure we don't overshoot
int pos = (line * size * jump) - (2 * size);
if(pos < 0) { pos = 0; }
// total jump
ifs.seekg(pos); // was seeking on wrong stream
count++;
if(pos != 0)
{
// MUST align read pointer after seekg()
std::string align;
std::getline(ifs, align); // moves to start of next line
}
// read in new particle in here
// to satisfy rest of loop
getparticle(ifs, p);
}
if(p.y == y)
{
// additional conditions here
if(p.dencity != 1.95 && p.y > 0.000001)
{
os << p;
}
next = true;
}
if(p.y != y && next == true)
{
return 0;
}
}
return 0;
}
int main(int argc, char* argv[])
{
// make input filename a parameter
if(argc < 2)
{
std::cerr << "Usage: particle_filter <filename>" << std::endl;
return 1;
}
// open input file
std::ifstream ifs(argv[1]);
if(!ifs)
{
std::cerr << "Error: opening file " << argv[2] << std::endl;
return 1;
}
// Might as well just open this once
ofstream fout("layer.cast", ios::app | ios::out);
double y = 0.00482236; // initial y value
double cast_step = 0.001; // step value
int layers = 2; // number of layers to extract
// y = 0.002; // debug
for(size_t i(0); i < layers; ++i, y+= cast_step)
{
process(ifs, fout, y);
}
return 0;
}
| |