POV-Ray : Newsgroups : povray.programming : DF3 utility Server Time
21 Dec 2024 20:34:42 EST (-0500)
  DF3 utility (Message 1 to 8 of 8)  
From: Woody
Subject: DF3 utility
Date: 25 Jan 2008 08:35:01
Message: <web.4799e4c49ec55a3e94e61a50@news.povray.org>
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

From: Warp
Subject: Re: DF3 utility
Date: 25 Jan 2008 10:24:58
Message: <4799ff49@news.povray.org>
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

From: Woody
Subject: Re: DF3 utility
Date: 27 Jan 2008 09:15:00
Message: <web.479c911029e70c2394e61a50@news.povray.org>
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

From: nemesis
Subject: Re: DF3 utility
Date: 30 Jan 2008 19:27:13
Message: <47a115e1$1@news.povray.org>
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

From: William Tracy
Subject: Re: DF3 utility
Date: 31 Jan 2008 02:42:45
Message: <47a17bf5$1@news.povray.org>
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

From: Warp
Subject: Re: DF3 utility
Date: 31 Jan 2008 03:40:21
Message: <47a18975@news.povray.org>
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

From: nemesis
Subject: Re: DF3 utility
Date: 31 Jan 2008 13:52:51
Message: <47a21903@news.povray.org>
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

From: nemesis
Subject: Re: DF3 utility
Date: 1 Feb 2008 11:23:19
Message: <47a34777@news.povray.org>
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

Copyright 2003-2023 Persistence of Vision Raytracer Pty. Ltd.