|
|
|
|
|
|
| |
| |
|
|
|
|
| |
| |
|
|
I'm trying to create a utility that converts a text file (testdata.txt) into a
df3 file (testdata.df3). It takes a file that might look like
3 3 3
0 1 2 255
0 2 0 128
(first 3 characters are the dimensions,
next 3 characters are the index, next character is the value,
next 3 characters are the index, next character is the value,
etc.)
No matter what I try, and no matter what values I use in the input file, when it
comes time to render I either get an interior of completely filled, or empty.
I'm not proficient enough with C++ to know what I'm doing wrong. The source code
for using vc++ is as follows
#include <iostream>
#include <fstream>
using namespace std;
// max min functions
float max(float n1, float n2){
if(n1>=n2){return n1;}
else{return n2;}
}
float min(float n1, float n2){
if(n1>=n2){return n2;}
else{return n1;}
}
int main(){
int nx_size, ny_size, nz_size, nt; // dimensions
int vx, vy, vz;
float vt;
int s;
char dxb, dxe, dyb, dye, dzb, dze;
char char_send;
char curr='t';
float ***new_arr;
float themin=1e32,themax=-1e32;
char v;
float float_send;
ifstream inFile; // On the stack
ofstream outFile; // On the stack
outFile.open( "testdata.df3", ios::binary );
inFile.open( "testdata.txt", ios::binary );
// Create Array
inFile >> nx_size >> ny_size >> nz_size ;
new_arr = new float**[nx_size];
for(int i=0;i<nx_size;i++)
new_arr[i]=new float*[ny_size];
for(int i=0;i<nx_size;i++)
for(int j=0;j<ny_size;j++)
new_arr[i][j] = new float[nz_size];
// Write headers
dxb = (nx_size >> 8);
dxe = (nx_size & 0xff);
dyb = (ny_size >> 8);
dye = (ny_size & 0xff);
dzb = (nz_size >> 8);
dze = (nz_size & 0xff);
outFile.write( &dxb, sizeof(dxb) );
outFile.write( &dxe, sizeof(dxe) );
outFile.write( &dxb, sizeof(dyb) );
outFile.write( &dxe, sizeof(dye) );
outFile.write( &dxb, sizeof(dzb) );
outFile.write( &dxe, sizeof(dze) );
// Initialize with 0s
for(int i=0;i<nx_size;i++)
for(int j=0;j<ny_size;j++)
for(int k=0;k<nz_size;k++)
new_arr[i][j][k]=0;
// Get data
while(inFile){
inFile >> s;
if(inFile){
switch(curr){
case 'x': curr='y'; break;
case 'y': curr='z'; break;
case 'z': curr='t'; break;
case 't': curr='x'; break;
}
switch(curr){
case 'x':
vx=s; break;
case 'y':
vy=s; break;
case 'z':
vz=s; break;
case 't':
vt=s;
new_arr[vx][vy][vz]=vt;
break;
}
}
else{
if(curr!='t'){
cout << "missing parameter"<< endl;
}
}
}
// Write Data
for (int i=0;i<nx_size;i++) {
for (int j=0;j<ny_size;j++) {
for (int k=0;k<nz_size;k++) {
themax = max(themax,new_arr[i][j][k]);
themin = min(themin,new_arr[i][j][k]);
}
}
}
if (themin >= themax) {
themax = themin + 1;
themin -= 1;
}
for (int k=0;k<nz_size;k++) {
for (int j=0;j<ny_size;j++) {
for (int i=0;i<nx_size;i++) {
v = (char)(255 * (new_arr[i][j][k]-themin)/(themax-themin));
outFile.write( &v, sizeof(v) );
}
}
}
outFile.close();
inFile.close();
return 0;
}
Post a reply to this message
|
|
| |
| |
|
|
|
|
| |
| |
|
|
Woody <nomail@nomail> wrote:
> I'm trying to create a utility that converts a text file (testdata.txt) into a
> df3 file (testdata.df3). It takes a file that might look like
> 3 3 3
> 0 1 2 255
> 0 2 0 128
> (first 3 characters are the dimensions,
> next 3 characters are the index, next character is the value,
> next 3 characters are the index, next character is the value,
> etc.)
> No matter what I try, and no matter what values I use in the input file, when it
> comes time to render I either get an interior of completely filled, or empty.
That might also be because of your scene settings.
Anyways, here's a clean C++ implementation of your task (untested in
povray, but I assume it will work):
#include <iostream>
#include <fstream>
#include <vector>
int main()
{
std::ifstream is("testdata.txt");
if(!is.good()) { std::cerr << "Couldn't open testdata.txt\n"; return 1; }
size_t xSize, ySize, zSize;
is >> xSize >> ySize >> zSize;
std::vector<char> data(xSize*ySize*zSize + 6, 0);
data[0] = xSize/256; data[1] = xSize%256;
data[2] = ySize/256; data[3] = ySize%256;
data[4] = zSize/256; data[5] = zSize%256;
while(true)
{
int x, y, z, value;
is >> x >> y >> z >> value;
if(!is.good()) break;
data.at(x + y*xSize + z*xSize*ySize + 6) = value;
}
std::ofstream os("testdata.df3", std::ios::binary);
os.write(&data[0], data.size());
}
--
- Warp
Post a reply to this message
|
|
| |
| |
|
|
|
|
| |
| |
|
|
It works. Thanks.
> That might also be because of your scene settings.
> Anyways, here's a clean C++ implementation of your task (untested in
> povray, but I assume it will work):
>
>
> #include <iostream>
> #include <fstream>
> #include <vector>
>
> int main()
> {
> std::ifstream is("testdata.txt");
> if(!is.good()) { std::cerr << "Couldn't open testdata.txt\n"; return 1; }
>
> size_t xSize, ySize, zSize;
> is >> xSize >> ySize >> zSize;
>
> std::vector<char> data(xSize*ySize*zSize + 6, 0);
>
> data[0] = xSize/256; data[1] = xSize%256;
> data[2] = ySize/256; data[3] = ySize%256;
> data[4] = zSize/256; data[5] = zSize%256;
>
> while(true)
> {
> int x, y, z, value;
> is >> x >> y >> z >> value;
> if(!is.good()) break;
> data.at(x + y*xSize + z*xSize*ySize + 6) = value;
> }
>
> std::ofstream os("testdata.df3", std::ios::binary);
> os.write(&data[0], data.size());
> }
>
>
> --
> - Warp
Post a reply to this message
|
|
| |
| |
|
|
|
|
| |
| |
|
|
Warp escreveu:
> std::ifstream is("testdata.txt");
> if(!is.good()) { std::cerr << "Couldn't open testdata.txt\n"; return 1; }
hehe, pretty cool idiom for testing a file... is.good() :)
Post a reply to this message
|
|
| |
| |
|
|
|
|
| |
| |
|
|
nemesis wrote:
> hehe, pretty cool idiom for testing a file... is.good() :)
double value; /* or your money back! */
short changed; /* so triple your money back! */
-- Larry Wall in cons.c from the perl source code
Larry Wall does things like this on purpose all the time. (I have no
idea if Warp did that on purpose--it wouldn't surprise me.)
--
William Tracy
afi### [at] gmailcom -- wtr### [at] calpolyedu
You know you've been raytracing too long when you post an idea to a news
group about starting an Internet movie project using PovRay.
-- Ken Tyler
Post a reply to this message
|
|
| |
| |
|
|
|
|
| |
| |
|
|
William Tracy <wtr### [at] calpolyedu> wrote:
> Larry Wall does things like this on purpose all the time. (I have no
> idea if Warp did that on purpose--it wouldn't surprise me.)
No, it was not on purpose. I think of it as "if the 'input stream'
is not good, then". Never thought about 'is' as a verb... :P
--
- Warp
Post a reply to this message
|
|
| |
| |
|
|
|
|
| |
| |
|
|
Warp wrote:
> William Tracy <wtr### [at] calpolyedu> wrote:
>> Larry Wall does things like this on purpose all the time. (I have no
>> idea if Warp did that on purpose--it wouldn't surprise me.)
>
> No, it was not on purpose. I think of it as "if the 'input stream'
> is not good, then". Never thought about 'is' as a verb... :P
>
ah! the double meaning acronym... an old geek tradition... :)
Post a reply to this message
|
|
| |
| |
|
|
|
|
| |
| |
|
|
William Tracy wrote:
> nemesis wrote:
>> hehe, pretty cool idiom for testing a file... is.good() :)
>
> double value; /* or your money back! */
> short changed; /* so triple your money back! */
> -- Larry Wall in cons.c from the perl source code
>
> Larry Wall does things like this on purpose all the time. (I have no
> idea if Warp did that on purpose--it wouldn't surprise me.)
yes, Larry is a lovely fellow. I like his LotR remarks. :)
I only wish he could get Perl6 out!
Post a reply to this message
|
|
| |
| |
|
|
|
|
| |
|
|