POV-Ray : Newsgroups : povray.general : comma separated value file for data input Server Time
5 Nov 2024 13:26:36 EST (-0500)
  comma separated value file for data input (Message 1 to 10 of 11)  
Goto Latest 10 Messages Next 1 Messages >>>
From: Eric
Subject: comma separated value file for data input
Date: 10 Feb 2007 02:00:05
Message: <web.45cd67c8a7d38e577fa0baee0@news.povray.org>
Although far from new to POV-Ray (programmed first rendering in about 1996),
I am an infrequent user and thus a novice.  Right now I'm returning to it to
produce some 3D statistical graphs.  It would be really convenient for me
read data in from some sort of two-dimensional data file (a table).  Comma
separated value files (.CSV) are commonly used for this.  Is there any
convenient way to read a CSV file into some sort of data container (an
array of arrays? or can MATRIX be used other than for transforms?)

Thanks!
-Eric


Post a reply to this message

From: Charles C
Subject: Re: comma separated value file for data input
Date: 10 Feb 2007 03:25:00
Message: <web.45cd80d8d81812bf7d5894630@news.povray.org>
"Eric" <[firstname]@theothersideofthenet.com> wrote:
> Although far from new to POV-Ray (programmed first rendering in about 1996),
> I am an infrequent user and thus a novice.  Right now I'm returning to it to
> produce some 3D statistical graphs.  It would be really convenient for me
> read data in from some sort of two-dimensional data file (a table).  Comma
> separated value files (.CSV) are commonly used for this.  Is there any
> convenient way to read a CSV file into some sort of data container (an
> array of arrays? or can MATRIX be used other than for transforms?)

Yes, and it's pretty much straight-away what you said, minus the arrays of
arrays.  I don't think you can really do arrays of arrays per se where
lengths or types may vary etc, but you can certainly do multi-dimensional
arrays up to 5D.  Here's a 1D example:

#fopen StatsFile "Stats.csv" read
#local Ctr = 0;
#local Limit = 150; //assuming you know how long it is
#declare StatsArray = array[Limit];
#while(Ctr < Limit)
  #read(StatsFile, StatsArray[Ctr])
  #local Ctr = Ctr + 1;
#end //end while

Or something like that....  I didn't test it for mistakes/typos.  Hope it
helps.
Charles


Post a reply to this message

From: Tim Attwood
Subject: Re: comma separated value file for data input
Date: 10 Feb 2007 04:25:42
Message: <45cd8f96$1@news.povray.org>
> Although far from new to POV-Ray (programmed first rendering in about 
> 1996),
> I am an infrequent user and thus a novice.  Right now I'm returning to it 
> to
> produce some 3D statistical graphs.  It would be really convenient for me
> read data in from some sort of two-dimensional data file (a table).  Comma
> separated value files (.CSV) are commonly used for this.

Pov can #read values from a file delimited by commas, these values
should be valid numbers, vectors, or strings. Strings should be inside
quotes. Vectors should be pov style <#,#,#>. Trailing commas are
expected. Files are opened with #fopen. Reading past the end of a
file automatically closes it, but #fclose them...
(3.2.2.3  File I/O Directives)

>... Is there any convenient way to read a CSV file into some sort of
> data container (an array of arrays? ...

Pov supports multi-dimensional arrays. (3.2.1.8  Array Identifiers)
Also, there are dot operators for vectors in pov, so you might
want to store your point data in an array of vectors?
(3.2.1.4.3  Operators)

> or can MATRIX be used other than for transforms?)

In pov "matrix" is a keyword for matrix transforms.

Here's a simple example that reads a list of 10 vectors
(as floats) from a .csv file and makes a graph.

union{
#declare point = array[10];
#fopen handle "data.csv" read
#declare c = 0;
#while (c < 10)
   #read (handle, t1, t2, t3)
   #declare point[c] = <t1, t2, t3>;
   sphere{point[c],0.02}
   #declare c = c + 1;
#end
#fclose handle
#declare c = 1;
#while (c < 10)
   cylinder{point[c-1],point[c],0.02}
   #declare c = c + 1;
#end
pigment{rgb <1,0,0>}
}

//data.csv
0.0,0.0,0,
0.1,0.01,0,
0.2,0.1,0,
0.3,0.2,0,
0.4,0.25,0,
0.5,0.25,0,
0.6,0.2,0,
0.7,0.1,0,
0.8,0.01,0,
0.9,0.0,0


Post a reply to this message

From: Grassblade
Subject: Re: comma separated value file for data input
Date: 10 Feb 2007 07:10:00
Message: <web.45cdb4f0d81812bfd2eafd5e0@news.povray.org>
"Eric" <[firstname]@theothersideofthenet.com> wrote:
> Although far from new to POV-Ray (programmed first rendering in about 1996),
> I am an infrequent user and thus a novice.  Right now I'm returning to it to
> produce some 3D statistical graphs.  It would be really convenient for me
> read data in from some sort of two-dimensional data file (a table).  Comma
> separated value files (.CSV) are commonly used for this.  Is there any
> convenient way to read a CSV file into some sort of data container (an
> array of arrays? or can MATRIX be used other than for transforms?)
>
> Thanks!
> -Eric
Well, it so happens that I've been working on a matrices.inc for statistical
analysis. It can do most of the elementary operations, plus invert any
matrix (compatibly with the, unknwon, maximum number of elements in an
array, and computer memory). It's still beta-ish, but I can upload it if
you are interested.


Post a reply to this message

From: Grassblade
Subject: Re: comma separated value file for data input
Date: 10 Feb 2007 07:15:00
Message: <web.45cdb6a8d81812bfd2eafd5e0@news.povray.org>
"Tim Attwood" <tim### [at] comcastnet> wrote:

> Pov supports multi-dimensional arrays. (3.2.1.8  Array Identifiers)
> Also, there are dot operators for vectors in pov, so you might
> want to store your point data in an array of vectors?
> (3.2.1.4.3  Operators)
Right, but vectors in POV are at most 5-dimensional. That can be a little
constraining for statistics.
>
> In pov "matrix" is a keyword for matrix transforms.
Likewise, matrix is at most 5*5 (well, 5*4 counting the normalized column).
>
> Here's a simple example that reads a list of 10 vectors
> (as floats) from a .csv file and makes a graph.
>
> union{
> #declare point = array[10];
> #fopen handle "data.csv" read
> #declare c = 0;
> #while (c < 10)
>    #read (handle, t1, t2, t3)
>    #declare point[c] = <t1, t2, t3>;
>    sphere{point[c],0.02}
>    #declare c = c + 1;
> #end
> #fclose handle
> #declare c = 1;
> #while (c < 10)
>    cylinder{point[c-1],point[c],0.02}
>    #declare c = c + 1;
> #end
> pigment{rgb <1,0,0>}
> }
>
> //data.csv
> 0.0,0.0,0,
> 0.1,0.01,0,
> 0.2,0.1,0,
> 0.3,0.2,0,
> 0.4,0.25,0,
> 0.5,0.25,0,
> 0.6,0.2,0,
> 0.7,0.1,0,
> 0.8,0.01,0,
> 0.9,0.0,0

Nifty! But it seems you have to know beforehand the number of data to be
read, is there some way to have something similar to dimension_size?
And can I use your code in my matrices.inc?


Post a reply to this message

From: Warp
Subject: Re: comma separated value file for data input
Date: 10 Feb 2007 09:09:42
Message: <45cdd225@news.povray.org>
Grassblade <nomail@nomail> wrote:
> Nifty! But it seems you have to know beforehand the number of data to be
> read, is there some way to have something similar to dimension_size?

  It's naturally impossible to know the amount of numbers in the input
file before the file has been parsed.
  There are two possible solutions:

  If you can make the program which creates the data file to store the
amount of values as the first value, that would be the easiest way of
automatically getting this value in the pov file.

  If that's just not possible, then you can simulate dynamic arrays in
povray by first creating an array of certain size, reading data into it,
and if it fills up, create a bigger array, copy the data from the old one
to this new one, and then continue reading values, and so on. In the end,
if an exact array (without unused values at the end) is needed, you can
create an array of that exact size and copy the data there.

-- 
                                                          - Warp


Post a reply to this message

From: Charles C
Subject: Re: comma separated value file for data input
Date: 10 Feb 2007 12:05:00
Message: <web.45cdfa7ad81812bf9356f54f0@news.povray.org>
Warp <war### [at] tagpovrayorg> wrote:
>   It's naturally impossible to know the amount of numbers in the input
> file before the file has been parsed.
>   There are two possible solutions:
>
>   If you can make the program which creates the data file to store the
> amount of values as the first value, that would be the easiest way of
> automatically getting this value in the pov file.
>
>   If that's just not possible, then you can simulate dynamic arrays in
> povray by first creating an array of certain size, reading data into it,
> and if it fills up, create a bigger array, copy the data from the old one
> to this new one, and then continue reading values, and so on. In the end,
> if an exact array (without unused values at the end) is needed, you can
> create an array of that exact size and copy the data there.

In my original reply I went with the assumption that knowing the amount of
data he'd want to display was something he could do manually.  But the docs
for the "#read" keyword reveal kindof a nifty way to do it.  Just add a(n
initialized) counter where the "..." is below, and then re-open the file
and count/read to that value.

Charles

From the docs:
  #fopen MyFile "mydata.txt" read
  #while (defined(MyFile))
    #read (MyFile,Var1,Var2,Var3)
    ...
  #end


Post a reply to this message

From: Mark Birch
Subject: Re: comma separated value file for data input
Date: 10 Feb 2007 15:20:00
Message: <web.45ce27e5d81812bfdbc5fd0e0@news.povray.org>
"Eric" <[firstname]@theothersideofthenet.com> wrote:
> Although far from new to POV-Ray (programmed first rendering in about 1996),
> I am an infrequent user and thus a novice.  Right now I'm returning to it to
> produce some 3D statistical graphs.  It would be really convenient for me
> read data in from some sort of two-dimensional data file (a table).  Comma
> separated value files (.CSV) are commonly used for this.  Is there any
> convenient way to read a CSV file into some sort of data container (an
> array of arrays? or can MATRIX be used other than for transforms?)
>
> Thanks!
> -Eric

Reading the data in one value at a time is unnecessary.
If you can format the text to read like a proper array declaration, then you
can simply #include that file:

// data file, saved as "Myarray.inc"
#declare Myarray = array[5][5]
{
  (1, 2, 3, 4, 5},
  {5, 4, 3, 2, 1},
  {6, 7, 8, 9, 0},
  {7, 4, 6, 3, 4},
  {9, 4, 2, 5, 7}
}
// -----------------------------


// Main POV file
#include "Myarray.inc"


Post a reply to this message

From: Warp
Subject: Re: comma separated value file for data input
Date: 10 Feb 2007 16:08:10
Message: <45ce343a@news.povray.org>
Charles C <nomail@nomail> wrote:
> In my original reply I went with the assumption that knowing the amount of
> data he'd want to display was something he could do manually.  But the docs
> for the "#read" keyword reveal kindof a nifty way to do it.  Just add a(n
> initialized) counter where the "..." is below, and then re-open the file
> and count/read to that value.

  The problem is not reading the data until it ends. The problem is
how to create an array which can be used to store those values.

-- 
                                                          - Warp


Post a reply to this message

From: Charles C
Subject: Re: comma separated value file for data input
Date: 10 Feb 2007 18:05:00
Message: <web.45ce4e5cd81812bfe94cc5130@news.povray.org>
Warp <war### [at] tagpovrayorg> wrote:
> Charles C <nomail@nomail> wrote:
> > In my original reply I went with the assumption that knowing the amount of
> > data he'd want to display was something he could do manually.  But the docs
> > for the "#read" keyword reveal kindof a nifty way to do it.  Just add a(n
> > initialized) counter where the "..." is below, and then re-open the file
> > and count/read to that value.
>
>   The problem is not reading the data until it ends. The problem is
> how to create an array which can be used to store those values.

I thought I answered that.... Here's an automatically created exact-fit
array for 1D (pending mistakes as always).  Hopefully if more than 1D is
needed, the data would be cleanly & predictably divisable quantities of
CSV's, but if not, I guess it's up to Eric to know how the CSV's are
formatted and what assumptions can be made.

#local LengthOfList = 0;
#fopen MyFile "mydata.txt" read
#while (defined(MyFile))
  #read (MyFile,Var1,Var2,Var3)
  #local LengthOfList = LengthOfList + 1;
#end
//The file is now closed and undefined but we know how long it is.

#declare MyArray = array[LengthOfList];

#fopen MyFile "mydata.txt" read
#local Ctr = 0;
#while (Ctr < LengthOfList)
  #read (MyFile, MyArray[Ctr])
  #local Ctr = Ctr + 1;
#end

Charles


Post a reply to this message

Goto Latest 10 Messages Next 1 Messages >>>

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